[gforth] / gforth / prims2x.fs  

gforth: gforth/prims2x.fs


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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help