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