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