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