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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help