raw
current commit
200512.1 · Merge upstream (#89)

Multi-value Extension

Introduction

Background

Motivation

Examples

Functions with multiple return Values

A simple swap function.

(func $swap (param i32 i32) (result i32 i32)
	(get_local 1) (get_local 0)
)

An addition function returning an additional carry bit.

(func $add64_u_with_carry (param $i i64) (param $j i64) (param $c i32) (result i64 i32)
	(local $k i64)
	(set_local $k
		(i64.add (i64.add (get_local $i) (get_local $j)) (i64.extend_u/i32 (get_local $c)))
	)
	(return (get_local $k) (i64.lt_u (get_local $k) (get_local $i)))
)

Instructions with multiple results

Blocks with inputs

Conditionally manipulating a stack operand without using a local.

(func $add64_u_saturated (param i64 i64) (result i64)
	($i64.add_u_carry (get_local 0) (get_local 1) (i32.const 0))
	(if (param i64) (result i64)
		(then (drop) (i64.const 0xffff_ffff_ffff_ffff))
	)
)

An iterative factorial function whose loop doesn’t use locals, but uses arguments like phis.

(func $fac (param i64) (result i64)
	(i64.const 1) (get_local 0)
	(loop $l (param i64 i64) (result i64)
		(pick 1) (pick 1) (i64.mul)
		(pick 1) (i64.const 1) (i64.sub)
		(pick 0) (i64.const 0) (i64.gt_u)
		(br_if $l)
		(pick 1) (return)
	)
)

Macro definition of an instruction expanding into an if.

i64.select3  =
     dup if (param i64 i64 i64 i32) (result i64) … select ... else … end

Macro expansion of if itself.

if (param t*) (result u*) A else B end  =
      block (param t* i32) (result u*)
          block (param t* i32) (result t*) (br_if 0)  B  (br 1) end  A
      end

Spec Changes

Structure

The structure of the language is mostly unaffected. The only changes are to the type syntax:

Validation

Arity restrictions are removed:

Validation for block instructions is generalised:

Execution

Nothing much needs to be done for multiple results:

The only non-mechanical change involves entering blocks with operands:

Binary Format

The binary requires a change to allow function types as block types. That requires extending the current ad-hoc encoding to allow references to function types.

Text Format

The text format is mostly unaffected, except that the syntax for block types is generalised:

Soundness Proof

The typing of administrative instructions need to be generalised, see the paper.

Possible Alternatives and Extensions

More Flexible Block and Function Types

Open Questions