Annotation of gforth/prims2x.fs, revision 1.72
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
43:
1.3 pazsan 44: warnings off
45:
1.39 jwilke 46: [IFUNDEF] vocabulary \ we are executed just with kernel image
47: \ load the rest that is needed
48: \ (require fails because this file is needed from a
49: \ different directory with the wordlibraries)
50: include ./search.fs
51: include ./extend.fs
1.40 anton 52: [THEN]
53:
54: [IFUNDEF] environment?
1.39 jwilke 55: include ./environ.fs
56: [THEN]
1.25 pazsan 57:
1.49 anton 58: : struct% struct ; \ struct is redefined in gray
59:
1.39 jwilke 60: include ./gray.fs
1.1 anton 61:
1.69 anton 62: 32 constant max-effect \ number of things on one side of a stack effect
1.71 anton 63: 4 constant max-stacks \ the max. number of stacks (including inst-stream).
1.1 anton 64: 255 constant maxchar
65: maxchar 1+ constant eof-char
1.17 anton 66: #tab constant tab-char
67: #lf constant nl-char
1.1 anton 68:
1.18 anton 69: variable rawinput \ pointer to next character to be scanned
70: variable endrawinput \ pointer to the end of the input (the char after the last)
71: variable cookedinput \ pointer to the next char to be parsed
1.17 anton 72: variable line \ line number of char pointed to by input
1.65 anton 73: variable line-start \ pointer to start of current line (for error messages)
74: 0 line !
1.17 anton 75: 2variable filename \ filename of original input file
76: 0 0 filename 2!
1.25 pazsan 77: 2variable f-comment
78: 0 0 f-comment 2!
1.17 anton 79: variable skipsynclines \ are sync lines ("#line ...") invisible to the parser?
80: skipsynclines on
1.1 anton 81:
1.72 ! anton 82: : th ( addr1 n -- addr2 )
! 83: cells + ;
! 84:
! 85: : holds ( addr u -- )
! 86: \ like HOLD, but for a string
! 87: tuck + swap 0 +do
! 88: 1- dup c@ hold
! 89: loop
! 90: drop ;
1.71 anton 91:
1.1 anton 92: : start ( -- addr )
1.18 anton 93: cookedinput @ ;
1.1 anton 94:
95: : end ( addr -- addr u )
1.18 anton 96: cookedinput @ over - ;
1.1 anton 97:
1.71 anton 98: : print-error-line ( -- )
99: \ print the current line and position
100: line-start @ endrawinput @ over - 2dup nl-char scan drop nip ( start end )
101: over - type cr
102: line-start @ rawinput @ over - typewhite ." ^" cr ;
103:
104: : ?print-error { f addr u -- }
105: f ?not? if
106: outfile-id >r try
107: stderr to outfile-id
108: filename 2@ type ." :" line @ 0 .r ." : " addr u type cr
109: print-error-line
110: 0
111: recover endtry
112: r> to outfile-id throw
113: abort
114: endif ;
115:
1.63 anton 116: : quote ( -- )
117: [char] " emit ;
118:
1.72 ! anton 119: variable output \ xt ( -- ) of output word for simple primitives
! 120: variable output-combined \ xt ( -- ) of output word for combined primitives
1.1 anton 121:
122: : printprim ( -- )
123: output @ execute ;
124:
1.49 anton 125: struct%
1.71 anton 126: cell% field stack-number \ the number of this stack
1.49 anton 127: cell% 2* field stack-pointer \ stackpointer name
1.72 ! anton 128: cell% 2* field stack-typename \ name for default type of stack items
1.49 anton 129: cell% 2* field stack-cast \ cast string for assignments to stack elements
1.53 anton 130: cell% field stack-in-index-xt \ ( in-size item -- in-index )
1.49 anton 131: end-struct stack%
132:
1.53 anton 133: struct%
134: cell% 2* field item-name \ name, excluding stack prefixes
135: cell% field item-stack \ descriptor for the stack used, 0 is default
136: cell% field item-type \ descriptor for the item type
137: cell% field item-offset \ offset in stack items, 0 for the deepest element
1.66 anton 138: cell% field item-first \ true if this is the first occurence of the item
1.53 anton 139: end-struct item%
140:
141: struct%
142: cell% 2* field type-c-name
143: cell% field type-stack \ default stack
144: cell% field type-size \ size of type in stack items
145: cell% field type-fetch \ xt of fetch code generator ( item -- )
146: cell% field type-store \ xt of store code generator ( item -- )
147: end-struct type%
148:
1.72 ! anton 149: variable next-stack-number 0 next-stack-number !
! 150: create stacks max-stacks cells allot \ array of stacks
! 151:
1.53 anton 152: : stack-in-index ( in-size item -- in-index )
153: item-offset @ - 1- ;
154:
155: : inst-in-index ( in-size item -- in-index )
156: nip dup item-offset @ swap item-type @ type-size @ + 1- ;
157:
1.72 ! anton 158: : make-stack ( addr-ptr u1 addr-stack u2 addr-cast u3 "stack-name" -- )
1.49 anton 159: create stack% %allot >r
1.72 ! anton 160: r@ stacks next-stack-number @ th !
1.71 anton 161: next-stack-number @ r@ stack-number ! 1 next-stack-number +!
1.49 anton 162: save-mem r@ stack-cast 2!
1.72 ! anton 163: save-mem r@ stack-typename 2!
1.53 anton 164: save-mem r@ stack-pointer 2!
165: ['] stack-in-index r> stack-in-index-xt ! ;
1.49 anton 166:
1.72 ! anton 167: s" sp" save-mem s" Cell" save-mem s" (Cell)" make-stack data-stack
! 168: s" fp" save-mem s" Float" save-mem s" " make-stack fp-stack
! 169: s" rp" save-mem s" Cell" save-mem s" (Cell)" make-stack return-stack
! 170: s" IP" save-mem s" Cell" save-mem s" error don't use # on results" make-stack inst-stream
1.53 anton 171: ' inst-in-index inst-stream stack-in-index-xt !
1.49 anton 172: \ !! initialize stack-in and stack-out
173:
174: \ stack items
175:
176: : init-item ( addr u addr1 -- )
177: \ initialize item at addr1 with name addr u
178: \ !! remove stack prefix
179: dup item% %size erase
180: item-name 2! ;
181:
1.64 anton 182: : map-items { addr end xt -- }
183: \ perform xt for all items in array addr...end
184: end addr ?do
185: i xt execute
186: item% %size +loop ;
187:
1.49 anton 188: \ various variables for storing stuff of one primitive
1.1 anton 189:
1.69 anton 190: struct%
191: cell% 2* field prim-name
192: cell% 2* field prim-wordset
193: cell% 2* field prim-c-name
194: cell% 2* field prim-doc
195: cell% 2* field prim-c-code
196: cell% 2* field prim-forth-code
197: cell% 2* field prim-stack-string
198: item% max-effect * field prim-effect-in
199: item% max-effect * field prim-effect-out
200: cell% field prim-effect-in-end
201: cell% field prim-effect-out-end
1.71 anton 202: cell% max-stacks * field prim-stacks-in \ number of in items per stack
203: cell% max-stacks * field prim-stacks-out \ number of out items per stack
1.69 anton 204: end-struct prim%
205:
1.70 anton 206: : make-prim ( -- prim )
207: prim% %alloc { p }
208: s" " p prim-doc 2! s" " p prim-forth-code 2! s" " p prim-wordset 2!
209: p ;
210:
211: 0 value prim
1.69 anton 212:
1.71 anton 213: wordlist constant primitives
214:
215: : create-prim ( prim -- )
216: get-current >r
217: primitives set-current
218: dup prim-name 2@ nextname constant
219: r> set-current ;
220:
221: : stack-in ( stack -- addr )
222: \ address of number of stack items in effect in
223: stack-number @ cells prim prim-stacks-in + ;
224:
225: : stack-out ( stack -- addr )
226: \ address of number of stack items in effect out
227: stack-number @ cells prim prim-stacks-out + ;
228:
1.69 anton 229: \ global vars
1.17 anton 230: variable c-line
231: 2variable c-filename
232: variable name-line
233: 2variable name-filename
234: 2variable last-name-filename
1.30 pazsan 235: Variable function-number 0 function-number !
1.1 anton 236:
237: \ for several reasons stack items of a word are stored in a wordlist
238: \ since neither forget nor marker are implemented yet, we make a new
239: \ wordlist for every word and store it in the variable items
240: variable items
241:
242: \ a few more set ops
243:
244: : bit-equivalent ( w1 w2 -- w3 )
245: xor invert ;
246:
247: : complement ( set1 -- set2 )
248: empty ['] bit-equivalent binary-set-operation ;
249:
250: \ types
251:
1.49 anton 252: : stack-access ( n stack -- )
253: \ print a stack access at index n of stack
254: stack-pointer 2@ type
255: dup
256: if
257: ." [" 0 .r ." ]"
258: else
259: drop ." TOS"
260: endif ;
1.1 anton 261:
1.53 anton 262: : item-in-index { item -- n }
1.49 anton 263: \ n is the index of item (in the in-effect)
1.53 anton 264: item item-stack @ dup >r stack-in @ ( in-size r:stack )
265: item r> stack-in-index-xt @ execute ;
1.1 anton 266:
267: : fetch-single ( item -- )
1.49 anton 268: \ fetch a single stack item from its stack
1.1 anton 269: >r
1.8 pazsan 270: r@ item-name 2@ type
271: ." = ("
1.1 anton 272: r@ item-type @ type-c-name 2@ type ." ) "
1.49 anton 273: r@ item-in-index r@ item-stack @ stack-access
274: ." ;" cr
1.1 anton 275: rdrop ;
276:
277: : fetch-double ( item -- )
1.49 anton 278: \ fetch a double stack item from its stack
1.1 anton 279: >r
1.20 anton 280: ." FETCH_DCELL("
281: r@ item-name 2@ type ." , "
1.61 anton 282: r@ item-in-index r@ item-stack @ 2dup ." (Cell)" stack-access
283: ." , " -1 under+ ." (Cell)" stack-access
1.20 anton 284: ." );" cr
1.1 anton 285: rdrop ;
286:
1.49 anton 287: : same-as-in? ( item -- f )
288: \ f is true iff the offset and stack of item is the same as on input
1.1 anton 289: >r
290: r@ item-name 2@ items @ search-wordlist 0=
1.8 pazsan 291: abort" bug"
1.1 anton 292: execute @
293: dup r@ =
294: if \ item first appeared in output
295: drop false
296: else
1.49 anton 297: dup item-stack @ r@ item-stack @ =
298: swap item-offset @ r@ item-offset @ = and
1.1 anton 299: endif
300: rdrop ;
301:
1.49 anton 302: : item-out-index ( item -- n )
303: \ n is the index of item (in the in-effect)
304: >r r@ item-stack @ stack-out @ r> item-offset @ - 1- ;
1.31 pazsan 305:
1.1 anton 306: : really-store-single ( item -- )
307: >r
1.49 anton 308: r@ item-out-index r@ item-stack @ stack-access ." = "
309: r@ item-stack @ stack-cast 2@ type
1.1 anton 310: r@ item-name 2@ type ." ;"
311: rdrop ;
312:
313: : store-single ( item -- )
314: >r
1.49 anton 315: r@ same-as-in?
1.1 anton 316: if
1.49 anton 317: r@ item-in-index 0= r@ item-out-index 0= xor
1.1 anton 318: if
1.49 anton 319: ." IF_" r@ item-stack @ stack-pointer 2@ type
320: ." TOS(" r@ really-store-single ." );" cr
1.1 anton 321: endif
322: else
323: r@ really-store-single cr
324: endif
325: rdrop ;
326:
327: : store-double ( item -- )
328: \ !! store optimization is not performed, because it is not yet needed
329: >r
1.20 anton 330: ." STORE_DCELL(" r@ item-name 2@ type ." , "
1.49 anton 331: r@ item-out-index r@ item-stack @ 2dup stack-access
332: ." , " -1 under+ stack-access
1.20 anton 333: ." );" cr
1.1 anton 334: rdrop ;
335:
1.54 anton 336: : single ( -- xt1 xt2 n )
337: ['] fetch-single ['] store-single 1 ;
1.1 anton 338:
1.54 anton 339: : double ( -- xt1 xt2 n )
340: ['] fetch-double ['] store-double 2 ;
1.1 anton 341:
342: : s, ( addr u -- )
343: \ allocate a string
344: here swap dup allot move ;
345:
1.50 anton 346: wordlist constant prefixes
347:
348: : declare ( addr "name" -- )
349: \ remember that there is a stack item at addr called name
350: create , ;
351:
352: : !default ( w addr -- )
353: dup @ if
354: 2drop \ leave nonzero alone
355: else
356: !
357: endif ;
358:
359: : create-type { addr u xt1 xt2 n stack -- } ( "prefix" -- )
1.49 anton 360: \ describes a type
361: \ addr u specifies the C type name
362: \ stack effect entries of the type start with prefix
363: create type% %allot >r
364: addr u save-mem r@ type-c-name 2!
365: xt1 r@ type-fetch !
366: xt2 r@ type-store !
367: n r@ type-size !
368: stack r@ type-stack !
369: rdrop ;
1.1 anton 370:
1.54 anton 371: : type-prefix ( xt1 xt2 n stack "prefix" -- )
1.50 anton 372: create-type
373: does> ( item -- )
374: \ initialize item
375: { item typ }
376: typ item item-type !
377: typ type-stack @ item item-stack !default
378: item item-name 2@ items @ search-wordlist 0= if \ new name
1.66 anton 379: item item-name 2@ nextname item declare
380: item item-first on
381: \ typ type-c-name 2@ type space type ." ;" cr
1.50 anton 382: else
383: drop
1.66 anton 384: item item-first off
1.50 anton 385: endif ;
386:
387: : execute-prefix ( item addr1 u1 -- )
388: \ execute the word ( item -- ) associated with the longest prefix
389: \ of addr1 u1
390: 0 swap ?do
391: dup i prefixes search-wordlist
392: if \ ok, we have the type ( item addr1 xt )
393: nip execute
394: UNLOOP EXIT
395: endif
396: -1 s+loop
397: \ we did not find a type, abort
398: true abort" unknown prefix" ;
1.1 anton 399:
400: : declaration ( item -- )
1.50 anton 401: dup item-name 2@ execute-prefix ;
1.1 anton 402:
1.64 anton 403: : declaration-list ( addr1 addr2 -- )
404: ['] declaration map-items ;
405:
406: : declarations ( -- )
407: wordlist dup items ! set-current
1.69 anton 408: prim prim-effect-in prim prim-effect-in-end @ declaration-list
409: prim prim-effect-out prim prim-effect-out-end @ declaration-list ;
1.64 anton 410:
1.66 anton 411: : print-declaration { item -- }
412: item item-first @ if
413: item item-type @ type-c-name 2@ type space
414: item item-name 2@ type ." ;" cr
415: endif ;
416:
417: : print-declarations ( -- )
1.69 anton 418: prim prim-effect-in prim prim-effect-in-end @ ['] print-declaration map-items
419: prim prim-effect-out prim prim-effect-out-end @ ['] print-declaration map-items ;
1.66 anton 420:
1.51 anton 421: : stack-prefix ( stack "prefix" -- )
422: name tuck nextname create ( stack length ) 2,
423: does> ( item -- )
424: 2@ { item stack prefix-length }
425: item item-name 2@ prefix-length /string item item-name 2!
426: stack item item-stack !
427: item declaration ;
1.1 anton 428:
429: \ offset computation
430: \ the leftmost (i.e. deepest) item has offset 0
431: \ the rightmost item has the highest offset
432:
1.49 anton 433: : compute-offset { item xt -- }
434: \ xt specifies in/out; update stack-in/out and set item-offset
435: item item-type @ type-size @
436: item item-stack @ xt execute dup @ >r +!
437: r> item item-offset ! ;
438:
1.64 anton 439: : compute-offset-in ( addr1 addr2 -- )
440: ['] stack-in compute-offset ;
441:
442: : compute-offset-out ( addr1 addr2 -- )
443: ['] stack-out compute-offset ;
1.49 anton 444:
445: : clear-stack { -- }
446: dup stack-in off stack-out off ;
1.1 anton 447:
448: : compute-offsets ( -- )
1.51 anton 449: data-stack clear-stack fp-stack clear-stack return-stack clear-stack
1.53 anton 450: inst-stream clear-stack
1.69 anton 451: prim prim-effect-in prim prim-effect-in-end @ ['] compute-offset-in map-items
452: prim prim-effect-out prim prim-effect-out-end @ ['] compute-offset-out map-items
1.53 anton 453: inst-stream stack-out @ 0<> abort" # can only be on the input side" ;
1.49 anton 454:
455: : flush-a-tos { stack -- }
456: stack stack-out @ 0<> stack stack-in @ 0= and
457: if
458: ." IF_" stack stack-pointer 2@ 2dup type ." TOS("
459: 2dup type ." [0] = " type ." TOS);" cr
460: endif ;
1.1 anton 461:
462: : flush-tos ( -- )
1.51 anton 463: data-stack flush-a-tos
464: fp-stack flush-a-tos
465: return-stack flush-a-tos ;
1.49 anton 466:
467: : fill-a-tos { stack -- }
468: stack stack-out @ 0= stack stack-in @ 0<> and
469: if
470: ." IF_" stack stack-pointer 2@ 2dup type ." TOS("
471: 2dup type ." TOS = " type ." [0]);" cr
472: endif ;
1.1 anton 473:
474: : fill-tos ( -- )
1.53 anton 475: \ !! inst-stream for prefetching?
1.51 anton 476: fp-stack fill-a-tos
477: data-stack fill-a-tos
478: return-stack fill-a-tos ;
1.49 anton 479:
480: : fetch ( addr -- )
1.72 ! anton 481: dup item-type @ type-fetch @ execute ;
1.1 anton 482:
483: : fetches ( -- )
1.69 anton 484: prim prim-effect-in prim prim-effect-in-end @ ['] fetch map-items ;
1.49 anton 485:
486: : stack-pointer-update { stack -- }
487: \ stack grow downwards
488: stack stack-in @ stack stack-out @ -
489: ?dup-if \ this check is not necessary, gcc would do this for us
490: stack stack-pointer 2@ type ." += " 0 .r ." ;" cr
491: endif ;
1.1 anton 492:
1.55 anton 493: : inst-pointer-update ( -- )
494: inst-stream stack-in @ ?dup-if
495: ." INC_IP(" 0 .r ." );" cr
496: endif ;
497:
1.1 anton 498: : stack-pointer-updates ( -- )
1.55 anton 499: inst-pointer-update
1.51 anton 500: data-stack stack-pointer-update
501: fp-stack stack-pointer-update
502: return-stack stack-pointer-update ;
1.1 anton 503:
504: : store ( item -- )
505: \ f is true if the item should be stored
506: \ f is false if the store is probably not necessary
1.49 anton 507: dup item-type @ type-store @ execute ;
1.1 anton 508:
509: : stores ( -- )
1.69 anton 510: prim prim-effect-out prim prim-effect-out-end @ ['] store map-items ;
1.8 pazsan 511:
1.52 anton 512: : output-c-tail ( -- )
513: \ the final part of the generated C code
514: ." NEXT_P1;" cr
515: stores
516: fill-tos
517: ." NEXT_P2;" cr ;
518:
519: : type-c ( c-addr u -- )
520: \ like TYPE, but replaces "TAIL;" with tail code
521: begin ( c-addr1 u1 )
522: 2dup s" TAIL;" search
523: while ( c-addr1 u1 c-addr3 u3 )
524: 2dup 2>r drop nip over - type
525: output-c-tail
526: 2r> 5 /string
527: \ !! resync #line missing
528: repeat
529: 2drop type ;
530:
1.63 anton 531: : print-type-prefix ( type -- )
532: body> >head .name ;
533:
534: : print-debug-arg { item -- }
535: ." fputs(" quote space item item-name 2@ type ." =" quote ." , vm_out); "
536: ." printarg_" item item-type @ print-type-prefix
537: ." (" item item-name 2@ type ." );" cr ;
538:
539: : print-debug-args ( -- )
540: ." #ifdef VM_DEBUG" cr
541: ." if (vm_debug) {" cr
1.69 anton 542: prim prim-effect-in prim prim-effect-in-end @ ['] print-debug-arg map-items
1.63 anton 543: ." fputc('\n', vm_out);" cr
544: ." }" cr
545: ." #endif" cr ;
1.72 ! anton 546:
! 547: : print-entry ( -- )
! 548: ." I_" prim prim-c-name 2@ type ." :" ;
1.63 anton 549:
1.43 jwilke 550: : output-c ( -- )
1.72 ! anton 551: print-entry ." /* " prim prim-name 2@ type ." ( " prim prim-stack-string 2@ type ." ) */" cr
1.69 anton 552: ." /* " prim prim-doc 2@ type ." */" cr
553: ." NAME(" quote prim prim-name 2@ type quote ." )" cr \ debugging
1.1 anton 554: ." {" cr
555: ." DEF_CA" cr
1.66 anton 556: print-declarations
1.13 anton 557: ." NEXT_P0;" cr
558: flush-tos
1.1 anton 559: fetches
1.63 anton 560: print-debug-args
1.13 anton 561: stack-pointer-updates
1.1 anton 562: ." {" cr
1.63 anton 563: ." #line " c-line @ . quote c-filename 2@ type quote cr
1.69 anton 564: prim prim-c-code 2@ type-c
1.1 anton 565: ." }" cr
1.52 anton 566: output-c-tail
1.1 anton 567: ." }" cr
568: cr
569: ;
570:
1.56 anton 571: : disasm-arg { item -- }
572: item item-stack @ inst-stream = if
1.63 anton 573: ." fputc(' ', vm_out); "
574: ." printarg_" item item-type @ print-type-prefix
575: ." ((" item item-type @ type-c-name 2@ type ." )"
576: ." ip[" item item-offset @ 1+ 0 .r ." ]);" cr
1.56 anton 577: endif ;
578:
579: : disasm-args ( -- )
1.69 anton 580: prim prim-effect-in prim prim-effect-in-end @ ['] disasm-arg map-items ;
1.56 anton 581:
582: : output-disasm ( -- )
583: \ generate code for disassembling VM instructions
584: ." if (ip[0] == prim[" function-number @ 0 .r ." ]) {" cr
1.69 anton 585: ." fputs(" quote prim prim-name 2@ type quote ." , vm_out);" cr
1.56 anton 586: disasm-args
587: ." ip += " inst-stream stack-in @ 1+ 0 .r ." ;" cr
1.68 anton 588: ." } else " ;
1.56 anton 589:
1.60 anton 590: : gen-arg-parm { item -- }
591: item item-stack @ inst-stream = if
592: ." , " item item-type @ type-c-name 2@ type space
593: item item-name 2@ type
594: endif ;
595:
596: : gen-args-parm ( -- )
1.69 anton 597: prim prim-effect-in prim prim-effect-in-end @ ['] gen-arg-parm map-items ;
1.60 anton 598:
599: : gen-arg-gen { item -- }
600: item item-stack @ inst-stream = if
601: ." genarg_" item item-type @ print-type-prefix
602: ." (ctp, " item item-name 2@ type ." );" cr
603: endif ;
604:
605: : gen-args-gen ( -- )
1.69 anton 606: prim prim-effect-in prim prim-effect-in-end @ ['] gen-arg-gen map-items ;
1.60 anton 607:
608: : output-gen ( -- )
609: \ generate C code for generating VM instructions
1.69 anton 610: ." void gen_" prim prim-c-name 2@ type ." (Inst **ctp" gen-args-parm ." )" cr
1.60 anton 611: ." {" cr
612: ." gen_inst(ctp, vm_prim[" function-number @ 0 .r ." ]);" cr
613: gen-args-gen
1.68 anton 614: ." }" cr ;
1.60 anton 615:
1.49 anton 616: : stack-used? { stack -- f }
617: stack stack-in @ stack stack-out @ or 0<> ;
1.44 jwilke 618:
1.30 pazsan 619: : output-funclabel ( -- )
1.69 anton 620: ." &I_" prim prim-c-name 2@ type ." ," cr ;
1.30 pazsan 621:
622: : output-forthname ( -- )
1.69 anton 623: '" emit prim prim-name 2@ type '" emit ." ," cr ;
1.30 pazsan 624:
625: : output-c-func ( -- )
1.44 jwilke 626: \ used for word libraries
1.69 anton 627: ." Cell * I_" prim prim-c-name 2@ type ." (Cell *SP, Cell **FP) /* " prim prim-name 2@ type
628: ." ( " prim prim-stack-string 2@ type ." ) */" cr
629: ." /* " prim prim-doc 2@ type ." */" cr
630: ." NAME(" quote prim prim-name 2@ type quote ." )" cr
1.30 pazsan 631: \ debugging
632: ." {" cr
1.66 anton 633: print-declarations
1.53 anton 634: inst-stream stack-used? IF ." Cell *ip=IP;" cr THEN
1.51 anton 635: data-stack stack-used? IF ." Cell *sp=SP;" cr THEN
636: fp-stack stack-used? IF ." Cell *fp=*FP;" cr THEN
637: return-stack stack-used? IF ." Cell *rp=*RP;" cr THEN
1.30 pazsan 638: flush-tos
639: fetches
640: stack-pointer-updates
1.49 anton 641: fp-stack stack-used? IF ." *FP=fp;" cr THEN
1.30 pazsan 642: ." {" cr
1.63 anton 643: ." #line " c-line @ . quote c-filename 2@ type quote cr
1.69 anton 644: prim prim-c-code 2@ type
1.30 pazsan 645: ." }" cr
646: stores
647: fill-tos
1.44 jwilke 648: ." return (sp);" cr
1.30 pazsan 649: ." }" cr
650: cr ;
651:
1.43 jwilke 652: : output-label ( -- )
1.69 anton 653: ." (Label)&&I_" prim prim-c-name 2@ type ." ," cr ;
1.1 anton 654:
1.43 jwilke 655: : output-alias ( -- )
1.69 anton 656: ( primitive-number @ . ." alias " ) ." Primitive " prim prim-name 2@ type cr ;
1.1 anton 657:
1.43 jwilke 658: : output-forth ( -- )
1.69 anton 659: prim prim-forth-code @ 0=
1.30 pazsan 660: IF \ output-alias
1.28 jwilke 661: \ this is bad for ec: an alias is compiled if tho word does not exist!
662: \ JAW
1.69 anton 663: ELSE ." : " prim prim-name 2@ type ." ( "
664: prim prim-stack-string 2@ type ." )" cr
665: prim prim-forth-code 2@ type cr
1.30 pazsan 666: THEN ;
1.10 anton 667:
1.17 anton 668: : output-tag-file ( -- )
669: name-filename 2@ last-name-filename 2@ compare if
670: name-filename 2@ last-name-filename 2!
671: #ff emit cr
672: name-filename 2@ type
673: ." ,0" cr
674: endif ;
675:
676: : output-tag ( -- )
677: output-tag-file
1.69 anton 678: prim prim-name 2@ 1+ type
1.17 anton 679: 127 emit
1.69 anton 680: space prim prim-name 2@ type space
1.17 anton 681: 1 emit
682: name-line @ 0 .r
683: ." ,0" cr ;
684:
1.10 anton 685: [IFDEF] documentation
686: : register-doc ( -- )
687: get-current documentation set-current
1.69 anton 688: prim prim-name 2@ nextname create
689: prim prim-name 2@ 2,
690: prim prim-stack-string 2@ condition-stack-effect 2,
691: prim prim-wordset 2@ 2,
692: prim prim-c-name 2@ condition-pronounciation 2,
693: prim prim-doc 2@ 2,
1.10 anton 694: set-current ;
695: [THEN]
1.67 anton 696:
697:
1.69 anton 698: \ combining instructions
699:
700: \ The input should look like this:
701:
702: \ lit_+ = lit +
703:
704: \ The output should look like this:
705:
706: \ I_lit_+:
707: \ {
708: \ DEF_CA
709: \ Cell _x_ip0;
710: \ Cell _x_sp0;
711: \ Cell _x_sp1;
712: \ NEXT_P0;
713: \ _x_ip0 = (Cell) IPTOS;
714: \ _x_sp0 = (Cell) spTOS;
715: \ INC_IP(1);
716: \ /* sp += 0; */
717: \ /* lit ( #w -- w ) */
718: \ /* */
719: \ NAME("lit")
720: \ {
721: \ Cell w;
722: \ w = (Cell) _x_ip0;
723: \ #ifdef VM_DEBUG
724: \ if (vm_debug) {
725: \ fputs(" w=", vm_out); printarg_w (w);
726: \ fputc('\n', vm_out);
727: \ }
728: \ #endif
729: \ {
730: \ #line 136 "./prim"
731: \ }
732: \ _x_sp1 = (Cell)w;
733: \ }
734: \ I_plus: /* + ( n1 n2 -- n ) */
735: \ /* */
736: \ NAME("+")
737: \ {
738: \ DEF_CA
739: \ Cell n1;
740: \ Cell n2;
741: \ Cell n;
742: \ NEXT_P0;
743: \ n1 = (Cell) _x_sp0;
744: \ n2 = (Cell) _x_sp1;
745: \ #ifdef VM_DEBUG
746: \ if (vm_debug) {
747: \ fputs(" n1=", vm_out); printarg_n (n1);
748: \ fputs(" n2=", vm_out); printarg_n (n2);
749: \ fputc('\n', vm_out);
750: \ }
751: \ #endif
752: \ {
753: \ #line 516 "./prim"
754: \ n = n1+n2;
755: \ }
756: \ NEXT_P1;
757: \ _x_sp0 = (Cell)n;
758: \ NEXT_P2;
759: \ }
760: \ NEXT_P1;
761: \ spTOS = (Cell)_x_sp0;
762: \ NEXT_P2;
763:
1.71 anton 764: 1000 constant max-combined
765: create combined-prims max-combined cells allot
766: variable num-combined
767:
768: create current-depth max-stacks cells allot
769: create max-depth max-stacks cells allot
1.72 ! anton 770: create min-depth max-stacks cells allot
1.71 anton 771:
772: : init-combined ( -- )
773: 0 num-combined !
774: current-depth max-stacks cells erase
1.72 ! anton 775: max-depth max-stacks cells erase
! 776: min-depth max-stacks cells erase
! 777: prim prim-effect-in prim prim-effect-in-end !
! 778: prim prim-effect-out prim prim-effect-out-end ! ;
1.71 anton 779:
780: : max! ( n addr -- )
781: tuck @ max swap ! ;
782:
1.72 ! anton 783: : min! ( n addr -- )
! 784: tuck @ min swap ! ;
! 785:
1.71 anton 786: : add-depths { p -- }
787: \ combine stack effect of p with *-depths
788: max-stacks 0 ?do
1.72 ! anton 789: current-depth i th @
! 790: p prim-stacks-in i th @ +
! 791: dup max-depth i th max!
! 792: p prim-stacks-out i th @ -
! 793: dup min-depth i th min!
! 794: current-depth i th !
1.71 anton 795: loop ;
796:
797: : add-prim ( addr u -- )
798: \ add primitive given by "addr u" to combined-prims
799: primitives search-wordlist s" unknown primitive" ?print-error
800: execute { p }
1.72 ! anton 801: p combined-prims num-combined @ th !
1.71 anton 802: 1 num-combined +!
803: p add-depths ;
804:
805: : compute-effects { q -- }
806: \ compute the stack effects of q from the depths
807: max-stacks 0 ?do
1.72 ! anton 808: max-depth i th @ dup
! 809: q prim-stacks-in i th !
! 810: current-depth i th @ -
! 811: q prim-stacks-out i th !
! 812: loop ;
! 813:
! 814: : make-effect-items { stack# items effect-endp -- }
! 815: \ effect-endp points to a pointer to the end of the current item-array
! 816: \ and has to be updated
! 817: stacks stack# th @ { stack }
! 818: items 0 +do
! 819: effect-endp @ { item }
! 820: i 0 <# #s stack stack-pointer 2@ holds [char] _ hold #> save-mem
! 821: item item-name 2!
! 822: stack item item-stack !
! 823: 0 item item-type !
! 824: i item item-offset !
! 825: item item-first on
! 826: item% %size effect-endp +!
! 827: loop ;
! 828:
! 829: : init-effects { q -- }
! 830: \ initialize effects field for FETCHES and STORES
! 831: max-stacks 0 ?do
! 832: i q prim-stacks-in i th @ q prim-effect-in-end make-effect-items
! 833: i q prim-stacks-out i th @ q prim-effect-out-end make-effect-items
1.71 anton 834: loop ;
835:
836: : process-combined ( -- )
1.72 ! anton 837: prim compute-effects
! 838: prim init-effects
! 839: output-combined perform ;
! 840:
! 841: \ C output
! 842:
! 843: : print-item { n stack -- }
! 844: \ print nth stack item name
! 845: ." _" stack stack-typename 2@ type space
! 846: stack stack-pointer 2@ type n 0 .r ;
! 847:
! 848: : print-declarations-combined ( -- )
! 849: max-stacks 0 ?do
! 850: max-depth i th @ min-depth i th @ - 0 +do
! 851: i stacks j th @ print-item ." ;" cr
! 852: loop
! 853: loop ;
! 854:
! 855: : output-c-combined ( -- )
! 856: print-entry cr
! 857: \ debugging messages just in constituents
! 858: ." {" cr
! 859: ." DEF_CA" cr
! 860: print-declarations-combined
! 861: ." NEXT_P0;" cr
! 862: flush-tos
! 863: fetches
! 864: ;
! 865:
! 866: : output-forth-combined ( -- )
! 867: ;
1.69 anton 868:
1.67 anton 869: \ the parser
870:
871: eof-char max-member \ the whole character set + EOF
872:
873: : getinput ( -- n )
874: rawinput @ endrawinput @ =
875: if
876: eof-char
877: else
878: cookedinput @ c@
879: endif ;
880:
881: :noname ( n -- )
882: dup bl > if
883: emit space
884: else
885: .
886: endif ;
887: print-token !
888:
889: : testchar? ( set -- f )
890: getinput member? ;
891: ' testchar? test-vector !
892:
893: : checksyncline ( -- )
894: \ when input points to a newline, check if the next line is a
895: \ sync line. If it is, perform the appropriate actions.
896: rawinput @ >r
897: s" #line " r@ over compare 0<> if
898: rdrop 1 line +! EXIT
899: endif
900: 0. r> 6 chars + 20 >number drop >r drop line ! r> ( c-addr )
901: dup c@ bl = if
902: char+ dup c@ [char] " <> abort" sync line syntax"
903: char+ dup 100 [char] " scan drop swap 2dup - save-mem filename 2!
904: char+
905: endif
906: dup c@ nl-char <> abort" sync line syntax"
907: skipsynclines @ if
908: dup char+ rawinput !
909: rawinput @ c@ cookedinput @ c!
910: endif
911: drop ;
912:
913: : ?nextchar ( f -- )
1.71 anton 914: s" syntax error, wrong char" ?print-error
1.67 anton 915: rawinput @ endrawinput @ <> if
916: rawinput @ c@
917: 1 chars rawinput +!
918: 1 chars cookedinput +!
919: nl-char = if
920: checksyncline
921: rawinput @ line-start !
922: endif
923: rawinput @ c@ cookedinput @ c!
924: endif ;
925:
926: : charclass ( set "name" -- )
927: ['] ?nextchar terminal ;
928:
929: : .. ( c1 c2 -- set )
930: ( creates a set that includes the characters c, c1<=c<=c2 )
931: empty copy-set
932: swap 1+ rot do
933: i over add-member
934: loop ;
935:
936: : ` ( -- terminal ) ( use: ` c )
937: ( creates anonymous terminal for the character c )
938: char singleton ['] ?nextchar make-terminal ;
939:
940: char a char z .. char A char Z .. union char _ singleton union charclass letter
941: char 0 char 9 .. charclass digit
942: bl singleton tab-char over add-member charclass white
943: nl-char singleton eof-char over add-member complement charclass nonl
944: nl-char singleton eof-char over add-member
945: char : over add-member complement charclass nocolonnl
946: bl 1+ maxchar .. char \ singleton complement intersection
947: charclass nowhitebq
948: bl 1+ maxchar .. charclass nowhite
949: char " singleton eof-char over add-member complement charclass noquote
950: nl-char singleton charclass nl
951: eof-char singleton charclass eof
952:
953:
954: (( letter (( letter || digit )) **
955: )) <- c-ident ( -- )
956:
957: (( ` # ?? (( letter || digit || ` : )) **
958: )) <- stack-ident ( -- )
959:
960: (( nowhitebq nowhite ** ))
961: <- forth-ident ( -- )
962:
963: Variable forth-flag
964: Variable c-flag
965:
966: (( (( ` e || ` E )) {{ start }} nonl **
967: {{ end evaluate }}
968: )) <- eval-comment ( ... -- ... )
969:
970: (( (( ` f || ` F )) {{ start }} nonl **
971: {{ end forth-flag @ IF type cr ELSE 2drop THEN }}
972: )) <- forth-comment ( -- )
973:
974: (( (( ` c || ` C )) {{ start }} nonl **
975: {{ end c-flag @ IF type cr ELSE 2drop THEN }}
976: )) <- c-comment ( -- )
977:
978: (( ` - nonl ** {{
979: forth-flag @ IF ." [ELSE]" cr THEN
980: c-flag @ IF ." #else" cr THEN }}
981: )) <- else-comment
982:
983: (( ` + {{ start }} nonl ** {{ end
984: dup
985: IF c-flag @
986: IF ." #ifdef HAS_" bounds ?DO I c@ toupper emit LOOP cr
987: THEN
988: forth-flag @
989: IF ." has? " type ." [IF]" cr THEN
990: ELSE 2drop
991: c-flag @ IF ." #endif" cr THEN
992: forth-flag @ IF ." [THEN]" cr THEN
993: THEN }}
994: )) <- if-comment
995:
996: (( (( eval-comment || forth-comment || c-comment || else-comment || if-comment )) ?? nonl ** )) <- comment-body
997:
998: (( ` \ comment-body nl )) <- comment ( -- )
999:
1000: (( {{ start }} stack-ident {{ end 2 pick init-item item% %size + }} white ** )) **
1001: <- stack-items
1002:
1.69 anton 1003: (( {{ prim prim-effect-in }} stack-items {{ prim prim-effect-in-end ! }}
1.67 anton 1004: ` - ` - white **
1.69 anton 1005: {{ prim prim-effect-out }} stack-items {{ prim prim-effect-out-end ! }}
1.67 anton 1006: )) <- stack-effect ( -- )
1007:
1.71 anton 1008: (( {{ prim create-prim }}
1.69 anton 1009: ` ( white ** {{ start }} stack-effect {{ end prim prim-stack-string 2! }} ` ) white **
1010: (( {{ start }} forth-ident {{ end prim prim-wordset 2! }} white **
1011: (( {{ start }} c-ident {{ end prim prim-c-name 2! }} )) ??
1012: )) ?? nl
1013: (( ` " ` " {{ start }} (( noquote ++ ` " )) ++ {{ end 1- prim prim-doc 2! }} ` " white ** nl )) ??
1014: {{ skipsynclines off line @ c-line ! filename 2@ c-filename 2! start }} (( nocolonnl nonl ** nl white ** )) ** {{ end prim prim-c-code 2! skipsynclines on }}
1.67 anton 1015: (( ` : white ** nl
1.69 anton 1016: {{ start }} (( nonl ++ nl white ** )) ++ {{ end prim prim-forth-code 2! }}
1.68 anton 1017: )) ?? {{ declarations compute-offsets printprim 1 function-number +! }}
1.67 anton 1018: (( nl || eof ))
1.69 anton 1019: )) <- simple-primitive ( -- )
1020:
1.71 anton 1021: (( {{ init-combined }}
1022: ` = (( white ++ {{ start }} forth-ident {{ end add-prim }} )) ++
1023: (( nl || eof )) {{ process-combined }}
1.69 anton 1024: )) <- combined-primitive
1025:
1.70 anton 1026: (( {{ make-prim to prim
1.69 anton 1027: line @ name-line ! filename 2@ name-filename 2!
1028: start }} forth-ident {{ end 2dup prim prim-name 2! prim prim-c-name 2! }} white ++
1029: (( simple-primitive || combined-primitive ))
1.67 anton 1030: )) <- primitive ( -- )
1031:
1032: (( (( comment || primitive || nl white ** )) ** eof ))
1033: parser primitives2something
1034: warnings @ [IF]
1035: .( parser generated ok ) cr
1036: [THEN]
1037:
1.69 anton 1038: : primfilter ( addr u -- )
1039: \ process the string at addr u
1040: over dup rawinput ! dup line-start ! cookedinput !
1041: + endrawinput !
1042: checksyncline
1043: primitives2something ;
1.8 pazsan 1044:
1.72 ! anton 1045: : process-file ( addr u xt-simple x-combined -- )
! 1046: output-combined ! output !
1.61 anton 1047: save-mem 2dup filename 2!
1.69 anton 1048: slurp-file
1.17 anton 1049: warnings @ if
1050: ." ------------ CUT HERE -------------" cr endif
1.69 anton 1051: primfilter ;
1.30 pazsan 1052:
1.72 ! anton 1053: \ : process ( xt -- )
! 1054: \ bl word count rot
! 1055: \ process-file ;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>