diff options
author | Heejin Ahn <aheejin@gmail.com> | 2019-12-30 17:55:20 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-12-30 17:55:20 -0800 |
commit | bcc76146fed433cbc8ba01a9f568d979c145110b (patch) | |
tree | ab70ad24afc257b73513c3e62f3aab9938d05944 /src/wasm-builder.h | |
parent | a30f1df5696ccb3490e2eaa3a9ed5e7e487c7b0e (diff) | |
download | binaryen-bcc76146fed433cbc8ba01a9f568d979c145110b.tar.gz binaryen-bcc76146fed433cbc8ba01a9f568d979c145110b.tar.bz2 binaryen-bcc76146fed433cbc8ba01a9f568d979c145110b.zip |
Add support for reference types proposal (#2451)
This adds support for the reference type proposal. This includes support
for all reference types (`anyref`, `funcref`(=`anyfunc`), and `nullref`)
and four new instructions: `ref.null`, `ref.is_null`, `ref.func`, and
new typed `select`. This also adds subtype relationship support between
reference types.
This does not include table instructions yet. This also does not include
wasm2js support.
Fixes #2444 and fixes #2447.
Diffstat (limited to 'src/wasm-builder.h')
-rw-r--r-- | src/wasm-builder.h | 73 |
1 files changed, 68 insertions, 5 deletions
diff --git a/src/wasm-builder.h b/src/wasm-builder.h index 918e6a4ab..38009cb8a 100644 --- a/src/wasm-builder.h +++ b/src/wasm-builder.h @@ -110,6 +110,12 @@ public: ret->finalize(); return ret; } + Block* makeBlock(const std::vector<Expression*>& items, Type type) { + auto* ret = allocator.alloc<Block>(); + ret->list.set(items); + ret->finalize(type); + return ret; + } Block* makeBlock(const ExpressionList& items) { auto* ret = allocator.alloc<Block>(); ret->list.set(items); @@ -164,6 +170,13 @@ public: ret->finalize(); return ret; } + Loop* makeLoop(Name name, Expression* body, Type type) { + auto* ret = allocator.alloc<Loop>(); + ret->name = name; + ret->body = body; + ret->finalize(type); + return ret; + } Break* makeBreak(Name name, Expression* value = nullptr, Expression* condition = nullptr) { @@ -459,6 +472,7 @@ public: return ret; } Const* makeConst(Literal value) { + assert(value.type.isNumber()); auto* ret = allocator.alloc<Const>(); ret->value = value; ret->type = value.type; @@ -488,6 +502,17 @@ public: ret->finalize(); return ret; } + Select* makeSelect(Expression* condition, + Expression* ifTrue, + Expression* ifFalse, + Type type) { + auto* ret = allocator.alloc<Select>(); + ret->condition = condition; + ret->ifTrue = ifTrue; + ret->ifFalse = ifFalse; + ret->finalize(type); + return ret; + } Return* makeReturn(Expression* value = nullptr) { auto* ret = allocator.alloc<Return>(); ret->value = value; @@ -502,6 +527,23 @@ public: ret->finalize(); return ret; } + RefNull* makeRefNull() { + auto* ret = allocator.alloc<RefNull>(); + ret->finalize(); + return ret; + } + RefIsNull* makeRefIsNull(Expression* value) { + auto* ret = allocator.alloc<RefIsNull>(); + ret->value = value; + ret->finalize(); + return ret; + } + RefFunc* makeRefFunc(Name func) { + auto* ret = allocator.alloc<RefFunc>(); + ret->func = func; + ret->finalize(); + return ret; + } Try* makeTry(Expression* body, Expression* catchBody) { auto* ret = allocator.alloc<Try>(); ret->body = body; @@ -569,6 +611,21 @@ public: return ret; } + Expression* makeConstExpression(Literal value) { + switch (value.type) { + case Type::nullref: + return makeRefNull(); + case Type::funcref: + if (value.getFunc()[0] != 0) { + return makeRefFunc(value.getFunc()); + } + return makeRefNull(); + default: + assert(value.type.isNumber()); + return makeConst(value); + } + } + // Additional utility functions for building on top of nodes // Convenient to have these on Builder, as it has allocation built in @@ -663,6 +720,13 @@ public: return block; } + Block* makeSequence(Expression* left, Expression* right, Type type) { + auto* block = makeBlock(left); + block->list.push_back(right); + block->finalize(type); + return block; + } + // Grab a slice out of a block, replacing it with nops, and returning // either another block with the contents (if more than 1) or a single // expression @@ -728,16 +792,15 @@ public: value = Literal(bytes.data()); break; } + case funcref: case anyref: - // TODO Implement and return nullref - assert(false && "anyref not implemented yet"); + case nullref: case exnref: - // TODO Implement and return nullref - assert(false && "exnref not implemented yet"); + return ExpressionManipulator::refNull(curr); case none: return ExpressionManipulator::nop(curr); case unreachable: - return ExpressionManipulator::convert<T, Unreachable>(curr); + return ExpressionManipulator::unreachable(curr); } return makeConst(value); } |