1: \ converts primitives to, e.g., C code
2:
3: \ Copyright (C) 1995,1996,1997,1998,2000,2003,2004,2005,2006 Free Software Foundation, Inc.
4:
5: \ This file is part of Gforth.
6:
7: \ Gforth is free software; you can redistribute it and/or
8: \ modify it under the terms of the GNU General Public License
9: \ as published by the Free Software Foundation; either version 2
10: \ of the License, or (at your option) any later version.
11:
12: \ This program is distributed in the hope that it will be useful,
13: \ but WITHOUT ANY WARRANTY; without even the implied warranty of
14: \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: \ GNU General Public License for more details.
16:
17: \ You should have received a copy of the GNU General Public License
18: \ along with this program; if not, write to the Free Software
19: \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
20:
21:
22: \ This is not very nice (hard limits, no checking, assumes 1 chars = 1).
23: \ And it grew even worse when it aged.
24:
25: \ Optimizations:
26: \ superfluous stores are removed. GCC removes the superfluous loads by itself
27: \ TOS and FTOS can be kept in register( variable)s.
28: \
29: \ Problems:
30: \ The TOS optimization is somewhat hairy. The problems by example:
31: \ 1) dup ( w -- w w ): w=TOS; sp-=1; sp[1]=w; TOS=w;
32: \ The store is not superfluous although the earlier opt. would think so
33: \ Alternatively: sp[0]=TOS; w=TOS; sp-=1; TOS=w;
34: \ 2) ( -- .. ): sp[0] = TOS; ... /* This additional store is necessary */
35: \ 3) ( .. -- ): ... TOS = sp[0]; /* as well as this load */
36: \ 4) ( -- ): /* but here they are unnecessary */
37: \ 5) Words that call NEXT themselves have to be done very carefully.
38: \
39: \ To do:
40: \ add the store optimization for doubles
41: \ regarding problem 1 above: It would be better (for over) to implement
42: \ the alternative
43: \ store optimization for combined instructions.
44:
45: \ Design Uglyness:
46:
47: \ - global state (values, variables) in connection with combined instructions.
48:
49: \ - index computation is different for instruction-stream and the
50: \ stacks; there are two mechanisms for dealing with that
51: \ (stack-in-index-xt and a test for stack==instruction-stream); there
52: \ should be only one.
53:
54:
55: \ for backwards compatibility, jaw
56: require compat/strcomp.fs
57:
58: warnings off
59:
60: \ redefinitions of kernel words not present in gforth-0.6.1
61: : latestxt lastcfa @ ;
62: : latest last @ ;
63:
64: [IFUNDEF] try
65: include startup.fs
66: [THEN]
67:
68: : struct% struct ; \ struct is redefined in gray
69:
70: warnings off
71: \ warnings on
72:
73: include ./gray.fs
74: 128 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 out-filename \ filename of the output file (for sync lines)
90: 0 0 out-filename 2!
91: 2variable f-comment
92: 0 0 f-comment 2!
93: variable skipsynclines \ are sync lines ("#line ...") invisible to the parser?
94: skipsynclines on
95: variable out-nls \ newlines in output (for output sync lines)
96: 0 out-nls !
97: variable store-optimization \ use store optimization?
98: store-optimization off
99:
100: variable include-skipped-insts
101: \ does the threaded code for a combined instruction include the cells
102: \ for the component instructions (true) or only the cells for the
103: \ inline arguments (false)
104: include-skipped-insts off
105:
106: 2variable threaded-code-pointer-type \ type used for geninst etc.
107: s" Inst **" threaded-code-pointer-type 2!
108:
109: variable immarg \ values for immediate arguments (to be used in IMM_ARG macros)
110: $12340000 immarg !
111:
112: : th ( addr1 n -- addr2 )
113: cells + ;
114:
115: : holds ( addr u -- )
116: \ like HOLD, but for a string
117: tuck + swap 0 +do
118: 1- dup c@ hold
119: loop
120: drop ;
121:
122: : insert-wordlist { c-addr u wordlist xt -- }
123: \ adds name "addr u" to wordlist using defining word xt
124: \ xt may cause additional stack effects
125: get-current >r wordlist set-current
126: c-addr u nextname xt execute
127: r> set-current ;
128:
129: : start ( -- addr )
130: cookedinput @ ;
131:
132: : end ( addr -- addr u )
133: cookedinput @ over - ;
134:
135: : print-error-line ( -- )
136: \ print the current line and position
137: line-start @ endrawinput @ over - 2dup nl-char scan drop nip ( start end )
138: over - type cr
139: line-start @ rawinput @ over - typewhite ." ^" cr ;
140:
141: : ?print-error { f addr u -- }
142: f ?not? if
143: stderr >outfile
144: filename 2@ type ." :" line @ 0 .r ." : " addr u type cr
145: print-error-line
146: outfile<
147: 1 (bye) \ abort
148: endif ;
149:
150: : quote ( -- )
151: [char] " emit ;
152:
153: \ count output lines to generate sync lines for output
154:
155: : count-nls ( addr u -- )
156: bounds u+do
157: i c@ nl-char = negate out-nls +!
158: loop ;
159:
160: :noname ( addr u -- )
161: 2dup count-nls
162: defers type ;
163: is type
164:
165: variable output \ xt ( -- ) of output word for simple primitives
166: variable output-combined \ xt ( -- ) of output word for combined primitives
167:
168: struct%
169: cell% field stack-number \ the number of this stack
170: cell% 2* field stack-pointer \ stackpointer name
171: cell% field stack-type \ name for default type of stack items
172: cell% field stack-in-index-xt \ ( in-size item -- in-index )
173: cell% field stack-access-transform \ ( nitem -- index )
174: end-struct stack%
175:
176: struct%
177: cell% 2* field item-name \ name, excluding stack prefixes
178: cell% field item-stack \ descriptor for the stack used, 0 is default
179: cell% field item-type \ descriptor for the item type
180: cell% field item-offset \ offset in stack items, 0 for the deepest element
181: cell% field item-first \ true if this is the first occurence of the item
182: end-struct item%
183:
184: struct%
185: cell% 2* field type-c-name
186: cell% field type-stack \ default stack
187: cell% field type-size \ size of type in stack items
188: cell% field type-fetch \ xt of fetch code generator ( item -- )
189: cell% field type-store \ xt of store code generator ( item -- )
190: end-struct type%
191:
192: struct%
193: cell% field register-number
194: cell% field register-type \ pointer to type
195: cell% 2* field register-name \ c name
196: end-struct register%
197:
198: struct%
199: cell% 2* field ss-registers \ addr u; ss-registers[0] is TOS
200: \ 0 means: use memory
201: cell% field ss-offset \ stack pointer offset: sp[-offset] is TOS
202: end-struct ss% \ stack-state
203:
204: struct%
205: cell% field state-enabled
206: cell% field state-number
207: cell% max-stacks * field state-sss
208: end-struct state%
209:
210: variable next-stack-number 0 next-stack-number !
211: create stacks max-stacks cells allot \ array of stacks
212: 256 constant max-registers
213: create registers max-registers cells allot \ array of registers
214: variable nregisters 0 nregisters ! \ number of registers
215: variable next-state-number 0 next-state-number ! \ next state number
216:
217: : stack-in-index ( in-size item -- in-index )
218: item-offset @ - 1- ;
219:
220: : inst-in-index ( in-size item -- in-index )
221: nip dup item-offset @ swap item-type @ type-size @ + 1- ;
222:
223: : make-stack ( addr-ptr u1 type "stack-name" -- )
224: next-stack-number @ max-stacks < s" too many stacks" ?print-error
225: create stack% %allot >r
226: r@ stacks next-stack-number @ th !
227: next-stack-number @ r@ stack-number !
228: 1 next-stack-number +!
229: r@ stack-type !
230: save-mem r@ stack-pointer 2!
231: ['] stack-in-index r@ stack-in-index-xt !
232: ['] noop r@ stack-access-transform !
233: rdrop ;
234:
235: : map-stacks { xt -- }
236: \ perform xt ( stack -- ) for all stacks
237: next-stack-number @ 0 +do
238: stacks i th @ xt execute
239: loop ;
240:
241: : map-stacks1 { xt -- }
242: \ perform xt ( stack -- ) for all stacks except inst-stream
243: next-stack-number @ 1 +do
244: stacks i th @ xt execute
245: loop ;
246:
247: \ stack items
248:
249: : init-item ( addr u addr1 -- )
250: \ initialize item at addr1 with name addr u
251: \ the stack prefix is removed by the stack-prefix
252: dup item% %size erase
253: item-name 2! ;
254:
255: : map-items { addr end xt -- }
256: \ perform xt for all items in array addr...end
257: end addr ?do
258: i xt execute
259: item% %size +loop ;
260:
261: \ types
262:
263: : print-type-prefix ( type -- )
264: body> >head name>string type ;
265:
266: \ various variables for storing stuff of one primitive
267:
268: struct%
269: cell% 2* field prim-name
270: cell% 2* field prim-wordset
271: cell% 2* field prim-c-name
272: cell% 2* field prim-c-name-orig \ for reprocessed prims, the original name
273: cell% 2* field prim-doc
274: cell% 2* field prim-c-code
275: cell% 2* field prim-forth-code
276: cell% 2* field prim-stack-string
277: cell% field prim-num \ ordinal number
278: cell% field prim-items-wordlist \ unique items
279: item% max-effect * field prim-effect-in
280: item% max-effect * field prim-effect-out
281: cell% field prim-effect-in-end
282: cell% field prim-effect-out-end
283: cell% max-stacks * field prim-stacks-in \ number of in items per stack
284: cell% max-stacks * field prim-stacks-out \ number of out items per stack
285: cell% max-stacks * field prim-stacks-sync \ sync flag per stack
286: end-struct prim%
287:
288: : make-prim ( -- prim )
289: prim% %alloc { p }
290: s" " p prim-doc 2! s" " p prim-forth-code 2! s" " p prim-wordset 2!
291: p ;
292:
293: 0 value prim \ in combined prims either combined or a part
294: 0 value combined \ in combined prims the combined prim
295: variable in-part \ true if processing a part
296: in-part off
297: 0 value state-in \ state on entering prim
298: 0 value state-out \ state on exiting prim
299: 0 value state-default \ canonical state at bb boundaries
300:
301: : prim-context ( ... p xt -- ... )
302: \ execute xt with prim set to p
303: prim >r
304: swap to prim
305: catch
306: r> to prim
307: throw ;
308:
309: : prim-c-name-2! ( c-addr u -- )
310: 2dup prim prim-c-name 2! prim prim-c-name-orig 2! ;
311:
312: 1000 constant max-combined
313: create combined-prims max-combined cells allot
314: variable num-combined
315: variable part-num \ current part number during process-combined
316:
317: : map-combined { xt -- }
318: \ perform xt for all components of the current combined instruction
319: num-combined @ 0 +do
320: combined-prims i th @ xt execute
321: loop ;
322:
323: table constant combinations
324: \ the keys are the sequences of pointers to primitives
325:
326: create current-depth max-stacks cells allot
327: create max-depth max-stacks cells allot
328: create min-depth max-stacks cells allot
329:
330: create sp-update-in max-stacks cells allot
331: \ where max-depth occured the first time
332: create max-depths max-stacks max-combined 1+ * cells allot
333: \ maximum depth at start of each part: array[parts] of array[stack]
334: create max-back-depths max-stacks max-combined 1+ * cells allot
335: \ maximun depth from end of the combination to the start of the each part
336:
337: : s-c-max-depth ( nstack ncomponent -- addr )
338: max-stacks * + cells max-depths + ;
339:
340: : s-c-max-back-depth ( nstack ncomponent -- addr )
341: max-stacks * + cells max-back-depths + ;
342:
343: wordlist constant primitives
344:
345: : create-prim ( prim -- )
346: dup prim-name 2@ primitives ['] constant insert-wordlist ;
347:
348: : stack-in ( stack -- addr )
349: \ address of number of stack items in effect in
350: stack-number @ cells prim prim-stacks-in + ;
351:
352: : stack-out ( stack -- addr )
353: \ address of number of stack items in effect out
354: stack-number @ cells prim prim-stacks-out + ;
355:
356: : stack-prim-stacks-sync ( stack -- addr )
357: prim prim-stacks-sync swap stack-number @ th ;
358:
359: \ global vars
360: variable c-line
361: 2variable c-filename
362: variable name-line
363: 2variable name-filename
364: 2variable last-name-filename
365: Variable function-number 0 function-number !
366: Variable function-old 0 function-old !
367: : function-diff ( n -- )
368: ." GROUPADD(" function-number @ function-old @ - 0 .r ." )" cr
369: function-number @ function-old ! ;
370: : forth-fdiff ( -- )
371: function-number @ function-old @ - 0 .r ." groupadd" cr
372: function-number @ function-old ! ;
373:
374: \ a few more set ops
375:
376: : bit-equivalent ( w1 w2 -- w3 )
377: xor invert ;
378:
379: : complement ( set1 -- set2 )
380: empty ['] bit-equivalent binary-set-operation ;
381:
382: \ forward declaration for inst-stream (breaks cycle in definitions)
383: defer inst-stream-f ( -- stack )
384:
385: \ stack access stuff
386:
387: : normal-stack-access0 { n stack -- }
388: \ n has the ss-offset already applied (see ...-access1)
389: n stack stack-access-transform @ execute ." [" 0 .r ." ]" ;
390:
391: : state-ss { stack state -- ss }
392: state state-sss stack stack-number @ th @ ;
393:
394: : stack-reg { n stack state -- reg }
395: \ n is the index (TOS=0); reg is 0 if the access is to memory
396: stack state state-ss ss-registers 2@ n u> if ( addr ) \ in ss-registers?
397: n th @
398: else
399: drop 0
400: endif ;
401:
402: : .reg ( reg -- )
403: register-name 2@ type ;
404:
405: : stack-offset ( stack state -- n )
406: \ offset for stack in state
407: state-ss ss-offset @ ;
408:
409: : normal-stack-access1 { n stack state -- }
410: n stack state stack-reg ?dup-if
411: .reg exit
412: endif
413: stack stack-pointer 2@ type
414: n stack state stack-offset - stack normal-stack-access0 ;
415:
416: : normal-stack-access ( n stack state -- )
417: over inst-stream-f = if
418: ." IMM_ARG(" normal-stack-access1 ." ," immarg ? ." )"
419: 1 immarg +!
420: else
421: normal-stack-access1
422: endif ;
423:
424: : stack-depth { stack -- n }
425: current-depth stack stack-number @ th @ ;
426:
427: : part-stack-access { n stack -- }
428: \ print _<stack><x>, x=inst-stream? n : maxdepth-currentdepth-n-1
429: ." _" stack stack-pointer 2@ type
430: stack stack-number @ { stack# }
431: stack stack-depth n + { access-depth }
432: stack inst-stream-f = if
433: access-depth
434: else
435: combined prim-stacks-in stack# th @
436: assert( dup max-depth stack# th @ = )
437: access-depth - 1-
438: endif
439: 0 .r ;
440:
441: : part-stack-read { n stack -- }
442: stack stack-depth n + ( ndepth )
443: stack stack-number @ part-num @ s-c-max-depth @
444: \ max-depth stack stack-number @ th @ ( ndepth nmaxdepth )
445: over <= if ( ndepth ) \ load from memory
446: stack state-in normal-stack-access
447: else
448: drop n stack part-stack-access
449: endif ;
450:
451: : stack-diff ( stack -- n )
452: \ in-out
453: dup stack-in @ swap stack-out @ - ;
454:
455: : part-stack-write { n stack -- }
456: stack stack-depth n +
457: stack stack-number @ part-num @ s-c-max-back-depth @
458: over <= if ( ndepth )
459: stack combined ['] stack-diff prim-context -
460: stack state-out normal-stack-access
461: else
462: drop n stack part-stack-access
463: endif ;
464:
465: : stack-read ( n stack -- )
466: \ print a stack access at index n of stack
467: in-part @ if
468: part-stack-read
469: else
470: state-in normal-stack-access
471: endif ;
472:
473: : stack-write ( n stack -- )
474: \ print a stack access at index n of stack
475: in-part @ if
476: part-stack-write
477: else
478: state-out normal-stack-access
479: endif ;
480:
481: : item-in-index { item -- n }
482: \ n is the index of item (in the in-effect)
483: item item-stack @ dup >r stack-in @ ( in-size r:stack )
484: item r> stack-in-index-xt @ execute ;
485:
486: : item-stack-type-name ( item -- addr u )
487: item-stack @ stack-type @ type-c-name 2@ ;
488:
489: : fetch-single ( item -- )
490: \ fetch a single stack item from its stack
491: >r
492: ." vm_" r@ item-stack-type-name type
493: ." 2" r@ item-type @ print-type-prefix ." ("
494: r@ item-in-index r@ item-stack @ stack-read ." ,"
495: r@ item-name 2@ type
496: ." );" cr
497: rdrop ;
498:
499: : fetch-double ( item -- )
500: \ fetch a double stack item from its stack
501: >r
502: ." vm_two"
503: r@ item-stack-type-name type ." 2"
504: r@ item-type @ print-type-prefix ." ("
505: r@ item-in-index r@ item-stack @ 2dup stack-read
506: ." , " -1 under+ stack-read
507: ." , " r@ item-name 2@ type
508: ." )" cr
509: rdrop ;
510:
511: : same-as-in? ( item -- f )
512: \ f is true iff the offset and stack of item is the same as on input
513: >r
514: r@ item-stack @ stack-prim-stacks-sync @ if
515: rdrop false exit
516: endif
517: r@ item-first @ if
518: rdrop false exit
519: endif
520: r@ item-name 2@ prim prim-items-wordlist @ search-wordlist 0= abort" bug"
521: execute @
522: dup r@ =
523: if \ item first appeared in output
524: drop false
525: else
526: dup item-stack @ r@ item-stack @ =
527: swap item-offset @ r@ item-offset @ = and
528: endif
529: rdrop ;
530:
531: : item-out-index ( item -- n )
532: \ n is the index of item (in the out-effect)
533: >r r@ item-stack @ stack-out @ r> item-offset @ - 1- ;
534:
535: : really-store-single ( item -- )
536: >r
537: ." vm_"
538: r@ item-type @ print-type-prefix ." 2"
539: r@ item-stack-type-name type ." ("
540: r@ item-name 2@ type ." ,"
541: r@ item-out-index r@ item-stack @ stack-write ." );"
542: rdrop ;
543:
544: : store-single { item -- }
545: item item-stack @ { stack }
546: store-optimization @ in-part @ 0= and item same-as-in? and
547: item item-in-index stack state-in stack-reg \ in reg/mem
548: item item-out-index stack state-out stack-reg = and \ out reg/mem
549: 0= if
550: item really-store-single cr
551: endif ;
552:
553: : store-double ( item -- )
554: \ !! store optimization is not performed, because it is not yet needed
555: >r
556: ." vm_"
557: r@ item-type @ print-type-prefix ." 2two"
558: r@ item-stack-type-name type ." ("
559: r@ item-name 2@ type ." , "
560: r@ item-out-index r@ item-stack @ 2dup stack-write
561: ." , " -1 under+ stack-write
562: ." )" cr
563: rdrop ;
564:
565: : single ( -- xt1 xt2 n )
566: ['] fetch-single ['] store-single 1 ;
567:
568: : double ( -- xt1 xt2 n )
569: ['] fetch-double ['] store-double 2 ;
570:
571: : s, ( addr u -- )
572: \ allocate a string
573: here swap dup allot move ;
574:
575: wordlist constant prefixes
576:
577: : declare ( addr "name" -- )
578: \ remember that there is a stack item at addr called name
579: create , ;
580:
581: : !default ( w addr -- )
582: dup @ if
583: 2drop \ leave nonzero alone
584: else
585: !
586: endif ;
587:
588: : create-type { addr u xt1 xt2 n stack -- } ( "prefix" -- )
589: \ describes a type
590: \ addr u specifies the C type name
591: \ stack effect entries of the type start with prefix
592: create type% %allot >r
593: addr u save-mem r@ type-c-name 2!
594: xt1 r@ type-fetch !
595: xt2 r@ type-store !
596: n r@ type-size !
597: stack r@ type-stack !
598: rdrop ;
599:
600: : type-prefix ( addr u xt1 xt2 n stack "prefix" -- )
601: get-current >r prefixes set-current
602: create-type r> set-current
603: does> ( item -- )
604: \ initialize item
605: { item typ }
606: typ item item-type !
607: typ type-stack @ item item-stack !default
608: item item-name 2@ prim prim-items-wordlist @ search-wordlist 0= if
609: item item-name 2@ nextname item declare
610: item item-first on
611: \ typ type-c-name 2@ type space type ." ;" cr
612: else
613: drop
614: item item-first off
615: endif ;
616:
617: : execute-prefix ( item addr1 u1 -- )
618: \ execute the word ( item -- ) associated with the longest prefix
619: \ of addr1 u1
620: 0 swap ?do
621: dup i prefixes search-wordlist
622: if \ ok, we have the type ( item addr1 xt )
623: nip execute
624: UNLOOP EXIT
625: endif
626: -1 s+loop
627: \ we did not find a type, abort
628: abort
629: false s" unknown prefix" ?print-error ;
630:
631: : declaration ( item -- )
632: dup item-name 2@ execute-prefix ;
633:
634: : declaration-list ( addr1 addr2 -- )
635: ['] declaration map-items ;
636:
637: : declarations ( -- )
638: wordlist dup prim prim-items-wordlist ! set-current
639: prim prim-effect-in prim prim-effect-in-end @ declaration-list
640: prim prim-effect-out prim prim-effect-out-end @ declaration-list ;
641:
642: : print-declaration { item -- }
643: item item-first @ if
644: item item-type @ type-c-name 2@ type space
645: item item-name 2@ type ." ;" cr
646: endif ;
647:
648: : print-declarations ( -- )
649: prim prim-effect-in prim prim-effect-in-end @ ['] print-declaration map-items
650: prim prim-effect-out prim prim-effect-out-end @ ['] print-declaration map-items ;
651:
652: : stack-prefix ( stack "prefix" -- )
653: get-current >r prefixes set-current
654: name tuck nextname create ( stack length ) 2,
655: r> set-current
656: does> ( item -- )
657: 2@ { item stack prefix-length }
658: item item-name 2@ prefix-length /string item item-name 2!
659: stack item item-stack !
660: item declaration ;
661:
662: : set-prim-stacks-sync ( stack -- )
663: stack-prim-stacks-sync on ;
664:
665: : clear-prim-stacks-sync ( stack -- )
666: stack-prim-stacks-sync off ;
667:
668:
669: get-current prefixes set-current
670: : ... ( item -- )
671: \ this "prefix" ensures that the appropriate stack is synced with memory
672: dup item-name 2@ s" ..." str= 0= abort" '...' must end the item name"
673: item-stack @ dup if
674: set-prim-stacks-sync
675: else \ prefixless "..." syncs all stacks
676: drop ['] set-prim-stacks-sync map-stacks1
677: endif ;
678: set-current
679:
680: create ...-item ( -- addr ) \ just used for letting stack-prefixes work on it
681: item% %allot \ stores the stack temporarily until used by ...
682:
683: : init-item1 ( addr1 addr u -- addr2 )
684: \ initialize item at addr1 with name addr u, next item is at addr2
685: \ !! make sure that any mention of "..." is only stack-prefixed
686: 2dup s" ..." search nip nip if ( addr1 addr u )
687: 0 ...-item item-stack ! \ initialize to prefixless
688: 2dup ...-item item-name 2!
689: ...-item rot rot execute-prefix ( addr1 )
690: else
691: 2 pick init-item item% %size +
692: endif ;
693:
694: \ types pointed to by stacks for use in combined prims
695: \ !! output-c-combined shouldn't use these names!
696: : stack-type-name ( addr u "name" -- )
697: single 0 create-type ;
698:
699: wordlist constant type-names \ this is here just to meet the requirement
700: \ that a type be a word; it is never used for lookup
701:
702: : define-type ( addr u -- xt )
703: \ define single type with name addr u, without stack
704: get-current type-names set-current >r
705: 2dup nextname stack-type-name
706: r> set-current
707: latestxt ;
708:
709: : stack ( "name" "stack-pointer" "type" -- )
710: \ define stack
711: name { d: stack-name }
712: name { d: stack-pointer }
713: name { d: stack-type }
714: stack-type define-type
715: stack-pointer rot >body stack-name nextname make-stack ;
716:
717: stack inst-stream IP Cell
718: ' inst-in-index inst-stream stack-in-index-xt !
719: ' inst-stream <is> inst-stream-f
720: \ !! initialize stack-in and stack-out
721:
722: \ registers
723:
724: : make-register ( type addr u -- )
725: \ define register with type TYPE and name ADDR U.
726: nregisters @ max-registers < s" too many registers" ?print-error
727: 2dup nextname create register% %allot >r
728: r@ register-name 2!
729: r@ register-type !
730: nregisters @ r@ register-number !
731: 1 nregisters +!
732: rdrop ;
733:
734: : register ( "name" "type" -- )
735: \ define register
736: name { d: reg-name }
737: name { d: reg-type }
738: reg-type define-type >body
739: reg-name make-register ;
740:
741: \ stack-states
742:
743: : stack-state ( a-addr u uoffset "name" -- )
744: create ss% %allot >r
745: r@ ss-offset !
746: r@ ss-registers 2!
747: rdrop ;
748:
749: 0 0 0 stack-state default-ss
750:
751: \ state
752:
753: : state ( "name" -- )
754: \ create a state initialized with default-sss
755: create state% %allot { s }
756: s state-enabled on
757: next-state-number @ s state-number ! 1 next-state-number +!
758: max-stacks 0 ?do
759: default-ss s state-sss i th !
760: loop ;
761:
762: : state-disable ( state -- )
763: state-enabled off ;
764:
765: : state-enabled? ( state -- f )
766: state-enabled @ ;
767:
768: : .state ( state -- )
769: 0 >body - >name .name ;
770:
771: : set-ss ( ss stack state -- )
772: state-sss swap stack-number @ th ! ;
773:
774: \ offset computation
775: \ the leftmost (i.e. deepest) item has offset 0
776: \ the rightmost item has the highest offset
777:
778: : compute-offset { item xt -- }
779: \ xt specifies in/out; update stack-in/out and set item-offset
780: item item-type @ type-size @
781: item item-stack @ xt execute dup @ >r +!
782: r> item item-offset ! ;
783:
784: : compute-offset-in ( addr1 addr2 -- )
785: ['] stack-in compute-offset ;
786:
787: : compute-offset-out ( addr1 addr2 -- )
788: ['] stack-out compute-offset ;
789:
790: : compute-offsets ( -- )
791: prim prim-stacks-in max-stacks cells erase
792: prim prim-stacks-out max-stacks cells erase
793: prim prim-effect-in prim prim-effect-in-end @ ['] compute-offset-in map-items
794: prim prim-effect-out prim prim-effect-out-end @ ['] compute-offset-out map-items
795: inst-stream stack-out @ 0= s" # can only be on the input side" ?print-error ;
796:
797: : init-simple { prim -- }
798: \ much of the initialization is elsewhere
799: ['] clear-prim-stacks-sync map-stacks ;
800:
801: : process-simple ( -- )
802: prim prim { W^ key } key cell
803: combinations ['] constant insert-wordlist
804: declarations compute-offsets
805: output @ execute ;
806:
807: : stack-state-items ( stack state -- n )
808: state-ss ss-registers 2@ nip ;
809:
810: : unused-stack-items { stack -- n-in n-out }
811: \ n-in are the stack items in state-in not used by prim
812: \ n-out are the stack items in state-out not written by prim
813: stack state-in stack-state-items stack stack-in @ - 0 max
814: stack state-out stack-state-items stack stack-out @ - 0 max ;
815:
816: : spill-stack-items { stack -- u }
817: \ there are u items to spill in stack
818: stack unused-stack-items
819: stack stack-prim-stacks-sync @ if
820: drop 0
821: endif
822: swap - ;
823:
824: : spill-stack { stack -- }
825: \ spill regs of state-in that are not used by prim and are not in state-out
826: stack state-in stack-offset { offset }
827: stack state-in stack-state-items ( items )
828: dup stack spill-stack-items + +do
829: \ loop through the bottom items
830: stack stack-pointer 2@ type
831: i offset - stack normal-stack-access0 ." = "
832: i stack state-in normal-stack-access1 ." ;" cr
833: loop ;
834:
835: : spill-state ( -- )
836: ['] spill-stack map-stacks1 ;
837:
838: : fill-stack-items { stack -- u }
839: \ there are u items to fill in stack
840: stack unused-stack-items
841: stack stack-prim-stacks-sync @ if
842: swap drop 0 swap
843: endif
844: - ;
845:
846: : fill-stack { stack -- }
847: stack state-out stack-offset { offset }
848: stack state-out stack-state-items ( items )
849: dup stack fill-stack-items + +do
850: \ loop through the bottom items
851: i stack state-out normal-stack-access1 ." = "
852: stack stack-pointer 2@ type
853: i offset - stack normal-stack-access0 ." ;" cr
854: loop ;
855:
856: : fill-state ( -- )
857: \ !! inst-stream for prefetching?
858: ['] fill-stack map-stacks1 ;
859:
860: : fetch ( addr -- )
861: dup item-type @ type-fetch @ execute ;
862:
863: : fetches ( -- )
864: prim prim-effect-in prim prim-effect-in-end @ ['] fetch map-items ;
865:
866: : reg-reg-move ( reg-from reg-to -- )
867: 2dup = if
868: 2drop
869: else
870: .reg ." = " .reg ." ;" cr
871: endif ;
872:
873: : stack-bottom-reg { n stack state -- reg }
874: stack state stack-state-items n - 1- stack state stack-reg ;
875:
876: : stack-moves { stack -- }
877: \ generate moves between registers in state-in/state-out that are
878: \ not spilled or consumed/produced by prim.
879: \ !! this works only for a simple stack cache, not e.g., for
880: \ rotating stack caches, or registers shared between stacks (the
881: \ latter would also require a change in interface)
882: \ !! maybe place this after NEXT_P1?
883: stack unused-stack-items 2dup < if ( n-in n-out )
884: \ move registers from 0..n_in-1 to n_out-n_in..n_out-1
885: over - { diff } ( n-in )
886: -1 swap 1- -do
887: i stack state-in stack-bottom-reg ( reg-from )
888: i diff + stack state-out stack-bottom-reg reg-reg-move
889: 1 -loop
890: else
891: \ move registers from n_in-n_out..n_in-1 to 0..n_out-1
892: swap over - { diff } ( n-out )
893: 0 +do
894: i diff + stack state-in stack-bottom-reg ( reg-from )
895: i stack state-out stack-bottom-reg reg-reg-move
896: loop
897: endif ;
898:
899: : stack-update-transform ( n1 stack -- n2 )
900: \ n2 is the number by which the stack pointer should be
901: \ incremented to pop n1 items
902: stack-access-transform @ dup >r execute
903: 0 r> execute - ;
904:
905: : update-stack-pointer { stack n -- }
906: n if \ this check is not necessary, gcc would do this for us
907: stack inst-stream = if
908: ." INC_IP(" n 0 .r ." );" cr
909: else
910: stack stack-pointer 2@ type ." += "
911: n stack stack-update-transform 0 .r ." ;" cr
912: endif
913: endif ;
914:
915: : stack-pointer-update { stack -- }
916: \ and moves
917: \ stacks grow downwards
918: stack stack-prim-stacks-sync @ if
919: stack stack-in @
920: stack state-in stack-offset -
921: stack swap update-stack-pointer
922: else
923: stack stack-diff ( in-out )
924: stack state-in stack-offset -
925: stack state-out stack-offset + ( [in-in_offset]-[out-out_offset] )
926: stack swap update-stack-pointer
927: stack stack-moves
928: endif ;
929:
930: : stack-pointer-updates ( -- )
931: ['] stack-pointer-update map-stacks ;
932:
933: : stack-pointer-update2 { stack -- }
934: stack stack-prim-stacks-sync @ if
935: stack state-out stack-offset
936: stack stack-out @ -
937: stack swap update-stack-pointer
938: endif ;
939:
940: : stack-pointer-updates2 ( -- )
941: \ update stack pointers after C code, where necessary
942: ['] stack-pointer-update2 map-stacks ;
943:
944: : store ( item -- )
945: \ f is true if the item should be stored
946: \ f is false if the store is probably not necessary
947: dup item-type @ type-store @ execute ;
948:
949: : stores ( -- )
950: prim prim-effect-out prim prim-effect-out-end @ ['] store map-items ;
951:
952: : print-debug-arg { item -- }
953: ." fputs(" quote space item item-name 2@ type ." =" quote ." , vm_out); "
954: ." printarg_" item item-type @ print-type-prefix
955: ." (" item item-name 2@ type ." );" cr ;
956:
957: : print-debug-args ( -- )
958: ." #ifdef VM_DEBUG" cr
959: ." if (vm_debug) {" cr
960: prim prim-effect-in prim prim-effect-in-end @ ['] print-debug-arg map-items
961: \ ." fputc('\n', vm_out);" cr
962: ." }" cr
963: ." #endif" cr ;
964:
965: : print-debug-result { item -- }
966: item item-first @ if
967: item print-debug-arg
968: endif ;
969:
970: : print-debug-results ( -- )
971: cr
972: ." #ifdef VM_DEBUG" cr
973: ." if (vm_debug) {" cr
974: ." fputs(" quote ." -- " quote ." , vm_out); "
975: prim prim-effect-out prim prim-effect-out-end @ ['] print-debug-result map-items
976: ." fputc('\n', vm_out);" cr
977: ." }" cr
978: ." #endif" cr ;
979:
980: : output-super-end ( -- )
981: prim prim-c-code 2@ s" SET_IP" search if
982: ." SUPER_END;" cr
983: endif
984: 2drop ;
985:
986:
987: defer output-nextp0
988: :noname ( -- )
989: ." NEXT_P0;" cr ;
990: is output-nextp0
991:
992: defer output-nextp1
993: :noname ( -- )
994: ." NEXT_P1;" cr ;
995: is output-nextp1
996:
997: : output-nextp2 ( -- )
998: ." NEXT_P2;" cr ;
999:
1000: variable tail-nextp2 \ xt to execute for printing NEXT_P2 in INST_TAIL
1001: ' output-nextp2 tail-nextp2 !
1002:
1003: : output-label2 ( -- )
1004: ." LABEL2(" prim prim-c-name 2@ type ." )" cr
1005: ." NEXT_P1_5;" cr
1006: ." LABEL3(" prim prim-c-name 2@ type ." )" cr
1007: ." DO_GOTO;" cr ;
1008:
1009: : output-c-tail1 { xt -- }
1010: \ the final part of the generated C code, with xt printing LABEL2 or not.
1011: output-super-end
1012: print-debug-results
1013: output-nextp1
1014: stack-pointer-updates2
1015: stores
1016: fill-state
1017: xt execute ;
1018:
1019: : output-c-vm-jump-tail ( -- )
1020: \ !! this functionality not yet implemented for superinstructions
1021: output-super-end
1022: print-debug-results
1023: stores
1024: fill-state
1025: ." LABEL2(" prim prim-c-name 2@ type ." )" cr
1026: ." LABEL3(" prim prim-c-name 2@ type ." )" cr
1027: ." DO_GOTO;" cr ;
1028:
1029: : output-c-tail1-no-stores { xt -- }
1030: \ the final part of the generated C code for combinations
1031: output-super-end
1032: output-nextp1
1033: fill-state
1034: xt execute ;
1035:
1036: : output-c-tail ( -- )
1037: tail-nextp2 @ output-c-tail1 ;
1038:
1039: : output-c-tail2 ( -- )
1040: prim prim-c-code 2@ s" VM_JUMP(" search nip nip if
1041: output-c-vm-jump-tail
1042: else
1043: ['] output-label2 output-c-tail1
1044: endif ;
1045:
1046: : output-c-tail-no-stores ( -- )
1047: tail-nextp2 @ output-c-tail1-no-stores ;
1048:
1049: : output-c-tail2-no-stores ( -- )
1050: ['] output-label2 output-c-tail1-no-stores ;
1051:
1052: : type-c-code ( c-addr u xt -- )
1053: \ like TYPE, but replaces "INST_TAIL;" with tail code produced by xt
1054: { xt }
1055: ." {" cr
1056: ." #line " c-line @ . quote c-filename 2@ type quote cr
1057: begin ( c-addr1 u1 )
1058: 2dup s" INST_TAIL;" search
1059: while ( c-addr1 u1 c-addr3 u3 )
1060: 2dup 2>r drop nip over - type
1061: xt execute
1062: 2r> 10 /string
1063: \ !! resync #line missing
1064: repeat
1065: 2drop type
1066: ." #line " out-nls @ 2 + . quote out-filename 2@ type quote cr
1067: ." }" cr ;
1068:
1069: : print-entry ( -- )
1070: ." LABEL(" prim prim-c-name 2@ type ." )" ;
1071:
1072: : prim-type ( addr u -- )
1073: \ print out a primitive, but avoid "*/"
1074: 2dup s" */" search nip nip IF
1075: bounds ?DO I c@ dup '* = IF drop 'x THEN emit LOOP
1076: ELSE type THEN ;
1077:
1078: : output-c ( -- )
1079: print-entry ." /* " prim prim-name 2@ prim-type
1080: ." ( " prim prim-stack-string 2@ type ." ) "
1081: state-in .state ." -- " state-out .state ." */" cr
1082: ." /* " prim prim-doc 2@ type ." */" cr
1083: ." NAME(" quote prim prim-name 2@ type quote ." )" cr \ debugging
1084: ." {" cr
1085: ." DEF_CA" cr
1086: print-declarations
1087: output-nextp0
1088: spill-state
1089: fetches
1090: print-debug-args
1091: stack-pointer-updates
1092: prim prim-c-code 2@ ['] output-c-tail type-c-code
1093: output-c-tail2
1094: ." }" cr
1095: cr
1096: ;
1097:
1098: : disasm-arg { item -- }
1099: item item-stack @ inst-stream = if
1100: ." {" cr
1101: item print-declaration
1102: item fetch
1103: item print-debug-arg
1104: ." }" cr
1105: endif ;
1106:
1107: : disasm-args ( -- )
1108: prim prim-effect-in prim prim-effect-in-end @ ['] disasm-arg map-items ;
1109:
1110: : output-disasm ( -- )
1111: \ generate code for disassembling VM instructions
1112: ." if (VM_IS_INST(*ip, " function-number @ 0 .r ." )) {" cr
1113: ." fputs(" quote prim prim-name 2@ type quote ." , vm_out);" cr
1114: disasm-args
1115: ." ip += " inst-stream stack-in @ 1+ 0 .r ." ;" cr
1116: ." goto _endif_;" cr
1117: ." }" cr ;
1118:
1119: : output-profile ( -- )
1120: \ generate code for postprocessing the VM block profile stuff
1121: ." if (VM_IS_INST(*ip, " function-number @ 0 .r ." )) {" cr
1122: ." add_inst(b, " quote prim prim-name 2@ type quote ." );" cr
1123: ." ip += " inst-stream stack-in @ 1+ 0 .r ." ;" cr
1124: prim prim-c-code 2@ s" SET_IP" search nip nip
1125: prim prim-c-code 2@ s" SUPER_END" search nip nip or if
1126: ." return;" cr
1127: else
1128: ." goto _endif_;" cr
1129: endif
1130: ." }" cr ;
1131:
1132: : output-profile-part ( p )
1133: ." add_inst(b, " quote
1134: prim-name 2@ type
1135: quote ." );" cr ;
1136:
1137: : output-profile-combined ( -- )
1138: \ generate code for postprocessing the VM block profile stuff
1139: ." if (VM_IS_INST(*ip, " function-number @ 0 .r ." )) {" cr
1140: ['] output-profile-part map-combined
1141: ." ip += " inst-stream stack-in @ 1+ 0 .r ." ;" cr
1142: combined-prims num-combined @ 1- th @ prim-c-code 2@ s" SET_IP" search nip nip
1143: combined-prims num-combined @ 1- th @ prim-c-code 2@ s" SUPER_END" search nip nip or if
1144: ." return;" cr
1145: else
1146: ." goto _endif_;" cr
1147: endif
1148: ." }" cr ;
1149:
1150: : prim-branch? { prim -- f }
1151: \ true if prim is a branch or super-end
1152: prim prim-c-code 2@ s" SET_IP" search nip nip 0<> ;
1153:
1154: : output-superend ( -- )
1155: \ output flag specifying whether the current word ends a dynamic superinst
1156: prim prim-branch?
1157: prim prim-c-code 2@ s" SUPER_END" search nip nip 0<> or
1158: prim prim-c-code 2@ s" SUPER_CONTINUE" search nip nip 0= and
1159: negate 0 .r ." , /* " prim prim-name 2@ prim-type ." */" cr ;
1160:
1161: : gen-arg-parm { item -- }
1162: item item-stack @ inst-stream = if
1163: ." , " item item-type @ type-c-name 2@ type space
1164: item item-name 2@ type
1165: endif ;
1166:
1167: : gen-args-parm ( -- )
1168: prim prim-effect-in prim prim-effect-in-end @ ['] gen-arg-parm map-items ;
1169:
1170: : gen-arg-gen { item -- }
1171: item item-stack @ inst-stream = if
1172: ." genarg_" item item-type @ print-type-prefix
1173: ." (ctp, " item item-name 2@ type ." );" cr
1174: endif ;
1175:
1176: : gen-args-gen ( -- )
1177: prim prim-effect-in prim prim-effect-in-end @ ['] gen-arg-gen map-items ;
1178:
1179: : output-gen ( -- )
1180: \ generate C code for generating VM instructions
1181: ." void gen_" prim prim-c-name 2@ type ." ("
1182: threaded-code-pointer-type 2@ type ." ctp" gen-args-parm ." )" cr
1183: ." {" cr
1184: ." gen_inst(ctp, " function-number @ 0 .r ." );" cr
1185: gen-args-gen
1186: ." }" cr ;
1187:
1188: : stack-used? { stack -- f }
1189: stack stack-in @ stack stack-out @ or 0<> ;
1190:
1191: : output-funclabel ( -- )
1192: ." &I_" prim prim-c-name 2@ type ." ," cr ;
1193:
1194: : output-forthname ( -- )
1195: '" emit prim prim-name 2@ type '" emit ." ," cr ;
1196:
1197: \ : output-c-func ( -- )
1198: \ \ used for word libraries
1199: \ ." Cell * I_" prim prim-c-name 2@ type ." (Cell *SP, Cell **FP) /* " prim prim-name 2@ type
1200: \ ." ( " prim prim-stack-string 2@ type ." ) */" cr
1201: \ ." /* " prim prim-doc 2@ type ." */" cr
1202: \ ." NAME(" quote prim prim-name 2@ type quote ." )" cr
1203: \ \ debugging
1204: \ ." {" cr
1205: \ print-declarations
1206: \ \ !! don't know what to do about that
1207: \ inst-stream stack-used? IF ." Cell *ip=IP;" cr THEN
1208: \ data-stack stack-used? IF ." Cell *sp=SP;" cr THEN
1209: \ fp-stack stack-used? IF ." Cell *fp=*FP;" cr THEN
1210: \ return-stack stack-used? IF ." Cell *rp=*RP;" cr THEN
1211: \ spill-state
1212: \ fetches
1213: \ stack-pointer-updates
1214: \ fp-stack stack-used? IF ." *FP=fp;" cr THEN
1215: \ ." {" cr
1216: \ ." #line " c-line @ . quote c-filename 2@ type quote cr
1217: \ prim prim-c-code 2@ type
1218: \ ." }" cr
1219: \ stores
1220: \ fill-state
1221: \ ." return (sp);" cr
1222: \ ." }" cr
1223: \ cr ;
1224:
1225: : output-label ( -- )
1226: ." INST_ADDR(" prim prim-c-name 2@ type ." )," cr ;
1227:
1228: : output-alias ( -- )
1229: ( primitive-number @ . ." alias " ) ." Primitive " prim prim-name 2@ type cr ;
1230:
1231: defer output-c-prim-num ( -- )
1232:
1233: :noname ( -- )
1234: ." N_" prim prim-c-name 2@ type ." ," cr ;
1235: is output-c-prim-num
1236:
1237: : output-forth ( -- )
1238: prim prim-forth-code @ 0=
1239: IF \ output-alias
1240: \ this is bad for ec: an alias is compiled if tho word does not exist!
1241: \ JAW
1242: ELSE ." : " prim prim-name 2@ type ." ( "
1243: prim prim-stack-string 2@ type ." )" cr
1244: prim prim-forth-code 2@ type cr
1245: THEN ;
1246:
1247: : output-tag-file ( -- )
1248: name-filename 2@ last-name-filename 2@ compare if
1249: name-filename 2@ last-name-filename 2!
1250: #ff emit cr
1251: name-filename 2@ type
1252: ." ,0" cr
1253: endif ;
1254:
1255: : output-tag ( -- )
1256: output-tag-file
1257: prim prim-name 2@ 1+ type
1258: 127 emit
1259: space prim prim-name 2@ type space
1260: 1 emit
1261: name-line @ 0 .r
1262: ." ,0" cr ;
1263:
1264: : output-vi-tag ( -- )
1265: name-filename 2@ type #tab emit
1266: prim prim-name 2@ type #tab emit
1267: ." /^" prim prim-name 2@ type ." *(/" cr ;
1268:
1269: [IFDEF] documentation
1270: : register-doc ( -- )
1271: prim prim-name 2@ documentation ['] create insert-wordlist
1272: prim prim-name 2@ 2,
1273: prim prim-stack-string 2@ condition-stack-effect 2,
1274: prim prim-wordset 2@ 2,
1275: prim prim-c-name 2@ condition-pronounciation 2,
1276: prim prim-doc 2@ 2, ;
1277: [THEN]
1278:
1279:
1280: \ combining instructions
1281:
1282: \ The input should look like this:
1283:
1284: \ lit_+ = lit +
1285:
1286: \ The output should look like this:
1287:
1288: \ I_lit_+:
1289: \ {
1290: \ DEF_CA
1291: \ Cell _x_ip0;
1292: \ Cell _x_sp0;
1293: \ Cell _x_sp1;
1294: \ NEXT_P0;
1295: \ _x_ip0 = (Cell) IPTOS;
1296: \ _x_sp0 = (Cell) spTOS;
1297: \ INC_IP(1);
1298: \ /* sp += 0; */
1299: \ /* lit ( #w -- w ) */
1300: \ /* */
1301: \ NAME("lit")
1302: \ {
1303: \ Cell w;
1304: \ w = (Cell) _x_ip0;
1305: \ #ifdef VM_DEBUG
1306: \ if (vm_debug) {
1307: \ fputs(" w=", vm_out); printarg_w (w);
1308: \ fputc('\n', vm_out);
1309: \ }
1310: \ #endif
1311: \ {
1312: \ #line 136 "./prim"
1313: \ }
1314: \ _x_sp1 = (Cell)w;
1315: \ }
1316: \ I_plus: /* + ( n1 n2 -- n ) */
1317: \ /* */
1318: \ NAME("+")
1319: \ {
1320: \ DEF_CA
1321: \ Cell n1;
1322: \ Cell n2;
1323: \ Cell n;
1324: \ NEXT_P0;
1325: \ n1 = (Cell) _x_sp0;
1326: \ n2 = (Cell) _x_sp1;
1327: \ #ifdef VM_DEBUG
1328: \ if (vm_debug) {
1329: \ fputs(" n1=", vm_out); printarg_n (n1);
1330: \ fputs(" n2=", vm_out); printarg_n (n2);
1331: \ fputc('\n', vm_out);
1332: \ }
1333: \ #endif
1334: \ {
1335: \ #line 516 "./prim"
1336: \ n = n1+n2;
1337: \ }
1338: \ _x_sp0 = (Cell)n;
1339: \ }
1340: \ NEXT_P1;
1341: \ spTOS = (Cell)_x_sp0;
1342: \ NEXT_P2;
1343:
1344: : init-combined ( -- )
1345: prim to combined
1346: 0 num-combined !
1347: current-depth max-stacks cells erase
1348: include-skipped-insts @ current-depth 0 th !
1349: max-depth max-stacks cells erase
1350: min-depth max-stacks cells erase
1351: prim prim-effect-in prim prim-effect-in-end !
1352: prim prim-effect-out prim prim-effect-out-end ! ;
1353:
1354: : max! ( n addr -- )
1355: tuck @ max swap ! ;
1356:
1357: : min! ( n addr -- )
1358: tuck @ min swap ! ;
1359:
1360: : inst-stream-adjustment ( nstack -- n )
1361: \ number of stack items to add for each part
1362: 0= include-skipped-insts @ and negate ;
1363:
1364: : add-depths { p -- }
1365: \ combine stack effect of p with *-depths
1366: max-stacks 0 ?do
1367: current-depth i th @
1368: p prim-stacks-in i th @ + i inst-stream-adjustment +
1369: dup max-depth i th max!
1370: p prim-stacks-out i th @ -
1371: dup min-depth i th min!
1372: current-depth i th !
1373: loop ;
1374:
1375: : copy-maxdepths ( n -- )
1376: max-depth max-depths rot max-stacks * th max-stacks cells move ;
1377:
1378: : add-prim ( addr u -- )
1379: \ add primitive given by "addr u" to combined-prims
1380: primitives search-wordlist s" unknown primitive" ?print-error
1381: execute { p }
1382: p combined-prims num-combined @ th !
1383: num-combined @ copy-maxdepths
1384: 1 num-combined +!
1385: p add-depths
1386: num-combined @ copy-maxdepths ;
1387:
1388: : compute-effects { q -- }
1389: \ compute the stack effects of q from the depths
1390: max-stacks 0 ?do
1391: max-depth i th @ dup
1392: q prim-stacks-in i th !
1393: current-depth i th @ -
1394: q prim-stacks-out i th !
1395: loop ;
1396:
1397: : make-effect-items { stack# items effect-endp -- }
1398: \ effect-endp points to a pointer to the end of the current item-array
1399: \ and has to be updated
1400: stacks stack# th @ { stack }
1401: items 0 +do
1402: effect-endp @ { item }
1403: i 0 <# #s stack stack-pointer 2@ holds [char] _ hold #> save-mem
1404: item item-name 2!
1405: stack item item-stack !
1406: stack stack-type @ item item-type !
1407: i item item-offset !
1408: item item-first on
1409: item% %size effect-endp +!
1410: loop ;
1411:
1412: : init-effects { q -- }
1413: \ initialize effects field for FETCHES and STORES
1414: max-stacks 0 ?do
1415: i q prim-stacks-in i th @ q prim-effect-in-end make-effect-items
1416: i q prim-stacks-out i th @ q prim-effect-out-end make-effect-items
1417: loop ;
1418:
1419: : compute-stack-max-back-depths ( stack -- )
1420: stack-number @ { stack# }
1421: current-depth stack# th @ dup
1422: dup stack# num-combined @ s-c-max-back-depth !
1423: -1 num-combined @ 1- -do ( max-depth current-depth )
1424: combined-prims i th @ { p }
1425: p prim-stacks-out stack# th @ +
1426: dup >r max r>
1427: over stack# i s-c-max-back-depth !
1428: p prim-stacks-in stack# th @ -
1429: stack# inst-stream-adjustment -
1430: 1 -loop
1431: assert( dup stack# inst-stream-adjustment negate = )
1432: assert( over max-depth stack# th @ = )
1433: 2drop ;
1434:
1435: : compute-max-back-depths ( -- )
1436: \ compute max-back-depths.
1437: \ assumes that current-depths is correct for the end of the combination
1438: ['] compute-stack-max-back-depths map-stacks ;
1439:
1440: : process-combined ( -- )
1441: combined combined-prims num-combined @ cells
1442: combinations ['] constant insert-wordlist
1443: combined-prims num-combined @ 1- th ( last-part )
1444: @ prim-c-code 2@ prim prim-c-code 2! \ used by output-super-end
1445: prim compute-effects
1446: prim init-effects
1447: compute-max-back-depths
1448: output-combined perform ;
1449:
1450: \ reprocessing (typically to generate versions for another cache states)
1451: \ !! use prim-context
1452:
1453: variable reprocessed-num 0 reprocessed-num !
1454:
1455: : new-name ( -- c-addr u )
1456: reprocessed-num @ 0
1457: 1 reprocessed-num +!
1458: <# #s 'p hold '_ hold #> save-mem ;
1459:
1460: : reprocess-simple ( prim -- )
1461: to prim
1462: new-name prim prim-c-name 2!
1463: output @ execute ;
1464:
1465: : lookup-prim ( c-addr u -- prim )
1466: primitives search-wordlist 0= -13 and throw execute ;
1467:
1468: : state-prim1 { in-state out-state prim -- }
1469: in-state out-state state-default dup d= ?EXIT
1470: in-state state-enabled? out-state state-enabled? and 0= ?EXIT
1471: in-state to state-in
1472: out-state to state-out
1473: prim reprocess-simple ;
1474:
1475: : state-prim ( in-state out-state "name" -- )
1476: parse-word lookup-prim state-prim1 ;
1477:
1478: \ reprocessing with default states
1479:
1480: \ This is a simple scheme and should be generalized
1481: \ assumes we only cache one stack and use simple states for that
1482:
1483: 0 value cache-stack \ stack that we cache
1484: 2variable cache-states \ states of the cache, starting with the empty state
1485:
1486: : compute-default-state-out ( n-in -- n-out )
1487: \ for the current prim
1488: cache-stack stack-in @ - 0 max
1489: cache-stack stack-prim-stacks-sync @ if
1490: drop 0
1491: endif
1492: cache-stack stack-out @ + cache-states 2@ nip 1- min ;
1493:
1494: : gen-prim-states ( prim -- )
1495: to prim
1496: cache-states 2@ swap { states } ( nstates )
1497: cache-stack stack-in @ +do
1498: states i th @
1499: states i compute-default-state-out th @
1500: prim state-prim1
1501: loop ;
1502:
1503: : prim-states ( "name" -- )
1504: parse-word lookup-prim gen-prim-states ;
1505:
1506: : gen-branch-states ( prim -- )
1507: \ generate versions that produce state-default; useful for branches
1508: to prim
1509: cache-states 2@ swap { states } ( nstates )
1510: cache-stack stack-in @ +do
1511: states i th @ state-default prim state-prim1
1512: loop ;
1513:
1514: : branch-states ( out-state "name" -- )
1515: parse-word lookup-prim gen-branch-states ;
1516:
1517: \ producing state transitions
1518:
1519: : gen-transitions ( "name" -- )
1520: parse-word lookup-prim { prim }
1521: cache-states 2@ { states nstates }
1522: nstates 0 +do
1523: nstates 0 +do
1524: i j <> if
1525: states i th @ states j th @ prim state-prim1
1526: endif
1527: loop
1528: loop ;
1529:
1530: \ C output
1531:
1532: : print-item { n stack -- }
1533: \ print nth stack item name
1534: stack stack-type @ type-c-name 2@ type space
1535: ." MAYBE_UNUSED _" stack stack-pointer 2@ type n 0 .r ;
1536:
1537: : print-declarations-combined ( -- )
1538: max-stacks 0 ?do
1539: max-depth i th @ min-depth i th @ - 0 +do
1540: i stacks j th @ print-item ." ;" cr
1541: loop
1542: loop ;
1543:
1544: : part-fetches ( -- )
1545: fetches ;
1546:
1547: : part-output-c-tail ( -- )
1548: print-debug-results
1549: stores ;
1550:
1551: : output-combined-tail ( -- )
1552: part-output-c-tail
1553: in-part @ >r in-part off
1554: combined ['] output-c-tail-no-stores prim-context
1555: r> in-part ! ;
1556:
1557: : part-stack-pointer-updates ( -- )
1558: next-stack-number @ 0 +do
1559: i part-num @ 1+ s-c-max-depth @ dup
1560: i num-combined @ s-c-max-depth @ = \ final depth
1561: swap i part-num @ s-c-max-depth @ <> \ just reached now
1562: part-num @ 0= \ first part
1563: or and if
1564: stacks i th @ stack-pointer-update
1565: endif
1566: loop ;
1567:
1568: : output-part ( p -- )
1569: to prim
1570: ." /* " prim prim-name 2@ prim-type ." ( " prim prim-stack-string 2@ type ." ) */" cr
1571: ." NAME(" quote prim prim-name 2@ type quote ." )" cr \ debugging
1572: ." {" cr
1573: print-declarations
1574: part-fetches
1575: print-debug-args
1576: combined ['] part-stack-pointer-updates prim-context
1577: 1 part-num +!
1578: prim add-depths \ !! right place?
1579: prim prim-c-code 2@ ['] output-combined-tail type-c-code
1580: part-output-c-tail
1581: ." }" cr ;
1582:
1583: : output-parts ( -- )
1584: prim >r in-part on
1585: current-depth max-stacks cells erase
1586: 0 part-num !
1587: ['] output-part map-combined
1588: in-part off
1589: r> to prim ;
1590:
1591: : output-c-combined ( -- )
1592: print-entry cr
1593: \ debugging messages just in parts
1594: ." {" cr
1595: ." DEF_CA" cr
1596: print-declarations-combined
1597: output-nextp0
1598: spill-state
1599: \ fetches \ now in parts
1600: \ print-debug-args
1601: \ stack-pointer-updates now in parts
1602: output-parts
1603: output-c-tail2-no-stores
1604: ." }" cr
1605: cr ;
1606:
1607: : output-forth-combined ( -- )
1608: ;
1609:
1610:
1611: \ peephole optimization rules
1612:
1613: \ data for a simple peephole optimizer that always tries to combine
1614: \ the currently compiled instruction with the last one.
1615:
1616: \ in order for this to work as intended, shorter combinations for each
1617: \ length must be present, and the longer combinations must follow
1618: \ shorter ones (this restriction may go away in the future).
1619:
1620: : output-peephole ( -- )
1621: combined-prims num-combined @ 1- cells combinations search-wordlist
1622: s" the prefix for this superinstruction must be defined earlier" ?print-error
1623: ." {"
1624: execute prim-num @ 5 .r ." ,"
1625: combined-prims num-combined @ 1- th @ prim-num @ 5 .r ." ,"
1626: combined prim-num @ 5 .r ." }, /* "
1627: combined prim-c-name 2@ type ." */"
1628: cr ;
1629:
1630:
1631: \ cost and superinstruction data for a sophisticated combiner (e.g.,
1632: \ shortest path)
1633:
1634: \ This is intended as initializer for a structure like this
1635:
1636: \ struct cost {
1637: \ char loads; /* number of stack loads */
1638: \ char stores; /* number of stack stores */
1639: \ char updates; /* number of stack pointer updates */
1640: \ char branch; /* is it a branch (SET_IP) */
1641: \ char state_in; /* state on entry */
1642: \ char state_out; /* state on exit */
1643: \ short offset; /* offset into super2 table */
1644: \ char length; /* number of components */
1645: \ };
1646:
1647: \ How do you know which primitive or combined instruction this
1648: \ structure refers to? By the order of cost structures, as in most
1649: \ other cases.
1650:
1651: : super2-length ( -- n )
1652: combined if
1653: num-combined @
1654: else
1655: 1
1656: endif ;
1657:
1658: : compute-costs { p -- nloads nstores nupdates }
1659: \ compute the number of loads, stores, and stack pointer updates
1660: \ of a primitive or combined instruction; does not take TOS
1661: \ caching into account
1662: 0 max-stacks 0 +do
1663: p prim-stacks-in i th @ +
1664: loop
1665: super2-length 1- - \ don't count instruction fetches of subsumed insts
1666: 0 max-stacks 0 +do
1667: p prim-stacks-out i th @ +
1668: loop
1669: 0 max-stacks 1 +do \ don't count ip updates, therefore "1 +do"
1670: p prim-stacks-in i th @ p prim-stacks-out i th @ <> -
1671: loop ;
1672:
1673: : output-num-part ( p -- )
1674: ." N_" prim-c-name-orig 2@ type ." ," ;
1675: \ prim-num @ 4 .r ." ," ;
1676:
1677: : output-name-comment ( -- )
1678: ." /* " prim prim-name 2@ prim-type ." */" ;
1679:
1680: variable offset-super2 0 offset-super2 ! \ offset into the super2 table
1681:
1682: : output-costs-prefix ( -- )
1683: ." {" prim compute-costs
1684: rot 2 .r ." ," swap 2 .r ." ," 2 .r ." , "
1685: prim prim-branch? negate . ." ,"
1686: state-in state-number @ 2 .r ." ,"
1687: state-out state-number @ 2 .r ." ,"
1688: inst-stream stack-in @ 1 .r ." ,"
1689: ;
1690:
1691: : output-costs-gforth-simple ( -- )
1692: output-costs-prefix
1693: prim output-num-part
1694: 1 2 .r ." },"
1695: output-name-comment
1696: cr ;
1697:
1698: : output-costs-gforth-combined ( -- )
1699: output-costs-prefix
1700: ." N_START_SUPER+" offset-super2 @ 5 .r ." ,"
1701: super2-length dup 2 .r ." }," offset-super2 +!
1702: output-name-comment
1703: cr ;
1704:
1705: \ : output-costs ( -- )
1706: \ \ description of superinstructions and simple instructions
1707: \ ." {" prim compute-costs
1708: \ rot 2 .r ." ," swap 2 .r ." ," 2 .r ." ,"
1709: \ offset-super2 @ 5 .r ." ,"
1710: \ super2-length dup 2 .r ." ," offset-super2 +!
1711: \ inst-stream stack-in @ 1 .r ." },"
1712: \ output-name-comment
1713: \ cr ;
1714:
1715: : output-super2-simple ( -- )
1716: prim prim-c-name 2@ prim prim-c-name-orig 2@ d= if
1717: prim output-num-part
1718: output-name-comment
1719: cr
1720: endif ;
1721:
1722: : output-super2-combined ( -- )
1723: ['] output-num-part map-combined
1724: output-name-comment
1725: cr ;
1726:
1727: \ the parser
1728:
1729: eof-char max-member \ the whole character set + EOF
1730:
1731: : getinput ( -- n )
1732: rawinput @ endrawinput @ =
1733: if
1734: eof-char
1735: else
1736: cookedinput @ c@
1737: endif ;
1738:
1739: :noname ( n -- )
1740: dup bl > if
1741: emit space
1742: else
1743: .
1744: endif ;
1745: print-token !
1746:
1747: : testchar? ( set -- f )
1748: getinput member? ;
1749: ' testchar? test-vector !
1750:
1751: : checksynclines ( -- )
1752: \ when input points to a newline, check if the next line is a
1753: \ sync line. If it is, perform the appropriate actions.
1754: rawinput @ begin >r
1755: s" #line " r@ over compare if
1756: rdrop 1 line +! EXIT
1757: endif
1758: 0. r> 6 chars + 20 >number drop >r drop line ! r> ( c-addr )
1759: dup c@ bl = if
1760: char+ dup c@ [char] " <> 0= s" sync line syntax" ?print-error
1761: char+ dup 100 [char] " scan drop swap 2dup - save-mem filename 2!
1762: char+
1763: endif
1764: dup c@ nl-char <> 0= s" sync line syntax" ?print-error
1765: skipsynclines @ if
1766: char+ dup rawinput !
1767: rawinput @ c@ cookedinput @ c!
1768: endif
1769: again ;
1770:
1771: : ?nextchar ( f -- )
1772: s" syntax error, wrong char" ?print-error
1773: rawinput @ endrawinput @ <> if
1774: rawinput @ c@
1775: 1 chars rawinput +!
1776: 1 chars cookedinput +!
1777: nl-char = if
1778: checksynclines
1779: rawinput @ line-start !
1780: endif
1781: rawinput @ c@
1782: cookedinput @ c!
1783: endif ;
1784:
1785: : charclass ( set "name" -- )
1786: ['] ?nextchar terminal ;
1787:
1788: : .. ( c1 c2 -- set )
1789: ( creates a set that includes the characters c, c1<=c<=c2 )
1790: empty copy-set
1791: swap 1+ rot do
1792: i over add-member
1793: loop ;
1794:
1795: : ` ( -- terminal ) ( use: ` c )
1796: ( creates anonymous terminal for the character c )
1797: char singleton ['] ?nextchar make-terminal ;
1798:
1799: char a char z .. char A char Z .. union char _ singleton union charclass letter
1800: char 0 char 9 .. charclass digit
1801: bl singleton tab-char over add-member charclass white
1802: nl-char singleton eof-char over add-member complement charclass nonl
1803: nl-char singleton eof-char over add-member
1804: char : over add-member complement charclass nocolonnl
1805: nl-char singleton eof-char over add-member
1806: char } over add-member complement charclass nobracenl
1807: bl 1+ maxchar .. char \ singleton complement intersection
1808: charclass nowhitebq
1809: bl 1+ maxchar .. charclass nowhite
1810: char " singleton eof-char over add-member complement charclass noquote
1811: nl-char singleton charclass nl
1812: eof-char singleton charclass eof
1813: nl-char singleton eof-char over add-member charclass nleof
1814:
1815: (( letter (( letter || digit )) **
1816: )) <- c-ident ( -- )
1817:
1818: (( ` . ` . ` .
1819: )) <- sync-stack ( -- )
1820:
1821: (( ` # ?? (( letter || digit || ` : )) ++ sync-stack ??
1822: || sync-stack
1823: )) <- stack-ident ( -- )
1824:
1825: (( nowhitebq nowhite ** ))
1826: <- forth-ident ( -- )
1827:
1828: Variable forth-flag
1829: Variable c-flag
1830:
1831: (( (( ` e || ` E )) {{ start }} nonl **
1832: {{ end evaluate }}
1833: )) <- eval-comment ( ... -- ... )
1834:
1835: (( (( ` f || ` F )) {{ start }} nonl **
1836: {{ end forth-flag @ IF type cr ELSE 2drop THEN }}
1837: )) <- forth-comment ( -- )
1838:
1839: (( (( ` c || ` C )) {{ start }} nonl **
1840: {{ end c-flag @ IF type cr ELSE 2drop THEN }}
1841: )) <- c-comment ( -- )
1842:
1843: (( ` - nonl ** {{
1844: forth-flag @ IF forth-fdiff ." [ELSE]" cr THEN
1845: c-flag @ IF
1846: function-diff
1847: ." #else /* " function-number @ 0 .r ." */" cr THEN }}
1848: )) <- else-comment
1849:
1850: (( ` + {{ start }} nonl ** {{ end
1851: dup
1852: IF c-flag @
1853: IF
1854: function-diff
1855: ." #ifdef HAS_" bounds ?DO I c@ toupper emit LOOP cr
1856: THEN
1857: forth-flag @
1858: IF forth-fdiff ." has? " type ." [IF]" cr THEN
1859: ELSE 2drop
1860: c-flag @ IF
1861: function-diff ." #endif" cr THEN
1862: forth-flag @ IF forth-fdiff ." [THEN]" cr THEN
1863: THEN }}
1864: )) <- if-comment
1865:
1866: (( (( ` g || ` G )) {{ start }} nonl **
1867: {{ end
1868: forth-flag @ IF forth-fdiff ." group " type cr THEN
1869: c-flag @ IF function-diff
1870: ." GROUP(" type ." , " function-number @ 0 .r ." )" cr THEN }}
1871: )) <- group-comment
1872:
1873: (( (( eval-comment || forth-comment || c-comment || else-comment || if-comment || group-comment )) ?? nonl ** )) <- comment-body
1874:
1875: (( ` \ comment-body nleof )) <- comment ( -- )
1876:
1877: (( {{ start }} stack-ident {{ end init-item1 }} white ** )) **
1878: <- stack-items ( addr1 -- addr2 )
1879:
1880: (( {{ prim prim-effect-in }} stack-items {{ prim prim-effect-in-end ! }}
1881: ` - ` - white **
1882: {{ prim prim-effect-out }} stack-items {{ prim prim-effect-out-end ! }}
1883: )) <- stack-effect ( -- )
1884:
1885: (( {{ prim create-prim prim init-simple }}
1886: ` ( white ** {{ start }} stack-effect {{ end prim prim-stack-string 2! }} ` ) white **
1887: (( {{ start }} forth-ident {{ end prim prim-wordset 2! }} white **
1888: (( {{ start }} c-ident {{ end 2dup prim-c-name-2! }} )) ??
1889: )) ?? nleof
1890: (( ` " ` " {{ start }} (( noquote ++ ` " )) ++ {{ end 1- prim prim-doc 2! }} ` " white ** nleof )) ??
1891: {{ skipsynclines off line @ c-line ! filename 2@ c-filename 2! start }}
1892: (( (( ` { nonl ** nleof (( (( nobracenl {{ line @ drop }} nonl ** )) ?? nleof )) ** ` } white ** nleof white ** ))
1893: || (( nocolonnl nonl ** nleof white ** )) ** ))
1894: {{ end prim prim-c-code 2! skipsynclines on }}
1895: (( ` : white ** nleof
1896: {{ start }} (( nonl ++ nleof white ** )) ++ {{ end prim prim-forth-code 2! }}
1897: )) ?? {{ process-simple }}
1898: nleof
1899: )) <- simple-primitive ( -- )
1900:
1901: (( {{ init-combined }}
1902: ` = white ** (( {{ start }} forth-ident {{ end add-prim }} white ** )) ++
1903: nleof {{ process-combined }}
1904: )) <- combined-primitive
1905:
1906: (( {{ make-prim to prim 0 to combined
1907: line @ name-line ! filename 2@ name-filename 2!
1908: function-number @ prim prim-num !
1909: start }} [ifdef] vmgen c-ident [else] forth-ident [then] {{ end
1910: 2dup prim prim-name 2! prim-c-name-2! }} white **
1911: (( ` / white ** {{ start }} c-ident {{ end prim-c-name-2! }} white ** )) ??
1912: (( simple-primitive || combined-primitive ))
1913: {{ 1 function-number +! }}
1914: )) <- primitive ( -- )
1915:
1916: (( (( comment || primitive || nl white ** )) ** eof ))
1917: parser primitives2something
1918: warnings @ [IF]
1919: .( parser generated ok ) cr
1920: [THEN]
1921:
1922:
1923: \ run with gforth-0.5.0 (slurp-file is missing)
1924: [IFUNDEF] slurp-file
1925: : slurp-file ( c-addr1 u1 -- c-addr2 u2 )
1926: \ c-addr1 u1 is the filename, c-addr2 u2 is the file's contents
1927: r/o bin open-file throw >r
1928: r@ file-size throw abort" file too large"
1929: dup allocate throw swap
1930: 2dup r@ read-file throw over <> abort" could not read whole file"
1931: r> close-file throw ;
1932: [THEN]
1933:
1934: : primfilter ( addr u -- )
1935: \ process the string at addr u
1936: over dup rawinput ! dup line-start ! cookedinput !
1937: + endrawinput !
1938: checksynclines
1939: primitives2something ;
1940:
1941: : unixify ( c-addr u1 -- c-addr u2 )
1942: \ delete crs from the string
1943: bounds tuck tuck ?do ( c-addr1 )
1944: i c@ dup #cr <> if
1945: over c! char+
1946: else
1947: drop
1948: endif
1949: loop
1950: over - ;
1951:
1952: : process-file ( addr u xt-simple x-combined -- )
1953: output-combined ! output !
1954: save-mem 2dup filename 2!
1955: slurp-file unixify
1956: warnings @ if
1957: ." ------------ CUT HERE -------------" cr endif
1958: primfilter ;
1959:
1960: \ : process ( xt -- )
1961: \ bl word count rot
1962: \ process-file ;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>