Struct binaryen::Module[][src]

pub struct Module { /* fields omitted */ }

Modules contain lists of functions, imports, exports, function types.

Methods

impl Module
[src]

Create a new empty Module.

Deserialize a module from binary form.

Auto-generate drop() operations where needed. This lets you generate code without worrying about where they are needed.

It is more efficient to do it yourself, but simpler to use autodrop.

Run the standard optimization passes on the module.

Validate a module, printing errors to stdout on problems.

Print a module to stdout in s-expression text format. Useful for debugging.

Important traits for Vec<u8>

Serialize a module into binary form.

Examples

let module = Module::new();
let wasm = module.write();

Set start function. One per module.

See http://webassembly.org/docs/modules/#module-start-function.

Set memory to be used by this module.

See http://webassembly.org/docs/modules/#linear-memory-section

Examples

let module = Module::new();

// Create a segment from data and offset. Read more in `Segment` documentation.
let data = b"Hello world\0";
let offset_expr = module.const_(Literal::I32(0));
let segment = Segment::new(data, offset_expr);

// Set memory definition, also exporting it with name "mem".
module.set_memory(1, 1, Some("mem"), vec![segment]);

assert!(module.is_valid());

Add a new function type.

If name is None, name will be autogenerated.

Examples

let module = Module::new();

// Roughly (u32, u64) -> ().
let viI = module.add_fn_type(Some("viI"), &[ValueTy::I32, ValueTy::I64], Ty::None);

Add a new function.

You can declare variables by passing types of that variables into var_tys.

Examples

let module = Module::new();

// Add a new function type (u32, u32) -> u32.
let params = &[ValueTy::I32, ValueTy::I32];
let iii = module.add_fn_type(Some("iii"), params, Ty::I32);

// Load parameter x (local 0) and y (local 1) and do the addition.
let x = module.get_local(0, ValueTy::I32);
let y = module.get_local(1, ValueTy::I32);
let add = module.binary(BinaryOp::AddI32, x, y);

// Finally add a new function.
let adder = module.add_fn("adder", &iii, &[], add);

Add a new global.

See http://webassembly.org/docs/modules/#global-section

Examples

let module = Module::new();

let init_expr = module.const_(Literal::I32(0));
module.add_global("counter", ValueTy::I32, true, init_expr);

Add a function import.

Examples

let module = Module::new();

// Add a new function type () -> ().
let vv = module.add_fn_type(None::<&str>, &[], Ty::None);

// Add function called "_abort" from the module "env".
// This module can call this function via name "abort".
module.add_fn_import("abort", "env", "_abort", &vv);

Add a function export.

Examples

let module = Module::new();

// Create a simple function that does nothing.
let vv = module.add_fn_type(None::<&str>, &[], Ty::None);
let nop = module.nop();
let do_nothing = module.add_fn("do_nothing", &vv, &[], nop);

// Export "do_nothing" function with an external name "_do_nothing".
module.add_fn_export("do_nothing", "_do_nothing");

Evaluate condition, and if condition yields non-zero value if_true is executed. If condition yielded zero value and if_false is not None then if_false is executed.

Evaluate body.

Loop provides a name which can be used together with break_ to make a loop. Breaking to a loop will transfer control at the start of the loop, which is roughly equivalent to continue statement.

Examples

let module = Module::new();
let break_to_loop = module.break_("loop1", None, None);
let infinite_loop = module.loop_("loop1", break_to_loop);

Examples

let module = Module::new();

let condition = module.const_(Literal::I32(2));
let switch = module.switch(vec!["block", "loop"], "default", condition, None);

Evaluate each node in children one by one. You can provide type of this block via ty, otherwise type would be figured out for you.

Blocks may have names. Branch targets in the IR are resolved by name (as opposed to nesting depth in WebAssembly). This is the only IR node that has a variable-length list of operands.

Examples

let module = Module::new();

let children = vec![module.nop()];
let block = module.block(Some("b1"), children, Some(Ty::None));

Breaking to a block will transfer control past the end of the block.

// This will transfer control after the `b1` block.
let br = module.break_("b1", None, None);

Yield specified literal.

Evaluate ptr, load value of type ty at the address provided by ptr offseted by offset.

Evaluate ptr and value, store value at the address provided by ptr offseted by offset.

Load value from a global with a specified name.

Evaluate value and store that value to a global with a specified name.

Load value from a local with a specified index.

Note that function parameters and variables share a single locals index space, so if a function has one parameter then it would be at index 0 and first variable would be at index 1.

Evaluate value and store that value into a local with a specified index.

Note that function parameters and variables share a single locals index space, so if a function has one parameter then it would be at index 0 and first variable would be at index 1.

Like set_local but also returns the value.

Return control from the current function, optionally returning value.

Evaluate all operands one by one and then call function defined in the current module.

Evaluate all operands one by one and then call imported function.

Evaluate lhs, then rhs and then do a binary operation with them.

Examples

let module = Module::new();

let x = module.get_local(0, ValueTy::I32);
let y = module.const_(Literal::I32(3));
let mul_by_3 = module.binary(BinaryOp::MulI32, x, y);

Evaluate value and then do a unary operation with it.

Examples

let module = Module::new();

let x = module.get_local(0, ValueTy::F64);
let square_root = module.unary(UnaryOp::SqrtF64, x);

No operation, no effect.

Instruction that always traps and have type of unreachable. This has an interesting consequences on the type system, for example:

This example is not tested
(func $test (result i32)
  (call $return_i64
    (unreachable)
  )
)

Function test is expected to return i32, but it calls some function that returns i64. Passing unreachable as argument to a function makes this function to also have unreachable type.

Because unreachable is a bottom type (i.e. can be used in place every other type) this example is perfectly valid.

Evaluate if_true, if_false and then condition. Exprsession if_true and if_false should have the same type as each other. The value of if_true returned if condition evaluated to non-zero, or the value of if_false otherwise.

Evaluate value and discard it.

Trait Implementations

impl Default for Module
[src]

Returns the "default value" for a type. Read more

Auto Trait Implementations

impl !Send for Module

impl !Sync for Module