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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help