A WebAssembly Compilation Method for Resource-Constrained Internet of Things Devices

By adopting streaming backtracking AOT compilation method and memory optimization technology on resource-constrained IoT devices, WebAssembly's low execution efficiency on these devices is solved, and efficient machine code generation and execution is achieved.

CN115033242BActive Publication Date: 2025-06-27ZHEJIANG UNIV
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202210563610.7
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-05-23
Publication Date
2025-06-27
Estimated Expiration
2042-05-23

AI Technical Summary

Technical Problem

The prior art is difficult to efficiently execute WebAssembly bytecode on resource-constrained IoT devices, especially because WebAssembly's Ahead-of-time (AOT) compilation method cannot be performed on these devices.

Method used

An optimized streaming backtracking AOT compilation method is adopted to compile WebAssembly bytecode into machine code one by one, mark the uncertain address through pseudo-address, and replace the address after the initial compilation is completed, adjust the memory allocation to save RAM space, and transfer constants to Flash to further save memory.

Benefits of technology

It realizes efficient operation of WebAssembly bytecode on resource-constrained IoT devices, generates efficient machine code, improves execution speed, and simplifies the compilation process of high-level languages ​​on devices.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115033242B_ABST
    Figure CN115033242B_ABST
Patent Text Reader

Abstract

A WebAssembly compilation method for resource-constrained Internet of Things devices, comprising: sequentially compiling WebAssembly bytecodes in units of single instructions. When compiling an indeterminate address in a control flow instruction, first replace it with a pseudo-address and make a mark, and at the same time record the stack depth at the entrance of each control flow instruction. After one compilation, replace the previous pseudo-address with the actual physical address determined by subsequent compilation, and restore the stack depth to an appropriate value; then optimize the RAM, set aside a.wait memory segment in the RAM space to save useful data structures generated by compilation, and change the heap parameters stored therein after compilation to expand the size of the heap, which is used as the linear memory space during instruction execution; optimize constants, transfer constants from the RAM to the Flash to save memory resources, and establish a linear memory jump table in the.wait memory segment to establish a mapping between the logical address of the constant RAM and the actual address of the Flash. The present invention can generate efficient machine codes on resource-constrained Internet of Things devices.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to Internet of Things technology, and particularly to a WebAssembly compilation method for resource-constrained Internet of Things devices. Background Art

[0002] WebAssembly is a bytecode format designed to run on various platforms. It can also be used as a common compilation target for various high-level languages (such as Rust, C++), and exhibits better runtime performance than other general-purpose languages.

[0003] Currently, there are still relatively large challenges in executing WebAssembly bytecode on resource-constrained Internet of Things devices. Existing technologies (such as Wasm3) have built an interpreter for WebAssembly to achieve the purpose of running WebAssembly bytecode on low-end Internet of Things devices, but its execution speed is more than ten times slower than that of machine code. The Ahead-of-time (AOT) compilation method of WebAssembly can improve the execution speed by converting bytecode into machine code at load time. However, the AOT of WebAssembly cannot be performed on resource-constrained Internet of Things devices. Summary of the Invention

[0004] The present invention aims to overcome the above-mentioned drawbacks of the prior art and provides a WebAssembly compilation method for resource-constrained Internet of Things devices.

[0005] To achieve the above objectives, the solution adopted by the present invention is: a WebAssembly compilation method for resource-constrained Internet of Things devices, comprising the following steps:

[0006] (1) Perform AOT compilation on WebAssembly bytecode in a streaming backtracking manner;

[0007] (1.1) Compile WebAssembly bytecode sequentially in units of single instructions;

[0008] (1.2) Mark the uncertain addresses during the compilation process with pseudo addresses;

[0009] (1.3) Record the stack depth of each entry when compiling control flow instructions;

[0010] (1.4) Replace the pseudo addresses generated in (1.2) with actual physical addresses after initially completing the compilation of the entire bytecode;

[0011] (1.5) Restore the stack depth to an appropriate value after initially completing the compilation of the entire bytecode;

[0012] (2) Adjust the memory after compilation is completed;

[0013] (2.1) Divide the RAM into a.wait memory segment for storing useful data structures;

[0014] (2.2) Store heap-related parameters (starting address, length, etc.) in.wait;

[0015] (2.3) Modify the parameters in (2.2) to change the length of the heap after step 1 is completed;

[0016] (2.4) Store runtime data with the heap as a linear memory space;

[0017] (3) Remap constants to save RAM space;

[0018] (3.1) Transfer constants to the relatively more abundant flash;

[0019] (3.2) Define a linear memory jump table in the.wait segment to map the constant addresses in the original linear memory space to the transferred constant flash addresses one by one;

[0020] The present invention uses an optimized pre-runtime compilation to translate WebAssembly bytecode into machine code. Different from the existing pre-runtime compilation methods, the present invention does not load all the bytecodes to be compiled into the memory at one time, but uses a streaming compilation with multiple partial loads. At the same time, for the problem of uncertain addresses generated during the compilation process, the present invention proposes a strategy of backtracking compilation, first marking the uncertain addresses and then determining the marked addresses after one compilation is completed. After the compilation is completed, the present invention also expands the dynamic runtime space by adjusting the RAM memory space allocation.

[0021] The advantages of the present invention are: it can be used in any resource-constrained Internet of Things devices to generate machine code that runs efficiently on such devices, and has strong usability; with the support of the present invention, any user can choose to use a high-level language to write device code without considering the problem of running efficiency on the device. BRIEF DESCRIPTION OF THE DRAWINGS

[0022] In order to more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the following will briefly introduce the drawings required for use in the description of the embodiments or the prior art. Obviously, the following drawings are only some embodiments of the present invention. For those of ordinary skill in the art, other drawings can be obtained based on these drawings without creative efforts.

[0023] Figure 1 is the working flowchart of this method.

[0024] Figure 2 It is the storage space structure diagram of this method. Specific implementation manners

[0025] The present invention will be further described below with reference to the accompanying drawings. A WebAssembly compilation method for resource-constrained Internet of Things devices according to the present invention has the following specific implementation manners:

[0026] (1) Perform AOT compilation on the WebAssembly bytecode in a streaming backtracking manner, that is, compile it in units of single instructions, mark the parts that cannot be determined temporarily, and supplement the corresponding parts after the initial compilation is completed;

[0027] (1.1) Compile the WebAssembly bytecode into machine code that can be directly run by the Internet of Things device in sequence in units of single instructions;

[0028] (1.2) In the control flow instructions involving jumps to target addresses, the target addresses may not have been compiled yet. Use pseudo-addresses to mark these uncertain addresses during the compilation process;

[0029] (1.3) Record the stack depth of each entry when compiling the control flow instructions;

[0030] (1.4) Record the physical address corresponding to each marked pseudo-address during compilation, and replace the pseudo-addresses generated in (1.2) with actual physical addresses after the initial compilation of the entire bytecode is completed;

[0031] (1.5) After the initial compilation of the entire bytecode is completed, restore the stack depth to an appropriate value. When the length to be restored is 1, use the pop instruction. When the restored length exceeds 1, directly modify the value of the SP register;

[0032] (2) Adjust the memory after compilation;

[0033] (2.1) On the basis of the original.data and.bss memory segments, further divide the RAM into a.wait memory segment, and save the data structures to be used later during compilation. Data that can be optimized later is not placed in this memory segment;

[0034] (2.2) Store heap-related parameters (starting address, length, etc.) in.wait;

[0035] (2.3) After completing step 1, modify the parameters in (2.2) to change the length of the heap, directly occupy the space of the original.data and.bss memory segments, and form a complete heap space;

[0036] (2.4) Store the data during runtime in the heap as a linear memory space;

[0037] (3) Remap constants to save RAM space;

[0038] (3.1) Transfer constants to the relatively more abundant flash space;

[0039] (3.2) Define a linear memory jump table in the.wait section to map the constant addresses in the original linear memory space to the transferred constant flash addresses one by one. When an instruction is executed, the actual address of the constant in the flash is found by looking up the linear memory jump table;

[0040] A WebAssembly compilation method for resource-constrained Internet of Things devices according to the present invention compiles WebAssembly bytecodes one by one according to a single instruction. When compiling an indefinite address in a control flow instruction, a pseudo-address is used to replace it first and marked. At the same time, the stack depth at the entrance of each control flow instruction is recorded. After one compilation, the previous pseudo-address is replaced with the actual physical address determined by subsequent compilation, and the stack depth is restored to an appropriate value; then the RAM is optimized. A.wait memory segment is set aside in the RAM space to save useful data structures generated by compilation. After compilation is completed, the heap parameters stored therein are changed to expand the size of the heap, which is used as the linear memory space during instruction execution; the constants are optimized. The constants are transferred from the RAM to the Flash to save memory resources, and a linear memory jump table is established in the.wait memory segment to establish the mapping between the logical address of the constant in the RAM and the actual address in the Flash.

[0041] The content described in the embodiments of this specification is only an enumeration of the implementation forms of the inventive concept. The protection scope of the present invention should not be regarded as limited to the specific forms stated in the embodiments. The protection scope of the present invention also extends to equivalent technical means that can be conceived by those skilled in the art according to the inventive concept.

Claims

1. A WebAssembly compilation method for resource-constrained Internet of Things devices, comprising the following steps: (1) Perform AOT compilation on WebAssembly bytecode in a streaming backtracking manner (1.1) Compile WebAssembly bytecode sequentially in units of single instructions; (1.2) Mark the uncertain addresses during the compilation process with pseudo-addresses; (1.3) Record the stack depth of each entry when compiling control flow instructions; (1.4) After initially completing the compilation of the entire bytecode, replace the pseudo-addresses generated in (1.2) with actual physical addresses; (1.5) After initially completing the compilation of the entire bytecode, restore the stack depth to an appropriate value; (2) Adjust the memory after compilation; (2.1) Divide the RAM into a.wait memory segment for storing useful data structures; (2.2) Store heap-related parameters (starting address, length, etc.) in.wait; (2.3) Modify the parameters in (2.2) to change the heap length after completing step 1; (2.4) Store runtime data with the heap as a linear memory space; (3) Remap constants to save RAM space; (3.1) Transfer constants to the relatively abundant flash; (3.2) By defining a linear memory jump table in the.wait segment, map the constant addresses in the original linear memory space to the transferred constant flash addresses one by one.

Citation Information

Patent Citations

  • Neural network compiling method for storage and calculation integrated platform

    CN112465108A

  • Debugging method, debugging equipment and debugging system for smart contract

    CN114327776A