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