[gforth] / gforth / prims2x.fs  

gforth: gforth/prims2x.fs


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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help