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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help