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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help