Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I386 improvements #8

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
*.asm
*.o
*.seed
/preForth/preForthdemo
/preForth/preForth
/preForth/forth
/preForth/seedForth
94 changes: 57 additions & 37 deletions preForth/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ test: runseedforthdemo runseedforthinteractive

.PHONY=runseedforthdemo
runseedforthdemo: seedForth seedForthDemo.seed
cat seedForthDemo.seed | ./seedForth
cat seedForthDemo.seed |./seedForth

.PHONY=runseedfortinteractive
runseedforthinteractive: seedForth seedForthInteractive.seed
Expand All @@ -25,52 +25,66 @@ runseedforthinteractive: seedForth seedForthInteractive.seed
UNIXFLAVOUR=$(shell uname -s)
EXT=asm

seedForth-i386.asm: seedForth-i386.pre preForth
cat seedForth-i386.pre | ./preForth >seedForth-i386.asm

# preForth connected to stdin - output to preForth.asm
preForth.asm: preForth.pre preForth-i386-backend.pre load-i386-preForth.fs
cat preForth-i386-rts.pre preForth-rts.pre preForth-i386-backend.pre preForth.pre \
| $(HOSTFORTH) load-i386-preForth.fs >preForth.asm

preForth: preForth.$(UNIXFLAVOUR)
cp preForth.$(UNIXFLAVOUR) preForth

%.asm: %.pre preForth preForth-i386-rts.pre preForth-rts.pre
cat preForth-i386-rts.pre preForth-rts.pre $< | ./preForth >$@

%: %.$(UNIXFLAVOUR)
cp $< $@

preForth.asm: \
preForth-i386-rts.pre \
preForth-rts.pre \
preForth-i386-backend.pre \
preForth.pre \
load-i386-preForth.fs
cat \
preForth-i386-rts.pre \
preForth-rts.pre \
preForth-i386-backend.pre \
preForth.pre \
|$(HOSTFORTH) load-i386-preForth.fs >preForth.asm

%.asm: %.pre preForth-i386-rts.pre preForth-rts.pre preForth
cat preForth-i386-rts.pre preForth-rts.pre $< |./preForth >$@

ifeq ($(UNIXFLAVOUR),Linux)
# assemble and link executable on linux
%.Linux: %.asm
%: %.asm
fasm $< [email protected]
LDEMULATION=elf_i386 ld -arch i386 -o $@ \
-dynamic-linker /lib32/ld-linux.so.2 \
/usr/lib/i386-linux-gnu/crt1.o /usr/lib/i386-linux-gnu/crti.o \
[email protected] \
-lc /usr/lib/i386-linux-gnu/crtn.o
-dynamic-linker /lib32/ld-linux.so.2 \
/usr/lib/i386-linux-gnu/crt1.o /usr/lib/i386-linux-gnu/crti.o \
[email protected] \
-lc /usr/lib/i386-linux-gnu/crtn.o
# rm [email protected]

else
ifeq ($(UNIXFLAVOUR),Darwin)
# assemble and link executable on MacOS
%.Darwin: %.asm
%: %.asm
fasm $< [email protected]
objconv -fmacho32 -nu [email protected] $@_m.o
ld -arch i386 -macosx_version_min 10.6 -o $@ \
$@_m.o /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib/crt1.o /usr/lib/libc.dylib
$@_m.o /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib/crt1.o \
/usr/lib/libc.dylib
# rm [email protected] $@_m.o
endif
endif

# run preForth on its own source code to perform a bootstrap
# should produce identical results
bootstrap: preForth preForth-i386-backend.pre preForth.pre preForth.$(EXT)
cat preForth-i386-rts.pre preForth-rts.pre preForth-i386-backend.pre preForth.pre\
| ./preForth >preForth1.$(EXT)
bootstrap: \
preForth-i386-rts.pre \
preForth-rts.pre \
preForth-i386-backend.pre \
preForth.pre \
preForth \
preForth.$(EXT)
cat \
preForth-i386-rts.pre \
preForth-rts.pre \
preForth-i386-backend.pre \
preForth.pre \
|./preForth >preForth1.$(EXT)
cmp preForth.$(EXT) preForth1.$(EXT)

# preForth connected to stdin - output to stdout
.PHONY=visible-bootstrap
visible-bootstrap: preForth preForth-i386-backend.pre preForth.pre
cat preForth-i386-backend.pre preForth.pre | ./preForth
cat preForth-i386-backend.pre preForth.pre |./preForth

# ------------------------------------------------------------------------
# Docker support (for Linux version)
Expand All @@ -89,16 +103,22 @@ rundocker: docker-image
# ------------------------------------------------------------------------
# seedForth
# ------------------------------------------------------------------------
seedForth.$(EXT): seedForth-i386.pre preForth
cat seedForth-i386.pre | ./preForth >seedForth.$(EXT)

seedForth: seedForth.$(UNIXFLAVOUR)
cp seedForth.$(UNIXFLAVOUR) seedForth
seedForth.$(EXT): \
seedForth-i386-header.pre \
preForth-i386-rts.pre \
seedForth-i386.pre \
seedForth.pre \
preForth
cat \
seedForth-i386-header.pre \
preForth-i386-rts.pre \
seedForth-i386.pre \
seedForth.pre \
|./preForth >seedForth.$(EXT)

%.seed: %.seedsource seedForth-tokenizer.fs
gforth seedForth-tokenizer.fs $<


.PHONY=clean
clean:
rm -f *.asm *.o *.fas *.s *.c *.Darwin *.Linux preForthdemo preForth forth seedForth seedForthDemo.seed seedForthInteractive.seed
rm -f *.asm *.o *.seed preForthdemo preForth seedForth
71 changes: 29 additions & 42 deletions preForth/preForth-i386-backend.pre
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
'Q' swap '?' case? ?exit nip
'R' swap '"' case? ?exit nip
\ 'S' swap '!' case? ?exit nip
'T' swap '*' case? ?exit nip
'T' swap '*' case? ?exit nip
'U' swap '(' case? ?exit nip
'V' swap '|' case? ?exit nip
'W' swap ',' case? ?exit nip
Expand All @@ -42,22 +42,13 @@
\ output words
\ ------------
\ Output is done by emit.
\ We define convenience words of the form ."xxx" to output xxx and >"xxx" to output xxx on a new line indented by a tab.
\ We define convenience words of the form ."xxx" to output xxx (maybe tabbed)

: ."dd" ( -- )
'D' emit 'D' emit space ;

: >"dd" ( -- )
cr tab ."dd" ;
tab 'd' emit 'd' emit tab ;

: ."db" ( -- )
'D' emit 'B' emit space ;

: >"db" ( -- )
cr tab ."db" ;

: >"ds" ( -- )
cr tab 'D' emit 'S' emit space ;
tab 'd' emit 'b' emit tab ;

: ."nest" ( -- )
'n' 'e' 's' 't' 4 alter show ;
Expand All @@ -74,55 +65,50 @@

\ ,string compiles the topmost string as a sequence of numeric DB values.
: ,string ( S -- )
\ ."ds" show ;
?dup 0= ?exit
dup roll >"db" u. \ 1st char
dup roll ."db" u. cr \ 1st char
1- ,string ;

\ reproduce a verbatim line
: ,line ( x1 ...cn n -- )
show ;

\ compile a reference to an invoked word
\ compile a reference to an invoked word
: ,word ( S -- )
."dd" alter show ;

\ compile a reference to an invoked word on a new line
: ,>word ( S -- )
>"dd" alter show ;
."dd" alter show cr ;

\ compile reference to nest primitive
: ,nest ( -- )
."dd" ."nest" ;
."dd" ."nest" cr ;

\ compile reference to unnest primitive
: ,unnest ( -- )
>"dd" ."unnest"
cr ;
."dd" ."unnest" cr cr ;

\ compile signed number
: ,n ( n -- )
>"dd" . ;
."dd" . cr ;

\ compile unsigned number
: ,u ( u -- )
>"dd" u. ;
."dd" u. cr ;

\ compile literal
: ,_lit ( S -- )
>"dd" ."lit" ,>word ;
."dd" ."lit" cr ,word ;

\ compile literal
: ,lit ( x -- )
>"dd" ."lit" ,n ;
."dd" ."lit" cr ,n ;

\ output string as comment
: ,comment ( S -- )
cr tab ';' emit space show ;
tab ';' emit space show cr ;

\ create a new symbolic label
\ if label is 6 characters or less, stay on same line for following code
: label ( S -- )
cr alter show ':' emit tab ;
alter dup >r show ':' emit r> 7 - 0< ?exit cr ;

\ body calculates the name of the body from a token
: body ( S1 -- S2 )
Expand All @@ -138,7 +124,7 @@

: ,end-code ( -- )
cr ;

\ -----------------------------------
\ tail call optimization tail word ; -> [ ' word >body ] literal >r ;

Expand All @@ -148,19 +134,20 @@
\ ,tail compiles a tail call
: ,tail ( S -- )
body ,_lit
'>' 'r' 2 ,>word ;

: ."done" ( -- )
';' emit space 'd' emit 'o' emit 'n' emit 'e' emit ;
'>' 'r' 2 ,word ;

: ."last:" ( -- )
';' emit space 'l' emit 'a' emit 's' emit 't' emit ':' emit space ;
\ : ."done" ( -- )
\ ';' emit space 'd' emit 'o' emit 'n' emit 'e' emit ;
\
\ : ."last:" ( -- )
\ ';' emit space 'l' emit 'a' emit 's' emit 't' emit ':' emit space ;

: ,end ( S -- )
cr ."last:" alter show
cr ."done" cr ;
\ cr ."last:" alter show
\ cr ."done" cr
;

\ create a new header with given name S2 and flags - do nothing
: header ( S1 S2 flags -- S3 S2 )
drop ;
\ \ create a new header with given name S2 and flags - do nothing
\ : header ( S1 S2 flags -- S3 S2 )
\ drop ;

Loading