[gforth] / gforth / cross.fs  

gforth: gforth/cross.fs


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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help