|
| 1 | +/* |
| 2 | + * Wasm3 - high performance WebAssembly interpreter written in C. |
| 3 | + * Copyright © 2020 Volodymyr Shymanskyy, Steven Massey. |
| 4 | + * All rights reserved. |
| 5 | + */ |
| 6 | + |
| 7 | +#include <wasm3.h> |
| 8 | +#include <m3_env.h> |
| 9 | + |
| 10 | +/* |
| 11 | + * Configuration |
| 12 | + */ |
| 13 | +#define FIB_ARG "24" |
| 14 | +#define WASM_STACK_SLOTS 1024 |
| 15 | + |
| 16 | +/* |
| 17 | + * WebAssembly app (recursive Fibonacci) |
| 18 | + */ |
| 19 | + |
| 20 | +unsigned char fib_wasm[] = { |
| 21 | + 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x06, 0x01, 0x60, |
| 22 | + 0x01, 0x7f, 0x01, 0x7f, 0x03, 0x02, 0x01, 0x00, 0x07, 0x07, 0x01, 0x03, |
| 23 | + 0x66, 0x69, 0x62, 0x00, 0x00, 0x0a, 0x1f, 0x01, 0x1d, 0x00, 0x20, 0x00, |
| 24 | + 0x41, 0x02, 0x49, 0x04, 0x40, 0x20, 0x00, 0x0f, 0x0b, 0x20, 0x00, 0x41, |
| 25 | + 0x02, 0x6b, 0x10, 0x00, 0x20, 0x00, 0x41, 0x01, 0x6b, 0x10, 0x00, 0x6a, |
| 26 | + 0x0f, 0x0b |
| 27 | +}; |
| 28 | + |
| 29 | +/* |
| 30 | + * Engine start, liftoff! |
| 31 | + */ |
| 32 | + |
| 33 | +#define FATAL(func, msg) { Serial.print("Fatal: " func " "); Serial.println(msg); return; } |
| 34 | +#define TSTART() { tstart = micros(); } |
| 35 | +#define TFINISH(s) { tend = micros(); Serial.print(s " in "); Serial.print(tend-tstart); Serial.println(" us"); } |
| 36 | + |
| 37 | +void wasm_task(void*) |
| 38 | +{ |
| 39 | + uint32_t tend, tstart; |
| 40 | + TSTART(); |
| 41 | + |
| 42 | + M3Result result = m3Err_none; |
| 43 | + |
| 44 | + IM3Environment env = m3_NewEnvironment (); |
| 45 | + if (!env) FATAL("NewEnvironment", "failed"); |
| 46 | + |
| 47 | + IM3Runtime runtime = m3_NewRuntime (env, WASM_STACK_SLOTS, NULL); |
| 48 | + if (!runtime) FATAL("NewRuntime", "failed"); |
| 49 | + |
| 50 | + IM3Module module; |
| 51 | + result = m3_ParseModule (env, &module, fib_wasm, sizeof(fib_wasm)-1); |
| 52 | + if (result) FATAL("ParseModule", result); |
| 53 | + |
| 54 | + result = m3_LoadModule (runtime, module); |
| 55 | + if (result) FATAL("LoadModule", result); |
| 56 | + |
| 57 | + IM3Function f; |
| 58 | + result = m3_FindFunction (&f, runtime, "fib"); |
| 59 | + if (result) FATAL("FindFunction", result); |
| 60 | + |
| 61 | + TFINISH("Init"); |
| 62 | + |
| 63 | + Serial.println("Running fib(" FIB_ARG ")..."); |
| 64 | + |
| 65 | + TSTART(); |
| 66 | + |
| 67 | + const char* i_argv[2] = { FIB_ARG , NULL }; |
| 68 | + result = m3_CallWithArgs (f, 1, i_argv); |
| 69 | + |
| 70 | + TFINISH("Done"); |
| 71 | + |
| 72 | + if (result == m3Err_none) { |
| 73 | + uint32_t value = *(uint32_t*)(runtime->stack); |
| 74 | + Serial.print("Result: "); |
| 75 | + Serial.println(value); |
| 76 | + } else { |
| 77 | + M3ErrorInfo info; |
| 78 | + m3_GetErrorInfo (runtime, &info); |
| 79 | + Serial.print("Error: "); |
| 80 | + Serial.print(result); |
| 81 | + Serial.print(" ("); |
| 82 | + Serial.print(info.message); |
| 83 | + Serial.println(")"); |
| 84 | + if (info.file && strlen(info.file) && info.line) { |
| 85 | + Serial.print("At "); |
| 86 | + Serial.print(info.file); |
| 87 | + Serial.print(":"); |
| 88 | + Serial.println(info.line); |
| 89 | + } |
| 90 | + } |
| 91 | +#ifdef ESP32 |
| 92 | + vTaskDelete(NULL); |
| 93 | +#endif |
| 94 | +} |
| 95 | + |
| 96 | +void setup() |
| 97 | +{ |
| 98 | + Serial.begin(115200); |
| 99 | + delay(100); |
| 100 | + |
| 101 | + // Wait for serial port to connect |
| 102 | + // Needed for native USB port only |
| 103 | + while(!Serial) {} |
| 104 | + |
| 105 | + Serial.println("\nWasm3 v" M3_VERSION " (" M3_ARCH "), build " __DATE__ " " __TIME__); |
| 106 | + |
| 107 | +#ifdef ESP32 |
| 108 | + // On ESP32, we can launch in a separate thread (with 16Kb stack) |
| 109 | + Serial.println("Running a separate task"); |
| 110 | + xTaskCreate(&wasm_task, "wasm3", 16*1024, NULL, 5, NULL); |
| 111 | +#else |
| 112 | + wasm_task(NULL); |
| 113 | +#endif |
| 114 | +} |
| 115 | + |
| 116 | +void loop() |
| 117 | +{ |
| 118 | + delay(100); |
| 119 | +} |
0 commit comments