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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help