[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.168 \ Copyright (C) 1995,1996,1997,1998,1999,2000,2003,2004,2005,2006,2007,2009 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 : anton 1.166 \ as published by the Free Software Foundation, either version 3
11 : anton 1.30 \ 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 : anton 1.166 \ along with this program. If not, see http://www.gnu.org/licenses/.
20 : anton 1.1
21 : jwilke 1.67 0
22 :     [IF]
23 :    
24 :     ToDo:
25 : jwilke 1.107 - Crossdoc destination ./doc/crossdoc.fd makes no sense when
26 :     cross.fs is used seperately. jaw
27 :     - Do we need this char translation with >address and in branchoffset?
28 :     (>body also affected) jaw
29 : jwilke 1.67
30 :     [THEN]
31 : anton 1.48
32 : anton 1.128 s" compat/strcomp.fs" included
33 :    
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 : jwilke 1.105
66 : jwilke 1.77 : >cross also Cross definitions previous ;
67 :     : >target also Target definitions previous ;
68 :     : >minimal also Minimal definitions previous ;
69 :    
70 :     H
71 :    
72 :     >CROSS
73 :    
74 : jwilke 1.123 \ Test against this definitions to find out whether we are cross-compiling
75 :     \ may be usefull for assemblers
76 :     0 Constant gforth-cross-indicator
77 :    
78 : jwilke 1.77 \ find out whether we are compiling with gforth
79 :    
80 :     : defined? bl word find nip ;
81 :     defined? emit-file defined? toupper and \ drop 0
82 :     [IF]
83 :     \ use this in a gforth system
84 :     : \GFORTH ; immediate
85 :     : \ANSI postpone \ ; immediate
86 :     [ELSE]
87 :     : \GFORTH postpone \ ; immediate
88 :     : \ANSI ; immediate
89 :     [THEN]
90 :    
91 :     \ANSI : [IFUNDEF] defined? 0= postpone [IF] ; immediate
92 :     \ANSI : [IFDEF] defined? postpone [IF] ; immediate
93 :     0 \ANSI drop 1
94 :     [IF]
95 :     : \G postpone \ ; immediate
96 :     : rdrop postpone r> postpone drop ; immediate
97 :     : name bl word count ;
98 :     : bounds over + swap ;
99 :     : scan >r BEGIN dup WHILE over c@ r@ <> WHILE 1 /string REPEAT THEN rdrop ;
100 :     : linked here over @ , swap ! ;
101 :     : alias create , DOES> @ EXECUTE ;
102 :     : defer ['] noop alias ;
103 :     : is state @
104 :     IF ' >body postpone literal postpone !
105 :     ELSE ' >body ! THEN ; immediate
106 :     : 0>= 0< 0= ;
107 :     : d<> rot <> -rot <> or ;
108 :     : toupper dup [char] a [char] z 1+ within IF [char] A [char] a - + THEN ;
109 :     Variable ebuf
110 :     : emit-file ( c fd -- ior ) swap ebuf c! ebuf 1 chars rot write-file ;
111 :     0a Constant #lf
112 :     0d Constant #cr
113 :    
114 :     [IFUNDEF] Warnings Variable Warnings [THEN]
115 :    
116 :     \ \ Number parsing 23feb93py
117 :    
118 :     \ number? number 23feb93py
119 :    
120 :     Variable dpl
121 :    
122 :     hex
123 :     Create bases 10 , 2 , A , 100 ,
124 :     \ 16 2 10 character
125 :    
126 :     \ !! protect BASE saving wrapper against exceptions
127 :     : getbase ( addr u -- addr' u' )
128 :     over c@ [char] $ - dup 4 u<
129 :     IF
130 :     cells bases + @ base ! 1 /string
131 :     ELSE
132 :     drop
133 :     THEN ;
134 :    
135 :     : sign? ( addr u -- addr u flag )
136 :     over c@ [char] - = dup >r
137 :     IF
138 :     1 /string
139 :     THEN
140 :     r> ;
141 :    
142 :     : s>unumber? ( addr u -- ud flag )
143 : jwilke 1.78 over [char] ' =
144 :     IF \ a ' alone is rather unusual :-)
145 :     drop char+ c@ 0 true EXIT
146 :     THEN
147 : jwilke 1.77 base @ >r dpl on getbase
148 :     0. 2swap
149 :     BEGIN ( d addr len )
150 :     dup >r >number dup
151 :     WHILE \ there are characters left
152 :     dup r> -
153 :     WHILE \ the last >number parsed something
154 :     dup 1- dpl ! over c@ [char] . =
155 :     WHILE \ the current char is '.'
156 :     1 /string
157 :     REPEAT THEN \ there are unparseable characters left
158 :     2drop false
159 :     ELSE
160 :     rdrop 2drop true
161 :     THEN
162 :     r> base ! ;
163 :    
164 :     \ ouch, this is complicated; there must be a simpler way - anton
165 :     : s>number? ( addr len -- d f )
166 :     \ converts string addr len into d, flag indicates success
167 :     sign? >r
168 :     s>unumber?
169 :     0= IF
170 :     rdrop false
171 :     ELSE \ no characters left, all ok
172 :     r>
173 :     IF
174 :     dnegate
175 :     THEN
176 :     true
177 :     THEN ;
178 :    
179 :     : s>number ( addr len -- d )
180 :     \ don't use this, there is no way to tell success
181 :     s>number? drop ;
182 :    
183 :     : snumber? ( c-addr u -- 0 / n -1 / d 0> )
184 :     s>number? 0=
185 :     IF
186 :     2drop false EXIT
187 :     THEN
188 :     dpl @ dup 0< IF
189 :     nip
190 :     ELSE
191 :     1+
192 :     THEN ;
193 :    
194 : anton 1.167 : (number?) ( string -- string 0 / n -1 / d 0> )
195 : jwilke 1.77 dup >r count snumber? dup if
196 :     rdrop
197 :     else
198 :     r> swap
199 :     then ;
200 :    
201 :     : number ( string -- d )
202 : anton 1.167 (number?) ?dup 0= abort" ?" 0<
203 : jwilke 1.77 IF
204 :     s>d
205 :     THEN ;
206 :    
207 :     [THEN]
208 : anton 1.48
209 : anton 1.167 [IFUNDEF] (number?) : (number?) number? ; [THEN]
210 :    
211 : jwilke 1.115 \ this provides assert( and struct stuff
212 :     \GFORTH [IFUNDEF] assert1(
213 :     \GFORTH also forth definitions require assert.fs previous
214 :     \GFORTH [THEN]
215 :    
216 :     >CROSS
217 :    
218 : jwilke 1.52 hex \ the defualt base for the cross-compiler is hex !!
219 : jwilke 1.78 \ Warnings off
220 : jwilke 1.52
221 :     \ words that are generaly useful
222 :    
223 : pazsan 1.59 : KB 400 * ;
224 : jwilke 1.52 : >wordlist ( vocabulary-xt -- wordlist-struct )
225 :     also execute get-order swap >r 1- set-order r> ;
226 :    
227 :     : umax 2dup u< IF swap THEN drop ;
228 :     : umin 2dup u> IF swap THEN drop ;
229 : anton 1.1
230 : pazsan 1.23 : string, ( c-addr u -- )
231 :     \ puts down string as cstring
232 :     dup c, here swap chars dup allot move ;
233 : pazsan 1.5
234 : jwilke 1.77 : ," [char] " parse string, ;
235 :    
236 : jwilke 1.52 : SetValue ( n -- <name> )
237 : pazsan 1.54 \G Same behaviour as "Value" if the <name> is not defined
238 :     \G Same behaviour as "to" if <name> is defined
239 : jwilke 1.52 \G SetValue searches in the current vocabulary
240 : jwilke 1.68 save-input bl word >r restore-input throw r> count
241 :     get-current search-wordlist
242 :     IF drop >r
243 :     \ we have to set current to be topmost context wordlist
244 :     get-order get-order get-current swap 1+ set-order
245 :     r> ['] to execute
246 : jwilke 1.74 set-order
247 : jwilke 1.68 ELSE Value THEN ;
248 : jwilke 1.52
249 :     : DefaultValue ( n -- <name> )
250 : pazsan 1.54 \G Same behaviour as "Value" if the <name> is not defined
251 :     \G DefaultValue searches in the current vocabulary
252 : jwilke 1.52 save-input bl word >r restore-input throw r> count
253 :     get-current search-wordlist
254 : pazsan 1.56 IF bl word drop 2drop ELSE Value THEN ;
255 : anton 1.1
256 :     hex
257 :    
258 : jwilke 1.120 \ FIXME delete`
259 : jwilke 1.52 \ 1 Constant Cross-Flag \ to check whether assembler compiler plug-ins are
260 :     \ for cross-compiling
261 :     \ No! we use "[IFUNDEF]" there to find out whether we are target compiling!!!
262 :    
263 : jwilke 1.120 \ FIXME move down
264 : jwilke 1.52 : comment? ( c-addr u -- c-addr u )
265 : anton 1.128 2dup s" (" str=
266 : jwilke 1.52 IF postpone (
267 : anton 1.128 ELSE 2dup s" \" str= IF postpone \ THEN
268 : jwilke 1.52 THEN ;
269 :    
270 : jwilke 1.120 : X ( -- <name> )
271 :     \G The next word in the input is a target word.
272 :     \G Equivalent to T <name> but without permanent
273 :     \G switch to target dictionary. Used as prefix e.g. for @, !, here etc.
274 :     bl word count [ ' target >wordlist ] Literal search-wordlist
275 :     IF state @ IF compile, ELSE execute THEN
276 :     ELSE -1 ABORT" Cross: access method not supported!"
277 :     THEN ; immediate
278 : jwilke 1.105
279 : jwilke 1.52 \ Begin CROSS COMPILER:
280 :    
281 : jwilke 1.74 \ debugging
282 : jwilke 1.52
283 : jwilke 1.74 0 [IF]
284 :    
285 :     This implements debugflags for the cross compiler and the compiled
286 :     images. It works identical to the has-flags in the environment.
287 :     The debugflags are defined in a vocabluary. If the word exists and
288 :     its value is true, the flag is switched on.
289 :    
290 :     [THEN]
291 :    
292 : jwilke 1.77 >CROSS
293 :    
294 : jwilke 1.74 Vocabulary debugflags \ debug flags for cross
295 :     also debugflags get-order over
296 :     Constant debugflags-wl
297 :     set-order previous
298 :    
299 :     : DebugFlag
300 :     get-current >r debugflags-wl set-current
301 :     SetValue
302 :     r> set-current ;
303 :    
304 :     : Debug? ( adr u -- flag )
305 :     \G return true if debug flag is defined or switched on
306 :     debugflags-wl search-wordlist
307 :     IF EXECUTE
308 :     ELSE false THEN ;
309 :    
310 :     : D? ( <name> -- flag )
311 :     \G return true if debug flag is defined or switched on
312 :     \G while compiling we do not return the current value but
313 :     bl word count debug? ;
314 :    
315 :     : [d?]
316 :     \G compile the value-xt so the debug flag can be switched
317 :     \G the flag must exist!
318 :     bl word count debugflags-wl search-wordlist
319 :     IF compile,
320 :     ELSE -1 ABORT" unknown debug flag"
321 :     \ POSTPONE false
322 :     THEN ; immediate
323 : pazsan 1.54
324 : jwilke 1.123 : symentry ( adr len taddr -- )
325 :     \G Produce a symbol table (an optional symbol address
326 :     \G map) if wanted
327 :     [ [IFDEF] fd-symbol-table ]
328 :     base @ swap hex s>d <# 8 0 DO # LOOP #> fd-symbol-table write-file throw base !
329 :     s" :" fd-symbol-table write-file throw
330 :     fd-symbol-table write-line throw
331 :     [ [ELSE] ]
332 :     2drop drop
333 :     [ [THEN] ] ;
334 :    
335 :    
336 : jwilke 1.77 \ \ -------------------- source file
337 :    
338 :     decimal
339 :    
340 :     Variable cross-file-list
341 :     0 cross-file-list !
342 :     Variable target-file-list
343 :     0 target-file-list !
344 :     Variable host-file-list
345 :     0 host-file-list !
346 :    
347 :     cross-file-list Value file-list
348 :     0 Value source-desc
349 :    
350 :     \ file loading
351 :    
352 :     : >fl-id 1 cells + ;
353 :     : >fl-name 2 cells + ;
354 :    
355 :     Variable filelist 0 filelist !
356 :     Create NoFile ," #load-file#"
357 :    
358 :     : loadfile ( -- adr )
359 :     source-desc ?dup IF >fl-name ELSE NoFile THEN ;
360 :    
361 :     : sourcefilename ( -- adr len )
362 :     loadfile count ;
363 :    
364 :     \ANSI : sourceline# 0 ;
365 :    
366 :     \ \ -------------------- path handling from kernel/paths.fs
367 :    
368 :     \ paths.fs path file handling 03may97jaw
369 :    
370 :     \ -Changing the search-path:
371 :     \ fpath+ <path> adds a directory to the searchpath
372 :     \ fpath= <path>|<path> makes complete now searchpath
373 :     \ seperator is |
374 :     \ .fpath displays the search path
375 :     \ remark I:
376 :     \ a ./ in the beginning of filename is expanded to the directory the
377 :     \ current file comes from. ./ can also be included in the search-path!
378 :     \ ~+/ loads from the current working directory
379 :    
380 :     \ remark II:
381 :     \ if there is no sufficient space for the search path increase it!
382 :    
383 :    
384 :     \ -Creating custom paths:
385 :    
386 :     \ It is possible to use the search mechanism on yourself.
387 :    
388 :     \ Make a buffer for the path:
389 :     \ create mypath 100 chars , \ maximum length (is checked)
390 :     \ 0 , \ real len
391 :     \ 100 chars allot \ space for path
392 :     \ use the same functions as above with:
393 :     \ mypath path+
394 :     \ mypath path=
395 :     \ mypath .path
396 :    
397 :     \ do a open with the search path:
398 :     \ open-path-file ( adr len path -- fd adr len ior )
399 :     \ the file is opened read-only; if the file is not found an error is generated
400 :    
401 :     \ questions to: wilke@jwdt.com
402 :    
403 :     [IFUNDEF] +place
404 :     : +place ( adr len adr )
405 :     2dup >r >r
406 :     dup c@ char+ + swap move
407 :     r> r> dup c@ rot + swap c! ;
408 :     [THEN]
409 :    
410 :     [IFUNDEF] place
411 :     : place ( c-addr1 u c-addr2 )
412 :     2dup c! char+ swap move ;
413 :     [THEN]
414 :    
415 :     \ if we have path handling, use this and the setup of it
416 :     [IFUNDEF] open-fpath-file
417 :    
418 :     create sourcepath 1024 chars , 0 , 1024 chars allot \ !! make this dynamic
419 :     sourcepath value fpath
420 :    
421 :     : also-path ( adr len path^ -- )
422 :     >r
423 :     \ len check
424 :     r@ cell+ @ over + r@ @ u> ABORT" path buffer too small!"
425 :     \ copy into
426 :     tuck r@ cell+ dup @ cell+ + swap cmove
427 :     \ make delimiter
428 :     0 r@ cell+ dup @ cell+ + 2 pick + c! 1 + r> cell+ +!
429 :     ;
430 :    
431 :     : only-path ( adr len path^ -- )
432 :     dup 0 swap cell+ ! also-path ;
433 :    
434 :     : path+ ( path-addr "dir" -- ) \ gforth
435 :     \G Add the directory @var{dir} to the search path @var{path-addr}.
436 :     name rot also-path ;
437 :    
438 :     : fpath+ ( "dir" ) \ gforth
439 :     \G Add directory @var{dir} to the Forth search path.
440 :     fpath path+ ;
441 :    
442 :     : path= ( path-addr "dir1|dir2|dir3" ) \ gforth
443 :     \G Make a complete new search path; the path separator is |.
444 :     name 2dup bounds ?DO i c@ [char] | = IF 0 i c! THEN LOOP
445 :     rot only-path ;
446 :    
447 :     : fpath= ( "dir1|dir2|dir3" ) \ gforth
448 :     \G Make a complete new Forth search path; the path separator is |.
449 :     fpath path= ;
450 :    
451 : anton 1.145 : path>string cell+ dup cell+ swap @ ;
452 : jwilke 1.77
453 :     : next-path ( adr len -- adr2 len2 )
454 :     2dup 0 scan
455 :     dup 0= IF 2drop 0 -rot 0 -rot EXIT THEN
456 :     >r 1+ -rot r@ 1- -rot
457 :     r> - ;
458 :    
459 :     : previous-path ( path^ -- )
460 : anton 1.145 dup path>string
461 : jwilke 1.77 BEGIN tuck dup WHILE repeat ;
462 :    
463 :     : .path ( path-addr -- ) \ gforth
464 :     \G Display the contents of the search path @var{path-addr}.
465 : anton 1.145 path>string
466 : jwilke 1.77 BEGIN next-path dup WHILE type space REPEAT 2drop 2drop ;
467 :    
468 :     : .fpath ( -- ) \ gforth
469 :     \G Display the contents of the Forth search path.
470 :     fpath .path ;
471 :    
472 :     : absolut-path? ( addr u -- flag ) \ gforth
473 :     \G A path is absolute if it starts with a / or a ~ (~ expansion),
474 :     \G or if it is in the form ./*, extended regexp: ^[/~]|./, or if
475 :     \G it has a colon as second character ("C:..."). Paths simply
476 :     \G containing a / are not absolute!
477 :     2dup 2 u> swap 1+ c@ [char] : = and >r \ dos absoulte: c:/....
478 :     over c@ [char] / = >r
479 :     over c@ [char] ~ = >r
480 : anton 1.128 \ 2dup S" ../" string-prefix? r> or >r \ not catered for in expandtopic
481 :     S" ./" string-prefix?
482 : jwilke 1.77 r> r> r> or or or ;
483 :    
484 :     Create ofile 0 c, 255 chars allot
485 :     Create tfile 0 c, 255 chars allot
486 :    
487 :     : pathsep? dup [char] / = swap [char] \ = or ;
488 :    
489 :     : need/ ofile dup c@ + c@ pathsep? 0= IF s" /" ofile +place THEN ;
490 :    
491 :     : extractpath ( adr len -- adr len2 )
492 :     BEGIN dup WHILE 1-
493 :     2dup + c@ pathsep? IF EXIT THEN
494 :     REPEAT ;
495 :    
496 :     : remove~+ ( -- )
497 : anton 1.128 ofile count s" ~+/" string-prefix?
498 : jwilke 1.77 IF
499 :     ofile count 3 /string ofile place
500 :     THEN ;
501 :    
502 :     : expandtopic ( -- ) \ stack effect correct? - anton
503 :     \ expands "./" into an absolute name
504 : anton 1.128 ofile count s" ./" string-prefix?
505 : jwilke 1.77 IF
506 :     ofile count 1 /string tfile place
507 :     0 ofile c! sourcefilename extractpath ofile place
508 :     ofile c@ IF need/ THEN
509 :     tfile count over c@ pathsep? IF 1 /string THEN
510 :     ofile +place
511 :     THEN ;
512 :    
513 :     : compact.. ( adr len -- adr2 len2 )
514 : pazsan 1.85 \ deletes phrases like "xy/.." out of our directory name 2dec97jaw
515 :     over swap
516 :     BEGIN dup WHILE
517 : anton 1.128 dup >r '/ scan 2dup s" /../" string-prefix?
518 : pazsan 1.85 IF
519 :     dup r> - >r 4 /string over r> + 4 -
520 :     swap 2dup + >r move dup r> over -
521 :     ELSE
522 :     rdrop dup 1 min /string
523 :     THEN
524 :     REPEAT drop over - ;
525 : jwilke 1.77
526 :     : reworkdir ( -- )
527 :     remove~+
528 :     ofile count compact..
529 :     nip ofile c! ;
530 :    
531 :     : open-ofile ( -- fid ior )
532 :     \G opens the file whose name is in ofile
533 :     expandtopic reworkdir
534 :     ofile count r/o open-file ;
535 :    
536 :     : check-path ( adr1 len1 adr2 len2 -- fd 0 | 0 <>0 )
537 :     0 ofile ! >r >r ofile place need/
538 :     r> r> ofile +place
539 :     open-ofile ;
540 :    
541 :     : open-path-file ( addr1 u1 path-addr -- wfileid addr2 u2 0 | ior ) \ gforth
542 :     \G Look in path @var{path-addr} for the file specified by @var{addr1 u1}.
543 :     \G If found, the resulting path and an open file descriptor
544 :     \G are returned. If the file is not found, @var{ior} is non-zero.
545 :     >r
546 :     2dup absolut-path?
547 :     IF rdrop
548 :     ofile place open-ofile
549 :     dup 0= IF >r ofile count r> THEN EXIT
550 : anton 1.145 ELSE r> path>string
551 : jwilke 1.77 BEGIN next-path dup
552 :     WHILE 5 pick 5 pick check-path
553 :     0= IF >r 2drop 2drop r> ofile count 0 EXIT ELSE drop THEN
554 :     REPEAT
555 :     2drop 2drop 2drop -38
556 :     THEN ;
557 :    
558 :     : open-fpath-file ( addr1 u1 -- wfileid addr2 u2 0 | ior ) \ gforth
559 :     \G Look in the Forth search path for the file specified by @var{addr1 u1}.
560 :     \G If found, the resulting path and an open file descriptor
561 :     \G are returned. If the file is not found, @var{ior} is non-zero.
562 :     fpath open-path-file ;
563 :    
564 :     fpath= ~+
565 :    
566 :     [THEN]
567 :    
568 :     \ \ -------------------- include require 13may99jaw
569 :    
570 :     >CROSS
571 :    
572 :     : add-included-file ( adr len -- adr )
573 :     dup >fl-name char+ allocate throw >r
574 :     file-list @ r@ ! r@ file-list !
575 :     r@ >fl-name place r> ;
576 :    
577 :     : included? ( c-addr u -- f )
578 :     file-list
579 :     BEGIN @ dup
580 : anton 1.128 WHILE >r 2dup r@ >fl-name count str=
581 : jwilke 1.77 IF rdrop 2drop true EXIT THEN
582 :     r>
583 :     REPEAT
584 :     2drop drop false ;
585 :    
586 :     false DebugFlag showincludedfiles
587 :    
588 :     : included1 ( fd adr u -- )
589 :     \ include file adr u / fd
590 :     \ we don't use fd with include-file, because the forth system
591 :     \ doesn't know the name of the file to get a nice error report
592 :     [d?] showincludedfiles
593 :     IF cr ." Including: " 2dup type ." ..." THEN
594 :     rot close-file throw
595 :     source-desc >r
596 :     add-included-file to source-desc
597 :     sourcefilename
598 :     ['] included catch
599 :     r> to source-desc
600 :     throw ;
601 :    
602 :     : included ( adr len -- )
603 :     cross-file-list to file-list
604 :     open-fpath-file throw
605 :     included1 ;
606 :    
607 :     : required ( adr len -- )
608 :     cross-file-list to file-list
609 :     open-fpath-file throw \ 2dup cr ." R:" type
610 :     2dup included?
611 :     IF 2drop close-file throw
612 :     ELSE included1
613 :     THEN ;
614 :    
615 :     : include bl word count included ;
616 :    
617 :     : require bl word count required ;
618 :    
619 : jwilke 1.78 0 [IF]
620 :    
621 : jwilke 1.77 also forth definitions previous
622 :    
623 :     : included ( adr len -- ) included ;
624 :    
625 :     : required ( adr len -- ) required ;
626 :    
627 :     : include include ;
628 :    
629 :     : require require ;
630 :    
631 : jwilke 1.78 [THEN]
632 :    
633 : jwilke 1.77 >CROSS
634 :     hex
635 :    
636 :     \ \ -------------------- Error Handling 05aug97jaw
637 :    
638 :     \ Flags
639 :    
640 :     also forth definitions \ these values may be predefined before
641 :     \ the cross-compiler is loaded
642 :    
643 :     false DefaultValue stack-warn \ check on empty stack at any definition
644 :     false DefaultValue create-forward-warn \ warn on forward declaration of created words
645 :    
646 :     previous >CROSS
647 :    
648 :     : .dec
649 :     base @ decimal swap . base ! ;
650 :    
651 :     : .sourcepos
652 :     cr sourcefilename type ." :"
653 :     sourceline# .dec ;
654 :    
655 :     : warnhead
656 :     \G display error-message head
657 :     \G perhaps with linenumber and filename
658 :     .sourcepos ." Warning: " ;
659 :    
660 :     : empty? depth IF .sourcepos ." Stack not empty!" THEN ;
661 :    
662 :     stack-warn [IF]
663 :     : defempty? empty? ;
664 :     [ELSE]
665 :     : defempty? ; immediate
666 : jwilke 1.139 \ : defempty? .sourcepos ;
667 : jwilke 1.77 [THEN]
668 :    
669 : jwilke 1.105 \ \ -------------------- Compiler Plug Ins 01aug97jaw
670 :    
671 :     >CROSS
672 :    
673 :     \ Compiler States
674 :    
675 :     Variable comp-state
676 :     0 Constant interpreting
677 :     1 Constant compiling
678 :     2 Constant resolving
679 :     3 Constant assembling
680 :    
681 :     : compiling? comp-state @ compiling = ;
682 :    
683 :     : pi-undefined -1 ABORT" Plugin undefined" ;
684 :    
685 :     : Plugin ( -- : pluginname )
686 :     Create
687 :     \ for normal cross-compiling only one action
688 :     \ exists, this fields are identical. For the instant
689 :     \ simulation environment we need, two actions for each plugin
690 :     \ the target one and the one that generates the simulation code
691 :     ['] pi-undefined , \ action
692 :     ['] pi-undefined , \ target plugin action
693 :     8765 , \ plugin magic
694 :     DOES> perform ;
695 :    
696 :     Plugin DummyPlugin
697 :    
698 :     : 'PI ( -- addr : pluginname )
699 :     ' >body dup 2 cells + @ 8765 <> ABORT" not a plugin" ;
700 :    
701 :     : plugin-of ( xt -- : pluginname )
702 :     dup 'PI 2! ;
703 :    
704 :     : action-of ( xt -- : plunginname )
705 :     'PI cell+ ! ;
706 :    
707 :     : TPA ( -- : plugin )
708 :     \ target plugin action
709 :     \ executes current target action of plugin
710 :     'PI cell+ POSTPONE literal POSTPONE perform ; immediate
711 :    
712 :     Variable ppi-temp 0 ppi-temp !
713 :    
714 :     : pa:
715 :     \g define plugin action
716 :     ppi-temp @ ABORT" pa: definition not closed"
717 :     'PI ppi-temp ! :noname ;
718 :    
719 :     : ;pa
720 :     \g end a definition for plugin action
721 :     POSTPONE ; ppi-temp @ ! 0 ppi-temp ! ; immediate
722 :    
723 :    
724 : jwilke 1.107 Plugin dlit, ( d -- ) \ compile numerical value the target
725 : jwilke 1.105 Plugin lit, ( n -- )
726 :     Plugin alit, ( n -- )
727 :    
728 :     Plugin branch, ( target-addr -- ) \ compiles a branch
729 :     Plugin ?branch, ( target-addr -- ) \ compiles a ?branch
730 :     Plugin branchmark, ( -- branch-addr ) \ reserves room for a branch
731 :     Plugin ?branchmark, ( -- branch-addr ) \ reserves room for a ?branch
732 :     Plugin ?domark, ( -- branch-addr ) \ reserves room for a ?do branch
733 :     Plugin branchto, ( -- ) \ actual program position is target of a branch (do e.g. alignment)
734 :     ' NOOP plugin-of branchto,
735 :     Plugin branchtoresolve, ( branch-addr -- ) \ resolves a forward reference from branchmark
736 :     Plugin branchtomark, ( -- target-addr ) \ marks a branch destination
737 :    
738 :     Plugin colon, ( tcfa -- ) \ compiles call to tcfa at current position
739 :     Plugin prim, ( tcfa -- ) \ compiles primitive invocation
740 :     Plugin colonmark, ( -- addr ) \ marks a colon call
741 :     Plugin colon-resolve ( tcfa addr -- )
742 :    
743 :     Plugin addr-resolve ( target-addr addr -- )
744 :     Plugin doer-resolve ( ghost res-pnt target-addr addr -- ghost res-pnt )
745 :    
746 :     Plugin ncontrols? ( [ xn ... x1 ] n -- ) \ checks wheter n control structures are open
747 :     Plugin if, ( -- if-token )
748 :     Plugin else, ( if-token -- if-token )
749 :     Plugin then, ( if-token -- )
750 :     Plugin ahead,
751 :     Plugin begin,
752 :     Plugin while,
753 :     Plugin until,
754 :     Plugin again,
755 :     Plugin repeat,
756 :     Plugin cs-swap ( x1 x2 -- x2 x1 )
757 :    
758 :     Plugin case, ( -- n )
759 :     Plugin of, ( n -- x1 n )
760 :     Plugin endof, ( x1 n -- x2 n )
761 :     Plugin endcase, ( x1 .. xn n -- )
762 :    
763 :     Plugin do, ( -- do-token )
764 :     Plugin ?do, ( -- ?do-token )
765 :     Plugin for, ( -- for-token )
766 :     Plugin loop, ( do-token / ?do-token -- )
767 :     Plugin +loop, ( do-token / ?do-token -- )
768 :     Plugin next, ( for-token )
769 :     Plugin leave, ( -- )
770 :     Plugin ?leave, ( -- )
771 :    
772 : jwilke 1.121 Plugin ca>native \ Convert a code address to the processors
773 :     \ native address. This is used in doprim, and
774 :     \ code/code: primitive definitions word to
775 :     \ convert the addresses.
776 :     \ The only target where we need this is the misc
777 :     \ which is a 16 Bit processor with word addresses
778 :     \ but the forth system we build has a normal byte
779 :     \ addressed memory model
780 : jwilke 1.105
781 :     Plugin doprim, \ compiles start of a primitive
782 :     Plugin docol, \ compiles start of a colon definition
783 :     Plugin doer,
784 :     Plugin fini, \ compiles end of definition ;s
785 :     Plugin doeshandler,
786 :     Plugin dodoes,
787 :    
788 :     Plugin colon-start
789 :     ' noop plugin-of colon-start
790 :     Plugin colon-end
791 :     ' noop plugin-of colon-end
792 :    
793 :     Plugin ]comp \ starts compilation
794 :     ' noop plugin-of ]comp
795 :     Plugin comp[ \ ends compilation
796 :     ' noop plugin-of comp[
797 :    
798 :     Plugin t>body \ we need the system >body
799 :     \ and the target >body
800 :    
801 :     >TARGET
802 :     : >body t>body ;
803 :    
804 :    
805 : jwilke 1.103 \ Ghost Builder 06oct92py
806 :    
807 : jwilke 1.105 >CROSS
808 : jwilke 1.103 hex
809 : jwilke 1.105 \ Values for ghost magic
810 : jwilke 1.103 4711 Constant <fwd> 4712 Constant <res>
811 :     4713 Constant <imm> 4714 Constant <do:>
812 :     4715 Constant <skip>
813 :    
814 : jwilke 1.105 \ Bitmask for ghost flags
815 : jwilke 1.103 1 Constant <unique>
816 : jwilke 1.105 2 Constant <primitive>
817 :    
818 :     \ FXIME: move this to general stuff?
819 :     : set-flag ( addr flag -- )
820 :     over @ or swap ! ;
821 :    
822 :     : reset-flag ( addr flag -- )
823 :     invert over @ and swap ! ;
824 :    
825 :     : get-flag ( addr flag -- f )
826 :     swap @ and 0<> ;
827 :    
828 : jwilke 1.103
829 :     Struct
830 : jwilke 1.52
831 : jwilke 1.103 \ link to next ghost (always the first element)
832 :     cell% field >next-ghost
833 : jwilke 1.52
834 : jwilke 1.103 \ type of ghost
835 :     cell% field >magic
836 :    
837 :     \ pointer where ghost is in target, or if unresolved
838 :     \ points to the where we have to resolve (linked-list)
839 :     cell% field >link
840 : jwilke 1.52
841 : jwilke 1.106 \ execution semantics (while target compiling) of ghost
842 : jwilke 1.103 cell% field >exec
843 : jwilke 1.52
844 : jwilke 1.106 \ compilation action of this ghost; this is what is
845 :     \ done to compile a call (or whatever) to this definition.
846 :     \ E.g. >comp contains the semantic of postpone s"
847 :     \ whereas >exec-compile contains the semantic of s"
848 : jwilke 1.105 cell% field >comp
849 :    
850 : jwilke 1.106 \ Compilation sematics (while parsing) of this ghost. E.g.
851 :     \ "\" will skip the rest of line.
852 :     \ These semantics are defined by Cond: and
853 :     \ if a word is made immediate in instant, then the >exec2 field
854 :     \ gets copied to here
855 : jwilke 1.103 cell% field >exec-compile
856 : jwilke 1.52
857 : jwilke 1.106 \ Additional execution semantics of this ghost. This is used
858 :     \ for code generated by instant and for the doer-xt of created
859 :     \ words
860 : jwilke 1.103 cell% field >exec2
861 : jwilke 1.52
862 : jwilke 1.103 cell% field >created
863 : jwilke 1.52
864 : jwilke 1.103 \ the xt of the created ghost word itself
865 :     cell% field >ghost-xt
866 : jwilke 1.52
867 : jwilke 1.103 \ pointer to the counted string of the assiciated
868 :     \ assembler label
869 :     cell% field >asm-name
870 : jwilke 1.52
871 : jwilke 1.103 \ mapped primitives have a special address, so
872 :     \ we are able to detect them
873 :     cell% field >asm-dummyaddr
874 :    
875 :     \ for builder (create, variable...) words
876 :     \ the execution symantics of words built are placed here
877 :     \ this is a doer ghost or a dummy ghost
878 :     cell% field >do:ghost
879 : pazsan 1.93
880 : jwilke 1.103 cell% field >ghost-flags
881 : pazsan 1.93
882 : jwilke 1.103 cell% field >ghost-name
883 : jwilke 1.52
884 : jwilke 1.103 End-Struct ghost-struct
885 : jwilke 1.52
886 : jwilke 1.103 Variable ghost-list
887 :     0 ghost-list !
888 : jwilke 1.52
889 :     Variable executed-ghost \ last executed ghost, needed in tcreate and gdoes>
890 : jwilke 1.103 \ Variable last-ghost \ last ghost that is created
891 : jwilke 1.52 Variable last-header-ghost \ last ghost definitions with header
892 :    
893 : jwilke 1.103 \ space for ghosts resolve structure
894 :     \ we create ghosts in a sepearte space
895 :     \ and not to the current host dp, because this
896 :     \ gives trouble with instant while compiling and creating
897 :     \ a ghost for a forward reference
898 :     \ BTW: we cannot allocate another memory region
899 :     \ because allot will check the overflow!!
900 :     Variable cross-space-dp
901 :     Create cross-space 250000 allot here 100 allot align
902 :     Constant cross-space-end
903 :     cross-space cross-space-dp !
904 :     Variable cross-space-dp-orig
905 :    
906 :     : cross-space-used cross-space-dp @ cross-space - ;
907 :    
908 :     : >space ( -- )
909 :     dp @ cross-space-dp-orig !
910 :     cross-space-dp @ dp ! ;
911 :    
912 :     : space> ( -- )
913 :     dp @ dup cross-space-dp !
914 :     cross-space-end u> ABORT" CROSS: cross-space overflow"
915 :     cross-space-dp-orig @ dp ! ;
916 :    
917 : jwilke 1.106 \ this is just for debugging, to see this in the backtrace
918 : jwilke 1.103 : execute-exec execute ;
919 :     : execute-exec2 execute ;
920 :     : execute-exec-compile execute ;
921 :    
922 :     : NoExec
923 :     executed-ghost @ >exec2 @
924 :     ?dup
925 :     IF execute-exec2
926 :     ELSE true ABORT" CROSS: Don't execute ghost, or immediate target word"
927 :     THEN ;
928 :    
929 : jwilke 1.105 Defer is-forward
930 :    
931 : jwilke 1.103 : (ghostheader) ( -- )
932 : jwilke 1.121 ghost-list linked <fwd> , 0 , ['] NoExec , ['] is-forward ,
933 : pazsan 1.118 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , ;
934 : jwilke 1.103
935 :     : ghostheader ( -- ) (ghostheader) 0 , ;
936 :    
937 :     ' Ghosts >wordlist Constant ghosts-wordlist
938 :    
939 :     \ the current wordlist for ghost definitions in the host
940 :     ghosts-wordlist Value current-ghosts
941 :    
942 : jwilke 1.52 : Make-Ghost ( "name" -- ghost )
943 : jwilke 1.103 >space
944 :     \ save current and create in ghost vocabulary
945 :     get-current >r current-ghosts set-current
946 :     >in @ Create >in !
947 :     \ some forth systems like iForth need the immediate directly
948 :     \ after the word is created
949 :     \ restore current
950 :     r> set-current
951 :     here (ghostheader)
952 :     bl word count string, align
953 :     space>
954 :     \ set ghost-xt field by doing a search
955 :     dup >ghost-name count
956 :     current-ghosts search-wordlist
957 :     0= ABORT" CROSS: Just created, must be there!"
958 :     over >ghost-xt !
959 :     DOES>
960 :     dup executed-ghost !
961 :     >exec @ execute-exec ;
962 : jwilke 1.52
963 :     \ ghost words 14oct92py
964 :     \ changed: 10may93py/jaw
965 :    
966 : jwilke 1.103 Defer search-ghosts
967 :    
968 :     : (search-ghosts) ( adr len -- cfa true | 0 )
969 :     current-ghosts search-wordlist ;
970 :    
971 :     ' (search-ghosts) IS search-ghosts
972 :    
973 :     : gsearch ( addr len -- ghost true | 0 )
974 :     search-ghosts
975 :     dup IF swap >body swap THEN ;
976 :    
977 :     : gfind ( string -- ghost true / string false )
978 : jwilke 1.52 \ searches for string in word-list ghosts
979 : jwilke 1.103 \ dup count type space
980 :     dup >r count gsearch
981 :     dup IF rdrop ELSE r> swap THEN ;
982 : jwilke 1.52
983 :     : gdiscover ( xt -- ghost true | xt false )
984 : jwilke 1.103 >r ghost-list
985 : jwilke 1.52 BEGIN @ dup
986 : jwilke 1.103 WHILE dup >magic @ <fwd> <>
987 :     IF dup >link @ r@ =
988 :     IF rdrop true EXIT THEN
989 :     THEN
990 : jwilke 1.52 REPEAT
991 : jwilke 1.103 drop r> false ;
992 : jwilke 1.52
993 : jwilke 1.105 : xt>ghost ( xt -- ghost )
994 :     gdiscover 0= ABORT" CROSS: ghost not found for this xt" ;
995 :    
996 : jwilke 1.103 : Ghost ( "name" -- ghost )
997 :     >in @ bl word gfind IF nip EXIT THEN
998 : jwilke 1.52 drop >in ! Make-Ghost ;
999 :    
1000 :     : >ghostname ( ghost -- adr len )
1001 : jwilke 1.103 >ghost-name count ;
1002 :    
1003 :     : forward? ( ghost -- flag )
1004 :     >magic @ <fwd> = ;
1005 :    
1006 :     : undefined? ( ghost -- flag )
1007 :     >magic @ dup <fwd> = swap <skip> = or ;
1008 :    
1009 :     : immediate? ( ghost -- flag )
1010 :     >magic @ <imm> = ;
1011 :    
1012 :     Variable TWarnings
1013 :     TWarnings on
1014 :     Variable Exists-Warnings
1015 :     Exists-Warnings on
1016 :    
1017 :     : exists-warning ( ghost -- ghost )
1018 :     TWarnings @ Exists-Warnings @ and
1019 :     IF dup >ghostname warnhead type ." exists " THEN ;
1020 :    
1021 :     \ : HeaderGhost Ghost ;
1022 :    
1023 :     Variable reuse-ghosts reuse-ghosts off
1024 :    
1025 :     : HeaderGhost ( "name" -- ghost )
1026 :     >in @
1027 :     bl word count
1028 :     \ 2dup type space
1029 :     current-ghosts search-wordlist
1030 :     IF >body dup undefined? reuse-ghosts @ or
1031 :     IF nip EXIT
1032 :     ELSE exists-warning
1033 :     THEN
1034 :     drop >in !
1035 :     ELSE >in !
1036 :     THEN
1037 :     \ we keep the execution semantics of the prviously
1038 :     \ defined words, this is a workaround
1039 :     \ for the redefined \ until vocs work
1040 :     Make-Ghost ;
1041 :    
1042 : jwilke 1.78 : .ghost ( ghost -- ) >ghostname type ;
1043 :    
1044 : jwilke 1.77 \ ' >ghostname ALIAS @name
1045 : jwilke 1.52
1046 : jwilke 1.115 : findghost ( "ghostname" -- ghost )
1047 :     bl word gfind 0= ABORT" CROSS: Ghost don't exists" ;
1048 :    
1049 : jwilke 1.103 : [G'] ( -- ghost : name )
1050 :     \G ticks a ghost and returns its address
1051 : jwilke 1.115 findghost
1052 :     state @ IF postpone literal THEN ; immediate
1053 : jwilke 1.103
1054 : jwilke 1.105 : g>xt ( ghost -- xt )
1055 :     \G Returns the xt (cfa) of a ghost. Issues a warning if undefined.
1056 : jwilke 1.103 dup undefined? ABORT" CROSS: forward " >link @ ;
1057 :    
1058 : jwilke 1.105 : g>body ( ghost -- body )
1059 :     \G Returns the body-address (pfa) of a ghost.
1060 :     \G Issues a warning if undefined (a forward-reference).
1061 :     g>xt X >body ;
1062 :    
1063 : jwilke 1.103 1 Constant <label>
1064 : jwilke 1.52
1065 : jwilke 1.103 Struct
1066 :     \ bitmask of address type (not used for now)
1067 :     cell% field addr-type
1068 :     \ if this address is an xt, this field points to the ghost
1069 :     cell% field addr-xt-ghost
1070 :     \ a bit mask that tells as what part of the cell
1071 :     \ is refenced from an address pointer, used for assembler generation
1072 :     cell% field addr-refs
1073 :     End-Struct addr-struct
1074 :    
1075 :     : %allocerase ( align size -- addr )
1076 :     dup >r %alloc dup r> erase ;
1077 :    
1078 :     \ returns the addr struct, define it if 0 reference
1079 :     : define-addr-struct ( addr -- struct-addr )
1080 :     dup @ ?dup IF nip EXIT THEN
1081 :     addr-struct %allocerase tuck swap ! ;
1082 : jwilke 1.75
1083 : pazsan 1.118 >cross
1084 :    
1085 : jwilke 1.52 \ Predefined ghosts 12dec92py
1086 :    
1087 : jwilke 1.114 Ghost - drop \ need a ghost otherwise "-" would be treated as a number
1088 :    
1089 : jwilke 1.103 Ghost 0= drop
1090 :     Ghost branch Ghost ?branch 2drop
1091 :     Ghost unloop Ghost ;S 2drop
1092 : pazsan 1.118 Ghost lit Ghost ! 2drop
1093 :     Ghost noop drop
1094 : jwilke 1.103 Ghost over Ghost = Ghost drop 2drop drop
1095 :     Ghost 2drop drop
1096 :     Ghost 2dup drop
1097 : jwilke 1.115 Ghost call drop
1098 :     Ghost @ drop
1099 :     Ghost useraddr drop
1100 :     Ghost execute drop
1101 :     Ghost + drop
1102 :     Ghost decimal drop
1103 :     Ghost hex drop
1104 : pazsan 1.118 Ghost lit@ drop
1105 :     Ghost lit-perform drop
1106 :     Ghost lit+ drop
1107 :     Ghost does-exec drop
1108 : jwilke 1.52
1109 : pazsan 1.119 Ghost :docol Ghost :doesjump Ghost :dodoes 2drop drop
1110 :     Ghost :dovar drop
1111 :    
1112 : jwilke 1.52 \ \ Parameter for target systems 06oct92py
1113 :    
1114 : jwilke 1.103
1115 : jwilke 1.52 \ we define it ans like...
1116 :     wordlist Constant target-environment
1117 :    
1118 : jwilke 1.103 \ save information of current dictionary to restore with environ>
1119 :     Variable env-current
1120 : jwilke 1.52
1121 :     : >ENVIRON get-current env-current ! target-environment set-current ;
1122 :     : ENVIRON> env-current @ set-current ;
1123 : pazsan 1.43
1124 : anton 1.48 >TARGET
1125 : pazsan 1.43
1126 : jwilke 1.103 : environment? ( addr len -- [ x ] true | false )
1127 :     \G returns the content of environment variable and true or
1128 :     \G false if not present
1129 :     target-environment search-wordlist
1130 :     IF EXECUTE true ELSE false THEN ;
1131 :    
1132 :     : $has? ( addr len -- x | false )
1133 :     \G returns the content of environment variable
1134 :     \G or false if not present
1135 :     T environment? H dup IF drop THEN ;
1136 :    
1137 :     : e? ( "name" -- x )
1138 :     \G returns the content of environment variable.
1139 :     \G The variable is expected to exist. If not, issue an error.
1140 :     bl word count T environment? H
1141 :     0= ABORT" environment variable not defined!" ;
1142 :    
1143 :     : has? ( "name" --- x | false )
1144 :     \G returns the content of environment variable
1145 :     \G or false if not present
1146 :     bl word count T $has? H ;
1147 : jwilke 1.52
1148 : anton 1.48
1149 : pazsan 1.54 >ENVIRON get-order get-current swap 1+ set-order
1150 :     true SetValue compiler
1151 : pazsan 1.83 true SetValue cross
1152 : pazsan 1.54 true SetValue standard-threading
1153 :     >TARGET previous
1154 : jwilke 1.52
1155 : jwilke 1.77 0
1156 : jwilke 1.103 [IFDEF] mach-file drop mach-file count 1 [THEN]
1157 :     [IFDEF] machine-file drop machine-file 1 [THEN]
1158 :     [IF] included hex
1159 : jwilke 1.77 [ELSE] cr ." No machine description!" ABORT
1160 :     [THEN]
1161 : pazsan 1.43
1162 : jwilke 1.53 >ENVIRON
1163 :    
1164 : pazsan 1.54 T has? ec H
1165 :     [IF]
1166 :     false DefaultValue relocate
1167 :     false DefaultValue file
1168 :     false DefaultValue OS
1169 :     false DefaultValue prims
1170 :     false DefaultValue floating
1171 :     false DefaultValue glocals
1172 :     false DefaultValue dcomps
1173 :     false DefaultValue hash
1174 :     false DefaultValue xconds
1175 :     false DefaultValue header
1176 : jwilke 1.105 false DefaultValue backtrace
1177 :     false DefaultValue new-input
1178 :     false DefaultValue peephole
1179 : pazsan 1.162 false DefaultValue primcentric
1180 : jwilke 1.129 false DefaultValue abranch
1181 : jwilke 1.139 true DefaultValue f83headerstring
1182 : pazsan 1.141 true DefaultValue control-rack
1183 : pazsan 1.54 [THEN]
1184 :    
1185 : jwilke 1.139 true DefaultValue gforthcross
1186 : pazsan 1.54 true DefaultValue interpreter
1187 :     true DefaultValue ITC
1188 :     false DefaultValue rom
1189 : pazsan 1.158 false DefaultValue flash
1190 : jwilke 1.73 true DefaultValue standardthreading
1191 : jwilke 1.53
1192 : jwilke 1.139 \ ANSForth environment stuff
1193 :     8 DefaultValue ADDRESS-UNIT-BITS
1194 :     255 DefaultValue MAX-CHAR
1195 :     255 DefaultValue /COUNTED-STRING
1196 :    
1197 : jwilke 1.52 >TARGET
1198 : jwilke 1.53 s" relocate" T environment? H
1199 : jwilke 1.103 \ JAW why set NIL to this?!
1200 :     [IF] drop \ SetValue NIL
1201 : jwilke 1.113 [ELSE] >ENVIRON X NIL SetValue relocate
1202 : jwilke 1.53 [THEN]
1203 : jwilke 1.113 >TARGET
1204 :    
1205 :     0 Constant NIL
1206 : pazsan 1.43
1207 :     >CROSS
1208 :    
1209 : jwilke 1.52 \ \ Create additional parameters 19jan95py
1210 : pazsan 1.19
1211 : jwilke 1.71 \ currently cross only works for host machines with address-unit-bits
1212 :     \ eual to 8 because of s! and sc!
1213 :     \ but I start to query the environment just to modularize a little bit
1214 :    
1215 :     : check-address-unit-bits ( -- )
1216 :     \ s" ADDRESS-UNIT-BITS" environment?
1217 :     \ IF 8 <> ELSE true THEN
1218 :     \ ABORT" ADDRESS-UNIT-BITS unknown or not equal to 8!"
1219 :    
1220 :     \ shit, this doesn't work because environment? is only defined for
1221 :     \ gforth.fi and not kernl???.fi
1222 :     ;
1223 :    
1224 :     check-address-unit-bits
1225 :     8 Constant bits/byte \ we define: byte is address-unit
1226 :    
1227 :     1 bits/byte lshift Constant maxbyte
1228 : jwilke 1.77 \ this sets byte size for the target machine, (probably right guess) jaw
1229 : jwilke 1.67
1230 : pazsan 1.19 T
1231 : jwilke 1.71 NIL Constant TNIL
1232 :     cell Constant tcell
1233 :     cell<< Constant tcell<<
1234 :     cell>bit Constant tcell>bit
1235 :     bits/char Constant tbits/char
1236 :     bits/char H bits/byte T /
1237 :     Constant tchar
1238 :     float Constant tfloat
1239 :     1 bits/char lshift Constant tmaxchar
1240 :     [IFUNDEF] bits/byte
1241 :     8 Constant tbits/byte
1242 :     [ELSE]
1243 :     bits/byte Constant tbits/byte
1244 :     [THEN]
1245 : pazsan 1.19 H
1246 : pazsan 1.81 tbits/char bits/byte / Constant tbyte
1247 : jwilke 1.71
1248 : pazsan 1.164 : >signed ( u -- n )
1249 :     1 tbits/char tcell * 1- lshift 2dup and
1250 :     IF negate or ELSE drop THEN ;
1251 : pazsan 1.19
1252 : anton 1.48 \ Variables 06oct92py
1253 :    
1254 : jwilke 1.124 Variable (tlast)
1255 :     (tlast) Value tlast TNIL tlast ! \ Last name field
1256 : anton 1.48 Variable tlastcfa \ Last code field
1257 : jwilke 1.52
1258 :     \ statistics 10jun97jaw
1259 :    
1260 :     Variable headers-named 0 headers-named !
1261 :     Variable user-vars 0 user-vars !
1262 :    
1263 : jwilke 1.67 : target>bitmask-size ( u1 -- u2 )
1264 :     1- tcell>bit rshift 1+ ;
1265 :    
1266 : jwilke 1.105 : allocatetarget ( size -- adr )
1267 : jwilke 1.67 dup allocate ABORT" CROSS: No memory for target"
1268 :     swap over swap erase ;
1269 : jwilke 1.52
1270 : pazsan 1.54 \ \ memregion.fs
1271 : jwilke 1.52
1272 :    
1273 :     Variable last-defined-region \ pointer to last defined region
1274 :     Variable region-link \ linked list with all regions
1275 :     Variable mirrored-link \ linked list for mirrored regions
1276 :     0 dup mirrored-link ! region-link !
1277 :    
1278 : jwilke 1.139 : >rname 9 cells + ;
1279 :     : >rtouch 8 cells + ; \ executed when region is accessed
1280 : jwilke 1.124 : >rbm 4 cells + ; \ bitfield per cell witch indicates relocation
1281 : jwilke 1.103 : >rmem 5 cells + ;
1282 : jwilke 1.124 : >rtype 6 cells + ; \ field per cell witch points to a type struct
1283 :     : >rrom 7 cells + ; \ a -1 indicates that this region is rom
1284 : jwilke 1.67 : >rlink 3 cells + ;
1285 : jwilke 1.52 : >rdp 2 cells + ;
1286 :     : >rlen cell+ ;
1287 :     : >rstart ;
1288 :    
1289 : jwilke 1.113 : (region) ( addr len region -- )
1290 :     \G change startaddress and length of an existing region
1291 :     >r r@ last-defined-region !
1292 :     r@ >rlen ! dup r@ >rstart ! r> >rdp ! ;
1293 : jwilke 1.52
1294 : jwilke 1.139 : uninitialized -1 ABORT" CROSS: Region is uninitialized" ;
1295 :    
1296 : jwilke 1.123 : region ( addr len -- "name" )
1297 : jwilke 1.103 \G create a new region
1298 : jwilke 1.52 \ check whether predefined region exists
1299 :     save-input bl word find >r >r restore-input throw r> r> 0=
1300 :     IF \ make region
1301 :     drop
1302 :     save-input create restore-input throw
1303 :     here last-defined-region !
1304 :     over ( startaddr ) , ( length ) , ( dp ) ,
1305 : jwilke 1.139 region-link linked 0 , 0 , 0 , 0 ,
1306 :     ['] uninitialized ,
1307 :     bl word count string,
1308 : jwilke 1.52 ELSE \ store new parameters in region
1309 :     bl word drop
1310 : jwilke 1.113 >body (region)
1311 : jwilke 1.52 THEN ;
1312 :    
1313 : jwilke 1.103 : borders ( region -- startaddr endaddr )
1314 :     \G returns lower and upper region border
1315 : jwilke 1.67 dup >rstart @ swap >rlen @ over + ;
1316 : jwilke 1.52
1317 : jwilke 1.103 : extent ( region -- startaddr len )
1318 :     \G returns the really used area
1319 : jwilke 1.67 dup >rstart @ swap >rdp @ over - ;
1320 : jwilke 1.52
1321 : jwilke 1.103 : area ( region -- startaddr totallen )
1322 :     \G returns the total area
1323 : jwilke 1.79 dup >rstart @ swap >rlen @ ;
1324 : jwilke 1.52
1325 : jwilke 1.139 : dp@ ( region -- dp )
1326 :     >rdp @ ;
1327 :    
1328 : jwilke 1.124 : mirrored ( -- )
1329 :     \G mark last defined region as mirrored
1330 : jwilke 1.52 mirrored-link
1331 : jwilke 1.68 align linked last-defined-region @ , ;
1332 : jwilke 1.52
1333 : jwilke 1.124 : writeprotected
1334 :     \G mark a region as write protected
1335 :     -1 last-defined-region @ >rrom ! ;
1336 :    
1337 : jwilke 1.67 : .addr ( u -- )
1338 :     \G prints a 16 or 32 Bit nice hex value
1339 : jwilke 1.52 base @ >r hex
1340 :     tcell 2 u>
1341 : jwilke 1.77 IF s>d <# # # # # [char] . hold # # # # #> type
1342 : jwilke 1.52 ELSE s>d <# # # # # # #> type
1343 : jwilke 1.103 THEN r> base ! space ;
1344 : jwilke 1.52
1345 :     : .regions \G display region statistic
1346 :    
1347 :     \ we want to list the regions in the right order
1348 :     \ so first collect all regions on stack
1349 :     0 region-link @
1350 :     BEGIN dup WHILE dup @ REPEAT drop
1351 :     BEGIN dup
1352 : jwilke 1.67 WHILE cr
1353 :     0 >rlink - >r
1354 :     r@ >rname count tuck type
1355 : jwilke 1.52 12 swap - 0 max spaces space
1356 : jwilke 1.67 ." Start: " r@ >rstart @ dup .addr space
1357 :     ." End: " r@ >rlen @ + .addr space
1358 :     ." DP: " r> >rdp @ .addr
1359 : jwilke 1.52 REPEAT drop
1360 : jwilke 1.53 s" rom" T $has? H 0= ?EXIT
1361 : jwilke 1.52 cr ." Mirrored:"
1362 :     mirrored-link @
1363 :     BEGIN dup
1364 : jwilke 1.67 WHILE space dup cell+ @ >rname count type @
1365 : jwilke 1.52 REPEAT drop cr
1366 :     ;
1367 :    
1368 :     \ -------- predefined regions
1369 :    
1370 :     0 0 region address-space
1371 :     \ total memory addressed and used by the target system
1372 :    
1373 : jwilke 1.139 0 0 region user-region
1374 :     \ data for user variables goes here
1375 :     \ this has to be defined before dictionary or ram-dictionary
1376 :    
1377 : jwilke 1.52 0 0 region dictionary
1378 :     \ rom area for the compiler
1379 :    
1380 : jwilke 1.53 T has? rom H
1381 : jwilke 1.52 [IF]
1382 :     0 0 region ram-dictionary mirrored
1383 :     \ ram area for the compiler
1384 :     [ELSE]
1385 :     ' dictionary ALIAS ram-dictionary
1386 :     [THEN]
1387 :    
1388 :     0 0 region return-stack
1389 :    
1390 :     0 0 region data-stack
1391 :    
1392 :     0 0 region tib-region
1393 :    
1394 :     ' dictionary ALIAS rom-dictionary
1395 :    
1396 : jwilke 1.139 : setup-region ( region -- )
1397 :     >r
1398 :     \ allocate mem
1399 :     r@ >rlen @ allocatetarget
1400 :     r@ >rmem !
1401 :    
1402 :     r@ >rlen @
1403 :     target>bitmask-size allocatetarget
1404 :     r@ >rbm !
1405 :    
1406 :     r@ >rlen @
1407 :     tcell / 1+ cells allocatetarget r@ >rtype !
1408 :    
1409 :     ['] noop r@ >rtouch !
1410 :     rdrop ;
1411 : jwilke 1.52
1412 : jwilke 1.105 : setup-target ( -- ) \G initialize target's memory space
1413 : jwilke 1.53 s" rom" T $has? H
1414 : jwilke 1.52 IF \ check for ram and rom...
1415 : jwilke 1.72 \ address-space area nip 0<>
1416 : jwilke 1.67 ram-dictionary area nip 0<>
1417 :     rom-dictionary area nip 0<>
1418 : jwilke 1.72 and 0=
1419 : jwilke 1.52 ABORT" CROSS: define address-space, rom- , ram-dictionary, with rom-support!"
1420 :     THEN
1421 :     address-space area nip
1422 :     IF
1423 :     address-space area
1424 :     ELSE
1425 :     dictionary area
1426 :     THEN
1427 : jwilke 1.67 nip 0=
1428 : jwilke 1.52 ABORT" CROSS: define at least address-space or dictionary!!"
1429 : jwilke 1.67
1430 :     \ allocate target for each region
1431 :     region-link
1432 :     BEGIN @ dup
1433 :     WHILE dup
1434 :     0 >rlink - >r
1435 :     r@ >rlen @
1436 : jwilke 1.139 IF r@ setup-region
1437 :     THEN rdrop
1438 : jwilke 1.72 REPEAT drop ;
1439 :    
1440 : jwilke 1.105 \ MakeKernel 22feb99jaw
1441 : jwilke 1.72
1442 : pazsan 1.134 : makekernel ( start targetsize -- )
1443 : jwilke 1.113 \G convenience word to setup the memory of the target
1444 :     \G used by main.fs of the c-engine based systems
1445 : pazsan 1.134 dictionary (region) setup-target ;
1446 : jwilke 1.72
1447 :     >MINIMAL
1448 :     : makekernel makekernel ;
1449 :     >CROSS
1450 : jwilke 1.52
1451 : pazsan 1.54 \ \ switched tdp for rom support 03jun97jaw
1452 : jwilke 1.52
1453 :     \ second value is here to store some maximal value for statistics
1454 :     \ tempdp is also embedded here but has nothing to do with rom support
1455 :     \ (needs switched dp)
1456 :    
1457 :     variable tempdp 0 , \ temporary dp for resolving
1458 :     variable tempdp-save
1459 :    
1460 :     0 [IF]
1461 :     variable romdp 0 , \ Dictionary-Pointer for ramarea
1462 :     variable ramdp 0 , \ Dictionary-Pointer for romarea
1463 :    
1464 :     \
1465 :     variable sramdp \ start of ram-area for forth
1466 :     variable sromdp \ start of rom-area for forth
1467 :    
1468 :     [THEN]
1469 :    
1470 : jwilke 1.123 0 Value current-region
1471 :     0 Value tdp
1472 :     Variable fixed \ flag: true: no automatic switching
1473 : jwilke 1.52 \ false: switching is done automatically
1474 :    
1475 :     \ Switch-Policy:
1476 :     \
1477 :     \ a header is always compiled into rom
1478 :     \ after a created word (create and variable) compilation goes to ram
1479 :     \
1480 :     \ Be careful: If you want to make the data behind create into rom
1481 :     \ you have to put >rom before create!
1482 :    
1483 :     variable constflag constflag off
1484 :    
1485 : jwilke 1.67 : activate ( region -- )
1486 :     \G next code goes to this region
1487 : jwilke 1.123 dup to current-region >rdp to tdp ;
1488 : jwilke 1.67
1489 : jwilke 1.52 : (switchram)
1490 : jwilke 1.53 fixed @ ?EXIT s" rom" T $has? H 0= ?EXIT
1491 : jwilke 1.67 ram-dictionary activate ;
1492 : jwilke 1.52
1493 :     : switchram
1494 :     constflag @
1495 :     IF constflag off ELSE (switchram) THEN ;
1496 :    
1497 :     : switchrom
1498 : jwilke 1.67 fixed @ ?EXIT rom-dictionary activate ;
1499 : jwilke 1.52
1500 :     : >tempdp ( addr -- )
1501 :     tdp tempdp-save ! tempdp to tdp tdp ! ;
1502 :     : tempdp> ( -- )
1503 :     tempdp-save @ to tdp ;
1504 :    
1505 :     : >ram fixed off (switchram) fixed on ;
1506 :     : >rom fixed off switchrom fixed on ;
1507 :     : >auto fixed off switchrom ;
1508 :    
1509 :    
1510 :    
1511 :     \ : romstart dup sromdp ! romdp ! ;
1512 :     \ : ramstart dup sramdp ! ramdp ! ;
1513 :    
1514 : jwilke 1.67 \ default compilation goes to rom
1515 : jwilke 1.52 \ when romable support is off, only the rom switch is used (!!)
1516 :     >auto
1517 :    
1518 : anton 1.48 : there tdp @ ;
1519 :    
1520 : jwilke 1.52 >TARGET
1521 : anton 1.48
1522 : jwilke 1.52 \ \ Target Memory Handling
1523 : anton 1.1
1524 :     \ Byte ordering and cell size 06oct92py
1525 :    
1526 : pazsan 1.19 : cell+ tcell + ;
1527 :     : cells tcell<< lshift ;
1528 : jwilke 1.71 : chars tchar * ;
1529 :     : char+ tchar + ;
1530 : pazsan 1.19 : floats tfloat * ;
1531 : anton 1.6
1532 : anton 1.1 >CROSS
1533 : pazsan 1.19 : cell/ tcell<< rshift ;
1534 : anton 1.1 >TARGET
1535 :     20 CONSTANT bl
1536 : jwilke 1.52 \ TNIL Constant NIL
1537 : anton 1.1
1538 :     >CROSS
1539 :    
1540 : pazsan 1.20 bigendian
1541 :     [IF]
1542 : jwilke 1.106 : DS! ( d addr -- ) tcell bounds swap 1-
1543 : pazsan 1.20 DO maxbyte ud/mod rot I c! -1 +LOOP 2drop ;
1544 : jwilke 1.106 : DS@ ( addr -- d ) >r 0 0 r> tcell bounds
1545 :     DO maxbyte * swap maxbyte um* rot + swap I c@ + swap LOOP ;
1546 : pazsan 1.57 : Sc! ( n addr -- ) >r s>d r> tchar bounds swap 1-
1547 :     DO maxbyte ud/mod rot I c! -1 +LOOP 2drop ;
1548 :     : Sc@ ( addr -- n ) >r 0 0 r> tchar bounds
1549 :     DO maxbyte * swap maxbyte um* rot + swap I c@ + swap LOOP d>s ;
1550 : pazsan 1.19 [ELSE]
1551 : jwilke 1.106 : DS! ( d addr -- ) tcell bounds
1552 : pazsan 1.20 DO maxbyte ud/mod rot I c! LOOP 2drop ;
1553 : jwilke 1.106 : DS@ ( addr -- n ) >r 0 0 r> tcell bounds swap 1-
1554 :     DO maxbyte * swap maxbyte um* rot + swap I c@ + swap -1 +LOOP ;
1555 : pazsan 1.57 : Sc! ( n addr -- ) >r s>d r> tchar bounds
1556 :     DO maxbyte ud/mod rot I c! LOOP 2drop ;
1557 :     : Sc@ ( addr -- n ) >r 0 0 r> tchar bounds swap 1-
1558 :     DO maxbyte * swap maxbyte um* rot + swap I c@ + swap -1 +LOOP d>s ;
1559 : anton 1.1 [THEN]
1560 :    
1561 : jwilke 1.106 : S! ( n addr -- ) >r s>d r> DS! ;
1562 :     : S@ ( addr -- n ) DS@ d>s ;
1563 :    
1564 : jwilke 1.67 : taddr>region ( taddr -- region | 0 )
1565 :     \G finds for a target-address the correct region
1566 :     \G returns 0 if taddr is not in range of a target memory region
1567 :     region-link
1568 :     BEGIN @ dup
1569 :     WHILE dup >r
1570 :     0 >rlink - >r
1571 :     r@ >rlen @
1572 :     IF dup r@ borders within
1573 : jwilke 1.139 IF r> r> drop nip
1574 :     dup >rtouch @ EXECUTE EXIT
1575 :     THEN
1576 : jwilke 1.67 THEN
1577 :     r> drop
1578 :     r>
1579 :     REPEAT
1580 :     2drop 0 ;
1581 :    
1582 : jwilke 1.103 : taddr>region-abort ( taddr -- region | 0 )
1583 : jwilke 1.122 \G Same as taddr>region but aborts if taddr is not
1584 :     \G a valid address in the target address space
1585 : jwilke 1.103 dup taddr>region dup 0=
1586 :     IF drop cr ." Wrong address: " .addr
1587 :     -1 ABORT" Address out of range!"
1588 :     THEN nip ;
1589 :    
1590 : jwilke 1.67 : (>regionimage) ( taddr -- 'taddr )
1591 :     dup
1592 :     \ find region we want to address
1593 : jwilke 1.103 taddr>region-abort
1594 : jwilke 1.67 >r
1595 :     \ calculate offset in region
1596 :     r@ >rstart @ -
1597 :     \ add regions real address in our memory
1598 :     r> >rmem @ + ;
1599 :    
1600 : jwilke 1.124 : (>regionramimage) ( taddr -- 'taddr )
1601 :     \G same as (>regionimage) but aborts if the region is rom
1602 :     dup
1603 :     \ find region we want to address
1604 :     taddr>region-abort
1605 :     >r
1606 :     r@ >rrom @ ABORT" CROSS: region is write-protected!"
1607 :     \ calculate offset in region
1608 :     r@ >rstart @ -
1609 :     \ add regions real address in our memory
1610 :     r> >rmem @ + ;
1611 :    
1612 : jwilke 1.103 : (>regionbm) ( taddr -- 'taddr bitmaskbaseaddr )
1613 :     dup
1614 :     \ find region we want to address
1615 :     taddr>region-abort
1616 :     >r
1617 :     \ calculate offset in region
1618 :     r@ >rstart @ -
1619 :     \ add regions real address in our memory
1620 :     r> >rbm @ ;
1621 :    
1622 :     : (>regiontype) ( taddr -- 'taddr )
1623 :     dup
1624 :     \ find region we want to address
1625 :     taddr>region-abort
1626 :     >r
1627 :     \ calculate offset in region
1628 :     r@ >rstart @ - tcell / cells
1629 :     \ add regions real address in our memory
1630 :     r> >rtype @ + ;
1631 :    
1632 : anton 1.1 \ Bit string manipulation 06oct92py
1633 :     \ 9may93jaw
1634 :     CREATE Bittable 80 c, 40 c, 20 c, 10 c, 8 c, 4 c, 2 c, 1 c,
1635 :     : bits ( n -- n ) chars Bittable + c@ ;
1636 :    
1637 :     : >bit ( addr n -- c-addr mask ) 8 /mod rot + swap bits ;
1638 :     : +bit ( addr n -- ) >bit over c@ or swap c! ;
1639 : pazsan 1.4 : -bit ( addr n -- ) >bit invert over c@ and swap c! ;
1640 : jwilke 1.67
1641 : jwilke 1.103 : @relbit ( taddr -- f ) (>regionbm) swap cell/ >bit swap c@ and ;
1642 :    
1643 : jwilke 1.84 : (relon) ( taddr -- )
1644 :     [ [IFDEF] fd-relocation-table ]
1645 :     s" +" fd-relocation-table write-file throw
1646 :     dup s>d <# #s #> fd-relocation-table write-line throw
1647 :     [ [THEN] ]
1648 : jwilke 1.103 (>regionbm) swap cell/ +bit ;
1649 : jwilke 1.84
1650 :     : (reloff) ( taddr -- )
1651 :     [ [IFDEF] fd-relocation-table ]
1652 :     s" -" fd-relocation-table write-file throw
1653 :     dup s>d <# #s #> fd-relocation-table write-line throw
1654 :     [ [THEN] ]
1655 : jwilke 1.103 (>regionbm) swap cell/ -bit ;
1656 : jwilke 1.67
1657 :     DEFER >image
1658 : jwilke 1.124 DEFER >ramimage
1659 : jwilke 1.67 DEFER relon
1660 :     DEFER reloff
1661 :     DEFER correcter
1662 :    
1663 :     T has? relocate H
1664 :     [IF]
1665 :     ' (relon) IS relon
1666 :     ' (reloff) IS reloff
1667 : jwilke 1.103 ' (>regionimage) IS >image
1668 : jwilke 1.124 ' (>regionimage) IS >ramimage
1669 : jwilke 1.67 [ELSE]
1670 :     ' drop IS relon
1671 :     ' drop IS reloff
1672 : jwilke 1.68 ' (>regionimage) IS >image
1673 : jwilke 1.124 ' (>regionimage) IS >ramimage
1674 : jwilke 1.67 [THEN]
1675 : anton 1.1
1676 : jwilke 1.124 : enforce-writeprotection ( -- )
1677 :     ['] (>regionramimage) IS >ramimage ;
1678 :    
1679 :     : relax-writeprotection ( -- )
1680 :     ['] (>regionimage) IS >ramimage ;
1681 :    
1682 :     : writeprotection-relaxed? ( -- )
1683 :     ['] >ramimage >body @ ['] (>regionimage) = ;
1684 :    
1685 : anton 1.1 \ Target memory access 06oct92py
1686 :    
1687 :     : align+ ( taddr -- rest )
1688 : anton 1.48 tcell tuck 1- and - [ tcell 1- ] Literal and ;
1689 : anton 1.22 : cfalign+ ( taddr -- rest )
1690 : pazsan 1.39 \ see kernel.fs:cfaligned
1691 : pazsan 1.43 /maxalign tuck 1- and - [ /maxalign 1- ] Literal and ;
1692 : anton 1.1
1693 :     >TARGET
1694 :     : aligned ( taddr -- ta-addr ) dup align+ + ;
1695 :     \ assumes cell alignment granularity (as GNU C)
1696 :    
1697 : anton 1.22 : cfaligned ( taddr1 -- taddr2 )
1698 : pazsan 1.39 \ see kernel.fs
1699 : anton 1.22 dup cfalign+ + ;
1700 :    
1701 : jwilke 1.52 : @ ( taddr -- w ) >image S@ ;
1702 : jwilke 1.124 : ! ( w taddr -- ) >ramimage S! ;
1703 : pazsan 1.57 : c@ ( taddr -- char ) >image Sc@ ;
1704 : jwilke 1.124 : c! ( char taddr -- ) >ramimage Sc! ;
1705 : anton 1.7 : 2@ ( taddr -- x1 x2 ) T dup cell+ @ swap @ H ;
1706 : pazsan 1.82 : 2! ( x1 x2 taddr -- ) T tuck ! cell+ ! H ;
1707 : anton 1.1
1708 :     \ Target compilation primitives 06oct92py
1709 :     \ included A! 16may93jaw
1710 :    
1711 :     : here ( -- there ) there ;
1712 :     : allot ( n -- ) tdp +! ;
1713 : jwilke 1.78 : , ( w -- ) T here H tcell T allot ! H ;
1714 :     : c, ( char -- ) T here H tchar T allot c! H ;
1715 :     : align ( -- ) T here H align+ 0 ?DO bl T c, H tchar +LOOP ;
1716 : anton 1.22 : cfalign ( -- )
1717 : jwilke 1.78 T here H cfalign+ 0 ?DO bl T c, H tchar +LOOP ;
1718 : anton 1.1
1719 : jwilke 1.71 : >address dup 0>= IF tbyte / THEN ; \ ?? jaw
1720 : pazsan 1.59 : A! swap >address swap dup relon T ! H ;
1721 :     : A, ( w -- ) >address T here H relon T , H ;
1722 : anton 1.1
1723 : pazsan 1.118 \ high-level ghosts
1724 :    
1725 :     >CROSS
1726 :    
1727 :     Ghost (do) Ghost (?do) 2drop
1728 :     Ghost (for) drop
1729 :     Ghost (loop) Ghost (+loop) 2drop
1730 :     Ghost (next) drop
1731 : pazsan 1.170 Ghost !does drop
1732 : anton 1.146 Ghost compile, drop
1733 : pazsan 1.118 Ghost (.") Ghost (S") Ghost (ABORT") 2drop drop
1734 : pazsan 1.130 Ghost (C") Ghost c(abort") Ghost type 2drop drop
1735 : pazsan 1.118 Ghost ' drop
1736 :    
1737 :     \ user ghosts
1738 :    
1739 :     Ghost state drop
1740 : jwilke 1.103
1741 :     \ \ -------------------- Host/Target copy etc. 29aug01jaw
1742 :    
1743 :    
1744 :     >CROSS
1745 :    
1746 : jwilke 1.107 : TD! >image DS! ;
1747 :     : TD@ >image DS@ ;
1748 :    
1749 : jwilke 1.103 : th-count ( taddr -- host-addr len )
1750 :     \G returns host address of target string
1751 :     assert1( tbyte 1 = )
1752 :     dup X c@ swap X char+ >image swap ;
1753 :    
1754 :     : ht-move ( haddr taddr len -- )
1755 :     \G moves data from host-addr to destination in target-addr
1756 :     \G character by character
1757 :     swap -rot bounds ?DO I c@ over X c! X char+ LOOP drop ;
1758 :    
1759 :     2Variable last-string
1760 : pazsan 1.157 X has? rom [IF] $60 [ELSE] $00 [THEN] Constant header-masks
1761 : jwilke 1.103
1762 : pazsan 1.157 : ht-header, ( addr count -- )
1763 :     dup there swap last-string 2!
1764 :     dup header-masks or T c, H bounds ?DO I c@ T c, H LOOP ;
1765 : jwilke 1.103 : ht-string, ( addr count -- )
1766 :     dup there swap last-string 2!
1767 : pazsan 1.130 dup T c, H bounds ?DO I c@ T c, H LOOP ;
1768 :     : ht-mem, ( addr count )
1769 :     bounds ?DO I c@ T c, H LOOP ;
1770 : jwilke 1.103
1771 :     >TARGET
1772 :    
1773 :     : count dup X c@ swap X char+ swap ;
1774 : jwilke 1.110
1775 : pazsan 1.149 : on >r -1 -1 r> TD! ;
1776 : jwilke 1.103 : off T 0 swap ! H ;
1777 : anton 1.1
1778 : jwilke 1.107 : tcmove ( source dest len -- )
1779 :     \G cmove in target memory
1780 :     tchar * bounds
1781 :     ?DO dup T c@ H I T c! H 1+
1782 :     tchar +LOOP drop ;
1783 :    
1784 : pazsan 1.154 : tcallot ( char size -- )
1785 :     0 ?DO dup T c, H tchar +LOOP drop ;
1786 :    
1787 : jwilke 1.107 : td, ( d -- )
1788 :     \G Store a host value as one cell into the target
1789 :     there tcell X allot TD! ;
1790 :    
1791 :     \ \ Load Assembler
1792 :    
1793 :     >TARGET
1794 :     H also Forth definitions
1795 :    
1796 :     \ FIXME: should we include the assembler really in the forth
1797 :     \ dictionary?!?!?!? This conflicts with the existing assembler
1798 :     \ of the host forth system!!
1799 :     [IFDEF] asm-include asm-include [THEN] hex
1800 :    
1801 :     previous
1802 :    
1803 :    
1804 : jwilke 1.52 >CROSS
1805 :    
1806 : jwilke 1.103 : (cc) T a, H ; ' (cc) plugin-of colon,
1807 : jwilke 1.105 : (prim) T a, H ; ' (prim) plugin-of prim,
1808 : jwilke 1.52
1809 : pazsan 1.118 : (cr) >tempdp colon, tempdp> ; ' (cr) plugin-of colon-resolve
1810 : jwilke 1.103 : (ar) T ! H ; ' (ar) plugin-of addr-resolve
1811 : pazsan 1.54 : (dr) ( ghost res-pnt target-addr addr )
1812 :     >tempdp drop over
1813 :     dup >magic @ <do:> =
1814 :     IF doer,
1815 :     ELSE dodoes,
1816 :     THEN
1817 : jwilke 1.103 tempdp> ; ' (dr) plugin-of doer-resolve
1818 : pazsan 1.54
1819 :     : (cm) ( -- addr )
1820 : jwilke 1.121 there -1 colon, ; ' (cm) plugin-of colonmark,
1821 : anton 1.1
1822 : jwilke 1.52 >TARGET
1823 : jwilke 1.105 : compile, ( xt -- )
1824 : jwilke 1.107 dup xt>ghost >comp @ EXECUTE ;
1825 : jwilke 1.52 >CROSS
1826 : anton 1.1
1827 : jwilke 1.102 \ resolve structure
1828 :    
1829 :     : >next ; \ link to next field
1830 :     : >tag cell+ ; \ indecates type of reference: 0: call, 1: address, 2: doer
1831 :     : >taddr cell+ cell+ ;
1832 :     : >ghost 3 cells + ;
1833 :     : >file 4 cells + ;
1834 :     : >line 5 cells + ;
1835 :    
1836 :     : (refered) ( ghost addr tag -- )
1837 :     \G creates a reference to ghost at address taddr
1838 : jwilke 1.103 >space
1839 :     rot >link linked
1840 : jwilke 1.102 ( taddr tag ) ,
1841 :     ( taddr ) ,
1842 :     last-header-ghost @ ,
1843 :     loadfile ,
1844 :     sourceline# ,
1845 : jwilke 1.103 space>
1846 : pazsan 1.118 ;
1847 :    
1848 : jwilke 1.52 : refered ( ghost tag -- )
1849 : jwilke 1.53 \G creates a resolve structure
1850 : pazsan 1.54 T here aligned H swap (refered)
1851 :     ;
1852 :    
1853 :     : killref ( addr ghost -- )
1854 :     \G kills a forward reference to ghost at position addr
1855 :     \G this is used to eleminate a :dovar refence after making a DOES>
1856 :     dup >magic @ <fwd> <> IF 2drop EXIT THEN
1857 :     swap >r >link
1858 :     BEGIN dup @ dup ( addr last this )
1859 :     WHILE dup >taddr @ r@ =
1860 :     IF @ over !
1861 :     ELSE nip THEN
1862 :     REPEAT rdrop 2drop
1863 : jwilke 1.53 ;
1864 : anton 1.48
1865 : jwilke 1.52 Defer resolve-warning
1866 : anton 1.1
1867 : jwilke 1.52 : reswarn-test ( ghost res-struct -- ghost res-struct )
1868 : jwilke 1.78 over cr ." Resolving " .ghost dup ." in " >ghost @ .ghost ;
1869 : anton 1.1
1870 : jwilke 1.52 : reswarn-forward ( ghost res-struct -- ghost res-struct )
1871 : jwilke 1.78 over warnhead .ghost dup ." is referenced in "
1872 :     >ghost @ .ghost ;
1873 : anton 1.1
1874 : jwilke 1.52 \ ' reswarn-test IS resolve-warning
1875 :    
1876 : anton 1.1 \ resolve 14oct92py
1877 :    
1878 : pazsan 1.54 : resolve-loop ( ghost resolve-list tcfa -- )
1879 :     >r
1880 :     BEGIN dup WHILE
1881 :     \ dup >tag @ 2 = IF reswarn-forward THEN
1882 :     resolve-warning
1883 :     r@ over >taddr @
1884 :     2 pick >tag @
1885 :     CASE 0 OF colon-resolve ENDOF
1886 :     1 OF addr-resolve ENDOF
1887 :     2 OF doer-resolve ENDOF
1888 :     ENDCASE
1889 :     @ \ next list element
1890 :     REPEAT 2drop rdrop
1891 :     ;
1892 : jwilke 1.52
1893 :     \ : resolve-loop ( ghost tcfa -- ghost tcfa )
1894 :     \ >r dup >link @
1895 :     \ BEGIN dup WHILE dup T @ H r@ rot T ! H REPEAT drop r> ;
1896 : anton 1.1
1897 :     \ exists 9may93jaw
1898 :    
1899 : jwilke 1.103 : exists ( ghost tcfa -- )
1900 :     \G print warning and set new target link in ghost
1901 :     swap exists-warning
1902 :     >link ! ;
1903 : jwilke 1.52
1904 : jwilke 1.105 : colon-resolved ( ghost -- )
1905 : jwilke 1.121 \ compiles a call to a colon definition,
1906 :     \ compile action for >comp field
1907 :     >link @ colon, ;
1908 : jwilke 1.105
1909 :     : prim-resolved ( ghost -- )
1910 : jwilke 1.121 \ compiles a call to a primitive
1911 : jwilke 1.105 >link @ prim, ;
1912 :    
1913 : jwilke 1.121 : (is-forward) ( ghost -- )
1914 :     colonmark, 0 (refered) ; \ compile space for call
1915 :     ' (is-forward) IS is-forward
1916 :    
1917 : pazsan 1.118 0 Value resolved
1918 : anton 1.1
1919 : jwilke 1.122 : resolve-forward-references ( ghost resolve-list -- )
1920 :     \ loop through forward referencies
1921 :     comp-state @ >r Resolving comp-state !
1922 :     over >link @ resolve-loop
1923 :     r> comp-state !
1924 :    
1925 :     ['] noop IS resolve-warning ;
1926 :    
1927 :    
1928 :     : (resolve) ( ghost tcfa -- ghost resolve-list )
1929 :     \ check for a valid address, it is a primitive reference
1930 :     \ otherwise
1931 : jwilke 1.103 dup taddr>region 0<> IF
1932 : jwilke 1.122 \ define this address in the region address type table
1933 : jwilke 1.103 2dup (>regiontype) define-addr-struct addr-xt-ghost
1934 :     \ we define new address only if empty
1935 : jwilke 1.105 \ this is for not to take over the alias ghost
1936 :     \ (different ghost, but identical xt)
1937 : jwilke 1.103 \ but the very first that really defines it
1938 :     dup @ 0= IF ! ELSE 2drop THEN
1939 :     THEN
1940 : jwilke 1.122 swap >r
1941 : pazsan 1.118 r@ to resolved
1942 : jwilke 1.121
1943 :     \ r@ >comp @ ['] is-forward =
1944 :     \ ABORT" >comp action not set on a resolved ghost"
1945 :    
1946 :     \ copmile action defaults to colon-resolved
1947 :     \ if this is not right something must be set before
1948 :     \ calling resolve
1949 :     r@ >comp @ ['] is-forward = IF
1950 : jwilke 1.122 ['] colon-resolved r@ >comp !
1951 :     THEN
1952 :     r@ >link @ swap \ ( list tcfa R: ghost )
1953 :     \ mark ghost as resolved
1954 :     r@ >link ! <res> r@ >magic !
1955 :     r> swap ;
1956 : pazsan 1.54
1957 : jwilke 1.122 : resolve ( ghost tcfa -- )
1958 :     \G resolve referencies to ghost with tcfa
1959 :     \ is ghost resolved?, second resolve means another
1960 :     \ definition with the same name
1961 :     over undefined? 0= IF exists EXIT THEN
1962 :     (resolve)
1963 :     ( ghost resolve-list )
1964 :     resolve-forward-references ;
1965 :    
1966 :     : resolve-noforwards ( ghost tcfa -- )
1967 :     \G Same as resolve but complain if there are any
1968 :     \G forward references on this ghost
1969 :     \ is ghost resolved?, second resolve means another
1970 :     \ definition with the same name
1971 :     over undefined? 0= IF exists EXIT THEN
1972 :     (resolve)
1973 :     IF cr ." No forward references allowed on: " .ghost cr
1974 :     -1 ABORT" Illegal forward reference"
1975 :     THEN
1976 :     drop ;
1977 : anton 1.1
1978 :     \ gexecute ghost, 01nov92py
1979 :    
1980 : jwilke 1.103 : (gexecute) ( ghost -- )
1981 : jwilke 1.105 dup >comp @ EXECUTE ;
1982 : jwilke 1.103
1983 :     : gexecute ( ghost -- )
1984 : jwilke 1.120 dup >magic @ <imm> = ABORT" CROSS: gexecute on immediate word"
1985 : jwilke 1.103 (gexecute) ;
1986 : jwilke 1.52
1987 :     : addr, ( ghost -- )
1988 : jwilke 1.105 dup forward? IF 1 refered 0 T a, H ELSE >link @ T a, H THEN ;
1989 : jwilke 1.52
1990 : anton 1.1 \ .unresolved 11may93jaw
1991 :    
1992 :     variable ResolveFlag
1993 :    
1994 :     \ ?touched 11may93jaw
1995 :    
1996 : jwilke 1.52 : ?touched ( ghost -- flag ) dup forward? swap >link @
1997 : anton 1.1 0 <> and ;
1998 :    
1999 : jwilke 1.53 : .forwarddefs ( ghost -- )
2000 : jwilke 1.103 ." appeared in:"
2001 :     >link
2002 :     BEGIN @ dup
2003 :     WHILE cr 5 spaces
2004 :     dup >ghost @ .ghost
2005 :     ." file " dup >file @ ?dup IF count type ELSE ." CON" THEN
2006 :     ." line " dup >line @ .dec
2007 :     REPEAT
2008 :     drop ;
2009 :    
2010 :     : ?resolved ( ghost -- )
2011 :     dup ?touched
2012 :     IF ResolveFlag on
2013 :     dup cr .ghost .forwarddefs
2014 : jwilke 1.53 ELSE drop
2015 :     THEN ;
2016 : anton 1.1
2017 :     : .unresolved ( -- )
2018 :     ResolveFlag off cr ." Unresolved: "
2019 : jwilke 1.103 ghost-list
2020 : anton 1.1 BEGIN @ dup
2021 :     WHILE dup ?resolved
2022 : anton 1.10 REPEAT drop ResolveFlag @
2023 :     IF
2024 : anton 1.48 -1 abort" Unresolved words!"
2025 : anton 1.10 ELSE
2026 :     ." Nothing!"
2027 :     THEN
2028 :     cr ;
2029 : anton 1.1
2030 : jwilke 1.52 : .stats
2031 :     base @ >r decimal
2032 :     cr ." named Headers: " headers-named @ .
2033 :     r> base ! ;
2034 :    
2035 : jwilke 1.79 >MINIMAL
2036 :    
2037 :     : .unresolved .unresolved ;
2038 :    
2039 : anton 1.1 >CROSS
2040 :     \ Header states 12dec92py
2041 :    
2042 : jwilke 1.102 \ : flag! ( 8b -- ) tlast @ dup >r T c@ xor r> c! H ;
2043 : pazsan 1.142 X has? f83headerstring bigendian or [IF] 0 [ELSE] tcell 1- [THEN] Constant flag+
2044 : pazsan 1.90 : flag! ( w -- ) tlast @ flag+ + dup >r T c@ xor r> c! H ;
2045 : anton 1.1
2046 :     VARIABLE ^imm
2047 :    
2048 : jwilke 1.105 \ !! should be target wordsize specific
2049 :     $80 constant alias-mask
2050 :     $40 constant immediate-mask
2051 :     $20 constant restrict-mask
2052 :    
2053 : anton 1.1 >TARGET
2054 : jwilke 1.105 : immediate immediate-mask flag!
2055 : pazsan 1.18 ^imm @ @ dup <imm> = IF drop EXIT THEN
2056 : anton 1.1 <res> <> ABORT" CROSS: Cannot immediate a unresolved word"
2057 :     <imm> ^imm @ ! ;
2058 : jwilke 1.105 : restrict restrict-mask flag! ;
2059 : jwilke 1.52
2060 : pazsan 1.54 : isdoer
2061 :     \G define a forth word as doer, this makes obviously only sence on
2062 :     \G forth processors such as the PSC1000
2063 :     <do:> last-header-ghost @ >magic ! ;
2064 : anton 1.1 >CROSS
2065 :    
2066 :     \ Target Header Creation 01nov92py
2067 :    
2068 : jwilke 1.103 : ht-lstring, ( addr count -- )
2069 : jwilke 1.102 dup T , H bounds ?DO I c@ T c, H LOOP ;
2070 :    
2071 : jwilke 1.103 >TARGET
2072 : jwilke 1.139 X has? f83headerstring [IF]
2073 : pazsan 1.157 : name, ( "name" -- ) bl word count ht-header, X cfalign ;
2074 : jwilke 1.139 [ELSE]
2075 : jwilke 1.103 : name, ( "name" -- ) bl word count ht-lstring, X cfalign ;
2076 : jwilke 1.139 [THEN]
2077 : anton 1.1 : view, ( -- ) ( dummy ) ;
2078 : jwilke 1.52 >CROSS
2079 : anton 1.1
2080 : pazsan 1.25 \ Target Document Creation (goes to crossdoc.fd) 05jul95py
2081 :    
2082 : pazsan 1.55 s" ./doc/crossdoc.fd" r/w create-file throw value doc-file-id
2083 : pazsan 1.25 \ contains the file-id of the documentation file
2084 :    
2085 : pazsan 1.40 : T-\G ( -- )
2086 : pazsan 1.25 source >in @ /string doc-file-id write-line throw
2087 : pazsan 1.40 postpone \ ;
2088 : pazsan 1.25
2089 : pazsan 1.39 Variable to-doc to-doc on
2090 : pazsan 1.25
2091 :     : cross-doc-entry ( -- )
2092 :     to-doc @ tlast @ 0<> and \ not an anonymous (i.e. noname) header
2093 :     IF
2094 :     s" " doc-file-id write-line throw
2095 :     s" make-doc " doc-file-id write-file throw
2096 : jwilke 1.102
2097 : jwilke 1.105 Last-Header-Ghost @ >ghostname doc-file-id write-file throw
2098 : pazsan 1.25 >in @
2099 :     [char] ( parse 2drop
2100 :     [char] ) parse doc-file-id write-file throw
2101 :     s" )" doc-file-id write-file throw
2102 :     [char] \ parse 2drop
2103 : pazsan 1.40 T-\G
2104 : pazsan 1.25 >in !
2105 : pazsan 1.39 THEN ;
2106 : pazsan 1.25
2107 : pazsan 1.28 \ Target TAGS creation
2108 :    
2109 : pazsan 1.39 s" kernel.TAGS" r/w create-file throw value tag-file-id
2110 : pazsan 1.116 s" kernel.tags" r/w create-file throw value vi-tag-file-id
2111 : pazsan 1.28 \ contains the file-id of the tags file
2112 :    
2113 : anton 1.137 Create tag-beg 1 c, 7F c,
2114 :     Create tag-end 1 c, 01 c,
2115 : pazsan 1.28 Create tag-bof 1 c, 0C c,
2116 : pazsan 1.116 Create tag-tab 1 c, 09 c,
2117 : pazsan 1.28
2118 :     2variable last-loadfilename 0 0 last-loadfilename 2!
2119 :    
2120 :     : put-load-file-name ( -- )
2121 : jwilke 1.77 sourcefilename last-loadfilename 2@ d<>
2122 : pazsan 1.28 IF
2123 :     tag-bof count tag-file-id write-line throw
2124 : anton 1.31 sourcefilename 2dup
2125 : pazsan 1.28 tag-file-id write-file throw
2126 :     last-loadfilename 2!
2127 :     s" ,0" tag-file-id write-line throw
2128 :     THEN ;
2129 :    
2130 : pazsan 1.116 : cross-gnu-tag-entry ( -- )
2131 : pazsan 1.28 tlast @ 0<> \ not an anonymous (i.e. noname) header
2132 :     IF
2133 :     put-load-file-name
2134 :     source >in @ min tag-file-id write-file throw
2135 :     tag-beg count tag-file-id write-file throw
2136 : pazsan 1.116 Last-Header-Ghost @ >ghostname tag-file-id write-file throw
2137 : pazsan 1.28 tag-end count tag-file-id write-file throw
2138 : anton 1.31 base @ decimal sourceline# 0 <# #s #> tag-file-id write-file throw
2139 : pazsan 1.28 \ >in @ 0 <# #s [char] , hold #> tag-file-id write-line throw
2140 :     s" ,0" tag-file-id write-line throw
2141 :     base !
2142 :     THEN ;
2143 : pazsan 1.116
2144 :     : cross-vi-tag-entry ( -- )
2145 :     tlast @ 0<> \ not an anonymous (i.e. noname) header
2146 :     IF
2147 :     sourcefilename vi-tag-file-id write-file throw
2148 :     tag-tab count vi-tag-file-id write-file throw
2149 :     Last-Header-Ghost @ >ghostname vi-tag-file-id write-file throw
2150 :     tag-tab count vi-tag-file-id write-file throw
2151 :     s" /^" vi-tag-file-id write-file throw
2152 :     source vi-tag-file-id write-file throw
2153 :     s" $/" vi-tag-file-id write-line throw
2154 :     THEN ;
2155 :    
2156 :     : cross-tag-entry ( -- )
2157 :     cross-gnu-tag-entry
2158 :     cross-vi-tag-entry ;
2159 : pazsan 1.28
2160 : pazsan 1.43 \ Check for words
2161 :    
2162 :     Defer skip? ' false IS skip?
2163 :    
2164 : jwilke 1.103 : skipdef ( "name" -- )
2165 : jwilke 1.79 \G skip definition of an undefined word in undef-words and
2166 :     \G all-words mode
2167 : jwilke 1.103 Ghost dup forward?
2168 : jwilke 1.75 IF >magic <skip> swap !
2169 :     ELSE drop THEN ;
2170 :    
2171 : jwilke 1.103 : tdefined? ( "name" -- flag )
2172 :     Ghost undefined? 0= ;
2173 : jwilke 1.75
2174 : jwilke 1.103 : defined2? ( "name" -- flag )
2175 : jwilke 1.75 \G return true for anything else than forward, even for <skip>
2176 :     \G that's what we want
2177 : jwilke 1.103 Ghost forward? 0= ;
2178 : pazsan 1.43
2179 : jwilke 1.103 : forced? ( "name" -- flag )
2180 : jwilke 1.79 \G return ture if it is a foreced skip with defskip
2181 : jwilke 1.103 Ghost >magic @ <skip> = ;
2182 : jwilke 1.79
2183 : pazsan 1.43 : needed? ( -- flag ) \ name
2184 : anton 1.48 \G returns a false flag when
2185 :     \G a word is not defined
2186 :     \G a forward reference exists
2187 :     \G so the definition is not skipped!
2188 :     bl word gfind
2189 : jwilke 1.75 IF dup undefined?
2190 : anton 1.48 nip
2191 :     0=
2192 :     ELSE drop true THEN ;
2193 : pazsan 1.43
2194 : jwilke 1.139 : doer? ( "name" -- 0 | addr ) \ name
2195 :     Ghost dup >magic @ <do:> =
2196 :     IF >link @ ELSE drop 0 THEN ;
2197 : pazsan 1.44
2198 : pazsan 1.43 : skip-defs ( -- )
2199 :     BEGIN refill WHILE source -trailing nip 0= UNTIL THEN ;
2200 :    
2201 : pazsan 1.28 \ Target header creation
2202 :    
2203 : pazsan 1.54 Variable NoHeaderFlag
2204 :     NoHeaderFlag off
2205 : anton 1.1
2206 : pazsan 1.54 : 0.r ( n1 n2 -- )
2207 :     base @ >r hex
2208 :     0 swap <# 0 ?DO # LOOP #> type
2209 :     r> base ! ;
2210 : jwilke 1.84
2211 :     : .sym ( adr len -- )
2212 :     \G escapes / and \ to produce sed output
2213 : jwilke 1.52 bounds
2214 :     DO I c@ dup
2215 : jwilke 1.77 CASE [char] / OF drop ." \/" ENDOF
2216 :     [char] \ OF drop ." \\" ENDOF
2217 : jwilke 1.52 dup OF emit ENDOF
2218 :     ENDCASE
2219 : pazsan 1.54 LOOP ;
2220 : jwilke 1.52
2221 : pazsan 1.147 Defer setup-execution-semantics ' noop IS setup-execution-semantics
2222 : jwilke 1.103 0 Value lastghost
2223 :    
2224 :     : (THeader ( "name" -- ghost )
2225 : pazsan 1.54 \ >in @ bl word count type 2 spaces >in !
2226 :     \ wordheaders will always be compiled to rom
2227 :     switchrom
2228 :     \ build header in target
2229 :     NoHeaderFlag @
2230 :     IF NoHeaderFlag off
2231 :     ELSE
2232 :     T align H view,
2233 : jwilke 1.78 tlast @ dup 0> IF tcell - THEN T A, H there tlast !
2234 : pazsan 1.54 1 headers-named +! \ Statistic
2235 :     >in @ T name, H >in !
2236 :     THEN
2237 :     T cfalign here H tlastcfa !
2238 : jwilke 1.84 \ Old Symbol table sed-script
2239 : pazsan 1.54 \ >in @ cr ." sym:s/CFA=" there 4 0.r ." /" bl word count .sym ." /g" cr >in !
2240 : jwilke 1.103 HeaderGhost
2241 : jwilke 1.84 \ output symbol table to extra file
2242 : jwilke 1.123 dup >ghostname there symentry
2243 : jwilke 1.103 dup Last-Header-Ghost ! dup to lastghost
2244 : pazsan 1.54 dup >magic ^imm ! \ a pointer for immediate
2245 : jwilke 1.105 alias-mask flag!
2246 : jwilke 1.103 cross-doc-entry cross-tag-entry
2247 :     setup-execution-semantics
2248 :     ;
2249 : anton 1.1
2250 : jwilke 1.52 \ this is the resolver information from ":"
2251 :     \ resolving is done by ";"
2252 : jwilke 1.103 Variable ;Resolve 1 cells allot
2253 :    
2254 :     : hereresolve ( ghost -- )
2255 :     there resolve 0 ;Resolve ! ;
2256 : anton 1.1
2257 : pazsan 1.11 : Theader ( "name" -- ghost )
2258 : jwilke 1.103 (THeader dup hereresolve ;
2259 :    
2260 :     Variable aprim-nr -20 aprim-nr !
2261 :    
2262 :     : copy-execution-semantics ( ghost-from ghost-dest -- )
2263 :     >r
2264 :     dup >exec @ r@ >exec !
2265 : pazsan 1.117 dup >comp @ r@ >comp !
2266 : jwilke 1.103 dup >exec2 @ r@ >exec2 !
2267 :     dup >exec-compile @ r@ >exec-compile !
2268 :     dup >ghost-xt @ r@ >ghost-xt !
2269 :     dup >created @ r@ >created !
2270 :     rdrop drop ;
2271 : anton 1.1
2272 :     >TARGET
2273 : jwilke 1.103
2274 : anton 1.1 : Alias ( cfa -- ) \ name
2275 : jwilke 1.103 >in @ skip? IF 2drop EXIT THEN >in !
2276 :     (THeader ( S xt ghost )
2277 : jwilke 1.105 2dup swap xt>ghost swap copy-execution-semantics
2278 :     over resolve T A, H alias-mask flag! ;
2279 : jwilke 1.103
2280 :     Variable last-prim-ghost
2281 :     0 last-prim-ghost !
2282 :    
2283 :     : asmprimname, ( ghost -- : name )
2284 :     dup last-prim-ghost !
2285 :     >r
2286 :     here bl word count string, r@ >asm-name !
2287 :     aprim-nr @ r> >asm-dummyaddr ! ;
2288 :    
2289 :     Defer setup-prim-semantics
2290 :    
2291 : jwilke 1.112 : mapprim ( "forthname" "asmlabel" -- )
2292 : jwilke 1.103 THeader -1 aprim-nr +! aprim-nr @ T A, H
2293 :     asmprimname,
2294 :     setup-prim-semantics ;
2295 :    
2296 : jwilke 1.112 : mapprim: ( "forthname" "asmlabel" -- )
2297 : jwilke 1.103 -1 aprim-nr +! aprim-nr @
2298 : jwilke 1.122 Ghost tuck swap resolve-noforwards <do:> swap tuck >magic !
2299 : jwilke 1.103 asmprimname, ;
2300 :    
2301 : pazsan 1.119 : Doer: ( cfa -- ) \ name
2302 : jwilke 1.103 >in @ skip? IF 2drop EXIT THEN >in !
2303 :     dup 0< s" prims" T $has? H 0= and
2304 :     IF
2305 :     .sourcepos ." needs doer: " >in @ bl word count type >in ! cr
2306 :     THEN
2307 : pazsan 1.119 Ghost
2308 : jwilke 1.122 tuck swap resolve-noforwards <do:> swap >magic ! ;
2309 : pazsan 1.64
2310 :     Variable prim#
2311 :     : first-primitive ( n -- ) prim# ! ;
2312 : pazsan 1.132 : group 0 word drop prim# @ 1- -$200 and prim# ! ;
2313 : pazsan 1.140 : groupadd ( n -- ) drop ;
2314 : pazsan 1.64 : Primitive ( -- ) \ name
2315 : pazsan 1.117 >in @ skip? IF drop EXIT THEN >in !
2316 :     s" prims" T $has? H 0=
2317 : jwilke 1.103 IF
2318 :     .sourcepos ." needs prim: " >in @ bl word count type >in ! cr
2319 :     THEN
2320 :     prim# @ (THeader ( S xt ghost )
2321 : jwilke 1.121 ['] prim-resolved over >comp !
2322 : jwilke 1.105 dup >ghost-flags <primitive> set-flag
2323 : pazsan 1.163 s" EC" T $has? H 0=
2324 :     IF
2325 : pazsan 1.164 over resolve-noforwards T A, H
2326 : pazsan 1.163 alias-mask flag!
2327 : pazsan 1.164 ELSE
2328 :     T here H resolve-noforwards T A, H
2329 : pazsan 1.163 THEN
2330 : jwilke 1.103 -1 prim# +! ;
2331 : anton 1.1 >CROSS
2332 :    
2333 :     \ Conditionals and Comments 11may93jaw
2334 :    
2335 : jwilke 1.103 \G saves the existing cond action, this is used for redefining in
2336 :     \G instant
2337 :     Variable cond-xt-old
2338 :    
2339 :     : cond-target ( -- )
2340 :     \G Compiles semantic of redefined cond into new one
2341 :     cond-xt-old @ compile, ; immediate restrict
2342 :    
2343 : anton 1.1 : ;Cond
2344 :     postpone ;
2345 :     swap ! ; immediate
2346 :    
2347 : jwilke 1.103 : Cond: ( "name" -- )
2348 :     \g defines a conditional or another word that must
2349 :     \g be executed directly while compiling
2350 :     \g these words have no interpretative semantics by default
2351 :     Ghost
2352 :     >exec-compile
2353 :     dup @ cond-xt-old !
2354 : anton 1.1 :NONAME ;
2355 :    
2356 :    
2357 :     : Comment ( -- )
2358 : jwilke 1.103 >in @ Ghost swap >in ! ' swap
2359 :     2dup >exec-compile ! >exec ! ;
2360 : anton 1.1
2361 :     Comment ( Comment \
2362 :    
2363 :     \ compile 10may93jaw
2364 :    
2365 : jwilke 1.103 : compile ( "name" -- ) \ name
2366 : jwilke 1.115 findghost
2367 : jwilke 1.103 dup >exec-compile @ ?dup
2368 :     IF nip compile,
2369 :     ELSE postpone literal postpone gexecute THEN ; immediate restrict
2370 :    
2371 : jwilke 1.52 >TARGET
2372 :    
2373 : jwilke 1.105 : ' ( -- xt )
2374 :     \G returns the target-cfa of a ghost
2375 : jwilke 1.52 bl word gfind 0= ABORT" CROSS: Ghost don't exists"
2376 : jwilke 1.105 g>xt ;
2377 : jwilke 1.52
2378 : jwilke 1.103 \ FIXME: this works for the current use cases, but is not
2379 :     \ in all cases correct ;-)
2380 :     : comp' X ' 0 ;
2381 :    
2382 : jwilke 1.52 Cond: ['] T ' H alit, ;Cond
2383 :    
2384 :     >CROSS
2385 :    
2386 :     : [T']
2387 :     \ returns the target-cfa of a ghost, or compiles it as literal
2388 : jwilke 1.115 postpone [G']
2389 :     state @ IF postpone g>xt ELSE g>xt THEN ; immediate
2390 : pazsan 1.42
2391 : jwilke 1.52 \ \ threading modell 13dec92py
2392 :     \ modularized 14jun97jaw
2393 :    
2394 : jwilke 1.106 T 2 cells H Value xt>body
2395 : jwilke 1.52
2396 : jwilke 1.103 : (>body) ( cfa -- pfa )
2397 : jwilke 1.105 xt>body + ; ' (>body) plugin-of t>body
2398 :    
2399 :     : fillcfa ( usedcells -- )
2400 : jwilke 1.106 T cells H xt>body swap -
2401 : jwilke 1.105 assert1( dup 0 >= )
2402 :     0 ?DO 0 X c, tchar +LOOP ;
2403 : jwilke 1.52
2404 : jwilke 1.103 : (doer,) ( ghost -- )
2405 : jwilke 1.105 addr, 1 fillcfa ; ' (doer,) plugin-of doer,
2406 : jwilke 1.52
2407 : jwilke 1.105 : (docol,) ( -- ) [G'] :docol (doer,) ; ' (docol,) plugin-of docol,
2408 : jwilke 1.52
2409 : jwilke 1.121 ' NOOP plugin-of ca>native
2410 :    
2411 : jwilke 1.52 : (doprim,) ( -- )
2412 : jwilke 1.105 there xt>body + ca>native T a, H 1 fillcfa ; ' (doprim,) plugin-of doprim,
2413 : jwilke 1.52
2414 :     : (doeshandler,) ( -- )
2415 : pazsan 1.170 T cfalign H ; ' (doeshandler,) plugin-of doeshandler,
2416 : jwilke 1.52
2417 :     : (dodoes,) ( does-action-ghost -- )
2418 : pazsan 1.119 ]comp [G'] :dodoes addr, comp[
2419 : jwilke 1.52 addr,
2420 : jwilke 1.103 \ the relocator in the c engine, does not like the
2421 :     \ does-address to marked for relocation
2422 :     [ T e? ec H 0= [IF] ] T here H tcell - reloff [ [THEN] ]
2423 : jwilke 1.105 2 fillcfa ; ' (dodoes,) plugin-of dodoes,
2424 : jwilke 1.52
2425 : jwilke 1.107 : (dlit,) ( n -- ) compile lit td, ; ' (dlit,) plugin-of dlit,
2426 :    
2427 :     : (lit,) ( n -- ) s>d dlit, ; ' (lit,) plugin-of lit,
2428 : jwilke 1.52
2429 : jwilke 1.67 \ if we dont produce relocatable code alit, defaults to lit, jaw
2430 : jwilke 1.84 \ this is just for convenience, so we don't have to define alit,
2431 :     \ seperately for embedded systems....
2432 :     T has? relocate H
2433 : jwilke 1.67 [IF]
2434 : jwilke 1.105 : (alit,) ( n -- ) compile lit T a, H ; ' (alit,) plugin-of alit,
2435 : jwilke 1.67 [ELSE]
2436 : jwilke 1.105 : (alit,) ( n -- ) lit, ; ' (alit,) plugin-of alit,
2437 : jwilke 1.67 [THEN]
2438 : jwilke 1.52
2439 : jwilke 1.105 : (fini,) compile ;s ; ' (fini,) plugin-of fini,
2440 : pazsan 1.42
2441 : pazsan 1.43 [IFUNDEF] (code)
2442 :     Defer (code)
2443 :     Defer (end-code)
2444 :     [THEN]
2445 :    
2446 : anton 1.1 >TARGET
2447 : pazsan 1.43 : Code
2448 : jwilke 1.52 defempty?
2449 : jwilke 1.121 (THeader ( ghost )
2450 :     ['] prim-resolved over >comp !
2451 : jwilke 1.122 there resolve-noforwards
2452 : jwilke 1.121
2453 : jwilke 1.53 [ T e? prims H 0= [IF] T e? ITC H [ELSE] true [THEN] ] [IF]
2454 : jwilke 1.52 doprim,
2455 : anton 1.48 [THEN]
2456 :     depth (code) ;
2457 : pazsan 1.43
2458 : jwilke 1.121 \ FIXME : no-compile -1 ABORT" this ghost is not for compilation" ;
2459 :    
2460 : pazsan 1.43 : Code:
2461 : jwilke 1.52 defempty?
2462 : jwilke 1.121 Ghost >r
2463 : jwilke 1.123 r@ >ghostname there symentry
2464 : jwilke 1.122 r@ there ca>native resolve-noforwards
2465 : jwilke 1.121 <do:> r@ >magic !
2466 :     r> drop
2467 : pazsan 1.43 depth (code) ;
2468 :    
2469 :     : end-code
2470 : jwilke 1.52 (end-code)
2471 : pazsan 1.43 depth ?dup IF 1- <> ABORT" CROSS: Stack changed"
2472 :     ELSE true ABORT" CROSS: Stack empty" THEN
2473 : jwilke 1.52 ;
2474 : anton 1.14
2475 : anton 1.1 >CROSS
2476 : jwilke 1.52
2477 : anton 1.1 \ tLiteral 12dec92py
2478 :    
2479 :     >TARGET
2480 : pazsan 1.40 Cond: \G T-\G ;Cond
2481 :    
2482 : jwilke 1.105 Cond: Literal ( n -- ) lit, ;Cond
2483 : jwilke 1.103 Cond: ALiteral ( n -- ) alit, ;Cond
2484 : anton 1.1
2485 :     : Char ( "<char>" -- ) bl word char+ c@ ;
2486 : jwilke 1.103 Cond: [Char] ( "<char>" -- ) Char lit, ;Cond
2487 :    
2488 : jwilke 1.139 : (x#) ( adr len base -- )
2489 :     base @ >r base ! 0 0 name >number 2drop drop r> base ! ;
2490 :    
2491 :     : d# $0a (x#) ;
2492 :     : h# $010 (x#) ;
2493 :    
2494 :     Cond: d# $0a (x#) lit, ;Cond
2495 :     Cond: h# $010 (x#) lit, ;Cond
2496 :    
2497 : jwilke 1.104 tchar 1 = [IF]
2498 : jwilke 1.110 Cond: chars ;Cond
2499 : jwilke 1.104 [THEN]
2500 : anton 1.1
2501 : pazsan 1.43 \ some special literals 27jan97jaw
2502 :    
2503 :     Cond: MAXU
2504 : jwilke 1.125 -1 s>d dlit,
2505 : jwilke 1.52 ;Cond
2506 : pazsan 1.43
2507 : jwilke 1.125 tcell 2 = tcell 4 = or tcell 8 = or 0=
2508 :     [IF]
2509 :     .( Warning: MINI and MAXI may not work with this host) cr
2510 :     [THEN]
2511 :    
2512 : pazsan 1.43 Cond: MINI
2513 : jwilke 1.125 tcell 2 = IF $8000 ELSE $80000000 THEN 0
2514 :     tcell 8 = IF swap THEN dlit,
2515 : jwilke 1.52 ;Cond
2516 : pazsan 1.43
2517 :     Cond: MAXI
2518 : jwilke 1.125 tcell 2 = IF $7fff ELSE $7fffffff THEN 0
2519 :     tcell 8 = IF drop -1 swap THEN dlit,
2520 :     ;Cond
2521 : pazsan 1.43
2522 : anton 1.1 >CROSS
2523 : jwilke 1.115
2524 : anton 1.1 \ Target compiling loop 12dec92py
2525 :     \ ">tib trick thrown out 10may93jaw
2526 :     \ number? defined at the top 11may93jaw
2527 : jwilke 1.77 \ replaced >in by save-input
2528 :    
2529 :     : discard 0 ?DO drop LOOP ;
2530 : anton 1.1
2531 :     \ compiled word might leave items on stack!
2532 : jwilke 1.77 : tcom ( x1 .. xn n name -- )
2533 :     \ dup count type space
2534 : jwilke 1.103 gfind
2535 :     IF >r ( discard saved input state ) discard r>
2536 :     dup >exec-compile @ ?dup
2537 :     IF nip execute-exec-compile ELSE gexecute THEN
2538 : jwilke 1.77 EXIT
2539 :     THEN
2540 : anton 1.167 (number?) dup
2541 : jwilke 1.77 IF 0> IF swap lit, THEN lit, discard
2542 : jwilke 1.103 ELSE 2drop restore-input throw Ghost gexecute THEN ;
2543 : anton 1.1
2544 :     \ : ; DOES> 13dec92py
2545 :     \ ] 9may93py/jaw
2546 :    
2547 : jwilke 1.115 >CROSS
2548 :    
2549 : jwilke 1.105 : compiling-state ( -- )
2550 :     \G set states to compililng
2551 : pazsan 1.54 Compiling comp-state !
2552 : jwilke 1.103 \ if we have a state in target, change it with the compile state
2553 : jwilke 1.105 [G'] state dup undefined? 0=
2554 :     IF >ghost-xt @ execute X on ELSE drop THEN ;
2555 :    
2556 :     : interpreting-state ( -- )
2557 :     \G set states to interpreting
2558 :     \ if target has a state variable, change it according to our state
2559 :     [G'] state dup undefined? 0=
2560 :     IF >ghost-xt @ execute X off ELSE drop THEN
2561 :     Interpreting comp-state ! ;
2562 : jwilke 1.115
2563 :     >TARGET
2564 : jwilke 1.105
2565 :     : ]
2566 :     compiling-state
2567 : anton 1.1 BEGIN
2568 : jwilke 1.77 BEGIN save-input bl word
2569 :     dup c@ 0= WHILE drop discard refill 0=
2570 : anton 1.1 ABORT" CROSS: End of file while target compiling"
2571 :     REPEAT
2572 :     tcom
2573 : jwilke 1.103 compiling? 0=
2574 : anton 1.1 UNTIL ;
2575 :    
2576 :     \ by the way: defining a second interpreter (a compiler-)loop
2577 :     \ is not allowed if a system should be ans conform
2578 :    
2579 : jwilke 1.110 : (:) ( ghost -- )
2580 :     \ common factor of : and :noname. Prepare ;Resolve and start definition
2581 :     ;Resolve ! there ;Resolve cell+ !
2582 :     docol, ]comp colon-start depth T ] H ;
2583 :    
2584 : anton 1.1 : : ( -- colon-sys ) \ Name
2585 : jwilke 1.52 defempty?
2586 :     constflag off \ don't let this flag work over colon defs
2587 :     \ just to go sure nothing unwanted happens
2588 : pazsan 1.43 >in @ skip? IF drop skip-defs EXIT THEN >in !
2589 : jwilke 1.110 (THeader (:) ;
2590 : anton 1.1
2591 : pazsan 1.37 : :noname ( -- colon-sys )
2592 : pazsan 1.155 switchrom X cfalign there
2593 : jwilke 1.110 \ define a nameless ghost
2594 :     here ghostheader dup last-header-ghost ! dup to lastghost
2595 :     (:) ;
2596 : pazsan 1.37
2597 : jwilke 1.103 Cond: EXIT ( -- ) compile ;S ;Cond
2598 : anton 1.6
2599 :     Cond: ?EXIT ( -- ) 1 abort" CROSS: using ?exit" ;Cond
2600 : pazsan 1.2
2601 : jwilke 1.52 >CROSS
2602 :     : LastXT ;Resolve @ 0= abort" CROSS: no definition for LastXT"
2603 :     ;Resolve cell+ @ ;
2604 :    
2605 :     >TARGET
2606 :    
2607 : jwilke 1.103 Cond: recurse ( -- ) Last-Header-Ghost @ gexecute ;Cond
2608 : jwilke 1.52
2609 : jwilke 1.103 Cond: ; ( -- )
2610 : jwilke 1.105 depth ?dup
2611 :     IF 1- <> ABORT" CROSS: Stack changed"
2612 :     ELSE true ABORT" CROSS: Stack empty"
2613 :     THEN
2614 :     colon-end
2615 :     fini,
2616 :     comp[
2617 :     ;Resolve @
2618 : pazsan 1.118 IF ['] colon-resolved ;Resolve @ >comp !
2619 :     ;Resolve @ ;Resolve cell+ @ resolve
2620 : jwilke 1.105 THEN
2621 :     interpreting-state
2622 :     ;Cond
2623 :    
2624 :     Cond: [ ( -- ) interpreting-state ;Cond
2625 : anton 1.1
2626 :     >CROSS
2627 : pazsan 1.54
2628 : pazsan 1.117 0 Value created
2629 : pazsan 1.54
2630 : jwilke 1.52 : !does ( does-action -- )
2631 : pazsan 1.54 tlastcfa @ [G'] :dovar killref
2632 : pazsan 1.117 >space here >r ghostheader space>
2633 : jwilke 1.121 ['] colon-resolved r@ >comp !
2634 : pazsan 1.117 r@ created >do:ghost ! r@ swap resolve
2635 :     r> tlastcfa @ >tempdp dodoes, tempdp> ;
2636 : jwilke 1.103
2637 : pazsan 1.147 Defer instant-interpret-does>-hook ' noop IS instant-interpret-does>-hook
2638 : jwilke 1.103
2639 : pazsan 1.162 T has? primcentric H [IF]
2640 : pazsan 1.118 : does-resolved ( ghost -- )
2641 :     compile does-exec g>xt T a, H ;
2642 : pazsan 1.134 [ELSE]
2643 :     : does-resolved ( ghost -- )
2644 :     g>xt T a, H ;
2645 :     [THEN]
2646 : pazsan 1.118
2647 : jwilke 1.103 : resolve-does>-part ( -- )
2648 :     \ resolve words made by builders
2649 : jwilke 1.105 Last-Header-Ghost @ >do:ghost @ ?dup
2650 : pazsan 1.117 IF there resolve THEN ;
2651 : jwilke 1.103
2652 : anton 1.1 >TARGET
2653 : jwilke 1.103 Cond: DOES>
2654 : pazsan 1.162 T here H [ T has? primcentric H [IF] ] 5 [ [ELSE] ] 4 [ [THEN] ] T cells
2655 : pazsan 1.170 H + alit, compile !does compile ;s
2656 : pazsan 1.144 doeshandler, resolve-does>-part
2657 : anton 1.1 ;Cond
2658 : jwilke 1.103
2659 : pazsan 1.118 : DOES>
2660 :     ['] does-resolved created >comp !
2661 :     switchrom doeshandler, T here H !does
2662 :     instant-interpret-does>-hook
2663 :     depth T ] H ;
2664 : anton 1.1
2665 :     >CROSS
2666 : jwilke 1.108 \ Creation 01nov92py
2667 : anton 1.1
2668 :     \ Builder 11may93jaw
2669 :    
2670 : jwilke 1.108 0 Value built
2671 :    
2672 : jwilke 1.105 : Builder ( Create-xt do-ghost "name" -- )
2673 : jwilke 1.52 \ builds up a builder in current vocabulary
2674 :     \ create-xt is executed when word is interpreted
2675 : jwilke 1.106 \ do:-xt is executed when the created word from builder is executed
2676 : jwilke 1.105 \ for do:-xt an additional entry after the normal ghost-entrys is used
2677 : jwilke 1.52
2678 : jwilke 1.109 ghost to built
2679 : jwilke 1.108 built >created @ 0= IF
2680 :     built >created on
2681 :     THEN ;
2682 : anton 1.1
2683 : jwilke 1.52 : gdoes, ( ghost -- )
2684 :     \ makes the codefield for a word that is built
2685 : jwilke 1.103 >do:ghost @ dup undefined? 0=
2686 : jwilke 1.52 IF
2687 : pazsan 1.42 dup >magic @ <do:> =
2688 : pazsan 1.54 IF doer,
2689 :     ELSE dodoes,
2690 :     THEN
2691 :     EXIT
2692 : jwilke 1.52 THEN
2693 :     \ compile :dodoes gexecute
2694 :     \ T here H tcell - reloff
2695 : pazsan 1.54 2 refered
2696 :     0 fillcfa
2697 :     ;
2698 : anton 1.1
2699 : jwilke 1.103 : takeover-x-semantics ( S constructor-ghost new-ghost -- )
2700 : pazsan 1.117 \g stores execution semantic and compilation semantic in the built word
2701 :     swap >do:ghost @ 2dup swap >do:ghost !
2702 : jwilke 1.110 \ we use the >exec2 field for the semantic of a created word,
2703 :     \ using exec or exec2 makes no difference for normal cross-compilation
2704 :     \ but is usefull for instant where the exec field is already
2705 :     \ defined (e.g. Vocabularies)
2706 : jwilke 1.105 2dup >exec @ swap >exec2 !
2707 :     >comp @ swap >comp ! ;
2708 : jwilke 1.103
2709 : pazsan 1.118 0 Value createhere
2710 :    
2711 :     : create-resolve ( -- )
2712 :     created createhere resolve 0 ;Resolve ! ;
2713 :     : create-resolve-immediate ( -- )
2714 :     create-resolve T immediate H ;
2715 :    
2716 : jwilke 1.52 : TCreate ( <name> -- )
2717 :     create-forward-warn
2718 :     IF ['] reswarn-forward IS resolve-warning THEN
2719 : jwilke 1.103 executed-ghost @ (Theader
2720 : pazsan 1.117 dup >created on dup to created
2721 : pazsan 1.118 2dup takeover-x-semantics
2722 :     there to createhere drop gdoes, ;
2723 : jwilke 1.52
2724 :     : RTCreate ( <name> -- )
2725 :     \ creates a new word with code-field in ram
2726 :     create-forward-warn
2727 :     IF ['] reswarn-forward IS resolve-warning THEN
2728 :     \ make Alias
2729 : jwilke 1.103 executed-ghost @ (THeader
2730 : pazsan 1.118 dup >created on dup to created
2731 : jwilke 1.103 2dup takeover-x-semantics
2732 : jwilke 1.105 there 0 T a, H alias-mask flag!
2733 : jwilke 1.103 \ store poiter to code-field
2734 : jwilke 1.52 switchram T cfalign H
2735 :     there swap T ! H
2736 :     there tlastcfa !
2737 : pazsan 1.118 there to createhere drop gdoes, ;
2738 : anton 1.1
2739 :     : Build: ( -- [xt] [colon-sys] )
2740 : jwilke 1.52 :noname postpone TCreate ;
2741 :    
2742 :     : BuildSmart: ( -- [xt] [colon-sys] )
2743 :     :noname
2744 : jwilke 1.53 [ T has? rom H [IF] ]
2745 : jwilke 1.52 postpone RTCreate
2746 :     [ [ELSE] ]
2747 :     postpone TCreate
2748 :     [ [THEN] ] ;
2749 : anton 1.1
2750 : jwilke 1.108 : ;Build
2751 : pazsan 1.118 postpone create-resolve postpone ; built >exec ! ; immediate
2752 :    
2753 :     : ;Build-immediate
2754 :     postpone create-resolve-immediate
2755 :     postpone ; built >exec ! ; immediate
2756 : jwilke 1.108
2757 : anton 1.1 : gdoes> ( ghost -- addr flag )
2758 : jwilke 1.110 executed-ghost @ g>body ;
2759 : anton 1.1
2760 :     \ DO: ;DO 11may93jaw
2761 :    
2762 : jwilke 1.108 : do:ghost! ( ghost -- ) built >do:ghost ! ;
2763 :     : doexec! ( xt -- ) built >do:ghost @ >exec ! ;
2764 :    
2765 :     : DO: ( -- [xt] [colon-sys] )
2766 :     here ghostheader do:ghost!
2767 : jwilke 1.110 :noname postpone gdoes> ;
2768 : anton 1.1
2769 : jwilke 1.108 : by: ( -- [xt] [colon-sys] ) \ name
2770 :     Ghost do:ghost!
2771 : jwilke 1.110 :noname postpone gdoes> ;
2772 : pazsan 1.42
2773 : jwilke 1.108 : ;DO ( [xt] [colon-sys] -- )
2774 :     postpone ; doexec! ; immediate
2775 : anton 1.1
2776 : jwilke 1.109 : by ( -- ) \ Name
2777 : jwilke 1.108 Ghost >do:ghost @ do:ghost! ;
2778 : jwilke 1.107
2779 : jwilke 1.109 : compile: ( --[xt] [colon-sys] )
2780 : jwilke 1.107 \G defines a compile time action for created words
2781 :     \G by this builder
2782 :     :noname ;
2783 :    
2784 : jwilke 1.109 : ;compile ( [xt] [colon-sys] -- )
2785 :     postpone ; built >do:ghost @ >comp ! ; immediate
2786 : jwilke 1.107
2787 : anton 1.1 \ Variables and Constants 05dec92py
2788 :    
2789 : jwilke 1.108 Builder (Constant)
2790 :     Build: ( n -- ) ;Build
2791 : jwilke 1.103 by: :docon ( target-body-addr -- n ) T @ H ;DO
2792 : jwilke 1.52
2793 : jwilke 1.108 Builder Constant
2794 :     Build: ( n -- ) T , H ;Build
2795 : jwilke 1.52 by (Constant)
2796 :    
2797 : jwilke 1.108 Builder AConstant
2798 :     Build: ( n -- ) T A, H ;Build
2799 : jwilke 1.52 by (Constant)
2800 :    
2801 : jwilke 1.108 Builder 2Constant
2802 :     Build: ( d -- ) T , , H ;Build
2803 : jwilke 1.52 DO: ( ghost -- d ) T dup cell+ @ swap @ H ;DO
2804 :    
2805 : jwilke 1.108 Builder Create
2806 :     BuildSmart: ;Build
2807 : jwilke 1.103 by: :dovar ( target-body-addr -- addr ) ;DO
2808 : anton 1.1
2809 : jwilke 1.108 Builder Variable
2810 : jwilke 1.53 T has? rom H [IF]
2811 : jwilke 1.108 Build: ( -- ) T here 0 A, H switchram T align here swap ! 0 , H ( switchrom ) ;Build
2812 : jwilke 1.52 by (Constant)
2813 :     [ELSE]
2814 : jwilke 1.108 Build: T 0 , H ;Build
2815 : anton 1.1 by Create
2816 : jwilke 1.52 [THEN]
2817 : anton 1.1
2818 : jwilke 1.108 Builder 2Variable
2819 : jwilke 1.53 T has? rom H [IF]
2820 : jwilke 1.108 Build: ( -- ) T here 0 A, H switchram T align here swap ! 0 , 0 , H ( switchrom ) ;Build
2821 : pazsan 1.54 by (Constant)
2822 :     [ELSE]
2823 : jwilke 1.108 Build: T 0 , 0 , H ;Build
2824 : pazsan 1.54 by Create
2825 :     [THEN]
2826 :    
2827 : jwilke 1.108 Builder AVariable
2828 : pazsan 1.54 T has? rom H [IF]
2829 : jwilke 1.108 Build: ( -- ) T here 0 A, H switchram T align here swap ! 0 A, H ( switchrom ) ;Build
2830 : jwilke 1.52 by (Constant)
2831 :     [ELSE]
2832 : jwilke 1.108 Build: T 0 A, H ;Build
2833 : anton 1.1 by Create
2834 : jwilke 1.52 [THEN]
2835 : anton 1.1
2836 : pazsan 1.3 \ User variables 04may94py
2837 :    
2838 : jwilke 1.139 : tup@ user-region >rstart @ ;
2839 :    
2840 :     \ Variable tup 0 tup !
2841 :     \ Variable tudp 0 tudp !
2842 : jwilke 1.78
2843 : pazsan 1.3 : u, ( n -- udp )
2844 : pazsan 1.153 current-region >r user-region activate
2845 :     X here swap X , tup@ -
2846 : jwilke 1.139 r> activate ;
2847 : jwilke 1.78
2848 : pazsan 1.3 : au, ( n -- udp )
2849 : jwilke 1.139 current-region >r user-region activate
2850 :     X here swap X a, tup@ -
2851 :     r> activate ;
2852 :    
2853 :     T has? no-userspace H [IF]
2854 :    
2855 :     : buildby
2856 :     ghost >exec @ built >exec ! ;
2857 :    
2858 :     Builder User
2859 :     buildby Variable
2860 :     by Variable
2861 :    
2862 :     Builder 2User
2863 :     buildby 2Variable
2864 :     by 2Variable
2865 :    
2866 :     Builder AUser
2867 :     buildby AVariable
2868 :     by AVariable
2869 :    
2870 :     [ELSE]
2871 : jwilke 1.78
2872 : jwilke 1.108 Builder User
2873 :     Build: 0 u, X , ;Build
2874 : jwilke 1.139 by: :douser ( ghost -- up-addr ) X @ tup@ + ;DO
2875 : anton 1.1
2876 : jwilke 1.108 Builder 2User
2877 :     Build: 0 u, X , 0 u, drop ;Build
2878 : pazsan 1.3 by User
2879 : anton 1.1
2880 : jwilke 1.108 Builder AUser
2881 :     Build: 0 au, X , ;Build
2882 : pazsan 1.3 by User
2883 : anton 1.1
2884 : jwilke 1.139 [THEN]
2885 :    
2886 : pazsan 1.155 T has? rom H [IF]
2887 :     Builder (Value)
2888 :     Build: ( n -- ) ;Build
2889 :     by: :dovalue ( target-body-addr -- n ) T @ @ H ;DO
2890 :    
2891 :     Builder Value
2892 :     Build: T here 0 A, H switchram T align here swap ! , H ;Build
2893 :     by (Value)
2894 :    
2895 :     Builder AValue
2896 :     Build: T here 0 A, H switchram T align here swap ! A, H ;Build
2897 :     by (Value)
2898 :     [ELSE]
2899 : jwilke 1.108 Builder (Value)
2900 :     Build: ( n -- ) ;Build
2901 : pazsan 1.156 by: :dovalue ( target-body-addr -- n ) T @ H ;DO
2902 : jwilke 1.108
2903 : anton 1.1 Builder Value
2904 : jwilke 1.108 BuildSmart: T , H ;Build
2905 :     by (Value)
2906 : anton 1.1
2907 : pazsan 1.32 Builder AValue
2908 : jwilke 1.108 BuildSmart: T A, H ;Build
2909 :     by (Value)
2910 : pazsan 1.155 [THEN]
2911 : pazsan 1.32
2912 : jwilke 1.103 Defer texecute
2913 :    
2914 : jwilke 1.108 Builder Defer
2915 : pazsan 1.155 T has? rom H [IF]
2916 :     Build: ( -- ) T here 0 A, H switchram T align here swap ! H [T'] noop T A, H ( switchrom ) ;Build
2917 :     by: :dodefer ( ghost -- ) X @ X @ texecute ;DO
2918 :     [ELSE]
2919 :     BuildSmart: ( -- ) [T'] noop T A, H ;Build
2920 :     by: :dodefer ( ghost -- ) X @ texecute ;DO
2921 :     [THEN]
2922 : pazsan 1.37
2923 : jwilke 1.108 Builder interpret/compile:
2924 : pazsan 1.118 Build: ( inter comp -- ) swap T A, A, H ;Build-immediate
2925 : pazsan 1.37 DO: ( ghost -- ) ABORT" CROSS: Don't execute" ;DO
2926 : pazsan 1.24
2927 :     \ Sturctures 23feb95py
2928 :    
2929 :     : nalign ( addr1 n -- addr2 )
2930 :     \ addr2 is the aligned version of addr1 wrt the alignment size n
2931 :     1- tuck + swap invert and ;
2932 : jwilke 1.109
2933 : pazsan 1.24
2934 : jwilke 1.108 Builder (Field)
2935 :     Build: ;Build
2936 : pazsan 1.44 by: :dofield T @ H + ;DO
2937 :    
2938 : jwilke 1.108 Builder Field
2939 : anton 1.51 Build: ( align1 offset1 align size "name" -- align2 offset2 )
2940 :     rot dup T , H ( align1 align size offset1 )
2941 : jwilke 1.108 + >r nalign r> ;Build
2942 : pazsan 1.44 by (Field)
2943 : pazsan 1.24
2944 : jwilke 1.109 >TARGET
2945 : anton 1.51 : struct T 1 chars 0 H ;
2946 : pazsan 1.24 : end-struct T 2Constant H ;
2947 :    
2948 : jwilke 1.52 : cell% ( n -- size align )
2949 : anton 1.51 T 1 cells H dup ;
2950 : jwilke 1.109 >CROSS
2951 : pazsan 1.88
2952 : dvdkhlng 1.169 \ ABI-CODE support
2953 :     Builder (ABI-CODE)
2954 :     Build: ;Build
2955 :     by: :doabicode noop ;DO
2956 :    
2957 : jwilke 1.105 \ Input-Methods 01py
2958 : jwilke 1.103
2959 : jwilke 1.108 Builder input-method
2960 :     Build: ( m v -- m' v ) dup T , cell+ H ;Build
2961 : pazsan 1.88 DO: abort" Not in cross mode" ;DO
2962 :    
2963 : jwilke 1.108 Builder input-var
2964 :     Build: ( m v size -- m v' ) over T , H + ;Build
2965 : pazsan 1.88 DO: abort" Not in cross mode" ;DO
2966 : pazsan 1.24
2967 : jwilke 1.108 \ Peephole optimization 05sep01jaw
2968 :    
2969 :     \ this section defines different compilation
2970 :     \ actions for created words
2971 :     \ this will help the peephole optimizer
2972 : jwilke 1.121 \ I (jaw) took this from bernds latest cross-compiler
2973 : jwilke 1.108 \ changes but seperated it from the original
2974 :     \ Builder words. The final plan is to put this
2975 :     \ into a seperate file, together with the peephole
2976 :     \ optimizer for cross
2977 :    
2978 :    
2979 : pazsan 1.162 T has? primcentric H [IF]
2980 : jwilke 1.108
2981 : jwilke 1.139 \ .( loading peephole optimization) cr
2982 :    
2983 : jwilke 1.109 >CROSS
2984 : pazsan 1.118
2985 : jwilke 1.109 : (callc) compile call T >body a, H ; ' (callc) plugin-of colon,
2986 : jwilke 1.121 : (callcm) T here 0 a, 0 a, H ; ' (callcm) plugin-of colonmark,
2987 : pazsan 1.118 : (call-res) >tempdp resolved gexecute tempdp> drop ;
2988 :     ' (call-res) plugin-of colon-resolve
2989 : pazsan 1.164 T has? ec H [IF]
2990 :     : (pprim) T @ H >signed dup 0< IF $4000 - ELSE
2991 :     cr ." wrong usage of (prim) "
2992 :     dup gdiscover IF .ghost ELSE . THEN cr -1 throw THEN
2993 :     T a, H ; ' (pprim) plugin-of prim,
2994 :     [ELSE]
2995 : jwilke 1.121 : (pprim) dup 0< IF $4000 - ELSE
2996 :     cr ." wrong usage of (prim) "
2997 :     dup gdiscover IF .ghost ELSE . THEN cr -1 throw THEN
2998 :     T a, H ; ' (pprim) plugin-of prim,
2999 : pazsan 1.164 [THEN]
3000 : jwilke 1.109
3001 :     \ if we want this, we have to spilt aconstant
3002 :     \ and constant!!
3003 :     \ Builder (Constant)
3004 :     \ compile: g>body X @ lit, ;compile
3005 : jwilke 1.108
3006 :     Builder (Constant)
3007 : pazsan 1.118 compile: g>body compile lit@ T a, H ;compile
3008 : jwilke 1.108
3009 :     Builder (Value)
3010 : pazsan 1.118 compile: g>body compile lit@ T a, H ;compile
3011 : jwilke 1.108
3012 :     \ this changes also Variable, AVariable and 2Variable
3013 :     Builder Create
3014 : pazsan 1.117 compile: g>body alit, ;compile
3015 : jwilke 1.108
3016 :     Builder User
3017 :     compile: g>body compile useraddr T @ , H ;compile
3018 :    
3019 :     Builder Defer
3020 : pazsan 1.118 compile: g>body compile lit-perform T A, H ;compile
3021 : jwilke 1.102
3022 : jwilke 1.108 Builder (Field)
3023 : pazsan 1.164 compile: g>body T @ H compile lit+ T here H reloff T , H ;compile
3024 : pazsan 1.117
3025 :     Builder interpret/compile:
3026 :     compile: does-resolved ;compile
3027 :    
3028 :     Builder input-method
3029 :     compile: does-resolved ;compile
3030 :    
3031 :     Builder input-var
3032 :     compile: does-resolved ;compile
3033 : jwilke 1.102
3034 : jwilke 1.108 [THEN]
3035 : jwilke 1.103
3036 : jwilke 1.52 \ structural conditionals 17dec92py
3037 :    
3038 :     >CROSS
3039 : jwilke 1.103 : (ncontrols?) ( n -- )
3040 :     \g We expect n open control structures
3041 :     depth over u<=
3042 :     ABORT" CROSS: unstructured, stack underflow"
3043 :     0 ?DO I pick 0=
3044 :     ABORT" CROSS: unstructured"
3045 :     LOOP ; ' (ncontrols?) plugin-of ncontrols?
3046 :    
3047 :     \ : ?struc ( flag -- ) ABORT" CROSS: unstructured " ;
3048 :     \ : sys? ( sys -- sys ) dup 0= ?struc ;
3049 :    
3050 : jwilke 1.52 : >mark ( -- sys ) T here ( dup ." M" hex. ) 0 , H ;
3051 :    
3052 : pazsan 1.133 X has? abranch [IF]
3053 :     : branchoffset ( src dest -- ) drop ;
3054 :     : offset, ( n -- ) X A, ;
3055 :     [ELSE]
3056 :     : branchoffset ( src dest -- ) - tchar / ; \ ?? jaw
3057 :     : offset, ( n -- ) X , ;
3058 :     [THEN]
3059 : jwilke 1.52
3060 : pazsan 1.133 :noname compile branch X here branchoffset offset, ;
3061 : pazsan 1.54 IS branch, ( target-addr -- )
3062 : pazsan 1.133 :noname compile ?branch X here branchoffset offset, ;
3063 : pazsan 1.54 IS ?branch, ( target-addr -- )
3064 : pazsan 1.133 :noname compile branch T here 0 H offset, ;
3065 : pazsan 1.54 IS branchmark, ( -- branchtoken )
3066 : pazsan 1.133 :noname compile ?branch T here 0 H offset, ;
3067 : pazsan 1.54 IS ?branchmark, ( -- branchtoken )
3068 : pazsan 1.133 :noname T here 0 H offset, ;
3069 : pazsan 1.54 IS ?domark, ( -- branchtoken )
3070 : jwilke 1.78 :noname dup X @ ?struc X here over branchoffset swap X ! ;
3071 : pazsan 1.54 IS branchtoresolve, ( branchtoken -- )
3072 : jwilke 1.78 :noname branchto, X here ;
3073 : pazsan 1.54 IS branchtomark, ( -- target-addr )
3074 : jwilke 1.52
3075 :     >TARGET
3076 :    
3077 :     \ Structural Conditionals 12dec92py
3078 :    
3079 : jwilke 1.103 \ CLEANUP Cond: BUT sys? swap ;Cond
3080 :     \ CLEANUP Cond: YET sys? dup ;Cond
3081 : jwilke 1.52
3082 :     >CROSS
3083 :    
3084 : jwilke 1.78 Variable tleavings 0 tleavings !
3085 : jwilke 1.52
3086 : jwilke 1.129 : (done) ( do-addr -- )
3087 :     \G resolve branches of leave and ?leave and ?do
3088 :     \G do-addr is the address of the beginning of our
3089 :     \G loop so we can take care of nested loops
3090 : pazsan 1.54 tleavings @
3091 :     BEGIN dup
3092 :     WHILE
3093 :     >r dup r@ cell+ @ \ address of branch
3094 :     u> 0= \ lower than DO?
3095 :     WHILE
3096 :     r@ 2 cells + @ \ branch token
3097 :     branchtoresolve,
3098 :     r@ @ r> free throw
3099 :     REPEAT r> THEN
3100 :     tleavings ! drop ;
3101 :    
3102 : jwilke 1.53 >TARGET
3103 :    
3104 : jwilke 1.103 \ What for? ANS? JAW Cond: DONE ( addr -- ) (done) ;Cond
3105 : jwilke 1.53
3106 :     >CROSS
3107 : pazsan 1.54 : (leave) ( branchtoken -- )
3108 : jwilke 1.53 3 cells allocate throw >r
3109 :     T here H r@ cell+ !
3110 :     r@ 2 cells + !
3111 :     tleavings @ r@ !
3112 :     r> tleavings ! ;
3113 :     >TARGET
3114 :    
3115 : jwilke 1.103 : (leave,) ( -- )
3116 :     branchmark, (leave) ; ' (leave,) plugin-of leave,
3117 :    
3118 :     : (?leave,) ( -- )
3119 :     compile 0= ?branchmark, (leave) ; ' (?leave,) plugin-of ?leave,
3120 :    
3121 :     Cond: LEAVE leave, ;Cond
3122 :     Cond: ?LEAVE ?leave, ;Cond
3123 : jwilke 1.53
3124 : pazsan 1.54 >CROSS
3125 :     \ !!JW ToDo : Move to general tools section
3126 :    
3127 :     : to1 ( x1 x2 xn n -- addr )
3128 : jwilke 1.103 \G packs n stack elements in am allocated memory region
3129 : pazsan 1.54 dup dup 1+ cells allocate throw dup >r swap 1+
3130 :     0 DO tuck ! cell+ LOOP
3131 :     drop r> ;
3132 : jwilke 1.103
3133 : pazsan 1.54 : 1to ( addr -- x1 x2 xn )
3134 :     \G unpacks the elements saved by to1
3135 :     dup @ swap over cells + swap
3136 :     0 DO dup @ swap 1 cells - LOOP
3137 :     free throw ;
3138 :    
3139 : jwilke 1.129 : loop] ( target-addr -- )
3140 :     branchto,
3141 : pazsan 1.133 dup X here branchoffset offset,
3142 : jwilke 1.129 tcell - (done) ;
3143 : pazsan 1.54
3144 :     : skiploop] ?dup IF branchto, branchtoresolve, THEN ;
3145 :    
3146 :     >TARGET
3147 :    
3148 : jwilke 1.52 \ Structural Conditionals 12dec92py
3149 :    
3150 : jwilke 1.103 : (cs-swap) ( x1 x2 -- x2 x1 )
3151 :     swap ; ' (cs-swap) plugin-of cs-swap
3152 :    
3153 :     : (ahead,) branchmark, ; ' (ahead,) plugin-of ahead,
3154 :    
3155 :     : (if,) ?branchmark, ; ' (if,) plugin-of if,
3156 :    
3157 :     : (then,) branchto, branchtoresolve, ; ' (then,) plugin-of then,
3158 :    
3159 :     : (else,) ( ahead ) branchmark,
3160 :     swap
3161 :     ( then ) branchto, branchtoresolve, ; ' (else,) plugin-of else,
3162 :    
3163 :     : (begin,) branchtomark, ; ' (begin,) plugin-of begin,
3164 :    
3165 :     : (while,) ( if ) ?branchmark,
3166 :     swap ; ' (while,) plugin-of while,
3167 :    
3168 :     : (again,) branch, ; ' (again,) plugin-of again,
3169 :    
3170 :     : (until,) ?branch, ; ' (until,) plugin-of until,
3171 :    
3172 :     : (repeat,) ( again ) branch,
3173 :     ( then ) branchto, branchtoresolve, ; ' (repeat,) plugin-of repeat,
3174 :    
3175 :     : (case,) ( -- n )
3176 :     0 ; ' (case,) plugin-of case,
3177 :    
3178 :     : (of,) ( n -- x1 n )
3179 :     1+ >r
3180 :     compile over compile =
3181 :     if, compile drop r> ; ' (of,) plugin-of of,
3182 :    
3183 :     : (endof,) ( x1 n -- x2 n )
3184 :     >r 1 ncontrols? else, r> ; ' (endof,) plugin-of endof,
3185 :    
3186 :     : (endcase,) ( x1 .. xn n -- )
3187 :     compile drop 0 ?DO 1 ncontrols? then, LOOP ; ' (endcase,) plugin-of endcase,
3188 :    
3189 : jwilke 1.53 >TARGET
3190 : jwilke 1.103 Cond: AHEAD ahead, ;Cond
3191 :     Cond: IF if, ;Cond
3192 :     Cond: THEN 1 ncontrols? then, ;Cond
3193 :     Cond: ENDIF 1 ncontrols? then, ;Cond
3194 :     Cond: ELSE 1 ncontrols? else, ;Cond
3195 :    
3196 :     Cond: BEGIN begin, ;Cond
3197 :     Cond: WHILE 1 ncontrols? while, ;Cond
3198 :     Cond: AGAIN 1 ncontrols? again, ;Cond
3199 :     Cond: UNTIL 1 ncontrols? until, ;Cond
3200 :     Cond: REPEAT 2 ncontrols? repeat, ;Cond
3201 :    
3202 :     Cond: CASE case, ;Cond
3203 :     Cond: OF of, ;Cond
3204 :     Cond: ENDOF endof, ;Cond
3205 :     Cond: ENDCASE endcase, ;Cond
3206 : anton 1.1
3207 :     \ Structural Conditionals 12dec92py
3208 :    
3209 : jwilke 1.103 : (do,) ( -- target-addr )
3210 :     \ ?? i think 0 is too much! jaw
3211 : pazsan 1.54 0 compile (do)
3212 : jwilke 1.103 branchtomark, 2 to1 ; ' (do,) plugin-of do,
3213 : pazsan 1.54
3214 : jwilke 1.103 \ alternative for if no ?do
3215 :     \ : (do,)
3216 : pazsan 1.54 \ compile 2dup compile = compile IF
3217 :     \ compile 2drop compile ELSE
3218 :     \ compile (do) branchtomark, 2 to1 ;
3219 :    
3220 : jwilke 1.103 : (?do,) ( -- target-addr )
3221 : pazsan 1.54 0 compile (?do) ?domark, (leave)
3222 : jwilke 1.103 branchtomark, 2 to1 ; ' (?do,) plugin-of ?do,
3223 :    
3224 :     : (for,) ( -- target-addr )
3225 :     compile (for) branchtomark, ; ' (for,) plugin-of for,
3226 :    
3227 :     : (loop,) ( target-addr -- )
3228 :     1to compile (loop) loop]
3229 :     compile unloop skiploop] ; ' (loop,) plugin-of loop,
3230 :    
3231 :     : (+loop,) ( target-addr -- )
3232 :     1to compile (+loop) loop]
3233 :     compile unloop skiploop] ; ' (+loop,) plugin-of +loop,
3234 :    
3235 :     : (next,)
3236 :     compile (next) loop] compile unloop ; ' (next,) plugin-of next,
3237 :    
3238 :     Cond: DO do, ;Cond
3239 :     Cond: ?DO ?do, ;Cond
3240 :     Cond: FOR for, ;Cond
3241 :    
3242 :     Cond: LOOP 1 ncontrols? loop, ;Cond
3243 :     Cond: +LOOP 1 ncontrols? +loop, ;Cond
3244 :     Cond: NEXT 1 ncontrols? next, ;Cond
3245 : jwilke 1.52
3246 : anton 1.1 \ String words 23feb93py
3247 :    
3248 : jwilke 1.103 : ," [char] " parse ht-string, X align ;
3249 : anton 1.1
3250 : pazsan 1.130 X has? control-rack [IF]
3251 : jwilke 1.103 Cond: ." compile (.") T ," H ;Cond
3252 :     Cond: S" compile (S") T ," H ;Cond
3253 :     Cond: C" compile (C") T ," H ;Cond
3254 :     Cond: ABORT" compile (ABORT") T ," H ;Cond
3255 : pazsan 1.130 [ELSE]
3256 :     Cond: ." '" parse tuck 2>r ahead, there 2r> ht-mem, X align
3257 :     >r then, r> compile ALiteral compile Literal compile type ;Cond
3258 :     Cond: S" '" parse tuck 2>r ahead, there 2r> ht-mem, X align
3259 :     >r then, r> compile ALiteral compile Literal ;Cond
3260 :     Cond: C" ahead, there [char] " parse ht-string, X align
3261 :     >r then, r> compile ALiteral ;Cond
3262 :     Cond: ABORT" if, ahead, there [char] " parse ht-string, X align
3263 :     >r then, r> compile ALiteral compile c(abort") then, ;Cond
3264 :     [THEN]
3265 : anton 1.1
3266 : pazsan 1.155 X has? rom [IF]
3267 :     Cond: IS T ' >body @ H compile ALiteral compile ! ;Cond
3268 :     : IS T >address ' >body @ ! H ;
3269 :     Cond: TO T ' >body @ H compile ALiteral compile ! ;Cond
3270 :     : TO T ' >body @ ! H ;
3271 : pazsan 1.160 Cond: CTO T ' >body H compile ALiteral compile ! ;Cond
3272 :     : CTO T ' >body ! H ;
3273 : pazsan 1.155 [ELSE]
3274 : anton 1.1 Cond: IS T ' >body H compile ALiteral compile ! ;Cond
3275 : pazsan 1.66 : IS T >address ' >body ! H ;
3276 : pazsan 1.9 Cond: TO T ' >body H compile ALiteral compile ! ;Cond
3277 :     : TO T ' >body ! H ;
3278 : pazsan 1.155 [THEN]
3279 : anton 1.1
3280 : jwilke 1.52 Cond: defers T ' >body @ compile, H ;Cond
3281 :    
3282 : anton 1.1 \ LINKED ERR" ENV" 2ENV" 18may93jaw
3283 :    
3284 :     \ linked list primitive
3285 : jwilke 1.79 : linked X here over X @ X A, swap X ! ;
3286 : jwilke 1.52 : chained T linked A, H ;
3287 : anton 1.1
3288 :     : err" s" ErrLink linked" evaluate T , H
3289 : jwilke 1.103 [char] " parse ht-string, X align ;
3290 : anton 1.1
3291 :     : env" [char] " parse s" EnvLink linked" evaluate
3292 : jwilke 1.103 ht-string, X align X , ;
3293 : anton 1.1
3294 :     : 2env" [char] " parse s" EnvLink linked" evaluate
3295 : jwilke 1.103 here >r ht-string, X align X , X ,
3296 : anton 1.1 r> dup T c@ H 80 and swap T c! H ;
3297 :    
3298 :     \ compile must be last 22feb93py
3299 :    
3300 : jwilke 1.103 Cond: [compile] ( -- ) \ name
3301 :     \g For immediate words, works even if forward reference
3302 :     bl word gfind 0= ABORT" CROSS: Can't compile"
3303 :     (gexecute) ;Cond
3304 :    
3305 :     Cond: postpone ( -- ) \ name
3306 :     bl word gfind 0= ABORT" CROSS: Can't compile"
3307 :     dup >magic @ <fwd> =
3308 :     ABORT" CROSS: Can't postpone on forward declaration"
3309 :     dup >magic @ <imm> =
3310 :     IF (gexecute)
3311 : pazsan 1.143 ELSE >link @ alit, compile compile, THEN ;Cond
3312 : pazsan 1.54
3313 : jwilke 1.77 \ save-cross 17mar93py
3314 :    
3315 :     hex
3316 :    
3317 :     >CROSS
3318 : anton 1.131 Create magic s" Gforth3x" here over allot swap move
3319 : jwilke 1.77
3320 :     bigendian 1+ \ strangely, in magic big=0, little=1
3321 :     tcell 1 = 0 and or
3322 :     tcell 2 = 2 and or
3323 :     tcell 4 = 4 and or
3324 :     tcell 8 = 6 and or
3325 :     tchar 1 = 00 and or
3326 :     tchar 2 = 28 and or
3327 :     tchar 4 = 50 and or
3328 :     tchar 8 = 78 and or
3329 :     magic 7 + c!
3330 :    
3331 :     : save-cross ( "image-name" "binary-name" -- )
3332 : pazsan 1.152 .regions \ s" ec" X $has? IF .regions THEN
3333 : jwilke 1.77 bl parse ." Saving to " 2dup type cr
3334 :     w/o bin create-file throw >r
3335 : jwilke 1.113 s" header" X $has? IF
3336 : anton 1.80 s" #! " r@ write-file throw
3337 :     bl parse r@ write-file throw
3338 :     s" --image-file" r@ write-file throw
3339 : jwilke 1.77 #lf r@ emit-file throw
3340 :     r@ dup file-position throw drop 8 mod 8 swap ( file-id limit index )
3341 :     ?do
3342 :     bl over emit-file throw
3343 :     loop
3344 :     drop
3345 :     magic 8 r@ write-file throw \ write magic
3346 :     ELSE
3347 :     bl parse 2drop
3348 :     THEN
3349 : pazsan 1.151 >rom dictionary >rmem @ there
3350 :     s" rom" X $has? IF dictionary >rstart @ - THEN
3351 : jwilke 1.77 r@ write-file throw \ write image
3352 : jwilke 1.113 s" relocate" X $has? IF
3353 : jwilke 1.139 dictionary >rbm @ there 1- tcell>bit rshift 1+
3354 : jwilke 1.77 r@ write-file throw \ write tags
3355 :     THEN
3356 :     r> close-file throw ;
3357 :    
3358 :     : save-region ( addr len -- )
3359 :     bl parse w/o bin create-file throw >r
3360 :     swap >image swap r@ write-file throw
3361 :     r> close-file throw ;
3362 :    
3363 : jwilke 1.110 \ save-asm-region 29aug01jaw
3364 : jwilke 1.103
3365 :     Variable name-ptr
3366 :     Create name-buf 200 chars allot
3367 :     : init-name-buf name-buf name-ptr ! ;
3368 :     : nb, name-ptr @ c! 1 chars name-ptr +! ;
3369 :     : $nb, ( adr len -- ) bounds ?DO I c@ nb, LOOP ;
3370 :     : @nb name-ptr @ name-buf tuck - ;
3371 :    
3372 :     \ stores a usefull string representation of the character
3373 :     \ in the name buffer
3374 :     : name-char, ( c -- )
3375 :     dup 'a 'z 1+ within IF nb, EXIT THEN
3376 :     dup 'A 'Z 1+ within IF $20 + nb, EXIT THEN
3377 :     dup '0 '9 1+ within IF nb, EXIT THEN
3378 :     CASE '+ OF s" _PLUS" $nb, ENDOF
3379 :     '- OF s" _MINUS" $nb, ENDOF
3380 :     '* OF s" _STAR" $nb, ENDOF
3381 :     '/ OF s" _SLASH" $nb, ENDOF
3382 :     '' OF s" _TICK" $nb, ENDOF
3383 :     '( OF s" _OPAREN" $nb, ENDOF
3384 :     ') OF s" _CPAREN" $nb, ENDOF
3385 :     '[ OF s" _OBRACKET" $nb, ENDOF
3386 :     '] OF s" _CBRACKET" $nb, ENDOF
3387 :     '! OF s" _STORE" $nb, ENDOF
3388 :     '@ OF s" _FETCH" $nb, ENDOF
3389 :     '> OF s" _GREATER" $nb, ENDOF
3390 :     '< OF s" _LESS" $nb, ENDOF
3391 :     '= OF s" _EQUAL" $nb, ENDOF
3392 :     '# OF s" _HASH" $nb, ENDOF
3393 :     '? OF s" _QUEST" $nb, ENDOF
3394 :     ': OF s" _COL" $nb, ENDOF
3395 :     '; OF s" _SEMICOL" $nb, ENDOF
3396 :     ', OF s" _COMMA" $nb, ENDOF
3397 :     '. OF s" _DOT" $nb, ENDOF
3398 :     '" OF s" _DQUOT" $nb, ENDOF
3399 :     dup
3400 :     base @ >r hex s>d <# #s 'X hold '_ hold #> $nb, r> base !
3401 :     ENDCASE ;
3402 :    
3403 :     : label-from-ghostname ( ghost -- addr len )
3404 :     dup >ghostname init-name-buf 'L nb, bounds
3405 :     ?DO I c@ name-char, LOOP
3406 :     \ we add the address to a name to make in unique
3407 :     \ because one name may appear more then once
3408 :     \ there are names (e.g. boot) that may be reference from other
3409 :     \ assembler source files, so we declare them as unique
3410 :     \ and don't add the address suffix
3411 :     dup >ghost-flags @ <unique> and 0=
3412 :     IF s" __" $nb, >link @ base @ >r hex 0 <# #s 'L hold #> r> base ! $nb,
3413 :     ELSE drop
3414 :     THEN
3415 :     @nb ;
3416 :    
3417 : jwilke 1.111 \ FIXME why disabled?!
3418 : jwilke 1.103 : label-from-ghostnameXX ( ghost -- addr len )
3419 :     \ same as (label-from-ghostname) but caches generated names
3420 :     dup >asm-name @ ?dup IF nip count EXIT THEN
3421 :     \ dup >r (label-from-ghostname) 2dup
3422 :     align here >r string, align
3423 :     r> r> >asm-name ! ;
3424 :    
3425 :     : primghostdiscover ( xt -- ghost true | xt false )
3426 :     dup 0= IF false EXIT THEN
3427 :     >r last-prim-ghost
3428 :     BEGIN @ dup
3429 :     WHILE dup >asm-dummyaddr @ r@ =
3430 :     IF rdrop true EXIT THEN
3431 :     REPEAT
3432 :     drop r> false ;
3433 :    
3434 :     : gdiscover2 ( xt -- ghost true | xt false )
3435 :     dup taddr>region 0= IF false EXIT THEN
3436 :     dup (>regiontype) @ dup 0= IF drop false EXIT THEN
3437 :     addr-xt-ghost @ dup 0= IF drop false EXIT THEN
3438 :     nip true ;
3439 :     \ dup >ghost-name @ IF nip true ELSE drop false THEN ;
3440 :    
3441 :     \ generates a label name for the target address
3442 :     : generate-label-name ( taddr -- addr len )
3443 :     gdiscover2
3444 :     IF dup >magic @ <do:> =
3445 :     IF >asm-name @ count EXIT THEN
3446 :     label-from-ghostname
3447 :     ELSE
3448 :     primghostdiscover
3449 :     IF >asm-name @ count
3450 :     ELSE base @ >r hex 0 <# #s 'L hold #> r> base !
3451 :     THEN
3452 :     THEN ;
3453 :    
3454 :     Variable outfile-fd
3455 :    
3456 :     : $out ( adr len -- ) outfile-fd @ write-file throw ;
3457 :     : nlout newline $out ;
3458 :     : .ux ( n -- )
3459 :     base @ hex swap 0 <# #S #> $out base ! ;
3460 :    
3461 :     : save-asm-region-part-aligned ( taddr len -- 'taddr 'len )
3462 :     dup cell/ 0
3463 :     ?DO nlout s" .word " $out over @relbit
3464 :     IF over X @ generate-label-name $out
3465 :     ELSE over X @ s" 0x0" $out .ux
3466 :     THEN
3467 :     tcell /string
3468 :     LOOP ;
3469 :    
3470 :     : print-bytes ( taddr len n -- taddr' len' )
3471 :     over min dup 0>
3472 :     IF nlout s" .byte " $out 0
3473 :     ?DO I 0> IF s" , " $out THEN
3474 :     over X c@ s" 0x0" $out .ux 1 /string
3475 :     LOOP
3476 :     THEN ;
3477 :    
3478 :     : save-asm-region-part ( addr len -- )
3479 :     over dup X aligned swap - ?dup
3480 :     IF print-bytes THEN
3481 :     save-asm-region-part-aligned
3482 :     dup dup X aligned swap - ?dup
3483 :     IF 2 pick @relbit ABORT" relocated field splitted"
3484 :     print-bytes
3485 :     THEN
3486 :     2drop ;
3487 :    
3488 :     : print-label ( taddr -- )
3489 :     nlout generate-label-name $out s" :" $out ;
3490 :    
3491 :     : snl-calc ( taddr taddr2 -- )
3492 :     tuck over - ;
3493 :    
3494 :     : skip-nolables ( taddr -- taddr2 taddr len )
3495 :     \G skips memory region where no lables are defined
3496 :     \G starting from taddr+1
3497 :     \G Labels will be introduced for each reference mark
3498 :     \G in addr-refs.
3499 :     \G This word deals with lables at byte addresses as well.
3500 :     \G The main idea is to have an intro part which
3501 :     \G skips until the next cell boundary, the middle part
3502 :     \G which skips whole cells very efficiently and the third
3503 :     \G part which skips the bytes to the label in a cell
3504 :     dup 1+ dup (>regiontype)
3505 :     ( S taddr taddr-realstart type-addr )
3506 :     dup @ dup IF addr-refs @ THEN
3507 :     swap >r
3508 :     over align+ tuck tcell swap - rshift swap 0
3509 : jwilke 1.126 ?DO dup 1 and
3510 : jwilke 1.103 IF drop rdrop snl-calc UNLOOP EXIT THEN
3511 :     2/ swap 1+ swap
3512 :     LOOP
3513 :     drop r> cell+
3514 :     ( S .. taddr2 type-addr ) dup
3515 :     BEGIN dup @ dup IF addr-refs @ THEN 0= WHILE cell+ REPEAT
3516 :     dup >r swap - 1 cells / tcell * + r>
3517 :     ( S .. taddr2+skiplencells type-addr )
3518 :     @ addr-refs @ 1 tcell lshift or
3519 :     BEGIN dup 1 and 0= WHILE swap 1+ swap 2/ REPEAT drop
3520 :     ( S .. taddr2+skiplencells+skiplenbytes )
3521 :     snl-calc ;
3522 :    
3523 :     : insert-label ( taddr -- )
3524 :     dup 0= IF drop EXIT THEN
3525 :     \ ignore everything which points outside our memory regions
3526 :     \ maybe a primitive pointer or whatever
3527 :     dup taddr>region 0= IF drop EXIT THEN
3528 :     dup >r (>regiontype) define-addr-struct addr-refs dup @
3529 :     r> tcell 1- and 1 swap lshift or swap ! ;
3530 :    
3531 :     \ this generates a sorted list of addresses which must be labels
3532 :     \ it scans therefore a whole region
3533 :     : generate-label-list-region ( taddr len -- )
3534 :     BEGIN over @relbit IF over X @ insert-label THEN
3535 :     tcell /string dup 0<
3536 :     UNTIL 2drop ;
3537 :    
3538 :     : generate-label-list ( -- )
3539 :     region-link
3540 :     BEGIN @ dup WHILE
3541 :     dup 0 >rlink - extent
3542 :     ?dup IF generate-label-list-region ELSE drop THEN
3543 :     REPEAT drop ;
3544 :    
3545 :     : create-outfile ( addr len -- )
3546 :     w/o bin create-file throw outfile-fd ! ;
3547 :    
3548 :     : close-outfile ( -- )
3549 :     outfile-fd @ close-file throw ;
3550 :    
3551 :     : (save-asm-region) ( region -- )
3552 :     \ ." label list..."
3553 :     generate-label-list
3554 :     \ ." ok!" cr
3555 :     extent ( S taddr len )
3556 :     over insert-label
3557 :     2dup + dup insert-label >r ( R end-label )
3558 :     ( S taddr len ) drop
3559 :     BEGIN
3560 :     dup print-label
3561 :     dup r@ <> WHILE
3562 :     skip-nolables save-asm-region-part
3563 :     REPEAT drop rdrop ;
3564 :    
3565 :     : lineout ( addr len -- )
3566 :     outfile-fd @ write-line throw ;
3567 :    
3568 :     : save-asm-region ( region adr len -- )
3569 :     create-outfile (save-asm-region) close-outfile ;
3570 :    
3571 : pazsan 1.54 \ \ minimal definitions
3572 :    
3573 : jwilke 1.77 >MINIMAL also minimal
3574 :    
3575 : anton 1.1 \ Usefull words 13feb93py
3576 :    
3577 :     : KB 400 * ;
3578 :    
3579 : pazsan 1.54 \ \ [IF] [ELSE] [THEN] ... 14sep97jaw
3580 :    
3581 :     \ it is useful to define our own structures and not to rely
3582 : jwilke 1.109 \ on the words in the host system
3583 :     \ The words in the host system might be defined with vocabularies
3584 : pazsan 1.54 \ this doesn't work with our self-made compile-loop
3585 :    
3586 :     Create parsed 20 chars allot \ store word we parsed
3587 :    
3588 :     : upcase
3589 :     parsed count bounds
3590 :     ?DO I c@ toupper I c! LOOP ;
3591 :    
3592 :     : [ELSE]
3593 :     1 BEGIN
3594 :     BEGIN bl word count dup WHILE
3595 : jwilke 1.77 comment? 20 umin parsed place upcase parsed count
3596 : anton 1.128 2dup s" [IF]" str= >r
3597 :     2dup s" [IFUNDEF]" str= >r
3598 :     2dup s" [IFDEF]" str= r> or r> or
3599 : pazsan 1.54 IF 2drop 1+
3600 : anton 1.128 ELSE 2dup s" [ELSE]" str=
3601 : pazsan 1.54 IF 2drop 1- dup
3602 :     IF 1+
3603 :     THEN
3604 :     ELSE
3605 : anton 1.128 2dup s" [ENDIF]" str= >r
3606 :     s" [THEN]" str= r> or
3607 : pazsan 1.54 IF 1- THEN
3608 :     THEN
3609 :     THEN
3610 :     ?dup 0= ?EXIT
3611 :     REPEAT
3612 :     2drop refill 0=
3613 :     UNTIL drop ; immediate
3614 :    
3615 :     : [THEN] ( -- ) ; immediate
3616 :    
3617 :     : [ENDIF] ( -- ) ; immediate
3618 :    
3619 :     : [IF] ( flag -- )
3620 :     0= IF postpone [ELSE] THEN ; immediate
3621 :    
3622 :     Cond: [IF] postpone [IF] ;Cond
3623 :     Cond: [THEN] postpone [THEN] ;Cond
3624 :     Cond: [ELSE] postpone [ELSE] ;Cond
3625 :    
3626 : anton 1.1 \ define new [IFDEF] and [IFUNDEF] 20may93jaw
3627 :    
3628 : jwilke 1.77 : defined? tdefined? ;
3629 : pazsan 1.44 : needed? needed? ;
3630 :     : doer? doer? ;
3631 : anton 1.1
3632 : jwilke 1.52 \ we want to use IFDEF on compiler directives (e.g. E?) in the source, too
3633 :    
3634 :     : directive?
3635 : jwilke 1.77 bl word count [ ' target >wordlist ] literal search-wordlist
3636 : jwilke 1.52 dup IF nip THEN ;
3637 :    
3638 :     : [IFDEF] >in @ directive? swap >in !
3639 : jwilke 1.77 0= IF tdefined? ELSE name 2drop true THEN
3640 : jwilke 1.52 postpone [IF] ;
3641 :    
3642 : jwilke 1.77 : [IFUNDEF] tdefined? 0= postpone [IF] ;
3643 : anton 1.1
3644 : pazsan 1.54 Cond: [IFDEF] postpone [IFDEF] ;Cond
3645 :    
3646 :     Cond: [IFUNDEF] postpone [IFUNDEF] ;Cond
3647 :    
3648 : anton 1.1 \ C: \- \+ Conditional Compiling 09jun93jaw
3649 :    
3650 : jwilke 1.77 : C: >in @ tdefined? 0=
3651 :     IF >in ! X :
3652 : anton 1.1 ELSE drop
3653 :     BEGIN bl word dup c@
3654 : anton 1.128 IF count comment? s" ;" str= ?EXIT
3655 : anton 1.1 ELSE refill 0= ABORT" CROSS: Out of Input while C:"
3656 :     THEN
3657 :     AGAIN
3658 :     THEN ;
3659 :    
3660 : jwilke 1.74 : d? d? ;
3661 :    
3662 : jwilke 1.123 : \D ( -- "debugswitch" )
3663 : jwilke 1.74 \G doesn't skip line when debug switch is on
3664 : jwilke 1.123 D? 0= IF postpone \ THEN ;
3665 : jwilke 1.52
3666 : jwilke 1.123 : \- ( -- "wordname" )
3667 : anton 1.48 \G interprets the line if word is not defined
3668 : jwilke 1.123 tdefined? IF postpone \ THEN ;
3669 : anton 1.48
3670 : jwilke 1.123 : \+ ( -- "wordname" )
3671 : anton 1.48 \G interprets the line if word is defined
3672 : jwilke 1.123 tdefined? 0= IF postpone \ THEN ;
3673 :    
3674 :     : \? ( -- "envorinstring" )
3675 :     \G Skip line if environmental variable evaluates to false
3676 :     X has? 0= IF postpone \ THEN ;
3677 : anton 1.1
3678 : anton 1.48 Cond: \- \- ;Cond
3679 :     Cond: \+ \+ ;Cond
3680 : jwilke 1.52 Cond: \D \D ;Cond
3681 : jwilke 1.123 Cond: \? \? ;Cond
3682 : anton 1.48
3683 :     : ?? bl word find IF execute ELSE drop 0 THEN ;
3684 :    
3685 :     : needed:
3686 :     \G defines ghost for words that we want to be compiled
3687 : jwilke 1.103 BEGIN >in @ bl word c@ WHILE >in ! Ghost drop REPEAT drop ;
3688 : anton 1.48
3689 : anton 1.1 \ words that should be in minimal
3690 : jwilke 1.52
3691 :     create s-buffer 50 chars allot
3692 :    
3693 : jwilke 1.77 bigendian Constant bigendian
3694 : anton 1.1
3695 : jwilke 1.52 : here there ;
3696 : jwilke 1.67 : equ constant ;
3697 :     : mark there constant ;
3698 : pazsan 1.54
3699 :     \ compiler directives
3700 : jwilke 1.52 : >ram >ram ;
3701 :     : >rom >rom ;
3702 :     : >auto >auto ;
3703 :     : >tempdp >tempdp ;
3704 :     : tempdp> tempdp> ;
3705 :     : const constflag on ;
3706 : jwilke 1.103
3707 :     : Redefinitions-start
3708 :     \G Starts a redefinition section. Warnings are disabled and
3709 :     \G existing ghosts are reused. This is used in the kernel
3710 :     \G where ( and \ and the like are redefined
3711 :     twarnings off warnings off reuse-ghosts on ;
3712 :    
3713 :     : Redefinitions-end
3714 :     \G Ends a redefinition section. Warnings are enabled again.
3715 :     twarnings on warnings on reuse-ghosts off ;
3716 :    
3717 :     : warnings name 3 =
3718 :     IF twarnings off warnings off ELSE twarnings on warnings on THEN drop ;
3719 : jwilke 1.102
3720 : anton 1.60 : | ;
3721 :     \ : | NoHeaderFlag on ; \ This is broken (damages the last word)
3722 : jwilke 1.52
3723 : anton 1.48 : save-cross save-cross ;
3724 : jwilke 1.52 : save-region save-region ;
3725 :     : tdump swap >image swap dump ;
3726 :    
3727 : anton 1.48 also forth
3728 : jwilke 1.52 [IFDEF] Label : Label defempty? Label ; [THEN]
3729 :     [IFDEF] start-macros : start-macros defempty? start-macros ; [THEN]
3730 : jwilke 1.77 \ [IFDEF] builttag : builttag builttag ; [THEN]
3731 : anton 1.48 previous
3732 :    
3733 : jwilke 1.52 : s" [char] " parse s-buffer place s-buffer count ; \ for environment?
3734 : pazsan 1.43 : + + ;
3735 : jwilke 1.52 : 1+ 1 + ;
3736 :     : 2+ 2 + ;
3737 : pazsan 1.43 : 1- 1- ;
3738 :     : - - ;
3739 : jwilke 1.52 : and and ;
3740 :     : or or ;
3741 : pazsan 1.43 : 2* 2* ;
3742 :     : * * ;
3743 :     : / / ;
3744 :     : dup dup ;
3745 : jwilke 1.139 : ?dup ?dup ;
3746 : pazsan 1.43 : over over ;
3747 :     : swap swap ;
3748 :     : rot rot ;
3749 :     : drop drop ;
3750 : jwilke 1.139 : 2drop 2drop ;
3751 : pazsan 1.43 : = = ;
3752 : jwilke 1.122 : <> <> ;
3753 : pazsan 1.43 : 0= 0= ;
3754 :     : lshift lshift ;
3755 :     : 2/ 2/ ;
3756 : jwilke 1.139 : hex. base @ $10 base ! swap . base ! ;
3757 :     : invert invert ;
3758 : pazsan 1.159 : linkstring ( addr u n addr -- )
3759 :     X here over X @ X , swap X ! X , ht-string, X align ;
3760 : jwilke 1.103 \ : . . ;
3761 : anton 1.1
3762 : jwilke 1.79 : all-words ['] forced? IS skip? ;
3763 : pazsan 1.43 : needed-words ['] needed? IS skip? ;
3764 : jwilke 1.75 : undef-words ['] defined2? IS skip? ;
3765 :     : skipdef skipdef ;
3766 : anton 1.1
3767 : pazsan 1.40 : \ postpone \ ; immediate
3768 : pazsan 1.47 : \G T-\G ; immediate
3769 : pazsan 1.40 : ( postpone ( ; immediate
3770 : anton 1.1 : include bl word count included ;
3771 : jwilke 1.103 : included swap >image swap included ;
3772 : jwilke 1.52 : require require ;
3773 : jwilke 1.103 : needs require ;
3774 : anton 1.1 : .( [char] ) parse type ;
3775 : jwilke 1.139 : ERROR" [char] " parse
3776 :     rot
3777 :     IF cr ." *** " type ." ***" -1 ABORT" CROSS: Target error, see text above"
3778 :     ELSE 2drop
3779 :     THEN ;
3780 : jwilke 1.52 : ." [char] " parse type ;
3781 : anton 1.1 : cr cr ;
3782 :    
3783 : jwilke 1.77 : times 0 ?DO dup X c, LOOP drop ; \ used for space table creation
3784 :    
3785 :     \ only forth also cross also minimal definitions order
3786 : anton 1.1
3787 :     \ cross-compiler words
3788 :    
3789 : jwilke 1.103 : decimal decimal [g'] decimal >exec2 @ ?dup IF EXECUTE THEN ;
3790 :     : hex hex [g'] hex >exec2 @ ?dup IF EXECUTE THEN ;
3791 : anton 1.1
3792 : jwilke 1.77 \ : tudp X tudp ;
3793 :     \ : tup X tup ;
3794 :    
3795 :     : doc-off false to-doc ! ;
3796 :     : doc-on true to-doc ! ;
3797 : pazsan 1.39
3798 : jwilke 1.103 : declareunique ( "name" -- )
3799 : anton 1.127 \ Sets the unique flag for a ghost. The assembler output
3800 :     \ generates labels with the ghostname concatenated with the address
3801 :     \ while cross-compiling. The address is concatenated
3802 :     \ because we have double occurences of the same name.
3803 :     \ If we want to reference the labels from the assembler or C
3804 :     \ code we declare them unique, so the address is skipped.
3805 : jwilke 1.103 Ghost >ghost-flags dup @ <unique> or swap ! ;
3806 :    
3807 :     \ [IFDEF] dbg : dbg dbg ; [THEN]
3808 : pazsan 1.39
3809 : anton 1.1 \ for debugging...
3810 : jwilke 1.103 \ : dbg dbg ;
3811 :     : horder order ;
3812 :     : hwords words ;
3813 :     \ : words also ghosts
3814 :     \ words previous ;
3815 : jwilke 1.105 : .s .s ;
3816 : jwilke 1.122 : depth depth ;
3817 : anton 1.1 : bye bye ;
3818 : pazsan 1.99
3819 : jwilke 1.103 \ dummy
3820 :    
3821 : anton 1.1 \ turnkey direction
3822 :     : H forth ; immediate
3823 :     : T minimal ; immediate
3824 :     : G ghosts ; immediate
3825 :    
3826 :    
3827 :     \ these ones are pefered:
3828 :    
3829 : jwilke 1.74 : unlock previous forth also cross ;
3830 : jwilke 1.52
3831 : jwilke 1.77 \ also minimal
3832 : jwilke 1.103 >cross
3833 :    
3834 :     : turnkey
3835 :     ghosts-wordlist 1 set-order
3836 :     also target definitions
3837 :     also Minimal also ;
3838 :    
3839 :     >minimal
3840 :    
3841 :     : [[+++
3842 :     turnkey unlock ;
3843 : anton 1.1
3844 :     unlock definitions also minimal
3845 : jwilke 1.77
3846 : jwilke 1.103 : lock turnkey ;
3847 :    
3848 :     Defer +++]]-hook
3849 :     : +++]] +++]]-hook lock ;
3850 :    
3851 :     LOCK
3852 : jwilke 1.77 \ load cross compiler extension defined in mach file
3853 :    
3854 :     UNLOCK >CROSS
3855 :    
3856 :     [IFDEF] extend-cross extend-cross [THEN]
3857 :    
3858 :     LOCK

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help