| 1 : |
anton
|
1.16
|
\ converts primitives to, e.g., C code |
| 2 : |
|
|
|
| 3 : |
anton
|
1.35
|
\ Copyright (C) 1995,1996,1997,1998 Free Software Foundation, Inc. |
| 4 : |
anton
|
1.16
|
|
| 5 : |
|
|
\ This file is part of Gforth. |
| 6 : |
|
|
|
| 7 : |
|
|
\ Gforth is free software; you can redistribute it and/or |
| 8 : |
|
|
\ modify it under the terms of the GNU General Public License |
| 9 : |
|
|
\ as published by the Free Software Foundation; either version 2 |
| 10 : |
|
|
\ of the License, or (at your option) any later version. |
| 11 : |
|
|
|
| 12 : |
|
|
\ This program is distributed in the hope that it will be useful, |
| 13 : |
|
|
\ but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 : |
|
|
\ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 : |
|
|
\ GNU General Public License for more details. |
| 16 : |
|
|
|
| 17 : |
|
|
\ You should have received a copy of the GNU General Public License |
| 18 : |
|
|
\ along with this program; if not, write to the Free Software |
| 19 : |
|
|
\ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 : |
|
|
|
| 21 : |
|
|
|
| 22 : |
anton
|
1.1
|
\ This is not very nice (hard limits, no checking, assumes 1 chars = 1) |
| 23 : |
|
|
|
| 24 : |
|
|
\ Optimizations: |
| 25 : |
|
|
\ superfluous stores are removed. GCC removes the superfluous loads by itself |
| 26 : |
|
|
\ TOS and FTOS can be kept in register( variable)s. |
| 27 : |
|
|
\ |
| 28 : |
|
|
\ Problems: |
| 29 : |
|
|
\ The TOS optimization is somewhat hairy. The problems by example: |
| 30 : |
|
|
\ 1) dup ( w -- w w ): w=TOS; sp-=1; sp[1]=w; TOS=w; |
| 31 : |
|
|
\ The store is not superfluous although the earlier opt. would think so |
| 32 : |
|
|
\ Alternatively: sp[0]=TOS; w=TOS; sp-=1; TOS=w; |
| 33 : |
|
|
\ 2) ( -- .. ): sp[0] = TOS; ... /* This additional store is necessary */ |
| 34 : |
|
|
\ 3) ( .. -- ): ... TOS = sp[0]; /* as well as this load */ |
| 35 : |
|
|
\ 4) ( -- ): /* but here they are unnecessary */ |
| 36 : |
|
|
\ 5) Words that call NEXT themselves have to be done very carefully. |
| 37 : |
|
|
\ |
| 38 : |
|
|
\ To do: |
| 39 : |
pazsan
|
1.8
|
\ add the store optimization for doubles |
| 40 : |
anton
|
1.1
|
\ regarding problem 1 above: It would be better (for over) to implement |
| 41 : |
|
|
\ the alternative |
| 42 : |
|
|
|
| 43 : |
pazsan
|
1.3
|
warnings off |
| 44 : |
|
|
|
| 45 : |
jwilke
|
1.39
|
[IFUNDEF] vocabulary \ we are executed just with kernel image |
| 46 : |
|
|
\ load the rest that is needed |
| 47 : |
|
|
\ (require fails because this file is needed from a |
| 48 : |
|
|
\ different directory with the wordlibraries) |
| 49 : |
|
|
include ./search.fs |
| 50 : |
|
|
include ./extend.fs |
| 51 : |
anton
|
1.40
|
[THEN] |
| 52 : |
|
|
|
| 53 : |
|
|
[IFUNDEF] environment? |
| 54 : |
jwilke
|
1.39
|
include ./environ.fs |
| 55 : |
|
|
[THEN] |
| 56 : |
pazsan
|
1.25
|
|
| 57 : |
jwilke
|
1.39
|
include ./gray.fs |
| 58 : |
anton
|
1.1
|
|
| 59 : |
|
|
100 constant max-effect \ number of things on one side of a stack effect |
| 60 : |
|
|
255 constant maxchar |
| 61 : |
|
|
maxchar 1+ constant eof-char |
| 62 : |
anton
|
1.17
|
#tab constant tab-char |
| 63 : |
|
|
#lf constant nl-char |
| 64 : |
anton
|
1.1
|
|
| 65 : |
|
|
: read-whole-file ( c-addr1 file-id -- c-addr2 ) |
| 66 : |
|
|
\ reads the contents of the file file-id puts it into memory at c-addr1 |
| 67 : |
|
|
\ c-addr2 is the first address after the file block |
| 68 : |
anton
|
1.23
|
>r dup $7fffffff r> read-file throw + ; |
| 69 : |
anton
|
1.1
|
|
| 70 : |
anton
|
1.18
|
variable rawinput \ pointer to next character to be scanned |
| 71 : |
|
|
variable endrawinput \ pointer to the end of the input (the char after the last) |
| 72 : |
|
|
variable cookedinput \ pointer to the next char to be parsed |
| 73 : |
anton
|
1.17
|
variable line \ line number of char pointed to by input |
| 74 : |
|
|
1 line ! |
| 75 : |
|
|
2variable filename \ filename of original input file |
| 76 : |
|
|
0 0 filename 2! |
| 77 : |
pazsan
|
1.25
|
2variable f-comment |
| 78 : |
|
|
0 0 f-comment 2! |
| 79 : |
anton
|
1.17
|
variable skipsynclines \ are sync lines ("#line ...") invisible to the parser? |
| 80 : |
|
|
skipsynclines on |
| 81 : |
anton
|
1.1
|
|
| 82 : |
pazsan
|
1.26
|
Variable flush-comment flush-comment off |
| 83 : |
|
|
|
| 84 : |
|
|
: ?flush-comment |
| 85 : |
pazsan
|
1.34
|
flush-comment @ 0= ?EXIT |
| 86 : |
|
|
f-comment 2@ nip |
| 87 : |
|
|
IF cr f-comment 2@ 2 /string 1- |
| 88 : |
|
|
dup IF |
| 89 : |
pazsan
|
1.36
|
2dup s" -" compare 0= |
| 90 : |
|
|
IF |
| 91 : |
|
|
flush-comment @ 1 = |
| 92 : |
|
|
IF ." #else" |
| 93 : |
|
|
ELSE ." [ELSE]" THEN |
| 94 : |
|
|
ELSE |
| 95 : |
|
|
flush-comment @ 1 = |
| 96 : |
|
|
IF ." #ifdef HAS_" bounds ?DO I c@ toupper emit LOOP |
| 97 : |
|
|
ELSE ." has? " type ." [IF]" THEN |
| 98 : |
|
|
THEN cr |
| 99 : |
pazsan
|
1.34
|
ELSE flush-comment @ 1 = IF ." #endif" ELSE ." [THEN]" THEN |
| 100 : |
|
|
cr THEN |
| 101 : |
|
|
0 0 f-comment 2! THEN ; |
| 102 : |
pazsan
|
1.26
|
|
| 103 : |
anton
|
1.1
|
: start ( -- addr ) |
| 104 : |
anton
|
1.18
|
cookedinput @ ; |
| 105 : |
anton
|
1.1
|
|
| 106 : |
|
|
: end ( addr -- addr u ) |
| 107 : |
anton
|
1.18
|
cookedinput @ over - ; |
| 108 : |
anton
|
1.1
|
|
| 109 : |
|
|
variable output \ xt ( -- ) of output word |
| 110 : |
|
|
|
| 111 : |
|
|
: printprim ( -- ) |
| 112 : |
|
|
output @ execute ; |
| 113 : |
|
|
|
| 114 : |
|
|
: field |
| 115 : |
|
|
<builds-field ( n1 n2 -- n3 ) |
| 116 : |
|
|
does> ( addr1 -- addr2 ) |
| 117 : |
|
|
@ + ; |
| 118 : |
|
|
|
| 119 : |
|
|
: const-field |
| 120 : |
|
|
<builds-field ( n1 n2 -- n3 ) |
| 121 : |
|
|
does> ( addr -- w ) |
| 122 : |
|
|
@ + @ ; |
| 123 : |
|
|
|
| 124 : |
|
|
struct |
| 125 : |
|
|
2 cells field item-name |
| 126 : |
|
|
cell field item-d-offset |
| 127 : |
|
|
cell field item-f-offset |
| 128 : |
|
|
cell field item-type |
| 129 : |
|
|
constant item-descr |
| 130 : |
|
|
|
| 131 : |
|
|
2variable forth-name |
| 132 : |
|
|
2variable wordset |
| 133 : |
|
|
2variable c-name |
| 134 : |
|
|
2variable doc |
| 135 : |
|
|
2variable c-code |
| 136 : |
|
|
2variable forth-code |
| 137 : |
|
|
2variable stack-string |
| 138 : |
|
|
create effect-in max-effect item-descr * allot |
| 139 : |
|
|
create effect-out max-effect item-descr * allot |
| 140 : |
|
|
variable effect-in-end ( pointer ) |
| 141 : |
|
|
variable effect-out-end ( pointer ) |
| 142 : |
|
|
2variable effect-in-size |
| 143 : |
|
|
2variable effect-out-size |
| 144 : |
anton
|
1.17
|
variable c-line |
| 145 : |
|
|
2variable c-filename |
| 146 : |
|
|
variable name-line |
| 147 : |
|
|
2variable name-filename |
| 148 : |
|
|
2variable last-name-filename |
| 149 : |
anton
|
1.1
|
|
| 150 : |
pazsan
|
1.14
|
variable primitive-number -10 primitive-number ! |
| 151 : |
pazsan
|
1.30
|
Variable function-number 0 function-number ! |
| 152 : |
anton
|
1.1
|
|
| 153 : |
|
|
\ for several reasons stack items of a word are stored in a wordlist |
| 154 : |
|
|
\ since neither forget nor marker are implemented yet, we make a new |
| 155 : |
|
|
\ wordlist for every word and store it in the variable items |
| 156 : |
|
|
variable items |
| 157 : |
|
|
|
| 158 : |
|
|
\ a few more set ops |
| 159 : |
|
|
|
| 160 : |
|
|
: bit-equivalent ( w1 w2 -- w3 ) |
| 161 : |
|
|
xor invert ; |
| 162 : |
|
|
|
| 163 : |
|
|
: complement ( set1 -- set2 ) |
| 164 : |
|
|
empty ['] bit-equivalent binary-set-operation ; |
| 165 : |
|
|
|
| 166 : |
|
|
\ the parser |
| 167 : |
|
|
|
| 168 : |
|
|
eof-char max-member \ the whole character set + EOF |
| 169 : |
|
|
|
| 170 : |
|
|
: getinput ( -- n ) |
| 171 : |
anton
|
1.18
|
rawinput @ endrawinput @ = |
| 172 : |
anton
|
1.1
|
if |
| 173 : |
anton
|
1.18
|
eof-char |
| 174 : |
anton
|
1.1
|
else |
| 175 : |
anton
|
1.18
|
cookedinput @ c@ |
| 176 : |
anton
|
1.1
|
endif ; |
| 177 : |
|
|
|
| 178 : |
|
|
:noname ( n -- ) |
| 179 : |
|
|
dup bl > if |
| 180 : |
|
|
emit space |
| 181 : |
|
|
else |
| 182 : |
|
|
. |
| 183 : |
|
|
endif ; |
| 184 : |
|
|
print-token ! |
| 185 : |
|
|
|
| 186 : |
|
|
: testchar? ( set -- f ) |
| 187 : |
|
|
getinput member? ; |
| 188 : |
|
|
' testchar? test-vector ! |
| 189 : |
|
|
|
| 190 : |
anton
|
1.17
|
: checksyncline ( -- ) |
| 191 : |
|
|
\ when input points to a newline, check if the next line is a |
| 192 : |
|
|
\ sync line. If it is, perform the appropriate actions. |
| 193 : |
anton
|
1.18
|
rawinput @ >r |
| 194 : |
anton
|
1.17
|
s" #line " r@ over compare 0<> if |
| 195 : |
|
|
rdrop 1 line +! EXIT |
| 196 : |
|
|
endif |
| 197 : |
|
|
0. r> 6 chars + 20 >number drop >r drop line ! r> ( c-addr ) |
| 198 : |
|
|
dup c@ bl = if |
| 199 : |
|
|
char+ dup c@ [char] " <> abort" sync line syntax" |
| 200 : |
anton
|
1.24
|
char+ dup 100 [char] " scan drop swap 2dup - save-mem filename 2! |
| 201 : |
anton
|
1.17
|
char+ |
| 202 : |
|
|
endif |
| 203 : |
|
|
dup c@ nl-char <> abort" sync line syntax" |
| 204 : |
|
|
skipsynclines @ if |
| 205 : |
anton
|
1.18
|
dup char+ rawinput ! |
| 206 : |
|
|
rawinput @ c@ cookedinput @ c! |
| 207 : |
anton
|
1.17
|
endif |
| 208 : |
|
|
drop ; |
| 209 : |
|
|
|
| 210 : |
anton
|
1.1
|
: ?nextchar ( f -- ) |
| 211 : |
anton
|
1.17
|
?not? if |
| 212 : |
anton
|
1.18
|
filename 2@ type ." :" line @ 0 .r ." : syntax error, wrong char:" |
| 213 : |
anton
|
1.17
|
getinput . cr |
| 214 : |
anton
|
1.18
|
rawinput @ endrawinput @ over - 100 min type cr |
| 215 : |
anton
|
1.17
|
abort |
| 216 : |
|
|
endif |
| 217 : |
anton
|
1.18
|
rawinput @ endrawinput @ <> if |
| 218 : |
|
|
rawinput @ c@ |
| 219 : |
|
|
1 chars rawinput +! |
| 220 : |
|
|
1 chars cookedinput +! |
| 221 : |
anton
|
1.17
|
nl-char = if |
| 222 : |
|
|
checksyncline |
| 223 : |
|
|
endif |
| 224 : |
anton
|
1.18
|
rawinput @ c@ cookedinput @ c! |
| 225 : |
anton
|
1.17
|
endif ; |
| 226 : |
anton
|
1.1
|
|
| 227 : |
|
|
: charclass ( set "name" -- ) |
| 228 : |
|
|
['] ?nextchar terminal ; |
| 229 : |
|
|
|
| 230 : |
|
|
: .. ( c1 c2 -- set ) |
| 231 : |
|
|
( creates a set that includes the characters c, c1<=c<=c2 ) |
| 232 : |
|
|
empty copy-set |
| 233 : |
|
|
swap 1+ rot do |
| 234 : |
|
|
i over add-member |
| 235 : |
|
|
loop ; |
| 236 : |
|
|
|
| 237 : |
|
|
: ` ( -- terminal ) ( use: ` c ) |
| 238 : |
|
|
( creates anonymous terminal for the character c ) |
| 239 : |
anton
|
1.21
|
char singleton ['] ?nextchar make-terminal ; |
| 240 : |
anton
|
1.1
|
|
| 241 : |
|
|
char a char z .. char A char Z .. union char _ singleton union charclass letter |
| 242 : |
|
|
char 0 char 9 .. charclass digit |
| 243 : |
|
|
bl singleton charclass blank |
| 244 : |
|
|
tab-char singleton charclass tab |
| 245 : |
|
|
nl-char singleton eof-char over add-member complement charclass nonl |
| 246 : |
|
|
nl-char singleton eof-char over add-member char : over add-member complement charclass nocolonnl |
| 247 : |
|
|
bl 1+ maxchar .. charclass nowhite |
| 248 : |
|
|
char " singleton eof-char over add-member complement charclass noquote |
| 249 : |
|
|
nl-char singleton charclass nl |
| 250 : |
|
|
eof-char singleton charclass eof |
| 251 : |
|
|
|
| 252 : |
|
|
|
| 253 : |
|
|
(( letter (( letter || digit )) ** |
| 254 : |
|
|
)) <- c-name ( -- ) |
| 255 : |
|
|
|
| 256 : |
|
|
nowhite ++ |
| 257 : |
|
|
<- name ( -- ) |
| 258 : |
|
|
|
| 259 : |
pazsan
|
1.26
|
(( {{ ?flush-comment start }} ` \ nonl ** nl {{ end |
| 260 : |
pazsan
|
1.25
|
2dup 2 min s" \+" compare 0= IF f-comment 2! ELSE 2drop THEN }} |
| 261 : |
anton
|
1.1
|
)) <- comment ( -- ) |
| 262 : |
|
|
|
| 263 : |
|
|
(( {{ effect-in }} (( {{ start }} c-name {{ end 2 pick item-name 2! item-descr + }} blank ** )) ** {{ effect-in-end ! }} |
| 264 : |
|
|
` - ` - blank ** |
| 265 : |
|
|
{{ effect-out }} (( {{ start }} c-name {{ end 2 pick item-name 2! item-descr + }} blank ** )) ** {{ effect-out-end ! }} |
| 266 : |
|
|
)) <- stack-effect ( -- ) |
| 267 : |
|
|
|
| 268 : |
|
|
(( {{ s" " doc 2! s" " forth-code 2! }} |
| 269 : |
|
|
(( comment || nl )) ** |
| 270 : |
anton
|
1.17
|
(( {{ line @ name-line ! filename 2@ name-filename 2! }} |
| 271 : |
|
|
{{ start }} name {{ end 2dup forth-name 2! c-name 2! }} tab ++ |
| 272 : |
anton
|
1.1
|
{{ start }} stack-effect {{ end stack-string 2! }} tab ++ |
| 273 : |
|
|
{{ start }} name {{ end wordset 2! }} tab ** |
| 274 : |
|
|
(( {{ start }} c-name {{ end c-name 2! }} )) ?? nl |
| 275 : |
|
|
)) |
| 276 : |
|
|
(( ` " ` " {{ start }} (( noquote ++ ` " )) ++ {{ end 1- doc 2! }} ` " nl )) ?? |
| 277 : |
anton
|
1.18
|
{{ skipsynclines off line @ c-line ! filename 2@ c-filename 2! start }} (( nocolonnl nonl ** nl )) ** {{ end c-code 2! skipsynclines on }} |
| 278 : |
anton
|
1.1
|
(( ` : nl |
| 279 : |
|
|
{{ start }} (( nonl ++ nl )) ++ {{ end forth-code 2! }} |
| 280 : |
|
|
)) ?? |
| 281 : |
|
|
(( nl || eof )) |
| 282 : |
|
|
)) <- primitive ( -- ) |
| 283 : |
|
|
|
| 284 : |
pazsan
|
1.36
|
(( (( primitive {{ printprim }} )) ** eof )) |
| 285 : |
anton
|
1.1
|
parser primitives2something |
| 286 : |
pazsan
|
1.3
|
warnings @ [IF] |
| 287 : |
anton
|
1.1
|
.( parser generated ok ) cr |
| 288 : |
pazsan
|
1.3
|
[THEN] |
| 289 : |
anton
|
1.1
|
|
| 290 : |
|
|
: primfilter ( file-id xt -- ) |
| 291 : |
|
|
\ fileid is for the input file, xt ( -- ) is for the output word |
| 292 : |
|
|
output ! |
| 293 : |
anton
|
1.18
|
here dup rawinput ! cookedinput ! |
| 294 : |
anton
|
1.1
|
here swap read-whole-file |
| 295 : |
anton
|
1.18
|
dup endrawinput ! |
| 296 : |
anton
|
1.1
|
here - allot |
| 297 : |
pazsan
|
1.2
|
align |
| 298 : |
anton
|
1.17
|
checksyncline |
| 299 : |
anton
|
1.18
|
\ begin |
| 300 : |
|
|
\ getinput dup eof-char = ?EXIT emit true ?nextchar |
| 301 : |
|
|
\ again ; |
| 302 : |
anton
|
1.1
|
primitives2something ; |
| 303 : |
|
|
|
| 304 : |
|
|
\ types |
| 305 : |
|
|
|
| 306 : |
|
|
struct |
| 307 : |
|
|
2 cells field type-c-name |
| 308 : |
|
|
cell const-field type-d-size |
| 309 : |
|
|
cell const-field type-f-size |
| 310 : |
|
|
cell const-field type-fetch-handler |
| 311 : |
|
|
cell const-field type-store-handler |
| 312 : |
|
|
constant type-description |
| 313 : |
|
|
|
| 314 : |
|
|
: data-stack-access ( n1 n2 n3 -- ) |
| 315 : |
|
|
\ n1 is the offset of the accessed item, n2, n3 are effect-*-size |
| 316 : |
|
|
drop swap - 1- dup |
| 317 : |
|
|
if |
| 318 : |
pazsan
|
1.2
|
." sp[" 0 .r ." ]" |
| 319 : |
anton
|
1.1
|
else |
| 320 : |
|
|
drop ." TOS" |
| 321 : |
|
|
endif ; |
| 322 : |
|
|
|
| 323 : |
|
|
: fp-stack-access ( n1 n2 n3 -- ) |
| 324 : |
|
|
\ n1 is the offset of the accessed item, n2, n3 are effect-*-size |
| 325 : |
|
|
nip swap - 1- dup |
| 326 : |
|
|
if |
| 327 : |
pazsan
|
1.2
|
." fp[" 0 .r ." ]" |
| 328 : |
anton
|
1.1
|
else |
| 329 : |
|
|
drop ." FTOS" |
| 330 : |
|
|
endif ; |
| 331 : |
|
|
|
| 332 : |
|
|
: fetch-single ( item -- ) |
| 333 : |
|
|
>r |
| 334 : |
pazsan
|
1.8
|
r@ item-name 2@ type |
| 335 : |
|
|
." = (" |
| 336 : |
anton
|
1.1
|
r@ item-type @ type-c-name 2@ type ." ) " |
| 337 : |
|
|
r@ item-d-offset @ effect-in-size 2@ data-stack-access ." ;" cr |
| 338 : |
|
|
rdrop ; |
| 339 : |
|
|
|
| 340 : |
|
|
: fetch-double ( item -- ) |
| 341 : |
|
|
>r |
| 342 : |
anton
|
1.20
|
." FETCH_DCELL(" |
| 343 : |
|
|
r@ item-name 2@ type ." , " |
| 344 : |
anton
|
1.1
|
r@ item-d-offset @ dup effect-in-size 2@ data-stack-access |
| 345 : |
anton
|
1.20
|
." , " 1+ effect-in-size 2@ data-stack-access |
| 346 : |
|
|
." );" cr |
| 347 : |
anton
|
1.1
|
rdrop ; |
| 348 : |
|
|
|
| 349 : |
|
|
: fetch-float ( item -- ) |
| 350 : |
|
|
>r |
| 351 : |
pazsan
|
1.8
|
r@ item-name 2@ type |
| 352 : |
|
|
." = " |
| 353 : |
anton
|
1.1
|
\ ." (" r@ item-type @ type-c-name 2@ type ." ) " |
| 354 : |
|
|
r@ item-f-offset @ effect-in-size 2@ fp-stack-access ." ;" cr |
| 355 : |
|
|
rdrop ; |
| 356 : |
|
|
|
| 357 : |
|
|
: d-same-as-in? ( item -- f ) |
| 358 : |
|
|
\ f is true iff the offset of item is the same as on input |
| 359 : |
|
|
>r |
| 360 : |
|
|
r@ item-name 2@ items @ search-wordlist 0= |
| 361 : |
pazsan
|
1.8
|
abort" bug" |
| 362 : |
anton
|
1.1
|
execute @ |
| 363 : |
|
|
dup r@ = |
| 364 : |
|
|
if \ item first appeared in output |
| 365 : |
|
|
drop false |
| 366 : |
|
|
else |
| 367 : |
|
|
item-d-offset @ r@ item-d-offset @ = |
| 368 : |
|
|
endif |
| 369 : |
|
|
rdrop ; |
| 370 : |
|
|
|
| 371 : |
|
|
: is-in-tos? ( item -- f ) |
| 372 : |
|
|
\ true if item has the same offset as the input TOS |
| 373 : |
|
|
item-d-offset @ 1+ effect-in-size 2@ drop = ; |
| 374 : |
|
|
|
| 375 : |
pazsan
|
1.31
|
: is-out-tos? ( item -- f ) |
| 376 : |
|
|
\ true if item has the same offset as the input TOS |
| 377 : |
|
|
item-d-offset @ 1+ effect-out-size 2@ drop = ; |
| 378 : |
|
|
|
| 379 : |
anton
|
1.1
|
: really-store-single ( item -- ) |
| 380 : |
|
|
>r |
| 381 : |
|
|
r@ item-d-offset @ effect-out-size 2@ data-stack-access ." = (Cell)" |
| 382 : |
|
|
r@ item-name 2@ type ." ;" |
| 383 : |
|
|
rdrop ; |
| 384 : |
|
|
|
| 385 : |
|
|
: store-single ( item -- ) |
| 386 : |
|
|
>r |
| 387 : |
|
|
r@ d-same-as-in? |
| 388 : |
|
|
if |
| 389 : |
pazsan
|
1.31
|
r@ is-in-tos? r@ is-out-tos? xor |
| 390 : |
anton
|
1.1
|
if |
| 391 : |
|
|
." IF_TOS(" r@ really-store-single ." );" cr |
| 392 : |
|
|
endif |
| 393 : |
|
|
else |
| 394 : |
|
|
r@ really-store-single cr |
| 395 : |
|
|
endif |
| 396 : |
|
|
rdrop ; |
| 397 : |
|
|
|
| 398 : |
|
|
: store-double ( item -- ) |
| 399 : |
|
|
\ !! store optimization is not performed, because it is not yet needed |
| 400 : |
|
|
>r |
| 401 : |
anton
|
1.20
|
." STORE_DCELL(" r@ item-name 2@ type ." , " |
| 402 : |
|
|
r@ item-d-offset @ dup effect-out-size 2@ data-stack-access |
| 403 : |
|
|
." , " 1+ effect-out-size 2@ data-stack-access |
| 404 : |
|
|
." );" cr |
| 405 : |
anton
|
1.1
|
rdrop ; |
| 406 : |
|
|
|
| 407 : |
|
|
: f-same-as-in? ( item -- f ) |
| 408 : |
|
|
\ f is true iff the offset of item is the same as on input |
| 409 : |
|
|
>r |
| 410 : |
|
|
r@ item-name 2@ items @ search-wordlist 0= |
| 411 : |
pazsan
|
1.8
|
abort" bug" |
| 412 : |
anton
|
1.1
|
execute @ |
| 413 : |
|
|
dup r@ = |
| 414 : |
|
|
if \ item first appeared in output |
| 415 : |
|
|
drop false |
| 416 : |
|
|
else |
| 417 : |
|
|
item-f-offset @ r@ item-f-offset @ = |
| 418 : |
|
|
endif |
| 419 : |
|
|
rdrop ; |
| 420 : |
|
|
|
| 421 : |
|
|
: is-in-ftos? ( item -- f ) |
| 422 : |
|
|
\ true if item has the same offset as the input TOS |
| 423 : |
|
|
item-f-offset @ 1+ effect-in-size 2@ nip = ; |
| 424 : |
|
|
|
| 425 : |
|
|
: really-store-float ( item -- ) |
| 426 : |
|
|
>r |
| 427 : |
|
|
r@ item-f-offset @ effect-out-size 2@ fp-stack-access ." = " |
| 428 : |
|
|
r@ item-name 2@ type ." ;" |
| 429 : |
|
|
rdrop ; |
| 430 : |
|
|
|
| 431 : |
|
|
: store-float ( item -- ) |
| 432 : |
|
|
>r |
| 433 : |
|
|
r@ f-same-as-in? |
| 434 : |
|
|
if |
| 435 : |
|
|
r@ is-in-ftos? |
| 436 : |
|
|
if |
| 437 : |
|
|
." IF_FTOS(" r@ really-store-float ." );" cr |
| 438 : |
|
|
endif |
| 439 : |
|
|
else |
| 440 : |
|
|
r@ really-store-float cr |
| 441 : |
|
|
endif |
| 442 : |
|
|
rdrop ; |
| 443 : |
|
|
|
| 444 : |
anton
|
1.10
|
: single-type ( -- xt1 xt2 n1 n2 ) |
| 445 : |
anton
|
1.1
|
['] fetch-single ['] store-single 1 0 ; |
| 446 : |
|
|
|
| 447 : |
anton
|
1.10
|
: double-type ( -- xt1 xt2 n1 n2 ) |
| 448 : |
anton
|
1.1
|
['] fetch-double ['] store-double 2 0 ; |
| 449 : |
|
|
|
| 450 : |
anton
|
1.10
|
: float-type ( -- xt1 xt2 n1 n2 ) |
| 451 : |
anton
|
1.1
|
['] fetch-float ['] store-float 0 1 ; |
| 452 : |
|
|
|
| 453 : |
|
|
: s, ( addr u -- ) |
| 454 : |
|
|
\ allocate a string |
| 455 : |
|
|
here swap dup allot move ; |
| 456 : |
|
|
|
| 457 : |
|
|
: starts-with ( addr u xt1 xt2 n1 n2 "prefix" -- ) |
| 458 : |
|
|
\ describes a type |
| 459 : |
|
|
\ addr u specifies the C type name |
| 460 : |
|
|
\ n1 is the size of the type on the data stack |
| 461 : |
|
|
\ n2 is the size of the type on the FP stack |
| 462 : |
|
|
\ stack effect entries of the type start with prefix |
| 463 : |
|
|
>r >r >r >r |
| 464 : |
|
|
dup >r here >r s, |
| 465 : |
|
|
create |
| 466 : |
|
|
r> r> 2, |
| 467 : |
|
|
r> r> r> , r> , swap , , ; |
| 468 : |
|
|
|
| 469 : |
|
|
wordlist constant types |
| 470 : |
|
|
get-current |
| 471 : |
|
|
types set-current |
| 472 : |
|
|
|
| 473 : |
|
|
s" Bool" single-type starts-with f |
| 474 : |
|
|
s" Char" single-type starts-with c |
| 475 : |
|
|
s" Cell" single-type starts-with n |
| 476 : |
|
|
s" Cell" single-type starts-with w |
| 477 : |
|
|
s" UCell" single-type starts-with u |
| 478 : |
|
|
s" DCell" double-type starts-with d |
| 479 : |
|
|
s" UDCell" double-type starts-with ud |
| 480 : |
|
|
s" Float" float-type starts-with r |
| 481 : |
|
|
s" Cell *" single-type starts-with a_ |
| 482 : |
|
|
s" Char *" single-type starts-with c_ |
| 483 : |
|
|
s" Float *" single-type starts-with f_ |
| 484 : |
|
|
s" DFloat *" single-type starts-with df_ |
| 485 : |
|
|
s" SFloat *" single-type starts-with sf_ |
| 486 : |
|
|
s" Xt" single-type starts-with xt |
| 487 : |
|
|
s" WID" single-type starts-with wid |
| 488 : |
pazsan
|
1.33
|
s" struct F83Name *" single-type starts-with f83name |
| 489 : |
anton
|
1.1
|
|
| 490 : |
|
|
set-current |
| 491 : |
|
|
|
| 492 : |
|
|
: get-type ( addr1 u1 -- type-descr ) |
| 493 : |
|
|
\ get the type of the name in addr1 u1 |
| 494 : |
|
|
\ type-descr is a pointer to a type-descriptor |
| 495 : |
|
|
0 swap ?do |
| 496 : |
|
|
dup i types search-wordlist |
| 497 : |
|
|
if \ ok, we have the type ( addr1 xt ) |
| 498 : |
|
|
execute nip |
| 499 : |
|
|
UNLOOP EXIT |
| 500 : |
|
|
endif |
| 501 : |
anton
|
1.9
|
-1 s+loop |
| 502 : |
anton
|
1.1
|
\ we did not find a type, abort |
| 503 : |
pazsan
|
1.8
|
true abort" unknown type prefix" ; |
| 504 : |
anton
|
1.1
|
|
| 505 : |
|
|
: declare ( addr "name" -- ) |
| 506 : |
|
|
\ remember that there is a stack item at addr called name |
| 507 : |
|
|
create , ; |
| 508 : |
|
|
|
| 509 : |
|
|
: declaration ( item -- ) |
| 510 : |
|
|
dup item-name 2@ items @ search-wordlist |
| 511 : |
|
|
if \ already declared ( item xt ) |
| 512 : |
|
|
execute @ item-type @ swap item-type ! |
| 513 : |
|
|
else ( addr ) |
| 514 : |
|
|
dup item-name 2@ nextname dup declare ( addr ) |
| 515 : |
|
|
dup >r item-name 2@ 2dup get-type ( addr1 u type-descr ) |
| 516 : |
|
|
dup r> item-type ! ( addr1 u type-descr ) |
| 517 : |
|
|
type-c-name 2@ type space type ." ;" cr |
| 518 : |
|
|
endif ; |
| 519 : |
|
|
|
| 520 : |
|
|
: declaration-list ( addr1 addr2 -- ) |
| 521 : |
|
|
swap ?do |
| 522 : |
|
|
i declaration |
| 523 : |
|
|
item-descr +loop ; |
| 524 : |
|
|
|
| 525 : |
pazsan
|
1.8
|
: fetch ( addr -- ) |
| 526 : |
|
|
dup item-type @ type-fetch-handler execute ; |
| 527 : |
|
|
|
| 528 : |
anton
|
1.1
|
: declarations ( -- ) |
| 529 : |
|
|
wordlist dup items ! set-current |
| 530 : |
|
|
effect-in effect-in-end @ declaration-list |
| 531 : |
|
|
effect-out effect-out-end @ declaration-list ; |
| 532 : |
|
|
|
| 533 : |
|
|
\ offset computation |
| 534 : |
|
|
\ the leftmost (i.e. deepest) item has offset 0 |
| 535 : |
|
|
\ the rightmost item has the highest offset |
| 536 : |
|
|
|
| 537 : |
|
|
: compute-offset ( n1 n2 item -- n3 n4 ) |
| 538 : |
|
|
\ n1, n3 are data-stack-offsets |
| 539 : |
|
|
\ n2, n4 are the fp-stack-offsets |
| 540 : |
|
|
>r |
| 541 : |
|
|
swap dup r@ item-d-offset ! |
| 542 : |
|
|
r@ item-type @ type-d-size + |
| 543 : |
|
|
swap dup r@ item-f-offset ! |
| 544 : |
|
|
r@ item-type @ type-f-size + |
| 545 : |
|
|
rdrop ; |
| 546 : |
|
|
|
| 547 : |
|
|
: compute-list ( addr1 addr2 -- n1 n2 ) |
| 548 : |
|
|
\ n1, n2 are the final offsets |
| 549 : |
|
|
0 0 2swap swap ?do |
| 550 : |
|
|
i compute-offset |
| 551 : |
|
|
item-descr +loop ; |
| 552 : |
|
|
|
| 553 : |
|
|
: compute-offsets ( -- ) |
| 554 : |
|
|
effect-in effect-in-end @ compute-list effect-in-size 2! |
| 555 : |
|
|
effect-out effect-out-end @ compute-list effect-out-size 2! ; |
| 556 : |
|
|
|
| 557 : |
|
|
: flush-tos ( -- ) |
| 558 : |
|
|
effect-in-size 2@ effect-out-size 2@ |
| 559 : |
|
|
0<> rot 0= and |
| 560 : |
|
|
if |
| 561 : |
anton
|
1.13
|
." IF_FTOS(fp[0] = FTOS);" cr |
| 562 : |
|
|
endif |
| 563 : |
anton
|
1.1
|
0<> swap 0= and |
| 564 : |
|
|
if |
| 565 : |
anton
|
1.13
|
." IF_TOS(sp[0] = TOS);" cr |
| 566 : |
|
|
endif ; |
| 567 : |
anton
|
1.1
|
|
| 568 : |
|
|
: fill-tos ( -- ) |
| 569 : |
|
|
effect-in-size 2@ effect-out-size 2@ |
| 570 : |
|
|
0= rot 0<> and |
| 571 : |
|
|
if |
| 572 : |
|
|
." IF_FTOS(FTOS = fp[0]);" cr |
| 573 : |
|
|
endif |
| 574 : |
|
|
0= swap 0<> and |
| 575 : |
|
|
if |
| 576 : |
|
|
." IF_TOS(TOS = sp[0]);" cr |
| 577 : |
|
|
endif ; |
| 578 : |
|
|
|
| 579 : |
|
|
: fetches ( -- ) |
| 580 : |
|
|
effect-in-end @ effect-in ?do |
| 581 : |
|
|
i fetch |
| 582 : |
|
|
item-descr +loop ; |
| 583 : |
|
|
|
| 584 : |
|
|
: stack-pointer-updates ( -- ) |
| 585 : |
pazsan
|
1.8
|
\ we need not check if an update is a noop; gcc does this for us |
| 586 : |
anton
|
1.1
|
effect-in-size 2@ |
| 587 : |
|
|
effect-out-size 2@ |
| 588 : |
|
|
rot swap - ( d-in d-out f-diff ) |
| 589 : |
|
|
rot rot - ( f-diff d-diff ) |
| 590 : |
pazsan
|
1.2
|
?dup IF ." sp += " 0 .r ." ;" cr THEN |
| 591 : |
|
|
?dup IF ." fp += " 0 .r ." ;" cr THEN ; |
| 592 : |
anton
|
1.1
|
|
| 593 : |
|
|
: store ( item -- ) |
| 594 : |
|
|
\ f is true if the item should be stored |
| 595 : |
|
|
\ f is false if the store is probably not necessary |
| 596 : |
|
|
dup item-type @ type-store-handler execute ; |
| 597 : |
|
|
|
| 598 : |
|
|
: stores ( -- ) |
| 599 : |
|
|
effect-out-end @ effect-out ?do |
| 600 : |
|
|
i store |
| 601 : |
|
|
item-descr +loop ; |
| 602 : |
|
|
|
| 603 : |
pazsan
|
1.8
|
: .stack-list ( start end -- ) |
| 604 : |
|
|
swap ?do |
| 605 : |
|
|
i item-name 2@ type space |
| 606 : |
|
|
item-descr +loop ; |
| 607 : |
|
|
|
| 608 : |
pazsan
|
1.34
|
: output-c ( -- ) 1 flush-comment ! |
| 609 : |
|
|
?flush-comment |
| 610 : |
pazsan
|
1.2
|
." I_" c-name 2@ type ." : /* " forth-name 2@ type ." ( " stack-string 2@ type ." ) */" cr |
| 611 : |
anton
|
1.1
|
." /* " doc 2@ type ." */" cr |
| 612 : |
anton
|
1.13
|
." NAME(" [char] " emit forth-name 2@ type [char] " emit ." )" cr \ debugging |
| 613 : |
anton
|
1.1
|
." {" cr |
| 614 : |
|
|
." DEF_CA" cr |
| 615 : |
|
|
declarations |
| 616 : |
|
|
compute-offsets \ for everything else |
| 617 : |
anton
|
1.13
|
." NEXT_P0;" cr |
| 618 : |
|
|
flush-tos |
| 619 : |
anton
|
1.1
|
fetches |
| 620 : |
anton
|
1.13
|
stack-pointer-updates |
| 621 : |
anton
|
1.1
|
." {" cr |
| 622 : |
anton
|
1.17
|
." #line " c-line @ . [char] " emit c-filename 2@ type [char] " emit cr |
| 623 : |
anton
|
1.1
|
c-code 2@ type |
| 624 : |
|
|
." }" cr |
| 625 : |
|
|
." NEXT_P1;" cr |
| 626 : |
|
|
stores |
| 627 : |
|
|
fill-tos |
| 628 : |
anton
|
1.9
|
." NEXT_P2;" cr |
| 629 : |
anton
|
1.1
|
." }" cr |
| 630 : |
|
|
cr |
| 631 : |
|
|
; |
| 632 : |
|
|
|
| 633 : |
pazsan
|
1.30
|
: output-funclabel ( -- ) |
| 634 : |
|
|
1 function-number +! |
| 635 : |
|
|
." &I_" c-name 2@ type ." ," cr ; |
| 636 : |
|
|
|
| 637 : |
|
|
: output-forthname ( -- ) |
| 638 : |
|
|
1 function-number +! |
| 639 : |
|
|
'" emit forth-name 2@ type '" emit ." ," cr ; |
| 640 : |
|
|
|
| 641 : |
|
|
: output-c-func ( -- ) |
| 642 : |
|
|
1 function-number +! |
| 643 : |
|
|
." void I_" c-name 2@ type ." () /* " forth-name 2@ type |
| 644 : |
|
|
." ( " stack-string 2@ type ." ) */" cr |
| 645 : |
|
|
." /* " doc 2@ type ." */" cr |
| 646 : |
|
|
." NAME(" [char] " emit forth-name 2@ type [char] " emit ." )" cr |
| 647 : |
|
|
\ debugging |
| 648 : |
|
|
." {" cr |
| 649 : |
|
|
." DEF_CA" cr |
| 650 : |
|
|
declarations |
| 651 : |
|
|
compute-offsets \ for everything else |
| 652 : |
|
|
." NEXT_P0;" cr |
| 653 : |
|
|
flush-tos |
| 654 : |
|
|
fetches |
| 655 : |
|
|
stack-pointer-updates |
| 656 : |
|
|
." {" cr |
| 657 : |
|
|
." #line " c-line @ . [char] " emit c-filename 2@ type [char] " emit cr |
| 658 : |
|
|
c-code 2@ type |
| 659 : |
|
|
." }" cr |
| 660 : |
|
|
." NEXT_P1;" cr |
| 661 : |
|
|
stores |
| 662 : |
|
|
fill-tos |
| 663 : |
|
|
." NEXT_P2;" cr |
| 664 : |
|
|
." }" cr |
| 665 : |
|
|
cr ; |
| 666 : |
|
|
|
| 667 : |
pazsan
|
1.34
|
: output-label ( -- ) 1 flush-comment ! |
| 668 : |
|
|
?flush-comment |
| 669 : |
|
|
." (Label)&&I_" c-name 2@ type ." ," cr |
| 670 : |
|
|
-1 primitive-number +! ; |
| 671 : |
anton
|
1.1
|
|
| 672 : |
pazsan
|
1.26
|
: output-alias ( -- ) flush-comment on |
| 673 : |
pazsan
|
1.34
|
?flush-comment |
| 674 : |
pazsan
|
1.38
|
( primitive-number @ . ." alias " ) ." Primitive " forth-name 2@ type cr |
| 675 : |
pazsan
|
1.34
|
-1 primitive-number +! ; |
| 676 : |
anton
|
1.1
|
|
| 677 : |
pazsan
|
1.26
|
: output-forth ( -- ) flush-comment on |
| 678 : |
pazsan
|
1.30
|
?flush-comment |
| 679 : |
|
|
forth-code @ 0= |
| 680 : |
|
|
IF \ output-alias |
| 681 : |
jwilke
|
1.28
|
\ this is bad for ec: an alias is compiled if tho word does not exist! |
| 682 : |
|
|
\ JAW |
| 683 : |
pazsan
|
1.30
|
ELSE ." : " forth-name 2@ type ." ( " |
| 684 : |
|
|
effect-in effect-in-end @ .stack-list ." -- " |
| 685 : |
|
|
effect-out effect-out-end @ .stack-list ." )" cr |
| 686 : |
|
|
forth-code 2@ type cr |
| 687 : |
|
|
-1 primitive-number +! |
| 688 : |
|
|
THEN ; |
| 689 : |
anton
|
1.10
|
|
| 690 : |
anton
|
1.17
|
: output-tag-file ( -- ) |
| 691 : |
|
|
name-filename 2@ last-name-filename 2@ compare if |
| 692 : |
|
|
name-filename 2@ last-name-filename 2! |
| 693 : |
|
|
#ff emit cr |
| 694 : |
|
|
name-filename 2@ type |
| 695 : |
|
|
." ,0" cr |
| 696 : |
|
|
endif ; |
| 697 : |
|
|
|
| 698 : |
|
|
: output-tag ( -- ) |
| 699 : |
|
|
output-tag-file |
| 700 : |
|
|
forth-name 2@ 1+ type |
| 701 : |
|
|
127 emit |
| 702 : |
|
|
space forth-name 2@ type space |
| 703 : |
|
|
1 emit |
| 704 : |
|
|
name-line @ 0 .r |
| 705 : |
|
|
." ,0" cr ; |
| 706 : |
|
|
|
| 707 : |
anton
|
1.10
|
[IFDEF] documentation |
| 708 : |
|
|
: register-doc ( -- ) |
| 709 : |
|
|
get-current documentation set-current |
| 710 : |
|
|
forth-name 2@ nextname create |
| 711 : |
|
|
forth-name 2@ 2, |
| 712 : |
anton
|
1.15
|
stack-string 2@ condition-stack-effect 2, |
| 713 : |
anton
|
1.10
|
wordset 2@ 2, |
| 714 : |
anton
|
1.15
|
c-name 2@ condition-pronounciation 2, |
| 715 : |
anton
|
1.10
|
doc 2@ 2, |
| 716 : |
|
|
set-current ; |
| 717 : |
|
|
[THEN] |
| 718 : |
pazsan
|
1.8
|
|
| 719 : |
anton
|
1.1
|
: process-file ( addr u xt -- ) |
| 720 : |
anton
|
1.17
|
>r |
| 721 : |
|
|
2dup filename 2! |
| 722 : |
pazsan
|
1.30
|
0 function-number ! |
| 723 : |
anton
|
1.17
|
r/o open-file abort" cannot open file" |
| 724 : |
|
|
warnings @ if |
| 725 : |
|
|
." ------------ CUT HERE -------------" cr endif |
| 726 : |
|
|
r> primfilter ; |
| 727 : |
pazsan
|
1.30
|
|
| 728 : |
|
|
: process ( xt -- ) |
| 729 : |
|
|
bl word count rot |
| 730 : |
|
|
process-file ; |