A bytecode instruction set simplification method and system

By converting or combining high-frequency instructions in the virtual machine into macro instructions, the problems of virtual machine bytecode storage space and execution performance are solved, and more efficient storage and execution efficiency is achieved.

CN112631724BActive Publication Date: 2025-01-07BEIJING WATCH DATA SYSTEM CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202011553334.3
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2020-12-24
Publication Date
2025-01-07
Estimated Expiration
2040-12-24

AI Technical Summary

Technical Problem

Existing virtual machine bytecodes have large storage space and low execution performance, especially in the presence of multiple adjacent functions of the same instructions and static method calls, there is room for optimization.

Method used

By converting existing instructions into one macro instruction or merging multiple instructions into one macro instruction, using static method calls and constant index frequency statistics and analysis, high-frequency instructions are identified and converted or merged, forming suitable for operation-based operations A streamlined method and system for bytecode instruction sets of number stacks and registers.

Benefits of technology

Reduce the storage space of bytecode, improve execution performance, and significantly saves storage space and execution time by replacing and merging instructions.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN112631724B_ABST
    Figure CN112631724B_ABST
Patent Text Reader

Abstract

The present invention discloses a bytecode instruction set simplification method and system, which is applicable to an operand stack-based and register-based instruction set. The simplification method includes: converting an existing instruction into a macro instruction or merging multiple instructions into a macro instruction. The present invention replaces the existing instruction or merges multiple instructions into a macro instruction, thereby reducing the storage space of the bytecode and improving the execution performance.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of virtual machine instruction sets, and in particular to a bytecode instruction set simplification method and system. Background Art

[0002] A virtual machine is an abstract computer generated by a software application or instruction sequence executed by a processor. A virtual machine can execute instruction sets supported by the virtual machine. Instruction sets include two types: operand stack-based and register-based instruction sets. The Java Card instruction set is based on the operand stack, and the Dalvik virtual machine instruction set is based on the register. The method for simplifying instructions proposed in the present invention is applicable to both of the above-mentioned instruction sets. Summary of the invention

[0003] In view of the defects existing in the prior art, the purpose of the present invention is to provide a bytecode instruction set simplification method and system to simplify the virtual machine bytecode, reduce the storage space of the bytecode, and improve the execution performance of the bytecode.

[0004] To achieve the above purpose, the technical solution adopted by the present invention is as follows:

[0005] The present invention provides a bytecode instruction set simplification method, which is applicable to an operand stack-based and register-based instruction set. The simplification method comprises:

[0006] Convert existing instructions into a macro instruction or combine multiple instructions into a macro instruction.

[0007] Further, in the above-mentioned streamlining method, converting the existing instruction into a macro instruction includes:

[0008] Step A1, counting the number of times each existing method call instruction in the API appears in SE applications of different industry types, and putting the existing method call instructions whose number reaches a first preset threshold into a candidate list;

[0009] Step A2, analyzing whether each existing method call instruction in the candidate list meets the applicable conditions for conversion into a macro instruction, and putting the existing method call instructions that meet the applicable conditions into a list to be converted;

[0010] Step A3, putting the list to be converted into a configuration file;

[0011] Step A4, based on the constant pool index of a static method call instruction, searching for information of the static method from the constant pool, and based on the information of the static method, searching from the configuration file whether there is a corresponding static method call instruction, and when it is determined that the static method call instruction exists in the list to be converted, converting the static method call instruction into a corresponding macro instruction.

[0012] Furthermore, in the above-mentioned streamlining method, the applicable conditions include:

[0013] 1) The existing method calling instruction is a static method calling instruction;

[0014] 2) The number of occurrences of the static method call instruction reaches the first preset threshold;

[0015] 3) The function implementation of the static method call instruction does not rely on other methods or domains, only processes the input parameters, and feeds back the result of the instruction execution by outputting parameters or returning values, and is implemented in the form of bytecode or native method.

[0016] Further, in the above-mentioned streamlining method, merging multiple instructions into one macro instruction includes:

[0017] Step B1: Count and analyze different bytecode instruction sets converted from SE applications of different industry types, identify at least two instructions that can be merged and put them into a list to be merged;

[0018] Step B2: determining whether to merge the at least two instructions that can be merged in the to-be-merged list based on the number of times the at least two instructions that can be merged appear;

[0019] Step B3, merging the at least two instructions that can be merged and whose number of times reaches a second preset threshold into one macro instruction;

[0020] The at least two instructions that can be combined include at least two instructions that complete specific functions or have the same functions.

[0021] Further, in the above-mentioned streamlining method, the at least two instructions for completing the specific functions are:

[0022] A constant assignment instruction and an array member access instruction used to implement constant-indexed array member access; or

[0023] A constant assignment instruction and a static method call instruction used to implement a static method call with a constant parameter.

[0024] A bytecode instruction set reduction system is applicable to an operand stack-based instruction set and a register-based instruction set, wherein the reduction system is used for:

[0025] Convert existing instructions into a macro instruction or combine multiple instructions into a macro instruction.

[0026] Further, in the streamlined system as described above, converting the existing instruction into a macro instruction includes:

[0027] Counting the number of times each existing method call instruction in the API appears in SE applications of different industry types, and putting the existing method call instructions whose number reaches a first preset threshold into a candidate list;

[0028] Analyze whether each existing method call instruction in the candidate list meets the applicable conditions for conversion into a macro instruction, and put the existing method call instructions that meet the applicable conditions into a list to be converted;

[0029] Put the list to be converted into a configuration file;

[0030] Based on a constant pool index of a static method calling instruction, information of the static method is searched from a constant pool, and based on the information of the static method, a search is made from the configuration file to determine whether a corresponding static method calling instruction exists. When it is determined that the static method calling instruction exists in the list to be converted, the static method calling instruction is converted into a corresponding macro instruction.

[0031] Furthermore, in the simplified system as described above, the applicable conditions include:

[0032] 1) The existing method calling instruction is a static method calling instruction;

[0033] 2) The number of times the static method call instruction is called reaches the first preset threshold;

[0034] 3) The function implementation of the static method call instruction does not rely on other methods or domains. It only processes the input parameters and feeds back the result of the instruction execution by outputting parameters or returning values. It is implemented in the form of bytecode or native method.

[0035] Further, in the streamlined system as described above, merging the multiple instructions into one macro instruction includes:

[0036] Count and analyze different bytecode instruction sets converted from SE applications of different industry types, identify at least two instructions that can be merged and put them into the list to be merged;

[0037] Determining whether to merge based on the number of occurrences of the at least two instructions that can be merged in the to-be-merged list;

[0038] Merging the at least two instructions that can be merged, the number of times of which reaches a second preset threshold, into one macro instruction;

[0039] The at least two instructions that can be combined include at least two instructions that complete specific functions or have the same functions.

[0040] Further, in the streamlined system as described above, the at least two instructions for completing the specific functions are:

[0041] A constant assignment instruction and an array member access instruction used to implement constant-indexed array member access; or

[0042] A constant assignment instruction and a static method call instruction used to implement a static method call with a constant parameter.

[0043] The beneficial effects of the present invention are as follows: the present invention is applicable to operand stack-based and register-based instruction sets, and can merge multiple adjacent instructions with the same function that appear multiple times into one macro instruction, merge multiple adjacent instructions that complete specific functions that appear multiple times into one macro instruction, and replace multiple static method calls that appear multiple times with one macro instruction. By replacing existing instructions or merging multiple instructions into one macro instruction, the storage space of bytecodes can be reduced and the execution performance can be improved. BRIEF DESCRIPTION OF THE DRAWINGS

[0044] Figure 1 A schematic diagram of a step-by-step flow chart of a bytecode instruction set simplification method provided in an embodiment of the present invention;

[0045] Figure 2 The present invention provides a step-by-step flowchart of a bytecode instruction set simplification method. DETAILED DESCRIPTION

[0046] The present invention will be further described in detail below in conjunction with the accompanying drawings and specific implementation methods.

[0047] The macroinstruction referred to in the present invention is an extension of the standard instruction set, has an independent opcode of one byte, can have one or more operands, can convert existing instructions or merge multiple instructions into one macroinstruction, thereby reducing the storage space of the bytecode and improving the execution performance. Since macroinstructions need to use an opcode like normal instructions, the opcodes of the existing instruction set use a size of one byte and are limited in number. Excessive increase in the number of instructions will also make the implementation of the instruction interpreter of the virtual machine complicated, increase the virtual machine code, and occupy more storage space. Therefore, the design priority condition of the macroinstruction is that the frequency of use should be high. Only with a high frequency can the complexity of the virtual machine implementation not be significantly increased, the size of the bytecode of the application can be reduced, and the execution performance of the application can be improved.

[0048] The method of the present invention is applicable to Java Card instruction set, Dalvik instruction set and TGoMOS instruction set. The method of the present invention is described below with reference to TGoMOS instruction set. TGoMOS instruction set is a reduced instruction set of micro-operating system bytecode, including six instructions.

[0049] The first instruction consists of an operation code, and the operation code implicitly contains parameter information of the first instruction; the first instruction includes: an instruction implicitly including both operands and register numbers in the operation code; an instruction implicitly including register numbers in the operation code; an instruction implicitly including constant operands in the operation code; an array member access instruction implicitly including the member type of an array member in the operation code; an instruction implicitly including the type and parameters of a method call in the operation code.

[0050] The second instruction is a high-frequency instruction with multiple instruction formats; the second instruction includes: commonly used arithmetic operation instructions with multiple operation formats; array member access instructions using a 4-bit register format; array creation instructions that implicitly embed the member type of the array member into the opcode; and branch instructions with multiple instruction formats based on equal and unequal comparison results.

[0051] The third instruction includes instructions with different instruction formats based on different numbers of parameters; the third instruction includes: static method call instructions with different instruction formats based on different numbers of parameters; virtual method call instructions with different instruction formats based on different numbers of parameters; private instance method call instructions with different instruction formats based on different numbers of parameters.

[0052] The fourth instruction includes instructions of commonly used data types and instructions of uncommon data types. The instructions of commonly used data types have multiple instruction formats, and the instructions of uncommon data types have one instruction format; the instructions of commonly used data types include instructions of short data types, and the instructions of uncommon data types include instructions of int data types.

[0053] The fifth instruction is an instruction with a single-byte constant pool index; the fifth instruction includes: a static method call instruction with a single-byte constant pool index; a virtual method call instruction with a single-byte constant pool index; a static domain access instruction with a single-byte constant pool index; and an instance domain access instruction with a single-byte constant pool index.

[0054] The sixth instruction is a macro instruction, and includes: a macro instruction formed by converting a static method call instruction and a macro instruction formed by merging multiple instructions.

[0055] The embodiment of the present invention provides a bytecode instruction set simplification method, which is applicable to an operand stack-based and register-based instruction set, including a Java Card instruction set, a Dalvik instruction set, and the aforementioned TGoMOS instruction set (such as the sixth instruction). The simplification method includes:

[0056] Convert existing instructions into a macro instruction or combine multiple instructions into a macro instruction.

[0057] like Figure 1 As shown, step A, converting the existing instruction into a macro instruction includes:

[0058] Step A1, counting the number of times each static method call instruction in the API appears in SE applications of different industry types, and putting the static method call instructions whose number reaches a first preset threshold into a candidate list;

[0059] Step A2: analyzing whether each static method call instruction in the candidate list meets the applicable conditions for conversion into a macro instruction, and putting the static method call instructions that meet the applicable conditions into a list to be converted;

[0060] Step A3, putting the list to be converted into a configuration file;

[0061] Step A4: based on the constant pool index of a static method call instruction, search the constant pool for information of the static method, and based on the information of the static method, search the configuration file for whether there is a corresponding static method call instruction. When it is determined that the static method call instruction exists in the list to be converted, convert the static method call instruction into a corresponding macro instruction.

[0062] Select SE applications from different industries such as finance, telecommunications, and transportation. These applications meet the standards of various industries, call different APIs to implement business logic, and then convert different bytecode instruction sets. The difficulty is moderate, and there is sufficient coverage and representativeness of application scenarios. Then, statistical analysis is performed on the converted bytecode instruction sets to identify instructions that can be replaced with macro instructions with high frequency. Whether to implement as a macro instruction needs to be determined based on the frequency of occurrence and the effect of the replacement after the number of instructions needs to be increased. It can be determined by setting a threshold. For example, if a static method call instruction appears more than 100 times in multiple typical applications, the static method called by the application is placed in the candidate list. The static method call instructions that meet the applicable conditions in the candidate list are placed in the list to be converted. The applicable conditions include: 1) It must be a static method in the API; 2) The number of application calls of the static method is high; 3) The functional implementation of the static method does not rely on other methods or domains, and only processes the input parameters. The result of the instruction execution is fed back by the output parameter or return value method, and it is implemented in the form of bytecode or native method. Native method generally refers to a local method implemented in C language or assembly language. Based on the above applicable conditions, the following static methods are more suitable for conversion into macro instructions as shown in Table 1.

[0063] Table 1

[0064]

[0065]

[0066] As can be seen from Table 1, the advantages of converting static method call instructions in APIs into macro instructions include that these instructions have a high number of calls, which can improve the execution performance of the application, reduce the size of the bytecode and thus save the storage space of the application, and the application developers do not need to pay attention to the implementation details of the macro instructions. In terms of instruction storage, there is no need for constant pool indexing, and the size of the instruction can be reduced by 1 or 2 bytes each time it appears. If the instruction appears N times, the size can be reduced by N or 2N bytes. In terms of instruction execution, the overhead of method calls is reduced, which improves the speed of instruction execution.

[0067] like Figure 2 As shown, step B, combining multiple instructions into one macro instruction includes:

[0068] Step B1: Count and analyze different bytecode instruction sets converted from SE applications of different industry types, identify at least two instructions that can be merged and put them into a list to be merged;

[0069] Step B2: determining whether to merge based on the number of occurrences of at least two instructions that can be merged in the to-be-merged list;

[0070] Step B3, merging at least two instructions that can be merged and whose number of times reaches a second preset threshold into one macro instruction;

[0071] The at least two instructions that can be combined include at least two instructions that complete specific functions or have the same functions.

[0072] SE applications from different industries such as finance, telecommunications, and transportation are selected. These applications meet the standards of various industries and then converted into different bytecode instruction sets with moderate difficulty and sufficient application scenario coverage and representativeness. Then, statistical analysis is performed on the converted bytecode instruction sets to identify two or more instructions that can be merged. Whether to implement it as a macro instruction needs to be determined based on the frequency of occurrence and the effect of the replacement after the number of instructions to be increased. This can be determined by setting a threshold. For example, if the instructions that can be merged appear more than 30 times in multiple applications, two or more instructions will be identified and implemented as a macro instruction.

[0073] For step B, the following instructions are included:

[0074] 1. Merge the instructions used to implement constant index array member access into macro instructions.

[0075] Accessing a specified member of an array is generally implemented by two instructions, namely, a constant assignment instruction and an array member access instruction. The two instructions on the left are combined into one instruction on the right, as shown in Table 2.

[0076] Table 2

[0077]

[0078]

[0079] As can be seen from Table 2, the const instruction can be removed, reducing 2 or 3 bytes. At the same time, the execution performance of the method can be improved by reducing the execution of one instruction.

[0080] 2. Merge the instructions for implementing a static method call with a constant parameter into macro instructions.

[0081] For example, in the aforementioned TGoMOS micro-operating system, an application calling the API in Table 3 below may throw an exception.

[0082] Table 3

[0083] Method Name Function tgo.core.ISOException.throwIt(short reason) throw an exception tgo.core.SystemException.throwIt(short reason) throw an exception tgo.core.UserException.throwIt(short reason) throw an exception tgo.core.TransactionException.throwIt(short reason) throw an exception tgo.core.CryptoException.throwIt(short reason) throw an exception

[0084] The method in Table 3 is generally converted into two instructions: a constant assignment instruction and a throwIt method call instruction of a specified class. These two instructions can be combined into corresponding macro instructions for implementation, as shown in Table 4.

[0085] Table 4

[0086]

[0087] As can be seen from Table 3, if an application has 292 throwIt method calls, the storage space can be saved by 292*3=876 bytes, and the execution time can be saved by the execution time of 292 const instructions.

[0088] 3. Combine multiple adjacent instructions with the same function into macro instructions, as shown in Table 5.

[0089] Table 5

[0090]

[0091] From Table 5, we can see that in addition to reducing one byte, macro instructions can also reduce the execution time of an instruction.

[0092] In addition, the virtual machine needs to support the implementation of the following macro instructions, as shown in Table 6.

[0093] Table 6

[0094]

[0095]

[0096] In order to verify that the streamlining method of the present invention has the technical effect of saving storage space and execution time, a typical financial application is taken as an example to count the number of calls of the following methods. As can be seen from Table 7, the storage space saved by calling all methods is 561*2=1122 bytes in total.

[0097] Table 7

[0098] Method Name Number of calls makeShort 15 getShort 98 setShort 67 arrayCopy 38 arrayCopyNonAtomic 258 arrayCompare 29 arrayFillNonAtomic 56 total 561

[0099] In order to prove that the streamlining method of the present invention has more outstanding technical effects than the prior art, the performance of static method call macro instructions in TGoMOS instruction set and Java Card instruction set are compared. In the virtual machine system implemented on the same chip, except for the bytecode interpreter code, other codes are basically the same. The average time of 1000 execution results is statistically compared. The comparison results are shown in Table 8.

[0100] Table 8

[0101]

[0102]

[0103] It can be seen from Table 8 that the macro instruction performance obtained by using the simplification method of the present invention is obviously better.

[0104] The present invention also provides a bytecode instruction set simplification system, which is applicable to an operand stack-based and register-based instruction set, and the simplification system comprises:

[0105] Convert existing instructions into a macro instruction or combine multiple instructions into a macro instruction.

[0106] Converting an existing instruction to a macro instruction involves:

[0107] Counting the number of times each existing method call instruction in the API appears in SE applications of different industry types, and putting the existing method call instructions whose number reaches a first preset threshold into a candidate list;

[0108] Analyze whether each existing method call instruction in the candidate list meets the applicable conditions for conversion into a macro instruction, and put the existing method call instructions that meet the applicable conditions into a list to be converted;

[0109] Put the list to be converted into a configuration file;

[0110] Based on the constant pool index of a static method calling instruction, the information of the static method is searched from the constant pool. Based on the information of the static method, it is searched from the configuration file whether there is a corresponding static method calling instruction. When it is determined that the static method calling instruction exists in the list to be converted, the static method calling instruction is converted into a corresponding macro instruction.

[0111] Applicable conditions include:

[0112] 1) The existing method call instruction is a static method call instruction;

[0113] 2) The number of times the static method call instruction is called reaches a first preset threshold;

[0114] 3) The function implementation of the static method call instruction does not rely on other methods or domains. It only processes the input parameters and feedbacks the result of the instruction execution by output parameters or return values. It is implemented in the form of bytecode or native method.

[0115] Combining multiple instructions into one macro instruction includes:

[0116] Count and analyze different bytecode instruction sets converted from SE applications of different industry types, identify at least two instructions that can be merged and put them into the list to be merged;

[0117] Determine whether to merge based on the number of times at least two instructions that can be merged appear in the to-be-merged list;

[0118] Merge at least two instructions that can be merged and whose number of times reaches a second preset threshold into one macro instruction;

[0119] The at least two instructions that can be combined include at least two instructions that perform specific functions or at least two instructions with the same functions.

[0120] At least two instructions to complete a specific function are:

[0121] A constant assignment instruction and an array member access instruction used to implement constant-indexed array member access; or

[0122] A constant assignment instruction and a static method call instruction used to implement a static method call with a constant parameter.

[0123] Obviously, those skilled in the art can make various changes and modifications to the present invention without departing from the spirit and scope of the present invention. Thus, if these modifications and variations of the present invention fall within the scope of the claims of the present invention and their equivalents, the present invention is also intended to include these modifications and variations.

Claims

1. A bytecode instruction set simplification method, applicable to operand stack-based and register-based instruction sets, characterized in that: The streamlining method comprises: Converting an existing instruction into a macro instruction or merging multiple instructions into a macro instruction includes the following steps: Step A1, counting the number of times each existing method call instruction in the API appears in SE applications of different industry types, and putting the existing method call instructions whose number reaches a first preset threshold into a candidate list; Step A2, analyzing whether each existing method call instruction in the candidate list meets the applicable conditions for conversion into a macro instruction, and putting the existing method call instructions that meet the applicable conditions into a list to be converted; Step A3, putting the list to be converted into a configuration file; Step A4, based on the constant pool index of a static method call instruction, searching for information of the static method from the constant pool, and based on the information of the static method, searching from the configuration file whether there is a corresponding static method call instruction, and when it is determined that the static method call instruction exists in the list to be converted, converting the static method call instruction into a corresponding macro instruction.

2. The streamlining method according to claim 1, characterized in that: The applicable conditions include: 1) The existing method calling instruction is a static method calling instruction; 2) The number of times the static method call instruction is called reaches the first preset threshold; 3) The function implementation of the static method call instruction does not rely on other methods or domains, only processes the input parameters, and feeds back the result of the instruction execution by outputting parameters or returning values, and is implemented in the form of bytecode or native method.

3. The streamlining method according to claim 1, characterized in that: The step of combining multiple instructions into one macro instruction includes: Step B1: Count and analyze different bytecode instruction sets converted from SE applications of different industry types, identify at least two instructions that can be merged and put them into a list to be merged; Step B2: determining whether to merge the at least two instructions that can be merged in the to-be-merged list based on the number of times the at least two instructions that can be merged appear; Step B3, merging the at least two instructions that can be merged and whose number of times reaches a second preset threshold into one macro instruction; The at least two instructions that can be combined include at least two instructions that complete specific functions or have the same functions.

4. The streamlining method according to claim 3, characterized in that: The at least two instructions for completing the specific functions are: A constant assignment instruction and an array member access instruction used to implement constant-indexed array member access; or A constant assignment instruction and a static method call instruction used to implement a static method call with a constant parameter.

5. A bytecode instruction set reduction system, applicable to operand stack-based and register-based instruction sets, characterized in that: The streamlined system is used to: Convert existing instructions into a macro instruction or combine multiple instructions into a macro instruction, including: Counting the number of times each existing method call instruction in the API appears in SE applications of different industry types, and putting the existing method call instructions whose number reaches a first preset threshold into a candidate list; Analyze whether each existing method call instruction in the candidate list meets the applicable conditions for conversion into a macro instruction, and put the existing method call instructions that meet the applicable conditions into a list to be converted; Put the list to be converted into a configuration file; Based on a constant pool index of a static method calling instruction, information of the static method is searched from a constant pool, and based on the information of the static method, a search is made from the configuration file to determine whether a corresponding static method calling instruction exists. When it is determined that the static method calling instruction exists in the list to be converted, the static method calling instruction is converted into a corresponding macro instruction.

6. The streamlined system according to claim 5, characterized in that: The applicable conditions include: 1) The existing method calling instruction is a static method calling instruction; 2) The number of times the static method call instruction is called reaches the first preset threshold; 3) The function implementation of the static method call instruction does not rely on other methods or domains, only processes the input parameters, and feeds back the result of the instruction execution by outputting parameters or returning values, and is implemented in the form of bytecode or native method.

7. The streamlined system according to claim 5, characterized in that: The step of combining multiple instructions into one macro instruction includes: Count and analyze different bytecode instruction sets converted by SE applications of different industry types, identify at least two instructions that can be merged and put them into the list to be merged; Determining whether to merge based on the number of occurrences of the at least two instructions that can be merged in the to-be-merged list; Merging the at least two instructions that can be merged, the number of times of which reaches a second preset threshold, into one macro instruction; The at least two instructions that can be combined include at least two instructions that complete specific functions or have the same functions.

8. The streamlined system according to claim 7, characterized in that: The at least two instructions for completing the specific functions are: A constant assignment instruction and an array member access instruction used to implement constant-indexed array member access; or A constant assignment instruction and a static method call instruction used to implement a static method call with a constant parameter.