-
Notifications
You must be signed in to change notification settings - Fork 10
Add import/export mutable globals #329
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
8df566f
7877ba9
eecb4c7
2d25e63
b60fd56
e423347
3b8c0df
bd29124
557d673
2dd47af
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -136,8 +136,12 @@ int resolve_isr(int pin) { | |
|
|
||
| #define ALL_PRIMITIVES (NUM_PRIMITIVES + NUM_PRIMITIVES_ARDUINO) | ||
|
|
||
| // Global index for installing primitives | ||
| #define NUM_GLOBALS 0 | ||
| #define ALL_GLOBALS NUM_GLOBALS | ||
|
|
||
| // Indices for installing primitives and globals | ||
| int prim_index = 0; | ||
| int global_index = 0; | ||
|
|
||
| /* | ||
| Private macros to install a primitive | ||
|
|
@@ -196,8 +200,25 @@ int prim_index = 0; | |
| #define arg8 get_arg(m, 8) | ||
| #define arg9 get_arg(m, 9) | ||
|
|
||
| #define def_glob(name, type, mut, init_value) \ | ||
| StackValue name##_sv{.value_type = type, init_value}; \ | ||
| Global name = { \ | ||
| .mutability = mut, .import_field = #name, .value = &name##_sv}; | ||
|
|
||
| #define install_global(global_name) \ | ||
| { \ | ||
| dbg_info("installing global: %s\n", #global_name); \ | ||
| if (global_index < ALL_GLOBALS) { \ | ||
| globals[global_index++] = (global_name); \ | ||
| } else { \ | ||
| FATAL("global_index out of bounds"); \ | ||
| } \ | ||
| } | ||
|
|
||
| // The primitive table | ||
| PrimitiveEntry primitives[ALL_PRIMITIVES]; | ||
| // The globals table | ||
| Global globals[ALL_GLOBALS]; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this array not always length 0 now? I think we need a dynamic array here since we only know how many globals will be exported at runtime (when loading the wasm module). Maybe I am missing something here.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes indeed, for now all these arrays of globals are length 0 (not only in arduino.cpp, but also in the emulated.cpp, idf.cpp and zephyr.cpp). These arrays are intended only for globals that are defined by the host, available for import within the Wasm module (those exported by the module are not handled here). Since the number of such host-defined globals is known at compile time this array size can be statically determined. Currently, there are no such globals, but it is possible to add such importable globals in the future (e.g. status flags provided by the host ...). On the contrary, the other, not imported globals in the module are indeed only known at runtime. Therefore, these globals are dynamically allocated during instantiation of the Wasm module (see To summarize, the host-defined (importable) globals are statically known (and therefore also their array size). The module-defined (exportable) globals are dynamically allocated during instantiation. |
||
|
|
||
| // | ||
| uint32_t param_arr_len0[0] = {}; | ||
|
|
@@ -1076,6 +1097,19 @@ bool resolve_external_memory(char *symbol, Memory **val) { | |
| return false; | ||
| } | ||
|
|
||
| bool resolve_external_global(char *symbol, Global **val) { | ||
| debug("Resolve external global for %s \n", symbol); | ||
|
|
||
| for (auto &global : globals) { | ||
| if (!strcmp(symbol, global.import_field)) { | ||
| *val = &global; | ||
| return true; | ||
| } | ||
| } | ||
| FATAL("Could not find global %s \n", symbol); | ||
| return false; | ||
| } | ||
|
|
||
| //------------------------------------------------------ | ||
| // Restore external state when restoring a snapshot | ||
| //------------------------------------------------------ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think the globals are part of the primitives. Can we move this to the interpreter or the WARDuino class? Now it will only get loaded on Arduino.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Globals can be defined by the host (e.g. status flags). These globals can then be imported in the Wasm module. As mentioned in the other comment, the number of host-defined globals is statically known. Similar to the host-defined function primitives, this host-defined globals functionality is added not only for
arduino.cpp, but also foremulated.cpp,idf.cppandzephyr.cpp. These platforms should not necessarily have the exact same host-defined globals.Did I understand your comment correctly?
P.S. The
NUM_GLOBALS 1is incorrect and should beNUM_GLOBALS 0. It is a leftover from me testing the functionality of these host-defined globals. EDIT: corrected in 2dd47af.