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