[gforth] / gforth / cross.fs  

gforth: gforth/cross.fs


1 : anton 1.1 \ CROSS.FS The Cross-Compiler 06oct92py
2 :     \ Idea and implementation: Bernd Paysan (py)
3 : anton 1.30
4 : anton 1.58 \ Copyright (C) 1995,1996,1997,1998 Free Software Foundation, Inc.
5 : anton 1.30
6 :     \ This file is part of Gforth.
7 :    
8 :     \ Gforth is free software; you can redistribute it and/or
9 :     \ modify it under the terms of the GNU General Public License
10 :     \ as published by the Free Software Foundation; either version 2
11 :     \ of the License, or (at your option) any later version.
12 :    
13 :     \ This program is distributed in the hope that it will be useful,
14 :     \ but WITHOUT ANY WARRANTY; without even the implied warranty of
15 :     \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 :     \ GNU General Public License for more details.
17 :    
18 :     \ You should have received a copy of the GNU General Public License
19 :     \ along with this program; if not, write to the Free Software
20 :     \ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 : anton 1.1
22 : jwilke 1.67 0
23 :     [IF]
24 :    
25 :     ToDo:
26 :     Crossdoc destination ./doc/crossdoc.fd makes no sense when
27 :     cross.fs is uses seperately. jaw
28 :     Do we need this char translation with >address and in branchoffset?
29 :     (>body also affected) jaw
30 :     Clean up mark> and >resolve stuff jaw
31 :    
32 :     [THEN]
33 : anton 1.48
34 :    
35 : jwilke 1.52 hex \ the defualt base for the cross-compiler is hex !!
36 :     Warnings off
37 :    
38 :     \ words that are generaly useful
39 :    
40 : pazsan 1.59 : KB 400 * ;
41 : jwilke 1.52 : >wordlist ( vocabulary-xt -- wordlist-struct )
42 :     also execute get-order swap >r 1- set-order r> ;
43 :    
44 :     : umax 2dup u< IF swap THEN drop ;
45 :     : umin 2dup u> IF swap THEN drop ;
46 : anton 1.1
47 : pazsan 1.23 : string, ( c-addr u -- )
48 :     \ puts down string as cstring
49 :     dup c, here swap chars dup allot move ;
50 : pazsan 1.5
51 : jwilke 1.52 : SetValue ( n -- <name> )
52 : pazsan 1.54 \G Same behaviour as "Value" if the <name> is not defined
53 :     \G Same behaviour as "to" if <name> is defined
54 : jwilke 1.52 \G SetValue searches in the current vocabulary
55 :     save-input bl word >r restore-input throw r> count
56 :     get-current search-wordlist
57 : pazsan 1.56 IF ['] to execute ELSE Value THEN ;
58 : jwilke 1.52
59 :     : DefaultValue ( n -- <name> )
60 : pazsan 1.54 \G Same behaviour as "Value" if the <name> is not defined
61 :     \G DefaultValue searches in the current vocabulary
62 : jwilke 1.52 save-input bl word >r restore-input throw r> count
63 :     get-current search-wordlist
64 : pazsan 1.56 IF bl word drop 2drop ELSE Value THEN ;
65 : anton 1.1
66 :     hex
67 :    
68 :     Vocabulary Cross
69 :     Vocabulary Target
70 :     Vocabulary Ghosts
71 :     VOCABULARY Minimal
72 :     only Forth also Target also also
73 :     definitions Forth
74 :    
75 :     : T previous Cross also Target ; immediate
76 :     : G Ghosts ; immediate
77 :     : H previous Forth also Cross ; immediate
78 :    
79 :     forth definitions
80 :    
81 :     : T previous Cross also Target ; immediate
82 :     : G Ghosts ; immediate
83 :    
84 :     : >cross also Cross definitions previous ;
85 :     : >target also Target definitions previous ;
86 :     : >minimal also Minimal definitions previous ;
87 :    
88 :     H
89 :    
90 :     >CROSS
91 :    
92 : jwilke 1.52 \ 1 Constant Cross-Flag \ to check whether assembler compiler plug-ins are
93 :     \ for cross-compiling
94 :     \ No! we use "[IFUNDEF]" there to find out whether we are target compiling!!!
95 :    
96 :     : comment? ( c-addr u -- c-addr u )
97 :     2dup s" (" compare 0=
98 :     IF postpone (
99 :     ELSE 2dup s" \" compare 0= IF postpone \ THEN
100 :     THEN ;
101 :    
102 :     \ Begin CROSS COMPILER:
103 :    
104 :    
105 :    
106 :     \ \ -------------------- Error Handling 05aug97jaw
107 :    
108 :     \ Flags
109 :    
110 :     also forth definitions \ these values may be predefined before
111 :     \ the cross-compiler is loaded
112 :    
113 :     false DefaultValue stack-warn \ check on empty stack at any definition
114 :     false DefaultValue create-forward-warn \ warn on forward declaration of created words
115 :    
116 :     [IFUNDEF] DebugMaskSrouce Variable DebugMaskSource 0 DebugMaskSource ! [THEN]
117 :     [IFUNDEF] DebugMaskCross Variable DebugMaskCross 0 DebugMaskCross ! [THEN]
118 :    
119 :     previous >CROSS
120 :    
121 : jwilke 1.53 : .dec
122 :     base @ decimal swap . base ! ;
123 :    
124 : jwilke 1.52 : .sourcepos
125 :     cr sourcefilename type ." :"
126 : jwilke 1.53 sourceline# .dec ;
127 : jwilke 1.52
128 :     : warnhead
129 :     \G display error-message head
130 :     \G perhaps with linenumber and filename
131 :     .sourcepos ." Warning: " ;
132 :    
133 :     : empty? depth IF .sourcepos ." Stack not empty!" THEN ;
134 :    
135 :     stack-warn [IF]
136 :     : defempty? empty? ;
137 :     [ELSE]
138 :     : defempty? ; immediate
139 :     [THEN]
140 :    
141 :    
142 : pazsan 1.54
143 : jwilke 1.52 \ \ GhostNames Ghosts 9may93jaw
144 :    
145 :     \ second name source to search trough list
146 :    
147 :     VARIABLE GhostNames
148 :     0 GhostNames !
149 :    
150 :     : GhostName ( -- addr )
151 :     here GhostNames @ , GhostNames ! here 0 ,
152 :     bl word count
153 :     \ 2dup type space
154 :     string, \ !! cfalign ?
155 :     align ;
156 :    
157 :     \ Ghost Builder 06oct92py
158 :    
159 :     \ <T T> new version with temp variable 10may93jaw
160 :    
161 :     VARIABLE VocTemp
162 :    
163 :     : <T get-current VocTemp ! also Ghosts definitions ;
164 :     : T> previous VocTemp @ set-current ;
165 :    
166 :     hex
167 :     4711 Constant <fwd> 4712 Constant <res>
168 :     4713 Constant <imm> 4714 Constant <do:>
169 :    
170 :     \ iForth makes only immediate directly after create
171 :     \ make atonce trick! ?
172 :    
173 :     Variable atonce atonce off
174 :    
175 :     : NoExec true ABORT" CROSS: Don't execute ghost" ;
176 :    
177 :     : GhostHeader <fwd> , 0 , ['] NoExec , ;
178 :    
179 :     : >magic ; \ type of ghost
180 :     : >link cell+ ; \ pointer where ghost is in target, or if unresolved
181 :     \ points to the where we have to resolve (linked-list)
182 :     : >exec cell+ cell+ ; \ execution symantics (while target compiling) of ghost
183 :     : >end 3 cells + ; \ room for additional tags
184 :     \ for builder (create, variable...) words the
185 :     \ execution symantics of words built are placed here
186 :    
187 :     Variable executed-ghost \ last executed ghost, needed in tcreate and gdoes>
188 :     Variable last-ghost \ last ghost that is created
189 :     Variable last-header-ghost \ last ghost definitions with header
190 :    
191 :     : Make-Ghost ( "name" -- ghost )
192 :     >in @ GhostName swap >in !
193 :     <T Create atonce @ IF immediate atonce off THEN
194 :     here tuck swap ! ghostheader T>
195 :     dup last-ghost !
196 :     DOES> dup executed-ghost ! >exec @ execute ;
197 :    
198 :     \ ghost words 14oct92py
199 :     \ changed: 10may93py/jaw
200 :    
201 :     : gfind ( string -- ghost true/1 / string false )
202 :     \ searches for string in word-list ghosts
203 :     dup count [ ' ghosts >wordlist ] ALiteral search-wordlist
204 :     dup IF >r >body nip r> THEN ;
205 :    
206 :     : gdiscover ( xt -- ghost true | xt false )
207 :     GhostNames
208 :     BEGIN @ dup
209 :     WHILE 2dup
210 :     cell+ @ dup >magic @ <fwd> <>
211 :     >r >link @ = r> and
212 :     IF cell+ @ nip true EXIT THEN
213 :     REPEAT
214 :     drop false ;
215 :    
216 :     VARIABLE Already
217 :    
218 :     : ghost ( "name" -- ghost )
219 :     Already off
220 :     >in @ bl word gfind IF Already on nip EXIT THEN
221 :     drop >in ! Make-Ghost ;
222 :    
223 :     : >ghostname ( ghost -- adr len )
224 :     GhostNames
225 :     BEGIN @ dup
226 :     WHILE 2dup cell+ @ =
227 :     UNTIL nip 2 cells + count
228 : pazsan 1.54 ELSE 2drop
229 :     \ true abort" CROSS: Ghostnames inconsistent"
230 :     s" ?!?!?!"
231 : jwilke 1.52 THEN ;
232 :    
233 :     ' >ghostname ALIAS @name
234 :    
235 :     : forward? ( ghost -- flag )
236 :     >magic @ <fwd> = ;
237 :    
238 :     \ Predefined ghosts 12dec92py
239 :    
240 :     ghost 0= drop
241 :     ghost branch ghost ?branch 2drop
242 :     ghost (do) ghost (?do) 2drop
243 :     ghost (for) drop
244 :     ghost (loop) ghost (+loop) 2drop
245 :     ghost (next) drop
246 :     ghost unloop ghost ;S 2drop
247 :     ghost lit ghost (compile) ghost ! 2drop drop
248 :     ghost (does>) ghost noop 2drop
249 :     ghost (.") ghost (S") ghost (ABORT") 2drop drop
250 :     ghost ' drop
251 :     ghost :docol ghost :doesjump ghost :dodoes 2drop drop
252 : pazsan 1.54 ghost :dovar drop
253 : jwilke 1.52 ghost over ghost = ghost drop 2drop drop
254 :     ghost - drop
255 : pazsan 1.54 ghost 2drop drop
256 :     ghost 2dup drop
257 : jwilke 1.52
258 :     \ \ Parameter for target systems 06oct92py
259 :    
260 :     \ we define it ans like...
261 :     wordlist Constant target-environment
262 :    
263 :     VARIABLE env-current \ save information of current dictionary to restore with environ>
264 :    
265 :     : >ENVIRON get-current env-current ! target-environment set-current ;
266 :     : ENVIRON> env-current @ set-current ;
267 : pazsan 1.43
268 : anton 1.48 >TARGET
269 : pazsan 1.43
270 : jwilke 1.67 : environment? ( adr len -- [ x ] true | false )
271 : jwilke 1.52 target-environment search-wordlist
272 :     IF execute true ELSE false THEN ;
273 :    
274 : jwilke 1.67 : e? bl word count T environment? H 0= ABORT" environment variable not defined!" ;
275 : jwilke 1.52
276 : jwilke 1.67 : has? bl word count T environment? H
277 : jwilke 1.53 IF \ environment variable is present, return its value
278 :     ELSE \ environment variable is not present, return false
279 :     \ !! JAW abort is just for testing
280 :     false true ABORT" arg"
281 :     THEN ;
282 : jwilke 1.52
283 :     : $has? T environment? H IF ELSE false THEN ;
284 : anton 1.48
285 : pazsan 1.54 >ENVIRON get-order get-current swap 1+ set-order
286 :     true SetValue compiler
287 : jwilke 1.53 true SetValue cross
288 : pazsan 1.54 true SetValue standard-threading
289 :     >TARGET previous
290 : jwilke 1.52
291 : pazsan 1.59
292 : jwilke 1.52 mach-file count included hex
293 : pazsan 1.43
294 : jwilke 1.53 >ENVIRON
295 :    
296 : pazsan 1.54 T has? ec H
297 :     [IF]
298 :     false DefaultValue relocate
299 :     false DefaultValue file
300 :     false DefaultValue OS
301 :     false DefaultValue prims
302 :     false DefaultValue floating
303 :     false DefaultValue glocals
304 :     false DefaultValue dcomps
305 :     false DefaultValue hash
306 :     false DefaultValue xconds
307 :     false DefaultValue header
308 :     [THEN]
309 :    
310 :     true DefaultValue interpreter
311 :     true DefaultValue ITC
312 :     false DefaultValue rom
313 : jwilke 1.53
314 : jwilke 1.52 >TARGET
315 : jwilke 1.53 s" relocate" T environment? H
316 :     [IF] SetValue NIL
317 :     [ELSE] >ENVIRON T NIL H SetValue relocate
318 :     [THEN]
319 : pazsan 1.43
320 :     >CROSS
321 :    
322 : jwilke 1.52 \ \ Create additional parameters 19jan95py
323 : pazsan 1.19
324 : jwilke 1.67 1 8 lshift Constant maxbyte
325 :     \ this sets byte size for the target machine, an (probably right guess) jaw
326 :    
327 : pazsan 1.19 T
328 : anton 1.48 NIL Constant TNIL
329 : pazsan 1.19 cell Constant tcell
330 :     cell<< Constant tcell<<
331 :     cell>bit Constant tcell>bit
332 :     bits/byte Constant tbits/byte
333 : pazsan 1.57 bits/byte 8 / Constant tchar
334 : pazsan 1.19 float Constant tfloat
335 : pazsan 1.57 1 bits/byte lshift Constant tmaxbyte
336 : pazsan 1.19 H
337 :    
338 : anton 1.48 \ Variables 06oct92py
339 :    
340 :     Variable image
341 :     Variable tlast TNIL tlast ! \ Last name field
342 :     Variable tlastcfa \ Last code field
343 :     Variable tdoes \ Resolve does> calls
344 :     Variable bit$
345 : jwilke 1.52
346 :     \ statistics 10jun97jaw
347 :    
348 :     Variable headers-named 0 headers-named !
349 :     Variable user-vars 0 user-vars !
350 :    
351 :     \ Memory initialisation 05dec92py
352 :    
353 :     [IFDEF] Memory \ Memory is a bigFORTH feature
354 :     also Memory
355 :     : initmem ( var len -- )
356 :     2dup swap handle! >r @ r> erase ;
357 :     toss
358 :     [ELSE]
359 :     : initmem ( var len -- )
360 :     tuck allocate abort" CROSS: No memory for target"
361 :     ( len var adr ) dup rot !
362 :     ( len adr ) swap erase ;
363 :     [THEN]
364 :    
365 :     \ MakeKernal 12dec92py
366 :    
367 :     : makekernel ( targetsize -- targetsize )
368 :     bit$ over 1- tcell>bit rshift 1+ initmem
369 :     image over initmem ;
370 :    
371 :     >MINIMAL
372 :     : makekernel makekernel ;
373 : jwilke 1.67 >CROSS
374 :    
375 :     : target>bitmask-size ( u1 -- u2 )
376 :     1- tcell>bit rshift 1+ ;
377 :    
378 :     : allocatetarget ( size --- adr )
379 :     dup allocate ABORT" CROSS: No memory for target"
380 :     swap over swap erase ;
381 : jwilke 1.52
382 :    
383 :    
384 : pazsan 1.54 \ \ memregion.fs
385 : jwilke 1.52
386 :    
387 :     Variable last-defined-region \ pointer to last defined region
388 :     Variable region-link \ linked list with all regions
389 :     Variable mirrored-link \ linked list for mirrored regions
390 :     0 dup mirrored-link ! region-link !
391 :    
392 :    
393 : jwilke 1.67 : >rname 6 cells + ;
394 :     : >rbm 5 cells + ;
395 :     : >rmem 4 cells + ;
396 :     : >rlink 3 cells + ;
397 : jwilke 1.52 : >rdp 2 cells + ;
398 :     : >rlen cell+ ;
399 :     : >rstart ;
400 :    
401 :    
402 :     : region ( addr len -- ) \G create a new region
403 :     \ check whether predefined region exists
404 :     save-input bl word find >r >r restore-input throw r> r> 0=
405 :     IF \ make region
406 :     drop
407 :     save-input create restore-input throw
408 :     here last-defined-region !
409 :     over ( startaddr ) , ( length ) , ( dp ) ,
410 : jwilke 1.67 region-link linked 0 , 0 , bl word count string,
411 : jwilke 1.52 ELSE \ store new parameters in region
412 :     bl word drop
413 :     >body >r r@ last-defined-region !
414 : jwilke 1.67 r@ >rlen ! dup r@ >rstart ! r> >rdp !
415 : jwilke 1.52 THEN ;
416 :    
417 :     : borders ( region -- startaddr endaddr ) \G returns lower and upper region border
418 : jwilke 1.67 dup >rstart @ swap >rlen @ over + ;
419 : jwilke 1.52
420 :     : extent ( region -- startaddr len ) \G returns the really used area
421 : jwilke 1.67 dup >rstart @ swap >rdp @ over - ;
422 : jwilke 1.52
423 :     : area ( region -- startaddr totallen ) \G returns the total area
424 : jwilke 1.67 dup >rstart swap >rlen @ ;
425 : jwilke 1.52
426 :     : mirrored \G mark a region as mirrored
427 :     mirrored-link
428 :     linked last-defined-region @ , ;
429 :    
430 : jwilke 1.67 : .addr ( u -- )
431 :     \G prints a 16 or 32 Bit nice hex value
432 : jwilke 1.52 base @ >r hex
433 :     tcell 2 u>
434 :     IF s>d <# # # # # '. hold # # # # #> type
435 :     ELSE s>d <# # # # # # #> type
436 :     THEN r> base ! ;
437 :    
438 :     : .regions \G display region statistic
439 :    
440 :     \ we want to list the regions in the right order
441 :     \ so first collect all regions on stack
442 :     0 region-link @
443 :     BEGIN dup WHILE dup @ REPEAT drop
444 :     BEGIN dup
445 : jwilke 1.67 WHILE cr
446 :     0 >rlink - >r
447 :     r@ >rname count tuck type
448 : jwilke 1.52 12 swap - 0 max spaces space
449 : jwilke 1.67 ." Start: " r@ >rstart @ dup .addr space
450 :     ." End: " r@ >rlen @ + .addr space
451 :     ." DP: " r> >rdp @ .addr
452 : jwilke 1.52 REPEAT drop
453 : jwilke 1.53 s" rom" T $has? H 0= ?EXIT
454 : jwilke 1.52 cr ." Mirrored:"
455 :     mirrored-link @
456 :     BEGIN dup
457 : jwilke 1.67 WHILE space dup cell+ @ >rname count type @
458 : jwilke 1.52 REPEAT drop cr
459 :     ;
460 :    
461 :     \ -------- predefined regions
462 :    
463 :     0 0 region address-space
464 :     \ total memory addressed and used by the target system
465 :    
466 :     0 0 region dictionary
467 :     \ rom area for the compiler
468 :    
469 : jwilke 1.53 T has? rom H
470 : jwilke 1.52 [IF]
471 :     0 0 region ram-dictionary mirrored
472 :     \ ram area for the compiler
473 :     [ELSE]
474 :     ' dictionary ALIAS ram-dictionary
475 :     [THEN]
476 :    
477 :     0 0 region return-stack
478 :    
479 :     0 0 region data-stack
480 :    
481 :     0 0 region tib-region
482 :    
483 :     ' dictionary ALIAS rom-dictionary
484 :    
485 :    
486 :     : setup-target ( -- ) \G initialize targets memory space
487 : jwilke 1.53 s" rom" T $has? H
488 : jwilke 1.52 IF \ check for ram and rom...
489 : jwilke 1.67 address-space area nip 0<>
490 :     ram-dictionary area nip 0<>
491 :     rom-dictionary area nip 0<>
492 : jwilke 1.52 and and 0=
493 :     ABORT" CROSS: define address-space, rom- , ram-dictionary, with rom-support!"
494 :     THEN
495 :     address-space area nip
496 :     IF
497 :     address-space area
498 :     ELSE
499 :     dictionary area
500 :     THEN
501 : jwilke 1.67 nip 0=
502 : jwilke 1.52 ABORT" CROSS: define at least address-space or dictionary!!"
503 : jwilke 1.67
504 :     \ allocate target for each region
505 :     region-link
506 :     BEGIN @ dup
507 :     WHILE dup
508 :     0 >rlink - >r
509 :     r@ >rlen @
510 :     IF \ allocate mem
511 :     r@ >rlen @ dup
512 :    
513 :     allocatetarget dup image !
514 :     r@ >rmem !
515 :    
516 :     target>bitmask-size allocatetarget
517 :     dup
518 :     bit$ !
519 :     r> >rbm !
520 :    
521 :     ELSE r> drop THEN
522 :     REPEAT ;
523 : jwilke 1.52
524 : pazsan 1.54 \ \ switched tdp for rom support 03jun97jaw
525 : jwilke 1.52
526 :     \ second value is here to store some maximal value for statistics
527 :     \ tempdp is also embedded here but has nothing to do with rom support
528 :     \ (needs switched dp)
529 :    
530 :     variable tempdp 0 , \ temporary dp for resolving
531 :     variable tempdp-save
532 :    
533 :     0 [IF]
534 :     variable romdp 0 , \ Dictionary-Pointer for ramarea
535 :     variable ramdp 0 , \ Dictionary-Pointer for romarea
536 :    
537 :     \
538 :     variable sramdp \ start of ram-area for forth
539 :     variable sromdp \ start of rom-area for forth
540 :    
541 :     [THEN]
542 :    
543 :    
544 :     0 value tdp
545 :     variable fixed \ flag: true: no automatic switching
546 :     \ false: switching is done automatically
547 :    
548 :     \ Switch-Policy:
549 :     \
550 :     \ a header is always compiled into rom
551 :     \ after a created word (create and variable) compilation goes to ram
552 :     \
553 :     \ Be careful: If you want to make the data behind create into rom
554 :     \ you have to put >rom before create!
555 :    
556 :     variable constflag constflag off
557 :    
558 : jwilke 1.67 : activate ( region -- )
559 :     \G next code goes to this region
560 :     >rdp to tdp ;
561 :    
562 : jwilke 1.52 : (switchram)
563 : jwilke 1.53 fixed @ ?EXIT s" rom" T $has? H 0= ?EXIT
564 : jwilke 1.67 ram-dictionary activate ;
565 : jwilke 1.52
566 :     : switchram
567 :     constflag @
568 :     IF constflag off ELSE (switchram) THEN ;
569 :    
570 :     : switchrom
571 : jwilke 1.67 fixed @ ?EXIT rom-dictionary activate ;
572 : jwilke 1.52
573 :     : >tempdp ( addr -- )
574 :     tdp tempdp-save ! tempdp to tdp tdp ! ;
575 :     : tempdp> ( -- )
576 :     tempdp-save @ to tdp ;
577 :    
578 :     : >ram fixed off (switchram) fixed on ;
579 :     : >rom fixed off switchrom fixed on ;
580 :     : >auto fixed off switchrom ;
581 :    
582 :    
583 :    
584 :     \ : romstart dup sromdp ! romdp ! ;
585 :     \ : ramstart dup sramdp ! ramdp ! ;
586 :    
587 : jwilke 1.67 \ default compilation goes to rom
588 : jwilke 1.52 \ when romable support is off, only the rom switch is used (!!)
589 :     >auto
590 :    
591 : anton 1.48 : there tdp @ ;
592 :    
593 : jwilke 1.52 >TARGET
594 : anton 1.48
595 : jwilke 1.52 \ \ Target Memory Handling
596 : anton 1.1
597 :     \ Byte ordering and cell size 06oct92py
598 :    
599 : pazsan 1.19 : cell+ tcell + ;
600 :     : cells tcell<< lshift ;
601 : anton 1.1 : chars ;
602 : anton 1.48 : char+ 1 + ;
603 : pazsan 1.19 : floats tfloat * ;
604 : anton 1.6
605 : anton 1.1 >CROSS
606 : pazsan 1.19 : cell/ tcell<< rshift ;
607 : anton 1.1 >TARGET
608 :     20 CONSTANT bl
609 : jwilke 1.52 \ TNIL Constant NIL
610 : anton 1.1
611 :     >CROSS
612 :    
613 : pazsan 1.20 bigendian
614 :     [IF]
615 : jwilke 1.52 : S! ( n addr -- ) >r s>d r> tcell bounds swap 1-
616 : pazsan 1.20 DO maxbyte ud/mod rot I c! -1 +LOOP 2drop ;
617 : jwilke 1.52 : S@ ( addr -- n ) >r 0 0 r> tcell bounds
618 : pazsan 1.20 DO maxbyte * swap maxbyte um* rot + swap I c@ + swap LOOP d>s ;
619 : pazsan 1.57 : Sc! ( n addr -- ) >r s>d r> tchar bounds swap 1-
620 :     DO maxbyte ud/mod rot I c! -1 +LOOP 2drop ;
621 :     : Sc@ ( addr -- n ) >r 0 0 r> tchar bounds
622 :     DO maxbyte * swap maxbyte um* rot + swap I c@ + swap LOOP d>s ;
623 : pazsan 1.19 [ELSE]
624 : jwilke 1.52 : S! ( n addr -- ) >r s>d r> tcell bounds
625 : pazsan 1.20 DO maxbyte ud/mod rot I c! LOOP 2drop ;
626 : jwilke 1.52 : S@ ( addr -- n ) >r 0 0 r> tcell bounds swap 1-
627 : pazsan 1.20 DO maxbyte * swap maxbyte um* rot + swap I c@ + swap -1 +LOOP d>s ;
628 : pazsan 1.57 : Sc! ( n addr -- ) >r s>d r> tchar bounds
629 :     DO maxbyte ud/mod rot I c! LOOP 2drop ;
630 :     : Sc@ ( addr -- n ) >r 0 0 r> tchar bounds swap 1-
631 :     DO maxbyte * swap maxbyte um* rot + swap I c@ + swap -1 +LOOP d>s ;
632 : anton 1.1 [THEN]
633 :    
634 : jwilke 1.67 : taddr>region ( taddr -- region | 0 )
635 :     \G finds for a target-address the correct region
636 :     \G returns 0 if taddr is not in range of a target memory region
637 :     region-link
638 :     BEGIN @ dup
639 :     WHILE dup >r
640 :     0 >rlink - >r
641 :     r@ >rlen @
642 :     IF dup r@ borders within
643 :     IF r> r> drop nip EXIT THEN
644 :     THEN
645 :     r> drop
646 :     r>
647 :     REPEAT
648 :     2drop 0 ;
649 :    
650 :     : (>regionimage) ( taddr -- 'taddr )
651 :     dup
652 :     \ find region we want to address
653 :     taddr>region dup 0= ABORT" Address out of range!"
654 :     >r
655 :     \ calculate offset in region
656 :     r@ >rstart @ -
657 :     \ add regions real address in our memory
658 :     r> >rmem @ + ;
659 :    
660 : anton 1.1 \ Bit string manipulation 06oct92py
661 :     \ 9may93jaw
662 :     CREATE Bittable 80 c, 40 c, 20 c, 10 c, 8 c, 4 c, 2 c, 1 c,
663 :     : bits ( n -- n ) chars Bittable + c@ ;
664 :    
665 :     : >bit ( addr n -- c-addr mask ) 8 /mod rot + swap bits ;
666 :     : +bit ( addr n -- ) >bit over c@ or swap c! ;
667 : pazsan 1.4 : -bit ( addr n -- ) >bit invert over c@ and swap c! ;
668 : jwilke 1.67
669 :     : (relon) ( taddr -- ) bit$ @ swap cell/ +bit ;
670 :     : (reloff) ( taddr -- ) bit$ @ swap cell/ -bit ;
671 :    
672 :     : (>image) ( taddr -- absaddr ) image @ + ;
673 :    
674 :     DEFER >image
675 :     DEFER relon
676 :     DEFER reloff
677 :     DEFER correcter
678 :    
679 :     T has? relocate H
680 :     [IF]
681 :     ' (relon) IS relon
682 :     ' (reloff) IS reloff
683 :     ' (>image) IS >image
684 :     [ELSE]
685 :     ' drop IS relon
686 :     ' drop IS reloff
687 :     ' (correcter) IS >image
688 :     [THEN]
689 : anton 1.1
690 :     \ Target memory access 06oct92py
691 :    
692 :     : align+ ( taddr -- rest )
693 : anton 1.48 tcell tuck 1- and - [ tcell 1- ] Literal and ;
694 : anton 1.22 : cfalign+ ( taddr -- rest )
695 : pazsan 1.39 \ see kernel.fs:cfaligned
696 : pazsan 1.43 /maxalign tuck 1- and - [ /maxalign 1- ] Literal and ;
697 : anton 1.1
698 :     >TARGET
699 :     : aligned ( taddr -- ta-addr ) dup align+ + ;
700 :     \ assumes cell alignment granularity (as GNU C)
701 :    
702 : anton 1.22 : cfaligned ( taddr1 -- taddr2 )
703 : pazsan 1.39 \ see kernel.fs
704 : anton 1.22 dup cfalign+ + ;
705 :    
706 : jwilke 1.52 : @ ( taddr -- w ) >image S@ ;
707 :     : ! ( w taddr -- ) >image S! ;
708 : pazsan 1.57 : c@ ( taddr -- char ) >image Sc@ ;
709 :     : c! ( char taddr -- ) >image Sc! ;
710 : anton 1.7 : 2@ ( taddr -- x1 x2 ) T dup cell+ @ swap @ H ;
711 :     : 2! ( x1 x2 taddr -- ) T swap over ! cell+ ! H ;
712 : anton 1.1
713 :     \ Target compilation primitives 06oct92py
714 :     \ included A! 16may93jaw
715 :    
716 :     : here ( -- there ) there ;
717 :     : allot ( n -- ) tdp +! ;
718 : jwilke 1.52 : , ( w -- ) T here H tcell T allot ! H T here drop H ;
719 : pazsan 1.57 : c, ( char -- ) T here tchar allot c! H ;
720 : jwilke 1.67 : align ( -- ) T here H align+ 0 ?DO bl T c, tchar H +LOOP ;
721 : anton 1.22 : cfalign ( -- )
722 : pazsan 1.59 T here H cfalign+ 0 ?DO bl T c, tchar H +LOOP ;
723 : anton 1.1
724 : jwilke 1.67 : >address dup 0>= IF tchar / THEN ; \ ?? jaw
725 : pazsan 1.59 : A! swap >address swap dup relon T ! H ;
726 :     : A, ( w -- ) >address T here H relon T , H ;
727 : anton 1.1
728 :     >CROSS
729 :    
730 : jwilke 1.52 : tcmove ( source dest len -- )
731 :     \G cmove in target memory
732 : pazsan 1.57 tchar * bounds
733 : jwilke 1.52 ?DO dup T c@ H I T c! H 1+
734 : pazsan 1.57 tchar +LOOP drop ;
735 : anton 1.1
736 : jwilke 1.67 \ \ Load Assembler
737 :    
738 : anton 1.1 >TARGET
739 : jwilke 1.52 H also Forth definitions \ ." asm: " order
740 : anton 1.1
741 : jwilke 1.52 : X also target bl word find
742 :     IF state @ IF compile,
743 :     ELSE execute THEN
744 :     ELSE previous ABORT" Cross: access method not supported!"
745 :     THEN
746 :     previous ; immediate
747 : anton 1.1
748 : jwilke 1.52 [IFDEF] asm-include asm-include [THEN] hex
749 : anton 1.1
750 : jwilke 1.52 previous
751 :     >CROSS H
752 : anton 1.1
753 : jwilke 1.52 \ \ -------------------- Compiler Plug Ins 01aug97jaw
754 : anton 1.1
755 : pazsan 1.54 \ Compiler States
756 :    
757 :     Variable comp-state
758 :     0 Constant interpreting
759 :     1 Constant compiling
760 :     2 Constant resolving
761 :     3 Constant assembling
762 :    
763 : jwilke 1.52 Defer lit, ( n -- )
764 :     Defer alit, ( n -- )
765 : pazsan 1.54
766 :     Defer branch, ( target-addr -- ) \ compiles a branch
767 :     Defer ?branch, ( target-addr -- ) \ compiles a ?branch
768 :     Defer branchmark, ( -- branch-addr ) \ reserves room for a branch
769 :     Defer ?branchmark, ( -- branch-addr ) \ reserves room for a ?branch
770 :     Defer ?domark, ( -- branch-addr ) \ reserves room for a ?do branch
771 :     Defer branchto, ( -- ) \ actual program position is target of a branch (do e.g. alignment)
772 :     Defer branchtoresolve, ( branch-addr -- ) \ resolves a forward reference from branchmark
773 :     Defer branchfrom, ( -- ) \ ?!
774 :     Defer branchtomark, ( -- target-addr ) \ marks a branch destination
775 :    
776 : jwilke 1.52 Defer colon, ( tcfa -- ) \ compiles call to tcfa at current position
777 : pazsan 1.54 Defer colonmark, ( -- addr ) \ marks a colon call
778 : jwilke 1.52 Defer colon-resolve ( tcfa addr -- )
779 : pazsan 1.54
780 : jwilke 1.52 Defer addr-resolve ( target-addr addr -- )
781 : pazsan 1.54 Defer doer-resolve ( ghost res-pnt target-addr addr -- ghost res-pnt )
782 :    
783 :     Defer do, ( -- do-token )
784 :     Defer ?do, ( -- ?do-token )
785 :     Defer for, ( -- for-token )
786 :     Defer loop, ( do-token / ?do-token -- )
787 :     Defer +loop, ( do-token / ?do-token -- )
788 :     Defer next, ( for-token )
789 : anton 1.1
790 : jwilke 1.52 [IFUNDEF] ca>native
791 :     defer ca>native
792 :     [THEN]
793 : anton 1.1
794 : jwilke 1.52 >TARGET
795 :     DEFER >body \ we need the system >body
796 :     \ and the target >body
797 :     >CROSS
798 :     T 2 cells H VALUE xt>body
799 : pazsan 1.54 DEFER doprim, \ compiles start of a primitive
800 :     DEFER docol, \ compiles start of a colon definition
801 : jwilke 1.52 DEFER doer,
802 :     DEFER fini, \ compiles end of definition ;s
803 :     DEFER doeshandler,
804 :     DEFER dodoes,
805 :    
806 :     DEFER ]comp \ starts compilation
807 :     DEFER comp[ \ ends compilation
808 :    
809 : pazsan 1.54 : (cc) T a, H ; ' (cc) IS colon,
810 :    
811 :     : (cr) >tempdp ]comp colon, comp[ tempdp> ; ' (cr) IS colon-resolve
812 :     : (ar) T ! H ; ' (ar) IS addr-resolve
813 :     : (dr) ( ghost res-pnt target-addr addr )
814 :     >tempdp drop over
815 :     dup >magic @ <do:> =
816 :     IF doer,
817 :     ELSE dodoes,
818 :     THEN
819 :     tempdp> ; ' (dr) IS doer-resolve
820 :    
821 :     : (cm) ( -- addr )
822 :     T here align H
823 :     -1 colon, ; ' (cm) IS colonmark,
824 : anton 1.1
825 : jwilke 1.52 >TARGET
826 :     : compile, colon, ;
827 :     >CROSS
828 : anton 1.1
829 : jwilke 1.53 \ file loading
830 :    
831 : pazsan 1.54 : >fl-id 1 cells + ;
832 :     : >fl-name 2 cells + ;
833 :    
834 : jwilke 1.53 Variable filelist 0 filelist !
835 : jwilke 1.67 Create NoFile ," #load-file#"
836 : pazsan 1.54 0 Value filemem
837 : jwilke 1.67 : loadfile FileMem ?dup IF >fl-name ELSE NoFile THEN ;
838 : jwilke 1.53
839 : pazsan 1.54 1 [IF] \ !! JAW WIP
840 : anton 1.1
841 : jwilke 1.53 : add-included-file ( adr len -- )
842 : pazsan 1.54 dup char+ >fl-name allocate throw >r
843 :     r@ >fl-name place
844 : jwilke 1.53 filelist @ r@ !
845 : pazsan 1.54 r> dup filelist ! to FileMem
846 :     ;
847 : jwilke 1.53
848 :     : included? ( c-addr u -- f )
849 :     filelist
850 :     BEGIN @ dup
851 :     WHILE >r r@ 1 cells + count compare 0=
852 :     IF rdrop 2drop true EXIT THEN
853 :     r>
854 :     REPEAT
855 :     2drop drop false ;
856 :    
857 :     : included
858 : pazsan 1.54 \ cr ." Including: " 2dup type ." ..."
859 :     FileMem >r
860 :     2dup add-included-file included
861 :     r> to FileMem ;
862 : jwilke 1.53
863 :     : include bl word count included ;
864 :    
865 :     : require bl word count included ;
866 :    
867 :     [THEN]
868 : anton 1.1
869 : jwilke 1.52 \ resolve structure
870 : anton 1.1
871 : jwilke 1.52 : >next ; \ link to next field
872 : pazsan 1.54 : >tag cell+ ; \ indecates type of reference: 0: call, 1: address, 2: doer
873 : jwilke 1.53 : >taddr cell+ cell+ ;
874 : jwilke 1.52 : >ghost 3 cells + ;
875 : jwilke 1.53 : >file 4 cells + ;
876 :     : >line 5 cells + ;
877 : anton 1.48
878 : pazsan 1.54 : (refered) ( ghost addr tag -- )
879 :     \G creates a reference to ghost at address taddr
880 :     rot >r here r@ >link @ , r> >link !
881 :     ( taddr tag ) ,
882 :     ( taddr ) ,
883 :     last-header-ghost @ ,
884 :     loadfile ,
885 :     sourceline# ,
886 :     ;
887 :    
888 : jwilke 1.52 : refered ( ghost tag -- )
889 : jwilke 1.53 \G creates a resolve structure
890 : pazsan 1.54 T here aligned H swap (refered)
891 :     ;
892 :    
893 :     : killref ( addr ghost -- )
894 :     \G kills a forward reference to ghost at position addr
895 :     \G this is used to eleminate a :dovar refence after making a DOES>
896 :     dup >magic @ <fwd> <> IF 2drop EXIT THEN
897 :     swap >r >link
898 :     BEGIN dup @ dup ( addr last this )
899 :     WHILE dup >taddr @ r@ =
900 :     IF @ over !
901 :     ELSE nip THEN
902 :     REPEAT rdrop 2drop
903 : jwilke 1.53 ;
904 : anton 1.48
905 : jwilke 1.52 Defer resolve-warning
906 : anton 1.1
907 : jwilke 1.52 : reswarn-test ( ghost res-struct -- ghost res-struct )
908 :     over cr ." Resolving " >ghostname type dup ." in " >ghost @ >ghostname type ;
909 : anton 1.1
910 : jwilke 1.52 : reswarn-forward ( ghost res-struct -- ghost res-struct )
911 :     over warnhead >ghostname type dup ." is referenced in "
912 :     >ghost @ >ghostname type ;
913 : anton 1.1
914 : jwilke 1.52 \ ' reswarn-test IS resolve-warning
915 :    
916 : anton 1.1 \ resolve 14oct92py
917 :    
918 : pazsan 1.54 : resolve-loop ( ghost resolve-list tcfa -- )
919 :     >r
920 :     BEGIN dup WHILE
921 :     \ dup >tag @ 2 = IF reswarn-forward THEN
922 :     resolve-warning
923 :     r@ over >taddr @
924 :     2 pick >tag @
925 :     CASE 0 OF colon-resolve ENDOF
926 :     1 OF addr-resolve ENDOF
927 :     2 OF doer-resolve ENDOF
928 :     ENDCASE
929 :     @ \ next list element
930 :     REPEAT 2drop rdrop
931 :     ;
932 : jwilke 1.52
933 :     \ : resolve-loop ( ghost tcfa -- ghost tcfa )
934 :     \ >r dup >link @
935 :     \ BEGIN dup WHILE dup T @ H r@ rot T ! H REPEAT drop r> ;
936 : anton 1.1
937 :     \ exists 9may93jaw
938 :    
939 : jwilke 1.52 Variable TWarnings
940 :     TWarnings on
941 :     Variable Exists-Warnings
942 :     Exists-Warnings on
943 :    
944 : anton 1.1 : exists ( ghost tcfa -- )
945 :     over GhostNames
946 :     BEGIN @ dup
947 :     WHILE 2dup cell+ @ =
948 :     UNTIL
949 : jwilke 1.52 2 cells + count
950 :     TWarnings @ Exists-Warnings @ and
951 :     IF warnhead type ." exists"
952 :     ELSE 2drop THEN
953 :     drop swap >link !
954 : pazsan 1.24 ELSE true abort" CROSS: Ghostnames inconsistent "
955 : anton 1.1 THEN ;
956 :    
957 :     : resolve ( ghost tcfa -- )
958 : pazsan 1.54 \G resolve referencies to ghost with tcfa
959 :     \ is ghost resolved?, second resolve means another definition with the
960 :     \ same name
961 :     over forward? 0= IF exists EXIT THEN
962 :     \ get linked-list
963 :     swap >r r@ >link @ swap \ ( list tcfa R: ghost )
964 :     \ mark ghost as resolved
965 :     dup r@ >link ! <res> r@ >magic !
966 :     \ loop through forward referencies
967 :     r> -rot
968 :     comp-state @ >r Resolving comp-state !
969 :     resolve-loop
970 :     r> comp-state !
971 :    
972 :     ['] noop IS resolve-warning
973 : jwilke 1.52 ;
974 : anton 1.1
975 :     \ gexecute ghost, 01nov92py
976 :    
977 : jwilke 1.52 : is-forward ( ghost -- )
978 : pazsan 1.54 colonmark, 0 (refered) ; \ compile space for call
979 : jwilke 1.52
980 :     : is-resolved ( ghost -- )
981 :     >link @ colon, ; \ compile-call
982 :    
983 :     : gexecute ( ghost -- )
984 :     dup @ <fwd> = IF is-forward ELSE is-resolved THEN ;
985 :    
986 :     : addr, ( ghost -- )
987 :     dup @ <fwd> = IF 1 refered 0 T a, H ELSE >link @ T a, H THEN ;
988 :    
989 :     \ !! : ghost, ghost gexecute ;
990 : anton 1.1
991 :     \ .unresolved 11may93jaw
992 :    
993 :     variable ResolveFlag
994 :    
995 :     \ ?touched 11may93jaw
996 :    
997 : jwilke 1.52 : ?touched ( ghost -- flag ) dup forward? swap >link @
998 : anton 1.1 0 <> and ;
999 :    
1000 : jwilke 1.53 : .forwarddefs ( ghost -- )
1001 :     ." appeared in:"
1002 :     >link
1003 :     BEGIN @ dup
1004 :     WHILE cr 5 spaces
1005 :     dup >ghost @ >ghostname type
1006 :     ." file " dup >file @ ?dup IF count type ELSE ." CON" THEN
1007 :     ." line " dup >line @ .dec
1008 :     REPEAT
1009 :     drop ;
1010 :    
1011 : anton 1.1 : ?resolved ( ghostname -- )
1012 :     dup cell+ @ ?touched
1013 : jwilke 1.53 IF dup
1014 :     cell+ cell+ count cr type ResolveFlag on
1015 :     cell+ @ .forwarddefs
1016 :     ELSE drop
1017 :     THEN ;
1018 : anton 1.1
1019 :     >MINIMAL
1020 :     : .unresolved ( -- )
1021 :     ResolveFlag off cr ." Unresolved: "
1022 :     Ghostnames
1023 :     BEGIN @ dup
1024 :     WHILE dup ?resolved
1025 : anton 1.10 REPEAT drop ResolveFlag @
1026 :     IF
1027 : anton 1.48 -1 abort" Unresolved words!"
1028 : anton 1.10 ELSE
1029 :     ." Nothing!"
1030 :     THEN
1031 :     cr ;
1032 : anton 1.1
1033 : jwilke 1.52 : .stats
1034 :     base @ >r decimal
1035 :     cr ." named Headers: " headers-named @ .
1036 :     r> base ! ;
1037 :    
1038 : anton 1.1 >CROSS
1039 :     \ Header states 12dec92py
1040 :    
1041 :     : flag! ( 8b -- ) tlast @ dup >r T c@ xor r> c! H ;
1042 :    
1043 :     VARIABLE ^imm
1044 :    
1045 :     >TARGET
1046 : anton 1.36 : immediate 40 flag!
1047 : pazsan 1.18 ^imm @ @ dup <imm> = IF drop EXIT THEN
1048 : anton 1.1 <res> <> ABORT" CROSS: Cannot immediate a unresolved word"
1049 :     <imm> ^imm @ ! ;
1050 : anton 1.36 : restrict 20 flag! ;
1051 : jwilke 1.52
1052 : pazsan 1.54 : isdoer
1053 :     \G define a forth word as doer, this makes obviously only sence on
1054 :     \G forth processors such as the PSC1000
1055 :     <do:> last-header-ghost @ >magic ! ;
1056 : anton 1.1 >CROSS
1057 :    
1058 :     \ ALIAS2 ansforth conform alias 9may93jaw
1059 :    
1060 :     : ALIAS2 create here 0 , DOES> @ execute ;
1061 :     \ usage:
1062 : pazsan 1.18 \ ' <name> alias2 bla !
1063 : anton 1.1
1064 :     \ Target Header Creation 01nov92py
1065 :    
1066 : jwilke 1.52 >TARGET
1067 : anton 1.1 : string, ( addr count -- )
1068 : pazsan 1.28 dup T c, H bounds ?DO I c@ T c, H LOOP ;
1069 : jwilke 1.52 : name, ( "name" -- ) bl word count T string, cfalign H ;
1070 : anton 1.1 : view, ( -- ) ( dummy ) ;
1071 : jwilke 1.52 >CROSS
1072 : anton 1.1
1073 : pazsan 1.25 \ Target Document Creation (goes to crossdoc.fd) 05jul95py
1074 :    
1075 : pazsan 1.55 s" ./doc/crossdoc.fd" r/w create-file throw value doc-file-id
1076 : pazsan 1.25 \ contains the file-id of the documentation file
1077 :    
1078 : pazsan 1.40 : T-\G ( -- )
1079 : pazsan 1.25 source >in @ /string doc-file-id write-line throw
1080 : pazsan 1.40 postpone \ ;
1081 : pazsan 1.25
1082 : pazsan 1.39 Variable to-doc to-doc on
1083 : pazsan 1.25
1084 :     : cross-doc-entry ( -- )
1085 :     to-doc @ tlast @ 0<> and \ not an anonymous (i.e. noname) header
1086 :     IF
1087 :     s" " doc-file-id write-line throw
1088 :     s" make-doc " doc-file-id write-file throw
1089 :     tlast @ >image count $1F and doc-file-id write-file throw
1090 :     >in @
1091 :     [char] ( parse 2drop
1092 :     [char] ) parse doc-file-id write-file throw
1093 :     s" )" doc-file-id write-file throw
1094 :     [char] \ parse 2drop
1095 : pazsan 1.40 T-\G
1096 : pazsan 1.25 >in !
1097 : pazsan 1.39 THEN ;
1098 : pazsan 1.25
1099 : pazsan 1.28 \ Target TAGS creation
1100 :    
1101 : pazsan 1.39 s" kernel.TAGS" r/w create-file throw value tag-file-id
1102 : pazsan 1.28 \ contains the file-id of the tags file
1103 :    
1104 :     Create tag-beg 2 c, 7F c, bl c,
1105 :     Create tag-end 2 c, bl c, 01 c,
1106 :     Create tag-bof 1 c, 0C c,
1107 :    
1108 :     2variable last-loadfilename 0 0 last-loadfilename 2!
1109 :    
1110 :     : put-load-file-name ( -- )
1111 :     loadfilename 2@ last-loadfilename 2@ d<>
1112 :     IF
1113 :     tag-bof count tag-file-id write-line throw
1114 : anton 1.31 sourcefilename 2dup
1115 : pazsan 1.28 tag-file-id write-file throw
1116 :     last-loadfilename 2!
1117 :     s" ,0" tag-file-id write-line throw
1118 :     THEN ;
1119 :    
1120 :     : cross-tag-entry ( -- )
1121 :     tlast @ 0<> \ not an anonymous (i.e. noname) header
1122 :     IF
1123 :     put-load-file-name
1124 :     source >in @ min tag-file-id write-file throw
1125 :     tag-beg count tag-file-id write-file throw
1126 :     tlast @ >image count $1F and tag-file-id write-file throw
1127 :     tag-end count tag-file-id write-file throw
1128 : anton 1.31 base @ decimal sourceline# 0 <# #s #> tag-file-id write-file throw
1129 : pazsan 1.28 \ >in @ 0 <# #s [char] , hold #> tag-file-id write-line throw
1130 :     s" ,0" tag-file-id write-line throw
1131 :     base !
1132 :     THEN ;
1133 :    
1134 : pazsan 1.43 \ Check for words
1135 :    
1136 :     Defer skip? ' false IS skip?
1137 :    
1138 :     : defined? ( -- flag ) \ name
1139 : jwilke 1.52 ghost forward? 0= ;
1140 : pazsan 1.43
1141 :     : needed? ( -- flag ) \ name
1142 : anton 1.48 \G returns a false flag when
1143 :     \G a word is not defined
1144 :     \G a forward reference exists
1145 :     \G so the definition is not skipped!
1146 :     bl word gfind
1147 : jwilke 1.52 IF dup forward?
1148 : anton 1.48 nip
1149 :     0=
1150 :     ELSE drop true THEN ;
1151 : pazsan 1.43
1152 : pazsan 1.44 : doer? ( -- flag ) \ name
1153 :     ghost >magic @ <do:> = ;
1154 :    
1155 : pazsan 1.43 : skip-defs ( -- )
1156 :     BEGIN refill WHILE source -trailing nip 0= UNTIL THEN ;
1157 :    
1158 : pazsan 1.28 \ Target header creation
1159 :    
1160 : pazsan 1.54 Variable CreateFlag
1161 :     CreateFlag off
1162 : jwilke 1.52
1163 : pazsan 1.54 Variable NoHeaderFlag
1164 :     NoHeaderFlag off
1165 : anton 1.1
1166 : pazsan 1.54 : 0.r ( n1 n2 -- )
1167 :     base @ >r hex
1168 :     0 swap <# 0 ?DO # LOOP #> type
1169 :     r> base ! ;
1170 : jwilke 1.52 : .sym
1171 :     bounds
1172 :     DO I c@ dup
1173 :     CASE '/ OF drop ." \/" ENDOF
1174 :     '\ OF drop ." \\" ENDOF
1175 :     dup OF emit ENDOF
1176 :     ENDCASE
1177 : pazsan 1.54 LOOP ;
1178 : jwilke 1.52
1179 : pazsan 1.43 : (Theader ( "name" -- ghost )
1180 : pazsan 1.54 \ >in @ bl word count type 2 spaces >in !
1181 :     \ wordheaders will always be compiled to rom
1182 :     switchrom
1183 :     \ build header in target
1184 :     NoHeaderFlag @
1185 :     IF NoHeaderFlag off
1186 :     ELSE
1187 :     T align H view,
1188 : pazsan 1.59 tlast @ dup 0> IF T 1 cells - H THEN T A, H there tlast !
1189 : pazsan 1.54 1 headers-named +! \ Statistic
1190 :     >in @ T name, H >in !
1191 :     THEN
1192 :     T cfalign here H tlastcfa !
1193 :     \ Symbol table
1194 :     \ >in @ cr ." sym:s/CFA=" there 4 0.r ." /" bl word count .sym ." /g" cr >in !
1195 :     CreateFlag @
1196 :     IF
1197 :     >in @ alias2 swap >in ! \ create alias in target
1198 :     >in @ ghost swap >in !
1199 :     swap also ghosts ' previous swap ! \ tick ghost and store in alias
1200 :     CreateFlag off
1201 :     ELSE ghost
1202 :     THEN
1203 :     dup Last-Header-Ghost !
1204 :     dup >magic ^imm ! \ a pointer for immediate
1205 :     Already @
1206 :     IF dup >end tdoes !
1207 :     ELSE 0 tdoes !
1208 :     THEN
1209 :     80 flag!
1210 :     cross-doc-entry cross-tag-entry ;
1211 : anton 1.1
1212 :     VARIABLE ;Resolve 1 cells allot
1213 : jwilke 1.52 \ this is the resolver information from ":"
1214 :     \ resolving is done by ";"
1215 : anton 1.1
1216 : pazsan 1.11 : Theader ( "name" -- ghost )
1217 :     (THeader dup there resolve 0 ;Resolve ! ;
1218 : anton 1.1
1219 :     >TARGET
1220 :     : Alias ( cfa -- ) \ name
1221 : pazsan 1.43 >in @ skip? IF 2drop EXIT THEN >in !
1222 : jwilke 1.53 dup 0< s" prims" T $has? H 0= and
1223 : pazsan 1.43 IF
1224 : jwilke 1.53 .sourcepos ." needs prim: " >in @ bl word count type >in ! cr
1225 : pazsan 1.43 THEN
1226 :     (THeader over resolve T A, H 80 flag! ;
1227 : pazsan 1.42 : Alias: ( cfa -- ) \ name
1228 : pazsan 1.43 >in @ skip? IF 2drop EXIT THEN >in !
1229 : jwilke 1.53 dup 0< s" prims" T $has? H 0= and
1230 : pazsan 1.43 IF
1231 : jwilke 1.53 .sourcepos ." needs doer: " >in @ bl word count type >in ! cr
1232 : pazsan 1.43 THEN
1233 :     ghost tuck swap resolve <do:> swap >magic ! ;
1234 : pazsan 1.64
1235 :     Variable prim#
1236 :     : first-primitive ( n -- ) prim# ! ;
1237 :     : Primitive ( -- ) \ name
1238 :     prim# @ T Alias H -1 prim# +! ;
1239 : anton 1.1 >CROSS
1240 :    
1241 :     \ Conditionals and Comments 11may93jaw
1242 :    
1243 :     : ;Cond
1244 :     postpone ;
1245 :     swap ! ; immediate
1246 :    
1247 :     : Cond: ( -- ) \ name {code } ;
1248 :     atonce on
1249 :     ghost
1250 :     >exec
1251 :     :NONAME ;
1252 :    
1253 :     : restrict? ( -- )
1254 :     \ aborts on interprete state - ae
1255 :     state @ 0= ABORT" CROSS: Restricted" ;
1256 :    
1257 :     : Comment ( -- )
1258 :     >in @ atonce on ghost swap >in ! ' swap >exec ! ;
1259 :    
1260 :     Comment ( Comment \
1261 :    
1262 :     \ compile 10may93jaw
1263 :    
1264 :     : compile ( -- ) \ name
1265 :     restrict?
1266 : pazsan 1.13 bl word gfind dup 0= ABORT" CROSS: Can't compile "
1267 : anton 1.1 0> ( immediate? )
1268 :     IF >exec @ compile,
1269 :     ELSE postpone literal postpone gexecute THEN ;
1270 :     immediate
1271 :    
1272 : jwilke 1.52 : [G']
1273 :     \G ticks a ghost and returns its address
1274 :     bl word gfind 0= ABORT" CROSS: Ghost don't exists"
1275 :     state @
1276 :     IF postpone literal
1277 :     THEN ; immediate
1278 :    
1279 :     : ghost>cfa
1280 :     dup forward? ABORT" CROSS: forward " >link @ ;
1281 :    
1282 :     >TARGET
1283 :    
1284 :     : ' ( -- cfa )
1285 :     \ returns the target-cfa of a ghost
1286 :     bl word gfind 0= ABORT" CROSS: Ghost don't exists"
1287 :     ghost>cfa ;
1288 :    
1289 :     Cond: ['] T ' H alit, ;Cond
1290 :    
1291 :     >CROSS
1292 :    
1293 :     : [T']
1294 :     \ returns the target-cfa of a ghost, or compiles it as literal
1295 :     postpone [G'] state @ IF postpone ghost>cfa ELSE ghost>cfa THEN ; immediate
1296 : pazsan 1.42
1297 : jwilke 1.52 \ \ threading modell 13dec92py
1298 :     \ modularized 14jun97jaw
1299 :    
1300 :     : fillcfa ( usedcells -- )
1301 : pazsan 1.59 T cells H xt>body swap - 0 ?DO 0 T c, tchar H +LOOP ;
1302 : jwilke 1.52
1303 :     : (>body) ( cfa -- pfa ) xt>body + ; ' (>body) T IS >body H
1304 :    
1305 :     : (doer,) ( ghost -- ) ]comp gexecute comp[ 1 fillcfa ; ' (doer,) IS doer,
1306 :    
1307 :     : (docol,) ( -- ) [G'] :docol doer, ; ' (docol,) IS docol,
1308 :    
1309 :     : (doprim,) ( -- )
1310 :     there xt>body + ca>native T a, H 1 fillcfa ; ' (doprim,) IS doprim,
1311 :    
1312 :     : (doeshandler,) ( -- )
1313 :     T cfalign H compile :doesjump T 0 , H ; ' (doeshandler,) IS doeshandler,
1314 :    
1315 :     : (dodoes,) ( does-action-ghost -- )
1316 :     ]comp [G'] :dodoes gexecute comp[
1317 :     addr,
1318 :     T here H tcell - reloff 2 fillcfa ; ' (dodoes,) IS dodoes,
1319 :    
1320 :     : (lit,) ( n -- ) compile lit T , H ; ' (lit,) IS lit,
1321 :    
1322 : jwilke 1.67 \ if we dont produce relocatable code alit, defaults to lit, jaw
1323 :     has? relocate
1324 :     [IF]
1325 : pazsan 1.66 : (alit,) ( n -- ) compile lit T a, H ; ' (alit,) IS alit,
1326 : jwilke 1.67 [ELSE]
1327 :     : (alit,) ( n -- ) lit, ; ' (alit,) IS alit,
1328 :     [THEN]
1329 : jwilke 1.52
1330 :     : (fini,) compile ;s ; ' (fini,) IS fini,
1331 : pazsan 1.42
1332 : pazsan 1.43 [IFUNDEF] (code)
1333 :     Defer (code)
1334 :     Defer (end-code)
1335 :     [THEN]
1336 :    
1337 : anton 1.1 >TARGET
1338 : pazsan 1.43 : Code
1339 : jwilke 1.52 defempty?
1340 : anton 1.48 (THeader there resolve
1341 : jwilke 1.53 [ T e? prims H 0= [IF] T e? ITC H [ELSE] true [THEN] ] [IF]
1342 : jwilke 1.52 doprim,
1343 : anton 1.48 [THEN]
1344 :     depth (code) ;
1345 : pazsan 1.43
1346 :     : Code:
1347 : jwilke 1.52 defempty?
1348 : anton 1.48 ghost dup there ca>native resolve <do:> swap >magic !
1349 : pazsan 1.43 depth (code) ;
1350 :    
1351 :     : end-code
1352 : jwilke 1.52 (end-code)
1353 : pazsan 1.43 depth ?dup IF 1- <> ABORT" CROSS: Stack changed"
1354 :     ELSE true ABORT" CROSS: Stack empty" THEN
1355 : jwilke 1.52 ;
1356 : anton 1.14
1357 : pazsan 1.57 ( Cond ) : chars tchar * ; ( Cond )
1358 : anton 1.1
1359 :     >CROSS
1360 : jwilke 1.52
1361 : anton 1.1 \ tLiteral 12dec92py
1362 :    
1363 :     >TARGET
1364 : pazsan 1.40 Cond: \G T-\G ;Cond
1365 :    
1366 : anton 1.1 Cond: Literal ( n -- ) restrict? lit, ;Cond
1367 :     Cond: ALiteral ( n -- ) restrict? alit, ;Cond
1368 :    
1369 :     : Char ( "<char>" -- ) bl word char+ c@ ;
1370 :     Cond: [Char] ( "<char>" -- ) restrict? Char lit, ;Cond
1371 :    
1372 : pazsan 1.43 \ some special literals 27jan97jaw
1373 :    
1374 : jwilke 1.52 \ !! Known Bug: Special Literals and plug-ins work only correct
1375 :     \ on 16 and 32 Bit Targets and 32 Bit Hosts!
1376 :    
1377 : pazsan 1.43 Cond: MAXU
1378 : jwilke 1.52 restrict?
1379 :     tcell 1 cells u>
1380 :     IF compile lit tcell 0 ?DO FF T c, H LOOP
1381 :     ELSE $ffffffff lit, THEN
1382 :     ;Cond
1383 : pazsan 1.43
1384 :     Cond: MINI
1385 : jwilke 1.52 restrict?
1386 :     tcell 1 cells u>
1387 :     IF compile lit bigendian
1388 :     IF 80 T c, H tcell 1 ?DO 0 T c, H LOOP
1389 :     ELSE tcell 1 ?DO 0 T c, H LOOP 80 T c, H
1390 :     THEN
1391 :     ELSE tcell 2 = IF $8000 ELSE $80000000 THEN lit, THEN
1392 :     ;Cond
1393 : pazsan 1.43
1394 :     Cond: MAXI
1395 : jwilke 1.52 restrict?
1396 :     tcell 1 cells u>
1397 :     IF compile lit bigendian
1398 :     IF 7F T c, H tcell 1 ?DO FF T c, H LOOP
1399 :     ELSE tcell 1 ?DO FF T c, H LOOP 7F T c, H
1400 :     THEN
1401 :     ELSE tcell 2 = IF $7fff ELSE $7fffffff THEN lit, THEN
1402 : pazsan 1.43 ;Cond
1403 :    
1404 : anton 1.1 >CROSS
1405 :     \ Target compiling loop 12dec92py
1406 :     \ ">tib trick thrown out 10may93jaw
1407 :     \ number? defined at the top 11may93jaw
1408 :    
1409 :     \ compiled word might leave items on stack!
1410 :     : tcom ( in name -- )
1411 :     gfind ?dup IF 0> IF nip >exec @ execute
1412 :     ELSE nip gexecute THEN EXIT THEN
1413 :     number? dup IF 0> IF swap lit, THEN lit, drop
1414 :     ELSE 2drop >in !
1415 :     ghost gexecute THEN ;
1416 :    
1417 :     >TARGET
1418 :     \ : ; DOES> 13dec92py
1419 :     \ ] 9may93py/jaw
1420 :    
1421 :     : ] state on
1422 : pazsan 1.54 Compiling comp-state !
1423 : anton 1.1 BEGIN
1424 : pazsan 1.13 BEGIN >in @ bl word
1425 : anton 1.1 dup c@ 0= WHILE 2drop refill 0=
1426 :     ABORT" CROSS: End of file while target compiling"
1427 :     REPEAT
1428 :     tcom
1429 :     state @
1430 :     0=
1431 :     UNTIL ;
1432 :    
1433 :     \ by the way: defining a second interpreter (a compiler-)loop
1434 :     \ is not allowed if a system should be ans conform
1435 :    
1436 :     : : ( -- colon-sys ) \ Name
1437 : jwilke 1.52 defempty?
1438 :     constflag off \ don't let this flag work over colon defs
1439 :     \ just to go sure nothing unwanted happens
1440 : pazsan 1.43 >in @ skip? IF drop skip-defs EXIT THEN >in !
1441 : anton 1.1 (THeader ;Resolve ! there ;Resolve cell+ !
1442 : jwilke 1.52 docol, ]comp depth T ] H ;
1443 : anton 1.1
1444 : pazsan 1.37 : :noname ( -- colon-sys )
1445 : jwilke 1.52 T cfalign H there docol, 0 ;Resolve ! depth T ] H ;
1446 : pazsan 1.37
1447 : pazsan 1.2 Cond: EXIT ( -- ) restrict? compile ;S ;Cond
1448 : anton 1.6
1449 :     Cond: ?EXIT ( -- ) 1 abort" CROSS: using ?exit" ;Cond
1450 : pazsan 1.2
1451 : jwilke 1.52 >CROSS
1452 :     : LastXT ;Resolve @ 0= abort" CROSS: no definition for LastXT"
1453 :     ;Resolve cell+ @ ;
1454 :    
1455 :     >TARGET
1456 :    
1457 :     Cond: recurse ( -- ) Last-Ghost @ gexecute ;Cond
1458 :    
1459 : anton 1.1 Cond: ; ( -- ) restrict?
1460 :     depth ?dup IF 1- <> ABORT" CROSS: Stack changed"
1461 :     ELSE true ABORT" CROSS: Stack empty" THEN
1462 : jwilke 1.52 fini,
1463 :     comp[
1464 :     state off
1465 : anton 1.1 ;Resolve @
1466 :     IF ;Resolve @ ;Resolve cell+ @ resolve THEN
1467 : pazsan 1.54 Interpreting comp-state !
1468 : anton 1.1 ;Cond
1469 : pazsan 1.54 Cond: [ restrict? state off Interpreting comp-state ! ;Cond
1470 : anton 1.1
1471 :     >CROSS
1472 : pazsan 1.54
1473 :     Create GhostDummy ghostheader
1474 :     <res> GhostDummy >magic !
1475 :    
1476 : jwilke 1.52 : !does ( does-action -- )
1477 :     \ !! zusammenziehen und dodoes, machen!
1478 : pazsan 1.54 tlastcfa @ [G'] :dovar killref
1479 :     \ tlastcfa @ dup there >r tdp ! compile :dodoes r> tdp ! T cell+ ! H ;
1480 : jwilke 1.52 \ !! geht so nicht, da dodoes, ghost will!
1481 : pazsan 1.54 GhostDummy >link ! GhostDummy
1482 :     tlastcfa @ >tempdp dodoes, tempdp> ;
1483 : anton 1.1
1484 :     >TARGET
1485 :     Cond: DOES> restrict?
1486 : jwilke 1.52 compile (does>) doeshandler,
1487 :     \ resolve words made by builders
1488 :     tdoes @ ?dup IF @ T here H resolve THEN
1489 : anton 1.1 ;Cond
1490 : jwilke 1.52 : DOES> switchrom doeshandler, T here H !does depth T ] H ;
1491 : anton 1.1
1492 :     >CROSS
1493 :     \ Creation 01nov92py
1494 :    
1495 :     \ Builder 11may93jaw
1496 :    
1497 : jwilke 1.52 : Builder ( Create-xt do:-xt "name" -- )
1498 :     \ builds up a builder in current vocabulary
1499 :     \ create-xt is executed when word is interpreted
1500 :     \ do:-xt is executet when the created word from builder is executed
1501 :     \ for do:-xt an additional entry after the normal ghost-enrys is used
1502 :    
1503 : anton 1.1 >in @ alias2 swap dup >in ! >r >r
1504 : pazsan 1.54 Make-Ghost
1505 :     rot swap >exec dup @ ['] NoExec <>
1506 :     IF 2drop ELSE ! THEN
1507 :     ,
1508 : anton 1.1 r> r> >in !
1509 : pazsan 1.11 also ghosts ' previous swap ! ;
1510 :     \ DOES> dup >exec @ execute ;
1511 : anton 1.1
1512 : jwilke 1.52 : gdoes, ( ghost -- )
1513 :     \ makes the codefield for a word that is built
1514 :     >end @ dup forward? 0=
1515 :     IF
1516 : pazsan 1.42 dup >magic @ <do:> =
1517 : pazsan 1.54 IF doer,
1518 :     ELSE dodoes,
1519 :     THEN
1520 :     EXIT
1521 : jwilke 1.52 THEN
1522 :     \ compile :dodoes gexecute
1523 :     \ T here H tcell - reloff
1524 : pazsan 1.54 2 refered
1525 :     0 fillcfa
1526 :     ;
1527 : anton 1.1
1528 : jwilke 1.52 : TCreate ( <name> -- )
1529 :     executed-ghost @
1530 : anton 1.1 CreateFlag on
1531 : jwilke 1.52 create-forward-warn
1532 :     IF ['] reswarn-forward IS resolve-warning THEN
1533 : pazsan 1.11 Theader >r dup gdoes,
1534 : jwilke 1.52 \ stores execution symantic in the built word
1535 :     >end @ >exec @ r> >exec ! ;
1536 :    
1537 :     : RTCreate ( <name> -- )
1538 :     \ creates a new word with code-field in ram
1539 :     executed-ghost @
1540 :     CreateFlag on
1541 :     create-forward-warn
1542 :     IF ['] reswarn-forward IS resolve-warning THEN
1543 :     \ make Alias
1544 :     (THeader there 0 T a, H 80 flag! ( S executed-ghost new-ghost )
1545 :     \ store poiter to code-field
1546 :     switchram T cfalign H
1547 :     there swap T ! H
1548 :     there tlastcfa !
1549 :     dup there resolve 0 ;Resolve !
1550 :     >r dup gdoes,
1551 : pazsan 1.11 >end @ >exec @ r> >exec ! ;
1552 : anton 1.1
1553 :     : Build: ( -- [xt] [colon-sys] )
1554 : jwilke 1.52 :noname postpone TCreate ;
1555 :    
1556 :     : BuildSmart: ( -- [xt] [colon-sys] )
1557 :     :noname
1558 : jwilke 1.53 [ T has? rom H [IF] ]
1559 : jwilke 1.52 postpone RTCreate
1560 :     [ [ELSE] ]
1561 :     postpone TCreate
1562 :     [ [THEN] ] ;
1563 : anton 1.1
1564 :     : gdoes> ( ghost -- addr flag )
1565 : jwilke 1.52 executed-ghost @
1566 : anton 1.1 state @ IF gexecute true EXIT THEN
1567 : jwilke 1.52 >link @ T >body H false ;
1568 : anton 1.1
1569 :     \ DO: ;DO 11may93jaw
1570 :     \ changed to ?EXIT 10may93jaw
1571 :    
1572 :     : DO: ( -- addr [xt] [colon-sys] )
1573 :     here ghostheader
1574 : pazsan 1.11 :noname postpone gdoes> postpone ?EXIT ;
1575 : anton 1.1
1576 : pazsan 1.42 : by: ( -- addr [xt] [colon-sys] ) \ name
1577 :     ghost
1578 :     :noname postpone gdoes> postpone ?EXIT ;
1579 :    
1580 : jwilke 1.52 : ;DO ( addr [xt] [colon-sys] -- addr )
1581 : anton 1.1 postpone ; ( S addr xt )
1582 :     over >exec ! ; immediate
1583 :    
1584 :     : by ( -- addr ) \ Name
1585 :     ghost >end @ ;
1586 :    
1587 :     >TARGET
1588 :     \ Variables and Constants 05dec92py
1589 :    
1590 : jwilke 1.52 Build: ( n -- ) ;
1591 :     by: :docon ( ghost -- n ) T @ H ;DO
1592 :     Builder (Constant)
1593 :    
1594 :     Build: ( n -- ) T , H ;
1595 :     by (Constant)
1596 :     Builder Constant
1597 :    
1598 :     Build: ( n -- ) T A, H ;
1599 :     by (Constant)
1600 :     Builder AConstant
1601 :    
1602 :     Build: ( d -- ) T , , H ;
1603 :     DO: ( ghost -- d ) T dup cell+ @ swap @ H ;DO
1604 :     Builder 2Constant
1605 :    
1606 :     BuildSmart: ;
1607 : pazsan 1.42 by: :dovar ( ghost -- addr ) ;DO
1608 : anton 1.1 Builder Create
1609 :    
1610 : jwilke 1.53 T has? rom H [IF]
1611 : pazsan 1.54 Build: ( -- ) T here 0 , H switchram T align here swap ! 0 , H ( switchrom ) ;
1612 : jwilke 1.52 by (Constant)
1613 :     Builder Variable
1614 :     [ELSE]
1615 : anton 1.1 Build: T 0 , H ;
1616 :     by Create
1617 :     Builder Variable
1618 : jwilke 1.52 [THEN]
1619 : anton 1.1
1620 : jwilke 1.53 T has? rom H [IF]
1621 : pazsan 1.54 Build: ( -- ) T here 0 , H switchram T align here swap ! 0 , 0 , H ( switchrom ) ;
1622 :     by (Constant)
1623 :     Builder 2Variable
1624 :     [ELSE]
1625 :     Build: T 0 , 0 , H ;
1626 :     by Create
1627 :     Builder 2Variable
1628 :     [THEN]
1629 :    
1630 :     T has? rom H [IF]
1631 :     Build: ( -- ) T here 0 , H switchram T align here swap ! 0 , H ( switchrom ) ;
1632 : jwilke 1.52 by (Constant)
1633 :     Builder AVariable
1634 :     [ELSE]
1635 : anton 1.1 Build: T 0 A, H ;
1636 :     by Create
1637 :     Builder AVariable
1638 : jwilke 1.52 [THEN]
1639 : anton 1.1
1640 : pazsan 1.3 \ User variables 04may94py
1641 :    
1642 :     >CROSS
1643 :     Variable tup 0 tup !
1644 :     Variable tudp 0 tudp !
1645 :     : u, ( n -- udp )
1646 :     tup @ tudp @ + T ! H
1647 : pazsan 1.19 tudp @ dup T cell+ H tudp ! ;
1648 : pazsan 1.3 : au, ( n -- udp )
1649 :     tup @ tudp @ + T A! H
1650 : pazsan 1.19 tudp @ dup T cell+ H tudp ! ;
1651 : pazsan 1.3 >TARGET
1652 :    
1653 :     Build: T 0 u, , H ;
1654 : pazsan 1.42 by: :douser ( ghost -- up-addr ) T @ H tup @ + ;DO
1655 : anton 1.1 Builder User
1656 :    
1657 : pazsan 1.3 Build: T 0 u, , 0 u, drop H ;
1658 :     by User
1659 : anton 1.1 Builder 2User
1660 :    
1661 : pazsan 1.3 Build: T 0 au, , H ;
1662 :     by User
1663 : anton 1.1 Builder AUser
1664 :    
1665 : jwilke 1.52 BuildSmart: T , H ;
1666 : pazsan 1.44 by (Constant)
1667 : anton 1.1 Builder Value
1668 :    
1669 : jwilke 1.52 BuildSmart: T A, H ;
1670 : pazsan 1.44 by (Constant)
1671 : pazsan 1.32 Builder AValue
1672 :    
1673 : jwilke 1.52 BuildSmart: ( -- ) [T'] noop T A, H ;
1674 : pazsan 1.42 by: :dodefer ( ghost -- ) ABORT" CROSS: Don't execute" ;DO
1675 : anton 1.1 Builder Defer
1676 : pazsan 1.37
1677 : jwilke 1.52 BuildSmart: ( inter comp -- ) swap T immediate A, A, H ;
1678 : pazsan 1.37 DO: ( ghost -- ) ABORT" CROSS: Don't execute" ;DO
1679 : anton 1.38 Builder interpret/compile:
1680 : pazsan 1.24
1681 :     \ Sturctures 23feb95py
1682 :    
1683 :     >CROSS
1684 :     : nalign ( addr1 n -- addr2 )
1685 :     \ addr2 is the aligned version of addr1 wrt the alignment size n
1686 :     1- tuck + swap invert and ;
1687 :     >TARGET
1688 :    
1689 : pazsan 1.44 Build: ;
1690 :     by: :dofield T @ H + ;DO
1691 :     Builder (Field)
1692 :    
1693 : anton 1.51 Build: ( align1 offset1 align size "name" -- align2 offset2 )
1694 :     rot dup T , H ( align1 align size offset1 )
1695 :     + >r nalign r> ;
1696 : pazsan 1.44 by (Field)
1697 : pazsan 1.24 Builder Field
1698 :    
1699 : anton 1.51 : struct T 1 chars 0 H ;
1700 : pazsan 1.24 : end-struct T 2Constant H ;
1701 :    
1702 : jwilke 1.52 : cell% ( n -- size align )
1703 : anton 1.51 T 1 cells H dup ;
1704 : pazsan 1.24
1705 :     \ ' 2Constant Alias2 end-struct
1706 :     \ 0 1 T Chars H 2Constant struct
1707 : anton 1.1
1708 : jwilke 1.52 \ structural conditionals 17dec92py
1709 :    
1710 :     >CROSS
1711 :     : ?struc ( flag -- ) ABORT" CROSS: unstructured " ;
1712 :     : sys? ( sys -- sys ) dup 0= ?struc ;
1713 :     : >mark ( -- sys ) T here ( dup ." M" hex. ) 0 , H ;
1714 :    
1715 : jwilke 1.67 : branchoffset ( src dest -- ) - tchar / ; \ ?? jaw
1716 : jwilke 1.52
1717 :     : >resolve ( sys -- ) T here ( dup ." >" hex. ) over branchoffset swap ! H ;
1718 :    
1719 :     : <resolve ( sys -- ) T here ( dup ." <" hex. ) branchoffset , H ;
1720 :    
1721 : pazsan 1.54 :noname compile branch T here branchoffset , H ;
1722 :     IS branch, ( target-addr -- )
1723 :     :noname compile ?branch T here branchoffset , H ;
1724 :     IS ?branch, ( target-addr -- )
1725 :     :noname compile branch T here 0 , H ;
1726 :     IS branchmark, ( -- branchtoken )
1727 :     :noname compile ?branch T here 0 , H ;
1728 :     IS ?branchmark, ( -- branchtoken )
1729 :     :noname T here 0 , H ;
1730 :     IS ?domark, ( -- branchtoken )
1731 :     :noname dup T @ H ?struc T here over branchoffset swap ! H ;
1732 :     IS branchtoresolve, ( branchtoken -- )
1733 :     :noname branchto, T here H ;
1734 :     IS branchtomark, ( -- target-addr )
1735 : jwilke 1.52
1736 :     >TARGET
1737 :    
1738 :     \ Structural Conditionals 12dec92py
1739 :    
1740 :     Cond: BUT restrict? sys? swap ;Cond
1741 :     Cond: YET restrict? sys? dup ;Cond
1742 :    
1743 : pazsan 1.54 0 [IF]
1744 : jwilke 1.52 >CROSS
1745 :     Variable tleavings
1746 :     >TARGET
1747 :    
1748 :     Cond: DONE ( addr -- ) restrict? tleavings @
1749 :     BEGIN 2dup u> 0= WHILE dup T @ H swap >resolve REPEAT
1750 :     tleavings ! drop ;Cond
1751 :    
1752 :     >CROSS
1753 : pazsan 1.54 : (leave) T here H tleavings @ T , H tleavings ! ;
1754 : jwilke 1.52 >TARGET
1755 :    
1756 : pazsan 1.54 Cond: LEAVE restrict? compile branch (leave) ;Cond
1757 :     Cond: ?LEAVE restrict? compile 0= compile ?branch (leave) ;Cond
1758 : jwilke 1.52
1759 : jwilke 1.53 [ELSE]
1760 :     \ !! This is WIP
1761 :     \ The problem is (?DO)!
1762 :     \ perhaps we need a plug-in for (?DO)
1763 :    
1764 :     >CROSS
1765 :     Variable tleavings 0 tleavings !
1766 : pazsan 1.54 : (done) ( addr -- )
1767 :     tleavings @
1768 :     BEGIN dup
1769 :     WHILE
1770 :     >r dup r@ cell+ @ \ address of branch
1771 :     u> 0= \ lower than DO?
1772 :     WHILE
1773 :     r@ 2 cells + @ \ branch token
1774 :     branchtoresolve,
1775 :     r@ @ r> free throw
1776 :     REPEAT r> THEN
1777 :     tleavings ! drop ;
1778 :    
1779 : jwilke 1.53 >TARGET
1780 :    
1781 : pazsan 1.54 Cond: DONE ( addr -- ) restrict? (done) ;Cond
1782 : jwilke 1.53
1783 :     >CROSS
1784 : pazsan 1.54 : (leave) ( branchtoken -- )
1785 : jwilke 1.53 3 cells allocate throw >r
1786 :     T here H r@ cell+ !
1787 :     r@ 2 cells + !
1788 :     tleavings @ r@ !
1789 :     r> tleavings ! ;
1790 :     >TARGET
1791 :    
1792 : pazsan 1.54 Cond: LEAVE restrict? branchmark, (leave) ;Cond
1793 :     Cond: ?LEAVE restrict? compile 0= ?branchmark, (leave) ;Cond
1794 : jwilke 1.53
1795 :     [THEN]
1796 :    
1797 : pazsan 1.54 >CROSS
1798 :     \ !!JW ToDo : Move to general tools section
1799 :    
1800 :     : to1 ( x1 x2 xn n -- addr )
1801 :     \G packs n stack elements in a allocated memory region
1802 :     dup dup 1+ cells allocate throw dup >r swap 1+
1803 :     0 DO tuck ! cell+ LOOP
1804 :     drop r> ;
1805 :     : 1to ( addr -- x1 x2 xn )
1806 :     \G unpacks the elements saved by to1
1807 :     dup @ swap over cells + swap
1808 :     0 DO dup @ swap 1 cells - LOOP
1809 :     free throw ;
1810 :    
1811 :     : loop] branchto, dup <resolve tcell - (done) ;
1812 :    
1813 :     : skiploop] ?dup IF branchto, branchtoresolve, THEN ;
1814 :    
1815 :     >TARGET
1816 :    
1817 : jwilke 1.52 \ Structural Conditionals 12dec92py
1818 :    
1819 : jwilke 1.53 >TARGET
1820 : jwilke 1.52 Cond: AHEAD restrict? branchmark, ;Cond
1821 :     Cond: IF restrict? ?branchmark, ;Cond
1822 :     Cond: THEN restrict? sys? branchto, branchtoresolve, ;Cond
1823 :     Cond: ELSE restrict? sys? compile AHEAD swap compile THEN ;Cond
1824 :    
1825 :     Cond: BEGIN restrict? branchtomark, ;Cond
1826 :     Cond: WHILE restrict? sys? compile IF swap ;Cond
1827 :     Cond: AGAIN restrict? sys? branch, ;Cond
1828 :     Cond: UNTIL restrict? sys? ?branch, ;Cond
1829 :     Cond: REPEAT restrict? over 0= ?struc compile AGAIN compile THEN ;Cond
1830 :    
1831 :     Cond: CASE restrict? 0 ;Cond
1832 :     Cond: OF restrict? 1+ >r compile over compile =
1833 :     compile IF compile drop r> ;Cond
1834 : pazsan 1.45 Cond: ENDOF restrict? >r compile ELSE r> ;Cond
1835 :     Cond: ENDCASE restrict? compile drop 0 ?DO compile THEN LOOP ;Cond
1836 : anton 1.1
1837 :     \ Structural Conditionals 12dec92py
1838 :    
1839 : jwilke 1.67 :noname \ ?? i think 0 is too much! jaw
1840 : pazsan 1.54 0 compile (do)
1841 :     branchtomark, 2 to1 ;
1842 :     IS do, ( -- target-addr )
1843 :    
1844 :     \ :noname
1845 :     \ compile 2dup compile = compile IF
1846 :     \ compile 2drop compile ELSE
1847 :     \ compile (do) branchtomark, 2 to1 ;
1848 :     \ IS ?do,
1849 :    
1850 :     :noname
1851 :     0 compile (?do) ?domark, (leave)
1852 :     branchtomark, 2 to1 ;
1853 :     IS ?do, ( -- target-addr )
1854 :     :noname compile (for) branchtomark, ;
1855 :     IS for, ( -- target-addr )
1856 :     :noname 1to compile (loop) loop] compile unloop skiploop] ;
1857 :     IS loop, ( target-addr -- )
1858 :     :noname 1to compile (+loop) loop] compile unloop skiploop] ;
1859 :     IS +loop, ( target-addr -- )
1860 :     :noname compile (next) loop] compile unloop ;
1861 :     IS next, ( target-addr -- )
1862 :    
1863 :     Cond: DO restrict? do, ;Cond
1864 :     Cond: ?DO restrict? ?do, ;Cond
1865 :     Cond: FOR restrict? for, ;Cond
1866 :    
1867 :     Cond: LOOP restrict? sys? loop, ;Cond
1868 :     Cond: +LOOP restrict? sys? +loop, ;Cond
1869 :     Cond: NEXT restrict? sys? next, ;Cond
1870 : jwilke 1.52
1871 : anton 1.1 \ String words 23feb93py
1872 :    
1873 : jwilke 1.52 : ," [char] " parse T string, align H ;
1874 : anton 1.1
1875 :     Cond: ." restrict? compile (.") T ," H ;Cond
1876 :     Cond: S" restrict? compile (S") T ," H ;Cond
1877 :     Cond: ABORT" restrict? compile (ABORT") T ," H ;Cond
1878 :    
1879 :     Cond: IS T ' >body H compile ALiteral compile ! ;Cond
1880 : pazsan 1.66 : IS T >address ' >body ! H ;
1881 : pazsan 1.9 Cond: TO T ' >body H compile ALiteral compile ! ;Cond
1882 :     : TO T ' >body ! H ;
1883 : anton 1.1
1884 : jwilke 1.52 Cond: defers T ' >body @ compile, H ;Cond
1885 :     : on T -1 swap ! H ;
1886 :     : off T 0 swap ! H ;
1887 :    
1888 : anton 1.1 \ LINKED ERR" ENV" 2ENV" 18may93jaw
1889 :    
1890 :     \ linked list primitive
1891 :     : linked T here over @ A, swap ! H ;
1892 : jwilke 1.52 : chained T linked A, H ;
1893 : anton 1.1
1894 :     : err" s" ErrLink linked" evaluate T , H
1895 : jwilke 1.52 [char] " parse T string, align H ;
1896 : anton 1.1
1897 :     : env" [char] " parse s" EnvLink linked" evaluate
1898 : jwilke 1.52 T string, align , H ;
1899 : anton 1.1
1900 :     : 2env" [char] " parse s" EnvLink linked" evaluate
1901 : jwilke 1.52 here >r T string, align , , H
1902 : anton 1.1 r> dup T c@ H 80 and swap T c! H ;
1903 :    
1904 :     \ compile must be last 22feb93py
1905 :    
1906 :     Cond: compile ( -- ) restrict? \ name
1907 : pazsan 1.13 bl word gfind dup 0= ABORT" CROSS: Can't compile"
1908 : anton 1.1 0> IF gexecute
1909 :     ELSE dup >magic @ <imm> =
1910 :     IF gexecute
1911 : pazsan 1.54 ELSE compile (compile) addr, THEN THEN ;Cond
1912 : anton 1.1
1913 :     Cond: postpone ( -- ) restrict? \ name
1914 : pazsan 1.13 bl word gfind dup 0= ABORT" CROSS: Can't compile"
1915 : anton 1.1 0> IF gexecute
1916 :     ELSE dup >magic @ <imm> =
1917 :     IF gexecute
1918 : pazsan 1.54 ELSE compile (compile) addr, THEN THEN ;Cond
1919 :    
1920 :     \ \ minimal definitions
1921 :    
1922 : anton 1.1 >MINIMAL
1923 :     also minimal
1924 :     \ Usefull words 13feb93py
1925 :    
1926 :     : KB 400 * ;
1927 :    
1928 : pazsan 1.54 \ \ [IF] [ELSE] [THEN] ... 14sep97jaw
1929 :    
1930 :     \ it is useful to define our own structures and not to rely
1931 :     \ on the words in the compiler
1932 :     \ The words in the compiler might be defined with vocabularies
1933 :     \ this doesn't work with our self-made compile-loop
1934 :    
1935 :     Create parsed 20 chars allot \ store word we parsed
1936 :    
1937 :     : upcase
1938 :     parsed count bounds
1939 :     ?DO I c@ toupper I c! LOOP ;
1940 :    
1941 :     : [ELSE]
1942 :     1 BEGIN
1943 :     BEGIN bl word count dup WHILE
1944 :     comment? parsed place upcase parsed count
1945 :     2dup s" [IF]" compare 0= >r
1946 :     2dup s" [IFUNDEF]" compare 0= >r
1947 :     2dup s" [IFDEF]" compare 0= r> or r> or
1948 :     IF 2drop 1+
1949 :     ELSE 2dup s" [ELSE]" compare 0=
1950 :     IF 2drop 1- dup
1951 :     IF 1+
1952 :     THEN
1953 :     ELSE
1954 :     2dup s" [ENDIF]" compare 0= >r
1955 :     s" [THEN]" compare 0= r> or
1956 :     IF 1- THEN
1957 :     THEN
1958 :     THEN
1959 :     ?dup 0= ?EXIT
1960 :     REPEAT
1961 :     2drop refill 0=
1962 :     UNTIL drop ; immediate
1963 :    
1964 :     : [THEN] ( -- ) ; immediate
1965 :    
1966 :     : [ENDIF] ( -- ) ; immediate
1967 :    
1968 :     : [IF] ( flag -- )
1969 :     0= IF postpone [ELSE] THEN ; immediate
1970 :    
1971 :     Cond: [IF] postpone [IF] ;Cond
1972 :     Cond: [THEN] postpone [THEN] ;Cond
1973 :     Cond: [ELSE] postpone [ELSE] ;Cond
1974 :    
1975 : anton 1.1 \ define new [IFDEF] and [IFUNDEF] 20may93jaw
1976 :    
1977 : pazsan 1.43 : defined? defined? ;
1978 : pazsan 1.44 : needed? needed? ;
1979 :     : doer? doer? ;
1980 : anton 1.1
1981 : jwilke 1.52 \ we want to use IFDEF on compiler directives (e.g. E?) in the source, too
1982 :    
1983 :     : directive?
1984 :     bl word count [ ' target >wordlist ] aliteral search-wordlist
1985 :     dup IF nip THEN ;
1986 :    
1987 :     : [IFDEF] >in @ directive? swap >in !
1988 :     0= IF defined? ELSE name 2drop true THEN
1989 :     postpone [IF] ;
1990 :    
1991 : pazsan 1.43 : [IFUNDEF] defined? 0= postpone [IF] ;
1992 : anton 1.1
1993 : pazsan 1.54 Cond: [IFDEF] postpone [IFDEF] ;Cond
1994 :    
1995 :     Cond: [IFUNDEF] postpone [IFUNDEF] ;Cond
1996 :    
1997 : anton 1.1 \ C: \- \+ Conditional Compiling 09jun93jaw
1998 :    
1999 : pazsan 1.43 : C: >in @ defined? 0=
2000 : anton 1.1 IF >in ! T : H
2001 :     ELSE drop
2002 :     BEGIN bl word dup c@
2003 :     IF count comment? s" ;" compare 0= ?EXIT
2004 :     ELSE refill 0= ABORT" CROSS: Out of Input while C:"
2005 :     THEN
2006 :     AGAIN
2007 :     THEN ;
2008 :    
2009 :     also minimal
2010 :    
2011 : jwilke 1.52 \G doesn't skip line when bit is set in debugmask
2012 :     : \D name evaluate debugmasksource @ and 0= IF postpone \ THEN ;
2013 :    
2014 : anton 1.48 \G interprets the line if word is not defined
2015 : pazsan 1.43 : \- defined? IF postpone \ THEN ;
2016 : anton 1.48
2017 :     \G interprets the line if word is defined
2018 : pazsan 1.43 : \+ defined? 0= IF postpone \ THEN ;
2019 : anton 1.1
2020 : anton 1.48 Cond: \- \- ;Cond
2021 :     Cond: \+ \+ ;Cond
2022 : jwilke 1.52 Cond: \D \D ;Cond
2023 : anton 1.48
2024 :     : ?? bl word find IF execute ELSE drop 0 THEN ;
2025 :    
2026 :     : needed:
2027 :     \G defines ghost for words that we want to be compiled
2028 :     BEGIN >in @ bl word c@ WHILE >in ! ghost drop REPEAT drop ;
2029 :    
2030 :     previous
2031 :    
2032 : anton 1.1 \ save-cross 17mar93py
2033 :    
2034 : anton 1.48 >CROSS
2035 : pazsan 1.62 Create magic s" Gforth2x" here over allot swap move
2036 : pazsan 1.26
2037 : anton 1.63 bigendian 1+ \ strangely, in magic big=0, little=1
2038 :     tcell 1 = 0 and or
2039 : pazsan 1.62 tcell 2 = 2 and or
2040 :     tcell 4 = 4 and or
2041 :     tcell 8 = 6 and or
2042 : pazsan 1.65 tchar 1 = $00 and or
2043 :     tchar 2 = $28 and or
2044 :     tchar 4 = $50 and or
2045 :     tchar 8 = $78 and or
2046 : pazsan 1.62 magic 7 + c!
2047 : pazsan 1.26
2048 : anton 1.34 : save-cross ( "image-name" "binary-name" -- )
2049 :     bl parse ." Saving to " 2dup type cr
2050 : anton 1.1 w/o bin create-file throw >r
2051 : anton 1.48 TNIL IF
2052 : pazsan 1.43 s" #! " r@ write-file throw
2053 :     bl parse r@ write-file throw
2054 :     s" -i" r@ write-file throw
2055 :     #lf r@ emit-file throw
2056 :     r@ dup file-position throw drop 8 mod 8 swap ( file-id limit index )
2057 :     ?do
2058 :     bl over emit-file throw
2059 :     loop
2060 :     drop
2061 :     magic 8 r@ write-file throw \ write magic
2062 :     ELSE
2063 :     bl parse 2drop
2064 :     THEN
2065 : jwilke 1.52 image @ there
2066 :     r@ write-file throw \ write image
2067 : anton 1.48 TNIL IF
2068 :     bit$ @ there 1- tcell>bit rshift 1+
2069 : pazsan 1.16 r@ write-file throw \ write tags
2070 : pazsan 1.43 THEN
2071 : anton 1.1 r> close-file throw ;
2072 :    
2073 : jwilke 1.52 : save-region ( addr len -- )
2074 :     bl parse w/o bin create-file throw >r
2075 : jwilke 1.67 swap >image swap r@ write-file throw
2076 : jwilke 1.52 r> close-file throw ;
2077 :    
2078 : anton 1.1 \ words that should be in minimal
2079 : jwilke 1.52
2080 :     create s-buffer 50 chars allot
2081 :    
2082 : anton 1.48 >MINIMAL
2083 :     also minimal
2084 : anton 1.1
2085 : anton 1.48 bigendian Constant bigendian
2086 : jwilke 1.52 : here there ;
2087 : jwilke 1.67 : equ constant ;
2088 :     : mark there constant ;
2089 : pazsan 1.54
2090 :     \ compiler directives
2091 : jwilke 1.52 : >ram >ram ;
2092 :     : >rom >rom ;
2093 :     : >auto >auto ;
2094 :     : >tempdp >tempdp ;
2095 :     : tempdp> tempdp> ;
2096 :     : const constflag on ;
2097 :     : warnings name 3 = 0= twarnings ! drop ;
2098 : anton 1.60 : | ;
2099 :     \ : | NoHeaderFlag on ; \ This is broken (damages the last word)
2100 : jwilke 1.52
2101 : anton 1.48 : save-cross save-cross ;
2102 : jwilke 1.52 : save-region save-region ;
2103 :     : tdump swap >image swap dump ;
2104 :    
2105 : anton 1.48 also forth
2106 : jwilke 1.52 [IFDEF] Label : Label defempty? Label ; [THEN]
2107 :     [IFDEF] start-macros : start-macros defempty? start-macros ; [THEN]
2108 :     [IFDEF] builttag : builttag builttag ; [THEN]
2109 : anton 1.48 previous
2110 :    
2111 : jwilke 1.52 : s" [char] " parse s-buffer place s-buffer count ; \ for environment?
2112 : pazsan 1.43 : + + ;
2113 : jwilke 1.52 : 1+ 1 + ;
2114 :     : 2+ 2 + ;
2115 : pazsan 1.43 : or or ;
2116 :     : 1- 1- ;
2117 :     : - - ;
2118 : jwilke 1.52 : and and ;
2119 :     : or or ;
2120 : pazsan 1.43 : 2* 2* ;
2121 :     : * * ;
2122 :     : / / ;
2123 :     : dup dup ;
2124 :     : over over ;
2125 :     : swap swap ;
2126 :     : rot rot ;
2127 :     : drop drop ;
2128 :     : = = ;
2129 :     : 0= 0= ;
2130 :     : lshift lshift ;
2131 :     : 2/ 2/ ;
2132 : pazsan 1.19 : . . ;
2133 : anton 1.1
2134 : pazsan 1.43 : all-words ['] false IS skip? ;
2135 :     : needed-words ['] needed? IS skip? ;
2136 :     : undef-words ['] defined? IS skip? ;
2137 : anton 1.1
2138 : pazsan 1.40 : \ postpone \ ; immediate
2139 : pazsan 1.47 : \G T-\G ; immediate
2140 : pazsan 1.40 : ( postpone ( ; immediate
2141 : anton 1.1 : include bl word count included ;
2142 : jwilke 1.52 : require require ;
2143 : anton 1.1 : .( [char] ) parse type ;
2144 : jwilke 1.52 : ." [char] " parse type ;
2145 : anton 1.1 : cr cr ;
2146 :    
2147 :     : times 0 ?DO dup T c, H LOOP drop ; \ used for space table creation
2148 :     only forth also minimal definitions
2149 :    
2150 :     \ cross-compiler words
2151 :    
2152 :     : decimal decimal ;
2153 :     : hex hex ;
2154 :    
2155 : pazsan 1.3 : tudp T tudp H ;
2156 : pazsan 1.39 : tup T tup H ;
2157 :    
2158 :     : doc-off false T to-doc H ! ;
2159 :     : doc-on true T to-doc H ! ;
2160 : jwilke 1.52 [IFDEF] dbg : dbg dbg ; [THEN]
2161 : pazsan 1.39
2162 :     minimal
2163 : anton 1.1
2164 :     \ for debugging...
2165 :     : order order ;
2166 : jwilke 1.52 : hwords words ;
2167 :     : words also ghosts words previous ;
2168 : anton 1.1 : .s .s ;
2169 :     : bye bye ;
2170 :    
2171 :     \ turnkey direction
2172 :     : H forth ; immediate
2173 :     : T minimal ; immediate
2174 :     : G ghosts ; immediate
2175 :    
2176 :     : turnkey 0 set-order also Target definitions
2177 :     also Minimal also ;
2178 :    
2179 :     \ these ones are pefered:
2180 :    
2181 :     : lock turnkey ;
2182 :     : unlock forth also cross ;
2183 : jwilke 1.52
2184 :     : [[ also unlock ;
2185 :     : ]] previous previous ;
2186 : anton 1.1
2187 :     unlock definitions also minimal
2188 :     : lock lock ;
2189 :     lock

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help