依旧先展示某位大神的作品

See the Pen Marching ants spiral by 攻伤菊菊长 (@THISISAGOODNAME) on CodePen.

简介

       Binaryen是WebAssembly的一个编译工具链,用来把C/C++代码转换成WebAssembly代码

官方简介

  • Parse and emit WebAssembly, supporting the current S-Expression format.
  • Interpret WebAssembly. The interpreter passes 100% of the spec test suite.
  • Compile asm.js to WebAssembly, which together with Emscripten which compiles C and C++ to asm.js, gives you a complete compiler toolchain from C and C++ to WebAssembly. This passes all of the relevant part of Emscripten’s test suite (everything but some odd Emscripten features like split memory).
  • Polyfill WebAssembly, by running it in the interpreter compiled to JavaScript, if the browser does not yet have native support.

       如同最后一条介绍的,WebAssembly到现在为止没有任何实际意义,就是看着乐呵乐呵。当然,其实Binaryen给了一个测试用的js解释器,用来介绍wasm代码,叫binaryen.js,是一个asm.js实现的wasm解释器。因为chrome并不支持asm.js,所以这个玩意并没有什么卵用。

Binaryen 工具链

       Binaryen提供了一系列工具,用来形成他们独有的工作流。

  • binaryen-shell: A shell that can load and interpret WebAssembly code in S-Expression format, as well as run transformation passes on it. It can also run the spec test suite.
  • wasm-as: Assembles WebAssembly in text format (currently S-Expression format) into binary format (currently v8 format).
  • wasm-dis: Un-assembles WebAssembly in binary format (currently v8 format) into text format (currently S-Expression format).
  • asm2wasm: An asm.js-to-WebAssembly compiler, built on Emscripten’s asm optimizer infrastructure. This is used by Emscripten in Binaryen mode when it uses Emscripten’s fastcomp asm.js backend.
  • wasm2asm: A WebAssembly-to-asm.js compiler, the reverse of asm2wasm. This is a work in progress.
  • s2wasm: A compiler from the .s format emitted by the new WebAssembly backend being developed in LLVM. This is used by Emscripten in Binaryen mode when it integrates with the new LLVM backend.
  • wasm.js: wasm.js contains Binaryen components compiled to JavaScript, including the interpreter, asm2wasm, the S-Expression parser, etc., which allow you to use Binaryen with Emscripten and execute code compiled to WASM even if the browser doesn’t have native support yet. This can be useful as a (slow) polyfill.
  • binaryen.js: A stand alone library that exposes Binaryen methods for parsing s-expressions and instantiating WASM modules in JavaScript.

现阶段比较有用的命令

binaryen-shell

bin/binaryen-shell [.wast file] [options] [passes, see --help] [--help]

       可以高亮显示wast文件

asm2wasm

bin/asm2wasm [input.asm.js file]

       可以转换asm.js文件到wasm格式,并高亮显示

WebAssembly生成工作流

       binaryen提供了两个流程

C/C++ Source ⇒ asm2wasm ⇒ WebAssembly

C/C++ Source ⇒ WebAssembly LLVM backend ⇒ s2wasm ⇒ WebAssembly

       还有一个方法,就是使用ilwasm把.NET CIL变成WebAssembly S-expressions,不过这个项目已经很久没动过了。

总结

       Binaryen只能使用它自己的API(例如wasm.h),而不能像emscripten一样使用各种现有的C++库,已经很大程度限制了Binaryen使用的广泛程度。

       而Binaryen或者说WebAssembly另一个限制,就是大佬们的支持程度了。虽然貌似微软、谷歌、苹果、moz的大佬们共同商议了WebAssembly标准,但是现在为止唯一的实现还是asm.js的,原生支持更是一个没有。WebAssembly标准以及提出了大半年,记得刚宣布的时候,大家还很咋呼,现在就已经被遗忘了吗。

       现在开始,倡议谷歌大佬,快让chrome支持asm.js吧,虽然不大可能了。