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 17, 2025
1 parent acf00db commit aa3a087
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 3 deletions.
18 changes: 18 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 @@ -2476,6 +2477,23 @@ class Throw : public ByteCode {
uint32_t m_offsetsSize;
};

class Nop : public ByteCode {
public:
Nop()
: ByteCode(Opcode::NopOpcode)
{
}

#if !defined(NDEBUG)
void dump(size_t pos)
{
printf("nop");
}
#endif

protected:
};

class Unreachable : public ByteCode {
public:
Unreachable()
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
DEFINE_OPCODE(End)
:
{
Expand Down
4 changes: 4 additions & 0 deletions src/jit/Backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1345,6 +1345,10 @@ void JITCompiler::compileFunction(JITFunction* jitFunc, bool isExternal)
m_context.appendTrapJump(ExecutionContext::GenericTrap, sljit_emit_jump(m_compiler, SLJIT_JUMP));
break;
}
case ByteCode::NopOpcode: {
sljit_emit_op0(m_compiler, SLJIT_NOP);
break;
}
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
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
#define NOP_CHECK instr->opcode() == ByteCode::NopOpcode
#endif

#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
}

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
return Result::Ok;
}
Result OnRethrowExpr(Index depth) override {
Expand Down

0 comments on commit aa3a087

Please sign in to comment.