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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help