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