X-Git-Url: https://pd.if.org/git/?p=lice;a=blobdiff_plain;f=tests%2Fcgoto.c;fp=tests%2Fcgoto.c;h=b7c06a95fbb426e1074aabcd081c38f65f769d41;hp=0000000000000000000000000000000000000000;hb=f1abb26903687c7967cf37c3f6c830051bdeb371;hpb=bb650f4a52a456c1aa0a34508d6f3dcce58291b6 diff --git a/tests/cgoto.c b/tests/cgoto.c new file mode 100644 index 0000000..b7c06a9 --- /dev/null +++ b/tests/cgoto.c @@ -0,0 +1,50 @@ +// labels as values (computed goto) + +int main(void) { + // implement a small dispatch table of instructions for a mini + // state machine, to test computed goto + + // test state + int instruction_0_state = 0; + int instruction_1_state = 0; + int instruction_2_state = 0; + + unsigned char bytecode[] = {0x01, 0x05, 0x01, 0x02, 0x02, 0x03}; + unsigned char *pc = bytecode; + + void *dispatch[] = { + 0, + &&instruction_0, + &&instruction_1, + &&instruction_2 + }; + + #define DISPATCH(INDEX) \ + do { \ + pc += (INDEX); \ + goto *dispatch[*pc]; \ + } while (0) + + // machine loop + do { + + instruction_0: + instruction_0_state += *(pc + 1); + DISPATCH(2); + + instruction_1: + instruction_1_state ++; + DISPATCH(1); + + instruction_2: + instruction_2_state --; + break; + + } while (1); + + expecti(instruction_0_state, 7); + expecti(instruction_1_state, 1); + expecti(instruction_2_state, -1); + + return 0; +}