[gforth] / gforth / prims2x.fs  

gforth: gforth/prims2x.fs


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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help