[gforth] / gforth / prims2x.fs  

gforth: gforth/prims2x.fs


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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help