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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help