Giter Club home page Giter Club logo

wasmcodeexplorer's Introduction

Build Status

WebAssembly SDK

A toolkit for creating WebAssembly modules.

Installing Wasdk

Installing from NPM

npm install wasdk

Installing from GitHub

This is recommended if you want to contribute to the wasdk project.

Clone this repository and run:

npm run installdeps

and to build sources:

npm run build

To download all necessary pre-compiled binaries for your operating system, which includes: Emscripten, Binaryen, SpiderMonkey, LLVM and Clang:

wasdk sdk --install

And clean up (built sourced and download binaries):

npm run clean

Usage

Wasdk includes a variety of tools which accessible through the wasdk command:

Compiling Modules

wasdk ez test/malloc.json -o malloc.js

This producs several files:

  • malloc.asm.js: asm.js code
  • malloc.wasm: WebAssembly Binary
  • malloc.wast WebAssembly Text
  • malloc.js: Node / Browser runtime environment.

Running Modules

wasdk js dist/wasm-shell.js malloc.wasm

This creates a lightweight WebAssembly environemnt and runs the malloc.wasm file. At the moment, this only works with the malloc.wasm file.

Viewing WebAssembly Compiled Machine Code

Use the disassemble command to view the compiled machine code for a WebAssembly program. This code is generated by the ION compiler (used in the Firefox Web Browser). It should be fairly similar to the code produced by other browser engines.

wasdk disassemble test/universe.wast
(module
  (export "answer" (func $answer))
  (func $answer (result i32)
    (return (i32.const 42))
  )
)

Compiled x86/AMD64 Code:

Total Code Size: 5.00 Bytes
100.00%    5          Func 0:
Func 0:
  sub rsp, 8                                        ; 0x000050 48 83 ec 08
  mov eax, 0x2a                                     ; 0x000054 b8 2a 00 00 00
  nop                                               ; 0x000059 66 90
  add rsp, 8                                        ; 0x00005b 48 83 c4 08
  ret                                               ; 0x00005f c3

SDK Management

wasdk sdk --install
wasdk sdk --clean

Testing

npm test

wasmcodeexplorer's People

Contributors

mbebenita avatar yurydelendik avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

wasmcodeexplorer's Issues

Name Change

I'd suggest WasmBinaryExplorer as an alternate name. WasmCodeExplorer bugs me a ltitle becasue there are two 'e's following each other.

The x86 asm view cuts the emitted asm after the br_table

For the specific function https://goo.gl/EYwhBF, with the baseline JIT option enabled, it appears to me as if the asm listing is cut before the blocks for a br_table has been printed. I don't see any mov instructions that would correspond to the get_global and i32.load instructions in the blocks.

This C code...

#define CASE break;case

extern int A, B, C, D;

int f(int x) {
  int r = 0;
  switch (x) {
    CASE(0):      r = A;
    CASE(1):      r = B;
    CASE(2):      r = C;
    CASE(3):      r = D;
  }
  return r;
} 

...generates this wast...

(module
  (type $type0 (func (param i32) (result i32)))
  (import $global0 "env" "A" (global i32))
  (import $global1 "env" "B" (global i32))
  (import $global2 "env" "C" (global i32))
  (import $global3 "env" "D" (global i32))
  (table 0 anyfunc)
  (memory 1)
  (export "memory" memory)
  (export "_Z1fi" $func0)
  (func $func0 (param $var0 i32) (result i32)
    block $label4 block $label3 block $label2 block $label0
      get_local $var0
      i32.const 3
      i32.gt_u
      br_if $label0
      block $label1
        get_local $var0
        br_table $label1 $label2 $label3 $label4 $label1
      end $label1
      get_global $global0
      i32.load
      return
    end $label0
      i32.const 0
      return
    end $label2
      get_global $global1
      i32.load
      return
    end $label3
      get_global $global2
      i32.load
      return
    end $label4
    get_global $global3
    i32.load
  )
)

which generates this asm:

wasm-function[0]:
  sub rsp, 0x18                         ; 0x000000 48 83 ec 18
  mov qword ptr [rsp + 8], r14          ; 0x000004 4c 89 74 24 08
  mov rax, rsp                          ; 0x000009 48 8b c4
  add rax, 0                            ; 0x00000c 48 05 00 00 00 00
  cmp qword ptr [r14 + 0x28], rax       ; 0x000012 49 39 46 28
  jae 0xd1                              ; 0x000016 0f 83 b5 00 00 00
 0x00001c:                              
  mov dword ptr [rsp + 4], edi          ; 0x00001c 89 7c 24 04
  mov eax, dword ptr [rsp + 4]          ; 0x000020 8b 44 24 04
  cmp eax, 3                            ; 0x000024 83 f8 03
  ja 0x93                               ; 0x000027 0f 87 66 00 00 00
 0x00002d:                              
  mov eax, dword ptr [rsp + 4]          ; 0x00002d 8b 44 24 04
  cmp eax, 4                            ; 0x000031 83 f8 04
  jb 0x73                               ; 0x000034 0f 82 39 00 00 00
 0x00003a:                              
  jmp 0x81                              ; 0x00003a e9 42 00 00 00
 0x00003f:                              
  jmp 0x81                              ; 0x00003f e9 3d 00 00 00
 0x000044:                              
  jmp 0x9a                              ; 0x000044 e9 51 00 00 00
 0x000049:                              
  jmp 0xac                              ; 0x000049 e9 5e 00 00 00
 0x00004e:                              
  jmp 0xbe                              ; 0x00004e e9 6b 00 00 00

If I unselect the baseline jit option, I get this output instead (notice the four labels with 2 mov instructions each that are missing in the output above):

wasm-function[0]:
  sub rsp, 8                            ; 0x000000 48 83 ec 08
  cmp edi, 3                            ; 0x000004 83 ff 03
  ja 0x32                               ; 0x000007 0f 87 25 00 00 00
 0x00000d:                              
  mov eax, edi                          ; 0x00000d 8b c7
  cmp eax, 4                            ; 0x00000f 83 f8 04
  jae 0x25                              ; 0x000012 0f 83 0d 00 00 00
 0x000018:                              
  movabs rcx, 0                         ; 0x000018 48 b9 00 00 00 00 00 00 00 00
  jmp qword ptr [rcx + rax*8]           ; 0x000022 ff 24 c1
 0x000025:                              
  mov eax, dword ptr [r14 + 0x40]       ; 0x000025 41 8b 46 40
  mov eax, dword ptr [r15 + rax]        ; 0x000029 41 8b 04 07
  jmp 0x5b                              ; 0x00002d e9 29 00 00 00
 0x000032:                              
  xor eax, eax                          ; 0x000032 33 c0
  jmp 0x5b                              ; 0x000034 e9 22 00 00 00
 0x000039:                              
  mov eax, dword ptr [r14 + 0x44]       ; 0x000039 41 8b 46 44
  mov eax, dword ptr [r15 + rax]        ; 0x00003d 41 8b 04 07
  jmp 0x5b                              ; 0x000041 e9 15 00 00 00
 0x000046:                              
  mov eax, dword ptr [r14 + 0x48]       ; 0x000046 41 8b 46 48
  mov eax, dword ptr [r15 + rax]        ; 0x00004a 41 8b 04 07
  jmp 0x5b                              ; 0x00004e e9 08 00 00 00
 0x000053:                              
  mov eax, dword ptr [r14 + 0x4c]       ; 0x000053 41 8b 46 4c
  mov eax, dword ptr [r15 + rax]        ; 0x000057 41 8b 04 07
 0x00005b:                              ; 0x00005b from: [0x00002d, 0x000034, 0x000041, 0x00004e]
  nop                                   ; 0x00005b 66 90
  add rsp, 8                            ; 0x00005d 48 83 c4 08
  ret                                   ; 0x000061 c3

Thank you for a super useful tool. So much easier than using IONFLAGS=codegen with the spidermonkey shell!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.