Bug #35

dynamic WASM core

Added by Kevin O'Dwyer 7 months ago. Updated 3 months ago.

Target version:
Start date:
Due date:
% Done:


Estimated time:


Investigate further a dynamic core based on the ARM cores.

It should build emscripten WASM side modules on demand
Useful to start with POC
Attached is an example of a toy CPU first implemented in javascript and a port to C.
The C version is enough to load side modules on demand. It does not yet construct blocks dynamically.


compiled.html (7.54 KB) compiled.html Kevin O'Dwyer, 08/02/2020 12:27 PM
main.c (10.5 KB) main.c Kevin O'Dwyer, 08/02/2020 12:55 PM
main.c (15.2 KB) main.c Kevin O'Dwyer, 08/16/2020 06:29 AM (2.67 KB) Kevin O'Dwyer, 12/12/2020 01:21 PM



Updated by Kevin O'Dwyer 7 months ago

  • File deleted (main.c)

Updated by Kevin O'Dwyer 7 months ago

attach correct c file


Updated by Kevin O'Dwyer 7 months ago

Updated main.c attached
This demonstrates building up WASM modules on the fly. The modules are not compiled from source, but stitched together from binary fragments.
2 blocks
- 1 consists of 3 instructions
- 1 consists of 6 instructions

This was an experiment.
Learnings - the WASM format has offsets in the various structure. Therefore sticking together fragments is not straight forward (see blockHack bool var). This was necessary because while both compiled blocks contained STORE_EAX, the binary for the instruction is different by an offset amount.
This seems unfortunate. It may be partly a result of the compiler producing the output.

The toy emulator executes a number of loops.
Using the following values:
char stack[stackLength] = {1, 0, 11, 1, 101, 1, 101, 1, 101};

Without the compiler execution takes 20 seconds
exe.html:1 opCounter= 1100961661 compiled calls=0
exe.js:2169 finish at Sun Aug 16 2020 13:26:49 GMT+0100 (British Summer Time)

With compiler it takes 37 seconds!
exe.html:1 hello from compile hash=1880050
exe.html:1 hello from compile hash=748804039
exe.html:1 opCounter= 495129685 compiled calls=136809012


Updated by Kevin O'Dwyer 3 months ago

I came across the CHASM language. This is a simple toy language and associated compiler that produces WASM output:
I ported this to java here:

It is enough to handle variable assignment, numeric operations, bracket scope, if/while statements and produce a valid WASM module.
It was done with the view to produce the dynamic blocks required for the attached toy emulator.

To that end i re-worked the java code from here:
to something that would integrate into the 'poor' c code used in the toy emulator
Attached as

Blockers for making progress:
1. As the intention is to use emscripten SIDE_MODULES the WASM module is a lot more complicated then the built-from-scratch WASM module.
- It seems like a better idea would be to use a binary template for a SIDE_MODULE and splice in the 'code' section.
2. The Chasm code uses f32 for numeric operations. The emulator use char and char * which would transate to i32 and the use of shl, shr_s as well as array indexing.

Also available in: Atom PDF