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 27, 2025
1 parent acf00db commit 38b674d
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 2 deletions.
69 changes: 69 additions & 0 deletions src/interpreter/ByteCode.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace Walrus {

class FunctionType;

#if defined(NDEBUG)
#define FOR_EACH_BYTECODE_OP(F) \
F(Unreachable) \
F(Throw) \
Expand Down Expand Up @@ -78,6 +79,58 @@ class FunctionType;
F(Store32) \
F(Store64) \
F(FillOpcodeTable)
#else /* !NDEBUG */
#define FOR_EACH_BYTECODE_OP(F) \
F(Nop) \
F(Unreachable) \
F(Throw) \
F(End) \
F(BrTable) \
F(Call) \
F(CallIndirect) \
F(Select) \
F(MemorySize) \
F(MemoryGrow) \
F(MemoryInit) \
F(DataDrop) \
F(MemoryCopy) \
F(MemoryFill) \
F(TableInit) \
F(ElemDrop) \
F(TableCopy) \
F(TableGet) \
F(TableSet) \
F(TableGrow) \
F(TableSize) \
F(TableFill) \
F(RefFunc) \
F(MoveI32) \
F(MoveF32) \
F(MoveI64) \
F(MoveF64) \
F(MoveV128) \
F(I32ReinterpretF32) \
F(I64ReinterpretF64) \
F(F32ReinterpretI32) \
F(F64ReinterpretI64) \
F(Jump) \
F(JumpIfTrue) \
F(JumpIfFalse) \
F(GlobalGet32) \
F(GlobalGet64) \
F(GlobalGet128) \
F(GlobalSet32) \
F(GlobalSet64) \
F(GlobalSet128) \
F(Const32) \
F(Const64) \
F(Const128) \
F(Load32) \
F(Load64) \
F(Store32) \
F(Store64) \
F(FillOpcodeTable)
#endif /* NDEBUG */

#define FOR_EACH_BYTECODE_BINARY_OP(F) \
F(I32Add, add, int32_t, int32_t) \
Expand Down Expand Up @@ -2476,6 +2529,22 @@ 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 /* !NDEBUG*/

protected:
};
class Unreachable : public ByteCode {
public:
Unreachable()
Expand Down
9 changes: 9 additions & 0 deletions src/interpreter/Interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
:
{
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 38b674d

Please sign in to comment.