Moved OP_HALT to the beginning (stable ABI)

OP_HALT = 1 now.  This commit also adjusts the error checking in
inst_read_bytecode.

The main reasoning behind this is when other platforms or applications
target the AVM: whenever a new opcode may be added, the actual binary
for OP_HALT changes (as a result of how C enums work).

Say your application targets commit alpha of AVM.  OP_HALT is, say,
98.  In commit beta, AVM is updated with a new opcode so OP_HALT is
changed to 99 (due to the new opcode being placed before OP_HALT).  If
your application builds a binary for AVM version alpha and AVM version
beta is used instead, OP_HALT will be interpreted as another
instruction, which can lead to undefined behaviour.

This can be hard to debug, so here I've made the decision to try and
not place new opcodes in between old ones; new ones will always be
placed *before* NUMBER_OF_OPCODES.
This commit is contained in:
2024-06-18 23:43:06 +01:00
parent d0ab1644cb
commit 99fb0a0e2e
2 changed files with 2 additions and 2 deletions

View File

@@ -382,7 +382,7 @@ int inst_read_bytecode(inst_t *ptr, byte_t *bytes, size_t size_bytes)
static_assert(NUMBER_OF_OPCODES == 99, "inst_read_bytecode: Out of date");
opcode_t opcode = *(bytes++);
if (opcode > OP_HALT || opcode < OP_NOOP)
if (opcode >= NUMBER_OF_OPCODES || opcode < OP_NOOP)
return READ_ERR_INVALID_OPCODE;
inst_t inst = {opcode, {0}};

View File

@@ -28,6 +28,7 @@
typedef enum
{
OP_NOOP = 0,
OP_HALT,
// Dealing with data and registers
OP_PUSH_BYTE,
@@ -161,7 +162,6 @@ typedef enum
OP_CALL_STACK,
OP_RET,
OP_HALT,
// Should not be an opcode
NUMBER_OF_OPCODES,
} opcode_t;