Annotation of gforth/prims2x.fs, revision 1.65
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.1 anton 22: \ This is not very nice (hard limits, no checking, assumes 1 chars = 1)
23:
24: \ Optimizations:
25: \ superfluous stores are removed. GCC removes the superfluous loads by itself
26: \ TOS and FTOS can be kept in register( variable)s.
27: \
28: \ Problems:
29: \ The TOS optimization is somewhat hairy. The problems by example:
30: \ 1) dup ( w -- w w ): w=TOS; sp-=1; sp[1]=w; TOS=w;
31: \ The store is not superfluous although the earlier opt. would think so
32: \ Alternatively: sp[0]=TOS; w=TOS; sp-=1; TOS=w;
33: \ 2) ( -- .. ): sp[0] = TOS; ... /* This additional store is necessary */
34: \ 3) ( .. -- ): ... TOS = sp[0]; /* as well as this load */
35: \ 4) ( -- ): /* but here they are unnecessary */
36: \ 5) Words that call NEXT themselves have to be done very carefully.
37: \
38: \ To do:
1.8 pazsan 39: \ add the store optimization for doubles
1.1 anton 40: \ regarding problem 1 above: It would be better (for over) to implement
41: \ the alternative
42:
1.3 pazsan 43: warnings off
44:
1.39 jwilke 45: [IFUNDEF] vocabulary \ we are executed just with kernel image
46: \ load the rest that is needed
47: \ (require fails because this file is needed from a
48: \ different directory with the wordlibraries)
49: include ./search.fs
50: include ./extend.fs
1.40 anton 51: [THEN]
52:
53: [IFUNDEF] environment?
1.39 jwilke 54: include ./environ.fs
55: [THEN]
1.25 pazsan 56:
1.49 anton 57: : struct% struct ; \ struct is redefined in gray
58:
1.39 jwilke 59: include ./gray.fs
1.1 anton 60:
61: 100 constant max-effect \ number of things on one side of a stack effect
62: 255 constant maxchar
63: maxchar 1+ constant eof-char
1.17 anton 64: #tab constant tab-char
65: #lf constant nl-char
1.1 anton 66:
1.18 anton 67: variable rawinput \ pointer to next character to be scanned
68: variable endrawinput \ pointer to the end of the input (the char after the last)
69: variable cookedinput \ pointer to the next char to be parsed
1.17 anton 70: variable line \ line number of char pointed to by input
1.65 ! anton 71: variable line-start \ pointer to start of current line (for error messages)
! 72: 0 line !
1.17 anton 73: 2variable filename \ filename of original input file
74: 0 0 filename 2!
1.25 pazsan 75: 2variable f-comment
76: 0 0 f-comment 2!
1.17 anton 77: variable skipsynclines \ are sync lines ("#line ...") invisible to the parser?
78: skipsynclines on
1.1 anton 79:
80: : start ( -- addr )
1.18 anton 81: cookedinput @ ;
1.1 anton 82:
83: : end ( addr -- addr u )
1.18 anton 84: cookedinput @ over - ;
1.1 anton 85:
1.63 anton 86: : quote ( -- )
87: [char] " emit ;
88:
1.1 anton 89: variable output \ xt ( -- ) of output word
90:
91: : printprim ( -- )
92: output @ execute ;
93:
1.49 anton 94: struct%
95: cell% 2* field stack-pointer \ stackpointer name
96: cell% 2* field stack-cast \ cast string for assignments to stack elements
1.53 anton 97: cell% field stack-in-index-xt \ ( in-size item -- in-index )
1.49 anton 98: cell% field stack-in \ number of stack items in effect in
99: cell% field stack-out \ number of stack items in effect out
100: end-struct stack%
101:
1.53 anton 102: struct%
103: cell% 2* field item-name \ name, excluding stack prefixes
104: cell% field item-stack \ descriptor for the stack used, 0 is default
105: cell% field item-type \ descriptor for the item type
106: cell% field item-offset \ offset in stack items, 0 for the deepest element
107: end-struct item%
108:
109: struct%
110: cell% 2* field type-c-name
111: cell% field type-stack \ default stack
112: cell% field type-size \ size of type in stack items
113: cell% field type-fetch \ xt of fetch code generator ( item -- )
114: cell% field type-store \ xt of store code generator ( item -- )
115: end-struct type%
116:
117: : stack-in-index ( in-size item -- in-index )
118: item-offset @ - 1- ;
119:
120: : inst-in-index ( in-size item -- in-index )
121: nip dup item-offset @ swap item-type @ type-size @ + 1- ;
122:
1.49 anton 123: : make-stack ( addr-ptr u1 addr-cast u2 "stack-name" -- )
124: create stack% %allot >r
125: save-mem r@ stack-cast 2!
1.53 anton 126: save-mem r@ stack-pointer 2!
127: ['] stack-in-index r> stack-in-index-xt ! ;
1.49 anton 128:
129: s" sp" save-mem s" (Cell)" make-stack data-stack
130: s" fp" save-mem s" " make-stack fp-stack
1.51 anton 131: s" rp" save-mem s" (Cell)" make-stack return-stack
1.62 anton 132: s" IP" save-mem s" error don't use # on results" make-stack inst-stream
1.53 anton 133: ' inst-in-index inst-stream stack-in-index-xt !
1.49 anton 134: \ !! initialize stack-in and stack-out
135:
136: \ stack items
137:
138: : init-item ( addr u addr1 -- )
139: \ initialize item at addr1 with name addr u
140: \ !! remove stack prefix
141: dup item% %size erase
142: item-name 2! ;
143:
1.64 anton 144: : map-items { addr end xt -- }
145: \ perform xt for all items in array addr...end
146: end addr ?do
147: i xt execute
148: item% %size +loop ;
149:
1.49 anton 150: \ various variables for storing stuff of one primitive
1.1 anton 151:
152: 2variable forth-name
153: 2variable wordset
154: 2variable c-name
155: 2variable doc
156: 2variable c-code
157: 2variable forth-code
158: 2variable stack-string
1.49 anton 159: create effect-in max-effect item% %size * allot
160: create effect-out max-effect item% %size * allot
1.1 anton 161: variable effect-in-end ( pointer )
162: variable effect-out-end ( pointer )
1.17 anton 163: variable c-line
164: 2variable c-filename
165: variable name-line
166: 2variable name-filename
167: 2variable last-name-filename
1.1 anton 168:
1.14 pazsan 169: variable primitive-number -10 primitive-number !
1.30 pazsan 170: Variable function-number 0 function-number !
1.1 anton 171:
172: \ for several reasons stack items of a word are stored in a wordlist
173: \ since neither forget nor marker are implemented yet, we make a new
174: \ wordlist for every word and store it in the variable items
175: variable items
176:
177: \ a few more set ops
178:
179: : bit-equivalent ( w1 w2 -- w3 )
180: xor invert ;
181:
182: : complement ( set1 -- set2 )
183: empty ['] bit-equivalent binary-set-operation ;
184:
185: \ the parser
186:
187: eof-char max-member \ the whole character set + EOF
188:
189: : getinput ( -- n )
1.18 anton 190: rawinput @ endrawinput @ =
1.1 anton 191: if
1.18 anton 192: eof-char
1.1 anton 193: else
1.18 anton 194: cookedinput @ c@
1.1 anton 195: endif ;
196:
197: :noname ( n -- )
198: dup bl > if
199: emit space
200: else
201: .
202: endif ;
203: print-token !
204:
205: : testchar? ( set -- f )
206: getinput member? ;
207: ' testchar? test-vector !
208:
1.17 anton 209: : checksyncline ( -- )
210: \ when input points to a newline, check if the next line is a
211: \ sync line. If it is, perform the appropriate actions.
1.18 anton 212: rawinput @ >r
1.17 anton 213: s" #line " r@ over compare 0<> if
214: rdrop 1 line +! EXIT
215: endif
216: 0. r> 6 chars + 20 >number drop >r drop line ! r> ( c-addr )
217: dup c@ bl = if
218: char+ dup c@ [char] " <> abort" sync line syntax"
1.24 anton 219: char+ dup 100 [char] " scan drop swap 2dup - save-mem filename 2!
1.17 anton 220: char+
221: endif
222: dup c@ nl-char <> abort" sync line syntax"
223: skipsynclines @ if
1.18 anton 224: dup char+ rawinput !
225: rawinput @ c@ cookedinput @ c!
1.17 anton 226: endif
227: drop ;
228:
1.65 ! anton 229: : print-error-line ( -- )
! 230: \ print the current line and position
! 231: line-start @ endrawinput @ over - 2dup nl-char scan drop nip ( start end )
! 232: over - type cr
! 233: line-start @ rawinput @ over - typewhite ." ^" cr ;
! 234:
1.1 anton 235: : ?nextchar ( f -- )
1.17 anton 236: ?not? if
1.65 ! anton 237: outfile-id >r try
! 238: stderr to outfile-id
! 239: filename 2@ type ." :" line @ 0 .r ." : syntax error, wrong char:"
! 240: getinput . cr
! 241: print-error-line
! 242: 0
! 243: recover endtry
! 244: r> to outfile-id throw
1.17 anton 245: abort
246: endif
1.18 anton 247: rawinput @ endrawinput @ <> if
248: rawinput @ c@
249: 1 chars rawinput +!
250: 1 chars cookedinput +!
1.17 anton 251: nl-char = if
252: checksyncline
1.65 ! anton 253: rawinput @ line-start !
1.17 anton 254: endif
1.18 anton 255: rawinput @ c@ cookedinput @ c!
1.17 anton 256: endif ;
1.1 anton 257:
258: : charclass ( set "name" -- )
259: ['] ?nextchar terminal ;
260:
261: : .. ( c1 c2 -- set )
262: ( creates a set that includes the characters c, c1<=c<=c2 )
263: empty copy-set
264: swap 1+ rot do
265: i over add-member
266: loop ;
267:
268: : ` ( -- terminal ) ( use: ` c )
269: ( creates anonymous terminal for the character c )
1.21 anton 270: char singleton ['] ?nextchar make-terminal ;
1.1 anton 271:
272: char a char z .. char A char Z .. union char _ singleton union charclass letter
273: char 0 char 9 .. charclass digit
1.45 anton 274: bl singleton tab-char over add-member charclass white
1.1 anton 275: nl-char singleton eof-char over add-member complement charclass nonl
1.46 pazsan 276: nl-char singleton eof-char over add-member
277: char : over add-member complement charclass nocolonnl
278: bl 1+ maxchar .. char \ singleton complement intersection
279: charclass nowhitebq
280: bl 1+ maxchar .. charclass nowhite
1.1 anton 281: char " singleton eof-char over add-member complement charclass noquote
282: nl-char singleton charclass nl
283: eof-char singleton charclass eof
284:
285:
286: (( letter (( letter || digit )) **
1.51 anton 287: )) <- c-ident ( -- )
288:
289: (( ` # ?? (( letter || digit || ` : )) **
290: )) <- stack-ident ( -- )
1.1 anton 291:
1.46 pazsan 292: (( nowhitebq nowhite ** ))
1.51 anton 293: <- forth-ident ( -- )
1.1 anton 294:
1.42 jwilke 295: Variable forth-flag
296: Variable c-flag
297:
1.54 anton 298: (( (( ` e || ` E )) {{ start }} nonl **
299: {{ end evaluate }}
300: )) <- eval-comment ( ... -- ... )
301:
1.42 jwilke 302: (( (( ` f || ` F )) {{ start }} nonl **
303: {{ end forth-flag @ IF type cr ELSE 2drop THEN }}
304: )) <- forth-comment ( -- )
305:
306: (( (( ` c || ` C )) {{ start }} nonl **
307: {{ end c-flag @ IF type cr ELSE 2drop THEN }}
308: )) <- c-comment ( -- )
309:
1.43 jwilke 310: (( ` - nonl ** {{
311: forth-flag @ IF ." [ELSE]" cr THEN
312: c-flag @ IF ." #else" cr THEN }}
313: )) <- else-comment
314:
315: (( ` + {{ start }} nonl ** {{ end
316: dup
317: IF c-flag @
318: IF ." #ifdef HAS_" bounds ?DO I c@ toupper emit LOOP cr
319: THEN
320: forth-flag @
321: IF ." has? " type ." [IF]" cr THEN
322: ELSE 2drop
1.46 pazsan 323: c-flag @ IF ." #endif" cr THEN
324: forth-flag @ IF ." [THEN]" cr THEN
1.43 jwilke 325: THEN }}
326: )) <- if-comment
327:
1.54 anton 328: (( (( eval-comment || forth-comment || c-comment || else-comment || if-comment )) ?? nonl ** )) <- comment-body
1.42 jwilke 329:
1.43 jwilke 330: (( ` \ comment-body nl )) <- comment ( -- )
1.1 anton 331:
1.51 anton 332: (( {{ start }} stack-ident {{ end 2 pick init-item item% %size + }} white ** )) **
333: <- stack-items
334:
335: (( {{ effect-in }} stack-items {{ effect-in-end ! }}
1.45 anton 336: ` - ` - white **
1.51 anton 337: {{ effect-out }} stack-items {{ effect-out-end ! }}
1.1 anton 338: )) <- stack-effect ( -- )
339:
1.57 anton 340: (( {{ s" " doc 2! s" " forth-code 2! s" " wordset 2! }}
1.17 anton 341: (( {{ line @ name-line ! filename 2@ name-filename 2! }}
1.51 anton 342: {{ start }} forth-ident {{ end 2dup forth-name 2! c-name 2! }} white ++
1.45 anton 343: ` ( white ** {{ start }} stack-effect {{ end stack-string 2! }} ` ) white **
1.57 anton 344: (( {{ start }} forth-ident {{ end wordset 2! }} white **
345: (( {{ start }} c-ident {{ end c-name 2! }} )) ??
346: )) ?? nl
1.1 anton 347: ))
1.58 anton 348: (( ` " ` " {{ start }} (( noquote ++ ` " )) ++ {{ end 1- doc 2! }} ` " white ** nl )) ??
1.59 anton 349: {{ skipsynclines off line @ c-line ! filename 2@ c-filename 2! start }} (( nocolonnl nonl ** nl white ** )) ** {{ end c-code 2! skipsynclines on }}
1.58 anton 350: (( ` : white ** nl
1.59 anton 351: {{ start }} (( nonl ++ nl white ** )) ++ {{ end forth-code 2! }}
1.46 pazsan 352: )) ?? {{ printprim }}
1.1 anton 353: (( nl || eof ))
354: )) <- primitive ( -- )
355:
1.59 anton 356: (( (( comment || primitive || nl white ** )) ** eof ))
1.1 anton 357: parser primitives2something
1.3 pazsan 358: warnings @ [IF]
1.1 anton 359: .( parser generated ok ) cr
1.3 pazsan 360: [THEN]
1.1 anton 361:
362: : primfilter ( file-id xt -- )
363: \ fileid is for the input file, xt ( -- ) is for the output word
364: output !
1.65 ! anton 365: here dup rawinput ! dup line-start ! cookedinput !
1.41 anton 366: here unused rot read-file throw
367: dup here + endrawinput !
368: allot
1.2 pazsan 369: align
1.17 anton 370: checksyncline
1.18 anton 371: \ begin
372: \ getinput dup eof-char = ?EXIT emit true ?nextchar
373: \ again ;
1.1 anton 374: primitives2something ;
375:
376: \ types
377:
1.49 anton 378: : stack-access ( n stack -- )
379: \ print a stack access at index n of stack
380: stack-pointer 2@ type
381: dup
382: if
383: ." [" 0 .r ." ]"
384: else
385: drop ." TOS"
386: endif ;
1.1 anton 387:
1.53 anton 388: : item-in-index { item -- n }
1.49 anton 389: \ n is the index of item (in the in-effect)
1.53 anton 390: item item-stack @ dup >r stack-in @ ( in-size r:stack )
391: item r> stack-in-index-xt @ execute ;
1.1 anton 392:
393: : fetch-single ( item -- )
1.49 anton 394: \ fetch a single stack item from its stack
1.1 anton 395: >r
1.8 pazsan 396: r@ item-name 2@ type
397: ." = ("
1.1 anton 398: r@ item-type @ type-c-name 2@ type ." ) "
1.49 anton 399: r@ item-in-index r@ item-stack @ stack-access
400: ." ;" cr
1.1 anton 401: rdrop ;
402:
403: : fetch-double ( item -- )
1.49 anton 404: \ fetch a double stack item from its stack
1.1 anton 405: >r
1.20 anton 406: ." FETCH_DCELL("
407: r@ item-name 2@ type ." , "
1.61 anton 408: r@ item-in-index r@ item-stack @ 2dup ." (Cell)" stack-access
409: ." , " -1 under+ ." (Cell)" stack-access
1.20 anton 410: ." );" cr
1.1 anton 411: rdrop ;
412:
1.49 anton 413: : same-as-in? ( item -- f )
414: \ f is true iff the offset and stack of item is the same as on input
1.1 anton 415: >r
416: r@ item-name 2@ items @ search-wordlist 0=
1.8 pazsan 417: abort" bug"
1.1 anton 418: execute @
419: dup r@ =
420: if \ item first appeared in output
421: drop false
422: else
1.49 anton 423: dup item-stack @ r@ item-stack @ =
424: swap item-offset @ r@ item-offset @ = and
1.1 anton 425: endif
426: rdrop ;
427:
1.49 anton 428: : item-out-index ( item -- n )
429: \ n is the index of item (in the in-effect)
430: >r r@ item-stack @ stack-out @ r> item-offset @ - 1- ;
1.31 pazsan 431:
1.1 anton 432: : really-store-single ( item -- )
433: >r
1.49 anton 434: r@ item-out-index r@ item-stack @ stack-access ." = "
435: r@ item-stack @ stack-cast 2@ type
1.1 anton 436: r@ item-name 2@ type ." ;"
437: rdrop ;
438:
439: : store-single ( item -- )
440: >r
1.49 anton 441: r@ same-as-in?
1.1 anton 442: if
1.49 anton 443: r@ item-in-index 0= r@ item-out-index 0= xor
1.1 anton 444: if
1.49 anton 445: ." IF_" r@ item-stack @ stack-pointer 2@ type
446: ." TOS(" r@ really-store-single ." );" cr
1.1 anton 447: endif
448: else
449: r@ really-store-single cr
450: endif
451: rdrop ;
452:
453: : store-double ( item -- )
454: \ !! store optimization is not performed, because it is not yet needed
455: >r
1.20 anton 456: ." STORE_DCELL(" r@ item-name 2@ type ." , "
1.49 anton 457: r@ item-out-index r@ item-stack @ 2dup stack-access
458: ." , " -1 under+ stack-access
1.20 anton 459: ." );" cr
1.1 anton 460: rdrop ;
461:
1.54 anton 462: : single ( -- xt1 xt2 n )
463: ['] fetch-single ['] store-single 1 ;
1.1 anton 464:
1.54 anton 465: : double ( -- xt1 xt2 n )
466: ['] fetch-double ['] store-double 2 ;
1.1 anton 467:
468: : s, ( addr u -- )
469: \ allocate a string
470: here swap dup allot move ;
471:
1.50 anton 472: wordlist constant prefixes
473:
474: : declare ( addr "name" -- )
475: \ remember that there is a stack item at addr called name
476: create , ;
477:
478: : !default ( w addr -- )
479: dup @ if
480: 2drop \ leave nonzero alone
481: else
482: !
483: endif ;
484:
485: : create-type { addr u xt1 xt2 n stack -- } ( "prefix" -- )
1.49 anton 486: \ describes a type
487: \ addr u specifies the C type name
488: \ stack effect entries of the type start with prefix
489: create type% %allot >r
490: addr u save-mem r@ type-c-name 2!
491: xt1 r@ type-fetch !
492: xt2 r@ type-store !
493: n r@ type-size !
494: stack r@ type-stack !
495: rdrop ;
1.1 anton 496:
1.54 anton 497: : type-prefix ( xt1 xt2 n stack "prefix" -- )
1.50 anton 498: create-type
499: does> ( item -- )
500: \ initialize item
501: { item typ }
502: typ item item-type !
503: typ type-stack @ item item-stack !default
504: item item-name 2@ items @ search-wordlist 0= if \ new name
505: item item-name 2@ 2dup nextname item declare
506: typ type-c-name 2@ type space type ." ;" cr
507: else
508: drop
509: endif ;
510:
511: : execute-prefix ( item addr1 u1 -- )
512: \ execute the word ( item -- ) associated with the longest prefix
513: \ of addr1 u1
514: 0 swap ?do
515: dup i prefixes search-wordlist
516: if \ ok, we have the type ( item addr1 xt )
517: nip execute
518: UNLOOP EXIT
519: endif
520: -1 s+loop
521: \ we did not find a type, abort
522: true abort" unknown prefix" ;
1.1 anton 523:
524: : declaration ( item -- )
1.50 anton 525: dup item-name 2@ execute-prefix ;
1.1 anton 526:
1.64 anton 527: : declaration-list ( addr1 addr2 -- )
528: ['] declaration map-items ;
529:
530: : declarations ( -- )
531: wordlist dup items ! set-current
532: effect-in effect-in-end @ declaration-list
533: effect-out effect-out-end @ declaration-list ;
534:
1.51 anton 535: : stack-prefix ( stack "prefix" -- )
536: name tuck nextname create ( stack length ) 2,
537: does> ( item -- )
538: 2@ { item stack prefix-length }
539: item item-name 2@ prefix-length /string item item-name 2!
540: stack item item-stack !
541: item declaration ;
1.1 anton 542:
543: \ offset computation
544: \ the leftmost (i.e. deepest) item has offset 0
545: \ the rightmost item has the highest offset
546:
1.49 anton 547: : compute-offset { item xt -- }
548: \ xt specifies in/out; update stack-in/out and set item-offset
549: item item-type @ type-size @
550: item item-stack @ xt execute dup @ >r +!
551: r> item item-offset ! ;
552:
1.64 anton 553: : compute-offset-in ( addr1 addr2 -- )
554: ['] stack-in compute-offset ;
555:
556: : compute-offset-out ( addr1 addr2 -- )
557: ['] stack-out compute-offset ;
1.49 anton 558:
559: : clear-stack { -- }
560: dup stack-in off stack-out off ;
1.1 anton 561:
562: : compute-offsets ( -- )
1.51 anton 563: data-stack clear-stack fp-stack clear-stack return-stack clear-stack
1.53 anton 564: inst-stream clear-stack
1.64 anton 565: effect-in effect-in-end @ ['] compute-offset-in map-items
566: effect-out effect-out-end @ ['] compute-offset-out map-items
1.53 anton 567: inst-stream stack-out @ 0<> abort" # can only be on the input side" ;
1.49 anton 568:
569: : flush-a-tos { stack -- }
570: stack stack-out @ 0<> stack stack-in @ 0= and
571: if
572: ." IF_" stack stack-pointer 2@ 2dup type ." TOS("
573: 2dup type ." [0] = " type ." TOS);" cr
574: endif ;
1.1 anton 575:
576: : flush-tos ( -- )
1.51 anton 577: data-stack flush-a-tos
578: fp-stack flush-a-tos
579: return-stack flush-a-tos ;
1.49 anton 580:
581: : fill-a-tos { stack -- }
582: stack stack-out @ 0= stack stack-in @ 0<> and
583: if
584: ." IF_" stack stack-pointer 2@ 2dup type ." TOS("
585: 2dup type ." TOS = " type ." [0]);" cr
586: endif ;
1.1 anton 587:
588: : fill-tos ( -- )
1.53 anton 589: \ !! inst-stream for prefetching?
1.51 anton 590: fp-stack fill-a-tos
591: data-stack fill-a-tos
592: return-stack fill-a-tos ;
1.49 anton 593:
594: : fetch ( addr -- )
595: dup item-type @ type-fetch @ execute ;
1.1 anton 596:
597: : fetches ( -- )
1.64 anton 598: effect-in effect-in-end @ ['] fetch map-items ;
1.49 anton 599:
600: : stack-pointer-update { stack -- }
601: \ stack grow downwards
602: stack stack-in @ stack stack-out @ -
603: ?dup-if \ this check is not necessary, gcc would do this for us
604: stack stack-pointer 2@ type ." += " 0 .r ." ;" cr
605: endif ;
1.1 anton 606:
1.55 anton 607: : inst-pointer-update ( -- )
608: inst-stream stack-in @ ?dup-if
609: ." INC_IP(" 0 .r ." );" cr
610: endif ;
611:
1.1 anton 612: : stack-pointer-updates ( -- )
1.55 anton 613: inst-pointer-update
1.51 anton 614: data-stack stack-pointer-update
615: fp-stack stack-pointer-update
616: return-stack stack-pointer-update ;
1.1 anton 617:
618: : store ( item -- )
619: \ f is true if the item should be stored
620: \ f is false if the store is probably not necessary
1.49 anton 621: dup item-type @ type-store @ execute ;
1.1 anton 622:
623: : stores ( -- )
1.64 anton 624: effect-out effect-out-end @ ['] store map-items ;
1.8 pazsan 625:
1.52 anton 626: : output-c-tail ( -- )
627: \ the final part of the generated C code
628: ." NEXT_P1;" cr
629: stores
630: fill-tos
631: ." NEXT_P2;" cr ;
632:
633: : type-c ( c-addr u -- )
634: \ like TYPE, but replaces "TAIL;" with tail code
635: begin ( c-addr1 u1 )
636: 2dup s" TAIL;" search
637: while ( c-addr1 u1 c-addr3 u3 )
638: 2dup 2>r drop nip over - type
639: output-c-tail
640: 2r> 5 /string
641: \ !! resync #line missing
642: repeat
643: 2drop type ;
644:
1.63 anton 645: : print-type-prefix ( type -- )
646: body> >head .name ;
647:
648: : print-debug-arg { item -- }
649: ." fputs(" quote space item item-name 2@ type ." =" quote ." , vm_out); "
650: ." printarg_" item item-type @ print-type-prefix
651: ." (" item item-name 2@ type ." );" cr ;
652:
653: : print-debug-args ( -- )
654: ." #ifdef VM_DEBUG" cr
655: ." if (vm_debug) {" cr
1.64 anton 656: effect-in effect-in-end @ ['] print-debug-arg map-items
1.63 anton 657: ." fputc('\n', vm_out);" cr
658: ." }" cr
659: ." #endif" cr ;
660:
1.43 jwilke 661: : output-c ( -- )
1.45 anton 662: ." I_" c-name 2@ type ." : /* " forth-name 2@ type ." ( " stack-string 2@ type ." ) */" cr
1.1 anton 663: ." /* " doc 2@ type ." */" cr
1.63 anton 664: ." NAME(" quote forth-name 2@ type quote ." )" cr \ debugging
1.1 anton 665: ." {" cr
666: ." DEF_CA" cr
667: declarations
668: compute-offsets \ for everything else
1.13 anton 669: ." NEXT_P0;" cr
670: flush-tos
1.1 anton 671: fetches
1.63 anton 672: print-debug-args
1.13 anton 673: stack-pointer-updates
1.1 anton 674: ." {" cr
1.63 anton 675: ." #line " c-line @ . quote c-filename 2@ type quote cr
1.52 anton 676: c-code 2@ type-c
1.1 anton 677: ." }" cr
1.52 anton 678: output-c-tail
1.1 anton 679: ." }" cr
680: cr
681: ;
682:
1.56 anton 683: : disasm-arg { item -- }
684: item item-stack @ inst-stream = if
1.63 anton 685: ." fputc(' ', vm_out); "
686: ." printarg_" item item-type @ print-type-prefix
687: ." ((" item item-type @ type-c-name 2@ type ." )"
688: ." ip[" item item-offset @ 1+ 0 .r ." ]);" cr
1.56 anton 689: endif ;
690:
691: : disasm-args ( -- )
1.64 anton 692: effect-in effect-in-end @ ['] disasm-arg map-items ;
1.56 anton 693:
694: : output-disasm ( -- )
695: \ generate code for disassembling VM instructions
696: ." if (ip[0] == prim[" function-number @ 0 .r ." ]) {" cr
1.63 anton 697: ." fputs(" quote forth-name 2@ type quote ." , vm_out);" cr
1.56 anton 698: ." /* " declarations ." */" cr
699: compute-offsets
700: disasm-args
701: ." ip += " inst-stream stack-in @ 1+ 0 .r ." ;" cr
702: ." } else "
703: 1 function-number +! ;
704:
1.60 anton 705: : gen-arg-parm { item -- }
706: item item-stack @ inst-stream = if
707: ." , " item item-type @ type-c-name 2@ type space
708: item item-name 2@ type
709: endif ;
710:
711: : gen-args-parm ( -- )
1.64 anton 712: effect-in effect-in-end @ ['] gen-arg-parm map-items ;
1.60 anton 713:
714: : gen-arg-gen { item -- }
715: item item-stack @ inst-stream = if
716: ." genarg_" item item-type @ print-type-prefix
717: ." (ctp, " item item-name 2@ type ." );" cr
718: endif ;
719:
720: : gen-args-gen ( -- )
1.64 anton 721: effect-in effect-in-end @ ['] gen-arg-gen map-items ;
1.60 anton 722:
723: : output-gen ( -- )
724: \ generate C code for generating VM instructions
725: ." /* " declarations ." */" cr
726: compute-offsets
727: ." void gen_" c-name 2@ type ." (Inst **ctp" gen-args-parm ." )" cr
728: ." {" cr
729: ." gen_inst(ctp, vm_prim[" function-number @ 0 .r ." ]);" cr
730: gen-args-gen
731: ." }" cr
732: 1 function-number +! ;
733:
1.49 anton 734: : stack-used? { stack -- f }
735: stack stack-in @ stack stack-out @ or 0<> ;
1.44 jwilke 736:
1.30 pazsan 737: : output-funclabel ( -- )
738: 1 function-number +!
739: ." &I_" c-name 2@ type ." ," cr ;
740:
741: : output-forthname ( -- )
742: 1 function-number +!
743: '" emit forth-name 2@ type '" emit ." ," cr ;
744:
745: : output-c-func ( -- )
1.44 jwilke 746: \ used for word libraries
1.30 pazsan 747: 1 function-number +!
1.44 jwilke 748: ." Cell * I_" c-name 2@ type ." (Cell *SP, Cell **FP) /* " forth-name 2@ type
1.30 pazsan 749: ." ( " stack-string 2@ type ." ) */" cr
750: ." /* " doc 2@ type ." */" cr
1.63 anton 751: ." NAME(" quote forth-name 2@ type quote ." )" cr
1.30 pazsan 752: \ debugging
753: ." {" cr
754: declarations
755: compute-offsets \ for everything else
1.53 anton 756: inst-stream stack-used? IF ." Cell *ip=IP;" cr THEN
1.51 anton 757: data-stack stack-used? IF ." Cell *sp=SP;" cr THEN
758: fp-stack stack-used? IF ." Cell *fp=*FP;" cr THEN
759: return-stack stack-used? IF ." Cell *rp=*RP;" cr THEN
1.30 pazsan 760: flush-tos
761: fetches
762: stack-pointer-updates
1.49 anton 763: fp-stack stack-used? IF ." *FP=fp;" cr THEN
1.30 pazsan 764: ." {" cr
1.63 anton 765: ." #line " c-line @ . quote c-filename 2@ type quote cr
1.30 pazsan 766: c-code 2@ type
767: ." }" cr
768: stores
769: fill-tos
1.44 jwilke 770: ." return (sp);" cr
1.30 pazsan 771: ." }" cr
772: cr ;
773:
1.43 jwilke 774: : output-label ( -- )
1.34 pazsan 775: ." (Label)&&I_" c-name 2@ type ." ," cr
776: -1 primitive-number +! ;
1.1 anton 777:
1.43 jwilke 778: : output-alias ( -- )
1.38 pazsan 779: ( primitive-number @ . ." alias " ) ." Primitive " forth-name 2@ type cr
1.34 pazsan 780: -1 primitive-number +! ;
1.1 anton 781:
1.43 jwilke 782: : output-forth ( -- )
1.30 pazsan 783: forth-code @ 0=
784: IF \ output-alias
1.28 jwilke 785: \ this is bad for ec: an alias is compiled if tho word does not exist!
786: \ JAW
1.30 pazsan 787: ELSE ." : " forth-name 2@ type ." ( "
1.49 anton 788: stack-string 2@ type ." )" cr
1.30 pazsan 789: forth-code 2@ type cr
790: -1 primitive-number +!
791: THEN ;
1.10 anton 792:
1.17 anton 793: : output-tag-file ( -- )
794: name-filename 2@ last-name-filename 2@ compare if
795: name-filename 2@ last-name-filename 2!
796: #ff emit cr
797: name-filename 2@ type
798: ." ,0" cr
799: endif ;
800:
801: : output-tag ( -- )
802: output-tag-file
803: forth-name 2@ 1+ type
804: 127 emit
805: space forth-name 2@ type space
806: 1 emit
807: name-line @ 0 .r
808: ." ,0" cr ;
809:
1.10 anton 810: [IFDEF] documentation
811: : register-doc ( -- )
812: get-current documentation set-current
813: forth-name 2@ nextname create
814: forth-name 2@ 2,
1.15 anton 815: stack-string 2@ condition-stack-effect 2,
1.10 anton 816: wordset 2@ 2,
1.15 anton 817: c-name 2@ condition-pronounciation 2,
1.10 anton 818: doc 2@ 2,
819: set-current ;
820: [THEN]
1.8 pazsan 821:
1.1 anton 822: : process-file ( addr u xt -- )
1.17 anton 823: >r
1.61 anton 824: save-mem 2dup filename 2!
1.30 pazsan 825: 0 function-number !
1.17 anton 826: r/o open-file abort" cannot open file"
827: warnings @ if
828: ." ------------ CUT HERE -------------" cr endif
829: r> primfilter ;
1.30 pazsan 830:
831: : process ( xt -- )
832: bl word count rot
833: process-file ;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>