Story:
In the interpreter loop (or in another state machine) there are several ways to send operations (or transition state based on the next input). The obvious way in C is with the switch statement in a loop. However, there are performance issues using the switch inside the loop. In the worst case, the switch can be encoded as a sequence of branches, which happens if the op encoding is not continuous (or the compiler cannot output it continuous). Even in the optimistic case, when the compiler generates a jump table, there is still an excessive jump back to the dispatcher (I have not seen the compiler that the dispatcher sends when viewing the switch in a loop). In addition, a superfluous assessment is usually performed in the dispatcher.
This problem was encountered by the GCC team, introducing Computed Gotos. This language extension allows you to reference a label, which can later be skipped using gotoa label in the pointer. This generates perfect assembler code: a table with an indexed index, indexed by index, bound to an index with built-in dispatch after each operation.
Question:
Is there a way using ISO C to create a similar build code?
Notes:
, , , . : , . .
, (GAS.intel_syntax, NASM)
.intel_syntax noprefix
.section .rodata
optable:
.quad nop
.quad add
.section .text
nop:
call nextinput # return value = table index in rax
jmp [optable + rax]
add:
call nextinput
jmp [optable + rax]