DEV Community

Murahashi [Matt] Kenichi
Murahashi [Matt] Kenichi

Posted on • Updated on

8cc.wasi: Pure WASI C Compiler

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;
}
Enter fullscreen mode Exit fullscreen mode

8cc.wasi compiles C code into ELVM IR (EIR).

$ cat hello.c | wasmtime 8cc.c.eir.wasi > hello.eir
Enter fullscreen mode Exit fullscreen mode
$ 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)
Enter fullscreen mode Exit fullscreen mode

elc.wasi compiles EIR into WASI.

$ (echo "wasi" && cat hello.eir) | wasmtime elc.c.eir.wasi > hello.wasi
Enter fullscreen mode Exit fullscreen mode
$ 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)
Enter fullscreen mode Exit fullscreen mode

Run hello.wasi and get a result!

$ wasmtime hello.wasi 
Hello, world!
Enter fullscreen mode Exit fullscreen mode

Refs

Top comments (0)