Bytecode file optimization method and device, bytecode file execution method and device and intelligent card
Optimizing bytecode files by repackaging instructions with index values addresses the space and efficiency issues, achieving up to 80% reduction in constant data and 30% overall file size, enhancing execution performance in embedded devices.
Patent Information
- Application Number
- CN202411265331.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-10
- Publication Date
- 2024-12-31
- Estimated Expiration
- 2044-09-10
AI Technical Summary
Existing bytecode files for smart card applications occupy a large amount of storage space in embedded devices with limited resources and are executed inefficiently.
A method and device for optimizing bytecode files by traversing instructions, determining operand storage areas, and repackaging bytecode instructions using index values to reduce storage space, along with a smart card implementation for execution.
The optimized bytecode files significantly reduce storage space by up to 80% for constant data and 30% for overall files, improving execution efficiency and speed.
Smart Images

Figure CN119225731A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of compilation technology, and in particular to a bytecode file optimization method, execution method, device and smart card. Background Art
[0002] The bytecode instructions in the bytecode file are a binary instruction format for a stack-based virtual machine. It is a low-level assembly-like language with a compact binary format that can run at near-native performance and provide a compilation target for languages such as C / C++.
[0003] However, in embedded devices with limited storage space resources, existing bytecode files occupy a large space and are executed inefficiently. Summary of the invention
[0004] In view of this, the embodiments of the present application provide a bytecode file optimization method, execution method, device and smart card, which can effectively solve the problem that the executable bytecode files corresponding to the existing smart card applications occupy a large space.
[0005] In a first aspect, an embodiment of the present application provides a method for optimizing a bytecode file, comprising:
[0006] Get the bytecode file to be optimized;
[0007] 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 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;
[0008] Determine the index of each target operand information in the corresponding operand storage area to obtain each corresponding target index value;
[0009] 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.
[0010] In some embodiments, repackaging the corresponding target bytecode instructions according to each of the target index values and the preset index value length includes:
[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] The target operand information in the target bytecode instruction is replaced using the target index value of the preset byte length.
[0013] In some embodiments, sequentially traversing the bytecode instructions in the bytecode file, acquiring the target bytecode instruction based on the instruction opcode of the target bytecode instruction, acquiring the target operand information carried by 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 includes:
[0014] Traversing each bytecode instruction in the bytecode file in sequence, determining the instruction type of the bytecode instruction currently traversed according to the instruction operation code in the bytecode instruction currently traversed, if it is determined according to the instruction type that the bytecode instruction currently traversed is a constant load instruction, then determining according to the instruction operation code of the constant load instruction that a preset operand storage area is a constant field, and determining that the target operand information is the instruction operand of the constant load instruction;
[0015] The step of determining the index of each target operand information in the corresponding operand storage area to obtain the corresponding target index value includes:
[0016] Obtaining an instruction operand in each of the constant load instructions, and determining an index of each of the instruction operands in the constant field, to obtain a corresponding first target index value;
[0017] The step of replacing the target operand information in the target bytecode instruction with the target index value of the preset byte length comprises:
[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, sequentially traversing the bytecode instructions in the bytecode file, acquiring the target bytecode instruction based on the instruction opcode of the target bytecode instruction, acquiring 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 includes:
[0020] Traversing each bytecode instruction in the bytecode file in sequence, determining the instruction type of the bytecode instruction currently traversed according to the instruction operation code in the bytecode instruction currently traversed, if it is determined according to the instruction type that the bytecode instruction currently traversed is a stack operation instruction, then determining according to the instruction operation code of the stack operation instruction, determining a preset operand storage area as an operand stack, and determining the target operand information as an operand address;
[0021] Determining the index of each target operand information in the corresponding operand storage area to obtain the corresponding target index value includes:
[0022] Obtaining the operand address in each of the stack operation instructions, and determining the index of each of the operand addresses in the operand stack, to obtain a corresponding second target index value;
[0023] The step of replacing the target operand information in the target bytecode instruction with the target index value of the preset byte length comprises:
[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] A bytecode file acquisition module is used to acquire 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 instruction based on the instruction operation code of the target bytecode instruction, acquire the target operand information carried in each of the target bytecode instructions according to the instruction type of the target bytecode instruction, and determine the operand storage area corresponding to the instruction type;
[0030] A target index value determination module, used to determine the index of each target operand information in the corresponding operand storage area, and obtain the corresponding target index value;
[0031] The encapsulation module is used to re-encapsulate the corresponding target bytecode instructions according to each of the target index values 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, including:
[0033] Loading an optimized bytecode file obtained by a bytecode file optimization method provided in the first aspect of the present application to obtain target bytecode instructions;
[0034] 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;
[0035] The target bytecode instruction is executed according to the target operand information.
[0036] In some embodiments, 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:
[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 load 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, 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:
[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 a smart card, including: the smart card is used to implement a bytecode execution method provided 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 instructions are obtained based on the instruction opcode of the target bytecode instructions, 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 values; according to each target index value and the preset index value length, the corresponding target bytecode instruction is repackaged to obtain an optimized bytecode file. The present application determines the corresponding target index value according to the target operand information, and repackages the bytecode file according to the target index value. In this way, the problem that the executable bytecode file corresponding to the existing smart card application occupies a large space can be effectively solved. BRIEF DESCRIPTION OF THE DRAWINGS
[0045] In order to more clearly illustrate the technical solutions of the embodiments of the present application, the drawings required for use in the embodiments will be briefly introduced below. It should be understood that the following drawings only show certain embodiments of the present application and therefore should not be regarded as limiting the scope. For ordinary technicians in this field, other related drawings can be obtained based on these drawings without paying creative work.
[0046] Figure 1 A flow chart showing a method for optimizing a bytecode file according to an embodiment of the present application is shown;
[0047] Figure 2 Another flow chart showing the bytecode file optimization method of the embodiment of the present application;
[0048] Figure 3 A flow chart showing a bytecode execution method according to an embodiment of the present application;
[0049] Figure 4 A schematic diagram showing the structure of a bytecode file optimization device according to an embodiment of the present application;
[0050] Figure 5 A structural schematic diagram of a bytecode execution device according to an embodiment of the present application is shown.
[0051] Description of main component symbols:
[0052] 410 - bytecode file acquisition module; 420 - target information acquisition module; 430 - target index value determination module; 440 - encapsulation module; 510 - loading module; 520 - operand 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 conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, rather than all of the embodiments.
[0054] The components of the embodiments of the present application generally described and shown in the drawings herein may be arranged and designed in various 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 application claimed for protection, but merely represents the 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 making creative work belong to the scope of protection of the present application.
[0055] Hereinafter, the terms "including", "having" and their cognates that can be used in various embodiments of the present application are intended only to indicate specific features, numbers, steps, operations, elements, components or a combination of the foregoing items, and should not be understood as first excluding the existence of one or more other features, numbers, steps, operations, elements, components or a combination of the foregoing items or increasing the possibility of one or more features, numbers, steps, operations, elements, components or a combination of the foregoing items. In addition, the terms "first", "second", "third" and the like are only used to distinguish descriptions and cannot be understood as indicating or implying relative importance.
[0056] Unless otherwise defined, all terms (including technical terms and scientific terms) used herein have the same meanings as those generally understood by those skilled in the art to which the various embodiments of the present application belong. The terms (such as those defined in generally used dictionaries) will be interpreted as having the same meanings as the contextual meanings in the relevant technical field and will not be interpreted as having idealized meanings or overly formal meanings unless clearly defined in the various embodiments of the present application.
[0057] In conjunction with the accompanying drawings, some embodiments of the present application are described in detail below. In the absence of conflict, the following embodiments and features in the embodiments can be combined with each other.
[0058] In order to optimize the storage space occupied by smart card applications (corresponding bytecode files) in embedded devices with limited resources, 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] Bytecode files consist of a series of byte sequences, which contain 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, a wasm file starts with the string `wasm` (`77 61 72 6d` in hexadecimal), which is a version identifier 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. This consists 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 declaration, 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: used to define all data types within the module, including function types, global variable types, module internal types, etc.
[0067] (3-3) Function Section: used to define the functions in the module and their parameters and return value types.
[0068] (3-4) Table Section: used to describe the information of indirect method call dependencies.
[0069] (3-5) Memory Section: used to describe the memory configuration information, including the initial size, maximum size, and alignment attributes of the memory.
[0070] (3-6)Global Section: used to define the type and initial value of global variables.
[0071] (3-7) Export Section, which 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, which contains the actual wasm instruction set and the functional code for executing the program.
[0073] (3-10) Data Section: used to store the initialization values of application variables and to initialize memory space.
[0074] The instruction types of the bytecode instructions of the bytecode file include: control instructions, parameter instructions, variable instructions, memory instructions, numerical instructions, etc. Constant instructions are a type of numerical instructions.
[0075] Load Constant Instruction: In the bytecode file, the load instruction is used to load the value in the memory into the operand stack, and then these values can be used in subsequent instructions for calculation or operation. The advantage of this is that it reduces the memory access at runtime, improves execution efficiency, and makes 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 the values on the operation stack. Stack operation instructions include but are not limited to: Push, Pop, Copy, etc. Stack operation instructions are usually used to perform operations such as calculation, storage and management of data.
[0078] The optimization method of the bytecode file is described below in conjunction with 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, obtaining a bytecode file to be optimized.
[0081] S120, sequentially traverse the bytecode instructions in the bytecode file, obtain the target bytecode instruction based on the instruction opcode of the target bytecode instruction, obtain the target operand information carried in each target bytecode instruction according to the instruction type of the target bytecode instruction, and determine the 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 multiple instruction types, such as constant loading instructions, stack operation instructions, etc. When traversing the bytecode file, it is necessary to obtain the target bytecode instructions corresponding to various instruction types respectively. The operand storage area is a pre-set storage area.
[0083] S130, determining the index of each target operand information in the corresponding operand storage area, and obtaining each corresponding target index value. The target operand information includes instruction operand, operand address, etc.
[0084] S140, repackaging the corresponding target bytecode instructions according to each of the target index values 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 of the target index values and the preset index value length includes:
[0086] S141, setting the length of the target index value to a preset byte length according to the instruction type of the target bytecode instruction.
[0087] Typically, a bytecode instruction consists of a 1-byte instruction opcode and several bytes of instruction operands. The instruction opcode indicates a specific instruction operation, such as loading a constant, storing a value in memory, calling a function, etc. The instruction operands (Operands) follow the instruction opcode and indicate the actual parameters of the instruction. Their length is determined by the instruction opcode. In order to optimize the bytecode file, this application optimizes the byte length of the instruction operands in the target bytecode instruction.
[0088] S142, using the target index value of the preset byte length to replace the target operand information in the target bytecode instruction.
[0089] For example, the bytecode before encapsulation is 00 02 03 04 02 03, where 02 03 is the first constant (two bytes), and the preset constant field is 07 08 09 02 03, where 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 subsequently referenced, 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 has been optimized to a great extent, reducing the storage space.
[0090] In one embodiment, the target bytecode instruction includes a constant load instruction; the instruction operand storage area of the constant load instruction includes a constant field.
[0091] Further, the traversing the bytecode instructions in the bytecode file in sequence, acquiring the target bytecode instruction based on the instruction opcode of the target bytecode instruction, acquiring the target operand information carried by 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, includes:
[0092] Each bytecode instruction in the bytecode file is traversed in turn, and the instruction type of the bytecode instruction currently traversed is determined according to the instruction operation code in the bytecode instruction currently traversed. If it is determined according to the instruction type that the bytecode instruction currently traversed is a constant load instruction, then according to the instruction operation code of the constant load instruction, a preset operand storage area is determined to be a constant field, and the target operand information is determined to be the instruction operand of the constant load instruction.
[0093] Among them, the bytecode instructions, that is, the functional codes for executing the program, are located in the CodeSection.
[0094] Exemplarily, the last bytecode is usually a terminator, which is used to mark 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 the type of constant, for example, an integer, a floating point number, etc. Different types of instruction opcodes are different.
[0096] Further, the determining the index of each target operand information in the corresponding operand storage area to obtain the corresponding target index value includes:
[0097] The instruction operands in each of the constant load instructions are obtained, and the index of each of the instruction operands in the constant field is determined to obtain a corresponding first target index value.
[0098] Further, the using the target index value of the preset byte length to replace the target operand information in the target bytecode instruction includes:
[0099] 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.
[0100] For example, the constant loading instruction is: 00 01, where 00 represents the instruction opcode (also called instruction header) of the constant loading instruction, 01 represents the first constant, and the data stored in the preset constant field is: 03 02 01. Then, there is the first constant "01" 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. The index value (target index value) corresponding to the instruction operand in the present application is represented by 1 byte or 2 bytes, while the instruction operand in the bytecode instruction in the prior art is represented by 2 bytes; the present application uses the highest bit to indicate that 1 byte or 2 bytes are valid, and the second highest bit indicates the source of the instruction operand.
[0102] In one embodiment, the target bytecode instruction includes a stack operation instruction; the target operand information includes an operand address; and the operand storage area includes an operand stack.
[0103] Further, the traversing the bytecode instructions in the bytecode file in sequence, acquiring the target bytecode instruction based on the instruction operation code of the target bytecode instruction, acquiring 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, includes:
[0104] Traverse each bytecode instruction in the bytecode file in turn, and determine the instruction type of the bytecode instruction currently traversed according to the instruction operation code in the bytecode instruction currently traversed; if it is determined according to the instruction type that the bytecode instruction currently traversed is a stack operation instruction, then determine the preset operand storage area as the operand stack according to the instruction operation code of the stack operation instruction, and determine the target operand information as the operand address.
[0105] Further, the determining the index of each target operand information in the corresponding operand storage area to obtain the corresponding target index value includes:
[0106] The operand address in each of the stack operation instructions is obtained, and the index of each of the operand addresses in the operand stack is determined 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 includes:
[0108] The target index value of the set second preset byte length is used 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 the operand address in the bytecode instruction in the prior art 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 instruction can be determined according to the instruction operation code, 05 is the instruction operand of the stack operation instruction, and its address is 09, the operand stack is: 05 0607 08, and the index value of 05 in the operand stack is 00. The operand address in the stack operation instruction is replaced with the index value of the instruction operand in the operand stack, so the stack operation instruction: 09 is replaced with 00. Therefore, each time the instruction operand 05 is used, it can be directly called in the operand stack through the index value 00, and there is no need 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 one embodiment, the method further comprises:
[0112] 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;
[0113] If the target operand information is not in the operand storage area, the target operand information is added to the operand storage area. Wherein, the instruction type of the target bytecode instruction includes constant loading instructions and stack operation instructions, the corresponding target operand information includes instruction operands and operand addresses, and the corresponding operand storage area includes constant fields and operand stacks. Specifically, for constant loading instructions, it is determined whether the instruction operands of each constant loading instruction are in the constant field; if the instruction operands of the constant loading instructions are not in the constant field, the instruction operands of the constant loading instructions are added to the constant field. For stack operation instructions, it is determined whether the operand address of the stack operation instruction is in the operand stack; 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 to the operand stack.
[0114] Suppose, the constant loading instruction is: 00 04, 00 represents the instruction opcode of the constant loading instruction, 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. The index value of the first constant "04" in the preset constant field is 03. Therefore, each time the first constant "04" is referenced, it is directly referenced through the index "03", without importing the first constant each time, thereby improving the execution speed.
[0115] The following is an introduction to the optimization method of the bytecode file of this application with a specific example. In this example, the wasm bytecode file is used as an example. Figure 2 As shown, the following steps are included:
[0116] S101, obtaining a bytecode file corresponding to a smart card application, and traversing bytecode instructions in the bytecode file.
[0117] S102, determine the instruction type of the currently traversed bytecode instruction. If it is a constant load instruction, execute step S103, if it is a stack operation instruction, execute step S104.
[0118] S103, obtaining a first constant carried by the constant load instruction, and determining whether the first constant exists in a preset constant field; if so, obtaining an index value of the first constant in the preset constant field, and recording it as a constant index; if not, adding the first constant to the preset constant field, and recording the index value of the first constant in the constant field as a constant index. The constant index is used to replace the first constant in the constant load instruction.
[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 the operand stack index; and using the operand stack index to replace the operand address corresponding to the stack operation instruction.
[0120] 105, determine whether the currently traversed bytecode instruction is the last bytecode instruction in the bytecode file, if so, end the traversal; otherwise, return to step S102.
[0121] Figure 3 A flow chart of a bytecode execution method according to an embodiment of the present application is shown. Exemplarily, the bytecode execution method includes the following steps:
[0122] S210, loading an optimized bytecode file obtained by a bytecode file optimization method according to the present application, and obtaining target bytecode instructions;
[0123] S220, determine the target operand storage area according to the target index value in the target bytecode instruction, and obtain the target operand information in the determined target operand storage area according to the target index value.
[0124] Furthermore, the target operand storage area includes an operand stack and a constant field.
[0125] According to the highest bit of the target index value, it is distinguished whether the index value is valid for 1 byte or 2 bytes. According to the second highest bit of the index value, it is distinguished whether the index value represents the index of the operand stack or the index of the constant field. 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 numbers in each position represent different information. For example, if the highest bit is 0, it means that one byte is valid, and if the highest bit is 1, it means that two bytes are valid.
[0126] S230: Execute the target bytecode instruction according to the target operand information.
[0127] In one embodiment, 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:
[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] 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.
[0130] In one embodiment, 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:
[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] The corresponding target operand information is obtained in the operand stack according to the target index value; wherein the target operand information is the operand address of the instruction operand.
[0133] For example, a byte has 8 bits, the leftmost is the highest bit, the second bit from the left is the second highest bit, and the rightmost is the lowest bit. The numbers at each position represent different information. The preset bit of the target index value is the second highest bit. If the value on the second highest bit is 0 (the first preset value), the target operand storage area is determined to be a constant field. If the value on the second highest bit is 1 (the second preset value), the target operand storage area is determined to be an operand stack.
[0134] In short, the type corresponding to the target index value is determined according to the preset bytes 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 loading instruction is updated.
[0135] Furthermore, the wasm runtime is used in the runtime of this application. Once the source code is compiled into bytecode, the wasm runtime will be required to execute them. The wasm runtime provides all the features and advantages usually associated with wasm, such as sandbox security, speed, and cross-platform portability. The WebAssembly Micro Runtime (WAMR) is a lightweight, independent WebAssembly (wasm) runtime with small size, high performance, and high configurability. It is suitable for applications ranging from embedded, IoT, edge to trusted execution environment (TEE), smart contracts, cloud native, etc., so the wasm runtime is used in the runtime of this application.
[0136] The method bytecode for parsing wasm files in this application traverses the bytecode instructions. If it is a constant loading instruction, the constant (instruction operand) carried by the constant loading instruction is extracted, and the newly added const segment is searched for the same constant value; if found, the index of this constant in the const segment is recorded; if not found, this constant is added to the end of the const segment, and the index of this constant in the const segment is recorded. All instructions in the bytecode file that reference this constant are modified to reference through the index corresponding to the constant. This optimization operation can reduce code redundancy caused by repeated constants.
[0137] The method bytecode for parsing wasm files in this application traverses the bytecode instructions. If it is a stack operation instruction, the operand address is replaced with the index of the operand address in the operand stack. The highest bit of the index is used to distinguish whether the index is valid for 1 byte or 2 bytes, and the second highest bit of the index is used to distinguish whether the index represents the index of the operand stack or the index of a constant. This optimization operation can reduce the space occupied by instruction operands.
[0138] The present application replaces the instruction operands in the constant loading instruction and the operand addresses in the stack operation instruction with their index values in the constant field and the index values in the operand stack; the index of the instruction operand in the present application is represented by 1 byte or 2 bytes instead of the 2 bytes in the original instruction, the highest bit is used to represent that 1 byte or 2 bytes are valid, and the second highest bit represents the source of the instruction operand. In this way, the space occupied by the instruction operand can be reduced. After the improvement, the storage space occupied by the constant data is reduced by 80% compared with that before the improvement. After the improvement, the space occupied by the bytecode instruction is reduced by 30% compared with that before the improvement.
[0139] This application is based on the Intel wamr interpreter (fast) mode applied to the management platform of smart card applications to achieve application file loading and operation. Through the test and verification of industry applications, the application operation efficiency of this application is slightly higher than that of other virtual machine smart card application management platforms.
[0140] Figure 4 A schematic diagram of the structure of the bytecode file optimization device of the embodiment of the present 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 acquisition module 420 is used to sequentially traverse the bytecode instructions in the bytecode file, acquire the target bytecode instruction based on the instruction operation code of the target bytecode instruction, acquire the target operand information carried in each of the target bytecode instructions according to the instruction type of the target bytecode instruction, and determine the operand storage area corresponding to the instruction type;
[0143] A target index value determination module 430 is used to determine the index of each target operand information in the corresponding operand storage area to obtain the corresponding target index value;
[0144] The encapsulation module 440 is used to re-encapsulate the corresponding target bytecode instructions according to each of the target index values and the preset index value length to obtain an optimized bytecode file.
[0145] It can be understood that the device of this embodiment corresponds to the optimization method of the bytecode file of the above embodiment, and the options in the above embodiment are also applicable to this embodiment, so they will not be described repeatedly here.
[0146] Figure 5 A schematic diagram of the structure of a bytecode execution device according to an embodiment of the present application is shown. Exemplarily, the bytecode execution device includes: a loading module 510 , an operand determination module 520 and an execution module 530 .
[0147] A loading module 510, used to load an optimized bytecode file obtained by a bytecode file optimization method according to the present application, and obtain target bytecode instructions;
[0148] An operand determination module 520 is used 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 used to execute the target bytecode instruction according to the target operand information.
[0150] It can be understood that the device of this embodiment corresponds to the bytecode execution method of the above embodiment, and the options in the above embodiment are also applicable to this embodiment, so they will not be described repeatedly here.
[0151] The present application also provides a smart card. Exemplarily, the smart card includes a processor and a memory, wherein the memory stores a computer program, and the processor runs the computer program to enable the smart card to execute the above-mentioned bytecode execution method or the functions of each module in the above-mentioned bytecode execution device.
[0152] The present application also provides a terminal device. Exemplarily, the terminal device includes a processor and a memory, wherein the memory stores a computer program, and the processor runs the computer program to enable the terminal device to execute the functions of each module in the above-mentioned bytecode file optimization method or the above-mentioned bytecode file optimization device.
[0153] Among them, the processor can be an integrated circuit chip with signal processing capabilities. The processor can be a general-purpose processor, including a central processing unit (CPU), a graphics processing unit (GPU) and a network processor (NP), a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field programmable gate array (FPGA) or at least one of other programmable logic devices, discrete gates or transistor logic devices, and discrete hardware components. The general-purpose processor can be a microprocessor or the processor can also be any conventional processor, etc., which can implement or execute the disclosed methods, steps and logic block diagrams in the embodiments of the present application.
[0154] The memory may 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), etc. The memory is used to store a computer program, and the processor may execute the computer program accordingly after receiving an execution instruction.
[0155] The present application also provides a computer-readable storage medium for storing the computer program used in the above-mentioned terminal device. For example, the computer-readable storage medium may include, but is not limited to, various media that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.
[0156] In several embodiments provided in the present application, it should be understood that the disclosed devices and methods can also be implemented in other ways. The device embodiments described above are merely schematic. For example, the flowcharts and structure diagrams in the accompanying drawings show the possible architecture, functions and operations of the devices, methods and computer program products according to multiple embodiments of the present application. In this regard, each box in the flowchart or block diagram can represent a module, a program segment or a part of a code, and the module, a program segment or a part of a code contains one or more executable instructions for implementing the specified logical function. It should also be noted that in an alternative implementation, the functions marked in the box can also occur in a different order from the order marked in the accompanying drawings. For example, two consecutive boxes can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the structure diagram and / or the flow diagram, and the combination of boxes in the structure diagram and / or the flow diagram, can be implemented with a dedicated hardware-based system that performs a specified function or action, or can be implemented with a combination of dedicated hardware and computer instructions.
[0157] In addition, the functional modules or units in the various embodiments of the present application may be integrated together to form an independent part, or each module may exist separately, or two or more modules may 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 solution of the present application, or the part that contributes to the prior art, or the part of the technical solution, can be embodied in the form of a software product, which is stored in a storage medium and includes several instructions for a computer device (which can be a smart phone, a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the methods described in the various embodiments of the present application.
[0159] The above description is only a specific implementation manner of the present application, but the protection scope of the present application is not limited thereto. Any technician familiar with the technical field can easily think of changes or substitutions within the technical scope disclosed in the present application, which should be included in 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 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; Determine the index of each target operand information in the corresponding operand storage area to obtain each corresponding 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.
2. The bytecode file optimization method according to claim 1, characterized in that: The repackaging of the corresponding target bytecode instructions according to each of the target index values and the preset index value length comprises: Setting the length of the target index value to a preset byte length according to the instruction type of the target bytecode instruction; The target operand information in the target bytecode instruction is replaced using the target index value of the preset byte length.
3. The bytecode file optimization method according to claim 2, characterized in that: The step of sequentially traversing the bytecode instructions in the bytecode file, acquiring the target bytecode instruction based on the instruction operation code of the target bytecode instruction, acquiring the target operand information carried by 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 includes: Traversing each bytecode instruction in the bytecode file in sequence, determining the instruction type of the bytecode instruction currently traversed according to the instruction operation code in the bytecode instruction currently traversed, if it is determined according to the instruction type that the bytecode instruction currently traversed is a constant load instruction, then determining according to the instruction operation code of the constant load instruction that a preset operand storage area is a constant field, and determining that the target operand information is the instruction operand of the constant load instruction; The step of determining the index of each target operand information in the corresponding operand storage area to obtain the corresponding target index value includes: Obtaining an instruction operand in each of the constant load instructions, and determining an index of each of the instruction operands in the constant field, to obtain a corresponding first target index value; The step of replacing the target operand information in the target bytecode instruction with the target index value of the preset byte length comprises: 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.
4. The bytecode file optimization method according to claim 2, characterized in that: The step of sequentially traversing the bytecode instructions in the bytecode file, acquiring the target bytecode instruction based on the instruction operation code of the target bytecode instruction, acquiring 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 includes: Traversing each bytecode instruction in the bytecode file in sequence, determining the instruction type of the bytecode instruction currently traversed according to the instruction operation code in the bytecode instruction currently traversed, if it is determined according to the instruction type that the bytecode instruction currently traversed is a stack operation instruction, then determining according to the instruction operation code of the stack operation instruction, determining a preset operand storage area as 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 the operand address in each of the stack operation instructions, and determining the index of each of the operand addresses in the operand stack, to obtain a corresponding second target index value; The step of replacing the target operand information in the target bytecode instruction with the target index value of the preset byte length comprises: The target index value of the set second preset byte length is used to replace the operand address in the corresponding stack operation instruction.
5. The bytecode file optimization method according to any one of claims 1 to 4, 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.
6. A bytecode file optimization device, characterized in that: include: A bytecode file acquisition module is used to acquire 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 instruction based on the instruction operation code of the target bytecode instruction, acquire the target operand information carried in each of the target bytecode instructions according to the instruction type of the target bytecode instruction, and determine the operand storage area corresponding to the instruction type; A target index value determination module, used to determine the index of each target operand information in the corresponding operand storage area, and obtain the corresponding target index value; The encapsulation module is used to re-encapsulate the corresponding target bytecode instructions according to each of the target index values and the preset index value length to obtain an optimized bytecode file.
7. 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 5 to obtain each target bytecode instruction; 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; The target bytecode instruction is executed according to the target operand information.
8. The bytecode execution method according to claim 7, characterized in that: 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.
9. The bytecode execution method according to claim 7, characterized in that: The step of determining a target operand storage area according to a target index value corresponding to the 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; 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.
10. A smart card, characterized in that: include: The smart card is used to implement the bytecode execution method according to any one of claims 7 to 9.
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
Instruction processing method, terminal equipment and computer readable storage medium
CN115878183A
Compilation method and device of Java smart contract, storage medium and electronic equipment
CN117762423A
Cited By
Bytecode editing method and device, electronic equipment and program product
CN119396412A
Bytecode Editing Method, Apparatus, Electronic Device, and Program Product
CN119396412B
Constant data processing method based on inline constant table, electronic equipment and medium
CN120523472A
Execution custom logic in an open platform communcation unifed architecture, OPC UA, server
EP4822534A1