WebAssembly - assembly language for your browser

Just when you thought assembly language was dead…it’s now the newest thing to enable browser apps to run at near-native speed. WebAssembly is the next generation of browser assembly language, following on the heels of asm.js.

The latest IEEE Spectrum has the background story and intro to the subject.

The wasm stack machine is designed to be encoded in a size- and load-time-efficient binary format. WebAssembly aims to execute at native speed by taking advantage of common hardware capabilities available on a wide range of platforms.

http://webassembly.org/

4 Likes

Kind of old by reddit standards. One may want to look into https://kripken.github.io/llvm.js/demo.html and https://github.com/kripken/emscripten

There is also a bable plugin: https://github.com/RReverser/babel-plugin-asm-js

Hmm, maybe I should target WebAssembly?

1 Like

Hmm… Tensorflow in a browser via webassembly… i’d say that’s very worthy…

Hell, there’s even Blockchain via webassembly

I just spent the day down another rabbit hole.

While I was learning Python, I focused on the present and past of the language. Namely learning Python 3 while being aware of what was different in Python 2.

I totally missed developments into the typing of variables being proposed. While dynamic typing has its advantages, it also comes with costs. It was deemed desirable to be able to write statically-typed code, both for efficiency at run-time and to allow automated detection of programming errors (what a concept!)

Because WebAssembly was designed to be statically typed, use of the new static types in Python code will allow the implementation of a Python to WebAssembly compiler which can generate suitable efficient code.

Some reading material:

https://docs.python.org/3/library/typing.html

Wednesday was for me to get all excited about WebAssembly.

Thursday was for returning to Earth.

In studying the bytecode https://github.com/WebAssembly/design/blob/master/BinaryEncoding.md the lack of a jump instruction is glaring.

Why do we need a jump instruction when there are structured if/else and looping instructions?

Consider the following Python code:

while condition:
    # loop body
else:
    # executes only if the loop terminates without hitting a "break"

There is no way to compile the “else” part without resorting to a single entry branch table to implement a jump. Dogma sucks.

Something else I do not comprehend.

In https://github.com/WebAssembly/design/blob/master/Semantics.md#local-variables under local variables, it says…

“Each function has a fixed, pre-declared number of local variables which occupy a single index space local to the function. Parameters are addressed as local variables. Local variables do not have addresses and are not aliased by linear memory.”

I interpret that to say that I cannot pass the address of a local variable to a function. How can they support much of today’s C code with that restriction?

OK, my head officially hurts now.

The control constructs are:

  • nop: no operation, no effect
  • block: the beginning of a block construct, a sequence of instructions with a label at the end
  • loop: a block with a label at the beginning which may be used to form loops
  • if: the beginning of an if construct with an implicit then block
  • else: marks the else block of an if
  • br: branch to a given label in an enclosing construct
  • br_if: conditionally branch to a given label in an enclosing construct
  • br_table: a jump table which jumps to a label in an enclosing construct
  • return: return zero or more values from this function
  • end: an instruction that marks the end of a block, loop, if, or function

So I can build a standard while loop as follows:
while

The green box is a loop-end pair with a label at the top; the yellow box is a block-end pair with the label at the bottom.

But I cannot see how to build the Python while-else loop without overlapping blocks which do not appear to be allowed:
whileelse

Get started with WebAssembly — using only 14 lines of JavaScript

2 Likes

I have discussed this with WebAssembly experts on GitHub. The solution was right in front of me and I failed to see it.

So this

while condition:
    # loop body
    if something:
        break
else:
    # executes only if the loop terminates without hitting a "break"

would compile to this:

    (loop $loop
        (if $break (..condition..)
            (then
                (..the loop body goes here..)
                (br_if (something) $break)
                (br $loop)
            )
        (else
            (..the while-else code goes here..)
        )
    )

The $ signifies a label. The label for a loop is at the top of the block and at the end for the if.