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