Programming language conversion method, device, electronic device and storage medium

By using the temporary variable polling mechanism during Java code conversion, temporary variables are assigned to variables and data in each expression, and outputting them at the end of the expression, the problem of excessive code overhead in Java bytecode conversion is solved, and the stability and efficiency of system performance is improved.

CN116204196BActive Publication Date: 2025-08-19WUHAN TIANYU INFORMATION IND
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202211740921.2
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-12-30
Publication Date
2025-08-19
Estimated Expiration
2042-12-30

AI Technical Summary

Technical Problem

When converting the bytecode of the Java language to other programming languages, the existing technology has the problem of excessive code overhead, resulting in system performance degradation or even crashes.

Method used

The temporary variable polling mechanism is used to identify the expressions of Java code and assign temporary variables to them, and store them in the temporary variable storage space. Only the content of the temporary variable is output at the end of the expression, eliminating the statement output of the intermediate process.

Benefits of technology

It effectively reduces code overhead, avoids system performance degradation, and improves conversion efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN116204196B_ABST
    Figure CN116204196B_ABST
Patent Text Reader

Abstract

The present invention discloses a programming language conversion method, device, electronic device, and storage medium, relating to the computer field. The method comprises identifying Java code that currently requires format conversion based on a preset temporary variable polling mechanism; format conversion of the Java code, and sequentially assigning temporary variables to variables and data used in each expression in the Java code; and outputting the contents of the temporary variables when the expression in the Java code identified during the format conversion process ends. The present invention can effectively reduce code overhead and prevent system performance degradation.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of computers, and in particular to a programming language conversion method, device, electronic device and storage medium. Background Art

[0002] Java Card technology is currently the Java technology used in smart cards and is a widely used programming platform for smart cards. As a programming platform based on a virtual machine, Java (an object-oriented programming language) source code must first be compiled into byte code, which is then run on the virtual machine that interprets the byte code.

[0003] The compilation language used by Java Card applets is Java. The bytecode (class file) generated by this language is suitable for use on the Java virtual machine. However, when the application needs to be ported to other types of virtual machines, the application needs to be recompiled before it can be used.

[0004] During the recompilation process, Java requires a programming language conversion process. A typical approach is to convert the compiled Java bytecode (class files) into C for use. However, the large amount of information generated during the Java virtual machine stack pop and push parsing during this conversion process results in excessive overhead for the converted code, which can easily lead to system performance degradation or even system crashes. Summary of the Invention

[0005] In view of the defects existing in the prior art, the purpose of the present invention is to provide a programming language conversion method, device, electronic device and storage medium, which can effectively reduce code overhead and avoid system performance degradation.

[0006] To achieve the above objectives, the present invention provides a programming language conversion method, which specifically includes the following steps:

[0007] Based on the set temporary variable polling mechanism, identify the Java code that needs to be formatted;

[0008] Performing format conversion on the Java code, and sequentially assigning variables and data used in each expression in the Java code to temporary variables;

[0009] When the expression of the Java code is identified to be finished during the format conversion process, the content of the temporary variable is output.

[0010] On the basis of the above technical solutions,

[0011] After identifying the bytecode of the Java code that needs to be converted, create a temporary variable storage space;

[0012] After assigning temporary variables to the variables and data used in each expression of the Java code in sequence, the temporary variables are stored in the created temporary variable storage space;

[0013] After the contents of the temporary variable are output, the temporary variable stored in the temporary variable storage space is cleared.

[0014] On the basis of the above technical solutions,

[0015] The stack depth used by the Java code includes the stack occupied by code parameters, the stack occupied by code local variables and the operation stack;

[0016] The temporary variables correspond to the operation stack one by one.

[0017] On the basis of the above technical solutions,

[0018] The size of the operation stack is declared in the first byte of the bytecode of the Java code;

[0019] The size of the operation stack is equal to the total number of temporary variables used in the Java code format conversion process.

[0020] On the basis of the above technical solution, the temporary variable polling mechanism is specifically as follows:

[0021] Before format conversion, the number of temporary variables of each bytecode of the Java code before and after the conversion is pre-processed, and the number of temporary variables after the conversion is calculated based on the number of temporary variables used before the conversion and the number of stack pushes and pops during the conversion process;

[0022] Before format conversion, the types of temporary variables of each bytecode of the Java code before and after conversion are pre-processed according to the type of the bytecode of the Java code and the output result;

[0023] During the format conversion process, starting from the first bytecode of the Java code, each time a data is pushed into the stack, the data is assigned to a newly added temporary variable. Each time a data is popped from the stack, the last temporary variable is taken out and set to invalid.

[0024] Based on the above technical solution, the types of the temporary variables include object type and int type.

[0025] The present invention provides a programming language conversion device, comprising:

[0026] An execution module is used to identify the Java code that needs to be format converted based on a set temporary variable polling mechanism;

[0027] a conversion module, configured to convert the format of the Java code and assign temporary variables to the variables and data used in each expression of the Java code in sequence;

[0028] The output module is used to output the content of the temporary variable when the expression of the Java code is identified to be finished during the format conversion process.

[0029] On the basis of the above technical solutions,

[0030] After identifying the bytecode of the Java code that needs to be converted, create a temporary variable storage space;

[0031] After assigning temporary variables to the variables and data used in each expression of the Java code in sequence, the temporary variables are stored in the created temporary variable storage space;

[0032] After the contents of the temporary variable are output, the temporary variable stored in the temporary variable storage space is cleared.

[0033] The present invention provides an electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the program, the steps of the programming language conversion method described above are implemented.

[0034] The present invention provides a non-transitory computer-readable storage medium having a computer program stored thereon. When the computer program is executed by a processor, the steps of the programming language conversion method described above are implemented.

[0035] Compared with the prior art, the advantages of the present invention are: a temporary variable polling method is adopted when performing format conversion, and the variables and data used in each expression are assigned temporary variables in sequence. When the end of the expression is recognized, the content of the last temporary variable is output, thereby eliminating the code overhead of outputting intermediate process statements and only outputting statements that have been successfully converted, thereby effectively reducing code overhead and avoiding system performance degradation. BRIEF DESCRIPTION OF THE DRAWINGS

[0036] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the following briefly introduces the drawings required for use in the description of the embodiments. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without any creative work.

[0037] Figure 1The figure is a flowchart of a programming language conversion method according to an embodiment of the present invention. DETAILED DESCRIPTION

[0038] In order to make the purpose, technical solutions and advantages of the embodiments of the present application clearer, the technical solutions in the embodiments of the present application will be clearly and completely described below in combination with the drawings in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments.

[0039] See also Figure 1 As shown, an embodiment of the present invention provides a programming language conversion method. During the code conversion process, a temporary variable polling mechanism is provided. Intermediate statements in the code conversion are represented by temporary variables, thereby reducing code overhead during code conversion and avoiding system performance degradation. The programming language conversion method specifically includes the following steps:

[0040] S1: Based on the set temporary variable polling mechanism, the Java code that currently needs to be formatted is identified; at the same time, the bytecode of the Java code that currently needs to be formatted is also identified, and the statement for starting the conversion is determined.

[0041] Furthermore, after identifying the bytecode of the Java code that needs to be formatted, temporary variable storage space is created. That is, the space required for temporary variable storage in the Java code is analyzed and created to facilitate subsequent temporary variable storage.

[0042] S2: Convert the Java code to a new format and assign temporary variables to the variables and data used in each expression in the Java code in order; that is, assign temporary variables to the variables and data used in each expression in the Java code in the order of conversion. It should be noted that when converting the Java code to a new format, the conversion is essentially performed from three aspects: functions, expressions, and variables. The present invention primarily focuses on the conversion process of expressions.

[0043] Furthermore, after the variables and data used in each expression of the Java code are assigned to temporary variables in sequence, the temporary variables are stored in the created temporary variable storage space.

[0044] In an embodiment of the present invention, the format of the Java code is converted to convert the programming language type, for example, from programming language A to programming language B. The intermediate processes generated by programming language B during the conversion process are all represented by temporary variables, and only the final output result is output. After one round of output, the temporary variables are cleared and the next round of polling begins.

[0045] For the conversion object of the expression, the read value (variable or constant) and the operation process can all be represented by temporary variables. The operation includes arithmetic objects and logical operations. During the conversion, if the expression content is recognized as a read value and an operation, it is automatically represented by a temporary variable.

[0046] S3: When the Java code expression is identified as finished during the format conversion process, the contents of the temporary variable are output. Furthermore, after the contents of the temporary variable are output, the temporary variable stored in the temporary variable storage space is cleared. Specifically, after the contents of the temporary variable are output and the final converted content is output, the temporary variable contents are cleared, and then the next round of polling conversion begins.

[0047] In this embodiment of the present invention, the stack depth used by Java code includes the stack occupied by code parameters, the stack occupied by code local variables, and the operand stack; temporary variables correspond one-to-one with the operand stack. The size of the operand stack is declared in the first byte of the Java code bytecode; the size of the operand stack is equal to the total number of temporary variables used during the Java code format conversion process.

[0048] In the present invention, the temporary variable polling mechanism is specifically as follows:

[0049] Before format conversion, the number of temporary variables (depth of the operation stack) of each bytecode of the Java code before and after the conversion is pre-processed. The number of temporary variables after the conversion is calculated based on the number of temporary variables used before the conversion (the number of temporary variables after the previous bytecode conversion) and the number of stack pushes and pops during the conversion process.

[0050] Before format conversion, the types of temporary variables of each bytecode of the Java code before and after conversion are pre-processed according to the type of the bytecode of the Java code and the output result. The types of temporary variables include object type (object type) and int type (integer);

[0051] During the format conversion process, starting from the first bytecode of the Java code, each time a data is pushed into the stack, the data is assigned to a newly added temporary variable. Each time a data is popped from the stack, the last temporary variable is taken out and set to invalid.

[0052] The programming language conversion method of the present invention is described in detail below with reference to an example.

[0053] Assume that Java code adds two byte type local variables and assigns the result to another local variable: c = a + b. The code is compiled to generate bytecode, and then the bytecode is converted into C language code. This expression is used as an example to illustrate the conversion process.

[0054] The Java bytecode generated by this expression includes:

[0055] 1. iload_0: Get the value of local variable a and push it into the stack;

[0056] 2. iload_1: Get the value of local variable b and push it into the stack;

[0057] 3. add: pop 2 data from the stack, add them, and push the result into the stack;

[0058] 4. istore_2: Pops a data from the stack and assigns the data to the local variable c.

[0059] In the Java virtual machine, the operation stack increases by 1 each time the stack is pushed, and decreases by 1 each time the stack is popped. According to the above bytecode, the maximum number of operation stacks used by this expression is 2, that is, 2 temporary variables will be used in the conversion process. In addition, according to the meaning of the bytecodes iload_1 and iload_2, it can be seen that these are local variables of 2 basic types (such as byte\short\int types), and the corresponding temporary variable types are also basic types. Integer (int type) is uniformly used for representation during conversion.

[0060] From the above preliminary analysis, we can see that two temporary integer variables will be used when converting this expression, for example, i1 and i2. The code result after directly converting the above bytecode is:

[0061] 1. i1 = a;

[0062] 2. i2 = b;

[0063] 3. i1=i1+i2;

[0064] 4. c=i1.

[0065] It can be seen that the amount of code in this code is obviously greater than that of the original statement, and the efficiency is low. Therefore, in order to solve the code redundancy problem caused by direct bytecode conversion, the present invention applies for each temporary variable a temporary cache, which stores the intermediate state value of the temporary variable and outputs the temporary variable only when the expression ends.

[0066] Since the Java virtual machine stack itself does not distinguish between object data and primitive data, the conversion tool generates two sets of corresponding temporary variables (for example, a1 to ax and i1 to ix), where a1 to ax represent object type data, i1 to ix represent primitive type data, and x represents the depth of the operation stack obtained during preprocessing. In addition, a temporary buffer of string type is allocated for each temporary variable, such as aa[x] and ii[x], where aa[x] corresponds one-to-one with a1 to ax, and ii[x] corresponds one-to-one with i1 to ix. Take the above example of c=a+b to illustrate the conversion process:

[0067] 1. i1 = a; processed as: ii[1] = "a";

[0068] 2. i2 = b; processed as: ii[2] = "b";

[0069] 3. i1=i1+i2;

[0070] Determine whether the buffer ii[1] corresponding to i1 and the buffer ii[2] corresponding to i2 are not empty, indicating that they are valid data. Use the buffer value, that is: ii[1] = ii[1] + ii[2] = "a + b";

[0071] If ii[2] is already used, set ii[2] to be empty, indicating that it is invalid.

[0072] 4. c = i1;

[0073] Determine whether the buffer ii[1] corresponding to i1 is not empty, indicating that it is valid data, and use the buffer value, that is: c=i1=ii[1]=a+b.

[0074] From the above, we can see that when assigning a value to a temporary variable, it is not directly converted to the C language application. Instead, the assigned content is stored in the temporary variable buffer in the form of a string. When the temporary variable is used later, it is first determined whether the buffer corresponding to the variable is valid. If it is valid, the buffer data of the corresponding subscript is used, otherwise the original ix or ax is used.

[0075] The first data in the expression is pushed onto the stack, and the temporary variable subscript increments from 1. The temporary variable subscript cycle ends when the expression ends, and the next expression starts over again at 1. Within the same expression scope, the subscripts x of primitive type temporary variables ix and object type temporary variables ax are shared and continuous, and cannot be repeated.

[0076] The expressions in the Java statement that will end the current temporary variable subscript loop are:

[0077] 1. Assignment statement a=b;

[0078] Where b can be a constant, variable, function or a complex value expression. After the assignment operation is completed, the virtual machine stack is restored to the state before the statement is executed, that is, the temporary variable subscript is restored to the state before the expression is executed;

[0079] 2. Conditional statement if(a==b);

[0080] Where a and b can be constants, variables, functions, or complex value expressions. After the assignment operation is completed, the virtual machine stack is restored to the state before the statement is executed, that is, the temporary variable subscript is restored to the state before execution. After the conditional statement is executed, when jumping to the next expression, the temporary variable subscript starts from 1;

[0081] 3. Function call Fun(a,b);

[0082] Among them, a and b can be constants, variables, functions or complex value expressions. After the assignment operation is executed, the virtual machine stack is restored to the state before the statement is executed, that is, the temporary variable subscript is restored to the state before execution.

[0083] Assignment statements and function call statements can also be nested in other expressions, so after execution, the temporary variable loop can only be restored to the state before the statement is executed.

[0084] The programming language conversion method of an embodiment of the present invention adopts a temporary variable polling method when performing format conversion, and assigns temporary variables to the variables and data used in each expression in sequence. When the end of the expression is recognized, the content of the last temporary variable is output, thereby eliminating the code overhead of outputting intermediate process statements and only outputting successfully converted statements, thereby effectively reducing code overhead and avoiding system performance degradation.

[0085] An embodiment of the present invention provides a programming language conversion device, which includes an execution module, a conversion module and an output module.

[0086] The execution module is used to identify the Java code that currently needs to be formatted based on the set temporary variable polling mechanism; the conversion module is used to format the Java code and assign temporary variables to the variables and data used in each expression of the Java code in sequence; the output module is used to output the content of the temporary variable when the expression of the Java code identified in the format conversion process ends.

[0087] In the present invention, after the bytecode of the Java code that currently needs to be format converted is identified, a temporary variable storage space is created; after the variables and data used in each expression of the Java code are assigned temporary variables in sequence, the temporary variables are stored in the created temporary variable storage space; after the content of the temporary variable is output, the temporary variable stored in the temporary variable storage space is cleared.

[0088] In a possible implementation, an embodiment of the present invention further provides an electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the steps of the above-mentioned programming language conversion method when executing the program.

[0089] In a possible implementation, an embodiment of the present invention further provides a non-transitory computer-readable storage medium having a computer program stored thereon, characterized in that when the computer program is executed by a processor, the steps of the above-mentioned programming language conversion method are implemented.

[0090] The storage medium can adopt any combination of one or more computer-readable media. The computer-readable medium can be a computer-readable signal medium or a computer-readable storage medium. The computer-readable storage medium can be, for example, but not limited to: an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or component, or any combination of the above. More specific examples (non-exhaustive list) of computer-readable storage media include: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In this document, a computer-readable storage medium can be any tangible medium containing or storing a program that can be used by or in conjunction with an instruction execution system, device or device.

[0091] A computer-readable signal medium may include a data signal propagated in baseband or as part of a carrier wave, which carries computer-readable program code. Such propagated data signals may take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination thereof. A computer-readable signal medium may also be any computer-readable medium other than a computer-readable storage medium, which may transmit, propagate, or transfer a program for use by or in conjunction with an instruction execution system, apparatus, or device. The program code contained on the computer-readable medium may be transmitted using any suitable medium, including but not limited to wireless, wire, optical cable, RF, etc., or any suitable combination thereof.

[0092] Computer program code for performing the operations of the present invention can be written in one or more programming languages, or a combination thereof, including object-oriented programming languages such as Java, Smalltalk, C++, and conventional procedural programming languages such as "C" or similar programming languages. The program code can be executed entirely on the user's computer, partially on the user's computer, as a stand-alone software package, partially on the user's computer and partially on a remote computer, or entirely on a remote computer or server. In cases involving a remote computer, the remote computer can be connected to the user's computer through any type of network, including a local area network (LAN) or a wide area network (WAN), or can be connected to an external computer (e.g., through the Internet using an Internet service provider).

[0093] The foregoing is merely a list of specific embodiments of the present application, intended to enable those skilled in the art to understand or implement the present application. Various modifications to these embodiments will be readily apparent to those skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of the present application. Therefore, the present application is not limited to the embodiments shown herein, but is intended to conform to the broadest scope consistent with the principles and novel features of the present application.

[0094] The present invention is described with reference to flowcharts and / or block diagrams of methods, devices (systems) and computer program products according to embodiments of the present invention. It should be understood that each process and / or block in the flowcharts and / or block diagrams, as well as combinations of processes and / or blocks in the flowcharts and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor or other programmable data processing device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowcharts and / or block diagrams. Figure 1 a process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.

Claims

1. A programming language conversion method, characterized in that: The specific steps include: Based on the set temporary variable polling mechanism, identify the Java code that needs to be formatted; Performing format conversion on the Java code, and sequentially assigning variables and data used in each expression in the Java code to temporary variables; When the expression of the Java code is identified to be finished during the format conversion process, the content of the temporary variable is output; The temporary variable polling mechanism is specifically as follows: Before format conversion, the number of temporary variables of each bytecode of the Java code before and after the conversion is pre-processed, and the number of temporary variables after the conversion is calculated based on the number of temporary variables used before the conversion and the number of stack pushes and pops during the conversion process; Before format conversion, the types of temporary variables of each bytecode of the Java code before and after conversion are pre-processed according to the type of the bytecode of the Java code and the output result; During the format conversion process, starting from the first bytecode of the Java code, each time a data is pushed into the stack, the data is assigned to a newly added temporary variable. Each time a data is popped from the stack, the last temporary variable is taken out and set to invalid.

2. The programming language conversion method according to claim 1, wherein: After identifying the bytecode of the Java code that needs to be converted, create a temporary variable storage space; After assigning temporary variables to the variables and data used in each expression of the Java code in sequence, the temporary variables are stored in the created temporary variable storage space; After the contents of the temporary variable are output, the temporary variable stored in the temporary variable storage space is cleared.

3. The programming language conversion method according to claim 1, wherein: The stack depth used by the Java code includes the stack occupied by code parameters, the stack occupied by code local variables and the operation stack; The temporary variables correspond to the operation stack one by one.

4. A programming language conversion method according to claim 3, characterized in that: The size of the operation stack is declared in the first byte of the bytecode of the Java code; The size of the operation stack is equal to the total number of temporary variables used in the Java code format conversion process.

5. A programming language conversion method according to claim 4, characterized in that: The types of the temporary variable include object type and int type.

6. A programming language conversion device, characterized in that: include: An execution module is used to identify the Java code that needs to be format converted based on a set temporary variable polling mechanism; a conversion module, configured to convert the format of the Java code and assign temporary variables to the variables and data used in each expression of the Java code in sequence; An output module, configured to output the contents of a temporary variable when the expression of the Java code is identified to be finished during the format conversion process; The temporary variable polling mechanism is specifically as follows: Before format conversion, the number of temporary variables of each bytecode of the Java code before and after the conversion is pre-processed, and the number of temporary variables after the conversion is calculated based on the number of temporary variables used before the conversion and the number of stack pushes and pops during the conversion process; Before format conversion, the types of temporary variables of each bytecode of the Java code before and after conversion are pre-processed according to the type of the bytecode of the Java code and the output result; During the format conversion process, starting from the first bytecode of the Java code, each time a data is pushed into the stack, the data is assigned to a newly added temporary variable. Each time a data is popped from the stack, the last temporary variable is taken out and set to invalid.

7. A programming language conversion device according to claim 6, characterized in that: After identifying the bytecode of the Java code that needs to be converted, create a temporary variable storage space; After assigning temporary variables to the variables and data used in each expression of the Java code in sequence, the temporary variables are stored in the created temporary variable storage space; After the contents of the temporary variable are output, the temporary variable stored in the temporary variable storage space is cleared.

8. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein: When the processor executes the program, the steps of the programming language conversion method according to any one of claims 1 to 5 are implemented.

9. A non-transitory computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the programming language conversion method according to any one of claims 1 to 5 are implemented.

Citation Information

Patent Citations

  • Method for converting Transact-SQL (Structured Query Language) program into Java program

    CN110851142A

  • Method and device for compiling ST code into WebAssembly code

    CN114721667A