Skip to content

Commit

Permalink
Implement Nop
Browse files Browse the repository at this point in the history
Signed-off-by: Laszlo Voros <[email protected]>
  • Loading branch information
vorosl committed Jan 22, 2025
1 parent acf00db commit 3a90044
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 3 deletions.
17 changes: 17 additions & 0 deletions src/interpreter/ByteCode.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ namespace Walrus {
class FunctionType;

#define FOR_EACH_BYTECODE_OP(F) \
F(Nop) \
F(Unreachable) \
F(Throw) \
F(End) \
Expand Down Expand Up @@ -2475,6 +2476,22 @@ class Throw : public ByteCode {
uint32_t m_tagIndex;
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:
Expand Down
9 changes: 8 additions & 1 deletion src/interpreter/Interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1446,7 +1446,14 @@ ByteCodeStackOffset* Interpreter::interpret(ExecutionState& state,
ASSERT_NOT_REACHED();
NEXT_INSTRUCTION();
}

#if !defined(NDEBUG)
DEFINE_OPCODE(Nop)
:
{
ADD_PROGRAM_COUNTER(Nop);
NEXT_INSTRUCTION();
}
#endif /* !NDEBUG */
DEFINE_OPCODE(End)
:
{
Expand Down
6 changes: 6 additions & 0 deletions src/jit/Backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions src/jit/ByteCodeParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Jump*>(byteCode);
compiler->appendBranch(jump, opcode, labels[COMPUTE_OFFSET(idx, jump->offset())], 0);
Expand Down
10 changes: 8 additions & 2 deletions src/jit/RegisterAlloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
#include "jit/Compiler.h"
#include <set>

#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) { \
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
3 changes: 3 additions & 0 deletions src/parser/WASMParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions third_party/wabt/src/walrus/binary-reader-walrus.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 3a90044

Please sign in to comment.