CN114661300B - Method and device for compiling PLC program code into WebAssembly code - Google Patents

Method and device for compiling PLC program code into WebAssembly code Download PDF

Info

Publication number
CN114661300B
CN114661300B CN202210374707.3A CN202210374707A CN114661300B CN 114661300 B CN114661300 B CN 114661300B CN 202210374707 A CN202210374707 A CN 202210374707A CN 114661300 B CN114661300 B CN 114661300B
Authority
CN
China
Prior art keywords
memory
webassembly
function
variable
address
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
CN202210374707.3A
Other languages
Chinese (zh)
Other versions
CN114661300A (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.)
Beijing Wansheng Intelligent Technology Co ltd
Original Assignee
Beijing Wansheng Intelligent 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 Beijing Wansheng Intelligent Technology Co ltd filed Critical Beijing Wansheng Intelligent Technology Co ltd
Priority to CN202210374707.3A priority Critical patent/CN114661300B/en
Publication of CN114661300A publication Critical patent/CN114661300A/en
Application granted granted Critical
Publication of CN114661300B publication Critical patent/CN114661300B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation

Landscapes

  • Engineering & Computer Science (AREA)
  • General Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Software Systems (AREA)
  • Physics & Mathematics (AREA)
  • General Physics & Mathematics (AREA)
  • Devices For Executing Special Programs (AREA)
  • Stored Programmes (AREA)

Abstract

The application discloses a method and a device for compiling PLC program codes into WebAssembly codes, which comprise the following steps: acquiring a unit type of a code unit to be compiled in a PLC program code to be compiled; allocating the memory space of the memory type represented by the memory layout mode to the variable of the code unit according to the preset memory layout mode corresponding to the unit type; and generating a WebAssembly instruction code corresponding to the code unit according to the memory type of the allocated memory space, wherein the WebAssembly instruction code can use the allocated memory type of the memory space to access the variable of the code unit in the executed process. By adopting the scheme, the effective memory space is distributed for the variable in the PLC program code in the process of compiling the PLC program code into the WebAssembly code, so that the variable can be effectively accessed in the subsequent process of executing the WebAssembly code obtained by compiling.

Description

Method and device for compiling PLC program code into WebAssembly code
Technical Field
The application relates to the technical field of industrial control and the technical field of computers, in particular to a method and a device for compiling PLC program codes into WebAssembly codes.
Background
The PLC (Programmable Logic Controller) has wide application in the field of industrial control, and the IEC61131-3 specification defines 5 standard PLC programming languages, including three graphical languages of Sequential Function Chart (SFC), ladder Diagram (LD) and function module diagram (FBD), and two text languages of statement list (IL) and Structure Text (ST).
There are some methods for compiling IEC61131-3 PLC language. In some methods, the PLC language is directly compiled into machine instructions of the controller platform, which are strictly bound to the hardware architecture of the controller and are not easily distributed to controllers with different hardware architectures. In other approaches, the ST language is compiled into a custom bytecode format, and then an interpreter of these bytecodes is implemented on the controller, thereby eliminating the dependency on the hardware architecture. However, the custom bytecode has a close coupling relationship with the implementation of a compiler, and some existing programming languages cannot be compiled into the custom bytecode, so that it is difficult to reuse the ecology of other languages, thereby limiting the development efficiency of engineers.
Disclosure of Invention
Embodiments of the present application provide a method and an apparatus for compiling a PLC program code into a WebAssembly code, so as to solve a problem in the prior art that how to allocate a memory space for a variable in a PLC program code in a process of compiling the PLC program code into the WebAssembly code.
The embodiment of the application provides a method for compiling a PLC program code into a WebAssembly code, which comprises the following steps:
acquiring a unit type of a code unit to be compiled in a PLC program code to be compiled;
allocating the memory space of the memory type represented by the memory layout mode to the variable of the code unit according to the preset memory layout mode corresponding to the unit type;
and generating a WebAssembly instruction code corresponding to the code unit according to the memory type of the allocated memory space, wherein the WebAssembly instruction code can use the allocated memory space of the memory type to access the variable of the code unit in the executed process.
Further, the code units include at least one of the following types of code units:
programs, function blocks, functions, global variables, and direct address variables;
the memory types include: the WebAssembly linear memory is divided into three memory spaces of a data area, an auxiliary stack and a heap, and the memory types further comprise a WebAssembly global memory, a WebAssembly local memory, a WebAssembly host memory and an independent linear memory except the WebAssembly linear memory.
Further, the allocating, according to a preset memory layout mode corresponding to the unit type, a memory space of the memory type represented by the memory layout mode for the variable of the code unit includes:
when the code unit is a program, acquiring the type and the number of member variables of the program;
and determining the size and the first address of a memory space which needs to be allocated to the member variables of the program in a data area of the WebAssembly linear memory based on the type and the number of the member variables of the program.
Further, the generating a WebAssembly instruction code corresponding to the code unit according to the memory type of the allocated memory space includes:
storing the first address of the memory space allocated for the program into a global memory of the WebAssembly; generating a first address acquisition instruction code for acquiring the first address from the global memory; generating a WebAssembly function corresponding to the content logic of the program, wherein the parameter of the WebAssembly function is a numerical value obtained by executing the first address acquisition instruction code; or
Generating a first address acquisition function aiming at a first address of a memory space allocated for the program, wherein a return value of the first address acquisition function after being executed is the first address; generating a WebAssembly function corresponding to the content logic of the program, wherein the parameter of the WebAssembly function is a return value obtained by executing the first address acquisition function; or
Storing the first address of the memory space allocated for the program into a global pointer array which is pre-established in the WebAssembly linear memory and is created aiming at all programs of a PLC program code to be compiled; generating a second address fetch instruction code for fetching the first address from the global pointer array; generating a WebAssembly function corresponding to the content logic of the program, wherein the parameter of the WebAssembly function is a numerical value obtained by executing the second address to acquire an instruction code; or
Generating a uniform call entry function and a WebAssembly function corresponding to the content logic of the program aiming at all programs of a PLC program code to be compiled, and enabling the uniform call entry function to call the WebAssembly function by taking the first address of a memory space allocated by the program as the parameter of the WebAssembly function when the entry parameter of the uniform call entry function is the name or the ID of the program.
Further, the allocating, according to a preset memory layout mode corresponding to the unit type, a memory space of a memory type represented by the memory layout mode for the variable of the code unit includes:
when the code unit is a program, determining to dynamically allocate a memory space for member variables of the program from a stack of a WebAssembly linear memory;
the generating of the WebAssembly instruction code corresponding to the code unit according to the memory type of the allocated memory space includes:
generating a second address acquisition function for the program, wherein the second address acquisition function is used for calling a memory allocation function to allocate a memory space on a stack of the WebAssembly linear memory for the member variable of the program; and generating a WebAssembly function corresponding to the content logic of the program, wherein the parameter of the WebAssembly function is a return value obtained by executing the memory allocation function, and the return value is the first address of a memory space allocated on a stack of the WebAssembly linear memory for the member variable of the program.
Further, the allocating, according to a preset memory layout mode corresponding to the unit type, a memory space of the memory type represented by the memory layout mode for the variable of the code unit includes:
when the code unit is a functional block, determining the memory space allocated to the program to which the functional block belongs, including the memory space allocated to the functional block;
calculating the size of memory space occupied by all member variables before the function block in the program as an offset based on the position of the function block serving as the member variable in the program to which the function block belongs;
generating the WebAssembly instruction code corresponding to the code unit according to the memory type of the allocated memory space, wherein the WebAssembly instruction code comprises the following steps:
generating a third address acquisition instruction code for the functional block, wherein the third address acquisition instruction code is used for adding the offset to the first address of the memory space allocated to the program to obtain the first address of the memory space allocated to the functional block; and generating a WebAssembly function corresponding to the content logic of the functional block, wherein the parameter of the WebAssembly function is a numerical value obtained by executing the third address acquisition instruction code.
Further, the allocating, according to a preset memory layout mode corresponding to the unit type, a memory space of the memory type represented by the memory layout mode for the variable of the code unit includes:
when the code unit is a functional block, allocating a memory space in a host memory of the WebAssembly for the functional block, wherein the memory space allocated for a program to which the functional block belongs comprises a memory space for storing a functional block handle of the host memory, and the host memory stores a mapping relation between the functional block handle and a first address of the memory space allocated for the functional block;
calculating the size of memory space occupied by all member variables before the function block in the program as an offset based on the position of the function block serving as the member variable in the program to which the function block belongs;
generating the WebAssembly instruction code corresponding to the code unit according to the memory type of the allocated memory space, wherein the WebAssembly instruction code comprises the following steps:
generating a fourth address acquisition instruction code for the functional block, where the fourth address acquisition instruction code is used to obtain a first address of a memory space allocated for the functional block handle by adding the offset to the first address of the memory space allocated for the program; and generating a numerical value acquiring instruction code for acquiring the function block handle from the initial address; and generating a first host function call instruction which takes the function block handle obtained by executing the numerical value acquisition instruction code as a parameter, wherein the first host function call instruction is used for calling a host function, and the host function is used for searching a head address of a memory space allocated for the function block in a host memory based on the function block handle and realizing the content logic of the function block based on the head address.
Further, the allocating, according to a preset memory layout mode corresponding to the unit type, a memory space of the memory type represented by the memory layout mode for the variable of the code unit includes:
when the code unit is a function, allocating a memory space on an auxiliary stack of the WebAssembly linear memory for a local variable of the function;
generating the WebAssembly instruction code corresponding to the code unit according to the memory type of the allocated memory space, wherein the WebAssembly instruction code comprises the following steps:
generating a fifth address acquisition instruction code, configured to acquire a first address of a memory space allocated to a local variable of the function, where the first address is a stack top pointer plus an offset of the local variable of the function within the function;
and generating a WebAssembly function corresponding to the content logic of the function, wherein the parameter of the WebAssembly function is the parameter of the function, and the WebAssembly function is used for accessing the local variable of the function by using the head address of the allocated memory space.
Further, the allocating, according to a preset memory layout mode corresponding to the unit type, a memory space of a memory type represented by the memory layout mode for the variable of the code unit includes:
when the code unit is a function, allocating a memory space in a local memory of the WebAssembly for a local variable of the function;
generating the WebAssembly instruction code corresponding to the code unit according to the memory type of the allocated memory space, wherein the WebAssembly instruction code comprises the following steps:
acquiring the number of a local memory allocated to the local variable of the function;
and generating a WebAssembly function corresponding to the content logic of the function, wherein the parameter of the WebAssembly function is the parameter of the function, and the WebAssembly function is used for accessing the local variable of the function by using the number of the distributed local memory.
Further, the allocating, according to a preset memory layout mode corresponding to the unit type, a memory space of the memory type represented by the memory layout mode for the variable of the code unit includes:
when the code unit is a global variable, allocating a memory space in a data area of the WebAssembly linear memory for the global variable;
generating the WebAssembly instruction code corresponding to the code unit according to the memory type of the allocated memory space, wherein the WebAssembly instruction code comprises the following steps:
and generating a first access instruction code which uses the address of the global variable in the data area of the WebAssembly linear memory as an operand.
Further, the allocating, according to a preset memory layout mode corresponding to the unit type, a memory space of a memory type represented by the memory layout mode for the variable of the code unit includes:
when the code unit is a global variable, allocating a memory space in a global memory of WebAssembly for the global variable;
the generating of the WebAssembly instruction code corresponding to the code unit according to the memory type of the allocated memory space includes:
and generating a second access instruction code using the number of the global memory allocated to the global variable as an operand.
Further, the allocating, according to a preset memory layout mode corresponding to the unit type, a memory space of the memory type represented by the memory layout mode for the variable of the code unit includes:
when the code unit is a global variable, allocating a memory space for the global variable in a host memory of WebAssembly, wherein a mapping relation between an ID of the global variable and a first address of the memory space allocated for the global variable is stored in the host memory;
generating the WebAssembly instruction code corresponding to the code unit according to the memory type of the allocated memory space, wherein the WebAssembly instruction code comprises the following steps:
and generating a second host function call instruction with the ID of the global variable as a parameter, wherein the second host function call instruction is used for calling a host function, and the host function is used for searching the first address of the memory space allocated for the global variable based on the ID of the global variable and the mapping relation and accessing the global variable based on the first address.
Further, the allocating, according to a preset memory layout mode corresponding to the unit type, a memory space of a memory type represented by the memory layout mode for the variable of the code unit includes:
when the code unit is a direct address variable, allocating a memory space in a data area of the WebAssembly linear memory for the direct address variable; or
When the code unit is a direct address variable, determining to dynamically allocate a memory space for the direct address variable on a stack of the WebAssembly linear memory; or alternatively
When the code unit is a direct address variable, allocating a memory space in a host memory of WebAssembly for the direct address variable; or
When the code unit is a direct address variable, allocating a memory space in an additional independent linear memory for the direct address variable;
generating the WebAssembly instruction code corresponding to the code unit according to the memory type of the allocated memory space, wherein the WebAssembly instruction code comprises the following steps:
generating a third access instruction code for directly accessing the direct address variable by using the address of the memory space allocated for the direct address variable when the memory space is allocated for the direct address variable in the data area of the WebAssembly linear memory; or
When determining that a memory space is dynamically allocated for the direct address variable on a stack of the WebAssembly linear memory, generating a third address acquisition function for the direct address variable, wherein the third address acquisition function is used for calling a memory allocation function to allocate the memory space for the direct address variable on the stack of the WebAssembly linear memory; or
When a memory space is allocated for the direct address variable in a host memory of the WebAssembly, generating a third host function call instruction for the direct address variable, wherein the third host function call instruction is used for calling a host function, and the host function is used for accessing the direct address variable based on the address of the memory space allocated for the direct address variable; or
When a memory space is allocated in an additional independent linear memory for the direct address variable, a fourth access instruction code is generated for directly accessing the direct address variable using an address of the memory space allocated for the direct address variable.
The embodiment of the present application further provides a device for compiling a PLC program code into a WebAssembly code, including:
the unit type acquisition module is used for acquiring the unit type of a code unit to be compiled in a PLC program code to be compiled;
the space allocation module is used for allocating the memory space of the memory type represented by the memory layout mode for the variable of the code unit according to the preset memory layout mode corresponding to the unit type;
and the code generation module is used for generating the WebAssembly instruction code corresponding to the code unit according to the memory type of the allocated memory space, and the WebAssembly instruction code can use the allocated memory space of the memory type to access the variable of the code unit in the executed process.
Embodiments of the present application further provide an electronic device, including a processor and a machine-readable storage medium storing machine-executable instructions executable by the processor, the processor being caused by the machine-executable instructions to: implementing any of the methods described above.
An embodiment of the present application further provides a computer-readable storage medium, in which a computer program is stored, and when the computer program is executed by a processor, the computer program implements any of the above methods.
Embodiments of the present application also provide a computer program product containing instructions that, when executed on a computer, cause the computer to perform any of the methods described above.
The beneficial effect of this application includes:
in the method provided by the embodiment of the application, a unit type of a code unit is obtained for the code unit to be compiled in a PLC program code to be compiled, a memory space of a memory type indicated by a memory layout manner is allocated to a variable of the code unit according to a preset memory layout manner corresponding to the unit type, and a WebAssembly instruction code corresponding to the code unit is generated according to the memory type of the allocated memory space, and the WebAssembly instruction code can access the variable of the code unit using the allocated memory space of the memory type in an executed process. Therefore, in the process of compiling the PLC program code into the WebAssembly code, the effective memory space is distributed for the variable in the PLC program code, so that the variable can be effectively accessed in the subsequent process of executing the compiled WebAssembly code.
Additional features and advantages of the present application will be set forth in the description which follows, and in part will be obvious from the description, or may be learned by the practice of the present application. The objectives and other advantages of the application may be realized and attained by the structure particularly pointed out in the written description and claims hereof as well as the appended drawings.
Drawings
The accompanying drawings are included to provide a further understanding of the application and are incorporated in and constitute a part of this specification, illustrate embodiments of the application and together with the description serve to explain the principles of the application and not to limit the application. In the drawings:
fig. 1 is a flowchart of a method for compiling PLC program codes into WebAssembly codes according to an embodiment of the present application;
FIG. 2 is a schematic structural diagram of a WebAssembly linear memory in an embodiment of the present application;
FIG. 3 is a schematic diagram of WebAssembly code accessing host memory at runtime in an embodiment of the present application;
fig. 4 is a schematic diagram of a memory allocation manner for a program provided in an embodiment of the present application;
fig. 5 is a schematic diagram of a first memory allocation manner for a function block provided in the embodiment of the present application;
fig. 6 is a schematic diagram of a second memory allocation manner for functional blocks provided in this embodiment of the present application;
fig. 7 is a schematic diagram of a memory allocation manner for global variables provided in this embodiment of the present application;
fig. 8 is a schematic diagram of a memory allocation manner for direct address variables provided in an embodiment of the present application;
fig. 9 is a schematic structural diagram of an apparatus for compiling PLC program codes into WebAssembly codes according to an embodiment of the present application;
fig. 10 is a schematic structural diagram of an electronic device according to an embodiment of the present application.
Detailed Description
In order to provide an implementation scheme for allocating effective memory space for variables in PLC program codes in the process of compiling the PLC program codes into WebAssembly codes, embodiments of the present application provide a method and an apparatus for compiling PLC program codes into WebAssembly codes. And the embodiments and features of the embodiments in the present application may be combined with each other without conflict.
The embodiment of the present application provides a method for compiling a PLC program code into a WebAssembly code, as shown in fig. 1, including:
step 11, aiming at a code unit to be compiled in a PLC program code to be compiled, acquiring a unit type of the code unit;
step 12, allocating the memory space of the memory type represented by the memory layout mode to the variable of the code unit according to the preset memory layout mode corresponding to the unit type;
and step 13, generating a WebAssembly instruction code corresponding to the code unit according to the memory type of the allocated memory space, wherein the WebAssembly instruction code can use the allocated memory type of the memory space to access the variable of the code unit in the executed process.
By adopting the method provided by the embodiment of the application, the corresponding memory layout mode is set aiming at various unit types of the code unit in advance, so that the effective memory space is distributed for the variable in the PLC program code in the process of compiling the PLC program code into the WebAssembly code, and the variable can be effectively accessed in the subsequent process of executing the compiled WebAssembly code.
In this embodiment of the present application, a PLC program code to be compiled is divided into a plurality of code units, a specific division manner may be based on each standard PLC programming language to perform corresponding division, and the divided code units may include the following types of code units:
PROGRAM (PROGRAM), FUNCTION BLOCK (FUNCTION BLOCK), FUNCTION (FUNCTION), global variable, and direct address variable (IO variable);
among them, programs, function blocks, and functions are also called POUs (Programming organization units).
In the embodiment of the application, the following memory types are defined:
a first memory type: webAssembly Linear memory (Linear memory): the WebAssembly code can be regarded as a one-dimensional array, and can realize random access by programming offset positions;
further, as shown in fig. 2, the WebAssembly linear memory may be divided into three memory spaces, namely a data area, an auxiliary stack and a heap, wherein the size of the data area is fixed and can be used for storing static data, the spatial position of the auxiliary stack is also predetermined, the space used by the WebAssembly code to the auxiliary stack in the running process changes with the invocation of a function, and the size of the heap can be dynamically increased during the running process.
The second memory type: GLOBAL memory of WebAssembly (GLOBAL): the global memory may be accessed by an index, which may also be understood as the number of each global memory, the scope being the entire WebAssembly code.
The third memory type: LOCAL memory of WebAssembly (LOCAL): the local memories may be accessed by an index, which may also be understood as the number of each local memory, the scope being internal to the function.
The three memory types also belong to memory objects specified by the WebAssembly specification.
The defined memory types may also include:
a fourth memory type: HOST (HOST) memory for WebAssembly: as shown in fig. 3, when the WebAssembly code is running, the WebAssembly code can be supported to access the memory space of the HOST memory by a mechanism of calling HOST (HOST) function in the WebAssembly.
A fifth memory type: the independent linear memory except the WebAssembly linear memory is not divided into three memory spaces of a data area, an auxiliary stack and a heap any more, and the WebAssembly linear memory can be regarded as a default linear memory of the WebAssembly.
In the method provided in the embodiment of the present application, in the process of compiling a PLC program code into a WebAssembly code, for various types of code units, memory spaces of corresponding memory types are allocated to variables of the code units according to corresponding memory layout manners, and in the subsequent corresponding WebAssembly instruction codes obtained by performing compilation, access to each variable using the memory space allocated to the variable is implemented.
The method and apparatus provided herein are described in detail below with reference to the accompanying drawings using specific embodiments.
Example 1:
in the embodiment of the present application, for a code unit whose unit type is a program, a memory space may be allocated in the following manner, and a WebAssembly instruction code corresponding to the code unit may be generated:
the static memory allocation mode is as follows: when the code unit is a program, acquiring the type and the number of member variables of the program;
determining the size and the first address of a memory space which needs to be allocated to the member variable of the program in a data area of the WebAssembly linear memory based on the type and the number of the member variable of the program;
as shown in fig. 4, in the static memory allocation manner, a memory space is allocated to a member variable of the program in a data area of the WebAssembly linear memory, where the allocated memory space may not include a memory space allocated to a direct address variable of the program, and a memory space is additionally allocated for the direct address variable.
Correspondingly, corresponding to the above static memory allocation manner, in the embodiment of the present application, the following four instruction code generation manners are proposed:
the first instruction code generation mode comprises the following steps: storing a first address of a memory space allocated for a program into a GLOBAL memory (GLOBAL) of the WebAssembly, and further naming the GLOBAL memory by using a name of the program, or naming the GLOBAL memory by using the name of the program plus a specified prefix or suffix;
generating a first address acquisition instruction code for acquiring a first address from the global memory;
generating a WebAssembly function corresponding to the content logic of the program, wherein the parameter of the WebAssembly function is a numerical value obtained by executing a first address acquisition instruction code;
when the WebAssembly code generated by the first instruction code generation mode is operated, firstly, the instruction code is obtained by operating the first address, the numerical value of the global memory, namely the first address of the memory space distributed by the program, is obtained, and the numerical value is used as a parameter to call the WebAssembly function corresponding to the program, so that the member variable of the program is accessed.
The second instruction code generation mode is as follows: generating a first address obtaining function aiming at a first address of a memory space allocated to a program, wherein a return value of the first address obtaining function after being executed is the first address, namely the first address obtaining function directly hard-codes and returns the first address of the memory space allocated to the program, further, the name of the program can be used for naming the first address obtaining function, and the name of the program and a specified prefix or suffix are also used for naming the first address obtaining function;
generating a WebAssembly function corresponding to the content logic of the program, wherein the parameter of the WebAssembly function is a return value obtained by executing the first address acquisition function;
when the WebAssembly code generated by the second instruction code generation method is operated, the first address of the memory space allocated to the program is obtained by operating the first address acquisition function, and the WebAssembly function corresponding to the program is called by taking the first address as a parameter, so that the member variable of the program is accessed.
The third instruction code generation mode: storing the first address of the memory space allocated for the program into a global pointer array which is pre-arranged in a WebAssembly linear memory and is created aiming at all programs of a PLC program code to be compiled, defining a reference number for each program of the PLC program code, and storing the first address in the global pointer array by using the reference number as a subscript;
generating a second address acquisition instruction code for acquiring a first address of a memory space allocated for the program from the global pointer array;
generating a WebAssembly function corresponding to the content logic of the program, wherein the parameter of the WebAssembly function is a numerical value obtained by executing the second address to obtain the instruction code;
when the WebAssembly code generated by the third instruction code generation method is operated, firstly, the instruction code is obtained by operating the second address, that is, the first address of the memory space allocated to the program is obtained from the global pointer array by directly using the index number of the program, and the WebAssembly function corresponding to the program is called by using the first address as a parameter, so that the member variable of the program is accessed.
The fourth instruction code generation method: generating a uniform call entry function and a WebAssembly function corresponding to the content logic of the program aiming at all programs of a PLC program code to be compiled, and enabling the uniform call entry function to call the WebAssembly function by taking the first address of a memory space allocated by the program as the parameter of the WebAssembly function when the entry parameter of the uniform call entry function is the name or ID of the program;
when the WebAssembly code generated by the fourth instruction code generation manner is operated, the unified call entry function is called by using the name or the ID of the program, so that the unified call entry function takes the first address of the memory space allocated by the program as the parameter of the WebAssembly function, and calls the WebAssembly function, thereby realizing the access to the member variable of the program.
Dynamic memory allocation mode: when the code unit is a program, determining to dynamically allocate a memory space for member variables of the program from a stack of a WebAssembly linear memory;
that is, in the process of compiling the PLC code, a fixed memory space is not allocated to the program, but a memory space is dynamically allocated to the program from the stack of the WebAssembly linear memory when the corresponding code is generated and the program is running, so that only the memory space is allocated to the program which really needs to run, and the memory consumption in running is reduced;
as shown in fig. 4, in the dynamic memory allocation manner, on the heap of the WebAssembly linear memory, a memory space is allocated for the member variable of the program, wherein the allocated memory space may not include a memory space allocated for the direct address variable of the program, and a memory space is additionally allocated for the direct address variable.
Correspondingly, in the embodiment of the present application, a fifth instruction code generation manner is provided, corresponding to the dynamic memory allocation manner described above:
generating a second address obtaining function for the program, where the second address obtaining function is used to call a memory allocation function (such as malloc) to allocate a memory space on a heap of the WebAssembly linear memory for a member variable of the program, and further, the second address obtaining function may be named by using a name of the program, or named by using the name of the program plus a specified prefix or suffix;
generating a WebAssembly function corresponding to the content logic of the program, wherein the parameter of the WebAssembly function is a return value obtained by executing a memory allocation function, and the return value is the first address of a memory space allocated to the member variable of the program on a stack of a WebAssembly linear memory;
when the WebAssembly code generated by the fifth instruction code generation mode is operated, the second address acquisition function is operated firstly, the memory allocation function is called inside the second address acquisition function, the memory space is dynamically allocated on the stack of the WebAssembly linear memory for the member variable of the program, the first address of the allocated memory space is returned, and the first address is used as a parameter to call the WebAssembly function corresponding to the program, so that the member variable of the program is accessed.
Example 2:
in the embodiment of the present application, for a code unit whose unit type is a function block, a memory space may be allocated in the following manner, and a WebAssembly instruction code corresponding to the code unit may be generated:
a first memory allocation method for functional blocks:
when the code unit is a function block, determining the memory space allocated to the program to which the function block belongs, including the memory space allocated to the function block;
calculating the size of memory space occupied by all member variables in front of the function block in the program as an offset based on the position of the function block serving as the member variable in the program to which the function block belongs;
in the PLC code, a function block belongs to a part of a program and can be used as a member variable of the program, so, as shown in fig. 5, a memory space allocated to the function block of the program can be included in a memory space allocated to the program, for example, when the memory space allocated to the program to which the function block belongs to a data area of a WebAssembly linear memory, when the memory space allocated to the program to which the functional block belongs to the heap of the WebAssembly linear memory, the memory space allocated to the functional block also belongs to the heap of the WebAssembly linear memory;
the offset may be added to the first address of the memory space allocated to the program to which the functional block belongs, and the first address of the memory space allocated to the functional block may be represented as the first address;
correspondingly, in response to the first memory allocation manner for the functional block, in the embodiment of the present application, the following first instruction code generation manner for the functional block is provided:
generating a third address acquisition instruction code aiming at the functional block, wherein the third address acquisition instruction code is used for adding an offset to a first address of a memory space allocated for the program to obtain the first address of the memory space allocated for the functional block;
generating a WebAssembly function corresponding to the content logic of the functional block, wherein the parameter of the WebAssembly function is a numerical value obtained by executing a third address acquisition instruction code;
when the WebAssembly code generated by the first instruction code generation mode for the functional block is operated, firstly, the instruction code is obtained by operating the third address, the first address of the memory space allocated to the program is added with the offset to obtain the first address of the memory space allocated to the functional block, and the first address is used as a parameter to call the WebAssembly function corresponding to the functional block, so that the member variable of the functional block is accessed.
A second memory allocation method for the functional blocks:
when the code unit is a functional block, allocating a memory space in a HOST (HOST) memory of the WebAssembly for the functional block, wherein the memory space allocated to a program to which the functional block belongs comprises a memory space for storing a functional block handle (reference handle) of the HOST memory, and the HOST memory stores a mapping relation between the functional block handle and a first address of the memory space allocated to the functional block;
calculating the size of memory space occupied by all member variables in front of the function block in the program as an offset based on the position of the function block as the member variable in the program to which the function block belongs;
for the second memory allocation manner of the function block, as shown in fig. 6, a memory space is allocated to the function block in a HOST (HOST) memory of the WebAssembly, that is, a content logic of the function block is supported to be implemented in the HOST, instead of operating in the WebAssembly, in this manner, all member variables of the function block are no longer stored in a WebAssembly linear memory, but a space with a preset size (for example, 4 bytes) is used to store a function block handle of the HOST memory, the function block handle may be an integer, and a mapping relationship between the function block handle and a first address of the memory space allocated to the function block is created in the HOST memory, so that a first address of the memory space allocated to the function block may be found based on the mapping relationship in a subsequent operation;
correspondingly, corresponding to the second memory allocation manner for the functional block, in the embodiment of the present application, the following second instruction code generation manner for the functional block is proposed:
generating a fourth address acquisition instruction code aiming at the functional block, wherein the fourth address acquisition instruction code is used for adding an offset to a first address of a memory space allocated for the program to obtain the first address of the memory space allocated for the functional block handle;
generating a numerical value obtaining instruction code for obtaining the function block handle from the initial address;
and generating a first HOST (HOST) function call instruction which takes the function block handle obtained by executing the numerical value acquisition instruction code as a parameter, wherein the first HOST function call instruction is used for calling a HOST function, and the HOST function is used for searching a head address of a memory space allocated for the function block in a HOST memory based on the function block handle and realizing the content logic of the function block based on the head address.
When the WebAssembly code generated by the second instruction code generation method for the function block is operated, firstly, the instruction code is obtained by operating the fourth address, the address of the storage function block handle is obtained by adding the offset to the head address of the memory space allocated to the program, the instruction code is obtained by operating the numerical value based on the address, the value of the function block handle is obtained, then, the first host function call instruction is operated by taking the obtained value of the function block handle as a parameter, the host function is called, the mapping relation is searched by the host function based on the function block handle, the head address of the memory space allocated to the function block in the host memory is obtained, the access to the member variable of the function block is realized based on the head address, and the content logic of the function block is realized.
Example 3:
in the embodiment of the present application, for a code unit whose unit type is a function, a memory space may be allocated in the following manner, and a WebAssembly instruction code corresponding to the code unit may be generated:
a first memory allocation for functions:
when the code unit is a function, allocating a memory space on an auxiliary stack of the WebAssembly linear memory for a local variable of the function;
correspondingly, in response to the first memory allocation manner for the function, in the embodiment of the present application, the following first instruction code generation manner for the function is proposed:
generating a fifth address obtaining instruction code (which may be a load/store instruction code) for obtaining a first address of a memory space allocated to a local variable of the function, where the first address is a stack top pointer plus an offset of the local variable of the function inside the function;
generating a WebAssembly function corresponding to the content logic of the function, wherein the parameter of the WebAssembly function is the parameter of the function, and the WebAssembly function is used for accessing the local variable of the function by using the head address of the allocated memory space;
when the WebAssembly code generated by the first instruction code generation method for the function is operated, firstly, the instruction code is obtained by operating the fifth address, the first address of the memory space allocated to the local variable of the function is obtained, the parameter of the function is used as the parameter to call the WebAssembly function corresponding to the function, and the WebAssembly function uses the first address of the allocated memory space to realize the access to the local variable of the function.
For the second memory allocation method of the function:
when the code unit is a function, allocating a memory space in a local memory of the WebAssembly for a local variable of the function, wherein the allocated local memory can be represented by the number of the local memory;
correspondingly, in the embodiment of the present application, corresponding to the second memory allocation manner for the function, the following second instruction code generation manner for the function is proposed:
acquiring the number of a local memory allocated to the local variable of the function;
generating a WebAssembly function corresponding to the content logic of the function, wherein the parameter of the WebAssembly function is the parameter of the function, and the WebAssembly function is used for accessing the local variable of the function by using the number of the distributed local memory;
when the WebAssembly code generated by the second instruction code generation mode for the function is operated, the parameter of the function is used as a parameter to call the WebAssembly function corresponding to the function, and the WebAssembly function uses the number of the distributed local memory to realize the access to the local variable of the function.
Example 4:
in the embodiment of the present application, for a code unit of which the unit type is a global variable, a memory space may be allocated in the following manner, and a WebAssembly instruction code corresponding to the code unit may be generated:
a first memory allocation method for global variables:
when the code unit is a global variable, allocating a memory space in a data area of the WebAssembly linear memory for the global variable;
correspondingly, corresponding to the first memory allocation manner for the global variable, in the embodiment of the present application, the following first instruction code generation manner for the global variable is proposed:
and generating a first access instruction code which uses the address of the global variable in the data area of the WebAssembly linear memory as an operand, namely directly using the address to access the global variable when the first access instruction code is executed, wherein the first access instruction code can be iload/istare instruction code.
A second memory allocation method for global variables:
when the code unit is a GLOBAL variable, allocating a memory space in a GLOBAL memory (GLOBAL) of the WebAssembly for the GLOBAL variable, and using an index of the GLOBAL memory as a number to represent the GLOBAL memory;
correspondingly, in the embodiment of the present application, corresponding to the second memory allocation manner for the global variable, the following second instruction code generation manner for the global variable is provided:
and generating a second access instruction code (which can be set/get global) using the number of the global memory allocated to the global variable as an operand, namely accessing the global variable in the global memory based on the allocated number of the global memory as the operand when the second access instruction code is executed.
A third memory allocation method for global variables:
when the code unit is a global variable, allocating a memory space for the global variable in a host memory of the WebAssembly, and storing a mapping relation between the ID of the global variable and a first address of the memory space allocated for the global variable in the host memory, namely creating a mapping relation between the ID of each global variable of the PLC code and the first address of each allocated memory space in the host memory;
correspondingly, corresponding to the third memory allocation manner for the global variable, in the embodiment of the present application, the following third instruction code generation manner for the global variable is proposed:
and generating a second host function call instruction with the ID of the global variable as a parameter, wherein the second host function call instruction is used for calling a host function, and the host function is used for searching the initial address of the memory space allocated for the global variable based on the ID of the global variable and the mapping relation and accessing the global variable based on the initial address.
In the embodiment of the present application, a schematic diagram of allocating a memory space for a global variable by using the above three manners is shown in fig. 7.
Example 5:
in this embodiment of the present application, for a code unit whose unit type is a direct address variable, as shown in fig. 8, a memory space may be allocated in the following manner, and a WebAssembly instruction code corresponding to the code unit may be generated:
a first memory allocation method for direct address variables:
when the code unit is a direct address variable, allocating a memory space in a data area of the WebAssembly linear memory for the direct address variable;
correspondingly, corresponding to the first memory allocation manner for the direct address variable, in the embodiment of the present application, the following first instruction code generation manner for the direct address variable is proposed:
and generating a third access instruction code for directly accessing the direct address variable using an address of the memory space allocated for the direct address variable.
A second memory allocation method for direct address variables:
when the code unit is a direct address variable, determining to dynamically allocate a memory space on a stack of the WebAssembly linear memory for the direct address variable;
correspondingly, corresponding to the second memory allocation manner for the direct address variable, in the embodiment of the present application, the following second instruction code generation manner for the direct address variable is proposed:
generating a third address acquisition function aiming at the direct address variable, wherein the third address acquisition function is used for calling a memory allocation function (malloc) to allocate a memory space for the direct address variable on a stack of the WebAssembly linear memory;
specifically, when the direct address variable needs to be accessed, the memory allocation function is called on a stack of the WebAssembly linear memory by operating the third address acquisition function to allocate a memory space for the IO region, the first address of the memory space allocated for the IO region is stored in a global variable or a global memory, when a certain direct address variable needs to be accessed, the first address is acquired from the global variable or the global memory storing the first address, and the offset of the direct address variable in the IO region is added to obtain the first address of the memory space allocated for the direct address variable, so that the direct address variable is accessed.
A third memory allocation approach for direct address variables:
when the code unit is a direct address variable, allocating a memory space in a host memory of the WebAssembly for the direct address variable, namely allocating the memory space for an IO region of a PLC code in the host memory of the WebAssembly;
correspondingly, corresponding to the third memory allocation manner for the direct address variable, in the embodiment of the present application, the following third instruction code generation manner for the direct address variable is proposed:
generating a third host function call instruction for the direct address variable, wherein the third host function call instruction is used for calling a host function, and the host function is used for accessing the direct address variable based on an address of a memory space allocated for the direct address variable;
specifically, the first address of the memory space allocated to the IO area may be obtained by adding the offset of the direct address variable in the IO area to the first address of the memory space allocated to the direct address variable, and the called host function uses the first address to implement access to the direct address variable.
A fourth memory allocation method for direct address variables:
when the code unit is a direct address variable, allocating a memory space in an additional independent linear memory for the direct address variable, wherein the additional independent linear memory is not divided into a data area, an auxiliary stack and a heap;
correspondingly, in the embodiment of the present application, corresponding to the fourth memory allocation manner for the direct address variable, the following fourth instruction code generation manner for the direct address variable is provided:
and generating a fourth access instruction code (which can be a store/load) for directly accessing the direct address variable by using the address of the memory space allocated for the direct address variable, namely, by executing the fourth access instruction code, directly accessing the direct address variable by using the address of the memory space allocated for the direct address variable.
Based on the same inventive concept, according to the method for compiling PLC program codes into WebAssembly codes provided in the foregoing embodiments of the present application, correspondingly, another embodiment of the present application further provides a device for compiling PLC program codes into WebAssembly codes, a schematic structural diagram of which is shown in fig. 9, and specifically includes:
a unit type obtaining module 91, configured to obtain, for a code unit to be compiled in a PLC program code to be compiled, a unit type of the code unit;
a space allocation module 92, configured to allocate, according to a preset memory layout manner corresponding to the unit type, a memory space of the memory type represented by the memory layout manner for the variable of the code unit;
a code generating module 93, configured to generate, according to the memory type of the allocated memory space, a WebAssembly instruction code corresponding to the code unit, where the WebAssembly instruction code can access a variable of the code unit using the allocated memory space of the memory type in an executed process.
Further, the code units include at least one of the following types of code units:
programs, function blocks, functions, global variables, and direct address variables;
the memory types include: the WebAssembly linear memory is divided into three memory spaces of a data area, an auxiliary stack and a heap, and the memory types further comprise a WebAssembly global memory, a WebAssembly local memory, a WebAssembly host memory and an independent linear memory except the WebAssembly linear memory.
Further, the space allocation module 92 is specifically configured to, when the code unit is a program, obtain the type and number of member variables of the program;
and determining the size and the first address of a memory space which needs to be allocated to the member variables of the program in a data area of the WebAssembly linear memory based on the type and the number of the member variables of the program.
Further, the code generating module 93 is specifically configured to store a first address of a memory space allocated to the program into a global memory of the WebAssembly; generating a first address acquisition instruction code for acquiring the first address from the global memory; generating a WebAssembly function corresponding to the content logic of the program, wherein the parameter of the WebAssembly function is a numerical value obtained by executing the first address acquisition instruction code; or
Generating a first address acquisition function aiming at a first address of a memory space allocated for the program, wherein a return value of the first address acquisition function after being executed is the first address; generating a WebAssembly function corresponding to the content logic of the program, wherein the parameter of the WebAssembly function is a return value obtained by executing the first address acquisition function; or
Storing the first address of the memory space allocated for the program into a global pointer array which is pre-established in the WebAssembly linear memory and is created for all programs of a PLC program code to be compiled; generating a second address fetch instruction code for fetching the first address from the global pointer array; generating a WebAssembly function corresponding to the content logic of the program, wherein the parameter of the WebAssembly function is a numerical value obtained by executing the second address to obtain an instruction code; or
And when the entry parameter of the uniform calling entry function is the name or the ID of the program, enabling the uniform calling entry function to call the WebAssembly function by taking the first address of the memory space allocated by the program as the parameter of the WebAssembly function.
Further, the space allocation module 92 is specifically configured to determine to dynamically allocate a memory space for a member variable of a program from a heap of the WebAssembly linear memory when the code unit is the program;
the code generating module 93 is specifically configured to generate a second address obtaining function for the program, where the second address obtaining function is configured to call a memory allocation function to allocate a memory space on a heap of the WebAssembly linear memory for a member variable of the program; and generating a WebAssembly function corresponding to the content logic of the program, wherein the parameter of the WebAssembly function is a return value obtained by executing the memory allocation function, and the return value is the first address of a memory space allocated on a stack of the WebAssembly linear memory for the member variable of the program.
Further, the space allocation module 92 is specifically configured to, when the code unit is a function block, determine a memory space allocated to a program to which the function block belongs, including a memory space allocated to the function block;
calculating the size of memory space occupied by all member variables before the function block in the program as an offset based on the position of the function block as the member variable in the program to which the function block belongs;
the code generating module 93 is specifically configured to generate a third address obtaining instruction code for the functional block, where the third address obtaining instruction code is configured to obtain a first address of a memory space allocated to the functional block by adding the offset to the first address of the memory space allocated to the program; and generating a WebAssembly function corresponding to the content logic of the functional block, wherein the parameter of the WebAssembly function is a numerical value obtained by executing the third address acquisition instruction code.
Further, the space allocation module 92 is specifically configured to, when the code unit is a function block, allocate a memory space to the function block in a host memory of the WebAssembly, and allocate a memory space to a program to which the function block belongs, where the memory space includes a memory space used for storing a function block handle of the host memory, and the host memory stores a mapping relationship between the function block handle and a first address of the memory space allocated to the function block;
calculating the size of memory space occupied by all member variables before the function block in the program as an offset based on the position of the function block as the member variable in the program to which the function block belongs;
the code generating module 93 is specifically configured to generate a fourth address obtaining instruction code for the functional block, where the fourth address obtaining instruction code is configured to obtain a first address of a memory space allocated to the functional block handle by adding the offset to the first address of the memory space allocated to the program; and generating a numerical value acquiring instruction code for acquiring the function block handle from the first address; and generating a first host function call instruction with the function block handle obtained by executing the numerical value acquisition instruction code as a parameter, wherein the first host function call instruction is used for calling a host function, and the host function is used for finding a head address of a memory space allocated for the function block in a host memory based on the function block handle and realizing the content logic of the function block based on the head address.
Further, the space allocation module 92 is specifically configured to, when the code unit is a function, allocate a memory space on an auxiliary stack of the WebAssembly linear memory for a local variable of the function;
the code generating module 93 is specifically configured to generate a fifth address obtaining instruction code, and is configured to obtain a first address of a memory space allocated to the local variable of the function, where the first address is a stack top pointer plus an offset of the local variable of the function within the function;
and generating a WebAssembly function corresponding to the content logic of the function, wherein the parameter of the WebAssembly function is the parameter of the function, and the WebAssembly function is used for accessing the local variable of the function by using the head address of the allocated memory space.
Further, the space allocation module 92 is specifically configured to, when the code unit is a function, allocate a memory space in a local memory of the WebAssembly for a local variable of the function;
the code generating module 93 is specifically configured to obtain a number of a local memory allocated to a local variable of the function;
and generating a WebAssembly function corresponding to the content logic of the function, wherein the parameter of the WebAssembly function is the parameter of the function, and the WebAssembly function is used for accessing the local variable of the function by using the number of the distributed local memory.
Further, the space allocation module 92 is specifically configured to, when the code unit is a global variable, allocate a memory space for the global variable in a data area of the WebAssembly linear memory;
the code generating module 93 is specifically configured to generate a first access instruction code using an address of the global variable in a data area of the WebAssembly linear memory as an operand.
Further, the space allocation module 92 is specifically configured to, when the code unit is a global variable, allocate a memory space in a global memory of the WebAssembly for the global variable;
the code generating module 93 is specifically configured to generate a second access instruction code using the number of the global memory allocated to the global variable as an operand.
Further, the space allocation module 92 is specifically configured to, when the code unit is a global variable, allocate a memory space for the global variable in a host memory of the WebAssembly, where a mapping relationship between an ID of the global variable and a first address of the memory space allocated for the global variable is stored in the host memory;
the code generating module 93 is specifically configured to generate a second host function call instruction using the ID of the global variable as a parameter, where the second host function call instruction is used to call a host function, and the host function is used to find a head address of a memory space allocated to the global variable based on the ID of the global variable and the mapping relationship, and access the global variable based on the head address.
Further, the space allocation module 92 is specifically configured to, when the code unit is a direct address variable, allocate a memory space for the direct address variable in a data area of the WebAssembly linear memory; or
When the code unit is a direct address variable, determining to dynamically allocate a memory space for the direct address variable on a stack of the WebAssembly linear memory; or
When the code unit is a direct address variable, allocating a memory space in a host memory of WebAssembly for the direct address variable; or
When the code unit is a direct address variable, allocating a memory space in an additional independent linear memory for the direct address variable;
the code generating module 93 is specifically configured to generate a third access instruction code for directly accessing the direct address variable by using an address of the memory space allocated to the direct address variable when the memory space is allocated to the direct address variable in the data area of the WebAssembly linear memory; or alternatively
When determining that memory space is dynamically allocated to the direct address variable on a stack of the WebAssembly linear memory, generating a third address acquisition function for the direct address variable, wherein the third address acquisition function is used for calling a memory allocation function to allocate memory space to the direct address variable on the stack of the WebAssembly linear memory; or
When a memory space is allocated in a host memory of the WebAssembly for the direct address variable, generating a third host function call instruction for the direct address variable, wherein the third host function call instruction is used for calling a host function, and the host function is used for accessing the direct address variable based on the address of the memory space allocated for the direct address variable; or
Generating a fourth access instruction code for directly accessing the direct address variable using an address of the memory space allocated for the direct address variable when the memory space is allocated in an additional independent linear memory for the direct address variable.
The above-described apparatus provided by the embodiments of the present application can be implemented by a computer program. It should be understood by those skilled in the art that the above-mentioned division of the modules is only one of many divisions of the modules, and if the division into other modules or no division into modules is performed, the above-mentioned apparatus having the above-mentioned functions should be within the scope of the present application.
Another embodiment of the present application further provides an electronic device, a schematic structural diagram of which is shown in fig. 10, and specifically includes: a processor 1001 and a machine-readable storage medium 1002, the machine-readable storage medium 1002 storing machine-executable instructions executable by the processor 1001, the processor 1001 being caused by the machine-executable instructions to: any one of the above methods for compiling PLC program codes into WebAssembly codes is realized.
An embodiment of the present application further provides a computer-readable storage medium, in which a computer program is stored, and when the computer program is executed by a processor, the method for compiling the PLC program code into the WebAssembly code is implemented.
Embodiments of the present application also provide a computer program product containing instructions that, when executed on a computer, cause the computer to perform any of the above-described methods for compiling PLC program code into WebAssembly code.
The machine-readable storage medium in the electronic device may include a Random Access Memory (RAM) and a Non-Volatile Memory (NVM), such as at least one disk Memory. Optionally, the memory may also be at least one memory device located remotely from the processor.
The Processor may be a general-purpose Processor, including a Central Processing Unit (CPU), a Network Processor (NP), and the like; but also Digital Signal Processors (DSPs), application Specific Integrated Circuits (ASICs), field Programmable Gate Arrays (FPGAs) or other Programmable logic devices, discrete Gate or transistor logic devices, discrete hardware components.
All the embodiments in the present specification are described in a related manner, and the same and similar parts among the embodiments may be referred to each other, and each embodiment focuses on the differences from the other embodiments. In particular, for the apparatus, the electronic device, the computer-readable storage medium, and the computer program product embodiment, since they are substantially similar to the method embodiment, the description is relatively simple, and for the relevant points, reference may be made to the partial description of the method embodiment.
It should be noted that, in this document, relational terms such as first and second, and the like are used solely to distinguish one entity or action from another entity or action without necessarily requiring or implying any actual such relationship or order between such entities or actions. Also, the terms "comprises," "comprising," or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements does not include only those elements but may include other elements not expressly listed or inherent to such process, method, article, or apparatus. Without further limitation, an element defined by the phrases "comprising one of 8230; \8230;" 8230; "does not exclude the presence of additional like elements in a process, method, article, or apparatus that comprises the element.
The present application is described with reference to flowchart illustrations and/or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of the application. It will be understood that each flow and/or block of the flow diagrams and/or block diagrams, and combinations of flows and/or blocks in the flow diagrams and/or block diagrams, can be implemented by computer program instructions. These computer program instructions may be provided to a processor of a general purpose computer, special purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, create means for implementing the functions specified in the flowchart flow or flows and/or block diagram block or blocks.
These computer program instructions may also be stored in a computer-readable memory that can direct a computer or other programmable data processing apparatus to function in a particular manner, such that the instructions stored in the computer-readable memory produce an article of manufacture including instruction means which implement the function specified in the flowchart flow or flows and/or block diagram block or blocks.
These computer program instructions may also be loaded onto a computer or other programmable data processing apparatus to cause a series of operational steps to be performed on the computer or other programmable apparatus to produce a computer implemented process such that the instructions which execute on the computer or other programmable apparatus provide steps for implementing the functions specified in the flowchart flow or flows and/or block diagram block or blocks.
It will be apparent to those skilled in the art that various changes and modifications may be made in the present application without departing from the spirit and scope of the application. Thus, if such modifications and variations of the present application fall within the scope of the claims of the present application and their equivalents, the present application is intended to include such modifications and variations as well.

Claims (16)

1. A method of compiling PLC program code into WebAssembly code, comprising:
acquiring a unit type of a code unit to be compiled in a PLC program code to be compiled;
allocating memory space of the memory type represented by the memory layout mode to the variable of the code unit according to a preset memory layout mode corresponding to the unit type;
and generating a WebAssembly instruction code corresponding to the code unit according to the memory type of the allocated memory space, wherein the WebAssembly instruction code can use the allocated memory space of the memory type to access the variable of the code unit in the executed process.
2. The method of claim 1, wherein the code units comprise at least one of the following types of code units:
programs, function blocks, functions, global variables, and direct address variables;
the memory types include: the WebAssembly linear memory is divided into three memory spaces of a data area, an auxiliary stack and a heap, and the memory types further comprise a WebAssembly global memory, a WebAssembly local memory, a WebAssembly host memory and an independent linear memory except the WebAssembly linear memory.
3. The method according to claim 1, wherein the allocating a memory space of a memory type represented by the memory layout manner to the variable of the code unit according to a preset memory layout manner corresponding to the unit type includes:
when the code unit is a program, acquiring the type and the number of member variables of the program;
and determining the size of a memory space and a first address which need to be allocated to the member variable of the program in a data area of the WebAssembly linear memory based on the type and the number of the member variable of the program.
4. The method as claimed in claim 3, wherein said generating the WebAssembly instruction code corresponding to the code unit according to the memory type of the allocated memory space comprises:
storing the first address of the memory space allocated for the program into a global memory of the WebAssembly; generating a first address acquisition instruction code for acquiring the first address from the global memory; generating a WebAssembly function corresponding to the content logic of the program, wherein the parameter of the WebAssembly function is a numerical value obtained by executing the first address acquisition instruction code; or alternatively
Generating a first address acquisition function aiming at a first address of a memory space allocated for the program, wherein a return value of the first address acquisition function after being executed is the first address; generating a WebAssembly function corresponding to the content logic of the program, wherein the parameter of the WebAssembly function is a return value obtained by executing the first address acquisition function; or alternatively
Storing the first address of the memory space allocated for the program into a global pointer array which is pre-established in the WebAssembly linear memory and is created aiming at all programs of a PLC program code to be compiled; generating a second address fetch instruction code for fetching the first address from the global pointer array; generating a WebAssembly function corresponding to the content logic of the program, wherein the parameter of the WebAssembly function is a numerical value obtained by executing the second address to obtain an instruction code; or alternatively
And when the entry parameter of the uniform calling entry function is the name or the ID of the program, enabling the uniform calling entry function to call the WebAssembly function by taking the first address of the memory space allocated by the program as the parameter of the WebAssembly function.
5. The method according to claim 1, wherein the allocating a memory space of a memory type represented by the memory layout mode to the variable of the code unit according to a preset memory layout mode corresponding to the unit type comprises:
when the code unit is a program, determining to dynamically allocate a memory space for member variables of the program from a stack of a WebAssembly linear memory;
the generating of the WebAssembly instruction code corresponding to the code unit according to the memory type of the allocated memory space includes:
generating a second address acquisition function for the program, wherein the second address acquisition function is used for calling a memory allocation function to allocate a memory space for member variables of the program on a stack of the WebAssembly linear memory; and generating a WebAssembly function corresponding to the content logic of the program, wherein the parameter of the WebAssembly function is a return value obtained by executing the memory allocation function, and the return value is the first address of a memory space allocated on a stack of the WebAssembly linear memory for the member variable of the program.
6. The method according to claim 1, wherein the allocating a memory space of a memory type represented by the memory layout manner to the variable of the code unit according to a preset memory layout manner corresponding to the unit type includes:
when the code unit is a function block, determining memory space allocated to a program to which the function block belongs, including the memory space allocated to the function block;
calculating the size of memory space occupied by all member variables before the function block in the program as an offset based on the position of the function block as the member variable in the program to which the function block belongs;
generating the WebAssembly instruction code corresponding to the code unit according to the memory type of the allocated memory space, wherein the WebAssembly instruction code comprises the following steps:
generating a third address acquisition instruction code for the functional block, wherein the third address acquisition instruction code is used for adding the offset to the first address of the memory space allocated to the program to obtain the first address of the memory space allocated to the functional block; and generating a WebAssembly function corresponding to the content logic of the functional block, wherein the parameter of the WebAssembly function is a numerical value obtained by executing the third address acquisition instruction code.
7. The method according to claim 1, wherein the allocating a memory space of a memory type represented by the memory layout manner to the variable of the code unit according to a preset memory layout manner corresponding to the unit type includes:
when the code unit is a functional block, allocating a memory space in a host memory of the WebAssembly for the functional block, wherein the memory space allocated for a program to which the functional block belongs comprises a memory space for storing a functional block handle of the host memory, and the host memory stores a mapping relation between the functional block handle and a first address of the memory space allocated for the functional block;
calculating the size of memory space occupied by all member variables before the function block in the program as an offset based on the position of the function block as the member variable in the program to which the function block belongs;
generating the WebAssembly instruction code corresponding to the code unit according to the memory type of the allocated memory space, wherein the WebAssembly instruction code comprises the following steps:
generating a fourth address acquisition instruction code for the functional block, where the fourth address acquisition instruction code is used to obtain a first address of a memory space allocated for the functional block handle by adding the offset to the first address of the memory space allocated for the program; and generating a numerical value acquiring instruction code for acquiring the function block handle from the initial address; and generating a first host function call instruction with the function block handle obtained by executing the numerical value acquisition instruction code as a parameter, wherein the first host function call instruction is used for calling a host function, and the host function is used for finding a head address of a memory space allocated for the function block in a host memory based on the function block handle and realizing the content logic of the function block based on the head address.
8. The method according to claim 1, wherein the allocating a memory space of a memory type represented by the memory layout manner to the variable of the code unit according to a preset memory layout manner corresponding to the unit type includes:
when the code unit is a function, allocating a memory space on an auxiliary stack of the WebAssembly linear memory for a local variable of the function;
generating the WebAssembly instruction code corresponding to the code unit according to the memory type of the allocated memory space, wherein the WebAssembly instruction code comprises the following steps:
generating a fifth address acquisition instruction code, configured to acquire a first address of a memory space allocated to a local variable of the function, where the first address is a stack top pointer plus an offset of the local variable of the function within the function;
and generating a WebAssembly function corresponding to the content logic of the function, wherein the parameter of the WebAssembly function is the parameter of the function, and the WebAssembly function is used for accessing the local variable of the function by using the head address of the allocated memory space.
9. The method according to claim 1, wherein the allocating a memory space of a memory type represented by the memory layout mode to the variable of the code unit according to a preset memory layout mode corresponding to the unit type comprises:
when the code unit is a function, allocating a memory space in a local memory of WebAssembly for a local variable of the function;
the generating of the WebAssembly instruction code corresponding to the code unit according to the memory type of the allocated memory space includes:
acquiring the number of a local memory distributed for the local variable of the function;
and generating a WebAssembly function corresponding to the content logic of the function, wherein the parameter of the WebAssembly function is the parameter of the function, and the WebAssembly function is used for accessing the local variable of the function by using the number of the distributed local memory.
10. The method according to claim 1, wherein the allocating a memory space of a memory type represented by the memory layout mode to the variable of the code unit according to a preset memory layout mode corresponding to the unit type comprises:
when the code unit is a global variable, allocating a memory space in a data area of the WebAssembly linear memory for the global variable;
generating the WebAssembly instruction code corresponding to the code unit according to the memory type of the allocated memory space, wherein the WebAssembly instruction code comprises the following steps:
and generating a first access instruction code which uses the address of the global variable in the data area of the WebAssembly linear memory as an operand.
11. The method according to claim 1, wherein the allocating a memory space of a memory type represented by the memory layout manner to the variable of the code unit according to a preset memory layout manner corresponding to the unit type includes:
when the code unit is a global variable, allocating a memory space in a global memory of the WebAssembly for the global variable;
the generating of the WebAssembly instruction code corresponding to the code unit according to the memory type of the allocated memory space includes:
and generating a second access instruction code using the number of the global memory allocated to the global variable as an operand.
12. The method according to claim 1, wherein the allocating a memory space of a memory type represented by the memory layout manner to the variable of the code unit according to a preset memory layout manner corresponding to the unit type includes:
when the code unit is a global variable, allocating a memory space for the global variable in a host memory of WebAssembly, wherein a mapping relation between an ID of the global variable and a first address of the memory space allocated for the global variable is stored in the host memory;
generating the WebAssembly instruction code corresponding to the code unit according to the memory type of the allocated memory space, wherein the WebAssembly instruction code comprises the following steps:
and generating a second host function call instruction with the ID of the global variable as a parameter, wherein the second host function call instruction is used for calling a host function, and the host function is used for searching the first address of the memory space allocated for the global variable based on the ID of the global variable and the mapping relation and accessing the global variable based on the first address.
13. The method according to claim 1, wherein the allocating a memory space of a memory type represented by the memory layout mode to the variable of the code unit according to a preset memory layout mode corresponding to the unit type comprises:
when the code unit is a direct address variable, allocating a memory space in a data area of the WebAssembly linear memory for the direct address variable; or
When the code unit is a direct address variable, determining to dynamically allocate a memory space for the direct address variable on a stack of the WebAssembly linear memory; or
When the code unit is a direct address variable, allocating a memory space in a host memory of WebAssembly for the direct address variable; or
When the code unit is a direct address variable, allocating a memory space in an additional independent linear memory for the direct address variable;
generating the WebAssembly instruction code corresponding to the code unit according to the memory type of the allocated memory space, wherein the WebAssembly instruction code comprises the following steps:
generating a third access instruction code for directly accessing the direct address variable by using the address of the memory space allocated for the direct address variable when the memory space is allocated for the direct address variable in the data area of the WebAssembly linear memory; or
When determining that memory space is dynamically allocated to the direct address variable on a stack of the WebAssembly linear memory, generating a third address acquisition function for the direct address variable, wherein the third address acquisition function is used for calling a memory allocation function to allocate memory space to the direct address variable on the stack of the WebAssembly linear memory; or
When a memory space is allocated in a host memory of the WebAssembly for the direct address variable, generating a third host function call instruction for the direct address variable, wherein the third host function call instruction is used for calling a host function, and the host function is used for accessing the direct address variable based on the address of the memory space allocated for the direct address variable; or
When a memory space is allocated in an additional independent linear memory for the direct address variable, a fourth access instruction code is generated for directly accessing the direct address variable using an address of the memory space allocated for the direct address variable.
14. An apparatus for compiling PLC program code into WebAssembly code, comprising:
the unit type acquisition module is used for acquiring the unit type of a code unit to be compiled in a PLC program code to be compiled;
the space allocation module is used for allocating the memory space of the memory type represented by the memory layout mode to the variable of the code unit according to the preset memory layout mode corresponding to the unit type;
and the code generation module is used for generating a WebAssembly instruction code corresponding to the code unit according to the memory type of the allocated memory space, and the WebAssembly instruction code can access the variable of the code unit by using the allocated memory space of the memory type in the executed process.
15. An electronic device comprising a processor and a machine-readable storage medium storing machine-executable instructions executable by the processor, the processor being caused by the machine-executable instructions to: carrying out the method of any one of claims 1 to 13.
16. A computer-readable storage medium, in which a computer program is stored which, when being executed by a processor, carries out the method of any one of claims 1 to 13.
CN202210374707.3A 2022-04-11 2022-04-11 Method and device for compiling PLC program code into WebAssembly code Active CN114661300B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202210374707.3A CN114661300B (en) 2022-04-11 2022-04-11 Method and device for compiling PLC program code into WebAssembly code

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202210374707.3A CN114661300B (en) 2022-04-11 2022-04-11 Method and device for compiling PLC program code into WebAssembly code

Publications (2)

Publication Number Publication Date
CN114661300A CN114661300A (en) 2022-06-24
CN114661300B true CN114661300B (en) 2022-10-18

Family

ID=82035532

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202210374707.3A Active CN114661300B (en) 2022-04-11 2022-04-11 Method and device for compiling PLC program code into WebAssembly code

Country Status (1)

Country Link
CN (1) CN114661300B (en)

Families Citing this family (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN115167265B (en) * 2022-07-07 2023-03-24 北京万昇智能科技有限公司 PLC application package generation method, PLC application package operation method and related device
CN115982784B (en) * 2023-03-21 2023-09-12 紫光同芯微电子有限公司 Method and device for calling function by WebAsssembly module

Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
GB201708974D0 (en) * 2017-06-06 2017-07-19 Canon Kk Method and apparatus for executing a scripting language
CN110198479A (en) * 2019-05-24 2019-09-03 浪潮软件集团有限公司 A kind of browser audio/video decoding playback method based on webassembly
WO2021175053A1 (en) * 2020-03-05 2021-09-10 支付宝(杭州)信息技术有限公司 Method and apparatus for executing functional module in virtual machine

Family Cites Families (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US11341192B2 (en) * 2020-01-28 2022-05-24 Sap Se Cross platform collaborative document management system
US11461508B2 (en) * 2020-05-28 2022-10-04 Izuma Tech, Inc. Memory protection unit boundary partitioning based on compiled software objects

Patent Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
GB201708974D0 (en) * 2017-06-06 2017-07-19 Canon Kk Method and apparatus for executing a scripting language
CN110198479A (en) * 2019-05-24 2019-09-03 浪潮软件集团有限公司 A kind of browser audio/video decoding playback method based on webassembly
WO2021175053A1 (en) * 2020-03-05 2021-09-10 支付宝(杭州)信息技术有限公司 Method and apparatus for executing functional module in virtual machine

Also Published As

Publication number Publication date
CN114661300A (en) 2022-06-24

Similar Documents

Publication Publication Date Title
CN114661300B (en) Method and device for compiling PLC program code into WebAssembly code
US10089119B2 (en) API namespace virtualization
US4953084A (en) Method and apparatus using variable ranges to support symbolic debugging of optimized code
CN114721667B (en) Method and device for compiling ST code into WebAssembly code
KR101059633B1 (en) Heap configuration for multitasking virtual machines
US8984231B2 (en) Methods and apparatus to perform adaptive pre-fetch operations in managed runtime environments
CN108762751A (en) A kind of logic figure configuration and the method and system for changing into data flow
CN104965687A (en) Big data processing method and apparatus based on instruction set generation
CN100504761C (en) Method for mapping debugging information in system of different kinds
US10459702B2 (en) Flow control for language-embedded programming in general purpose computing on graphics processing units
WO2019105565A1 (en) Systems for compiling and executing code within one or more virtual memory pages
CN112631656A (en) Intelligent contract optimization method and device based on source code
Li et al. Fast data-dependence profiling by skipping repeatedly executed memory operations
CN110333870B (en) Simulink model variable distribution processing method, device and equipment
CN116113927A (en) Method and apparatus for reusable and relatively indexed register resource allocation in function calls
CN117251234B (en) Function calling method and device based on patches
Strobel et al. A backend tool for the integration of memory optimizations into embedded software
Zhuykov et al. Ahead-of-time compilation of JavaScript programs
US11755300B1 (en) Systems and methods for array structure processing
KR100884926B1 (en) Method for utilizing legacy shared libraries on VM-less embedded systems
CN112306632B (en) Java Card virtual machine execution engine device and execution method
US7424596B2 (en) Code interpretation using stack state information
von Ronne et al. Interpreting programs in static single assignment form
Chaflekar et al. Compiler Architecture and Design Issues
Ziegler WebAssembly Code Generation from Java Bytecode

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
GR01 Patent grant
GR01 Patent grant