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