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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help