bugfixes and cleanup; use tinyjort to bootstrap itself!

This commit is contained in:
Jeremy Penner 2023-09-09 14:08:36 -04:00
parent c01f114655
commit 36066a6f93
6 changed files with 36 additions and 28 deletions

View file

@ -56,7 +56,6 @@ array oparg2 3 cells allot
target @ >r encode-op <r target @ = target @ >r encode-op <r target @ =
if s" invalid argument types" operror then ; if s" invalid argument types" operror then ;
: oparg-complete! : oparg-complete!
dbg" oparg-complete!"
opargs-remaining @ dup if opargs-remaining @ dup if
1 - dup opargs-remaining ! 1 - dup opargs-remaining !
if arg2 if arg2
@ -237,7 +236,6 @@ var ignoreimm
1 :op LOOP 0xe2 >short-jmp* ; 1 :op LOOP 0xe2 >short-jmp* ;
1 :op JCXZ 0xe3 >short-jmp* ; 1 :op JCXZ 0xe3 >short-jmp* ;
1 :op JMP 1 :op JMP
dbg" JMP!"
0xe9 >near-reljmp* 0xe9 >near-reljmp*
0xeb >short-jmp* 0xeb >short-jmp*
0xea >far-jmp* 0xea >far-jmp*

View file

@ -1,2 +1,2 @@
minijort < tinyjort.jrt jort.com < tinyjort.jrt
copy tinyjort.com jort.com copy tinyjort.com jort.com

View file

@ -1,3 +0,0 @@
s" defs.jrt" loadfile
dbg" debugging!"

BIN
jort.com

Binary file not shown.

Binary file not shown.

View file

@ -14,8 +14,8 @@ dbg" assembling..."
increment the instruction pointer. increment the instruction pointer.
) )
JMP dbg" JMP" 9 @> JMP 9 @>
dbg" first jmp"
: NEXT : NEXT
LODSW LODSW
MOV BX AX MOV BX AX
@ -42,7 +42,7 @@ L: LATEST 0 w>t
0x0f const BUCKETMASK 0x0f const BUCKETMASK
: savelabel ( word -- ) : savelabel ( word -- )
dup type s" : " type ( dup type s" : " type )
here swap begin dup b@ dup while b, 1+ repeat b, drop lastlabel ! ; here swap begin dup b@ dup while b, 1+ repeat b, drop lastlabel ! ;
: DICTLIST ( word -- tptr ) b@ BUCKETMASK & cells [ L@ DICTIONARY lit ] + ; : DICTLIST ( word -- tptr ) b@ BUCKETMASK & cells [ L@ DICTIONARY lit ] + ;
: strlen ( word -- len ) 0 swap begin dup b@ while swap 1+ swap 1+ repeat drop ; : strlen ( word -- len ) 0 swap begin dup b@ while swap 1+ swap 1+ repeat drop ;
@ -52,7 +52,7 @@ L: LATEST 0 w>t
: link>t ( tptr-head -- ) dup @t swap patch!t w>t ; : link>t ( tptr-head -- ) dup @t swap patch!t w>t ;
: DEF target @ [ L@ LATEST lit ] !t : DEF target @ [ L@ LATEST lit ] !t
word dup savelabel dup DICTLIST link>t dup strlen w>t str>t word dup savelabel dup DICTLIST link>t dup strlen w>t str>t
target @ cell + .hex cr ; ( target @ cell + .hex cr ) ;
: WORD= ( word len tptr -- f ) : WORD= ( word len tptr -- f )
cell + dup b@t <rot != if 2drop 0 return then cell + ( word tword ) cell + dup b@t <rot != if 2drop 0 return then cell + ( word tword )
@ -81,6 +81,7 @@ L: $$CONST
L@ $$CONST CONST $DOCONST L@ $$CONST CONST $DOCONST
L@ DICTIONARY CONST dictionary L@ DICTIONARY CONST dictionary
L@ LATEST CONST &latest L@ LATEST CONST &latest
0 CONST 0 1 CONST 1
L: $$VAR L: $$VAR
INC BX INC BX INC BX INC BX
@ -116,7 +117,7 @@ L@ $$VAR CONST $DOVAR
MOV @[ BP] SI MOV @[ BP] SI
INC BP INC BP INC BP INC BP
INC BX INC BX INC BX INC BX
MOV SI BX MOV SI @[ BX]
INC BX INC BX PUSH BX INC BX INC BX PUSH BX
NEXT NEXT
@ -125,6 +126,8 @@ L@ $$VAR CONST $DOVAR
PUSH AX PUSH AX
NEXT NEXT
:ASM noop NEXT
( some helpers for making manually defining colon words less ugly ) ( some helpers for making manually defining colon words less ugly )
: '>t t' w>t ; : '>t t' w>t ;
@ -523,10 +526,9 @@ dbg" i/o"
-1 CONST EOF -1 CONST EOF
0 VAR, fbuffer 0 VAR, fbuffer
: skip>t 1+ cells target @ + w>t ;
:t fgetc ( fp -- c ) :t fgetc ( fp -- c )
1 fbuffer <rot fread fbuffer ub@ 1 fbuffer <rot fread fbuffer ub@
fcount @ not BZ_ [ 2 skip>t ] drop EOF ; fcount @ not BZ_ [ patchpt ] drop EOF [ patch!t ] ;
:t fputc ( c fp -- ) swap fbuffer b! 1 fbuffer <rot fwrite ; :t fputc ( c fp -- ) swap fbuffer b! 1 fbuffer <rot fwrite ;
:ASM console-emit :ASM console-emit
@ -559,7 +561,7 @@ t& inbuf VAR, inptr
0 VAR, infile ( 0 is a predefined file handle meaning stdin ) 0 VAR, infile ( 0 is a predefined file handle meaning stdin )
:t stdin-key :t stdin-key
buf-key dup BZ_ [ 1 skip>t ] return drop buf-key dup BZ_ [ patchpt ] return [ patch!t ] drop
( if buffer is empty, refresh from file ) ( if buffer is empty, refresh from file )
INBUFSIZE inbuf 0 fread INBUFSIZE inbuf 0 fread
( if there's any more data, start returning it ) ( if there's any more data, start returning it )
@ -728,7 +730,7 @@ L: write-next-digit
0 [ target @ ] drop key dup whitespace? not BZ_ [ w>t ] 0 [ target @ ] drop key dup whitespace? not BZ_ [ w>t ]
( consume non-whitespace / eoi characters ) ( consume non-whitespace / eoi characters )
[ target @ ] dup whitespace? over eoi? or [ target @ ] dup whitespace? over eoi? or
( if whitespace or eoi, end ) BZ_ [ 5 skip>t ] drop 0 b, return ( if whitespace or eoi, end ) BZ_ [ patchpt ] drop 0 b, return [ patch!t ]
( otherwise, write byte and continue ) b, key GOTO_ [ w>t ] ; ( otherwise, write byte and continue ) b, key GOTO_ [ w>t ] ;
ARRAY wordbuf 48 ALLOT ARRAY wordbuf 48 ALLOT
:t redir-, ( cp buf -- buf ) here >r dup >r here! execute <r <r here! ; :t redir-, ( cp buf -- buf ) here >r dup >r here! execute <r <r here! ;
@ -751,8 +753,8 @@ dbg" compiler"
( link bucket to new ) latest swap ! ; ( link bucket to new ) latest swap ! ;
:t 2inc ( x y -- x+1 y+1 ) 1+ swap 1+ swap ; :t 2inc ( x y -- x+1 y+1 ) 1+ swap 1+ swap ;
:t strlen ( name -- len ) :t strlen ( name -- len )
0 swap [ target @ ] dup b@ BZ_ [ 3 skip>t ] 0 swap [ target @ ] dup b@ BZ_ [ patchpt ]
2inc GOTO_ [ w>t ] drop ; 2inc GOTO_ [ swap w>t patch!t ] drop ;
:ASM dict-lookup ( name dict -- cp meta | name 0 ) :ASM dict-lookup ( name dict -- cp meta | name 0 )
MOV AX DS MOV AX DS
@ -828,13 +830,31 @@ L: check-next-entry
:t ?err ( word -- ) type LIT_ [ key ? w>t ] emit cr ; :t ?err ( word -- ) type LIT_ [ key ? w>t ] emit cr ;
DEFERRED err ?err DEFERRED err ?err
:ASM sp+ss
PUSH SP
PUSH SS
NEXT
:ASM underflow?
CMP SP 0x100 #
JLE 0 @>
MOV SP 0x100 #
PUSH TRUE
NEXT
0 <:
PUSH FALSE
NEXT
:t checkstack underflow? BZ_ [ patchpt ]
INLINEDATA_ [ patchpt t" underflow!" patch!t ] type cr [ patch!t ] ;
:t compileword ( word -- ) :t compileword ( word -- )
lookup dup BZ_ [ patchpt ] interpretword return [ patch!t ] lookup dup BZ_ [ patchpt ] interpretword return [ patch!t ]
drop number BZ_ [ patchpt ] interpretnumber return [ patch!t ] drop number BZ_ [ patchpt ] interpretnumber return [ patch!t ]
err ; err ;
:t interpreter :t interpreter
[ target @ ] word dup b@ BZ_ [ patchpt ] compileword GOTO_ [ swap w>t ] [ target @ ] noop ( f28 ) word noop ( f2c ) dup b@ BZ_
[ patch!t ] drop ; [ patchpt ] noop ( f36 ) compileword checkstack
GOTO_ [ swap w>t patch!t ] noop ( f40 ) drop ;
dbg" flow control words and misc." dbg" flow control words and misc."
:t if LIT_ BZ_ , here 0 , ; IMMEDIATE :t if LIT_ BZ_ , here 0 , ; IMMEDIATE
@ -857,7 +877,7 @@ dbg" flow control words and misc."
:t |; LIT_ return , |inline ; IMMEDIATE :t |; LIT_ return , |inline ; IMMEDIATE
:t s", [ target @ ] key dup LIT_ [ key " w>t ] != over 0 != and :t s", [ target @ ] key dup LIT_ [ key " w>t ] != over 0 != and
BZ_ [ 3 skip>t ] b, GOTO_ [ w>t ] BZ_ [ patchpt ] b, GOTO_ [ swap w>t patch!t ]
drop 0 b, ; drop 0 b, ;
:t s" state BZ_ [ patchpt ] inline| s", |inline return [ patch!t ] :t s" state BZ_ [ patchpt ] inline| s", |inline return [ patch!t ]
LIT_ s", tmp-, ; IMMEDIATE LIT_ s", tmp-, ; IMMEDIATE
@ -871,22 +891,15 @@ dbg" flow control words and misc."
<r infile ! ; <r infile ! ;
:t loadfile ( filename -- ) open loadfp close ; :t loadfile ( filename -- ) open loadfp close ;
:t noop ;
:t defer new-word $DODEFERRED , LIT_ noop , ; :t defer new-word $DODEFERRED , LIT_ noop , ;
:t redefine ( cp cpdeferred ) cell + ! ; :t redefine ( cp cpdeferred ) cell + ! ;
:t definition ( cpdeferred ) cell + @ ; :t definition ( cpdeferred ) cell + @ ;
:ASM sp+ss
PUSH SP
PUSH SS
NEXT
:t .s sp+ss swap >r 0x100 [ target @ ] 2 - dup r@ < BZ_ :t .s sp+ss swap >r 0x100 [ target @ ] 2 - dup r@ < BZ_
( past top of stack ) [ 4 skip>t ] drop drop rdrop return ( past top of stack ) [ patchpt ] drop drop rdrop return [ patch!t ]
2dup swap @far . GOTO_ [ w>t ] ; 2dup swap @far . GOTO_ [ w>t ] ;
dbg" boot stub" dbg" boot stub"
:ASM debug NEXT
:t tinyjort interpreter terminate ; :t tinyjort interpreter terminate ;
9 <: ( actual entry point ) 9 <: ( actual entry point )