Skip to content
This repository has been archived by the owner on Aug 16, 2018. It is now read-only.
/ wasm Public archive

Haskell compiler infastructure for WebAssembly

License

Notifications You must be signed in to change notification settings

sdiehl/wasm

Repository files navigation

Haskell WebAssembly

Build Status

A portable machine architecture for the web.

Define a portable, size- and load-time-efficient binary format to serve as a compilation target which can be compiled to execute at native speed by taking advantage of common hardware capabilities available on a wide range of platforms, including mobile and IoT.

Binary Protocol

Chrome

As of Chrome 56 the flag #enable-webassembly can be enabled to add support for WebAssembly.

Firefox

Goals

  • Parse WebAssembly textual AST
  • Read binary format
  • Emit binary format
  • Pretty print textual AST
  • Verify soundness of code
  • Interpreter
  • High level code generation API for retargeting
  • Translation to LLVM IR backend

Build with stack (recommended)

$ git clone https://github.com/sdiehl/wasm
$ cd wasm
$ stack install
$ wasm test/fac.wast

Build with cabal

$ git clone https://github.com/sdiehl/wasm
$ cd wasm

$ cabal sandbox init
$ cabal install

How to Run Test Suite

$ stack test

How to Run inside GHCi

$ stack ghci wasm

Building v8

In Chrome:

WebAssembly.compile(new Uint8Array(
 `00 61 73 6d  0d 00 00 00  01 09 02 60  00 00 60 01
7f 01 7f 03  03 02 00 01  05 04 01 00  80 01 07 07
01 03 66 6f  6f 00 01 08  01 00 0a 3a  02 02 00 0b
35 01 01 7f  20 00 41 04  6c 21 01 03  40 01 01 01
0b 03 7f 41  01 0b 20 01  41 e4 00 6c  41 cd 02 20
01 1b 21 01  41 00 20 01  36 02 00 41  00 21 01 41
00 28 02 00  0f 0b 0b 0e  01 00 41 00  0b 08 00 00
00 00 2c 00  00 00`.split(/[\s\r\n]+/g).map(v => parseInt(v, 16))
)).then(mod => {
 let m = new WebAssembly.Instance(mod)
 console.log('foo(1) =>', m.exports.foo(1))
 console.log('foo(2) =>', m.exports.foo(2))
 console.log('foo(3) =>', m.exports.foo(3))
})

In V8:

$ git clone [email protected]:v8/v8.git
$ make native wasm=on

$ out/native/v8 --expose-wasm
V8 version 4.9.0 (candidate)
d8> buffer = readbuffer('test.bin');
[object ArrayBufferr
d8> module = WASM.instantiateModule(buffer, {});
{memory: [object ArrayBuffer], test: function test() { [native code] }}
d8> module.test()
3

Codebase

Core modules

Example

(module

  ;; Recursive factorial
  (func (param i64) (result i64)
    (if_else (i64.eq (get_local 0) (i64.const 0))
      (i64.const 1)
      (i64.mul (get_local 0) (call 0 (i64.sub (get_local 0) (i64.const 1))))
    )
  )

)
(module
  (export "test" 0)
  (func (result i32)
    (i32.add (i32.const 1) (i32.const 2))))

Will generate

0000000 0101 0100 0102 0009 1500 0000 0500 4000
0000010 0109 0209 7406 7365 0074               
000001a

About

Haskell compiler infastructure for WebAssembly

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •