[gforth] / gforth / prims2x.fs  

gforth: gforth/prims2x.fs


1 : anton 1.16 \ converts primitives to, e.g., C code
2 :    
3 : anton 1.47 \ Copyright (C) 1995,1996,1997,1998,2000 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 : anton 1.48 \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
20 : anton 1.16
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 : anton 1.49 : struct% struct ; \ struct is redefined in gray
58 :    
59 : jwilke 1.39 include ./gray.fs
60 : anton 1.1
61 :     100 constant max-effect \ number of things on one side of a stack effect
62 :     255 constant maxchar
63 :     maxchar 1+ constant eof-char
64 : anton 1.17 #tab constant tab-char
65 :     #lf constant nl-char
66 : anton 1.1
67 : anton 1.18 variable rawinput \ pointer to next character to be scanned
68 :     variable endrawinput \ pointer to the end of the input (the char after the last)
69 :     variable cookedinput \ pointer to the next char to be parsed
70 : anton 1.17 variable line \ line number of char pointed to by input
71 :     1 line !
72 :     2variable filename \ filename of original input file
73 :     0 0 filename 2!
74 : pazsan 1.25 2variable f-comment
75 :     0 0 f-comment 2!
76 : anton 1.17 variable skipsynclines \ are sync lines ("#line ...") invisible to the parser?
77 :     skipsynclines on
78 : anton 1.1
79 :     : start ( -- addr )
80 : anton 1.18 cookedinput @ ;
81 : anton 1.1
82 :     : end ( addr -- addr u )
83 : anton 1.18 cookedinput @ over - ;
84 : anton 1.1
85 :     variable output \ xt ( -- ) of output word
86 :    
87 :     : printprim ( -- )
88 :     output @ execute ;
89 :    
90 : anton 1.49 struct%
91 :     cell% 2* field stack-pointer \ stackpointer name
92 :     cell% 2* field stack-cast \ cast string for assignments to stack elements
93 : anton 1.53 cell% field stack-in-index-xt \ ( in-size item -- in-index )
94 : anton 1.49 cell% field stack-in \ number of stack items in effect in
95 :     cell% field stack-out \ number of stack items in effect out
96 :     end-struct stack%
97 :    
98 : anton 1.53 struct%
99 :     cell% 2* field item-name \ name, excluding stack prefixes
100 :     cell% field item-stack \ descriptor for the stack used, 0 is default
101 :     cell% field item-type \ descriptor for the item type
102 :     cell% field item-offset \ offset in stack items, 0 for the deepest element
103 :     end-struct item%
104 :    
105 :     struct%
106 :     cell% 2* field type-c-name
107 :     cell% field type-stack \ default stack
108 :     cell% field type-size \ size of type in stack items
109 :     cell% field type-fetch \ xt of fetch code generator ( item -- )
110 :     cell% field type-store \ xt of store code generator ( item -- )
111 :     end-struct type%
112 :    
113 :     : stack-in-index ( in-size item -- in-index )
114 :     item-offset @ - 1- ;
115 :    
116 :     : inst-in-index ( in-size item -- in-index )
117 :     nip dup item-offset @ swap item-type @ type-size @ + 1- ;
118 :    
119 : anton 1.49 : make-stack ( addr-ptr u1 addr-cast u2 "stack-name" -- )
120 :     create stack% %allot >r
121 :     save-mem r@ stack-cast 2!
122 : anton 1.53 save-mem r@ stack-pointer 2!
123 :     ['] stack-in-index r> stack-in-index-xt ! ;
124 : anton 1.49
125 :     s" sp" save-mem s" (Cell)" make-stack data-stack
126 :     s" fp" save-mem s" " make-stack fp-stack
127 : anton 1.51 s" rp" save-mem s" (Cell)" make-stack return-stack
128 : anton 1.53 s" ip" save-mem s" error don't use # on results" make-stack inst-stream
129 :     ' inst-in-index inst-stream stack-in-index-xt !
130 : anton 1.49 \ !! initialize stack-in and stack-out
131 :    
132 :     \ stack items
133 :    
134 :     : init-item ( addr u addr1 -- )
135 :     \ initialize item at addr1 with name addr u
136 :     \ !! remove stack prefix
137 :     dup item% %size erase
138 :     item-name 2! ;
139 :    
140 :     \ various variables for storing stuff of one primitive
141 : anton 1.1
142 :     2variable forth-name
143 :     2variable wordset
144 :     2variable c-name
145 :     2variable doc
146 :     2variable c-code
147 :     2variable forth-code
148 :     2variable stack-string
149 : anton 1.49 create effect-in max-effect item% %size * allot
150 :     create effect-out max-effect item% %size * allot
151 : anton 1.1 variable effect-in-end ( pointer )
152 :     variable effect-out-end ( pointer )
153 : anton 1.17 variable c-line
154 :     2variable c-filename
155 :     variable name-line
156 :     2variable name-filename
157 :     2variable last-name-filename
158 : anton 1.1
159 : pazsan 1.14 variable primitive-number -10 primitive-number !
160 : pazsan 1.30 Variable function-number 0 function-number !
161 : anton 1.1
162 :     \ for several reasons stack items of a word are stored in a wordlist
163 :     \ since neither forget nor marker are implemented yet, we make a new
164 :     \ wordlist for every word and store it in the variable items
165 :     variable items
166 :    
167 :     \ a few more set ops
168 :    
169 :     : bit-equivalent ( w1 w2 -- w3 )
170 :     xor invert ;
171 :    
172 :     : complement ( set1 -- set2 )
173 :     empty ['] bit-equivalent binary-set-operation ;
174 :    
175 :     \ the parser
176 :    
177 :     eof-char max-member \ the whole character set + EOF
178 :    
179 :     : getinput ( -- n )
180 : anton 1.18 rawinput @ endrawinput @ =
181 : anton 1.1 if
182 : anton 1.18 eof-char
183 : anton 1.1 else
184 : anton 1.18 cookedinput @ c@
185 : anton 1.1 endif ;
186 :    
187 :     :noname ( n -- )
188 :     dup bl > if
189 :     emit space
190 :     else
191 :     .
192 :     endif ;
193 :     print-token !
194 :    
195 :     : testchar? ( set -- f )
196 :     getinput member? ;
197 :     ' testchar? test-vector !
198 :    
199 : anton 1.17 : checksyncline ( -- )
200 :     \ when input points to a newline, check if the next line is a
201 :     \ sync line. If it is, perform the appropriate actions.
202 : anton 1.18 rawinput @ >r
203 : anton 1.17 s" #line " r@ over compare 0<> if
204 :     rdrop 1 line +! EXIT
205 :     endif
206 :     0. r> 6 chars + 20 >number drop >r drop line ! r> ( c-addr )
207 :     dup c@ bl = if
208 :     char+ dup c@ [char] " <> abort" sync line syntax"
209 : anton 1.24 char+ dup 100 [char] " scan drop swap 2dup - save-mem filename 2!
210 : anton 1.17 char+
211 :     endif
212 :     dup c@ nl-char <> abort" sync line syntax"
213 :     skipsynclines @ if
214 : anton 1.18 dup char+ rawinput !
215 :     rawinput @ c@ cookedinput @ c!
216 : anton 1.17 endif
217 :     drop ;
218 :    
219 : anton 1.1 : ?nextchar ( f -- )
220 : anton 1.17 ?not? if
221 : anton 1.18 filename 2@ type ." :" line @ 0 .r ." : syntax error, wrong char:"
222 : anton 1.17 getinput . cr
223 : anton 1.18 rawinput @ endrawinput @ over - 100 min type cr
224 : anton 1.17 abort
225 :     endif
226 : anton 1.18 rawinput @ endrawinput @ <> if
227 :     rawinput @ c@
228 :     1 chars rawinput +!
229 :     1 chars cookedinput +!
230 : anton 1.17 nl-char = if
231 :     checksyncline
232 :     endif
233 : anton 1.18 rawinput @ c@ cookedinput @ c!
234 : anton 1.17 endif ;
235 : anton 1.1
236 :     : charclass ( set "name" -- )
237 :     ['] ?nextchar terminal ;
238 :    
239 :     : .. ( c1 c2 -- set )
240 :     ( creates a set that includes the characters c, c1<=c<=c2 )
241 :     empty copy-set
242 :     swap 1+ rot do
243 :     i over add-member
244 :     loop ;
245 :    
246 :     : ` ( -- terminal ) ( use: ` c )
247 :     ( creates anonymous terminal for the character c )
248 : anton 1.21 char singleton ['] ?nextchar make-terminal ;
249 : anton 1.1
250 :     char a char z .. char A char Z .. union char _ singleton union charclass letter
251 :     char 0 char 9 .. charclass digit
252 : anton 1.45 bl singleton tab-char over add-member charclass white
253 : anton 1.1 nl-char singleton eof-char over add-member complement charclass nonl
254 : pazsan 1.46 nl-char singleton eof-char over add-member
255 :     char : over add-member complement charclass nocolonnl
256 :     bl 1+ maxchar .. char \ singleton complement intersection
257 :     charclass nowhitebq
258 :     bl 1+ maxchar .. charclass nowhite
259 : anton 1.1 char " singleton eof-char over add-member complement charclass noquote
260 :     nl-char singleton charclass nl
261 :     eof-char singleton charclass eof
262 :    
263 :    
264 :     (( letter (( letter || digit )) **
265 : anton 1.51 )) <- c-ident ( -- )
266 :    
267 :     (( ` # ?? (( letter || digit || ` : )) **
268 :     )) <- stack-ident ( -- )
269 : anton 1.1
270 : pazsan 1.46 (( nowhitebq nowhite ** ))
271 : anton 1.51 <- forth-ident ( -- )
272 : anton 1.1
273 : jwilke 1.42 Variable forth-flag
274 :     Variable c-flag
275 :    
276 : anton 1.54 (( (( ` e || ` E )) {{ start }} nonl **
277 :     {{ end evaluate }}
278 :     )) <- eval-comment ( ... -- ... )
279 :    
280 : jwilke 1.42 (( (( ` f || ` F )) {{ start }} nonl **
281 :     {{ end forth-flag @ IF type cr ELSE 2drop THEN }}
282 :     )) <- forth-comment ( -- )
283 :    
284 :     (( (( ` c || ` C )) {{ start }} nonl **
285 :     {{ end c-flag @ IF type cr ELSE 2drop THEN }}
286 :     )) <- c-comment ( -- )
287 :    
288 : jwilke 1.43 (( ` - nonl ** {{
289 :     forth-flag @ IF ." [ELSE]" cr THEN
290 :     c-flag @ IF ." #else" cr THEN }}
291 :     )) <- else-comment
292 :    
293 :     (( ` + {{ start }} nonl ** {{ end
294 :     dup
295 :     IF c-flag @
296 :     IF ." #ifdef HAS_" bounds ?DO I c@ toupper emit LOOP cr
297 :     THEN
298 :     forth-flag @
299 :     IF ." has? " type ." [IF]" cr THEN
300 :     ELSE 2drop
301 : pazsan 1.46 c-flag @ IF ." #endif" cr THEN
302 :     forth-flag @ IF ." [THEN]" cr THEN
303 : jwilke 1.43 THEN }}
304 :     )) <- if-comment
305 :    
306 : anton 1.54 (( (( eval-comment || forth-comment || c-comment || else-comment || if-comment )) ?? nonl ** )) <- comment-body
307 : jwilke 1.42
308 : jwilke 1.43 (( ` \ comment-body nl )) <- comment ( -- )
309 : anton 1.1
310 : anton 1.51 (( {{ start }} stack-ident {{ end 2 pick init-item item% %size + }} white ** )) **
311 :     <- stack-items
312 :    
313 :     (( {{ effect-in }} stack-items {{ effect-in-end ! }}
314 : anton 1.45 ` - ` - white **
315 : anton 1.51 {{ effect-out }} stack-items {{ effect-out-end ! }}
316 : anton 1.1 )) <- stack-effect ( -- )
317 :    
318 : anton 1.57 (( {{ s" " doc 2! s" " forth-code 2! s" " wordset 2! }}
319 : anton 1.17 (( {{ line @ name-line ! filename 2@ name-filename 2! }}
320 : anton 1.51 {{ start }} forth-ident {{ end 2dup forth-name 2! c-name 2! }} white ++
321 : anton 1.45 ` ( white ** {{ start }} stack-effect {{ end stack-string 2! }} ` ) white **
322 : anton 1.57 (( {{ start }} forth-ident {{ end wordset 2! }} white **
323 :     (( {{ start }} c-ident {{ end c-name 2! }} )) ??
324 :     )) ?? nl
325 : anton 1.1 ))
326 : anton 1.58 (( ` " ` " {{ start }} (( noquote ++ ` " )) ++ {{ end 1- doc 2! }} ` " white ** nl )) ??
327 : anton 1.18 {{ skipsynclines off line @ c-line ! filename 2@ c-filename 2! start }} (( nocolonnl nonl ** nl )) ** {{ end c-code 2! skipsynclines on }}
328 : anton 1.58 (( ` : white ** nl
329 : anton 1.1 {{ start }} (( nonl ++ nl )) ++ {{ end forth-code 2! }}
330 : pazsan 1.46 )) ?? {{ printprim }}
331 : anton 1.1 (( nl || eof ))
332 :     )) <- primitive ( -- )
333 :    
334 : pazsan 1.46 (( (( comment || primitive || nl )) ** eof ))
335 : anton 1.1 parser primitives2something
336 : pazsan 1.3 warnings @ [IF]
337 : anton 1.1 .( parser generated ok ) cr
338 : pazsan 1.3 [THEN]
339 : anton 1.1
340 :     : primfilter ( file-id xt -- )
341 :     \ fileid is for the input file, xt ( -- ) is for the output word
342 :     output !
343 : anton 1.18 here dup rawinput ! cookedinput !
344 : anton 1.41 here unused rot read-file throw
345 :     dup here + endrawinput !
346 :     allot
347 : pazsan 1.2 align
348 : anton 1.17 checksyncline
349 : anton 1.18 \ begin
350 :     \ getinput dup eof-char = ?EXIT emit true ?nextchar
351 :     \ again ;
352 : anton 1.1 primitives2something ;
353 :    
354 :     \ types
355 :    
356 : anton 1.49 : stack-access ( n stack -- )
357 :     \ print a stack access at index n of stack
358 :     stack-pointer 2@ type
359 :     dup
360 :     if
361 :     ." [" 0 .r ." ]"
362 :     else
363 :     drop ." TOS"
364 :     endif ;
365 : anton 1.1
366 : anton 1.53 : item-in-index { item -- n }
367 : anton 1.49 \ n is the index of item (in the in-effect)
368 : anton 1.53 item item-stack @ dup >r stack-in @ ( in-size r:stack )
369 :     item r> stack-in-index-xt @ execute ;
370 : anton 1.1
371 :     : fetch-single ( item -- )
372 : anton 1.49 \ fetch a single stack item from its stack
373 : anton 1.1 >r
374 : pazsan 1.8 r@ item-name 2@ type
375 :     ." = ("
376 : anton 1.1 r@ item-type @ type-c-name 2@ type ." ) "
377 : anton 1.49 r@ item-in-index r@ item-stack @ stack-access
378 :     ." ;" cr
379 : anton 1.1 rdrop ;
380 :    
381 :     : fetch-double ( item -- )
382 : anton 1.49 \ fetch a double stack item from its stack
383 : anton 1.1 >r
384 : anton 1.20 ." FETCH_DCELL("
385 :     r@ item-name 2@ type ." , "
386 : anton 1.49 r@ item-in-index r@ item-stack @ 2dup stack-access
387 :     ." , " -1 under+ stack-access
388 : anton 1.20 ." );" cr
389 : anton 1.1 rdrop ;
390 :    
391 : anton 1.49 : same-as-in? ( item -- f )
392 :     \ f is true iff the offset and stack of item is the same as on input
393 : anton 1.1 >r
394 :     r@ item-name 2@ items @ search-wordlist 0=
395 : pazsan 1.8 abort" bug"
396 : anton 1.1 execute @
397 :     dup r@ =
398 :     if \ item first appeared in output
399 :     drop false
400 :     else
401 : anton 1.49 dup item-stack @ r@ item-stack @ =
402 :     swap item-offset @ r@ item-offset @ = and
403 : anton 1.1 endif
404 :     rdrop ;
405 :    
406 : anton 1.49 : item-out-index ( item -- n )
407 :     \ n is the index of item (in the in-effect)
408 :     >r r@ item-stack @ stack-out @ r> item-offset @ - 1- ;
409 : pazsan 1.31
410 : anton 1.1 : really-store-single ( item -- )
411 :     >r
412 : anton 1.49 r@ item-out-index r@ item-stack @ stack-access ." = "
413 :     r@ item-stack @ stack-cast 2@ type
414 : anton 1.1 r@ item-name 2@ type ." ;"
415 :     rdrop ;
416 :    
417 :     : store-single ( item -- )
418 :     >r
419 : anton 1.49 r@ same-as-in?
420 : anton 1.1 if
421 : anton 1.49 r@ item-in-index 0= r@ item-out-index 0= xor
422 : anton 1.1 if
423 : anton 1.49 ." IF_" r@ item-stack @ stack-pointer 2@ type
424 :     ." TOS(" r@ really-store-single ." );" cr
425 : anton 1.1 endif
426 :     else
427 :     r@ really-store-single cr
428 :     endif
429 :     rdrop ;
430 :    
431 :     : store-double ( item -- )
432 :     \ !! store optimization is not performed, because it is not yet needed
433 :     >r
434 : anton 1.20 ." STORE_DCELL(" r@ item-name 2@ type ." , "
435 : anton 1.49 r@ item-out-index r@ item-stack @ 2dup stack-access
436 :     ." , " -1 under+ stack-access
437 : anton 1.20 ." );" cr
438 : anton 1.1 rdrop ;
439 :    
440 : anton 1.54 : single ( -- xt1 xt2 n )
441 :     ['] fetch-single ['] store-single 1 ;
442 : anton 1.1
443 : anton 1.54 : double ( -- xt1 xt2 n )
444 :     ['] fetch-double ['] store-double 2 ;
445 : anton 1.1
446 :     : s, ( addr u -- )
447 :     \ allocate a string
448 :     here swap dup allot move ;
449 :    
450 : anton 1.50 wordlist constant prefixes
451 :    
452 :     : declare ( addr "name" -- )
453 :     \ remember that there is a stack item at addr called name
454 :     create , ;
455 :    
456 :     : !default ( w addr -- )
457 :     dup @ if
458 :     2drop \ leave nonzero alone
459 :     else
460 :     !
461 :     endif ;
462 :    
463 :     : create-type { addr u xt1 xt2 n stack -- } ( "prefix" -- )
464 : anton 1.49 \ describes a type
465 :     \ addr u specifies the C type name
466 :     \ stack effect entries of the type start with prefix
467 :     create type% %allot >r
468 :     addr u save-mem r@ type-c-name 2!
469 :     xt1 r@ type-fetch !
470 :     xt2 r@ type-store !
471 :     n r@ type-size !
472 :     stack r@ type-stack !
473 :     rdrop ;
474 : anton 1.1
475 : anton 1.54 : type-prefix ( xt1 xt2 n stack "prefix" -- )
476 : anton 1.50 create-type
477 :     does> ( item -- )
478 :     \ initialize item
479 :     { item typ }
480 :     typ item item-type !
481 :     typ type-stack @ item item-stack !default
482 :     item item-name 2@ items @ search-wordlist 0= if \ new name
483 :     item item-name 2@ 2dup nextname item declare
484 :     typ type-c-name 2@ type space type ." ;" cr
485 :     else
486 :     drop
487 :     endif ;
488 :    
489 :     : execute-prefix ( item addr1 u1 -- )
490 :     \ execute the word ( item -- ) associated with the longest prefix
491 :     \ of addr1 u1
492 :     0 swap ?do
493 :     dup i prefixes search-wordlist
494 :     if \ ok, we have the type ( item addr1 xt )
495 :     nip execute
496 :     UNLOOP EXIT
497 :     endif
498 :     -1 s+loop
499 :     \ we did not find a type, abort
500 :     true abort" unknown prefix" ;
501 : anton 1.1
502 :     : declaration ( item -- )
503 : anton 1.50 dup item-name 2@ execute-prefix ;
504 : anton 1.1
505 : anton 1.51 : stack-prefix ( stack "prefix" -- )
506 :     name tuck nextname create ( stack length ) 2,
507 :     does> ( item -- )
508 :     2@ { item stack prefix-length }
509 :     item item-name 2@ prefix-length /string item item-name 2!
510 :     stack item item-stack !
511 :     item declaration ;
512 :    
513 : anton 1.1 : declaration-list ( addr1 addr2 -- )
514 :     swap ?do
515 :     i declaration
516 : anton 1.49 item% %size +loop ;
517 : pazsan 1.8
518 : anton 1.1 : declarations ( -- )
519 :     wordlist dup items ! set-current
520 :     effect-in effect-in-end @ declaration-list
521 :     effect-out effect-out-end @ declaration-list ;
522 :    
523 :     \ offset computation
524 :     \ the leftmost (i.e. deepest) item has offset 0
525 :     \ the rightmost item has the highest offset
526 :    
527 : anton 1.49 : compute-offset { item xt -- }
528 :     \ xt specifies in/out; update stack-in/out and set item-offset
529 :     item item-type @ type-size @
530 :     item item-stack @ xt execute dup @ >r +!
531 :     r> item item-offset ! ;
532 :    
533 :     : compute-list ( addr1 addr2 xt -- )
534 :     { xt }
535 :     swap u+do
536 :     i xt compute-offset
537 :     item% %size +loop ;
538 :    
539 :     : clear-stack { -- }
540 :     dup stack-in off stack-out off ;
541 : anton 1.1
542 :     : compute-offsets ( -- )
543 : anton 1.51 data-stack clear-stack fp-stack clear-stack return-stack clear-stack
544 : anton 1.53 inst-stream clear-stack
545 : anton 1.49 effect-in effect-in-end @ ['] stack-in compute-list
546 : anton 1.53 effect-out effect-out-end @ ['] stack-out compute-list
547 :     inst-stream stack-out @ 0<> abort" # can only be on the input side" ;
548 : anton 1.49
549 :     : flush-a-tos { stack -- }
550 :     stack stack-out @ 0<> stack stack-in @ 0= and
551 :     if
552 :     ." IF_" stack stack-pointer 2@ 2dup type ." TOS("
553 :     2dup type ." [0] = " type ." TOS);" cr
554 :     endif ;
555 : anton 1.1
556 :     : flush-tos ( -- )
557 : anton 1.51 data-stack flush-a-tos
558 :     fp-stack flush-a-tos
559 :     return-stack flush-a-tos ;
560 : anton 1.49
561 :     : fill-a-tos { stack -- }
562 :     stack stack-out @ 0= stack stack-in @ 0<> and
563 :     if
564 :     ." IF_" stack stack-pointer 2@ 2dup type ." TOS("
565 :     2dup type ." TOS = " type ." [0]);" cr
566 :     endif ;
567 : anton 1.1
568 :     : fill-tos ( -- )
569 : anton 1.53 \ !! inst-stream for prefetching?
570 : anton 1.51 fp-stack fill-a-tos
571 :     data-stack fill-a-tos
572 :     return-stack fill-a-tos ;
573 : anton 1.49
574 :     : fetch ( addr -- )
575 :     dup item-type @ type-fetch @ execute ;
576 : anton 1.1
577 :     : fetches ( -- )
578 :     effect-in-end @ effect-in ?do
579 :     i fetch
580 : anton 1.49 item% %size +loop ;
581 :    
582 :     : stack-pointer-update { stack -- }
583 :     \ stack grow downwards
584 :     stack stack-in @ stack stack-out @ -
585 :     ?dup-if \ this check is not necessary, gcc would do this for us
586 :     stack stack-pointer 2@ type ." += " 0 .r ." ;" cr
587 :     endif ;
588 : anton 1.1
589 : anton 1.55 : inst-pointer-update ( -- )
590 :     inst-stream stack-in @ ?dup-if
591 :     ." INC_IP(" 0 .r ." );" cr
592 :     endif ;
593 :    
594 : anton 1.1 : stack-pointer-updates ( -- )
595 : anton 1.55 inst-pointer-update
596 : anton 1.51 data-stack stack-pointer-update
597 :     fp-stack stack-pointer-update
598 :     return-stack stack-pointer-update ;
599 : anton 1.1
600 :     : store ( item -- )
601 :     \ f is true if the item should be stored
602 :     \ f is false if the store is probably not necessary
603 : anton 1.49 dup item-type @ type-store @ execute ;
604 : anton 1.1
605 :     : stores ( -- )
606 :     effect-out-end @ effect-out ?do
607 :     i store
608 : anton 1.49 item% %size +loop ;
609 : pazsan 1.8
610 : anton 1.52 : output-c-tail ( -- )
611 :     \ the final part of the generated C code
612 :     ." NEXT_P1;" cr
613 :     stores
614 :     fill-tos
615 :     ." NEXT_P2;" cr ;
616 :    
617 :     : type-c ( c-addr u -- )
618 :     \ like TYPE, but replaces "TAIL;" with tail code
619 :     begin ( c-addr1 u1 )
620 :     2dup s" TAIL;" search
621 :     while ( c-addr1 u1 c-addr3 u3 )
622 :     2dup 2>r drop nip over - type
623 :     output-c-tail
624 :     2r> 5 /string
625 :     \ !! resync #line missing
626 :     repeat
627 :     2drop type ;
628 :    
629 : jwilke 1.43 : output-c ( -- )
630 : anton 1.45 ." I_" c-name 2@ type ." : /* " forth-name 2@ type ." ( " stack-string 2@ type ." ) */" cr
631 : anton 1.1 ." /* " doc 2@ type ." */" cr
632 : anton 1.13 ." NAME(" [char] " emit forth-name 2@ type [char] " emit ." )" cr \ debugging
633 : anton 1.1 ." {" cr
634 :     ." DEF_CA" cr
635 :     declarations
636 :     compute-offsets \ for everything else
637 : anton 1.13 ." NEXT_P0;" cr
638 :     flush-tos
639 : anton 1.1 fetches
640 : anton 1.13 stack-pointer-updates
641 : anton 1.1 ." {" cr
642 : anton 1.17 ." #line " c-line @ . [char] " emit c-filename 2@ type [char] " emit cr
643 : anton 1.52 c-code 2@ type-c
644 : anton 1.1 ." }" cr
645 : anton 1.52 output-c-tail
646 : anton 1.1 ." }" cr
647 :     cr
648 :     ;
649 :    
650 : anton 1.56 : print-type-prefix ( type -- )
651 :     body> >head .name ;
652 :    
653 :     : disasm-arg { item -- }
654 :     item item-stack @ inst-stream = if
655 :     ." printarg_" item item-type @ print-type-prefix
656 :     ." (ip[" item item-offset @ 1+ 0 .r ." ]);" cr
657 :     endif ;
658 :    
659 :     : disasm-args ( -- )
660 :     effect-in-end @ effect-in ?do
661 :     i disasm-arg
662 :     item% %size +loop ;
663 :    
664 :     : output-disasm ( -- )
665 :     \ generate code for disassembling VM instructions
666 :     ." if (ip[0] == prim[" function-number @ 0 .r ." ]) {" cr
667 :     ." fputs(" [char] " emit forth-name 2@ type [char] " emit ." ,stdout);" cr
668 :     ." /* " declarations ." */" cr
669 :     compute-offsets
670 :     disasm-args
671 :     ." ip += " inst-stream stack-in @ 1+ 0 .r ." ;" cr
672 :     ." } else "
673 :     1 function-number +! ;
674 :    
675 : anton 1.49 : stack-used? { stack -- f }
676 :     stack stack-in @ stack stack-out @ or 0<> ;
677 : jwilke 1.44
678 : pazsan 1.30 : output-funclabel ( -- )
679 :     1 function-number +!
680 :     ." &I_" c-name 2@ type ." ," cr ;
681 :    
682 :     : output-forthname ( -- )
683 :     1 function-number +!
684 :     '" emit forth-name 2@ type '" emit ." ," cr ;
685 :    
686 :     : output-c-func ( -- )
687 : jwilke 1.44 \ used for word libraries
688 : pazsan 1.30 1 function-number +!
689 : jwilke 1.44 ." Cell * I_" c-name 2@ type ." (Cell *SP, Cell **FP) /* " forth-name 2@ type
690 : pazsan 1.30 ." ( " stack-string 2@ type ." ) */" cr
691 :     ." /* " doc 2@ type ." */" cr
692 :     ." NAME(" [char] " emit forth-name 2@ type [char] " emit ." )" cr
693 :     \ debugging
694 :     ." {" cr
695 :     declarations
696 :     compute-offsets \ for everything else
697 : anton 1.53 inst-stream stack-used? IF ." Cell *ip=IP;" cr THEN
698 : anton 1.51 data-stack stack-used? IF ." Cell *sp=SP;" cr THEN
699 :     fp-stack stack-used? IF ." Cell *fp=*FP;" cr THEN
700 :     return-stack stack-used? IF ." Cell *rp=*RP;" cr THEN
701 : pazsan 1.30 flush-tos
702 :     fetches
703 :     stack-pointer-updates
704 : anton 1.49 fp-stack stack-used? IF ." *FP=fp;" cr THEN
705 : pazsan 1.30 ." {" cr
706 :     ." #line " c-line @ . [char] " emit c-filename 2@ type [char] " emit cr
707 :     c-code 2@ type
708 :     ." }" cr
709 :     stores
710 :     fill-tos
711 : jwilke 1.44 ." return (sp);" cr
712 : pazsan 1.30 ." }" cr
713 :     cr ;
714 :    
715 : jwilke 1.43 : output-label ( -- )
716 : pazsan 1.34 ." (Label)&&I_" c-name 2@ type ." ," cr
717 :     -1 primitive-number +! ;
718 : anton 1.1
719 : jwilke 1.43 : output-alias ( -- )
720 : pazsan 1.38 ( primitive-number @ . ." alias " ) ." Primitive " forth-name 2@ type cr
721 : pazsan 1.34 -1 primitive-number +! ;
722 : anton 1.1
723 : jwilke 1.43 : output-forth ( -- )
724 : pazsan 1.30 forth-code @ 0=
725 :     IF \ output-alias
726 : jwilke 1.28 \ this is bad for ec: an alias is compiled if tho word does not exist!
727 :     \ JAW
728 : pazsan 1.30 ELSE ." : " forth-name 2@ type ." ( "
729 : anton 1.49 stack-string 2@ type ." )" cr
730 : pazsan 1.30 forth-code 2@ type cr
731 :     -1 primitive-number +!
732 :     THEN ;
733 : anton 1.10
734 : anton 1.17 : output-tag-file ( -- )
735 :     name-filename 2@ last-name-filename 2@ compare if
736 :     name-filename 2@ last-name-filename 2!
737 :     #ff emit cr
738 :     name-filename 2@ type
739 :     ." ,0" cr
740 :     endif ;
741 :    
742 :     : output-tag ( -- )
743 :     output-tag-file
744 :     forth-name 2@ 1+ type
745 :     127 emit
746 :     space forth-name 2@ type space
747 :     1 emit
748 :     name-line @ 0 .r
749 :     ." ,0" cr ;
750 :    
751 : anton 1.10 [IFDEF] documentation
752 :     : register-doc ( -- )
753 :     get-current documentation set-current
754 :     forth-name 2@ nextname create
755 :     forth-name 2@ 2,
756 : anton 1.15 stack-string 2@ condition-stack-effect 2,
757 : anton 1.10 wordset 2@ 2,
758 : anton 1.15 c-name 2@ condition-pronounciation 2,
759 : anton 1.10 doc 2@ 2,
760 :     set-current ;
761 :     [THEN]
762 : pazsan 1.8
763 : anton 1.1 : process-file ( addr u xt -- )
764 : anton 1.17 >r
765 :     2dup filename 2!
766 : pazsan 1.30 0 function-number !
767 : anton 1.17 r/o open-file abort" cannot open file"
768 :     warnings @ if
769 :     ." ------------ CUT HERE -------------" cr endif
770 :     r> primfilter ;
771 : pazsan 1.30
772 :     : process ( xt -- )
773 :     bl word count rot
774 :     process-file ;

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help