8cc.wasi is a Pure WebAssembly System Interface (WASI) C Compiler.
Compile C Code
Prepare your C code. Following is a 'Hello world' example.
int putchar(int x);
int main() {
  const char* p = "Hello, world!\n";
  for (; *p; p++)
    putchar(*p);
  return 0;
}
8cc.wasi compiles C code into ELVM IR (EIR).
$ cat hello.c | wasmtime 8cc.c.eir.wasi > hello.eir
$ cat hello.eir
    .text
main:
    #{push:main}
    mov D, SP
    add D, -1
    store BP, D
    mov SP, D
    mov BP, SP
    sub SP, 1
(snip)
elc.wasi compiles EIR into WASI.
$ (echo "wasi" && cat hello.eir) | wasmtime elc.c.eir.wasi > hello.wasi
$ cat hello.wasi
(module
 (import "wasi_unstable" "fd_write" (func $__wasi_fd_write (param i32 i32 i32 i32) (result i32)))
 (import "wasi_unstable" "fd_read" (func $__wasi_fd_read (param i32 i32 i32 i32) (result i32)))
 (import "wasi_unstable" "proc_exit" (func $__wasi_proc_exit (param i32)))
 (memory (export "memory") 1025)
 (global $a (mut i32) (i32.const 0))
 (global $b (mut i32) (i32.const 0))
(snip)
Run hello.wasi and get a result!
$ wasmtime hello.wasi 
Hello, world!
 
![Murahashi [Matt] Kenichi](https://media2.dev.to/dynamic/image/width=50,height=50,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F46600%2F57894017-7a1c-406c-ba80-eda061b07d64.jpeg)
 
    
Top comments (0)