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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help