Random note: The SDK compiler generates some pretty strange code for
'n++' when 'n' is a reference:
instruction state of stack after instruction
----------- --------------------------------
PUSHLOCALPAGE ; P(ref n)
PUSH <variable index of n> ; P(ref n),V(ref n)
REFREF ; P(n),V(n)
DUP2 ; P(n),V(n),P(n),V(n)
REF ; P(n),V(n),n
DUP_X2 ; n,P(n),V(n),n
POP ; n,P(n),V(n)
INC ; n
POP ;
This would seem to be completely equivalent to the following:
PUSHLOCALPAGE
PUSH <variable index of n>
REFREF
INC
Use an array of unions for the stack. I'm still not sure if System40.exe
uses separate stack for string operations or not. Need to check if POP
after S_PUSH removes the pushed string.
A bit of a hack: we insert an extra 'CALLSYS 0x0' instruction
(system.Exit) at the end of the code section, then set up a stack frame
so that when main() returns, it jumps to that instruction.
An assortment of instructions have been implemented so far, mostly basic
stuff such as stack management. The following program can be executed:
int main(void)
{
hello(2);
for (;;) {
system.Peek();
system.Sleep(1);
}
return 0;
}
void hello(int n)
{
int i;
for (i = 0; i < n; i++) {
system.Output("Hello, world! " + string(i));
}
}