[gforth] / gforth / prims2x.fs  

gforth: gforth/prims2x.fs


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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help