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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help