Just-In-Time Compiled WebAssembly on Microcontrollers
Summary
WebAssembly (WASM) is increasingly used as a standardized, virtual RISC instruction set outside of web applications. WASM's architecture is designed to be platform-independent: instead of having complex instructions that perform tasks such as calling JavaScript or modifying a website, WASM applications call generic functions offered by the environment (aka embedder). The Java virtual machine might share the most similarities with WebAssembly: they are both virtual bytecode formats that different source languages get compiled to, they have a number of execution engines for different platforms, and they aim to have better safety guarantees (e.g. no segmentation faults) than machine code.
A reoccurring problem with using the JVM for new use-cases is that it is a complex instruction set architecture. There are operations that perform very complex – and thus hard to implement – tasks. For example, the invokeinterface opcode makes no sense other than for dynamically dispatched, object-oriented languages. Implementing these operations comes at a cost: the memory requirements of the JVM are notoriously high, which makes it ill-suited for constrained devices, such as microcontrollers. The outcome is that for the most part these devices still rely on natively compiled code, usually written in C, which has to be device-specific and doesn't offer any safety guarantees.
In this Bachelor's thesis and paper I look at common dynamic Just-In-Time compilation techniques used widely in interpreted languages such as Java, Scheme, SmallTalk, or JavaScript and evaluate them for use in a low-resource, microcontroller compatible WASM runtime. Such dynamic optimizations are accredited for removing much of the overhead of dynamic dispatch and object-orientation in widely used languages like Java, even being able to surpass the performance of native C code in some applications. With energy- and memory-efficiency being extremely important to applications running in microcontrollers, these techniques could make WebAssembly a viable alternative to bare-metal C code.