I think I'll stick with straight contants for now.. but I might look into that later.
One thing I've noticed while disassembling AGI (and further proof they used a compiler of some sorts for parts of it) is their implementation of switch statements.
They always had the switch statement like this (pseudo):
Code: Select all
goto switch;
one:
code
two:
code
three:
code
default:
code
goto next;
switch:
if 1 goto one;
if 2 goto two;
if 3 goto three
goto default;
/* jump tables were also used */
next:
I hope that makes sense. the point I'm trying to make is that they always had the statement block first and then the switch jump table
I finally realised the reason why when I started doing my own: single pass compilers.
If you were to put all the switch jumps at the start.. you would have to parse the statements first to look for any case labels, store that in memory somewhere, print out your switch jump table and then the saved statements.
Storing all that data could take a lot of space. And you don't want to have to parse the statments twice because your parser might not allow it.
Alternatively, you could save a bit of space to insert a goto, print out your statement block, and then your switch jump table. once you know where you jump table is, you just have to fill in the proper address at the start via backpatching.
umm. I thought it was interesting.. but kinda hard to explain
- Nick