Method, device and smart card for optimizing and executing a bytecode file
By optimizing the operand storage and encapsulation methods of bytecode files, the problem of large space occupation of bytecode files in embedded devices is solved, achieving more efficient storage and execution performance.
Patent Information
- Application Number
- CN202411265331.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-10
- Publication Date
- 2025-10-17
- Estimated Expiration
- 2044-09-10
AI Technical Summary
Existing bytecode files occupy a large amount of space and have low execution efficiency in embedded devices with limited storage space.
By sequentially traversing the bytecode instructions in the bytecode file, the target operand information is obtained and the index of the operand storage area is determined. The bytecode instructions are then repackaged according to the preset index value length to optimize the bytecode file.
It effectively reduces the storage space of bytecode files and improves execution efficiency, especially in smart card applications where it significantly reduces storage space usage and increases running speed.
Smart Images

Figure CN119225731B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of compiling, in particular to a bytecode file optimization method, an execution method, a bytecode file optimization device and a smart card. BACKGROUND
[0002] The bytecode instruction in the bytecode file is a binary instruction format of a stack-based virtual machine. It is a low-level assembly-like language with a compact binary format that can run close to native performance and provides a compilation target for languages such as C / C++.
[0003] However, in an embedded device with limited storage space resources, the existing bytecode file occupies a large space and has low execution efficiency. SUMMARY
[0004] Therefore, the bytecode file optimization method, the execution method, the bytecode file optimization device and the smart card provided by the embodiments of the present application can effectively solve the problem that the executable bytecode file corresponding to the existing smart card application occupies a large space and the like.
[0005] In a first aspect, the embodiments of the present application provide a bytecode file optimization method, comprising:
[0006] obtaining a bytecode file to be optimized;
[0007] sequentially traversing bytecode instructions in the bytecode file, obtaining a target bytecode instruction based on an instruction operation code of the target bytecode instruction, obtaining target operand information carried in each target bytecode instruction according to an instruction type of the target bytecode instruction, and determining an operand storage area corresponding to the instruction type;
[0008] determining an index of each target operand information in the corresponding operand storage area to obtain each target index value;
[0009] repackaging the corresponding target bytecode instruction according to each target index value and a preset index value length to obtain an optimized bytecode file.
[0010] In some embodiments, the repackaging the corresponding target bytecode instruction according to each target index value and a preset index value length comprises:
[0011] setting the length of the target index value to a preset byte length according to the instruction type of the target bytecode instruction;
[0012] replacing the target operand information in the target bytecode instruction with the target index value of the preset byte length.
[0013] In some embodiments, the sequentially traversing the bytecode instructions in the bytecode file, obtaining the target bytecode instruction based on the instruction operation code of the target bytecode instruction, obtaining the target operand information carried in each of the target bytecode instructions according to the instruction type of the target bytecode instruction, and determining the operand storage area corresponding to the instruction type, comprises:
[0014] sequentially traversing each bytecode instruction in the bytecode file, determining the instruction type of the currently traversed bytecode instruction according to the instruction operation code in the currently traversed bytecode instruction, if it is determined according to the instruction type that the currently traversed bytecode instruction is a constant load instruction, then determining the preset operand storage area as a constant field according to the instruction operation code of the constant load instruction, and determining the target operand information as the instruction operand of the constant load instruction;
[0015] The determination of the index of each of the target operand information in the corresponding operand storage area obtains a corresponding target index value, comprising:
[0016] obtaining the instruction operand in each of the constant load instructions and determining the index of each of the instruction operands in the constant field to obtain a corresponding first target index value;
[0017] The target index value of the preset byte length is used to replace the target operand information in the target bytecode instruction, comprising:
[0018] The first target index value of the set first preset byte length is used to replace the instruction operand in the corresponding constant load instruction.
[0019] In some embodiments, the sequentially traversing the bytecode instructions in the bytecode file, obtaining the target bytecode instruction based on the instruction operation code of the target bytecode instruction, obtaining the target operand information carried in each of the target bytecode instructions according to the instruction type of the target bytecode instruction, and determining the operand storage area corresponding to the instruction type, comprises:
[0020] sequentially traversing each bytecode instruction in the bytecode file, determining the instruction type of the currently traversed bytecode instruction according to the instruction operation code in the currently traversed bytecode instruction, if it is determined according to the instruction type that the currently traversed bytecode instruction is a stack operation instruction, then determining the preset operand storage area as an operand stack according to the instruction operation code of the stack operation instruction, and determining the target operand information as an operand address;
[0021] The determination of the index of each of the target operand information in the corresponding operand storage area obtains a corresponding target index value, comprising:
[0022] Obtaining an operand address in each stack operation instruction, and determining an index of each operand address in the operand stack to obtain a corresponding second target index value;
[0023] The replacing target operand information in the target bytecode instruction with the target index value of the preset byte length includes:
[0024] The target index value of the set second preset byte length is used to replace the operand address in the corresponding stack operation instruction.
[0025] In some embodiments, the method further includes: before determining the index of each target operand information in the operand storage area, determining whether the target operand information is in the operand storage area;
[0026] If the target operand information is not in the operand storage area, the target operand information is added to the operand storage area.
[0027] In a second aspect, an embodiment of the present application provides a bytecode file optimization device, comprising:
[0028] Bytecode file acquisition module, used to obtain the bytecode file to be optimized;
[0029] a target information acquisition module, configured to sequentially traverse the bytecode instructions in the bytecode file, acquire the target bytecode instructions based on the instruction opcodes of the target bytecode instructions, acquire target operand information carried in each target bytecode instruction according to the instruction type of the target bytecode instructions, and determine an operand storage area corresponding to the instruction type;
[0030] a target index value determination module, configured to determine the index of each target operand information in the corresponding operand storage area, and obtain corresponding target index values;
[0031] The encapsulation module is used to re-encapsulate the corresponding target bytecode instructions according to each target index value and the preset index value length to obtain an optimized bytecode file.
[0032] In a third aspect, an embodiment of the present application provides a bytecode execution method, comprising:
[0033] Loading an optimized bytecode file obtained by the bytecode file optimization method provided in the first aspect of the present application to obtain target bytecode instructions;
[0034] Determining a target operand storage area according to a target index value in a target bytecode instruction, and acquiring target operand information in the determined target operand storage area according to the target index value;
[0035] execute the target bytecode instruction according to the target operand information.
[0036] In some embodiments, the determining the target operand storage area according to the target index value in the target bytecode instruction, and the obtaining the target operand information in the target operand storage area according to the target index value, comprises:
[0037] If the value of the preset bit of the target index value is a first preset value, the target operand storage area is a constant field, and the target bytecode instruction is a constant loading instruction.
[0038] The corresponding target operand information is obtained in the constant field according to the target index value; wherein the target operand information is an instruction operand.
[0039] In some embodiments, the determining the target operand storage area according to the target index value in the target bytecode instruction, and the obtaining the target operand information in the target operand storage area according to the target index value, comprises:
[0040] If the value of the preset bit of the target index value is a second preset value, the target operand storage area is an operand stack, and the target bytecode instruction is a stack operation instruction.
[0041] The corresponding target operand information is obtained in the operand stack according to the target index value; wherein the target operand information is an operand address.
[0042] In a fourth aspect, an embodiment of the present application provides an intelligent card, comprising: the intelligent card is configured to implement the bytecode execution method according to the third aspect of the present application.
[0043] The embodiments of the present application have the following beneficial effects:
[0044] In the present application, the bytecode instructions in the bytecode file are traversed in sequence, the target bytecode instruction is obtained based on the instruction operation code of the target bytecode instruction, the target operand information carried in each target bytecode instruction is obtained according to the instruction type of the target bytecode instruction, and the operand storage area corresponding to the instruction type is determined; the index of each target operand information in the corresponding operand storage area is determined to obtain the corresponding target index value; and the corresponding target bytecode instruction is re-encapsulated according to each target index value and a preset index value length to obtain an optimized bytecode file. According to the target operand information, the corresponding target index value is determined, and the bytecode file is re-encapsulated according to the target index value. Thus, the problem that the existing executable bytecode file corresponding to the intelligent card application occupies a large space can be effectively solved. BRIEF DESCRIPTION OF DRAWINGS
[0045] In order to more clearly illustrate the technical solutions of the embodiments of the present application, the drawings needed to be used in the embodiments will be briefly introduced as follows. It should be understood that the following drawings only show some of the embodiments of the present application, and therefore should not be regarded as a limitation on the scope, and for those skilled in the art, other related drawings can also be obtained without creative labor on the basis of these drawings.
[0046] Figure 1 A flow chart of the optimization method of the bytecode file according to an embodiment of the present application is shown;
[0047] Figure 2 Another flow chart of the optimization method of the bytecode file according to an embodiment of the present application is shown;
[0048] Figure 3 A flow chart of the execution method of the bytecode according to an embodiment of the present application is shown;
[0049] Figure 4 A structural schematic diagram of the optimization device of the bytecode file according to an embodiment of the present application is shown;
[0050] Figure 5 A structural schematic diagram of the execution device of the bytecode according to an embodiment of the present application is shown.
[0051] Main element symbol explanation:
[0052] 410-bytecode file acquisition module; 420-target information acquisition module; 430-target index value determination module; 440-encapsulation module; 510-loading module; 520-operational number determination module; 530-execution module. DETAILED DESCRIPTION
[0053] The technical solutions in the embodiments of the present application will be described clearly and completely below in combination with the drawings in the embodiments of the present application. Obviously, the described embodiments are only some of the embodiments of the present application, but not all the embodiments.
[0054] The components of the embodiments of the present application generally described and shown in the drawings herein can be arranged and designed in various different configurations. Therefore, the following detailed description of the embodiments of the present application provided in the drawings is not intended to limit the scope of the claimed present application, but only represents selected embodiments of the present application. Based on the embodiments of the present application, all other embodiments obtained by those skilled in the art without creative labor are within the scope of protection of the present application.
[0055] Hereinafter, the terms "include", "has", and similar terms used in the various embodiments of the present application are merely intended to denote that there is a certain feature, number, step, operation, element, component, or combination thereof, and are not intended to exclude the presence or addition of one or more other features, numbers, steps, operations, elements, components, or combinations thereof.
[0056] Unless defined otherwise, all terms used herein (including technical and scientific terms) have the same meaning as commonly understood by one of ordinary skill in the art to which various embodiments of the present application belong. Such terms, as commonly used in the art, are to be interpreted in the same way as they are interpreted in the context of the relevant art and will not be interpreted in an idealized or overly formal sense unless clearly defined in various embodiments of the present application.
[0057] Some embodiments of the present application will be described in detail below with reference to the attached drawings. The following embodiments and features of the embodiments described below can be combined with each other unless there is a conflict.
[0058] In order to optimize the storage space occupied by the smart card application (corresponding to the bytecode file) in the resource-limited embedded device, the present application provides a bytecode file optimization method, execution method, device and smart card, which optimizes the binary structure of the bytecode file, thereby reducing the size of the smart card application.
[0059] The bytecode file is composed of a series of byte sequences, which contains instructions, constants and metadata of functions that define program logic and data. The bytecode file structure mainly includes the following key parts:
[0060] (1) Magic Number.
[0061] For example, the wasm file starts with a `wasm` string (hexadecimal representation `77 61 72 6d`) as a version identification mark, indicating that the file format is WebAssembly.
[0062] (2) Version Number.
[0063] The second part is the version number, which indicates the version information of the file. It is composed of two bytes, where the first byte represents the major version number and the second byte represents the minor version number.
[0064] (3) Sections.
[0065] (3-1) Import Section: Import declarations are used to describe the import of external resources, such as global variables, functions, types, etc. imported from JavaScript, other wasm modules, or other resources.
[0066] (3-2) Type Section: This section is used to define all data types within the module, including function types, global variable types, and internal module types.
[0067] (3-3) Function Section: This section is used to define functions within the module, including their parameters and return value types.
[0068] (3-4) Table Section: This section is used to describe information about indirect method calls.
[0069] (3-5) Memory Section: This section is used to describe the configuration information of memory, including the initial size, maximum size, and alignment attributes.
[0070] (3-6) Global Section: This section is used to define the types and initial values of global variables.
[0071] (3-7) Export Section: This section is used to specify which functions, global variables, and module types can be accessed externally, as well as their externally visible names.
[0072] (3-8) Code Section: This section contains the actual wasm instruction set and the function code of the execution program.
[0073] (3-10) Data Section: This section is used to store the initialization values of application variables, which are used to initialize memory space.
[0074] The instruction types of the bytecode instructions in the bytecode file include control instructions, parameter instructions, variable instructions, memory instructions, numerical instructions, etc. Constant instructions belong to a type of numerical instructions.
[0075] Load Constant Instruction: In the bytecode file, the load constant instruction is used to load values from memory into the operand stack, which can then be used in subsequent instructions for calculation or operation. This has the benefits of reducing runtime memory access, improving execution efficiency, and making the code more compact and secure.
[0076] Constant instructions: For example, `i32.const` / `f32.const` / `f64.const`: used to create integer, single-precision floating-point, or double-precision floating-point constants. For example, `i64.const` / `f64.const`: used to create integer (64-bit) or double-precision floating-point constants.
[0077] Stack Operation Instructions: Stack operation instructions are related to manipulating values on the stack. They include, but are not limited to, push, pop, and copy. Stack operation instructions are typically used to perform calculations, store, and manage data.
[0078] The following describes the optimization method for the bytecode file with reference to some specific embodiments.
[0079] Figure 1 A flow chart of a bytecode file optimization method according to an embodiment of the present application is shown. Exemplarily, the bytecode file optimization method includes the following steps:
[0080] S110: Obtain the bytecode file to be optimized.
[0081] S120, sequentially traverse the bytecode instructions in the bytecode file, obtain the target bytecode instructions based on the instruction opcodes of the target bytecode instructions, obtain target operand information carried by each target bytecode instruction based on the instruction type of the target bytecode instructions, and determine an operand storage area corresponding to the instruction type. Exemplarily, the operand storage area is a constant segment or an operand stack.
[0082] The target bytecode instructions include various instruction types. For example, constant load instructions, stack operation instructions, etc. When traversing the bytecode file, it is necessary to obtain the target bytecode instructions corresponding to each instruction type. The operand storage area is a pre-defined storage area.
[0083] S130, determining the index of each target operand information in the corresponding operand storage area, and obtaining corresponding target index values. The target operand information includes instruction operands, operand addresses, etc.
[0084] S140: Re-encapsulate the corresponding target bytecode instructions according to each target index value and the preset index value length to obtain an optimized bytecode file.
[0085] In one embodiment, in step S140, repackaging the corresponding target bytecode instruction according to each target index value and the preset index value length includes:
[0086] S141, setting a length of the target index value as a preset byte length according to an instruction type of the target bytecode instruction.
[0087] Generally, a bytecode instruction is composed of an instruction operation code of 1 byte and instruction operands of several bytes. The instruction operation code represents a specific instruction operation, such as loading a constant, storing a value to a memory, calling a function, etc. The instruction operands follow the instruction operation code and represent actual parameters of the instruction, the length of which is determined by the instruction operation code. In order to optimize a bytecode file, the present application optimizes the byte length of the instruction operands in a target bytecode instruction.
[0088] S142, replacing target operand information in the target bytecode instruction with the target index value of the preset byte length.
[0089] For example, the bytecode before encapsulation is 00 02 03 04 02 03, wherein 02 03 is a first constant (two bytes), the preset constant field is 07 08 09 02 03, wherein the index value of 07 08 09 (three bytes) is 00, and the index value of 02 03 is 01. When the first constant "02 03" is referenced subsequently, the first constant "02 03" can be obtained through the index value "01", that is, the bytecode after encapsulation is 00 01 04 01, and the bytecode length is greatly optimized, reducing the storage space.
[0090] In an embodiment, the target bytecode instruction includes a constant load instruction; and the instruction operand storage area of the constant load instruction includes a constant field.
[0091] Further, the sequentially traversing the bytecode instructions in the bytecode file, obtaining the target bytecode instruction based on the instruction operation code of the target bytecode instruction, obtaining the target operand information carried in each target bytecode instruction according to the instruction type of the target bytecode instruction, and determining the operand storage area corresponding to the instruction type, include:
[0092] sequentially traversing each bytecode instruction in the bytecode file, determining the instruction type of the currently traversed bytecode instruction according to the instruction operation code in the currently traversed bytecode instruction, if the currently traversed bytecode instruction is determined to be a constant load instruction according to the instruction type, determining the preset operand storage area as a constant field according to the instruction operation code of the constant load instruction, and determining the target operand information as the instruction operand of the constant load instruction.
[0093] The bytecode instruction, i.e., the function code of the execution program, is located in CodeSection (code section).
[0094] Exemplarily, the last bytecode is usually a terminator for marking the end of the instruction sequence. The hexadecimal representation of the terminator is FF, and its binary form is 11111111. The present application sequentially traverses the bytecode instructions in the bytecode file based on the terminator.
[0095] The instruction opcode in the constant load instruction indicates which type of constant, such as an integer, a floating-point number, etc., and different types of instruction opcodes are different.
[0096] Further, the determining of the index of each target operand information in the corresponding operand storage area to obtain a corresponding target index value comprises:
[0097] The instruction operand in each constant load instruction is obtained, and the index of each instruction operand in the constant field is determined to obtain a corresponding first target index value.
[0098] Further, the replacing of the target operand information in the target bytecode instruction with the target index value of the preset byte length comprises:
[0099] The instruction operand in the corresponding constant load instruction is replaced with the first target index value of the set first preset byte length.
[0100] For example, the constant load instruction is 00 01, wherein 00 represents the instruction opcode (also known as the instruction header) of the constant load instruction, and 01 represents the first constant. The preset constant field stores data as 03 02 01, so the first constant "01" exists in the preset constant field, and the index value of the first constant "01" in the preset constant field is 02.
[0101] The first preset byte length is 1 byte or 2 bytes. In the present application, the index value (target index value) corresponding to the instruction operand is represented by 1 byte or 2 bytes, while in the prior art, the instruction operand in the bytecode instruction is represented by 2 bytes; in the present application, the highest bit represents 1 byte or 2 bytes, and the next highest bit represents the source of the instruction operand.
[0102] In an embodiment, the target bytecode instruction comprises a stack operation instruction; the target operand information comprises an operand address; and the operand storage area comprises an operand stack.
[0103] Further, the sequentially traversing the bytecode instructions in the bytecode file, obtaining the target bytecode instruction based on an instruction operation code of the target bytecode instruction, obtaining target operand information carried in each of the target bytecode instructions according to an instruction type of the target bytecode instruction, and determining an operand storage area corresponding to the instruction type, comprises:
[0104] sequentially traversing each bytecode instruction in the bytecode file, determining an instruction type of the currently traversed bytecode instruction according to an instruction operation code in the currently traversed bytecode instruction, if it is determined according to the instruction type that the currently traversed bytecode instruction is a stack operation instruction, then determining, according to an instruction operation code of the stack operation instruction, that a preset operand storage area is an operand stack, and determining that the target operand information is an operand address.
[0105] Further, the determining an index of each of the target operand information in the corresponding operand storage area to obtain a corresponding target index value comprises:
[0106] obtaining an operand address in each of the stack operation instructions and determining an index of each of the operand addresses in the operand stack to obtain a corresponding second target index value.
[0107] Further, the using the target index value of the preset byte length to replace the target operand information in the target bytecode instruction comprises:
[0108] using the target index value of a set second preset byte length to replace the operand address in the corresponding stack operation instruction.
[0109] The second preset byte length is 1 byte or 2 bytes. In the present application, the index value (target index value) corresponding to the instruction operand is represented by 1 byte or 2 bytes, while in the prior art, the operand address in the bytecode instruction is represented by 2 bytes.
[0110] For example, the stack operation instruction is: 04 05, 04 represents the instruction operation code of the stack operation instruction, wherein the type of the instruction can be determined according to the instruction operation code, 05 is the instruction operand of the stack operation instruction, the address of which is 09, the operand stack is: 05 06 07 08, and the index value of 05 in the operand stack is 00. The address of the instruction operand in the stack operation instruction is replaced by the index value of the instruction operand in the operand stack, so that 09 in the stack operation instruction is replaced by 00. Thus, each time the instruction operand 05 is used, the instruction operand can be directly called in the operand stack through the index value 00, and it is not necessary to obtain the instruction operand according to the operand address, which not only optimizes the bytecode file, but also improves the execution speed.
[0111] In an embodiment, the method further comprises:
[0112] Before the determining the index of each of the target operand information in the operand storage area, it is further needed to judge whether the target operand information is in the operand storage area or not.
[0113] If the target operand information is not in the operand storage area, the target operand information is added into the operand storage area. Wherein, the instruction type of the target bytecode instruction includes constant load instruction and stack operation instruction, the corresponding target operand information includes instruction operand and operand address, and the corresponding operand storage area includes constant field and operand stack. Specifically, for the constant load instruction, it is judged whether the instruction operand of each constant load instruction is in the constant field or not; if the instruction operand of the constant load instruction is not in the constant field, the instruction operand of the constant load instruction is added into the constant field. For the stack operation instruction, it is judged whether the operand address of the stack operation instruction is in the operand stack or not; if the operand address of the stack operation instruction is not in the operand stack, the operand address of the stack operation instruction is added into the operand stack.
[0114] Suppose the constant load instruction is 00 04, 00 represents the instruction operation code of the constant load instruction, and 04 represents the first constant, and the preset constant field is 03 02 01, then the first constant does not exist in the preset constant field, the first constant "04" is added to the preset constant field, and the new constant field is 03 02 01 04, and the index value of the first constant "04" in the preset constant field is 03; thus, each time the first constant "04" is referenced, the index "03" is directly referenced, and the first constant does not need to be imported each time, thereby improving the execution speed.
[0115] The optimization method of the bytecode file of the present application will be introduced below in combination with a specific example. In the present example, the wasm bytecode file is taken as an example for illustration, as shown in Figure 2 The following steps are included:
[0116] S101, a bytecode file corresponding to a smart card application is obtained, and the bytecode instructions in the bytecode file are traversed.
[0117] S102, the instruction type of the bytecode instruction currently traversed is judged. If it is a constant load instruction, step S103 is executed, and if it is a stack operation instruction, step S104 is executed.
[0118] S103, obtaining the first constant carried by the constant load instruction, and judging whether the first constant exists in the preset constant field; if the first constant exists, obtaining the index value of the first constant in the preset constant field and recording the index value as a constant index; if the first constant does not exist, adding the first constant to the preset constant field and recording the index value of the first constant in the constant field as the constant index. The first constant in the constant load instruction is replaced by the constant index.
[0119] S104, replacing the operand address corresponding to the stack operation instruction with the index of the operand address in the operand stack, recording the index of the instruction operand in the operand stack as an operand stack index, and replacing the operand address corresponding to the stack operation instruction with the operand stack index.
[0120] 105, judging whether the bytecode instruction currently traversed is the last bytecode instruction in the bytecode file; if yes, ending the traversal; otherwise, returning to step S102.
[0121] Figure 3 A flow chart of the bytecode execution method according to an embodiment of the present application is shown. The bytecode execution method according to an embodiment of the present application includes the following steps:
[0122] S210, loading an optimized bytecode file obtained based on the optimization method of the bytecode file according to an embodiment of the present application, to obtain target bytecode instructions;
[0123] S220, determining a target operand storage area according to a target index value in the target bytecode instruction, and obtaining target operand information in the determined target operand storage area according to the target index value.
[0124] Further, the target operand storage area includes an operand stack and a constant field.
[0125] The highest bit of the target index value is used to distinguish whether the index value is 1 byte valid or 2 bytes valid, and the second highest bit of the index value is used to distinguish whether the index value represents an index of the operand stack or an index of the constant field. For example, one byte has 8 bits, the leftmost bit is the highest bit, the second bit from the left is the second highest bit, and the rightmost bit is the lowest bit. Each position represents different information. For another example, if the highest bit is 0, it represents 1 byte valid, and if the highest bit is 1, it represents 2 bytes valid.
[0126] S230, executing the target bytecode instruction according to the target operand information.
[0127] In an embodiment, the step of determining a target operand storage area according to a target index value in the target bytecode instruction, and obtaining target operand information in the determined target operand storage area according to the target index value includes:
[0128] If the value of the preset bit of the target index value is a first preset value, the target operand storage area is a constant field, and the target bytecode instruction is a constant load instruction.
[0129] According to the target index value, corresponding target operand information is obtained in the constant field; wherein the target operand information is an instruction operand.
[0130] In an embodiment, the determining of the target operand storage area according to the target index value corresponding to the target bytecode instruction, and the obtaining of the target operand information according to the target index value in the target operand storage area, comprises:
[0131] If the value of the preset bit of the target index value is a second preset value, the target operand storage area is an operand stack, and the target bytecode instruction is a stack operation instruction.
[0132] According to the target index value, corresponding target operand information is obtained in the constant field; wherein the target operand information is an instruction operand.
[0133] For example, a byte has 8 bits, the leftmost bit is the highest bit, the second bit from the left is the second highest bit, and the rightmost bit is the lowest bit. The number in each position represents different information. The preset bit of the target index value is the second highest bit. If the value of the second highest bit is 0 (the first preset value), it is determined that the target operand storage area is a constant field. If the value of the second highest bit is 1 (the second preset value), it is determined that the target operand storage area is an operand stack.
[0134] In summary, the type corresponding to the target index value is determined according to the preset byte of the target index value. If the target index value is an operand stack index, the target operand information is obtained from the operand stack based on the operand stack index and the stack operation instruction is updated. If it is a constant field index, the target operand information is obtained from the preset constant field based on the constant field index and the constant load instruction is updated.
[0135] Further, the runtime of the present application adopts a wasm runtime. Once the source code is compiled into bytecode, the wasm runtime will be needed to execute them. The wasm runtime provides all the functions and advantages usually associated with wasm, such as sandbox security, speed, and cross-platform portability. While WebAssembly Micro Runtime (WAMR) is a lightweight standalone WebAssembly (wasm) runtime with small size, high performance, and high configurability, suitable for applications from embedded, Internet of Things, edge to Trusted Execution Environment (TEE), smart contract, cloud native, etc. Therefore, the runtime of the present application adopts a wasm runtime.
[0136] In the method for parsing the wasm file in the application, the bytecode is traversed, if it is a constant loading instruction, the constant (instruction operand) carried by the constant loading instruction is extracted, and it is checked whether there is a same constant value in the newly added const segment; if found, the index of the constant in the const segment is recorded; if not found, the constant is added to the last of the const segment, and the index of the constant in the const segment is recorded. All instructions in the bytecode file referring to the constant are modified to refer to the index corresponding to the constant. The optimization operation can reduce the code redundancy caused by repeated constants.
[0137] In the method for parsing the wasm file in the application, the bytecode is traversed, if it is a stack operation instruction, the operand address is replaced by the index of the operand address in the operand stack. According to the highest bit of the index, it is distinguished whether the index is 1 byte valid or 2 byte valid, and according to the second highest bit of the index, it is distinguished whether the index represents the index of the operand stack or the index of the constant. The optimization operation can reduce the space occupied by the instruction operand.
[0138] In the application, the instruction operand in the constant loading instruction and the operand address in the stack operation instruction are replaced by the index value in the constant field and the index value in the operand stack, respectively. In the application, the index of the instruction operand is represented by 1 byte or 2 bytes instead of 2 bytes in the original instruction, and the highest bit is used to represent 1 byte or 2 byte valid, and the second highest bit is used to represent the source of the instruction operand. Thus, the space occupied by the instruction operand can be reduced. The storage space occupied by the constant data after the improvement is reduced by 80% compared with that before the improvement. The space occupied by the bytecode instruction after the improvement is reduced by 30% compared with that before the improvement.
[0139] The application is based on the Intel wamr interpreter (fast) mode applied to the management platform of the smart card application, and the application file loading and running are realized. Through the test and verification of the industry application, the application running efficiency of the application is slightly higher than that of other virtual machine smart card application management platforms.
[0140] Figure 4 A structural schematic diagram of the bytecode file optimization device of the embodiment of the application is shown. Exemplarily, the bytecode file optimization device includes a bytecode file acquisition module 410, a target information acquisition module 420, a target index value determination module 430, and a packaging module 440.
[0141] The bytecode file acquisition module 410 is used to acquire the bytecode file to be optimized;
[0142] The target information obtaining module 420 is configured to sequentially traverse bytecode instructions in the bytecode file, obtain the target bytecode instruction based on an instruction operation code of the target bytecode instruction, obtain target operand information carried in each target bytecode instruction according to an instruction type of the target bytecode instruction, and determine an operand storage area corresponding to the instruction type.
[0143] The target index value determining module 430 is configured to determine an index of each target operand information in the corresponding operand storage area, and obtain a corresponding target index value.
[0144] The encapsulating module 440 is configured to re-encapsulate the corresponding target bytecode instruction according to each target index value and a preset index value length, and obtain an optimized bytecode file.
[0145] It can be understood that the device of the embodiment corresponds to the bytecode file optimization method of the above-described embodiment, and the optional items in the above-described embodiment are also applicable to the embodiment, and thus are not repeatedly described herein.
[0146] Figure 5 A structural schematic diagram of a bytecode execution device is shown. The bytecode execution device includes a loading module 510, an operand determining module 520, and an execution module 530.
[0147] The loading module 510 is configured to load an optimized bytecode file obtained based on the bytecode file optimization method of the present application, and obtain each target bytecode instruction.
[0148] The operand determining module 520 is configured to determine a target operand storage area according to a target index value in a target bytecode instruction, and obtain target operand information in the determined target operand storage area according to the target index value.
[0149] The execution module 530 is configured to execute the target bytecode instruction according to the target operand information.
[0150] It can be understood that the device of the embodiment corresponds to the bytecode execution method of the above-described embodiment, and the optional items in the above-described embodiment are also applicable to the embodiment, and thus are not repeatedly described herein.
[0151] The present application also provides a smart card. The smart card includes a processor and a memory. The memory stores a computer program. The processor runs the computer program, so that the smart card executes the bytecode execution method or the functions of each module in the bytecode execution device.
[0152] The application further provides a terminal device, which exemplarily comprises a processor and a memory, wherein the memory stores a computer program, and the processor executes the computer program to enable the terminal device to perform the functions of each module in the bytecode file optimization method or the bytecode file optimization apparatus.
[0153] The processor can be an integrated circuit chip with a processing capability of signals. The processor can be a general processor, including a central processing unit (CPU), a graphics processing unit (GPU), a network processor (NP), a digital signal processor (DSP), an application specific integrated circuit (ASIC), a field programmable gate array (FPGA) or other programmable logic devices, a discrete gate or transistor logic device, a discrete hardware component, or at least one of the above. The general processor can be a microprocessor or the processor can also be any conventional processor or the like, which can realize or execute the disclosed methods, steps and logic block diagrams in the embodiments of the application.
[0154] The memory can be, but is not limited to, a random access memory (RAM), a read only memory (ROM), a programmable read-only memory (PROM), an erasable programmable read-only memory (EPROM), an electrically erasable programmable read-only memory (EEPROM) and the like. The memory is used to store a computer program, and the processor can execute the computer program after receiving an execution instruction.
[0155] The application further provides a computer readable storage medium for storing the computer program used in the terminal device. For example, the computer readable storage medium can include, but is not limited to, a U disk, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk and various program code storage media.
[0156] In several embodiments provided in the present application, it should be understood that the disclosed apparatus and method can also be implemented by other means. The apparatus embodiments described above are only illustrative, for example, the flowcharts and structural diagrams in the drawings show the possible implementation architecture, function and operation of the apparatus, method and computer program product according to the embodiments of the present application. In this regard, each block in the flowchart or block diagram can represent a module, a program segment or a part of code, which contains one or more executable instructions for implementing the specified logic function. It should also be noted that in alternative implementation, the functions noted in the block can also occur in different order from that noted in the drawings. For example, two consecutive blocks can actually be executed substantially in parallel, and sometimes they can be executed in reverse order, depending on the functions involved. It should also be noted that each block in the structural diagram and / or flowchart, and the combination of blocks in the structural diagram and / or flowchart, can be implemented by a dedicated hardware-based system for executing the specified function or action, or can be implemented by a combination of dedicated hardware and computer instructions.
[0157] In addition, each functional module or unit in the embodiments of the present application can be integrated together to form an independent part, or each module can exist independently, or two or more modules can be integrated to form an independent part.
[0158] If the functions are implemented in the form of software function modules and sold or used as independent products, they can be stored in a computer readable storage medium. Based on this understanding, the technical solutions of the present application can be embodied in the form of a software product, which is stored in a storage medium and includes a number of instructions for causing a computer device (which can be a smart phone, a personal computer, a server, or a network device, etc.) to execute all or part of the steps of the methods described in the embodiments of the present application.
[0159] The above description is only a specific implementation of the present application, but the protection scope of the present application is not limited thereto, and any person skilled in the art can easily think of changes or replacements within the technical scope disclosed in the present application, which should be covered within the protection scope of the present application.
Claims
1. A method for optimizing a bytecode file, characterized in that: include: Get the bytecode file to be optimized; Traversing the bytecode instructions in the bytecode file in sequence, obtaining the target bytecode instruction based on the instruction opcode of the target bytecode instruction, obtaining target operand information carried in each target bytecode instruction according to the instruction type of the target bytecode instruction, and determining the operand storage area corresponding to the instruction type, specifically comprising: traversing each bytecode instruction in the bytecode file in sequence, determining the instruction type of the currently traversed bytecode instruction according to the instruction opcode in the currently traversed bytecode instruction, if it is determined that the currently traversed bytecode instruction is a constant load instruction according to the instruction type, then determining a preset operand storage area as a constant field according to the instruction opcode of the constant load instruction, and determining that the target operand information is the instruction operand of the constant load instruction; Determining the index of each target operand information in the corresponding operand storage area to obtain corresponding target index values, specifically comprising: obtaining the instruction operand in each constant load instruction, and determining the index of each instruction operand in the constant field to obtain the corresponding first target index value; According to each of the target index values and the preset index value length, the corresponding target bytecode instructions are repackaged to obtain an optimized bytecode file, specifically including: setting the length of the target index value to a preset byte length according to the instruction type of the target bytecode instruction; using the target index value of the preset byte length to replace the target operand information in the target bytecode instruction; wherein, using the target index value of the preset byte length to replace the target operand information in the target bytecode instruction includes: using the first target index value of the set first preset byte length to replace the instruction operand in the corresponding constant load instruction.
2. The bytecode file optimization method according to claim 1, characterized in that: The step of sequentially traversing the bytecode instructions in the bytecode file, obtaining the target bytecode instruction based on the instruction opcode of the target bytecode instruction, obtaining target operand information carried in each target bytecode instruction according to the instruction type of the target bytecode instruction, and determining the operand storage area corresponding to the instruction type further includes: traversing each bytecode instruction in the bytecode file in sequence, determining an instruction type of the currently traversed bytecode instruction based on an instruction opcode in the currently traversed bytecode instruction, and if it is determined based on the instruction type that the currently traversed bytecode instruction is a stack operation instruction, determining based on the instruction opcode of the stack operation instruction that a preset operand storage area is an operand stack, and determining the target operand information as an operand address; Determining the index of each target operand information in the corresponding operand storage area to obtain the corresponding target index value includes: Obtaining an operand address in each stack operation instruction, and determining an index of each operand address in the operand stack to obtain a corresponding second target index value; The replacing target operand information in the target bytecode instruction with the target index value of the preset byte length includes: The target index value of the set second preset byte length is used to replace the operand address in the corresponding stack operation instruction.
3. The bytecode file optimization method according to claim 1 or 2, characterized in that: The method further comprises: Before determining the index of each target operand information in the operand storage area, it is also necessary to determine whether the target operand information is in the operand storage area; If the target operand information is not in the operand storage area, the target operand information is added to the operand storage area.
4. A bytecode file optimization device, characterized in that: include: Bytecode file acquisition module, used to obtain the bytecode file to be optimized; a target information acquisition module, configured to sequentially traverse the bytecode instructions in the bytecode file, acquire the target bytecode instructions based on the instruction opcodes of the target bytecode instructions, acquire target operand information carried in each target bytecode instruction according to the instruction type of the target bytecode instruction, and determine an operand storage area corresponding to the instruction type; the target information acquisition module is specifically configured to: sequentially traverse each bytecode instruction in the bytecode file, determine the instruction type of the currently traversed bytecode instruction according to the instruction opcode in the currently traversed bytecode instruction, if the currently traversed bytecode instruction is determined to be a constant load instruction according to the instruction type, determine the preset operand storage area to be a constant field according to the instruction opcode of the constant load instruction, and determine the target operand information to be the instruction operand of the constant load instruction; a target index value determination module, configured to determine the index of each target operand information in the corresponding operand storage area to obtain the corresponding target index value; the target index value determination module is specifically configured to: obtain the instruction operand in each constant load instruction, and determine the index of each instruction operand in the constant field to obtain the corresponding first target index value; An encapsulation module is used to re-encapsulate the corresponding target bytecode instructions according to each target index value and the preset index value length to obtain an optimized bytecode file; the encapsulation module is specifically used to: set the length of the target index value to a preset byte length according to the instruction type of the target bytecode instruction; use the target index value of the preset byte length to replace the target operand information in the target bytecode instruction; wherein, using the target index value of the preset byte length to replace the target operand information in the target bytecode instruction includes: using the first target index value of the set first preset byte length to replace the instruction operand in the corresponding constant load instruction.
5. A bytecode execution method, characterized in that: include: Loading the optimized bytecode file obtained by the bytecode file optimization method according to any one of claims 1 to 3 to obtain target bytecode instructions; Determining a target operand storage area according to a target index value in a target bytecode instruction, and acquiring target operand information in the determined target operand storage area according to the target index value; The target bytecode instruction is executed according to the target operand information.
6. The bytecode execution method according to claim 5, wherein: The step of determining a target operand storage area according to a target index value in a target bytecode instruction, and acquiring target operand information in the determined target operand storage area according to the target index value, comprises: If the value of the preset bit of the target index value is a first preset value, the target operand storage area is a constant field, and the target bytecode instruction is a constant load instruction; The corresponding target operand information is obtained in the constant field according to the target index value; wherein the target operand information is an instruction operand.
7. The bytecode execution method according to claim 5, wherein: The step of determining a target operand storage area according to a target index value corresponding to a target bytecode instruction, and acquiring target operand information in the target operand storage area according to the target index value, includes: If the value of the preset bit of the target index value is a second preset value, the target operand storage area is an operand stack, and the target bytecode instruction is a stack operation instruction; According to the target index value, corresponding target operand information is obtained in the operand stack; wherein the target operand information is an operand address.
8. A smart card, characterized in that: include: The smart card is used to implement the bytecode execution method according to any one of claims 5 to 7.
Citation Information
Patent Citations
Byte code verification method and device and storage medium
CN112988157A
Byte code compression method for Java card and intelligent card system
CN115618910A