WebAssembly compiling method oriented to Cangjie programming language

By combining improved methods of the LLVM compiler and the Emscripten compiler, the problem of the Cangjie programming language being unable to be compiled into WebAssembly was solved, enabling the porting and efficient compilation of the Cangjie project on the Web platform and establishing a highly maintainable ecosystem.

CN121560334APending Publication Date: 2026-02-24HUNAN UNIV
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202511753301.6
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-11-26
Publication Date
2026-02-24

AI Technical Summary

Technical Problem

Currently, there are no tools that support compiling the source code of the Cangjie programming language into WebAssembly, and Emscripten cannot directly process the LLVM IR compiled by Cangjie.

Method used

By combining the advantages of the LLVM compiler and the Emscripten compiler, a new LLVM Pass is added to handle the LLVM built-in functions unique to Cangjie, and the LLVM backend code of the Emscripten compiler is modified to compile the LLVM IR of the Cangjie programming language into WebAssembly format.

Benefits of technology

It provides the ability to port the Cangjie programming language project to the Web platform, establishing a highly maintainable WebAssembly ecosystem that requires only minor modifications to existing code.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121560334A_ABST
    Figure CN121560334A_ABST
Patent Text Reader

Abstract

The invention discloses a WebAssembly compiling method for Cangjie language. The method comprises the following steps: firstly, compiling a Cangjie source code and a standard library code into LLVM IR by using the front end of a Cangjie compiler, and optimizing an opt tool of a modified Cangjie LLVM; a built-in function which cannot be directly converted into a WebAssembly instruction is newly added and converted into Pass processing; an LLVM rear end of the Emscripten is modified so as to process a related LLVM IR instruction, and the processed IR is compiled into a WebAssembly target file; meanwhile, the modified Emscripten is used for compiling a Cangjie standard library and a C / C + + code during operation into a WebAssembly static library; then, linking the target file with the static library to form a JavaScript file and a WebAssembly file; and finally, the verification of the JavaScript file is operated through a Node.js or http website. According to the method, on the basis of LLVM and Emscripten, by adding or modifying LLVM Pass, transplantation of Cangjie projects to a Web platform is achieved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the Cangjie programming language, the LLVM compilation framework, and the Emscripten compiler toolchain, and particularly to a WebAssembly compilation method for the Cangjie programming language. Background Technology

[0002] Cangjie programming language is a next-generation general-purpose programming language developed by Huawei for all-scenario intelligence. It emphasizes native intelligence, inherently all-scenario capabilities, high performance, and strong security, supporting multiple programming paradigms including functional, imperative, and object-oriented programming. Its compilation process involves the Cangjie standard library, the Cangjie runtime, and the LLVM compilation framework.

[0003] LLVM is a modular, reusable compiler toolchain framework. It supports compilation of multiple programming languages, achieves cross-platform optimization through intermediate representations (IR), and generates efficient machine code. It is widely used in compilers such as Clang and Rust, balancing performance and flexibility.

[0004] WebAssembly is a low-level binary instruction format that provides a high-performance execution environment for high-level languages, enabling efficient execution within browsers. As a complement to JavaScript, it supports compilation of languages ​​such as C / C++ and Rust into its format, balancing near-native speed with web security and portability. It is widely used in high-performance web scenarios such as gaming and video processing.

[0005] Emscripten is a compiler toolchain that compiles C / C++ code into WebAssembly or JavaScript, allowing native applications to run in web environments such as browsers. Based on LLVM, it supports libraries such as SDL and OpenGL, simplifying the process of porting traditional native projects to the web. It is widely used in the development of high-performance web applications such as games and multimedia processing.

[0006] However, the current ecosystem for the Cangjie programming language lacks tools that can compile Cangjie source code into WebAssembly. While Emscripten can also compile LLVM IR into WebAssembly, it supports LLVM IR compiled from C / C++ code, not LLVM IR compiled from Cangjie code.

[0007] Therefore, supporting the compilation of the Cangjie programming language into WebAssembly will enrich the Cangjie programming language ecosystem and provide more assistance for porting Cangjie programming language projects to the Web. Summary of the Invention

[0008] The purpose of this invention is to provide a WebAssembly compilation method for the Cangjie programming language. This method combines the advantages of the LLVM compiler and the Emscripten compiler, providing the ability to port Cangjie programming language projects to the Web platform.

[0009] The objective of this invention is achieved through the following technical solution:

[0010] A WebAssembly compilation method for the Cangjie programming language, which specifically includes the following steps:

[0011] (1) First, use the compiler front-end of the Cangjie programming language to compile the user-written Cangjie language source code and the Cangjie code in the Cangjie standard library into LLVM IR, and then use the opt tool in the modified Cangjie LLVM to optimize the LLVM IR;

[0012] (2) Based on the LLVM IR obtained in step (1), add relevant conversion passes to handle built-in functions in the LLVM IR that cannot be directly converted into WebAssembly instructions;

[0013] (3) Modify the LLVM backend code of the Emscripten compiler to process the LLVM IR instructions in the LLVMIR obtained in step (2) that cannot be directly converted into WebAssembly instructions, and use the modified Emscripten compiler to compile the LLVM IR obtained in step (2) after the Pass process into a target file in WebAssembly platform format;

[0014] (4) Use the modified Emscripten compiler to compile the C / C++ code files in the Cangjie standard library and the Cangjie runtime (C / C++ code writing) into static library files in WebAssembly platform format;

[0015] (5) Use the modified Emscripten compiler to link the target file obtained in step (3) and the static library file obtained in step (4) together into a JavaScript file and a WebAssembly file;

[0016] (6) The JavaScript file obtained in step (5) can be run using Node.js runtime or by starting an HTTP website to verify whether it can be executed and the correctness of the execution result.

[0017] Furthermore, in step (1), modifying Cangjie LLVM specifically involves removing some passes that are no longer needed during the LLVM IR optimization process.

[0018] Furthermore, in step (2), the addition of the relevant conversion Pass specifically refers to the addition of a Pass that processes the LLVM built-in functions unique to Cangjie. This Pass converts the LLVM built-in functions unique to Cangjie into native LLVM IR instructions or calls to Cangjie runtime library functions.

[0019] Furthermore, in step (3), modifying the LLVM backend code of the Emscripten compiler specifically involves modifying the handling of the addrspacecast instruction in the WebAssembly backend of LLVM, that is, treating the instruction as a Nop instruction.

[0020] The beneficial effects of this invention are as follows:

[0021] This invention is a WebAssembly compilation method for the Cangjie programming language. It fully combines the advantages of the LLVM compiler and the Emscripten compiler. By adding an LLVM Pass, it provides special handling for LLVM built-in functions specific to the Cangjie programming language in LLVM IR. Simultaneously, by modifying the LLVM backend code of the Emscripten compiler, it treats unsupported LLVM IR instructions as supported ones, providing the ability to port Cangjie programming language projects to the Web platform. In this invention, only the user-written Cangjie language source code and the Cangjie code in the Cangjie standard library need to be modified specifically to compile the resulting LLVM IR. No other code needs to be modified, resulting in minimal changes to the compilation process. Subsequent maintenance only requires modifying the corresponding LLVM Pass. Furthermore, the modification to the Emscripten compiler's LLVM backend enables the compilation of Cangjie LLVM IR into WebAssembly, providing a highly maintainable WebAssembly ecosystem. Attached Figure Description

[0022] Figure 1 This is a flowchart of the WebAssembly compilation method for the Cangjie programming language. Detailed Implementation

[0023] To make the objectives, technical solutions, and effects of this invention clearer, the invention will be further described in detail below with reference to the accompanying drawings and preferred embodiments. It should be noted that the specific embodiments described herein are merely illustrative and not intended to limit the invention.

[0024] like Figure 1As shown, a WebAssembly compilation method for the Cangjie programming language is presented. This method first uses the Cangjie compiler frontend to compile the Cangjie source code and standard library code into LLVM IR, followed by optimization using the modified Cangjie LLVM opt tool. Next, a conversion pass is added to handle LLVM built-in functions that cannot be directly converted into WebAssembly instructions. Then, the Emscripten LLVM backend is modified to handle LLVM IR instructions that cannot be directly converted into WebAssembly instructions and compile them into WebAssembly object files. Simultaneously, the Cangjie standard library and runtime C / C++ code are compiled into a WebAssembly static library. Subsequently, the object files and static library are compiled into JavaScript and WebAssembly files. Finally, the JavaScript file is run and verified using Node.js or an HTTP website. The method specifically includes the following steps:

[0025] (1) First, use the compiler front-end of the Cangjie programming language to compile the user-written Cangjie language source code and the Cangjie code in the Cangjie standard library into LLVM IR, and then use the opt tool in the modified Cangjie LLVM to optimize the LLVM IR.

[0026] Different programming languages ​​have their own compiler front-end programs. For the Cangjie programming language, the official Cangjie compiler front-end program needs to be used to compile the Cangjie source code (files with the .cj extension) into LLVM IR (files with the .bc extension).

[0027] For the Cangjie LLVM project, some modifications are needed. Specifically, during the optimization process of the opt tool, only the CJSpecificOpt pass (including this pass) needs to be executed. Passes after CJSpecificOpt do not need to be executed.

[0028] The modified LLVM tool opt is used to optimize the LLVM IR generated by the Cangjie compiler frontend.

[0029] (2) Based on the LLVM IR obtained in step (1), add relevant conversion passes to handle built-in functions in the LLVM IR that cannot be directly converted to WebAssembly instructions. The specific processing flow of this LLVM pass is as follows:

[0030] Iterate through all functions in the modules of the LLVM IR file. For each function, iterate through all instructions in the function. For each instruction, determine whether it is an LLVM call instruction that calls a Cangjie-specific built-in function. If it is, convert it to a native LLVM instruction or a call instruction for a Cangjie runtime library function. If not, no processing is required.

[0031] (3) Modify the LLVM backend code of the Emscripten compiler to handle the LLVM IR instructions in the LLVMIR obtained in step (2) that cannot be directly converted into WebAssembly instructions, and use the modified Emscripten compiler to compile the LLVM IR obtained in step (2) after the Pass process into a target file in the WebAssembly platform format.

[0032] This step processes the addrspacecast instruction in LLVM IR, treating it as a NOP instruction, meaning that the pointer before and after the address space translation points to the same variable.

[0033] (4) Use the modified Emscripten compiler to compile the C / C++ code files in the Cangjie standard library and the Cangjie runtime (C / C++ code writing) into static library files in WebAssembly platform format.

[0034] The LLVM IR compiled in the previous steps involves calls to functions written in C / C++ code in the Cangjie standard library and calls to functions written in C / C++ code in the Cangjie runtime. Therefore, these C / C++ codes need to be compiled into static library files under the WebAssembly platform for use in subsequent linking. The modified Emscripten compiler in step (3) is used to compile these C / C++ code files into static library files under the WebAssembly platform.

[0035] (5) Use the modified Emscripten compiler to link the target file obtained in step (3) and the static library file obtained in step (4) together into a JavaScript file and a WebAssembly file.

[0036] The compilation method for this step is similar to that for step (4), except that step (4) only compiles without linking (i.e., the '-c' option needs to be added to the compilation command), while this step does not require this option.

[0037] (6) The JavaScript file obtained in step (5) can be run using Node.js runtime or by starting an HTTP website to verify whether it can be executed and the correctness of the execution result.

[0038] Running the generated JavaScript file (which instantiates the wasm module in the relevant WebAssembly file and calls its functions) in the Node.js runtime makes it easy to verify its execution and the correctness of the results in the command-line interface. Furthermore, the generated JavaScript file can also be run within an HTTP website; detailed steps can be found in the 'Running HTML files with emrun' section of the Emscripten official website.

[0039] It will be understood by those skilled in the art that the above descriptions are merely preferred examples of the invention and are not intended to limit the invention. Although the invention has been described in detail with reference to the foregoing examples, those skilled in the art can still modify the technical solutions described in the foregoing examples or make equivalent substitutions for some of the technical features. All modifications and equivalent substitutions made within the spirit and principles of the invention should be included within the scope of protection of the invention.

Claims

1. A WebAssembly compilation method for the Cangjie programming language, characterized in that, The method specifically includes the following steps: (1) First, use the compiler front-end of the Cangjie programming language to compile the user-written Cangjie language source code and the Cangjie code in the Cangjie standard library into LLVM IR, and then use the opt tool in the modified Cangjie LLVM to optimize the LLVM IR; (2) Based on the LLVM IR obtained in step (1), add relevant conversion passes to handle built-in functions in the LLVM IR that cannot be directly converted into WebAssembly instructions; (3) Modify the LLVM backend code of the Emscripten compiler to handle the LLVMIR instructions in the LLVMIR obtained in step (2) that cannot be directly converted into WebAssembly instructions, and use the modified Emscripten compiler to compile the LLVM IR obtained in step (2) after the Pass process into a target file in WebAssembly platform format; (4) Use the modified Emscripten compiler to compile the C / C++ code files in the Cangjie standard library and the Cangjie runtime (C / C++ code writing) into static library files in WebAssembly platform format; (5) Use the modified Emscripten compiler to link the target file obtained in step (3) and the static library file obtained in step (4) together into a JavaScript file and a WebAssembly file; (6) The JavaScript file obtained in step (5) can be run using Node.js runtime or by starting an HTTP website to verify whether it can be executed and the correctness of the execution result.

2. The WebAssembly compilation method for the Cangjie programming language according to claim 1, characterized in that, Based on the LLVM IR obtained by compiling the Cangjie source code using the Cangjie compiler front-end, the opt tool in Cangjie LLVM is modified to optimize this LLVM IR.

3. The WebAssembly compilation method for the Cangjie programming language according to claim 1, characterized in that, Based on the LLVM IR obtained by optimizing the modified Cangjie LLVM opt tool, relevant conversion passes have been added to handle built-in functions in this LLVM IR that cannot be directly converted into WebAssembly instructions.

4. The WebAssembly compilation method for the Cangjie programming language according to claim 1, characterized in that, Based on the LLVM IR obtained in step (2), modify the LLVM backend code of the Emscripten compiler to handle LLVM IR instructions that cannot be directly converted into WebAssembly instructions in this LLVM IR.

5. The WebAssembly compilation method for the Cangjie programming language according to claim 1, characterized in that, Using the modified Emscripten compiler, the C / C++ code files in the Cangjie standard library and the Cangjie runtime (written in C / C++) are compiled into static library files in WebAssembly platform format. The target file obtained in step (3) and the static library file obtained in step (4) are linked together into a JavaScript file and a WebAssembly file. The JavaScript file obtained in step (5) can be run using the Node.js runtime or by starting an HTTP website to verify whether it can be executed and the correctness of the execution result.