Annotation of gforth/prims2x0.6.2.fs, revision 1.7
1.1 anton 1: \ converts primitives to, e.g., C code
2:
1.2 anton 3: \ Copyright (C) 1995,1996,1997,1998,2000,2003,2008 Free Software Foundation, Inc.
1.1 anton 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: \ for backwards compatibility, jaw
55: require compat/strcomp.fs
56:
57: warnings off
58:
59: \ redefinitions of kernel words not present in gforth-0.6.1
60: : latestxt lastcfa @ ;
61: : latest last @ ;
62:
63: [IFUNDEF] try
64: include startup.fs
65: [THEN]
66:
67: : struct% struct ; \ struct is redefined in gray
68:
69: warnings off
70: \ warnings on
71:
72: include ./gray.fs
73: 128 constant max-effect \ number of things on one side of a stack effect
74: 4 constant max-stacks \ the max. number of stacks (including inst-stream).
75: 255 constant maxchar
76: maxchar 1+ constant eof-char
77: #tab constant tab-char
78: #lf constant nl-char
79:
80: variable rawinput \ pointer to next character to be scanned
81: variable endrawinput \ pointer to the end of the input (the char after the last)
82: variable cookedinput \ pointer to the next char to be parsed
83: variable line \ line number of char pointed to by input
84: variable line-start \ pointer to start of current line (for error messages)
85: 0 line !
86: 2variable filename \ filename of original input file
87: 0 0 filename 2!
88: 2variable out-filename \ filename of the output file (for sync lines)
89: 0 0 out-filename 2!
90: 2variable f-comment
91: 0 0 f-comment 2!
92: variable skipsynclines \ are sync lines ("#line ...") invisible to the parser?
93: skipsynclines on
94: variable out-nls \ newlines in output (for output sync lines)
95: 0 out-nls !
96: variable store-optimization \ use store optimization?
97: store-optimization off
98:
99: variable include-skipped-insts
100: \ does the threaded code for a combined instruction include the cells
101: \ for the component instructions (true) or only the cells for the
102: \ inline arguments (false)
103: include-skipped-insts off
104:
105: variable immarg \ values for immediate arguments (to be used in IMM_ARG macros)
106: $12340000 immarg !
107:
108: : th ( addr1 n -- addr2 )
109: cells + ;
110:
111: : holds ( addr u -- )
112: \ like HOLD, but for a string
113: tuck + swap 0 +do
114: 1- dup c@ hold
115: loop
116: drop ;
117:
118: : insert-wordlist { c-addr u wordlist xt -- }
119: \ adds name "addr u" to wordlist using defining word xt
120: \ xt may cause additional stack effects
121: get-current >r wordlist set-current
122: c-addr u nextname xt execute
123: r> set-current ;
124:
125: : start ( -- addr )
126: cookedinput @ ;
127:
128: : end ( addr -- addr u )
129: cookedinput @ over - ;
130:
131: : print-error-line ( -- )
132: \ print the current line and position
133: line-start @ endrawinput @ over - 2dup nl-char scan drop nip ( start end )
134: over - type cr
135: line-start @ rawinput @ over - typewhite ." ^" cr ;
136:
137: : ?print-error { f addr u -- }
138: f ?not? if
139: outfile-id >r try
140: stderr to outfile-id
141: filename 2@ type ." :" line @ 0 .r ." : " addr u type cr
142: print-error-line
143: 0
144: endtry-iferror
145: then
146: r> to outfile-id throw
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: variable next-stack-number 0 next-stack-number !
193: create stacks max-stacks cells allot \ array of stacks
194:
195: : stack-in-index ( in-size item -- in-index )
196: item-offset @ - 1- ;
197:
198: : inst-in-index ( in-size item -- in-index )
199: nip dup item-offset @ swap item-type @ type-size @ + 1- ;
200:
201: : make-stack ( addr-ptr u1 type "stack-name" -- )
202: next-stack-number @ max-stacks < s" too many stacks" ?print-error
203: create stack% %allot >r
204: r@ stacks next-stack-number @ th !
205: next-stack-number @ r@ stack-number !
206: 1 next-stack-number +!
207: r@ stack-type !
208: save-mem r@ stack-pointer 2!
209: ['] stack-in-index r@ stack-in-index-xt !
210: ['] noop r@ stack-access-transform !
211: rdrop ;
212:
213: : map-stacks { xt -- }
214: \ perform xt for all stacks
215: next-stack-number @ 0 +do
216: stacks i th @ xt execute
217: loop ;
218:
219: : map-stacks1 { xt -- }
220: \ perform xt for all stacks except inst-stream
221: next-stack-number @ 1 +do
222: stacks i th @ xt execute
223: loop ;
224:
225: \ stack items
226:
227: : init-item ( addr u addr1 -- )
228: \ initialize item at addr1 with name addr u
229: \ !! remove stack prefix
230: dup item% %size erase
231: item-name 2! ;
232:
233: : map-items { addr end xt -- }
234: \ perform xt for all items in array addr...end
235: end addr ?do
236: i xt execute
237: item% %size +loop ;
238:
239: \ types
240:
241: : print-type-prefix ( type -- )
242: body> >head name>string type ;
243:
244: \ various variables for storing stuff of one primitive
245:
246: struct%
247: cell% 2* field prim-name
248: cell% 2* field prim-wordset
249: cell% 2* field prim-c-name
250: cell% 2* field prim-doc
251: cell% 2* field prim-c-code
252: cell% 2* field prim-forth-code
253: cell% 2* field prim-stack-string
254: cell% field prim-num \ ordinal number
255: cell% field prim-items-wordlist \ unique items
256: item% max-effect * field prim-effect-in
257: item% max-effect * field prim-effect-out
258: cell% field prim-effect-in-end
259: cell% field prim-effect-out-end
260: cell% max-stacks * field prim-stacks-in \ number of in items per stack
261: cell% max-stacks * field prim-stacks-out \ number of out items per stack
262: end-struct prim%
263:
264: : make-prim ( -- prim )
265: prim% %alloc { p }
266: s" " p prim-doc 2! s" " p prim-forth-code 2! s" " p prim-wordset 2!
267: p ;
268:
269: 0 value prim \ in combined prims either combined or a part
270: 0 value combined \ in combined prims the combined prim
271: variable in-part \ true if processing a part
272: in-part off
273:
274: : prim-context ( ... p xt -- ... )
275: \ execute xt with prim set to p
276: prim >r
277: swap to prim
278: catch
279: r> to prim
280: throw ;
281:
282: 1000 constant max-combined
283: create combined-prims max-combined cells allot
284: variable num-combined
285: variable part-num \ current part number during process-combined
286:
287: : map-combined { xt -- }
288: \ perform xt for all components of the current combined instruction
289: num-combined @ 0 +do
290: combined-prims i th @ xt execute
291: loop ;
292:
293: table constant combinations
294: \ the keys are the sequences of pointers to primitives
295:
296: create current-depth max-stacks cells allot
297: create max-depth max-stacks cells allot
298: create min-depth max-stacks cells allot
299:
300: create sp-update-in max-stacks cells allot
301: \ where max-depth occured the first time
302: create max-depths max-stacks max-combined 1+ * cells allot
303: \ maximum depth at start of each part: array[parts] of array[stack]
304: create max-back-depths max-stacks max-combined 1+ * cells allot
1.4 anton 305: \ maximum depth from end of the combination to the start of the each part
306: create depths max-stacks max-combined 1+ * cells allot
307: \ depth at the start of each part: array[parts] of array[stack]
1.1 anton 308:
309: : s-c-max-depth ( nstack ncomponent -- addr )
310: max-stacks * + cells max-depths + ;
311:
312: : s-c-max-back-depth ( nstack ncomponent -- addr )
313: max-stacks * + cells max-back-depths + ;
314:
1.4 anton 315: : s-c-depth ( nstack ncomponent -- addr )
316: max-stacks * + cells depths + ;
317:
1.7 ! anton 318: : print-depths1 { stack# xt -- }
! 319: num-combined @ 1+ 0 +do
! 320: stack# i xt execute @ 3 .r
! 321: loop ;
! 322:
! 323: : print-depths { stack# -- }
! 324: \ for debugging
! 325: cr ." depths: " stack# ['] s-c-depth print-depths1
! 326: cr ." max-depths: " stack# ['] s-c-max-depth print-depths1
! 327: cr ." max-back-depths: " stack# ['] s-c-max-back-depth print-depths1 ;
! 328:
1.4 anton 329: : final-max-depth? { nstack ncomponent -- flag }
330: \ does the stack reach its final maxdepth before the component?
331: nstack ncomponent s-c-max-depth @
332: nstack num-combined @ s-c-max-depth @ = ;
333:
1.1 anton 334: wordlist constant primitives
335:
336: : create-prim ( prim -- )
337: dup prim-name 2@ primitives ['] constant insert-wordlist ;
338:
339: : stack-in ( stack -- addr )
340: \ address of number of stack items in effect in
341: stack-number @ cells prim prim-stacks-in + ;
342:
343: : stack-out ( stack -- addr )
344: \ address of number of stack items in effect out
345: stack-number @ cells prim prim-stacks-out + ;
346:
347: \ global vars
348: variable c-line
349: 2variable c-filename
350: variable name-line
351: 2variable name-filename
352: 2variable last-name-filename
353: Variable function-number 0 function-number !
354: Variable function-old 0 function-old !
355: : function-diff ( n -- )
356: ." GROUPADD(" function-number @ function-old @ - 0 .r ." )" cr
357: function-number @ function-old ! ;
358: : forth-fdiff ( -- )
359: function-number @ function-old @ - 0 .r ." groupadd" cr
360: function-number @ function-old ! ;
361:
362: \ a few more set ops
363:
364: : bit-equivalent ( w1 w2 -- w3 )
365: xor invert ;
366:
367: : complement ( set1 -- set2 )
368: empty ['] bit-equivalent binary-set-operation ;
369:
370: \ forward declaration for inst-stream (breaks cycle in definitions)
371: defer inst-stream-f ( -- stack )
372:
373: \ stack access stuff
374:
375: : normal-stack-access0 { n stack -- }
376: n stack stack-access-transform @ execute ." [" 0 .r ." ]" ;
377:
378: : normal-stack-access1 { n stack -- }
379: stack stack-pointer 2@ type
380: n if
381: n stack normal-stack-access0
382: else
383: ." TOS"
384: endif ;
385:
386: : normal-stack-access ( n stack -- )
387: dup inst-stream-f = if
388: ." IMM_ARG(" normal-stack-access1 ." ," immarg ? ." )"
389: 1 immarg +!
390: else
391: normal-stack-access1
392: endif ;
393:
394: : stack-depth { stack -- n }
395: current-depth stack stack-number @ th @ ;
396:
397: : part-stack-access { n stack -- }
398: \ print _<stack><x>, x=inst-stream? n : maxdepth-currentdepth-n-1
399: ." _" stack stack-pointer 2@ type
400: stack stack-number @ { stack# }
401: stack stack-depth n + { access-depth }
402: stack inst-stream-f = if
403: access-depth
404: else
405: combined prim-stacks-in stack# th @
406: assert( dup max-depth stack# th @ = )
407: access-depth - 1-
408: endif
409: 0 .r ;
410:
411: : part-stack-read { n stack -- }
412: stack stack-depth n + ( ndepth )
413: stack stack-number @ part-num @ s-c-max-depth @
414: \ max-depth stack stack-number @ th @ ( ndepth nmaxdepth )
415: over <= if ( ndepth ) \ load from memory
416: stack normal-stack-access
417: else
418: drop n stack part-stack-access
419: endif ;
420:
421: : stack-diff ( stack -- n )
422: \ in-out
423: dup stack-in @ swap stack-out @ - ;
424:
425: : part-stack-write { n stack -- }
426: stack stack-depth n +
427: stack stack-number @ part-num @ s-c-max-back-depth @
428: over <= if ( ndepth )
429: stack combined ['] stack-diff prim-context -
430: stack normal-stack-access
431: else
432: drop n stack part-stack-access
433: endif ;
434:
435: : stack-read ( n stack -- )
436: \ print a stack access at index n of stack
437: in-part @ if
438: part-stack-read
439: else
440: normal-stack-access
441: endif ;
442:
443: : stack-write ( n stack -- )
444: \ print a stack access at index n of stack
445: in-part @ if
446: part-stack-write
447: else
448: normal-stack-access
449: endif ;
450:
451: : item-in-index { item -- n }
452: \ n is the index of item (in the in-effect)
453: item item-stack @ dup >r stack-in @ ( in-size r:stack )
454: item r> stack-in-index-xt @ execute ;
455:
456: : item-stack-type-name ( item -- addr u )
457: item-stack @ stack-type @ type-c-name 2@ ;
458:
459: : fetch-single ( item -- )
460: \ fetch a single stack item from its stack
461: >r
462: ." vm_" r@ item-stack-type-name type
463: ." 2" r@ item-type @ print-type-prefix ." ("
464: r@ item-in-index r@ item-stack @ stack-read ." ,"
465: r@ item-name 2@ type
466: ." );" cr
467: rdrop ;
468:
469: : fetch-double ( item -- )
470: \ fetch a double stack item from its stack
471: >r
472: ." vm_two"
473: r@ item-stack-type-name type ." 2"
474: r@ item-type @ print-type-prefix ." ("
475: r@ item-in-index r@ item-stack @ 2dup ." (Cell)" stack-read
476: ." , " -1 under+ ." (Cell)" stack-read
477: ." , " r@ item-name 2@ type
478: ." )" cr
479: rdrop ;
480:
481: : same-as-in? ( item -- f )
482: \ f is true iff the offset and stack of item is the same as on input
483: >r
484: r@ item-first @ if
485: rdrop false exit
486: endif
487: r@ item-name 2@ prim prim-items-wordlist @ search-wordlist 0= abort" bug"
488: execute @
489: dup r@ =
490: if \ item first appeared in output
491: drop false
492: else
493: dup item-stack @ r@ item-stack @ =
494: swap item-offset @ r@ item-offset @ = and
495: endif
496: rdrop ;
497:
498: : item-out-index ( item -- n )
499: \ n is the index of item (in the in-effect)
500: >r r@ item-stack @ stack-out @ r> item-offset @ - 1- ;
501:
502: : really-store-single ( item -- )
503: >r
504: ." vm_"
505: r@ item-type @ print-type-prefix ." 2"
506: r@ item-stack-type-name type ." ("
507: r@ item-name 2@ type ." ,"
508: r@ item-out-index r@ item-stack @ stack-write ." );"
509: rdrop ;
510:
511: : store-single ( item -- )
512: >r
513: store-optimization @ in-part @ 0= and r@ same-as-in? and if
514: r@ item-in-index 0= r@ item-out-index 0= xor if
515: ." IF_" r@ item-stack @ stack-pointer 2@ type
516: ." TOS(" r@ really-store-single ." );" cr
517: endif
518: else
519: r@ really-store-single cr
520: endif
521: rdrop ;
522:
523: : store-double ( item -- )
524: \ !! store optimization is not performed, because it is not yet needed
525: >r
526: ." vm_"
527: r@ item-type @ print-type-prefix ." 2two"
528: r@ item-stack-type-name type ." ("
529: r@ item-name 2@ type ." , "
530: r@ item-out-index r@ item-stack @ 2dup stack-write
531: ." , " -1 under+ stack-write
532: ." )" cr
533: rdrop ;
534:
535: : single ( -- xt1 xt2 n )
536: ['] fetch-single ['] store-single 1 ;
537:
538: : double ( -- xt1 xt2 n )
539: ['] fetch-double ['] store-double 2 ;
540:
541: : s, ( addr u -- )
542: \ allocate a string
543: here swap dup allot move ;
544:
545: wordlist constant prefixes
546:
547: : declare ( addr "name" -- )
548: \ remember that there is a stack item at addr called name
549: create , ;
550:
551: : !default ( w addr -- )
552: dup @ if
553: 2drop \ leave nonzero alone
554: else
555: !
556: endif ;
557:
558: : create-type { addr u xt1 xt2 n stack -- } ( "prefix" -- )
559: \ describes a type
560: \ addr u specifies the C type name
561: \ stack effect entries of the type start with prefix
562: create type% %allot >r
563: addr u save-mem r@ type-c-name 2!
564: xt1 r@ type-fetch !
565: xt2 r@ type-store !
566: n r@ type-size !
567: stack r@ type-stack !
568: rdrop ;
569:
570: : type-prefix ( addr u xt1 xt2 n stack "prefix" -- )
571: get-current >r prefixes set-current
572: create-type r> set-current
573: does> ( item -- )
574: \ initialize item
575: { item typ }
576: typ item item-type !
577: typ type-stack @ item item-stack !default
578: item item-name 2@ prim prim-items-wordlist @ search-wordlist 0= if
579: item item-name 2@ nextname item declare
580: item item-first on
581: \ typ type-c-name 2@ type space type ." ;" cr
582: else
583: drop
584: item item-first off
585: endif ;
586:
587: : execute-prefix ( item addr1 u1 -- )
588: \ execute the word ( item -- ) associated with the longest prefix
589: \ of addr1 u1
590: 0 swap ?do
591: dup i prefixes search-wordlist
592: if \ ok, we have the type ( item addr1 xt )
593: nip execute
594: UNLOOP EXIT
595: endif
596: -1 s+loop
597: \ we did not find a type, abort
598: false s" unknown prefix" ?print-error ;
599:
600: : declaration ( item -- )
601: dup item-name 2@ execute-prefix ;
602:
603: : declaration-list ( addr1 addr2 -- )
604: ['] declaration map-items ;
605:
606: : declarations ( -- )
607: wordlist dup prim prim-items-wordlist ! set-current
608: prim prim-effect-in prim prim-effect-in-end @ declaration-list
609: prim prim-effect-out prim prim-effect-out-end @ declaration-list ;
610:
611: : print-declaration { item -- }
612: item item-first @ if
613: item item-type @ type-c-name 2@ type space
614: item item-name 2@ type ." ;" cr
615: endif ;
616:
617: : print-declarations ( -- )
618: prim prim-effect-in prim prim-effect-in-end @ ['] print-declaration map-items
619: prim prim-effect-out prim prim-effect-out-end @ ['] print-declaration map-items ;
620:
621: : stack-prefix ( stack "prefix" -- )
622: get-current >r prefixes set-current
623: name tuck nextname create ( stack length ) 2,
624: r> set-current
625: does> ( item -- )
626: 2@ { item stack prefix-length }
627: item item-name 2@ prefix-length /string item item-name 2!
628: stack item item-stack !
629: item declaration ;
630:
631: \ types pointed to by stacks for use in combined prims
632: \ !! output-c-combined shouldn't use these names!
633: : stack-type-name ( addr u "name" -- )
634: single 0 create-type ;
635:
636: wordlist constant type-names \ this is here just to meet the requirement
637: \ that a type be a word; it is never used for lookup
638:
639: : stack ( "name" "stack-pointer" "type" -- )
640: \ define stack
641: name { d: stack-name }
642: name { d: stack-pointer }
643: name { d: stack-type }
644: get-current type-names set-current
645: stack-type 2dup nextname stack-type-name
646: set-current
647: stack-pointer latestxt >body stack-name nextname make-stack ;
648:
649: stack inst-stream IP Cell
650: ' inst-in-index inst-stream stack-in-index-xt !
651: ' inst-stream <is> inst-stream-f
652: \ !! initialize stack-in and stack-out
653:
654: \ offset computation
655: \ the leftmost (i.e. deepest) item has offset 0
656: \ the rightmost item has the highest offset
657:
658: : compute-offset { item xt -- }
659: \ xt specifies in/out; update stack-in/out and set item-offset
660: item item-type @ type-size @
661: item item-stack @ xt execute dup @ >r +!
662: r> item item-offset ! ;
663:
664: : compute-offset-in ( addr1 addr2 -- )
665: ['] stack-in compute-offset ;
666:
667: : compute-offset-out ( addr1 addr2 -- )
668: ['] stack-out compute-offset ;
669:
670: : compute-offsets ( -- )
671: prim prim-stacks-in max-stacks cells erase
672: prim prim-stacks-out max-stacks cells erase
673: prim prim-effect-in prim prim-effect-in-end @ ['] compute-offset-in map-items
674: prim prim-effect-out prim prim-effect-out-end @ ['] compute-offset-out map-items
675: inst-stream stack-out @ 0= s" # can only be on the input side" ?print-error ;
676:
677: : process-simple ( -- )
678: prim prim { W^ key } key cell
679: combinations ['] constant insert-wordlist
680: declarations compute-offsets
681: output @ execute ;
682:
683: : flush-a-tos { stack -- }
684: stack stack-out @ 0<> stack stack-in @ 0= and
685: if
686: ." IF_" stack stack-pointer 2@ 2dup type ." TOS("
687: 2dup type 0 stack normal-stack-access0 ." = " type ." TOS);" cr
688: endif ;
689:
690: : flush-tos ( -- )
691: ['] flush-a-tos map-stacks1 ;
692:
1.7 ! anton 693: : ?really-fill-a-tos { f stack -- }
! 694: f if
! 695: ." IF_" stack stack-pointer 2@ 2dup type ." TOS("
! 696: 2dup type ." TOS = " type 0 stack normal-stack-access0 ." );" cr
! 697: endif ;
! 698:
1.1 anton 699: : fill-a-tos { stack -- }
1.7 ! anton 700: stack stack-out @ 0= stack stack-in @ 0<> and stack ?really-fill-a-tos ;
1.1 anton 701:
702: : fill-tos ( -- )
703: \ !! inst-stream for prefetching?
704: ['] fill-a-tos map-stacks1 ;
705:
706: : fetch ( addr -- )
707: dup item-type @ type-fetch @ execute ;
708:
709: : fetches ( -- )
710: prim prim-effect-in prim prim-effect-in-end @ ['] fetch map-items ;
711:
712: : stack-update-transform ( n1 stack -- n2 )
713: \ n2 is the number by which the stack pointer should be
714: \ incremented to pop n1 items
715: stack-access-transform @ dup >r execute
716: 0 r> execute - ;
717:
1.4 anton 718: : n-stack-pointer-update { n stack -- }
719: \ stack pointer update by n
720: n if \ this check is not necessary, gcc would do this for us
1.1 anton 721: stack inst-stream = if
1.4 anton 722: ." INC_IP(" n 0 .r ." );" cr
1.1 anton 723: else
724: stack stack-pointer 2@ type ." += "
1.4 anton 725: n stack stack-update-transform 0 .r ." ;" cr
1.1 anton 726: endif
727: endif ;
728:
1.4 anton 729: : stack-pointer-update { stack -- }
730: \ stacks grow downwards
731: stack stack-diff stack n-stack-pointer-update ;
732:
1.1 anton 733: : stack-pointer-updates ( -- )
734: ['] stack-pointer-update map-stacks ;
735:
736: : store ( item -- )
737: \ f is true if the item should be stored
738: \ f is false if the store is probably not necessary
739: dup item-type @ type-store @ execute ;
740:
741: : stores ( -- )
742: prim prim-effect-out prim prim-effect-out-end @ ['] store map-items ;
743:
744: : print-debug-arg { item -- }
745: ." fputs(" quote space item item-name 2@ type ." =" quote ." , vm_out); "
746: ." printarg_" item item-type @ print-type-prefix
747: ." (" item item-name 2@ type ." );" cr ;
748:
749: : print-debug-args ( -- )
750: ." #ifdef VM_DEBUG" cr
751: ." if (vm_debug) {" cr
752: prim prim-effect-in prim prim-effect-in-end @ ['] print-debug-arg map-items
753: \ ." fputc('\n', vm_out);" cr
754: ." }" cr
755: ." #endif" cr ;
756:
757: : print-debug-result { item -- }
758: item item-first @ if
759: item print-debug-arg
760: endif ;
761:
762: : print-debug-results ( -- )
763: cr
764: ." #ifdef VM_DEBUG" cr
765: ." if (vm_debug) {" cr
766: ." fputs(" quote ." -- " quote ." , vm_out); "
767: prim prim-effect-out prim prim-effect-out-end @ ['] print-debug-result map-items
768: ." fputc('\n', vm_out);" cr
769: ." }" cr
770: ." #endif" cr ;
771:
772: : output-super-end ( -- )
773: prim prim-c-code 2@ s" SET_IP" search if
774: ." SUPER_END;" cr
775: endif
776: 2drop ;
777:
778: : output-nextp2 ( -- )
779: ." NEXT_P2;" cr ;
780:
781: variable tail-nextp2 \ xt to execute for printing NEXT_P2 in INST_TAIL
782: ' output-nextp2 tail-nextp2 !
783:
784: : output-label2 ( -- )
785: ." LABEL2(" prim prim-c-name 2@ type ." )" cr
786: ." NEXT_P2;" cr ;
787:
788: : output-c-tail1 { xt -- }
789: \ the final part of the generated C code, with xt printing LABEL2 or not.
790: output-super-end
791: print-debug-results
792: ." NEXT_P1;" cr
793: stores
794: fill-tos
795: xt execute ;
796:
797: : output-c-tail1-no-stores { xt -- }
798: \ the final part of the generated C code for combinations
799: output-super-end
800: ." NEXT_P1;" cr
801: fill-tos
802: xt execute ;
803:
804: : output-c-tail ( -- )
805: tail-nextp2 @ output-c-tail1 ;
806:
807: : output-c-tail2 ( -- )
808: ['] output-label2 output-c-tail1 ;
809:
810: : output-c-tail2-no-stores ( -- )
811: ['] output-label2 output-c-tail1-no-stores ;
812:
813: : type-c-code ( c-addr u xt -- )
814: \ like TYPE, but replaces "INST_TAIL;" with tail code produced by xt
815: { xt }
816: ." {" cr
817: ." #line " c-line @ . quote c-filename 2@ type quote cr
818: begin ( c-addr1 u1 )
819: 2dup s" INST_TAIL;" search
820: while ( c-addr1 u1 c-addr3 u3 )
821: 2dup 2>r drop nip over - type
822: xt execute
823: 2r> 10 /string
824: \ !! resync #line missing
825: repeat
826: 2drop type
827: ." #line " out-nls @ 2 + . quote out-filename 2@ type quote cr
828: ." }" cr ;
829:
830: : print-entry ( -- )
831: ." LABEL(" prim prim-c-name 2@ type ." )" ;
832:
833: : output-c ( -- )
834: print-entry ." /* " prim prim-name 2@ type ." ( " prim prim-stack-string 2@ type ." ) */" cr
835: ." /* " prim prim-doc 2@ type ." */" cr
836: ." NAME(" quote prim prim-name 2@ type quote ." )" cr \ debugging
837: ." {" cr
838: ." DEF_CA" cr
839: print-declarations
840: ." NEXT_P0;" cr
841: flush-tos
842: fetches
843: print-debug-args
844: stack-pointer-updates
845: prim prim-c-code 2@ ['] output-c-tail type-c-code
846: output-c-tail2
847: ." }" cr
848: cr
849: ;
850:
851: : disasm-arg { item -- }
852: item item-stack @ inst-stream = if
853: ." {" cr
854: item print-declaration
855: item fetch
856: item print-debug-arg
857: ." }" cr
858: endif ;
859:
860: : disasm-args ( -- )
861: prim prim-effect-in prim prim-effect-in-end @ ['] disasm-arg map-items ;
862:
863: : output-disasm ( -- )
864: \ generate code for disassembling VM instructions
865: ." if (VM_IS_INST(*ip, " function-number @ 0 .r ." )) {" cr
866: ." fputs(" quote prim prim-name 2@ type quote ." , vm_out);" cr
867: disasm-args
868: ." ip += " inst-stream stack-in @ 1+ 0 .r ." ;" cr
869: ." goto _endif_;" cr
870: ." }" cr ;
871:
872: : output-profile ( -- )
873: \ generate code for postprocessing the VM block profile stuff
874: ." if (VM_IS_INST(*ip, " function-number @ 0 .r ." )) {" cr
875: ." add_inst(b, " quote prim prim-name 2@ type quote ." );" cr
876: ." ip += " inst-stream stack-in @ 1+ 0 .r ." ;" cr
877: prim prim-c-code 2@ s" SET_IP" search nip nip
878: prim prim-c-code 2@ s" SUPER_END" search nip nip or if
879: ." return;" cr
880: else
881: ." goto _endif_;" cr
882: endif
883: ." }" cr ;
884:
885: : output-profile-part ( p )
886: ." add_inst(b, " quote
887: prim-name 2@ type
888: quote ." );" cr ;
889:
890: : output-profile-combined ( -- )
891: \ generate code for postprocessing the VM block profile stuff
892: ." if (VM_IS_INST(*ip, " function-number @ 0 .r ." )) {" cr
893: ['] output-profile-part map-combined
894: ." ip += " inst-stream stack-in @ 1+ 0 .r ." ;" cr
895: combined-prims num-combined @ 1- th @ prim-c-code 2@ s" SET_IP" search nip nip
896: combined-prims num-combined @ 1- th @ prim-c-code 2@ s" SUPER_END" search nip nip or if
897: ." return;" cr
898: else
899: ." goto _endif_;" cr
900: endif
901: ." }" cr ;
902:
903: : output-superend ( -- )
904: \ output flag specifying whether the current word ends a dynamic superinst
905: prim prim-c-code 2@ s" SET_IP" search nip nip
906: prim prim-c-code 2@ s" SUPER_END" search nip nip or 0<>
907: prim prim-c-code 2@ s" SUPER_CONTINUE" search nip nip 0= and
908: negate 0 .r ." , /* " prim prim-name 2@ type ." */" cr ;
909:
910: : gen-arg-parm { item -- }
911: item item-stack @ inst-stream = if
912: ." , " item item-type @ type-c-name 2@ type space
913: item item-name 2@ type
914: endif ;
915:
916: : gen-args-parm ( -- )
917: prim prim-effect-in prim prim-effect-in-end @ ['] gen-arg-parm map-items ;
918:
919: : gen-arg-gen { item -- }
920: item item-stack @ inst-stream = if
921: ." genarg_" item item-type @ print-type-prefix
922: ." (ctp, " item item-name 2@ type ." );" cr
923: endif ;
924:
925: : gen-args-gen ( -- )
926: prim prim-effect-in prim prim-effect-in-end @ ['] gen-arg-gen map-items ;
927:
928: : output-gen ( -- )
929: \ generate C code for generating VM instructions
930: ." void gen_" prim prim-c-name 2@ type ." (Inst **ctp" gen-args-parm ." )" cr
931: ." {" cr
932: ." gen_inst(ctp, vm_prim[" function-number @ 0 .r ." ]);" cr
933: gen-args-gen
934: ." }" cr ;
935:
936: : stack-used? { stack -- f }
937: stack stack-in @ stack stack-out @ or 0<> ;
938:
939: : output-funclabel ( -- )
940: ." &I_" prim prim-c-name 2@ type ." ," cr ;
941:
942: : output-forthname ( -- )
943: '" emit prim prim-name 2@ type '" emit ." ," cr ;
944:
945: \ : output-c-func ( -- )
946: \ \ used for word libraries
947: \ ." Cell * I_" prim prim-c-name 2@ type ." (Cell *SP, Cell **FP) /* " prim prim-name 2@ type
948: \ ." ( " prim prim-stack-string 2@ type ." ) */" cr
949: \ ." /* " prim prim-doc 2@ type ." */" cr
950: \ ." NAME(" quote prim prim-name 2@ type quote ." )" cr
951: \ \ debugging
952: \ ." {" cr
953: \ print-declarations
954: \ \ !! don't know what to do about that
955: \ inst-stream stack-used? IF ." Cell *ip=IP;" cr THEN
956: \ data-stack stack-used? IF ." Cell *sp=SP;" cr THEN
957: \ fp-stack stack-used? IF ." Cell *fp=*FP;" cr THEN
958: \ return-stack stack-used? IF ." Cell *rp=*RP;" cr THEN
959: \ flush-tos
960: \ fetches
961: \ stack-pointer-updates
962: \ fp-stack stack-used? IF ." *FP=fp;" cr THEN
963: \ ." {" cr
964: \ ." #line " c-line @ . quote c-filename 2@ type quote cr
965: \ prim prim-c-code 2@ type
966: \ ." }" cr
967: \ stores
968: \ fill-tos
969: \ ." return (sp);" cr
970: \ ." }" cr
971: \ cr ;
972:
973: : output-label ( -- )
974: ." INST_ADDR(" prim prim-c-name 2@ type ." )," cr ;
975:
976: : output-alias ( -- )
977: ( primitive-number @ . ." alias " ) ." Primitive " prim prim-name 2@ type cr ;
978:
979: : output-c-prim-num ( -- )
980: ." N_" prim prim-c-name 2@ type ." ," cr ;
981:
982: : output-forth ( -- )
983: prim prim-forth-code @ 0=
984: IF \ output-alias
985: \ this is bad for ec: an alias is compiled if tho word does not exist!
986: \ JAW
987: ELSE ." : " prim prim-name 2@ type ." ( "
988: prim prim-stack-string 2@ type ." )" cr
989: prim prim-forth-code 2@ type cr
990: THEN ;
991:
992: : output-tag-file ( -- )
993: name-filename 2@ last-name-filename 2@ compare if
994: name-filename 2@ last-name-filename 2!
995: #ff emit cr
996: name-filename 2@ type
997: ." ,0" cr
998: endif ;
999:
1000: : output-tag ( -- )
1001: output-tag-file
1002: prim prim-name 2@ 1+ type
1003: 127 emit
1004: space prim prim-name 2@ type space
1005: 1 emit
1006: name-line @ 0 .r
1007: ." ,0" cr ;
1008:
1009: : output-vi-tag ( -- )
1010: name-filename 2@ type #tab emit
1011: prim prim-name 2@ type #tab emit
1012: ." /^" prim prim-name 2@ type ." *(/" cr ;
1013:
1014: [IFDEF] documentation
1015: : register-doc ( -- )
1016: prim prim-name 2@ documentation ['] create insert-wordlist
1017: prim prim-name 2@ 2,
1018: prim prim-stack-string 2@ condition-stack-effect 2,
1019: prim prim-wordset 2@ 2,
1020: prim prim-c-name 2@ condition-pronounciation 2,
1021: prim prim-doc 2@ 2, ;
1022: [THEN]
1023:
1024:
1025: \ combining instructions
1026:
1027: \ The input should look like this:
1028:
1029: \ lit_+ = lit +
1030:
1031: \ The output should look like this:
1032:
1033: \ I_lit_+:
1034: \ {
1035: \ DEF_CA
1036: \ Cell _x_ip0;
1037: \ Cell _x_sp0;
1038: \ Cell _x_sp1;
1039: \ NEXT_P0;
1040: \ _x_ip0 = (Cell) IPTOS;
1041: \ _x_sp0 = (Cell) spTOS;
1042: \ INC_IP(1);
1043: \ /* sp += 0; */
1044: \ /* lit ( #w -- w ) */
1045: \ /* */
1046: \ NAME("lit")
1047: \ {
1048: \ Cell w;
1049: \ w = (Cell) _x_ip0;
1050: \ #ifdef VM_DEBUG
1051: \ if (vm_debug) {
1052: \ fputs(" w=", vm_out); printarg_w (w);
1053: \ fputc('\n', vm_out);
1054: \ }
1055: \ #endif
1056: \ {
1057: \ #line 136 "./prim"
1058: \ }
1059: \ _x_sp1 = (Cell)w;
1060: \ }
1061: \ I_plus: /* + ( n1 n2 -- n ) */
1062: \ /* */
1063: \ NAME("+")
1064: \ {
1065: \ DEF_CA
1066: \ Cell n1;
1067: \ Cell n2;
1068: \ Cell n;
1069: \ NEXT_P0;
1070: \ n1 = (Cell) _x_sp0;
1071: \ n2 = (Cell) _x_sp1;
1072: \ #ifdef VM_DEBUG
1073: \ if (vm_debug) {
1074: \ fputs(" n1=", vm_out); printarg_n (n1);
1075: \ fputs(" n2=", vm_out); printarg_n (n2);
1076: \ fputc('\n', vm_out);
1077: \ }
1078: \ #endif
1079: \ {
1080: \ #line 516 "./prim"
1081: \ n = n1+n2;
1082: \ }
1083: \ _x_sp0 = (Cell)n;
1084: \ }
1085: \ NEXT_P1;
1086: \ spTOS = (Cell)_x_sp0;
1087: \ NEXT_P2;
1088:
1089: : init-combined ( -- )
1090: prim to combined
1091: 0 num-combined !
1092: current-depth max-stacks cells erase
1093: include-skipped-insts @ current-depth 0 th !
1094: max-depth max-stacks cells erase
1095: min-depth max-stacks cells erase
1096: prim prim-effect-in prim prim-effect-in-end !
1097: prim prim-effect-out prim prim-effect-out-end ! ;
1098:
1099: : max! ( n addr -- )
1100: tuck @ max swap ! ;
1101:
1102: : min! ( n addr -- )
1103: tuck @ min swap ! ;
1104:
1105: : inst-stream-adjustment ( nstack -- n )
1106: \ number of stack items to add for each part
1107: 0= include-skipped-insts @ and negate ;
1108:
1109: : add-depths { p -- }
1110: \ combine stack effect of p with *-depths
1111: max-stacks 0 ?do
1112: current-depth i th @
1113: p prim-stacks-in i th @ + i inst-stream-adjustment +
1114: dup max-depth i th max!
1115: p prim-stacks-out i th @ -
1116: dup min-depth i th min!
1117: current-depth i th !
1118: loop ;
1119:
1.4 anton 1120: : copy-maxdepths { n -- }
1121: \ transfer current-depth to depths and max-depth to max-depths
1122: max-depth max-depths n max-stacks * th max-stacks cells move
1123: current-depth depths n max-stacks * th max-stacks cells move ;
1.1 anton 1124:
1125: : add-prim ( addr u -- )
1126: \ add primitive given by "addr u" to combined-prims
1127: primitives search-wordlist s" unknown primitive" ?print-error
1128: execute { p }
1129: p combined-prims num-combined @ th !
1130: num-combined @ copy-maxdepths
1131: 1 num-combined +!
1132: p add-depths
1133: num-combined @ copy-maxdepths ;
1134:
1135: : compute-effects { q -- }
1136: \ compute the stack effects of q from the depths
1137: max-stacks 0 ?do
1138: max-depth i th @ dup
1139: q prim-stacks-in i th !
1140: current-depth i th @ -
1141: q prim-stacks-out i th !
1142: loop ;
1143:
1144: : make-effect-items { stack# items effect-endp -- }
1145: \ effect-endp points to a pointer to the end of the current item-array
1146: \ and has to be updated
1147: stacks stack# th @ { stack }
1148: items 0 +do
1149: effect-endp @ { item }
1150: i 0 <# #s stack stack-pointer 2@ holds [char] _ hold #> save-mem
1151: item item-name 2!
1152: stack item item-stack !
1153: stack stack-type @ item item-type !
1154: i item item-offset !
1155: item item-first on
1156: item% %size effect-endp +!
1157: loop ;
1158:
1159: : init-effects { q -- }
1160: \ initialize effects field for FETCHES and STORES
1161: max-stacks 0 ?do
1162: i q prim-stacks-in i th @ q prim-effect-in-end make-effect-items
1163: i q prim-stacks-out i th @ q prim-effect-out-end make-effect-items
1164: loop ;
1165:
1166: : compute-stack-max-back-depths ( stack -- )
1167: stack-number @ { stack# }
1168: current-depth stack# th @ dup
1169: dup stack# num-combined @ s-c-max-back-depth !
1170: -1 num-combined @ 1- -do ( max-depth current-depth )
1171: combined-prims i th @ { p }
1172: p prim-stacks-out stack# th @ +
1173: dup >r max r>
1174: over stack# i s-c-max-back-depth !
1175: p prim-stacks-in stack# th @ -
1176: stack# inst-stream-adjustment -
1177: 1 -loop
1178: assert( dup stack# inst-stream-adjustment negate = )
1179: assert( over max-depth stack# th @ = )
1180: 2drop ;
1181:
1182: : compute-max-back-depths ( -- )
1183: \ compute max-back-depths.
1184: \ assumes that current-depths is correct for the end of the combination
1185: ['] compute-stack-max-back-depths map-stacks ;
1186:
1187: : process-combined ( -- )
1188: combined combined-prims num-combined @ cells
1189: combinations ['] constant insert-wordlist
1190: combined-prims num-combined @ 1- th ( last-part )
1191: @ prim-c-code 2@ prim prim-c-code 2! \ used by output-super-end
1192: prim compute-effects
1193: prim init-effects
1194: compute-max-back-depths
1195: output-combined perform ;
1196:
1197: \ C output
1198:
1199: : print-item { n stack -- }
1200: \ print nth stack item name
1201: stack stack-type @ type-c-name 2@ type space
1202: ." MAYBE_UNUSED _" stack stack-pointer 2@ type n 0 .r ;
1203:
1204: : print-declarations-combined ( -- )
1205: max-stacks 0 ?do
1206: max-depth i th @ min-depth i th @ - 0 +do
1207: i stacks j th @ print-item ." ;" cr
1208: loop
1209: loop ;
1210:
1211: : part-fetches ( -- )
1212: fetches ;
1213:
1214: : part-output-c-tail ( -- )
1215: print-debug-results
1216: stores ;
1217:
1.7 ! anton 1218: : stack-combined-tail-stores-limits { stack -- nupper nlower }
1.6 anton 1219: stack stack-number @ part-num @ 2dup 2>r s-c-max-depth @
1220: 2r> s-c-max-back-depth @ min
1221: stack stack-depth -
1.7 ! anton 1222: stack stack-out @ ;
! 1223:
! 1224: : stack-combined-tail-stores { stack -- }
! 1225: \ the top stack-out items of this part are stored elsewhere; so
! 1226: \ this store everything between max-depth and stack-out, unless it
! 1227: \ was stored previously (below back-max-depth)
! 1228: stack stack-combined-tail-stores-limits +do
1.6 anton 1229: i stack normal-stack-access ." = "
1230: i stack part-stack-access ." ;" cr
1231: loop ;
1232:
1233: : combined-tail-stores ( -- )
1234: \ All the stores we have yet to do on an INST_TAIL that are the
1235: \ result of earlier parts (and will be consumed by other parts in
1236: \ the fallthrough path, and thus have not been stored in the
1237: \ earlier part).
1238: ['] stack-combined-tail-stores map-stacks ;
1239:
1.7 ! anton 1240: : combined-tail-stack-pointer-update-n { stack -- }
1.4 anton 1241: stack stack-number @ { nstack }
1242: nstack part-num @ 1+ s-c-depth @ ( nupdate-raw )
1243: \ correct for possible earlier update
1244: nstack part-num @ 1+ final-max-depth? if
1.7 ! anton 1245: stack combined ['] stack-diff prim-context -
! 1246: endif ;
! 1247:
! 1248: : combined-tail-stack-pointer-update { stack -- }
! 1249: stack combined-tail-stack-pointer-update-n stack n-stack-pointer-update ;
1.4 anton 1250:
1251: : combined-tail-stack-pointer-updates ( -- )
1252: ['] combined-tail-stack-pointer-update map-stacks ;
1253:
1.7 ! anton 1254: : stack-combined-tail-stores-limits { stack -- nupper nlower }
! 1255: stack stack-number @ part-num @ 2dup 2>r s-c-max-depth @
! 1256: 2r> s-c-max-back-depth @ min
! 1257: stack stack-depth -
! 1258: stack stack-out @ ;
! 1259:
! 1260: : tail-fill-a-tos { stack -- }
! 1261: \ 1 print-depths
! 1262: stack stack-number @ part-num @ 2dup s-c-max-depth @ { scmde }
! 1263: 1+ 2dup s-c-max-depth @ { scmd } s-c-depth @ { scd }
! 1264: scmd scd = scmd 0<> and \ normal fill (TOS consumed)
! 1265: \ or it was stored to (what's now) sp[0] by an earlier part:
! 1266: stack stack-combined-tail-stores-limits <= \ no stores in this tail
! 1267: scmde 0= and \ there were stores to this stack earlier
! 1268: stack combined-tail-stack-pointer-update-n 0<> and \ but not to spTOS
! 1269: or stack ?really-fill-a-tos ;
! 1270:
! 1271: : tail-fill-tos ( -- )
! 1272: \ fill tos in INST_TAIL
! 1273: ['] tail-fill-a-tos map-stacks1 ;
! 1274:
! 1275: : output-c-tail-no-stores ( -- )
! 1276: \ the final part of INST_TAIL
! 1277: output-super-end
! 1278: ." NEXT_P1;" cr
! 1279: tail-fill-tos
! 1280: tail-nextp2 perform ;
! 1281:
1.1 anton 1282: : output-combined-tail ( -- )
1.3 anton 1283: in-part @ >r in-part off
1.5 anton 1284: combined-tail-stack-pointer-updates
1.1 anton 1285: part-output-c-tail
1.6 anton 1286: combined-tail-stores
1.1 anton 1287: combined ['] output-c-tail-no-stores prim-context
1288: r> in-part ! ;
1289:
1290: : part-stack-pointer-updates ( -- )
1291: next-stack-number @ 0 +do
1.4 anton 1292: i part-num @ 1+ final-max-depth? \ reached afterwards
1293: i part-num @ final-max-depth? 0= \ but not before
1294: part-num @ 0= \ exception: first part
1.1 anton 1295: or and if
1296: stacks i th @ stack-pointer-update
1297: endif
1298: loop ;
1299:
1300: : output-part ( p -- )
1301: to prim
1302: ." /* " prim prim-name 2@ type ." ( " prim prim-stack-string 2@ type ." ) */" cr
1303: ." NAME(" quote prim prim-name 2@ type quote ." )" cr \ debugging
1304: ." {" cr
1305: print-declarations
1306: part-fetches
1307: print-debug-args
1308: combined ['] part-stack-pointer-updates prim-context
1309: prim add-depths \ !! right place?
1310: prim prim-c-code 2@ ['] output-combined-tail type-c-code
1.4 anton 1311: 1 part-num +!
1.1 anton 1312: part-output-c-tail
1313: ." }" cr ;
1314:
1315: : output-parts ( -- )
1316: prim >r in-part on
1317: current-depth max-stacks cells erase
1318: 0 part-num !
1319: ['] output-part map-combined
1320: in-part off
1321: r> to prim ;
1322:
1323: : output-c-combined ( -- )
1324: print-entry cr
1325: \ debugging messages just in parts
1326: ." {" cr
1327: ." DEF_CA" cr
1328: print-declarations-combined
1329: ." NEXT_P0;" cr
1330: flush-tos
1331: \ fetches \ now in parts
1332: \ print-debug-args
1333: \ stack-pointer-updates now in parts
1334: output-parts
1335: output-c-tail2-no-stores
1336: ." }" cr
1337: cr ;
1338:
1339: : output-forth-combined ( -- )
1340: ;
1341:
1342:
1343: \ peephole optimization rules
1344:
1345: \ data for a simple peephole optimizer that always tries to combine
1346: \ the currently compiled instruction with the last one.
1347:
1348: \ in order for this to work as intended, shorter combinations for each
1349: \ length must be present, and the longer combinations must follow
1350: \ shorter ones (this restriction may go away in the future).
1351:
1352: : output-peephole ( -- )
1353: combined-prims num-combined @ 1- cells combinations search-wordlist
1354: s" the prefix for this superinstruction must be defined earlier" ?print-error
1355: ." {"
1356: execute prim-num @ 5 .r ." ,"
1357: combined-prims num-combined @ 1- th @ prim-num @ 5 .r ." ,"
1358: combined prim-num @ 5 .r ." }, /* "
1359: combined prim-c-name 2@ type ." */"
1360: cr ;
1361:
1362:
1363: \ cost and superinstruction data for a sophisticated combiner (e.g.,
1364: \ shortest path)
1365:
1366: \ This is intended as initializer for a structure like this
1367:
1368: \ struct cost {
1369: \ int loads; /* number of stack loads */
1370: \ int stores; /* number of stack stores */
1371: \ int updates; /* number of stack pointer updates */
1372: \ int offset; /* offset into super2 table */
1373: \ int length; /* number of components */
1374: \ };
1375:
1376: \ How do you know which primitive or combined instruction this
1377: \ structure refers to? By the order of cost structures, as in most
1378: \ other cases.
1379:
1380: : super2-length ( -- n )
1381: combined if
1382: num-combined @
1383: else
1384: 1
1385: endif ;
1386:
1387: : compute-costs { p -- nloads nstores nupdates }
1388: \ compute the number of loads, stores, and stack pointer updates
1389: \ of a primitive or combined instruction; does not take TOS
1390: \ caching into account
1391: 0 max-stacks 0 +do
1392: p prim-stacks-in i th @ +
1393: loop
1394: super2-length 1- - \ don't count instruction fetches of subsumed insts
1395: 0 max-stacks 0 +do
1396: p prim-stacks-out i th @ +
1397: loop
1398: 0 max-stacks 1 +do \ don't count ip updates, therefore "1 +do"
1399: p prim-stacks-in i th @ p prim-stacks-out i th @ <> -
1400: loop ;
1401:
1402: : output-num-part ( p -- )
1403: ." N_" prim-c-name 2@ type ." ," ;
1404: \ prim-num @ 4 .r ." ," ;
1405:
1406: : output-name-comment ( -- )
1407: ." /* " prim prim-name 2@ type ." */" ;
1408:
1409: variable offset-super2 0 offset-super2 ! \ offset into the super2 table
1410:
1411: : output-costs-gforth-simple ( -- )
1412: ." {" prim compute-costs
1413: rot 2 .r ." ," swap 2 .r ." ," 2 .r ." , "
1414: prim output-num-part
1415: 1 2 .r ." },"
1416: output-name-comment
1417: cr ;
1418:
1419: : output-costs-gforth-combined ( -- )
1420: ." {" prim compute-costs
1421: rot 2 .r ." ," swap 2 .r ." ," 2 .r ." , "
1422: ." N_START_SUPER+" offset-super2 @ 5 .r ." ,"
1423: super2-length dup 2 .r ." }," offset-super2 +!
1424: output-name-comment
1425: cr ;
1426:
1427: : output-costs ( -- )
1428: \ description of superinstructions and simple instructions
1429: ." {" prim compute-costs
1430: rot 2 .r ." ," swap 2 .r ." ," 2 .r ." ,"
1431: offset-super2 @ 5 .r ." ,"
1432: super2-length dup 2 .r ." }," offset-super2 +!
1433: output-name-comment
1434: cr ;
1435:
1436: : output-super2 ( -- )
1437: \ table of superinstructions without requirement for existing prefixes
1438: combined if
1439: ['] output-num-part map-combined
1440: else
1441: prim output-num-part
1442: endif
1443: output-name-comment
1444: cr ;
1445:
1446: \ the parser
1447:
1448: eof-char max-member \ the whole character set + EOF
1449:
1450: : getinput ( -- n )
1451: rawinput @ endrawinput @ =
1452: if
1453: eof-char
1454: else
1455: cookedinput @ c@
1456: endif ;
1457:
1458: :noname ( n -- )
1459: dup bl > if
1460: emit space
1461: else
1462: .
1463: endif ;
1464: print-token !
1465:
1466: : testchar? ( set -- f )
1467: getinput member? ;
1468: ' testchar? test-vector !
1469:
1470: : checksynclines ( -- )
1471: \ when input points to a newline, check if the next line is a
1472: \ sync line. If it is, perform the appropriate actions.
1473: rawinput @ begin >r
1474: s" #line " r@ over compare if
1475: rdrop 1 line +! EXIT
1476: endif
1477: 0. r> 6 chars + 20 >number drop >r drop line ! r> ( c-addr )
1478: dup c@ bl = if
1479: char+ dup c@ [char] " <> 0= s" sync line syntax" ?print-error
1480: char+ dup 100 [char] " scan drop swap 2dup - save-mem filename 2!
1481: char+
1482: endif
1483: dup c@ nl-char <> 0= s" sync line syntax" ?print-error
1484: skipsynclines @ if
1485: char+ dup rawinput !
1486: rawinput @ c@ cookedinput @ c!
1487: endif
1488: again ;
1489:
1490: : ?nextchar ( f -- )
1491: s" syntax error, wrong char" ?print-error
1492: rawinput @ endrawinput @ <> if
1493: rawinput @ c@
1494: 1 chars rawinput +!
1495: 1 chars cookedinput +!
1496: nl-char = if
1497: checksynclines
1498: rawinput @ line-start !
1499: endif
1500: rawinput @ c@
1501: cookedinput @ c!
1502: endif ;
1503:
1504: : charclass ( set "name" -- )
1505: ['] ?nextchar terminal ;
1506:
1507: : .. ( c1 c2 -- set )
1508: ( creates a set that includes the characters c, c1<=c<=c2 )
1509: empty copy-set
1510: swap 1+ rot do
1511: i over add-member
1512: loop ;
1513:
1514: : ` ( -- terminal ) ( use: ` c )
1515: ( creates anonymous terminal for the character c )
1516: char singleton ['] ?nextchar make-terminal ;
1517:
1518: char a char z .. char A char Z .. union char _ singleton union charclass letter
1519: char 0 char 9 .. charclass digit
1520: bl singleton tab-char over add-member charclass white
1521: nl-char singleton eof-char over add-member complement charclass nonl
1522: nl-char singleton eof-char over add-member
1523: char : over add-member complement charclass nocolonnl
1524: nl-char singleton eof-char over add-member
1525: char } over add-member complement charclass nobracenl
1526: bl 1+ maxchar .. char \ singleton complement intersection
1527: charclass nowhitebq
1528: bl 1+ maxchar .. charclass nowhite
1529: char " singleton eof-char over add-member complement charclass noquote
1530: nl-char singleton charclass nl
1531: eof-char singleton charclass eof
1532: nl-char singleton eof-char over add-member charclass nleof
1533:
1534: (( letter (( letter || digit )) **
1535: )) <- c-ident ( -- )
1536:
1537: (( ` # ?? (( letter || digit || ` : )) ++
1538: )) <- stack-ident ( -- )
1539:
1540: (( nowhitebq nowhite ** ))
1541: <- forth-ident ( -- )
1542:
1543: Variable forth-flag
1544: Variable c-flag
1545:
1546: (( (( ` e || ` E )) {{ start }} nonl **
1547: {{ end evaluate }}
1548: )) <- eval-comment ( ... -- ... )
1549:
1550: (( (( ` f || ` F )) {{ start }} nonl **
1551: {{ end forth-flag @ IF type cr ELSE 2drop THEN }}
1552: )) <- forth-comment ( -- )
1553:
1554: (( (( ` c || ` C )) {{ start }} nonl **
1555: {{ end c-flag @ IF type cr ELSE 2drop THEN }}
1556: )) <- c-comment ( -- )
1557:
1558: (( ` - nonl ** {{
1559: forth-flag @ IF forth-fdiff ." [ELSE]" cr THEN
1560: c-flag @ IF
1561: function-diff
1562: ." #else /* " function-number @ 0 .r ." */" cr THEN }}
1563: )) <- else-comment
1564:
1565: (( ` + {{ start }} nonl ** {{ end
1566: dup
1567: IF c-flag @
1568: IF
1569: function-diff
1570: ." #ifdef HAS_" bounds ?DO I c@ toupper emit LOOP cr
1571: THEN
1572: forth-flag @
1573: IF forth-fdiff ." has? " type ." [IF]" cr THEN
1574: ELSE 2drop
1575: c-flag @ IF
1576: function-diff ." #endif" cr THEN
1577: forth-flag @ IF forth-fdiff ." [THEN]" cr THEN
1578: THEN }}
1579: )) <- if-comment
1580:
1581: (( (( ` g || ` G )) {{ start }} nonl **
1582: {{ end
1583: forth-flag @ IF forth-fdiff ." group " type cr THEN
1584: c-flag @ IF function-diff
1585: ." GROUP(" type ." , " function-number @ 0 .r ." )" cr THEN }}
1586: )) <- group-comment
1587:
1588: (( (( eval-comment || forth-comment || c-comment || else-comment || if-comment || group-comment )) ?? nonl ** )) <- comment-body
1589:
1590: (( ` \ comment-body nleof )) <- comment ( -- )
1591:
1592: (( {{ start }} stack-ident {{ end 2 pick init-item item% %size + }} white ** )) **
1593: <- stack-items
1594:
1595: (( {{ prim prim-effect-in }} stack-items {{ prim prim-effect-in-end ! }}
1596: ` - ` - white **
1597: {{ prim prim-effect-out }} stack-items {{ prim prim-effect-out-end ! }}
1598: )) <- stack-effect ( -- )
1599:
1600: (( {{ prim create-prim }}
1601: ` ( white ** {{ start }} stack-effect {{ end prim prim-stack-string 2! }} ` ) white **
1602: (( {{ start }} forth-ident {{ end prim prim-wordset 2! }} white **
1603: (( {{ start }} c-ident {{ end prim prim-c-name 2! }} )) ??
1604: )) ?? nleof
1605: (( ` " ` " {{ start }} (( noquote ++ ` " )) ++ {{ end 1- prim prim-doc 2! }} ` " white ** nleof )) ??
1606: {{ skipsynclines off line @ c-line ! filename 2@ c-filename 2! start }}
1607: (( (( ` { nonl ** nleof (( (( nobracenl {{ line @ drop }} nonl ** )) ?? nleof )) ** ` } white ** nleof white ** ))
1608: || (( nocolonnl nonl ** nleof white ** )) ** ))
1609: {{ end prim prim-c-code 2! skipsynclines on }}
1610: (( ` : white ** nleof
1611: {{ start }} (( nonl ++ nleof white ** )) ++ {{ end prim prim-forth-code 2! }}
1612: )) ?? {{ process-simple }}
1613: nleof
1614: )) <- simple-primitive ( -- )
1615:
1616: (( {{ init-combined }}
1617: ` = white ** (( {{ start }} forth-ident {{ end add-prim }} white ** )) ++
1618: nleof {{ process-combined }}
1619: )) <- combined-primitive
1620:
1621: (( {{ make-prim to prim 0 to combined
1622: line @ name-line ! filename 2@ name-filename 2!
1623: function-number @ prim prim-num !
1624: start }} [ifdef] vmgen c-ident [else] forth-ident [then] {{ end
1625: 2dup prim prim-name 2! prim prim-c-name 2! }} white **
1626: (( ` / white ** {{ start }} c-ident {{ end prim prim-c-name 2! }} white ** )) ??
1627: (( simple-primitive || combined-primitive ))
1628: {{ 1 function-number +! }}
1629: )) <- primitive ( -- )
1630:
1631: (( (( comment || primitive || nl white ** )) ** eof ))
1632: parser primitives2something
1633: warnings @ [IF]
1634: .( parser generated ok ) cr
1635: [THEN]
1636:
1637:
1638: \ run with gforth-0.5.0 (slurp-file is missing)
1639: [IFUNDEF] slurp-file
1640: : slurp-file ( c-addr1 u1 -- c-addr2 u2 )
1641: \ c-addr1 u1 is the filename, c-addr2 u2 is the file's contents
1642: r/o bin open-file throw >r
1643: r@ file-size throw abort" file too large"
1644: dup allocate throw swap
1645: 2dup r@ read-file throw over <> abort" could not read whole file"
1646: r> close-file throw ;
1647: [THEN]
1648:
1649: : primfilter ( addr u -- )
1650: \ process the string at addr u
1651: over dup rawinput ! dup line-start ! cookedinput !
1652: + endrawinput !
1653: checksynclines
1654: primitives2something ;
1655:
1656: : unixify ( c-addr u1 -- c-addr u2 )
1657: \ delete crs from the string
1658: bounds tuck tuck ?do ( c-addr1 )
1659: i c@ dup #cr <> if
1660: over c! char+
1661: else
1662: drop
1663: endif
1664: loop
1665: over - ;
1666:
1667: : process-file ( addr u xt-simple x-combined -- )
1668: output-combined ! output !
1669: save-mem 2dup filename 2!
1670: slurp-file unixify
1671: warnings @ if
1672: ." ------------ CUT HERE -------------" cr endif
1673: primfilter ;
1674:
1675: \ : process ( xt -- )
1676: \ bl word count rot
1677: \ process-file ;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>