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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help