home .. forth .. misc mail list archive ..

No Subject


Here's the C compiler status.  Thanks to Raul Miller for his comments
and getting me thinking about stack conventions, etc.!  Below is a simple
C program (about all I can compile for now :-) ) and the compiled P21 code
in P21Forth assembler format.  I did a quick benchmark test on this code vs.
high level Forth.  The code was only about 3x faster than Forth.  Some
optimization can be done, but maybe C doesn't map onto stack machines all
that well.  There are a couple of manual entries I have to make, as I haven't
solved the "linker" (forward reference) problem yet.  The assembler is
one-pass, so there's no way to compile calls to subroutines not yet defined.
The traditional method is to go back and "fixup" addresses in call and jump
instructions, which is do-able, but not trivial.

-Dave

=============================
int glob;

sub(a)
int a;
{
glob = a;
}

main()
{
int i;

i = 11;
sub(i);
sub(22);
}

=============================
DECIMAL

VARIABLE RPSAVE			\ to save p21forth context
VARIABLE SPSAVE
VARIABLE IPSAVE

256 ALLOT HERE 1- CONSTANT CSTACK

CREATE GLOB 1 ALLOT		\ manual entry - global variable

: <DUP> DUP ;			\ for use inside the assembler
: <+> + ;

ASSEMBLER

CODE CC
PUSH A PUSH IPSAVE #		\ save P21Forth context
A! POP !A SPSAVE #
A! POP !A RPSAVE #
A! POP !A CSTACK #
A! NOP NOP NOP

HERE 1+ <DUP> <DUP> 8 <+> JUMP	\ manual entry - addr of sub and jump to main
				\ sub()
A 1 # NOP NOP			\ accum <- [stack top+1]
+ @A NOP NOP
A PUSH PUSH GLOB #		\ glob <- accum
A! POP !A POP
A! NOP NOP NOP
;' NOP NOP NOP			\ return
				\ main()
				\ lots of optimizer fodder here...
11 # NOP NOP NOP		\ accum <- 11
!A NOP NOP NOP			\ [stack top] <- accum
@A NOP NOP NOP			\ accum <- [stack top]
A DUP DUP COM			\ push accum
XOR NOP NOP +
A! !A NOP NOP
CALL				\ call sub
A 1 # NOP NOP			\ clean up stack after sub
+ NOP A! NOP
22 # NOP NOP NOP		\ accum <- 22
A DUP DUP COM			\ push accum
XOR NOP NOP +
A! !A NOP NOP
CALL				\ call sub
A 1 # NOP NOP			\ clean up stack
+ NOP A! NOP

RPSAVE # NOP A! @A		\ restore P21Forth context
PUSH SPSAVE # NOP A!
@A PUSH IPSAVE # NOP
A! @A NOP A!
POP NEXT
END-CODE

FORTH DEFINITIONS