From 2cee984092224d829ef46bd0777242beb5f6d4f8 Mon Sep 17 00:00:00 2001 From: Max Wash Date: Sun, 17 May 2026 15:33:20 +0100 Subject: [PATCH] runtime: add definitions for bshell vm bytecode --- bshell/runtime/opcode.c | 27 ++++++++++++++ bshell/runtime/opcode.h | 81 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 bshell/runtime/opcode.c create mode 100644 bshell/runtime/opcode.h diff --git a/bshell/runtime/opcode.c b/bshell/runtime/opcode.c new file mode 100644 index 0000000..e4c39d8 --- /dev/null +++ b/bshell/runtime/opcode.c @@ -0,0 +1,27 @@ +#include "opcode.h" + +#include + +#define OPCODE_MAX 0xFF +#define ARG_MAX 0xFFFFFF + +bshell_instruction bshell_instruction_encode( + enum bshell_opcode opcode, + uint32_t arg) +{ + if (opcode > OPCODE_MAX) { + return BSHELL_INSTRUCTION_INVALID; + } + + return fx_u32_htob((opcode & OPCODE_MAX) | (arg & ARG_MAX) << 8); +} + +void bshell_instruction_decode( + bshell_instruction instr, + enum bshell_opcode *out_opcode, + uint32_t *out_arg) +{ + instr = fx_u32_btoh(instr); + *out_opcode = (instr & OPCODE_MAX); + *out_arg = ((instr >> 8) & ARG_MAX); +} diff --git a/bshell/runtime/opcode.h b/bshell/runtime/opcode.h new file mode 100644 index 0000000..5e17929 --- /dev/null +++ b/bshell/runtime/opcode.h @@ -0,0 +1,81 @@ +#ifndef RUNTIME_OPCODE_H_ +#define RUNTIME_OPCODE_H_ + +#include + +#define BSHELL_INSTRUCTION_INVALID 0xFFFFFFFF + +typedef uint32_t bshell_instruction; + +enum bshell_opcode { + OPCODE_NOP = 0, + OPCODE_LDC_INT, + OPCODE_LDC_STR, + OPCODE_LDGLOBAL, + OPCODE_LDLOCAL, + OPCODE_LDBLOCK, + OPCODE_LDENV, + OPCODE_LDSTATIC, + OPCODE_LDARG, + OPCODE_LDPROP, + OPCODE_LDELEM, + OPCODE_LDELEM_C, + OPCODE_STLOCAL, + OPCODE_STPROP, + OPCODE_STELEM, + OPCODE_LDCMD, + OPCODE_PEXEC, + OPCODE_CALL, + OPCODE_MK_STR, + OPCODE_MK_ARRAY, + OPCODE_MK_HTAB, + OPCODE_POP, + OPCODE_ADD, + OPCODE_SUB, + OPCODE_MUL, + OPCODE_DIV, + OPCODE_ADD_IP, + OPCODE_MOD, + OPCODE_INC, + OPCODE_DEC, + OPCODE_SHL, + OPCODE_SHR, + OPCODE_BAND, + OPCODE_BOR, + OPCODE_BXOR, + OPCODE_BNOT, + OPCODE_CMP_NULL, + OPCODE_CMP_EQ, + OPCODE_CMP_NE, + OPCODE_CMP_LT, + OPCODE_CMP_GT, + OPCODE_CMP_LE, + OPCODE_CMP_GE, + OPCODE_LAND, + OPCODE_LOR, + OPCODE_LXOR, + OPCODE_LNOT, + OPCODE_BR, + OPCODE_BR_TRUE, + OPCODE_RANGE, + OPCODE_MATCH, + OPCODE_REPLACE, + OPCODE_LIKE, + OPCODE_IN, + OPCODE_FMT, + OPCODE_CONTAINS, + OPCODE_SPLIT, + OPCODE_JOIN, + OPCODE_IS, + OPCODE_AS, +}; + +extern bshell_instruction bshell_instruction_encode( + enum bshell_opcode opcode, + uint32_t arg); +extern void bshell_instruction_decode( + bshell_instruction instr, + enum bshell_opcode *out_opcode, + uint32_t *out_arg); + +#endif