Code execution method and electronic equipment

By compiling the code of the dynamic type programming language in advance to generate machine code and detecting information consistency when calling function, the performance degradation caused by dynamic type changes in JIT compilation is solved, and execution speed and debugging convenience are improved.

CN119883266APending Publication Date: 2025-04-25HUAWEI TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202311376038.4
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2023-10-23
Publication Date
2025-04-25

AI Technical Summary

Technical Problem

The operating efficiency of the dynamic type programming language cannot meet the needs of the interpreter in performance-critical scenarios. Due to changes in dynamic type, the compiler optimization strategy fails, resulting in performance degradation, and the non-standard stack frame structure is not convenient for debugging.

Method used

Compile the code in advance to generate machine code, and store the compiled function and function information, detect the consistency of information when the function is called. If it is consistent, execute the machine code, otherwise it will degenerate to the interpreter to execute, and rebuild the standard stack frame structure for debugging.

Benefits of technology

Improves the execution speed of code, avoids compiler degradation, simplifies the debugging process, and supports cross-platform porting.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119883266A_ABST
    Figure CN119883266A_ABST
Patent Text Reader

Abstract

The invention provides a code execution method and electronic equipment, and the method comprises the steps: obtaining a first code which comprises a first function; compiling the first code in advance to generate a second code which comprises a second function; storing the second function and the first function information, wherein the first function information is the function information of the second function when the compilation is carried out in advance; when the second function is called, whether second function information is the same as the first function information or not is detected, and the second function information is function information of the second function when the second function is called; and if the first function information is the same as the second function information, executing the second function. In the embodiment of the invention, the first code can be compiled in advance, when the function is called, whether the function information is the same as the function information compiled in advance or not can be detected, when the function information is the same as the function information compiled in advance, the compiled function can be executed, and due to the fact that the second function is the machine code, the execution speed of the code can be increased.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of computers, and more particularly, to a method for executing codes and an electronic device. Background Art

[0002] The flexible characteristics of dynamically typed programming languages ​​(such as JavaScript) make them widely used in scenarios such as scripts and browsers. However, the characteristics of dynamic types mean that variable types can change at any time during runtime, and the data structure of objects may also change. Therefore, in order to cope with these dynamic characteristics, dynamically typed programming languages ​​usually use interpreters to interpret and execute code to dynamically handle changes in types and data structures at runtime. However, in performance-critical scenarios, the operating efficiency of interpreters cannot meet the requirements. In order to improve performance, just-in-time (JIT) compilation is generally used, in which the compiler converts source code into machine code, which is then directly executed at runtime to achieve better performance. However, although JIT compilation has high performance, since variable types and data structures in dynamically typed languages ​​may change at any time, the compiler has to consider various different execution paths. In order to optimize these paths, the compiler may use speculative optimization strategies. However, although speculative optimization can improve performance, it cannot guarantee that the assumptions made during compilation are always met when the program is running. When the actual execution path does not match the compiler's assumptions, the compiler's optimization strategy will fail and performance will degrade. This situation is called compiler deoptimization, that is, the optimized code generated by the compiler cannot work properly on certain execution paths and needs to fall back to the interpreter for execution, which may affect the execution speed of the code. In order to facilitate switching between the interpreter and the compiled code, a non-standard stack frame structure is currently used, which requires developers to customize and is not easy to debug. Based on this, how to improve the execution speed of the code has become a technical problem that needs to be solved urgently. Summary of the invention

[0003] The present application provides a method for executing code and an electronic device, which can compile the code in advance and store the pre-compiled functions and function information. When the function is called, the compiled function can be directly called, thereby improving the execution speed of the code.

[0004] In a first aspect, a method for executing code is provided, the method comprising: obtaining a first code, the first code comprising a first function, the first code being a source code; compiling the first code in advance to generate a second code, the second code comprising a second function, the second function being a compiled function of the first function, the second code being a machine code; storing the second function and first function information, the first function information being function information of the second function when performing the advance compilation, the function information comprising at least one of the following: a parameter type in the function, a function pointer; when calling the second function, detecting whether the second function information is the same as the first function information, the second function information being function information of the second function when calling the second function; if the first function information is the same as the second function information, executing the second function.

[0005] In an embodiment of the present application, the first code can be compiled in advance, and the pre-compiled function and function information can be stored. When the function is called, it can be detected whether the function information is the same as the function information during the pre-compilation. When they are the same, the compiled function can be executed. Since the second function is a machine code, the execution speed of the code can be improved compared to executing a bytecode function or a source code function through an interpreter.

[0006] In combination with the first aspect, in certain implementations of the first aspect, the method further includes: if the first function information is different from the second function information, calling the interpreter to execute the first function.

[0007] In an embodiment of the present application, the first code can be compiled in advance, and the pre-compiled functions and function information can be stored. When the function is called, it can be detected whether the function information is the same as the function information during the pre-compilation. When they are different, the function in the source code can be executed, thereby ensuring the correct execution of the code.

[0008] In combination with the first aspect, in certain implementations of the first aspect, the method further includes: if the first function information is different from the second function information, calling the interpreter to execute a third function, the third function being the bytecode function corresponding to the first function.

[0009] In an embodiment of the present application, the first code can be compiled in advance, and the pre-compiled functions and function information can be stored. When the function is called, it can be detected whether the function information is the same as the function information during pre-compilation. When they are different, the bytecode function corresponding to the function in the source code can be executed, thereby ensuring the correct execution of the code.

[0010] In combination with the first aspect, in certain implementations of the first aspect, the first code also includes a fourth function, the second code does not include a function corresponding to the fourth function, and the method also includes: calling an interpreter to execute the fourth function.

[0011] In combination with the first aspect, in certain implementations of the first aspect, the first code also includes a fourth function, and the second code does not include a function corresponding to the fourth function. The method also includes: calling an interpreter to execute a fifth function, and the fifth function is a bytecode function corresponding to the fourth function.

[0012] In the embodiment of the present application, when there is a function that is not compiled in advance in the first code, an interpreter can be called to execute the function or the bytecode function corresponding to the function. The embodiment of the present application combines machine code execution and interpreter execution, which not only ensures the running efficiency of the code but also ensures the correct execution of the code.

[0013] In combination with the first aspect, in some implementations of the first aspect, the method further includes: during the execution of the second function, detecting whether a speculative optimization condition is met; if the speculative optimization condition is met, continuing to execute the second function.

[0014] In an embodiment of the present application, it is possible to detect whether the speculative optimization conditions are met when executing the second function. When the speculative optimization conditions are met, the second function can continue to be executed. Since the second function is a function composed of machine code, the running efficiency of the code can be effectively improved.

[0015] In combination with the first aspect, in certain implementations of the first aspect, the method also includes: if the speculative optimization condition is not met, rebuilding the stack frame structure and calling the interpreter to execute the first function from a first part of the first function, wherein the first part of the first function is determined based on the unexecuted part of the second function.

[0016] In the embodiment of the present application, it is possible to detect whether the speculative optimization condition is met when executing the second function. When the speculative optimization condition is not met, the stack frame structure can be rebuilt and the interpreter can be called to execute the code associated with the remaining code of the second function in the first function, thereby ensuring the correct operation of the code. In addition, the stack frame structure is a standard stack frame structure, and no special stack frame analyzer is required. There is no need to use a non-standard parameter passing method when calling external code, which is convenient for program transplantation and debugging.

[0017] In combination with the first aspect, in certain implementations of the first aspect, the method also includes: if the speculative optimization condition is not met, rebuilding the stack frame structure and calling the interpreter to execute the third function from the first part of the third function, the third function being the bytecode function corresponding to the first function, and the first part of the third function being determined based on the unexecuted part of the second function.

[0018] In the embodiment of the present application, it is possible to detect whether the speculative optimization condition is met when executing the second function. When the speculative optimization condition is not met, the stack frame structure can be rebuilt and the interpreter can be called to execute the code associated with the remaining code of the second function in the third function, thereby ensuring the correct operation of the code. In addition, the stack frame structure is a standard stack frame structure, and no special stack frame analyzer is required. There is no need to use a non-standard parameter passing method when calling external code, which is convenient for program transplantation and debugging.

[0019] In combination with the first aspect, in certain implementations of the first aspect, the calling interpreter executes the third function starting from the first part of the third function, including: allocating space required for the operand stack and the variable table; assigning the state of the interpreter to corresponding positions of the operand stack and the variable table; passing the third function, operand stack, local variables and program counter as parameters to the interpreter.

[0020] In combination with the first aspect, in certain implementations of the first aspect, the method further includes: instructing the interpreter to jump to the exit of the second function after returning.

[0021] In a second aspect, a method for executing code is provided, the method comprising: obtaining a first code, the first code comprising a first function; compiling the first code in advance to generate a second code, the second code comprising a second function, the second function being a compiled function of the first function; when the first function is called, executing the second function; when executing the second function, checking whether a speculative optimization condition is met; if the speculative optimization condition is met, continuing to execute the second function.

[0022] In combination with the second aspect, in certain implementations of the second aspect, the method also includes: if the speculative optimization condition is not met, rebuilding the stack frame structure and calling the interpreter to execute the first function starting from the first part of the first function, and the first part of the first function is determined based on the unexecuted part of the second function.

[0023] In combination with the second aspect, in certain implementations of the second aspect, the method also includes: if the speculative optimization condition is not met, rebuilding the stack frame structure and calling the interpreter to execute the third function from the first part of the third function, and the first part of the third function is determined based on the unexecuted part of the second function.

[0024] In combination with the second aspect, in certain implementations of the second aspect, the calling interpreter executes the third function starting from the first part of the third function, including: allocating space required for the operand stack and the variable table; assigning the state of the interpreter to corresponding positions of the operand stack and the variable table; passing the third function, operand stack, local variables and program counter as parameters to the interpreter.

[0025] In combination with the second aspect, in some implementations of the second aspect, the method further includes: instructing the interpreter to jump to the exit of the second function after returning.

[0026] In a third aspect, an electronic device is provided, which includes one or more processors; one or more memories; the one or more memories store one or more computer programs, and the one or more computer programs include instructions, which, when executed by the one or more processors, enable the above aspects or any possible implementation of the above aspects to be executed.

[0027] In a fourth aspect, a computer-readable storage medium is provided, which includes a computer program or instructions. When the computer program or instructions are run on a computer, the first aspect and any possible implementation method of the first aspect are executed.

[0028] In a fifth aspect, a computer program product is provided, which includes a computer program or instructions. When the computer program or instructions are run on a computer, the first aspect and any possible implementation method of the first aspect are executed.

[0029] In a sixth aspect, a computer program is provided, which, when executed on a computer, enables the method in the first aspect and any possible implementation thereof to be executed.

[0030] The seventh aspect is an electronic device of an embodiment of the present application, which includes modules / units for executing the above aspects or any possible design method of the above aspects; these modules / units can be implemented by hardware, or the corresponding software can be implemented by hardware.

[0031] Among them, for the beneficial effects of the second to seventh aspects, please refer to the beneficial effects of the first aspect and will not be repeated. BRIEF DESCRIPTION OF THE DRAWINGS

[0032] Figure 1 This is a diagram of the stack frame structure of the V8 compiler.

[0033] Figure 2 It is a schematic flowchart of the method for executing code provided in an embodiment of the present application.

[0034] Figure 3 It is a schematic diagram of the AOT compiler provided in an embodiment of the present application.

[0035] Figure 4 It is a schematic flowchart of the method for executing code provided in an embodiment of the present application.

[0036] Figure 5 It is a schematic diagram of the stack frame structure of the interpreter provided in the embodiment of the present application.

[0037] Figure 6 It is a schematic flowchart of the method for executing code provided in an embodiment of the present application.

[0038] Figure 7 It is a schematic diagram of the structure of an electronic device provided in an embodiment of the present application.

[0039] Figure 8 It is a schematic diagram of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0040] The technical solution in this application will be described below in conjunction with the accompanying drawings.

[0041] In the description of the embodiments of the present application, unless otherwise specified, " / " means or, for example, A / B can mean A or B; "and / or" in this article is only a description of the association relationship of associated objects, indicating that there can be three relationships, for example, A and / or B can mean: A exists alone, A and B exist at the same time, and B exists alone. In addition, in the description of the embodiments of the present application, "multiple" means two or more than two.

[0042] In the following, the terms "first" and "second" are used for descriptive purposes only and are not to be understood as indicating or implying relative importance or implicitly indicating the number of the indicated technical features. Thus, a feature defined as "first" or "second" may explicitly or implicitly include one or more of the features. In the description of this embodiment, unless otherwise specified, "plurality" means two or more.

[0043] In addition, the terms "include" and "have" and any variations thereof are intended to cover non-exclusive inclusions. For example, a process, method, system, product or device comprising a series of steps or units is not limited to the listed steps or units, but may optionally include steps or units not listed, or may optionally include other steps or units inherent to these processes, methods, products or devices.

[0044] In the embodiments of the present application, words such as "exemplary" or "for example" are used to indicate examples, illustrations or descriptions. Any embodiment or design described as "exemplary" or "for example" in the embodiments of the present application should not be interpreted as being more preferred or more advantageous than other embodiments or designs. Specifically, the use of words such as "exemplary" or "for example" is intended to present related concepts in a concrete way for easy understanding.

[0045] In the present application, "at least one (item)" refers to one or more. "Multiple" refers to two or more. "At least two (items)" refers to two or three and more than three. "And / or" is used to describe the association relationship of associated objects, indicating that three relationships can exist. For example, "A and / or B" can mean: only A exists, only B exists, and A and B exist at the same time, where A and B can be singular or plural. The character " / " generally indicates that the previous and next associated objects are in an "or" relationship. "At least one of the following items" or similar expressions refers to any combination of these items, including any combination of single items or plural items. For example, at least one of a, b and / or c can mean: a, b, c, "a and b", "a and c", "b and c", or "a and b and c", where a, b, c can be single or multiple. “When” and “if” both mean that corresponding measures will be taken under certain objective circumstances. It does not limit the time, nor does it require any judgment when it is implemented, nor does it mean that there are other limitations.

[0046] References to "one embodiment" or "some embodiments" etc. described in this specification mean that a particular feature, structure or characteristic described in conjunction with the embodiment is included in one or more embodiments of the present application. Thus, the phrases "in one embodiment", "in some embodiments", "in some other embodiments", "in some other embodiments", etc. that appear at different places in this specification do not necessarily refer to the same embodiment, but mean "one or more but not all embodiments", unless otherwise specifically emphasized in other ways. The terms "including", "comprising", "having" and their variations all mean "including but not limited to", unless otherwise specifically emphasized in other ways.

[0047] For ease of understanding, the following introduces the relevant terms and concepts involved in the embodiments of the present application. The terms used in the embodiments of the present application are only used to explain the specific embodiments of the present application, and are not intended to limit the present application.

[0048] Dynamic typing: In a programming language, a feature that allows the type of a variable to be changed at runtime.

[0049] Weak typing: A feature in a programming language where type conversion is relatively loose or automatic, and does not require explicit type declarations or conversion operations.

[0050] Compiler: A compiler is a program that converts one language into another language, or is also called a functional module. Most compilers are functional modules that translate the target program source code into machine code. The compiler is responsible for executing the compilation step in the program processing process.

[0051] In order to reduce the difficulty of compiler code implementation and improve the scalability of the compiler, the compiler can be divided into the compiler front end, compiler mid-end, and compiler back end during implementation, and the compiler front end, compiler mid-end, and compiler back end are implemented independently. Among them, the compiler front end is mainly responsible for preprocessing, lexical analysis, syntax analysis, semantic analysis, and generating intermediate representations (IR) that are convenient for subsequent processing; the compiler mid-end performs analysis and various optimizations, such as constant folding, dead code elimination, etc.; the compiler back end is responsible for generating target code (compiled code or machine code), and the target code is platform-related assembly code. Among them, if there is only one compiler in the program processing process, the compiler will further translate the assembly code into machine code after generating the target code.

[0052] Interpreter: Also known as a translator, it is a computer program that can directly translate and run high-level programming languages ​​line by line. The interpreter does not translate the entire program at once, but acts as a "middleman". Each time the program is run, it must first be translated into another language before running it. Therefore, the interpreter's program runs slowly. It runs immediately after translating a line of program description, then translates the next line, and runs it again, and so on.

[0053] In some embodiments, the interpreter can also translate and execute bytecode functions.

[0054] JIT compilation: A technique that dynamically compiles source code into machine code while the program is running.

[0055] Ahead of time (AOT) compilation: A technique that compiles source code into machine code before the program runs.

[0056] Compiler degeneration: The process of switching from compiled code to an interpreter when running the compiled code and discovering that the data structures in the runtime do not have the expected semantics.

[0057] Speculative optimization: An optimization made hypothetically to improve program performance that may work in some cases but is not always correct.

[0058] The flexible characteristics of dynamically typed programming languages ​​(such as JavaScript) have made them widely used in scenarios such as scripts and browsers. However, the characteristics of dynamic types mean that variable types can change at any time during runtime, and the data structure of objects may also change. Therefore, in order to cope with these dynamic characteristics, dynamically typed programming languages ​​usually use interpreters to interpret and execute code to dynamically handle changes in types and data structures at runtime. However, in performance-critical scenarios, the operating efficiency of interpreters cannot meet the requirements. In order to improve performance, JIT compilation is widely used, in which the compiler converts source code into machine code, which is then directly executed at runtime to achieve better performance. However, although JIT compilation has high performance, since variable types and data structures in dynamically typed languages ​​may change at any time, the compiler has to consider various different execution paths. In order to optimize these paths, the compiler may use speculative optimization strategies. However, although speculative optimization can improve performance, it cannot guarantee that the assumptions made at compile time are always met when the program is running. When the actual execution path does not match the compiler's assumptions, the compiler's optimization strategy will fail and performance will degrade. This situation is called compiler degradation, that is, the optimized code generated by the compiler cannot work properly on certain execution paths and needs to fall back to the interpreter for execution.

[0059] For example, if the code needs to execute the "x+1" operation, the compiler can assume that x is an integer before executing the above operation. Then the execution path corresponding to "x+1" is integer calculation. When it is actually executed, x is a floating point number, which is inconsistent with the compiler's assumption. "x+1" cannot be executed normally on the original execution path, so it needs to be degraded to the interpreter execution. Because the interpreter executes source code or bytecode, the execution speed of the interpreter is slower than that of the machine code.

[0060] Taking the V8 compiler as an example, the compiler will perform the above-mentioned speculative optimization and will also perform compiler degeneration when the optimization conditions are not met. In order to enable the degenerated compiled code to be quickly switched to the interpreter, the compiled code and the interpreter share the same stack frame structure, such as Figure 1 As shown, when there is a need to return to the interpreter, the compiled code can use certain jump codes to allow the interpreter to take over the current stack frame while keeping the stack frame structure unchanged.

[0061] However, the functions in the code compiled by the V8 compiler are usually in a non-standard stack frame format, and the debugging information needs to be processed accordingly, making the debugging operation cumbersome. In addition, the degenerate and interpreter-related codes in this way are not applicable to the stack frame structure automatically generated by other compilers (such as the C compiler). It is usually necessary to write platform-related code or assembly, which is not convenient for cross-platform porting and other operations. In addition, for the V8 compiler, its optimizing compiler will directly use the pointer of the data structure in the memory of the current running environment in the generated code. Although it improves the efficiency of the generated code, the data structure in the memory will be lost after the operation ends. The generated code cannot be reused in subsequent operations, nor can it be reused in AOT compilation.

[0062] In summary, currently when compiling code, the compiler cannot directly obtain the data used when the code is running in the memory, and can only generate code for performing related operations at runtime. In addition, the above technical solution requires a customized stack frame structure and the generation of code to jump to the interpreter, which is not convenient for cross-platform porting. Based on this, an embodiment of the present application provides a method for executing code, which can perform mixed execution of compiled and generated code and interpreter at any execution point of a function, and does not involve a customized stack frame structure, which can improve the optimization opportunities of the execution engine, the robustness of the runtime, and the ability of cross-platform porting.

[0063] In an embodiment of the present application, detection can be performed at the function entry of the code and when the function is running. When it is detected that the conditions are not met, it can be degraded to the interpreter for execution. The following will introduce the degradation of the code at the function entry to the interpreter and the degradation of the function to the interpreter during runtime in conjunction with the accompanying drawings.

[0064] Figure 2 A schematic flow chart of a method for executing code provided in an embodiment of the present application is shown, as follows: Figure 2 As shown, the method includes:

[0065] S201, obtaining a first code.

[0066] The compiler obtains a first code, which is a source code, and the source code may be a source code written in a dynamic type programming language. The first code includes a first function, and the first function will be called in the speech of the first code.

[0067] For example, the first code may be a code written using JavaScript.

[0068] S202: Pre-compile the first code to generate a second code.

[0069] After the compiler obtains the first code, it can compile the first code in advance to generate a second code. The second code can be a machine code. The second code includes a second function, which is a corresponding function after compiling the first function.

[0070] In some embodiments, the compiler may perform speculative optimization when compiling the first code in advance.

[0071] For example, the first function includes a parameter "x". When compiling, the compiler may assume that the parameter "x" is an integer type and compile the code in the first function according to the parameter "x" being an integer type.

[0072] In some embodiments, the compiler may compile all functions in the first code.

[0073] In some embodiments, due to reasons such as the functions in the first code being too large, the compiler may not be able to compile all functions in the first code in advance.

[0074] For example, the first code includes function #1, function #2, and function #3, but function #3 is too large. When the compiler compiles the first code in advance, it may compile function #1 and function #2, but not function #3.

[0075] S203, storing the second function and function information of the second function during advance compilation.

[0076] After the compiler compiles the function in the first code, it can store the compiled function and the function information during the advance compilation, and the function information includes but is not limited to the parameter type of the function and the function pointer.

[0077] In the embodiment of the present application, there is no limitation on the storage location of the compiled function and the function information during advance compilation. The following is an exemplary method.

[0078] Exemplarily, the compiler in the embodiment of the present application may be an AOT compiler modified from the QuickJS interpreter. Figure 3As shown, the data structure of a function in the QuickJS interpreter can be represented by JSObject. There is a class field in JSObject to indicate whether it is a function. For a function object, class = function. The pointer at the end of JSObject points to the bytecode JSFunctionBytecode and other function-related data structures. JSFunctionBytecode stores the bytecode and metadata information of the function, such as name, number of parameters, constant pool, etc. In an embodiment of the present application, the compiled function and the function information during advance compilation can be used as metadata information, and the above metadata information can be inserted into JSFunctionBytecode when the first code is initialized, that is, the function information of the compiled function and the function during advance compilation is stored.

[0079] For example, the first code includes a first function, which is compiled into a second function. The first function includes parameter #1. When the compiler compiles the first function, the type of parameter #1 is an integer. The compiler can store the information that the second function and parameter #1 are of integer type in JSFunctionBytecode.

[0080] It is understandable that for functions that are not pre-compiled, the value of the relevant field in JSFunctionBytecode is empty. For functions that are not pre-compiled, the interpreter can be directly called to execute the source code function or the corresponding bytecode function.

[0081] It should be noted that the above is only an example of an AOT compiler modified from a QuickJS interpreter. The type of compiler corresponding to the embodiment of the present application, the storage location of the compiled function and the function information during advance compilation are not specifically limited. Other compilers can also be used in the embodiment of the present application, and the compiled function and the function information during advance compilation can be stored in other locations.

[0082] S204, when calling the second function, detecting whether the function information of the second function when calling the second function is the same as the function information of the second function when compiled in advance.

[0083] The semantics of the first code calls the first function, the first code is compiled to generate the second code, and the second function is the function obtained by pre-compilation of the first function, then the execution engine will call the second function when executing the second code. When the execution engine calls the second function, it can detect whether the function information of the second function at this time is the same as the function information of the second function stored in advance compilation, if the same, execute step S205, if not the same, execute step S206 or execute step S207.

[0084] For example, when the compiler compiles in advance, the type of the parameter "x" in the second function is an integer. When calling the second function, the execution engine can first detect the type of the parameter "x" in the second function, and execute step S205 when "x" is an integer, and execute step S206 or step S207 when "x" is not an integer.

[0085] S205, executing the second function.

[0086] Since the function information of the second function when calling the second function is the same as the function information of the second function when compiled in advance, the execution engine can execute the second function.

[0087] In an embodiment of the present application, the first code can be compiled in advance, and the pre-compiled function and function information can be stored. When the function is called, it can be detected whether the function information is the same as the function information during the pre-compilation. When they are the same, the compiled function can be executed. Since the second function is a machine code, the execution speed of the code can be improved compared to executing a bytecode function or a source code function through an interpreter.

[0088] S206, calling the interpreter to execute the first function.

[0089] When the second function is called and the function information when it is called is different from the function information when it is compiled in advance, it can be degenerated to the interpreter, and the interpreter interprets and executes the first function.

[0090] In an embodiment of the present application, the first code can be compiled in advance, and the pre-compiled functions and function information can be stored. When the function is called, it can be detected whether the function information is the same as the function information during the pre-compilation. When they are different, the function in the source code can be executed, thereby ensuring the correct execution of the code.

[0091] S207, calling the interpreter to execute the third function.

[0092] The third function is the bytecode function corresponding to the first function. In an embodiment of the present application, after obtaining the source code (i.e., the first code), in addition to compiling the source code into machine code (i.e., the second code), the source code can also be structured to generate an abstract syntax tree (AST), and then generate bytecode code (referred to as the third code) based on the AST and the scope. Bytecode is an intermediate code between AST and machine code. When the second function is called and the function information when it is called is different from the function information when it is compiled in advance, it can be degraded to the interpreter, which interprets and executes the third function composed of bytecode.

[0093] It should be noted that the embodiment of the present application does not limit the time for generating the third function.

[0094] Exemplarily, the third function may be generated before executing step S204. In other words, after obtaining the source code, the source code may be compiled into machine code or byte code. It should be noted that, in this manner, the embodiment of the present application does not specifically limit the order of generating the machine code and the byte code.

[0095] Exemplarily, the third function may be generated when it is determined that step S207 needs to be executed. That is, if it is determined that the function information of the second function when calling the second function is different from the function information of the second function when compiled in advance, the source code may be compiled into bytecode to obtain the third function.

[0096] In an embodiment of the present application, the first code can be compiled in advance, and the pre-compiled functions and function information can be stored. When the function is called, it can be detected whether the function information is the same as the function information during pre-compilation. When they are different, the bytecode function corresponding to the function in the source code can be executed, thereby ensuring the correct execution of the code.

[0097] In some embodiments, the first code further includes a fourth function, and there is no function corresponding to the fourth function in the second code. In other words, the compiler does not pre-compile the fourth function.

[0098] Exemplarily, the fourth function may be a function newly created during operation, and the compiler cannot pre-compile the fourth function.

[0099] Exemplarily, the fourth function in the first code is too large, and the compiler may not be able to compile the fourth function during advance compilation.

[0100] Since there is no function corresponding to the fourth function in the second code, it needs to be degenerated into the interpreter for execution.

[0101] In a possible implementation, the interpreter interprets and executes the fourth function.

[0102] In a possible implementation, the interpreter interprets and executes the fifth function, wherein the fifth function is the bytecode function corresponding to the fourth function. It should be understood that the description of the fifth function can refer to the description of the third function above, and for the sake of brevity, it will not be repeated here.

[0103] In the embodiment of the present application, when there is a function that is not compiled in advance in the first code, an interpreter can be called to execute the function or the bytecode function corresponding to the function. The embodiment of the present application combines machine code execution and interpreter execution, which not only ensures the running efficiency of the code but also ensures the correct execution of the code.

[0104] In the above method, it is detected at the function entrance whether the function information of the function is the same as the function information during advance compilation. In other embodiments of the present application, the first code can be compiled in advance to generate the second code. Speculative optimization can be used during advance compilation. Then, during the execution of the function of the second code, it is detected whether the speculative optimization conditions are met. When the speculative optimization conditions are met, the function in the second code can continue to be executed. If the speculative optimization conditions are not met, it is degraded to the interpreter to execute the remaining code. The following is combined with Figure 4 Give a detailed introduction.

[0105] Figure 4 A schematic flow chart of a method for executing code provided in an embodiment of the present application is shown, such as Figure 4 As shown, the method includes:

[0106] S401, execute the second function.

[0107] The compiler compiles the first code in advance to generate the second code, the second function is a function in the second code, the second function is associated with the first function in the first code, the first code is a source code, and the second code is a machine code. The execution engine can execute the second function.

[0108] S402, detecting whether the operating environment when executing the second function satisfies the speculative optimization condition.

[0109] When executing the second function, the execution engine can detect whether the current operating environment meets the speculative optimization conditions. If the compiler performs speculative optimization on the first function during advance compilation, the compiler can determine the speculative optimization conditions corresponding to the second function during advance compilation, and generate corresponding detection code according to the speculative optimization conditions. When it is determined that the current operating environment meets the speculative optimization conditions, step S403 can be executed, and when it is determined that the current operating environment does not meet the speculative optimization conditions, step S404 or step S405 can be executed.

[0110] For example, when the compiler compiles in advance, it assumes that the parameter "x" is an integer type and generates corresponding detection code, so that when the execution engine executes the second function and involves the parameter "x", it can execute the corresponding detection code to detect whether the parameter "x" is an integer type. When the parameter "x" is an integer type, step S403 is executed; when the parameter "x" is not an integer type, step S404 or step S405 is executed.

[0111] S403, continue to execute the second function.

[0112] Since the operating environment when executing the second function meets the speculative optimization condition, the second function can continue to be executed.

[0113] In an embodiment of the present application, it is possible to detect whether the speculative optimization conditions are met when executing the second function. When the speculative optimization conditions are met, the second function can continue to be executed. Since the second function is a function composed of machine code, the running efficiency of the code can be effectively improved.

[0114] S404, rebuilding the stack frame structure and calling the interpreter to execute the first function starting from the first part of the first function.

[0115] When the operating environment when executing the second function does not meet the speculative optimization conditions, the stack frame structure can be rebuilt and the interpreter can be called to execute the first function from the first part of the first function user, the first part of the first function is determined according to the unexecuted part of the second function, and the stack frame structure is the stack frame result applicable to the interpreter. In other words, when it is determined that the speculative optimization conditions are not met, the stack frame structure applicable to the interpreter can be rebuilt and degenerated into the interpreter, which interprets and executes the code in the first function associated with the remaining code of the second function (i.e., the unexecuted code).

[0116] In the embodiment of the present application, it is possible to detect whether the speculative optimization condition is met when executing the second function. When the speculative optimization condition is not met, the stack frame structure can be rebuilt and the interpreter can be called to execute the code associated with the remaining code of the second function in the first function, thereby ensuring the correct operation of the code. In addition, the stack frame structure is a standard stack frame structure, and no special stack frame analyzer is required. There is no need to use a non-standard parameter passing method when calling external code, which is convenient for program transplantation and debugging.

[0117] S405, rebuilding the stack frame structure and calling the interpreter to execute the third function starting from the first part of the third function.

[0118] When the operating environment when executing the second function does not meet the speculative optimization conditions, the stack frame structure can be rebuilt and the interpreter can be called to execute the third function from the first part of the third function user, the first part of the third function is determined according to the unexecuted part of the second function, the stack frame structure is the stack frame result applicable to the interpreter, and the third function is a bytecode function associated with the first function. In other words, when it is determined that the speculative optimization conditions are not met, the stack frame structure applicable to the interpreter can be rebuilt and degenerated into the interpreter, and the code associated with the remaining code of the second function (i.e., the unexecuted code) in the third function can be interpreted and executed by the interpreter.

[0119] In the embodiment of the present application, it is possible to detect whether the speculative optimization condition is met when executing the second function. When the speculative optimization condition is not met, the stack frame structure can be rebuilt and the interpreter can be called to execute the code associated with the remaining code of the second function in the third function, thereby ensuring the correct operation of the code. In addition, the stack frame structure is a standard stack frame structure, and no special stack frame analyzer is required. There is no need to use a non-standard parameter passing method when calling external code, which is convenient for program transplantation and debugging.

[0120] In the embodiment of the present application, the interpreter can be called at the entrance of the compiled function, or when the compiled function is executed. There are differences in the stack frames when the interpreter is called at the above times. Figure 5 Make an introduction.

[0121] Figure 5 A schematic diagram of the stack frame structure of the interpreter provided in an embodiment of the present application is shown.

[0122] like Figure 5 As shown in (a) of FIG. 1 , the stack frame structure of the interpreter includes parameters, local variables and operation data. When the interpreter is called normally, for example, at the function entry, the parameters can be initialized by the caller, and the local variables and operand stack are allocated by the callee, i.e., the interpreter. After completing the above steps, the interpreter starts executing from the program counter (PC) = 0.

[0123] like Figure 5 As shown in (b) in the figure, when the function is degraded to the interpreter during execution, the parameters, local variables and operand stack are allocated by the caller and initialized to the corresponding content executed by the interpreter here, and the PC is set so that the interpreter can start execution from the set PC.

[0124] The interpreter in the embodiment of the present application can be understood as a virtual interpreter maintained by the compiler, including the current operand stack, local variable table, PC, etc. When the second function is executed, it is necessary to degenerate to the interpreter, and the compiler can perform the following steps to make the interpreter execute the corresponding code: allocate the space required for the operand stack and the local variable table, assign the state of the interpreter to the corresponding position of the operand stack and the local variable table, call the degenerate interface of the interpreter, and pass the first function or the third function, the operand stack, the local variables and the PC as parameters to the interpreter, so that the interpreter can execute the remaining code in the first function or the third function. After executing the first function or the third function, the interpreter can jump back to the function exit of the second function so that the execution engine can continue to execute the second code.

[0125] In the embodiment of the present application, when jumping to the interpreter execution function, the corresponding parameters can be passed to the interpreter so that the interpreter can execute the code from the corresponding position. In addition, since the corresponding parameters have been passed to the interpreter, the interpreter can be adapted to the stack frame structure of the standard format created by the compiler, without the need for the developer to customize the stack frame structure, thereby improving the cross-platform capability of the code.

[0126] In the above, when calling a function, that is, at the function entry, it can be detected whether the function information is the same as the function information at the time of advance compilation, or it can be detected whether the operating environment meets the speculative optimization conditions when executing the function. In other embodiments of the present application, when calling a function, it can be detected whether the function information is the same as the function at the time of advance compilation, and in the process of determining that the function information is the same and executing the function, it can be detected whether the operating environment meets the speculative optimization conditions. Figure 6 Provide explanation.

[0127] Figure 6 A schematic flow chart of a method for executing code provided in an embodiment of the present application is shown, as follows: Figure 6 As shown, the method includes:

[0128] S601, obtaining a first code.

[0129] The compiler obtains a first code, which is a source code, and the source code may be a source code written in a dynamic type programming language. The first code includes a first function, and the first function will be called in the speech of the first code.

[0130] S602: Pre-compile the first code to generate a second code.

[0131] After the compiler obtains the first code, it can compile the first code in advance to generate a second code. The second code can be a machine code. The second code includes a second function, which is a corresponding function after compiling the first function.

[0132] In some embodiments, the compiler may perform speculative optimization when compiling the first code in advance.

[0133] In some embodiments, the compiler may compile all functions in the first code.

[0134] In some embodiments, the compiler may not be able to compile all functions in the first code due to reasons such as the functions in the first code being too large.

[0135] S603: Store the second function and function information of the second function during advance compilation.

[0136] After the compiler compiles the function in the first code, it can store the compiled function and the function information during the advance compilation, and the function information includes but is not limited to the parameter type of the function and the function pointer.

[0137] S604: When the second function is called, detect whether the function information of the second function when the second function is called is the same as the function information of the second function when it is compiled in advance.

[0138] The semantics of the first code calls the first function, the first code is compiled to generate the second code, and the second function is the function obtained by pre-compilation of the first function, then the execution engine will call the second function when executing the second code. When the execution engine calls the second function, it can detect whether the function information of the second function at this time is the same as the function information of the second function stored in advance compilation, if the same, execute step S605, if not the same, execute step S606 or execute step S607.

[0139] S605, executing the second function.

[0140] Since the function information of the second function when calling the second function is the same as the function information of the second function when compiled in advance, the execution engine can execute the second function.

[0141] In an embodiment of the present application, the first code can be compiled in advance, and the pre-compiled function and function information can be stored. When the function is called, it can be detected whether the function information is the same as the function information during the pre-compilation. When they are the same, the compiled function can be executed. Since the second function is a machine code, the execution speed of the code can be improved compared to executing the bytecode function through an interpreter.

[0142] S606, calling the interpreter to execute the first function.

[0143] When the second function is called and the function information when it is called is different from the function information when it is compiled in advance, it can be degenerated to the interpreter, and the interpreter interprets and executes the first function.

[0144] In an embodiment of the present application, the first code can be compiled in advance, and the pre-compiled functions and function information can be stored. When the function is called, it can be detected whether the function information is the same as the function information during the pre-compilation. When they are different, the function in the source code can be executed, thereby ensuring the correct execution of the code.

[0145] S607, calling the interpreter to execute the third function.

[0146] The third function is a bytecode function corresponding to the first function. When the second function is called and the function information when called is different from the function information when compiled in advance, it can be degenerated to the interpreter, and the interpreter interprets and executes the third function composed of bytecodes.

[0147] In an embodiment of the present application, the first code can be compiled in advance, and the pre-compiled functions and function information can be stored. When the function is called, it can be detected whether the function information is the same as the function information during pre-compilation. When they are different, the bytecode function corresponding to the function in the source code can be executed, thereby ensuring the correct execution of the code.

[0148] In some embodiments, the first code further includes a fourth function, and there is no function corresponding to the fourth function in the second code. In other words, the compiler does not pre-compile the fourth function.

[0149] Since there is no function corresponding to the fourth function in the second code, it needs to be degenerated into the interpreter for execution.

[0150] In a possible implementation, the interpreter interprets and executes the fourth function.

[0151] In a possible implementation, the interpreter interprets and executes a fifth function, where the fifth function is a bytecode function corresponding to the fourth function.

[0152] In the embodiment of the present application, when there is a function that is not compiled in advance in the first code, an interpreter can be called to execute the function or the bytecode function corresponding to the function. The embodiment of the present application combines machine code execution and interpreter execution, which not only ensures the running efficiency of the code but also ensures the correct execution of the code.

[0153] S608, detecting whether the operating environment when executing the second function satisfies the speculative optimization condition.

[0154] When executing the second function, the execution engine can detect whether the current operating environment meets the speculative optimization conditions. If the compiler performs speculative optimization on the first function during advance compilation, the compiler can determine the speculative optimization conditions corresponding to the second function during advance compilation, and generate corresponding detection code according to the speculative optimization conditions. When it is determined that the current operating environment meets the speculative optimization conditions, step S609 can be executed, and when it is determined that the current operating environment does not meet the speculative optimization conditions, step S610 or step S611 can be executed.

[0155] S610, continue to execute the second function.

[0156] Since the operating environment when executing the second function meets the speculative optimization condition, the second function can continue to be executed.

[0157] In an embodiment of the present application, it is possible to detect whether the speculative optimization conditions are met when executing the second function. When the speculative optimization conditions are met, the second function can continue to be executed. Since the second function is a function composed of machine code, the running efficiency of the code can be effectively improved.

[0158] S611, rebuild the stack frame structure and call the interpreter to execute the first function starting from the first part of the first function.

[0159] When the operating environment when executing the second function does not meet the speculative optimization conditions, the stack frame structure can be rebuilt and the interpreter can be called to execute the first function starting from the first part of the first function. The first part of the first function is determined based on the unexecuted part of the second function. The stack frame structure is the stack frame result applicable to the interpreter.

[0160] In the embodiment of the present application, it is possible to detect whether the speculative optimization condition is met when executing the second function. When the speculative optimization condition is not met, the stack frame structure can be rebuilt and the interpreter can be called to execute the code associated with the remaining code of the second function in the first function, thereby ensuring the correct operation of the code. In addition, the stack frame structure is a standard stack frame structure, and no special stack frame analyzer is required. There is no need to use a non-standard parameter passing method when calling external code, which is convenient for program transplantation and debugging.

[0161] S612, rebuild the stack frame structure and call the interpreter to execute the third function starting from the first part of the third function.

[0162] When the operating environment when executing the second function does not meet the speculative optimization conditions, the stack frame structure can be rebuilt and the interpreter can be called to execute the third function from the first part of the third function user. The first part of the third function is determined based on the unexecuted part of the second function. The stack frame structure is the stack frame result applicable to the interpreter, and the third function is a bytecode function associated with the first function.

[0163] In the embodiment of the present application, it is possible to detect whether the speculative optimization condition is met when executing the second function. When the speculative optimization condition is not met, the stack frame structure can be rebuilt and the interpreter can be called to execute the code associated with the remaining code of the second function in the third function, thereby ensuring the correct operation of the code. In addition, the stack frame structure is a standard stack frame structure, and no special stack frame analyzer is required. There is no need to use a non-standard parameter passing method when calling external code, which is convenient for program transplantation and debugging.

[0164] In the embodiment of the present application, the code generated by the compiler is as follows:

[0165] Assume that the parameter "x" is an integer:

[0166] if (x is not an integer) {

[0167] locals = alloca (number of local variables) / / alloca refers to the allocation of space on the stack

[0168] opstack = alloca (operand stack size)

[0169] locals[0] = local variable 0

[0170] locals[1] = local variable 1 ...

[0172] opstack[0] = operand stack element 0 / / assign value according to the state of virtual interpreter ...

[0174] ip = the instruction that should be executed currently

[0175] result = resume_with_interpreter(func, args, locals, opstack, ...) / / Call the interpreter, pass in the state that the current interpreter should have, and execute until the end of the function

[0176] return result

[0177] }

[0178] / / It has been determined that x is an integer, which meets the speculative optimization conditions. Execute the optimized code:

[0179] x_int = x as int

[0180] y=x_int+1 / / Optimized operation

[0181] / / ...the following procedures

[0182] It should be noted that the code execution method provided in the embodiment of the present application can be applied in an electronic device. Exemplarily, the electronic device can be a portable mobile device such as a mobile phone, a tablet computer, a desktop computer, a personal digital assistant (PDA), an augmented reality (AR), a virtual reality (VR) device, a media player, etc. The electronic device can also be a wearable electronic device such as a smart watch. The embodiment of the present application does not impose any special restrictions on the specific form of the device.

[0183] For example, Figure 71 shows a schematic diagram of the structure of the electronic device 100. The electronic device 100 may include a processor 110, an external memory interface 120, an internal memory 121, a universal serial bus (USB) interface 130, a charging management module 140, a power management module 141, a battery 142, an antenna 1, an antenna 2, a mobile communication module 150, a wireless communication module 160, an audio module 170, a speaker 170A, a receiver 170B, a microphone 170C, an earphone interface 170D, a sensor module 180, a button 190, a motor 191, an indicator 192, a camera 193, a display screen 194, and a subscriber identification module (SIM) card interface 195, etc. The sensor module 180 may include a pressure sensor 180A, a gyroscope sensor 180B, an air pressure sensor 180C, a magnetic sensor 180D, an acceleration sensor 180E, a distance sensor 180F, a proximity light sensor 180G, a fingerprint sensor 180H, a temperature sensor 180J, a touch sensor 180K, an ambient light sensor 180L, a bone conduction sensor 180M, etc.

[0184] It is to be understood that the structure illustrated in the embodiment of the present application does not constitute a specific limitation on the electronic device 100. In other embodiments of the present application, the electronic device 100 may include more or fewer components than shown in the figure, or combine some components, or split some components, or arrange the components differently. The components shown in the figure may be implemented in hardware, software, or a combination of software and hardware.

[0185] The processor 110 may include one or more processing units, for example, the processor 110 may include an application processor (AP), a modem processor, a graphics processor (GPU), an image signal processor (ISP), a controller, a memory, a video codec, a digital signal processor (DSP), a baseband processor, and / or a neural-network processing unit (NPU), etc. Different processing units may be independent devices or integrated into one or more processors.

[0186] The controller may be the nerve center and command center of the electronic device 100. The controller may generate an operation control signal according to the instruction operation code and the timing signal to complete the control of fetching and executing instructions.

[0187] The processor 110 may also be provided with a memory for storing instructions and data. In some embodiments, the memory in the processor 110 is a cache memory. The memory may store instructions or data that the processor 110 has just used or cyclically used. If the processor 110 needs to use the instruction or data again, it may be directly called from the memory. This avoids repeated access, reduces the waiting time of the processor 110, and thus improves the efficiency of the system.

[0188] In some embodiments, the processor 110 may include one or more interfaces. The interface may include an inter-integrated circuit (I2C) interface, an inter-integrated circuit sound (I2S) interface, a pulse code modulation (PCM) interface, a universal asynchronous receiver / transmitter (UART) interface, a mobile industry processor interface (MIPI), a general-purpose input / output (GPIO) interface, a subscriber identity module (SIM) interface, and / or a universal serial bus (USB) interface, etc.

[0189] The wireless communication function of the electronic device 100 can be implemented through the antenna 1, the antenna 2, the mobile communication module 150, the wireless communication module 160, the modem processor and the baseband processor.

[0190] Antenna 1 and antenna 2 are used to transmit and receive electromagnetic wave signals. Each antenna in electronic device 100 can be used to cover a single or multiple communication frequency bands. Different antennas can also be reused to improve the utilization of antennas. For example, antenna 1 can be reused as a diversity antenna for a wireless local area network. In some other embodiments, the antenna can be used in combination with a tuning switch.

[0191] The mobile communication module 150 can provide solutions for wireless communications including 2G / 3G / 4G / 5G, etc., applied to the electronic device 100. The mobile communication module 150 may include at least one filter, a switch, a power amplifier, a low noise amplifier (LNA), etc. The mobile communication module 150 can receive electromagnetic waves from the antenna 1, and filter, amplify, and process the received electromagnetic waves, and transmit them to the modulation and demodulation processor for demodulation. The mobile communication module 150 can also amplify the signal modulated by the modulation and demodulation processor, and convert it into electromagnetic waves for radiation through the antenna 1. In some embodiments, at least some of the functional modules of the mobile communication module 150 can be set in the processor 110. In some embodiments, at least some of the functional modules of the mobile communication module 150 can be set in the same device as at least some of the modules of the processor 110.

[0192] The modem processor may include a modulator and a demodulator. Among them, the modulator is used to modulate the low-frequency baseband signal to be sent into a medium-high frequency signal. The demodulator is used to demodulate the received electromagnetic wave signal into a low-frequency baseband signal. The demodulator then transmits the demodulated low-frequency baseband signal to the baseband processor for processing. After the low-frequency baseband signal is processed by the baseband processor, it is passed to the application processor. The application processor outputs a sound signal through an audio device (not limited to a speaker 170A, a receiver 170B, etc.), or displays an image or video through a display screen 194. In some embodiments, the modem processor may be an independent device. In other embodiments, the modem processor may be independent of the processor 110 and be set in the same device as the mobile communication module 150 or other functional modules.

[0193] The wireless communication module 160 can provide wireless communication solutions including wireless local area networks (WLAN) (such as wireless fidelity (Wi-Fi) networks), bluetooth (BT), global navigation satellite system (GNSS), frequency modulation (FM), near field communication (NFC), infrared (IR), etc., which are applied to the electronic device 100. The wireless communication module 160 can be one or more devices integrating at least one communication processing module. The wireless communication module 160 receives electromagnetic waves via the antenna 2, modulates the frequency of the electromagnetic wave signal and performs filtering, and sends the processed signal to the processor 110. The wireless communication module 160 can also receive the signal to be sent from the processor 110, modulate the frequency of it, amplify it, and convert it into electromagnetic waves for radiation through the antenna 2.

[0194] In some embodiments, the antenna 1 of the electronic device 100 is coupled to the mobile communication module 150, and the antenna 2 is coupled to the wireless communication module 160, so that the electronic device 100 can communicate with the network and other devices through wireless communication technology. The wireless communication technology may include global system for mobile communications (GSM), general packet radio service (GPRS), code division multiple access (CDMA), wideband code division multiple access (WCDMA), time-division code division multiple access (TD-CDMA), long term evolution (LTE), BT, GNSS, WLAN, NFC, FM, and / or IR technology. The GNSS may include a global positioning system (GPS), a global navigation satellite system (GLONASS), a Beidou navigation satellite system (BDS), a quasi-zenith satellite system (QZSS) and / or a satellite based augmentation system (SBAS).

[0195] The electronic device 100 implements the display function through a GPU, a display screen 194, and an application processor. The GPU is a microprocessor for image processing, which connects the display screen 194 and the application processor. The GPU is used to perform mathematical and geometric calculations for graphics rendering. The processor 110 may include one or more GPUs that execute program instructions to generate or change display information.

[0196] The display screen 194 is used to display images, videos, etc. The display screen 194 includes a display panel. The display panel can be a liquid crystal display (LCD), an organic light-emitting diode (OLED), an active-matrix organic light-emitting diode or an active-matrix organic light-emitting diode (AMOLED), a flexible light-emitting diode (FLED), Miniled, MicroLed, Micro-oLed, a quantum dot light-emitting diode (QLED), etc. In some embodiments, the electronic device 100 may include 1 or N display screens 194, where N is a positive integer greater than 1.

[0197] The electronic device 100 can realize the shooting function through ISP, camera 193, video codec, GPU, display screen 194 and application processor.

[0198] ISP is used to process the data fed back by camera 193. For example, when taking a photo, the shutter is opened, and the light is transmitted to the camera photosensitive element through the lens. The light signal is converted into an electrical signal, and the camera photosensitive element transmits the electrical signal to ISP for processing and converts it into an image visible to the naked eye. ISP can also perform algorithm optimization on the noise, brightness, and skin color of the image. ISP can also optimize the exposure, color temperature and other parameters of the shooting scene. In some embodiments, ISP can be set in camera 193.

[0199] The camera 193 is used to capture still images or videos. The object generates an optical image through the lens and projects it onto the photosensitive element. The photosensitive element can be a charge coupled device (CCD) or a complementary metal-oxide-semiconductor (CMOS) phototransistor. The photosensitive element converts the optical signal into an electrical signal, and then passes the electrical signal to the ISP to be converted into a digital image signal. The ISP outputs the digital image signal to the DSP for processing. The DSP converts the digital image signal into an image signal in a standard RGB, YUV or other format. In some embodiments, the electronic device 100 may include 1 or N cameras 193, where N is a positive integer greater than 1.

[0200] The digital signal processor is used to process digital signals, and can process not only digital image signals but also other digital signals. For example, when the electronic device 100 is selecting a frequency point, the digital signal processor is used to perform Fourier transform on the frequency point energy.

[0201] Video codecs are used to compress or decompress digital videos. The electronic device 100 may support one or more video codecs. Thus, the electronic device 100 may play or record videos in a variety of coding formats, such as Moving Picture Experts Group (MPEG) 1, MPEG2, MPEG3, MPEG4, etc.

[0202] NPU is a neural network (NN) computing processor. By drawing on the structure of biological neural networks, such as the transmission mode between neurons in the human brain, it can quickly process input information and can also continuously self-learn. Through NPU, applications such as intelligent cognition of electronic device 100 can be realized, such as image recognition, face recognition, voice recognition, text understanding, etc.

[0203] The external memory interface 120 can be used to connect an external memory card, such as a Micro SD card, to expand the storage capacity of the electronic device 100. The external memory card communicates with the processor 110 through the external memory interface 120 to implement a data storage function, such as storing music, video and other files in the external memory card.

[0204] The internal memory 121 can be used to store computer executable program codes, which include instructions. The processor 110 executes various functional applications and data processing of the electronic device 100 by running the instructions stored in the internal memory 121. The internal memory 121 may include a program storage area and a data storage area. Among them, the program storage area may store an operating system, an application required for at least one function (such as a sound playback function, an image playback function, etc.), etc. The data storage area may store data created during the use of the electronic device 100 (such as audio data, a phone book, etc.), etc. In addition, the internal memory 121 may include a high-speed random access memory, and may also include a non-volatile memory, such as at least one disk storage device, a flash memory device, a universal flash storage (UFS), etc.

[0205] The electronic device 100 can implement audio functions such as music playing and recording through the audio module 170, the speaker 170A, the receiver 170B, the microphone 170C, the headphone jack 170D, and the application processor.

[0206] The audio module 170 is used to convert digital audio information into analog audio signal output, and is also used to convert analog audio input into digital audio signals. The audio module 170 can also be used to encode and decode audio signals. In some embodiments, the audio module 170 can be arranged in the processor 110, or some functional modules of the audio module 170 can be arranged in the processor 110.

[0207] The speaker 170A, also called a "speaker", is used to convert an audio electrical signal into a sound signal. The electronic device 100 can listen to music or listen to a hands-free call through the speaker 170A.

[0208] The receiver 170B, also called a "earpiece", is used to convert audio electrical signals into sound signals. When the electronic device 100 receives a call or voice message, the voice can be received by placing the receiver 170B close to the human ear.

[0209] Microphone 170C, also called "microphone" or "microphone", is used to convert sound signals into electrical signals. When making a call or sending a voice message, the user can speak by putting their mouth close to microphone 170C to input the sound signal into microphone 170C. The electronic device 100 can be provided with at least one microphone 170C. In other embodiments, the electronic device 100 can be provided with two microphones 170C, which can not only collect sound signals but also realize noise reduction function. In other embodiments, the electronic device 100 can also be provided with three, four or more microphones 170C to collect sound signals, reduce noise, identify the sound source, realize directional recording function, etc.

[0210] The pressure sensor 180A is used to sense the pressure signal and can convert the pressure signal into an electrical signal. In some embodiments, the pressure sensor 180A can be set on the display screen 194. There are many types of pressure sensors 180A, such as resistive pressure sensors, inductive pressure sensors, capacitive pressure sensors, etc. The capacitive pressure sensor can be a parallel plate including at least two conductive materials. When a force acts on the pressure sensor 180A, the capacitance between the electrodes changes. The electronic device 100 determines the intensity of the pressure based on the change in capacitance. When a touch operation acts on the display screen 194, the electronic device 100 detects the touch operation intensity according to the pressure sensor 180A. The electronic device 100 can also calculate the touch position based on the detection signal of the pressure sensor 180A. In some embodiments, touch operations acting on the same touch position but with different touch operation intensities can correspond to different operation instructions. For example, when a touch operation with a touch operation intensity greater than or equal to the first pressure threshold acts on the alarm application icon, an instruction to create a new alarm is executed.

[0211] The fingerprint sensor 180H is used to collect fingerprints. The electronic device 100 can use the collected fingerprint characteristics to realize fingerprint unlocking, access application locks, fingerprint photography, fingerprint answering calls, etc. For example, when the mobile phone detects a user's touch operation on the lock screen interface, the mobile phone can collect the user's fingerprint information through the fingerprint sensor 180H, and match the collected fingerprint information with the fingerprint information preset in the mobile phone. If the match is successful, the mobile phone can enter the non-lock screen interface from the lock screen interface.

[0212] The touch sensor 180K is also called a "touch panel". The touch sensor 180K can be set on the display screen 194, and the touch sensor 180K and the display screen 194 form a touch screen, also called a "touch screen". The touch sensor 180K is used to detect touch operations acting on or near it. The touch sensor can pass the detected touch operation to the application processor to determine the type of touch event. Visual output related to the touch operation can be provided through the display screen 194. In other embodiments, the touch sensor 180K can also be set on the surface of the electronic device 100, which is different from the position of the display screen 194.

[0213] The code execution method provided in the embodiments of the present application can be applied to the above Figure 7 It should be noted that, in other embodiments of the present application, the hardware composition of the electronic device 100 may also be different from the hardware composition of the electronic device 100 shown in FIG. Figure 7 For example, the electronic device 100 may include more components, or the electronic device 100 may be simplified as required to support the following functions: Figure 7 Some of the components in the composition shown.

[0214] In some embodiments, a software system may be running in the electronic device 100. For example, the software system may be an Android system, an IOS system, a Linux system, a Windows system, or the like.

[0215] In the embodiments of the present application, the architecture of the software system is not specifically limited. For example, a layered architecture, an event-driven architecture, a microkernel architecture, a microservice architecture, or a cloud architecture may be adopted.

[0216] In order to realize the above functions, it includes hardware structures and / or software modules corresponding to the execution of each function. It should be easily appreciated by those skilled in the art that, in combination with the units and algorithm steps of each example described in the embodiments disclosed herein, the present application can be implemented in the form of hardware or a combination of hardware and computer software. Whether a function is executed in the form of hardware or computer software driving hardware depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of the present application.

[0217] The embodiment of the present application can divide the functional modules of the devices involved therein according to the above method examples. For example, each functional module can be divided corresponding to each function, or two or more functions can be integrated into one processing module. The above integrated modules can be implemented in the form of hardware or in the form of software functional modules. It should be noted that the division of modules in the embodiment of the present application is schematic and is only a logical function division. There may be other division methods in actual implementation.

[0218] Figure 8 FIG. 8 shows an electronic device 800 provided in an embodiment of the present application. For example, the hardware components of the electronic device may include: Figure 7 Composition shown.

[0219] like Figure 8 As shown, the electronic device 800 includes an acquisition unit 810 and a processing unit 820, wherein the acquisition unit 810 is used to acquire the first code. The processing unit 820 is used to: compile the first code in advance to generate a second code, the second code includes a second function, the second function is a function after the first function is compiled, and the second code is a machine code; store the second function and the first function information, the first function information is the function information of the second function when the advance compilation is performed, and the function information includes at least one of the following: parameter type in the function, function pointer; when calling the second function, detect whether the second function information is the same as the first function information, the second function information is the function information of the second function when calling the second function; if the first function information is the same as the second function information, execute the second function.

[0220] In some embodiments, the processing unit 820 is further configured to call the interpreter to execute the first function if the first function information is different from the second function information.

[0221] In some embodiments, the processing unit 820 is further used to call the interpreter to execute a third function if the first function information is different from the second function information, where the third function is a bytecode function corresponding to the first function.

[0222] In some embodiments, the first code also includes a fourth function, and the second code does not include a function corresponding to the fourth function. The processing unit 820 is further used to call the interpreter to execute the fourth function.

[0223] In some embodiments, the first code also includes a fourth function, and the second code does not include a function corresponding to the fourth function. The processing unit 820 is also used to call the interpreter to execute a fifth function, and the fifth function is a bytecode function corresponding to the fourth function.

[0224] In some embodiments, the processing unit 820 is further configured to detect whether a speculative optimization condition is satisfied during the execution of the second function; if the speculative optimization condition is satisfied, the second function continues to be executed.

[0225] In some embodiments, the processing unit 820 is further used to rebuild the stack frame structure and call the interpreter to execute the first function from the first part of the first function if the speculative optimization condition is not met, and the first part of the first function is determined based on the unexecuted part of the second function.

[0226] In some embodiments, the processing unit 820 is also used to rebuild the stack frame structure and call the interpreter to execute the third function from the first part of the third function if the speculative optimization conditions are not met. The third function is a bytecode function corresponding to the first function, and the first part of the third function is determined based on the unexecuted part of the second function.

[0227] In some embodiments, the processing unit 820 is specifically used to allocate the space required for the operand stack and the variable table; assign the state of the interpreter to the corresponding positions of the operand stack and the variable table; and pass the third function, the operand stack, the local variables and the program counter as parameters to the interpreter.

[0228] In some embodiments, the processing unit 820 is further used to instruct the interpreter to jump to the exit of the second function after returning.

[0229] In some embodiments, the acquisition unit 810 is used to acquire a first code, the first code includes a first function. The processing unit 810 is used to compile the first code in advance to generate a second code, the second code includes a second function, and the second function is a function after the first function is compiled; when the first function is called, the second function is executed; when executing the second function, whether a speculative optimization condition is met; if the speculative optimization condition is met, the second function is continued to be executed.

[0230] In some embodiments, the processing unit 820 is further used to rebuild the stack frame structure and call the interpreter to execute the first function from the first part of the first function if the speculative optimization condition is not met, and the first part of the first function is determined based on the unexecuted part of the second function.

[0231] In some embodiments, the processing unit 820 is further used to rebuild the stack frame structure and call the interpreter to execute the first function starting from the first part of the third function if the speculative optimization conditions are not met, and the first part of the third function is determined based on the unexecuted part of the second function.

[0232] In some embodiments, the processing unit 820 is specifically used to allocate the space required for the operand stack and the variable table; assign the state of the interpreter to the corresponding positions of the operand stack and the variable table; and pass the third function, the operand stack, the local variables and the program counter as parameters to the interpreter.

[0233] In some embodiments, the processing unit 820 is further used to instruct the interpreter to jump to the exit of the second function after returning.

[0234] The embodiment of the present application provides a computer program product, when the computer program product is run on an electronic device, the electronic device executes the technical solution in the above embodiment. Its implementation principle and technical effect are similar to those of the above method-related embodiments, and will not be repeated here.

[0235] The embodiment of the present application provides a readable storage medium, the readable storage medium contains instructions, when the instructions are executed in an electronic device, the electronic device executes the technical solution of the above embodiment. The implementation principle and technical effect are similar, and will not be repeated here.

[0236] The embodiment of the present application provides a chip, the chip is used to execute instructions, when the chip is running, the technical solution in the above embodiment is executed. The implementation principle and technical effect are similar, and will not be repeated here.

[0237] Those of ordinary skill in the art will appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of the embodiments of the present application.

[0238] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working processes of the systems, devices and units described above can refer to the corresponding processes in the aforementioned method embodiments and will not be repeated here.

[0239] In the several embodiments provided in the present application, it should be understood that the disclosed systems, devices and methods can be implemented in other ways. For example, the device embodiments described above are only schematic. For example, the division of the units is only a logical function division. There may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.

[0240] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.

[0241] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit.

[0242] If the function is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the embodiment of the present application is essentially or the part that contributes to the prior art or the part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions to enable a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present application. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM), random access memory (RAM), disk or optical disk and other media that can store program code.

[0243] The above is only a specific implementation of the embodiment of the present application, but the protection scope of the embodiment of the present application is not limited thereto. Any technician familiar with the technical field can easily think of changes or replacements within the technical scope disclosed in the embodiment of the present application, which should be included in the protection scope of the embodiment of the present application. Therefore, the protection scope of the embodiment of the present application should be based on the protection scope of the claims.

Claims

1. A method for executing code, characterized in that: The method comprises: Obtain a first code, where the first code includes a first function and the first code is a source code; Precompile the first code to generate a second code, where the second code includes a second function, the second function is a function obtained by compiling the first function, and the second code is a machine code; storing the second function and the first function information, wherein the first function information is the function information of the second function when the advance compilation is performed, and the function information includes at least one of the following: a parameter type in a function and a function pointer; When calling the second function, detecting whether the second function information is the same as the first function information, the second function information being the function information of the second function when calling the second function; If the first function information is the same as the second function information, the second function is executed.

2. The method according to claim 1, characterized in that The method further comprises: If the first function information is different from the second function information, the interpreter is called to execute the first function.

3. The method according to claim 1, characterized in that The method further comprises: If the first function information is different from the second function information, the interpreter is called to execute a third function, where the third function is the bytecode function corresponding to the first function.

4. The method according to any one of claims 1 to 3, characterized in that The first code further includes a fourth function, the second code does not include a function corresponding to the fourth function, and the method further includes: The interpreter is called to execute the fourth function.

5. The method according to any one of claims 1 to 3, characterized in that The first code further includes a fourth function, the second code does not include a function corresponding to the fourth function, and the method further includes: The interpreter is called to execute a fifth function, where the fifth function is a bytecode function corresponding to the fourth function.

6. The method according to any one of claims 1 to 5, characterized in that The method further comprises: During the execution of the second function, detecting whether a speculative optimization condition is satisfied; If the speculative optimization condition is met, the second function continues to be executed.

7. The method according to claim 6, characterized in that The method further comprises: If the speculative optimization condition is not met, the stack frame structure is rebuilt and the interpreter is called to execute the first function starting from the first part of the first function, where the first part of the first function is determined based on the unexecuted part of the second function.

8. The method according to claim 7, characterized in that The method further comprises: If the speculative optimization condition is not met, the stack frame structure is rebuilt and the interpreter is called to execute the third function from the first part of the third function, where the third function is the bytecode function corresponding to the first function, and the first part of the third function is determined based on the unexecuted part of the second function.

9. The method according to claim 8, characterized in that The calling interpreter starts to execute the third function from the first part of the third function, including: Allocate the space required for the operand stack and variable table; Assigning the state of the interpreter to corresponding positions of the operand stack and the variable table; The third function, operand stack, local variables and program counter are passed as parameters to the interpreter.

10. The method according to claim 9, characterized in that The method further comprises: Instruct the interpreter to jump to the exit of the second function after returning.

11. A method for executing code, characterized in that: The method comprises: Obtaining a first code, wherein the first code includes a first function; Pre-compile the first code to generate a second code, where the second code includes a second function, and the second function is a function obtained by compiling the first function; When the first function is called, executing the second function; When executing the second function, checking whether the speculative optimization condition is met; If the speculative optimization condition is met, the second function continues to be executed.

12. The method according to claim 11, characterized in that The method further comprises: If the speculative optimization condition is not met, the stack frame structure is rebuilt and the interpreter is called to execute the first function starting from the first part of the first function, where the first part of the first function is determined based on the unexecuted part of the second function.

13. The method according to claim 11, characterized in that The method further comprises: If the speculative optimization condition is not met, the stack frame structure is rebuilt and the interpreter is called to execute the third function from the first part of the third function, where the first part of the third function is determined based on the unexecuted part of the second function.

14. The method according to claim 13, characterized in that The calling interpreter starts to execute the third function from the first part of the third function, including: Allocate the space required for the operand stack and variable table; Assigning the state of the interpreter to corresponding positions of the operand stack and the variable table; The third function, operand stack, local variables and program counter are passed as parameters to the interpreter.

15. The method according to claim 14, characterized in that The method further comprises: Instruct the interpreter to jump to the exit of the second function after returning.

16. An electronic device, characterized in that: The method comprises one or more processors; one or more memories; the one or more memories store one or more computer programs, the one or more computer programs include instructions, and when the instructions are executed by the one or more processors, the method according to any one of claims 1 to 15 is executed.

17. A chip, characterized in that: The chip includes a processor and a communication interface, wherein the communication interface is used to receive a signal and transmit the signal to the processor, and the processor processes the signal so that the method according to any one of claims 1 to 15 is executed.

18. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer instructions, and when the computer instructions are executed on a computer, the method according to any one of claims 1 to 15 is executed.