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