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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help