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