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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help