CN111488154B - ST language source code compiling method, device, computer equipment and medium - Google Patents

ST language source code compiling method, device, computer equipment and medium Download PDF

Info

Publication number
CN111488154B
CN111488154B CN202010328050.8A CN202010328050A CN111488154B CN 111488154 B CN111488154 B CN 111488154B CN 202010328050 A CN202010328050 A CN 202010328050A CN 111488154 B CN111488154 B CN 111488154B
Authority
CN
China
Prior art keywords
source code
declaration
language
compiling
code
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Active
Application number
CN202010328050.8A
Other languages
Chinese (zh)
Other versions
CN111488154A (en
Inventor
李胤颉
宋健玮
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Kyland Technology Co Ltd
Original Assignee
Kyland Technology Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Kyland Technology Co Ltd filed Critical Kyland Technology Co Ltd
Priority to CN202010328050.8A priority Critical patent/CN111488154B/en
Publication of CN111488154A publication Critical patent/CN111488154A/en
Application granted granted Critical
Publication of CN111488154B publication Critical patent/CN111488154B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation
    • G06F8/42Syntactic analysis
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation
    • G06F8/42Syntactic analysis
    • G06F8/425Lexical analysis
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation
    • G06F8/44Encoding
    • G06F8/447Target code generation
    • YGENERAL TAGGING OF NEW TECHNOLOGICAL DEVELOPMENTS; GENERAL TAGGING OF CROSS-SECTIONAL TECHNOLOGIES SPANNING OVER SEVERAL SECTIONS OF THE IPC; TECHNICAL SUBJECTS COVERED BY FORMER USPC CROSS-REFERENCE ART COLLECTIONS [XRACs] AND DIGESTS
    • Y02TECHNOLOGIES OR APPLICATIONS FOR MITIGATION OR ADAPTATION AGAINST CLIMATE CHANGE
    • Y02DCLIMATE CHANGE MITIGATION TECHNOLOGIES IN INFORMATION AND COMMUNICATION TECHNOLOGIES [ICT], I.E. INFORMATION AND COMMUNICATION TECHNOLOGIES AIMING AT THE REDUCTION OF THEIR OWN ENERGY USE
    • Y02D10/00Energy efficient computing, e.g. low power processors, power management or thermal management

Landscapes

  • Engineering & Computer Science (AREA)
  • General Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Software Systems (AREA)
  • Physics & Mathematics (AREA)
  • General Physics & Mathematics (AREA)
  • Debugging And Monitoring (AREA)

Abstract

A ST language source code compiling method, comprising: acquiring a service source code in an ST language source code; performing lexical and grammatical analysis on the service source code according to lexical and grammatical rules to obtain an abstract grammar tree; generating instruction codes which can be identified by the target equipment according to the abstract syntax tree by the GCC back end. Compiling apparatus, computing devices and storage media are also provided accordingly. The invention can realize the compiling of the ST language source code based on the GCC back end, thereby supporting the direct debugging of the ST language source code.

Description

ST language source code compiling method, device, computer equipment and medium
Technical Field
The invention relates to the technical field of computer software, in particular to a method, a device, computer equipment and a medium for compiling ST language source codes.
Background
The ST language source code is one of programming languages of a programmable controller (PLC) in the IEC61131-3 standard, and is suitable for writing a PLC program with a complex structure in a large and medium-sized system. The PLC is designed and manufactured for industrial control application, the logic control is realized through a PLC programming language, and 5 PLC programming languages are defined by international standard IEC 61131-3: ladder Diagram (LD), functional Block Diagram (FBD), sequential functional diagram (SFC), instruction List (IL) and Structured Text (ST). The first three are visual programming languages, and are characterized by being simple and visual, wherein the ladder diagram is similar to the traditional relay control diagram, and a user can write the ladder diagram program without professional computer programming knowledge, so that the application of the PLC in production practice is greatly promoted; the instruction list and the ST language source code belong to a text programming language, and the instruction list is similar to assembly language and has a corresponding relation with the ladder diagram; the ST language source code is similar to the C language and the PASCAL language, is suitable for writing complex programs, and accords with the use habit of programmers.
The ST compiler is used for converting ST language source codes written by programmers into code instructions which can be recognized by the PLC according to rules of lexical analysis and grammar analysis of the ST language source codes in IEC-61131-3. At present, ST compilers are few, most of the ST compilers are realized by each large manufacturer, the realization modes are approximately the same, and the basic adopted mode is a compiling scheme as shown in a flow chart of fig. 1: firstly, performing lexical analysis and grammar analysis on an ST language source code, converting the ST language source code into a C language, and then calling a GCC compiler to compile the C language into a code instruction which can be recognized by a PLC so as to realize downloading into the PLC. For example, the open source software iec c adopts the scheme to realize the compiling of the ST language source code. Such a compiling method is easy to implement, but the drawbacks are also obvious, for example:
1. the compiling process needs two stages, resulting in slow compiling speed: after the language C is compiled by iec C, the GCC is continuously called for secondary compiling.
2. Debugging is difficult: the code instruction of the downloaded program compiled by GCC is converted by C language, so that the code displayed in the debugging process is the converted C language code, and can not be directly compared with ST language source code, and breakpoint debugging is difficult to support.
3. Error reporting information is inaccurate: because the code instruction of the downloading program is compiled by GCC (global control system) to C language, in the debugging process, the prompt in the code instruction running process is the prompt to C language errors and does not directly prompt to ST language source code errors, so that a developer of an ST compiler is required to carry out conversion to prompt engineering personnel, the method is complex, and the information misguidance to the engineering personnel is often caused by unsmooth communication between the developer and the engineering personnel, so that the debugging support to ST language source code is poor.
Disclosure of Invention
In view of the foregoing, a primary object of the present invention is to provide a method, an apparatus, a computing device and a medium for compiling ST language source code, so as to enable compiling ST language source code directly, thereby supporting direct debugging of ST language source code.
The invention provides a compiling method of ST language source codes, which comprises the following steps:
acquiring a service source code in an ST language source code;
performing lexical and grammatical analysis on the service source code according to lexical and grammatical rules to obtain an abstract grammar tree;
generating instruction codes which can be identified by the target equipment according to the abstract syntax tree by the GCC back end.
By the method, the abstract grammar tree is obtained by lexical and grammatical analysis of the service source codes in the ST language source codes, and then the abstract grammar tree is compiled by the GCC back end according to the abstract grammar tree, so that machine code instructions are generated. The compiling method of the invention realizes direct compiling of the ST language source code without using the C language as an intermediate language, so that the compiling speed is high, and the service source code in the ST language source code is directly mapped into debug_info, so that the service source code can be directly compared with the service source code in the debugging process, and the compiling and error reporting are carried out on the service source code in the debugging process, thereby realizing good support for the debugging process and solving the technical problems mentioned in the background art.
Wherein the step of obtaining an abstract syntax tree comprises; performing lexical analysis and grammar analysis on the service source codes to obtain each node stored in a tree structure; and traversing each node in turn to form the abstract syntax tree.
By the method, the mode that each node stored in the tree structure is firstly obtained, and then each node is traversed in sequence to form the abstract syntax tree is avoided, the mode that conversion is needed through an intermediate language (such as a C language) in the prior art is avoided, the overall compiling speed is increased, and the support of direct debugging of service source codes in ST language source codes is also realized due to no conversion of the intermediate language.
Wherein, still include: acquiring declaration class source codes in ST language source codes; the get abstract syntax tree is also obtained from the declarative class source code.
By writing the service source code and the declaration type source code of the ST language source code respectively, the service source code mainly comprises a part of logic judgment, operation and function call function in code operation, so that the information of the service source code content is conveniently recorded in the debug_info section, and programmers can mainly pay attention to the part of the service source code in the breakpoint debugging process.
The service source code and the declaration class source code in the ST language source code are acquired through a JSON format file containing the service source code and the declaration class source code in the ST language source code.
By the above, the JSON text format completely independent of language is adopted, and the method has the advantages of easiness in reading and writing and easiness in machine analysis and generation.
Wherein the declaration class source code includes source code for at least one of: global variable declaration, local variable declaration, structure declaration, and array declaration.
By the above, these declaration source codes and service source codes in the ST language source code are respectively written, so that programmers can pay attention to the writing and subsequent debugging of the service source codes.
The source code of the service source code is recorded in a debug information debug_info section; and when judging that the following source codes are not recorded in the debug_info section: the declaration class source code and the conditional class statement source code.
By the method, the source codes of the declaration class and the conditional class are not recorded in the debug_info section, so that a programmer can mainly pay attention to debugging of the service source code part in the breakpoint debugging process.
The invention also provides a compiling device for the ST language source code, which comprises the following steps:
The acquisition module is used for acquiring service source codes in the ST language source codes;
the front-end compiling module is used for carrying out lexical and grammatical analysis on the service source codes according to lexical and grammatical rules to obtain an abstract grammar tree;
and the back-end compiling module is used for generating instruction codes which can be identified by the target equipment by the GCC back-end according to the abstract syntax tree.
Wherein the front-end compiling module comprises;
the node generation module is used for performing lexical analysis and grammar analysis on the service source codes to obtain each node stored in a tree structure;
and the abstract syntax tree generating module is used for traversing each node in sequence to form the abstract syntax tree.
The present invention also provides a computing device comprising:
a bus;
a communication interface connected to the bus;
at least one processor coupled to the bus; and
at least one memory coupled to the bus and storing program instructions that, when executed by the at least one processor, cause the at least one processor to perform any of the methods described above.
The present invention also provides a computer readable storage medium having stored thereon program instructions which when executed by a computer cause the computer to perform any of the methods described above.
In summary, the compiler of the present application realizes direct compiling of the ST language source code without using the C language as an intermediate language, so that the compiling speed is fast, and since the ST language source code is directly mapped into debug_info, the ST language source code can be directly compared with the debug_info in the debugging process, and the compiling and error reporting are performed on the ST language source code in the debugging process, so that good support of the debugging process, such as breakpoint debugging and single step debugging, is realized.
Drawings
FIG. 1 is a flow chart of compiling ST language source code according to the prior art;
FIG. 2 is a basic schematic diagram of the compiling method of the ST language source code of the present application;
FIG. 3 is a flow chart of the method for compiling ST language source code of the present application;
FIG. 4 is a schematic diagram of nodes storing a valuation expression for a tree structure;
FIG. 5 is a schematic diagram of traversing nodes of a tree structure store;
FIG. 6 is a schematic diagram of a compiling device for ST language source codes in the present application;
FIG. 7 is a schematic diagram of a computing device.
Detailed Description
In the following description, reference is made to "some embodiments" which describe a subset of all possible embodiments, but it is to be understood that "some embodiments" can be the same subset or different subsets of all possible embodiments and can be combined with one another without conflict.
In the following description, references to the terms "first/second/third, etc." or module a, module B, module C, etc. are merely used to distinguish similar objects from each other and do not represent a particular ordering of the objects, it being understood that particular orders or precedence may be interchanged as permitted to enable embodiments of the present application described herein to be implemented in an order other than that illustrated or described herein.
In the following description, reference numerals indicating steps such as S331, S332, … …, etc. do not necessarily indicate that the steps are performed in this order, and the order of the steps may be interchanged or performed simultaneously as allowed.
Unless defined otherwise, all technical and scientific terms used herein have the same meaning as commonly understood by one of ordinary skill in the art to which this application belongs. The terminology used herein is for the purpose of describing embodiments of the present application only and is not intended to be limiting of the present application.
Before the detailed description of the embodiments of the present application is further described, terms and expressions which are referred to in the embodiments of the present application and their corresponding uses/actions/functions etc. in the present application are described, and the terms and expressions which are referred to in the embodiments of the present application are applicable to the following explanations.
1. IEC-61131-3, international Electrotechnical Commission (IEC), sets forth standards for programmable systems for PLCs in the field of industrial control, which define the syntax and grammar of structured text ST.
2. ST language source code, a structured Text (structured Text) language, one of the PLC programming languages defined in the IEC61131-3 standard.
3. The GCC, GNU compiler suite (GNU Compiler Collection), is a compiler developed based on the GNU plan.
4. The GCC compiling tool chain mainly comprises four parts: preprocessing, helping a compiler to process something that is not a core, if a comment is deleted, etc.; compiling, namely compiling the file into an assembly file (.s file); assembling, namely generating a machine code file (.o file) from the file; and linking, namely connecting the target file of the program with all required additional target files (such as a static connection library and/or a dynamic connection library), and finally generating a file which can be executed by the PLC (namely, a file of code instructions to be downloaded to the PLC).
5. Front and back ends, front ends (or front end compilation modules), for decomposing a source program into abstract syntax trees as intermediate representations; a back-end (back-end), or back-end compilation module, is used to construct a target program (e.g., an executable file of a PLC) that is expected by a programmer from information in an abstract syntax tree. In this application, the compiler front-end is used for compiling ST language, and is therefore also referred to as ST compiling module, and the compiler back-end is implemented based on GCC, and is therefore referred to as GCC back-end.
6. Breakpoint debugging and single step debugging are one of the functions of a compiler, and a program can be interrupted at a required place, so that the programmer can conveniently analyze the program; when the program is interrupted at each step of the program execution, the single step debugging is performed.
7. debug_info, debug information files generated during compilation.
8. JSON (JavaScript Object Notation, JS object notation) is a lightweight data interchange format, defined by standard ECMA-262, that uses a text format that is completely language independent. The format is easy to read and write, and also easy to machine parse and generate.
9. POU (Programming Organization Unit), program organization unit, is the basic programming unit in the IEC61131-3 standard. POU consists of 3 types of basic units: program (Program), function Block (Function Block), function (Function).
The program is a logic execution main body, and the application logic is realized through the inter-program call.
The functional block is an object-oriented specific basic algorithm unit, and has local variables, the local variables form state characteristics of the functional block, and execution logic of the functional block forms object behavior characteristics of the functional block.
The function is a basic arithmetic unit without internal states, for example, various mathematical operation functions such as sin (x), sqrt (x), etc. are typical of this function type.
10. An abstract syntax tree (abstract tree), language independent intermediate representation, wherein in the process of syntax analysis, the identified language components (i.e. nodes) are all saved using tree variables of the abstract syntax tree, each tree variable constitutes the abstract syntax tree, and the abstract syntax tree can reflect the processing logic of the source code.
11. A structure, a collection of data consisting of a series of data of the same type or different types, is called a structure (struct).
12. Declaration class source code, business source code, for convenience of description, in this application, such codes as global variable declaration, local variable declaration, structure declaration, array declaration in ST language source code are referred to as declaration class source code; the source code related to the service implementation in the ST language source code (see source code in "source_code" in JSON code instance) is referred to as service source code, which corresponds to main program code.
Aiming at the defects of the prior art described in the background art, the application provides a GCC-based ST language source code compiling method, as shown in fig. 2, a basic schematic diagram of the ST language source code compiling method is shown, a ST compiling module is arranged at the front end of a compiler to perform lexical and grammatical analysis on the ST language source code, each node stored in a tree structure is generated, then each node is traversed in sequence to create an abstract grammar tree, and then the GCC rear end compiles according to the abstract grammar tree to generate a machine code instruction. The compiling method of the invention realizes direct compiling of the ST language source code without using the C language as an intermediate language, so that the compiling speed is high, and the ST language source code is directly mapped into debug_info, so that the ST language source code can be directly compared with the ST language source code in the debugging process, and the compiling and error reporting are carried out on the ST language source code in the debugging process, thereby realizing good support for the debugging process. The present application is described in detail below.
[ embodiment of ST language compiling method ]
As shown in fig. 3, which is a flowchart of the ST language source code compiling method according to the invention, the method according to the invention comprises the steps of:
s310: acquiring a service source code in an ST language source code; declaration class source code in the ST language source code is obtained.
The step can be obtained through a JSON format file containing the service source code and the declaration class source code. The service source code and the declaration type source code are respectively written, so that the service source code mainly comprises a part of logic judgment, operation and function call function when the code operates, the information of the part of content is conveniently recorded in the debug_info section, and the declaration type source code is not recorded in the debug_info section, so that programmers can mainly pay attention to the part of the service source code in the breakpoint debugging process.
In addition, the service source code and the declaration class source code may exist as two files, and may be acquired separately.
S320-S330: performing lexical and grammatical analysis on the service source codes in the ST language source codes according to lexical and grammatical rules to obtain abstract grammar trees corresponding to the service source codes; obtaining an abstract syntax tree corresponding to the declaration class source code according to the declaration class source code in the ST language source code;
The step of obtaining the abstract syntax tree corresponding to the service source code may be: performing lexical analysis and grammar analysis on the service source codes to obtain each node stored in a tree structure; and traversing each node in turn to form the abstract syntax tree. By forming the abstract syntax tree, the direct operation of the GCC back end on the abstract syntax tree can be realized, the mode that conversion is needed through an intermediate language (such as a C language) in the background technology is avoided, the integral compiling speed is accelerated, and the support for direct debugging of ST language source codes is also realized due to no conversion of the intermediate language. The lexical and grammatical rules may employ rules defined by IEC 61131-3.
The source code of the class is declared, and all the declared internal variables are embodied in a relation linked list mode, so that the abstract syntax tree of each corresponding variable node can be directly obtained according to the relation linked list.
S340: generating instruction codes which can be identified by the target equipment according to the abstract syntax trees by the GCC back end.
Wherein, during the compiling process in steps S320 and S330, the ST language source code is recorded in the debug information debug_info section, and when it is determined that the following source code is not recorded in the debug_info section: the declaration class source code and the conditional class statement code. As described above, declaring that the class source code is not recorded to the debug_info segment may allow a programmer to focus primarily on debugging of the business source code portion during breakpoint debugging.
[ detailed description of the ST language compiling method ]
Referring to fig. 3, a flowchart of the ST compiling method according to the invention is shown, and a specific embodiment of the ST compiling method according to the invention is described, comprising the steps of:
s310, constructing a data exchange format file, namely a JSON file (the data format of which can be seen in the following [ JSON code example ]), wherein the JSON file records service source codes (in the following example, a text_code field) in ST language source codes, variable declaration class source codes (in the following example, a gbvar_table field is a global variable declaration, and a var_table field is a local variable declaration) in the ST language source codes, and the like.
The ST language source codes are written according to the specification of POU (program organization unit) in IEC61131-3 standard and are divided into three basic units: a FUNCTION (FUNCTION), a FUNCTION BLOCK (function_block) and a PROGRAM (PROGRAM), the type of a basic unit to which a service source code of an ST language source code of each section belongs is specified by a FUNCTION declaration in a JSON file (the section of code is declared as a FUNCTION BLOCK by a user_fb in the following example, and declared as a PROGRAM by a user_program), and a local variable declaration (by a var_table declaration in the following example) and a global variable declaration (by a gbvar_table declaration in the following example) are made in the corresponding basic unit.
In addition, according to the requirements of the written ST language source code, optionally, a structure (struct) type declaration (declaring a structure type and its variable members), an array (array) declaration (declaring an array type and its variable members) and the like can be recorded in the JSON file, and the declaration manner is similar to the variable declaration manner, so that the description is not repeated.
[ example JSON code ]
The above code example shows the basic structure of JSON, in which the service source code (in text_code field) in the ST language source code may specifically include codes such as logic judgment, functions (such as addition operation formulas), function call functions, etc., and the codes are written in program text conforming to IEC61131-3 standard, and the following shows an example of the service source code in a simple ST language source code.
[ service Source code instance ]
newvar1 =newvar 1+1; [ Functions, addition operations ]
fb_block_test (newvar 2: =10); [// function Call ]
if a= =1 { [ judgment condition ], no debug_info ] is written
c:=10;
}
FOR Var TO Num BY 1DO [// conditional statement, no debug_info ] is written
a =a+1; [ Functions, addition operations ]
END_FOR
By writing declaration type source code and service source code into JSON separately, in the following steps, the service source code (text_code field) is mainly concerned when the compiler front end performs lexical and grammatical analysis, and the part of the code is recorded into the debug_info section. For source codes of the declaration types such as global variable declaration, local variable declaration, structure declaration, array declaration and function declaration, a programmer cannot debug a declaration part in the breakpoint debugging process, so that the part cannot be recorded in a debug_info section in the subsequent compiling process, and the programmer mainly focuses on the debugging of the part of the logic judgment, operation and function call function when the service source code runs in the breakpoint debugging process.
S320, receiving the JSON file by the front end of a compiler (namely, an ST compiling module), performing lexical analysis and grammar analysis on service source codes (text_code fields) in ST language source codes in the JSON file according to lexical rules and grammar rules defined by IEC61131-3, and generating each node of tree structure storage. On the other hand, the global variable and local variable declarations, the structure (structure type declaration, its variable member declaration) type declarations, and the array declaration are transmitted to the GCC backend. The method comprises the following steps:
the JSON file is received and parsed by the front end of the compiler (i.e., ST compilation module), including determining service source codes in FUNCTION BLOCKs (function_block) or PROGRAMs (PROGRAM), then performing lexical analysis on these service source codes according to the lexical rules defined by IEC61131-3, respectively, to obtain symbol streams, and performing syntax analysis on the symbol streams according to the syntax rules defined by IEC61131-3, to obtain each node stored in a tree structure.
For example, fig. 4 is a schematic diagram of each node stored in a tree structure corresponding to the statement "newvar1: =newvar 1+1" in the above-mentioned [ service source code instance ], and is shown in the figure: program (i.e., program declaration node corresponding to the code statement) declaration node ast. Letstate (i.e., function declaration node corresponding to the code statement), the line node includes an identification node ast. Identifier (i.e., newvar1 in the code statement) and an expression node ast. Expression (i.e., newvar1+1 in the code statement). It can be seen from the figure that the nodes are stored in a binary tree structure in the generation process. In this example, statements correspond to an addition function, and in other examples, a judgment statement (IF statement), an assignment statement (FOR), a function call statement, and the like are also possible.
On the other hand, in the process of performing lexical analysis and grammar analysis on the service source codes in the text_codes, the front end of the compiler can check whether the lexical and grammar of the service source codes have problems, and records related information through the debug_info, wherein the information recorded through the debug_info section comprises the following steps: the POU (functional block or program) to which each node belongs, the line number (physical address) corresponding to the ST source file and the mapping relation with the address in the memory are filled in the empty line in the place which does not need to be recorded. Where notes do not need to be recorded as in [ service source code instance ], include: the reasons why the condition type sentences, such as judging conditions and circulating conditions, are not recorded are that programmers mainly pay attention to operation of service source codes during debugging, function calling functions and debugging during single-step operation, and do not pay attention to the logic judging conditions.
The following describes a specific implementation manner of the front end of the lower compiler (i.e., ST compilation module), which is specifically as follows:
firstly, downloading GCC source codes, and preparing a compiling tool chain of GCC;
the second step, add the front end of compiler in GCC source code, in order to simplify the description, refer to adding ST and compile the module, include specifically:
Firstly, a gcc/st folder of gcc source codes is created; the folder is used for storing the ST compiling module, namely storing the configuration file created by the building;
then, a configuration file required for the ST compilation module is created and contents required for the GCC are configured, the configuration file including: config-lang.in, lang-specs.h, make-lang.in, st1.C, the specific details of each configuration are as follows:
the ST1.C file is a source code of a compiler front end for implementing ST compilation, and is used for implementing various logic functions, such as a function of opening a source file, reading a file and storing the file, a function of calling lexical and grammatical analysis, a function of calling GCC ports, and the like.
The config-lang. In file is used to indicate the code language type (such as ST, c, go language, in this example ST language source code) used by the compiler front end;
the lang-specs.h file is a header file, indicating the type of the file to which the front end of the compiler is to perform compilation, i.e. indicating the suffix name of the file to which the compiler is to be directed, in this example, st;
the Make-lang.in files are compilation scripts that indicate which files under the directory need to be compiled, and the order in which the files are compiled.
Thirdly, after the files are configured, compiling is carried out through a compiling tool chain of GCC, so that the front end of the compiler (namely, ST compiling module) is enabled to be effective.
S330, creating different types of abstract syntax trees (GenericTree), wherein the method comprises the following substeps:
s331, creating an abstract syntax tree of a global variable node according to a global variable statement in a JSON file transmitted from the front end of a compiler; optionally, when judging that the structure variable type declaration exists in the JSON file, creating an abstract syntax tree of the structure variable node, and when judging that the array variable type declaration exists in the JSON file, creating the abstract syntax tree of the array variable node. The internal variables recorded in the declaration class source codes are embodied in a relation linked list mode, and accordingly abstract syntax trees of corresponding variable nodes can be directly obtained according to the relation linked list.
When the above three types are declared in the JSON file, they are not written in the service source code (text_code field) in the ST language source code, but are independently written, because the above variable declaration is not required to be debugged when breakpoint debugging is performed, and the related information of the declaration class source code is not added into the debug_info segment. It can be seen that the information recorded in debug_info is recorded according to the debugging requirements, and not all the contents in JSON are recorded.
S332, traversing each node stored in the tree structure obtained in the step S320, and creating an abstract syntax tree (GenericTree) corresponding to the service source code. The traversal process is described in further detail below:
As shown in fig. 5, states 0 in fig. 5 corresponds to fig. 4, after sequentially traversing states 0 in a depth traversing priority manner (see the sequence numbers in the figure, which indicate the traversing order, which are depth traversing priority), traversing states 1 and 2 again in the same traversing manner until each node is traversed to generate GenericTree files.
S340, transmitting each created abstract syntax tree to the GCC back end, and executing back-end compiling by the GCC back end according to the abstract syntax tree to generate an instruction code which can be identified by the PLC.
In the compiling process, compiling process information is recorded by a debug_info information segment, wherein the compiling process information comprises POU to which a service source code belongs, source code line numbers (namely physical addresses) of all lines in the service source code, and starting addresses of all instructions compiled by corresponding lines of source codes in a memory. On the other hand, in order for the function of debug_info to be normally implemented, the following attention is also required: if the compiling process needs to create a main function, an empty main function node can be created, because the ST language source code has no main function, and the compiling requirement can be met by adopting the empty main function node; all variables in the function come from a variable list (global variable or local variable) of the function parameters, and the variable is not newly added in the compiling process, so that a stack space is not required to be opened up for the variables.
An example of looking at the debug_info information segment, which displays the following information, by the command $obj dump-dwarf=decodedline a.out is shown below:
from the above, the File name (File name), i.e., the name of the POU (POU _name) in JSON) to which the service source code in the ST language source code belongs, the source code Line number (Line number), and the start address (Starting address) in the memory corresponding to the compiled instruction have been displayed in the debug_info. Specifically, the content displayed in the above debug_info section includes: the File name is the Line number corresponding to the first Line and the second Line of source codes of aaa. St, and the starting address in the memory where the instruction compiled by the two lines of source codes is located; the File name is the Line number corresponding to the first Line source code of the block. St and the starting address in the memory where the instruction compiled by the Line source code is located.
Therefore, the method can normally call the linemap_position_for_column () function of the GCC to establish the mapping between the physical address and the memory address of the service source code, realize the direct debugging of the service source code in the ST language source code, and support breakpoint debugging, single step debugging and the like of the service source code.
By the front end of the GCC-based ST language source code compiler, the invention can directly compile ST language source codes. And debug_info section information is added, ST language source code information is mapped in the debug_info section in the executable file and corresponds to the memory address, and functions of direct debugging, such as breakpoint debugging and single-step debugging, are realized. In addition, the invention adds declaration source codes such as variable declarations and the like into the debug_info, so that the debug_info can accurately record information required by the debugging process, and a debugging window is displayed to a programmer in the debugging process, such as ST logic operation, so that the programmer pays attention to the debugging on ST logic operation.
[ embodiment of ST language compiler ]
Corresponding to the ST language compiling method of the present application, the present application further provides an ST language compiling device, and the implementation manner and the beneficial effect of the functions or the uses of each module included in the device can be referred to the embodiment of the ST language compiling method, so that the description is omitted when introducing the ST language compiling device. As shown in fig. 6, the ST language compiling apparatus of the invention comprises:
an acquiring module 610, configured to acquire a service source code in an ST language source code; the method is also used for acquiring declaration class source codes in ST language source codes; may be obtained by a JSON format file containing the service source code and declaration class source code.
The front-end compiling module 620 is configured to perform lexical and grammatical analysis on the service source code according to the lexical and grammatical rules defined by IEC61131-3 to obtain an abstract grammar tree corresponding to the service source code; and obtaining an abstract syntax tree corresponding to the declaration class source code according to the declaration class source code. In this application, the front-end compiling module 620 is used for compiling ST language, which is also called ST compiling module.
The back-end compiling module 630 is configured to generate, according to the abstract syntax tree, an instruction code that is recognizable by the target device, where the back-end compiling module 630 is implemented based on GCC, and is also referred to as GCC back-end.
Wherein the front-end compilation module 620 includes;
a node generating module 621, configured to perform lexical analysis and syntax analysis on the service source codes in the ST language source codes, so as to obtain each node stored in a tree structure;
an abstract syntax tree generating module 622, configured to traverse each node in turn to form the abstract syntax tree.
Wherein, the obtaining module 610 is further configured to obtain declaration class source codes in ST language source codes; the obtained abstract syntax tree obtained by the front-end compilation module 620 is also obtained from the declarative class source code.
The system further comprises a debug information recording module 640, which is used for recording the source code of the ST language to a debug information debug_info segment; and when judging that the following source codes are not recorded in the debug_info section: the declaration class source code and the conditional class statement source code.
[ embodiment of a computing device ]
Fig. 7 is a schematic diagram of a computing device 5000 as provided by embodiments of the application. The computing device 5000 includes: processor 5010, memory 5020, communication interface 5030, bus 5040.
It should be appreciated that the communication interface 5030 in the computing device 5000 shown in this figure may be used to communicate with other devices.
The processor 5010 may be connected to a memory 5020, among other things. The memory 5020 can be used to store the program codes and data. Accordingly, the memory 5020 may be a storage unit inside the processor 5010, an external storage unit independent of the processor 5010, or a component including a storage unit inside the processor 5010 and an external storage unit independent of the processor 5010.
Optionally, the computing device 5000 may also include a bus 5040. The memory 5020 and the communication interface 5030 may be connected to the processor 5010 via a bus 5040. Bus 5040 may be a peripheral component interconnect standard (Peripheral Component Interconnect, PCI) bus or an extended industry standard architecture (Extended Industry Standard Architecture, EISA) bus, among others. The bus 5040 may be classified as an address bus, a data bus, a control bus, or the like. For ease of illustration, only one line is shown in the figure, but not only one bus or one type of bus.
It should be appreciated that in the present embodiment, the processor 5010 may employ a central processing unit (central processing unit, CPU). The processor may also be other general purpose processors, digital signal processors (digital signal processor, DSP), application specific integrated circuits (application specific integrated circuit, ASIC), off-the-shelf programmable gate arrays (field programmable gate Array, FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, or the like. A general purpose processor may be a microprocessor or the processor may be any conventional processor or the like. Or the processor 5010 may employ one or more integrated circuits for executing associated programs to carry out the techniques provided in the embodiments of the present application.
The memory 5020 may include read only memory and random access memory and provide instructions and data to the processor 5010. A portion of the processor 5010 may also include nonvolatile random access memory. The processor 5010 may also store information of the device type, for example.
When the computing device 5000 is running, the processor 5010 executes computer-executable instructions in the memory 5020 to perform the operational steps of the methods described above.
It should be understood that the computing device 5000 according to the embodiments of the present application may correspond to respective subjects performing the methods according to the embodiments of the present application, and that the above and other operations and/or functions of the respective modules in the computing device 5000 are respectively for implementing respective flows of the methods of the embodiments, and are not described herein for brevity.
Those of ordinary skill in the art will appreciate that the various illustrative elements and algorithm steps described in connection with the embodiments disclosed herein may be implemented as electronic hardware, or combinations of computer software and electronic hardware. Whether such functionality is implemented as hardware or software depends upon the particular application and design constraints imposed on the solution. Skilled artisans may implement the described functionality in varying ways for each particular application, but such implementation decisions should not be interpreted as causing a departure from the scope of the present application.
It will be clear to those skilled in the art that, for convenience and brevity of description, specific working procedures of the above-described systems, apparatuses and units may refer to corresponding procedures in the foregoing method embodiments, and are not repeated herein.
In the several embodiments provided in this application, it should be understood that the disclosed systems, devices, and methods may be implemented in other manners. For example, the apparatus embodiments described above are merely illustrative, e.g., the division of the units is merely a logical function division, and there may be additional divisions when actually implemented, e.g., multiple units or components may be combined or integrated into another system, or some features may be omitted or not performed. Alternatively, the coupling or direct coupling or communication connection shown or discussed with each other may be an indirect coupling or communication connection via some interfaces, devices or units, which may be in electrical, mechanical or other form.
The units described as separate units may or may not be physically separate, and units shown as units may or may not be physical units, may be located in one place, or may be distributed on a plurality of network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
In addition, each functional unit in each embodiment of the present application may be integrated in one processing unit, or each unit may exist alone physically, or two or more units may be integrated in one unit.
The functions, if implemented in the form of software functional units and sold or used as a stand-alone product, may be stored in a computer-readable storage medium. Based on such understanding, the technical solution of the present application may be embodied essentially or in a part contributing to the prior art or in a part of the technical solution, in the form of a software product stored in a storage medium, including several instructions for causing a computer device (which may be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the methods described in the embodiments of the present application. And the aforementioned storage medium includes: a U-disk, a removable hard disk, a Read-Only Memory (ROM), a random access Memory (Random Access Memory, RAM), a magnetic disk, or an optical disk, or other various media capable of storing program codes.
The embodiments of the present application also provide a computer-readable storage medium having stored thereon a computer program for executing a diversified problem generating method when executed by a processor, the method comprising at least one of the aspects described in the respective embodiments above.
Any combination of one or more computer readable media may be employed as the computer storage media of the embodiments herein. The computer readable medium may be a computer readable signal medium or a computer readable storage medium. The computer readable storage medium can be, for example, but not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or a combination of any of the foregoing. More specific examples (a non-exhaustive list) of the computer-readable storage medium would include the following: an electrical connection having one or more wires, a portable computer diskette, a hard disk, a Random Access Memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disc read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing. In this document, a computer readable storage medium may be any tangible medium that can contain, or store a program for use by or in connection with an instruction execution system, apparatus, or device.
The computer readable signal medium may include a propagated data signal with computer readable program code embodied therein, either in baseband or as part of a carrier wave. Such a propagated data signal may take any of a variety of forms, including, but not limited to, electro-magnetic, optical, or any suitable combination of the foregoing. A computer readable signal medium may also be any computer readable medium that is not a computer readable storage medium and that can communicate, propagate, or transport a program for use by or in connection with an instruction execution system, apparatus, or device.
Program code embodied on a computer readable medium may be transmitted using any appropriate medium, including but not limited to wireless, wireline, optical fiber cable, RF, etc., or any suitable combination of the foregoing.
Computer program code for carrying out operations of the present application may be written in one or more programming languages, including an object oriented programming language such as Java, smalltalk, C ++ and conventional procedural programming languages, such as the "C" programming language or similar programming languages. The program code may execute entirely on the user's computer, partly on the user's computer, as a stand-alone software package, partly on the user's computer and partly on a remote computer or entirely on the remote computer or server. In the case of a remote computer, the remote computer may be connected to the user's computer through any kind of network, including a Local Area Network (LAN) or a Wide Area Network (WAN), or may be connected to an external computer (for example, through the Internet using an Internet service provider).
Note that the above is only a preferred embodiment of the present application and the technical principle applied. Those skilled in the art will appreciate that the present application is not limited to the particular embodiments described herein, but is capable of numerous obvious changes, rearrangements and substitutions as will now become apparent to those skilled in the art without departing from the scope of the present application. Thus, while the present application has been described in terms of the foregoing embodiments, the present application is not limited to the foregoing embodiments, but may include many other equivalent embodiments without departing from the spirit of the present application, all of which fall within the scope of the present application.

Claims (4)

1. A method for compiling ST language source code, comprising:
the service source code and the declaration class source code in the ST language source code exist as two files;
acquiring a service source code in an ST language source code; performing lexical and grammatical analysis on the service source code according to lexical and grammatical rules to obtain an abstract grammar tree; the step of obtaining an abstract syntax tree comprises; performing lexical analysis and grammar analysis on the service source codes to obtain each node stored in a tree structure; traversing each node in turn to form the abstract syntax tree;
Acquiring declaration class source codes in ST language source codes; obtaining abstract syntax trees of corresponding variable nodes according to the relation linked list of all internal variables declared by the declaration class source codes;
executing back-end compiling by the GCC back-end according to the abstract syntax tree to generate an instruction code which can be identified by target equipment;
the ST language source code is recorded to a debug information debug_info segment; and when judging that the following source codes are not recorded in the debug_info section: the declaration class source code and the conditional class statement source code;
in the compiling process, the debug_info information segment records compiling process information, including POU to which the service source code belongs, source code line numbers of each line in the service source code, and initial addresses of each instruction compiled by the corresponding line source code in the memory, and when a main function needs to be created in the compiling process, an empty main function node is created;
the service source code and the declaration class source code in the ST language source code are acquired through a JSON format file containing the service source code and the declaration class source code in the ST language source code;
the service source code comprises logic judgment, operation and function call function parts when the code runs; the declarative class source code includes source code for at least one of: global variable declaration, local variable declaration, structure declaration, and array declaration.
2. An ST language source code compiling apparatus, comprising:
the acquisition module is used for acquiring service source codes in the ST language source codes; the method is also used for acquiring declaration class source codes in ST language source codes; the service source code and the declaration class source code in the ST language source code are respectively written or exist as two files; the service source code and declaration class source code in the ST language source code are acquired through a JSON format file containing the service source code and declaration class source code in the ST language source code
The front-end compiling module is used for carrying out lexical and grammatical analysis on the service source codes according to lexical and grammatical rules to obtain abstract grammar trees corresponding to the service source codes; the abstract syntax tree corresponding to the declaration class source code is obtained according to the declaration class source code, and the abstract syntax tree corresponding to each variable node is obtained according to the relation linked list of each internal variable declared by the declaration class source code;
the back-end compiling module is used for executing back-end compiling by the GCC back end according to the abstract syntax tree to generate an instruction code which can be identified by target equipment;
the debug information recording module is used for recording the source codes of the ST language to the debug information debug_info section; and when judging that the following source codes are not recorded in the debug_info section: the declaration class source code and the conditional class statement source code; recording compiling process information to the debug_info information section, wherein the compiling process information comprises POU to which a service source code belongs, source code line numbers of all lines in the service source code, and starting addresses of all instructions compiled by corresponding lines of source codes in a memory, and creating an empty main function node when a main function needs to be created in the compiling process;
The front-end compiling module comprises; the node generation module is used for performing lexical analysis and grammar analysis on the service source codes to obtain each node stored in a tree structure; the abstract syntax tree generation module is used for traversing each node in sequence to form the abstract syntax tree;
the service source code comprises logic judgment, operation and function call function parts when the code runs; the declarative class source code includes source code for at least one of: global variable declaration, local variable declaration, structure declaration, and array declaration.
3. A computing device, comprising:
a bus;
a communication interface connected to the bus;
at least one processor coupled to the bus; and
at least one memory coupled to the bus and storing program instructions that, when executed by the at least one processor, cause the at least one processor to perform the method of claim 1.
4. A computer readable storage medium having stored thereon program instructions, which when executed by a computer cause the computer to perform the method of claim 1.
CN202010328050.8A 2020-04-23 2020-04-23 ST language source code compiling method, device, computer equipment and medium Active CN111488154B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202010328050.8A CN111488154B (en) 2020-04-23 2020-04-23 ST language source code compiling method, device, computer equipment and medium

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202010328050.8A CN111488154B (en) 2020-04-23 2020-04-23 ST language source code compiling method, device, computer equipment and medium

Publications (2)

Publication Number Publication Date
CN111488154A CN111488154A (en) 2020-08-04
CN111488154B true CN111488154B (en) 2024-01-12

Family

ID=71792142

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202010328050.8A Active CN111488154B (en) 2020-04-23 2020-04-23 ST language source code compiling method, device, computer equipment and medium

Country Status (1)

Country Link
CN (1) CN111488154B (en)

Families Citing this family (21)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN112114817B (en) * 2020-09-28 2023-09-19 中国银行股份有限公司 COBOL language-based data dictionary field information acquisition method and device
CN112230934B (en) * 2020-10-27 2023-08-29 北京人大金仓信息技术股份有限公司 Method and device for converting code language
CN112486499A (en) * 2020-12-09 2021-03-12 广东三维家信息科技有限公司 Method, device, equipment and storage medium for searching target node in TS code
CN112835323B (en) * 2020-12-31 2022-05-17 深圳市雷赛控制技术有限公司 Programmable logic control language compiling method and device
CN112799677B (en) * 2021-02-05 2023-09-12 北京字节跳动网络技术有限公司 Method, device, equipment and storage medium for hook of compiling period
CN113467828B (en) * 2021-06-23 2024-01-12 中国海洋大学 Method and system for converting programming language in heterogeneous many-core processor
CN113467778B (en) * 2021-06-23 2024-01-09 中国海洋大学 Algorithm conversion method and system in heterogeneous many-core processor
CN113778440B (en) * 2021-08-18 2024-01-26 上海瑞家信息技术有限公司 Data processing method and device, electronic equipment and storage medium
CN117389570A (en) * 2021-10-14 2024-01-12 华为技术有限公司 Language interoperation method, device, storage medium and program product
CN114047922B (en) * 2021-11-16 2024-07-02 北京字节跳动网络技术有限公司 Transcoding method, device, medium and equipment for precompiled device
CN114527986B (en) * 2021-12-31 2023-12-26 北京邮电大学 C++ language-oriented source code anonymization method and related equipment
CN114912110B (en) * 2022-03-21 2024-08-06 中国科学院信息工程研究所 Node. Js code safety detection method and system
CN114780100B (en) * 2022-04-08 2023-04-07 芯华章科技股份有限公司 Compiling method, electronic device and storage medium
CN114721667B (en) * 2022-04-21 2022-12-09 北京万昇智能科技有限公司 Method and device for compiling ST code into WebAssembly code
CN114706590B (en) * 2022-05-05 2023-06-13 北京万昇智能科技有限公司 PLC application package generation method, PLC application execution method, and related devices and systems
CN114995796B (en) * 2022-05-30 2024-07-19 宁波三星医疗电气股份有限公司 A-XDR (advanced digital Signal processor) coding message processing method, storage medium and electronic equipment
CN115469875B (en) * 2022-08-22 2023-08-15 西安衍舆航天科技有限公司 Compiling method and device of domain-specific language DSL based on remote control operation
CN115658079B (en) * 2022-10-24 2023-10-13 上海繁易信息科技股份有限公司 Method for converting functional block diagram into structured text and related equipment
CN116360788A (en) * 2023-02-17 2023-06-30 深圳市亿维自动化技术有限公司 Compiling method, compiler and electronic device for structured text programming language
CN116991428B (en) * 2023-09-28 2023-12-15 飞腾信息技术有限公司 Compiling method, compiling device, compiler, computing device and storage medium
CN118069155B (en) * 2024-04-23 2024-07-12 光大环保技术研究院(深圳)有限公司 Compiling method, system, compiler and medium for graphical programmable language

Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
JP2009064207A (en) * 2007-09-06 2009-03-26 Fuji Electric Systems Co Ltd Compiler
CN104267999A (en) * 2014-09-26 2015-01-07 浙江中控技术股份有限公司 Method and device for compiling control program
CN109254776A (en) * 2018-09-29 2019-01-22 深圳市雷赛控制技术有限公司 Multilingual code compiling method and compiler
CN109976760A (en) * 2017-12-27 2019-07-05 北京东土科技股份有限公司 A kind of the cross compile method and cross-compiler of graphic language
CN110825384A (en) * 2019-10-28 2020-02-21 国电南瑞科技股份有限公司 ST language compiling method, system and compiler based on LLVM

Patent Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
JP2009064207A (en) * 2007-09-06 2009-03-26 Fuji Electric Systems Co Ltd Compiler
CN104267999A (en) * 2014-09-26 2015-01-07 浙江中控技术股份有限公司 Method and device for compiling control program
CN109976760A (en) * 2017-12-27 2019-07-05 北京东土科技股份有限公司 A kind of the cross compile method and cross-compiler of graphic language
CN109254776A (en) * 2018-09-29 2019-01-22 深圳市雷赛控制技术有限公司 Multilingual code compiling method and compiler
CN110825384A (en) * 2019-10-28 2020-02-21 国电南瑞科技股份有限公司 ST language compiling method, system and compiler based on LLVM

Also Published As

Publication number Publication date
CN111488154A (en) 2020-08-04

Similar Documents

Publication Publication Date Title
CN111488154B (en) ST language source code compiling method, device, computer equipment and medium
CN109143952B (en) Programmable logic controller programming language conversion system
US20070044066A1 (en) Embedded multi-language programming
US20110314337A1 (en) Method and Apparatus for Locating Input-Model Faults Using Dynamic Tainting
Basten et al. M3: A general model for code analytics in rascal
Flanagan Effective Static Debugging
CN114780100A (en) Compiling method, electronic device, and storage medium
CN116257245A (en) Multi-output compiling method and system based on flex and bison grammar analysis
Fritzson et al. Metamodelica–a symbolic-numeric modelica language and comparison to julia
Basten Ambiguity detection for programming language grammars
Bülow Proof visualization for the lean 4 theorem prover
Asensio et al. Industrial automation programming environment with a new translation algorithm among IEC 61131–3 languages based on the TC6-XML scheme
Noguera et al. Program querying with a SOUL: the barista tool suite
MaIm et al. Static flow analysis of the Action Language for Foundational UML
Grigorev et al. String-embedded language support in integrated development environment
Renggli Dynamic Language Embedding
Németh et al. HaskellCompass: Extending the CodeCompass comprehension framework for Haskell
Owens A generic framework facilitating automated quality assurance across programming languages of disparate paradigms.
Marticorena et al. Refactoring generics in JAVA: a case study on Extract Method
Buchgeher et al. Software Analytics and Evolution Team Report 2017
Rinko Efficient IEC 61131-3 Structured Text tooling in modern distributed control system
Mühlbauer et al. Automatic Transformation of Iterative to Tail-Recursive Functions in Python
Budimac et al. Consistent Static Analysis in Multilingual Software Products Development
Rakić et al. eCST to source code generation-An idea and perspectives
de Carvalho A meta-language and framework for aspect-oriented programming

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
EE01 Entry into force of recordation of patent licensing contract

Application publication date: 20200804

Assignee: Beijing Shuili Intelligent Building Technology Co.,Ltd.

Assignor: KYLAND TECHNOLOGY Co.,Ltd.

Contract record no.: X2022990000047

Denomination of invention: St language source code compilation method, device, computer equipment and medium

License type: Common License

Record date: 20220121

EE01 Entry into force of recordation of patent licensing contract
GR01 Patent grant
GR01 Patent grant