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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help