tyfkda / xcc

Standalone C compiler/assembler/linker/libc for x86-64/aarch64/riscv64/wasm
https://tyfkda.github.io/xcc/
MIT License
254 stars 17 forks source link

(optionally) export the stack pointer in `wcc` #110

Closed ghost closed 1 year ago

ghost commented 1 year ago

I am trying to implement coroutines (fibers) with the WebAssembly backend, alongside Binaryen’s Asyncify, and I now need to get information about the stack pointer so that I can copy the stack around. (See: WebAssembly/binaryen#5478)

I know this is a bit of a niche use‐case for this low‐level feature, so it is completely understandable if you don’t want to support it, but if it would be possible to export the stack pointer or add an option to export it, that would solve my problem entirely, and I’d really appreciate it!

Something along these lines (src/wcc/traverse.c):

 static void add_builtins(void) {
   // Stack pointer.
   {
     const Name *name = alloc_name(SP_NAME, NULL, false);
     Type *type = &tySize;
-    /*GVarInfo *info =*/ add_global_var(type, name);
+    GVarInfo *info = add_global_var(type, name);
+    info->export = true;
   }

Thank you for working on this project, I believe it is wonderful!

tyfkda commented 1 year ago

Hi @zamfofex , I'm not familiar with Binaryen and Asyncify, but it is good to enable asynchronous functionality in compiled code.

I think it's ok with above change and adding command line option. Please send a pull request when you are done.