LLVM-based ST language compiling method and system
Through the LLVM-based front-back-end separation design and LLVM IR optimization, the scalability and maintainability of ST language compilers in the field of industrial automation is solved, efficient object code generation and multi-platform support are achieved, and compilation performance and code optimization effect are improved.
Patent Information
- Application Number
- CN202510454592.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-04-11
- Publication Date
- 2025-08-08
AI Technical Summary
Existing ST language compilers are poor in scalability and maintainability in the field of industrial automation, and cannot fully utilize the powerful capabilities of LLVM, resulting in inefficient target code generated, especially poor compilation performance and code optimization when dealing with complex industrial automation programs.
The front-end and back-end separation design based on LLVM is adopted, and intermediate code generation and optimization is carried out through LLVM IR, including lexical analysis, grammatical analysis, semantic analysis, intermediate code generation and object code generation. The lexical and grammatical analysis is used using Flex and Bison tools, and the LLVM compiler is final compiled, supporting multi-platform goals.
It improves the performance and maintainability of the compiler, supports multi-platform goals, can better meet the needs of complex industrial automation applications, and generate efficient target code.
Smart Images

Figure CN120447904A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of language compilation technology, and in particular to an LLVM-based ST language compilation method and system. Background Art
[0002] The Structured Text (ST) language defined by the IEC 61131-3 standard is widely used in industrial automation, particularly in PLC programming. Traditional ST language compilers typically employ a tightly coupled front-end and back-end architecture, resulting in poor scalability and maintainability. Furthermore, these compilers have limitations in code optimization and multi-platform support, preventing them from fully leveraging the advantages of modern compilation technology.
[0003] LLVM (Low-Level Virtual Machine), a universal compilation toolchain, is widely used in the compilation process of various programming languages due to its powerful intermediate representation (IR) and optimization tools. However, existing ST language compilation systems fail to fully utilize the powerful features of LLVM, resulting in inefficient generated target code. This is particularly true when processing complex industrial automation programs, resulting in poor compilation performance and code optimization.
[0004] The current ST language compiler lacks effective modular design, especially in terms of symbol table management, type checking, control flow optimization, etc. The processing methods of the existing system are relatively primitive and cannot meet the requirements of modern industrial automation for compiler performance, scalability and maintainability. Summary of the Invention
[0005] This paper aims to provide an LLVM-based ST language compilation method and system, employing a front-end and back-end separation design and using LLVM IR for intermediate code generation and optimization. This system not only improves compiler performance and maintainability but also supports multiple platform targets, better meeting the needs of complex industrial automation applications.
[0006] To achieve the above objectives,
[0007] In a first aspect, the present application provides an embodiment of the present application providing an LLVM-based ST language compilation method, including:
[0008] Take the ST source code as input and decompose it into token streams by applying a lexical analyzer;
[0009] Pass the token stream token through the syntax analyzer to build the abstract syntax tree AST;
[0010] Fill the global symbol table and transmit it to the next stage together with the abstract syntax tree;
[0011] Perform semantic analysis, including type checking, generate an abstract syntax tree with additional semantic information and pass it to the next stage;
[0012] Parse the abstract syntax tree with additional semantic information, generate LLVM IR language code and transmit it to the next stage;
[0013] The LLVM compiler receives the LLVM IR code and compiles it to output X86 and PowerPC target code.
[0014] In a second aspect, an embodiment of the present application provides an LLVM-based ST language compilation system, including:
[0015] The lexical analysis module uses regular expressions to identify lexical units in the source code and is generated using the Flex tool;
[0016] The syntax analysis module combines lexical units into grammatical structures according to grammatical rules and constructs an abstract syntax tree, which is generated using the Bison tool;
[0017] The semantic analysis module performs type checking on each node of the abstract syntax tree and generates additional semantic information;
[0018] The intermediate code generation module traverses the abstract syntax tree through the visitor pattern and generates corresponding LLVM IR instructions according to different node types;
[0019] The target code generation module is used to optimize the intermediate representation and convert the optimized LLVM IR into target machine code;
[0020] The symbol table module is used to manage and maintain the symbol information in the program during the entire compilation process, including variables, functions, data types, etc.
[0021] The error management module is used to detect, record, and report errors during the compilation process, helping developers quickly locate and fix problems in the code, improving compiler debugging efficiency and user experience.
[0022] The lexical analysis module includes:
[0023] Configuration module, used to configure how Flex works;
[0024] Auxiliary modules are used to declare external variables and functions, forward function prototype declarations, trace location structures, and function definitions;
[0025] Lexical analysis state machine, used to coordinate lexical and grammatical analysis and manage the working status of lexical analysis;
[0026] The lexical rule definition module defines the lexical rules defined by IEC61131-3 through regular expressions, which is used to identify lexical units in source code.
[0027] The syntax analysis module includes:
[0028] Auxiliary modules, which are used to contain header file declarations, external variable and function declarations, global variable declarations, forward function prototype declarations, tracking location macro definitions, and function definitions;
[0029] The symbol value management module is used to help Bison process different types of data, including unions of grammatical values, terminal symbols, and value types of non-terminal symbols;
[0030] The grammar rule definition module defines the grammar rules defined in IEC61131-3 through context-free grammar, is used to perform shift-reduce on lexical units, and construct an abstract syntax tree.
[0031] The semantic analysis module includes:
[0032] The global symbol table filling module compiles each node of the abstract syntax tree through the visitor pattern and fills the function table, function block type table, program type table and total type table;
[0033] The type checking module is used to perform variable declaration safety checks, control flow analysis, constant folding, type safety checks, left value checks, and array range checks on the abstract syntax tree, and to add semantic information to the abstract syntax tree.
[0034] The intermediate code generation module includes:
[0035] Output management module, used to implement formatting, printing, file output and other functions;
[0036] Code generator: used to implement LLVM IR conversion functions for various types of nodes.
[0037] The target code generation module includes:
[0038] Intermediate representation optimization module, using LLVM's opt tool to perform various code optimizations;
[0039] The code generation module maps the high-level operations in the intermediate representation to the instruction set of the target architecture, maps the virtual registers to the physical registers of the target architecture, and generates target machine code or assembly code based on the selected, allocated and scheduled instruction sequence.
[0040] The basic node types of the abstract syntax tree include symbol, token and list, which are used to represent symbols, lexical units and symbol lists respectively, and are provided with a visitor mode interface.
[0041] The declaration of each type of node of the abstract syntax tree is performed through macro definition.
[0042] The visitor modes include an empty interface mode, an iterative interface access mode, a lookup interface access mode, and an LLVM IR conversion interface access mode.
[0043] The global symbol table includes:
[0044] Fill function table: a symbol table containing all globally declared function POUs;
[0045] Fill in the function block type table: Contains the symbol table of all globally declared function block POUs;
[0046] Fill in the program type table: a symbol table containing all globally declared program POUs;
[0047] Populate the total type table: A symbol table containing definitions of all user-declared data types.
[0048] The intermediate code generation module traverses various types of nodes in the abstract syntax tree through the visitor pattern and converts them into LLVM IR instructions. The specific process includes:
[0049] Variable declaration: allocate memory space for variables in LLVM. Global variables are defined using the @ symbol, local variables are allocated memory using the alloca instruction, and variable information is added to the symbol table.
[0050] Constants and variable references: Constants generate LLVM constant values, and variable references get their addresses from the symbol table and use the load instruction to load their values;
[0051] Arithmetic operations: Generate corresponding LLVM IR instructions based on the operators in the AST nodes;
[0052] Control flow: Process conditional statements (such as IF) and loop statements, generate basic blocks and branch jump instructions (br);
[0053] Function definition and call: Generate entry and return instructions for function definition, as well as function call instructions;
[0054] Compared with the prior art, the present invention has the following beneficial effects:
[0055] 1. By using LLVM as a backend, this invention supports multiple hardware platforms and generates efficient target code. Leveraging LLVM's optimization techniques, this approach reduces redundancy and improves execution speed. This approach is particularly suitable for industrial automation systems with stringent requirements for real-time performance and efficiency, thereby improving the compiler's portability and performance.
[0056] 2. The compilation system adopts a flexible macro definition expansion method, which can quickly support new syntax structures and functional requirements. The modular design makes the system easier to debug and expand.
[0057] 3. The visitor pattern decouples the compiler's logic and data structure, improves the maintainability and scalability of the system, and provides a solid foundation for future functional expansion. BRIEF DESCRIPTION OF THE DRAWINGS
[0058] In order to more clearly illustrate the technical solutions of the embodiments of the present application, the following is a brief introduction to the drawings required for use in the embodiments of the present application. It should be understood that the following drawings only show certain embodiments of the present application and therefore should not be regarded as limiting the scope. For ordinary technicians in this field, other relevant drawings can be obtained based on these drawings without creative work.
[0059] Figure 1 It is a system structure diagram of the present invention;
[0060] Figure 2 This is a flow chart of the lexical analysis state machine of the present invention;
[0061] Figure 3 This is a grammatical analysis workflow diagram of the present invention. DETAILED DESCRIPTION
[0062] In order to make the purpose, technical solutions and advantages of the present invention more clearly understood, the present invention is further described in detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present invention and are not intended to limit the present invention. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of the present invention.
[0063] It should be noted that the descriptions of "first", "second", etc. in the present invention are for descriptive purposes only and should not be understood as indicating or implying their relative importance or implicitly indicating the number of the indicated technical features. Therefore, the features defined as "first" or "second" may explicitly or implicitly include at least one of such features. In addition, the technical solutions between the various embodiments can be combined with each other, but this must be based on the fact that they can be implemented by ordinary technicians in this field. When the combination of technical solutions is contradictory or cannot be implemented, it should be deemed that such a combination of technical solutions does not exist and is not within the scope of protection required by the present invention.
[0064] (1) System structure, such as Figure 1 As shown:
[0065] 1) Lexical analysis
[0066] Lexical analysis is the first step in the front end of a compiler. The main task of a lexical analyzer is to decompose the source code into a sequence of tokens in a certain order.
[0067] The lexical analyzer uses regular expressions as a formal description of lexical rules, converting each regular expression into an equivalent finite automaton to achieve the corresponding word recognition process. A regular expression is an expression that describes the rules for string composition. It uses pre-agreed specific characters and other characters to form a special "regular string" that can match any string that conforms to a certain syntactic rule. In other words, it is the set of all strings that conform to a certain syntactic rule.
[0068] The present invention designs a lexical analysis state machine based on finite automata to synchronize the collaboration between bison and flex. Multiple states are explicitly defined in the lexical description file, and state switching is manually controlled. The workflow of the state machine is as follows: Figure 2 As shown in the figure, INITIAL is the initial state, used for parsing global content; header_state is the header file parsing state, used for parsing header file references; vardecl_state is the variable declaration state, used for parsing variable declarations; vardecl_list_state is the variable list state, used for parsing variable declarations; and body_state is the function body parsing state, used for parsing function bodies. A series of function interfaces for communication between Bison and Flex have also been designed. During the actual compilation process, the main lexical parsing function yylex() generated by Flex does not run actively, but is instead called by the parser.
[0069] 2) Syntax analysis
[0070] The parser is generated by the Bison tool and uses Extended Backus-Naur Form (EBNF) to describe the grammatical rules of the ST language. Bison implements bottom-up grammatical analysis based on the LALR(1) algorithm. The parser processes input symbols through two core operations: "shift" and "reduce". The grammatical analysis workflow is as follows: Figure 3As shown. The parser moves the symbol stream obtained from the lexical analysis into the symbol stack in sequence. By analyzing the contents of the symbol stack and the current input symbol, the parser can uniquely determine the next operation: if the shift condition is met, the current input symbol is pushed into the stack; if the reduction condition is met, the handle at the top of the stack is reduced to a non-terminal symbol according to the EBNF production rules predefined in Bison, and the corresponding abstract syntax tree (AST) node is constructed. Each reduction operation is performed strictly according to the handle of the current sentence pattern and generates the corresponding syntax subtree. This process continues until all the words in the ST source program have been processed and only the start symbol remains in the symbol stack. At this time, the parser completes the construction of the syntax tree for the entire program.
[0071] 3) Abstract Syntax Tree
[0072] The final product of grammatical analysis is an abstract syntax tree. This paper adopts an object-oriented design concept and achieves flexible node expansion through inheritance and polymorphism. The basic node types of the AST include symbol, token, and list, which are used to represent symbols, lexical units, and symbol lists, respectively.
[0073] Symbol is the base class of all AST nodes, which defines the common properties and methods of the nodes. Each AST node contains lexical information (such as starting line, starting column, ending line, ending column, file name, etc.) to support error location and debugging. In addition, the node also contains data type information (such as candidate data type and final data type) to support semantic analysis and type checking. Scope information (such as scope) is used to support symbol table management and scope analysis. Token inherits from symbol and is used to represent lexical units (such as keywords, identifiers, constants, etc.). The token node contains the value of the lexical unit. List also inherits from symbol and is used to represent a symbol list. The list node contains a dynamic array (elements) to store child nodes and provides methods for adding, inserting, deleting, and searching child nodes. Through the design of dynamic arrays, the list node can flexibly represent complex grammatical structures.
[0074] In order to support the expansion of complex grammatical structures in the ST language, AST node types are expanded through macro definitions. For example, the SYM_TOKEN macro is used to define lexical unit node types, such as real, integer, binary_integer, etc. These node types are implemented by inheriting token and can represent various lexical units in the ST language. Macros such as SYM_REF1 and SYM_REF2 are used to define node types that contain reference child nodes. For example, the integer_literal node contains two child nodes (type and value), which represent the type and value of the literal value respectively. Through macro definitions, the design of AST node types is highly flexible and extensible, and can easily support new grammatical structures and functional extensions.
[0075] 4) Semantic Analysis
[0076] Semantic analysis is performed on the abstract syntax tree (ABST) generated by syntactic analysis. Semantic analysis primarily refers to static semantic analysis, including declaration and type matching and type conversion. After the semantic analysis submodule, each expression node in the ABST is assigned a type, resulting in a type-labeled ABST. Key type checking tasks include: The external checker class ensures consistency between external and global variable declarations by searching and comparing variable declarations and types; the instruction control flow analysis class implements instruction control flow analysis, effectively handling control flow within functions, function blocks, and programs; the constant folding class performs constant folding, traversing the program's ABST, evaluating constant expressions, and replacing their results with the corresponding positions; the filler class analyzes the possible data types of each expression, field, or other expression and annotates the ABST; the assignment checker class verifies the legality of assignments, ensuring that lvalues meet specific conditions: assignments to control variables, output variables, constants, and expressions are not permitted; and the array range checker class implements array range checking.
[0077] 5) Intermediate code generation
[0078] The intermediate code generation module converts the abstract syntax tree (AST) of the ST language into LLVM IR. The AST is a tree structure generated by the compiler front-end that represents the syntactic hierarchy of the source code. LLVM IR, on the other hand, is a linear intermediate language based on the Static Single Assignment (SSA) format. To convert from the AST to LLVM IR, the compiler traverses the AST and generates corresponding LLVM IR instructions based on the node type.
[0079] The present invention uses the visitor pattern to traverse the abstract syntax tree, which separates the data structure from the data operation, so that new operations can be defined without modifying the data structure. The present invention designs a visitor pattern interface for LLVM IR code generation. The main implementation method is:
[0080] Variable declaration: allocate memory space for variables in LLVM. Global variables are defined using the @ symbol, local variables are allocated memory using the alloca instruction, and variable information is added to the symbol table.
[0081] Constants and variable references: Constants generate LLVM constant values, and variable references get their addresses from the symbol table and use the load instruction to load their values;
[0082] Arithmetic operations: Generate corresponding LLVM IR instructions based on the operators in the AST nodes;
[0083] Control flow: Process conditional statements (such as IF) and loop statements, generate basic blocks and branch jump instructions (br);
[0084] Function definition and call: Generate entry and return instructions for function definition, as well as function call instructions;
[0085] 5) Target code generation
[0086] After receiving LLVM IR, the LLVM intermediate representation (IR) is optimized. LLVM's opt tool performs various code optimizations, with each pass passing the IR to the next optimization pass. The optimized LLVM IR is converted into target machine code, including instruction selection, register allocation, and code generation. Instruction selection maps high-level operations in the IR to the target architecture's instruction set. This process selects the instructions most suitable for the target architecture to implement the required operations in the generated target code. Register allocation maps virtual registers to physical registers on the target architecture. This includes global register allocation and local register allocation. Global register allocation considers register requirements for an entire function or module, while local register allocation is performed within basic blocks or smaller code units. Instruction scheduling reorders the instruction sequence to maximize utilization of the processor's pipeline and other parallel execution resources. Instruction scheduling can be divided into static scheduling and dynamic scheduling. Static scheduling is performed during compilation, while dynamic scheduling is typically performed by the processor hardware. Target code generation generates target machine code or assembly code based on the selected, allocated, and scheduled instruction sequence. This step takes into account the characteristics and limitations of the target architecture to ensure that the generated code runs correctly in the target environment.
[0087] (2) Compilation process
[0088] Compilation begins with lexical analysis. The lexical analyzer breaks the source code into a sequence of tokens. It describes lexical rules in regular form and converts them into a finite automaton to recognize words. The lexical analysis state machine designed in this paper defines multiple states, such as INITIAL, to synchronize bison and flex. The parser then calls the yylex() function generated by flex.
[0089] Next, we move on to grammatical analysis. Using the Bison tool, we generate a grammatical analyzer based on Extended Backus-Naur Form (EBNF), working from the bottom up using the LALR(1) algorithm. It moves the symbol stream obtained from lexical analysis into a symbol stack and constructs an abstract syntax tree (AST) based on shift and reduce conditions until all words are processed and the grammar tree is constructed.
[0090] During the construction of the abstract syntax tree, a symbol table is populated. Its basic node types include symbol, token, and list, and flexible expansion is achieved through inheritance, polymorphism, and macro definitions. Semantic analysis performs static semantic analysis on the syntax tree, including declaration and type matching and type conversion. Each expression node is identified using this type, and tasks such as external variable declaration consistency checking, instruction control flow analysis, constant folding, candidate data type filling, assignment checking, and array range checking are also performed.
[0091] Intermediate code generation uses the visitor pattern to traverse the abstract syntax tree and convert it into LLVM IR, including memory allocation during variable declaration, constant and variable reference processing, arithmetic operation instruction generation, control flow instruction generation, and function definition and call instruction generation.
[0092] In the target code generation phase, LLVM IR is first optimized using LLVM's opt tool, and then the optimized result is converted into target machine code. This process covers instruction selection, register allocation, and instruction scheduling, and finally generates target code based on the processed instruction sequence.
[0093] The error management mechanism monitors the entire compilation process. Lexical analysis will report errors if it encounters illegal characters, syntax analysis will report errors if the input symbol stream doesn't conform to grammatical rules, and semantic analysis will report errors if there's a mismatch between declarations and types. These error messages help developers identify and correct problems, ensuring accurate compilation and program correctness.
[0094] Those skilled in the art may implement the present invention in various variations without departing from the scope and spirit of the present invention. For example, a feature of one embodiment may be applied to another embodiment to obtain yet another embodiment. Any modifications, equivalent substitutions, and improvements made within the technical concept of the present invention shall be within the scope of the present invention.
Claims
1. A ST language compilation method based on LLVM, characterized in that: The specific steps include: Take the ST source code as input and decompose it into token streams by applying a lexical analyzer; Pass the token stream token through the syntax analyzer to build the abstract syntax tree AST; Fill the global symbol table and transmit it to the next stage together with the abstract syntax tree; Perform semantic analysis, including type checking, generate an abstract syntax tree with additional semantic information and pass it to the next stage; Parse the abstract syntax tree with additional semantic information, generate LLVM IR language code and transmit it to the next stage; The LLVM compiler receives the LLVM IR code and compiles it to output X86 and PowerPC target code.
2. An LLVM-based ST language compilation system, characterized in that: include: The lexical analysis module uses regular expressions to identify lexical units in the source code and is generated using the Flex tool; The syntax analysis module combines lexical units into grammatical structures according to grammatical rules and constructs an abstract syntax tree, which is generated using the Bison tool; The semantic analysis module performs type checking on each node of the abstract syntax tree and generates additional semantic information; The intermediate code generation module traverses the abstract syntax tree through the visitor pattern and generates corresponding LLVM IR instructions according to different node types; The target code generation module is used to optimize the intermediate representation and convert the optimized LLVM IR into target machine code; The symbol table module is used to manage and maintain the symbol information in the program during the entire compilation process, including variables, functions, and data types; The error management module is used to detect, record, and report errors during the compilation process, helping developers quickly locate and fix problems in the code, improving compiler debugging efficiency and user experience.
3. The LLVM-based ST language compilation system according to claim 2, characterized in that: The lexical analysis module includes: Configuration module, used to configure how Flex works; Auxiliary modules are used to declare external variables and functions, forward function prototype declarations, trace location structures, and function definitions; Lexical analysis state machine, used to coordinate lexical and grammatical analysis and manage the working status of lexical analysis; The lexical rule definition module defines the lexical rules defined by IEC61131-3 through regular expressions, which is used to identify lexical units in source code.
4. The LLVM-based ST language compilation system according to claim 2, wherein: The syntax analysis module includes: Auxiliary modules, which are used to contain header file declarations, external variable and function declarations, global variable declarations, forward function prototype declarations, tracking location macro definitions, and function definitions; The symbol value management module is used to help Bison process different types of data, including unions of grammatical values, terminal symbols, and value types of non-terminal symbols; The grammar rule definition module defines the grammar rules defined in IEC61131-3 through context-free grammar, is used to perform shift-reduce on lexical units, and construct an abstract syntax tree.
5. The LLVM-based ST language compilation system according to claim 2, characterized in that: The semantic analysis module includes: The global symbol table filling module compiles each node of the abstract syntax tree through the visitor pattern and fills the function table, function block type table, program type table and total type table; The type checking module is used to perform variable declaration safety checks, control flow analysis, constant folding, type safety checks, left value checks, and array range checks on the abstract syntax tree, and to add semantic information to the abstract syntax tree.
6. The LLVM-based ST language compilation system according to claim 2, characterized in that: The intermediate code generation module includes: Output management module, used to implement formatting, printing, file output and other functions; Code generator: used to implement LLVM IR conversion functions for various types of nodes; The target code generation module includes: Intermediate representation optimization module, using LLVM's opt tool to perform various code optimizations; The code generation module maps the high-level operations in the intermediate representation to the instruction set of the target architecture, maps the virtual registers to the physical registers of the target architecture, and generates target machine code or assembly code based on the selected, allocated and scheduled instruction sequence.
7. The LLVM-based ST language compilation system according to claim 4, characterized in that: The basic node types of the abstract syntax tree include symbol, token and list, which are used to represent symbols, lexical units and symbol lists respectively, and are provided with a visitor mode interface; the declaration of each type of node of the abstract syntax tree is performed through macro definition.
8. The LLVM-based ST language compilation system according to claim 5, characterized in that: The visitor modes include an empty interface mode, an iterative interface access mode, a lookup interface access mode, and an LLVM IR conversion interface access mode.
9. The LLVM-based ST language compilation system according to claim 5, characterized in that: The global symbol table includes: Fill function table: a symbol table containing all globally declared function POUs; Fill in the function block type table: Contains the symbol table of all globally declared function block POUs; Fill in the program type table: a symbol table containing all globally declared program POUs; Populate the total type table: A symbol table containing definitions of all user-declared data types.
10. The LLVM-based ST language compilation system according to claim 6, characterized in that: The intermediate code generation module traverses various types of nodes in the abstract syntax tree through the visitor pattern and converts them into LLVM IR instructions. The specific process includes: Variable declaration: allocate memory space for variables in LLVM. Global variables are defined using the @ symbol, local variables are allocated memory using the alloca instruction, and variable information is added to the symbol table. Constants and variable references: Constants generate LLVM constant values, and variable references get their addresses from the symbol table and use the load instruction to load their values; Arithmetic operations: Generate corresponding LLVM IR instructions based on the operators in the AST nodes; Control flow: Process conditional statements and loop statements, generate basic blocks and branch jump instructions; Function definition and call: Generates the entry and return instructions of the function definition, as well as the function call instruction.
Citation Information
Cited By
Structured text code generation and review method
CN120743231A
Program debugging method and system driven by artificial intelligence
CN122019346A
A system and method for parsing PLC intermediate code based on compilation technology and automatically generating data bridge description
CN122387462A