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