[gforth] / gforth / prims2x.fs  

gforth: gforth/prims2x.fs


1 : anton 1.1 \ This is not very nice (hard limits, no checking, assumes 1 chars = 1)
2 :    
3 :     \ Optimizations:
4 :     \ superfluous stores are removed. GCC removes the superfluous loads by itself
5 :     \ TOS and FTOS can be kept in register( variable)s.
6 :     \
7 :     \ Problems:
8 :     \ The TOS optimization is somewhat hairy. The problems by example:
9 :     \ 1) dup ( w -- w w ): w=TOS; sp-=1; sp[1]=w; TOS=w;
10 :     \ The store is not superfluous although the earlier opt. would think so
11 :     \ Alternatively: sp[0]=TOS; w=TOS; sp-=1; TOS=w;
12 :     \ 2) ( -- .. ): sp[0] = TOS; ... /* This additional store is necessary */
13 :     \ 3) ( .. -- ): ... TOS = sp[0]; /* as well as this load */
14 :     \ 4) ( -- ): /* but here they are unnecessary */
15 :     \ 5) Words that call NEXT themselves have to be done very carefully.
16 :     \
17 :     \ To do:
18 : pazsan 1.8 \ add the store optimization for doubles
19 : anton 1.1 \ regarding problem 1 above: It would be better (for over) to implement
20 :     \ the alternative
21 :    
22 : pazsan 1.3 warnings off
23 :    
24 : pazsan 1.5 [IFUNDEF] vocabulary include search-order.fs [THEN]
25 : anton 1.1 include gray.fs
26 :    
27 :     100 constant max-effect \ number of things on one side of a stack effect
28 :     4096 constant batch-size \ no meaning, just make sure it's >0
29 :     255 constant maxchar
30 :     maxchar 1+ constant eof-char
31 :     9 constant tab-char
32 :     10 constant nl-char
33 :    
34 :     : read-whole-file ( c-addr1 file-id -- c-addr2 )
35 :     \ reads the contents of the file file-id puts it into memory at c-addr1
36 :     \ c-addr2 is the first address after the file block
37 :     begin ( c-addr file-id )
38 :     2dup batch-size swap read-file
39 :     if
40 : pazsan 1.8 true abort" I/O error"
41 : anton 1.1 endif
42 :     ( c-addr file-id actual-size ) rot over + -rot
43 :     batch-size <>
44 :     until
45 :     drop ;
46 :    
47 :     variable input \ pointer to next character to be parsed
48 :     variable endinput \ pointer to the end of the input (the char after the last)
49 :    
50 :     : start ( -- addr )
51 :     input @ ;
52 :    
53 :     : end ( addr -- addr u )
54 :     input @ over - ;
55 :    
56 :     variable output \ xt ( -- ) of output word
57 :    
58 :     : printprim ( -- )
59 :     output @ execute ;
60 :    
61 :     : field
62 :     <builds-field ( n1 n2 -- n3 )
63 :     does> ( addr1 -- addr2 )
64 :     @ + ;
65 :    
66 :     : const-field
67 :     <builds-field ( n1 n2 -- n3 )
68 :     does> ( addr -- w )
69 :     @ + @ ;
70 :    
71 :     struct
72 :     2 cells field item-name
73 :     cell field item-d-offset
74 :     cell field item-f-offset
75 :     cell field item-type
76 :     constant item-descr
77 :    
78 :     2variable forth-name
79 :     2variable wordset
80 :     2variable c-name
81 :     2variable doc
82 :     2variable c-code
83 :     2variable forth-code
84 :     2variable stack-string
85 :     create effect-in max-effect item-descr * allot
86 :     create effect-out max-effect item-descr * allot
87 :     variable effect-in-end ( pointer )
88 :     variable effect-out-end ( pointer )
89 :     2variable effect-in-size
90 :     2variable effect-out-size
91 :    
92 : anton 1.6 variable primitive-number -9 primitive-number !
93 : anton 1.1
94 :     \ for several reasons stack items of a word are stored in a wordlist
95 :     \ since neither forget nor marker are implemented yet, we make a new
96 :     \ wordlist for every word and store it in the variable items
97 :     variable items
98 :    
99 :     \ a few more set ops
100 :    
101 :     : bit-equivalent ( w1 w2 -- w3 )
102 :     xor invert ;
103 :    
104 :     : complement ( set1 -- set2 )
105 :     empty ['] bit-equivalent binary-set-operation ;
106 :    
107 :     \ the parser
108 :    
109 :     eof-char max-member \ the whole character set + EOF
110 :    
111 :     : getinput ( -- n )
112 :     input @
113 :     dup endinput @ =
114 :     if
115 :     drop eof-char
116 :     else
117 :     c@
118 :     endif ;
119 :    
120 :     :noname ( n -- )
121 :     dup bl > if
122 :     emit space
123 :     else
124 :     .
125 :     endif ;
126 :     print-token !
127 :    
128 :     : testchar? ( set -- f )
129 :     getinput member? ;
130 :     ' testchar? test-vector !
131 :    
132 :     : ?nextchar ( f -- )
133 :     ?not? if
134 :     ." syntax error" cr
135 :     getinput . cr
136 :     input @ endinput @ over - 100 min type cr
137 :     abort
138 :     endif
139 :     input @ endinput @ <> if
140 :     1 input +!
141 :     endif ;
142 :    
143 :     : charclass ( set "name" -- )
144 :     ['] ?nextchar terminal ;
145 :    
146 :     : .. ( c1 c2 -- set )
147 :     ( creates a set that includes the characters c, c1<=c<=c2 )
148 :     empty copy-set
149 :     swap 1+ rot do
150 :     i over add-member
151 :     loop ;
152 :    
153 :     : ` ( -- terminal ) ( use: ` c )
154 :     ( creates anonymous terminal for the character c )
155 :     [compile] ascii singleton ['] ?nextchar make-terminal ;
156 :    
157 :     char a char z .. char A char Z .. union char _ singleton union charclass letter
158 :     char 0 char 9 .. charclass digit
159 :     bl singleton charclass blank
160 :     tab-char singleton charclass tab
161 :     nl-char singleton eof-char over add-member complement charclass nonl
162 :     nl-char singleton eof-char over add-member char : over add-member complement charclass nocolonnl
163 :     bl 1+ maxchar .. charclass nowhite
164 :     char " singleton eof-char over add-member complement charclass noquote
165 :     nl-char singleton charclass nl
166 :     eof-char singleton charclass eof
167 :    
168 :    
169 :     (( letter (( letter || digit )) **
170 :     )) <- c-name ( -- )
171 :    
172 :     nowhite ++
173 :     <- name ( -- )
174 :    
175 :     (( ` \ nonl ** nl
176 :     )) <- comment ( -- )
177 :    
178 :     (( {{ effect-in }} (( {{ start }} c-name {{ end 2 pick item-name 2! item-descr + }} blank ** )) ** {{ effect-in-end ! }}
179 :     ` - ` - blank **
180 :     {{ effect-out }} (( {{ start }} c-name {{ end 2 pick item-name 2! item-descr + }} blank ** )) ** {{ effect-out-end ! }}
181 :     )) <- stack-effect ( -- )
182 :    
183 :     (( {{ s" " doc 2! s" " forth-code 2! }}
184 :     (( comment || nl )) **
185 :     (( {{ start }} name {{ end 2dup forth-name 2! c-name 2! }} tab ++
186 :     {{ start }} stack-effect {{ end stack-string 2! }} tab ++
187 :     {{ start }} name {{ end wordset 2! }} tab **
188 :     (( {{ start }} c-name {{ end c-name 2! }} )) ?? nl
189 :     ))
190 :     (( ` " ` " {{ start }} (( noquote ++ ` " )) ++ {{ end 1- doc 2! }} ` " nl )) ??
191 :     {{ start }} (( nocolonnl nonl ** nl )) ** {{ end c-code 2! }}
192 :     (( ` : nl
193 :     {{ start }} (( nonl ++ nl )) ++ {{ end forth-code 2! }}
194 :     )) ??
195 :     (( nl || eof ))
196 :     )) <- primitive ( -- )
197 :    
198 :     (( (( primitive {{ printprim }} )) ** eof ))
199 :     parser primitives2something
200 : pazsan 1.3 warnings @ [IF]
201 : anton 1.1 .( parser generated ok ) cr
202 : pazsan 1.3 [THEN]
203 : anton 1.1
204 :     : primfilter ( file-id xt -- )
205 :     \ fileid is for the input file, xt ( -- ) is for the output word
206 :     output !
207 :     here input !
208 :     here swap read-whole-file
209 :     dup endinput !
210 :     here - allot
211 : pazsan 1.2 align
212 : anton 1.1 primitives2something ;
213 :    
214 :     \ types
215 :    
216 :     struct
217 :     2 cells field type-c-name
218 :     cell const-field type-d-size
219 :     cell const-field type-f-size
220 :     cell const-field type-fetch-handler
221 :     cell const-field type-store-handler
222 :     constant type-description
223 :    
224 :     : data-stack-access ( n1 n2 n3 -- )
225 :     \ n1 is the offset of the accessed item, n2, n3 are effect-*-size
226 :     drop swap - 1- dup
227 :     if
228 : pazsan 1.2 ." sp[" 0 .r ." ]"
229 : anton 1.1 else
230 :     drop ." TOS"
231 :     endif ;
232 :    
233 :     : fp-stack-access ( n1 n2 n3 -- )
234 :     \ n1 is the offset of the accessed item, n2, n3 are effect-*-size
235 :     nip swap - 1- dup
236 :     if
237 : pazsan 1.2 ." fp[" 0 .r ." ]"
238 : anton 1.1 else
239 :     drop ." FTOS"
240 :     endif ;
241 :    
242 :     : fetch-single ( item -- )
243 :     >r
244 : pazsan 1.8 r@ item-name 2@ type
245 :     ." = ("
246 : anton 1.1 r@ item-type @ type-c-name 2@ type ." ) "
247 :     r@ item-d-offset @ effect-in-size 2@ data-stack-access ." ;" cr
248 :     rdrop ;
249 :    
250 :     : fetch-double ( item -- )
251 :     >r
252 : pazsan 1.8 r@ item-name 2@ type
253 :     ." = ({Double_Store _d; _d.cells.low = "
254 : anton 1.1 r@ item-d-offset @ dup effect-in-size 2@ data-stack-access
255 : pazsan 1.8 ." ; _d.cells.high = " 1+ effect-in-size 2@ data-stack-access
256 :     ." ; _d.dcell;});" cr
257 : anton 1.1 rdrop ;
258 :    
259 :     : fetch-float ( item -- )
260 :     >r
261 : pazsan 1.8 r@ item-name 2@ type
262 :     ." = "
263 : anton 1.1 \ ." (" r@ item-type @ type-c-name 2@ type ." ) "
264 :     r@ item-f-offset @ effect-in-size 2@ fp-stack-access ." ;" cr
265 :     rdrop ;
266 :    
267 :     : d-same-as-in? ( item -- f )
268 :     \ f is true iff the offset of item is the same as on input
269 :     >r
270 :     r@ item-name 2@ items @ search-wordlist 0=
271 : pazsan 1.8 abort" bug"
272 : anton 1.1 execute @
273 :     dup r@ =
274 :     if \ item first appeared in output
275 :     drop false
276 :     else
277 :     item-d-offset @ r@ item-d-offset @ =
278 :     endif
279 :     rdrop ;
280 :    
281 :     : is-in-tos? ( item -- f )
282 :     \ true if item has the same offset as the input TOS
283 :     item-d-offset @ 1+ effect-in-size 2@ drop = ;
284 :    
285 :     : really-store-single ( item -- )
286 :     >r
287 :     r@ item-d-offset @ effect-out-size 2@ data-stack-access ." = (Cell)"
288 :     r@ item-name 2@ type ." ;"
289 :     rdrop ;
290 :    
291 :     : store-single ( item -- )
292 :     >r
293 :     r@ d-same-as-in?
294 :     if
295 :     r@ is-in-tos?
296 :     if
297 :     ." IF_TOS(" r@ really-store-single ." );" cr
298 :     endif
299 :     else
300 :     r@ really-store-single cr
301 :     endif
302 :     rdrop ;
303 :    
304 :     : store-double ( item -- )
305 :     \ !! store optimization is not performed, because it is not yet needed
306 :     >r
307 :     ." {Double_Store _d; _d.dcell = " r@ item-name 2@ type ." ; "
308 :     r@ item-d-offset @ dup effect-out-size 2@ data-stack-access
309 : pazsan 1.2 ." = _d.cells.low; " 1+ effect-out-size 2@ data-stack-access
310 :     ." = _d.cells.high;}" cr
311 : anton 1.1 rdrop ;
312 :    
313 :     : f-same-as-in? ( item -- f )
314 :     \ f is true iff the offset of item is the same as on input
315 :     >r
316 :     r@ item-name 2@ items @ search-wordlist 0=
317 : pazsan 1.8 abort" bug"
318 : anton 1.1 execute @
319 :     dup r@ =
320 :     if \ item first appeared in output
321 :     drop false
322 :     else
323 :     item-f-offset @ r@ item-f-offset @ =
324 :     endif
325 :     rdrop ;
326 :    
327 :     : is-in-ftos? ( item -- f )
328 :     \ true if item has the same offset as the input TOS
329 :     item-f-offset @ 1+ effect-in-size 2@ nip = ;
330 :    
331 :     : really-store-float ( item -- )
332 :     >r
333 :     r@ item-f-offset @ effect-out-size 2@ fp-stack-access ." = "
334 :     r@ item-name 2@ type ." ;"
335 :     rdrop ;
336 :    
337 :     : store-float ( item -- )
338 :     >r
339 :     r@ f-same-as-in?
340 :     if
341 :     r@ is-in-ftos?
342 :     if
343 :     ." IF_FTOS(" r@ really-store-float ." );" cr
344 :     endif
345 :     else
346 :     r@ really-store-float cr
347 :     endif
348 :     rdrop ;
349 :    
350 :     : single-type ( -- xt n1 n2 )
351 :     ['] fetch-single ['] store-single 1 0 ;
352 :    
353 :     : double-type ( -- xt n1 n2 )
354 :     ['] fetch-double ['] store-double 2 0 ;
355 :    
356 :     : float-type ( -- xt n1 n2 )
357 :     ['] fetch-float ['] store-float 0 1 ;
358 :    
359 :     : s, ( addr u -- )
360 :     \ allocate a string
361 :     here swap dup allot move ;
362 :    
363 :     : starts-with ( addr u xt1 xt2 n1 n2 "prefix" -- )
364 :     \ describes a type
365 :     \ addr u specifies the C type name
366 :     \ n1 is the size of the type on the data stack
367 :     \ n2 is the size of the type on the FP stack
368 :     \ stack effect entries of the type start with prefix
369 :     >r >r >r >r
370 :     dup >r here >r s,
371 :     create
372 :     r> r> 2,
373 :     r> r> r> , r> , swap , , ;
374 :    
375 :     wordlist constant types
376 :     get-current
377 :     types set-current
378 :    
379 :     s" Bool" single-type starts-with f
380 :     s" Char" single-type starts-with c
381 :     s" Cell" single-type starts-with n
382 :     s" Cell" single-type starts-with w
383 :     s" UCell" single-type starts-with u
384 :     s" DCell" double-type starts-with d
385 :     s" UDCell" double-type starts-with ud
386 :     s" Float" float-type starts-with r
387 :     s" Cell *" single-type starts-with a_
388 :     s" Char *" single-type starts-with c_
389 :     s" Float *" single-type starts-with f_
390 :     s" DFloat *" single-type starts-with df_
391 :     s" SFloat *" single-type starts-with sf_
392 :     s" Xt" single-type starts-with xt
393 :     s" WID" single-type starts-with wid
394 :     s" F83Name *" single-type starts-with f83name
395 :    
396 :     set-current
397 :    
398 :     : get-type ( addr1 u1 -- type-descr )
399 :     \ get the type of the name in addr1 u1
400 :     \ type-descr is a pointer to a type-descriptor
401 :     0 swap ?do
402 :     dup i types search-wordlist
403 :     if \ ok, we have the type ( addr1 xt )
404 :     execute nip
405 :     UNLOOP EXIT
406 :     endif
407 : anton 1.9 -1 s+loop
408 : anton 1.1 \ we did not find a type, abort
409 : pazsan 1.8 true abort" unknown type prefix" ;
410 : anton 1.1
411 :     : declare ( addr "name" -- )
412 :     \ remember that there is a stack item at addr called name
413 :     create , ;
414 :    
415 :     : declaration ( item -- )
416 :     dup item-name 2@ items @ search-wordlist
417 :     if \ already declared ( item xt )
418 :     execute @ item-type @ swap item-type !
419 :     else ( addr )
420 :     dup item-name 2@ nextname dup declare ( addr )
421 :     dup >r item-name 2@ 2dup get-type ( addr1 u type-descr )
422 :     dup r> item-type ! ( addr1 u type-descr )
423 :     type-c-name 2@ type space type ." ;" cr
424 :     endif ;
425 :    
426 :     : declaration-list ( addr1 addr2 -- )
427 :     swap ?do
428 :     i declaration
429 :     item-descr +loop ;
430 :    
431 : pazsan 1.8 : fetch ( addr -- )
432 :     dup item-type @ type-fetch-handler execute ;
433 :    
434 : anton 1.1 : declarations ( -- )
435 :     wordlist dup items ! set-current
436 :     effect-in effect-in-end @ declaration-list
437 :     effect-out effect-out-end @ declaration-list ;
438 :    
439 :     \ offset computation
440 :     \ the leftmost (i.e. deepest) item has offset 0
441 :     \ the rightmost item has the highest offset
442 :    
443 :     : compute-offset ( n1 n2 item -- n3 n4 )
444 :     \ n1, n3 are data-stack-offsets
445 :     \ n2, n4 are the fp-stack-offsets
446 :     >r
447 :     swap dup r@ item-d-offset !
448 :     r@ item-type @ type-d-size +
449 :     swap dup r@ item-f-offset !
450 :     r@ item-type @ type-f-size +
451 :     rdrop ;
452 :    
453 :     : compute-list ( addr1 addr2 -- n1 n2 )
454 :     \ n1, n2 are the final offsets
455 :     0 0 2swap swap ?do
456 :     i compute-offset
457 :     item-descr +loop ;
458 :    
459 :     : compute-offsets ( -- )
460 :     effect-in effect-in-end @ compute-list effect-in-size 2!
461 :     effect-out effect-out-end @ compute-list effect-out-size 2! ;
462 :    
463 :     : flush-tos ( -- )
464 :     effect-in-size 2@ effect-out-size 2@
465 :     0<> rot 0= and
466 :     if
467 :     ." IF_FTOS(fp[0] = FTOS);" cr
468 :     endif
469 :     0<> swap 0= and
470 :     if
471 :     ." IF_TOS(sp[0] = TOS);" cr
472 :     endif ;
473 :    
474 :     : fill-tos ( -- )
475 :     effect-in-size 2@ effect-out-size 2@
476 :     0= rot 0<> and
477 :     if
478 :     ." IF_FTOS(FTOS = fp[0]);" cr
479 :     endif
480 :     0= swap 0<> and
481 :     if
482 :     ." IF_TOS(TOS = sp[0]);" cr
483 :     endif ;
484 :    
485 :     : fetches ( -- )
486 :     effect-in-end @ effect-in ?do
487 :     i fetch
488 :     item-descr +loop ;
489 :    
490 :     : stack-pointer-updates ( -- )
491 : pazsan 1.8 \ we need not check if an update is a noop; gcc does this for us
492 : anton 1.1 effect-in-size 2@
493 :     effect-out-size 2@
494 :     rot swap - ( d-in d-out f-diff )
495 :     rot rot - ( f-diff d-diff )
496 : pazsan 1.2 ?dup IF ." sp += " 0 .r ." ;" cr THEN
497 :     ?dup IF ." fp += " 0 .r ." ;" cr THEN ;
498 : anton 1.1
499 :     : store ( item -- )
500 :     \ f is true if the item should be stored
501 :     \ f is false if the store is probably not necessary
502 :     dup item-type @ type-store-handler execute ;
503 :    
504 :     : stores ( -- )
505 :     effect-out-end @ effect-out ?do
506 :     i store
507 :     item-descr +loop ;
508 :    
509 : pazsan 1.8 : .stack-list ( start end -- )
510 :     swap ?do
511 :     i item-name 2@ type space
512 :     item-descr +loop ;
513 :    
514 : anton 1.1 : output-c ( -- )
515 : pazsan 1.2 ." I_" c-name 2@ type ." : /* " forth-name 2@ type ." ( " stack-string 2@ type ." ) */" cr
516 : anton 1.1 ." /* " doc 2@ type ." */" cr
517 :     ." {" cr
518 :     ." DEF_CA" cr
519 :     declarations
520 :     compute-offsets \ for everything else
521 :     flush-tos
522 :     fetches
523 : pazsan 1.2 stack-pointer-updates cr
524 : anton 1.1 ." NAME(" [char] " emit forth-name 2@ type [char] " emit ." )" cr \ debugging
525 :     ." {" cr
526 :     c-code 2@ type
527 :     ." }" cr
528 :     ." NEXT_P1;" cr
529 :     stores
530 :     fill-tos
531 : anton 1.9 ." NEXT_P2;" cr
532 : anton 1.1 ." }" cr
533 :     cr
534 :     ;
535 :    
536 :     : output-label ( -- )
537 :     ." &&I_" c-name 2@ type ." ," cr ;
538 :    
539 :     : output-alias ( -- )
540 :     primitive-number @ . ." alias " forth-name 2@ type cr
541 :     -1 primitive-number +! ;
542 :    
543 : pazsan 1.8 : output-forth ( -- )
544 :     forth-code @ 0=
545 :     IF output-alias
546 :     ELSE ." : " forth-name 2@ type ." ( "
547 :     effect-in effect-in-end @ .stack-list ." -- "
548 :     effect-out effect-out-end @ .stack-list ." )" cr
549 :     forth-code 2@ type cr
550 :     -1 primitive-number +!
551 :     THEN
552 :     ;
553 :    
554 : anton 1.1 : process-file ( addr u xt -- )
555 : pazsan 1.8 >r r/o open-file abort" cannot open file"
556 : pazsan 1.3 warnings @ if
557 :     ." ------------ CUT HERE -------------" cr endif
558 : pazsan 1.5 r> primfilter ;
559 :    

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help