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