Method and device for compiling program code

The method and device for compiling program code into a mix of machine and bytecode addresses the performance-storage trade-off by optimizing frequently and less frequently executed code, enhancing startup performance and reducing storage consumption.

WO2026005636A1PCT designated stage Publication Date: 2026-01-02HUAWEI TECH CO LTD +1

Patent Information

Application Number
PCT/RU2024/000201
Authority / Receiving Office
WO · WO
Patent Type
Applications
Current Assignee / Owner
Filing Date
2024-06-27
Publication Date
2026-01-02

AI Technical Summary

Technical Problem

Existing programming languages face challenges in achieving a balance between performance and storage resource utilization, with static compilation consuming excessive storage and VM-based execution requiring long ramp-up cycles for acceptable performance.

Method used

A method and device for compiling program code that combines machine code and bytecode, where frequently executed code is compiled into machine code and less frequently executed code into bytecode, utilizing hook functions to dynamically obtain bytecode addresses at runtime, thereby optimizing performance and reducing storage consumption.

Benefits of technology

This approach ensures good performance from program startup while minimizing storage resource usage by leveraging machine code for quick execution and bytecode for compactness, improving overall efficiency and reducing storage requirements.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure RU2024000201_02012026_PF_FP_ABST
    Figure RU2024000201_02012026_PF_FP_ABST
Patent Text Reader

Abstract

Embodiments of the present application provide a method and device for compiling program code. The method for compiling program code may include: obtaining source code; and performing a compilation operation to obtain a first compilation result of the source code, where a first code segment in the source code is compiled into machine code in the first compilation result, a second code segment in the source code is compiled into bytecode in the first compilation result, and the first compilation result is configured to be deployed to a target environment. The proposed technical solutions may be conducive to ensuring performance at low storage resource utilization.
Need to check novelty before this filing date? Find Prior Art

Description

METHOD AND DEVICE FOR COMPILING PROGRAM CODETECHNICAL FIELD(0001] Embodiments of the present application relate to the field of computer technologies, and more specifically, to a method and device for compiling program code.BACKGROUND[00021 The implementations of programming languages can be done through static compilation or virtual machine (VM) execution. For static compilation, source code is compiled into native code that runs directly on hardware. For virtual machine execution, source code is compiled into bytecode with abstract stack or abstract register arguments, and then a VM that includes an interpreter and dynamic compiler may be used to execute the bytecode. Static compilation is known for its good steady performance from application startup. However, native code may consume a lot of storage resources and static compilation may need a long compilation time. The implementation of a programming language based on VM execution utilizes interpretation of compact bytecode and selective dynamic compilation of frequently executed code to improve performance. However, VM-based implementation requires a long ramp-up cycle to achieve an acceptable performance level, which leads to programs starting with poor performance, and for some short running processes, acceptable performance may never be achieved.SUMMARY

[0003] Embodiments of the present application provide a method and device for compiling program code. The proposed technical solutions may be conducive to ensuring performance at low storage resource utilization.|0004| According to a first aspect, an embodiment of the present application provides a method for compiling program code, including: obtaining source code; and performing a compilation operation to obtain a first compilation result of the source code, where a first code segment in the source code is compiled into machine code in the first compilation result, a second code segment in the source code is compiled into bytecode in the first compilation result, and the first compilation result is configured to be deployed to a target environment.,0005] According to the aforementioned technical solution, the compilation result of the source code includes machine code and bytecode, which is beneficial for achieving a better trade-off between performance and storage resource utilization,that is, it is conducive to ensuring performance at low storage resource utilization. Specifically, the machine code is conducive to ensuring good performance from the program startup, while the compactness of bytecode helps reduce the size of compilation result, thereby reducing storage resource consumption.|0006] The source code may be regarded as source code of a program or an application (APP).(0007] Optionally, the first code segment and / or the second segment may be determined by a user.|0008| In a possible implementation of the first aspect, the first code segment belongs to hot code, and / or the second code segment belongs to cold code.,0009] According to the aforementioned technical solution, compiling repeatedly executed code (such as hot code) into machine code can improve the running efficiency of these code, which is beneficial for further improving the running speed of the program and thus improving performance. Code with lower execution frequency (such as cold code) may be compiled into bytecode, which helps reduce the storage resource consumption without affecting performance.

[0010] In a possible implementation of the first aspect, the method further includes: performing a compilation operation to obtain a second compilation result of the source code; and pre-executing the second compilation result to obtain one or more profile files, where the one or more profile files are configured to indicate the hot code and / or the cold code.(0011] In a possible implementation of the first aspect, a first call from a first method in the source code to a second method in the source code is generated as a second call from the first method to a first hook method in the first compilation result, the first call is a direct call, the first method belongs to the first code segment, the second method belongs to the second code segment, and the first hook method is configured to obtain an address of bytecode of the second method at runtime.(0012) According to the aforementioned technical solution, the address of the first hook method can be determined at compile-time. The direct call from the machine code method (such as the first method in machine code form) to the bytecode method (such as the second method in the bytecode form) is generated as the call from the machine code method to the first hook function. The first hook method may be used to obtain the address of the bytecode method at runtime, which is equivalent to implementing the direct call from the machine code method to the bytecode method in the first compilation result. In addition, the first hook method may be a universal run-time function, which is beneficial for saving storage resource usage.

[0013] In a possible implementation of the first aspect, the second call is achieved through a first pointer pointing to the first hook method, the first pointer is related to ID of the second method, and the first hook method is configured to obtain the address of the bytecode of the second method according to the ID of the second method at runtime.(0014] In a possible implementation of the first aspect, the first hook method is configured to obtain the address of the bytecode of the second method according to a mapping relationship between addresses of multiple bytecode methods and IDs of the multiple bytecode methods, as well as the ID of the second method.

[0015] In a possible implementation of the first aspect, the first hook method is configured to patch the first pointer with an address of machine code of the second method, and the machine code of the second method is obtained through dynamic compilation at runtime.,0016] According to the aforementioned technical solution, the first hook function may be used to patch the address of the first call, so that the next call may bypass the first hook function and go directly to the machine code of the second method. This helps reduce call overheads and improve the execution speed when the second method has been compiled into machine code at runtime. Moreover, patching the address of second method through the first hook function makes it easier and more efficient to patch the address.

[0017] In a possible implementation of the first aspect, a third call from a third method in the source code to a fourth method in the source code is generated as a fourth call from the third method to a second hook method in the first compilation result, the third call is a virtual call, the third method belongs to the first code segment, the fourth method belongs to the second code segment, and the second hook method is configured to obtain an address of bytecode of the fourth method at runtime.

[0018] According to the aforementioned technical solution, the address of the second hook method can be determined at compile-time. The virtual call from the machine code method (such as the third method in machine code form) to the bytecode method (such as the fourth method in the bytecode form) is generated as the call from the machine code method to the second hook method. The second hook method may be used to obtain the address of the bytecode method at runtime, which is equivalent to implementing the virtual call from the machine code method to the bytecode method in the first compilation result. In addition, the second hook method may be a universal run-time function, which is beneficial for saving storage resource usage.

[0019] According to a second aspect, an embodiment of the present application provides a device for compiling program code, including: an obtaining unit, configured to obtain source code; and a first compilation unit, configured to perform a compilation operation to obtain a first compilation result of the source code, where a first code segment in the source code is compiled into machine code in the first compilation result, a second code segment in the source code is compiled into bytecode in the first compilation result, and the first compilation result is configured to be deployed to a target environment.,0020] According to the aforementioned technical solution, the compilation result of the source code includes machine code and bytecode, which is beneficial for achieving a better trade-off between performance and storage resource utilization, that is, it is conducive to ensuring performance at low storage resource utilization. Specifically, the machine code is conducive to ensuring good performance from the program startup, while the compactness of bytecode helps reduce the size of compilation result, thereby reducing storage resource consumption.,0021] In a possible implementation of the second aspect, the first code segment belongs to hot code, and / or the secondcode segment belongs to cold code.[0022[ In a possible implementation of the second aspect, the device further includes: a second compilation unit, configured to perform a compilation operation to obtain a second compilation result of the source code; and an execution unit, configured to pre-execute the second compilation result to obtain one or more profile files, where the one or more profile files are configured to indicate the hot code and / or the cold code.|0023 ] In a possible implementation of the second aspect, a first call from a first method in the source code to a second method in the source code is generated as a second call from the first method to a first hook method in the first compilation result, the first call is a direct call, the first method belongs to the first code segment, the second method belongs to the second code segment, and the first hook method is configured to obtain an address of bytecode of the second method at runtime.(0024] In a possible implementation of the second aspect, the second call is achieved through a first pointer pointing to the first hook method, the first pointer is related to ID of the second method, and the first hook method is configured to obtain the address of the bytecode of the second method according to the ID of the second method at runtime.[0025| In a possible implementation of the second aspect, the first hook method is configured to obtain the address of the bytecode of the second method according to a mapping relationship between addresses of multiple bytecode methods and IDs of the multiple bytecode methods, as well as the ID of the second method.

[0026] In a possible implementation of the second aspect, the first hook method is configured to patch the first pointer with an address of machine code of the second method, and the machine code of the second method is obtained through dynamic compilation at runtime.(0027[ In a possible implementation of the second aspect, athird call from a third method in the source code to a fourth method in the source code is generated as a fourth call from the third method to a second hook method in the first compilation result, the third call is a virtual call, the third method belongs to the first code segment, the fourth method belongs to the second code segment, and the second hook method is configured to obtain an address of bytecode of the fourth method at runtime.[0028) According to a third aspect, an embodiment of the present application provides a computer readable storage medium including instructions. When the instructions run on an electronic device, the electronic device is enabled to perform the method in the first aspect or any possible implementation of the first aspect.

[0029] According to a fourth aspect, an embodiment of the present application provides an electronic device, including a processor and a memory. The processor is connected to the memory. The memory is configured to store instructions, and the processor is configured to execute the instructions. When the processor executes the instructions stored in the memory, the processor is enabled to perform the method in the first aspect or any possible implementation of the first aspect.

[0030] According to a fifth aspect, an embodiment of the present application provides a chip system, where the chipsystem includes a memory and a processor, the memory is configured to store a computer program, and the processor is configured to invoke the computer program from the memory and run the computer program, so that an electronic device on which the chip system is disposed performs the method in the first aspect or any possible implementation of the first aspect.

[0031] According to a sixth aspect, an embodiment of the present application provides a computer program product, where when the computer program product runs on an electronic device, the electronic device is enabled to perform the method in the first aspect or any possible implementation of the first aspect.DESCRIPTION OF DRAWINGS

[0032] FIG. 1 is a schematic flowchart of a method for compiling program code according to some embodiments of the present application.

[0033] FIG. 2 illustrates an example of implementations of calls between methods with different types of compiled execution binary format at runtime according to some embodiments of the present application.

[0034] FIG. 3 is a schematic flowchart of a method for executing program code according to some embodiments of the present application.

[0035] FIG. 4 is a schematic block diagrams of a device for compiling program code according to some embodiments of the present application.,0036] FIG. 5 is a schematic block diagrams of an electronic device according to some embodiments of the present application.DESCRIPTION OF EMBODIMENTS

[0037] The following describes the technical solutions in the present application with reference to the accompanying drawings.

[0038] In order to facilitate understanding of the embodiments of the present application, terms that may be involved in the embodiments of the present application are described below.

[0039] (1) Programming language;

[0040] A programming language is a system of notation for writing computer programs.(0041] Programming languages are described in terms of their syntax (form) and semantics (meaning), usually defined by formal languages. Most languages have a type system including different data types (such as integers and strings) and catch type errors where one type is given where another is expected. Many languages support user-defined types including abstractdata types, often used for object-oriented programming. Implementation in the form of a compiler or interpreter allows programs to be translated into machine code and executed.

[0042] (2) Compiler;|0043] In computer science, a compiler is a computer program that translates computer code written in one programming language (source language) into another programming language (target language). The term "compiler" is primarily used for programs that translate source code from a high-level programming language to a low-level programming language, such as assembly language, object code, or machine code, to create an executable program.,0044] (3) Machine code;(0045] In computer programming, machine code is computer code including machine language instructions, which are used to control a computer's central processing unit (CPU). Although decimal computers were once common, the contemporary marketplace is dominated by binary computers. For these computers, machine code is "a binary representation of a computer program that is actually read and interpreted by the computer". A program in machine code includes a sequence of machine instructions (possibly interspersed with data).(0046] Machine code is generally different from bytecode, which is either executed by an interpreter or compiled into machine code for faster (direct) execution. One exception is that processors are designed to use particular bytecode directly as their machine code, such as Java processors.

[0047] When it comes to platform-dependent parts of language features or libraries, machine code and assembly code are sometimes called native code.10048] (4) Bytecode;

[0049] Bytecode (also called portable code or p-code) is a form of instruction set designed for efficient execution by a software interpreter. Unlike human-readable source code, bytecode is compact numeric code, constants, and references (normally numeric addresses) that encode the result of compiler parsing and performing semantic analysis of things like type, scope, and nesting depths of program objects.(0050] The name "bytecode" stems from instruction sets that have one-byte opcodes followed by optional parameters. Intermediate representations such as bytecode may be output by programming language implementations for ease interpretation, or dependence on hardware and operating systems may be reduced by allowing the same code to run cross-platform, on different devices. Bytecode may often be either directly executed on a virtual machine (p-code machine, i.e., interpreter), or it may be further compiled into machine code for better performance.[00511 Since bytecode instructions are processed by software, they may be arbitrarily complex, but are nonetheless often akin to traditional hardware instructions: virtual stack machines are the most common (Java, python), but virtual registermachines have also been built (Android). Different parts may often be stored in separate files, similar to object modules, but dynamically loaded during execution.

[0052] (5) Interpreter;(0053] In computer science, an interpreter is a computer program that directly executes instructions written in a programming or scripting language, without requiring them previously to have been compiled into a machine language program. An interpreter generally uses one of the following strategies for program execution:

[0054] Parse the source code and perform its behavior directly;

[0055] Translate the source code into some efficient intermediate representations or object code and execute it immediately;

[0056] Explicitly execute the stored precompiled bytecode made by a compiler and matched with the interpreter's virtual machine (VM).,0057] (6) Java virtual machine (JVM);(0058] A Java virtual machine is a virtual machine that enables a computer to run Java programs as well as programs written in other languages which are also compiled into Java bytecode. The JVM is detailed by a specification that formally describes what is required in a JVM implementation. Having a specification ensures interoperability of Java programs across different implementations so that program authors using the Java development kit (JDK) do not have to worry about idiosyncrasies of the underlying hardware platform.|0059] The JVM reference implementation is developed by the OpenJDK project as open source code and includes a just-in-time (JIT) compiler called HotSpot. Eclipse OpenJ9 is another open source JVM for OpenJDK.,0060] Implementations of some programming languages (e.g. C, C++, Go, and Rust) use a traditional approach based on static compilation, such as ahead-of-time (AOT) compilation. The source code is compiled into native code executable files that run directly on the hardware. Implementations of some programming languages (e.g. JS, Java, and Scala) utilize a language VM: the source code is first compiled into bytecode instructions with abstract stack or abstract register arguments (Java bytecode, Android DEX). Bytecode is then executed with a VM including an interpreter and a dynamic compiler, such as a JIT compiler.

[0061] Some modem programming languages, such as C# and Kotlin, have both compilers and execution mode implementations. For example, these programming languages can be implemented using AOT compilation or VM execution.,0062] There is only one mode that can be chosen for implementation of these programming languages, and two modes cannot be mixed together in one single pipeline. That is, the entire project can be compiled in AOT compilation mode, or the entire project can be compiled into bytecode and executed through a VM, but the AOT-compiled native code and bytecodecannot cooperate with each other.[0063| Static compilation is known for its stable performance from the very beginning of the application. However, the size of native code is very large and static compilation may take a long compilation time. Nowadays, as software size increases, the problem of static size becomes more prominent. Cloud software tends to use frameworks and components that may encounter thousands of lines of code and continue to grow. The static compilation of codebases will take a lot of time (usually incompatible with continuous integration during development) and result in huge executable files occupying extra disk space and memory. Embedded / mobile software also tends to grow in size despite the fact that devices are inherently resource- constrained. This makes static compilation impractical for the implementation of large software.

[0064] VM-based implementations of programming languages utilize interpretation of quite a compact bytecode with selective dynamic compilation of frequently executed code to improve performance. Interpreting and executing bytecode through an interpreter can achieve low startup time, but dynamic compilation may require a long ramp-up cycle to reach an acceptable performance level, which leads to programs starting with low performance and requiring some time to improve performance. Furthermore, acceptable performance may be never reached for short-running processes.

[0065] AOT-compiled native code may be in the form of executable and linking format (ELF) and the bytecode may be in the form of class file format. These two formats may negotiate through a Java native interface (JNI). However, the JNI approach imposes heavy overhead on JVM environment synchronization and object copying, which is somewhat inefficient.|0066] In view of this, the present application provides a method for compiling program code and a method for executing program code, which may be conducive to achieving a balance between performance and resource utilization.(0067] The embodiments provided by the present application may be applied to multiple programming languages, such as, C#, Kotlin, Java, python, or other programming languages that can be implemented with a VM. For example, when the present application is applied to Java, the source code is written in Java. When the present application is applied to Kotlin, the source code is written in Kotlin.|0068] For convenience, in the following embodiments, it is assumed that the source code is written in Java.] 0069] Reference numerals used for method steps may be used to identify the respective steps, but do not limit an order of the steps.,0070] FIG. 1 illustrates a method for compiling program code provided by some embodiments of the present application. Method 100 may be performed by a compiler.

[0071] As shown in FIG. 1, method 100 may include the following steps.[0072) 110, obtain source code.

[0073] 120, perform a complication operation to obtain a first compilation result of the source code. Afirst code segmentin the source code may be compiled into machine code in the first compilation result, and a second code segment code may be compiled into bytecode in the first compilation result.

[0074] The source code in step 110 may be regarded as source code of a program or an application (APP).

[0075] The source code may be written in the same programming language. For example, the source code may be written in any one of C#, Kotlin, Java, or python.(0076] The source code may be obtained in various ways.(0077| For example, step 110 may include: receiving source code from other device(s).]0078] For another example, step 100 may include: receiving source code from a user.(0079] For another example, step 100 may include: generating source code, for example, through Al.

[0080] The "first" in the "first code segment" and the "second" in the "second code segment" are only used to distinguish code segment(s) to be compiled into machine code and code segments) to be compiled into bytecode, and do not have any other limiting effects. The first code segment may be considered as a type of code segment There may be one or more code segments in the first code segment. The second code segment may be considered as a type of code segment. There may be one or more code segments in the second code segment.

[0081] In method 100, compilation is carried out in units of code segments in the source code. The compiled formats of different code segments may be the same or different.(0082 ] As an example, a compilation unit may be a function. The first code segment may include one or more functions. The second code segment may include one or more functions.

[0083] The function in the source code may also be replaced with a method in the source code.|0084] The first compilation result is in a mixed binary format including both machine code and bytecode. In other words, a portion of the source code is compiled into machine code, while the rest of the source code is compiled into bytecode. |0085] The machine code may also be referred to as native code. The bytecode may also be referred to as interpreted code.

[0086] The "first" in the first compilation result is only used to indicate that the compilation result is in a mixed binary format, and does not have any other limiting effect.10087 ] The first compilation result may also be referred to as executable file(s) including both native code and bytecode that are bundled with interpreter and / or JIT. When method 100 is applied to compile the source code of a program or the source code of an application, one or more executable files may be generated. The embodiments of the present application do not limit the number of executable files.(0088] The first compilation result may be used to be deployed to a target environment. For example, the targetenvironment may be a user equipment, a sever, a cloud platform or other runtime environments. An App is taken as an example. Method 100 may be applied to compile the source code of the APP into a mixed binary format, and then the first compilation result may be deployed. By executing the first compilation result, the APP can be executed.

[0089] In method 100, any function in the source code may be compiled into machine code or bytecode. In other words, any function in the source code may exist in either a machine code format or a bytecode format when deployed.

[0090] The code segment in the source code may be compiled into machine code through AOT compilation. In this case, the machine code may also be called AOT-compiled machine code. The embodiments of the present application mainly take the machine code in the first compilation result as AOT-compiled machine code as an example for explanation.

[0091] According to the technical solutions in the embodiments of the present application, the compilation result of the source code includes machine code and bytecode, which is beneficial for achieving a better trade-off between performance and storage resource utilization, that is, it is conducive to ensuring performance at low storage resource utilization. Specifically, the machine code is conducive to ensuring good performance from the program startup, while the compactness of bytecode helps reduce the size of compilation result, thereby reducing storage resource consumption, such as reducing the use of RAM and / or ROM.

[0092] Whether a code segment is compiled into machine code or bytecode may be set in various ways.

[0093] In some embodiments, whether a code segment is compiled into machine code or bytecode may be determined based on the execution frequency of the code segment. For example, the code segment with high execution frequency may be compiled into machine code, while the code segment with low execution frequency may be compiled into bytecode.[00941 Optionally, the first code segment may belong to hot code or the second code segment may belong to cold code.

[0095] The hot code in the source code may be compiled into machine code.|0096] The hot code may also be replaced with any one of hot method(s), hotspot function(s), hotspot(s), or hotspot code.(0097) The cold code in the source code may be compiled into bytecode.

[0098] Cold code may also be replaced with code method(s).(0099] Optionally, the first code segment may belong to the hot code and the second code segment may belong to the cold code.

[0100] Hot code refers to code that is executed frequently in the source code, whereas cold code refers to code that is executed less frequently in the source code. The hot code and cold code in the source code may be determined according to profiling. The embodiment of the present application does not limit how to determine the hot code and the cold code.[01011 Optionally, before step 120, method 100 may further include step 130 and step 140 (not shown in FIG. 1).

[0102] 130, perform a complication operation to obtain a second compilation result of the source code.

[0103] 140, pre-execute the second compilation result to obtain one or more profile files. The one or more profile files may be used to indicate the hot code in the source code and / or the cold code in the source code.[0104) In method 100, the source code may be pre-executed to determine the hot code and / or the cold code. Then the compilation may be performed again to obtain the mixed binary format compilation result according to the one or more profile files.

[0105] For example, the second compilation result may be in the form of bytecode. In other words, in step 130, the source code may be compiled into bytecode.

[0106] Step 120 may be performed in various ways.

[0107] Exemplarily, compilation may be performed on the source code to obtain the first compilation result.10108] Alternatively, compilation may be performed on the second compilation result to obtain the first compilation result. For example, some bytecode in the compilation result may be compiled into machine code.

[0109] According to the technical solutions in the embodiments of the present application, compiling repeatedly executed code (such as hot code) into machine code can improve the running efficiency of these code, which is beneficial for further improving the running speed of the program and thus improving performance. Code with lower execution frequency (such as cold code) may be compiled into bytecode, which helps reduce the storage resource consumption without affecting performance.

[0110] In some embodiments, whether a code segment is compiled into machine code or bytecode may be determined by a user.

[0111] In this case, the first code segment and / or the second segment may be determined by the user.

[0112] The above is only an example. Whether the code segment is compiled into bytecode or machine code may also be determined based on other information, such as the storage resource of the target environment. The embodiment of the present application is not limited to this.(0113] The inter-operations between the machine code and the bytecode in the first compilation result may be supported.

[0114] Specifically, transitions between methods in different forms may be supported. Transitions between methods may also be referred to as calls between methods or invocations between methods.[0115| There may be one or more types of calls in the first compilation result. The types of calls may include: a call from a machine code method to a bytecode method, a call from a machine code method to a machine code method, a call from a bytecode method to a machine code method, and a call from a bytecode method to a bytecode method.

[0116] A machine code method refers to a method in the source code that may be compiled into machine code in thefirst compilation result or a method which is kept in machine code at compile-time. The method in the machine code format in the first complication result may be regarded as the machine code method.

[0117] A bytecode method refers to a method in the source code that may be compiled into bytecode in the first compilation result or a method which is kept in bytecode at compile-time. The method in the bytecode format in the first complication result may be regarded as the machine code method.(0118] The call from a machine code method to a bytecode method is taken as an example. Acall from a machine code method to a bytecode method refers to a call whose target is kept in bytecode at compile-time and whose caller is kept in machine code at compile-time. For example, there is a call from method #a to method #b in the source code. The method #a may be compiled into machine code and the method #b may be compiled into bytecode. In this case, the method #a in the machine code format may be regarded as a machine code method. The method #b in the bytecode code format may be regarded as a bytecode method. The call from method #a to method #b in the source code becomes the call from a machine code method to the bytecode method.

[0119] For convenience, the call from a machine code method to a bytecode method may be called a native-to-bytecode call. The call from a machine code method to a machine method may be called a native-to-native call. The call from a bytecode method to a machine method may be called a bytecode-to-native call. The call from a bytecode method to a bytecode method may be called a bytecode-to-bytecode call.

[0120] The following describe examples of implementations for a native-to-bytecode call.[O121[ For AOT compilation, when the caller is compiled into machine code, the target's address at compile-time is required. A bytecode method is executed with an interpreter at runtime and does not has its own unique address at compiletime to be used for a call from a machine code method.

[0122] In some embodiments, a unique interpreter starter is generated for each bytecode method. In this way, the bytecode method may serve as the target of a call from a machine code method.

[0123] However, this approach may waste storage resource.

[0124] The following describe implementations for direct call(s) and indirect call(s), respectively.

[0125] A direct call may refer to a call whose target is determined at compile-time.

[0126] An indirect call may refer to a call whose target needs to be determined at runtime. The indirect call may also be called a virtual call.

[0127] In some embodiments, direct call(s) whose target is kept in bytecode at compile-time and whose caller is kept in machine code at compile-time, may be generated as indirect call(s) in the first compilation result.

[0128] Optionally, a first call from a first method to a second method may be generated as an indirect call from the firstmethod to the second method in the first compilation result. The first call is a direct call. The first method may belong to the first code segment. The second method may belong to the second code segment.[O129| The "first" in the first method is only used to indicate that the method is compiled into machine code, and does not have any other limiting effect. The "second" in the second method is only used to indicate that the method is compiled into bytecode, and does not have any other limiting effect. The "first" in the first call is only used to indicate that the call from the first method to the second method, and does not have any other limiting effect.[0130) The first method is compiled into machine code in the first compilation result. The second method is compiled into bytecode in the first compilation result. In other words, the first method in the machine code format is the machine code method, and the second method in the bytecode format is the bytecode method.[01311 The indirect call from the first method to the second method may be achieved through a second call from the first method to a first hook method in the first complication result, and the first hook method is used to obtain an address of the bytecode of the second method at runtime.

[0132] A hook method may also be referred to as a hook function. For example, the first hook method may also be called the first hook function.[01331 It can also be understood that the first call is generated in the first compilation result as a second call to the first hook function from the first method in the first complication result.[O134[ The "second" in the second call is only used to indicate that the call from the first method to the first hook function, and does not have any other limiting effect.[O13S[ The address of bytecode of a method may also be referred to as the address of a bytecode method.[O136[ The address of machine code of a method may also be referred to as the address of a machine method.[0137[ Direct call(s) from machine code method(s) to bytecode method(s) may be generated as call(s) to the first hook function from the machine code method(s) in the first compilation result. The first hook function may be used to obtain the address(s) of the target(s) of native-to-bytecode direct call(s) at runtime.[O138| The first hook function may be a universal run-time function, which means that the first hook function may be used to obtain the address of the target of any native-to-bytecode direct call at runtime.[ 01391 The first hook function may also be used to receive the target's parameters) passed by a caller on native locations(such as CPU registers and / or stack) according to the target's signature at runtime. The target may be interpreted at runtime.

[0140] The address of the first hook function can be determined at compile-time. The direct call from the machine code method to the bytecode method is generated as the call from the machine code method to the first hook function. The first hook function may obtain the address of the bytecode method at runtime, which is equivalent to implementing the direct call fromthe machine code method to the bytecode method in the first compilation result[01411 In addition, the first hook function may be a universal run-time function, which is beneficial for saving storage resource usage.

[0142] The first hook function may obtain the address of the bytecode method in various ways.

[0143] Further, optionally, the second call from the first method to the first hook function may be achieved through a first pointer which points to the first hook function. The first pointer is related to the identity (ID) of the second method. The first hook function may be used to obtain the address of the bytecode of the second method according to the ID of the second method at runtime.

[0144] The pointer may also be referred to as a function pointer. For example, the first pointer may also be called the first function pointer.[O145[ The ID of the second method may also be referred as the ID of the second method in the bytecode format or the ID of the bytecode of the second method.

[0016] The "first" in the first pointer is used to indicate the pointer corresponding to the second call, and does not have any other limiting effect.

[0147] There is an association between the first pointer and the ID of the second method. The first pointer may be used to determine the identity (ID) of the second method at runtime.

[0148] As an example, the address of the ID of the second method may be related with the address of the first pointer.

[0149] For example, the ID of the second method and the first pointer may be continuously stored. In this case, the distance between the address of the ID of the second method and the address of the first pointer may be determined based on the size of the ID of the second method or the size of the first pointer.

[0150] For another example, the distance between the address of the ID of the second method and the address of the first pointer may be pre-set. The specific implementation may refer to the examples in the following text.

[0151] The association between the first pointer and the ID of the second method may also be defined as other relationships, and the embodiment of the present application is not limited to this.

[0152] Direct calls whose targets are different bytecode methods correspond to different pointers, which may all point to the first hook function. The different pointers may correspond to IDs of different targets. The first hook function may obtain the address of a bytecode method according to the IDs of the bytecode method at runtime.

[0153] The ID of a method may be represented in various forms. For example, the ID of a method may be a name of the method. For another example, the ID of a method may be an atomic number of the method. The embodiment of the present application does not limit the representation form of the ID of the method, as long as different IDs can be used to distinguishdifferent methods.

[0154] Optionally, the first hook function may be used to obtain the address of the bytecode of the second method according to a mapping relationship between addresses of multiple bytecode methods and IDs of the multiple bytecode methods, as well as the ID of the second method at runtime.

[0155] The addresses of the multiple bytecode methods include the address of the bytecode of the second method. The IDs of the multiple bytecode methods include the ID of the second method.

[0156] For example, the mapping relationship the addresses of multiple bytecode methods and the IDs of the multiple bytecode methods may be represented as a mapping table.|O157] The following describes an example of how to implement the above solution. For convenience, one native-to- bytecode direct call is taken as an example for illustration. Other native-to-bytecode direct call(s) may refer to the implementation method of this direct call.(0158] A direct native-to-bytecode call may be generated as an indirect call in the first compilation result through a data structure called deferred access information (DAI).]O159] For direct native-to-bytecode calls, the compiler may create DAI for each call site. DAI corresponding to a call site may include an indirection slot and the ID of the target of the call site. The indirection slot is initialized with a hook function (i.e. the first hook function). In other words, the indirection slot lies a pointer (i.e. the first pointer) to the first hook function. The first hook function may also be called a DAI hook. For convenience, in DAI, the pointer may be regarded as the first field and the ID of the target may be regarded as the second field.

[0160] In this way, all direct native-to-bytecode calls are unified and have indirection slots which can be easily updated.]O161] The direct native-to-bytecode call may be generated as a call to the DAI hook through the DAI in the first compilation result.|O162] At runtime, the DAI hook may obtain the return address of the DAI hook from a native stack frame. Then, the DAI hook may find the DAI corresponding to the call site based on the return address and gets the ID of the target from the DAI.]O1 3] In some embodiments, virtual call(s) whose target is kept in bytecode at compile-time and whose caller is kept in machine code at compile-time, may be generated as call(s) whose target is a second hook method kept in machine code at compile-time and whose caller(s) is the caller(s) of the virtual call(s) in the first compilation result. The second hook method may be used to obtain the address of the target kept in bytecode at runtime.|0164] Optionally, a third call from a third method to a fourth method may be generated as a fourth call from the third method to a second hook method in the first compilation result. The third call is a virtual call. The third method may belong tothe first code segment. The fourth method may belong to the second code segment. The second hook method may be used to obtain an address of the bytecode of the fourth method at runtime.[0165| The "third" in the third method is only used to indicate that the method is compiled into machine code, and does not have any other limiting effect. The "fourth" in the fourth method is only used to indicate that the method is compiled into bytecode, and does not have any other limiting effect. The "third" in the third call is only used to indicate that the call from the third method to the fourth method, and does not have any other limiting effect.

[0166] The third method is compiled into machine code in the first compilation result. The fourth method is compiled into bytecode in the first compilation result. In other words, the third method in the machine code format is the machine code method and the fourth method in the bytecode format is the bytecode method. The third method and the first method may be the same or different The "fourth" in the fourth call is only used to indicate that the call from the third method to the second hook function, and does not have any other limiting effect.|O167] The second hook method may also be called the second hook function.

[0168] The "first" in the first hook function and the "second" in the second hook function are only used for convenience. The first hook function and the second hook function may be the same hook function or different hook functions.

[0169] Virtual call(s) from machine code method(s) to bytecode method(s) may be generated as call(s) to the second hook function from the machine code method(s) in the first compilation result. The second hook function may be used to obtain the address(s) of the target(s) of native-to-bytecode virtual call(s) at runtime.

[0170] The second hook function may be a universal run-time function, which means that the second hook function may be used to obtain the address of the target of any native-to-bytecode virtual call at runtime.

[0171] The second hook function may be a virtual method table (VMT) hook. The address of the VMT hook may be placed in a VMT slot. The function has arguments: type of receiver object (passed as first argument) and offset in VMT (obtained from the call site instruction in native code), and can find ID of a bytecode target.

[0172] The compiler will generate a VMT for each class at compile-time, where each VMT may be used to place the addresses of all virtual methods in that class. Each address may be placed in one VMT slot. However, the address of the bytecode method is unknown at compiler-time.

[0173] For a virtual call whose target is kept in bytecode, the address of the VMT hook function may be placed in the VMT slot corresponding to the target.

[0174] The third call is taken as an example. The fourth method is a virtual method. The fourth method is in the bytecode format in the first compilation result and the address is unknown at compile-time. The address of the VMT hook may be placed in the slot where the address of the fourth method should have been placed.

[0175] The ID of the fourth method may be obtained according to the type of receiver object and otfset corresponding to the fourth method in VMT at runtime. In other words, the type of receiver object may be used to determine which class's VTM the VMT hook belongs to, and the ID of the fourth method may be determined based on the offset, which may be regarded as the position of the VTM hook in the VTM. (0176] The second hook function may be further used to receive the target's parameters) passed by a caller on native locations (such as CPU registers and / or stack) according to the target's signature at runtime. The target may be interpreted at runtime.

[0177] A virtual native-to-bytecode call may be also regarded as being implemented through DAI in the first compilation result. In this way, all virtual native-to-bytecode calls have indirection slots which can be easily updated. (0178] As mentioned before, DAI may include an indirection slot and the ID of the target. For a virtual call, the indirection slot may be occupied by the VMT hook (i.e. the second hook function).

[0179] The DAI may be used to create generate indirect calls for all calls whose targets are bytecode methods or can potentially be bytecode methods (any virtual call falls into this category), which allows all these calls to be unified and have indirection slots which can be easily updated. |0180] The following shows example code of two direct calls and one virtual call.

[0181] The above code is an example of source code. The corresponding compilation result may be as follows.calls "C.foo", "C.zoo" and "C.goo". "C.bar" may be AOT-compiled, that is, "C.bar" may be a machine code method. "C.foo", "C.zoo" and "C.goo" may be bytecode methods. "C.foo", "C.zoo" and "C.goo" do not have static addresses at compile-time. In other words, the addresses of "C.foo", "C.zoo" and "C.goo" are unknown at compile-time.(0184] "C.foo DAI" is the DAI created for the call whose target is "C.foo". "C.zoo DAI" is the DAI created for the call whose target is "C.zoo". The address of "C.foo DAI" and "C.zoo DAI" is known at compile-time. For example, "C.foo DAI" and "C.zoo DAI" may be allocated in data section of executable. The hierarchy of "C.foo DAI" is shown in line 9 to line 12. The first field of "C.foo DAI" (i.e. C.foo DALtarget) lies a pointer to "DAIHook ()". The first field of "C.foo DAI" is the address of "DAIHook ()”. The second field of "C.foo DAI" is the ID of "C.foo". As shown in line 10, the address of the first field is OxblO, and storing the address of "DAIHook ()" requires 64 bits, which is 8 bytes. Therefore, the address of the second field may be OxblO+8, which is 0xbl8.

[0185] The hierarchy of "C.zoo DAI" is shown in line 13 toline 16. The first field of "C.zoo DAI" (i.e. C.zoo DALtarget) lies a pointer to "DAIHook ()". The first field of "C.zoo DAI" is the address of "DAIHook ()". The second field of "C.zoo DAI" is the ID of "C.zoo". As shown in line 14, the address of the first field is OxclO, and storing the address of "DAIHook ()" requires 64 bits, which is 8 bytes. Therefore, the address of the second field may be OxclO+8, which is 0xcl8.(0186] "DAIHook ()" is a hook function kept in the AOT-compiled machine code format. The address of "DAIHook ()" can be determined at compile-time. For example, the address of "DAIHook ()" is 0xb48.|O187] Instead of calling the addresses of "C.foo" and "C.zoo", "DAIHook ()" is called. Specifically, instead of calling the address of "C.foo", "C.foo DALtarget" which points to "DAIHook ()" is called. Instead of calling the address of "C.zoo", "C.zoo DALtarget" which points to "DAIHook ()" is called. The call to "C.foo" is taken as an example. For example, the returnaddress may be 0x7ff..4ca. The DAI hook may determine the DAI corresponding to the call site as the C.foo DAI according to the return address, and obtain the ID from it.

[0188] "VMTHook ()" is a hook function kept in the AOT-compiled machine code format The address of "VMTHook ()" can be determined at compile-time. For example, the address of "VMTHook ()" is 0xb58.|O189] Instead of calling the addresses of "C.goo", "VMTHook ()" is called. Specifically, instead of calling the address of "C.goo", "C.VMT.gooHook" which points to "gooHook" is called. "gooHook" is the "VMTHook ()".

[0190] The address of the second hook function can be determined at compile-time. The virtual call from the machine code method to the bytecode method is generated as the call from the machine code method to the second hook function. The second hook function may obtain the address of the bytecode method at runtime, which is equivalent to implementing the virtual call from the machine code method to the bytecode method in the first compilation result.

[0191] In addition, the second hook function may be a universal run-time function, which is beneficial for saving storage resource usage.,0192] Further, optionally, when the second method is compiled into machine code at runtime, the first call may be achieved through a call to the address of the machine code of the second method.[01931 In this case, the first hook function may be bypassed and the next first call will go directly to the machine code of the second method.|0194] The second method may be compiled into machine code at runtime through dynamic compilation, such as JIT. In other words, the second method may be JIT-compiled into machine code. In this case, the machine code machine may also be called JIT-compiled machine code.|0195] As a possible implementation, the first pointer may be used to point the address of the machine code of the second method.

[0196] The first hook function may also be used to patch the first pointer with the address of the machine code of the second method.|O197] Further, optionally, when the fourth method is compiled into machine code at runtime, the third call may be achieved through a call to the address of the machine code of the fourth method.

[0198] In this case, the second hook function may be bypassed and the next first call will go directly to the machine code of the fourth method.(0199] The fourth method may be compiled into machine code at runtime through dynamic compilation, such as JIT. In other words, the fourth method may be JIT-compiled into JIT-compiled machine code.(0200] As a possible implementation, the second pointer may be used to point the address of the machine code of thefourth method.|02011 The second hook function may also be used to patch the second pointer with the address of the machine code of the fourth method.|0202| During the execution of the first compilation result, some bytecode may be JIT-compiled into machine code. The hook function (such as the first hook function and / or the second hook function) may detect that the target method has been JIT-compiled when called. The hook function may patch the address of the hook function with the address of the machine code of the target method. DAI is taken as an example. For example, the hook function may patch the address in the indirection slot of the DAI corresponding to the call to the target method with the address of the machine code of the target method. As a result, a next call to the target method will bypass the hook function and go directly to the machine code of the target method.[oooi| According to the technical solutions of the embodiments in the present application, the hook function (such as the first hook function and / or the second hook function) may be used to patch the address of the native-to-bytecode call (such as the first call and / or the third call), so that the next call may bypass the hook function and go directly to the machine code of the target method. This helps reduce call overhead and improve the execution speed when the target method has been compiled into machine code at runtime. In this way, the native-to-bytecode call(s) may be achieved at low cost. Moreover, patching the address of target through hook function makes it easier and more efficient to patch the address.|0002| In some embodiments, call(s) whose target is kept in machine code at compile-time and whose caller is kept in bytecode at compile-time, may be achieved through meta-call(s) in the first compilation result.[0003) The bytecode-to-native call(s) may be achieved through meta-call(s). A meta-call refers to an invocation by name with proper passing of parameter(s) on CPU registers or stack according to the signature of the target method and native calling conventions. The meta-call is a known technique, for example, used to support reflective calls, and can be implemented within an interpreter, too.

[0004] In some embodiments, call(s) whose target and caller are kept in machine code at compile-time, may be generated as usual through AOT compilation in the first compilation result.[0005[ In some embodiments, call(s) whose target and caller are kept in bytecode at compile-time, may be generated as usual in the first compilation result.[0006| The bytecode-to-bytecode call(s) may be implemented within the interpreter at runtime.[0007[ FIG. 2 shows an example of implementations of calls between methods with different types of compiled execution binary format at runtime.

[0008] As shown in FIG. 2, the call from the AOT-compiled machine code method to the bytecode method may be achieved through a VMT hook or a DAI hook. The call from the JIT-compiled machine code method to the bytecode methodmay be achieved through a VMT hook or a DAI hook. The call from the bytecode method to AOT-compiled machine code may be achieved through a meta-call. The call from the bytecode method to JIT-compiled machine code may be achieved through a meta-call. At first there are only AOT-compiled machine code and bytecode in the compilation result. At runtime, some bytecode may be JIT-compiled into JIT-compiled machine code. Even if the target is JIT-compiled into JIT-compiled machine code, there is no need to change the AOT-compiled machine code of the caller. Thus, the call from the AOT-compiled method to JIT-compiled machine code may be achieved through the VMT hook or DAI hook at first. For the next calls, the VMT hook or DAI hook may be bypassed.

[0009] Java source code is taken as an example to illustrate the compilation process.[00i0| SI, obtain a package of Java source code.[00111 S2, compile the package with a Java compiler to obtain a compilation result including compiled execution binary.

[0012] S3, execute the compilation result including compiled execution binary.

[0013] S4, kill the program when the termination condition is met to obtain one or more profile files.

[0014] For example, when the program finishes or reaches all execution points, kill the program.

[0015] S5, compile the package again with the one or more profiles.

[0016] Then the compilation result in the mixed binary format is obtained and it could run with faster startup time and better performance.(0017] For array operations, the mixed binary format may reduce ROM by 60% compared to native code, and there is no performance loss. For HashMap operations, the mixed binary format may reduce ROM by 65% compared to native code, and there is no performance loss.|OO18| The mixed binary format provides a better trade-off between the performance of native code and the compactness of bytecode. For example, it is possible to achieve performance close to AOT compilation at lower cost by applying method 100.

[0019] According to the technical solutions of the embodiments in the present application, any function can exist in the form of bytecode or native code when deployed. Any function may be compiled into bytecode or native code without 1 imitation. For example, any function in the source code written in the same language can be capable of freely being compiled into either optional machine code or bytecode by a user. For another example, any function in the source code written in the same language can be compiled into either machine code or bytecode according to profiling flexibly.]0020] According to the technical solutions of the embodiments in the present application, the target of any call (such as direct, indirect or virtual) can exist in the form of bytecode or native code when deployed, which means that all methods can invoke each other in any direction.

[0021] According to the technical solutions of the embodiments in the present application, the bytecode method can be compiled into a machine code method at runtime through JIT compilation, which is beneficial for continuously optimizing the execution process and improving performance.

[0022] FIG. 3 illustrates a method for executing program code provided by some embodiments of the present application. The first compilation result in method 300 may be obtained through method 100. The relevant description can refer to method 100. To avoid repetition, some content may be omitted when describing method 300.]0023] As shown in FIG. 3, method 300 may include the following steps.]0024] 310, obtain a first compilation result of source code. The first compilation result may be in a mixed binary format including both machine code and bytecode.

[0025] A first code segment in the source code is in the machine code format in the first compilation result. A second code segment in the source code is in the bytecode format in the first compilation result.,0026] 320, execute the first compilation result.

[0027] Optionally, a first call from a first method in the source code to a second method in the source code is generated as a second call from the first method to a first hook function in the first compilation result. The first call is a direct call. The first method belongs to the first code segment. The second method belongs to the second code segment.

[0028] Step 320 may include the following steps.

[0029] 321, receive a parameter of the second method passed by the first method through the first hook function.(0030] 322, obtain an address of the second method through the first hook function.|0031] 323, perform an interpretation operation on bytecode of the second method according to the address of the bytecode of the second method and the parameter of the second method.

[0032] Optionally, the second call is achieved through a first pointer pointing to the first hook function. The first pointer is related to ID of the second method.]0033] Step 322 may include: obtaining the address of the bytecode of the second method according to the ID of the second method through the first hook function.

[0034] Further, optionally, step 322 may include: obtaining the address of the second method according to a mapping relationship between addresses of multiple bytecode methods and IDs of the multiple bytecode methods, as well as the ID of the second method.|0035] Optionally, step 320 may include the following steps.[00361 324, compile the bytecode of the second method to obtain machine code of the second method through dynamic compilation.|0037] 325, patch the first pointer with an address of the machine code of the second method.[0038| Optionally, a third call from a third method in the source code to a fourth method in the source code is generated as a fourth call from the third method to a second hook function in the first compilation result. The third call is a virtual call. The third method belongs to the first code segment. The fourth method belongs to the second code segment.[0039[ Step 320 may include the following steps.[0040[ 326, receive a parameter of the fourth method passed by the third method through the second hook function.[0041 [ 327, obtain an address of the fourth method through the second hook function.[0042) 328, perform an interpretation operation on the bytecode code of the fourth method according to the address of bytecode of the fourth method and the parameter of the fourth method.

[0043] FIG. 4 is a schematic block diagram of an electronic device 900 according to some embodiments of the present application. The electronic device 900 may be configured to perform the aforementioned method 100. Referring to FIG. 4, the electronic device 900 includes an obtaining unit 901 and a first compilation unit 902.

[0044] The obtaining unit 901 may be configured to obtain source code.[0045[ The first compilation unit 902 may be configured to perform a compilation operation to obtain a first compilation result of the source code, where a first code segment in the source code is compiled into machine code in the first compilation result, a second code segment in the source code is compiled into bytecode in the first compilation result, and the first compilation result is configured to be deployed to a target environment.[0046| Optionally, the first code segment belongs to hot code, and / or the second code segment belongs to cold code.[00471 Optionally, the device further includes: a second compilation unit, configured to perform a compilation operation to obtain a second compilation result of the source code; and an execution unit, configured to pre-execute the second compilation result to obtain one or more profile files, where the one or more profile files are configured to indicate the hot code and / or the cold code.

[0048] Optionally, a first call from a first method in the source code to a second method in the source code is generated as a second call from the first method to a first hook method in the first compilation result, the first call is a direct call, the first method belongs to the first code segment, the second method belongs to the second code segment, and the first hook method is configured to obtain an address of bytecode of the second method at runtime.

[0049] Optionally, the second call is achieved through a first pointer pointing to the first hook method, the first pointer is related to ID of the second method, and the first hook method is configured to obtain the address of the bytecode of the second method according to the ID of the second method at runtime.[0050| Optionally, the first hook method is configured to obtain the address of the bytecode of the second methodaccording to a mapping relationship between addresses of multiple bytecode methods and IDs of the multiple bytecode methods, as well as the ID of the second method.

[0051] Optionally, the first hook method is configured to patch the first pointer with an address of machine code of the second method, and the machine code of the second method is obtained through dynamic compilation at runtime.

[0052] Optionally, a third call from a third method in the source code to a fourth method in the source code is generated as a fourth call from the third method to a second hook method in the first compilation result, the third call is a virtual call, the third method belongs to the first code segment, the fourth method belongs to the second code segment, and the second hook method is configured to obtain an address of bytecode of the fourth method at runtime.

[0053] Details on how to compile source code may refer to the above-mentioned embodiments and will not be described here.|0054] As shown in FIG. 5, an electronic device 1200 may include a receiver 1201, a processor 1202, a memory 1203, and a transmitter 1204. The memory 1203 may be configured to store code, instructions, and the like executed by the processor 1202.

[0055] It should be understood that the processor 1202 may be an integrated circuit chip and has a signal processing capability. In an implementation process, steps of the foregoing method embodiments may be completed by using a hardware integrated logic circuit in the processor, or by using instructions in a form of software. The processor may be a general purpose processor, a central processing unit (CPU), a graphics processing unit (GPU), a neural processing unit (NPU), a system on chip (SoC) or another programmable logic device, a discrete gate or transistor logic device, or a discrete hardware component. The processor may implement or perform the methods, the steps, and the logical block diagrams that are disclosed in the embodiments of the present application. The general purpose processor may be a microprocessor, or the processor may be any conventional processor or the like. The steps of the methods disclosed with reference to the embodiments of the present application may be directly performed and completed by the processor, or may be performed and completed by using a combination of hardware in the processor and a software module. The software module may be located in a mature storage medium in the art, such as a random access memory, a flash memory, a read-only memory, a programmable read-only memory, an electrically erasable programmable memory, or a register. The storage medium is located in the memory, and the processor reads information in the memory and completes the steps of the foregoing methods performed by the electronic device in combination with hardware in the processor.(0056] It may be understood that the memory 1203 in the embodiments of the present application may be a volatile memory or a nonvolatile memory, or may include both a volatile memory and a nonvolatile memory. The nonvolatile memory may be a read-only memory (Read-Only Memory, ROM), a programmable read-only memory (Programmable ROM, PROM),an erasable programmable read-only memory (Erasable PROM, EPROM), an electrically erasable programmable read-only memory (Electrically EPROM, EEPROM), or a flash memory. The volatile memory may be a random access memory (Random Access Memory, RAM) and is used as an external cache. By way of example rather than limitation, many forms of RAMs may be used, and are, for example, a static random access memory (Static RAM, SRAM), a dynamic random access memory (Dynamic RAM, DRAM), a synchronous dynamic random access memory (Synchronous DRAM, SDRAM), a double data rate synchronous dynamic random access memory (Double Data Rate SDRAM, DDR SDRAM), an enhanced synchronous dynamic random access memory (Enhanced SDRAM, ESDRAM), a synchronous link dynamic random access memory (Synchronous link DRAM, SLDRAM), and a direct rambus random access memory (Direct Rambus RAM, DR RAM).

[0057] It should be noted that the memory in the electronic device and the methods described in this specification includes but is not limited to these memories and a memory of any other appropriate type.

[0058] The present application provides a computer readable storage medium including instructions. When the instructions run on an electronic device, the electronic device is enabled to perform the aforementioned method.(0059] The present application provides a chip system. The chip system includes a memory and a processor, and the memory is configured to store a computer program, and the processor is configured to invoke the computer program from the memory and run the computer program, so that an electronic device on which the chip system is disposed performs the aforementioned method.

[0060] The present application provides a computer program product. When the computer program product runs on an electronic device, the electronic device is enabled to perform the aforementioned method.(00 1 ] In the embodiments of the present application, "at least one" means one or more, and "a plurality of' means two or more. The term "and / or" describes an association relationship between associated objects and represents that three relationships may exist. For example, A and / or B may represent the following three cases: only A exists, both A and B exist, and only B exists, where A and B may be singular or plural. The character " / " generally indicates an "or" relationship between the associated objects. "At least one of the following" and a similar expression thereof refer to any combination of these items, including any combination of one item or a plurality of items. For example, at least one of a, b, and c may indicate: a, b, c, a and b, a and c, b and c, or a, b, and c, where a, b, and c may be singular or plural.(0062] A person of ordinary skill in the art may be aware that, in combination with the examples described in the embodiments disclosed in this specification, units and algorithm steps can be implemented by electronic hardware or a combination of computer software and electronic hardware. Whether the functions are performed by hardware or software depends on particular applications and design constraints of the technical solutions. A person skilled in the art may use different methods to implement the described functions for each particular application, but it should not be considered that theimplementation goes beyond the scope of this application.|00631 It may be clearly understood by a person skilled in the art that, for the purpose of convenient and brief description, for a detailed working process of the foregoing system, apparatus, and unit, refer to a corresponding process in the foregoing method embodiment. Details are not described herein again.

[0064] In the several embodiments provided in this application, it should be understood that the disclosed system, apparatus, and method may be implemented in other manners. For example, the described apparatus embodiment is merely an example. For example, the unit division is merely logical function division and may be other division in actual implementation. For example, a plurality of units or components may be combined or integrated into another system, or some features may be ignored or not performed. In addition, the displayed or discussed mutual couplings or direct couplings or communication connections may be implemented through some interfaces. The indirect couplings or communication connections between the apparatuses or units may be implemented in electronic, mechanical, or other forms.[0065| The units described as separate parts may be or may not be physically separate, and parts displayed as units may be or may not be physical units, may be located in one position, or may be distributed on a plurality of network units. Some or all of the units may be selected based on actual requirements to achieve the objectives of the solutions of the embodiments.

[0066] In addition, functional units in the embodiments of this application may be integrated into one processing unit, or each of the units may exist alone physically, or two or more units are integrated into one unit.[0067| When the functions are implemented in a form of a software functional unit and sold or used as an independent product, the functions may be stored in a computer readable storage medium. Based on such an understanding, the technical solutions in this application essentially, or the part contributing to the prior art, or some of the technical solutions may be implemented in a form of a software product. The computer software product is stored in a storage medium, and includes several instructions for instructing a computer device (which may be a personal computer, a server, a network device, or the like) to perform all or some of the steps of the methods described in the embodiments of this application. The foregoing storage medium includes: any medium that can store program code, such as a USB flash drive, a removable hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk, or an optical disc.[0068| The foregoing descriptions are merely specific implementations of this application, but are not intended to limit the protection scope of this application. Any variation or replacement readily figured out by a person skilled in the art within the technical scope disclosed in this application shall fall within the protection scope of this application. Therefore, the protection scope of this application shall be subject to the protection scope of the claims.

Claims

CLAIMSWhat is claimed is:

1. A method for compiling program code, comprising: obtaining source code; and performing a compilation operation to obtain a first compilation result of the source code, wherein a first code segment in the source code is compiled into machine code in the first compilation result, a second code segment in the source code is compiled into bytecode in the first compilation result, and the first compilation result is configured to be deployed to a target environment.

2. The method according to claim 1, wherein the first code segment belongs to hot code, and / or the second code segment belongs to cold code.

3. The method according to claim 2, further comprising: performing a compilation operation to obtain a second compilation result of the source code; and pre-executing the second compilation result to obtain one or more profile files, wherein the one or more profile files are configured to indicate the hot code and / or the cold code.

4. The method according to any one of claims 1 to 3, wherein a first call from a first method in the source code to a second method in the source code is generated as a second call from the first method to a first hook method in the first compilation result, the first call is a direct call, the first method belongs to the first code segment, the second method belongs to the second code segment, and the first hook method is configured to obtain an address of bytecode of the second method at runtime.

5. The method according to claim 4, wherein the second call is achieved through a first pointer pointing to the first hook method, the first pointer is related to ID of the second method, and the first hook method is configured to obtain the address of the bytecode of the second method according to the ID of the second method at runtime.

6. The method according to claim 5, wherein the first hook method is configured to obtain the address of the bytecode of the second method according to a mapping relationship between addresses of multiple bytecode methods and IDs of the multiple bytecode methods, as well as the ID of the second method.

7. The method according to claim 5 or 6, wherein the first hook method is configured to patch the first pointer with an address of machine code of the second method, and the machine code of the second method is obtained through dynamic compilation at runtime.

8. The method according to any one of claims 1 to 7, wherein a third call from a third method in the source code to a fourthmethod in the source code is generated as a fourth call from the third method to a second hook method in the first compilation result, the third call is a virtual call, the third method belongs to the first code segment, the fourth method belongs to the second code segment, and the second hook method is configured to obtain an address of bytecode of the fourth method at runtime.

9. An electronic device, comprising: an obtaining unit, configured to obtain source code; and a first compilation unit, configured to perform a compilation operation to obtain a first compilation result of the source code, wherein a first code segment in the source code is compiled into machine code in the first compilation result, a second code segment in the source code is compiled into bytecode in the first compilation result, and the first compilation result is configured to be deployed to a target environment10. The electronic device according to claim 9, wherein the first code segment belongs to hot code, and / or the second code segment belongs to cold code.

11. The electronic device according to claim 10, further comprising: a second compilation unit, configured to perform a compilation operation to obtain a second compilation result of the source code; and an execution unit, configured to pre-execute the second compilation result to obtain one or more profile files, wherein the one or more profile files are configured to indicate the hot code and / or the cold code.

12. The electronic device according to any one of claims 9 to 11, wherein a first call from a first method in the source code to a second method in the source code is generated as a second call from the first method to a first hook method in the first compilation result, the first call is a direct call, the first method belongs to the first code segment, the second method belongs to the second code segment, and the first hook method is configured to obtain an address of bytecode of the second method at runtime.

13. The electronic device according to claim 12, wherein the second call is achieved through a first pointer pointing to the first hook method, the first pointer is related to ID of the second method, and the first hook method is configured to obtain the address of the bytecode of the second method according to the ID of the second method at runtime.

14. The electronic device according to claim 13, wherein the first hook method is configured to obtain the address of the bytecode of the second method according to a mapping relationship between addresses of multiple bytecode methods and IDs of the multiple bytecode methods, as well as the ID of the second method.

15. The electronic device according to claim 13 or 14, wherein the first hook method is configured to patch the first pointer with an address of machine code of the second method, and the machine code of the second method is obtained through dynamic compilation at runtime.

16. The electronic device according to any one of claims 9 to 15, wherein a third call from a third method in the source code to a fourth method in the source code is generated as a fourth call from the third method to a second hook method in the first compilation result, the third call is a virtual call, the third method belongs to the first code segment, the fourth method belongs to the second code segment, and the second hook method is configured to obtain an address of bytecode of the fourth method at runtime.

17. A computer readable storage medium, wherein the computer readable storage medium stores instructions, and when the instructions run on an electronic device, the electronic device is enabled to perform the method according to any one of claims 1 to 8.

18. An electronic device, comprising a memory and a processor, wherein the memory is configured to store a computer program, and the processor is configured to invoke the computer program from the memory and run the computer program, so that the electronic device performs the method according to any one of claims 1 to 8.

19. A chip system, comprising a memory and a processor, wherein the memory is configured to store a computer program, and the processor is configured to invoke the computer program from the memory and run the computer program, so that an electronic device on which the chip system is disposed performs the method according to any one of claims 1 to 8.

20. A computer program product, wherein when the computer program product runs on an electronic device, the electronic device is enabled to perform the method according to any one of claims 1 to 8.

Citation Information

Patent Citations

  • Size dependent type in accessing dynamically typed array objects

    EP3039537B1

  • Selective compiling method, device, and corresponding computer program product

    US20130311980A1

  • Device and method for protecting code integrity

    WO2023001366A1

Cited By

  • An RTOS task hook type adaptation method based on a compile-time static trampoline function table

    CN122431993A