1: \ converts primitives to, e.g., C code
2:
3: \ Copyright (C) 1995 Free Software Foundation, Inc.
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
19: \ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20:
21:
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:
39: \ add the store optimization for doubles
40: \ regarding problem 1 above: It would be better (for over) to implement
41: \ the alternative
42:
43: warnings off
44:
45: include extend.fs
46:
47: \ require interpretation.fs
48: require debugging.fs
49: [IFUNDEF] vocabulary include search-order.fs [THEN]
50: [IFUNDEF] environment? include environ.fs [THEN]
51: include gray.fs
52:
53: 100 constant max-effect \ number of things on one side of a stack effect
54: 255 constant maxchar
55: maxchar 1+ constant eof-char
56: #tab constant tab-char
57: #lf constant nl-char
58:
59: : read-whole-file ( c-addr1 file-id -- c-addr2 )
60: \ reads the contents of the file file-id puts it into memory at c-addr1
61: \ c-addr2 is the first address after the file block
62: >r dup $7fffffff r> read-file throw + ;
63:
64: variable rawinput \ pointer to next character to be scanned
65: variable endrawinput \ pointer to the end of the input (the char after the last)
66: variable cookedinput \ pointer to the next char to be parsed
67: variable line \ line number of char pointed to by input
68: 1 line !
69: 2variable filename \ filename of original input file
70: 0 0 filename 2!
71: 2variable f-comment
72: 0 0 f-comment 2!
73: variable skipsynclines \ are sync lines ("#line ...") invisible to the parser?
74: skipsynclines on
75:
76: : start ( -- addr )
77: cookedinput @ ;
78:
79: : end ( addr -- addr u )
80: cookedinput @ over - ;
81:
82: variable output \ xt ( -- ) of output word
83:
84: : printprim ( -- )
85: output @ execute ;
86:
87: : field
88: <builds-field ( n1 n2 -- n3 )
89: does> ( addr1 -- addr2 )
90: @ + ;
91:
92: : const-field
93: <builds-field ( n1 n2 -- n3 )
94: does> ( addr -- w )
95: @ + @ ;
96:
97: struct
98: 2 cells field item-name
99: cell field item-d-offset
100: cell field item-f-offset
101: cell field item-type
102: constant item-descr
103:
104: 2variable forth-name
105: 2variable wordset
106: 2variable c-name
107: 2variable doc
108: 2variable c-code
109: 2variable forth-code
110: 2variable stack-string
111: create effect-in max-effect item-descr * allot
112: create effect-out max-effect item-descr * allot
113: variable effect-in-end ( pointer )
114: variable effect-out-end ( pointer )
115: 2variable effect-in-size
116: 2variable effect-out-size
117: variable c-line
118: 2variable c-filename
119: variable name-line
120: 2variable name-filename
121: 2variable last-name-filename
122:
123: variable primitive-number -10 primitive-number !
124:
125: \ for several reasons stack items of a word are stored in a wordlist
126: \ since neither forget nor marker are implemented yet, we make a new
127: \ wordlist for every word and store it in the variable items
128: variable items
129:
130: \ a few more set ops
131:
132: : bit-equivalent ( w1 w2 -- w3 )
133: xor invert ;
134:
135: : complement ( set1 -- set2 )
136: empty ['] bit-equivalent binary-set-operation ;
137:
138: \ the parser
139:
140: eof-char max-member \ the whole character set + EOF
141:
142: : getinput ( -- n )
143: rawinput @ endrawinput @ =
144: if
145: eof-char
146: else
147: cookedinput @ c@
148: endif ;
149:
150: :noname ( n -- )
151: dup bl > if
152: emit space
153: else
154: .
155: endif ;
156: print-token !
157:
158: : testchar? ( set -- f )
159: getinput member? ;
160: ' testchar? test-vector !
161:
162: : checksyncline ( -- )
163: \ when input points to a newline, check if the next line is a
164: \ sync line. If it is, perform the appropriate actions.
165: rawinput @ >r
166: s" #line " r@ over compare 0<> if
167: rdrop 1 line +! EXIT
168: endif
169: 0. r> 6 chars + 20 >number drop >r drop line ! r> ( c-addr )
170: dup c@ bl = if
171: char+ dup c@ [char] " <> abort" sync line syntax"
172: char+ dup 100 [char] " scan drop swap 2dup - save-mem filename 2!
173: char+
174: endif
175: dup c@ nl-char <> abort" sync line syntax"
176: skipsynclines @ if
177: dup char+ rawinput !
178: rawinput @ c@ cookedinput @ c!
179: endif
180: drop ;
181:
182: : ?nextchar ( f -- )
183: ?not? if
184: filename 2@ type ." :" line @ 0 .r ." : syntax error, wrong char:"
185: getinput . cr
186: rawinput @ endrawinput @ over - 100 min type cr
187: abort
188: endif
189: rawinput @ endrawinput @ <> if
190: rawinput @ c@
191: 1 chars rawinput +!
192: 1 chars cookedinput +!
193: nl-char = if
194: checksyncline
195: endif
196: rawinput @ c@ cookedinput @ c!
197: endif ;
198:
199: : charclass ( set "name" -- )
200: ['] ?nextchar terminal ;
201:
202: : .. ( c1 c2 -- set )
203: ( creates a set that includes the characters c, c1<=c<=c2 )
204: empty copy-set
205: swap 1+ rot do
206: i over add-member
207: loop ;
208:
209: : ` ( -- terminal ) ( use: ` c )
210: ( creates anonymous terminal for the character c )
211: char singleton ['] ?nextchar make-terminal ;
212:
213: char a char z .. char A char Z .. union char _ singleton union charclass letter
214: char 0 char 9 .. charclass digit
215: bl singleton charclass blank
216: tab-char singleton charclass tab
217: nl-char singleton eof-char over add-member complement charclass nonl
218: nl-char singleton eof-char over add-member char : over add-member complement charclass nocolonnl
219: bl 1+ maxchar .. charclass nowhite
220: char " singleton eof-char over add-member complement charclass noquote
221: nl-char singleton charclass nl
222: eof-char singleton charclass eof
223:
224:
225: (( letter (( letter || digit )) **
226: )) <- c-name ( -- )
227:
228: nowhite ++
229: <- name ( -- )
230:
231: (( {{ start }} ` \ nonl ** nl {{ end
232: 2dup 2 min s" \+" compare 0= IF f-comment 2! ELSE 2drop THEN }}
233: )) <- comment ( -- )
234:
235: (( {{ effect-in }} (( {{ start }} c-name {{ end 2 pick item-name 2! item-descr + }} blank ** )) ** {{ effect-in-end ! }}
236: ` - ` - blank **
237: {{ effect-out }} (( {{ start }} c-name {{ end 2 pick item-name 2! item-descr + }} blank ** )) ** {{ effect-out-end ! }}
238: )) <- stack-effect ( -- )
239:
240: (( {{ s" " doc 2! s" " forth-code 2! }}
241: (( comment || nl )) **
242: (( {{ line @ name-line ! filename 2@ name-filename 2! }}
243: {{ start }} name {{ end 2dup forth-name 2! c-name 2! }} tab ++
244: {{ start }} stack-effect {{ end stack-string 2! }} tab ++
245: {{ start }} name {{ end wordset 2! }} tab **
246: (( {{ start }} c-name {{ end c-name 2! }} )) ?? nl
247: ))
248: (( ` " ` " {{ start }} (( noquote ++ ` " )) ++ {{ end 1- doc 2! }} ` " nl )) ??
249: {{ skipsynclines off line @ c-line ! filename 2@ c-filename 2! start }} (( nocolonnl nonl ** nl )) ** {{ end c-code 2! skipsynclines on }}
250: (( ` : nl
251: {{ start }} (( nonl ++ nl )) ++ {{ end forth-code 2! }}
252: )) ??
253: (( nl || eof ))
254: )) <- primitive ( -- )
255:
256: (( (( primitive {{ printprim }} )) ** eof ))
257: parser primitives2something
258: warnings @ [IF]
259: .( parser generated ok ) cr
260: [THEN]
261:
262: : primfilter ( file-id xt -- )
263: \ fileid is for the input file, xt ( -- ) is for the output word
264: output !
265: here dup rawinput ! cookedinput !
266: here swap read-whole-file
267: dup endrawinput !
268: here - allot
269: align
270: checksyncline
271: \ begin
272: \ getinput dup eof-char = ?EXIT emit true ?nextchar
273: \ again ;
274: primitives2something ;
275:
276: \ types
277:
278: struct
279: 2 cells field type-c-name
280: cell const-field type-d-size
281: cell const-field type-f-size
282: cell const-field type-fetch-handler
283: cell const-field type-store-handler
284: constant type-description
285:
286: : data-stack-access ( n1 n2 n3 -- )
287: \ n1 is the offset of the accessed item, n2, n3 are effect-*-size
288: drop swap - 1- dup
289: if
290: ." sp[" 0 .r ." ]"
291: else
292: drop ." TOS"
293: endif ;
294:
295: : fp-stack-access ( n1 n2 n3 -- )
296: \ n1 is the offset of the accessed item, n2, n3 are effect-*-size
297: nip swap - 1- dup
298: if
299: ." fp[" 0 .r ." ]"
300: else
301: drop ." FTOS"
302: endif ;
303:
304: : fetch-single ( item -- )
305: >r
306: r@ item-name 2@ type
307: ." = ("
308: r@ item-type @ type-c-name 2@ type ." ) "
309: r@ item-d-offset @ effect-in-size 2@ data-stack-access ." ;" cr
310: rdrop ;
311:
312: : fetch-double ( item -- )
313: >r
314: ." FETCH_DCELL("
315: r@ item-name 2@ type ." , "
316: r@ item-d-offset @ dup effect-in-size 2@ data-stack-access
317: ." , " 1+ effect-in-size 2@ data-stack-access
318: ." );" cr
319: rdrop ;
320:
321: : fetch-float ( item -- )
322: >r
323: r@ item-name 2@ type
324: ." = "
325: \ ." (" r@ item-type @ type-c-name 2@ type ." ) "
326: r@ item-f-offset @ effect-in-size 2@ fp-stack-access ." ;" cr
327: rdrop ;
328:
329: : d-same-as-in? ( item -- f )
330: \ f is true iff the offset of item is the same as on input
331: >r
332: r@ item-name 2@ items @ search-wordlist 0=
333: abort" bug"
334: execute @
335: dup r@ =
336: if \ item first appeared in output
337: drop false
338: else
339: item-d-offset @ r@ item-d-offset @ =
340: endif
341: rdrop ;
342:
343: : is-in-tos? ( item -- f )
344: \ true if item has the same offset as the input TOS
345: item-d-offset @ 1+ effect-in-size 2@ drop = ;
346:
347: : really-store-single ( item -- )
348: >r
349: r@ item-d-offset @ effect-out-size 2@ data-stack-access ." = (Cell)"
350: r@ item-name 2@ type ." ;"
351: rdrop ;
352:
353: : store-single ( item -- )
354: >r
355: r@ d-same-as-in?
356: if
357: r@ is-in-tos?
358: if
359: ." IF_TOS(" r@ really-store-single ." );" cr
360: endif
361: else
362: r@ really-store-single cr
363: endif
364: rdrop ;
365:
366: : store-double ( item -- )
367: \ !! store optimization is not performed, because it is not yet needed
368: >r
369: ." STORE_DCELL(" r@ item-name 2@ type ." , "
370: r@ item-d-offset @ dup effect-out-size 2@ data-stack-access
371: ." , " 1+ effect-out-size 2@ data-stack-access
372: ." );" cr
373: rdrop ;
374:
375: : f-same-as-in? ( item -- f )
376: \ f is true iff the offset of item is the same as on input
377: >r
378: r@ item-name 2@ items @ search-wordlist 0=
379: abort" bug"
380: execute @
381: dup r@ =
382: if \ item first appeared in output
383: drop false
384: else
385: item-f-offset @ r@ item-f-offset @ =
386: endif
387: rdrop ;
388:
389: : is-in-ftos? ( item -- f )
390: \ true if item has the same offset as the input TOS
391: item-f-offset @ 1+ effect-in-size 2@ nip = ;
392:
393: : really-store-float ( item -- )
394: >r
395: r@ item-f-offset @ effect-out-size 2@ fp-stack-access ." = "
396: r@ item-name 2@ type ." ;"
397: rdrop ;
398:
399: : store-float ( item -- )
400: >r
401: r@ f-same-as-in?
402: if
403: r@ is-in-ftos?
404: if
405: ." IF_FTOS(" r@ really-store-float ." );" cr
406: endif
407: else
408: r@ really-store-float cr
409: endif
410: rdrop ;
411:
412: : single-type ( -- xt1 xt2 n1 n2 )
413: ['] fetch-single ['] store-single 1 0 ;
414:
415: : double-type ( -- xt1 xt2 n1 n2 )
416: ['] fetch-double ['] store-double 2 0 ;
417:
418: : float-type ( -- xt1 xt2 n1 n2 )
419: ['] fetch-float ['] store-float 0 1 ;
420:
421: : s, ( addr u -- )
422: \ allocate a string
423: here swap dup allot move ;
424:
425: : starts-with ( addr u xt1 xt2 n1 n2 "prefix" -- )
426: \ describes a type
427: \ addr u specifies the C type name
428: \ n1 is the size of the type on the data stack
429: \ n2 is the size of the type on the FP stack
430: \ stack effect entries of the type start with prefix
431: >r >r >r >r
432: dup >r here >r s,
433: create
434: r> r> 2,
435: r> r> r> , r> , swap , , ;
436:
437: wordlist constant types
438: get-current
439: types set-current
440:
441: s" Bool" single-type starts-with f
442: s" Char" single-type starts-with c
443: s" Cell" single-type starts-with n
444: s" Cell" single-type starts-with w
445: s" UCell" single-type starts-with u
446: s" DCell" double-type starts-with d
447: s" UDCell" double-type starts-with ud
448: s" Float" float-type starts-with r
449: s" Cell *" single-type starts-with a_
450: s" Char *" single-type starts-with c_
451: s" Float *" single-type starts-with f_
452: s" DFloat *" single-type starts-with df_
453: s" SFloat *" single-type starts-with sf_
454: s" Xt" single-type starts-with xt
455: s" WID" single-type starts-with wid
456: s" F83Name *" single-type starts-with f83name
457:
458: set-current
459:
460: : get-type ( addr1 u1 -- type-descr )
461: \ get the type of the name in addr1 u1
462: \ type-descr is a pointer to a type-descriptor
463: 0 swap ?do
464: dup i types search-wordlist
465: if \ ok, we have the type ( addr1 xt )
466: execute nip
467: UNLOOP EXIT
468: endif
469: -1 s+loop
470: \ we did not find a type, abort
471: true abort" unknown type prefix" ;
472:
473: : declare ( addr "name" -- )
474: \ remember that there is a stack item at addr called name
475: create , ;
476:
477: : declaration ( item -- )
478: dup item-name 2@ items @ search-wordlist
479: if \ already declared ( item xt )
480: execute @ item-type @ swap item-type !
481: else ( addr )
482: dup item-name 2@ nextname dup declare ( addr )
483: dup >r item-name 2@ 2dup get-type ( addr1 u type-descr )
484: dup r> item-type ! ( addr1 u type-descr )
485: type-c-name 2@ type space type ." ;" cr
486: endif ;
487:
488: : declaration-list ( addr1 addr2 -- )
489: swap ?do
490: i declaration
491: item-descr +loop ;
492:
493: : fetch ( addr -- )
494: dup item-type @ type-fetch-handler execute ;
495:
496: : declarations ( -- )
497: wordlist dup items ! set-current
498: effect-in effect-in-end @ declaration-list
499: effect-out effect-out-end @ declaration-list ;
500:
501: \ offset computation
502: \ the leftmost (i.e. deepest) item has offset 0
503: \ the rightmost item has the highest offset
504:
505: : compute-offset ( n1 n2 item -- n3 n4 )
506: \ n1, n3 are data-stack-offsets
507: \ n2, n4 are the fp-stack-offsets
508: >r
509: swap dup r@ item-d-offset !
510: r@ item-type @ type-d-size +
511: swap dup r@ item-f-offset !
512: r@ item-type @ type-f-size +
513: rdrop ;
514:
515: : compute-list ( addr1 addr2 -- n1 n2 )
516: \ n1, n2 are the final offsets
517: 0 0 2swap swap ?do
518: i compute-offset
519: item-descr +loop ;
520:
521: : compute-offsets ( -- )
522: effect-in effect-in-end @ compute-list effect-in-size 2!
523: effect-out effect-out-end @ compute-list effect-out-size 2! ;
524:
525: : flush-tos ( -- )
526: effect-in-size 2@ effect-out-size 2@
527: 0<> rot 0= and
528: if
529: ." IF_FTOS(fp[0] = FTOS);" cr
530: endif
531: 0<> swap 0= and
532: if
533: ." IF_TOS(sp[0] = TOS);" cr
534: endif ;
535:
536: : fill-tos ( -- )
537: effect-in-size 2@ effect-out-size 2@
538: 0= rot 0<> and
539: if
540: ." IF_FTOS(FTOS = fp[0]);" cr
541: endif
542: 0= swap 0<> and
543: if
544: ." IF_TOS(TOS = sp[0]);" cr
545: endif ;
546:
547: : fetches ( -- )
548: effect-in-end @ effect-in ?do
549: i fetch
550: item-descr +loop ;
551:
552: : stack-pointer-updates ( -- )
553: \ we need not check if an update is a noop; gcc does this for us
554: effect-in-size 2@
555: effect-out-size 2@
556: rot swap - ( d-in d-out f-diff )
557: rot rot - ( f-diff d-diff )
558: ?dup IF ." sp += " 0 .r ." ;" cr THEN
559: ?dup IF ." fp += " 0 .r ." ;" cr THEN ;
560:
561: : store ( item -- )
562: \ f is true if the item should be stored
563: \ f is false if the store is probably not necessary
564: dup item-type @ type-store-handler execute ;
565:
566: : stores ( -- )
567: effect-out-end @ effect-out ?do
568: i store
569: item-descr +loop ;
570:
571: : .stack-list ( start end -- )
572: swap ?do
573: i item-name 2@ type space
574: item-descr +loop ;
575:
576: : output-c ( -- )
577: ." I_" c-name 2@ type ." : /* " forth-name 2@ type ." ( " stack-string 2@ type ." ) */" cr
578: ." /* " doc 2@ type ." */" cr
579: ." NAME(" [char] " emit forth-name 2@ type [char] " emit ." )" cr \ debugging
580: ." {" cr
581: ." DEF_CA" cr
582: declarations
583: compute-offsets \ for everything else
584: ." NEXT_P0;" cr
585: flush-tos
586: fetches
587: stack-pointer-updates
588: ." {" cr
589: ." #line " c-line @ . [char] " emit c-filename 2@ type [char] " emit cr
590: c-code 2@ type
591: ." }" cr
592: ." NEXT_P1;" cr
593: stores
594: fill-tos
595: ." NEXT_P2;" cr
596: ." }" cr
597: cr
598: ;
599:
600: : output-label ( -- )
601: ." &&I_" c-name 2@ type ." ," cr ;
602:
603: : output-alias ( -- )
604: f-comment 2@ nip
605: IF cr f-comment 2@ 2 /string type 0 0 f-comment 2! THEN
606: primitive-number @ . ." alias " forth-name 2@ type cr
607: -1 primitive-number +! ;
608:
609: : output-forth ( -- )
610: f-comment 2@ 2 min s" \+" compare 0=
611: IF cr f-comment 2@ 2 /string type 0 0 f-comment 2! THEN
612: forth-code @ 0=
613: IF output-alias
614: ELSE ." : " forth-name 2@ type ." ( "
615: effect-in effect-in-end @ .stack-list ." -- "
616: effect-out effect-out-end @ .stack-list ." )" cr
617: forth-code 2@ type cr
618: -1 primitive-number +!
619: THEN ;
620:
621: : output-tag-file ( -- )
622: name-filename 2@ last-name-filename 2@ compare if
623: name-filename 2@ last-name-filename 2!
624: #ff emit cr
625: name-filename 2@ type
626: ." ,0" cr
627: endif ;
628:
629: : output-tag ( -- )
630: output-tag-file
631: forth-name 2@ 1+ type
632: 127 emit
633: space forth-name 2@ type space
634: 1 emit
635: name-line @ 0 .r
636: ." ,0" cr ;
637:
638: [IFDEF] documentation
639: : register-doc ( -- )
640: get-current documentation set-current
641: forth-name 2@ nextname create
642: forth-name 2@ 2,
643: stack-string 2@ condition-stack-effect 2,
644: wordset 2@ 2,
645: c-name 2@ condition-pronounciation 2,
646: doc 2@ 2,
647: set-current ;
648: [THEN]
649:
650: : process-file ( addr u xt -- )
651: >r
652: 2dup filename 2!
653: r/o open-file abort" cannot open file"
654: warnings @ if
655: ." ------------ CUT HERE -------------" cr endif
656: r> primfilter ;
657:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>