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