Method and apparatus for marking operation type of bytecode, and electronic device

By obtaining and analyzing the target data type information of bytecodes in electronic devices, establishing control flow graphs and data flow graphs, determining the bytecode operation types of dynamic type languages, and solving the problem of bytecode operation types in dynamic type language writing programs. Problems that are difficult to identify are solved, and program operation efficiency and compatibility are improved.

WO2024198779A1PCT designated stage expired Publication Date: 2024-10-03HUAWEI TECH CO LTD
View PDF 4 Cites 0 Cited by

Patent Information

Application Number
PCT/CN2024/078011
Authority / Receiving Office
WO · WO
Patent Type
Applications
Current Assignee / Owner
Priority Date
2023-03-30
Filing Date
2024-02-22
Publication Date
2024-10-03

AI Technical Summary

Technical Problem

In a program written in a dynamically typed language, the operation type of the bytecode cannot be determined through type tags, resulting in low program running efficiency and limited expression capabilities of the dynamically typed language, and is incompatible with other languages.

Method used

By obtaining the target data type information of each function corresponding to the bytecode in the electronic device, establish a control flow graph and a data flow graph, use the union search algorithm and simulate the running process to determine the operation type of the bytecode and avoid type checking. , improve operating efficiency and compatibility.

Benefits of technology

It achieves accurate marking of bytecode operation types during static compilation of dynamically typed languages, improves program running efficiency, enhances the compatibility of dynamically typed languages, and avoids type checking overhead.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN2024078011_03102024_PF_FP_ABST
    Figure CN2024078011_03102024_PF_FP_ABST
Patent Text Reader

Abstract

A method and apparatus for marking an operation type of a bytecode, and an electronic device. In the method, an electronic device can acquire a bytecode, acquire type information of target data in each function corresponding to the bytecode, determine a 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.
Need to check novelty before this filing date? Find Prior Art

Description

Method, device and electronic device for marking bytecode operation type

[0001] This application claims priority to the Chinese patent application filed with the China Patent Office on March 30, 2023, with application number 202310353125.1 and application name “Method, device and electronic device for marking bytecode operation types”, the entire contents of which are incorporated by reference into this application. Technical Field

[0002] 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 bytecode operation types. Background Art

[0003] When a virtual machine runs a program written in a programming language or scripting language, it can perform type checking on the program to read the type information of the program's parameters, variables, etc. The virtual machine can then perform subsequent calculations based on the type information of the program's parameters, variables, etc.

[0004] Programs written in dynamically typed languages ​​don't require type annotation within the code, so it's impossible to determine the type of bytecode operations using type annotations. Consequently, it's also impossible to determine the type information of formal parameters and variables within the program based on the bytecode operation type. Currently, it's possible to restrict some of the dynamic features of the programming or scripting language when writing programs, making it easier to infer the types of program variables and, by extension, the bytecode operation types. However, this approach limits the expressive power of dynamically typed languages ​​and can lead to language incompatibility issues.

[0005] Summary of the Invention

[0006] The embodiments of the present application provide a method, apparatus, and electronic device for marking the operation type of a bytecode, which can achieve the purpose of accurately marking the operation type of a bytecode without restricting the dynamic characteristics of a dynamic type language.

[0007] In a first aspect, embodiments of the present application provide a method for marking bytecode operation types. The method may be performed by an electronic device or a chip within the electronic device. The following description uses an electronic device as an example. It should be understood that a compiler may be deployed within the electronic device to implement marking of bytecode operation types during compilation of a dynamically typed language.

[0008] In this method, an electronic device may obtain bytecodes, and after obtaining the bytecodes, may obtain type information of target data in each function corresponding to the bytecodes, thereby determining a first operation type of the bytecodes based on the type information of the target data in each function. The electronic device may then mark the operation type of the bytecodes based on the first operation type. In one possible implementation, the target data includes at least one of the following: function parameters, function return values, global variables, and variables in a closure.

[0009] In a possible implementation, the bytecode is obtained from source code written in a dynamic type language.

[0010] For example, 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 another electronic device, and this application does not limit this. After receiving the source code, the electronic device may convert the source code into the bytecode.

[0011] 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. Not only can the operation type of the bytecode be accurately determined, but there is no need to pre-restrict the dynamic characteristics of the dynamic type language (such as a programming language or scripting language), which can improve the compatibility of the dynamic type language.

[0012] 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.

[0013] 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. This 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.

[0014] The following describes a method for an electronic device to obtain type information of target data in each function corresponding to a bytecode:

[0015] First, after obtaining 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 the target data of the unknown type based on the type of the target data of the known type in each first set to obtain the type information of the target data of the unknown type. Exemplarily, the electronic device can use the union of the types of the target data of the known type in the first set as the type of the target data of the unknown type, and the type information of the target data of the unknown type includes the type of the target data of the unknown type.

[0016] Secondly, the electronic device can also infer the type of target data associated with the bytecode based on the type of the bytecode. It should be understood that the bytecode obtained by the electronic device can be considered as a whole, which can include multiple bytecodes. For example, each function can correspond to at least one line of bytecode, and each line of bytecode can include multiple bytecodes.

[0017] Here, taking the example of a bytecode including a first bytecode and a second bytecode, the electronic device can determine the logical association relationship between the first bytecode and the second bytecode, and determine the type of the unknown second bytecode based on the type of the known type of the first bytecode, and then determine the type information of the target data associated with the second bytecode based on the type of the second bytecode.

[0018] 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. 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.

[0019] 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 second bytecode based on the type of the known first bytecode in each second set. Exemplarily, the electronic device can use the union of the types of the known first bytecodes in the first set as the type of the unknown second bytecode.

[0020] 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.

[0021] 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.

[0022] 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.

[0023] The following describes 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:

[0024] 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.

[0025] Second, the target data type information 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 may 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, where the first operation type includes the operation type of the at least one line of bytecode corresponding to each function.

[0026] 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.

[0027] The following describes a method for an electronic device to determine the second operation type of a bytecode during the execution of the bytecode:

[0028] Each time an electronic device executes a bytecode, it can call a different function corresponding to the bytecode. Therefore, during the execution of the bytecode, the electronic device can obtain operation information of the bytecode corresponding to the called function, where the operation information includes the type of operands and the type of the operation result. Based on the operation information of the bytecode corresponding to the called function, the electronic device can obtain the operation type of the bytecode corresponding to the called function.

[0029] 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 the need to use an inference method to infer the operation type of the bytecode. In this implementation method, the accuracy of the bytecode operation type is high.

[0030] 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. Therefore, 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.

[0031] The electronic device may obtain the proportion of 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.

[0032] 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.

[0033] In one possible implementation, the electronic device may use the union of the operation types corresponding to the top N operation information in terms of percentage as the operation type of the bytecode corresponding to the called function, where N is an integer greater than or equal to 1. In another 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.

[0034] In one possible implementation, after obtaining the first operation type 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 one possible implementation, for the same bytecode, the first operation type and the second operation type of the bytecode include different versions of the operation type. In this way, the electronic device can obtain the proportion of the operation types of the different versions of the bytecode and then use the union of the operation types corresponding to the top N operation information in terms of proportion as the operation type of the bytecode.

[0035] In one possible implementation, the bytecode operation type may include at least one version. After marking the bytecode operation type, the electronic device may further generate at least one version of binary code based on the marked bytecode. In this way, the electronic device may execute the binary code based on the at least one version of the binary code to implement program execution.

[0036] In this implementation, the electronic device can obtain at least one version of binary code, thereby achieving the purpose of accurately running the program.

[0037] In a second aspect, embodiments of the present application provide a device for marking bytecode operation types. The device for marking bytecode operation types can be an electronic device or a chip within an electronic device, and can be used to execute the method of the first aspect. The device for marking bytecode operation types can include a processing module and a marking module.

[0038] The processing module is used to obtain bytecode, obtain type information of target data in each function corresponding to the bytecode, and determine a first operation type of the bytecode according to the type information of the target data in each function.

[0039] A marking module is used to mark the operation type of the bytecode according to the first operation type.

[0040] In one possible implementation, the processing module is specifically configured to determine a second operation type of the bytecode during 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.

[0041] 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.

[0042] In one possible implementation, the processing module is specifically used to establish 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 set the target data appearing in the operands of the same bytecode together 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 type information of the unknown type of target data.

[0043] In a possible implementation, the bytecode includes a first bytecode and a second bytecode.

[0044] 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 based on the type of the first bytecode of known type; and determine the type information of the target data associated with the second bytecode based on the type of the second bytecode.

[0045] In one 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.

[0046] In a possible implementation, the preset condition is: neither the first bytecode nor the second bytecode is a comparison instruction.

[0047] In a possible implementation, the processing module is specifically configured to simulate the running process of the bytecode to obtain type information of the target data in each function.

[0048] In a possible implementation, the type information of the target data in each function includes: the type of the target data in each function.

[0049] 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.

[0050] In a possible implementation, 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.

[0051] The processing module is specifically configured 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, wherein the first operation type includes the operation type of at least one line of bytecode corresponding to each function.

[0052] 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.

[0053] In one possible implementation, the operation information of the bytecode corresponding to the called function includes at least two versions. The processing module is specifically configured to obtain a ratio of the operation information of each version of the bytecode corresponding to the called function; and obtain an operation type of the bytecode corresponding to the called function based on the ratio.

[0054] In one possible implementation, the processing module is specifically configured to 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.

[0055] 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.

[0056] In a possible implementation, the bytecode operation type includes at least one version. The bytecode operation type marking device may further include a binary code generation module.

[0057] The binary code generation module is used to generate at least one version of binary code according to the marked bytecode.

[0058] 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 configured to receive source code and the bytecode generation module is configured to convert the source code into the bytecode.

[0059] In a possible implementation, the bytecode is obtained from source code written in a dynamic type language.

[0060] 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 configured to store computer-executable program code, the program code including instructions; when the processor executes the instructions, the instructions cause the electronic device to perform the method described in the first aspect.

[0061] In a fourth aspect, an embodiment of the present application provides an electronic device, which may be the bytecode operation type marking device of the second aspect or the electronic device of the first aspect. The electronic device may include a unit, module, or circuit for executing the method provided in the first aspect above.

[0062] 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 described above.

[0063] In a sixth aspect, an embodiment of the present application provides a computer-readable storage medium, wherein the computer-readable storage medium stores instructions, which, when executed on a computer, enables the computer to execute the method in the first aspect above.

[0064] 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.

[0065] The embodiment of the present application provides a method, device and electronic device for marking the operation type of a bytecode. In this method, 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 based on the type information of the target data in each function, and mark the operation type of the bytecode based on 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 during static compilation, taking the function as the unit. 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, it can also achieve the purpose of accurately marking the operation type of the bytecode. BRIEF DESCRIPTION OF THE DRAWINGS

[0066] Figure 1 is a schematic diagram of a static compilation process;

[0067] FIG2 is a schematic diagram of a static compilation process according to an embodiment of the present application;

[0068] FIG3 is a flow chart of an embodiment of a method for marking bytecode operation types provided in an embodiment of the present application;

[0069] FIG4 is a schematic diagram of the source code and bytecode of the addition function provided in an embodiment of the present application;

[0070] FIG5 is a schematic diagram of a flow chart of the operation of a simulated addition function provided in an embodiment of the present application;

[0071] FIG6 is a schematic diagram of the source code and bytecode of the Fibonacci function provided in an embodiment of the present application;

[0072] FIG7 is a schematic diagram of a type of bytecode of a Fibonacci function provided in an embodiment of the present application;

[0073] FIG8 is a process of marking the operation type of a bytecode by an electronic device according to an embodiment of the present application;

[0074] FIG9 is a schematic diagram of a flow chart of a function call performed by an electronic device according to an embodiment of the present application;

[0075] FIG10 is another schematic diagram of a flow chart of an electronic device calling a function according to an embodiment of the present application;

[0076] FIG11 is a flow chart of another embodiment of a method for marking bytecode operation types according to an embodiment of the present application;

[0077] FIG12 is a schematic diagram of generating different versions of binary codes according to an embodiment of the present application;

[0078] FIG13 is another structural diagram of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0079] Definitions of terms involved in the embodiments of this application:

[0080] Source code: A source program written in a programming language or scripting language. In one embodiment, the programming language or scripting language may be referred to as a high-level language.

[0081] Source code cannot be recognized and executed by a computer. Before execution, the source code can be translated into machine language code. Depending on the different translation methods, execution is divided into 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 intermediate code while translating the source code. For example, when reading the source program, the interpreter will first call the language analysis program to perform lexical analysis and partial syntax checking, establish a mnemonic table, and convert the source program into intermediate code. The interpreter then calls the interpreted execution program to perform syntax checking and interpret and execute the intermediate code line by line. In short, the interpreter can read and translate the statements in the source program one by one, and execute the translated code line by line.

[0082] 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".

[0083] Bytecode: The intermediate code generated after the source code is parsed.

[0084] Dynamically typed languages: In the embodiments of this application, these languages ​​may include, but are not limited to, Python, Javascript (JS), Ruby, Perl, PHP, R, Dart, Lua, and Objective-C. It should be understood that the following embodiments use JS as an example for illustration.

[0085] A compiler is a program that translates one language (such as a high-level language) into another language (such as a low-level language). In one embodiment, the compiler can be hosted and executed in hardware. The hardware hosting the compiler is referred to as a compilation tool or compilation device. The electronic devices described in the following embodiments can host a compiler and implement static compilation of dynamically typed languages.

[0086] A virtual machine (VM) is a complete computer system that is simulated by software and has complete hardware system functions and runs in a completely isolated environment. In the embodiments of the present application, the virtual machine in the electronic device can run programs.

[0087] Type checking is the process of checking the type constraints of values ​​and expressions in a program to ensure that the program's behavior is well-defined. For example, for var a = 1, a is a variable and 1 is a value. Well-defined doesn't mean a program is completely bug-free. Rather, programming languages ​​define a set of program exceptions (defined differently by language) at the outset of their design. A program is considered well-defined if it doesn't exhibit these errors.

[0088] 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 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.

[0089] 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).

[0090] Type tagging: For example, tagging 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.

[0091] Bytecode operation types: These formally describe how the various steps of a computation are executed in a computer system. In one embodiment, bytecode operation types are similar to the concept of operational semantics. For example, if the bytecode is "A+B," where A is a string and B is a string, then the bytecode operation type is "string addition." For example, if the bytecode is "string.split()," the bytecode operation type is "function call."

[0092] 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.

[0093] 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.

[0094] Closure: can be seen as a combination of functions and variables.

[0095] Static compilation of dynamically typed languages ​​can be understood as compiling code written in a dynamically typed language into a binary executable file or binary code. Static compilation of dynamically typed languages ​​has a wide range of applications and offers the following advantages: First, it greatly simplifies program deployment. Second, it allows for more convenient calls to external native function interfaces. Third, compared to just-in-time (JIT) execution engines, program performance is more predictable. Fourth, compared to non-JIT execution engines, execution efficiency is higher.

[0096] Type tags in a program are crucial for determining the type of bytecode operations. However, for programs written in dynamically typed languages, type tags are not required. Therefore, it is currently impossible to determine the type of bytecode operations using type tags. Currently, when running a program written in a dynamically typed language, a virtual machine can perform dynamic type checks to obtain type information about parameters and variables in the program. The virtual machine can then perform subsequent calculations based on this type information.

[0097] 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.

[0098] In one embodiment, the Hindley-Milner type system can be used to type-tag programs. For example, 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. However, using this type system has the problem of not being able to accurately determine the types of variables.

[0099] For example, for the equation "A + B = C," assuming it's known that C is of type string, the Hindley-Milner type system can infer that A is of type string and B is of type integer (int), or vice versa. However, the specific types of A and B cannot be accurately determined. The types of variables inferred using the Hindley-Milner type system are over-generalized and cannot accurately determine the types of the variables.

[0100] In one embodiment, some dynamic characteristics of dynamically typed languages ​​can be restricted to make the types of variables in a program easier to infer. However, this approach will limit the expressive power of dynamically typed languages ​​and may also lead to language incompatibility issues. For example, by assigning A a value of 1 and pre-restricting A to an integer, the program can determine that A is an integer based on the pre-restrictions on the dynamically typed language during runtime. In one embodiment, for example, restrictions on type conversion, read-only attributes, prohibition of string assignment via indexing, and singleton mutable variables can also be used to restrict some dynamic characteristics of dynamically typed languages, making it easier to determine the type of a variable.

[0101] 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 the 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 by the type information of the target data in the bytecode, taking the function as a unit. 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.

[0102] It should be understood that the execution subject of the method for executing the bytecode operation type 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 (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.

[0103] Before introducing the method for marking the operation type of the bytecode provided in the embodiment of the present application, the static compilation process of the dynamic type language is first introduced in conjunction with Figure 1. The static compilation process of the 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 takes JS as an example of a dynamic type language and an electronic device compiling JS into machine code wasm code as an example to illustrate the static compilation process of the dynamic type language. In one embodiment, wasm code can be regarded as a binary code.

[0104] Referring to FIG1 , in one embodiment, an 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 a dynamically typed language. In one embodiment, the bytecode generation module, the virtual code generation module, and the machine code generation module may constitute a compiler.

[0105] 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 an LLVM backend.

[0106] In an embodiment of the present application, a developer can use the dynamic type language JS to write source code (JS code), and input the JS code into an electronic device for compilation. Among them, the bytecode generation module, in response to the JS code 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 this embodiment of the application are not described in detail.

[0107] In an 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. In one embodiment, referring to FIG2 , the electronic device may further include: an operation type marking module.

[0108] In one embodiment, after the bytecode generation module generates the bytecode, the operation type marking module may 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.

[0109] In one embodiment, the operation type marking module may further generate operation type information for the bytecode based on the operation type of the bytecode, where the operation type information is used to indicate the operation type of the bytecode. In this embodiment, the operation type marking module may send the bytecode and the operation type information of the bytecode to the virtual code generation module. In this embodiment of the present application, there is no limitation on the form of the content sent by the operation type marking module to the virtual code generation module.

[0110] 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 based on the operation type of the bytecode. For example, the virtual code generation module can generate LLVM IR based on the operation type of the bytecode, and the machine code generation module can generate binary code, such as wasm code, based on the LLVM IR.

[0111] It should be understood that in the prior art, referring to FIG1 , during 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, the electronic device needs to perform type checking on the wasm code when running the wasm code, which affects the operating efficiency. Referring to FIG2 , 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. In this way, when the electronic device runs the wasm code, type checking can be avoided or the overhead of type checking can be reduced, thereby improving operating efficiency.

[0112] The following describes the method for marking the operation type of the bytecode provided by the embodiments of the present application in conjunction with specific embodiments. The following embodiments can be combined with each other, and the same or similar concepts or processes may not be described in detail in some embodiments.

[0113] FIG3 is a flow chart of an embodiment of a method for marking bytecode operation types provided by an embodiment of the present application. FIG3 is illustrated by taking the electronic device shown in FIG2 as an example of an execution subject. Referring to FIG3 , the method for marking bytecode operation types provided by an embodiment of the present application may include:

[0114] S301, obtain bytecode.

[0115] In one embodiment, the electronic device can receive source code input by a developer and convert the source code into bytecode to obtain the bytecode, as described in FIG1 . In one embodiment, the bytecode generation module can be set independently of the electronic device. After the developer writes 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, and the electronic device can obtain the bytecode. The embodiment of the present application does not limit the manner in which the electronic device obtains the bytecode.

[0116] S302: Obtain type information of target data in each function corresponding to the bytecode.

[0117] 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 functions are important program segments, and each function may correspond to a function identifier, which is used to distinguish different functions. Exemplarily, the function identifier may include, but is not limited to, the function name, function number, etc. It should be understood that functions may be called during program execution.

[0118] In an embodiment of the present application, an electronic device may use a function identifier to obtain type information of target data in each function, taking the function as a unit. The target data may include at least one of the following: function parameters, function return values, global variables, and variables in a closure. 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.

[0119] In one embodiment, the type of some target data in a 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 parameters A and 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.

[0120] 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) based on the bytecode.

[0121] 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 uses a graph to represent the possible flow of execution of all basic blocks within a process, 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 used to characterize 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 a loop execution when the characteristic conditions are met. The data flow graph is a graph that represents the data dependency relationship between the operations of the bytecode.

[0122] In an embodiment of the present application, the electronic device can use a union-find algorithm in the process of establishing a control flow graph and a data flow graph to group together 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 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 and B is a string type, the electronic device can determine that C can be an integer or a string type.

[0123] In one embodiment, the electronic device can also simulate the execution of the bytecode to establish a relationship between the bytecode operands and the operation results, thereby determining the type of target data in the bytecode. For example, the electronic device can simulate the execution of the bytecode based on the semantics of the bytecode, ignoring the actual data values, and perform operations such as pushing and popping the bytecode operands.

[0124] Figures 4 and 5 use an addition function as an example to illustrate the process of an electronic device simulating the operation of bytecode. Figure 4 a shows the source code corresponding to the bytecode, and Figure 4 b shows the bytecode. Figures 5 ae show the process of pushing and popping operands when the electronic device simulates the operation of the bytecode. For example, when pointing to offset 5, that is, running the bytecode with an offset of 5, the values ​​of the two operands a and b of the addition have been written, and thus the types of the operands are determined, such as a is an integer and b is a string type. At the same time, the operand stack is empty, as shown in Figure 5 a.

[0125] When pointing to offset 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 operands. When the offset points to 7, the electronic device executes the addition instruction add. As shown in c in Figure 5, because the types of the two operands at the top of the operand stack are string type and integer type respectively, according to the ECMA-262 standard, the result of the addition operation can be a string type. Therefore, the electronic device can operate on the operand stack according to the agreed operation of the add instruction, pop the two operands at the top of the stack, and push the string type into the stack, as shown in d in Figure 5. The electronic device can finally bind the string type to the variable c, that is, mark the variable c as a string type.

[0126] 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.

[0127] In one embodiment, it should be understood that the bytecode obtained by converting the source code can be divided into multiple bytecodes. For example, the bytecode corresponding to each function can be referred to as a bytecode, or each line of bytecode corresponding to each function can be referred to as a bytecode, or each bytecode in each line of bytecode can be referred to as a bytecode. In the embodiments of the present application, each bytecode in each line of bytecode corresponding to a function can be used as an example. In this way, the bytecode obtained by converting the source code can include multiple bytecodes. Exemplarily, the following description takes the example that the bytecode obtained by converting the source code can include a first bytecode and a second bytecode.

[0128] The electronic device can determine the logical association between bytecodes based on the control flow graph, and determine the type of the unknown type of bytecode based on the type of the known type of bytecode, and then determine the type of the target data associated with the bytecode based on the type of the bytecode. In other words, the electronic device can determine the logical association between a first bytecode and a second bytecode based on 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 unknown type of the second bytecode based on the type of the known type of the first bytecode, and then determine the type of the target data associated with the second bytecode based on the type of the second bytecode.

[0129] When the output of a first bytecode is the input of a second bytecode, and 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 logically associated bytecodes. In one embodiment, the preset condition may be that the first bytecode and the second bytecode are not comparison instructions because the comparison instruction type is Boolean.

[0130] 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. The bytecodes of the operands and the bytecodes of the addition in the second set 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 type of the bytecodes of unknown type in the same second set. For example, 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 integer. Bytecode B corresponds to operand b, and the type of bytecode B is string. 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 integer or string.

[0131] After obtaining the bytecode type, the electronic device can determine the type of the target data in each function based on 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 of bytecode C (integer or string type) as the type of target data c.

[0132] For example, a in FIG6 is the source code of the Fibonacci function (fib), and b in FIG6 is the bytecode corresponding to the source code of the Fibonacci function. FIG7 shows the type of the bytecode determined based on the logical association relationship between the bytecodes. Taking the formal parameter n in the source code as an example, the bytecodes associated with the formal parameter n are bytecodes with offsets of 0, 7, 19, and 28, respectively. Based on FIG7, it can be determined that the types of the bytecodes with offsets of 0, 7, 19, and 28 are all integers, so the type of the formal parameter n can be determined to be an integer.

[0133] S303: Determine the operation type of the bytecode according to the type information of the target data in each function.

[0134] Taking a function as an example, the bytecode operation type for the function can be: the union of the target data types 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. If target data a is an integer and target data b is a string, the operation type of the bytecode corresponding to the addition function can be "integer and string addition operation".

[0135] 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.

[0136] In one embodiment, the electronic device may also determine the operation type of the bytecode based on 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. For example, it may be predefined that for a computational bytecode, the operation type of the bytecode may be determined based on the type of the computational output. For example, as shown in b in FIG. 4 , it may be predefined that for a bytecode containing "get_loc," the operation type of the bytecode may be determined based on the type of the bytecode's operand.

[0137] S304: Mark the operation type of the bytecode.

[0138] After obtaining the operation type of the bytecode, the electronic device can mark the operation type of the bytecode.

[0139] 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.

[0140] Exemplarily, taking the bytecode shown in b in FIG. 6 as an example, the bytecode marked with the operation type may be as shown in FIG. 7 .

[0141] Figure 8 illustrates the process of an electronic device marking the operation type of a bytecode. Referring to Figure 8 , after obtaining the bytecode, the electronic device can divide the bytecode into basic blocks based on function name, with each basic block corresponding to a function. The electronic device can also create 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 bytecode operation type based on the target data type information, and mark the bytecode operation type, ultimately obtaining the marked bytecode.

[0142] In an embodiment of the present application, during static compilation of a dynamically typed language, an electronic device can determine the operation type of the bytecode corresponding to a function using the type information of the target data in the bytecode, and mark the operation type of the bytecode, without restricting the dynamic characteristics of the dynamically typed language, and can also achieve the purpose of accurately marking the operation type of the bytecode. In this way, when the electronic device runs the program, it is not necessary to perform type checking on the program or the type checking of the program is reduced, which can improve the running efficiency of the program.

[0143] Based on the method shown in FIG3 , the electronic device is unable to 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 deficiencies of the method shown in FIG3 . For example, taking the target data as a variable as an example, if the type of the variable is not marked in the source code, and the calculation operation in which the variable participates in the program, the method shown in FIG3 is not sufficient to determine the type of the variable. For example, the over-generalization of the variable type leads to the problem of over-generalization of the inferred bytecode operation type. In this way, the embodiment of the present application can obtain the operation type of the bytecode during the running of the bytecode to supplement the deficiencies of the method shown in FIG3 .

[0144] In one embodiment, a virtual machine may be deployed in an 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 interpretation and execution process, the interpreter may convert the source code into bytecode and execute the bytecode. Exemplarily, the operation type marking module may obtain the operation type of the bytecode during the interpreter's execution of the bytecode and mark the operation type of the bytecode.

[0145] 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 the 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.

[0146] 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.

[0147] It should be understood that while the virtual machine is running bytecode, it can call the function corresponding to the bytecode. Each time the virtual machine calls a function, it maintains a stack frame, which can be used to pass parameters, save return addresses, and store local variables. Referring to Figure 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 state. For example, whenever the interpreter runs 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, 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 virtual machine's back-end information analysis module can maintain the bytecode operation information in the ScoreBoard.

[0148] ScoreBoard is used to store the bytecode operation information each time a function is called. In one embodiment, when the same function is called multiple times, the types of bytecode operands and the types of operation results obtained by the electronic device may be different, that is, the bytecode operation information is different. The back-end information analysis module can also calculate the hit rate of different bytecode operation information obtained when the same function is called. The back-end information analysis module can store the hit rate of different bytecode operation information of the function in correspondence with the function name.

[0149] For example, if a function is called 100 times, the bytecode operation information obtained 99 times includes the addition of string type a and string type b, and the bytecode operation information obtained once includes the addition 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.

[0150] The backend information analysis module can calculate that, out of 100 function calls, the first operation accounted for 99% of the time, and the second operation accounted for 1%. The backend information analysis module can then associate "the first operation accounted for 99% of the time, and the second operation accounted for 1% of the time" with the function. For example, the backend information analysis module can associate "the first operation accounted for 99% of the time, and the second operation accounted for 1% of the time" with the function's function signature in ScoreBoard, as shown in Figure 9.

[0151] The following describes the function calling process in conjunction with Figure 10, which is a flow chart of a single function calling process. Referring to Figure 10, the process of the virtual machine calling a function may include:

[0152] S1001, the virtual machine calls a function.

[0153] S1002: The stack frame management module (call stack) creates a stack frame and stores a function signature in the stack frame.

[0154] The stack frame management module can allocate space for a function at the top of the stack frame. This allocated space can be called a stack frame. The process of allocating space for a function by the stack frame management module can be called the process of establishing a stack frame. The stack frame management module can also store function signatures in the stack frame.

[0155] 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.

[0156] During a function call, the interpreter can interpret and execute the bytecode, and the instrumentation module can obtain the type of the bytecode operand and send the type of the bytecode operand to the stack frame management module. Accordingly, 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 instrumentation module can obtain the type of the bytecode operation result and send the type of the bytecode operation result to the stack frame management module. Accordingly, the stack frame management module can store the type of the bytecode operation result in the stack frame.

[0157] S1004, the virtual machine exits the calling function.

[0158] S1005: The stack frame management module eliminates the stack frame.

[0159] The stack frame management module eliminates 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.

[0160] 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.

[0161] 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 based on the number of times the bytecode operation information appears.

[0162] In one embodiment, the electronic device can obtain the operation information of the bytecode during the execution of the bytecode, and the electronic device can determine the operation type of the bytecode based on the operation information of the bytecode. The electronic device can determine the operation type of the bytecode based on the semantics of the bytecode and the operation information of the bytecode. Exemplarily, the rules for determining the operation type of the bytecode can be predefined, such as predefining the operation type of the bytecode for calculation type bytecodes, and determining the operation type of the bytecode based on 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 based on the type of the operation result. Exemplarily, for bytecodes containing "get_loc", the operation type of the bytecode can be determined based on the type of the bytecode operands. Exemplarily, for an add operation, the electronic device can pop the two operands at the top of the running stack and then put in the sum of the two operands. The electronic device can determine the operation type of the bytecode based on the type of the operands involved in the operation.

[0163] 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.

[0164] In embodiments of the present application, electronic devices can not only use the method shown in FIG3 to determine the operation type of a bytecode based on the type of target data in each function corresponding to the bytecode, but can also determine the operation type of the bytecode during the bytecode execution process. Due to the presence of branch instructions in the bytecode, the operation types of the bytecode obtained using the method shown in FIG3 alone or the method of executing the bytecode alone may be missing for some bytecodes. In other words, using the method shown in FIG3 alone or the method of executing the bytecode alone will result in the electronic device being unable to fully obtain the operation types of the bytecode. Furthermore, because the control flow of a function varies between multiple executions, the operation types of the bytecode obtained each time the function is executed will also be different. In addition, the asynchronous mechanism of JS can disrupt the execution of the function, resulting in interruptions in the operation types obtained by the electronic device. For example, in the QuickJS virtual machine, triggering the async / await mechanism interrupts the execution of the function, placing the asynchronous function in the task list, and then placing the saved task back into the execution stack after the main function exits.

[0165] 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 embodiment of the present application can combine the operation types of the bytecode obtained by the two methods so that the electronic device can obtain a more comprehensive operation type of the bytecode.

[0166] In this embodiment, referring to FIG. 11 , the method for marking the operation type of the bytecode provided in the embodiment of the present application may include:

[0167] S1101, obtain bytecode.

[0168] S1102, obtaining type information of target data in each function corresponding to the bytecode.

[0169] S1103: Determine the first operation type of the bytecode according to the type information of the target data in each function.

[0170] 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.

[0171] S1104: Run the bytecode to obtain a second operation type of the bytecode.

[0172] S1104 may refer to the description in Figure 9. In the embodiment of the present application, the operation type of the bytecode obtained by running the bytecode may be used as the second operation type of the bytecode.

[0173] It should be understood that there is no order of precedence between S1102-S1103 and S1104, and the two can be executed simultaneously.

[0174] S1105 , obtaining an operation type of the bytecode according to the first operation type of the bytecode and the second operation type of the bytecode.

[0175] After obtaining the first operation type and the second operation type of the bytecode, the electronic device may fuse the first operation type and the second operation type to obtain the operation type of the bytecode. The electronic device may pre-store a fusion strategy, and the electronic device may fuse the first operation type and the second operation type according to the fusion strategy.

[0176] 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. For example, if the electronic device adopts the method shown in FIG3 , it may be determined 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 and 1B, the operation type of bytecode 2 is 2A, and the operation type of bytecode 3 is 3A. The electronic device fuses 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 and 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.

[0177] 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 an 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.

[0178] In one embodiment, if the same bytecode operation type exists in the first operation type and the second operation type, the electronic device may 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 may 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 may delete the operation type of the same bytecode in the first operation type and the second operation type.

[0179] 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.

[0180] 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 another embodiment, the electronic device may also use all the operation types of the bytecode as the operation type of the bytecode.

[0181] For example, Figure 6(a) shows the source code of the Fibonacci function, and Figure 6(b) shows the bytecode of the Fibonacci function. As shown in Figure 6(b), for each bytecode, the number preceding the bytecode represents the offset of the bytecode among all bytecodes. For example, for a line of bytecode "0: get_arg0", the "first number 0" in the line of bytecode indicates that the offset of the bytecode among all bytecodes is 0, and the "get_arg0" in the line of bytecode represents the specific content of the bytecode.

[0182] Table 1 shows the bytecode for the Fibonacci function. The electronic device adopts the method shown in Figure 3 and the method for running the bytecode to obtain different operation types of the bytecode of the function, as shown in Table 1. The four different operation types of the bytecode of the Fibonacci function. In one embodiment, the four different operation types of the bytecode of the Fibonacci function can be referred to as four versions of operation types. 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 different versions of the operation types. 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.

[0183] 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.

[0184] Exemplarily, the electronic device may adopt the method shown in FIG3 to obtain the first operation type of the bytecode corresponding to the function, but if the electronic device has not called the function, the electronic device may adopt the method of running the bytecode and may not be able to 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. Exemplarily, the electronic device may adopt the method shown in FIG3 to obtain the first operation type of the bytecode corresponding to the function, but the electronic device may adopt the method of running the bytecode and may obtain the second operation type of the bytecode corresponding to the function. In this case, the electronic device can only obtain the second operation type of the bytecode. Exemplarily, for some functions, the electronic device may adopt the method shown in FIG3 to obtain the first operation type of the bytecode corresponding to the function, and the electronic device may adopt the method of running the bytecode and may also obtain the second operation type of the bytecode corresponding to the function. In this case, the electronic device can obtain both the first operation type and the second operation type of the bytecode.

[0185] Table 1

[0186] As shown in Table 1, which uses bytecode offsets "0-33" to represent different bytecodes in the Fibonacci function, Table 1 shows the operation types of the four versions of the Fibonacci function bytecode, as well as the number of hits for each operation type. The four versions are version v1, version v2, version v3, and version v4. Based on the number of hits for each operation type and the total number of hits for the four versions, the electronic device can determine the percentage of each operation type in the Fibonacci function bytecode.

[0187] For example, taking version v1 as an example, the proportion of operation types in 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 bytecode version. Here, I represents an integer, S represents a string, B represents a Boolean, and O represents an object.

[0188] The "-" in Table 1 indicates that the control flow does not pass through this bytecode. Versions v1 and v2 have the same control flow, but the difference is that in version v1, the actual parameter received by the formal parameter n is the string "20". Since the result of the operation between the string "20" and the integer constant is an integer, the actual parameters received by the Fibonacci function in subsequent function calls are all integers, resulting in a hit value of 1 in version v1. In versions v3 and v4, the actual parameters corresponding to the formal parameter n are 0 and 1, respectively. Even though the actual parameter types are the same, the different values ​​result in different control flows and, in turn, different type versions.

[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 may also generate binary code based on the marked bytecode. For example, after marking the operation type of the bytecode, the operation type marking module may send the marked bytecode to the virtual code generation module. The virtual code generation module and the machine code generation module may cooperate to generate binary code based on the marked bytecode, as described in FIG2 .

[0193] In an embodiment of the present application, for bytecodes, because the electronic device can obtain the operation types of different versions of the bytecode, the electronic device can generate different versions of binary code based on the operation types of the different versions of the bytecode. For example, in an embodiment of the present application, the electronic device can obtain the operation types of different versions of the bytecode corresponding to each function on a function-by-function basis, so the electronic device can obtain different versions of binary code for each function based on the operation types 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 based on the templates of binary codes corresponding to the operation types of different versions of the bytecode.

[0195] Figure 12 is a schematic diagram of generating different versions of binary code provided by an embodiment of the present application. Referring to Figure 12, for example, for a function, the electronic device can use the operation type of three versions of the bytecode corresponding to the function, and the operation types of the three versions are V1, V2, and V3. 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, wasm V2, and wasm V3.

[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, the electronic device can generate different versions of binary code 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 code.

[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 the 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 embodiment of the present application further provides an electronic device, and FIG13 is another structural diagram of the electronic device provided in the embodiment of the present application. Referring to FIG13 , the electronic device may include: a processor 1301 (e.g., 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 storage. Various instructions may be stored in the memory 1302 to perform various processing functions and implement the method steps of the present application.

[0199] Optionally, the electronic device involved in this application may further include: a power supply 1303, a communication bus 1304, and a communication port 1305. The communication port 1305 is used to enable communication between the electronic device and other peripheral devices. In the embodiment of the present application, the memory 1302 is used to store computer-executable program code, which includes instructions; when the processor 1301 executes the instructions, the instructions cause the processor 1301 of the electronic device to perform the actions in the above-mentioned method embodiment. The implementation principles and technical effects are similar and 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), one or more microprocessors (digital signal processors (DSPs), or one or more field programmable gate arrays (FPGAs). For another example, when a module is implemented by scheduling program code through a processing element, the processing element may be a general-purpose processor, such as a central processing unit (CPU) or other processor that can call program code, 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 software is used for implementation, 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 the computer program instructions are loaded and executed 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 device. 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, computer, server or data center to another website, computer, server or data center via a wired (e.g., coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) method. The computer-readable storage medium can be any available medium that a computer can access or a data storage device such as a server or data center that includes one or more available media integrations. 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 drive (SSD)).

[0202] The term "plurality" in this article refers to two or more. The term "and / or" in this article is merely a description of the association relationship of associated objects, indicating that three relationships may exist. For example, A and / or B can represent three situations: 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 the description, and cannot be understood as indicating or implying relative importance, nor can they be understood as indicating or implying order.

[0203] It will be understood that the various numerical numbers involved in the embodiments of the present application are merely distinctions for the convenience of description and are not intended 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

Patent Citations

  • Bytecode operation type marking method and device and electronic equipment

    CN118733040A

  • Byte code verification method and device and storage medium

    CN112988157A

  • PHP taint analysis method and device based on byte codes

    CN113609481A

  • Log file updating method and device, equipment, medium and product

    CN114996107A