Compilation method, compiler, terminal and storage medium

By combining the static analysis of the source program and the characteristics of the target programming language, the target program that conforms to the target programming language form is generated, which solves the problems of small scope of application and large memory overhead in the compilation of the error handling mechanism in the existing technology, and realizes the Wide range of applicability and efficient memory utilization.

CN118760438BActive Publication Date: 2024-11-26SHENZHEN YUEBIT TECHNOLOGY CO LTD
View PDF 5 Cites 0 Cited by

Patent Information

Application Number
CN202411254592.X
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-09-09
Publication Date
2024-11-26
Estimated Expiration
2044-09-09

AI Technical Summary

Technical Problem

In the prior art, when compiling the error handling mechanism in the source program into the target programming language, the scope of application is small or additional memory allocation overhead is incurred.

Method used

By statically analyzing the source program of the original programming language, the error handling mechanism code is identified, and combined with the characteristics of the target programming language, the compiler is used to convert it to generate a target program that conforms to the target programming language form, and the target program contains the second error. Processing mechanism code, using the reduction of the flow graph of the target programming language.

Benefits of technology

It significantly expands the scope of available target programming languages, reduces additional memory allocation overhead, improves compilation efficiency and quality of generating target programs.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118760438B_ABST
    Figure CN118760438B_ABST
Patent Text Reader

Abstract

The present invention provides a compilation method, a compiler, a terminal and a storage medium, the method comprising: statically analyzing a source program in the form of an original programming language to identify a first error handling mechanism code, the first error handling mechanism code being a potential error definition, an error throwing statement and an error catching code block; based on the category of the error throwing statement and the error propagation path, in combination with the characteristics of a target programming language, using a compiler to convert the source program to obtain a target program in the form of a target programming language, the target program including a second error handling mechanism code; the control flow graph of the target programming language has reducibility. The present invention converts the source program by determining and utilizing the category of the error throwing statement and the error propagation path, in combination with the reducibility of the control flow graph in the target programming language, thereby significantly expanding the scope of the target programming language that can be used and reducing additional memory allocation overhead.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of software engineering, and in particular to a compiling method, a compiler, a terminal and a storage medium. Background Art

[0002] In the field of source program compilation, error handling mechanism is an indispensable part. Different programming languages ​​are designed with different error handling mechanisms. When compiling the source program of the original programming language into the program of the target programming language, one of the challenges is how to effectively convert the original error handling mechanism. The characteristics of the target programming language will determine how to compile the error handling mechanism in the original programming language.

[0003] In the prior art, two methods are usually used to compile the error handling mechanism in the source program into the error handling mechanism in the target programming language: first, the target programming language is required to support the exception control flow mechanism, and the exception control flow mechanism of the target programming language is used for compilation, which makes the scope of target programming languages ​​applicable to this method smaller; second, when the error handling is compiled into ordinary data plus ordinary control flow in the target programming language, additional memory allocation overhead may be generated when the error is generated.

[0004] Therefore, the prior art has defects and needs to be improved and developed. Summary of the invention

[0005] The technical problem to be solved by the present invention is that, in view of the above-mentioned defects of the prior art, a compilation method, a compiler, a terminal and a storage medium are provided, aiming to solve the problem in the prior art that when error handling is compiled into a target programming language, the scope of application is small or additional memory allocation overhead is generated.

[0006] The technical solution adopted by the present invention to solve the technical problem is as follows:

[0007] In a first aspect, an embodiment of the present invention provides a compilation method, the method comprising:

[0008] Performing static analysis on the source program in the original programming language form to identify a first error handling mechanism code, wherein the first error handling mechanism code is a preset compiler-defined potential error definition, an error throwing statement, and an error catching code block;

[0009] Based on the category of the thrown error statement and the error propagation path determined by the static analysis, and in combination with the characteristics of the target programming language, the source program is converted by the compiler to obtain a target program conforming to the form of the target programming language, wherein the target program includes a second error handling mechanism code;

[0010] Wherein, the control flow graph of the target programming language is reducible.

[0011] Optionally, the static analysis of the source program in the original programming language includes:

[0012] Performing syntax analysis on the first error handling mechanism code;

[0013] During the syntax parsing process, the conditions or states that may cause errors in the code are identified and marked as potential error definitions, the statements used to throw errors in the code are identified and marked as error throwing statements, and the code blocks used for error capture in the code are identified and marked as error capture code blocks.

[0014] Optionally, the category of the thrown error statement and the error propagation path determined based on the static analysis include:

[0015] Identifying a function in the source program that directly throws an error according to the throw error statement;

[0016] Set the function that directly throws the error as the current function;

[0017] Performing static analysis on the current function and determining the error propagation path according to the static analysis conclusion;

[0018] The category of the thrown error statement is determined according to the static analysis conclusion.

[0019] Optionally, the category of the thrown error statement and the error propagation path determined based on the static analysis further include:

[0020] The function that calls the current function is used as the calling function;

[0021] Recursively perform static analysis on the calling function, and determine the category of the thrown error statement and the error propagation path according to the static analysis conclusion.

[0022] Optionally, the recursively performing static analysis on the calling function includes:

[0023] Performing static analysis with the calling function as a new current function to determine an error propagation path and a category of the thrown error statement;

[0024] The step of converting the source program by using the compiler is implemented on the current function.

[0025] Optionally, the converting the source program using the compiler to obtain a target program in the target programming language in combination with the category of the thrown error statement and the error propagation path determined by the static analysis and the characteristics of the target programming language includes:

[0026] If the category of the thrown error declaration is a local error, the error catching code block in the source program is converted by utilizing the reducibility of the control flow graph of the target programming language based on the compiler and combining the error propagation path to obtain a second error handling mechanism code;

[0027] Converting the code outside the error capture code block in the source program according to the grammatical rules of the target programming language to obtain normal process processing logic code;

[0028] Integrate the normal process processing logic code and the second error handling mechanism code into a target program;

[0029] Among them, the second error handling mechanism code includes an expected error type, a processing code for the expected error type and a jump instruction, and the jump instruction is used to execute the process to jump to the processing code for the expected error type when an error matching the expected error type is detected.

[0030] Optionally, the converting the source program using the compiler based on the category of the thrown error statement and the error propagation path determined by the static analysis in combination with the characteristics of the target programming language to obtain a target program in the target programming language also includes:

[0031] If the category of the thrown error declaration is a non-local error, while converting the current function based on the error propagation path, statically analyze the calling function layer by layer, and convert layer by layer according to the analysis results;

[0032] During each function conversion process, the source program is converted based on the target programming language's support for multiple return values;

[0033] If the support condition is that the target programming language supports multiple return values, then based on the return result type of the current function in the source program, determining that the return result of the current function is in a multiple return value format, the return result in the multiple return value format includes a first flag bit for indicating an execution state, a field for storing a correct execution return result or a default value of a correct execution return result, and a field for storing an incorrect execution return result or a default value of an incorrect execution return result;

[0034] Based on the logic codes of the current function and the calling function, a first current function and a first calling function are obtained, wherein both the first current function and the first calling function support receiving and processing return results in a multiple return value format;

[0035] Adding a first logic code in the first current function and the first calling function, wherein the first logic code is used to determine whether to execute a normal process or jump to an error handling code according to a state of the first flag bit;

[0036] Converting the first logic code, the first current function, the first calling function, the throwing error statement and the error catching code block to form a target program;

[0037] The target program includes a second error handling mechanism code formed by converting the logic code, the error throwing statement and the error catching code block.

[0038] Optionally, converting the source program based on the target programming language's support for multiple return values ​​further includes:

[0039] If the support condition is that the target programming language does not support multiple return values, modify the return result type of the current function in the source program to a preset format, where the preset format is a format supported by the target programming language, and the return result in the preset format includes a second flag bit for identifying an execution state, and a field indicating an execution result;

[0040] Based on the logic codes of the current function and the calling function in the source program, a second current function and a second calling function are obtained, wherein both the second current function and the second calling function support receiving and processing the return result in the preset format;

[0041] Adding a second logic code in the second current function and the second calling function, wherein the second logic code is used to determine whether to execute a normal process or jump to an error handling code according to a state of the second flag bit;

[0042] Converting the second logic code, the second current function, the second calling function, the error throwing statement and the error catching code block to form a target program;

[0043] The target program includes a second error handling mechanism code formed by converting the second logic code, the error throwing statement and the error catching code block.

[0044] In a second aspect, an embodiment of the present invention further provides a compiler, including:

[0045] A static analysis module, used for statically analyzing a source program in the original programming language to identify a first error handling mechanism code, wherein the first error handling mechanism code is a potential error definition, an error throwing statement, and an error catching code block defined by a preset compiler;

[0046] A program conversion module is used to convert the source program using the compiler based on the category of the thrown error statement and the error propagation path determined by the static analysis and in combination with the characteristics of the target programming language, so as to obtain a target program in the form of the target programming language, wherein the target program includes a second error handling mechanism code; wherein the control flow graph of the target programming language is reducible.

[0047] In a third aspect, an embodiment of the present invention further provides a terminal, comprising: a memory, a processor, and a compiler stored in the memory and executable on the processor, wherein the compiler implements the steps of the compilation method described above when executed by the processor.

[0048] In a fourth aspect, an embodiment of the present invention further provides a computer-readable storage medium, wherein the computer-readable storage medium stores a compiler program, and the compiler program can be executed to implement the steps of the compilation method as described above.

[0049] Beneficial effects of the present invention: The embodiment of the present invention statically analyzes the source program in the original programming language to identify the first error handling mechanism code, which is a potential error definition, an error throwing statement, and an error catching code block; based on the category of the error throwing statement and the error propagation path, combined with the characteristics of the target programming language, the source program is converted by a compiler to obtain a target program in the form of the target programming language, and the target program contains the second error handling mechanism code; the control flow graph of the target programming language has reducibility. The present invention converts the source program by determining and utilizing the category of the error throwing statement and the error propagation path, combined with the reducibility of the control flow graph in the target programming language, which significantly expands the range of target programming languages ​​that can be used and reduces additional memory allocation overhead. BRIEF DESCRIPTION OF THE DRAWINGS

[0050] Figure 1 It is a flow chart of a preferred embodiment of the compiling method in the present invention.

[0051] Figure 2 It is a code schematic diagram of a first error handling mechanism in the present invention.

[0052] Figure 3 It is a functional principle block diagram of a preferred embodiment of the compiler in the present invention.

[0053] Figure 4 It is a terminal principle block diagram of the present invention. DETAILED DESCRIPTION

[0054] In order to make the purpose, technical solution and advantages of the present invention clearer and more specific, the present invention is further described in detail below with reference to the accompanying drawings and examples. It should be understood that the specific embodiments described herein are only used to explain the present invention and are not used to limit the present invention.

[0055] In the field of program development, error handling mechanism is an indispensable part, ensuring the robustness and maintainability of the program. It should be understood that "error" and "error handling" in this solution refer to errors that occur during program operation and the handling of errors during operation defined in the program, and do not refer to errors such as programmers writing programs that do not conform to syntax. For example, when reading and writing files, if the file does not exist or there is no permission to read and write, the code needs to jump out of the normal logic and perform error handling. Different programming languages ​​have different error handling methods. Error handling includes the definition of errors, the throwing of errors in the code, and the catching of errors.

[0056] Taking Java's error handling syntax as an example, all captureable errors in Java are represented by a class that inherits from the Exception class (including the Exception class itself). If it inherits from a subtype of RuntimeExcepton (including the RuntimeExcepton class itself), then there is no need to declare it in the function signature. Other error declarations must be explicitly declared in the function to define the function as an error declaration, such as public int read(char[] cbuf)throws IOException. Throwing errors in Java is done through the throw statement, whose parameters are subtypes of RuntimeException (including the RuntimeException class itself) or the error type declared by the function and its subtypes. When this type of function executes the throw statement, it directly stops the execution of the remaining code of the current function and tries to capture it. Capturing errors is done through the trycatch code block, which can have multiple catch statements, and different catch statements correspond to different possible error types. The sample code is as follows:

[0057] try { reader.read(chars);}

[0058] catch (IOException e) { e.printStackTrace();}.

[0059] When an error occurs, the try catch code block is matched from the inside to the outside, and the catch conditions are matched sequentially from top to bottom. If no match is found, the error will be thrown directly, returned to the call of the current function, and continue to try to catch the error until the program is stopped directly.

[0060] When using a compiler to compile an original programming language into a target programming language, the characteristics of the target programming language will determine how to convert the error mechanism in the original programming language. Take the error implementation based on the WebAssembly MVP definition in the WebAssembly System Interface (WASI) as an example. In the definition of WASI Preview 1, all errors are returned through the enumeration type Result. This type contains two possibilities, corresponding to the return value when correct and the return value when error. The actual binary interface requires the function caller to provide an additional integer parameter to represent the return value, which corresponds to an address space allocated in memory. In WASIPreview 1, the caller needs to allocate space in advance to hold the return value when correct and the return value when error, and provide a pointer to the space when calling; the callee indicates whether an error occurs through the return value, and writes data to the address pointed to by the pointer according to the situation. In the current draft (commit 9eda882), the space that the function caller needs to allocate also contains an integer to indicate whether an error occurs. In the WebAssembly exception handling proposal, there are special instructions for defining error types, defining attempts to execute parts of code and trying to catch possible errors in them, etc. Different error handling mechanisms affect the readability, maintainability, and performance of the program.

[0061] In the prior art, there are different ways to compile error handling in a source program into an error handling mechanism in a target programming language: the first is to limit the target programming language to support an exception control flow mechanism, and use the exception control flow mechanism of the target programming language for compilation, which makes the scope of target programming languages ​​applicable to this method smaller; the second is to compile error handling into ordinary data plus ordinary control flow in the target programming language, which may have additional memory allocation overhead when errors are generated.

[0062] In view of the above-mentioned defects of the prior art, the present invention provides a compilation method, a compiler, a terminal and a storage medium, the method comprising: statically analyzing a source program in the form of an original programming language to identify a first error handling mechanism code, the first error handling mechanism code being a potential error definition, an error throwing statement and an error catching code block; based on the category of the error throwing statement and the error propagation path, in combination with the characteristics of the target programming language, using a compiler to convert the source program to obtain a target program in the form of the target programming language, the target program containing a second error handling mechanism code; the control flow graph of the target programming language has reducibility. The present invention converts the source program by determining and utilizing the category of the error throwing statement and the error propagation path, in combination with the reducibility of the control flow graph in the target programming language, thereby significantly expanding the range of target programming languages ​​that can be used and reducing additional memory allocation overhead.

[0063] See also Figure 1 The compilation method described in the embodiment of the present invention comprises the following steps:

[0064] Step S100: statically analyze the source program in the original programming language to identify a first error handling mechanism code, wherein the first error handling mechanism code is a preset compiler-defined potential error definition, an error throwing statement, and an error catching code block.

[0065] Specifically, the first error handling mechanism code is used to handle errors that occur during the runtime of the source program and is in the original programming language form.

[0066] In one implementation, the static analysis of the source program in the original programming language includes:

[0067] Performing syntax analysis on the first error handling mechanism code;

[0068] During the syntax parsing process, the conditions or states that may cause errors in the code are identified and marked as potential error definitions, the statements used to throw errors in the code are identified and marked as error throwing statements, and the code blocks used for error capture in the code are identified and marked as error capture code blocks.

[0069] Specifically, the compiler of the present invention presets a grammar rule set, and based on different grammar rules in the grammar rule set, identifies different code blocks in the source code, and obtains potential error definitions, error throwing statements, and error catching code blocks. Potential error definitions are conditions or states that potentially cause errors, error throwing statements are statements in the code for throwing errors, and error catching code blocks are used to capture and process errors. The grammar rule set is set based on the grammar of the original programming language.

[0070] For example, the first error handling mechanism code diagram for the original programming language extension definition is as follows: Figure 2 As shown. Figure 2 In the code, the keyword raise is defined with a value to indicate an error statement, the syntax try { expression block} catch { error pattern matching} is defined to indicate an error catching code block, the syntax "type! error type" is defined to indicate that a function may have an error, and the operator "!" is defined to indicate that a function call may have an error. If the dividend in the current function div is 0, an exception will be thrown through the error statement raise, and the exception type is character type (string), so the return value type of the div function is Int!String. In the call function compute, the div function is used and suffixed with the "!" operator to indicate that an exception may be thrown. Around the div function code (before and after), try {}catch {} is used to wrap it, indicating that the exception is caught. At the same time, the error handling of the type string (string) is defined in catch through pattern matching, and the return value 0 is obtained.

[0071] According to Figure 2 The definition syntax shown in the figure sets a preset syntax rule set. According to the preset syntax rule set, raise "Divided by zero" is identified as an error throwing statement, Int!String is a potential error definition, and try {div(a,b)!} catch {(s : String) =>0}} is an error catching code block. It can be understood that the original programming language and the target programming language in the present invention can be a programming language form that can be read and written by programmers, and can also be a structured data representation form that can be read and written by machines.

[0072] See also Figure 1 The compiling method described in the embodiment of the present invention further comprises the following steps:

[0073] Step S200: Based on the category of the thrown error statement and the error propagation path determined by the static analysis, and in combination with the characteristics of the target programming language, the source program is converted using the compiler to obtain a target program that conforms to the form of the target programming language, wherein the target program includes a second error handling mechanism code; wherein the control flow graph of the target programming language is reducible.

[0074] Specifically, static analysis can be used to determine the category of the thrown error statement and the error propagation path, and then the source program is converted by using the category of the thrown error statement and the error propagation path in combination with the characteristics of the target programming language.

[0075] In one implementation, the category of the thrown error statement and the error propagation path determined based on the static analysis include:

[0076] Identifying a function in the source program that directly throws an error according to the throw error statement;

[0077] Set the function that directly throws the error as the current function;

[0078] Performing static analysis on the current function and determining the error propagation path according to the static analysis conclusion;

[0079] The category of the thrown error statement is determined according to the static analysis conclusion.

[0080] Specifically, first determine the current function based on the throw error statement, then perform static analysis on the current function to obtain the static analysis conclusion. If the static analysis conclusion shows that the error is thrown via the throw error statement and is captured and processed in the error capture code block of the current function, then the category of the throw error statement is determined to be a local error; if the static analysis conclusion shows that the error is thrown via the throw error statement and is not captured or processed in the error capture code block of the current function, then the category of the throw error statement is determined to be a non-local error. It can be understood that if the error is thrown via the throw error statement and the error propagation stops at the error capture code block of the current function, it means that the error has been processed by the error capture code block at this time and is a local error. If the error is not captured or processed by the error capture code block of the current function, the error will continue to propagate outward until it reaches the function that calls the error.

[0081] For example, in Figure 2 In the example code shown, because the current function div is included in the try module in the call function compute, and the processing code for the divisor being 0 is included in the catch module, the error propagation stops at this time, and the category of the thrown error declaration is determined to be a local error of the call function compute (a non-local error relative to the current function div). The meaning of error propagation stopping is that the program after the error capture code block catch module (including the program after the outer function calls the compute function) does not need to consider the possibility of a specific type of error, because the error capture code block determines all conditions and provides solutions.

[0082] In one implementation, the category of the thrown error statement and the error propagation path determined based on the static analysis further include:

[0083] The function that calls the current function is used as the calling function;

[0084] Recursively perform static analysis on the calling function, and determine the category of the thrown error statement and the error propagation path according to the static analysis conclusion.

[0085] Specifically, since the source program includes the calling function in addition to the current function. The calling function is the function that calls the current function, and there can be one or more calling functions. Therefore, static analysis is also required for the calling function to determine the category of the error statement thrown in the calling function and the error propagation path.

[0086] In one implementation, recursively performing static analysis on the calling function includes:

[0087] Performing static analysis with the calling function as a new current function to determine an error propagation path and a category of the thrown error statement;

[0088] The step of converting the source program by using the compiler is implemented on the current function.

[0089] Specifically, while performing static analysis on the calling function, the current function can be compiled and converted from the original programming language form to the target programming language form. This method can speed up the compilation speed and thus improve the efficiency of generating the target program.

[0090] In one implementation, the category of the thrown error statement and the error propagation path determined based on the static analysis, combined with the characteristics of the target programming language, converting the source program using the compiler to obtain a target program in the target programming language, includes:

[0091] If the category of the thrown error declaration is a local error, the error catching code block in the source program is converted by utilizing the reducibility of the control flow graph of the target programming language based on the compiler and combining the error propagation path to obtain a second error handling mechanism code;

[0092] Converting the code outside the error capture code block in the source program according to the grammatical rules of the target programming language to obtain normal process processing logic code;

[0093] Integrate the normal process processing logic code and the second error handling mechanism code into a target program;

[0094] Among them, the second error handling mechanism code includes an expected error type, a processing code for the expected error type and a jump instruction, and the jump instruction is used to execute the process to jump to the processing code for the expected error type when an error matching the expected error type is detected.

[0095] Specifically, the present invention converts the source program according to the characteristics of the target programming language for different categories of thrown error statements and error propagation paths, so as to obtain a target program in the target programming language. In the field of compilation and program analysis, the reducibility of the control flow graph (CFG) is an important concept. If a control flow graph can be transformed into a sequential execution process through a series of transformations (usually including node fusion, loop folding, etc.) without changing the structural semantics of the program, it is considered to be reducible. The reducibility analysis of the control flow graph helps to optimize the design of the compiler and improve the efficiency of program analysis, especially in terms of loop optimization and code reorganization. If the category of the thrown error statement is a local error, the error capture code is converted into a second error handling mechanism code during the compilation process. For example, if the error capture code is as follows:

[0096] try {

[0097] raise "Error"

[0098] } catch {

[0099] (s : String) =>0

[0100] }

[0101] It can be seen that in the error catching code, the error thrown by the raise statement is wrapped by the try catch expression. At this time, when the error catching code is compiled, it is directly compiled into a jump to the error handling.

[0102] For a target programming language that includes GC and multivalue proposals, the following is an example of a target program:

[0103] block $exception_handling (result i32)

[0104] block $try (result (ref null $string))

[0105] global.get$error

[0106] br $catch

[0107] br $exception_handling

[0108] end

[0109] block $catch (param (ref null $string)) (result i32)

[0110] drop

[0111] i32.const 0

[0112] end

[0113] end

[0114] It can be seen that in this target code, the expected error type is to return a string type. Use global.get $error to simulate the corresponding error value, and then directly use br $catch to jump to the corresponding catch error handling code block. The code is relatively concise. At the same time, the above compilation process only uses structured control flow, and does not use the try catch and other exception control flow instructions provided in the WebAssembly exception handling proposal, so it has a wider range of applications.

[0115] In one implementation, the converting the source program using the compiler based on the category of the thrown error statement and the error propagation path determined by the static analysis in combination with the characteristics of the target programming language to obtain a target program in the target programming language also includes:

[0116] If the category of the thrown error statement is a non-local error, converting the source program based on the support of the target programming language for multiple return values;

[0117] If the support condition is that the target programming language supports multiple return values, while converting the current function based on the error propagation path, statically analyze the calling function layer by layer, and convert layer by layer according to the analysis results;

[0118] During each function conversion process, the source program is converted based on the target programming language's support for multiple return values;

[0119] If the support condition is that the target programming language supports multiple return values, then based on the return result type of the current function in the source program, determining that the return result of the current function is in a multiple return value format, the return result in the multiple return value format includes a first flag bit for indicating an execution state, a field for storing a correct execution return result or a default value of a correct execution return result, and a field for storing an incorrect execution return result or a default value of an incorrect execution return result;

[0120] Based on the logic codes of the current function and the calling function, a first current function and a first calling function are obtained, wherein both the first current function and the first calling function support receiving and processing return results in a multiple return value format;

[0121] Adding a first logic code in the first current function and the first calling function, wherein the first logic code is used to determine whether to execute a normal process or jump to an error handling code according to a state of the first flag bit;

[0122] Converting the first logic code, the first current function, the first calling function, the throwing error statement and the error catching code block to form a target program;

[0123] The target program includes a second error handling mechanism code formed by converting the logic code, the error throwing statement and the error catching code block.

[0124] Specifically, when the target programming language supports multiple return values, the return result of the current function is represented in the form of multiple return values. Taking the target programming language as the Wasm language with a multivalue proposal as an example, its return value has a type check, so for the case of returning Int!Double, its return value type can be: (result i32 i32 f64). Among them, the first i32 is the first flag bit, indicating the execution status, and the execution status includes normal and error. If the execution status is normal, the second i32 represents the correct execution return result, and f64 represents the default value of the error execution return result. If the execution status is abnormal, the second i32 correctly executes the return result default value, and f64 represents the error execution return result. It can be understood that (result i32 i32 f64) is only an example. The first flag bit in the present invention is used to store the field of the correct execution return result or the correct execution return result default value, and the field used to store the error execution return result or the error execution return result default value. Its position is not fixed and can be adjusted according to the support of the target programming language.

[0125] After obtaining the return result in the form of multiple return values, the indication value can be allocated on the stack, and space can be allocated to the value of the normal situation or the value of the error situation, whichever occupies more space, and the actual execution result can be stored therein. Multiple registers can also be used to store the indication value, the correct value, and the error value respectively, which are not limited here. In the prior art, there is a method of compiling the error handling mechanism in the source program into ordinary data combined with ordinary control flow in the target programming language. In this case, there will be additional memory allocation overhead when an error is generated. If the error handling mechanism in the source program is compiled into the form of ordinary data plus ordinary control flow plus multiple return value characteristics in the target programming language, it may lead to a large range of using the multiple return value characteristics, and the overall compilation needs to be considered when using it, and the implementation process is relatively complicated. However, in the case where the target programming language supports multiple return values, the present invention can be reasonably used in memory allocation, and the implementation is simple, which effectively overcomes the disadvantages of the prior art.

[0126] It can be understood that in the present invention, when the first flag bit is normal, the normal process is executed; when the first flag bit is an error, the error handling code corresponding to the error is jumped.

[0127] In one implementation, converting the source program based on the target programming language's support for multiple return values ​​further includes:

[0128] If the support condition is that the target programming language does not support multiple return values, modify the return result type of the current function in the source program to a preset format, where the preset format is a format supported by the target programming language, and the return result in the preset format includes a second flag bit for identifying an execution state, and a field indicating an execution result;

[0129] Based on the logic codes of the current function and the calling function in the source program, a second current function and a second calling function are obtained, wherein both the second current function and the second calling function support receiving and processing the return result in the preset format;

[0130] Adding a second logic code in the second current function and the second calling function, wherein the second logic code is used to determine whether to execute a normal process or jump to an error handling code according to a state of the second flag bit;

[0131] Converting the second logic code, the second current function, the second calling function, the error throwing statement and the error catching code block to form a target program;

[0132] The target program includes a second error handling mechanism code formed by converting the second logic code, the error throwing statement and the error catching code block.

[0133] Specifically, when the target programming language does not support multiple return values, the return result is set according to the preset format. The preset format can be an enumeration type or a data structure supported by the target programming language, which is not limited here. The return result of the preset format has a second flag bit that marks the execution status and a field that represents the execution result. The present invention determines the subsequent process direction through the second flag bit. If the second flag bit is normal, the normal process is executed. If the second flag bit is an error, it jumps to the error handling code of the error. It can be understood that in the present invention, when the category of the thrown error statement is a local error, the compiler is directly used to convert the current function. When the category of the thrown error statement is a non-local error, it is necessary to find the calling function that calls the current function, that is, confirm the calling relationship, and perform static analysis on the calling function layer by layer according to the calling relationship. While the calling function is statically analyzed, the current function can be converted.

[0134] In one implementation, if there is a special definition in the source program that does not directly throw an error declaration (for example, when the current function is called and the error type of the current function is a non-local error, the error is not directly thrown), the called function can be directly converted, and static analysis can be performed on the called function and it can be verified whether the static analysis result is consistent with the error declaration of the source program.

[0135] The present invention performs different compilation processes according to the support status of the target programming language for multiple return values, so that the compilation method is more flexible and the range of target programming languages ​​that can be supported is wider.

[0136] Taking Wasm with GC and multivalue proposals as an example, an example of a target program is as follows:

[0137] (func $div (param i32 i32) (result (ref null $string) i32 i32)

[0138] local.get 1 i32.eqz

[0139] if (result (ref null $string) i32 i32)

[0140] global.get $error i32.const 0 i32.const 1

[0141] return

[0142] else

[0143] (ref.null $string) local.get 0 local.get 1 i32.div_s i32.const 0

[0144] return

[0145] end )

[0147] (func $compute (param i32 i32) (result i32)

[0148] block $exception_handling (result i32)

[0149] block $try (result (ref null $string))

[0150] local.get 0 local.get 1

[0151] call $div i32.eqz

[0152] if $if (param (ref null $string) i32) (result i32)

[0153] drop

[0154] br $try

[0155] else

[0156] br $if

[0157] end

[0158] br $exception_handling

[0159] end

[0160] block $catch (param (ref null $string)) (result i32)

[0161] drop

[0162] i32.const 0

[0163] end

[0164] end )

[0166] In this code, the error value of the div function uses the multivalue proposal and is returned as (result (ref null $string) i32 i32), corresponding to the error value, correct value, and indication value. The error return corresponds to an error value (here represented by global.get $error), the default value of the correct value (i32.const 0), and the indication value (i32.const 1). The correct return corresponds to a default value of the error value (ref.null $string), the correct value (local.get 0 local.get 1 i32.div_s), and the indication value (i32.const 0). Error handling is wrapped in block $exception_handling, which is divided into block $try and block $catch. Block $try corresponds to the code block after try, and block $catch corresponds to the code block after catch. For function calls that may fail, such as call$div, add conditions to determine whether the top value of the stack is zero, and the if code block, the elements it focuses on are the error value and the correct value at the top of the stack. If it is zero, it enters the first branch of if, removes the useless default value of the correct value, directly retains the error value on the stack and jumps to the end of the $try code block. If the result is non-zero, it enters the else branch, using br $if to only retain the correct value on the stack. At the end of the $try code block, br $exception_handling is used to skip the $catch code block and complete the calculation directly. The $catch code block handles the error value at the top of the stack. As you can see, the compilation of this code does not generate any additional memory allocation.

[0167] In summary, the present invention defines the syntax of the error handling mechanism based on the original programming language, performs static analysis on the potential error definition, the thrown error statement and the error capture code block to determine the category of the thrown error statement and the error propagation path, and compiles the source program into the target programming language based on different categories of thrown error statements and error propagation paths, combined with the characteristics of the target programming language. During the compilation process, code optimization can be performed based on whether the target programming language has the characteristic of multiple return values. In this way, the scope of the target programming language can be effectively expanded, the versatility is strong, and the memory allocation is reduced. There is no need to modify the overall compilation scheme, and the implementation is relatively simple. It can be understood that since the characteristics of the target programming language are used to perform special compilation of the error handling mechanism, a relatively higher quality target program can be generated.

[0168] In one embodiment, if Figure 3As shown, based on the above compilation method, the present invention also provides a compiler, including:

[0169] The static analysis module 100 is used to perform static analysis on the source program in the original programming language to identify a first error handling mechanism code, wherein the first error handling mechanism code is a potential error definition, an error throwing statement, and an error catching code block defined by a preset compiler;

[0170] A program conversion module 200 is used to convert the source program using the compiler based on the category of the thrown error statement and the error propagation path determined by the static analysis, combined with the characteristics of the target programming language, to obtain a target program that conforms to the form of the target programming language, wherein the target program includes a second error handling mechanism code; wherein the control flow graph of the target programming language is reducible.

[0171] It is understandable that, in one embodiment, the compiler also includes a front-end syntax parser. The front-end syntax parser is used to perform syntax parsing on the source program in the original programming language form, and the static parsing module uses the result of the syntax parsing to perform static analysis to identify the first error handling mechanism code, which is a potential error definition, an error throwing statement, and an error capture code block defined by the preset compiler; based on the category of the error throwing statement and the error propagation path determined by the static analysis, combined with the characteristics of the target programming language, the source program is converted by the back-end code generator to obtain a target program that conforms to the target programming language form, and the target program contains the second error handling mechanism code; wherein the control flow graph of the target programming language is reducible.

[0172] In another embodiment, the program conversion module includes a code generator and a compilation backend. The code generator generates an intermediate representation code of a preset compiler, and the compilation backend further converts the intermediate representation code to obtain a target file that conforms to the target programming language format.

[0173] It should be noted that the above explanations of the compilation method embodiment are also applicable to the compiler of this embodiment and will not be repeated here.

[0174] Based on the above embodiment, the present invention further provides a terminal, whose principle block diagram can be shown as follows: Figure 4As shown. The terminal includes a processor, a memory, a network interface and a display screen connected via a system bus. The processor of the terminal is used to provide computing and control capabilities. The memory of the terminal includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system and a compiler. The internal memory provides an environment for the operation of the operating system and the compiler in the non-volatile storage medium. The network interface of the terminal is used to communicate with an external terminal via a network connection. When the compiler is executed by the processor, the steps of any one of the above-mentioned compilation methods are implemented. The display screen of the terminal can be a liquid crystal display screen or an electronic ink display screen.

[0175] Those skilled in the art will understand that Figure 4 The principle block diagram shown in the figure is only a block diagram of a partial structure related to the scheme of the present invention, and does not constitute a limitation on the terminal to which the scheme of the present invention is applied. The specific terminal may include more or fewer components than those shown in the figure, or combine certain components, or have a different arrangement of components.

[0176] In one embodiment, a terminal is provided, comprising a memory, a processor, and a compiler stored in the memory and executable on the processor, wherein the compiler implements the steps of any one of the compilation methods provided in the embodiments of the present invention when executed by the processor.

[0177] An embodiment of the present invention further provides a computer-readable storage medium, on which a compiler is stored. When the compiler is executed by a processor, the steps of any one of the compilation methods provided in the embodiments of the present invention are implemented.

[0178] It should be understood that the serial numbers of the steps in the above embodiments do not imply a sequence of execution, and the execution sequence of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of the present invention.

[0179] The technicians in the relevant field can clearly understand that for the convenience and simplicity of description, only the division of the above-mentioned functional units and modules is used as an example for illustration. In practical applications, the above-mentioned function allocation can be completed by different functional units and modules as needed, that is, the internal structure of the above-mentioned compiler can be divided into different functional units or modules to complete all or part of the functions described above. The functional units and modules in the embodiment can be integrated in a processing unit, or each unit can exist physically alone, or two or more units can be integrated in one unit. The above-mentioned integrated unit can be implemented in the form of hardware or in the form of software functional units. In addition, the specific names of the functional units and modules are only for the convenience of distinguishing each other, and are not used to limit the scope of protection of the present invention. The specific working process of the units and modules in the above-mentioned system can refer to the corresponding process in the aforementioned method embodiment, which will not be repeated here.

[0180] In the above embodiments, the description of each embodiment has its own emphasis. For parts that are not described or recorded in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.

[0181] Those skilled in the art will appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of the present invention.

[0182] In the embodiments provided by the present invention, it should be understood that the disclosed compiler / terminal device and method can be implemented in other ways. For example, the compiler / terminal device embodiments described above are only illustrative, for example, the division of the above modules or units is only a logical function division, and in actual implementation, other division methods can be used, for example, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed.

[0183] The embodiments described above are only used to illustrate the technical solutions of the present invention, rather than to limit the same. Although the present invention has been described in detail with reference to the aforementioned embodiments, it should be understood by those skilled in the art that the technical solutions described in the aforementioned embodiments may still be modified, or some of the technical features therein may be replaced by equivalents. However, these modifications or replacements do not deviate from the spirit and scope of the technical solutions of the embodiments of the present invention, and should be included in the protection scope of the present invention.

Claims

1. A compiling method, characterized in that: The method comprises: Performing static analysis on the source program in the original programming language form to identify a first error handling mechanism code, wherein the first error handling mechanism code is a preset compiler-defined potential error definition, an error throwing statement, and an error catching code block; Based on the category of the thrown error statement and the error propagation path determined by the static analysis, and in combination with the characteristics of the target programming language, the source program is converted by the compiler to obtain a target program conforming to the form of the target programming language, wherein the target program includes a second error handling mechanism code; Wherein, the control flow graph of the target programming language is reducible; If the conclusion of the static analysis indicates that the error is thrown by the throw error declaration and is captured and processed in the error capture code block of the current function, then the category of the throw error declaration is determined to be a local error; if the conclusion of the static analysis indicates that the error is thrown by the throw error declaration and is not captured or processed in the error capture code block of the current function, then the category of the throw error declaration is determined to be a non-local error; The method of converting the source program using the compiler based on the category of the thrown error statement and the error propagation path determined by the static analysis and combining the characteristics of the target programming language to obtain a target program in the target programming language includes: If the category of the thrown error declaration is a local error, the compiler is used to convert the error capture code block in the source program based on the reducibility of the control flow graph of the target programming language and the error propagation path to obtain a second error handling mechanism code; the code outside the error capture code block in the source program is converted according to the grammatical rules of the target programming language to obtain a normal process processing logic code; the normal process processing logic code and the second error handling mechanism code are integrated into a target program; Based on the category of the thrown error statement and the error propagation path determined by the static analysis, and in combination with the characteristics of the target programming language, the source program is converted by the compiler to obtain a target program in the target programming language, which further includes: If the category of the thrown error declaration is a non-local error, and the function that calls the current function is the calling function, the current function is converted based on the error propagation path, and the calling function is statically analyzed layer by layer, and converted layer by layer according to the analysis results; during each function conversion process, the source program is converted based on the target programming language's support for multiple return values.

2. The compiling method according to claim 1, characterized in that: The static analysis of the source program in the original programming language includes: Performing syntax analysis on the first error handling mechanism code; During the syntax parsing process, the conditions or states that may cause errors in the code are identified and marked as potential error definitions, the statements used to throw errors in the code are identified and marked as error throwing statements, and the code blocks used for error capture in the code are identified and marked as error capture code blocks.

3. The compiling method according to claim 2, characterized in that: The category of the thrown error statement and the error propagation path determined based on the static analysis include: Identifying a function in the source program that directly throws an error according to the throw error statement; Set the function that directly throws the error as the current function; Performing static analysis on the current function and determining the error propagation path according to the static analysis conclusion; The category of the thrown error statement is determined according to the static analysis conclusion.

4. The compiling method according to claim 3, characterized in that: The category of the thrown error statement and the error propagation path determined based on the static analysis also include: Recursively perform static analysis on the calling function, and determine the category of the thrown error statement and the error propagation path according to the static analysis conclusion.

5. The compiling method according to claim 4, characterized in that: The recursively performing static analysis on the calling function comprises: Performing static analysis with the calling function as a new current function to determine an error propagation path and a category of the thrown error statement; The step of converting the source program by using the compiler is implemented on the current function.

6. The compiling method according to claim 5, characterized in that: The method of converting the source program using the compiler based on the category of the thrown error statement and the error propagation path determined by the static analysis and combining the characteristics of the target programming language to obtain a target program in the target programming language includes: The second error handling mechanism code includes an expected error type, a processing code for the expected error type and a jump instruction, wherein the jump instruction is used to execute the flow to jump to the processing code for the expected error type when an error matching the expected error type is detected.

7. The compiling method according to claim 5, characterized in that: The method of converting the source program using the compiler based on the category of the thrown error statement and the error propagation path determined by the static analysis and combining the characteristics of the target programming language to obtain a target program in the target programming language also includes: If the support condition is that the target programming language supports multiple return values, then based on the return result type of the current function in the source program, determining that the return result of the current function is in a multiple return value format, the return result in the multiple return value format includes a first flag bit for indicating an execution state, a field for storing a correct execution return result or a default value of a correct execution return result, and a field for storing an incorrect execution return result or a default value of an incorrect execution return result; Based on the logic codes of the current function and the calling function, a first current function and a first calling function are obtained, wherein both the first current function and the first calling function support receiving and processing return results in a multi-return value format; Adding a first logic code in the first current function and the first calling function, wherein the first logic code is used to determine whether to execute a normal process or jump to an error handling code according to a state of the first flag bit; Converting the first logic code, the first current function, the first calling function, the throwing error statement and the error catching code block to form a target program; The target program includes a second error handling mechanism code formed by converting the logic code, the error throwing statement and the error catching code block.

8. The compiling method according to claim 7, characterized in that: The converting of the source program based on the target programming language's support for multiple return values ​​also includes: If the support condition is that the target programming language does not support multiple return values, modify the return result type of the current function in the source program to a preset format, where the preset format is a format supported by the target programming language, and the return result in the preset format includes a second flag bit for identifying an execution state, and a field indicating an execution result; Based on the logic codes of the current function and the calling function in the source program, a second current function and a second calling function are obtained, wherein both the second current function and the second calling function support receiving and processing the return result in the preset format; Adding a second logic code in the second current function and the second calling function, wherein the second logic code is used to determine whether to execute a normal process or jump to an error handling code according to a state of the second flag bit; Converting the second logic code, the second current function, the second calling function, the error throwing statement and the error catching code block to form a target program; The target program includes a second error handling mechanism code formed by converting the second logic code, the error throwing statement and the error catching code block.

9. A compiler, characterized in that The steps applied to implement the compiling method according to any one of claims 1 to 8 include: A static analysis module, used for statically analyzing a source program in the original programming language to identify a first error handling mechanism code, wherein the first error handling mechanism code is a potential error definition, an error throwing statement, and an error catching code block defined by a preset compiler; A program conversion module is used to convert the source program using the compiler based on the category of the thrown error statement and the error propagation path determined by the static analysis and in combination with the characteristics of the target programming language, so as to obtain a target program in the form of the target programming language, wherein the target program includes a second error handling mechanism code; wherein the control flow graph of the target programming language is reducible.

10. A terminal, characterized in that: The terminal comprises: a memory, a processor, and a compiler stored in the memory and executable on the processor, wherein the compiler implements the steps of the compilation method according to any one of claims 1 to 8 when executed by the processor.

11. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a compiler program, and the compiler program can be executed to implement the steps of the compilation method according to any one of claims 1 to 8.

Citation Information

Patent Citations

  • Dynamic and static combined Java program exception handling and optimization method

    CN102117228A

  • Abnormality processing method and device, equipment and medium

    CN117112073A

  • WebAssessment module exception handling method and device and electronic equipment

    CN117667738A

  • Wasm instruction set generation method and device, terminal and storage medium

    CN118092887A

  • System and method for handling an exception in a program

    US20030018961A1