Bytecode operation type marking method and device and electronic equipment
By obtaining and analyzing the type information of the bytecode during the static compilation of the dynamically typed language, and determining the operation type of the bytecode, the problem of difficult type marking in the dynamically typed language is solved, and the operating efficiency and compatibility are improved.
Patent Information
- Application Number
- CN202310353125.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-03-30
- Publication Date
- 2024-10-01
AI Technical Summary
In programs written in dynamically typed languages, the operation type of bytecode cannot be determined through type tags, resulting in low program running efficiency and limited expression capabilities of dynamically typed languages, resulting in poor compatibility.
During the static compilation of a dynamically typed language, we obtain the type information of the target data in each function corresponding to the bytecode, establish a control flow graph and a data flow graph, and use the union lookup algorithm and simulate the running process to determine the bytecode Operation type to avoid type checking and improve operating efficiency.
It achieves accurate marking of bytecode operation types, improves the compatibility and operating efficiency of dynamically typed languages, and avoids the overhead of type checking.
Smart Images

Figure CN118733040A_ABST
Abstract
Description
Technical Field
[0001] The embodiments of the present application relate to the field of computer technology, and in particular to a method, device and electronic device for marking the operation type of a bytecode. Background Art
[0002] When a virtual machine runs a program written in a programming language or a scripting language, it can perform type checking on the program to read type information of parameters, variables, etc. in the program. The virtual machine can perform subsequent calculations based on the type information of parameters, variables, etc. in the program.
[0003] In programs written in dynamically typed languages, type marking is not required in the code, so the type of bytecode operation cannot be determined by type marking, and therefore the type information of formal parameters, variables, etc. in the program cannot be determined based on the type of bytecode operation. Currently, when writing programs, some dynamic characteristics of programming languages or scripting languages can be restricted, making it easier to infer the types of variables in the program, and then infer the type of bytecode operation, but this method will limit the expressive power of dynamically typed languages and may also cause language incompatibility issues. Summary of the invention
[0004] The embodiments of the present application provide a method, device, and electronic device for marking the operation type of a bytecode, which can achieve the purpose of accurately marking the operation type of the bytecode without restricting the dynamic characteristics of the dynamic type language.
[0005] In a first aspect, an embodiment of the present application provides a method for marking the operation type of a bytecode, and the execution subject of the method may be an electronic device or a chip in the electronic device, and the following description is made using an electronic device as an example. It should be understood that a compiler may be deployed in the electronic device to implement marking the operation type of the bytecode during the compilation of a dynamic type language.
[0006] In this method, the electronic device can obtain the bytecode, and after obtaining the bytecode, can obtain the type information of the target data in each function corresponding to the bytecode, so as to determine the first operation type of the bytecode according to the type information of the target data in each function, and the electronic device can mark the operation type of the bytecode according to the first operation type. In a possible implementation, the target data includes at least one of the following: function parameters, function return values, global variables, and variables in closures.
[0007] In a possible implementation, the bytecode is obtained from source code written in a dynamic type language.
[0008] Exemplarily, the electronic device may receive source code, which may be written by a developer and input into the electronic device, or sent to the electronic device by other electronic devices, and this application does not limit this. After receiving the source code, the electronic device may convert the source code into the bytecode.
[0009] In an embodiment of the present application, the electronic device can mark the operation type of the bytecode during compilation, which can avoid the problem of low program operation efficiency caused by the electronic device performing type checking during program execution. In addition, the electronic device can obtain the operation type of the bytecode based on the type information of the target data in each function corresponding to the bytecode, which can not only accurately determine the operation type of the bytecode, but also eliminate the need to pre-restrict the dynamic characteristics of the dynamic type language (such as a programming language or a scripting language), thereby improving the compatibility of the dynamic type language.
[0010] In a possible implementation, the electronic device may also determine the second operation type of the bytecode during the execution of the bytecode, and obtain the operation type of the bytecode based on the first operation type and the second operation type; and then mark the operation type of the bytecode based on the operation type of the bytecode.
[0011] In this implementation, the electronic device may be unable to determine or accurately determine the operation type of the bytecode in some cases based on the type information of the target data in each function corresponding to the bytecode. In an embodiment of the present application, the electronic device can also determine the second operation type of the bytecode during the execution of the bytecode, and merge the first operation type and the second operation type to mark the operation type of the bytecode, which can not only supplement the deficiencies of the method of obtaining the operation type of the bytecode based on the type information of the target data in each function corresponding to the bytecode, but also improve the accuracy of the determined operation type of the bytecode.
[0012] The following describes a method for an electronic device to obtain type information of target data in each function corresponding to a bytecode:
[0013] First, after acquiring the bytecode, the electronic device can establish a control flow graph and a data flow graph of the bytecode, and in the process of establishing the control flow graph and the data flow graph, a union-find algorithm is used to group together the target data sets that appear in the operands of the same bytecode to obtain at least one first set. The electronic device can determine the type of target data of unknown type based on the type of target data of known type in each first set to obtain type information of the target data of unknown type. Exemplarily, the electronic device can use the union of the types of target data of known types in the first set as the type of the target data of unknown type, and the type information of the target data of unknown type includes the type of the target data of unknown type.
[0014] Secondly, the electronic device can also infer the type of target data associated with the bytecode according to the type of the bytecode. It should be understood that the bytecode obtained by the electronic device can be regarded as a whole, and the bytecode can include multiple bytecodes, such as each function can correspond to at least one line of bytecode, and each line of bytecode can include multiple bytecodes.
[0015] Here, taking the example that the bytecode includes a first bytecode and a second bytecode, the electronic device can determine the logical association between the first bytecode and the second bytecode, and determine the type of the second bytecode of an unknown type based on the type of the first bytecode of a known type, and then determine the type information of the target data associated with the second bytecode based on the type of the second bytecode.
[0016] Exemplarily, the electronic device may use the type of the first bytecode of a known type as the type of the second bytecode of an unknown type, and use the type of the second bytecode as the type of target data associated with the second bytecode, and the type information of the target data associated with the second bytecode may include the type of the target data associated with the second bytecode.
[0017] Exemplarily, when the output of the first bytecode is the input of the second bytecode, and when the first bytecode and the second bytecode meet a preset condition, the first bytecode and the second bytecode are grouped together to obtain at least one second set. The electronic device can determine the type of the unknown type of the second bytecode based on the type of the first bytecode of the known type in each second set. Exemplarily, the electronic device can use the union of the types of the first bytecode of the known type in the first set as the type of the second bytecode of the unknown type.
[0018] The preset condition is that neither the first bytecode nor the second bytecode is a comparison instruction, because the type of the comparison instruction is Boolean type.
[0019] Third, the electronic device can also simulate the running process of the bytecode to establish the relationship between the operands and operation results of the bytecode, and then determine the type of target data in the bytecode, and then determine the type information of the target data in each function corresponding to the bytecode.
[0020] In the present application, the electronic device can adopt the above multiple methods to obtain the type information of the target data in each function, and can obtain accurate type information of the target data, and further obtain accurate operation type of the bytecode.
[0021] The following is a description of a method for the electronic device to determine the first operation type of the bytecode according to the type information of the target data in each function:
[0022] First, the type information of the target data in each function includes: the type of the target data in each function. The electronic device may use the union of the types of the target data in each function as the first operation type.
[0023] Second, the type information of the target data in each function includes: the type of the target data in each function, and each function corresponds to at least one line of bytecode. The electronic device can use the union of the types of the target data in the function corresponding to each line of bytecode as the operation type of each line of bytecode, and the first operation type includes the operation type of at least one line of bytecode corresponding to each function.
[0024] In the present application, the electronic device can obtain the accurate operation type of the bytecode based on the accurately determined type information of the target data in each function.
[0025] The following is a description of a method for an electronic device to determine the second operation type of the bytecode during the running of the bytecode:
[0026] Each time the electronic device runs the bytecode, it can call different functions corresponding to the bytecode. Therefore, the electronic device can obtain the operation information of the bytecode corresponding to the called function during the running of the bytecode, and the operation information includes the type of operand and the type of operation result. The electronic device can obtain the operation type of the bytecode corresponding to the called function according to the operation information of the bytecode corresponding to the called function.
[0027] When an electronic device runs a bytecode, it can accurately determine the operation type of the bytecode based on the operation information of the bytecode corresponding to the called function, without using an inference method to infer the operation type of the bytecode. In this implementation, the accuracy of the bytecode operation type is high.
[0028] In one possible implementation, for the same function, each time the function is called, the operation information of the bytecode corresponding to the function may be different, so the operation information of the bytecode corresponding to the called function includes at least two versions, and different versions of operation information can represent different operation information of the bytecode corresponding to the called function.
[0029] The electronic device may obtain the proportion of the operation information of each version of the bytecode corresponding to the called function, and obtain the operation type of the bytecode corresponding to the called function based on the proportion.
[0030] In this implementation, the electronic device can obtain operation information of different versions of the bytecode corresponding to the called function, and thus obtain a more comprehensive and accurate operation type of the bytecode.
[0031] In a possible implementation, the electronic device may use the union of the operation types corresponding to the top N operation information as the operation type of the bytecode corresponding to the called function, where N is an integer greater than or equal to 1. In a possible implementation, the electronic device may use the union of the operation types of all versions of the bytecode corresponding to the called function as the operation type of the bytecode corresponding to the called function.
[0032] In a possible implementation, after obtaining the first operation type of the bytecode and the second operation type of the bytecode, the electronic device may use the union of the first operation type and the second operation type as the operation type of the bytecode. In a possible implementation, for the same bytecode, the first operation type and the second operation type of the bytecode include operation types of different versions, so that the electronic device can obtain the proportion of operation types of different versions of the bytecode, and then use the union of operation types corresponding to the top N operation information ranked by proportion as the operation type of the bytecode.
[0033] In a possible implementation, the operation type of the bytecode may include at least one version. After marking the operation type of the bytecode, the electronic device may also generate at least one version of binary code according to the marked bytecode. In this way, the electronic device may run the binary code according to the at least one version of the binary code to implement the operation of the program.
[0034] In this implementation, the electronic device can obtain at least one version of binary code, and can achieve the purpose of accurately running the program.
[0035] In a second aspect, an embodiment of the present application provides a device for marking the operation type of a bytecode, which can be an electronic device or a chip in an electronic device, and can be used to execute the method in the first aspect. The device for marking the operation type of a bytecode can include: a processing module and a marking module.
[0036] The processing module is used to obtain the bytecode, obtain the type information of the target data in each function corresponding to the bytecode, and determine the first operation type of the bytecode according to the type information of the target data in each function.
[0037] A marking module is used to mark the operation type of the bytecode according to the first operation type.
[0038] In a possible implementation, the processing module is specifically used to determine the second operation type of the bytecode during the execution of the bytecode; obtain the operation type of the bytecode according to the first operation type and the second operation type; and mark the operation type of the bytecode according to the operation type of the bytecode.
[0039] In a possible implementation, the target data includes at least one of the following: a function parameter, a function return value, a global variable, and a variable in a closure.
[0040] In one possible implementation, the processing module is specifically used to establish a control flow graph and a data flow graph for the bytecode; in the process of establishing the control flow graph and the data flow graph, a union-find algorithm is used to set together the target data groups that appear in the operands of the same bytecode to obtain at least one first set; based on the known type of target data in each first set, the type of the unknown type of target data is determined to obtain the type information of the unknown type of target data.
[0041] In a possible implementation manner, the bytecode includes a first bytecode and a second bytecode.
[0042] The processing module is specifically used to determine the logical association relationship between the first bytecode and the second bytecode; determine the type of the second bytecode of unknown type according to the type of the first bytecode of known type; and determine the type information of the target data associated with the second bytecode according to the type of the second bytecode.
[0043] In a possible implementation, when the output of the first bytecode is the input of the second bytecode, and when the first bytecode and the second bytecode meet a preset condition, the processing module is further used to group the first bytecode and the second bytecode together to obtain at least one second set.
[0044] In a possible implementation, the preset condition is: neither the first bytecode nor the second bytecode is a comparison instruction.
[0045] In a possible implementation, the processing module is specifically used to simulate the running process of the bytecode to obtain the type information of the target data in each function.
[0046] In a possible implementation manner, the type information of the target data in each function includes: the type of the target data in each function.
[0047] The processing module is specifically configured to use the union of the types of the target data in each function as the first operation type.
[0048] In a possible implementation manner, the type information of the target data in each function includes: the type of the target data in each function, and each function corresponds to at least one line of bytecode.
[0049] The processing module is specifically used to use the union of the types of target data in the function corresponding to each line of bytecode as the operation type of each line of bytecode, and the first operation type includes the operation type of at least one line of bytecode corresponding to each function.
[0050] In one possible implementation, the processing module is specifically used to obtain operation information of the bytecode corresponding to the called function during the execution of the bytecode, the operation information including the type of operand and the type of operation result; and obtain the operation type of the bytecode corresponding to the called function based on the operation information of the bytecode corresponding to the called function.
[0051] In a possible implementation, the operation information of the bytecode corresponding to the called function includes at least two versions. The processing module is specifically used to obtain the proportion of the operation information of each version of the bytecode corresponding to the called function; and according to the proportion, obtain the operation type of the bytecode corresponding to the called function.
[0052] In a possible implementation, the processing module is specifically used to use the union of operation types corresponding to the top N operation information as the operation type of the bytecode corresponding to the called function, where N is an integer greater than or equal to 1.
[0053] In a possible implementation, the processing module is specifically configured to use a union of the first operation type and the second operation type as the operation type of the bytecode.
[0054] In a possible implementation, the bytecode operation type includes at least one version. The bytecode operation type marking device may also include a binary code generation module.
[0055] The binary code generation module is used to generate at least one version of binary code according to the marked bytecode.
[0056] In a possible implementation, the bytecode operation type marking device may further include a transceiver module and a bytecode generation module, wherein the transceiver module is used to receive the source code and the bytecode generation module is used to convert the source code into the bytecode.
[0057] In a possible implementation, the bytecode is obtained from source code written in a dynamic type language.
[0058] In a third aspect, an embodiment of the present application provides an electronic device, which may include: a processor and a memory. The memory is used to store computer executable program code, and the program code includes instructions; when the processor executes the instructions, the instructions cause the electronic device to execute the method in the first aspect.
[0059] In a fourth aspect, an embodiment of the present application provides an electronic device, which may be the marking device for the operation type of the bytecode in the second aspect or the electronic device in the first aspect. The electronic device may include a unit, module or circuit for executing the method provided in the first aspect above.
[0060] In a fifth aspect, an embodiment of the present application provides a computer program product comprising instructions, which, when executed on a computer, enables the computer to execute the method in the first aspect.
[0061] In a sixth aspect, an embodiment of the present application provides a computer-readable storage medium, wherein instructions are stored in the computer-readable storage medium, and when the computer-readable storage medium is run on a computer, the computer executes the method in the above-mentioned first aspect.
[0062] The beneficial effects of the possible implementation methods of the second to sixth aspects mentioned above can be referred to the beneficial effects brought about by the first aspect mentioned above, and will not be elaborated here.
[0063] The embodiment of the present application provides a method, device and electronic device for marking the operation type of a bytecode, in which the electronic device can obtain the bytecode, obtain the type information of the target data in each function corresponding to the bytecode, and determine the first operation type of the bytecode according to the type information of the target data in each function, and mark the operation type of the bytecode according to the first operation type. The present application can determine the operation type of the bytecode corresponding to the function by the type information of the target data in the bytecode in units of functions during static compilation. The embodiment of the present application does not need to limit the dynamic characteristics of the dynamic type language on the one hand, and can also achieve the purpose of accurately marking the operation type of the bytecode on the other hand. BRIEF DESCRIPTION OF THE DRAWINGS
[0064] Figure 1 A flowchart for static compilation;
[0065] Figure 2 A schematic diagram of a static compilation process provided by an embodiment of the present application;
[0066] Figure 3 A flowchart of an embodiment of a method for marking the operation type of a bytecode provided in an embodiment of the present application;
[0067] Figure 4A schematic diagram of the source code and bytecode of the addition function provided in an embodiment of the present application;
[0068] Figure 5 A schematic diagram of a flow chart of the operation of a simulated addition function provided in an embodiment of the present application;
[0069] Figure 6 A schematic diagram of the source code and bytecode of the Fibonacci function provided in an embodiment of the present application;
[0070] Figure 7 A schematic diagram of the type of bytecode of the Fibonacci function provided in an embodiment of the present application;
[0071] Figure 8 A process for marking the operation type of bytecodes for an electronic device provided in an embodiment of the present application;
[0072] Fig. 9 A schematic diagram of a flow chart of a function call of an electronic device provided in an embodiment of the present application;
[0073] Fig.10 Another schematic diagram of a flow chart of calling a function of an electronic device provided in an embodiment of the present application;
[0074] Fig.11 A flowchart of another embodiment of a method for marking the operation type of a bytecode provided in an embodiment of the present application;
[0075] Fig.12 A schematic diagram of generating different versions of binary codes provided in an embodiment of the present application;
[0076] Fig.13 Another schematic diagram of the structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0077] Definitions of terms used in the embodiments of this application:
[0078] Source code: a source program written in a programming language or a scripting language. In one embodiment, the programming language or the scripting language may be referred to as a high-level language.
[0079] Source code cannot be recognized and executed by the computer. Before execution, the source code can be translated into machine language code. According to different translation methods, execution is divided into two types: compiled execution and interpreted execution. Compiled execution refers to the process of compiling the source code into a target program through a compiler, and then running the target program by a computer. The main difference between interpreted execution and compiled execution is that the interpreter does not generate target code during translation, and the interpreter executes the intermediate code while translating the source code. Exemplarily, when reading the source program, the interpreter will first call the language analysis program to perform lexical analysis and partial syntax check, establish a mnemonic table, and convert the source program into intermediate code. The interpreter then calls the interpreted execution program to perform syntax check and interpret the intermediate code line by line. In short, the interpreter can read and translate the statements in the source program line by line, and execute the translated code line by line.
[0080] Interpreter: It can directly interpret and execute source code without translating the source code into a target program in advance. The functions of the interpreter can refer to the relevant description of the interpreter in "Interpretation and Execution".
[0081] Bytecode: Intermediate code generated after syntax analysis of source code.
[0082] Dynamic type language: In the embodiments of the present application, it may include but is not limited to Python, Javascript (JS), Ruby, Perl, PHP, R, Dart, Lua, Objective-C. It should be understood that the following embodiments are illustrated by taking JS as a dynamic type language.
[0083] Compiler: is a program that translates "a language (such as a high-level language)" into "another language (such as a low-level language)". In one embodiment, the compiler can be carried in hardware and run, and the hardware carrying the compiler can be called a compilation tool or a compilation device. The electronic device described in the following embodiments can carry a compiler, and the electronic device can implement static compilation of a dynamic type language.
[0084] Virtual machine (VM): refers to a complete computer system with complete hardware system functions that is simulated by software and runs in a completely isolated environment. In the embodiment of the present application, the virtual machine in the electronic device can run programs.
[0085] Type checking: It is the process of checking the type constraints of values and expressions in a program to ensure that the program behavior is well-defined. For example, for var a=1, a is a variable and 1 is a value. Here, well-defined does not mean that the program has no bugs at all, but that the programming language will define a set of program exception errors at the beginning of its design (different programming languages have different definitions), and as long as the program does not have these errors, it is considered to be well-defined.
[0086] In one embodiment, type checking may include checking the types of constants, variables, arrays, lists, objects, etc. in a program. In one embodiment, constants, variables, arrays, lists, objects, values, and expressions, etc. in a program may be referred to as data in the program, and the types of constants, variables, arrays, lists, objects, etc. in a program may be referred to as data types.
[0087] In one embodiment, the data type may include, but is not limited to: integer type (int), string type, single-precision type / floating-point type (float), double-precision type (double), and Boolean type (bool), etc.
[0088] Type tagging: For example, marking the types of constants, variables, arrays, lists, objects, etc. in a program so that the virtual machine can determine the types of constants, variables, arrays, lists, objects, etc. in the program when running the program.
[0089] Bytecode operation type: It can formally describe how each step of the calculation runs in the computer system. In one embodiment, the bytecode operation type is similar to the concept of operational semantics. For example, the bytecode is "A+B", where A is a string type and B is a string type, then the bytecode operation type is "string type addition". For example, the bytecode is "string.split()", and the bytecode operation type is "function call".
[0090] Formal parameters: The parameters included in a function are called formal parameters. Actual parameters refer to the values actually assigned when the function is called.
[0091] Global variables: In JS, global variables can be created by a function or anywhere else in the program. Global variables can be referenced by all objects or functions in the program.
[0092] Closure: can be seen as a combination of functions and variables.
[0093] Static compilation of dynamically typed languages can be understood as compiling the code written in the dynamically typed languages into binary executable files or binary codes. Static compilation of dynamically typed languages has a wide range of applications and has the following advantages: First, it can greatly simplify the deployment of programs. Second, it can more conveniently call external native function interfaces. Third, compared with the just-in-time (JIT) execution engine, the program performance is more predictable. Fourth, compared with the non-JIT execution engine, the execution efficiency is higher.
[0094] The type tag in the program is an important basis for determining the operation type of the bytecode. However, for programs written in dynamic type languages, type tags are not required in the code. Therefore, for programs written in dynamic type languages, the operation type of the bytecode cannot be determined by type tags. At present, when a virtual machine runs a program written in a dynamic type language, it can perform dynamic type checking to obtain type information of parameters, variables, etc. in the program. The virtual machine can perform subsequent calculations based on the type information of parameters, variables, etc. in the program.
[0095] In order to improve the running efficiency of the program, static type checking or partial static type checking can be performed during the static compilation of the dynamically typed language, which can reduce the overhead of type checking when the program is running.
[0096] In one embodiment, the Hindley-Milner type system can be used to type-mark a program. Exemplarily, the Hindley-Milner type system can use the types of values of known types in a program to infer the types of variables of unknown types in the program, but the use of this type system has the problem of not being able to accurately determine the types of variables.
[0097] For example, for the calculation formula "A+B=C", assuming that the type of C is a string type, the Hindley-Milner type system can be used to infer that A can be a string type and B can be an integer (int), or that A can be an integer and B can be a string type, but it is impossible to accurately determine the specific type of A and the specific type of B. The types of variables inferred by the Hindley-Milner type system have the problem of over-generalization and cannot accurately determine the types of variables.
[0098] In one embodiment, some dynamic characteristics of the dynamic type language can also be restricted, so that the type of the variable in the program is easier to guess, but this method will limit the expressive power of the dynamic type language, and there will be language incompatibility issues. Exemplarily, A is assigned a value of 1, and A is pre-restricted to an integer. Then, when the program is running, it can determine that A is an integer based on the pre-restrictions on the dynamic type language. In one embodiment, for example, type conversion, read-only attributes, prohibition of string assignment through index, single-state mutable variables, etc. can also be restricted to limit some dynamic characteristics of the dynamic type language, so that the type of the variable can be determined more easily.
[0099] In an embodiment of the present application, in order to improve the running efficiency of the program, a method for marking the operation type of bytecode can be provided. During the static compilation of a dynamic type language, the operation type of the bytecode corresponding to the function can be determined on a function-by-function basis through the type information of the target data in the bytecode. On the one hand, the embodiment of the present application does not need to limit the dynamic characteristics of the dynamic type language, and on the other hand, the method can also achieve the purpose of accurately marking the operation type of the bytecode.
[0100] It should be understood that the execution subject of the method for executing the operation type of the bytecode provided in the embodiment of the present application can be an electronic device, and the electronic device can be a device deployed with a compiler. Among them, the electronic device can mark the operation type of the bytecode during the static compilation of the dynamic type language. Exemplarily, the electronic device can be a computer, a mobile phone, a tablet computer (portable android device, PAD), a personal digital assistant (personal digital assistant, PDA), a handheld device with wireless communication function, a computing device, etc., and the form of the electronic device is not specifically limited in the embodiment of the present application.
[0101] Before introducing the method for marking the operation type of the bytecode provided in the embodiment of the present application, first Figure 1 The static compilation process of a dynamic type language is introduced. The static compilation process of a dynamic type language can be understood as the process of an electronic device compiling a dynamic type language into another language. It should be understood that Figure 1 In the example of JS as a dynamic type language, and the example of an electronic device compiling JS into a machine code wasm code, the static compilation process of the dynamic type language is explained. In one embodiment, the wasm code can be regarded as a binary code.
[0102] Reference Figure 1In one embodiment, the electronic device may include: a bytecode generation module, a virtual code generation module, and a machine code generation module. The bytecode generation module, the virtual code generation module, and the machine code generation module may cooperate with each other to implement static compilation of dynamic type languages. In one embodiment, the bytecode generation module, the virtual code generation module, and the machine code generation module may form a compiler.
[0103] In one embodiment, the bytecode generation module may be a QuickJS frontend, the virtual code generation module may be a CodeGen module, and the machine code generation module may be a LLVM backend.
[0104] In an embodiment of the present application, a developer can write source code (JS code) in a dynamic type language JS, and input the JS code into an electronic device for compilation. Among them, the bytecode generation module, in response to the JScode input into the electronic device, can call a language analysis program to perform lexical analysis and partial syntax checking on the JS code to convert the JS code into bytecode. The virtual code generation module can generate virtual code based on the bytecode, such as LLVM intermediate representation (IR). The machine code generation module can convert LLVM IR into wasm code. It should be understood that the specific principles and methods of converting JS code into bytecode, bytecode into LLVM IR, and LLVM IR into wasm code in the embodiment of the present application are not repeated.
[0105] In the embodiment of the present application, in order to improve the running efficiency of the program, the operation type of the bytecode can be marked during the static compilation process of the dynamic type language. Figure 2 , the electronic device may also include: an operation type marking module.
[0106] In one embodiment, after the bytecode generation module generates the bytecode, the operation type marking module can obtain the operation type of the bytecode, mark the operation type of the bytecode, and send the marked bytecode to the virtual code generation module.
[0107] In one embodiment, the operation type marking module can also generate operation type information of the bytecode according to the operation type of the bytecode, and the operation type information is used to indicate the operation type of the bytecode. In this embodiment, the operation type marking module can send the bytecode and the operation type information of the bytecode to the virtual code generation module. In the embodiment of the present application, there is no restriction on the form of the content sent by the operation type marking module to the virtual code generation module.
[0108] In the embodiment of the present application, because the operation type marking module marks the operation type of the bytecode, the virtual code generation module can interact with the machine code generation module and generate binary code according to the operation type of the bytecode. Exemplarily, the virtual code generation module can generate LLVM IR according to the operation type of the bytecode, and the machine code generation module can generate binary code, such as wasm code, according to the LLVM IR.
[0109] It should be understood that in the prior art, reference Figure 1 In the static compilation process of a dynamically typed language, when the electronic device does not mark the operation type of the bytecode, the electronic device can also generate binary code based on the bytecode. However, because the electronic device does not mark the operation type of the bytecode, when the electronic device runs the wasm code, it needs to perform type checking on the wasm code, which affects the running efficiency. Figure 2 In an embodiment of the present application, during the static compilation process of a dynamically typed language, the electronic device can mark the operation type of the bytecode, so that when the electronic device runs the wasm code, type checking can be avoided or the overhead of type checking can be reduced, thereby improving operation efficiency.
[0110] The following is a description of the method for marking the operation type of the bytecode provided in the embodiments of the present application in conjunction with specific embodiments. The following embodiments may be combined with each other, and the same or similar concepts or processes may not be described in detail in some embodiments.
[0111] Figure 3 A flowchart of an embodiment of a method for marking the operation type of a bytecode provided in an embodiment of the present application. Figure 3 The executive entity is Figure 2 The electronic device shown is used as an example for explanation. Figure 3 The method for marking the operation type of the bytecode provided in the embodiment of the present application may include:
[0112] S301, obtaining bytecode.
[0113] In one embodiment, the electronic device can receive the source code input by the developer and convert the source code into bytecode to obtain the bytecode. Figure 1 In one embodiment, the bytecode generation module can be set independently of the electronic device. After the developer has written the source code, the bytecode generation module can be used to convert the source code into bytecode. The developer can input the bytecode into the electronic device, so that the electronic device can obtain the bytecode. The embodiment of the present application does not limit the way in which the electronic device obtains the bytecode.
[0114] S302, obtaining type information of target data in each function corresponding to the bytecode.
[0115] The source code may include at least one function. Accordingly, when the source code is converted into bytecode, the bytecode may correspond to at least one function. It should be understood that a function is an important program segment. Each function may correspond to a function identifier, and the function identifier is used to distinguish different functions. Exemplarily, the function identifier may include but is not limited to a function name, a function number, etc. It should be understood that a function may be called during program execution.
[0116] In an embodiment of the present application, the electronic device can obtain the type information of the target data in each function by function identification. The target data may include at least one of the following: function parameters, function return values, global variables, and variables in closures. The type information of the target data may include: the type of the target data. Exemplarily, the type of the target data may include, but is not limited to: integer type, string type, single-precision type / floating-point type, double-precision type, and Boolean type.
[0117] In one embodiment, the type of some target data in the function can be determined, wherein the electronic device can determine the type of the target data through the bytecode itself. For example, for a simple addition function "A+B=C", when the formal parameter A and the formal parameter B are written with values, the types of the two operands of the addition function are determined, such as A is an integer type and B is a string type.
[0118] In one embodiment, for target data of uncertain type in a function, the type of the target data can be obtained by clustering. In the embodiment of the present application, the electronic device can establish a control flow graph (CFG) and a data flow graph (DFG) according to the bytecode.
[0119] A control flow graph is an abstract representation of a process or bytecode. It is an abstract data structure used in a compiler, maintained internally by the compiler, and represents all paths that will be traversed during the execution of a program. The control flow graph represents the possible execution directions of all basic blocks in a process in the form of a graph, and can also reflect the real-time execution process of a process. In one embodiment, the control flow graph can be understood as the operation logic of the function corresponding to the bytecode. Exemplarily, the nodes in the control flow graph cover each statement in the execution bytecode, and the edges in the control flow graph cover each branch in the execution bytecode. The control flow graph can also include loop structures, such as for loops, while loops, etc. The loop structure can be understood as loop execution when the characteristic conditions are met. A data flow graph is a graph that represents the data dependencies between the operations of the bytecode.
[0120] In an embodiment of the present application, the electronic device can use a union-find algorithm to collect target data sets that appear in the operands of the same bytecode in the process of establishing a control flow graph and a data flow graph to obtain at least one first set. Among them, the electronic device can determine the type of target data of unknown type based on other target data of known types in the same first set. In one embodiment, target data of unknown type in the same first set can share the type of target data of known type. For example, if the first set includes target data "A, B, and C", and it is known that A is an integer type and B is a string type, the electronic device can determine that C can be an integer type or a string type.
[0121] In one embodiment, the electronic device can also simulate the running process of the bytecode to establish the relationship between the operands and the operation results of the bytecode, and then determine the type of the target data in the bytecode. Exemplarily, the electronic device can simulate the running process of the bytecode and perform operations such as stacking and popping the operands of the bytecode according to the semantics of the bytecode while ignoring the actual data value.
[0122] Figure 4 and Figure 5 Taking an addition function as an example, the process of electronic equipment simulating the operation of bytecode is explained. Figure 4 The a in the figure shows the source code corresponding to the bytecode. Figure 4 The b in the figure shows the bytecode. Figure 5 ae in the figure shows the process of pushing and popping operands when the electronic device simulates the bytecode. For example, when pointing to offset 5, that is, running the bytecode with offset 5, the two operands a and b of the addition have already been written with values, and thus the types of the operands are determined, such as a is an integer and b is a string. At the same time, the operand stack is empty, such as Figure 5 As shown in a in .
[0123] When the offset points to 6, the electronic device can load a and b into the operand stack respectively. It should be understood that the operand stack does not care about the values of a and b at this time, but is only related to the data type of the operand. When the offset points to 7, the electronic device executes the addition instruction add. Figure 5 As shown in c in the figure, because the two operands at the top of the operand stack are of type string and integer respectively, according to the ECMA-262 standard, the result of the addition operation can be of type string. Therefore, the electronic device can perform the agreed operation on the operand stack according to the add instruction, pop the two operands at the top of the stack, and push the string type into the stack, as shown in Figure 5 As shown in d in the figure, the electronic device can finally bind the string type to the variable c, that is, mark the variable c as a string type.
[0124] Among them, the electronic device can traverse the bytecode in sequence according to the control flow graph to simulate the running process of the bytecode to obtain the type of target data in each function corresponding to the bytecode.
[0125] In one embodiment, it should be understood that the bytecode obtained by converting the source code can be divided into multiple bytecodes, such as the bytecode corresponding to each function can be called a bytecode, or each line of bytecode corresponding to each function can be called a bytecode, or each bytecode in each line of bytecode can be called a bytecode. In the embodiment of the present application, each bytecode in each line of bytecode corresponding to the function can be taken as an example, so that the bytecode obtained by converting the source code can include multiple bytecodes. Exemplarily, the following is an example of the bytecode obtained by converting the source code can include the first bytecode and the second bytecode.
[0126] The electronic device can determine the logical association between the bytecodes according to the control flow graph, and determine the type of the bytecode of the unknown type according to the type of the bytecode of the known type, and then determine the type of the target data associated with the bytecode according to the type of the bytecode. In other words, the electronic device can determine the logical association between the first bytecode and the second bytecode according to the control flow graph. If the type of the first bytecode is known and the type of the second bytecode is unknown, the electronic device can determine the type of the second bytecode of the unknown type according to the type of the first bytecode of the known type, and then determine the type of the target data associated with the second bytecode according to the type of the second bytecode.
[0127] When the output of the first bytecode is the input of the second bytecode, and when the first bytecode and the second bytecode meet a preset condition, the first bytecode and the second bytecode can be combined to obtain at least one second set. The bytecodes included in the second set are bytecodes with logical associations. In one embodiment, the preset condition can be: the first bytecode and the second bytecode are not comparison instructions because the type of the comparison instruction is Boolean type.
[0128] For example, the bytecodes of the operands in the addition function can be grouped together with the bytecodes of the addition to obtain a second set, in which the bytecodes of the operands and the bytecodes of the addition can belong to the same equivalence class. In one embodiment, the set of bytecode types contained in the same second set can be used as the types of the bytecodes of unknown types in the same second set. Exemplarily, the same second set includes bytecode A, bytecode B, and bytecode C. Bytecode A corresponds to operand a, and the type of bytecode A can be an integer type. Bytecode B corresponds to operand b, and the type of bytecode B is a string type. Because bytecode C belongs to the same equivalence class as bytecode a and bytecode B and is in the same second set, the type of bytecode C can be an integer type or a string type.
[0129] After the electronic device obtains the type of the bytecode, it can determine the type of the target data in each function according to the type of the bytecode associated with the target data in each function. For example, if the target data c in the addition function is associated with bytecode C, the electronic device can use the type "integer or string type" of bytecode C as the type of the target data c.
[0130] For example, Figure 6 a in the code is the source code of the Fibonacci function (fib). Figure 6 The b in it is the bytecode corresponding to the source code of the Fibonacci function. Figure 7 The type of bytecode is determined based on the logical association between bytecodes. Taking the parameter n in the source code as an example, the bytecodes associated with parameter n are the bytecodes with offsets 0, 7, 19, and 28 respectively. Figure 7 As shown, it can be determined that the types of the bytecodes at offsets 0, 7, 19 and 28 are all integers, so the type of the formal parameter n can be determined as an integer.
[0131] S303, determining the operation type of the bytecode according to the type information of the target data in each function.
[0132] Taking a function as an example, the operation type of the bytecode corresponding to the function can be: the union of the types of the target data in the function. For example, for the addition function "a+b", the bytecode corresponding to the addition function can include the target data a, "+", and the bytecode corresponding to b. The target data a is an integer, and the target data b is a string type. Then the operation type of the bytecode corresponding to the addition function can be "integer and string addition operation".
[0133] In one embodiment, the bytecode obtained by converting the source code corresponds to at least one function, and each function may correspond to at least one line of bytecode. In the embodiment of the present application, the electronic device may determine the operation type of each line of bytecode based on the type information of the target data in the function corresponding to each line of bytecode.
[0134] In one embodiment, the electronic device may also determine the operation type of the bytecode according to the semantics of the bytecode and the type information of the target data. For example, a rule for determining the operation type of the bytecode may be predefined, such as predefining: for a bytecode of a calculation type, the operation type of the bytecode may be determined according to the type of the result of the calculation output. For example, Figure 4 As shown in b in FIG. 1 , it can be predefined that for a bytecode containing "get_loc", the operation type of the bytecode can be determined based on the type of the bytecode operand.
[0135] S304, marking the operation type of the bytecode.
[0136] After obtaining the operation type of the bytecode, the electronic device can mark the operation type of the bytecode.
[0137] In one embodiment, for each function, the electronic device may mark the operation type of each line of bytecode of the function, so that the electronic device may mark the operation type of the bytecode corresponding to the source code.
[0138] For example, Figure 6 Taking the bytecode shown in b as an example, the bytecode marked with the operation type can be as follows Figure 7 shown.
[0139] Figure 8 The process of labeling bytecodes for electronic devices to determine the type of operation they are performing. Figure 8 After obtaining the bytecode, the electronic device can divide the bytecode into basic blocks according to the function name and in units of functions, and each basic block can correspond to a function. The electronic device can also establish a control flow graph and a data flow graph based on the bytecode. The electronic device can determine the target data in each function, traverse the bytecode along the control flow graph, determine the operation type of the bytecode according to the type information of the target data, and mark the operation type of the bytecode, and finally obtain the marked bytecode.
[0140] In the embodiment of the present application, the electronic device can determine the operation type of the bytecode corresponding to the function by the type information of the target data in the bytecode, and mark the operation type of the bytecode, without limiting the dynamic characteristics of the dynamic type language, and can also achieve the purpose of accurately marking the operation type of the bytecode. In this way, when the electronic device is running the program, it is not necessary to perform type checking on the program or reduce the type checking of the program, which can improve the running efficiency of the program.
[0141] based on Figure 3 In the method shown in the figure, the electronic device cannot obtain the operation type of the bytecode in some cases. In order to obtain the operation type of the bytecode more comprehensively, in one embodiment, the electronic device can also run the bytecode to obtain the operation type of the bytecode during the running of the bytecode to supplement the Figure 3 For example, taking the target data as a variable, if the source code does not mark the type of the variable, and the calculation operation in which the variable participates in the program is performed, Figure 3 The method shown is not sufficient to determine the type of the variable. For example, the over-generalization of the variable type leads to the over-generalization of the inferred bytecode operation type. In this way, the embodiment of the present application can obtain the bytecode operation type during the bytecode execution process to supplement Figure 3 The shortcomings of the method shown.
[0142] In one embodiment, a virtual machine may be deployed in the electronic device, and the virtual machine may interpret and execute source code. In one embodiment, the virtual machine may include an interpreter, and the interpreter may interpret and execute source code. During the process of interpreting and executing, the interpreter may convert the source code into bytecode and run the bytecode. Exemplarily, the operation type marking module may obtain the operation type of the bytecode and mark the operation type of the bytecode during the process of the interpreter running the bytecode.
[0143] In an embodiment of the present application, a plug-in module can be set at the entrance of the function of the virtual machine, that is, a plug-in module can be inserted at the entrance of the function of the virtual machine. When the virtual machine (such as an interpreter in the virtual machine) is running the bytecode, the plug-in module can record the operation information of the bytecode. In one embodiment, the operation information of the bytecode may include the type of the operand of the bytecode and the type of the operation result. The back-end information analysis module of the virtual machine can receive the operation information of the bytecode from the plug-in module, and maintain the operation information of the bytecode of the bytecode. In one embodiment, the back-end information analysis module can also maintain the function status, such as the function is called, the function returns, etc.
[0144] In one embodiment, the operation type marking module may determine the operation type of the bytecode based on the operation information of the bytecode maintained by the backend information analysis module.
[0145] It should be understood that when the virtual machine runs the bytecode, the function corresponding to the bytecode can be called. Each time the virtual machine calls a function, a stack frame is maintained, which can be used to pass parameters, save return addresses, save local variables, etc. Fig. 9 During the execution of the bytecode, a stack frame can be created at the top of the stack frame (SF). The stack frame is responsible for tracking the interpreter status. For example, whenever the interpreter runs the bytecode and calls a function (i.e., into function), space can be allocated at the top of the stack frame to record the function signature of the currently called function. During the function call process, the bytecode operation information can also be recorded in the stack frame. When the function returns (out function), the stack frame can be destroyed, and the bytecode operation information recorded in the stack frame can be saved in the ScoreBoard (SB). The back-end information analysis module of the virtual machine can maintain the bytecode operation information in the ScoreBoard.
[0146] Among them, ScoreBoard is used to store the operation information of the bytecode each time the function is called. In one embodiment, when the same function is called multiple times, the type of bytecode operands and the type of operation results obtained by the electronic device may be different, that is, the operation information of the bytecode is different. The back-end information analysis module can also calculate the proportion (hit rate) of different operation information of the bytecode obtained when the same function is called, and the back-end information analysis module can store the proportion of the operation information of different bytecodes of the function in correspondence with the function name of the function.
[0147] For example, a function is called 100 times, and the bytecode operation information obtained 99 times includes: the addition operation of string type a and string type b, and the bytecode operation information obtained once includes the addition operation of string type a and integer type b. The bytecode operation information obtained 99 times can be called the first operation information, and the bytecode operation information obtained once can be called the second operation information.
[0148] The backend information analysis module can calculate that, in the 100 times the function is called, the first operation information accounts for 99% and the second operation information accounts for 1%. The backend information analysis module can store "the first operation information accounts for 99% and the second operation information accounts for 1%" in correspondence with the function. Exemplarily, the backend information analysis module can store "the first operation information accounts for 99% and the second operation information accounts for 1%" in correspondence with the function signature of the function in ScoreBoard, such as Fig. 9 shown.
[0149] Combine the following Fig.10 Describe the function calling process. Fig.10 This is a flowchart of a single function call process. Fig.10 , the process of the virtual machine calling a function may include:
[0150] S1001, the virtual machine calls a function.
[0151] S1002, the stack frame management module (call stack) creates a stack frame and stores a function signature in the stack frame.
[0152] The stack frame management module can allocate space to the function at the top of the stack frame, and the allocated space can be called a stack frame. The process of the stack frame management module allocating space to the function can be called the process of establishing a stack frame. The stack frame management module can store the function signature in the stack frame.
[0153] S1003, during the function call process, the stub module in the virtual machine obtains the bytecode operation information and sends the bytecode operation information to the stack frame management module.
[0154] Among them, during the function call process, the interpreter can interpret and execute the bytecode, and the stub module can obtain the type of the bytecode operand, and then send the type of the bytecode operand to the stack frame management module. Correspondingly, the stack frame management module can store the type of the bytecode operand in the stack frame. When the function call is completed, the interpreter interprets and executes the bytecode, and the stub module can obtain the type of the bytecode operation result, and then send the type of the bytecode operation result to the stack frame management module. Correspondingly, the stack frame management module can store the type of the bytecode operation result in the stack frame.
[0155] S1004, the virtual machine exits the calling function.
[0156] S1005: The stack frame management module eliminates the stack frame.
[0157] The stack frame management module destroys the stack frame, which can be understood as: the stack frame management module pops the stack frame allocated at the top of the stack frame.
[0158] S1006, the backend information analysis module in the virtual machine stores the bytecode operation information in the ScoreBoard, and updates the hit rate of the bytecode operation information in the ScoreBoard.
[0159] Each time a function is called, the proportion of bytecode operation information will change. Therefore, when the function exits, the backend information analysis module can update the proportion of bytecode operation information corresponding to the function in ScoreBoard according to the number of times the bytecode operation information appears.
[0160] In one embodiment, the electronic device can obtain the operation information of the bytecode during the operation of the bytecode, and the electronic device can determine the operation type of the bytecode according to the operation information of the bytecode. Among them, the electronic device can determine the operation type of the bytecode according to the semantics of the bytecode and the operation information of the bytecode. Exemplarily, the determination rules of the operation type of the bytecode can be predefined, such as predefining the bytecode of the calculation type, and determining the operation type of the bytecode according to the type of the operation result. For example, for push_0 (putting a 0 into the running stack), the operation type of the bytecode can be determined according to the type of the operation result. Exemplarily, for a bytecode containing "get_loc", the operation type of the bytecode can be determined according to the type of the bytecode operand. Exemplarily, for an add operation, the electronic device can pop the top two operands of the running stack and then put in the sum of the two, and the electronic device can determine the operation type of the bytecode according to the type of the operand involved in the operation.
[0161] Because the same function may obtain different bytecode operation types in different calling processes, in one embodiment, the electronic device may determine the bytecode operation type based on the proportion of the bytecode operation information of the function. In one embodiment, the electronic device may determine the bytecode operation type based on the operation information of the bytecode with the largest proportion. In one embodiment, the electronic device may also determine the bytecode operation type based on the operation information of the top N bytecodes in terms of proportion. N may be an integer greater than or equal to 1, and the proportion ranking may be sorted in descending order of proportion. In one embodiment, the electronic device may determine the bytecode operation type based on the operation information of all bytecodes, which is not limited in the embodiments of the present application.
[0162] In the embodiment of the present application, the electronic device can not only use Figure 3 The method shown in the figure determines the operation type of the bytecode according to the type of target data in each function corresponding to the bytecode. The electronic device can also determine the operation type of the bytecode during the bytecode operation. Figure 3 In the bytecode operation types obtained by the method shown in the figure or by running the bytecode alone, some bytecode operation types are missing. Figure 3The method shown or the method of running bytecode alone will cause the problem that the electronic device cannot fully obtain the operation type of the bytecode. Because the control flow of the function is different when it is run multiple times, the operation type of the bytecode obtained each time the function is run is also different. In addition, the asynchronous mechanism of JS will disrupt the execution of the function, which will cause the operation type obtained by the electronic device to be interrupted. For example, in the QuickJS virtual machine, triggering the async / await mechanism will interrupt the execution of the function, put the asynchronous function into the task list, and put the saved task back into the execution stack after the main function is launched.
[0163] In order to ensure that the electronic device can obtain the operation information of the bytecode comprehensively and completely, and to accurately mark the operation type of the bytecode, the operation types of the bytecode obtained by the two methods can be integrated in the embodiment of the present application, so that the electronic device can obtain a more comprehensive operation type of the bytecode.
[0164] In this embodiment, refer to Fig.11 The method for marking the operation type of the bytecode provided in the embodiment of the present application may include:
[0165] S1101, obtain bytecode.
[0166] S1102, obtaining type information of target data in each function corresponding to the bytecode.
[0167] S1103: Determine the first operation type of the bytecode according to the type information of the target data in each function.
[0168] S1101-S1103 can refer to the description in S301-S303. In one embodiment, in order to distinguish the operation types of bytecodes obtained by the electronic device using two methods, the operation type of the bytecode determined according to the type information of the target data in each function can be used as the first operation type of the bytecode.
[0169] S1104, run the bytecode to obtain a second operation type of the bytecode.
[0170] S1104 can refer to Fig. 9 In the embodiment of the present application, the operation type of the bytecode obtained by running the bytecode can be used as the second operation type of the bytecode.
[0171] It should be understood that there is no order of precedence between S1102 - S1103 and S1104 , and both can be executed simultaneously.
[0172] S1105: Obtain an operation type of the bytecode according to the first operation type of the bytecode and the second operation type of the bytecode.
[0173] After the electronic device obtains the first operation type of the bytecode and the second operation type of the bytecode, the first operation type and the second operation type can be merged to obtain the operation type of the bytecode. The electronic device can pre-store a fusion strategy, and the electronic device can merge the first operation type and the second operation type according to the fusion strategy.
[0174] In one embodiment, the fusion strategy may be: the electronic device may use the union of the first operation type and the second operation type as the operation type of the bytecode. Figure 3 The method shown can determine that the first operation type of the bytecode includes: the operation type of bytecode 1 is 1A, the operation type of bytecode 2 is 2A, and the operation type of bytecode 4 is 4A. The electronic device adopts the method of running the bytecode to determine that the second operation type of the bytecode includes: the operation type of bytecode 1 is 1A, 1B, the operation type of bytecode 2 is 2A, and the operation type of bytecode 3 is 3A. The electronic device integrates the first operation type and the second operation type to determine that the operation type of the bytecode includes: the operation type of bytecode 1 is 1A, 1B, the operation type of bytecode 2 is 2A, the operation type of bytecode 3 is 3A, and the operation type of bytecode 4 is 4A.
[0175] In one embodiment, when the electronic device merges the first operation type and the second operation type, if the first operation type and the second operation type both include the operation type of the same bytecode, then the operation type of the same bytecode in the first operation type and the second operation type should be the same. Therefore, if the operation type of the same bytecode exists in the first operation type and the second operation type, the electronic device can detect whether the operation type of the same bytecode in the first operation type and the second operation type is the same. If they are the same, the electronic device can merge the operation type of the same bytecode in the first operation type and the second operation type; if they are different, the electronic device may not merge or delete the operation type of the same bytecode in the first operation type and the second operation type.
[0176] In one embodiment, if the operation type of the same bytecode exists in the first operation type and the second operation type, the electronic device can detect whether the types of target data corresponding to the same bytecode in the first operation type and the second operation type are the same. If they are the same, the electronic device can merge the operation type of the same bytecode in the first operation type and the second operation type; if they are different, the electronic device can not merge or delete the operation type of the same bytecode in the first operation type and the second operation type.
[0177] In one embodiment, the fusion strategy may be: the electronic device may obtain the proportion of different operation types of the same bytecode in the first operation type and the second operation type, and then fuse the first operation type and the second operation type of the same bytecode according to the proportion of different operation types of the same bytecode.
[0178] In one embodiment, for the same bytecode, the electronic device may use the operation type with the largest proportion as the operation type of the bytecode. In one embodiment, the electronic device may also use all the operation types of the bytecode as the operation type of the bytecode.
[0179] For example, Figure 6 The source code of the Fibonacci function is shown in a. Figure 6 The bytecode of the Fibonacci function is shown in b. Figure 6 As shown in b, for each bytecode, the number in front of the bytecode represents the offset of the bytecode in all bytecodes. For example, for a line of bytecode "0: get_arg0", the "first number 0" in the line of bytecode represents that the offset of the line of bytecode in all bytecodes is 0, and the "get_arg0" in the line of bytecode represents the specific content of the line of bytecode.
[0180] Table 1 shows the bytecode for the Fibonacci function. The electronic device uses Figure 3 The method shown and the method for running the bytecode can obtain different operation types of the bytecode of the function, as shown in Table 1. The 4 different operation types of the bytecode of the Fibonacci function. In one embodiment, the 4 different operation types of the bytecode of the Fibonacci function can be referred to as 4 versions of the operation type. In the embodiment of the present application, the electronic device can determine the operation type of the bytecode of the Fibonacci function based on the operation types of different versions. In other words, in the embodiment of the present application, the electronic device can fuse the operation types of different versions of the bytecode of the function to determine the operation type of the bytecode.
[0181] In one embodiment, the operation types of different versions of the bytecode may include a first operation type and / or a second operation type. Exemplarily, for different functions, the electronic device may obtain the first operation type and / or the second operation type of the function, so the operation type of the bytecode corresponding to the function may include the first operation type and / or the second operation type.
[0182] For example, the electronic device may use Figure 3The method shown in the figure obtains the first operation type of the bytecode corresponding to the function. However, if the electronic device has not called the function, the electronic device uses the method of running the bytecode and cannot obtain the second operation type of the bytecode corresponding to the function. In this case, the electronic device can only obtain the first operation type of the bytecode. Figure 3 The method shown in the figure fails to obtain the first operation type of the bytecode corresponding to the function, but the electronic device can obtain the second operation type of the bytecode corresponding to the function by running the bytecode. In this case, the electronic device can only obtain the second operation type of the bytecode. For example, for some functions, the electronic device can use Figure 3 The method shown obtains the first operation type of the bytecode corresponding to the function, and the electronic device can also obtain the second operation type of the bytecode corresponding to the function by running the bytecode. In this case, the electronic device can obtain the first operation type and the second operation type of the bytecode.
[0183] Table 1
[0184]
[0185]
[0186] As shown in Table 1, different bytecodes in the Fibonacci function are represented by the bytecode offset "0-33". Table 1 shows the operation types of the bytecodes of the Fibonacci function in four versions, and the number of times the operation type of each version appears. The four versions are version v1, version v2, version v3, and version v4. The electronic device can obtain the proportion of the operation type of each version of the bytecode of the Fibonacci function according to the number of times the operation type of each version appears, and the total number of times the operation type of the four versions appears.
[0187] For example, taking version v1 as an example, the proportion of operation types of version v1 is 1 / (1+10944+4181+6765)=1 / 21891. Table 1 shows the types of operands and operation results included in the operation types of each version of the bytecode. Among them, I represents integer type, S represents string type, B represents Boolean type, and O represents object type (object).
[0188] The “-” in Table 1 indicates that the control flow does not pass through the bytecode. Among them, the v1 version and the v2 version have the same control flow, the difference is that in the v1 version, the actual parameter received by the formal parameter n is the string "20", and because the result of the operation of the string "20" and the integer constant is an integer, in the subsequent function call process, the actual parameters received by the Fibonacci function are all integers, and the hit value of the v1 version is 1. In the v3 version and the v4 version, the actual parameters corresponding to the formal parameter n are 0 and 1 respectively. Even if the type of the actual parameter is the same, due to the different values, different control flows are formed, and different type versions are generated.
[0189] S1106, marking the operation type of the bytecode.
[0190] S1106 may refer to the description in S304.
[0191] In an embodiment of the present application, the electronic device can also run the bytecode to obtain the second operation type of the bytecode during the running of the bytecode. In this way, the electronic device can integrate the first operation type and the second operation type of the bytecode to obtain the operation type of the bytecode, which can improve the comprehensiveness and accuracy of the operation type of the bytecode.
[0192] In one embodiment, after marking the operation type of the bytecode, the electronic device can also generate binary code according to the marked bytecode. Exemplarily, after marking the operation type of the bytecode, the operation type marking module can send the marked bytecode to the virtual code generation module. The virtual code generation module and the machine code generation module can cooperate and generate binary code according to the marked bytecode. Figure 2 Related description in .
[0193] In the embodiment of the present application, for the bytecode, because the electronic device can obtain the operation type of different versions of the bytecode, the electronic device can generate different versions of binary code according to the operation type of different versions of the bytecode. Exemplarily, in the embodiment of the present application, the electronic device can obtain the operation type of different versions of the bytecode corresponding to each function in units of functions, so the electronic device can obtain different versions of binary code for each function according to the operation type of different versions of the bytecode corresponding to each function.
[0194] In one embodiment, templates of binary codes corresponding to different versions of operation types may be pre-stored in the electronic device. After obtaining the operation types of different versions of the bytecode corresponding to each function, the electronic device may generate different versions of binary codes according to the templates of binary codes corresponding to the operation types of different versions of the bytecode.
[0195] Fig.12A schematic diagram of generating different versions of binary code provided in an embodiment of the present application. Fig.12 For example, for a function, the electronic device can use three versions of the operation type of the bytecode corresponding to the function, and the operation types of the three versions are V1 version, V2 version and V3 version respectively. Therefore, the electronic device can generate three corresponding versions of binary code according to the operation type of each version of the bytecode corresponding to the function, and the three versions of binary code are wasm V1 version, wasm V2 version, and wasm V3 version respectively.
[0196] In an embodiment of the present application, because the electronic device can obtain the operation types of different versions of the bytecode corresponding to the function, correspondingly, the electronic device can generate different versions of binary codes according to the operation types of different versions of the bytecode corresponding to the function, so that the electronic device can obtain comprehensive and accurate binary codes.
[0197] It should be noted that the user information and data involved in this application (including but not limited to data used for analysis, stored data, displayed data, etc.) are all information and data authorized by the user or fully authorized by all parties, and the collection, use and processing of relevant data must comply with relevant laws, regulations and standards of relevant countries and regions, and provide corresponding operation entrances for users to choose to authorize or refuse.
[0198] In one embodiment, the present application also provides an electronic device, Fig.13 Another structural diagram of an electronic device provided in an embodiment of the present application. Fig.13 , the electronic device may include: a processor 1301 (such as a CPU), and a memory 1302. The memory 1302 may include a high-speed random access memory (RAM), and may also include a non-volatile memory (NVM), such as at least one disk memory. The memory 1302 may store various instructions for completing various processing functions and implementing the method steps of the present application.
[0199] Optionally, the electronic device involved in the present application may further include: a power supply 1303, a communication bus 1304 and a communication port 1305. The above-mentioned communication port 1305 is used to realize the connection and communication between the electronic device and other peripherals. In the embodiment of the present application, the memory 1302 is used to store computer executable program code, and the program code includes instructions; when the processor 1301 executes the instruction, the instruction causes the processor 1301 of the electronic device to perform the action in the above-mentioned method embodiment, and its implementation principle and technical effect are similar, which will not be repeated here.
[0200] It should be noted that the modules or components described in the above embodiments may be one or more integrated circuits configured to implement the above methods, such as one or more application specific integrated circuits (ASICs), or one or more microprocessors (digital signal processors, DSPs), or one or more field programmable gate arrays (FPGAs), etc. For another example, when a module above is implemented in the form of a processing element scheduling program code, the processing element may be a general-purpose processor, such as a central processing unit (CPU) or other processors that can call program codes, such as a controller. For another example, these modules may be integrated together and implemented in the form of a system-on-a-chip (SOC).
[0201] In the above embodiments, it can be implemented in whole or in part by software, hardware, firmware or any combination thereof. When implemented using software, it can be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions. When loading and executing computer program instructions on a computer, the process or function according to the embodiment of the present application is generated in whole or in part. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable devices. The computer instructions can be stored in a computer-readable storage medium, or transmitted from one computer-readable storage medium to another computer-readable storage medium. For example, the computer instructions can be transmitted from a website site, a computer, a server or a data center by wired (e.g., coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) mode to another website site, computer, server or data center. The computer-readable storage medium can be any available medium that a computer can access or a data storage device such as a server, a data center, etc. that contains one or more available media integrated. The available medium can be a magnetic medium, (e.g., a floppy disk, a hard disk, a tape), an optical medium (e.g., a DVD), or a semiconductor medium (e.g., a solid-state hard disk Solid State Disk (SSD)), etc.
[0202] The term "plurality" in this article refers to two or more than two. The term "and / or" in this article is only a description of the association relationship of associated objects, indicating that three relationships may exist. For example, A and / or B can represent: A exists alone, A and B exist at the same time, and B exists alone. In addition, the character " / " in this article generally indicates that the previous and next associated objects are in an "or" relationship; in the formula, the character " / " indicates that the previous and next associated objects are in a "division" relationship. In addition, it should be understood that in the description of this application, words such as "first" and "second" are only used for the purpose of distinguishing descriptions, and cannot be understood as indicating or implying relative importance, nor can they be understood as indicating or implying order.
[0203] It should be understood that the various numerical numbers involved in the embodiments of the present application are only used for the convenience of description and are not used to limit the scope of the embodiments of the present application.
[0204] It can be understood that in the embodiments of the present application, the size of the serial numbers of the above-mentioned processes does not mean the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of the present application.
Claims
1. A method for marking the operation type of a bytecode, characterized in that: include: Get bytecode; Obtaining type information of target data in each function corresponding to the bytecode; Determining a first operation type of the bytecode according to type information of target data in each function; According to the first operation type, the operation type of the bytecode is marked.
2. The method according to claim 1, characterized in that The method further comprises: During the execution of the bytecode, determining a second operation type of the bytecode; The step of marking the operation type of the bytecode according to the first operation type includes: Acquire the operation type of the bytecode according to the first operation type and the second operation type; According to the operation type of the bytecode, the operation type of the bytecode is marked.
3. The method according to claim 1 or 2, characterized in that: The target data includes at least one of the following: function parameters, function return values, global variables, and variables in a closure.
4. The method according to any one of claims 1 to 3, characterized in that The obtaining type information of target data in each function corresponding to the bytecode includes: Establishing a control flow graph and a data flow graph of the bytecode; In the process of establishing the control flow graph and the data flow graph, a union-find algorithm is used to collect target data sets appearing in operands of the same bytecode to obtain at least one first set; The type of the target data of unknown type is determined according to the type of the target data of known type in each first set to obtain type information of the target data of unknown type.
5. The method according to any one of claims 1 to 3, characterized in that: The bytecode includes a first bytecode and a second bytecode, and the method further includes: Determining a logical association relationship between the first bytecode and the second bytecode; Determine the type of the second bytecode of unknown type according to the type of the first bytecode of known type; According to the type of the second bytecode, type information of the target data associated with the second bytecode is determined.
6. The method according to claim 5, characterized in that Before determining the type of the second bytecode of unknown type according to the type of the first bytecode of known type, the method further includes: When the output of the first bytecode is the input of the second bytecode, and when the first bytecode and the second bytecode meet a preset condition, the first bytecode and the second bytecode are set together to obtain at least one second set.
7. The method according to claim 6, characterized in that The preset condition is that neither the first bytecode nor the second bytecode is a comparison instruction.
8. The method according to any one of claims 1 to 3, characterized in that The obtaining type information of target data in each function corresponding to the bytecode includes: The running process of the bytecode is simulated to obtain type information of target data in each function.
9. The method according to any one of claims 1 to 8, characterized in that The type information of the target data in each function includes: the type of the target data in each function; Determining the first operation type of the bytecode according to the type information of the target data in each function includes: The union of the types of the target data in each function is used as the first operation type.
10. The method according to any one of claims 1 to 8, characterized in that The type information of the target data in each function includes: the type of the target data in each function, each function corresponding to at least one line of bytecode; Determining the first operation type of the bytecode according to the type information of the target data in each function includes: A union of types of target data in a function corresponding to each line of bytecode is used as an operation type of each line of bytecode, wherein the first operation type includes an operation type of at least one line of bytecode corresponding to each function.
11. The method according to claim 2, characterized in that The step of determining the second operation type of the bytecode during the execution of the bytecode includes: During the running of the bytecode, obtaining operation information of the bytecode corresponding to the called function, the operation information including the type of the operand and the type of the operation result; According to the operation information of the bytecode corresponding to the called function, the operation type of the bytecode corresponding to the called function is obtained.
12. The method according to claim 11, characterized in that The operation information of the bytecode corresponding to the called function includes at least two versions, and obtaining the operation type of the bytecode corresponding to the called function according to the operation information of the bytecode corresponding to the called function includes: Obtaining the proportion of operation information of each version of the bytecode corresponding to the called function; According to the proportion, an operation type of the bytecode corresponding to the called function is obtained.
13. The method according to claim 12, characterized in that The obtaining, according to the proportion, an operation type of the bytecode corresponding to the called function includes: The union of the operation types corresponding to the top N operation information ranked by percentage is used as the operation type of the bytecode corresponding to the called function, where N is an integer greater than or equal to 1.
14. The method according to claim 2, characterized in that The acquiring the operation type of the bytecode according to the first operation type and the second operation type includes: The union of the first operation type and the second operation type is used as the operation type of the bytecode.
15. The method according to claim 14, characterized in that The operation type of the bytecode includes at least one version, and after marking the operation type of the bytecode, the following further includes: Generate at least one version of binary code based on the marked bytecode.
16. The method according to any one of claims 1 to 15, characterized in that The obtaining of bytecodes comprises: Receive source code; The source code is converted into the bytecode.
17. The method according to any one of claims 1 to 16, characterized in that The bytecode is obtained from source code written in a dynamically typed language.
18. A device for marking the operation type of a bytecode, characterized in that: include: Processing modules for: Get bytecode; Obtaining type information of target data in each function corresponding to the bytecode; Determining the operation type of the bytecode according to the type information of the target data in each function; The marking module is used to mark the operation type of the bytecode.
19. An electronic device, characterized in that: include: Processor and memory; The memory stores computer instructions; The processor executes the computer instructions stored in the memory, so that the processor performs the method according to any one of claims 1 to 17.
20. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program or instruction, and when the computer program or instruction is executed, the method according to any one of claims 1 to 17 is implemented.
Citation Information
Cited By
Method and apparatus for marking operation type of bytecode, and electronic device
EP4481557A1
Method and apparatus for marking operation type of bytecode, and electronic device
WO2024198779A1
