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