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