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