[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.71 \ This is not very nice (hard limits, no checking, assumes 1 chars = 1).
23 :     \ And it grew even worse when it aged.
24 : anton 1.1
25 :     \ Optimizations:
26 :     \ superfluous stores are removed. GCC removes the superfluous loads by itself
27 :     \ TOS and FTOS can be kept in register( variable)s.
28 :     \
29 :     \ Problems:
30 :     \ The TOS optimization is somewhat hairy. The problems by example:
31 :     \ 1) dup ( w -- w w ): w=TOS; sp-=1; sp[1]=w; TOS=w;
32 :     \ The store is not superfluous although the earlier opt. would think so
33 :     \ Alternatively: sp[0]=TOS; w=TOS; sp-=1; TOS=w;
34 :     \ 2) ( -- .. ): sp[0] = TOS; ... /* This additional store is necessary */
35 :     \ 3) ( .. -- ): ... TOS = sp[0]; /* as well as this load */
36 :     \ 4) ( -- ): /* but here they are unnecessary */
37 :     \ 5) Words that call NEXT themselves have to be done very carefully.
38 :     \
39 :     \ To do:
40 : pazsan 1.8 \ add the store optimization for doubles
41 : anton 1.1 \ regarding problem 1 above: It would be better (for over) to implement
42 :     \ the alternative
43 : anton 1.80 \ store optimization for combined instructions.
44 :    
45 :     \ Design Uglyness:
46 :    
47 :     \ - global state (values, variables) in connection with combined instructions.
48 :    
49 :     \ - index computation is different for instruction-stream and the
50 :     \ stacks; there are two mechanisms for dealing with that
51 :     \ (stack-in-index-xt and a test for stack==instruction-stream); there
52 :     \ should be only one.
53 : anton 1.1
54 : pazsan 1.3 warnings off
55 :    
56 : jwilke 1.97 [IFUNDEF] try
57 :     include startup.fs
58 :     [THEN]
59 :    
60 : anton 1.49 : struct% struct ; \ struct is redefined in gray
61 :    
62 : pazsan 1.98 warnings off
63 : anton 1.110 \ warnings on
64 : pazsan 1.98
65 : jwilke 1.39 include ./gray.fs
66 : anton 1.69 32 constant max-effect \ number of things on one side of a stack effect
67 : anton 1.71 4 constant max-stacks \ the max. number of stacks (including inst-stream).
68 : anton 1.1 255 constant maxchar
69 :     maxchar 1+ constant eof-char
70 : anton 1.17 #tab constant tab-char
71 :     #lf constant nl-char
72 : anton 1.1
73 : anton 1.18 variable rawinput \ pointer to next character to be scanned
74 :     variable endrawinput \ pointer to the end of the input (the char after the last)
75 :     variable cookedinput \ pointer to the next char to be parsed
76 : anton 1.17 variable line \ line number of char pointed to by input
77 : anton 1.65 variable line-start \ pointer to start of current line (for error messages)
78 :     0 line !
79 : anton 1.17 2variable filename \ filename of original input file
80 :     0 0 filename 2!
81 : anton 1.111 2variable out-filename \ filename of the output file (for sync lines)
82 :     0 0 out-filename 2!
83 : pazsan 1.25 2variable f-comment
84 :     0 0 f-comment 2!
85 : anton 1.17 variable skipsynclines \ are sync lines ("#line ...") invisible to the parser?
86 : anton 1.111 skipsynclines on
87 :     variable out-nls \ newlines in output (for output sync lines)
88 :     0 out-nls !
89 : anton 1.112 variable store-optimization \ use store optimization?
90 :     store-optimization off
91 :    
92 : anton 1.1
93 : anton 1.72 : th ( addr1 n -- addr2 )
94 :     cells + ;
95 :    
96 :     : holds ( addr u -- )
97 :     \ like HOLD, but for a string
98 :     tuck + swap 0 +do
99 :     1- dup c@ hold
100 :     loop
101 :     drop ;
102 : anton 1.71
103 : anton 1.82 : insert-wordlist { c-addr u wordlist xt -- }
104 : anton 1.81 \ adds name "addr u" to wordlist using defining word xt
105 :     \ xt may cause additional stack effects
106 :     get-current >r wordlist set-current
107 :     c-addr u nextname xt execute
108 :     r> set-current ;
109 :    
110 : anton 1.1 : start ( -- addr )
111 : anton 1.18 cookedinput @ ;
112 : anton 1.1
113 :     : end ( addr -- addr u )
114 : anton 1.18 cookedinput @ over - ;
115 : anton 1.1
116 : anton 1.71 : print-error-line ( -- )
117 :     \ print the current line and position
118 :     line-start @ endrawinput @ over - 2dup nl-char scan drop nip ( start end )
119 :     over - type cr
120 :     line-start @ rawinput @ over - typewhite ." ^" cr ;
121 :    
122 :     : ?print-error { f addr u -- }
123 :     f ?not? if
124 :     outfile-id >r try
125 :     stderr to outfile-id
126 :     filename 2@ type ." :" line @ 0 .r ." : " addr u type cr
127 :     print-error-line
128 :     0
129 :     recover endtry
130 :     r> to outfile-id throw
131 : anton 1.111 1 (bye) \ abort
132 : anton 1.71 endif ;
133 :    
134 : anton 1.63 : quote ( -- )
135 :     [char] " emit ;
136 :    
137 : anton 1.111 \ count output lines to generate sync lines for output
138 :    
139 :     : count-nls ( addr u -- )
140 :     bounds u+do
141 :     i c@ nl-char = negate out-nls +!
142 :     loop ;
143 :    
144 :     :noname ( addr u -- )
145 :     2dup count-nls
146 :     defers type ;
147 :     is type
148 :    
149 : anton 1.72 variable output \ xt ( -- ) of output word for simple primitives
150 :     variable output-combined \ xt ( -- ) of output word for combined primitives
151 : anton 1.1
152 : anton 1.49 struct%
153 : anton 1.71 cell% field stack-number \ the number of this stack
154 : anton 1.49 cell% 2* field stack-pointer \ stackpointer name
155 : anton 1.74 cell% field stack-type \ name for default type of stack items
156 : anton 1.53 cell% field stack-in-index-xt \ ( in-size item -- in-index )
157 : anton 1.49 end-struct stack%
158 :    
159 : anton 1.53 struct%
160 :     cell% 2* field item-name \ name, excluding stack prefixes
161 :     cell% field item-stack \ descriptor for the stack used, 0 is default
162 :     cell% field item-type \ descriptor for the item type
163 :     cell% field item-offset \ offset in stack items, 0 for the deepest element
164 : anton 1.66 cell% field item-first \ true if this is the first occurence of the item
165 : anton 1.53 end-struct item%
166 :    
167 :     struct%
168 :     cell% 2* field type-c-name
169 :     cell% field type-stack \ default stack
170 :     cell% field type-size \ size of type in stack items
171 :     cell% field type-fetch \ xt of fetch code generator ( item -- )
172 :     cell% field type-store \ xt of store code generator ( item -- )
173 :     end-struct type%
174 :    
175 : anton 1.72 variable next-stack-number 0 next-stack-number !
176 :     create stacks max-stacks cells allot \ array of stacks
177 :    
178 : anton 1.53 : stack-in-index ( in-size item -- in-index )
179 :     item-offset @ - 1- ;
180 :    
181 :     : inst-in-index ( in-size item -- in-index )
182 :     nip dup item-offset @ swap item-type @ type-size @ + 1- ;
183 :    
184 : anton 1.92 : make-stack ( addr-ptr u1 type "stack-name" -- )
185 :     next-stack-number @ max-stacks < s" too many stacks" ?print-error
186 : anton 1.49 create stack% %allot >r
187 : anton 1.72 r@ stacks next-stack-number @ th !
188 : anton 1.92 next-stack-number @ r@ stack-number !
189 :     1 next-stack-number +!
190 : anton 1.74 r@ stack-type !
191 : anton 1.53 save-mem r@ stack-pointer 2!
192 :     ['] stack-in-index r> stack-in-index-xt ! ;
193 : anton 1.49
194 : anton 1.92 : map-stacks { xt -- }
195 :     \ perform xt for all stacks except inst-stream
196 :     next-stack-number @ 1 +do
197 :     stacks i th @ xt execute
198 :     loop ;
199 :    
200 : anton 1.49 \ stack items
201 :    
202 :     : init-item ( addr u addr1 -- )
203 :     \ initialize item at addr1 with name addr u
204 :     \ !! remove stack prefix
205 :     dup item% %size erase
206 :     item-name 2! ;
207 :    
208 : anton 1.64 : map-items { addr end xt -- }
209 :     \ perform xt for all items in array addr...end
210 :     end addr ?do
211 :     i xt execute
212 :     item% %size +loop ;
213 :    
214 : anton 1.77 \ types
215 :    
216 :     : print-type-prefix ( type -- )
217 :     body> >head name>string type ;
218 :    
219 : anton 1.49 \ various variables for storing stuff of one primitive
220 : anton 1.1
221 : anton 1.69 struct%
222 :     cell% 2* field prim-name
223 :     cell% 2* field prim-wordset
224 :     cell% 2* field prim-c-name
225 :     cell% 2* field prim-doc
226 :     cell% 2* field prim-c-code
227 :     cell% 2* field prim-forth-code
228 :     cell% 2* field prim-stack-string
229 : anton 1.82 cell% field prim-num \ ordinal number
230 : anton 1.75 cell% field prim-items-wordlist \ unique items
231 : anton 1.69 item% max-effect * field prim-effect-in
232 :     item% max-effect * field prim-effect-out
233 :     cell% field prim-effect-in-end
234 :     cell% field prim-effect-out-end
235 : anton 1.71 cell% max-stacks * field prim-stacks-in \ number of in items per stack
236 :     cell% max-stacks * field prim-stacks-out \ number of out items per stack
237 : anton 1.69 end-struct prim%
238 :    
239 : anton 1.70 : make-prim ( -- prim )
240 :     prim% %alloc { p }
241 :     s" " p prim-doc 2! s" " p prim-forth-code 2! s" " p prim-wordset 2!
242 :     p ;
243 :    
244 : anton 1.79 0 value prim \ in combined prims either combined or a part
245 :     0 value combined \ in combined prims the combined prim
246 :     variable in-part \ true if processing a part
247 :     in-part off
248 :    
249 :     1000 constant max-combined
250 :     create combined-prims max-combined cells allot
251 :     variable num-combined
252 :    
253 : anton 1.81 table constant combinations
254 :     \ the keys are the sequences of pointers to primitives
255 :    
256 : anton 1.79 create current-depth max-stacks cells allot
257 :     create max-depth max-stacks cells allot
258 :     create min-depth max-stacks cells allot
259 : anton 1.69
260 : anton 1.71 wordlist constant primitives
261 :    
262 :     : create-prim ( prim -- )
263 : anton 1.82 dup prim-name 2@ primitives ['] constant insert-wordlist ;
264 : anton 1.71
265 :     : stack-in ( stack -- addr )
266 :     \ address of number of stack items in effect in
267 :     stack-number @ cells prim prim-stacks-in + ;
268 :    
269 :     : stack-out ( stack -- addr )
270 :     \ address of number of stack items in effect out
271 :     stack-number @ cells prim prim-stacks-out + ;
272 :    
273 : anton 1.69 \ global vars
274 : anton 1.17 variable c-line
275 :     2variable c-filename
276 :     variable name-line
277 :     2variable name-filename
278 :     2variable last-name-filename
279 : pazsan 1.30 Variable function-number 0 function-number !
280 : anton 1.1
281 :     \ a few more set ops
282 :    
283 :     : bit-equivalent ( w1 w2 -- w3 )
284 :     xor invert ;
285 :    
286 :     : complement ( set1 -- set2 )
287 :     empty ['] bit-equivalent binary-set-operation ;
288 :    
289 : anton 1.80 \ stack access stuff
290 : anton 1.79
291 :     : normal-stack-access ( n stack -- )
292 : anton 1.49 stack-pointer 2@ type
293 :     dup
294 :     if
295 :     ." [" 0 .r ." ]"
296 :     else
297 :     drop ." TOS"
298 :     endif ;
299 : anton 1.1
300 : anton 1.80 \ forward declaration for inst-stream (breaks cycle in definitions)
301 :     defer inst-stream-f ( -- stack )
302 :    
303 : anton 1.79 : part-stack-access { n stack -- }
304 : anton 1.80 \ print _<stack><x>, x=inst-stream? n : maxdepth-currentdepth-n-1
305 : anton 1.79 ." _" stack stack-pointer 2@ type
306 :     stack stack-number @ { stack# }
307 : anton 1.80 current-depth stack# th @ n + { access-depth }
308 :     stack inst-stream-f = if
309 :     access-depth
310 :     else
311 :     combined prim-stacks-in stack# th @
312 :     assert( dup max-depth stack# th @ = )
313 :     access-depth - 1-
314 :     endif
315 : anton 1.79 0 .r ;
316 :    
317 :     : stack-access ( n stack -- )
318 :     \ print a stack access at index n of stack
319 :     in-part @ if
320 :     part-stack-access
321 :     else
322 :     normal-stack-access
323 :     endif ;
324 :    
325 : anton 1.53 : item-in-index { item -- n }
326 : anton 1.49 \ n is the index of item (in the in-effect)
327 : anton 1.53 item item-stack @ dup >r stack-in @ ( in-size r:stack )
328 :     item r> stack-in-index-xt @ execute ;
329 : anton 1.1
330 : anton 1.78 : item-stack-type-name ( item -- addr u )
331 :     item-stack @ stack-type @ type-c-name 2@ ;
332 :    
333 : anton 1.1 : fetch-single ( item -- )
334 : anton 1.106 \ fetch a single stack item from its stack
335 :     >r
336 :     ." vm_" r@ item-stack-type-name type
337 :     ." 2" r@ item-type @ print-type-prefix ." ("
338 :     r@ item-in-index r@ item-stack @ stack-access ." ,"
339 :     r@ item-name 2@ type
340 :     ." );" cr
341 :     rdrop ;
342 : anton 1.1
343 :     : fetch-double ( item -- )
344 : anton 1.106 \ fetch a double stack item from its stack
345 :     >r
346 :     ." vm_two"
347 :     r@ item-stack-type-name type ." 2"
348 :     r@ item-type @ print-type-prefix ." ("
349 :     r@ item-in-index r@ item-stack @ 2dup ." (Cell)" stack-access
350 :     ." , " -1 under+ ." (Cell)" stack-access
351 :     ." , " r@ item-name 2@ type
352 :     ." )" cr
353 :     rdrop ;
354 : anton 1.1
355 : anton 1.49 : same-as-in? ( item -- f )
356 :     \ f is true iff the offset and stack of item is the same as on input
357 : anton 1.1 >r
358 : anton 1.74 r@ item-first @ if
359 :     rdrop false exit
360 :     endif
361 : anton 1.75 r@ item-name 2@ prim prim-items-wordlist @ search-wordlist 0= abort" bug"
362 : anton 1.1 execute @
363 :     dup r@ =
364 :     if \ item first appeared in output
365 :     drop false
366 :     else
367 : anton 1.49 dup item-stack @ r@ item-stack @ =
368 :     swap item-offset @ r@ item-offset @ = and
369 : anton 1.1 endif
370 :     rdrop ;
371 :    
372 : anton 1.49 : item-out-index ( item -- n )
373 :     \ n is the index of item (in the in-effect)
374 :     >r r@ item-stack @ stack-out @ r> item-offset @ - 1- ;
375 : pazsan 1.31
376 : anton 1.1 : really-store-single ( item -- )
377 : anton 1.106 >r
378 :     ." vm_"
379 :     r@ item-type @ print-type-prefix ." 2"
380 :     r@ item-stack-type-name type ." ("
381 :     r@ item-name 2@ type ." ,"
382 :     r@ item-out-index r@ item-stack @ stack-access ." );"
383 :     rdrop ;
384 : anton 1.1
385 :     : store-single ( item -- )
386 : anton 1.112 >r
387 :     store-optimization @ r@ same-as-in? and if
388 :     r@ item-in-index 0= r@ item-out-index 0= xor if
389 :     ." IF_" r@ item-stack @ stack-pointer 2@ type
390 :     ." TOS(" r@ really-store-single ." );" cr
391 :     endif
392 :     else
393 :     r@ really-store-single cr
394 :     endif
395 :     rdrop ;
396 : anton 1.1
397 :     : store-double ( item -- )
398 :     \ !! store optimization is not performed, because it is not yet needed
399 :     >r
400 : anton 1.78 ." vm_"
401 :     r@ item-type @ print-type-prefix ." 2two"
402 :     r@ item-stack-type-name type ." ("
403 :     r@ item-name 2@ type ." , "
404 : anton 1.49 r@ item-out-index r@ item-stack @ 2dup stack-access
405 :     ." , " -1 under+ stack-access
406 : anton 1.106 ." )" cr
407 : anton 1.1 rdrop ;
408 :    
409 : anton 1.54 : single ( -- xt1 xt2 n )
410 :     ['] fetch-single ['] store-single 1 ;
411 : anton 1.1
412 : anton 1.54 : double ( -- xt1 xt2 n )
413 :     ['] fetch-double ['] store-double 2 ;
414 : anton 1.1
415 :     : s, ( addr u -- )
416 :     \ allocate a string
417 :     here swap dup allot move ;
418 :    
419 : anton 1.50 wordlist constant prefixes
420 :    
421 :     : declare ( addr "name" -- )
422 :     \ remember that there is a stack item at addr called name
423 :     create , ;
424 :    
425 :     : !default ( w addr -- )
426 :     dup @ if
427 :     2drop \ leave nonzero alone
428 :     else
429 :     !
430 :     endif ;
431 :    
432 :     : create-type { addr u xt1 xt2 n stack -- } ( "prefix" -- )
433 : anton 1.49 \ describes a type
434 :     \ addr u specifies the C type name
435 :     \ stack effect entries of the type start with prefix
436 :     create type% %allot >r
437 :     addr u save-mem r@ type-c-name 2!
438 :     xt1 r@ type-fetch !
439 :     xt2 r@ type-store !
440 :     n r@ type-size !
441 :     stack r@ type-stack !
442 :     rdrop ;
443 : anton 1.1
444 : anton 1.105 : type-prefix ( addr u xt1 xt2 n stack "prefix" -- )
445 : anton 1.94 get-current >r prefixes set-current
446 :     create-type r> set-current
447 : anton 1.50 does> ( item -- )
448 :     \ initialize item
449 :     { item typ }
450 :     typ item item-type !
451 :     typ type-stack @ item item-stack !default
452 : anton 1.75 item item-name 2@ prim prim-items-wordlist @ search-wordlist 0= if
453 : anton 1.66 item item-name 2@ nextname item declare
454 :     item item-first on
455 :     \ typ type-c-name 2@ type space type ." ;" cr
456 : anton 1.50 else
457 :     drop
458 : anton 1.66 item item-first off
459 : anton 1.50 endif ;
460 :    
461 :     : execute-prefix ( item addr1 u1 -- )
462 :     \ execute the word ( item -- ) associated with the longest prefix
463 :     \ of addr1 u1
464 :     0 swap ?do
465 :     dup i prefixes search-wordlist
466 :     if \ ok, we have the type ( item addr1 xt )
467 :     nip execute
468 :     UNLOOP EXIT
469 :     endif
470 :     -1 s+loop
471 :     \ we did not find a type, abort
472 : anton 1.81 false s" unknown prefix" ?print-error ;
473 : anton 1.1
474 :     : declaration ( item -- )
475 : anton 1.50 dup item-name 2@ execute-prefix ;
476 : anton 1.1
477 : anton 1.64 : declaration-list ( addr1 addr2 -- )
478 :     ['] declaration map-items ;
479 :    
480 :     : declarations ( -- )
481 : anton 1.75 wordlist dup prim prim-items-wordlist ! set-current
482 : anton 1.69 prim prim-effect-in prim prim-effect-in-end @ declaration-list
483 :     prim prim-effect-out prim prim-effect-out-end @ declaration-list ;
484 : anton 1.64
485 : anton 1.66 : print-declaration { item -- }
486 :     item item-first @ if
487 :     item item-type @ type-c-name 2@ type space
488 :     item item-name 2@ type ." ;" cr
489 :     endif ;
490 :    
491 :     : print-declarations ( -- )
492 : anton 1.69 prim prim-effect-in prim prim-effect-in-end @ ['] print-declaration map-items
493 :     prim prim-effect-out prim prim-effect-out-end @ ['] print-declaration map-items ;
494 : anton 1.66
495 : anton 1.51 : stack-prefix ( stack "prefix" -- )
496 : anton 1.94 get-current >r prefixes set-current
497 : anton 1.51 name tuck nextname create ( stack length ) 2,
498 : anton 1.94 r> set-current
499 : anton 1.51 does> ( item -- )
500 :     2@ { item stack prefix-length }
501 :     item item-name 2@ prefix-length /string item item-name 2!
502 :     stack item item-stack !
503 :     item declaration ;
504 : anton 1.73
505 : anton 1.74 \ types pointed to by stacks for use in combined prims
506 : anton 1.83 \ !! output-c-combined shouldn't use these names!
507 : anton 1.92 : stack-type-name ( addr u "name" -- )
508 :     single 0 create-type ;
509 :    
510 : anton 1.93 wordlist constant type-names \ this is here just to meet the requirement
511 :     \ that a type be a word; it is never used for lookup
512 : anton 1.83
513 : anton 1.93 : stack ( "name" "stack-pointer" "type" -- )
514 :     \ define stack
515 :     name { d: stack-name }
516 :     name { d: stack-pointer }
517 :     name { d: stack-type }
518 :     get-current type-names set-current
519 :     stack-type 2dup nextname stack-type-name
520 :     set-current
521 :     stack-pointer lastxt >body stack-name nextname make-stack ;
522 :    
523 :     stack inst-stream IP Cell
524 : anton 1.73 ' inst-in-index inst-stream stack-in-index-xt !
525 : anton 1.80 ' inst-stream <is> inst-stream-f
526 : anton 1.73 \ !! initialize stack-in and stack-out
527 : anton 1.1
528 :     \ offset computation
529 :     \ the leftmost (i.e. deepest) item has offset 0
530 :     \ the rightmost item has the highest offset
531 :    
532 : anton 1.49 : compute-offset { item xt -- }
533 :     \ xt specifies in/out; update stack-in/out and set item-offset
534 :     item item-type @ type-size @
535 :     item item-stack @ xt execute dup @ >r +!
536 :     r> item item-offset ! ;
537 :    
538 : anton 1.64 : compute-offset-in ( addr1 addr2 -- )
539 :     ['] stack-in compute-offset ;
540 :    
541 :     : compute-offset-out ( addr1 addr2 -- )
542 :     ['] stack-out compute-offset ;
543 : anton 1.49
544 :     : clear-stack { -- }
545 :     dup stack-in off stack-out off ;
546 : anton 1.1
547 :     : compute-offsets ( -- )
548 : anton 1.92 ['] clear-stack map-stacks
549 : anton 1.53 inst-stream clear-stack
550 : anton 1.69 prim prim-effect-in prim prim-effect-in-end @ ['] compute-offset-in map-items
551 :     prim prim-effect-out prim prim-effect-out-end @ ['] compute-offset-out map-items
552 : anton 1.81 inst-stream stack-out @ 0= s" # can only be on the input side" ?print-error ;
553 :    
554 :     : process-simple ( -- )
555 :     prim prim { W^ key } key cell
556 : anton 1.82 combinations ['] constant insert-wordlist
557 : anton 1.81 declarations compute-offsets
558 : anton 1.82 output @ execute ;
559 : anton 1.49
560 :     : flush-a-tos { stack -- }
561 :     stack stack-out @ 0<> stack stack-in @ 0= and
562 :     if
563 :     ." IF_" stack stack-pointer 2@ 2dup type ." TOS("
564 :     2dup type ." [0] = " type ." TOS);" cr
565 :     endif ;
566 : anton 1.1
567 :     : flush-tos ( -- )
568 : anton 1.92 ['] flush-a-tos map-stacks ;
569 : anton 1.49
570 :     : fill-a-tos { stack -- }
571 :     stack stack-out @ 0= stack stack-in @ 0<> and
572 :     if
573 :     ." IF_" stack stack-pointer 2@ 2dup type ." TOS("
574 :     2dup type ." TOS = " type ." [0]);" cr
575 :     endif ;
576 : anton 1.1
577 :     : fill-tos ( -- )
578 : anton 1.53 \ !! inst-stream for prefetching?
579 : anton 1.92 ['] fill-a-tos map-stacks ;
580 : anton 1.49
581 :     : fetch ( addr -- )
582 : anton 1.72 dup item-type @ type-fetch @ execute ;
583 : anton 1.1
584 :     : fetches ( -- )
585 : anton 1.69 prim prim-effect-in prim prim-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.92 ['] stack-pointer-update map-stacks ;
602 : anton 1.1
603 :     : store ( item -- )
604 :     \ f is true if the item should be stored
605 :     \ f is false if the store is probably not necessary
606 : anton 1.49 dup item-type @ type-store @ execute ;
607 : anton 1.1
608 :     : stores ( -- )
609 : anton 1.69 prim prim-effect-out prim prim-effect-out-end @ ['] store map-items ;
610 : pazsan 1.8
611 : anton 1.91 : print-debug-arg { item -- }
612 :     ." fputs(" quote space item item-name 2@ type ." =" quote ." , vm_out); "
613 :     ." printarg_" item item-type @ print-type-prefix
614 :     ." (" item item-name 2@ type ." );" cr ;
615 :    
616 :     : print-debug-args ( -- )
617 :     ." #ifdef VM_DEBUG" cr
618 :     ." if (vm_debug) {" cr
619 :     prim prim-effect-in prim prim-effect-in-end @ ['] print-debug-arg map-items
620 :     \ ." fputc('\n', vm_out);" cr
621 :     ." }" cr
622 :     ." #endif" cr ;
623 :    
624 :     : print-debug-result { item -- }
625 :     item item-first @ if
626 :     item print-debug-arg
627 :     endif ;
628 :    
629 :     : print-debug-results ( -- )
630 :     cr
631 :     ." #ifdef VM_DEBUG" cr
632 :     ." if (vm_debug) {" cr
633 :     ." fputs(" quote ." -- " quote ." , vm_out); "
634 :     prim prim-effect-out prim prim-effect-out-end @ ['] print-debug-result map-items
635 :     ." fputc('\n', vm_out);" cr
636 :     ." }" cr
637 :     ." #endif" cr ;
638 :    
639 : anton 1.86 : output-super-end ( -- )
640 :     prim prim-c-code 2@ s" SET_IP" search if
641 :     ." SUPER_END;" cr
642 :     endif
643 :     2drop ;
644 :    
645 : anton 1.108 : output-c-tail1 ( -- )
646 :     \ the final part of the generated C code except LABEL2 and NEXT_P2
647 : anton 1.86 output-super-end
648 : anton 1.91 print-debug-results
649 : anton 1.52 ." NEXT_P1;" cr
650 :     stores
651 : anton 1.108 fill-tos ;
652 :    
653 :     : output-c-tail ( -- )
654 :     \ the final part of the generated C code, without LABEL2
655 :     output-c-tail1
656 : anton 1.85 ." NEXT_P2;" ;
657 : anton 1.52
658 : anton 1.108 : output-c-tail2 ( -- )
659 :     \ the final part of the generated C code, including LABEL2
660 :     output-c-tail1
661 :     ." LABEL2(" prim prim-c-name 2@ type ." )" cr
662 :     ." NEXT_P2;" cr ;
663 :    
664 : anton 1.85 : type-c-code ( c-addr u xt -- )
665 : anton 1.109 \ like TYPE, but replaces "INST_TAIL;" with tail code produced by xt
666 : anton 1.85 { xt }
667 : anton 1.111 ." {" cr
668 :     ." #line " c-line @ . quote c-filename 2@ type quote cr
669 : anton 1.52 begin ( c-addr1 u1 )
670 : anton 1.109 2dup s" INST_TAIL;" search
671 : anton 1.52 while ( c-addr1 u1 c-addr3 u3 )
672 :     2dup 2>r drop nip over - type
673 : anton 1.85 xt execute
674 : anton 1.109 2r> 10 /string
675 : anton 1.52 \ !! resync #line missing
676 :     repeat
677 : anton 1.111 2drop type
678 :     ." #line " out-nls @ 2 + . quote out-filename 2@ type quote cr
679 :     ." }" cr ;
680 : anton 1.63
681 : anton 1.72 : print-entry ( -- )
682 : anton 1.109 ." LABEL(" prim prim-c-name 2@ type ." )" ;
683 : anton 1.63
684 : jwilke 1.43 : output-c ( -- )
685 : anton 1.111 print-entry ." /* " prim prim-name 2@ type ." ( " prim prim-stack-string 2@ type ." ) */" cr
686 :     ." /* " prim prim-doc 2@ type ." */" cr
687 :     ." NAME(" quote prim prim-name 2@ type quote ." )" cr \ debugging
688 :     ." {" cr
689 :     ." DEF_CA" cr
690 :     print-declarations
691 :     ." NEXT_P0;" cr
692 :     flush-tos
693 :     fetches
694 :     print-debug-args
695 :     stack-pointer-updates
696 :     prim prim-c-code 2@ ['] output-c-tail type-c-code
697 :     output-c-tail2
698 :     ." }" cr
699 :     cr
700 : anton 1.1 ;
701 :    
702 : anton 1.56 : disasm-arg { item -- }
703 :     item item-stack @ inst-stream = if
704 : anton 1.107 ." {" cr
705 :     item print-declaration
706 :     item fetch
707 :     item print-debug-arg
708 :     ." }" cr
709 : anton 1.56 endif ;
710 :    
711 :     : disasm-args ( -- )
712 : anton 1.69 prim prim-effect-in prim prim-effect-in-end @ ['] disasm-arg map-items ;
713 : anton 1.56
714 :     : output-disasm ( -- )
715 :     \ generate code for disassembling VM instructions
716 : anton 1.106 ." if (VM_IS_INST(*ip, " function-number @ 0 .r ." )) {" cr
717 : anton 1.69 ." fputs(" quote prim prim-name 2@ type quote ." , vm_out);" cr
718 : anton 1.56 disasm-args
719 :     ." ip += " inst-stream stack-in @ 1+ 0 .r ." ;" cr
720 : anton 1.91 ." goto _endif_;" cr
721 :     ." }" cr ;
722 : anton 1.56
723 : anton 1.86 : output-profile ( -- )
724 :     \ generate code for postprocessing the VM block profile stuff
725 : anton 1.87 ." if (VM_IS_INST(*ip, " function-number @ 0 .r ." )) {" cr
726 : anton 1.104 ." add_inst(b, " quote prim prim-name 2@ type quote ." );" cr
727 : anton 1.86 ." ip += " inst-stream stack-in @ 1+ 0 .r ." ;" cr
728 :     prim prim-c-code 2@ s" SET_IP" search nip nip
729 :     prim prim-c-code 2@ s" SUPER_END" search nip nip or if
730 :     ." return;" cr
731 : anton 1.91 else
732 :     ." goto _endif_;" cr
733 : anton 1.86 endif
734 : anton 1.91 ." }" cr ;
735 : anton 1.86
736 : anton 1.104 : output-profile-combined ( -- )
737 :     \ generate code for postprocessing the VM block profile stuff
738 :     ." if (VM_IS_INST(*ip, " function-number @ 0 .r ." )) {" cr
739 :     num-combined @ 0 +do
740 :     ." add_inst(b, " quote
741 :     combined-prims i th @ prim-name 2@ type
742 :     quote ." );" cr
743 :     loop
744 :     ." ip += " inst-stream stack-in @ 1+ 0 .r ." ;" cr
745 :     combined-prims num-combined @ 1- th @ prim-c-code 2@ s" SET_IP" search nip nip
746 :     combined-prims num-combined @ 1- th @ prim-c-code 2@ s" SUPER_END" search nip nip or if
747 :     ." return;" cr
748 :     else
749 :     ." goto _endif_;" cr
750 :     endif
751 :     ." }" cr ;
752 :    
753 : anton 1.103 : output-superend ( -- )
754 :     \ output flag specifying whether the current word ends a dynamic superinst
755 :     prim prim-c-code 2@ s" SET_IP" search nip nip
756 :     prim prim-c-code 2@ s" SUPER_END" search nip nip or 0<>
757 :     prim prim-c-code 2@ s" SUPER_CONTINUE" search nip nip 0= and
758 :     negate 0 .r ." , /* " prim prim-name 2@ type ." */" cr ;
759 :    
760 : anton 1.60 : gen-arg-parm { item -- }
761 :     item item-stack @ inst-stream = if
762 :     ." , " item item-type @ type-c-name 2@ type space
763 :     item item-name 2@ type
764 :     endif ;
765 :    
766 :     : gen-args-parm ( -- )
767 : anton 1.69 prim prim-effect-in prim prim-effect-in-end @ ['] gen-arg-parm map-items ;
768 : anton 1.60
769 :     : gen-arg-gen { item -- }
770 :     item item-stack @ inst-stream = if
771 :     ." genarg_" item item-type @ print-type-prefix
772 :     ." (ctp, " item item-name 2@ type ." );" cr
773 :     endif ;
774 :    
775 :     : gen-args-gen ( -- )
776 : anton 1.69 prim prim-effect-in prim prim-effect-in-end @ ['] gen-arg-gen map-items ;
777 : anton 1.60
778 :     : output-gen ( -- )
779 :     \ generate C code for generating VM instructions
780 : anton 1.69 ." void gen_" prim prim-c-name 2@ type ." (Inst **ctp" gen-args-parm ." )" cr
781 : anton 1.60 ." {" cr
782 :     ." gen_inst(ctp, vm_prim[" function-number @ 0 .r ." ]);" cr
783 :     gen-args-gen
784 : anton 1.68 ." }" cr ;
785 : anton 1.60
786 : anton 1.49 : stack-used? { stack -- f }
787 :     stack stack-in @ stack stack-out @ or 0<> ;
788 : jwilke 1.44
789 : pazsan 1.30 : output-funclabel ( -- )
790 : anton 1.69 ." &I_" prim prim-c-name 2@ type ." ," cr ;
791 : pazsan 1.30
792 :     : output-forthname ( -- )
793 : anton 1.69 '" emit prim prim-name 2@ type '" emit ." ," cr ;
794 : pazsan 1.30
795 : anton 1.92 \ : output-c-func ( -- )
796 :     \ \ used for word libraries
797 :     \ ." Cell * I_" prim prim-c-name 2@ type ." (Cell *SP, Cell **FP) /* " prim prim-name 2@ type
798 :     \ ." ( " prim prim-stack-string 2@ type ." ) */" cr
799 :     \ ." /* " prim prim-doc 2@ type ." */" cr
800 :     \ ." NAME(" quote prim prim-name 2@ type quote ." )" cr
801 :     \ \ debugging
802 :     \ ." {" cr
803 :     \ print-declarations
804 :     \ \ !! don't know what to do about that
805 :     \ inst-stream stack-used? IF ." Cell *ip=IP;" cr THEN
806 :     \ data-stack stack-used? IF ." Cell *sp=SP;" cr THEN
807 :     \ fp-stack stack-used? IF ." Cell *fp=*FP;" cr THEN
808 :     \ return-stack stack-used? IF ." Cell *rp=*RP;" cr THEN
809 :     \ flush-tos
810 :     \ fetches
811 :     \ stack-pointer-updates
812 :     \ fp-stack stack-used? IF ." *FP=fp;" cr THEN
813 :     \ ." {" cr
814 :     \ ." #line " c-line @ . quote c-filename 2@ type quote cr
815 :     \ prim prim-c-code 2@ type
816 :     \ ." }" cr
817 :     \ stores
818 :     \ fill-tos
819 :     \ ." return (sp);" cr
820 :     \ ." }" cr
821 :     \ cr ;
822 : pazsan 1.30
823 : jwilke 1.43 : output-label ( -- )
824 : anton 1.102 ." INST_ADDR(" prim prim-c-name 2@ type ." )," cr ;
825 : anton 1.1
826 : jwilke 1.43 : output-alias ( -- )
827 : anton 1.69 ( primitive-number @ . ." alias " ) ." Primitive " prim prim-name 2@ type cr ;
828 : anton 1.1
829 : jwilke 1.43 : output-forth ( -- )
830 : anton 1.69 prim prim-forth-code @ 0=
831 : pazsan 1.30 IF \ output-alias
832 : jwilke 1.28 \ this is bad for ec: an alias is compiled if tho word does not exist!
833 :     \ JAW
834 : anton 1.69 ELSE ." : " prim prim-name 2@ type ." ( "
835 :     prim prim-stack-string 2@ type ." )" cr
836 :     prim prim-forth-code 2@ type cr
837 : pazsan 1.30 THEN ;
838 : anton 1.10
839 : anton 1.17 : output-tag-file ( -- )
840 : anton 1.113 name-filename 2@ last-name-filename 2@ str= 0= if
841 : anton 1.17 name-filename 2@ last-name-filename 2!
842 :     #ff emit cr
843 :     name-filename 2@ type
844 :     ." ,0" cr
845 :     endif ;
846 :    
847 :     : output-tag ( -- )
848 :     output-tag-file
849 : anton 1.69 prim prim-name 2@ 1+ type
850 : anton 1.17 127 emit
851 : anton 1.69 space prim prim-name 2@ type space
852 : anton 1.17 1 emit
853 :     name-line @ 0 .r
854 :     ." ,0" cr ;
855 :    
856 : pazsan 1.100 : output-vi-tag ( -- )
857 :     name-filename 2@ type #tab emit
858 :     prim prim-name 2@ type #tab emit
859 :     ." /^" prim prim-name 2@ type ." *(/" cr ;
860 :    
861 : anton 1.10 [IFDEF] documentation
862 :     : register-doc ( -- )
863 : anton 1.82 prim prim-name 2@ documentation ['] create insert-wordlist
864 : anton 1.69 prim prim-name 2@ 2,
865 :     prim prim-stack-string 2@ condition-stack-effect 2,
866 :     prim prim-wordset 2@ 2,
867 :     prim prim-c-name 2@ condition-pronounciation 2,
868 : anton 1.82 prim prim-doc 2@ 2, ;
869 : anton 1.10 [THEN]
870 : anton 1.67
871 :    
872 : anton 1.69 \ combining instructions
873 :    
874 :     \ The input should look like this:
875 :    
876 :     \ lit_+ = lit +
877 :    
878 :     \ The output should look like this:
879 :    
880 :     \ I_lit_+:
881 :     \ {
882 :     \ DEF_CA
883 :     \ Cell _x_ip0;
884 :     \ Cell _x_sp0;
885 :     \ Cell _x_sp1;
886 :     \ NEXT_P0;
887 :     \ _x_ip0 = (Cell) IPTOS;
888 :     \ _x_sp0 = (Cell) spTOS;
889 :     \ INC_IP(1);
890 :     \ /* sp += 0; */
891 :     \ /* lit ( #w -- w ) */
892 :     \ /* */
893 :     \ NAME("lit")
894 :     \ {
895 :     \ Cell w;
896 :     \ w = (Cell) _x_ip0;
897 :     \ #ifdef VM_DEBUG
898 :     \ if (vm_debug) {
899 :     \ fputs(" w=", vm_out); printarg_w (w);
900 :     \ fputc('\n', vm_out);
901 :     \ }
902 :     \ #endif
903 :     \ {
904 :     \ #line 136 "./prim"
905 :     \ }
906 :     \ _x_sp1 = (Cell)w;
907 :     \ }
908 :     \ I_plus: /* + ( n1 n2 -- n ) */
909 :     \ /* */
910 :     \ NAME("+")
911 :     \ {
912 :     \ DEF_CA
913 :     \ Cell n1;
914 :     \ Cell n2;
915 :     \ Cell n;
916 :     \ NEXT_P0;
917 :     \ n1 = (Cell) _x_sp0;
918 :     \ n2 = (Cell) _x_sp1;
919 :     \ #ifdef VM_DEBUG
920 :     \ if (vm_debug) {
921 :     \ fputs(" n1=", vm_out); printarg_n (n1);
922 :     \ fputs(" n2=", vm_out); printarg_n (n2);
923 :     \ fputc('\n', vm_out);
924 :     \ }
925 :     \ #endif
926 :     \ {
927 :     \ #line 516 "./prim"
928 :     \ n = n1+n2;
929 :     \ }
930 :     \ _x_sp0 = (Cell)n;
931 :     \ }
932 :     \ NEXT_P1;
933 :     \ spTOS = (Cell)_x_sp0;
934 :     \ NEXT_P2;
935 :    
936 : anton 1.71 : init-combined ( -- )
937 : anton 1.79 prim to combined
938 : anton 1.71 0 num-combined !
939 :     current-depth max-stacks cells erase
940 : anton 1.72 max-depth max-stacks cells erase
941 :     min-depth max-stacks cells erase
942 :     prim prim-effect-in prim prim-effect-in-end !
943 :     prim prim-effect-out prim prim-effect-out-end ! ;
944 : anton 1.71
945 :     : max! ( n addr -- )
946 :     tuck @ max swap ! ;
947 :    
948 : anton 1.72 : min! ( n addr -- )
949 :     tuck @ min swap ! ;
950 :    
951 : anton 1.71 : add-depths { p -- }
952 :     \ combine stack effect of p with *-depths
953 :     max-stacks 0 ?do
954 : anton 1.72 current-depth i th @
955 :     p prim-stacks-in i th @ +
956 :     dup max-depth i th max!
957 :     p prim-stacks-out i th @ -
958 :     dup min-depth i th min!
959 :     current-depth i th !
960 : anton 1.71 loop ;
961 :    
962 :     : add-prim ( addr u -- )
963 :     \ add primitive given by "addr u" to combined-prims
964 :     primitives search-wordlist s" unknown primitive" ?print-error
965 :     execute { p }
966 : anton 1.72 p combined-prims num-combined @ th !
967 : anton 1.71 1 num-combined +!
968 :     p add-depths ;
969 :    
970 :     : compute-effects { q -- }
971 :     \ compute the stack effects of q from the depths
972 :     max-stacks 0 ?do
973 : anton 1.72 max-depth i th @ dup
974 :     q prim-stacks-in i th !
975 :     current-depth i th @ -
976 :     q prim-stacks-out i th !
977 :     loop ;
978 :    
979 :     : make-effect-items { stack# items effect-endp -- }
980 :     \ effect-endp points to a pointer to the end of the current item-array
981 :     \ and has to be updated
982 :     stacks stack# th @ { stack }
983 :     items 0 +do
984 :     effect-endp @ { item }
985 :     i 0 <# #s stack stack-pointer 2@ holds [char] _ hold #> save-mem
986 :     item item-name 2!
987 :     stack item item-stack !
988 : anton 1.74 stack stack-type @ item item-type !
989 : anton 1.72 i item item-offset !
990 :     item item-first on
991 :     item% %size effect-endp +!
992 :     loop ;
993 :    
994 :     : init-effects { q -- }
995 :     \ initialize effects field for FETCHES and STORES
996 :     max-stacks 0 ?do
997 :     i q prim-stacks-in i th @ q prim-effect-in-end make-effect-items
998 :     i q prim-stacks-out i th @ q prim-effect-out-end make-effect-items
999 : anton 1.71 loop ;
1000 :    
1001 :     : process-combined ( -- )
1002 : anton 1.81 combined combined-prims num-combined @ cells
1003 : anton 1.82 combinations ['] constant insert-wordlist
1004 : anton 1.86 combined-prims num-combined @ 1- th ( last-part )
1005 :     @ prim-c-code 2@ prim prim-c-code 2! \ used by output-super-end
1006 : anton 1.72 prim compute-effects
1007 :     prim init-effects
1008 :     output-combined perform ;
1009 :    
1010 :     \ C output
1011 :    
1012 :     : print-item { n stack -- }
1013 :     \ print nth stack item name
1014 : anton 1.79 stack stack-type @ type-c-name 2@ type space
1015 :     ." _" stack stack-pointer 2@ type n 0 .r ;
1016 : anton 1.72
1017 :     : print-declarations-combined ( -- )
1018 :     max-stacks 0 ?do
1019 :     max-depth i th @ min-depth i th @ - 0 +do
1020 :     i stacks j th @ print-item ." ;" cr
1021 :     loop
1022 :     loop ;
1023 : anton 1.79
1024 :     : part-fetches ( -- )
1025 :     fetches ;
1026 :    
1027 :     : part-output-c-tail ( -- )
1028 : anton 1.91 print-debug-results
1029 : anton 1.85 stores ;
1030 :    
1031 :     : output-combined-tail ( -- )
1032 :     part-output-c-tail
1033 :     prim >r combined to prim
1034 :     in-part @ >r in-part off
1035 :     output-c-tail
1036 :     r> in-part ! r> to prim ;
1037 : anton 1.79
1038 :     : output-part ( p -- )
1039 :     to prim
1040 :     ." /* " prim prim-name 2@ type ." ( " prim prim-stack-string 2@ type ." ) */" cr
1041 :     ." NAME(" quote prim prim-name 2@ type quote ." )" cr \ debugging
1042 :     ." {" cr
1043 :     print-declarations
1044 :     part-fetches
1045 :     print-debug-args
1046 :     prim add-depths \ !! right place?
1047 : anton 1.85 prim prim-c-code 2@ ['] output-combined-tail type-c-code
1048 : anton 1.79 part-output-c-tail
1049 :     ." }" cr ;
1050 :    
1051 : anton 1.74 : output-parts ( -- )
1052 : anton 1.79 prim >r in-part on
1053 :     current-depth max-stacks cells erase
1054 : anton 1.74 num-combined @ 0 +do
1055 : anton 1.79 combined-prims i th @ output-part
1056 : anton 1.74 loop
1057 : anton 1.79 in-part off
1058 : anton 1.74 r> to prim ;
1059 :    
1060 : anton 1.72 : output-c-combined ( -- )
1061 :     print-entry cr
1062 : anton 1.74 \ debugging messages just in parts
1063 : anton 1.72 ." {" cr
1064 :     ." DEF_CA" cr
1065 :     print-declarations-combined
1066 :     ." NEXT_P0;" cr
1067 :     flush-tos
1068 :     fetches
1069 : anton 1.74 \ print-debug-args
1070 :     stack-pointer-updates
1071 :     output-parts
1072 : anton 1.108 output-c-tail2
1073 : anton 1.74 ." }" cr
1074 :     cr ;
1075 : anton 1.72
1076 :     : output-forth-combined ( -- )
1077 : anton 1.81 ;
1078 :    
1079 :    
1080 : anton 1.83 \ peephole optimization rules
1081 : anton 1.81
1082 :     \ in order for this to work as intended, shorter combinations for each
1083 :     \ length must be present, and the longer combinations must follow
1084 :     \ shorter ones (this restriction may go away in the future).
1085 :    
1086 : anton 1.83 : output-peephole ( -- )
1087 : anton 1.81 combined-prims num-combined @ 1- cells combinations search-wordlist
1088 :     s" the prefix for this combination must be defined earlier" ?print-error
1089 : anton 1.82 ." {"
1090 :     execute prim-num @ 5 .r ." ,"
1091 :     combined-prims num-combined @ 1- th @ prim-num @ 5 .r ." ,"
1092 :     combined prim-num @ 5 .r ." }, /* "
1093 :     combined prim-c-name 2@ type ." */"
1094 :     cr ;
1095 :    
1096 : pazsan 1.90 : output-forth-peephole ( -- )
1097 :     combined-prims num-combined @ 1- cells combinations search-wordlist
1098 :     s" the prefix for this combination must be defined earlier" ?print-error
1099 :     execute prim-num @ 5 .r
1100 :     combined-prims num-combined @ 1- th @ prim-num @ 5 .r
1101 :     combined prim-num @ 5 .r ." prim, \ "
1102 :     combined prim-c-name 2@ type
1103 :     cr ;
1104 :    
1105 : anton 1.69
1106 : anton 1.67 \ the parser
1107 :    
1108 :     eof-char max-member \ the whole character set + EOF
1109 :    
1110 :     : getinput ( -- n )
1111 :     rawinput @ endrawinput @ =
1112 :     if
1113 :     eof-char
1114 :     else
1115 :     cookedinput @ c@
1116 :     endif ;
1117 :    
1118 :     :noname ( n -- )
1119 :     dup bl > if
1120 :     emit space
1121 :     else
1122 :     .
1123 :     endif ;
1124 :     print-token !
1125 :    
1126 :     : testchar? ( set -- f )
1127 :     getinput member? ;
1128 :     ' testchar? test-vector !
1129 :    
1130 :     : checksyncline ( -- )
1131 :     \ when input points to a newline, check if the next line is a
1132 :     \ sync line. If it is, perform the appropriate actions.
1133 :     rawinput @ >r
1134 : anton 1.113 s" #line " r@ over str= 0= if
1135 : anton 1.67 rdrop 1 line +! EXIT
1136 :     endif
1137 :     0. r> 6 chars + 20 >number drop >r drop line ! r> ( c-addr )
1138 :     dup c@ bl = if
1139 : anton 1.81 char+ dup c@ [char] " <> 0= s" sync line syntax" ?print-error
1140 : anton 1.67 char+ dup 100 [char] " scan drop swap 2dup - save-mem filename 2!
1141 :     char+
1142 :     endif
1143 : anton 1.81 dup c@ nl-char <> 0= s" sync line syntax" ?print-error
1144 : anton 1.67 skipsynclines @ if
1145 :     dup char+ rawinput !
1146 :     rawinput @ c@ cookedinput @ c!
1147 :     endif
1148 :     drop ;
1149 :    
1150 :     : ?nextchar ( f -- )
1151 : anton 1.71 s" syntax error, wrong char" ?print-error
1152 : anton 1.67 rawinput @ endrawinput @ <> if
1153 :     rawinput @ c@
1154 :     1 chars rawinput +!
1155 :     1 chars cookedinput +!
1156 :     nl-char = if
1157 :     checksyncline
1158 :     rawinput @ line-start !
1159 :     endif
1160 :     rawinput @ c@ cookedinput @ c!
1161 :     endif ;
1162 :    
1163 :     : charclass ( set "name" -- )
1164 :     ['] ?nextchar terminal ;
1165 :    
1166 :     : .. ( c1 c2 -- set )
1167 :     ( creates a set that includes the characters c, c1<=c<=c2 )
1168 :     empty copy-set
1169 :     swap 1+ rot do
1170 :     i over add-member
1171 :     loop ;
1172 :    
1173 :     : ` ( -- terminal ) ( use: ` c )
1174 :     ( creates anonymous terminal for the character c )
1175 :     char singleton ['] ?nextchar make-terminal ;
1176 :    
1177 :     char a char z .. char A char Z .. union char _ singleton union charclass letter
1178 :     char 0 char 9 .. charclass digit
1179 :     bl singleton tab-char over add-member charclass white
1180 :     nl-char singleton eof-char over add-member complement charclass nonl
1181 :     nl-char singleton eof-char over add-member
1182 :     char : over add-member complement charclass nocolonnl
1183 : anton 1.110 nl-char singleton eof-char over add-member
1184 :     char } over add-member complement charclass nobracenl
1185 : anton 1.67 bl 1+ maxchar .. char \ singleton complement intersection
1186 :     charclass nowhitebq
1187 :     bl 1+ maxchar .. charclass nowhite
1188 :     char " singleton eof-char over add-member complement charclass noquote
1189 :     nl-char singleton charclass nl
1190 :     eof-char singleton charclass eof
1191 : anton 1.79 nl-char singleton eof-char over add-member charclass nleof
1192 : anton 1.67
1193 :     (( letter (( letter || digit )) **
1194 :     )) <- c-ident ( -- )
1195 :    
1196 : anton 1.110 (( ` # ?? (( letter || digit || ` : )) ++
1197 : anton 1.67 )) <- stack-ident ( -- )
1198 :    
1199 :     (( nowhitebq nowhite ** ))
1200 :     <- forth-ident ( -- )
1201 :    
1202 :     Variable forth-flag
1203 :     Variable c-flag
1204 :    
1205 :     (( (( ` e || ` E )) {{ start }} nonl **
1206 :     {{ end evaluate }}
1207 :     )) <- eval-comment ( ... -- ... )
1208 :    
1209 :     (( (( ` f || ` F )) {{ start }} nonl **
1210 :     {{ end forth-flag @ IF type cr ELSE 2drop THEN }}
1211 :     )) <- forth-comment ( -- )
1212 :    
1213 :     (( (( ` c || ` C )) {{ start }} nonl **
1214 :     {{ end c-flag @ IF type cr ELSE 2drop THEN }}
1215 :     )) <- c-comment ( -- )
1216 :    
1217 :     (( ` - nonl ** {{
1218 :     forth-flag @ IF ." [ELSE]" cr THEN
1219 :     c-flag @ IF ." #else" cr THEN }}
1220 :     )) <- else-comment
1221 :    
1222 :     (( ` + {{ start }} nonl ** {{ end
1223 :     dup
1224 :     IF c-flag @
1225 :     IF ." #ifdef HAS_" bounds ?DO I c@ toupper emit LOOP cr
1226 :     THEN
1227 :     forth-flag @
1228 :     IF ." has? " type ." [IF]" cr THEN
1229 :     ELSE 2drop
1230 :     c-flag @ IF ." #endif" cr THEN
1231 :     forth-flag @ IF ." [THEN]" cr THEN
1232 :     THEN }}
1233 :     )) <- if-comment
1234 :    
1235 : pazsan 1.98 (( (( ` g || ` G )) {{ start }} nonl **
1236 :     {{ end
1237 :     forth-flag @ IF ." group " type cr THEN
1238 :     c-flag @ IF ." GROUP(" type ." )" cr THEN }}
1239 :     )) <- group-comment
1240 :    
1241 :     (( (( eval-comment || forth-comment || c-comment || else-comment || if-comment || group-comment )) ?? nonl ** )) <- comment-body
1242 : anton 1.67
1243 : anton 1.79 (( ` \ comment-body nleof )) <- comment ( -- )
1244 : anton 1.67
1245 :     (( {{ start }} stack-ident {{ end 2 pick init-item item% %size + }} white ** )) **
1246 :     <- stack-items
1247 :    
1248 : anton 1.69 (( {{ prim prim-effect-in }} stack-items {{ prim prim-effect-in-end ! }}
1249 : anton 1.67 ` - ` - white **
1250 : anton 1.69 {{ prim prim-effect-out }} stack-items {{ prim prim-effect-out-end ! }}
1251 : anton 1.67 )) <- stack-effect ( -- )
1252 :    
1253 : anton 1.71 (( {{ prim create-prim }}
1254 : anton 1.69 ` ( white ** {{ start }} stack-effect {{ end prim prim-stack-string 2! }} ` ) white **
1255 :     (( {{ start }} forth-ident {{ end prim prim-wordset 2! }} white **
1256 :     (( {{ start }} c-ident {{ end prim prim-c-name 2! }} )) ??
1257 : anton 1.79 )) ?? nleof
1258 :     (( ` " ` " {{ start }} (( noquote ++ ` " )) ++ {{ end 1- prim prim-doc 2! }} ` " white ** nleof )) ??
1259 : anton 1.110 {{ skipsynclines off line @ c-line ! filename 2@ c-filename 2! start }}
1260 :     (( (( ` { nonl ** nleof (( (( nobracenl {{ line @ drop }} nonl ** )) ?? nleof )) ** ` } white ** nleof white ** ))
1261 :     || (( nocolonnl nonl ** nleof white ** )) ** ))
1262 :     {{ end prim prim-c-code 2! skipsynclines on }}
1263 : anton 1.79 (( ` : white ** nleof
1264 :     {{ start }} (( nonl ++ nleof white ** )) ++ {{ end prim prim-forth-code 2! }}
1265 : anton 1.81 )) ?? {{ process-simple }}
1266 : anton 1.79 nleof
1267 : anton 1.69 )) <- simple-primitive ( -- )
1268 :    
1269 : anton 1.71 (( {{ init-combined }}
1270 : anton 1.89 ` = white ** (( {{ start }} forth-ident {{ end add-prim }} white ** )) ++
1271 : anton 1.79 nleof {{ process-combined }}
1272 : anton 1.69 )) <- combined-primitive
1273 :    
1274 : anton 1.79 (( {{ make-prim to prim 0 to combined
1275 : anton 1.69 line @ name-line ! filename 2@ name-filename 2!
1276 : anton 1.82 function-number @ prim prim-num !
1277 : anton 1.110 start }} [ifdef] vmgen c-ident [else] forth-ident [then] {{ end
1278 :     2dup prim prim-name 2! prim prim-c-name 2! }} white **
1279 : anton 1.104 (( ` / white ** {{ start }} c-ident {{ end prim prim-c-name 2! }} white ** )) ??
1280 : anton 1.82 (( simple-primitive || combined-primitive )) {{ 1 function-number +! }}
1281 : anton 1.67 )) <- primitive ( -- )
1282 :    
1283 :     (( (( comment || primitive || nl white ** )) ** eof ))
1284 :     parser primitives2something
1285 :     warnings @ [IF]
1286 :     .( parser generated ok ) cr
1287 :     [THEN]
1288 :    
1289 : jwilke 1.95
1290 : jwilke 1.97 \ run with gforth-0.5.0 (slurp-file is missing)
1291 : jwilke 1.95 [IFUNDEF] slurp-file
1292 :     : slurp-file ( c-addr1 u1 -- c-addr2 u2 )
1293 :     \ c-addr1 u1 is the filename, c-addr2 u2 is the file's contents
1294 :     r/o bin open-file throw >r
1295 :     r@ file-size throw abort" file too large"
1296 :     dup allocate throw swap
1297 :     2dup r@ read-file throw over <> abort" could not read whole file"
1298 :     r> close-file throw ;
1299 :     [THEN]
1300 :    
1301 : anton 1.69 : primfilter ( addr u -- )
1302 :     \ process the string at addr u
1303 :     over dup rawinput ! dup line-start ! cookedinput !
1304 :     + endrawinput !
1305 :     checksyncline
1306 :     primitives2something ;
1307 : pazsan 1.8
1308 : anton 1.72 : process-file ( addr u xt-simple x-combined -- )
1309 :     output-combined ! output !
1310 : anton 1.61 save-mem 2dup filename 2!
1311 : anton 1.69 slurp-file
1312 : anton 1.17 warnings @ if
1313 :     ." ------------ CUT HERE -------------" cr endif
1314 : anton 1.69 primfilter ;
1315 : pazsan 1.30
1316 : anton 1.72 \ : process ( xt -- )
1317 :     \ bl word count rot
1318 :     \ process-file ;

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help