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