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