neverwinter.nim
neverwinter.nim copied to clipboard
scriptcomp: Fold equivalent instructions
Sample script
void main() {
if (1) {
string s = "AAA";
}
}
gets compiled down to
0 JSR, 8
6 RET
8 CONSTANT, TYPE_INTEGER, 1
14 JZ, 41
20 RUNSTACK_ADD, TYPE_STRING
22 CONSTANT, TYPE_STRING, "AAA"
29 ASSIGNMENT, TYPE_VOID, -8, 4
37 MODIFY_STACK_POINTER, -4
43 MODIFY_STACK_POINTER, -4
49 JMP, 6
55 RET
the two MODIFY_STACK_POINTER
instructions could be a single one. There are other such instances.
I think for this to work, we probably need another pass over the generated NCS before writing it out, and then fixing up the jumps. Alternatively, we can just keep track of the last instruction, and if it is the same as current, we modify it instead.