Skip to content

Commit 3ded525

Browse files
committed
[[ LCB ]] Generalize bytecode op format
This patch defines constants which describe the layout of the VM's bytecode op byte - allowing the number of bits devoted to op code vs arity to change by altering the definition in the script-private.h header.
1 parent c97e686 commit 3ded525

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

libscript/src/script-builder.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,7 +1315,7 @@ static uindex_t __measure_instruction(MCScriptModuleBuilderRef self, MCScriptByt
13151315
uindex_t t_size;
13161316
t_size = 1;
13171317

1318-
if (p_instruction -> arity >= 15)
1318+
if (p_instruction -> arity >= kMCScriptBytecodeOpArityMax)
13191319
t_size += 1;
13201320

13211321
if (p_instruction -> operation == kMCScriptBytecodeOpJump)
@@ -1567,10 +1567,10 @@ void MCScriptEndHandlerInModule(MCScriptModuleBuilderRef self)
15671567

15681568
__emit_position(self, t_pos_address_offset + t_address, self -> instructions[i] . file, self -> instructions[i] . line);
15691569

1570-
__emit_bytecode_byte(self, (uint8_t)(t_op | (MCMin(t_arity, 15U) << 4)));
1570+
__emit_bytecode_byte(self, (uint8_t)(t_op | (MCMin(t_arity, kMCScriptBytecodeOpArityMax) << kMCScriptBytecodeOpArityShift)));
15711571

1572-
if (t_arity >= 15U)
1573-
__emit_bytecode_byte(self, uint8_t(t_arity - 15U));
1572+
if (t_arity >= kMCScriptBytecodeOpArityMax)
1573+
__emit_bytecode_byte(self, uint8_t(t_arity - kMCScriptBytecodeOpArityMax));
15741574

15751575
for(uindex_t j = 0; j < t_arity; j++)
15761576
__emit_bytecode_uint(self, t_operands[j]);

libscript/src/script-private.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,17 @@ MCScriptCreateErrorExpectedError(MCErrorRef& r_error);
670670
// present then the instruction requires 15 + extension byte arguments up to a
671671
// maximum of 256.
672672

673+
enum
674+
{
675+
kMCScriptBytecodeOpCodeMask = 0x0f,
676+
kMCScriptBytecodeOpCodeShift = 0,
677+
kMCScriptBytecodeOpCodeMax = kMCScriptBytecodeOpCodeMask >> kMCScriptBytecodeOpCodeShift,
678+
679+
kMCScriptBytecodeOpArityMask = 0xf0,
680+
kMCScriptBytecodeOpArityShift = 4,
681+
kMCScriptBytecodeOpArityMax = kMCScriptBytecodeOpArityMask >> kMCScriptBytecodeOpArityShift,
682+
};
683+
673684
enum MCScriptBytecodeOp
674685
{
675686
kMCScriptBytecodeOp__First,
@@ -793,14 +804,14 @@ MCScriptBytecodeDecodeOp(const byte_t*& x_bytecode_ptr,
793804

794805
// The lower nibble is the bytecode operation.
795806
MCScriptBytecodeOp t_op;
796-
t_op = (MCScriptBytecodeOp)(t_op_byte & 0xf);
807+
t_op = (MCScriptBytecodeOp)((t_op_byte & kMCScriptBytecodeOpCodeMask) >> kMCScriptBytecodeOpCodeShift);
797808

798809
// The upper nibble is the arity.
799810
uindex_t t_arity;
800-
t_arity = (t_op_byte >> 4);
811+
t_arity = (t_op_byte & kMCScriptBytecodeOpArityMask) >> kMCScriptBytecodeOpArityShift;
801812

802813
// If the arity is 15, then overflow to a subsequent byte.
803-
if (t_arity == 15)
814+
if (t_arity == kMCScriptBytecodeOpArityMax)
804815
t_arity += *x_bytecode_ptr++;
805816

806817
r_op = t_op;

0 commit comments

Comments
 (0)