diff options
Diffstat (limited to 'src/wasm/wasm-stack.cpp')
-rw-r--r-- | src/wasm/wasm-stack.cpp | 205 |
1 files changed, 104 insertions, 101 deletions
diff --git a/src/wasm/wasm-stack.cpp b/src/wasm/wasm-stack.cpp index af1e8907c..551b1f132 100644 --- a/src/wasm/wasm-stack.cpp +++ b/src/wasm/wasm-stack.cpp @@ -21,7 +21,7 @@ namespace wasm { void BinaryInstWriter::visitBlock(Block* curr) { breakStack.push_back(curr->name); o << int8_t(BinaryConsts::Block); - o << binaryType(curr->type != unreachable ? curr->type : none); + o << binaryType(curr->type != Type::unreachable ? curr->type : Type::none); } void BinaryInstWriter::visitIf(If* curr) { @@ -30,7 +30,7 @@ void BinaryInstWriter::visitIf(If* curr) { // instead) breakStack.emplace_back(IMPOSSIBLE_CONTINUE); o << int8_t(BinaryConsts::If); - o << binaryType(curr->type != unreachable ? curr->type : none); + o << binaryType(curr->type != Type::unreachable ? curr->type : Type::none); } void BinaryInstWriter::emitIfElse() { @@ -43,7 +43,7 @@ void BinaryInstWriter::emitIfElse() { void BinaryInstWriter::visitLoop(Loop* curr) { breakStack.push_back(curr->name); o << int8_t(BinaryConsts::Loop); - o << binaryType(curr->type != unreachable ? curr->type : none); + o << binaryType(curr->type != Type::unreachable ? curr->type : Type::none); } void BinaryInstWriter::visitBreak(Break* curr) { @@ -94,7 +94,7 @@ void BinaryInstWriter::visitGlobalSet(GlobalSet* curr) { void BinaryInstWriter::visitLoad(Load* curr) { if (!curr->isAtomic) { switch (curr->type) { - case i32: { + case Type::i32: { switch (curr->bytes) { case 1: o << int8_t(curr->signed_ ? BinaryConsts::I32LoadMem8S @@ -112,7 +112,7 @@ void BinaryInstWriter::visitLoad(Load* curr) { } break; } - case i64: { + case Type::i64: { switch (curr->bytes) { case 1: o << int8_t(curr->signed_ ? BinaryConsts::I64LoadMem8S @@ -134,30 +134,30 @@ void BinaryInstWriter::visitLoad(Load* curr) { } break; } - case f32: + case Type::f32: o << int8_t(BinaryConsts::F32LoadMem); break; - case f64: + case Type::f64: o << int8_t(BinaryConsts::F64LoadMem); break; - case v128: + case Type::v128: o << int8_t(BinaryConsts::SIMDPrefix) << U32LEB(BinaryConsts::V128Load); break; - case unreachable: + case Type::unreachable: // the pointer is unreachable, so we are never reached; just don't emit // a load return; - case funcref: - case anyref: - case nullref: - case exnref: - case none: + case Type::funcref: + case Type::anyref: + case Type::nullref: + case Type::exnref: + case Type::none: WASM_UNREACHABLE("unexpected type"); } } else { o << int8_t(BinaryConsts::AtomicPrefix); switch (curr->type) { - case i32: { + case Type::i32: { switch (curr->bytes) { case 1: o << int8_t(BinaryConsts::I32AtomicLoad8U); @@ -173,7 +173,7 @@ void BinaryInstWriter::visitLoad(Load* curr) { } break; } - case i64: { + case Type::i64: { switch (curr->bytes) { case 1: o << int8_t(BinaryConsts::I64AtomicLoad8U); @@ -192,7 +192,7 @@ void BinaryInstWriter::visitLoad(Load* curr) { } break; } - case unreachable: + case Type::unreachable: return; default: WASM_UNREACHABLE("unexpected type"); @@ -204,7 +204,7 @@ void BinaryInstWriter::visitLoad(Load* curr) { void BinaryInstWriter::visitStore(Store* curr) { if (!curr->isAtomic) { switch (curr->valueType) { - case i32: { + case Type::i32: { switch (curr->bytes) { case 1: o << int8_t(BinaryConsts::I32StoreMem8); @@ -220,7 +220,7 @@ void BinaryInstWriter::visitStore(Store* curr) { } break; } - case i64: { + case Type::i64: { switch (curr->bytes) { case 1: o << int8_t(BinaryConsts::I64StoreMem8); @@ -239,28 +239,28 @@ void BinaryInstWriter::visitStore(Store* curr) { } break; } - case f32: + case Type::f32: o << int8_t(BinaryConsts::F32StoreMem); break; - case f64: + case Type::f64: o << int8_t(BinaryConsts::F64StoreMem); break; - case v128: + case Type::v128: o << int8_t(BinaryConsts::SIMDPrefix) << U32LEB(BinaryConsts::V128Store); break; - case funcref: - case anyref: - case nullref: - case exnref: - case none: - case unreachable: + case Type::funcref: + case Type::anyref: + case Type::nullref: + case Type::exnref: + case Type::none: + case Type::unreachable: WASM_UNREACHABLE("unexpected type"); } } else { o << int8_t(BinaryConsts::AtomicPrefix); switch (curr->valueType) { - case i32: { + case Type::i32: { switch (curr->bytes) { case 1: o << int8_t(BinaryConsts::I32AtomicStore8); @@ -276,7 +276,7 @@ void BinaryInstWriter::visitStore(Store* curr) { } break; } - case i64: { + case Type::i64: { switch (curr->bytes) { case 1: o << int8_t(BinaryConsts::I64AtomicStore8); @@ -308,7 +308,7 @@ void BinaryInstWriter::visitAtomicRMW(AtomicRMW* curr) { #define CASE_FOR_OP(Op) \ case Op: \ switch (curr->type) { \ - case i32: \ + case Type::i32: \ switch (curr->bytes) { \ case 1: \ o << int8_t(BinaryConsts::I32AtomicRMW##Op##8U); \ @@ -323,7 +323,7 @@ void BinaryInstWriter::visitAtomicRMW(AtomicRMW* curr) { WASM_UNREACHABLE("invalid rmw size"); \ } \ break; \ - case i64: \ + case Type::i64: \ switch (curr->bytes) { \ case 1: \ o << int8_t(BinaryConsts::I64AtomicRMW##Op##8U); \ @@ -364,7 +364,7 @@ void BinaryInstWriter::visitAtomicRMW(AtomicRMW* curr) { void BinaryInstWriter::visitAtomicCmpxchg(AtomicCmpxchg* curr) { o << int8_t(BinaryConsts::AtomicPrefix); switch (curr->type) { - case i32: + case Type::i32: switch (curr->bytes) { case 1: o << int8_t(BinaryConsts::I32AtomicCmpxchg8U); @@ -379,7 +379,7 @@ void BinaryInstWriter::visitAtomicCmpxchg(AtomicCmpxchg* curr) { WASM_UNREACHABLE("invalid size"); } break; - case i64: + case Type::i64: switch (curr->bytes) { case 1: o << int8_t(BinaryConsts::I64AtomicCmpxchg8U); @@ -406,12 +406,12 @@ void BinaryInstWriter::visitAtomicCmpxchg(AtomicCmpxchg* curr) { void BinaryInstWriter::visitAtomicWait(AtomicWait* curr) { o << int8_t(BinaryConsts::AtomicPrefix); switch (curr->expectedType) { - case i32: { + case Type::i32: { o << int8_t(BinaryConsts::I32AtomicWait); emitMemoryAccess(4, 4, curr->offset); break; } - case i64: { + case Type::i64: { o << int8_t(BinaryConsts::I64AtomicWait); emitMemoryAccess(8, 8, curr->offset); break; @@ -622,23 +622,23 @@ void BinaryInstWriter::visitMemoryFill(MemoryFill* curr) { void BinaryInstWriter::visitConst(Const* curr) { switch (curr->type) { - case i32: { + case Type::i32: { o << int8_t(BinaryConsts::I32Const) << S32LEB(curr->value.geti32()); break; } - case i64: { + case Type::i64: { o << int8_t(BinaryConsts::I64Const) << S64LEB(curr->value.geti64()); break; } - case f32: { + case Type::f32: { o << int8_t(BinaryConsts::F32Const) << curr->value.reinterpreti32(); break; } - case f64: { + case Type::f64: { o << int8_t(BinaryConsts::F64Const) << curr->value.reinterpreti64(); break; } - case v128: { + case Type::v128: { o << int8_t(BinaryConsts::SIMDPrefix) << U32LEB(BinaryConsts::V128Const); std::array<uint8_t, 16> v = curr->value.getv128(); for (size_t i = 0; i < 16; ++i) { @@ -646,12 +646,12 @@ void BinaryInstWriter::visitConst(Const* curr) { } break; } - case funcref: - case anyref: - case nullref: - case exnref: - case none: - case unreachable: + case Type::funcref: + case Type::anyref: + case Type::nullref: + case Type::exnref: + case Type::none: + case Type::unreachable: WASM_UNREACHABLE("unexpected type"); } } @@ -1592,7 +1592,7 @@ void BinaryInstWriter::visitRefFunc(RefFunc* curr) { void BinaryInstWriter::visitTry(Try* curr) { breakStack.emplace_back(IMPOSSIBLE_CONTINUE); o << int8_t(BinaryConsts::Try); - o << binaryType(curr->type != unreachable ? curr->type : none); + o << binaryType(curr->type != Type::unreachable ? curr->type : Type::none); } void BinaryInstWriter::emitCatch() { @@ -1661,85 +1661,88 @@ void BinaryInstWriter::mapLocalsAndEmitHeader() { Type type = func->getLocalType(i); // increment now for simplicity, must decrement it in returns currLocalsByType[type]++; - if (type == i32) { - mappedLocals[i] = index + currLocalsByType[i32] - 1; + if (type == Type::i32) { + mappedLocals[i] = index + currLocalsByType[Type::i32] - 1; continue; } - index += numLocalsByType[i32]; - if (type == i64) { - mappedLocals[i] = index + currLocalsByType[i64] - 1; + index += numLocalsByType[Type::i32]; + if (type == Type::i64) { + mappedLocals[i] = index + currLocalsByType[Type::i64] - 1; continue; } - index += numLocalsByType[i64]; - if (type == f32) { - mappedLocals[i] = index + currLocalsByType[f32] - 1; + index += numLocalsByType[Type::i64]; + if (type == Type::f32) { + mappedLocals[i] = index + currLocalsByType[Type::f32] - 1; continue; } - index += numLocalsByType[f32]; - if (type == f64) { - mappedLocals[i] = index + currLocalsByType[f64] - 1; + index += numLocalsByType[Type::f32]; + if (type == Type::f64) { + mappedLocals[i] = index + currLocalsByType[Type::f64] - 1; continue; } - index += numLocalsByType[f64]; - if (type == v128) { - mappedLocals[i] = index + currLocalsByType[v128] - 1; + index += numLocalsByType[Type::f64]; + if (type == Type::v128) { + mappedLocals[i] = index + currLocalsByType[Type::v128] - 1; continue; } - index += numLocalsByType[v128]; - if (type == funcref) { - mappedLocals[i] = index + currLocalsByType[funcref] - 1; + index += numLocalsByType[Type::v128]; + if (type == Type::funcref) { + mappedLocals[i] = index + currLocalsByType[Type::funcref] - 1; continue; } - index += numLocalsByType[funcref]; - if (type == anyref) { - mappedLocals[i] = index + currLocalsByType[anyref] - 1; + index += numLocalsByType[Type::funcref]; + if (type == Type::anyref) { + mappedLocals[i] = index + currLocalsByType[Type::anyref] - 1; continue; } - index += numLocalsByType[anyref]; - if (type == nullref) { - mappedLocals[i] = index + currLocalsByType[nullref] - 1; + index += numLocalsByType[Type::anyref]; + if (type == Type::nullref) { + mappedLocals[i] = index + currLocalsByType[Type::nullref] - 1; continue; } - index += numLocalsByType[nullref]; - if (type == exnref) { - mappedLocals[i] = index + currLocalsByType[exnref] - 1; + index += numLocalsByType[Type::nullref]; + if (type == Type::exnref) { + mappedLocals[i] = index + currLocalsByType[Type::exnref] - 1; continue; } WASM_UNREACHABLE("unexpected type"); } // Emit them. - o << U32LEB( - (numLocalsByType[i32] ? 1 : 0) + (numLocalsByType[i64] ? 1 : 0) + - (numLocalsByType[f32] ? 1 : 0) + (numLocalsByType[f64] ? 1 : 0) + - (numLocalsByType[v128] ? 1 : 0) + (numLocalsByType[funcref] ? 1 : 0) + - (numLocalsByType[anyref] ? 1 : 0) + (numLocalsByType[nullref] ? 1 : 0) + - (numLocalsByType[exnref] ? 1 : 0)); - if (numLocalsByType[i32]) { - o << U32LEB(numLocalsByType[i32]) << binaryType(i32); + o << U32LEB((numLocalsByType[Type::i32] ? 1 : 0) + + (numLocalsByType[Type::i64] ? 1 : 0) + + (numLocalsByType[Type::f32] ? 1 : 0) + + (numLocalsByType[Type::f64] ? 1 : 0) + + (numLocalsByType[Type::v128] ? 1 : 0) + + (numLocalsByType[Type::funcref] ? 1 : 0) + + (numLocalsByType[Type::anyref] ? 1 : 0) + + (numLocalsByType[Type::nullref] ? 1 : 0) + + (numLocalsByType[Type::exnref] ? 1 : 0)); + if (numLocalsByType[Type::i32]) { + o << U32LEB(numLocalsByType[Type::i32]) << binaryType(Type::i32); } - if (numLocalsByType[i64]) { - o << U32LEB(numLocalsByType[i64]) << binaryType(i64); + if (numLocalsByType[Type::i64]) { + o << U32LEB(numLocalsByType[Type::i64]) << binaryType(Type::i64); } - if (numLocalsByType[f32]) { - o << U32LEB(numLocalsByType[f32]) << binaryType(f32); + if (numLocalsByType[Type::f32]) { + o << U32LEB(numLocalsByType[Type::f32]) << binaryType(Type::f32); } - if (numLocalsByType[f64]) { - o << U32LEB(numLocalsByType[f64]) << binaryType(f64); + if (numLocalsByType[Type::f64]) { + o << U32LEB(numLocalsByType[Type::f64]) << binaryType(Type::f64); } - if (numLocalsByType[v128]) { - o << U32LEB(numLocalsByType[v128]) << binaryType(v128); + if (numLocalsByType[Type::v128]) { + o << U32LEB(numLocalsByType[Type::v128]) << binaryType(Type::v128); } - if (numLocalsByType[funcref]) { - o << U32LEB(numLocalsByType[funcref]) << binaryType(funcref); + if (numLocalsByType[Type::funcref]) { + o << U32LEB(numLocalsByType[Type::funcref]) << binaryType(Type::funcref); } - if (numLocalsByType[anyref]) { - o << U32LEB(numLocalsByType[anyref]) << binaryType(anyref); + if (numLocalsByType[Type::anyref]) { + o << U32LEB(numLocalsByType[Type::anyref]) << binaryType(Type::anyref); } - if (numLocalsByType[nullref]) { - o << U32LEB(numLocalsByType[nullref]) << binaryType(nullref); + if (numLocalsByType[Type::nullref]) { + o << U32LEB(numLocalsByType[Type::nullref]) << binaryType(Type::nullref); } - if (numLocalsByType[exnref]) { - o << U32LEB(numLocalsByType[exnref]) << binaryType(exnref); + if (numLocalsByType[Type::exnref]) { + o << U32LEB(numLocalsByType[Type::exnref]) << binaryType(Type::exnref); } } @@ -1799,17 +1802,17 @@ StackInst* StackIRGenerator::makeStackInst(StackInst::Op op, auto stackType = origin->type; if (origin->is<Block>() || origin->is<Loop>() || origin->is<If>() || origin->is<Try>()) { - if (stackType == unreachable) { + if (stackType == Type::unreachable) { // There are no unreachable blocks, loops, or ifs. we emit extra // unreachables to fix that up, so that they are valid as having none // type. - stackType = none; + stackType = Type::none; } else if (op != StackInst::BlockEnd && op != StackInst::IfEnd && op != StackInst::LoopEnd && op != StackInst::TryEnd) { // If a concrete type is returned, we mark the end of the construct has // having that type (as it is pushed to the value stack at that point), // other parts are marked as none). - stackType = none; + stackType = Type::none; } } ret->type = stackType; |