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