Closed giann closed 1 month ago
Example:
| Say this triggers compilation for (int i = 0; i < 100; i = i + 1) { std.print(">{i}"); }
Bytecode:
006 | OP_HOTSPOT 30 125 -> Will check here if loops deserves to be compiled 008 | OP_GET_LOCAL 3 009 | OP_CONSTANT 4 100 010 | OP_LESS 011 | OP_JUMP_IF_FALSE 11 + 16 -> 28 012 | OP_POP 013 | OP_JUMP 13 + 6 -> 20 014 | OP_GET_LOCAL 3 015 | OP_CONSTANT 5 1 016 | OP_ADD_I 017 | OP_SET_LOCAL 3 018 | OP_POP 019 | OP_LOOP 19 - 14 -> 6 020 008 OP_GET_GLOBAL 1 021 | OP_CONSTANT 6 > 022 | OP_GET_LOCAL 3 023 | OP_TO_STRING 024 | OP_ADD_STRING 025 | OP_CALL 1 0 026 | OP_POP 027 007 OP_LOOP 27 - 14 -> 14 028 | OP_POP 029 | OP_POP
Once compiled, we patch the bytecode to call the hotspot function rather than doing the loop:
006 | OP_JUMP 6 + 21 -> 28 -> We jump directly at the hotspot call 007 | OP_VOID 008 | OP_GET_LOCAL 3 009 | OP_CONSTANT 4 100 010 | OP_LESS 011 | OP_JUMP_IF_FALSE 11 + 16 -> 28 012 | OP_POP 013 | OP_JUMP 13 + 6 -> 20 014 | OP_GET_LOCAL 3 015 | OP_CONSTANT 5 1 016 | OP_ADD_I 017 | OP_SET_LOCAL 3 018 | OP_POP 019 | OP_LOOP 19 - 14 -> 6 020 008 OP_GET_GLOBAL 1 021 | OP_CONSTANT 6 > 022 | OP_GET_LOCAL 3 023 | OP_TO_STRING 024 | OP_ADD_STRING 025 | OP_CALL 1 0 026 | OP_POP 027 007 OP_LOOP 27 - 14 -> 14 028 | OP_CONSTANT 7 native: 0x100d3b720 -> Push the hotspot function 029 | OP_HOTSPOT_CALL -> Call it
We keep the dead bytecode to avoid patching other OP_JUMP outside of this particular loop.
OP_JUMP
Example:
Bytecode:
Once compiled, we patch the bytecode to call the hotspot function rather than doing the loop:
We keep the dead bytecode to avoid patching other
OP_JUMP
outside of this particular loop.