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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help