From b73376b908ef539e675e6557ac8d40899f3d1f1c Mon Sep 17 00:00:00 2001 From: titzer Date: Tue, 16 Aug 2016 05:49:12 -0700 Subject: [PATCH] [wasm] Macro-ify checking of prototype flags. R=ahaas@chromium.org BUG= Review-Url: https://codereview.chromium.org/2253543003 Cr-Commit-Position: refs/heads/master@{#38658} --- src/wasm/ast-decoder.cc | 52 ++++++--------------- test/unittests/wasm/ast-decoder-unittest.cc | 13 +++--- 2 files changed, 20 insertions(+), 45 deletions(-) diff --git a/src/wasm/ast-decoder.cc b/src/wasm/ast-decoder.cc index edbdcfe1d9..740199c298 100644 --- a/src/wasm/ast-decoder.cc +++ b/src/wasm/ast-decoder.cc @@ -31,6 +31,12 @@ namespace wasm { #define TRACE(...) #endif +#define CHECK_PROTOTYPE_OPCODE(flag) \ + if (!FLAG_##flag) { \ + error("Invalid opcode (enable with --" #flag ")"); \ + break; \ + } + // An SsaEnv environment carries the current local variable renaming // as well as the current effect and control dependency in the TF graph. // It maintains a control state that tracks whether the environment @@ -671,23 +677,14 @@ class WasmFullDecoder : public WasmDecoder { break; } case kExprThrow: { - if (!FLAG_wasm_eh_prototype) { - error("Invalid opcode"); - return; - } - - // TODO(jpp): validate the poped value. - Pop(); + CHECK_PROTOTYPE_OPCODE(wasm_eh_prototype); + Pop(0, kAstI32); // TODO(jpp): start exception propagation. break; } case kExprTryCatch: { - if (!FLAG_wasm_eh_prototype) { - error("Invalid opcode"); - return; - } - + CHECK_PROTOTYPE_OPCODE(wasm_eh_prototype); SsaEnv* outer_env = ssa_env_; SsaEnv* try_env = Steal(outer_env); SsaEnv* catch_env = Split(try_env); @@ -696,11 +693,7 @@ class WasmFullDecoder : public WasmDecoder { break; } case kExprTryCatchFinally: { - if (!FLAG_wasm_eh_prototype) { - error("Invalid opcode"); - return; - } - + CHECK_PROTOTYPE_OPCODE(wasm_eh_prototype); SsaEnv* outer_env = ssa_env_; SsaEnv* try_env = Steal(outer_env); SsaEnv* catch_env = Split(try_env); @@ -710,11 +703,7 @@ class WasmFullDecoder : public WasmDecoder { break; } case kExprTryFinally: { - if (!FLAG_wasm_eh_prototype) { - error("Invalid opcode"); - return; - } - + CHECK_PROTOTYPE_OPCODE(wasm_eh_prototype); SsaEnv* outer_env = ssa_env_; SsaEnv* try_env = Steal(outer_env); SsaEnv* finally_env = Split(outer_env); @@ -723,11 +712,7 @@ class WasmFullDecoder : public WasmDecoder { break; } case kExprCatch: { - if (!FLAG_wasm_eh_prototype) { - error("Invalid opcode"); - return; - } - + CHECK_PROTOTYPE_OPCODE(wasm_eh_prototype); LocalIndexOperand operand(this, pc_); len = 1 + operand.length; @@ -766,11 +751,7 @@ class WasmFullDecoder : public WasmDecoder { break; } case kExprFinally: { - if (!FLAG_wasm_eh_prototype) { - error("Invalid opcode"); - return; - } - + CHECK_PROTOTYPE_OPCODE(wasm_eh_prototype); if (control_.empty()) { error(pc_, "finally does not match a any try"); break; @@ -885,8 +866,6 @@ class WasmFullDecoder : public WasmDecoder { name = "if_else:merge"; } } else if (c->is_try()) { - DCHECK(FLAG_wasm_eh_prototype); - name = "try:end"; // try blocks do not yield a value. @@ -1201,10 +1180,7 @@ class WasmFullDecoder : public WasmDecoder { break; } case kSimdPrefix: { - if (!FLAG_wasm_simd_prototype) { - error("Invalid opcode"); - return; - } + CHECK_PROTOTYPE_OPCODE(wasm_simd_prototype); len++; byte simd_index = *(pc_ + 1); opcode = static_cast(opcode << 8 | simd_index); diff --git a/test/unittests/wasm/ast-decoder-unittest.cc b/test/unittests/wasm/ast-decoder-unittest.cc index 621040ae17..7311f063a0 100644 --- a/test/unittests/wasm/ast-decoder-unittest.cc +++ b/test/unittests/wasm/ast-decoder-unittest.cc @@ -1833,13 +1833,12 @@ TEST_F(AstDecoderTest, Throw) { FLAG_wasm_eh_prototype = true; EXPECT_VERIFIES_INLINE(sigs.v_i(), WASM_GET_LOCAL(0), kExprThrow); - // TODO(jpp): can't throw d, f, or l. - EXPECT_VERIFIES_INLINE(sigs.i_d(), WASM_GET_LOCAL(0), kExprThrow, - WASM_I32V(0)); - EXPECT_VERIFIES_INLINE(sigs.i_f(), WASM_GET_LOCAL(0), kExprThrow, - WASM_I32V(0)); - EXPECT_VERIFIES_INLINE(sigs.l_l(), WASM_GET_LOCAL(0), kExprThrow, - WASM_I64V(0)); + EXPECT_FAILURE_INLINE(sigs.i_d(), WASM_GET_LOCAL(0), kExprThrow, + WASM_I32V(0)); + EXPECT_FAILURE_INLINE(sigs.i_f(), WASM_GET_LOCAL(0), kExprThrow, + WASM_I32V(0)); + EXPECT_FAILURE_INLINE(sigs.l_l(), WASM_GET_LOCAL(0), kExprThrow, + WASM_I64V(0)); } #define WASM_CATCH(local) kExprCatch, static_cast(local)