Annotation of gforth/prims2x.fs, revision 1.120
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:
660: : stack-pointer-update { stack -- }
661: \ stack grow downwards
1.119 anton 662: stack stack-diff
1.49 anton 663: ?dup-if \ this check is not necessary, gcc would do this for us
1.118 anton 664: stack inst-stream = if
1.120 ! anton 665: ." INC_IP(" 0 .r ." );" cr
1.118 anton 666: else
667: stack stack-pointer 2@ type ." += " 0 .r ." ;" cr
668: endif
1.55 anton 669: endif ;
670:
1.1 anton 671: : stack-pointer-updates ( -- )
1.92 anton 672: ['] stack-pointer-update map-stacks ;
1.1 anton 673:
674: : store ( item -- )
675: \ f is true if the item should be stored
676: \ f is false if the store is probably not necessary
1.49 anton 677: dup item-type @ type-store @ execute ;
1.1 anton 678:
679: : stores ( -- )
1.69 anton 680: prim prim-effect-out prim prim-effect-out-end @ ['] store map-items ;
1.8 pazsan 681:
1.91 anton 682: : print-debug-arg { item -- }
683: ." fputs(" quote space item item-name 2@ type ." =" quote ." , vm_out); "
684: ." printarg_" item item-type @ print-type-prefix
685: ." (" item item-name 2@ type ." );" cr ;
686:
687: : print-debug-args ( -- )
688: ." #ifdef VM_DEBUG" cr
689: ." if (vm_debug) {" cr
690: prim prim-effect-in prim prim-effect-in-end @ ['] print-debug-arg map-items
691: \ ." fputc('\n', vm_out);" cr
692: ." }" cr
693: ." #endif" cr ;
694:
695: : print-debug-result { item -- }
696: item item-first @ if
697: item print-debug-arg
698: endif ;
699:
700: : print-debug-results ( -- )
701: cr
702: ." #ifdef VM_DEBUG" cr
703: ." if (vm_debug) {" cr
704: ." fputs(" quote ." -- " quote ." , vm_out); "
705: prim prim-effect-out prim prim-effect-out-end @ ['] print-debug-result map-items
706: ." fputc('\n', vm_out);" cr
707: ." }" cr
708: ." #endif" cr ;
709:
1.86 anton 710: : output-super-end ( -- )
711: prim prim-c-code 2@ s" SET_IP" search if
712: ." SUPER_END;" cr
713: endif
714: 2drop ;
715:
1.120 ! anton 716: : output-label2 ( -- )
! 717: ." LABEL2(" prim prim-c-name 2@ type ." )" cr ;
! 718:
! 719: : output-c-tail1 { xt -- }
! 720: \ the final part of the generated C code, with xt printing LABEL2 or not.
1.86 anton 721: output-super-end
1.91 anton 722: print-debug-results
1.120 ! anton 723: ." NEXT_P1;" cr
1.52 anton 724: stores
1.119 anton 725: fill-tos
1.120 ! anton 726: xt execute
! 727: ." NEXT_P2;" cr ;
1.108 anton 728:
1.120 ! anton 729: : output-c-tail1-no-stores { xt -- }
! 730: \ the final part of the generated C code for combinations
! 731: output-super-end
! 732: ." NEXT_P1;" cr
1.119 anton 733: fill-tos
1.120 ! anton 734: xt execute
! 735: ." NEXT_P2;" cr ;
! 736:
! 737: : output-c-tail ( -- )
! 738: ['] noop output-c-tail1 ;
1.52 anton 739:
1.108 anton 740: : output-c-tail2 ( -- )
1.120 ! anton 741: ['] output-label2 output-c-tail1 ;
! 742:
! 743: : output-c-tail-no-stores ( -- )
! 744: ['] noop output-c-tail1-no-stores ;
1.119 anton 745:
746: : output-c-tail2-no-stores ( -- )
1.120 ! anton 747: ['] output-label2 output-c-tail1-no-stores ;
1.108 anton 748:
1.85 anton 749: : type-c-code ( c-addr u xt -- )
1.109 anton 750: \ like TYPE, but replaces "INST_TAIL;" with tail code produced by xt
1.85 anton 751: { xt }
1.111 anton 752: ." {" cr
753: ." #line " c-line @ . quote c-filename 2@ type quote cr
1.52 anton 754: begin ( c-addr1 u1 )
1.109 anton 755: 2dup s" INST_TAIL;" search
1.52 anton 756: while ( c-addr1 u1 c-addr3 u3 )
757: 2dup 2>r drop nip over - type
1.85 anton 758: xt execute
1.109 anton 759: 2r> 10 /string
1.52 anton 760: \ !! resync #line missing
761: repeat
1.111 anton 762: 2drop type
763: ." #line " out-nls @ 2 + . quote out-filename 2@ type quote cr
764: ." }" cr ;
1.63 anton 765:
1.72 anton 766: : print-entry ( -- )
1.109 anton 767: ." LABEL(" prim prim-c-name 2@ type ." )" ;
1.63 anton 768:
1.43 jwilke 769: : output-c ( -- )
1.111 anton 770: print-entry ." /* " prim prim-name 2@ type ." ( " prim prim-stack-string 2@ type ." ) */" cr
771: ." /* " prim prim-doc 2@ type ." */" cr
772: ." NAME(" quote prim prim-name 2@ type quote ." )" cr \ debugging
773: ." {" cr
774: ." DEF_CA" cr
775: print-declarations
776: ." NEXT_P0;" cr
777: flush-tos
778: fetches
779: print-debug-args
780: stack-pointer-updates
781: prim prim-c-code 2@ ['] output-c-tail type-c-code
782: output-c-tail2
783: ." }" cr
784: cr
1.1 anton 785: ;
786:
1.56 anton 787: : disasm-arg { item -- }
788: item item-stack @ inst-stream = if
1.107 anton 789: ." {" cr
790: item print-declaration
791: item fetch
792: item print-debug-arg
793: ." }" cr
1.56 anton 794: endif ;
795:
796: : disasm-args ( -- )
1.69 anton 797: prim prim-effect-in prim prim-effect-in-end @ ['] disasm-arg map-items ;
1.56 anton 798:
799: : output-disasm ( -- )
800: \ generate code for disassembling VM instructions
1.106 anton 801: ." if (VM_IS_INST(*ip, " function-number @ 0 .r ." )) {" cr
1.69 anton 802: ." fputs(" quote prim prim-name 2@ type quote ." , vm_out);" cr
1.56 anton 803: disasm-args
804: ." ip += " inst-stream stack-in @ 1+ 0 .r ." ;" cr
1.91 anton 805: ." goto _endif_;" cr
806: ." }" cr ;
1.56 anton 807:
1.86 anton 808: : output-profile ( -- )
809: \ generate code for postprocessing the VM block profile stuff
1.87 anton 810: ." if (VM_IS_INST(*ip, " function-number @ 0 .r ." )) {" cr
1.104 anton 811: ." add_inst(b, " quote prim prim-name 2@ type quote ." );" cr
1.86 anton 812: ." ip += " inst-stream stack-in @ 1+ 0 .r ." ;" cr
813: prim prim-c-code 2@ s" SET_IP" search nip nip
814: prim prim-c-code 2@ s" SUPER_END" search nip nip or if
815: ." return;" cr
1.91 anton 816: else
817: ." goto _endif_;" cr
1.86 anton 818: endif
1.91 anton 819: ." }" cr ;
1.86 anton 820:
1.114 anton 821: : output-profile-part ( p )
822: ." add_inst(b, " quote
823: prim-name 2@ type
824: quote ." );" cr ;
825:
1.104 anton 826: : output-profile-combined ( -- )
827: \ generate code for postprocessing the VM block profile stuff
828: ." if (VM_IS_INST(*ip, " function-number @ 0 .r ." )) {" cr
1.114 anton 829: ['] output-profile-part map-combined
1.104 anton 830: ." ip += " inst-stream stack-in @ 1+ 0 .r ." ;" cr
831: combined-prims num-combined @ 1- th @ prim-c-code 2@ s" SET_IP" search nip nip
832: combined-prims num-combined @ 1- th @ prim-c-code 2@ s" SUPER_END" search nip nip or if
833: ." return;" cr
834: else
835: ." goto _endif_;" cr
836: endif
837: ." }" cr ;
838:
1.103 anton 839: : output-superend ( -- )
840: \ output flag specifying whether the current word ends a dynamic superinst
841: prim prim-c-code 2@ s" SET_IP" search nip nip
842: prim prim-c-code 2@ s" SUPER_END" search nip nip or 0<>
843: prim prim-c-code 2@ s" SUPER_CONTINUE" search nip nip 0= and
844: negate 0 .r ." , /* " prim prim-name 2@ type ." */" cr ;
845:
1.60 anton 846: : gen-arg-parm { item -- }
847: item item-stack @ inst-stream = if
848: ." , " item item-type @ type-c-name 2@ type space
849: item item-name 2@ type
850: endif ;
851:
852: : gen-args-parm ( -- )
1.69 anton 853: prim prim-effect-in prim prim-effect-in-end @ ['] gen-arg-parm map-items ;
1.60 anton 854:
855: : gen-arg-gen { item -- }
856: item item-stack @ inst-stream = if
857: ." genarg_" item item-type @ print-type-prefix
858: ." (ctp, " item item-name 2@ type ." );" cr
859: endif ;
860:
861: : gen-args-gen ( -- )
1.69 anton 862: prim prim-effect-in prim prim-effect-in-end @ ['] gen-arg-gen map-items ;
1.60 anton 863:
864: : output-gen ( -- )
865: \ generate C code for generating VM instructions
1.69 anton 866: ." void gen_" prim prim-c-name 2@ type ." (Inst **ctp" gen-args-parm ." )" cr
1.60 anton 867: ." {" cr
868: ." gen_inst(ctp, vm_prim[" function-number @ 0 .r ." ]);" cr
869: gen-args-gen
1.68 anton 870: ." }" cr ;
1.60 anton 871:
1.49 anton 872: : stack-used? { stack -- f }
873: stack stack-in @ stack stack-out @ or 0<> ;
1.44 jwilke 874:
1.30 pazsan 875: : output-funclabel ( -- )
1.69 anton 876: ." &I_" prim prim-c-name 2@ type ." ," cr ;
1.30 pazsan 877:
878: : output-forthname ( -- )
1.69 anton 879: '" emit prim prim-name 2@ type '" emit ." ," cr ;
1.30 pazsan 880:
1.92 anton 881: \ : output-c-func ( -- )
882: \ \ used for word libraries
883: \ ." Cell * I_" prim prim-c-name 2@ type ." (Cell *SP, Cell **FP) /* " prim prim-name 2@ type
884: \ ." ( " prim prim-stack-string 2@ type ." ) */" cr
885: \ ." /* " prim prim-doc 2@ type ." */" cr
886: \ ." NAME(" quote prim prim-name 2@ type quote ." )" cr
887: \ \ debugging
888: \ ." {" cr
889: \ print-declarations
890: \ \ !! don't know what to do about that
891: \ inst-stream stack-used? IF ." Cell *ip=IP;" cr THEN
892: \ data-stack stack-used? IF ." Cell *sp=SP;" cr THEN
893: \ fp-stack stack-used? IF ." Cell *fp=*FP;" cr THEN
894: \ return-stack stack-used? IF ." Cell *rp=*RP;" cr THEN
895: \ flush-tos
896: \ fetches
897: \ stack-pointer-updates
898: \ fp-stack stack-used? IF ." *FP=fp;" cr THEN
899: \ ." {" cr
900: \ ." #line " c-line @ . quote c-filename 2@ type quote cr
901: \ prim prim-c-code 2@ type
902: \ ." }" cr
903: \ stores
904: \ fill-tos
905: \ ." return (sp);" cr
906: \ ." }" cr
907: \ cr ;
1.30 pazsan 908:
1.43 jwilke 909: : output-label ( -- )
1.102 anton 910: ." INST_ADDR(" prim prim-c-name 2@ type ." )," cr ;
1.1 anton 911:
1.43 jwilke 912: : output-alias ( -- )
1.69 anton 913: ( primitive-number @ . ." alias " ) ." Primitive " prim prim-name 2@ type cr ;
1.1 anton 914:
1.114 anton 915: : output-prim-num ( -- )
916: prim prim-num @ 8 + 4 .r space prim prim-name 2@ type cr ;
917:
1.43 jwilke 918: : output-forth ( -- )
1.69 anton 919: prim prim-forth-code @ 0=
1.30 pazsan 920: IF \ output-alias
1.28 jwilke 921: \ this is bad for ec: an alias is compiled if tho word does not exist!
922: \ JAW
1.69 anton 923: ELSE ." : " prim prim-name 2@ type ." ( "
924: prim prim-stack-string 2@ type ." )" cr
925: prim prim-forth-code 2@ type cr
1.30 pazsan 926: THEN ;
1.10 anton 927:
1.17 anton 928: : output-tag-file ( -- )
1.117 pazsan 929: name-filename 2@ last-name-filename 2@ compare if
1.17 anton 930: name-filename 2@ last-name-filename 2!
931: #ff emit cr
932: name-filename 2@ type
933: ." ,0" cr
934: endif ;
935:
936: : output-tag ( -- )
937: output-tag-file
1.69 anton 938: prim prim-name 2@ 1+ type
1.17 anton 939: 127 emit
1.69 anton 940: space prim prim-name 2@ type space
1.17 anton 941: 1 emit
942: name-line @ 0 .r
943: ." ,0" cr ;
944:
1.100 pazsan 945: : output-vi-tag ( -- )
946: name-filename 2@ type #tab emit
947: prim prim-name 2@ type #tab emit
948: ." /^" prim prim-name 2@ type ." *(/" cr ;
949:
1.10 anton 950: [IFDEF] documentation
951: : register-doc ( -- )
1.82 anton 952: prim prim-name 2@ documentation ['] create insert-wordlist
1.69 anton 953: prim prim-name 2@ 2,
954: prim prim-stack-string 2@ condition-stack-effect 2,
955: prim prim-wordset 2@ 2,
956: prim prim-c-name 2@ condition-pronounciation 2,
1.82 anton 957: prim prim-doc 2@ 2, ;
1.10 anton 958: [THEN]
1.67 anton 959:
960:
1.69 anton 961: \ combining instructions
962:
963: \ The input should look like this:
964:
965: \ lit_+ = lit +
966:
967: \ The output should look like this:
968:
969: \ I_lit_+:
970: \ {
971: \ DEF_CA
972: \ Cell _x_ip0;
973: \ Cell _x_sp0;
974: \ Cell _x_sp1;
975: \ NEXT_P0;
976: \ _x_ip0 = (Cell) IPTOS;
977: \ _x_sp0 = (Cell) spTOS;
978: \ INC_IP(1);
979: \ /* sp += 0; */
980: \ /* lit ( #w -- w ) */
981: \ /* */
982: \ NAME("lit")
983: \ {
984: \ Cell w;
985: \ w = (Cell) _x_ip0;
986: \ #ifdef VM_DEBUG
987: \ if (vm_debug) {
988: \ fputs(" w=", vm_out); printarg_w (w);
989: \ fputc('\n', vm_out);
990: \ }
991: \ #endif
992: \ {
993: \ #line 136 "./prim"
994: \ }
995: \ _x_sp1 = (Cell)w;
996: \ }
997: \ I_plus: /* + ( n1 n2 -- n ) */
998: \ /* */
999: \ NAME("+")
1000: \ {
1001: \ DEF_CA
1002: \ Cell n1;
1003: \ Cell n2;
1004: \ Cell n;
1005: \ NEXT_P0;
1006: \ n1 = (Cell) _x_sp0;
1007: \ n2 = (Cell) _x_sp1;
1008: \ #ifdef VM_DEBUG
1009: \ if (vm_debug) {
1010: \ fputs(" n1=", vm_out); printarg_n (n1);
1011: \ fputs(" n2=", vm_out); printarg_n (n2);
1012: \ fputc('\n', vm_out);
1013: \ }
1014: \ #endif
1015: \ {
1016: \ #line 516 "./prim"
1017: \ n = n1+n2;
1018: \ }
1019: \ _x_sp0 = (Cell)n;
1020: \ }
1021: \ NEXT_P1;
1022: \ spTOS = (Cell)_x_sp0;
1023: \ NEXT_P2;
1024:
1.71 anton 1025: : init-combined ( -- )
1.79 anton 1026: prim to combined
1.71 anton 1027: 0 num-combined !
1028: current-depth max-stacks cells erase
1.116 anton 1029: include-skipped-insts @ current-depth 0 th !
1.72 anton 1030: max-depth max-stacks cells erase
1031: min-depth max-stacks cells erase
1032: prim prim-effect-in prim prim-effect-in-end !
1033: prim prim-effect-out prim prim-effect-out-end ! ;
1.71 anton 1034:
1035: : max! ( n addr -- )
1036: tuck @ max swap ! ;
1037:
1.72 anton 1038: : min! ( n addr -- )
1039: tuck @ min swap ! ;
1040:
1.119 anton 1041: : inst-stream-adjustment ( nstack -- n )
1042: \ number of stack items to add for each part
1043: 0= include-skipped-insts @ and negate ;
1.116 anton 1044:
1.71 anton 1045: : add-depths { p -- }
1046: \ combine stack effect of p with *-depths
1047: max-stacks 0 ?do
1.72 anton 1048: current-depth i th @
1.119 anton 1049: p prim-stacks-in i th @ + i inst-stream-adjustment +
1.72 anton 1050: dup max-depth i th max!
1051: p prim-stacks-out i th @ -
1052: dup min-depth i th min!
1053: current-depth i th !
1.71 anton 1054: loop ;
1055:
1.118 anton 1056: : copy-maxdepths ( n -- )
1057: max-depth max-depths rot max-stacks * th max-stacks cells move ;
1058:
1.71 anton 1059: : add-prim ( addr u -- )
1060: \ add primitive given by "addr u" to combined-prims
1061: primitives search-wordlist s" unknown primitive" ?print-error
1062: execute { p }
1.72 anton 1063: p combined-prims num-combined @ th !
1.118 anton 1064: num-combined @ copy-maxdepths
1.71 anton 1065: 1 num-combined +!
1.118 anton 1066: p add-depths
1067: num-combined @ copy-maxdepths ;
1.71 anton 1068:
1069: : compute-effects { q -- }
1070: \ compute the stack effects of q from the depths
1071: max-stacks 0 ?do
1.72 anton 1072: max-depth i th @ dup
1073: q prim-stacks-in i th !
1074: current-depth i th @ -
1075: q prim-stacks-out i th !
1076: loop ;
1077:
1078: : make-effect-items { stack# items effect-endp -- }
1079: \ effect-endp points to a pointer to the end of the current item-array
1080: \ and has to be updated
1081: stacks stack# th @ { stack }
1082: items 0 +do
1083: effect-endp @ { item }
1084: i 0 <# #s stack stack-pointer 2@ holds [char] _ hold #> save-mem
1085: item item-name 2!
1086: stack item item-stack !
1.74 anton 1087: stack stack-type @ item item-type !
1.72 anton 1088: i item item-offset !
1089: item item-first on
1090: item% %size effect-endp +!
1091: loop ;
1092:
1093: : init-effects { q -- }
1094: \ initialize effects field for FETCHES and STORES
1095: max-stacks 0 ?do
1096: i q prim-stacks-in i th @ q prim-effect-in-end make-effect-items
1097: i q prim-stacks-out i th @ q prim-effect-out-end make-effect-items
1.71 anton 1098: loop ;
1099:
1.119 anton 1100: : compute-stack-max-back-depths ( stack -- )
1101: stack-number @ { stack# }
1102: current-depth stack# th @ dup
1103: dup stack# num-combined @ s-c-max-back-depth !
1104: -1 num-combined @ 1- -do ( max-depth current-depth )
1105: combined-prims i th @ { p }
1106: p prim-stacks-out stack# th @ +
1107: dup >r max r>
1108: over stack# i s-c-max-back-depth !
1109: p prim-stacks-in stack# th @ -
1110: stack# inst-stream-adjustment -
1111: 1 -loop
1112: assert( dup stack# inst-stream-adjustment negate = )
1113: assert( over max-depth stack# th @ = )
1114: 2drop ;
1115:
1116: : compute-max-back-depths ( -- )
1117: \ compute max-back-depths.
1118: \ assumes that current-depths is correct for the end of the combination
1119: ['] compute-stack-max-back-depths map-stacks ;
1120:
1.71 anton 1121: : process-combined ( -- )
1.81 anton 1122: combined combined-prims num-combined @ cells
1.82 anton 1123: combinations ['] constant insert-wordlist
1.86 anton 1124: combined-prims num-combined @ 1- th ( last-part )
1125: @ prim-c-code 2@ prim prim-c-code 2! \ used by output-super-end
1.72 anton 1126: prim compute-effects
1127: prim init-effects
1.119 anton 1128: compute-max-back-depths
1.72 anton 1129: output-combined perform ;
1130:
1131: \ C output
1132:
1133: : print-item { n stack -- }
1134: \ print nth stack item name
1.79 anton 1135: stack stack-type @ type-c-name 2@ type space
1136: ." _" stack stack-pointer 2@ type n 0 .r ;
1.72 anton 1137:
1138: : print-declarations-combined ( -- )
1139: max-stacks 0 ?do
1140: max-depth i th @ min-depth i th @ - 0 +do
1141: i stacks j th @ print-item ." ;" cr
1142: loop
1143: loop ;
1.79 anton 1144:
1145: : part-fetches ( -- )
1146: fetches ;
1147:
1148: : part-output-c-tail ( -- )
1.91 anton 1149: print-debug-results
1.85 anton 1150: stores ;
1151:
1152: : output-combined-tail ( -- )
1153: part-output-c-tail
1154: in-part @ >r in-part off
1.119 anton 1155: combined ['] output-c-tail-no-stores prim-context
1.118 anton 1156: r> in-part ! ;
1157:
1158: : part-stack-pointer-updates ( -- )
1159: max-stacks 0 +do
1160: i part-num @ 1+ s-c-max-depth @ dup
1161: i num-combined @ s-c-max-depth @ = \ final depth
1162: swap i part-num @ s-c-max-depth @ <> \ just reached now
1163: part-num @ 0= \ first part
1164: or and if
1165: stacks i th @ stack-pointer-update
1166: endif
1167: loop ;
1.79 anton 1168:
1169: : output-part ( p -- )
1170: to prim
1171: ." /* " prim prim-name 2@ type ." ( " prim prim-stack-string 2@ type ." ) */" cr
1172: ." NAME(" quote prim prim-name 2@ type quote ." )" cr \ debugging
1173: ." {" cr
1174: print-declarations
1175: part-fetches
1176: print-debug-args
1.118 anton 1177: combined ['] part-stack-pointer-updates prim-context
1178: 1 part-num +!
1.79 anton 1179: prim add-depths \ !! right place?
1.85 anton 1180: prim prim-c-code 2@ ['] output-combined-tail type-c-code
1.79 anton 1181: part-output-c-tail
1182: ." }" cr ;
1183:
1.74 anton 1184: : output-parts ( -- )
1.79 anton 1185: prim >r in-part on
1186: current-depth max-stacks cells erase
1.118 anton 1187: 0 part-num !
1.114 anton 1188: ['] output-part map-combined
1.79 anton 1189: in-part off
1.74 anton 1190: r> to prim ;
1191:
1.72 anton 1192: : output-c-combined ( -- )
1193: print-entry cr
1.74 anton 1194: \ debugging messages just in parts
1.72 anton 1195: ." {" cr
1196: ." DEF_CA" cr
1197: print-declarations-combined
1198: ." NEXT_P0;" cr
1199: flush-tos
1.118 anton 1200: \ fetches \ now in parts
1.74 anton 1201: \ print-debug-args
1.118 anton 1202: \ stack-pointer-updates now in parts
1.74 anton 1203: output-parts
1.119 anton 1204: output-c-tail2-no-stores
1.74 anton 1205: ." }" cr
1206: cr ;
1.72 anton 1207:
1208: : output-forth-combined ( -- )
1.81 anton 1209: ;
1210:
1211:
1.83 anton 1212: \ peephole optimization rules
1.81 anton 1213:
1.114 anton 1214: \ data for a simple peephole optimizer that always tries to combine
1215: \ the currently compiled instruction with the last one.
1216:
1.81 anton 1217: \ in order for this to work as intended, shorter combinations for each
1218: \ length must be present, and the longer combinations must follow
1219: \ shorter ones (this restriction may go away in the future).
1220:
1.83 anton 1221: : output-peephole ( -- )
1.81 anton 1222: combined-prims num-combined @ 1- cells combinations search-wordlist
1.114 anton 1223: s" the prefix for this superinstruction must be defined earlier" ?print-error
1.82 anton 1224: ." {"
1225: execute prim-num @ 5 .r ." ,"
1226: combined-prims num-combined @ 1- th @ prim-num @ 5 .r ." ,"
1227: combined prim-num @ 5 .r ." }, /* "
1228: combined prim-c-name 2@ type ." */"
1229: cr ;
1230:
1.114 anton 1231:
1.115 anton 1232: \ cost and superinstruction data for a sophisticated combiner (e.g.,
1233: \ shortest path)
1.114 anton 1234:
1235: \ This is intended as initializer for a structure like this
1236:
1.116 anton 1237: \ struct cost {
1.114 anton 1238: \ int loads; /* number of stack loads */
1239: \ int stores; /* number of stack stores */
1240: \ int updates; /* number of stack pointer updates */
1241: \ int length; /* number of components */
1242: \ int *components; /* array of vm_prim indexes of components */
1243: \ };
1244:
1.115 anton 1245: \ How do you know which primitive or combined instruction this
1246: \ structure refers to? By the order of cost structures, as in most
1247: \ other cases.
1248:
1249: : compute-costs { p -- nloads nstores nupdates }
1250: \ compute the number of loads, stores, and stack pointer updates
1251: \ of a primitive or combined instruction; does not take TOS
1252: \ caching into account, nor that IP updates are combined with
1253: \ other stuff
1254: 0 max-stacks 0 +do
1255: p prim-stacks-in i th @ +
1256: loop
1257: 0 max-stacks 0 +do
1258: p prim-stacks-out i th @ +
1259: loop
1260: 0 max-stacks 0 +do
1261: p prim-stacks-in i th @ p prim-stacks-out i th @ <> -
1262: loop ;
1.114 anton 1263:
1264: : output-num-part ( p -- )
1265: prim-num @ 4 .r ." ," ;
1266:
1.115 anton 1267: : output-costs ( -- )
1268: ." {" prim compute-costs
1269: rot 2 .r ." ," swap 2 .r ." ," 2 .r ." ,"
1270: combined if
1271: num-combined @ 2 .r
1272: ." , ((int []){" ['] output-num-part map-combined ." })}, /* "
1273: else
1274: ." 1, ((int []){" prim prim-num @ 4 .r ." })}, /* "
1275: endif
1276: prim prim-name 2@ type ." */"
1.90 pazsan 1277: cr ;
1.69 anton 1278:
1.67 anton 1279: \ the parser
1280:
1281: eof-char max-member \ the whole character set + EOF
1282:
1283: : getinput ( -- n )
1284: rawinput @ endrawinput @ =
1285: if
1286: eof-char
1287: else
1288: cookedinput @ c@
1289: endif ;
1290:
1291: :noname ( n -- )
1292: dup bl > if
1293: emit space
1294: else
1295: .
1296: endif ;
1297: print-token !
1298:
1299: : testchar? ( set -- f )
1300: getinput member? ;
1301: ' testchar? test-vector !
1302:
1303: : checksyncline ( -- )
1304: \ when input points to a newline, check if the next line is a
1305: \ sync line. If it is, perform the appropriate actions.
1306: rawinput @ >r
1.117 pazsan 1307: s" #line " r@ over compare if
1.67 anton 1308: rdrop 1 line +! EXIT
1309: endif
1310: 0. r> 6 chars + 20 >number drop >r drop line ! r> ( c-addr )
1311: dup c@ bl = if
1.81 anton 1312: char+ dup c@ [char] " <> 0= s" sync line syntax" ?print-error
1.67 anton 1313: char+ dup 100 [char] " scan drop swap 2dup - save-mem filename 2!
1314: char+
1315: endif
1.81 anton 1316: dup c@ nl-char <> 0= s" sync line syntax" ?print-error
1.67 anton 1317: skipsynclines @ if
1318: dup char+ rawinput !
1319: rawinput @ c@ cookedinput @ c!
1320: endif
1321: drop ;
1322:
1323: : ?nextchar ( f -- )
1.71 anton 1324: s" syntax error, wrong char" ?print-error
1.67 anton 1325: rawinput @ endrawinput @ <> if
1326: rawinput @ c@
1327: 1 chars rawinput +!
1328: 1 chars cookedinput +!
1329: nl-char = if
1330: checksyncline
1331: rawinput @ line-start !
1332: endif
1333: rawinput @ c@ cookedinput @ c!
1334: endif ;
1335:
1336: : charclass ( set "name" -- )
1337: ['] ?nextchar terminal ;
1338:
1339: : .. ( c1 c2 -- set )
1340: ( creates a set that includes the characters c, c1<=c<=c2 )
1341: empty copy-set
1342: swap 1+ rot do
1343: i over add-member
1344: loop ;
1345:
1346: : ` ( -- terminal ) ( use: ` c )
1347: ( creates anonymous terminal for the character c )
1348: char singleton ['] ?nextchar make-terminal ;
1349:
1350: char a char z .. char A char Z .. union char _ singleton union charclass letter
1351: char 0 char 9 .. charclass digit
1352: bl singleton tab-char over add-member charclass white
1353: nl-char singleton eof-char over add-member complement charclass nonl
1354: nl-char singleton eof-char over add-member
1355: char : over add-member complement charclass nocolonnl
1.110 anton 1356: nl-char singleton eof-char over add-member
1357: char } over add-member complement charclass nobracenl
1.67 anton 1358: bl 1+ maxchar .. char \ singleton complement intersection
1359: charclass nowhitebq
1360: bl 1+ maxchar .. charclass nowhite
1361: char " singleton eof-char over add-member complement charclass noquote
1362: nl-char singleton charclass nl
1363: eof-char singleton charclass eof
1.79 anton 1364: nl-char singleton eof-char over add-member charclass nleof
1.67 anton 1365:
1366: (( letter (( letter || digit )) **
1367: )) <- c-ident ( -- )
1368:
1.110 anton 1369: (( ` # ?? (( letter || digit || ` : )) ++
1.67 anton 1370: )) <- stack-ident ( -- )
1371:
1372: (( nowhitebq nowhite ** ))
1373: <- forth-ident ( -- )
1374:
1375: Variable forth-flag
1376: Variable c-flag
1377:
1378: (( (( ` e || ` E )) {{ start }} nonl **
1379: {{ end evaluate }}
1380: )) <- eval-comment ( ... -- ... )
1381:
1382: (( (( ` f || ` F )) {{ start }} nonl **
1383: {{ end forth-flag @ IF type cr ELSE 2drop THEN }}
1384: )) <- forth-comment ( -- )
1385:
1386: (( (( ` c || ` C )) {{ start }} nonl **
1387: {{ end c-flag @ IF type cr ELSE 2drop THEN }}
1388: )) <- c-comment ( -- )
1389:
1390: (( ` - nonl ** {{
1391: forth-flag @ IF ." [ELSE]" cr THEN
1392: c-flag @ IF ." #else" cr THEN }}
1393: )) <- else-comment
1394:
1395: (( ` + {{ start }} nonl ** {{ end
1396: dup
1397: IF c-flag @
1398: IF ." #ifdef HAS_" bounds ?DO I c@ toupper emit LOOP cr
1399: THEN
1400: forth-flag @
1401: IF ." has? " type ." [IF]" cr THEN
1402: ELSE 2drop
1403: c-flag @ IF ." #endif" cr THEN
1404: forth-flag @ IF ." [THEN]" cr THEN
1405: THEN }}
1406: )) <- if-comment
1407:
1.98 pazsan 1408: (( (( ` g || ` G )) {{ start }} nonl **
1409: {{ end
1410: forth-flag @ IF ." group " type cr THEN
1411: c-flag @ IF ." GROUP(" type ." )" cr THEN }}
1412: )) <- group-comment
1413:
1414: (( (( eval-comment || forth-comment || c-comment || else-comment || if-comment || group-comment )) ?? nonl ** )) <- comment-body
1.67 anton 1415:
1.79 anton 1416: (( ` \ comment-body nleof )) <- comment ( -- )
1.67 anton 1417:
1418: (( {{ start }} stack-ident {{ end 2 pick init-item item% %size + }} white ** )) **
1419: <- stack-items
1420:
1.69 anton 1421: (( {{ prim prim-effect-in }} stack-items {{ prim prim-effect-in-end ! }}
1.67 anton 1422: ` - ` - white **
1.69 anton 1423: {{ prim prim-effect-out }} stack-items {{ prim prim-effect-out-end ! }}
1.67 anton 1424: )) <- stack-effect ( -- )
1425:
1.71 anton 1426: (( {{ prim create-prim }}
1.69 anton 1427: ` ( white ** {{ start }} stack-effect {{ end prim prim-stack-string 2! }} ` ) white **
1428: (( {{ start }} forth-ident {{ end prim prim-wordset 2! }} white **
1429: (( {{ start }} c-ident {{ end prim prim-c-name 2! }} )) ??
1.79 anton 1430: )) ?? nleof
1431: (( ` " ` " {{ start }} (( noquote ++ ` " )) ++ {{ end 1- prim prim-doc 2! }} ` " white ** nleof )) ??
1.110 anton 1432: {{ skipsynclines off line @ c-line ! filename 2@ c-filename 2! start }}
1433: (( (( ` { nonl ** nleof (( (( nobracenl {{ line @ drop }} nonl ** )) ?? nleof )) ** ` } white ** nleof white ** ))
1434: || (( nocolonnl nonl ** nleof white ** )) ** ))
1435: {{ end prim prim-c-code 2! skipsynclines on }}
1.79 anton 1436: (( ` : white ** nleof
1437: {{ start }} (( nonl ++ nleof white ** )) ++ {{ end prim prim-forth-code 2! }}
1.81 anton 1438: )) ?? {{ process-simple }}
1.79 anton 1439: nleof
1.69 anton 1440: )) <- simple-primitive ( -- )
1441:
1.71 anton 1442: (( {{ init-combined }}
1.89 anton 1443: ` = white ** (( {{ start }} forth-ident {{ end add-prim }} white ** )) ++
1.79 anton 1444: nleof {{ process-combined }}
1.69 anton 1445: )) <- combined-primitive
1446:
1.79 anton 1447: (( {{ make-prim to prim 0 to combined
1.69 anton 1448: line @ name-line ! filename 2@ name-filename 2!
1.82 anton 1449: function-number @ prim prim-num !
1.110 anton 1450: start }} [ifdef] vmgen c-ident [else] forth-ident [then] {{ end
1451: 2dup prim prim-name 2! prim prim-c-name 2! }} white **
1.104 anton 1452: (( ` / white ** {{ start }} c-ident {{ end prim prim-c-name 2! }} white ** )) ??
1.82 anton 1453: (( simple-primitive || combined-primitive )) {{ 1 function-number +! }}
1.67 anton 1454: )) <- primitive ( -- )
1455:
1456: (( (( comment || primitive || nl white ** )) ** eof ))
1457: parser primitives2something
1458: warnings @ [IF]
1459: .( parser generated ok ) cr
1460: [THEN]
1461:
1.95 jwilke 1462:
1.97 jwilke 1463: \ run with gforth-0.5.0 (slurp-file is missing)
1.95 jwilke 1464: [IFUNDEF] slurp-file
1465: : slurp-file ( c-addr1 u1 -- c-addr2 u2 )
1466: \ c-addr1 u1 is the filename, c-addr2 u2 is the file's contents
1467: r/o bin open-file throw >r
1468: r@ file-size throw abort" file too large"
1469: dup allocate throw swap
1470: 2dup r@ read-file throw over <> abort" could not read whole file"
1471: r> close-file throw ;
1472: [THEN]
1473:
1.69 anton 1474: : primfilter ( addr u -- )
1475: \ process the string at addr u
1476: over dup rawinput ! dup line-start ! cookedinput !
1477: + endrawinput !
1478: checksyncline
1479: primitives2something ;
1.8 pazsan 1480:
1.72 anton 1481: : process-file ( addr u xt-simple x-combined -- )
1482: output-combined ! output !
1.61 anton 1483: save-mem 2dup filename 2!
1.69 anton 1484: slurp-file
1.17 anton 1485: warnings @ if
1486: ." ------------ CUT HERE -------------" cr endif
1.69 anton 1487: primfilter ;
1.30 pazsan 1488:
1.72 anton 1489: \ : process ( xt -- )
1490: \ bl word count rot
1491: \ process-file ;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>