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