From 721e142f510b04ebdcd695e5dbd8c2a625712efe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=B6r=C3=B6s=20L=C3=A1szl=C3=B3?= Date: Thu, 16 Jan 2025 11:43:29 +0100 Subject: [PATCH] Implement Nop Signed-off-by: Laszlo Voros --- src/interpreter/ByteCode.h | 25 +++++++++++++++++++ src/interpreter/Interpreter.cpp | 9 +++++++ src/jit/Backend.cpp | 6 +++++ src/jit/ByteCodeParser.cpp | 6 +++++ src/jit/RegisterAlloc.cpp | 10 ++++++-- src/parser/WASMParser.cpp | 3 +++ .../wabt/src/walrus/binary-reader-walrus.cc | 3 +++ 7 files changed, 60 insertions(+), 2 deletions(-) diff --git a/src/interpreter/ByteCode.h b/src/interpreter/ByteCode.h index c7e1859c5..fb566b92d 100644 --- a/src/interpreter/ByteCode.h +++ b/src/interpreter/ByteCode.h @@ -29,7 +29,14 @@ namespace Walrus { class FunctionType; +#if defined(NDEBUG) +#define F_NOP(F) +#else /* !NDEBUG */ +#define F_NOP(F) F(Nop) +#endif /* NDEBUG */ + #define FOR_EACH_BYTECODE_OP(F) \ + F_NOP(F) \ F(Unreachable) \ F(Throw) \ F(End) \ @@ -2476,6 +2483,24 @@ class Throw : public ByteCode { uint32_t m_offsetsSize; }; +#if !defined(NDEBUG) +class Nop : public ByteCode { +public: + Nop() + : ByteCode(Opcode::NopOpcode) + { + } + + + void dump(size_t pos) + { + printf("nop"); + } + +protected: +}; +#endif /* !NDEBUG*/ + class Unreachable : public ByteCode { public: Unreachable() diff --git a/src/interpreter/Interpreter.cpp b/src/interpreter/Interpreter.cpp index b28d23a0a..77345029a 100644 --- a/src/interpreter/Interpreter.cpp +++ b/src/interpreter/Interpreter.cpp @@ -1447,6 +1447,15 @@ ByteCodeStackOffset* Interpreter::interpret(ExecutionState& state, NEXT_INSTRUCTION(); } +#if !defined(NDEBUG) + DEFINE_OPCODE(Nop) + : + { + ADD_PROGRAM_COUNTER(Nop); + NEXT_INSTRUCTION(); + } +#endif /* !NDEBUG */ + DEFINE_OPCODE(End) : { diff --git a/src/jit/Backend.cpp b/src/jit/Backend.cpp index 13e47d626..8b24ee351 100644 --- a/src/jit/Backend.cpp +++ b/src/jit/Backend.cpp @@ -1345,6 +1345,12 @@ void JITCompiler::compileFunction(JITFunction* jitFunc, bool isExternal) m_context.appendTrapJump(ExecutionContext::GenericTrap, sljit_emit_jump(m_compiler, SLJIT_JUMP)); break; } +#if !defined(NDEBUG) + case ByteCode::NopOpcode: { + sljit_emit_op0(m_compiler, SLJIT_NOP); + break; + } +#endif /* !NDEBUG */ case ByteCode::EndOpcode: { emitEnd(m_compiler, item->asInstruction()); break; diff --git a/src/jit/ByteCodeParser.cpp b/src/jit/ByteCodeParser.cpp index 6a29e6390..2abe70752 100644 --- a/src/jit/ByteCodeParser.cpp +++ b/src/jit/ByteCodeParser.cpp @@ -1404,6 +1404,12 @@ static void compileFunction(JITCompiler* compiler) compiler->append(byteCode, group, opcode, 0, 0); break; } +#if !defined(NDEBUG) + case ByteCode::NopOpcode: { + compiler->append(byteCode, group, opcode, 0, 0); + break; + } +#endif /* !NDEBUG */ case ByteCode::JumpOpcode: { Jump* jump = reinterpret_cast(byteCode); compiler->appendBranch(jump, opcode, labels[COMPUTE_OFFSET(idx, jump->offset())], 0); diff --git a/src/jit/RegisterAlloc.cpp b/src/jit/RegisterAlloc.cpp index b2f34c051..c2ae10615 100644 --- a/src/jit/RegisterAlloc.cpp +++ b/src/jit/RegisterAlloc.cpp @@ -20,6 +20,12 @@ #include "jit/Compiler.h" #include +#if defined(NDEBUG) +#define NOP_CHECK false +#else /* !NDEBUG */ +#define NOP_CHECK instr->opcode() == ByteCode::NopOpcode +#endif /* NDEBUG */ + #if (defined SLJIT_SEPARATE_VECTOR_REGISTERS && SLJIT_SEPARATE_VECTOR_REGISTERS) #define VECTOR_SELECT(COND, VECTOR, FLOAT) \ if (COND) { \ @@ -688,7 +694,7 @@ void JITCompiler::allocateRegisters() ASSERT(instr->opcode() == ByteCode::EndOpcode || instr->opcode() == ByteCode::ThrowOpcode || instr->opcode() == ByteCode::CallOpcode || instr->opcode() == ByteCode::CallIndirectOpcode || instr->opcode() == ByteCode::ElemDropOpcode || instr->opcode() == ByteCode::DataDropOpcode - || instr->opcode() == ByteCode::JumpOpcode || instr->opcode() == ByteCode::UnreachableOpcode); + || instr->opcode() == ByteCode::JumpOpcode || instr->opcode() == ByteCode::UnreachableOpcode || NOP_CHECK); if (!hasResult) { continue; @@ -1021,7 +1027,7 @@ void JITCompiler::allocateRegistersSimple() ASSERT(instr->opcode() == ByteCode::EndOpcode || instr->opcode() == ByteCode::ThrowOpcode || instr->opcode() == ByteCode::CallOpcode || instr->opcode() == ByteCode::CallIndirectOpcode || instr->opcode() == ByteCode::ElemDropOpcode || instr->opcode() == ByteCode::DataDropOpcode - || instr->opcode() == ByteCode::JumpOpcode || instr->opcode() == ByteCode::UnreachableOpcode); + || instr->opcode() == ByteCode::JumpOpcode || instr->opcode() == ByteCode::UnreachableOpcode || NOP_CHECK); continue; } diff --git a/src/parser/WASMParser.cpp b/src/parser/WASMParser.cpp index 755beb4d8..565105009 100644 --- a/src/parser/WASMParser.cpp +++ b/src/parser/WASMParser.cpp @@ -2334,6 +2334,9 @@ class WASMBinaryReader : public wabt::WASMBinaryReaderDelegate { virtual void OnNopExpr() override { +#if !defined(NDEBUG) + pushByteCode(Walrus::Nop(), WASMOpcode::NopOpcode); +#endif /* !NDEBUG */ } virtual void OnReturnExpr() override diff --git a/third_party/wabt/src/walrus/binary-reader-walrus.cc b/third_party/wabt/src/walrus/binary-reader-walrus.cc index c5876fad7..84a117095 100644 --- a/third_party/wabt/src/walrus/binary-reader-walrus.cc +++ b/third_party/wabt/src/walrus/binary-reader-walrus.cc @@ -844,6 +844,9 @@ class BinaryReaderDelegateWalrus: public BinaryReaderDelegate { } Result OnNopExpr() override { CHECK_RESULT(m_validator.OnNop(GetLocation())); +#if !defined(NDEBUG) + m_externalDelegate->OnNopExpr(); +#endif /* !NDEBUG */ return Result::Ok; } Result OnRethrowExpr(Index depth) override {