[gforth] / gforth / prims2x.fs  

gforth: gforth/prims2x.fs


1 : anton 1.16 \ converts primitives to, e.g., C code
2 :    
3 : anton 1.175 \ Copyright (C) 1995,1996,1997,1998,2000,2003,2004,2005,2006,2007,2009,2010,2011 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 : anton 1.169 \ as published by the Free Software Foundation, either version 3
10 : anton 1.16 \ 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 : anton 1.169 \ along with this program. If not, see http://www.gnu.org/licenses/.
19 : anton 1.16
20 :    
21 : anton 1.71 \ This is not very nice (hard limits, no checking, assumes 1 chars = 1).
22 :     \ And it grew even worse when it aged.
23 : anton 1.1
24 :     \ Optimizations:
25 :     \ superfluous stores are removed. GCC removes the superfluous loads by itself
26 :     \ TOS and FTOS can be kept in register( variable)s.
27 :     \
28 :     \ Problems:
29 :     \ The TOS optimization is somewhat hairy. The problems by example:
30 :     \ 1) dup ( w -- w w ): w=TOS; sp-=1; sp[1]=w; TOS=w;
31 :     \ The store is not superfluous although the earlier opt. would think so
32 :     \ Alternatively: sp[0]=TOS; w=TOS; sp-=1; TOS=w;
33 :     \ 2) ( -- .. ): sp[0] = TOS; ... /* This additional store is necessary */
34 :     \ 3) ( .. -- ): ... TOS = sp[0]; /* as well as this load */
35 :     \ 4) ( -- ): /* but here they are unnecessary */
36 :     \ 5) Words that call NEXT themselves have to be done very carefully.
37 :     \
38 :     \ To do:
39 : pazsan 1.8 \ add the store optimization for doubles
40 : anton 1.1 \ regarding problem 1 above: It would be better (for over) to implement
41 :     \ the alternative
42 : anton 1.80 \ store optimization for combined instructions.
43 :    
44 :     \ Design Uglyness:
45 :    
46 :     \ - global state (values, variables) in connection with combined instructions.
47 :    
48 :     \ - index computation is different for instruction-stream and the
49 :     \ stacks; there are two mechanisms for dealing with that
50 :     \ (stack-in-index-xt and a test for stack==instruction-stream); there
51 :     \ should be only one.
52 : anton 1.1
53 : anton 1.159
54 : jwilke 1.137 \ for backwards compatibility, jaw
55 :     require compat/strcomp.fs
56 :    
57 : anton 1.166 [undefined] outfile-execute [if]
58 :     : outfile-execute ( ... xt file-id -- ... )
59 :     \ unsafe replacement
60 :     outfile-id >r to outfile-id execute r> to outfile-id ;
61 :     [then]
62 :    
63 : pazsan 1.3 warnings off
64 :    
65 : anton 1.136 \ redefinitions of kernel words not present in gforth-0.6.1
66 :     : latestxt lastcfa @ ;
67 :     : latest last @ ;
68 :    
69 : jwilke 1.97 [IFUNDEF] try
70 :     include startup.fs
71 :     [THEN]
72 :    
73 : anton 1.49 : struct% struct ; \ struct is redefined in gray
74 :    
75 : pazsan 1.98 warnings off
76 : anton 1.110 \ warnings on
77 : pazsan 1.98
78 : jwilke 1.39 include ./gray.fs
79 : anton 1.133 128 constant max-effect \ number of things on one side of a stack effect
80 : anton 1.71 4 constant max-stacks \ the max. number of stacks (including inst-stream).
81 : anton 1.1 255 constant maxchar
82 :     maxchar 1+ constant eof-char
83 : anton 1.17 #tab constant tab-char
84 :     #lf constant nl-char
85 : anton 1.1
86 : anton 1.18 variable rawinput \ pointer to next character to be scanned
87 :     variable endrawinput \ pointer to the end of the input (the char after the last)
88 :     variable cookedinput \ pointer to the next char to be parsed
89 : anton 1.17 variable line \ line number of char pointed to by input
90 : anton 1.65 variable line-start \ pointer to start of current line (for error messages)
91 :     0 line !
92 : anton 1.17 2variable filename \ filename of original input file
93 :     0 0 filename 2!
94 : anton 1.111 2variable out-filename \ filename of the output file (for sync lines)
95 :     0 0 out-filename 2!
96 : pazsan 1.25 2variable f-comment
97 :     0 0 f-comment 2!
98 : anton 1.17 variable skipsynclines \ are sync lines ("#line ...") invisible to the parser?
99 : anton 1.111 skipsynclines on
100 :     variable out-nls \ newlines in output (for output sync lines)
101 :     0 out-nls !
102 : anton 1.112 variable store-optimization \ use store optimization?
103 :     store-optimization off
104 :    
105 : anton 1.116 variable include-skipped-insts
106 :     \ does the threaded code for a combined instruction include the cells
107 :     \ for the component instructions (true) or only the cells for the
108 :     \ inline arguments (false)
109 :     include-skipped-insts off
110 : anton 1.1
111 : anton 1.159 2variable threaded-code-pointer-type \ type used for geninst etc.
112 :     s" Inst **" threaded-code-pointer-type 2!
113 :    
114 : anton 1.121 variable immarg \ values for immediate arguments (to be used in IMM_ARG macros)
115 :     $12340000 immarg !
116 :    
117 : anton 1.72 : th ( addr1 n -- addr2 )
118 :     cells + ;
119 :    
120 :     : holds ( addr u -- )
121 :     \ like HOLD, but for a string
122 :     tuck + swap 0 +do
123 :     1- dup c@ hold
124 :     loop
125 :     drop ;
126 : anton 1.71
127 : anton 1.82 : insert-wordlist { c-addr u wordlist xt -- }
128 : anton 1.81 \ adds name "addr u" to wordlist using defining word xt
129 :     \ xt may cause additional stack effects
130 :     get-current >r wordlist set-current
131 :     c-addr u nextname xt execute
132 :     r> set-current ;
133 :    
134 : anton 1.1 : start ( -- addr )
135 : anton 1.18 cookedinput @ ;
136 : anton 1.1
137 :     : end ( addr -- addr u )
138 : anton 1.18 cookedinput @ over - ;
139 : anton 1.1
140 : anton 1.71 : print-error-line ( -- )
141 :     \ print the current line and position
142 :     line-start @ endrawinput @ over - 2dup nl-char scan drop nip ( start end )
143 :     over - type cr
144 :     line-start @ rawinput @ over - typewhite ." ^" cr ;
145 :    
146 : anton 1.165 : print-error { addr u -- }
147 :     filename 2@ type ." :" line @ 0 .r ." : " addr u type cr
148 :     print-error-line ;
149 :    
150 : anton 1.71 : ?print-error { f addr u -- }
151 :     f ?not? if
152 : anton 1.165 addr u ['] print-error stderr outfile-execute
153 : anton 1.111 1 (bye) \ abort
154 : anton 1.71 endif ;
155 :    
156 : anton 1.63 : quote ( -- )
157 :     [char] " emit ;
158 :    
159 : anton 1.111 \ count output lines to generate sync lines for output
160 :    
161 :     : count-nls ( addr u -- )
162 :     bounds u+do
163 :     i c@ nl-char = negate out-nls +!
164 :     loop ;
165 :    
166 :     :noname ( addr u -- )
167 :     2dup count-nls
168 :     defers type ;
169 :     is type
170 :    
171 : anton 1.72 variable output \ xt ( -- ) of output word for simple primitives
172 :     variable output-combined \ xt ( -- ) of output word for combined primitives
173 : anton 1.1
174 : anton 1.49 struct%
175 : anton 1.71 cell% field stack-number \ the number of this stack
176 : anton 1.49 cell% 2* field stack-pointer \ stackpointer name
177 : anton 1.74 cell% field stack-type \ name for default type of stack items
178 : anton 1.53 cell% field stack-in-index-xt \ ( in-size item -- in-index )
179 : anton 1.126 cell% field stack-access-transform \ ( nitem -- index )
180 : anton 1.49 end-struct stack%
181 :    
182 : anton 1.53 struct%
183 :     cell% 2* field item-name \ name, excluding stack prefixes
184 :     cell% field item-stack \ descriptor for the stack used, 0 is default
185 :     cell% field item-type \ descriptor for the item type
186 :     cell% field item-offset \ offset in stack items, 0 for the deepest element
187 : anton 1.66 cell% field item-first \ true if this is the first occurence of the item
188 : anton 1.53 end-struct item%
189 :    
190 :     struct%
191 :     cell% 2* field type-c-name
192 :     cell% field type-stack \ default stack
193 :     cell% field type-size \ size of type in stack items
194 :     cell% field type-fetch \ xt of fetch code generator ( item -- )
195 :     cell% field type-store \ xt of store code generator ( item -- )
196 :     end-struct type%
197 :    
198 : anton 1.144 struct%
199 :     cell% field register-number
200 :     cell% field register-type \ pointer to type
201 :     cell% 2* field register-name \ c name
202 :     end-struct register%
203 :    
204 :     struct%
205 :     cell% 2* field ss-registers \ addr u; ss-registers[0] is TOS
206 :     \ 0 means: use memory
207 :     cell% field ss-offset \ stack pointer offset: sp[-offset] is TOS
208 :     end-struct ss% \ stack-state
209 :    
210 :     struct%
211 : anton 1.161 cell% field state-enabled
212 : anton 1.144 cell% field state-number
213 :     cell% max-stacks * field state-sss
214 :     end-struct state%
215 :    
216 : anton 1.72 variable next-stack-number 0 next-stack-number !
217 :     create stacks max-stacks cells allot \ array of stacks
218 : anton 1.144 256 constant max-registers
219 :     create registers max-registers cells allot \ array of registers
220 :     variable nregisters 0 nregisters ! \ number of registers
221 :     variable next-state-number 0 next-state-number ! \ next state number
222 : anton 1.72
223 : anton 1.53 : stack-in-index ( in-size item -- in-index )
224 :     item-offset @ - 1- ;
225 :    
226 :     : inst-in-index ( in-size item -- in-index )
227 :     nip dup item-offset @ swap item-type @ type-size @ + 1- ;
228 :    
229 : anton 1.92 : make-stack ( addr-ptr u1 type "stack-name" -- )
230 :     next-stack-number @ max-stacks < s" too many stacks" ?print-error
231 : anton 1.49 create stack% %allot >r
232 : anton 1.72 r@ stacks next-stack-number @ th !
233 : anton 1.92 next-stack-number @ r@ stack-number !
234 :     1 next-stack-number +!
235 : anton 1.74 r@ stack-type !
236 : anton 1.53 save-mem r@ stack-pointer 2!
237 : anton 1.126 ['] stack-in-index r@ stack-in-index-xt !
238 :     ['] noop r@ stack-access-transform !
239 :     rdrop ;
240 : anton 1.49
241 : anton 1.92 : map-stacks { xt -- }
242 : anton 1.157 \ perform xt ( stack -- ) for all stacks
243 : anton 1.118 next-stack-number @ 0 +do
244 :     stacks i th @ xt execute
245 :     loop ;
246 :    
247 :     : map-stacks1 { xt -- }
248 : anton 1.157 \ perform xt ( stack -- ) for all stacks except inst-stream
249 : anton 1.92 next-stack-number @ 1 +do
250 :     stacks i th @ xt execute
251 :     loop ;
252 :    
253 : anton 1.49 \ stack items
254 :    
255 :     : init-item ( addr u addr1 -- )
256 :     \ initialize item at addr1 with name addr u
257 : anton 1.156 \ the stack prefix is removed by the stack-prefix
258 : anton 1.49 dup item% %size erase
259 :     item-name 2! ;
260 :    
261 : anton 1.64 : map-items { addr end xt -- }
262 :     \ perform xt for all items in array addr...end
263 :     end addr ?do
264 :     i xt execute
265 :     item% %size +loop ;
266 :    
267 : anton 1.77 \ types
268 :    
269 :     : print-type-prefix ( type -- )
270 :     body> >head name>string type ;
271 :    
272 : anton 1.49 \ various variables for storing stuff of one primitive
273 : anton 1.1
274 : anton 1.69 struct%
275 :     cell% 2* field prim-name
276 :     cell% 2* field prim-wordset
277 :     cell% 2* field prim-c-name
278 : anton 1.146 cell% 2* field prim-c-name-orig \ for reprocessed prims, the original name
279 : anton 1.69 cell% 2* field prim-doc
280 :     cell% 2* field prim-c-code
281 :     cell% 2* field prim-forth-code
282 :     cell% 2* field prim-stack-string
283 : anton 1.82 cell% field prim-num \ ordinal number
284 : anton 1.75 cell% field prim-items-wordlist \ unique items
285 : anton 1.69 item% max-effect * field prim-effect-in
286 :     item% max-effect * field prim-effect-out
287 :     cell% field prim-effect-in-end
288 :     cell% field prim-effect-out-end
289 : anton 1.71 cell% max-stacks * field prim-stacks-in \ number of in items per stack
290 :     cell% max-stacks * field prim-stacks-out \ number of out items per stack
291 : anton 1.156 cell% max-stacks * field prim-stacks-sync \ sync flag per stack
292 : anton 1.69 end-struct prim%
293 :    
294 : anton 1.70 : make-prim ( -- prim )
295 :     prim% %alloc { p }
296 :     s" " p prim-doc 2! s" " p prim-forth-code 2! s" " p prim-wordset 2!
297 :     p ;
298 :    
299 : anton 1.79 0 value prim \ in combined prims either combined or a part
300 :     0 value combined \ in combined prims the combined prim
301 :     variable in-part \ true if processing a part
302 :     in-part off
303 : anton 1.144 0 value state-in \ state on entering prim
304 :     0 value state-out \ state on exiting prim
305 :     0 value state-default \ canonical state at bb boundaries
306 : anton 1.79
307 : anton 1.118 : prim-context ( ... p xt -- ... )
308 :     \ execute xt with prim set to p
309 :     prim >r
310 :     swap to prim
311 :     catch
312 :     r> to prim
313 :     throw ;
314 :    
315 : anton 1.146 : prim-c-name-2! ( c-addr u -- )
316 :     2dup prim prim-c-name 2! prim prim-c-name-orig 2! ;
317 :    
318 : anton 1.79 1000 constant max-combined
319 :     create combined-prims max-combined cells allot
320 :     variable num-combined
321 : anton 1.118 variable part-num \ current part number during process-combined
322 : anton 1.79
323 : anton 1.114 : map-combined { xt -- }
324 :     \ perform xt for all components of the current combined instruction
325 :     num-combined @ 0 +do
326 :     combined-prims i th @ xt execute
327 :     loop ;
328 :    
329 : anton 1.81 table constant combinations
330 :     \ the keys are the sequences of pointers to primitives
331 :    
332 : anton 1.79 create current-depth max-stacks cells allot
333 :     create max-depth max-stacks cells allot
334 :     create min-depth max-stacks cells allot
335 : anton 1.69
336 : anton 1.118 create sp-update-in max-stacks cells allot
337 :     \ where max-depth occured the first time
338 :     create max-depths max-stacks max-combined 1+ * cells allot
339 : anton 1.119 \ maximum depth at start of each part: array[parts] of array[stack]
340 :     create max-back-depths max-stacks max-combined 1+ * cells allot
341 :     \ maximun depth from end of the combination to the start of the each part
342 : anton 1.118
343 :     : s-c-max-depth ( nstack ncomponent -- addr )
344 :     max-stacks * + cells max-depths + ;
345 :    
346 : anton 1.119 : s-c-max-back-depth ( nstack ncomponent -- addr )
347 :     max-stacks * + cells max-back-depths + ;
348 :    
349 : anton 1.71 wordlist constant primitives
350 :    
351 :     : create-prim ( prim -- )
352 : anton 1.82 dup prim-name 2@ primitives ['] constant insert-wordlist ;
353 : anton 1.71
354 :     : stack-in ( stack -- addr )
355 :     \ address of number of stack items in effect in
356 :     stack-number @ cells prim prim-stacks-in + ;
357 :    
358 :     : stack-out ( stack -- addr )
359 :     \ address of number of stack items in effect out
360 :     stack-number @ cells prim prim-stacks-out + ;
361 :    
362 : anton 1.158 : stack-prim-stacks-sync ( stack -- addr )
363 :     prim prim-stacks-sync swap stack-number @ th ;
364 :    
365 : anton 1.69 \ global vars
366 : anton 1.17 variable c-line
367 :     2variable c-filename
368 :     variable name-line
369 :     2variable name-filename
370 :     2variable last-name-filename
371 : pazsan 1.30 Variable function-number 0 function-number !
372 : pazsan 1.140 Variable function-old 0 function-old !
373 : anton 1.167 : function-diff ( -- )
374 : pazsan 1.140 ." GROUPADD(" function-number @ function-old @ - 0 .r ." )" cr
375 :     function-number @ function-old ! ;
376 :     : forth-fdiff ( -- )
377 :     function-number @ function-old @ - 0 .r ." groupadd" cr
378 :     function-number @ function-old ! ;
379 : anton 1.1
380 :     \ a few more set ops
381 :    
382 :     : bit-equivalent ( w1 w2 -- w3 )
383 :     xor invert ;
384 :    
385 :     : complement ( set1 -- set2 )
386 :     empty ['] bit-equivalent binary-set-operation ;
387 :    
388 : anton 1.121 \ forward declaration for inst-stream (breaks cycle in definitions)
389 :     defer inst-stream-f ( -- stack )
390 :    
391 : anton 1.80 \ stack access stuff
392 : anton 1.79
393 : anton 1.126 : normal-stack-access0 { n stack -- }
394 : anton 1.144 \ n has the ss-offset already applied (see ...-access1)
395 : anton 1.126 n stack stack-access-transform @ execute ." [" 0 .r ." ]" ;
396 : anton 1.144
397 :     : state-ss { stack state -- ss }
398 :     state state-sss stack stack-number @ th @ ;
399 :    
400 :     : stack-reg { n stack state -- reg }
401 :     \ n is the index (TOS=0); reg is 0 if the access is to memory
402 :     stack state state-ss ss-registers 2@ n u> if ( addr ) \ in ss-registers?
403 :     n th @
404 : anton 1.49 else
405 : anton 1.144 drop 0
406 : anton 1.49 endif ;
407 : anton 1.1
408 : anton 1.144 : .reg ( reg -- )
409 :     register-name 2@ type ;
410 :    
411 :     : stack-offset ( stack state -- n )
412 :     \ offset for stack in state
413 :     state-ss ss-offset @ ;
414 :    
415 :     : normal-stack-access1 { n stack state -- }
416 :     n stack state stack-reg ?dup-if
417 :     .reg exit
418 :     endif
419 :     stack stack-pointer 2@ type
420 :     n stack state stack-offset - stack normal-stack-access0 ;
421 :    
422 :     : normal-stack-access ( n stack state -- )
423 :     over inst-stream-f = if
424 : anton 1.121 ." IMM_ARG(" normal-stack-access1 ." ," immarg ? ." )"
425 :     1 immarg +!
426 :     else
427 :     normal-stack-access1
428 :     endif ;
429 : anton 1.80
430 : anton 1.118 : stack-depth { stack -- n }
431 :     current-depth stack stack-number @ th @ ;
432 :    
433 : anton 1.79 : part-stack-access { n stack -- }
434 : anton 1.80 \ print _<stack><x>, x=inst-stream? n : maxdepth-currentdepth-n-1
435 : anton 1.79 ." _" stack stack-pointer 2@ type
436 :     stack stack-number @ { stack# }
437 : anton 1.118 stack stack-depth n + { access-depth }
438 : anton 1.80 stack inst-stream-f = if
439 :     access-depth
440 :     else
441 :     combined prim-stacks-in stack# th @
442 :     assert( dup max-depth stack# th @ = )
443 :     access-depth - 1-
444 :     endif
445 : anton 1.79 0 .r ;
446 :    
447 : anton 1.118 : part-stack-read { n stack -- }
448 :     stack stack-depth n + ( ndepth )
449 :     stack stack-number @ part-num @ s-c-max-depth @
450 :     \ max-depth stack stack-number @ th @ ( ndepth nmaxdepth )
451 :     over <= if ( ndepth ) \ load from memory
452 : anton 1.144 stack state-in normal-stack-access
453 : anton 1.118 else
454 :     drop n stack part-stack-access
455 :     endif ;
456 :    
457 : anton 1.119 : stack-diff ( stack -- n )
458 :     \ in-out
459 :     dup stack-in @ swap stack-out @ - ;
460 :    
461 :     : part-stack-write { n stack -- }
462 :     stack stack-depth n +
463 :     stack stack-number @ part-num @ s-c-max-back-depth @
464 :     over <= if ( ndepth )
465 :     stack combined ['] stack-diff prim-context -
466 : anton 1.144 stack state-out normal-stack-access
467 : anton 1.119 else
468 :     drop n stack part-stack-access
469 :     endif ;
470 : anton 1.118
471 :     : stack-read ( n stack -- )
472 :     \ print a stack access at index n of stack
473 :     in-part @ if
474 :     part-stack-read
475 :     else
476 : anton 1.144 state-in normal-stack-access
477 : anton 1.118 endif ;
478 :    
479 :     : stack-write ( n stack -- )
480 : anton 1.79 \ print a stack access at index n of stack
481 :     in-part @ if
482 : anton 1.118 part-stack-write
483 : anton 1.79 else
484 : anton 1.144 state-out normal-stack-access
485 : anton 1.79 endif ;
486 :    
487 : anton 1.53 : item-in-index { item -- n }
488 : anton 1.49 \ n is the index of item (in the in-effect)
489 : anton 1.53 item item-stack @ dup >r stack-in @ ( in-size r:stack )
490 :     item r> stack-in-index-xt @ execute ;
491 : anton 1.1
492 : anton 1.78 : item-stack-type-name ( item -- addr u )
493 :     item-stack @ stack-type @ type-c-name 2@ ;
494 :    
495 : anton 1.1 : fetch-single ( item -- )
496 : anton 1.106 \ fetch a single stack item from its stack
497 :     >r
498 :     ." vm_" r@ item-stack-type-name type
499 :     ." 2" r@ item-type @ print-type-prefix ." ("
500 : anton 1.118 r@ item-in-index r@ item-stack @ stack-read ." ,"
501 : anton 1.106 r@ item-name 2@ type
502 :     ." );" cr
503 :     rdrop ;
504 : anton 1.1
505 :     : fetch-double ( item -- )
506 : anton 1.106 \ fetch a double stack item from its stack
507 :     >r
508 :     ." vm_two"
509 :     r@ item-stack-type-name type ." 2"
510 :     r@ item-type @ print-type-prefix ." ("
511 : anton 1.162 r@ item-in-index r@ item-stack @ 2dup stack-read
512 :     ." , " -1 under+ stack-read
513 : anton 1.106 ." , " r@ item-name 2@ type
514 :     ." )" cr
515 :     rdrop ;
516 : anton 1.1
517 : anton 1.49 : same-as-in? ( item -- f )
518 : anton 1.158 \ f is true iff the offset and stack of item is the same as on input
519 :     >r
520 :     r@ item-stack @ stack-prim-stacks-sync @ if
521 :     rdrop false exit
522 :     endif
523 :     r@ item-first @ if
524 :     rdrop false exit
525 :     endif
526 :     r@ item-name 2@ prim prim-items-wordlist @ search-wordlist 0= abort" bug"
527 :     execute @
528 :     dup r@ =
529 :     if \ item first appeared in output
530 :     drop false
531 :     else
532 :     dup item-stack @ r@ item-stack @ =
533 :     swap item-offset @ r@ item-offset @ = and
534 :     endif
535 :     rdrop ;
536 : anton 1.1
537 : anton 1.49 : item-out-index ( item -- n )
538 : anton 1.144 \ n is the index of item (in the out-effect)
539 : anton 1.49 >r r@ item-stack @ stack-out @ r> item-offset @ - 1- ;
540 : pazsan 1.31
541 : anton 1.1 : really-store-single ( item -- )
542 : anton 1.106 >r
543 :     ." vm_"
544 :     r@ item-type @ print-type-prefix ." 2"
545 :     r@ item-stack-type-name type ." ("
546 :     r@ item-name 2@ type ." ,"
547 : anton 1.118 r@ item-out-index r@ item-stack @ stack-write ." );"
548 : anton 1.106 rdrop ;
549 : anton 1.1
550 : anton 1.144 : store-single { item -- }
551 :     item item-stack @ { stack }
552 :     store-optimization @ in-part @ 0= and item same-as-in? and
553 : anton 1.147 item item-in-index stack state-in stack-reg \ in reg/mem
554 :     item item-out-index stack state-out stack-reg = and \ out reg/mem
555 : anton 1.144 0= if
556 :     item really-store-single cr
557 :     endif ;
558 : anton 1.1
559 :     : store-double ( item -- )
560 :     \ !! store optimization is not performed, because it is not yet needed
561 :     >r
562 : anton 1.78 ." vm_"
563 :     r@ item-type @ print-type-prefix ." 2two"
564 :     r@ item-stack-type-name type ." ("
565 :     r@ item-name 2@ type ." , "
566 : anton 1.118 r@ item-out-index r@ item-stack @ 2dup stack-write
567 :     ." , " -1 under+ stack-write
568 : anton 1.106 ." )" cr
569 : anton 1.1 rdrop ;
570 :    
571 : anton 1.54 : single ( -- xt1 xt2 n )
572 :     ['] fetch-single ['] store-single 1 ;
573 : anton 1.1
574 : anton 1.54 : double ( -- xt1 xt2 n )
575 :     ['] fetch-double ['] store-double 2 ;
576 : anton 1.1
577 :     : s, ( addr u -- )
578 :     \ allocate a string
579 :     here swap dup allot move ;
580 :    
581 : anton 1.50 wordlist constant prefixes
582 :    
583 :     : declare ( addr "name" -- )
584 :     \ remember that there is a stack item at addr called name
585 :     create , ;
586 :    
587 :     : !default ( w addr -- )
588 :     dup @ if
589 :     2drop \ leave nonzero alone
590 :     else
591 :     !
592 :     endif ;
593 :    
594 :     : create-type { addr u xt1 xt2 n stack -- } ( "prefix" -- )
595 : anton 1.49 \ describes a type
596 :     \ addr u specifies the C type name
597 :     \ stack effect entries of the type start with prefix
598 :     create type% %allot >r
599 :     addr u save-mem r@ type-c-name 2!
600 :     xt1 r@ type-fetch !
601 :     xt2 r@ type-store !
602 :     n r@ type-size !
603 :     stack r@ type-stack !
604 :     rdrop ;
605 : anton 1.1
606 : anton 1.105 : type-prefix ( addr u xt1 xt2 n stack "prefix" -- )
607 : anton 1.94 get-current >r prefixes set-current
608 :     create-type r> set-current
609 : anton 1.50 does> ( item -- )
610 :     \ initialize item
611 :     { item typ }
612 :     typ item item-type !
613 :     typ type-stack @ item item-stack !default
614 : anton 1.75 item item-name 2@ prim prim-items-wordlist @ search-wordlist 0= if
615 : anton 1.66 item item-name 2@ nextname item declare
616 :     item item-first on
617 :     \ typ type-c-name 2@ type space type ." ;" cr
618 : anton 1.50 else
619 :     drop
620 : anton 1.66 item item-first off
621 : anton 1.50 endif ;
622 :    
623 :     : execute-prefix ( item addr1 u1 -- )
624 :     \ execute the word ( item -- ) associated with the longest prefix
625 :     \ of addr1 u1
626 :     0 swap ?do
627 :     dup i prefixes search-wordlist
628 :     if \ ok, we have the type ( item addr1 xt )
629 :     nip execute
630 :     UNLOOP EXIT
631 :     endif
632 :     -1 s+loop
633 : anton 1.158 \ we did not find a type, abort
634 :     abort
635 : anton 1.81 false s" unknown prefix" ?print-error ;
636 : anton 1.1
637 :     : declaration ( item -- )
638 : anton 1.50 dup item-name 2@ execute-prefix ;
639 : anton 1.1
640 : anton 1.64 : declaration-list ( addr1 addr2 -- )
641 :     ['] declaration map-items ;
642 :    
643 :     : declarations ( -- )
644 : anton 1.75 wordlist dup prim prim-items-wordlist ! set-current
645 : anton 1.69 prim prim-effect-in prim prim-effect-in-end @ declaration-list
646 :     prim prim-effect-out prim prim-effect-out-end @ declaration-list ;
647 : anton 1.64
648 : anton 1.66 : print-declaration { item -- }
649 :     item item-first @ if
650 :     item item-type @ type-c-name 2@ type space
651 :     item item-name 2@ type ." ;" cr
652 :     endif ;
653 :    
654 :     : print-declarations ( -- )
655 : anton 1.69 prim prim-effect-in prim prim-effect-in-end @ ['] print-declaration map-items
656 :     prim prim-effect-out prim prim-effect-out-end @ ['] print-declaration map-items ;
657 : anton 1.66
658 : anton 1.51 : stack-prefix ( stack "prefix" -- )
659 : anton 1.94 get-current >r prefixes set-current
660 : anton 1.51 name tuck nextname create ( stack length ) 2,
661 : anton 1.94 r> set-current
662 : anton 1.51 does> ( item -- )
663 :     2@ { item stack prefix-length }
664 :     item item-name 2@ prefix-length /string item item-name 2!
665 :     stack item item-stack !
666 :     item declaration ;
667 : anton 1.73
668 : anton 1.157 : set-prim-stacks-sync ( stack -- )
669 :     stack-prim-stacks-sync on ;
670 :    
671 :     : clear-prim-stacks-sync ( stack -- )
672 :     stack-prim-stacks-sync off ;
673 :    
674 :    
675 : anton 1.156 get-current prefixes set-current
676 :     : ... ( item -- )
677 :     \ this "prefix" ensures that the appropriate stack is synced with memory
678 :     dup item-name 2@ s" ..." str= 0= abort" '...' must end the item name"
679 :     item-stack @ dup if
680 : anton 1.157 set-prim-stacks-sync
681 : anton 1.156 else \ prefixless "..." syncs all stacks
682 : anton 1.158 drop ['] set-prim-stacks-sync map-stacks1
683 : anton 1.156 endif ;
684 :     set-current
685 :    
686 :     create ...-item ( -- addr ) \ just used for letting stack-prefixes work on it
687 : anton 1.167 item% %allot drop \ stores the stack temporarily until used by ...
688 : anton 1.156
689 :     : init-item1 ( addr1 addr u -- addr2 )
690 :     \ initialize item at addr1 with name addr u, next item is at addr2
691 :     \ !! make sure that any mention of "..." is only stack-prefixed
692 :     2dup s" ..." search nip nip if ( addr1 addr u )
693 :     0 ...-item item-stack ! \ initialize to prefixless
694 :     2dup ...-item item-name 2!
695 :     ...-item rot rot execute-prefix ( addr1 )
696 :     else
697 :     2 pick init-item item% %size +
698 :     endif ;
699 :    
700 : anton 1.74 \ types pointed to by stacks for use in combined prims
701 : anton 1.83 \ !! output-c-combined shouldn't use these names!
702 : anton 1.92 : stack-type-name ( addr u "name" -- )
703 :     single 0 create-type ;
704 :    
705 : anton 1.93 wordlist constant type-names \ this is here just to meet the requirement
706 :     \ that a type be a word; it is never used for lookup
707 : anton 1.83
708 : anton 1.144 : define-type ( addr u -- xt )
709 :     \ define single type with name addr u, without stack
710 :     get-current type-names set-current >r
711 :     2dup nextname stack-type-name
712 :     r> set-current
713 :     latestxt ;
714 :    
715 : anton 1.93 : stack ( "name" "stack-pointer" "type" -- )
716 :     \ define stack
717 :     name { d: stack-name }
718 :     name { d: stack-pointer }
719 :     name { d: stack-type }
720 : anton 1.144 stack-type define-type
721 :     stack-pointer rot >body stack-name nextname make-stack ;
722 : anton 1.93
723 :     stack inst-stream IP Cell
724 : anton 1.73 ' inst-in-index inst-stream stack-in-index-xt !
725 : anton 1.80 ' inst-stream <is> inst-stream-f
726 : anton 1.73 \ !! initialize stack-in and stack-out
727 : anton 1.1
728 : anton 1.144 \ registers
729 :    
730 :     : make-register ( type addr u -- )
731 :     \ define register with type TYPE and name ADDR U.
732 :     nregisters @ max-registers < s" too many registers" ?print-error
733 :     2dup nextname create register% %allot >r
734 :     r@ register-name 2!
735 :     r@ register-type !
736 :     nregisters @ r@ register-number !
737 :     1 nregisters +!
738 :     rdrop ;
739 :    
740 :     : register ( "name" "type" -- )
741 :     \ define register
742 :     name { d: reg-name }
743 :     name { d: reg-type }
744 :     reg-type define-type >body
745 :     reg-name make-register ;
746 :    
747 :     \ stack-states
748 :    
749 :     : stack-state ( a-addr u uoffset "name" -- )
750 :     create ss% %allot >r
751 :     r@ ss-offset !
752 :     r@ ss-registers 2!
753 :     rdrop ;
754 :    
755 :     0 0 0 stack-state default-ss
756 :    
757 :     \ state
758 :    
759 :     : state ( "name" -- )
760 :     \ create a state initialized with default-sss
761 :     create state% %allot { s }
762 : anton 1.161 s state-enabled on
763 : anton 1.144 next-state-number @ s state-number ! 1 next-state-number +!
764 :     max-stacks 0 ?do
765 :     default-ss s state-sss i th !
766 :     loop ;
767 :    
768 : anton 1.161 : state-disable ( state -- )
769 :     state-enabled off ;
770 :    
771 :     : state-enabled? ( state -- f )
772 :     state-enabled @ ;
773 :    
774 : anton 1.149 : .state ( state -- )
775 :     0 >body - >name .name ;
776 :    
777 : anton 1.144 : set-ss ( ss stack state -- )
778 :     state-sss swap stack-number @ th ! ;
779 :    
780 : anton 1.1 \ offset computation
781 :     \ the leftmost (i.e. deepest) item has offset 0
782 :     \ the rightmost item has the highest offset
783 :    
784 : anton 1.49 : compute-offset { item xt -- }
785 :     \ xt specifies in/out; update stack-in/out and set item-offset
786 :     item item-type @ type-size @
787 :     item item-stack @ xt execute dup @ >r +!
788 :     r> item item-offset ! ;
789 :    
790 : anton 1.64 : compute-offset-in ( addr1 addr2 -- )
791 :     ['] stack-in compute-offset ;
792 :    
793 :     : compute-offset-out ( addr1 addr2 -- )
794 :     ['] stack-out compute-offset ;
795 : anton 1.49
796 : anton 1.1 : compute-offsets ( -- )
797 : anton 1.132 prim prim-stacks-in max-stacks cells erase
798 :     prim prim-stacks-out max-stacks cells erase
799 : anton 1.69 prim prim-effect-in prim prim-effect-in-end @ ['] compute-offset-in map-items
800 :     prim prim-effect-out prim prim-effect-out-end @ ['] compute-offset-out map-items
801 : anton 1.81 inst-stream stack-out @ 0= s" # can only be on the input side" ?print-error ;
802 :    
803 : anton 1.156 : init-simple { prim -- }
804 :     \ much of the initialization is elsewhere
805 : anton 1.157 ['] clear-prim-stacks-sync map-stacks ;
806 : anton 1.156
807 : anton 1.81 : process-simple ( -- )
808 :     prim prim { W^ key } key cell
809 : anton 1.82 combinations ['] constant insert-wordlist
810 : anton 1.81 declarations compute-offsets
811 : anton 1.82 output @ execute ;
812 : anton 1.49
813 : anton 1.144 : stack-state-items ( stack state -- n )
814 :     state-ss ss-registers 2@ nip ;
815 :    
816 :     : unused-stack-items { stack -- n-in n-out }
817 :     \ n-in are the stack items in state-in not used by prim
818 :     \ n-out are the stack items in state-out not written by prim
819 :     stack state-in stack-state-items stack stack-in @ - 0 max
820 :     stack state-out stack-state-items stack stack-out @ - 0 max ;
821 :    
822 : anton 1.157 : spill-stack-items { stack -- u }
823 :     \ there are u items to spill in stack
824 :     stack unused-stack-items
825 :     stack stack-prim-stacks-sync @ if
826 :     drop 0
827 :     endif
828 :     swap - ;
829 :    
830 : anton 1.144 : spill-stack { stack -- }
831 :     \ spill regs of state-in that are not used by prim and are not in state-out
832 :     stack state-in stack-offset { offset }
833 :     stack state-in stack-state-items ( items )
834 : anton 1.157 dup stack spill-stack-items + +do
835 : anton 1.144 \ loop through the bottom items
836 :     stack stack-pointer 2@ type
837 :     i offset - stack normal-stack-access0 ." = "
838 :     i stack state-in normal-stack-access1 ." ;" cr
839 :     loop ;
840 : anton 1.1
841 : anton 1.144 : spill-state ( -- )
842 :     ['] spill-stack map-stacks1 ;
843 : anton 1.49
844 : anton 1.157 : fill-stack-items { stack -- u }
845 :     \ there are u items to fill in stack
846 :     stack unused-stack-items
847 :     stack stack-prim-stacks-sync @ if
848 :     swap drop 0 swap
849 :     endif
850 :     - ;
851 :    
852 : anton 1.144 : fill-stack { stack -- }
853 :     stack state-out stack-offset { offset }
854 :     stack state-out stack-state-items ( items )
855 : anton 1.157 dup stack fill-stack-items + +do
856 : anton 1.144 \ loop through the bottom items
857 :     i stack state-out normal-stack-access1 ." = "
858 :     stack stack-pointer 2@ type
859 :     i offset - stack normal-stack-access0 ." ;" cr
860 :     loop ;
861 : anton 1.1
862 : anton 1.144 : fill-state ( -- )
863 : anton 1.53 \ !! inst-stream for prefetching?
864 : anton 1.144 ['] fill-stack map-stacks1 ;
865 : anton 1.49
866 :     : fetch ( addr -- )
867 : anton 1.72 dup item-type @ type-fetch @ execute ;
868 : anton 1.1
869 :     : fetches ( -- )
870 : anton 1.69 prim prim-effect-in prim prim-effect-in-end @ ['] fetch map-items ;
871 : anton 1.49
872 : anton 1.144 : reg-reg-move ( reg-from reg-to -- )
873 :     2dup = if
874 :     2drop
875 :     else
876 :     .reg ." = " .reg ." ;" cr
877 :     endif ;
878 :    
879 :     : stack-bottom-reg { n stack state -- reg }
880 :     stack state stack-state-items n - 1- stack state stack-reg ;
881 :    
882 :     : stack-moves { stack -- }
883 :     \ generate moves between registers in state-in/state-out that are
884 :     \ not spilled or consumed/produced by prim.
885 :     \ !! this works only for a simple stack cache, not e.g., for
886 :     \ rotating stack caches, or registers shared between stacks (the
887 :     \ latter would also require a change in interface)
888 :     \ !! maybe place this after NEXT_P1?
889 :     stack unused-stack-items 2dup < if ( n-in n-out )
890 :     \ move registers from 0..n_in-1 to n_out-n_in..n_out-1
891 :     over - { diff } ( n-in )
892 :     -1 swap 1- -do
893 :     i stack state-in stack-bottom-reg ( reg-from )
894 :     i diff + stack state-out stack-bottom-reg reg-reg-move
895 :     1 -loop
896 :     else
897 :     \ move registers from n_in-n_out..n_in-1 to 0..n_out-1
898 :     swap over - { diff } ( n-out )
899 :     0 +do
900 :     i diff + stack state-in stack-bottom-reg ( reg-from )
901 :     i stack state-out stack-bottom-reg reg-reg-move
902 :     loop
903 :     endif ;
904 :    
905 : anton 1.126 : stack-update-transform ( n1 stack -- n2 )
906 :     \ n2 is the number by which the stack pointer should be
907 :     \ incremented to pop n1 items
908 :     stack-access-transform @ dup >r execute
909 :     0 r> execute - ;
910 :    
911 : anton 1.157 : update-stack-pointer { stack n -- }
912 :     n if \ this check is not necessary, gcc would do this for us
913 : anton 1.118 stack inst-stream = if
914 : anton 1.157 ." INC_IP(" n 0 .r ." );" cr
915 : anton 1.118 else
916 : anton 1.126 stack stack-pointer 2@ type ." += "
917 : anton 1.157 n stack stack-update-transform 0 .r ." ;" cr
918 : anton 1.118 endif
919 : anton 1.157 endif ;
920 :    
921 :     : stack-pointer-update { stack -- }
922 :     \ and moves
923 :     \ stacks grow downwards
924 : pazsan 1.174 \ ." /* stack pointer update " stack stack-pointer 2@ type ." */" cr
925 : anton 1.157 stack stack-prim-stacks-sync @ if
926 : pazsan 1.174 \ ." /* synced " stack stack-in ? stack stack-out ? stack state-in stack-offset . ." */" cr
927 : anton 1.157 stack stack-in @
928 :     stack state-in stack-offset -
929 :     stack swap update-stack-pointer
930 :     else
931 : pazsan 1.174 \ ." /* unsynced " stack stack-in ? stack stack-out ? ." */" cr
932 : anton 1.157 stack stack-diff ( in-out )
933 :     stack state-in stack-offset -
934 :     stack state-out stack-offset + ( [in-in_offset]-[out-out_offset] )
935 :     stack swap update-stack-pointer
936 :     stack stack-moves
937 :     endif ;
938 : anton 1.55
939 : anton 1.1 : stack-pointer-updates ( -- )
940 : anton 1.92 ['] stack-pointer-update map-stacks ;
941 : anton 1.1
942 : anton 1.157 : stack-pointer-update2 { stack -- }
943 : pazsan 1.174 \ ." /* stack pointer update2 " stack stack-pointer 2@ type ." */" cr
944 : anton 1.157 stack stack-prim-stacks-sync @ if
945 :     stack state-out stack-offset
946 :     stack stack-out @ -
947 :     stack swap update-stack-pointer
948 :     endif ;
949 :    
950 :     : stack-pointer-updates2 ( -- )
951 :     \ update stack pointers after C code, where necessary
952 :     ['] stack-pointer-update2 map-stacks ;
953 :    
954 : anton 1.1 : store ( item -- )
955 :     \ f is true if the item should be stored
956 :     \ f is false if the store is probably not necessary
957 : anton 1.49 dup item-type @ type-store @ execute ;
958 : anton 1.1
959 :     : stores ( -- )
960 : anton 1.69 prim prim-effect-out prim prim-effect-out-end @ ['] store map-items ;
961 : pazsan 1.8
962 : anton 1.91 : print-debug-arg { item -- }
963 :     ." fputs(" quote space item item-name 2@ type ." =" quote ." , vm_out); "
964 :     ." printarg_" item item-type @ print-type-prefix
965 :     ." (" item item-name 2@ type ." );" cr ;
966 :    
967 :     : print-debug-args ( -- )
968 :     ." #ifdef VM_DEBUG" cr
969 :     ." if (vm_debug) {" cr
970 :     prim prim-effect-in prim prim-effect-in-end @ ['] print-debug-arg map-items
971 :     \ ." fputc('\n', vm_out);" cr
972 :     ." }" cr
973 :     ." #endif" cr ;
974 :    
975 :     : print-debug-result { item -- }
976 :     item item-first @ if
977 :     item print-debug-arg
978 :     endif ;
979 :    
980 :     : print-debug-results ( -- )
981 :     cr
982 :     ." #ifdef VM_DEBUG" cr
983 :     ." if (vm_debug) {" cr
984 :     ." fputs(" quote ." -- " quote ." , vm_out); "
985 :     prim prim-effect-out prim prim-effect-out-end @ ['] print-debug-result map-items
986 :     ." fputc('\n', vm_out);" cr
987 :     ." }" cr
988 :     ." #endif" cr ;
989 :    
990 : anton 1.86 : output-super-end ( -- )
991 :     prim prim-c-code 2@ s" SET_IP" search if
992 :     ." SUPER_END;" cr
993 :     endif
994 :     2drop ;
995 :    
996 : anton 1.145
997 :     defer output-nextp0
998 :     :noname ( -- )
999 :     ." NEXT_P0;" cr ;
1000 :     is output-nextp0
1001 :    
1002 :     defer output-nextp1
1003 :     :noname ( -- )
1004 :     ." NEXT_P1;" cr ;
1005 :     is output-nextp1
1006 :    
1007 : anton 1.124 : output-nextp2 ( -- )
1008 :     ." NEXT_P2;" cr ;
1009 :    
1010 :     variable tail-nextp2 \ xt to execute for printing NEXT_P2 in INST_TAIL
1011 :     ' output-nextp2 tail-nextp2 !
1012 :    
1013 : anton 1.120 : output-label2 ( -- )
1014 : anton 1.121 ." LABEL2(" prim prim-c-name 2@ type ." )" cr
1015 : anton 1.153 ." NEXT_P1_5;" cr
1016 :     ." LABEL3(" prim prim-c-name 2@ type ." )" cr
1017 :     ." DO_GOTO;" cr ;
1018 : anton 1.120
1019 :     : output-c-tail1 { xt -- }
1020 :     \ the final part of the generated C code, with xt printing LABEL2 or not.
1021 : anton 1.86 output-super-end
1022 : anton 1.91 print-debug-results
1023 : anton 1.145 output-nextp1
1024 : anton 1.157 stack-pointer-updates2
1025 : anton 1.52 stores
1026 : anton 1.144 fill-state
1027 : anton 1.121 xt execute ;
1028 : anton 1.108
1029 : anton 1.155 : output-c-vm-jump-tail ( -- )
1030 :     \ !! this functionality not yet implemented for superinstructions
1031 :     output-super-end
1032 :     print-debug-results
1033 :     stores
1034 :     fill-state
1035 :     ." LABEL2(" prim prim-c-name 2@ type ." )" cr
1036 :     ." LABEL3(" prim prim-c-name 2@ type ." )" cr
1037 :     ." DO_GOTO;" cr ;
1038 :    
1039 : anton 1.120 : output-c-tail1-no-stores { xt -- }
1040 :     \ the final part of the generated C code for combinations
1041 :     output-super-end
1042 : anton 1.145 output-nextp1
1043 : anton 1.144 fill-state
1044 : anton 1.121 xt execute ;
1045 : anton 1.120
1046 :     : output-c-tail ( -- )
1047 : anton 1.124 tail-nextp2 @ output-c-tail1 ;
1048 : anton 1.52
1049 : anton 1.108 : output-c-tail2 ( -- )
1050 : anton 1.155 prim prim-c-code 2@ s" VM_JUMP(" search nip nip if
1051 :     output-c-vm-jump-tail
1052 :     else
1053 :     ['] output-label2 output-c-tail1
1054 :     endif ;
1055 : anton 1.120
1056 :     : output-c-tail-no-stores ( -- )
1057 : anton 1.124 tail-nextp2 @ output-c-tail1-no-stores ;
1058 : anton 1.119
1059 :     : output-c-tail2-no-stores ( -- )
1060 : anton 1.172 prim prim-c-code 2@ s" VM_JUMP(" search nip nip abort" Currently VM_JUMP is not supported in static superinstructions"
1061 : anton 1.120 ['] output-label2 output-c-tail1-no-stores ;
1062 : anton 1.108
1063 : anton 1.85 : type-c-code ( c-addr u xt -- )
1064 : anton 1.109 \ like TYPE, but replaces "INST_TAIL;" with tail code produced by xt
1065 : anton 1.85 { xt }
1066 : anton 1.111 ." {" cr
1067 :     ." #line " c-line @ . quote c-filename 2@ type quote cr
1068 : anton 1.52 begin ( c-addr1 u1 )
1069 : anton 1.109 2dup s" INST_TAIL;" search
1070 : anton 1.52 while ( c-addr1 u1 c-addr3 u3 )
1071 :     2dup 2>r drop nip over - type
1072 : anton 1.85 xt execute
1073 : anton 1.109 2r> 10 /string
1074 : anton 1.52 \ !! resync #line missing
1075 :     repeat
1076 : anton 1.111 2drop type
1077 :     ." #line " out-nls @ 2 + . quote out-filename 2@ type quote cr
1078 :     ." }" cr ;
1079 : anton 1.63
1080 : anton 1.72 : print-entry ( -- )
1081 : anton 1.109 ." LABEL(" prim prim-c-name 2@ type ." )" ;
1082 : pazsan 1.154
1083 :     : prim-type ( addr u -- )
1084 :     \ print out a primitive, but avoid "*/"
1085 :     2dup s" */" search nip nip IF
1086 :     bounds ?DO I c@ dup '* = IF drop 'x THEN emit LOOP
1087 :     ELSE type THEN ;
1088 :    
1089 : anton 1.157 : output-c ( -- )
1090 : pazsan 1.154 print-entry ." /* " prim prim-name 2@ prim-type
1091 : anton 1.149 ." ( " prim prim-stack-string 2@ type ." ) "
1092 :     state-in .state ." -- " state-out .state ." */" cr
1093 : anton 1.111 ." /* " prim prim-doc 2@ type ." */" cr
1094 :     ." NAME(" quote prim prim-name 2@ type quote ." )" cr \ debugging
1095 :     ." {" cr
1096 :     ." DEF_CA" cr
1097 :     print-declarations
1098 : anton 1.145 output-nextp0
1099 : anton 1.144 spill-state
1100 : anton 1.111 fetches
1101 :     print-debug-args
1102 :     stack-pointer-updates
1103 :     prim prim-c-code 2@ ['] output-c-tail type-c-code
1104 :     output-c-tail2
1105 :     ." }" cr
1106 :     cr
1107 : anton 1.1 ;
1108 :    
1109 : anton 1.56 : disasm-arg { item -- }
1110 :     item item-stack @ inst-stream = if
1111 : anton 1.107 ." {" cr
1112 :     item print-declaration
1113 :     item fetch
1114 :     item print-debug-arg
1115 :     ." }" cr
1116 : anton 1.56 endif ;
1117 :    
1118 :     : disasm-args ( -- )
1119 : anton 1.69 prim prim-effect-in prim prim-effect-in-end @ ['] disasm-arg map-items ;
1120 : anton 1.56
1121 :     : output-disasm ( -- )
1122 :     \ generate code for disassembling VM instructions
1123 : anton 1.106 ." if (VM_IS_INST(*ip, " function-number @ 0 .r ." )) {" cr
1124 : anton 1.69 ." fputs(" quote prim prim-name 2@ type quote ." , vm_out);" cr
1125 : anton 1.56 disasm-args
1126 :     ." ip += " inst-stream stack-in @ 1+ 0 .r ." ;" cr
1127 : anton 1.91 ." goto _endif_;" cr
1128 :     ." }" cr ;
1129 : anton 1.56
1130 : anton 1.86 : output-profile ( -- )
1131 :     \ generate code for postprocessing the VM block profile stuff
1132 : anton 1.87 ." if (VM_IS_INST(*ip, " function-number @ 0 .r ." )) {" cr
1133 : anton 1.104 ." add_inst(b, " quote prim prim-name 2@ type quote ." );" cr
1134 : anton 1.86 ." ip += " inst-stream stack-in @ 1+ 0 .r ." ;" cr
1135 :     prim prim-c-code 2@ s" SET_IP" search nip nip
1136 :     prim prim-c-code 2@ s" SUPER_END" search nip nip or if
1137 :     ." return;" cr
1138 : anton 1.91 else
1139 :     ." goto _endif_;" cr
1140 : anton 1.86 endif
1141 : anton 1.91 ." }" cr ;
1142 : anton 1.86
1143 : anton 1.114 : output-profile-part ( p )
1144 :     ." add_inst(b, " quote
1145 :     prim-name 2@ type
1146 :     quote ." );" cr ;
1147 :    
1148 : anton 1.104 : output-profile-combined ( -- )
1149 :     \ generate code for postprocessing the VM block profile stuff
1150 :     ." if (VM_IS_INST(*ip, " function-number @ 0 .r ." )) {" cr
1151 : anton 1.114 ['] output-profile-part map-combined
1152 : anton 1.104 ." ip += " inst-stream stack-in @ 1+ 0 .r ." ;" cr
1153 :     combined-prims num-combined @ 1- th @ prim-c-code 2@ s" SET_IP" search nip nip
1154 :     combined-prims num-combined @ 1- th @ prim-c-code 2@ s" SUPER_END" search nip nip or if
1155 :     ." return;" cr
1156 :     else
1157 :     ." goto _endif_;" cr
1158 :     endif
1159 :     ." }" cr ;
1160 :    
1161 : anton 1.143 : prim-branch? { prim -- f }
1162 :     \ true if prim is a branch or super-end
1163 :     prim prim-c-code 2@ s" SET_IP" search nip nip 0<> ;
1164 :    
1165 : anton 1.103 : output-superend ( -- )
1166 :     \ output flag specifying whether the current word ends a dynamic superinst
1167 : anton 1.143 prim prim-branch?
1168 :     prim prim-c-code 2@ s" SUPER_END" search nip nip 0<> or
1169 : anton 1.103 prim prim-c-code 2@ s" SUPER_CONTINUE" search nip nip 0= and
1170 : pazsan 1.154 negate 0 .r ." , /* " prim prim-name 2@ prim-type ." */" cr ;
1171 : anton 1.103
1172 : anton 1.60 : gen-arg-parm { item -- }
1173 :     item item-stack @ inst-stream = if
1174 :     ." , " item item-type @ type-c-name 2@ type space
1175 :     item item-name 2@ type
1176 :     endif ;
1177 :    
1178 :     : gen-args-parm ( -- )
1179 : anton 1.69 prim prim-effect-in prim prim-effect-in-end @ ['] gen-arg-parm map-items ;
1180 : anton 1.60
1181 :     : gen-arg-gen { item -- }
1182 :     item item-stack @ inst-stream = if
1183 :     ." genarg_" item item-type @ print-type-prefix
1184 :     ." (ctp, " item item-name 2@ type ." );" cr
1185 :     endif ;
1186 :    
1187 :     : gen-args-gen ( -- )
1188 : anton 1.69 prim prim-effect-in prim prim-effect-in-end @ ['] gen-arg-gen map-items ;
1189 : anton 1.60
1190 :     : output-gen ( -- )
1191 :     \ generate C code for generating VM instructions
1192 : anton 1.159 ." void gen_" prim prim-c-name 2@ type ." ("
1193 :     threaded-code-pointer-type 2@ type ." ctp" gen-args-parm ." )" cr
1194 : anton 1.60 ." {" cr
1195 : anton 1.159 ." gen_inst(ctp, " function-number @ 0 .r ." );" cr
1196 : anton 1.60 gen-args-gen
1197 : anton 1.68 ." }" cr ;
1198 : anton 1.60
1199 : anton 1.49 : stack-used? { stack -- f }
1200 :     stack stack-in @ stack stack-out @ or 0<> ;
1201 : jwilke 1.44
1202 : pazsan 1.30 : output-funclabel ( -- )
1203 : anton 1.69 ." &I_" prim prim-c-name 2@ type ." ," cr ;
1204 : pazsan 1.30
1205 :     : output-forthname ( -- )
1206 : anton 1.69 '" emit prim prim-name 2@ type '" emit ." ," cr ;
1207 : pazsan 1.30
1208 : anton 1.92 \ : output-c-func ( -- )
1209 :     \ \ used for word libraries
1210 :     \ ." Cell * I_" prim prim-c-name 2@ type ." (Cell *SP, Cell **FP) /* " prim prim-name 2@ type
1211 :     \ ." ( " prim prim-stack-string 2@ type ." ) */" cr
1212 :     \ ." /* " prim prim-doc 2@ type ." */" cr
1213 :     \ ." NAME(" quote prim prim-name 2@ type quote ." )" cr
1214 :     \ \ debugging
1215 :     \ ." {" cr
1216 :     \ print-declarations
1217 :     \ \ !! don't know what to do about that
1218 :     \ inst-stream stack-used? IF ." Cell *ip=IP;" cr THEN
1219 :     \ data-stack stack-used? IF ." Cell *sp=SP;" cr THEN
1220 :     \ fp-stack stack-used? IF ." Cell *fp=*FP;" cr THEN
1221 :     \ return-stack stack-used? IF ." Cell *rp=*RP;" cr THEN
1222 : anton 1.144 \ spill-state
1223 : anton 1.92 \ fetches
1224 :     \ stack-pointer-updates
1225 :     \ fp-stack stack-used? IF ." *FP=fp;" cr THEN
1226 :     \ ." {" cr
1227 :     \ ." #line " c-line @ . quote c-filename 2@ type quote cr
1228 :     \ prim prim-c-code 2@ type
1229 :     \ ." }" cr
1230 :     \ stores
1231 : anton 1.144 \ fill-state
1232 : anton 1.92 \ ." return (sp);" cr
1233 :     \ ." }" cr
1234 :     \ cr ;
1235 : pazsan 1.30
1236 : jwilke 1.43 : output-label ( -- )
1237 : anton 1.127 ." INST_ADDR(" prim prim-c-name 2@ type ." )," cr ;
1238 : anton 1.1
1239 : jwilke 1.43 : output-alias ( -- )
1240 : anton 1.69 ( primitive-number @ . ." alias " ) ." Primitive " prim prim-name 2@ type cr ;
1241 : anton 1.1
1242 : anton 1.148 defer output-c-prim-num ( -- )
1243 :    
1244 :     :noname ( -- )
1245 : anton 1.141 ." N_" prim prim-c-name 2@ type ." ," cr ;
1246 : anton 1.148 is output-c-prim-num
1247 : anton 1.114
1248 : jwilke 1.43 : output-forth ( -- )
1249 : anton 1.69 prim prim-forth-code @ 0=
1250 : pazsan 1.30 IF \ output-alias
1251 : jwilke 1.28 \ this is bad for ec: an alias is compiled if tho word does not exist!
1252 :     \ JAW
1253 : anton 1.69 ELSE ." : " prim prim-name 2@ type ." ( "
1254 :     prim prim-stack-string 2@ type ." )" cr
1255 :     prim prim-forth-code 2@ type cr
1256 : pazsan 1.30 THEN ;
1257 : anton 1.10
1258 : anton 1.17 : output-tag-file ( -- )
1259 : pazsan 1.117 name-filename 2@ last-name-filename 2@ compare if
1260 : anton 1.17 name-filename 2@ last-name-filename 2!
1261 :     #ff emit cr
1262 :     name-filename 2@ type
1263 :     ." ,0" cr
1264 :     endif ;
1265 :    
1266 :     : output-tag ( -- )
1267 :     output-tag-file
1268 : anton 1.69 prim prim-name 2@ 1+ type
1269 : anton 1.17 127 emit
1270 : anton 1.69 space prim prim-name 2@ type space
1271 : anton 1.17 1 emit
1272 :     name-line @ 0 .r
1273 :     ." ,0" cr ;
1274 :    
1275 : pazsan 1.100 : output-vi-tag ( -- )
1276 :     name-filename 2@ type #tab emit
1277 :     prim prim-name 2@ type #tab emit
1278 :     ." /^" prim prim-name 2@ type ." *(/" cr ;
1279 :    
1280 : anton 1.10 [IFDEF] documentation
1281 :     : register-doc ( -- )
1282 : anton 1.82 prim prim-name 2@ documentation ['] create insert-wordlist
1283 : anton 1.69 prim prim-name 2@ 2,
1284 :     prim prim-stack-string 2@ condition-stack-effect 2,
1285 :     prim prim-wordset 2@ 2,
1286 :     prim prim-c-name 2@ condition-pronounciation 2,
1287 : anton 1.82 prim prim-doc 2@ 2, ;
1288 : anton 1.10 [THEN]
1289 : anton 1.67
1290 :    
1291 : anton 1.69 \ combining instructions
1292 :    
1293 :     \ The input should look like this:
1294 :    
1295 :     \ lit_+ = lit +
1296 :    
1297 :     \ The output should look like this:
1298 :    
1299 :     \ I_lit_+:
1300 :     \ {
1301 :     \ DEF_CA
1302 :     \ Cell _x_ip0;
1303 :     \ Cell _x_sp0;
1304 :     \ Cell _x_sp1;
1305 :     \ NEXT_P0;
1306 :     \ _x_ip0 = (Cell) IPTOS;
1307 :     \ _x_sp0 = (Cell) spTOS;
1308 :     \ INC_IP(1);
1309 :     \ /* sp += 0; */
1310 :     \ /* lit ( #w -- w ) */
1311 :     \ /* */
1312 :     \ NAME("lit")
1313 :     \ {
1314 :     \ Cell w;
1315 :     \ w = (Cell) _x_ip0;
1316 :     \ #ifdef VM_DEBUG
1317 :     \ if (vm_debug) {
1318 :     \ fputs(" w=", vm_out); printarg_w (w);
1319 :     \ fputc('\n', vm_out);
1320 :     \ }
1321 :     \ #endif
1322 :     \ {
1323 :     \ #line 136 "./prim"
1324 :     \ }
1325 :     \ _x_sp1 = (Cell)w;
1326 :     \ }
1327 :     \ I_plus: /* + ( n1 n2 -- n ) */
1328 :     \ /* */
1329 :     \ NAME("+")
1330 :     \ {
1331 :     \ DEF_CA
1332 :     \ Cell n1;
1333 :     \ Cell n2;
1334 :     \ Cell n;
1335 :     \ NEXT_P0;
1336 :     \ n1 = (Cell) _x_sp0;
1337 :     \ n2 = (Cell) _x_sp1;
1338 :     \ #ifdef VM_DEBUG
1339 :     \ if (vm_debug) {
1340 :     \ fputs(" n1=", vm_out); printarg_n (n1);
1341 :     \ fputs(" n2=", vm_out); printarg_n (n2);
1342 :     \ fputc('\n', vm_out);
1343 :     \ }
1344 :     \ #endif
1345 :     \ {
1346 :     \ #line 516 "./prim"
1347 :     \ n = n1+n2;
1348 :     \ }
1349 :     \ _x_sp0 = (Cell)n;
1350 :     \ }
1351 :     \ NEXT_P1;
1352 :     \ spTOS = (Cell)_x_sp0;
1353 :     \ NEXT_P2;
1354 :    
1355 : anton 1.71 : init-combined ( -- )
1356 : pazsan 1.174 ['] clear-prim-stacks-sync map-stacks
1357 : anton 1.79 prim to combined
1358 : anton 1.71 0 num-combined !
1359 :     current-depth max-stacks cells erase
1360 : anton 1.116 include-skipped-insts @ current-depth 0 th !
1361 : anton 1.72 max-depth max-stacks cells erase
1362 :     min-depth max-stacks cells erase
1363 :     prim prim-effect-in prim prim-effect-in-end !
1364 :     prim prim-effect-out prim prim-effect-out-end ! ;
1365 : anton 1.71
1366 :     : max! ( n addr -- )
1367 :     tuck @ max swap ! ;
1368 :    
1369 : anton 1.72 : min! ( n addr -- )
1370 :     tuck @ min swap ! ;
1371 :    
1372 : anton 1.119 : inst-stream-adjustment ( nstack -- n )
1373 :     \ number of stack items to add for each part
1374 :     0= include-skipped-insts @ and negate ;
1375 : anton 1.116
1376 : anton 1.71 : add-depths { p -- }
1377 :     \ combine stack effect of p with *-depths
1378 :     max-stacks 0 ?do
1379 : anton 1.72 current-depth i th @
1380 : anton 1.119 p prim-stacks-in i th @ + i inst-stream-adjustment +
1381 : anton 1.72 dup max-depth i th max!
1382 :     p prim-stacks-out i th @ -
1383 :     dup min-depth i th min!
1384 :     current-depth i th !
1385 : anton 1.71 loop ;
1386 :    
1387 : anton 1.118 : copy-maxdepths ( n -- )
1388 :     max-depth max-depths rot max-stacks * th max-stacks cells move ;
1389 :    
1390 : anton 1.71 : add-prim ( addr u -- )
1391 :     \ add primitive given by "addr u" to combined-prims
1392 :     primitives search-wordlist s" unknown primitive" ?print-error
1393 :     execute { p }
1394 : anton 1.72 p combined-prims num-combined @ th !
1395 : anton 1.118 num-combined @ copy-maxdepths
1396 : anton 1.71 1 num-combined +!
1397 : anton 1.118 p add-depths
1398 :     num-combined @ copy-maxdepths ;
1399 : anton 1.71
1400 :     : compute-effects { q -- }
1401 :     \ compute the stack effects of q from the depths
1402 :     max-stacks 0 ?do
1403 : anton 1.72 max-depth i th @ dup
1404 :     q prim-stacks-in i th !
1405 :     current-depth i th @ -
1406 :     q prim-stacks-out i th !
1407 :     loop ;
1408 :    
1409 :     : make-effect-items { stack# items effect-endp -- }
1410 :     \ effect-endp points to a pointer to the end of the current item-array
1411 :     \ and has to be updated
1412 :     stacks stack# th @ { stack }
1413 :     items 0 +do
1414 :     effect-endp @ { item }
1415 :     i 0 <# #s stack stack-pointer 2@ holds [char] _ hold #> save-mem
1416 :     item item-name 2!
1417 :     stack item item-stack !
1418 : anton 1.74 stack stack-type @ item item-type !
1419 : anton 1.72 i item item-offset !
1420 :     item item-first on
1421 :     item% %size effect-endp +!
1422 :     loop ;
1423 :    
1424 :     : init-effects { q -- }
1425 :     \ initialize effects field for FETCHES and STORES
1426 :     max-stacks 0 ?do
1427 :     i q prim-stacks-in i th @ q prim-effect-in-end make-effect-items
1428 :     i q prim-stacks-out i th @ q prim-effect-out-end make-effect-items
1429 : anton 1.71 loop ;
1430 :    
1431 : anton 1.119 : compute-stack-max-back-depths ( stack -- )
1432 :     stack-number @ { stack# }
1433 :     current-depth stack# th @ dup
1434 :     dup stack# num-combined @ s-c-max-back-depth !
1435 :     -1 num-combined @ 1- -do ( max-depth current-depth )
1436 :     combined-prims i th @ { p }
1437 :     p prim-stacks-out stack# th @ +
1438 :     dup >r max r>
1439 :     over stack# i s-c-max-back-depth !
1440 :     p prim-stacks-in stack# th @ -
1441 :     stack# inst-stream-adjustment -
1442 :     1 -loop
1443 :     assert( dup stack# inst-stream-adjustment negate = )
1444 :     assert( over max-depth stack# th @ = )
1445 :     2drop ;
1446 :    
1447 :     : compute-max-back-depths ( -- )
1448 :     \ compute max-back-depths.
1449 :     \ assumes that current-depths is correct for the end of the combination
1450 :     ['] compute-stack-max-back-depths map-stacks ;
1451 :    
1452 : anton 1.71 : process-combined ( -- )
1453 : anton 1.81 combined combined-prims num-combined @ cells
1454 : anton 1.82 combinations ['] constant insert-wordlist
1455 : anton 1.86 combined-prims num-combined @ 1- th ( last-part )
1456 :     @ prim-c-code 2@ prim prim-c-code 2! \ used by output-super-end
1457 : anton 1.72 prim compute-effects
1458 :     prim init-effects
1459 : anton 1.119 compute-max-back-depths
1460 : anton 1.72 output-combined perform ;
1461 :    
1462 : anton 1.144 \ reprocessing (typically to generate versions for another cache states)
1463 :     \ !! use prim-context
1464 :    
1465 :     variable reprocessed-num 0 reprocessed-num !
1466 :    
1467 :     : new-name ( -- c-addr u )
1468 :     reprocessed-num @ 0
1469 :     1 reprocessed-num +!
1470 :     <# #s 'p hold '_ hold #> save-mem ;
1471 :    
1472 :     : reprocess-simple ( prim -- )
1473 :     to prim
1474 :     new-name prim prim-c-name 2!
1475 :     output @ execute ;
1476 :    
1477 :     : lookup-prim ( c-addr u -- prim )
1478 :     primitives search-wordlist 0= -13 and throw execute ;
1479 :    
1480 :     : state-prim1 { in-state out-state prim -- }
1481 :     in-state out-state state-default dup d= ?EXIT
1482 : anton 1.161 in-state state-enabled? out-state state-enabled? and 0= ?EXIT
1483 : anton 1.144 in-state to state-in
1484 :     out-state to state-out
1485 :     prim reprocess-simple ;
1486 :    
1487 :     : state-prim ( in-state out-state "name" -- )
1488 :     parse-word lookup-prim state-prim1 ;
1489 :    
1490 :     \ reprocessing with default states
1491 :    
1492 :     \ This is a simple scheme and should be generalized
1493 :     \ assumes we only cache one stack and use simple states for that
1494 :    
1495 :     0 value cache-stack \ stack that we cache
1496 :     2variable cache-states \ states of the cache, starting with the empty state
1497 :    
1498 :     : compute-default-state-out ( n-in -- n-out )
1499 :     \ for the current prim
1500 :     cache-stack stack-in @ - 0 max
1501 : anton 1.157 cache-stack stack-prim-stacks-sync @ if
1502 :     drop 0
1503 :     endif
1504 : anton 1.144 cache-stack stack-out @ + cache-states 2@ nip 1- min ;
1505 :    
1506 :     : gen-prim-states ( prim -- )
1507 :     to prim
1508 :     cache-states 2@ swap { states } ( nstates )
1509 :     cache-stack stack-in @ +do
1510 :     states i th @
1511 :     states i compute-default-state-out th @
1512 :     prim state-prim1
1513 :     loop ;
1514 :    
1515 :     : prim-states ( "name" -- )
1516 :     parse-word lookup-prim gen-prim-states ;
1517 :    
1518 :     : gen-branch-states ( prim -- )
1519 :     \ generate versions that produce state-default; useful for branches
1520 :     to prim
1521 :     cache-states 2@ swap { states } ( nstates )
1522 :     cache-stack stack-in @ +do
1523 :     states i th @ state-default prim state-prim1
1524 :     loop ;
1525 :    
1526 :     : branch-states ( out-state "name" -- )
1527 :     parse-word lookup-prim gen-branch-states ;
1528 :    
1529 :     \ producing state transitions
1530 :    
1531 :     : gen-transitions ( "name" -- )
1532 :     parse-word lookup-prim { prim }
1533 :     cache-states 2@ { states nstates }
1534 :     nstates 0 +do
1535 :     nstates 0 +do
1536 :     i j <> if
1537 :     states i th @ states j th @ prim state-prim1
1538 :     endif
1539 :     loop
1540 :     loop ;
1541 :    
1542 : anton 1.72 \ C output
1543 :    
1544 :     : print-item { n stack -- }
1545 :     \ print nth stack item name
1546 : anton 1.79 stack stack-type @ type-c-name 2@ type space
1547 : anton 1.142 ." MAYBE_UNUSED _" stack stack-pointer 2@ type n 0 .r ;
1548 : anton 1.72
1549 :     : print-declarations-combined ( -- )
1550 :     max-stacks 0 ?do
1551 :     max-depth i th @ min-depth i th @ - 0 +do
1552 :     i stacks j th @ print-item ." ;" cr
1553 :     loop
1554 :     loop ;
1555 : anton 1.79
1556 :     : part-fetches ( -- )
1557 :     fetches ;
1558 :    
1559 :     : part-output-c-tail ( -- )
1560 : anton 1.91 print-debug-results
1561 : anton 1.85 stores ;
1562 :    
1563 :     : output-combined-tail ( -- )
1564 : anton 1.170 in-part @ >r in-part off
1565 : anton 1.85 part-output-c-tail
1566 : anton 1.119 combined ['] output-c-tail-no-stores prim-context
1567 : anton 1.118 r> in-part ! ;
1568 :    
1569 :     : part-stack-pointer-updates ( -- )
1570 : anton 1.123 next-stack-number @ 0 +do
1571 : anton 1.118 i part-num @ 1+ s-c-max-depth @ dup
1572 :     i num-combined @ s-c-max-depth @ = \ final depth
1573 :     swap i part-num @ s-c-max-depth @ <> \ just reached now
1574 :     part-num @ 0= \ first part
1575 :     or and if
1576 :     stacks i th @ stack-pointer-update
1577 :     endif
1578 :     loop ;
1579 : anton 1.79
1580 :     : output-part ( p -- )
1581 :     to prim
1582 : pazsan 1.154 ." /* " prim prim-name 2@ prim-type ." ( " prim prim-stack-string 2@ type ." ) */" cr
1583 : anton 1.79 ." NAME(" quote prim prim-name 2@ type quote ." )" cr \ debugging
1584 :     ." {" cr
1585 :     print-declarations
1586 :     part-fetches
1587 :     print-debug-args
1588 : anton 1.118 combined ['] part-stack-pointer-updates prim-context
1589 :     1 part-num +!
1590 : anton 1.79 prim add-depths \ !! right place?
1591 : anton 1.85 prim prim-c-code 2@ ['] output-combined-tail type-c-code
1592 : anton 1.79 part-output-c-tail
1593 :     ." }" cr ;
1594 :    
1595 : anton 1.74 : output-parts ( -- )
1596 : anton 1.79 prim >r in-part on
1597 :     current-depth max-stacks cells erase
1598 : anton 1.118 0 part-num !
1599 : anton 1.114 ['] output-part map-combined
1600 : anton 1.79 in-part off
1601 : anton 1.74 r> to prim ;
1602 :    
1603 : anton 1.72 : output-c-combined ( -- )
1604 :     print-entry cr
1605 : anton 1.74 \ debugging messages just in parts
1606 : anton 1.72 ." {" cr
1607 :     ." DEF_CA" cr
1608 :     print-declarations-combined
1609 : anton 1.145 output-nextp0
1610 : anton 1.144 spill-state
1611 : anton 1.118 \ fetches \ now in parts
1612 : anton 1.74 \ print-debug-args
1613 : anton 1.118 \ stack-pointer-updates now in parts
1614 : anton 1.74 output-parts
1615 : anton 1.119 output-c-tail2-no-stores
1616 : anton 1.74 ." }" cr
1617 :     cr ;
1618 : anton 1.72
1619 :     : output-forth-combined ( -- )
1620 : anton 1.81 ;
1621 :    
1622 :    
1623 : anton 1.83 \ peephole optimization rules
1624 : anton 1.81
1625 : anton 1.114 \ data for a simple peephole optimizer that always tries to combine
1626 :     \ the currently compiled instruction with the last one.
1627 :    
1628 : anton 1.81 \ in order for this to work as intended, shorter combinations for each
1629 :     \ length must be present, and the longer combinations must follow
1630 :     \ shorter ones (this restriction may go away in the future).
1631 :    
1632 : anton 1.83 : output-peephole ( -- )
1633 : anton 1.81 combined-prims num-combined @ 1- cells combinations search-wordlist
1634 : anton 1.114 s" the prefix for this superinstruction must be defined earlier" ?print-error
1635 : anton 1.82 ." {"
1636 :     execute prim-num @ 5 .r ." ,"
1637 :     combined-prims num-combined @ 1- th @ prim-num @ 5 .r ." ,"
1638 :     combined prim-num @ 5 .r ." }, /* "
1639 :     combined prim-c-name 2@ type ." */"
1640 :     cr ;
1641 :    
1642 : anton 1.114
1643 : anton 1.115 \ cost and superinstruction data for a sophisticated combiner (e.g.,
1644 :     \ shortest path)
1645 : anton 1.114
1646 :     \ This is intended as initializer for a structure like this
1647 :    
1648 : anton 1.116 \ struct cost {
1649 : anton 1.146 \ char loads; /* number of stack loads */
1650 :     \ char stores; /* number of stack stores */
1651 :     \ char updates; /* number of stack pointer updates */
1652 :     \ char branch; /* is it a branch (SET_IP) */
1653 :     \ char state_in; /* state on entry */
1654 :     \ char state_out; /* state on exit */
1655 :     \ short offset; /* offset into super2 table */
1656 :     \ char length; /* number of components */
1657 : anton 1.114 \ };
1658 :    
1659 : anton 1.115 \ How do you know which primitive or combined instruction this
1660 :     \ structure refers to? By the order of cost structures, as in most
1661 :     \ other cases.
1662 :    
1663 : anton 1.139 : super2-length ( -- n )
1664 :     combined if
1665 :     num-combined @
1666 :     else
1667 :     1
1668 :     endif ;
1669 :    
1670 : anton 1.115 : compute-costs { p -- nloads nstores nupdates }
1671 :     \ compute the number of loads, stores, and stack pointer updates
1672 :     \ of a primitive or combined instruction; does not take TOS
1673 : anton 1.139 \ caching into account
1674 : anton 1.115 0 max-stacks 0 +do
1675 :     p prim-stacks-in i th @ +
1676 :     loop
1677 : anton 1.139 super2-length 1- - \ don't count instruction fetches of subsumed insts
1678 : anton 1.115 0 max-stacks 0 +do
1679 :     p prim-stacks-out i th @ +
1680 :     loop
1681 : anton 1.139 0 max-stacks 1 +do \ don't count ip updates, therefore "1 +do"
1682 : anton 1.115 p prim-stacks-in i th @ p prim-stacks-out i th @ <> -
1683 :     loop ;
1684 : anton 1.114
1685 :     : output-num-part ( p -- )
1686 : anton 1.146 ." N_" prim-c-name-orig 2@ type ." ," ;
1687 : anton 1.141 \ prim-num @ 4 .r ." ," ;
1688 : anton 1.138
1689 :     : output-name-comment ( -- )
1690 : pazsan 1.154 ." /* " prim prim-name 2@ prim-type ." */" ;
1691 : anton 1.138
1692 :     variable offset-super2 0 offset-super2 ! \ offset into the super2 table
1693 : anton 1.141
1694 : anton 1.143 : output-costs-prefix ( -- )
1695 :     ." {" prim compute-costs
1696 :     rot 2 .r ." ," swap 2 .r ." ," 2 .r ." , "
1697 : anton 1.144 prim prim-branch? negate . ." ,"
1698 :     state-in state-number @ 2 .r ." ,"
1699 : anton 1.151 state-out state-number @ 2 .r ." ,"
1700 :     inst-stream stack-in @ 1 .r ." ,"
1701 :     ;
1702 : anton 1.143
1703 : anton 1.141 : output-costs-gforth-simple ( -- )
1704 : anton 1.143 output-costs-prefix
1705 : anton 1.141 prim output-num-part
1706 : anton 1.151 1 2 .r ." },"
1707 : anton 1.141 output-name-comment
1708 :     cr ;
1709 :    
1710 :     : output-costs-gforth-combined ( -- )
1711 : anton 1.143 output-costs-prefix
1712 : anton 1.141 ." N_START_SUPER+" offset-super2 @ 5 .r ." ,"
1713 : anton 1.151 super2-length dup 2 .r ." }," offset-super2 +!
1714 : anton 1.141 output-name-comment
1715 :     cr ;
1716 : anton 1.138
1717 : anton 1.150 \ : output-costs ( -- )
1718 :     \ \ description of superinstructions and simple instructions
1719 :     \ ." {" prim compute-costs
1720 :     \ rot 2 .r ." ," swap 2 .r ." ," 2 .r ." ,"
1721 :     \ offset-super2 @ 5 .r ." ,"
1722 :     \ super2-length dup 2 .r ." ," offset-super2 +!
1723 :     \ inst-stream stack-in @ 1 .r ." },"
1724 :     \ output-name-comment
1725 :     \ cr ;
1726 : anton 1.138
1727 : anton 1.146 : output-super2-simple ( -- )
1728 :     prim prim-c-name 2@ prim prim-c-name-orig 2@ d= if
1729 : anton 1.138 prim output-num-part
1730 : anton 1.146 output-name-comment
1731 :     cr
1732 :     endif ;
1733 :    
1734 :     : output-super2-combined ( -- )
1735 :     ['] output-num-part map-combined
1736 : anton 1.138 output-name-comment
1737 :     cr ;
1738 : anton 1.69
1739 : anton 1.67 \ the parser
1740 :    
1741 :     eof-char max-member \ the whole character set + EOF
1742 :    
1743 :     : getinput ( -- n )
1744 :     rawinput @ endrawinput @ =
1745 :     if
1746 :     eof-char
1747 :     else
1748 :     cookedinput @ c@
1749 :     endif ;
1750 :    
1751 :     :noname ( n -- )
1752 :     dup bl > if
1753 :     emit space
1754 :     else
1755 :     .
1756 :     endif ;
1757 :     print-token !
1758 :    
1759 :     : testchar? ( set -- f )
1760 :     getinput member? ;
1761 :     ' testchar? test-vector !
1762 :    
1763 : anton 1.130 : checksynclines ( -- )
1764 : anton 1.67 \ when input points to a newline, check if the next line is a
1765 :     \ sync line. If it is, perform the appropriate actions.
1766 : anton 1.131 rawinput @ begin >r
1767 : anton 1.130 s" #line " r@ over compare if
1768 :     rdrop 1 line +! EXIT
1769 :     endif
1770 :     0. r> 6 chars + 20 >number drop >r drop line ! r> ( c-addr )
1771 :     dup c@ bl = if
1772 :     char+ dup c@ [char] " <> 0= s" sync line syntax" ?print-error
1773 :     char+ dup 100 [char] " scan drop swap 2dup - save-mem filename 2!
1774 :     char+
1775 :     endif
1776 :     dup c@ nl-char <> 0= s" sync line syntax" ?print-error
1777 :     skipsynclines @ if
1778 : anton 1.131 char+ dup rawinput !
1779 : anton 1.130 rawinput @ c@ cookedinput @ c!
1780 :     endif
1781 :     again ;
1782 : anton 1.67
1783 :     : ?nextchar ( f -- )
1784 : anton 1.71 s" syntax error, wrong char" ?print-error
1785 : anton 1.67 rawinput @ endrawinput @ <> if
1786 :     rawinput @ c@
1787 :     1 chars rawinput +!
1788 :     1 chars cookedinput +!
1789 :     nl-char = if
1790 : anton 1.130 checksynclines
1791 : anton 1.67 rawinput @ line-start !
1792 :     endif
1793 : anton 1.130 rawinput @ c@
1794 :     cookedinput @ c!
1795 : anton 1.67 endif ;
1796 :    
1797 :     : charclass ( set "name" -- )
1798 :     ['] ?nextchar terminal ;
1799 :    
1800 :     : .. ( c1 c2 -- set )
1801 :     ( creates a set that includes the characters c, c1<=c<=c2 )
1802 :     empty copy-set
1803 :     swap 1+ rot do
1804 :     i over add-member
1805 :     loop ;
1806 :    
1807 :     : ` ( -- terminal ) ( use: ` c )
1808 :     ( creates anonymous terminal for the character c )
1809 :     char singleton ['] ?nextchar make-terminal ;
1810 :    
1811 :     char a char z .. char A char Z .. union char _ singleton union charclass letter
1812 :     char 0 char 9 .. charclass digit
1813 :     bl singleton tab-char over add-member charclass white
1814 :     nl-char singleton eof-char over add-member complement charclass nonl
1815 :     nl-char singleton eof-char over add-member
1816 :     char : over add-member complement charclass nocolonnl
1817 : anton 1.110 nl-char singleton eof-char over add-member
1818 :     char } over add-member complement charclass nobracenl
1819 : anton 1.67 bl 1+ maxchar .. char \ singleton complement intersection
1820 :     charclass nowhitebq
1821 :     bl 1+ maxchar .. charclass nowhite
1822 :     char " singleton eof-char over add-member complement charclass noquote
1823 :     nl-char singleton charclass nl
1824 :     eof-char singleton charclass eof
1825 : anton 1.79 nl-char singleton eof-char over add-member charclass nleof
1826 : anton 1.67
1827 :     (( letter (( letter || digit )) **
1828 :     )) <- c-ident ( -- )
1829 :    
1830 : anton 1.158 (( ` . ` . ` .
1831 :     )) <- sync-stack ( -- )
1832 :    
1833 :     (( ` # ?? (( letter || digit || ` : )) ++ sync-stack ??
1834 :     || sync-stack
1835 : anton 1.67 )) <- stack-ident ( -- )
1836 :    
1837 :     (( nowhitebq nowhite ** ))
1838 :     <- forth-ident ( -- )
1839 :    
1840 :     Variable forth-flag
1841 :     Variable c-flag
1842 :    
1843 :     (( (( ` e || ` E )) {{ start }} nonl **
1844 :     {{ end evaluate }}
1845 :     )) <- eval-comment ( ... -- ... )
1846 :    
1847 :     (( (( ` f || ` F )) {{ start }} nonl **
1848 :     {{ end forth-flag @ IF type cr ELSE 2drop THEN }}
1849 :     )) <- forth-comment ( -- )
1850 :    
1851 :     (( (( ` c || ` C )) {{ start }} nonl **
1852 :     {{ end c-flag @ IF type cr ELSE 2drop THEN }}
1853 :     )) <- c-comment ( -- )
1854 :    
1855 :     (( ` - nonl ** {{
1856 : pazsan 1.140 forth-flag @ IF forth-fdiff ." [ELSE]" cr THEN
1857 :     c-flag @ IF
1858 :     function-diff
1859 :     ." #else /* " function-number @ 0 .r ." */" cr THEN }}
1860 : anton 1.67 )) <- else-comment
1861 :    
1862 :     (( ` + {{ start }} nonl ** {{ end
1863 : anton 1.167 dup
1864 :     IF
1865 :     c-flag @ IF
1866 :     function-diff
1867 :     ." #ifdef HAS_" 2dup bounds ?DO I c@ toupper emit LOOP cr
1868 :     THEN
1869 :     forth-flag @ IF
1870 :     forth-fdiff ." has? " 2dup type ." [IF]" cr
1871 :     THEN
1872 :     2drop
1873 :     ELSE
1874 :     2drop
1875 : pazsan 1.140 c-flag @ IF
1876 :     function-diff ." #endif" cr THEN
1877 :     forth-flag @ IF forth-fdiff ." [THEN]" cr THEN
1878 : anton 1.67 THEN }}
1879 :     )) <- if-comment
1880 :    
1881 : pazsan 1.98 (( (( ` g || ` G )) {{ start }} nonl **
1882 :     {{ end
1883 : anton 1.167 forth-flag @ IF forth-fdiff ." group " 2dup type cr THEN
1884 : pazsan 1.140 c-flag @ IF function-diff
1885 : anton 1.167 ." GROUP(" 2dup type ." , " function-number @ 0 .r ." )" cr THEN
1886 :     2drop }}
1887 : pazsan 1.98 )) <- group-comment
1888 :    
1889 :     (( (( eval-comment || forth-comment || c-comment || else-comment || if-comment || group-comment )) ?? nonl ** )) <- comment-body
1890 : anton 1.67
1891 : anton 1.79 (( ` \ comment-body nleof )) <- comment ( -- )
1892 : anton 1.67
1893 : anton 1.156 (( {{ start }} stack-ident {{ end init-item1 }} white ** )) **
1894 :     <- stack-items ( addr1 -- addr2 )
1895 : anton 1.67
1896 : anton 1.69 (( {{ prim prim-effect-in }} stack-items {{ prim prim-effect-in-end ! }}
1897 : anton 1.67 ` - ` - white **
1898 : anton 1.69 {{ prim prim-effect-out }} stack-items {{ prim prim-effect-out-end ! }}
1899 : anton 1.67 )) <- stack-effect ( -- )
1900 :    
1901 : anton 1.157 (( {{ prim create-prim prim init-simple }}
1902 : anton 1.69 ` ( white ** {{ start }} stack-effect {{ end prim prim-stack-string 2! }} ` ) white **
1903 :     (( {{ start }} forth-ident {{ end prim prim-wordset 2! }} white **
1904 : anton 1.167 (( {{ start }} c-ident {{ end prim-c-name-2! }} )) ??
1905 : anton 1.79 )) ?? nleof
1906 :     (( ` " ` " {{ start }} (( noquote ++ ` " )) ++ {{ end 1- prim prim-doc 2! }} ` " white ** nleof )) ??
1907 : anton 1.110 {{ skipsynclines off line @ c-line ! filename 2@ c-filename 2! start }}
1908 :     (( (( ` { nonl ** nleof (( (( nobracenl {{ line @ drop }} nonl ** )) ?? nleof )) ** ` } white ** nleof white ** ))
1909 :     || (( nocolonnl nonl ** nleof white ** )) ** ))
1910 :     {{ end prim prim-c-code 2! skipsynclines on }}
1911 : anton 1.79 (( ` : white ** nleof
1912 :     {{ start }} (( nonl ++ nleof white ** )) ++ {{ end prim prim-forth-code 2! }}
1913 : anton 1.81 )) ?? {{ process-simple }}
1914 : anton 1.79 nleof
1915 : anton 1.69 )) <- simple-primitive ( -- )
1916 :    
1917 : anton 1.71 (( {{ init-combined }}
1918 : anton 1.89 ` = white ** (( {{ start }} forth-ident {{ end add-prim }} white ** )) ++
1919 : anton 1.79 nleof {{ process-combined }}
1920 : anton 1.69 )) <- combined-primitive
1921 :    
1922 : anton 1.79 (( {{ make-prim to prim 0 to combined
1923 : anton 1.69 line @ name-line ! filename 2@ name-filename 2!
1924 : anton 1.82 function-number @ prim prim-num !
1925 : anton 1.110 start }} [ifdef] vmgen c-ident [else] forth-ident [then] {{ end
1926 : anton 1.146 2dup prim prim-name 2! prim-c-name-2! }} white **
1927 :     (( ` / white ** {{ start }} c-ident {{ end prim-c-name-2! }} white ** )) ??
1928 : anton 1.138 (( simple-primitive || combined-primitive ))
1929 :     {{ 1 function-number +! }}
1930 : anton 1.67 )) <- primitive ( -- )
1931 :    
1932 :     (( (( comment || primitive || nl white ** )) ** eof ))
1933 :     parser primitives2something
1934 :     warnings @ [IF]
1935 :     .( parser generated ok ) cr
1936 :     [THEN]
1937 :    
1938 : jwilke 1.95
1939 : jwilke 1.97 \ run with gforth-0.5.0 (slurp-file is missing)
1940 : jwilke 1.95 [IFUNDEF] slurp-file
1941 :     : slurp-file ( c-addr1 u1 -- c-addr2 u2 )
1942 :     \ c-addr1 u1 is the filename, c-addr2 u2 is the file's contents
1943 :     r/o bin open-file throw >r
1944 :     r@ file-size throw abort" file too large"
1945 :     dup allocate throw swap
1946 :     2dup r@ read-file throw over <> abort" could not read whole file"
1947 :     r> close-file throw ;
1948 :     [THEN]
1949 :    
1950 : anton 1.69 : primfilter ( addr u -- )
1951 :     \ process the string at addr u
1952 :     over dup rawinput ! dup line-start ! cookedinput !
1953 :     + endrawinput !
1954 : anton 1.130 checksynclines
1955 : anton 1.69 primitives2something ;
1956 : pazsan 1.8
1957 : anton 1.130 : unixify ( c-addr u1 -- c-addr u2 )
1958 :     \ delete crs from the string
1959 :     bounds tuck tuck ?do ( c-addr1 )
1960 :     i c@ dup #cr <> if
1961 :     over c! char+
1962 :     else
1963 :     drop
1964 :     endif
1965 :     loop
1966 :     over - ;
1967 :    
1968 : anton 1.72 : process-file ( addr u xt-simple x-combined -- )
1969 :     output-combined ! output !
1970 : anton 1.61 save-mem 2dup filename 2!
1971 : anton 1.130 slurp-file unixify
1972 : anton 1.17 warnings @ if
1973 :     ." ------------ CUT HERE -------------" cr endif
1974 : anton 1.69 primfilter ;
1975 : pazsan 1.30
1976 : anton 1.72 \ : process ( xt -- )
1977 :     \ bl word count rot
1978 :     \ process-file ;

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help