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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help