Implemented extra check for actually supported modes.
h3rald h3rald@h3rald.com
Fri, 17 Jul 2026 14:03:03 +0200
1 files changed,
62 insertions(+),
0 deletions(-)
jump to
M
xywasm.c
→
xywasm.c
@@ -62,6 +62,32 @@ TOKEN_END_COMMENT,
TOKEN_END_INVALID } xyw_token_terminator; +typedef enum +{ + M_NOM = 0, // no nodes + M_ONW, // Only w + M_EXC, // x or y and w + M_ALL // all modes +} xyw_allowed_modes; + +const xyw_byte xyw_instruction_modes[] = { +// "HLT", "NOP", "PSH", "POP", + M_NOM, M_NOM, M_ALL, M_ALL, +// "LDB", "LDW", "STB", "STW", + M_ALL, M_ALL, M_ALL, M_ALL, +// "INC", "DEC", "SHL", "SHR", + M_ALL, M_ALL, M_ALL, M_ALL, +// "ADD", "SUB", "MUL", "DIV", + M_ALL, M_ALL, M_ALL, M_ALL, +// "EQU", "NEQ", "GTH", "LTH", + M_ALL, M_ALL, M_ALL, M_ALL, +// "NOT", "AND", "IOR", "XOR", + M_ALL, M_ALL, M_ALL, M_ALL, +// "DUP", "SWP", "OVR", "ROT", + M_ALL, M_ALL, M_ONW, M_ONW, +// "JMP", "JCN", "JSR", "RTS" + M_EXC, M_EXC, M_EXC, M_NOM}; + // The token being processed static char token[TOKEN_MAX_LENGTH]; static unsigned int token_length = 0;@@ -408,6 +434,37 @@ }
return 0; } +// Returns 1 if modes are valid for instr, 0 otherwise. +static int validate_instruction_modes(xyw_context *ctx, xyw_byte instr, xyw_byte modes) +{ + xyw_byte result = 0; + switch (modes) + { + case M_NOM: + result = !((instr & XYW_MODE_X) || (instr & XYW_MODE_Y) || (instr & XYW_MODE_W)); + if (!result) { + token_error("Instruction does not support any modes."); + } + break; + case M_ONW: + result = !((instr & XYW_MODE_X) || (instr & XYW_MODE_Y)); + if (!result) { + token_error("Instruction only supports W mode."); + } + break; + case M_EXC: + result = !((instr & XYW_MODE_X) && (instr & XYW_MODE_Y)); + if (!result) { + token_error("Instruction only supports X or Y mode, not both."); + } + break; + case M_ALL: + result = 1; + break; + } + return result; +} + static int process_instruction(xyw_context *ctx) { (void)ctx;@@ -418,6 +475,7 @@ {
if (strcmpn(token, xyw_instructions[i], 3) == 0) { opcode = (xyw_byte)i; + xyw_byte modes = xyw_instruction_modes[opcode]; // There could be 0 to 3 modes (x y z) after the mnemonic in any order if (token_length > 3) {@@ -439,6 +497,10 @@ else if (mode_char == 'w' || mode_char == 'W')
{ opcode |= XYW_MODE_W; mode_found = 1; + } + if (!validate_instruction_modes(ctx, opcode, modes)) + { + return -1; } if (!mode_found) {