Is tooling available to 'assemble' WebAssembly to x86-64 native code?

廉价感情. 提交于 2019-12-21 17:15:36

问题


I am guessing that a Wasm binary is usually JIT-compiled to native code, but given a Wasm source, is there a tool to see the actual generated x86-64 machine code? Or asked in a different way, is there a tool that consumes Wasm and outputs native code?


回答1:


The online WasmExplorer compiles C code to both WebAssembly and FireFox x86, using the SpiderMonkey compiler. Given the following simple function:

int testFunction(int* input, int length) {
  int sum = 0;
  for (int i = 0; i < length; ++i) {
    sum += input[i];
  }
  return sum;
}

Here is the x86 output:

wasm-function[0]:
  sub rsp, 8                            ; 0x000000 48 83 ec 08
  cmp esi, 1                            ; 0x000004 83 fe 01
  jge 0x14                              ; 0x000007 0f 8d 07 00 00 00
 0x00000d:                              
  xor eax, eax                          ; 0x00000d 33 c0
  jmp 0x26                              ; 0x00000f e9 12 00 00 00
 0x000014:                              
  xor eax, eax                          ; 0x000014 33 c0
 0x000016:                              ; 0x000016 from: [0x000024]
  mov ecx, dword ptr [r15 + rdi]        ; 0x000016 41 8b 0c 3f
  add eax, ecx                          ; 0x00001a 03 c1
  add edi, 4                            ; 0x00001c 83 c7 04
  add esi, -1                           ; 0x00001f 83 c6 ff
  test esi, esi                         ; 0x000022 85 f6
  jne 0x16                              ; 0x000024 75 f0
 0x000026:                              
  nop                                   ; 0x000026 66 90
  add rsp, 8                            ; 0x000028 48 83 c4 08
  ret 

You can view this example online.

WasmExplorer compiles code into wasm / x86 via a service - you can see the scripts that are run on Github - you should be able to use these to construct a command-line tool yourself.



来源:https://stackoverflow.com/questions/52111076/is-tooling-available-to-assemble-webassembly-to-x86-64-native-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!