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