[gforth] / gforth / prims2x.fs  

gforth: gforth/prims2x.fs


1 : anton 1.16 \ converts primitives to, e.g., C code
2 :    
3 : anton 1.47 \ Copyright (C) 1995,1996,1997,1998,2000 Free Software Foundation, Inc.
4 : anton 1.16
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 : anton 1.48 \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
20 : anton 1.16
21 :    
22 : anton 1.71 \ This is not very nice (hard limits, no checking, assumes 1 chars = 1).
23 :     \ And it grew even worse when it aged.
24 : anton 1.1
25 :     \ Optimizations:
26 :     \ superfluous stores are removed. GCC removes the superfluous loads by itself
27 :     \ TOS and FTOS can be kept in register( variable)s.
28 :     \
29 :     \ Problems:
30 :     \ The TOS optimization is somewhat hairy. The problems by example:
31 :     \ 1) dup ( w -- w w ): w=TOS; sp-=1; sp[1]=w; TOS=w;
32 :     \ The store is not superfluous although the earlier opt. would think so
33 :     \ Alternatively: sp[0]=TOS; w=TOS; sp-=1; TOS=w;
34 :     \ 2) ( -- .. ): sp[0] = TOS; ... /* This additional store is necessary */
35 :     \ 3) ( .. -- ): ... TOS = sp[0]; /* as well as this load */
36 :     \ 4) ( -- ): /* but here they are unnecessary */
37 :     \ 5) Words that call NEXT themselves have to be done very carefully.
38 :     \
39 :     \ To do:
40 : pazsan 1.8 \ add the store optimization for doubles
41 : anton 1.1 \ regarding problem 1 above: It would be better (for over) to implement
42 :     \ the alternative
43 :    
44 : pazsan 1.3 warnings off
45 :    
46 : jwilke 1.39 [IFUNDEF] vocabulary \ we are executed just with kernel image
47 :     \ load the rest that is needed
48 :     \ (require fails because this file is needed from a
49 :     \ different directory with the wordlibraries)
50 :     include ./search.fs
51 :     include ./extend.fs
52 : anton 1.40 [THEN]
53 :    
54 :     [IFUNDEF] environment?
55 : jwilke 1.39 include ./environ.fs
56 :     [THEN]
57 : pazsan 1.25
58 : anton 1.49 : struct% struct ; \ struct is redefined in gray
59 :    
60 : jwilke 1.39 include ./gray.fs
61 : anton 1.1
62 : anton 1.69 32 constant max-effect \ number of things on one side of a stack effect
63 : anton 1.71 4 constant max-stacks \ the max. number of stacks (including inst-stream).
64 : anton 1.1 255 constant maxchar
65 :     maxchar 1+ constant eof-char
66 : anton 1.17 #tab constant tab-char
67 :     #lf constant nl-char
68 : anton 1.1
69 : anton 1.18 variable rawinput \ pointer to next character to be scanned
70 :     variable endrawinput \ pointer to the end of the input (the char after the last)
71 :     variable cookedinput \ pointer to the next char to be parsed
72 : anton 1.17 variable line \ line number of char pointed to by input
73 : anton 1.65 variable line-start \ pointer to start of current line (for error messages)
74 :     0 line !
75 : anton 1.17 2variable filename \ filename of original input file
76 :     0 0 filename 2!
77 : pazsan 1.25 2variable f-comment
78 :     0 0 f-comment 2!
79 : anton 1.17 variable skipsynclines \ are sync lines ("#line ...") invisible to the parser?
80 :     skipsynclines on
81 : anton 1.1
82 : anton 1.72 : th ( addr1 n -- addr2 )
83 :     cells + ;
84 :    
85 :     : holds ( addr u -- )
86 :     \ like HOLD, but for a string
87 :     tuck + swap 0 +do
88 :     1- dup c@ hold
89 :     loop
90 :     drop ;
91 : anton 1.71
92 : anton 1.1 : start ( -- addr )
93 : anton 1.18 cookedinput @ ;
94 : anton 1.1
95 :     : end ( addr -- addr u )
96 : anton 1.18 cookedinput @ over - ;
97 : anton 1.1
98 : anton 1.71 : print-error-line ( -- )
99 :     \ print the current line and position
100 :     line-start @ endrawinput @ over - 2dup nl-char scan drop nip ( start end )
101 :     over - type cr
102 :     line-start @ rawinput @ over - typewhite ." ^" cr ;
103 :    
104 :     : ?print-error { f addr u -- }
105 :     f ?not? if
106 :     outfile-id >r try
107 :     stderr to outfile-id
108 :     filename 2@ type ." :" line @ 0 .r ." : " addr u type cr
109 :     print-error-line
110 :     0
111 :     recover endtry
112 :     r> to outfile-id throw
113 :     abort
114 :     endif ;
115 :    
116 : anton 1.63 : quote ( -- )
117 :     [char] " emit ;
118 :    
119 : anton 1.72 variable output \ xt ( -- ) of output word for simple primitives
120 :     variable output-combined \ xt ( -- ) of output word for combined primitives
121 : anton 1.1
122 :     : printprim ( -- )
123 :     output @ execute ;
124 :    
125 : anton 1.49 struct%
126 : anton 1.71 cell% field stack-number \ the number of this stack
127 : anton 1.49 cell% 2* field stack-pointer \ stackpointer name
128 : anton 1.72 cell% 2* field stack-typename \ name for default type of stack items
129 : anton 1.49 cell% 2* field stack-cast \ cast string for assignments to stack elements
130 : anton 1.53 cell% field stack-in-index-xt \ ( in-size item -- in-index )
131 : anton 1.49 end-struct stack%
132 :    
133 : anton 1.53 struct%
134 :     cell% 2* field item-name \ name, excluding stack prefixes
135 :     cell% field item-stack \ descriptor for the stack used, 0 is default
136 :     cell% field item-type \ descriptor for the item type
137 :     cell% field item-offset \ offset in stack items, 0 for the deepest element
138 : anton 1.66 cell% field item-first \ true if this is the first occurence of the item
139 : anton 1.53 end-struct item%
140 :    
141 :     struct%
142 :     cell% 2* field type-c-name
143 :     cell% field type-stack \ default stack
144 :     cell% field type-size \ size of type in stack items
145 :     cell% field type-fetch \ xt of fetch code generator ( item -- )
146 :     cell% field type-store \ xt of store code generator ( item -- )
147 :     end-struct type%
148 :    
149 : anton 1.72 variable next-stack-number 0 next-stack-number !
150 :     create stacks max-stacks cells allot \ array of stacks
151 :    
152 : anton 1.53 : stack-in-index ( in-size item -- in-index )
153 :     item-offset @ - 1- ;
154 :    
155 :     : inst-in-index ( in-size item -- in-index )
156 :     nip dup item-offset @ swap item-type @ type-size @ + 1- ;
157 :    
158 : anton 1.72 : make-stack ( addr-ptr u1 addr-stack u2 addr-cast u3 "stack-name" -- )
159 : anton 1.49 create stack% %allot >r
160 : anton 1.72 r@ stacks next-stack-number @ th !
161 : anton 1.71 next-stack-number @ r@ stack-number ! 1 next-stack-number +!
162 : anton 1.49 save-mem r@ stack-cast 2!
163 : anton 1.72 save-mem r@ stack-typename 2!
164 : anton 1.53 save-mem r@ stack-pointer 2!
165 :     ['] stack-in-index r> stack-in-index-xt ! ;
166 : anton 1.49
167 :     \ stack items
168 :    
169 :     : init-item ( addr u addr1 -- )
170 :     \ initialize item at addr1 with name addr u
171 :     \ !! remove stack prefix
172 :     dup item% %size erase
173 :     item-name 2! ;
174 :    
175 : anton 1.64 : map-items { addr end xt -- }
176 :     \ perform xt for all items in array addr...end
177 :     end addr ?do
178 :     i xt execute
179 :     item% %size +loop ;
180 :    
181 : anton 1.49 \ various variables for storing stuff of one primitive
182 : anton 1.1
183 : anton 1.69 struct%
184 :     cell% 2* field prim-name
185 :     cell% 2* field prim-wordset
186 :     cell% 2* field prim-c-name
187 :     cell% 2* field prim-doc
188 :     cell% 2* field prim-c-code
189 :     cell% 2* field prim-forth-code
190 :     cell% 2* field prim-stack-string
191 :     item% max-effect * field prim-effect-in
192 :     item% max-effect * field prim-effect-out
193 :     cell% field prim-effect-in-end
194 :     cell% field prim-effect-out-end
195 : anton 1.71 cell% max-stacks * field prim-stacks-in \ number of in items per stack
196 :     cell% max-stacks * field prim-stacks-out \ number of out items per stack
197 : anton 1.69 end-struct prim%
198 :    
199 : anton 1.70 : make-prim ( -- prim )
200 :     prim% %alloc { p }
201 :     s" " p prim-doc 2! s" " p prim-forth-code 2! s" " p prim-wordset 2!
202 :     p ;
203 :    
204 :     0 value prim
205 : anton 1.69
206 : anton 1.71 wordlist constant primitives
207 :    
208 :     : create-prim ( prim -- )
209 :     get-current >r
210 :     primitives set-current
211 :     dup prim-name 2@ nextname constant
212 :     r> set-current ;
213 :    
214 :     : stack-in ( stack -- addr )
215 :     \ address of number of stack items in effect in
216 :     stack-number @ cells prim prim-stacks-in + ;
217 :    
218 :     : stack-out ( stack -- addr )
219 :     \ address of number of stack items in effect out
220 :     stack-number @ cells prim prim-stacks-out + ;
221 :    
222 : anton 1.69 \ global vars
223 : anton 1.17 variable c-line
224 :     2variable c-filename
225 :     variable name-line
226 :     2variable name-filename
227 :     2variable last-name-filename
228 : pazsan 1.30 Variable function-number 0 function-number !
229 : anton 1.1
230 :     \ for several reasons stack items of a word are stored in a wordlist
231 :     \ since neither forget nor marker are implemented yet, we make a new
232 :     \ wordlist for every word and store it in the variable items
233 :     variable items
234 :    
235 :     \ a few more set ops
236 :    
237 :     : bit-equivalent ( w1 w2 -- w3 )
238 :     xor invert ;
239 :    
240 :     : complement ( set1 -- set2 )
241 :     empty ['] bit-equivalent binary-set-operation ;
242 :    
243 :     \ types
244 :    
245 : anton 1.49 : stack-access ( n stack -- )
246 :     \ print a stack access at index n of stack
247 :     stack-pointer 2@ type
248 :     dup
249 :     if
250 :     ." [" 0 .r ." ]"
251 :     else
252 :     drop ." TOS"
253 :     endif ;
254 : anton 1.1
255 : anton 1.53 : item-in-index { item -- n }
256 : anton 1.49 \ n is the index of item (in the in-effect)
257 : anton 1.53 item item-stack @ dup >r stack-in @ ( in-size r:stack )
258 :     item r> stack-in-index-xt @ execute ;
259 : anton 1.1
260 :     : fetch-single ( item -- )
261 : anton 1.49 \ fetch a single stack item from its stack
262 : anton 1.1 >r
263 : pazsan 1.8 r@ item-name 2@ type
264 :     ." = ("
265 : anton 1.1 r@ item-type @ type-c-name 2@ type ." ) "
266 : anton 1.49 r@ item-in-index r@ item-stack @ stack-access
267 :     ." ;" cr
268 : anton 1.1 rdrop ;
269 :    
270 :     : fetch-double ( item -- )
271 : anton 1.49 \ fetch a double stack item from its stack
272 : anton 1.1 >r
273 : anton 1.20 ." FETCH_DCELL("
274 :     r@ item-name 2@ type ." , "
275 : anton 1.61 r@ item-in-index r@ item-stack @ 2dup ." (Cell)" stack-access
276 :     ." , " -1 under+ ." (Cell)" stack-access
277 : anton 1.20 ." );" cr
278 : anton 1.1 rdrop ;
279 :    
280 : anton 1.49 : same-as-in? ( item -- f )
281 :     \ f is true iff the offset and stack of item is the same as on input
282 : anton 1.1 >r
283 :     r@ item-name 2@ items @ search-wordlist 0=
284 : pazsan 1.8 abort" bug"
285 : anton 1.1 execute @
286 :     dup r@ =
287 :     if \ item first appeared in output
288 :     drop false
289 :     else
290 : anton 1.49 dup item-stack @ r@ item-stack @ =
291 :     swap item-offset @ r@ item-offset @ = and
292 : anton 1.1 endif
293 :     rdrop ;
294 :    
295 : anton 1.49 : item-out-index ( item -- n )
296 :     \ n is the index of item (in the in-effect)
297 :     >r r@ item-stack @ stack-out @ r> item-offset @ - 1- ;
298 : pazsan 1.31
299 : anton 1.1 : really-store-single ( item -- )
300 :     >r
301 : anton 1.49 r@ item-out-index r@ item-stack @ stack-access ." = "
302 :     r@ item-stack @ stack-cast 2@ type
303 : anton 1.1 r@ item-name 2@ type ." ;"
304 :     rdrop ;
305 :    
306 :     : store-single ( item -- )
307 :     >r
308 : anton 1.49 r@ same-as-in?
309 : anton 1.1 if
310 : anton 1.49 r@ item-in-index 0= r@ item-out-index 0= xor
311 : anton 1.1 if
312 : anton 1.49 ." IF_" r@ item-stack @ stack-pointer 2@ type
313 :     ." TOS(" r@ really-store-single ." );" cr
314 : anton 1.1 endif
315 :     else
316 :     r@ really-store-single cr
317 :     endif
318 :     rdrop ;
319 :    
320 :     : store-double ( item -- )
321 :     \ !! store optimization is not performed, because it is not yet needed
322 :     >r
323 : anton 1.20 ." STORE_DCELL(" r@ item-name 2@ type ." , "
324 : anton 1.49 r@ item-out-index r@ item-stack @ 2dup stack-access
325 :     ." , " -1 under+ stack-access
326 : anton 1.20 ." );" cr
327 : anton 1.1 rdrop ;
328 :    
329 : anton 1.54 : single ( -- xt1 xt2 n )
330 :     ['] fetch-single ['] store-single 1 ;
331 : anton 1.1
332 : anton 1.54 : double ( -- xt1 xt2 n )
333 :     ['] fetch-double ['] store-double 2 ;
334 : anton 1.1
335 :     : s, ( addr u -- )
336 :     \ allocate a string
337 :     here swap dup allot move ;
338 :    
339 : anton 1.50 wordlist constant prefixes
340 :    
341 :     : declare ( addr "name" -- )
342 :     \ remember that there is a stack item at addr called name
343 :     create , ;
344 :    
345 :     : !default ( w addr -- )
346 :     dup @ if
347 :     2drop \ leave nonzero alone
348 :     else
349 :     !
350 :     endif ;
351 :    
352 :     : create-type { addr u xt1 xt2 n stack -- } ( "prefix" -- )
353 : anton 1.49 \ describes a type
354 :     \ addr u specifies the C type name
355 :     \ stack effect entries of the type start with prefix
356 :     create type% %allot >r
357 :     addr u save-mem r@ type-c-name 2!
358 :     xt1 r@ type-fetch !
359 :     xt2 r@ type-store !
360 :     n r@ type-size !
361 :     stack r@ type-stack !
362 :     rdrop ;
363 : anton 1.1
364 : anton 1.54 : type-prefix ( xt1 xt2 n stack "prefix" -- )
365 : anton 1.50 create-type
366 :     does> ( item -- )
367 :     \ initialize item
368 :     { item typ }
369 :     typ item item-type !
370 :     typ type-stack @ item item-stack !default
371 :     item item-name 2@ items @ search-wordlist 0= if \ new name
372 : anton 1.66 item item-name 2@ nextname item declare
373 :     item item-first on
374 :     \ typ type-c-name 2@ type space type ." ;" cr
375 : anton 1.50 else
376 :     drop
377 : anton 1.66 item item-first off
378 : anton 1.50 endif ;
379 :    
380 :     : execute-prefix ( item addr1 u1 -- )
381 :     \ execute the word ( item -- ) associated with the longest prefix
382 :     \ of addr1 u1
383 :     0 swap ?do
384 :     dup i prefixes search-wordlist
385 :     if \ ok, we have the type ( item addr1 xt )
386 :     nip execute
387 :     UNLOOP EXIT
388 :     endif
389 :     -1 s+loop
390 :     \ we did not find a type, abort
391 :     true abort" unknown prefix" ;
392 : anton 1.1
393 :     : declaration ( item -- )
394 : anton 1.50 dup item-name 2@ execute-prefix ;
395 : anton 1.1
396 : anton 1.64 : declaration-list ( addr1 addr2 -- )
397 :     ['] declaration map-items ;
398 :    
399 :     : declarations ( -- )
400 :     wordlist dup items ! set-current
401 : anton 1.69 prim prim-effect-in prim prim-effect-in-end @ declaration-list
402 :     prim prim-effect-out prim prim-effect-out-end @ declaration-list ;
403 : anton 1.64
404 : anton 1.66 : print-declaration { item -- }
405 :     item item-first @ if
406 :     item item-type @ type-c-name 2@ type space
407 :     item item-name 2@ type ." ;" cr
408 :     endif ;
409 :    
410 :     : print-declarations ( -- )
411 : anton 1.69 prim prim-effect-in prim prim-effect-in-end @ ['] print-declaration map-items
412 :     prim prim-effect-out prim prim-effect-out-end @ ['] print-declaration map-items ;
413 : anton 1.66
414 : anton 1.51 : stack-prefix ( stack "prefix" -- )
415 :     name tuck nextname create ( stack length ) 2,
416 :     does> ( item -- )
417 :     2@ { item stack prefix-length }
418 :     item item-name 2@ prefix-length /string item item-name 2!
419 :     stack item item-stack !
420 :     item declaration ;
421 : anton 1.73
422 :     s" sp" save-mem s" Cell" save-mem s" (Cell)" make-stack data-stack
423 :     s" fp" save-mem s" Float" save-mem s" " make-stack fp-stack
424 :     s" rp" save-mem s" Cell" save-mem s" (Cell)" make-stack return-stack
425 :     s" IP" save-mem s" Cell" save-mem s" error don't use # on results" make-stack inst-stream
426 :     ' inst-in-index inst-stream stack-in-index-xt !
427 :     \ !! initialize stack-in and stack-out
428 : anton 1.1
429 :     \ offset computation
430 :     \ the leftmost (i.e. deepest) item has offset 0
431 :     \ the rightmost item has the highest offset
432 :    
433 : anton 1.49 : compute-offset { item xt -- }
434 :     \ xt specifies in/out; update stack-in/out and set item-offset
435 :     item item-type @ type-size @
436 :     item item-stack @ xt execute dup @ >r +!
437 :     r> item item-offset ! ;
438 :    
439 : anton 1.64 : compute-offset-in ( addr1 addr2 -- )
440 :     ['] stack-in compute-offset ;
441 :    
442 :     : compute-offset-out ( addr1 addr2 -- )
443 :     ['] stack-out compute-offset ;
444 : anton 1.49
445 :     : clear-stack { -- }
446 :     dup stack-in off stack-out off ;
447 : anton 1.1
448 :     : compute-offsets ( -- )
449 : anton 1.51 data-stack clear-stack fp-stack clear-stack return-stack clear-stack
450 : anton 1.53 inst-stream clear-stack
451 : anton 1.69 prim prim-effect-in prim prim-effect-in-end @ ['] compute-offset-in map-items
452 :     prim prim-effect-out prim prim-effect-out-end @ ['] compute-offset-out map-items
453 : anton 1.53 inst-stream stack-out @ 0<> abort" # can only be on the input side" ;
454 : anton 1.49
455 :     : flush-a-tos { stack -- }
456 :     stack stack-out @ 0<> stack stack-in @ 0= and
457 :     if
458 :     ." IF_" stack stack-pointer 2@ 2dup type ." TOS("
459 :     2dup type ." [0] = " type ." TOS);" cr
460 :     endif ;
461 : anton 1.1
462 :     : flush-tos ( -- )
463 : anton 1.51 data-stack flush-a-tos
464 :     fp-stack flush-a-tos
465 :     return-stack flush-a-tos ;
466 : anton 1.49
467 :     : fill-a-tos { stack -- }
468 :     stack stack-out @ 0= stack stack-in @ 0<> and
469 :     if
470 :     ." IF_" stack stack-pointer 2@ 2dup type ." TOS("
471 :     2dup type ." TOS = " type ." [0]);" cr
472 :     endif ;
473 : anton 1.1
474 :     : fill-tos ( -- )
475 : anton 1.53 \ !! inst-stream for prefetching?
476 : anton 1.51 fp-stack fill-a-tos
477 :     data-stack fill-a-tos
478 :     return-stack fill-a-tos ;
479 : anton 1.49
480 :     : fetch ( addr -- )
481 : anton 1.72 dup item-type @ type-fetch @ execute ;
482 : anton 1.1
483 :     : fetches ( -- )
484 : anton 1.69 prim prim-effect-in prim prim-effect-in-end @ ['] fetch map-items ;
485 : anton 1.49
486 :     : stack-pointer-update { stack -- }
487 :     \ stack grow downwards
488 :     stack stack-in @ stack stack-out @ -
489 :     ?dup-if \ this check is not necessary, gcc would do this for us
490 :     stack stack-pointer 2@ type ." += " 0 .r ." ;" cr
491 :     endif ;
492 : anton 1.1
493 : anton 1.55 : inst-pointer-update ( -- )
494 :     inst-stream stack-in @ ?dup-if
495 :     ." INC_IP(" 0 .r ." );" cr
496 :     endif ;
497 :    
498 : anton 1.1 : stack-pointer-updates ( -- )
499 : anton 1.55 inst-pointer-update
500 : anton 1.51 data-stack stack-pointer-update
501 :     fp-stack stack-pointer-update
502 :     return-stack stack-pointer-update ;
503 : anton 1.1
504 :     : store ( item -- )
505 :     \ f is true if the item should be stored
506 :     \ f is false if the store is probably not necessary
507 : anton 1.49 dup item-type @ type-store @ execute ;
508 : anton 1.1
509 :     : stores ( -- )
510 : anton 1.69 prim prim-effect-out prim prim-effect-out-end @ ['] store map-items ;
511 : pazsan 1.8
512 : anton 1.52 : output-c-tail ( -- )
513 :     \ the final part of the generated C code
514 :     ." NEXT_P1;" cr
515 :     stores
516 :     fill-tos
517 :     ." NEXT_P2;" cr ;
518 :    
519 :     : type-c ( c-addr u -- )
520 :     \ like TYPE, but replaces "TAIL;" with tail code
521 :     begin ( c-addr1 u1 )
522 :     2dup s" TAIL;" search
523 :     while ( c-addr1 u1 c-addr3 u3 )
524 :     2dup 2>r drop nip over - type
525 :     output-c-tail
526 :     2r> 5 /string
527 :     \ !! resync #line missing
528 :     repeat
529 :     2drop type ;
530 :    
531 : anton 1.63 : print-type-prefix ( type -- )
532 :     body> >head .name ;
533 :    
534 :     : print-debug-arg { item -- }
535 :     ." fputs(" quote space item item-name 2@ type ." =" quote ." , vm_out); "
536 :     ." printarg_" item item-type @ print-type-prefix
537 :     ." (" item item-name 2@ type ." );" cr ;
538 :    
539 :     : print-debug-args ( -- )
540 :     ." #ifdef VM_DEBUG" cr
541 :     ." if (vm_debug) {" cr
542 : anton 1.69 prim prim-effect-in prim prim-effect-in-end @ ['] print-debug-arg map-items
543 : anton 1.63 ." fputc('\n', vm_out);" cr
544 :     ." }" cr
545 :     ." #endif" cr ;
546 : anton 1.72
547 :     : print-entry ( -- )
548 :     ." I_" prim prim-c-name 2@ type ." :" ;
549 : anton 1.63
550 : jwilke 1.43 : output-c ( -- )
551 : anton 1.72 print-entry ." /* " prim prim-name 2@ type ." ( " prim prim-stack-string 2@ type ." ) */" cr
552 : anton 1.69 ." /* " prim prim-doc 2@ type ." */" cr
553 :     ." NAME(" quote prim prim-name 2@ type quote ." )" cr \ debugging
554 : anton 1.1 ." {" cr
555 :     ." DEF_CA" cr
556 : anton 1.66 print-declarations
557 : anton 1.13 ." NEXT_P0;" cr
558 :     flush-tos
559 : anton 1.1 fetches
560 : anton 1.63 print-debug-args
561 : anton 1.13 stack-pointer-updates
562 : anton 1.1 ." {" cr
563 : anton 1.63 ." #line " c-line @ . quote c-filename 2@ type quote cr
564 : anton 1.69 prim prim-c-code 2@ type-c
565 : anton 1.1 ." }" cr
566 : anton 1.52 output-c-tail
567 : anton 1.1 ." }" cr
568 :     cr
569 :     ;
570 :    
571 : anton 1.56 : disasm-arg { item -- }
572 :     item item-stack @ inst-stream = if
573 : anton 1.63 ." fputc(' ', vm_out); "
574 :     ." printarg_" item item-type @ print-type-prefix
575 :     ." ((" item item-type @ type-c-name 2@ type ." )"
576 :     ." ip[" item item-offset @ 1+ 0 .r ." ]);" cr
577 : anton 1.56 endif ;
578 :    
579 :     : disasm-args ( -- )
580 : anton 1.69 prim prim-effect-in prim prim-effect-in-end @ ['] disasm-arg map-items ;
581 : anton 1.56
582 :     : output-disasm ( -- )
583 :     \ generate code for disassembling VM instructions
584 :     ." if (ip[0] == prim[" function-number @ 0 .r ." ]) {" cr
585 : anton 1.69 ." fputs(" quote prim prim-name 2@ type quote ." , vm_out);" cr
586 : anton 1.56 disasm-args
587 :     ." ip += " inst-stream stack-in @ 1+ 0 .r ." ;" cr
588 : anton 1.68 ." } else " ;
589 : anton 1.56
590 : anton 1.60 : gen-arg-parm { item -- }
591 :     item item-stack @ inst-stream = if
592 :     ." , " item item-type @ type-c-name 2@ type space
593 :     item item-name 2@ type
594 :     endif ;
595 :    
596 :     : gen-args-parm ( -- )
597 : anton 1.69 prim prim-effect-in prim prim-effect-in-end @ ['] gen-arg-parm map-items ;
598 : anton 1.60
599 :     : gen-arg-gen { item -- }
600 :     item item-stack @ inst-stream = if
601 :     ." genarg_" item item-type @ print-type-prefix
602 :     ." (ctp, " item item-name 2@ type ." );" cr
603 :     endif ;
604 :    
605 :     : gen-args-gen ( -- )
606 : anton 1.69 prim prim-effect-in prim prim-effect-in-end @ ['] gen-arg-gen map-items ;
607 : anton 1.60
608 :     : output-gen ( -- )
609 :     \ generate C code for generating VM instructions
610 : anton 1.69 ." void gen_" prim prim-c-name 2@ type ." (Inst **ctp" gen-args-parm ." )" cr
611 : anton 1.60 ." {" cr
612 :     ." gen_inst(ctp, vm_prim[" function-number @ 0 .r ." ]);" cr
613 :     gen-args-gen
614 : anton 1.68 ." }" cr ;
615 : anton 1.60
616 : anton 1.49 : stack-used? { stack -- f }
617 :     stack stack-in @ stack stack-out @ or 0<> ;
618 : jwilke 1.44
619 : pazsan 1.30 : output-funclabel ( -- )
620 : anton 1.69 ." &I_" prim prim-c-name 2@ type ." ," cr ;
621 : pazsan 1.30
622 :     : output-forthname ( -- )
623 : anton 1.69 '" emit prim prim-name 2@ type '" emit ." ," cr ;
624 : pazsan 1.30
625 :     : output-c-func ( -- )
626 : jwilke 1.44 \ used for word libraries
627 : anton 1.69 ." Cell * I_" prim prim-c-name 2@ type ." (Cell *SP, Cell **FP) /* " prim prim-name 2@ type
628 :     ." ( " prim prim-stack-string 2@ type ." ) */" cr
629 :     ." /* " prim prim-doc 2@ type ." */" cr
630 :     ." NAME(" quote prim prim-name 2@ type quote ." )" cr
631 : pazsan 1.30 \ debugging
632 :     ." {" cr
633 : anton 1.66 print-declarations
634 : anton 1.53 inst-stream stack-used? IF ." Cell *ip=IP;" cr THEN
635 : anton 1.51 data-stack stack-used? IF ." Cell *sp=SP;" cr THEN
636 :     fp-stack stack-used? IF ." Cell *fp=*FP;" cr THEN
637 :     return-stack stack-used? IF ." Cell *rp=*RP;" cr THEN
638 : pazsan 1.30 flush-tos
639 :     fetches
640 :     stack-pointer-updates
641 : anton 1.49 fp-stack stack-used? IF ." *FP=fp;" cr THEN
642 : pazsan 1.30 ." {" cr
643 : anton 1.63 ." #line " c-line @ . quote c-filename 2@ type quote cr
644 : anton 1.69 prim prim-c-code 2@ type
645 : pazsan 1.30 ." }" cr
646 :     stores
647 :     fill-tos
648 : jwilke 1.44 ." return (sp);" cr
649 : pazsan 1.30 ." }" cr
650 :     cr ;
651 :    
652 : jwilke 1.43 : output-label ( -- )
653 : anton 1.69 ." (Label)&&I_" prim prim-c-name 2@ type ." ," cr ;
654 : anton 1.1
655 : jwilke 1.43 : output-alias ( -- )
656 : anton 1.69 ( primitive-number @ . ." alias " ) ." Primitive " prim prim-name 2@ type cr ;
657 : anton 1.1
658 : jwilke 1.43 : output-forth ( -- )
659 : anton 1.69 prim prim-forth-code @ 0=
660 : pazsan 1.30 IF \ output-alias
661 : jwilke 1.28 \ this is bad for ec: an alias is compiled if tho word does not exist!
662 :     \ JAW
663 : anton 1.69 ELSE ." : " prim prim-name 2@ type ." ( "
664 :     prim prim-stack-string 2@ type ." )" cr
665 :     prim prim-forth-code 2@ type cr
666 : pazsan 1.30 THEN ;
667 : anton 1.10
668 : anton 1.17 : 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 : anton 1.69 prim prim-name 2@ 1+ type
679 : anton 1.17 127 emit
680 : anton 1.69 space prim prim-name 2@ type space
681 : anton 1.17 1 emit
682 :     name-line @ 0 .r
683 :     ." ,0" cr ;
684 :    
685 : anton 1.10 [IFDEF] documentation
686 :     : register-doc ( -- )
687 :     get-current documentation set-current
688 : anton 1.69 prim prim-name 2@ nextname create
689 :     prim prim-name 2@ 2,
690 :     prim prim-stack-string 2@ condition-stack-effect 2,
691 :     prim prim-wordset 2@ 2,
692 :     prim prim-c-name 2@ condition-pronounciation 2,
693 :     prim prim-doc 2@ 2,
694 : anton 1.10 set-current ;
695 :     [THEN]
696 : anton 1.67
697 :    
698 : anton 1.69 \ combining instructions
699 :    
700 :     \ The input should look like this:
701 :    
702 :     \ lit_+ = lit +
703 :    
704 :     \ The output should look like this:
705 :    
706 :     \ I_lit_+:
707 :     \ {
708 :     \ DEF_CA
709 :     \ Cell _x_ip0;
710 :     \ Cell _x_sp0;
711 :     \ Cell _x_sp1;
712 :     \ NEXT_P0;
713 :     \ _x_ip0 = (Cell) IPTOS;
714 :     \ _x_sp0 = (Cell) spTOS;
715 :     \ INC_IP(1);
716 :     \ /* sp += 0; */
717 :     \ /* lit ( #w -- w ) */
718 :     \ /* */
719 :     \ NAME("lit")
720 :     \ {
721 :     \ Cell w;
722 :     \ w = (Cell) _x_ip0;
723 :     \ #ifdef VM_DEBUG
724 :     \ if (vm_debug) {
725 :     \ fputs(" w=", vm_out); printarg_w (w);
726 :     \ fputc('\n', vm_out);
727 :     \ }
728 :     \ #endif
729 :     \ {
730 :     \ #line 136 "./prim"
731 :     \ }
732 :     \ _x_sp1 = (Cell)w;
733 :     \ }
734 :     \ I_plus: /* + ( n1 n2 -- n ) */
735 :     \ /* */
736 :     \ NAME("+")
737 :     \ {
738 :     \ DEF_CA
739 :     \ Cell n1;
740 :     \ Cell n2;
741 :     \ Cell n;
742 :     \ NEXT_P0;
743 :     \ n1 = (Cell) _x_sp0;
744 :     \ n2 = (Cell) _x_sp1;
745 :     \ #ifdef VM_DEBUG
746 :     \ if (vm_debug) {
747 :     \ fputs(" n1=", vm_out); printarg_n (n1);
748 :     \ fputs(" n2=", vm_out); printarg_n (n2);
749 :     \ fputc('\n', vm_out);
750 :     \ }
751 :     \ #endif
752 :     \ {
753 :     \ #line 516 "./prim"
754 :     \ n = n1+n2;
755 :     \ }
756 :     \ NEXT_P1;
757 :     \ _x_sp0 = (Cell)n;
758 :     \ NEXT_P2;
759 :     \ }
760 :     \ NEXT_P1;
761 :     \ spTOS = (Cell)_x_sp0;
762 :     \ NEXT_P2;
763 :    
764 : anton 1.71 1000 constant max-combined
765 :     create combined-prims max-combined cells allot
766 :     variable num-combined
767 :    
768 :     create current-depth max-stacks cells allot
769 :     create max-depth max-stacks cells allot
770 : anton 1.72 create min-depth max-stacks cells allot
771 : anton 1.71
772 :     : init-combined ( -- )
773 :     0 num-combined !
774 :     current-depth max-stacks cells erase
775 : anton 1.72 max-depth max-stacks cells erase
776 :     min-depth max-stacks cells erase
777 :     prim prim-effect-in prim prim-effect-in-end !
778 :     prim prim-effect-out prim prim-effect-out-end ! ;
779 : anton 1.71
780 :     : max! ( n addr -- )
781 :     tuck @ max swap ! ;
782 :    
783 : anton 1.72 : min! ( n addr -- )
784 :     tuck @ min swap ! ;
785 :    
786 : anton 1.71 : add-depths { p -- }
787 :     \ combine stack effect of p with *-depths
788 :     max-stacks 0 ?do
789 : anton 1.72 current-depth i th @
790 :     p prim-stacks-in i th @ +
791 :     dup max-depth i th max!
792 :     p prim-stacks-out i th @ -
793 :     dup min-depth i th min!
794 :     current-depth i th !
795 : anton 1.71 loop ;
796 :    
797 :     : add-prim ( addr u -- )
798 :     \ add primitive given by "addr u" to combined-prims
799 :     primitives search-wordlist s" unknown primitive" ?print-error
800 :     execute { p }
801 : anton 1.72 p combined-prims num-combined @ th !
802 : anton 1.71 1 num-combined +!
803 :     p add-depths ;
804 :    
805 :     : compute-effects { q -- }
806 :     \ compute the stack effects of q from the depths
807 :     max-stacks 0 ?do
808 : anton 1.72 max-depth i th @ dup
809 :     q prim-stacks-in i th !
810 :     current-depth i th @ -
811 :     q prim-stacks-out i th !
812 :     loop ;
813 :    
814 :     : make-effect-items { stack# items effect-endp -- }
815 :     \ effect-endp points to a pointer to the end of the current item-array
816 :     \ and has to be updated
817 :     stacks stack# th @ { stack }
818 :     items 0 +do
819 :     effect-endp @ { item }
820 :     i 0 <# #s stack stack-pointer 2@ holds [char] _ hold #> save-mem
821 :     item item-name 2!
822 :     stack item item-stack !
823 :     0 item item-type !
824 :     i item item-offset !
825 :     item item-first on
826 :     item% %size effect-endp +!
827 :     loop ;
828 :    
829 :     : init-effects { q -- }
830 :     \ initialize effects field for FETCHES and STORES
831 :     max-stacks 0 ?do
832 :     i q prim-stacks-in i th @ q prim-effect-in-end make-effect-items
833 :     i q prim-stacks-out i th @ q prim-effect-out-end make-effect-items
834 : anton 1.71 loop ;
835 :    
836 :     : process-combined ( -- )
837 : anton 1.72 prim compute-effects
838 :     prim init-effects
839 :     output-combined perform ;
840 :    
841 :     \ C output
842 :    
843 :     : print-item { n stack -- }
844 :     \ print nth stack item name
845 :     ." _" stack stack-typename 2@ type space
846 :     stack stack-pointer 2@ type n 0 .r ;
847 :    
848 :     : print-declarations-combined ( -- )
849 :     max-stacks 0 ?do
850 :     max-depth i th @ min-depth i th @ - 0 +do
851 :     i stacks j th @ print-item ." ;" cr
852 :     loop
853 :     loop ;
854 :    
855 :     : output-c-combined ( -- )
856 :     print-entry cr
857 :     \ debugging messages just in constituents
858 :     ." {" cr
859 :     ." DEF_CA" cr
860 :     print-declarations-combined
861 :     ." NEXT_P0;" cr
862 :     flush-tos
863 :     fetches
864 :     ;
865 :    
866 :     : output-forth-combined ( -- )
867 :     ;
868 : anton 1.69
869 : anton 1.67 \ the parser
870 :    
871 :     eof-char max-member \ the whole character set + EOF
872 :    
873 :     : getinput ( -- n )
874 :     rawinput @ endrawinput @ =
875 :     if
876 :     eof-char
877 :     else
878 :     cookedinput @ c@
879 :     endif ;
880 :    
881 :     :noname ( n -- )
882 :     dup bl > if
883 :     emit space
884 :     else
885 :     .
886 :     endif ;
887 :     print-token !
888 :    
889 :     : testchar? ( set -- f )
890 :     getinput member? ;
891 :     ' testchar? test-vector !
892 :    
893 :     : checksyncline ( -- )
894 :     \ when input points to a newline, check if the next line is a
895 :     \ sync line. If it is, perform the appropriate actions.
896 :     rawinput @ >r
897 :     s" #line " r@ over compare 0<> if
898 :     rdrop 1 line +! EXIT
899 :     endif
900 :     0. r> 6 chars + 20 >number drop >r drop line ! r> ( c-addr )
901 :     dup c@ bl = if
902 :     char+ dup c@ [char] " <> abort" sync line syntax"
903 :     char+ dup 100 [char] " scan drop swap 2dup - save-mem filename 2!
904 :     char+
905 :     endif
906 :     dup c@ nl-char <> abort" sync line syntax"
907 :     skipsynclines @ if
908 :     dup char+ rawinput !
909 :     rawinput @ c@ cookedinput @ c!
910 :     endif
911 :     drop ;
912 :    
913 :     : ?nextchar ( f -- )
914 : anton 1.71 s" syntax error, wrong char" ?print-error
915 : anton 1.67 rawinput @ endrawinput @ <> if
916 :     rawinput @ c@
917 :     1 chars rawinput +!
918 :     1 chars cookedinput +!
919 :     nl-char = if
920 :     checksyncline
921 :     rawinput @ line-start !
922 :     endif
923 :     rawinput @ c@ cookedinput @ c!
924 :     endif ;
925 :    
926 :     : charclass ( set "name" -- )
927 :     ['] ?nextchar terminal ;
928 :    
929 :     : .. ( c1 c2 -- set )
930 :     ( creates a set that includes the characters c, c1<=c<=c2 )
931 :     empty copy-set
932 :     swap 1+ rot do
933 :     i over add-member
934 :     loop ;
935 :    
936 :     : ` ( -- terminal ) ( use: ` c )
937 :     ( creates anonymous terminal for the character c )
938 :     char singleton ['] ?nextchar make-terminal ;
939 :    
940 :     char a char z .. char A char Z .. union char _ singleton union charclass letter
941 :     char 0 char 9 .. charclass digit
942 :     bl singleton tab-char over add-member charclass white
943 :     nl-char singleton eof-char over add-member complement charclass nonl
944 :     nl-char singleton eof-char over add-member
945 :     char : over add-member complement charclass nocolonnl
946 :     bl 1+ maxchar .. char \ singleton complement intersection
947 :     charclass nowhitebq
948 :     bl 1+ maxchar .. charclass nowhite
949 :     char " singleton eof-char over add-member complement charclass noquote
950 :     nl-char singleton charclass nl
951 :     eof-char singleton charclass eof
952 :    
953 :    
954 :     (( letter (( letter || digit )) **
955 :     )) <- c-ident ( -- )
956 :    
957 :     (( ` # ?? (( letter || digit || ` : )) **
958 :     )) <- stack-ident ( -- )
959 :    
960 :     (( nowhitebq nowhite ** ))
961 :     <- forth-ident ( -- )
962 :    
963 :     Variable forth-flag
964 :     Variable c-flag
965 :    
966 :     (( (( ` e || ` E )) {{ start }} nonl **
967 :     {{ end evaluate }}
968 :     )) <- eval-comment ( ... -- ... )
969 :    
970 :     (( (( ` f || ` F )) {{ start }} nonl **
971 :     {{ end forth-flag @ IF type cr ELSE 2drop THEN }}
972 :     )) <- forth-comment ( -- )
973 :    
974 :     (( (( ` c || ` C )) {{ start }} nonl **
975 :     {{ end c-flag @ IF type cr ELSE 2drop THEN }}
976 :     )) <- c-comment ( -- )
977 :    
978 :     (( ` - nonl ** {{
979 :     forth-flag @ IF ." [ELSE]" cr THEN
980 :     c-flag @ IF ." #else" cr THEN }}
981 :     )) <- else-comment
982 :    
983 :     (( ` + {{ start }} nonl ** {{ end
984 :     dup
985 :     IF c-flag @
986 :     IF ." #ifdef HAS_" bounds ?DO I c@ toupper emit LOOP cr
987 :     THEN
988 :     forth-flag @
989 :     IF ." has? " type ." [IF]" cr THEN
990 :     ELSE 2drop
991 :     c-flag @ IF ." #endif" cr THEN
992 :     forth-flag @ IF ." [THEN]" cr THEN
993 :     THEN }}
994 :     )) <- if-comment
995 :    
996 :     (( (( eval-comment || forth-comment || c-comment || else-comment || if-comment )) ?? nonl ** )) <- comment-body
997 :    
998 :     (( ` \ comment-body nl )) <- comment ( -- )
999 :    
1000 :     (( {{ start }} stack-ident {{ end 2 pick init-item item% %size + }} white ** )) **
1001 :     <- stack-items
1002 :    
1003 : anton 1.69 (( {{ prim prim-effect-in }} stack-items {{ prim prim-effect-in-end ! }}
1004 : anton 1.67 ` - ` - white **
1005 : anton 1.69 {{ prim prim-effect-out }} stack-items {{ prim prim-effect-out-end ! }}
1006 : anton 1.67 )) <- stack-effect ( -- )
1007 :    
1008 : anton 1.71 (( {{ prim create-prim }}
1009 : anton 1.69 ` ( white ** {{ start }} stack-effect {{ end prim prim-stack-string 2! }} ` ) white **
1010 :     (( {{ start }} forth-ident {{ end prim prim-wordset 2! }} white **
1011 :     (( {{ start }} c-ident {{ end prim prim-c-name 2! }} )) ??
1012 :     )) ?? nl
1013 :     (( ` " ` " {{ start }} (( noquote ++ ` " )) ++ {{ end 1- prim prim-doc 2! }} ` " white ** nl )) ??
1014 :     {{ skipsynclines off line @ c-line ! filename 2@ c-filename 2! start }} (( nocolonnl nonl ** nl white ** )) ** {{ end prim prim-c-code 2! skipsynclines on }}
1015 : anton 1.67 (( ` : white ** nl
1016 : anton 1.69 {{ start }} (( nonl ++ nl white ** )) ++ {{ end prim prim-forth-code 2! }}
1017 : anton 1.68 )) ?? {{ declarations compute-offsets printprim 1 function-number +! }}
1018 : anton 1.67 (( nl || eof ))
1019 : anton 1.69 )) <- simple-primitive ( -- )
1020 :    
1021 : anton 1.71 (( {{ init-combined }}
1022 :     ` = (( white ++ {{ start }} forth-ident {{ end add-prim }} )) ++
1023 :     (( nl || eof )) {{ process-combined }}
1024 : anton 1.69 )) <- combined-primitive
1025 :    
1026 : anton 1.70 (( {{ make-prim to prim
1027 : anton 1.69 line @ name-line ! filename 2@ name-filename 2!
1028 :     start }} forth-ident {{ end 2dup prim prim-name 2! prim prim-c-name 2! }} white ++
1029 :     (( simple-primitive || combined-primitive ))
1030 : anton 1.67 )) <- primitive ( -- )
1031 :    
1032 :     (( (( comment || primitive || nl white ** )) ** eof ))
1033 :     parser primitives2something
1034 :     warnings @ [IF]
1035 :     .( parser generated ok ) cr
1036 :     [THEN]
1037 :    
1038 : anton 1.69 : primfilter ( addr u -- )
1039 :     \ process the string at addr u
1040 :     over dup rawinput ! dup line-start ! cookedinput !
1041 :     + endrawinput !
1042 :     checksyncline
1043 :     primitives2something ;
1044 : pazsan 1.8
1045 : anton 1.72 : process-file ( addr u xt-simple x-combined -- )
1046 :     output-combined ! output !
1047 : anton 1.61 save-mem 2dup filename 2!
1048 : anton 1.69 slurp-file
1049 : anton 1.17 warnings @ if
1050 :     ." ------------ CUT HERE -------------" cr endif
1051 : anton 1.69 primfilter ;
1052 : pazsan 1.30
1053 : anton 1.72 \ : process ( xt -- )
1054 :     \ bl word count rot
1055 :     \ process-file ;

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help