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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help