Web assembly

Web assembly (wasm) is a portable binary format designed for the web.

http://webassembly.org/

With the ppci.irs.wasm.wasm_to_ir() class it is possible to translate wasm code to ir-code. It is also possible to translate ir-code into wasm with the ppci.irs.wasm.ir_to_wasm() function.

>>> import io
>>> from ppci import api
>>> from ppci.irs.wasm import wasm_to_ir, read_wasm
>>> f = io.BytesIO(bytes.fromhex(
... '0061736d0100000001060160017f017f'
... '03020100070c01086d61696e5f666163'
... '00000a190117002000410148047f4101'
... '052000200041016b10006c0b0b'))
>>> wasm_module = read_wasm(f)
>>> wasm_module
<WASM-Module>
>>> ir_module = wasm_to_ir(wasm_module)
>>> obj = api.ir_to_object([ir_module], 'xtensa')
>>> print(obj)  
CodeObject of ... bytes

Module reference

Code for converting between PPCI-IR and WASM (Web Assembly)

ppci.irs.wasm.wasm_to_ir(wasm_module: ppci.irs.wasm.components.Module) → ppci.ir.Module

Convert a WASM module into a PPCI native module.

Parameters:wasm_module (ppci.irs.wasm.Module) – The wasm-module to compile
Returns:An IR-module.
ppci.irs.wasm.ir_to_wasm(ir_module, reporter=None)

Compiles ir-code to a wasm module.

Parameters:
  • ir_module (ir.Module) – The ir-module to compile
  • reporter – optionally report compilation steps
Returns:

A wasm module.

ppci.irs.wasm.read_wasm(f)

Read a wasm file

ppci.irs.wasm.wasm_to_wat(module, f)

Translate a wasm module into textual format.

class ppci.irs.wasm.WasmToIrCompiler

Convert WASM instructions into PPCI IR instructions.

emit(ppci_inst)

Emits the given instruction to the builder.

Can be muted for constants.

fill_phi(phi)

Fill phi with current stack value, if phi is needed

generate_function(name, signature, wasm_function)

Generate code for a single function

generate_instruction(instruction)

Generate ir-code for a single wasm instruction

get_phi(instruction)

Get phi function for the given loop/block/if

pop_condition()

Get comparison, a and b of the value stack

pop_value()

Pop a value of the stack

class ppci.irs.wasm.IrToWasmCompiler(reporter=None)

Translates ir-code into wasm

compile(ir_module)

Compile an ir-module into a wasm module

create_wasm_module()

Finalize the wasm module and return it

do_block(ir_block)

Generate code for the given block

do_function(ir_function)

Generate WASM for a single function

do_shape(shape)

Generate code for a given code shape

do_tree(tree)

Implement proper logic for an ir instruction

emit(instruction)

Emit a single wasm instruction

get_ty(ir_ty)

Get the right wasm type for an ir type

get_type_id(arg_types, ret_types)

Get wasm type id and create a signature if required!

get_value(value)

Create a local number for the given value

increment_stack_pointer()

Allocate stack if needed

class ppci.irs.wasm.WasmArchitecture

Web assembly architecture description