1: \ CROSS.FS The Cross-Compiler 06oct92py
2: \ Idea and implementation: Bernd Paysan (py)
3:
4: \ Copyright (C) 1995,1996,1997,1998,1999,2000 Free Software Foundation, Inc.
5:
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: \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
21:
22: 0
23: [IF]
24:
25: ToDo:
26: Crossdoc destination ./doc/crossdoc.fd makes no sense when
27: cross.fs is uses seperately. jaw
28: Do we need this char translation with >address and in branchoffset?
29: (>body also affected) jaw
30: Clean up mark> and >resolve stuff jaw
31:
32: [THEN]
33:
34: 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: : T previous Ghosts also Target ; immediate
57: : G Ghosts ; immediate
58: : H previous Forth also Cross ; immediate
59:
60: forth definitions
61:
62: : T previous Ghosts also Target ; immediate
63: : G Ghosts ; immediate
64:
65: : >cross also Cross definitions previous ;
66: : >target also Target definitions previous ;
67: : >minimal also Minimal definitions previous ;
68:
69: H
70:
71: >CROSS
72:
73: \ find out whether we are compiling with gforth
74:
75: : defined? bl word find nip ;
76: defined? emit-file defined? toupper and \ drop 0
77: [IF]
78: \ use this in a gforth system
79: : \GFORTH ; immediate
80: : \ANSI postpone \ ; immediate
81: [ELSE]
82: : \GFORTH postpone \ ; immediate
83: : \ANSI ; immediate
84: [THEN]
85:
86: \ANSI : [IFUNDEF] defined? 0= postpone [IF] ; immediate
87: \ANSI : [IFDEF] defined? postpone [IF] ; immediate
88: 0 \ANSI drop 1
89: [IF]
90: : \G postpone \ ; immediate
91: : rdrop postpone r> postpone drop ; immediate
92: : name bl word count ;
93: : bounds over + swap ;
94: : scan >r BEGIN dup WHILE over c@ r@ <> WHILE 1 /string REPEAT THEN rdrop ;
95: : linked here over @ , swap ! ;
96: : alias create , DOES> @ EXECUTE ;
97: : defer ['] noop alias ;
98: : is state @
99: IF ' >body postpone literal postpone !
100: ELSE ' >body ! THEN ; immediate
101: : 0>= 0< 0= ;
102: : d<> rot <> -rot <> or ;
103: : toupper dup [char] a [char] z 1+ within IF [char] A [char] a - + THEN ;
104: Variable ebuf
105: : emit-file ( c fd -- ior ) swap ebuf c! ebuf 1 chars rot write-file ;
106: 0a Constant #lf
107: 0d Constant #cr
108:
109: [IFUNDEF] Warnings Variable Warnings [THEN]
110:
111: \ \ Number parsing 23feb93py
112:
113: \ number? number 23feb93py
114:
115: Variable dpl
116:
117: hex
118: Create bases 10 , 2 , A , 100 ,
119: \ 16 2 10 character
120:
121: \ !! protect BASE saving wrapper against exceptions
122: : getbase ( addr u -- addr' u' )
123: over c@ [char] $ - dup 4 u<
124: IF
125: cells bases + @ base ! 1 /string
126: ELSE
127: drop
128: THEN ;
129:
130: : sign? ( addr u -- addr u flag )
131: over c@ [char] - = dup >r
132: IF
133: 1 /string
134: THEN
135: r> ;
136:
137: : s>unumber? ( addr u -- ud flag )
138: over [char] ' =
139: IF \ a ' alone is rather unusual :-)
140: drop char+ c@ 0 true EXIT
141: THEN
142: base @ >r dpl on getbase
143: 0. 2swap
144: BEGIN ( d addr len )
145: dup >r >number dup
146: WHILE \ there are characters left
147: dup r> -
148: WHILE \ the last >number parsed something
149: dup 1- dpl ! over c@ [char] . =
150: WHILE \ the current char is '.'
151: 1 /string
152: REPEAT THEN \ there are unparseable characters left
153: 2drop false
154: ELSE
155: rdrop 2drop true
156: THEN
157: r> base ! ;
158:
159: \ ouch, this is complicated; there must be a simpler way - anton
160: : s>number? ( addr len -- d f )
161: \ converts string addr len into d, flag indicates success
162: sign? >r
163: s>unumber?
164: 0= IF
165: rdrop false
166: ELSE \ no characters left, all ok
167: r>
168: IF
169: dnegate
170: THEN
171: true
172: THEN ;
173:
174: : s>number ( addr len -- d )
175: \ don't use this, there is no way to tell success
176: s>number? drop ;
177:
178: : snumber? ( c-addr u -- 0 / n -1 / d 0> )
179: s>number? 0=
180: IF
181: 2drop false EXIT
182: THEN
183: dpl @ dup 0< IF
184: nip
185: ELSE
186: 1+
187: THEN ;
188:
189: : number? ( string -- string 0 / n -1 / d 0> )
190: dup >r count snumber? dup if
191: rdrop
192: else
193: r> swap
194: then ;
195:
196: : number ( string -- d )
197: number? ?dup 0= abort" ?" 0<
198: IF
199: s>d
200: THEN ;
201:
202: [THEN]
203:
204: hex \ the defualt base for the cross-compiler is hex !!
205: \ Warnings off
206:
207: \ words that are generaly useful
208:
209: : KB 400 * ;
210: : >wordlist ( vocabulary-xt -- wordlist-struct )
211: also execute get-order swap >r 1- set-order r> ;
212:
213: : umax 2dup u< IF swap THEN drop ;
214: : umin 2dup u> IF swap THEN drop ;
215:
216: : string, ( c-addr u -- )
217: \ puts down string as cstring
218: dup c, here swap chars dup allot move ;
219:
220: : ," [char] " parse string, ;
221:
222: : SetValue ( n -- <name> )
223: \G Same behaviour as "Value" if the <name> is not defined
224: \G Same behaviour as "to" if <name> is defined
225: \G SetValue searches in the current vocabulary
226: save-input bl word >r restore-input throw r> count
227: get-current search-wordlist
228: IF drop >r
229: \ we have to set current to be topmost context wordlist
230: get-order get-order get-current swap 1+ set-order
231: r> ['] to execute
232: set-order
233: ELSE Value THEN ;
234:
235: : DefaultValue ( n -- <name> )
236: \G Same behaviour as "Value" if the <name> is not defined
237: \G DefaultValue searches in the current vocabulary
238: save-input bl word >r restore-input throw r> count
239: get-current search-wordlist
240: IF bl word drop 2drop ELSE Value THEN ;
241:
242: hex
243:
244: \ 1 Constant Cross-Flag \ to check whether assembler compiler plug-ins are
245: \ for cross-compiling
246: \ No! we use "[IFUNDEF]" there to find out whether we are target compiling!!!
247:
248: : comment? ( c-addr u -- c-addr u )
249: 2dup s" (" compare 0=
250: IF postpone (
251: ELSE 2dup s" \" compare 0= IF postpone \ THEN
252: THEN ;
253:
254: \ Begin CROSS COMPILER:
255:
256: \ debugging
257:
258: 0 [IF]
259:
260: This implements debugflags for the cross compiler and the compiled
261: images. It works identical to the has-flags in the environment.
262: The debugflags are defined in a vocabluary. If the word exists and
263: its value is true, the flag is switched on.
264:
265: [THEN]
266:
267: >CROSS
268:
269: Vocabulary debugflags \ debug flags for cross
270: also debugflags get-order over
271: Constant debugflags-wl
272: set-order previous
273:
274: : DebugFlag
275: get-current >r debugflags-wl set-current
276: SetValue
277: r> set-current ;
278:
279: : Debug? ( adr u -- flag )
280: \G return true if debug flag is defined or switched on
281: debugflags-wl search-wordlist
282: IF EXECUTE
283: ELSE false THEN ;
284:
285: : D? ( <name> -- flag )
286: \G return true if debug flag is defined or switched on
287: \G while compiling we do not return the current value but
288: bl word count debug? ;
289:
290: : [d?]
291: \G compile the value-xt so the debug flag can be switched
292: \G the flag must exist!
293: bl word count debugflags-wl search-wordlist
294: IF compile,
295: ELSE -1 ABORT" unknown debug flag"
296: \ POSTPONE false
297: THEN ; immediate
298:
299: \ \ -------------------- source file
300:
301: decimal
302:
303: Variable cross-file-list
304: 0 cross-file-list !
305: Variable target-file-list
306: 0 target-file-list !
307: Variable host-file-list
308: 0 host-file-list !
309:
310: cross-file-list Value file-list
311: 0 Value source-desc
312:
313: \ file loading
314:
315: : >fl-id 1 cells + ;
316: : >fl-name 2 cells + ;
317:
318: Variable filelist 0 filelist !
319: Create NoFile ," #load-file#"
320:
321: : loadfile ( -- adr )
322: source-desc ?dup IF >fl-name ELSE NoFile THEN ;
323:
324: : sourcefilename ( -- adr len )
325: loadfile count ;
326:
327: \ANSI : sourceline# 0 ;
328:
329: \ \ -------------------- path handling from kernel/paths.fs
330:
331: \ paths.fs path file handling 03may97jaw
332:
333: \ -Changing the search-path:
334: \ fpath+ <path> adds a directory to the searchpath
335: \ fpath= <path>|<path> makes complete now searchpath
336: \ seperator is |
337: \ .fpath displays the search path
338: \ remark I:
339: \ a ./ in the beginning of filename is expanded to the directory the
340: \ current file comes from. ./ can also be included in the search-path!
341: \ ~+/ loads from the current working directory
342:
343: \ remark II:
344: \ if there is no sufficient space for the search path increase it!
345:
346:
347: \ -Creating custom paths:
348:
349: \ It is possible to use the search mechanism on yourself.
350:
351: \ Make a buffer for the path:
352: \ create mypath 100 chars , \ maximum length (is checked)
353: \ 0 , \ real len
354: \ 100 chars allot \ space for path
355: \ use the same functions as above with:
356: \ mypath path+
357: \ mypath path=
358: \ mypath .path
359:
360: \ do a open with the search path:
361: \ open-path-file ( adr len path -- fd adr len ior )
362: \ the file is opened read-only; if the file is not found an error is generated
363:
364: \ questions to: wilke@jwdt.com
365:
366: [IFUNDEF] +place
367: : +place ( adr len adr )
368: 2dup >r >r
369: dup c@ char+ + swap move
370: r> r> dup c@ rot + swap c! ;
371: [THEN]
372:
373: [IFUNDEF] place
374: : place ( c-addr1 u c-addr2 )
375: 2dup c! char+ swap move ;
376: [THEN]
377:
378: \ if we have path handling, use this and the setup of it
379: [IFUNDEF] open-fpath-file
380:
381: create sourcepath 1024 chars , 0 , 1024 chars allot \ !! make this dynamic
382: sourcepath value fpath
383:
384: : also-path ( adr len path^ -- )
385: >r
386: \ len check
387: r@ cell+ @ over + r@ @ u> ABORT" path buffer too small!"
388: \ copy into
389: tuck r@ cell+ dup @ cell+ + swap cmove
390: \ make delimiter
391: 0 r@ cell+ dup @ cell+ + 2 pick + c! 1 + r> cell+ +!
392: ;
393:
394: : only-path ( adr len path^ -- )
395: dup 0 swap cell+ ! also-path ;
396:
397: : path+ ( path-addr "dir" -- ) \ gforth
398: \G Add the directory @var{dir} to the search path @var{path-addr}.
399: name rot also-path ;
400:
401: : fpath+ ( "dir" ) \ gforth
402: \G Add directory @var{dir} to the Forth search path.
403: fpath path+ ;
404:
405: : path= ( path-addr "dir1|dir2|dir3" ) \ gforth
406: \G Make a complete new search path; the path separator is |.
407: name 2dup bounds ?DO i c@ [char] | = IF 0 i c! THEN LOOP
408: rot only-path ;
409:
410: : fpath= ( "dir1|dir2|dir3" ) \ gforth
411: \G Make a complete new Forth search path; the path separator is |.
412: fpath path= ;
413:
414: : path>counted cell+ dup cell+ swap @ ;
415:
416: : next-path ( adr len -- adr2 len2 )
417: 2dup 0 scan
418: dup 0= IF 2drop 0 -rot 0 -rot EXIT THEN
419: >r 1+ -rot r@ 1- -rot
420: r> - ;
421:
422: : previous-path ( path^ -- )
423: dup path>counted
424: BEGIN tuck dup WHILE repeat ;
425:
426: : .path ( path-addr -- ) \ gforth
427: \G Display the contents of the search path @var{path-addr}.
428: path>counted
429: BEGIN next-path dup WHILE type space REPEAT 2drop 2drop ;
430:
431: : .fpath ( -- ) \ gforth
432: \G Display the contents of the Forth search path.
433: fpath .path ;
434:
435: : absolut-path? ( addr u -- flag ) \ gforth
436: \G A path is absolute if it starts with a / or a ~ (~ expansion),
437: \G or if it is in the form ./*, extended regexp: ^[/~]|./, or if
438: \G it has a colon as second character ("C:..."). Paths simply
439: \G containing a / are not absolute!
440: 2dup 2 u> swap 1+ c@ [char] : = and >r \ dos absoulte: c:/....
441: over c@ [char] / = >r
442: over c@ [char] ~ = >r
443: \ 2dup 3 min S" ../" compare 0= r> or >r \ not catered for in expandtopic
444: 2 min S" ./" compare 0=
445: r> r> r> or or or ;
446:
447: Create ofile 0 c, 255 chars allot
448: Create tfile 0 c, 255 chars allot
449:
450: : pathsep? dup [char] / = swap [char] \ = or ;
451:
452: : need/ ofile dup c@ + c@ pathsep? 0= IF s" /" ofile +place THEN ;
453:
454: : extractpath ( adr len -- adr len2 )
455: BEGIN dup WHILE 1-
456: 2dup + c@ pathsep? IF EXIT THEN
457: REPEAT ;
458:
459: : remove~+ ( -- )
460: ofile count 3 min s" ~+/" compare 0=
461: IF
462: ofile count 3 /string ofile place
463: THEN ;
464:
465: : expandtopic ( -- ) \ stack effect correct? - anton
466: \ expands "./" into an absolute name
467: ofile count 2 min s" ./" compare 0=
468: IF
469: ofile count 1 /string tfile place
470: 0 ofile c! sourcefilename extractpath ofile place
471: ofile c@ IF need/ THEN
472: tfile count over c@ pathsep? IF 1 /string THEN
473: ofile +place
474: THEN ;
475:
476: : compact.. ( adr len -- adr2 len2 )
477: \ deletes phrases like "xy/.." out of our directory name 2dec97jaw
478: over swap
479: BEGIN dup WHILE
480: dup >r '/ scan 2dup 4 min s" /../" compare 0=
481: IF
482: dup r> - >r 4 /string over r> + 4 -
483: swap 2dup + >r move dup r> over -
484: ELSE
485: rdrop dup 1 min /string
486: THEN
487: REPEAT drop over - ;
488:
489: : reworkdir ( -- )
490: remove~+
491: ofile count compact..
492: nip ofile c! ;
493:
494: : open-ofile ( -- fid ior )
495: \G opens the file whose name is in ofile
496: expandtopic reworkdir
497: ofile count r/o open-file ;
498:
499: : check-path ( adr1 len1 adr2 len2 -- fd 0 | 0 <>0 )
500: 0 ofile ! >r >r ofile place need/
501: r> r> ofile +place
502: open-ofile ;
503:
504: : open-path-file ( addr1 u1 path-addr -- wfileid addr2 u2 0 | ior ) \ gforth
505: \G Look in path @var{path-addr} for the file specified by @var{addr1 u1}.
506: \G If found, the resulting path and an open file descriptor
507: \G are returned. If the file is not found, @var{ior} is non-zero.
508: >r
509: 2dup absolut-path?
510: IF rdrop
511: ofile place open-ofile
512: dup 0= IF >r ofile count r> THEN EXIT
513: ELSE r> path>counted
514: BEGIN next-path dup
515: WHILE 5 pick 5 pick check-path
516: 0= IF >r 2drop 2drop r> ofile count 0 EXIT ELSE drop THEN
517: REPEAT
518: 2drop 2drop 2drop -38
519: THEN ;
520:
521: : open-fpath-file ( addr1 u1 -- wfileid addr2 u2 0 | ior ) \ gforth
522: \G Look in the Forth search path for the file specified by @var{addr1 u1}.
523: \G If found, the resulting path and an open file descriptor
524: \G are returned. If the file is not found, @var{ior} is non-zero.
525: fpath open-path-file ;
526:
527: fpath= ~+
528:
529: [THEN]
530:
531: \ \ -------------------- include require 13may99jaw
532:
533: >CROSS
534:
535: : add-included-file ( adr len -- adr )
536: dup >fl-name char+ allocate throw >r
537: file-list @ r@ ! r@ file-list !
538: r@ >fl-name place r> ;
539:
540: : included? ( c-addr u -- f )
541: file-list
542: BEGIN @ dup
543: WHILE >r 2dup r@ >fl-name count compare 0=
544: IF rdrop 2drop true EXIT THEN
545: r>
546: REPEAT
547: 2drop drop false ;
548:
549: false DebugFlag showincludedfiles
550:
551: : included1 ( fd adr u -- )
552: \ include file adr u / fd
553: \ we don't use fd with include-file, because the forth system
554: \ doesn't know the name of the file to get a nice error report
555: [d?] showincludedfiles
556: IF cr ." Including: " 2dup type ." ..." THEN
557: rot close-file throw
558: source-desc >r
559: add-included-file to source-desc
560: sourcefilename
561: ['] included catch
562: r> to source-desc
563: throw ;
564:
565: : included ( adr len -- )
566: cross-file-list to file-list
567: open-fpath-file throw
568: included1 ;
569:
570: : required ( adr len -- )
571: cross-file-list to file-list
572: open-fpath-file throw \ 2dup cr ." R:" type
573: 2dup included?
574: IF 2drop close-file throw
575: ELSE included1
576: THEN ;
577:
578: : include bl word count included ;
579:
580: : require bl word count required ;
581:
582: 0 [IF]
583:
584: also forth definitions previous
585:
586: : included ( adr len -- ) included ;
587:
588: : required ( adr len -- ) required ;
589:
590: : include include ;
591:
592: : require require ;
593:
594: [THEN]
595:
596: >CROSS
597: hex
598:
599: \ \ -------------------- Error Handling 05aug97jaw
600:
601: \ Flags
602:
603: also forth definitions \ these values may be predefined before
604: \ the cross-compiler is loaded
605:
606: false DefaultValue stack-warn \ check on empty stack at any definition
607: false DefaultValue create-forward-warn \ warn on forward declaration of created words
608:
609: previous >CROSS
610:
611: : .dec
612: base @ decimal swap . base ! ;
613:
614: : .sourcepos
615: cr sourcefilename type ." :"
616: sourceline# .dec ;
617:
618: : warnhead
619: \G display error-message head
620: \G perhaps with linenumber and filename
621: .sourcepos ." Warning: " ;
622:
623: : empty? depth IF .sourcepos ." Stack not empty!" THEN ;
624:
625: stack-warn [IF]
626: : defempty? empty? ;
627: [ELSE]
628: : defempty? ; immediate
629: [THEN]
630:
631: \ \ GhostNames Ghosts 9may93jaw
632:
633: \ second name source to search trough list
634:
635: VARIABLE GhostNames
636: 0 GhostNames !
637:
638: : GhostName ( -- addr )
639: align here GhostNames @ , GhostNames ! here 0 ,
640: bl word count
641: \ 2dup type space
642: string, \ !! cfalign ?
643: align ;
644:
645: \ Ghost Builder 06oct92py
646:
647: \ <T T> new version with temp variable 10may93jaw
648:
649: VARIABLE VocTemp
650:
651: : <T get-current VocTemp ! also Ghosts definitions ;
652: : T> previous VocTemp @ set-current ;
653:
654: hex
655: 4711 Constant <fwd> 4712 Constant <res>
656: 4713 Constant <imm> 4714 Constant <do:>
657: 4715 Constant <skip>
658:
659: \ iForth makes only immediate directly after create
660: \ make atonce trick! ?
661:
662: Variable atonce atonce off
663:
664: : NoExec true ABORT" CROSS: Don't execute ghost, or immediate target word" ;
665:
666: : GhostHeader <fwd> , 0 , ['] NoExec , ;
667:
668: : >magic ; \ type of ghost
669: : >link cell+ ; \ pointer where ghost is in target, or if unresolved
670: \ points to the where we have to resolve (linked-list)
671: : >exec cell+ cell+ ; \ execution symantics (while target compiling) of ghost
672: : >end 3 cells + ; \ room for additional tags
673: \ for builder (create, variable...) words the
674: \ execution symantics of words built are placed here
675:
676: Variable executed-ghost \ last executed ghost, needed in tcreate and gdoes>
677: Variable last-ghost \ last ghost that is created
678: Variable last-header-ghost \ last ghost definitions with header
679:
680: : Make-Ghost ( "name" -- ghost )
681: >in @ GhostName swap >in !
682: <T Create atonce @ IF immediate atonce off THEN
683: here tuck swap ! ghostheader T>
684: dup last-ghost !
685: DOES> dup executed-ghost ! >exec @ execute ;
686:
687: \ ghost words 14oct92py
688: \ changed: 10may93py/jaw
689:
690: : gfind ( string -- ghost true/1 / string false )
691: \ searches for string in word-list ghosts
692: dup count [ ' ghosts >wordlist ] Literal search-wordlist
693: dup IF >r >body nip r> THEN ;
694:
695: : gdiscover ( xt -- ghost true | xt false )
696: GhostNames
697: BEGIN @ dup
698: WHILE 2dup
699: cell+ @ dup >magic @ <fwd> <>
700: >r >link @ = r> and
701: IF cell+ @ nip true EXIT THEN
702: REPEAT
703: drop false ;
704:
705: VARIABLE Already
706:
707: : ghost ( "name" -- ghost )
708: Already off
709: >in @ bl word gfind IF atonce off Already on nip EXIT THEN
710: drop >in ! Make-Ghost ;
711:
712: : >ghostname ( ghost -- adr len )
713: GhostNames
714: BEGIN @ dup
715: WHILE 2dup cell+ @ =
716: UNTIL nip 2 cells + count
717: ELSE 2drop
718: \ true abort" CROSS: Ghostnames inconsistent"
719: s" ?!?!?!"
720: THEN ;
721:
722: : .ghost ( ghost -- ) >ghostname type ;
723:
724: \ ' >ghostname ALIAS @name
725:
726: : forward? ( ghost -- flag )
727: >magic @ <fwd> = ;
728:
729: : undefined? ( ghost -- flag )
730: >magic @ dup <fwd> = swap <skip> = or ;
731:
732: \ Predefined ghosts 12dec92py
733:
734: ghost 0= drop
735: ghost branch ghost ?branch 2drop
736: ghost (do) ghost (?do) 2drop
737: ghost (for) drop
738: ghost (loop) ghost (+loop) 2drop
739: ghost (next) drop
740: ghost unloop ghost ;S 2drop
741: ghost lit ghost (compile) ghost ! 2drop drop
742: ghost (does>) ghost noop 2drop
743: ghost (.") ghost (S") ghost (ABORT") 2drop drop
744: ghost ' drop
745: ghost :docol ghost :doesjump ghost :dodoes 2drop drop
746: ghost :dovar drop
747: ghost over ghost = ghost drop 2drop drop
748: ghost - drop
749: ghost 2drop drop
750: ghost 2dup drop
751:
752: \ \ Parameter for target systems 06oct92py
753:
754: \ we define it ans like...
755: wordlist Constant target-environment
756:
757: VARIABLE env-current \ save information of current dictionary to restore with environ>
758:
759: : >ENVIRON get-current env-current ! target-environment set-current ;
760: : ENVIRON> env-current @ set-current ;
761:
762: >TARGET
763:
764: : environment? ( adr len -- [ x ] true | false )
765: target-environment search-wordlist
766: IF execute true ELSE false THEN ;
767:
768: : e? bl word count T environment? H 0= ABORT" environment variable not defined!" ;
769:
770: : has? bl word count T environment? H
771: IF \ environment variable is present, return its value
772: ELSE \ environment variable is not present, return false
773: false \ debug true ABORT" arg"
774: THEN ;
775:
776: : $has? T environment? H IF ELSE false THEN ;
777:
778: >ENVIRON get-order get-current swap 1+ set-order
779: true SetValue compiler
780: true SetValue cross
781: true SetValue standard-threading
782: >TARGET previous
783:
784: 0
785: [IFDEF] mach-file mach-file count 1 [THEN]
786: [IFDEF] machine-file machine-file 1 [THEN]
787: [IF] included hex drop
788: [ELSE] cr ." No machine description!" ABORT
789: [THEN]
790:
791: >ENVIRON
792:
793: T has? ec H
794: [IF]
795: false DefaultValue relocate
796: false DefaultValue file
797: false DefaultValue OS
798: false DefaultValue prims
799: false DefaultValue floating
800: false DefaultValue glocals
801: false DefaultValue dcomps
802: false DefaultValue hash
803: false DefaultValue xconds
804: false DefaultValue header
805: false DefaultValue new-input
806: [THEN]
807:
808: true DefaultValue interpreter
809: true DefaultValue ITC
810: false DefaultValue rom
811: true DefaultValue standardthreading
812:
813: >TARGET
814: s" relocate" T environment? H
815: [IF] SetValue NIL
816: [ELSE] >ENVIRON T NIL H SetValue relocate
817: [THEN]
818:
819: >CROSS
820:
821: \ \ Create additional parameters 19jan95py
822:
823: \ currently cross only works for host machines with address-unit-bits
824: \ eual to 8 because of s! and sc!
825: \ but I start to query the environment just to modularize a little bit
826:
827: : check-address-unit-bits ( -- )
828: \ s" ADDRESS-UNIT-BITS" environment?
829: \ IF 8 <> ELSE true THEN
830: \ ABORT" ADDRESS-UNIT-BITS unknown or not equal to 8!"
831:
832: \ shit, this doesn't work because environment? is only defined for
833: \ gforth.fi and not kernl???.fi
834: ;
835:
836: check-address-unit-bits
837: 8 Constant bits/byte \ we define: byte is address-unit
838:
839: 1 bits/byte lshift Constant maxbyte
840: \ this sets byte size for the target machine, (probably right guess) jaw
841:
842: T
843: NIL Constant TNIL
844: cell Constant tcell
845: cell<< Constant tcell<<
846: cell>bit Constant tcell>bit
847: bits/char Constant tbits/char
848: bits/char H bits/byte T /
849: Constant tchar
850: float Constant tfloat
851: 1 bits/char lshift Constant tmaxchar
852: [IFUNDEF] bits/byte
853: 8 Constant tbits/byte
854: [ELSE]
855: bits/byte Constant tbits/byte
856: [THEN]
857: H
858: tbits/char bits/byte / Constant tbyte
859:
860:
861: \ Variables 06oct92py
862:
863: Variable image
864: Variable tlast TNIL tlast ! \ Last name field
865: Variable tlastcfa \ Last code field
866: Variable tdoes \ Resolve does> calls
867: Variable bit$
868:
869: \ statistics 10jun97jaw
870:
871: Variable headers-named 0 headers-named !
872: Variable user-vars 0 user-vars !
873:
874: : target>bitmask-size ( u1 -- u2 )
875: 1- tcell>bit rshift 1+ ;
876:
877: : allocatetarget ( size --- adr )
878: dup allocate ABORT" CROSS: No memory for target"
879: swap over swap erase ;
880:
881: \ \ memregion.fs
882:
883:
884: Variable last-defined-region \ pointer to last defined region
885: Variable region-link \ linked list with all regions
886: Variable mirrored-link \ linked list for mirrored regions
887: 0 dup mirrored-link ! region-link !
888:
889:
890: : >rname 6 cells + ;
891: : >rbm 5 cells + ;
892: : >rmem 4 cells + ;
893: : >rlink 3 cells + ;
894: : >rdp 2 cells + ;
895: : >rlen cell+ ;
896: : >rstart ;
897:
898:
899: : region ( addr len -- ) \G create a new region
900: \ check whether predefined region exists
901: save-input bl word find >r >r restore-input throw r> r> 0=
902: IF \ make region
903: drop
904: save-input create restore-input throw
905: here last-defined-region !
906: over ( startaddr ) , ( length ) , ( dp ) ,
907: region-link linked 0 , 0 , bl word count string,
908: ELSE \ store new parameters in region
909: bl word drop
910: >body >r r@ last-defined-region !
911: r@ >rlen ! dup r@ >rstart ! r> >rdp !
912: THEN ;
913:
914: : borders ( region -- startaddr endaddr ) \G returns lower and upper region border
915: dup >rstart @ swap >rlen @ over + ;
916:
917: : extent ( region -- startaddr len ) \G returns the really used area
918: dup >rstart @ swap >rdp @ over - ;
919:
920: : area ( region -- startaddr totallen ) \G returns the total area
921: dup >rstart @ swap >rlen @ ;
922:
923: : mirrored \G mark a region as mirrored
924: mirrored-link
925: align linked last-defined-region @ , ;
926:
927: : .addr ( u -- )
928: \G prints a 16 or 32 Bit nice hex value
929: base @ >r hex
930: tcell 2 u>
931: IF s>d <# # # # # [char] . hold # # # # #> type
932: ELSE s>d <# # # # # # #> type
933: THEN r> base ! ;
934:
935: : .regions \G display region statistic
936:
937: \ we want to list the regions in the right order
938: \ so first collect all regions on stack
939: 0 region-link @
940: BEGIN dup WHILE dup @ REPEAT drop
941: BEGIN dup
942: WHILE cr
943: 0 >rlink - >r
944: r@ >rname count tuck type
945: 12 swap - 0 max spaces space
946: ." Start: " r@ >rstart @ dup .addr space
947: ." End: " r@ >rlen @ + .addr space
948: ." DP: " r> >rdp @ .addr
949: REPEAT drop
950: s" rom" T $has? H 0= ?EXIT
951: cr ." Mirrored:"
952: mirrored-link @
953: BEGIN dup
954: WHILE space dup cell+ @ >rname count type @
955: REPEAT drop cr
956: ;
957:
958: \ -------- predefined regions
959:
960: 0 0 region address-space
961: \ total memory addressed and used by the target system
962:
963: 0 0 region dictionary
964: \ rom area for the compiler
965:
966: T has? rom H
967: [IF]
968: 0 0 region ram-dictionary mirrored
969: \ ram area for the compiler
970: [ELSE]
971: ' dictionary ALIAS ram-dictionary
972: [THEN]
973:
974: 0 0 region return-stack
975:
976: 0 0 region data-stack
977:
978: 0 0 region tib-region
979:
980: ' dictionary ALIAS rom-dictionary
981:
982:
983: : setup-target ( -- ) \G initialize targets memory space
984: s" rom" T $has? H
985: IF \ check for ram and rom...
986: \ address-space area nip 0<>
987: ram-dictionary area nip 0<>
988: rom-dictionary area nip 0<>
989: and 0=
990: ABORT" CROSS: define address-space, rom- , ram-dictionary, with rom-support!"
991: THEN
992: address-space area nip
993: IF
994: address-space area
995: ELSE
996: dictionary area
997: THEN
998: nip 0=
999: ABORT" CROSS: define at least address-space or dictionary!!"
1000:
1001: \ allocate target for each region
1002: region-link
1003: BEGIN @ dup
1004: WHILE dup
1005: 0 >rlink - >r
1006: r@ >rlen @
1007: IF \ allocate mem
1008: r@ >rlen @ dup
1009:
1010: allocatetarget dup image !
1011: r@ >rmem !
1012:
1013: target>bitmask-size allocatetarget
1014: dup bit$ !
1015: r> >rbm !
1016:
1017: ELSE r> drop THEN
1018: REPEAT drop ;
1019:
1020: \ MakeKernal 22feb99jaw
1021:
1022: : makekernel ( targetsize -- targetsize )
1023: dup dictionary >rlen ! setup-target ;
1024:
1025: >MINIMAL
1026: : makekernel makekernel ;
1027: >CROSS
1028:
1029: \ \ switched tdp for rom support 03jun97jaw
1030:
1031: \ second value is here to store some maximal value for statistics
1032: \ tempdp is also embedded here but has nothing to do with rom support
1033: \ (needs switched dp)
1034:
1035: variable tempdp 0 , \ temporary dp for resolving
1036: variable tempdp-save
1037:
1038: 0 [IF]
1039: variable romdp 0 , \ Dictionary-Pointer for ramarea
1040: variable ramdp 0 , \ Dictionary-Pointer for romarea
1041:
1042: \
1043: variable sramdp \ start of ram-area for forth
1044: variable sromdp \ start of rom-area for forth
1045:
1046: [THEN]
1047:
1048:
1049: 0 value tdp
1050: variable fixed \ flag: true: no automatic switching
1051: \ false: switching is done automatically
1052:
1053: \ Switch-Policy:
1054: \
1055: \ a header is always compiled into rom
1056: \ after a created word (create and variable) compilation goes to ram
1057: \
1058: \ Be careful: If you want to make the data behind create into rom
1059: \ you have to put >rom before create!
1060:
1061: variable constflag constflag off
1062:
1063: : activate ( region -- )
1064: \G next code goes to this region
1065: >rdp to tdp ;
1066:
1067: : (switchram)
1068: fixed @ ?EXIT s" rom" T $has? H 0= ?EXIT
1069: ram-dictionary activate ;
1070:
1071: : switchram
1072: constflag @
1073: IF constflag off ELSE (switchram) THEN ;
1074:
1075: : switchrom
1076: fixed @ ?EXIT rom-dictionary activate ;
1077:
1078: : >tempdp ( addr -- )
1079: tdp tempdp-save ! tempdp to tdp tdp ! ;
1080: : tempdp> ( -- )
1081: tempdp-save @ to tdp ;
1082:
1083: : >ram fixed off (switchram) fixed on ;
1084: : >rom fixed off switchrom fixed on ;
1085: : >auto fixed off switchrom ;
1086:
1087:
1088:
1089: \ : romstart dup sromdp ! romdp ! ;
1090: \ : ramstart dup sramdp ! ramdp ! ;
1091:
1092: \ default compilation goes to rom
1093: \ when romable support is off, only the rom switch is used (!!)
1094: >auto
1095:
1096: : there tdp @ ;
1097:
1098: >TARGET
1099:
1100: \ \ Target Memory Handling
1101:
1102: \ Byte ordering and cell size 06oct92py
1103:
1104: : cell+ tcell + ;
1105: : cells tcell<< lshift ;
1106: : chars tchar * ;
1107: : char+ tchar + ;
1108: : floats tfloat * ;
1109:
1110: >CROSS
1111: : cell/ tcell<< rshift ;
1112: >TARGET
1113: 20 CONSTANT bl
1114: \ TNIL Constant NIL
1115:
1116: >CROSS
1117:
1118: bigendian
1119: [IF]
1120: : S! ( n addr -- ) >r s>d r> tcell bounds swap 1-
1121: DO maxbyte ud/mod rot I c! -1 +LOOP 2drop ;
1122: : S@ ( addr -- n ) >r 0 0 r> tcell bounds
1123: DO maxbyte * swap maxbyte um* rot + swap I c@ + swap LOOP d>s ;
1124: : Sc! ( n addr -- ) >r s>d r> tchar bounds swap 1-
1125: DO maxbyte ud/mod rot I c! -1 +LOOP 2drop ;
1126: : Sc@ ( addr -- n ) >r 0 0 r> tchar bounds
1127: DO maxbyte * swap maxbyte um* rot + swap I c@ + swap LOOP d>s ;
1128: [ELSE]
1129: : S! ( n addr -- ) >r s>d r> tcell bounds
1130: DO maxbyte ud/mod rot I c! LOOP 2drop ;
1131: : S@ ( addr -- n ) >r 0 0 r> tcell bounds swap 1-
1132: DO maxbyte * swap maxbyte um* rot + swap I c@ + swap -1 +LOOP d>s ;
1133: : Sc! ( n addr -- ) >r s>d r> tchar bounds
1134: DO maxbyte ud/mod rot I c! LOOP 2drop ;
1135: : Sc@ ( addr -- n ) >r 0 0 r> tchar bounds swap 1-
1136: DO maxbyte * swap maxbyte um* rot + swap I c@ + swap -1 +LOOP d>s ;
1137: [THEN]
1138:
1139: : taddr>region ( taddr -- region | 0 )
1140: \G finds for a target-address the correct region
1141: \G returns 0 if taddr is not in range of a target memory region
1142: region-link
1143: BEGIN @ dup
1144: WHILE dup >r
1145: 0 >rlink - >r
1146: r@ >rlen @
1147: IF dup r@ borders within
1148: IF r> r> drop nip EXIT THEN
1149: THEN
1150: r> drop
1151: r>
1152: REPEAT
1153: 2drop 0 ;
1154:
1155: : (>regionimage) ( taddr -- 'taddr )
1156: dup
1157: \ find region we want to address
1158: taddr>region dup 0= ABORT" Address out of range!"
1159: >r
1160: \ calculate offset in region
1161: r@ >rstart @ -
1162: \ add regions real address in our memory
1163: r> >rmem @ + ;
1164:
1165: \ Bit string manipulation 06oct92py
1166: \ 9may93jaw
1167: CREATE Bittable 80 c, 40 c, 20 c, 10 c, 8 c, 4 c, 2 c, 1 c,
1168: : bits ( n -- n ) chars Bittable + c@ ;
1169:
1170: : >bit ( addr n -- c-addr mask ) 8 /mod rot + swap bits ;
1171: : +bit ( addr n -- ) >bit over c@ or swap c! ;
1172: : -bit ( addr n -- ) >bit invert over c@ and swap c! ;
1173:
1174: : (relon) ( taddr -- )
1175: [ [IFDEF] fd-relocation-table ]
1176: s" +" fd-relocation-table write-file throw
1177: dup s>d <# #s #> fd-relocation-table write-line throw
1178: [ [THEN] ]
1179: bit$ @ swap cell/ +bit ;
1180:
1181: : (reloff) ( taddr -- )
1182: [ [IFDEF] fd-relocation-table ]
1183: s" -" fd-relocation-table write-file throw
1184: dup s>d <# #s #> fd-relocation-table write-line throw
1185: [ [THEN] ]
1186: bit$ @ swap cell/ -bit ;
1187:
1188: : (>image) ( taddr -- absaddr ) image @ + ;
1189:
1190: DEFER >image
1191: DEFER relon
1192: DEFER reloff
1193: DEFER correcter
1194:
1195: T has? relocate H
1196: [IF]
1197: ' (relon) IS relon
1198: ' (reloff) IS reloff
1199: ' (>image) IS >image
1200: [ELSE]
1201: ' drop IS relon
1202: ' drop IS reloff
1203: ' (>regionimage) IS >image
1204: [THEN]
1205:
1206: \ Target memory access 06oct92py
1207:
1208: : align+ ( taddr -- rest )
1209: tcell tuck 1- and - [ tcell 1- ] Literal and ;
1210: : cfalign+ ( taddr -- rest )
1211: \ see kernel.fs:cfaligned
1212: /maxalign tuck 1- and - [ /maxalign 1- ] Literal and ;
1213:
1214: >TARGET
1215: : aligned ( taddr -- ta-addr ) dup align+ + ;
1216: \ assumes cell alignment granularity (as GNU C)
1217:
1218: : cfaligned ( taddr1 -- taddr2 )
1219: \ see kernel.fs
1220: dup cfalign+ + ;
1221:
1222: : @ ( taddr -- w ) >image S@ ;
1223: : ! ( w taddr -- ) >image S! ;
1224: : c@ ( taddr -- char ) >image Sc@ ;
1225: : c! ( char taddr -- ) >image Sc! ;
1226: : 2@ ( taddr -- x1 x2 ) T dup cell+ @ swap @ H ;
1227: : 2! ( x1 x2 taddr -- ) T tuck ! cell+ ! H ;
1228:
1229: \ Target compilation primitives 06oct92py
1230: \ included A! 16may93jaw
1231:
1232: : here ( -- there ) there ;
1233: : allot ( n -- ) tdp +! ;
1234: : , ( w -- ) T here H tcell T allot ! H ;
1235: : c, ( char -- ) T here H tchar T allot c! H ;
1236: : align ( -- ) T here H align+ 0 ?DO bl T c, H tchar +LOOP ;
1237: : cfalign ( -- )
1238: T here H cfalign+ 0 ?DO bl T c, H tchar +LOOP ;
1239:
1240: : >address dup 0>= IF tbyte / THEN ; \ ?? jaw
1241: : A! swap >address swap dup relon T ! H ;
1242: : A, ( w -- ) >address T here H relon T , H ;
1243:
1244: >CROSS
1245:
1246: : tcmove ( source dest len -- )
1247: \G cmove in target memory
1248: tchar * bounds
1249: ?DO dup T c@ H I T c! H 1+
1250: tchar +LOOP drop ;
1251:
1252: \ \ Load Assembler
1253:
1254: >TARGET
1255: H also Forth definitions
1256:
1257: : X bl word count [ ' target >wordlist ] Literal search-wordlist
1258: IF state @ IF compile,
1259: ELSE execute THEN
1260: ELSE -1 ABORT" Cross: access method not supported!"
1261: THEN ; immediate
1262:
1263: [IFDEF] asm-include asm-include [THEN] hex
1264:
1265: previous
1266: >CROSS H
1267:
1268: \ \ -------------------- Compiler Plug Ins 01aug97jaw
1269:
1270: \ Compiler States
1271:
1272: Variable comp-state
1273: 0 Constant interpreting
1274: 1 Constant compiling
1275: 2 Constant resolving
1276: 3 Constant assembling
1277:
1278: Defer lit, ( n -- )
1279: Defer alit, ( n -- )
1280:
1281: Defer branch, ( target-addr -- ) \ compiles a branch
1282: Defer ?branch, ( target-addr -- ) \ compiles a ?branch
1283: Defer branchmark, ( -- branch-addr ) \ reserves room for a branch
1284: Defer ?branchmark, ( -- branch-addr ) \ reserves room for a ?branch
1285: Defer ?domark, ( -- branch-addr ) \ reserves room for a ?do branch
1286: Defer branchto, ( -- ) \ actual program position is target of a branch (do e.g. alignment)
1287: Defer branchtoresolve, ( branch-addr -- ) \ resolves a forward reference from branchmark
1288: Defer branchfrom, ( -- ) \ ?!
1289: Defer branchtomark, ( -- target-addr ) \ marks a branch destination
1290:
1291: Defer colon, ( tcfa -- ) \ compiles call to tcfa at current position
1292: Defer colonmark, ( -- addr ) \ marks a colon call
1293: Defer colon-resolve ( tcfa addr -- )
1294:
1295: Defer addr-resolve ( target-addr addr -- )
1296: Defer doer-resolve ( ghost res-pnt target-addr addr -- ghost res-pnt )
1297:
1298: Defer do, ( -- do-token )
1299: Defer ?do, ( -- ?do-token )
1300: Defer for, ( -- for-token )
1301: Defer loop, ( do-token / ?do-token -- )
1302: Defer +loop, ( do-token / ?do-token -- )
1303: Defer next, ( for-token )
1304:
1305: [IFUNDEF] ca>native
1306: defer ca>native
1307: [THEN]
1308:
1309: >TARGET
1310: DEFER >body \ we need the system >body
1311: \ and the target >body
1312: >CROSS
1313: T 2 cells H VALUE xt>body
1314: DEFER doprim, \ compiles start of a primitive
1315: DEFER docol, \ compiles start of a colon definition
1316: DEFER doer,
1317: DEFER fini, \ compiles end of definition ;s
1318: DEFER doeshandler,
1319: DEFER dodoes,
1320:
1321: DEFER ]comp \ starts compilation
1322: DEFER comp[ \ ends compilation
1323:
1324: : (cc) T a, H ; ' (cc) IS colon,
1325:
1326: : (cr) >tempdp ]comp colon, comp[ tempdp> ; ' (cr) IS colon-resolve
1327: : (ar) T ! H ; ' (ar) IS addr-resolve
1328: : (dr) ( ghost res-pnt target-addr addr )
1329: >tempdp drop over
1330: dup >magic @ <do:> =
1331: IF doer,
1332: ELSE dodoes,
1333: THEN
1334: tempdp> ; ' (dr) IS doer-resolve
1335:
1336: : (cm) ( -- addr )
1337: T here align H
1338: -1 colon, ; ' (cm) IS colonmark,
1339:
1340: >TARGET
1341: : compile, colon, ;
1342: >CROSS
1343:
1344: \ resolve structure
1345:
1346: : >next ; \ link to next field
1347: : >tag cell+ ; \ indecates type of reference: 0: call, 1: address, 2: doer
1348: : >taddr cell+ cell+ ;
1349: : >ghost 3 cells + ;
1350: : >file 4 cells + ;
1351: : >line 5 cells + ;
1352:
1353: : (refered) ( ghost addr tag -- )
1354: \G creates a reference to ghost at address taddr
1355: rot >r here r@ >link @ , r> >link !
1356: ( taddr tag ) ,
1357: ( taddr ) ,
1358: last-header-ghost @ ,
1359: loadfile ,
1360: sourceline# ,
1361: ;
1362:
1363: : refered ( ghost tag -- )
1364: \G creates a resolve structure
1365: T here aligned H swap (refered)
1366: ;
1367:
1368: : killref ( addr ghost -- )
1369: \G kills a forward reference to ghost at position addr
1370: \G this is used to eleminate a :dovar refence after making a DOES>
1371: dup >magic @ <fwd> <> IF 2drop EXIT THEN
1372: swap >r >link
1373: BEGIN dup @ dup ( addr last this )
1374: WHILE dup >taddr @ r@ =
1375: IF @ over !
1376: ELSE nip THEN
1377: REPEAT rdrop 2drop
1378: ;
1379:
1380: Defer resolve-warning
1381:
1382: : reswarn-test ( ghost res-struct -- ghost res-struct )
1383: over cr ." Resolving " .ghost dup ." in " >ghost @ .ghost ;
1384:
1385: : reswarn-forward ( ghost res-struct -- ghost res-struct )
1386: over warnhead .ghost dup ." is referenced in "
1387: >ghost @ .ghost ;
1388:
1389: \ ' reswarn-test IS resolve-warning
1390:
1391: \ resolve 14oct92py
1392:
1393: : resolve-loop ( ghost resolve-list tcfa -- )
1394: >r
1395: BEGIN dup WHILE
1396: \ dup >tag @ 2 = IF reswarn-forward THEN
1397: resolve-warning
1398: r@ over >taddr @
1399: 2 pick >tag @
1400: CASE 0 OF colon-resolve ENDOF
1401: 1 OF addr-resolve ENDOF
1402: 2 OF doer-resolve ENDOF
1403: ENDCASE
1404: @ \ next list element
1405: REPEAT 2drop rdrop
1406: ;
1407:
1408: \ : resolve-loop ( ghost tcfa -- ghost tcfa )
1409: \ >r dup >link @
1410: \ BEGIN dup WHILE dup T @ H r@ rot T ! H REPEAT drop r> ;
1411:
1412: \ exists 9may93jaw
1413:
1414: Variable TWarnings
1415: TWarnings on
1416: Variable Exists-Warnings
1417: Exists-Warnings on
1418:
1419: : exists ( ghost tcfa -- )
1420: over GhostNames
1421: BEGIN @ dup
1422: WHILE 2dup cell+ @ =
1423: UNTIL
1424: 2 cells + count
1425: TWarnings @ Exists-Warnings @ and
1426: IF warnhead type ." exists"
1427: ELSE 2drop THEN
1428: drop swap >link !
1429: ELSE true abort" CROSS: Ghostnames inconsistent "
1430: THEN ;
1431:
1432: : resolve ( ghost tcfa -- )
1433: \G resolve referencies to ghost with tcfa
1434: \ is ghost resolved?, second resolve means another definition with the
1435: \ same name
1436: over undefined? 0= IF exists EXIT THEN
1437: \ get linked-list
1438: swap >r r@ >link @ swap \ ( list tcfa R: ghost )
1439: \ mark ghost as resolved
1440: dup r@ >link ! <res> r@ >magic !
1441: \ loop through forward referencies
1442: r> -rot
1443: comp-state @ >r Resolving comp-state !
1444: resolve-loop
1445: r> comp-state !
1446:
1447: ['] noop IS resolve-warning
1448: ;
1449:
1450: \ gexecute ghost, 01nov92py
1451:
1452: : is-forward ( ghost -- )
1453: colonmark, 0 (refered) ; \ compile space for call
1454:
1455: : is-resolved ( ghost -- )
1456: >link @ colon, ; \ compile-call
1457:
1458: : gexecute ( ghost -- )
1459: dup @ <fwd> = IF is-forward ELSE is-resolved THEN ;
1460:
1461: : addr, ( ghost -- )
1462: dup @ <fwd> = IF 1 refered 0 T a, H ELSE >link @ T a, H THEN ;
1463:
1464: \ !! : ghost, ghost gexecute ;
1465:
1466: \ .unresolved 11may93jaw
1467:
1468: variable ResolveFlag
1469:
1470: \ ?touched 11may93jaw
1471:
1472: : ?touched ( ghost -- flag ) dup forward? swap >link @
1473: 0 <> and ;
1474:
1475: : .forwarddefs ( ghost -- )
1476: ." appeared in:"
1477: >link
1478: BEGIN @ dup
1479: WHILE cr 5 spaces
1480: dup >ghost @ .ghost
1481: ." file " dup >file @ ?dup IF count type ELSE ." CON" THEN
1482: ." line " dup >line @ .dec
1483: REPEAT
1484: drop ;
1485:
1486: : ?resolved ( ghostname -- )
1487: dup cell+ @ ?touched
1488: IF dup
1489: cell+ cell+ count cr type ResolveFlag on
1490: cell+ @ .forwarddefs
1491: ELSE drop
1492: THEN ;
1493:
1494: : .unresolved ( -- )
1495: ResolveFlag off cr ." Unresolved: "
1496: Ghostnames
1497: BEGIN @ dup
1498: WHILE dup ?resolved
1499: REPEAT drop ResolveFlag @
1500: IF
1501: -1 abort" Unresolved words!"
1502: ELSE
1503: ." Nothing!"
1504: THEN
1505: cr ;
1506:
1507: : .stats
1508: base @ >r decimal
1509: cr ." named Headers: " headers-named @ .
1510: r> base ! ;
1511:
1512: >MINIMAL
1513:
1514: : .unresolved .unresolved ;
1515:
1516: >CROSS
1517: \ Header states 12dec92py
1518:
1519: : flag! ( w -- ) tlast @ dup >r T @ xor r> ! H ;
1520:
1521: VARIABLE ^imm
1522:
1523: \ !! should be target wordsize specific
1524: $80000000 constant alias-mask
1525: $40000000 constant immediate-mask
1526: $20000000 constant restrict-mask
1527:
1528: >TARGET
1529: : immediate immediate-mask flag!
1530: ^imm @ @ dup <imm> = IF drop EXIT THEN
1531: <res> <> ABORT" CROSS: Cannot immediate a unresolved word"
1532: <imm> ^imm @ ! ;
1533: : restrict restrict-mask flag! ;
1534:
1535: : isdoer
1536: \G define a forth word as doer, this makes obviously only sence on
1537: \G forth processors such as the PSC1000
1538: <do:> last-header-ghost @ >magic ! ;
1539: >CROSS
1540:
1541: \ Target Header Creation 01nov92py
1542:
1543: >TARGET
1544: : string, ( addr count -- )
1545: dup T c, H bounds ?DO I c@ T c, H LOOP ;
1546: : lstring, ( addr count -- )
1547: dup T , H bounds ?DO I c@ T c, H LOOP ;
1548: : name, ( "name" -- ) bl word count T lstring, cfalign H ;
1549: : view, ( -- ) ( dummy ) ;
1550: >CROSS
1551:
1552: \ Target Document Creation (goes to crossdoc.fd) 05jul95py
1553:
1554: s" ./doc/crossdoc.fd" r/w create-file throw value doc-file-id
1555: \ contains the file-id of the documentation file
1556:
1557: : T-\G ( -- )
1558: source >in @ /string doc-file-id write-line throw
1559: postpone \ ;
1560:
1561: Variable to-doc to-doc on
1562:
1563: : cross-doc-entry ( -- )
1564: to-doc @ tlast @ 0<> and \ not an anonymous (i.e. noname) header
1565: IF
1566: s" " doc-file-id write-line throw
1567: s" make-doc " doc-file-id write-file throw
1568:
1569: tlast @ >image count 1F and doc-file-id write-file throw
1570: >in @
1571: [char] ( parse 2drop
1572: [char] ) parse doc-file-id write-file throw
1573: s" )" doc-file-id write-file throw
1574: [char] \ parse 2drop
1575: T-\G
1576: >in !
1577: THEN ;
1578:
1579: \ Target TAGS creation
1580:
1581: s" kernel.TAGS" r/w create-file throw value tag-file-id
1582: \ contains the file-id of the tags file
1583:
1584: Create tag-beg 2 c, 7F c, bl c,
1585: Create tag-end 2 c, bl c, 01 c,
1586: Create tag-bof 1 c, 0C c,
1587:
1588: 2variable last-loadfilename 0 0 last-loadfilename 2!
1589:
1590: : put-load-file-name ( -- )
1591: sourcefilename last-loadfilename 2@ d<>
1592: IF
1593: tag-bof count tag-file-id write-line throw
1594: sourcefilename 2dup
1595: tag-file-id write-file throw
1596: last-loadfilename 2!
1597: s" ,0" tag-file-id write-line throw
1598: THEN ;
1599:
1600: : cross-tag-entry ( -- )
1601: tlast @ 0<> \ not an anonymous (i.e. noname) header
1602: IF
1603: put-load-file-name
1604: source >in @ min tag-file-id write-file throw
1605: tag-beg count tag-file-id write-file throw
1606: tlast @ >image count 1F and tag-file-id write-file throw
1607: tag-end count tag-file-id write-file throw
1608: base @ decimal sourceline# 0 <# #s #> tag-file-id write-file throw
1609: \ >in @ 0 <# #s [char] , hold #> tag-file-id write-line throw
1610: s" ,0" tag-file-id write-line throw
1611: base !
1612: THEN ;
1613:
1614: \ Check for words
1615:
1616: Defer skip? ' false IS skip?
1617:
1618: : skipdef ( <name> -- )
1619: \G skip definition of an undefined word in undef-words and
1620: \G all-words mode
1621: ghost dup forward?
1622: IF >magic <skip> swap !
1623: ELSE drop THEN ;
1624:
1625: : tdefined? ( -- flag ) \ name
1626: ghost undefined? 0= ;
1627:
1628: : defined2? ( -- flag ) \ name
1629: \G return true for anything else than forward, even for <skip>
1630: \G that's what we want
1631: ghost forward? 0= ;
1632:
1633: : forced? ( -- flag ) \ name
1634: \G return ture if it is a foreced skip with defskip
1635: ghost >magic @ <skip> = ;
1636:
1637: : needed? ( -- flag ) \ name
1638: \G returns a false flag when
1639: \G a word is not defined
1640: \G a forward reference exists
1641: \G so the definition is not skipped!
1642: bl word gfind
1643: IF dup undefined?
1644: nip
1645: 0=
1646: ELSE drop true THEN ;
1647:
1648: : doer? ( -- flag ) \ name
1649: ghost >magic @ <do:> = ;
1650:
1651: : skip-defs ( -- )
1652: BEGIN refill WHILE source -trailing nip 0= UNTIL THEN ;
1653:
1654: \ Target header creation
1655:
1656: Variable NoHeaderFlag
1657: NoHeaderFlag off
1658:
1659: : 0.r ( n1 n2 -- )
1660: base @ >r hex
1661: 0 swap <# 0 ?DO # LOOP #> type
1662: r> base ! ;
1663:
1664: : .sym ( adr len -- )
1665: \G escapes / and \ to produce sed output
1666: bounds
1667: DO I c@ dup
1668: CASE [char] / OF drop ." \/" ENDOF
1669: [char] \ OF drop ." \\" ENDOF
1670: dup OF emit ENDOF
1671: ENDCASE
1672: LOOP ;
1673:
1674: : (Theader ( "name" -- ghost )
1675: \ >in @ bl word count type 2 spaces >in !
1676: \ wordheaders will always be compiled to rom
1677: switchrom
1678: \ build header in target
1679: NoHeaderFlag @
1680: IF NoHeaderFlag off
1681: ELSE
1682: T align H view,
1683: tlast @ dup 0> IF tcell - THEN T A, H there tlast !
1684: 1 headers-named +! \ Statistic
1685: >in @ T name, H >in !
1686: THEN
1687: T cfalign here H tlastcfa !
1688: \ Old Symbol table sed-script
1689: \ >in @ cr ." sym:s/CFA=" there 4 0.r ." /" bl word count .sym ." /g" cr >in !
1690: ghost
1691: \ output symbol table to extra file
1692: [ [IFDEF] fd-symbol-table ]
1693: base @ hex there s>d <# 8 0 DO # LOOP #> fd-symbol-table write-file throw base !
1694: s" :" fd-symbol-table write-file throw
1695: dup >ghostname fd-symbol-table write-line throw
1696: [ [THEN] ]
1697: dup Last-Header-Ghost !
1698: dup >magic ^imm ! \ a pointer for immediate
1699: Already @
1700: IF dup >end tdoes !
1701: ELSE 0 tdoes !
1702: THEN
1703: alias-mask flag!
1704: cross-doc-entry cross-tag-entry ;
1705:
1706: VARIABLE ;Resolve 1 cells allot
1707: \ this is the resolver information from ":"
1708: \ resolving is done by ";"
1709:
1710: : Theader ( "name" -- ghost )
1711: (THeader dup there resolve 0 ;Resolve ! ;
1712:
1713: >TARGET
1714: : Alias ( cfa -- ) \ name
1715: >in @ skip? IF 2drop EXIT THEN >in !
1716: dup 0< s" prims" T $has? H 0= and
1717: IF
1718: .sourcepos ." needs prim: " >in @ bl word count type >in ! cr
1719: THEN
1720: (THeader over resolve T A, H alias-mask flag! ;
1721: : Alias: ( cfa -- ) \ name
1722: >in @ skip? IF 2drop EXIT THEN >in !
1723: dup 0< s" prims" T $has? H 0= and
1724: IF
1725: .sourcepos ." needs doer: " >in @ bl word count type >in ! cr
1726: THEN
1727: ghost tuck swap resolve <do:> swap >magic ! ;
1728:
1729: Variable prim#
1730: : first-primitive ( n -- ) prim# ! ;
1731: : Primitive ( -- ) \ name
1732: prim# @ T Alias H -1 prim# +! ;
1733: >CROSS
1734:
1735: \ Conditionals and Comments 11may93jaw
1736:
1737: : ;Cond
1738: postpone ;
1739: swap ! ; immediate
1740:
1741: : Cond: ( -- ) \ name {code } ;
1742: atonce on
1743: ghost
1744: >exec
1745: :NONAME ;
1746:
1747: : restrict? ( -- )
1748: \ aborts on interprete state - ae
1749: state @ 0= ABORT" CROSS: Restricted" ;
1750:
1751: : Comment ( -- )
1752: >in @ atonce on ghost swap >in ! ' swap >exec ! ;
1753:
1754: Comment ( Comment \
1755:
1756: \ compile 10may93jaw
1757:
1758: : compile ( -- ) \ name
1759: restrict?
1760: bl word gfind dup 0= ABORT" CROSS: Can't compile "
1761: 0> ( immediate? )
1762: IF >exec @ compile,
1763: ELSE postpone literal postpone gexecute THEN ;
1764: immediate
1765:
1766: : [G']
1767: \G ticks a ghost and returns its address
1768: bl word gfind 0= ABORT" CROSS: Ghost don't exists"
1769: state @
1770: IF postpone literal
1771: THEN ; immediate
1772:
1773: : ghost>cfa
1774: dup undefined? ABORT" CROSS: forward " >link @ ;
1775:
1776: >TARGET
1777:
1778: : ' ( -- cfa )
1779: \ returns the target-cfa of a ghost
1780: bl word gfind 0= ABORT" CROSS: Ghost don't exists"
1781: ghost>cfa ;
1782:
1783: Cond: ['] T ' H alit, ;Cond
1784:
1785: >CROSS
1786:
1787: : [T']
1788: \ returns the target-cfa of a ghost, or compiles it as literal
1789: postpone [G'] state @ IF postpone ghost>cfa ELSE ghost>cfa THEN ; immediate
1790:
1791: \ \ threading modell 13dec92py
1792: \ modularized 14jun97jaw
1793:
1794: : fillcfa ( usedcells -- )
1795: T cells H xt>body swap - 0 ?DO 0 X c, tchar +LOOP ;
1796:
1797: : (>body) ( cfa -- pfa ) xt>body + ; ' (>body) T IS >body H
1798:
1799: : (doer,) ( ghost -- ) ]comp gexecute comp[ 1 fillcfa ; ' (doer,) IS doer,
1800:
1801: : (docol,) ( -- ) [G'] :docol doer, ; ' (docol,) IS docol,
1802:
1803: : (doprim,) ( -- )
1804: there xt>body + ca>native T a, H 1 fillcfa ; ' (doprim,) IS doprim,
1805:
1806: : (doeshandler,) ( -- )
1807: T cfalign H compile :doesjump T 0 , H ; ' (doeshandler,) IS doeshandler,
1808:
1809: : (dodoes,) ( does-action-ghost -- )
1810: ]comp [G'] :dodoes gexecute comp[
1811: addr,
1812: T here H tcell - reloff 2 fillcfa ; ' (dodoes,) IS dodoes,
1813:
1814: : (lit,) ( n -- ) compile lit T , H ; ' (lit,) IS lit,
1815:
1816: \ if we dont produce relocatable code alit, defaults to lit, jaw
1817: \ this is just for convenience, so we don't have to define alit,
1818: \ seperately for embedded systems....
1819: T has? relocate H
1820: [IF]
1821: : (alit,) ( n -- ) compile lit T a, H ; ' (alit,) IS alit,
1822: [ELSE]
1823: : (alit,) ( n -- ) lit, ; ' (alit,) IS alit,
1824: [THEN]
1825:
1826: : (fini,) compile ;s ; ' (fini,) IS fini,
1827:
1828: [IFUNDEF] (code)
1829: Defer (code)
1830: Defer (end-code)
1831: [THEN]
1832:
1833: >TARGET
1834: : Code
1835: defempty?
1836: (THeader there resolve
1837: [ T e? prims H 0= [IF] T e? ITC H [ELSE] true [THEN] ] [IF]
1838: doprim,
1839: [THEN]
1840: depth (code) ;
1841:
1842: : Code:
1843: defempty?
1844: ghost dup there ca>native resolve <do:> swap >magic !
1845: depth (code) ;
1846:
1847: : end-code
1848: (end-code)
1849: depth ?dup IF 1- <> ABORT" CROSS: Stack changed"
1850: ELSE true ABORT" CROSS: Stack empty" THEN
1851: ;
1852:
1853: >CROSS
1854:
1855: \ tLiteral 12dec92py
1856:
1857: >TARGET
1858: Cond: \G T-\G ;Cond
1859:
1860: Cond: Literal ( n -- ) restrict? lit, ;Cond
1861: Cond: ALiteral ( n -- ) restrict? alit, ;Cond
1862:
1863: : Char ( "<char>" -- ) bl word char+ c@ ;
1864: Cond: [Char] ( "<char>" -- ) restrict? Char lit, ;Cond
1865:
1866: \ some special literals 27jan97jaw
1867:
1868: \ !! Known Bug: Special Literals and plug-ins work only correct
1869: \ on 16 and 32 Bit Targets and 32 Bit Hosts!
1870:
1871: Cond: MAXU
1872: restrict?
1873: tcell 1 cells u>
1874: IF compile lit tcell 0 ?DO FF T c, H LOOP
1875: ELSE ffffffff lit, THEN
1876: ;Cond
1877:
1878: Cond: MINI
1879: restrict?
1880: tcell 1 cells u>
1881: IF compile lit bigendian
1882: IF 80 T c, H tcell 1 ?DO 0 T c, H LOOP
1883: ELSE tcell 1 ?DO 0 T c, H LOOP 80 T c, H
1884: THEN
1885: ELSE tcell 2 = IF 8000 ELSE 80000000 THEN lit, THEN
1886: ;Cond
1887:
1888: Cond: MAXI
1889: restrict?
1890: tcell 1 cells u>
1891: IF compile lit bigendian
1892: IF 7F T c, H tcell 1 ?DO FF T c, H LOOP
1893: ELSE tcell 1 ?DO FF T c, H LOOP 7F T c, H
1894: THEN
1895: ELSE tcell 2 = IF 7fff ELSE 7fffffff THEN lit, THEN
1896: ;Cond
1897:
1898: >CROSS
1899: \ Target compiling loop 12dec92py
1900: \ ">tib trick thrown out 10may93jaw
1901: \ number? defined at the top 11may93jaw
1902: \ replaced >in by save-input
1903:
1904: : discard 0 ?DO drop LOOP ;
1905:
1906: \ compiled word might leave items on stack!
1907: : tcom ( x1 .. xn n name -- )
1908: \ dup count type space
1909: gfind ?dup
1910: IF >r >r discard r> r>
1911: 0> IF >exec @ execute
1912: ELSE gexecute THEN
1913: EXIT
1914: THEN
1915: number? dup
1916: IF 0> IF swap lit, THEN lit, discard
1917: ELSE 2drop restore-input throw ghost gexecute THEN ;
1918:
1919: >TARGET
1920: \ : ; DOES> 13dec92py
1921: \ ] 9may93py/jaw
1922:
1923: : ] state on
1924: Compiling comp-state !
1925: BEGIN
1926: BEGIN save-input bl word
1927: dup c@ 0= WHILE drop discard refill 0=
1928: ABORT" CROSS: End of file while target compiling"
1929: REPEAT
1930: tcom
1931: state @
1932: 0=
1933: UNTIL ;
1934:
1935: \ by the way: defining a second interpreter (a compiler-)loop
1936: \ is not allowed if a system should be ans conform
1937:
1938: : : ( -- colon-sys ) \ Name
1939: defempty?
1940: constflag off \ don't let this flag work over colon defs
1941: \ just to go sure nothing unwanted happens
1942: >in @ skip? IF drop skip-defs EXIT THEN >in !
1943: (THeader ;Resolve ! there ;Resolve cell+ !
1944: docol, ]comp depth T ] H ;
1945:
1946: : :noname ( -- colon-sys )
1947: T cfalign H there docol, 0 ;Resolve ! depth T ] H ;
1948:
1949: Cond: EXIT ( -- ) restrict? compile ;S ;Cond
1950:
1951: Cond: ?EXIT ( -- ) 1 abort" CROSS: using ?exit" ;Cond
1952:
1953: >CROSS
1954: : LastXT ;Resolve @ 0= abort" CROSS: no definition for LastXT"
1955: ;Resolve cell+ @ ;
1956:
1957: >TARGET
1958:
1959: Cond: recurse ( -- ) Last-Ghost @ gexecute ;Cond
1960:
1961: Cond: ; ( -- ) restrict?
1962: depth ?dup IF 1- <> ABORT" CROSS: Stack changed"
1963: ELSE true ABORT" CROSS: Stack empty" THEN
1964: fini,
1965: comp[
1966: state off
1967: ;Resolve @
1968: IF ;Resolve @ ;Resolve cell+ @ resolve THEN
1969: Interpreting comp-state !
1970: ;Cond
1971: Cond: [ restrict? state off Interpreting comp-state ! ;Cond
1972:
1973: >CROSS
1974:
1975: Create GhostDummy ghostheader
1976: <res> GhostDummy >magic !
1977:
1978: : !does ( does-action -- )
1979: \ !! zusammenziehen und dodoes, machen!
1980: tlastcfa @ [G'] :dovar killref
1981: \ tlastcfa @ dup there >r tdp ! compile :dodoes r> tdp ! T cell+ ! H ;
1982: \ !! geht so nicht, da dodoes, ghost will!
1983: GhostDummy >link ! GhostDummy
1984: tlastcfa @ >tempdp dodoes, tempdp> ;
1985:
1986: >TARGET
1987: Cond: DOES> restrict?
1988: compile (does>) doeshandler,
1989: \ resolve words made by builders
1990: tdoes @ ?dup IF @ T here H resolve THEN
1991: ;Cond
1992: : DOES> switchrom doeshandler, T here H !does depth T ] H ;
1993:
1994: >CROSS
1995: \ Creation 01nov92py
1996:
1997: \ Builder 11may93jaw
1998:
1999: : Builder ( Create-xt do:-xt "name" -- )
2000: \ builds up a builder in current vocabulary
2001: \ create-xt is executed when word is interpreted
2002: \ do:-xt is executet when the created word from builder is executed
2003: \ for do:-xt an additional entry after the normal ghost-enrys is used
2004:
2005: Make-Ghost ( Create-xt do:-xt ghost )
2006: rot swap ( do:-xt Create-xt ghost )
2007: >exec ! , ;
2008: \ rot swap >exec dup @ ['] NoExec <>
2009: \ IF 2drop ELSE ! THEN , ;
2010:
2011: : gdoes, ( ghost -- )
2012: \ makes the codefield for a word that is built
2013: >end @ dup undefined? 0=
2014: IF
2015: dup >magic @ <do:> =
2016: IF doer,
2017: ELSE dodoes,
2018: THEN
2019: EXIT
2020: THEN
2021: \ compile :dodoes gexecute
2022: \ T here H tcell - reloff
2023: 2 refered
2024: 0 fillcfa
2025: ;
2026:
2027: : TCreate ( <name> -- )
2028: executed-ghost @
2029: create-forward-warn
2030: IF ['] reswarn-forward IS resolve-warning THEN
2031: Theader >r dup gdoes,
2032: \ stores execution semantic in the built word
2033: \ if the word already has a semantic (concerns S", IS, .", DOES>)
2034: \ then keep it
2035: >end @ >exec @ r> >exec dup @ ['] NoExec =
2036: IF ! ELSE 2drop THEN ;
2037:
2038: : RTCreate ( <name> -- )
2039: \ creates a new word with code-field in ram
2040: executed-ghost @
2041: create-forward-warn
2042: IF ['] reswarn-forward IS resolve-warning THEN
2043: \ make Alias
2044: (THeader there 0 T a, H alias-mask flag! ( S executed-ghost new-ghost )
2045: \ store poiter to code-field
2046: switchram T cfalign H
2047: there swap T ! H
2048: there tlastcfa !
2049: dup there resolve 0 ;Resolve !
2050: >r dup gdoes,
2051: \ stores execution semantic in the built word
2052: \ if the word already has a semantic (concerns S", IS, .", DOES>)
2053: \ then keep it
2054: >end @ >exec @ r> >exec dup @ ['] NoExec =
2055: IF ! ELSE 2drop THEN ;
2056:
2057: : Build: ( -- [xt] [colon-sys] )
2058: :noname postpone TCreate ;
2059:
2060: : BuildSmart: ( -- [xt] [colon-sys] )
2061: :noname
2062: [ T has? rom H [IF] ]
2063: postpone RTCreate
2064: [ [ELSE] ]
2065: postpone TCreate
2066: [ [THEN] ] ;
2067:
2068: : gdoes> ( ghost -- addr flag )
2069: executed-ghost @
2070: state @ IF gexecute true EXIT THEN
2071: >link @ T >body H false ;
2072:
2073: \ DO: ;DO 11may93jaw
2074: \ changed to ?EXIT 10may93jaw
2075:
2076: : DO: ( -- addr [xt] [colon-sys] )
2077: here ghostheader
2078: :noname postpone gdoes> postpone ?EXIT ;
2079:
2080: : by: ( -- addr [xt] [colon-sys] ) \ name
2081: ghost
2082: :noname postpone gdoes> postpone ?EXIT ;
2083:
2084: : ;DO ( addr [xt] [colon-sys] -- addr )
2085: postpone ; ( S addr xt )
2086: over >exec ! ; immediate
2087:
2088: : by ( -- addr ) \ Name
2089: ghost >end @ ;
2090:
2091: >TARGET
2092: \ Variables and Constants 05dec92py
2093:
2094: Build: ( n -- ) ;
2095: by: :docon ( ghost -- n ) T @ H ;DO
2096: Builder (Constant)
2097:
2098: Build: ( n -- ) T , H ;
2099: by (Constant)
2100: Builder Constant
2101:
2102: Build: ( n -- ) T A, H ;
2103: by (Constant)
2104: Builder AConstant
2105:
2106: Build: ( d -- ) T , , H ;
2107: DO: ( ghost -- d ) T dup cell+ @ swap @ H ;DO
2108: Builder 2Constant
2109:
2110: BuildSmart: ;
2111: by: :dovar ( ghost -- addr ) ;DO
2112: Builder Create
2113:
2114: T has? rom H [IF]
2115: Build: ( -- ) T here 0 , H switchram T align here swap ! 0 , H ( switchrom ) ;
2116: by (Constant)
2117: Builder Variable
2118: [ELSE]
2119: Build: T 0 , H ;
2120: by Create
2121: Builder Variable
2122: [THEN]
2123:
2124: T has? rom H [IF]
2125: Build: ( -- ) T here 0 , H switchram T align here swap ! 0 , 0 , H ( switchrom ) ;
2126: by (Constant)
2127: Builder 2Variable
2128: [ELSE]
2129: Build: T 0 , 0 , H ;
2130: by Create
2131: Builder 2Variable
2132: [THEN]
2133:
2134: T has? rom H [IF]
2135: Build: ( -- ) T here 0 , H switchram T align here swap ! 0 , H ( switchrom ) ;
2136: by (Constant)
2137: Builder AVariable
2138: [ELSE]
2139: Build: T 0 A, H ;
2140: by Create
2141: Builder AVariable
2142: [THEN]
2143:
2144: \ User variables 04may94py
2145:
2146: >CROSS
2147:
2148: Variable tup 0 tup !
2149: Variable tudp 0 tudp !
2150:
2151: : u, ( n -- udp )
2152: tup @ tudp @ + T ! H
2153: tudp @ dup T cell+ H tudp ! ;
2154:
2155: : au, ( n -- udp )
2156: tup @ tudp @ + T A! H
2157: tudp @ dup T cell+ H tudp ! ;
2158:
2159: >TARGET
2160:
2161: Build: 0 u, X , ;
2162: by: :douser ( ghost -- up-addr ) X @ tup @ + ;DO
2163: Builder User
2164:
2165: Build: 0 u, X , 0 u, drop ;
2166: by User
2167: Builder 2User
2168:
2169: Build: 0 au, X , ;
2170: by User
2171: Builder AUser
2172:
2173: BuildSmart: T , H ;
2174: by (Constant)
2175: Builder Value
2176:
2177: BuildSmart: T A, H ;
2178: by (Constant)
2179: Builder AValue
2180:
2181: BuildSmart: ( -- ) [T'] noop T A, H ;
2182: by: :dodefer ( ghost -- ) ABORT" CROSS: Don't execute" ;DO
2183: Builder Defer
2184:
2185: Build: ( inter comp -- ) swap T immediate A, A, H ;
2186: DO: ( ghost -- ) ABORT" CROSS: Don't execute" ;DO
2187: Builder interpret/compile:
2188:
2189: \ Sturctures 23feb95py
2190:
2191: >CROSS
2192: : nalign ( addr1 n -- addr2 )
2193: \ addr2 is the aligned version of addr1 wrt the alignment size n
2194: 1- tuck + swap invert and ;
2195: >TARGET
2196:
2197: Build: ;
2198: by: :dofield T @ H + ;DO
2199: Builder (Field)
2200:
2201: Build: ( align1 offset1 align size "name" -- align2 offset2 )
2202: rot dup T , H ( align1 align size offset1 )
2203: + >r nalign r> ;
2204: by (Field)
2205: Builder Field
2206:
2207: : struct T 1 chars 0 H ;
2208: : end-struct T 2Constant H ;
2209:
2210: : cell% ( n -- size align )
2211: T 1 cells H dup ;
2212:
2213: Build: ( m v -- m' v ) dup T , H cell+ ;
2214: DO: abort" Not in cross mode" ;DO
2215: Builder input-method
2216:
2217: Build: ( m v size -- m v' ) over T , H + ;
2218: DO: abort" Not in cross mode" ;DO
2219: Builder input-var
2220:
2221: \ structural conditionals 17dec92py
2222:
2223: >CROSS
2224: : ?struc ( flag -- ) ABORT" CROSS: unstructured " ;
2225: : sys? ( sys -- sys ) dup 0= ?struc ;
2226: : >mark ( -- sys ) T here ( dup ." M" hex. ) 0 , H ;
2227:
2228: : branchoffset ( src dest -- ) - tchar / ; \ ?? jaw
2229:
2230: : >resolve ( sys -- )
2231: X here ( dup ." >" hex. ) over branchoffset swap X ! ;
2232:
2233: : <resolve ( sys -- )
2234: X here ( dup ." <" hex. ) branchoffset X , ;
2235:
2236: :noname compile branch X here branchoffset X , ;
2237: IS branch, ( target-addr -- )
2238: :noname compile ?branch X here branchoffset X , ;
2239: IS ?branch, ( target-addr -- )
2240: :noname compile branch T here 0 , H ;
2241: IS branchmark, ( -- branchtoken )
2242: :noname compile ?branch T here 0 , H ;
2243: IS ?branchmark, ( -- branchtoken )
2244: :noname T here 0 , H ;
2245: IS ?domark, ( -- branchtoken )
2246: :noname dup X @ ?struc X here over branchoffset swap X ! ;
2247: IS branchtoresolve, ( branchtoken -- )
2248: :noname branchto, X here ;
2249: IS branchtomark, ( -- target-addr )
2250:
2251: >TARGET
2252:
2253: \ Structural Conditionals 12dec92py
2254:
2255: Cond: BUT restrict? sys? swap ;Cond
2256: Cond: YET restrict? sys? dup ;Cond
2257:
2258: >CROSS
2259:
2260: Variable tleavings 0 tleavings !
2261:
2262: : (done) ( addr -- )
2263: tleavings @
2264: BEGIN dup
2265: WHILE
2266: >r dup r@ cell+ @ \ address of branch
2267: u> 0= \ lower than DO?
2268: WHILE
2269: r@ 2 cells + @ \ branch token
2270: branchtoresolve,
2271: r@ @ r> free throw
2272: REPEAT r> THEN
2273: tleavings ! drop ;
2274:
2275: >TARGET
2276:
2277: Cond: DONE ( addr -- ) restrict? (done) ;Cond
2278:
2279: >CROSS
2280: : (leave) ( branchtoken -- )
2281: 3 cells allocate throw >r
2282: T here H r@ cell+ !
2283: r@ 2 cells + !
2284: tleavings @ r@ !
2285: r> tleavings ! ;
2286: >TARGET
2287:
2288: Cond: LEAVE restrict? branchmark, (leave) ;Cond
2289: Cond: ?LEAVE restrict? compile 0= ?branchmark, (leave) ;Cond
2290:
2291: >CROSS
2292: \ !!JW ToDo : Move to general tools section
2293:
2294: : to1 ( x1 x2 xn n -- addr )
2295: \G packs n stack elements in a allocated memory region
2296: dup dup 1+ cells allocate throw dup >r swap 1+
2297: 0 DO tuck ! cell+ LOOP
2298: drop r> ;
2299: : 1to ( addr -- x1 x2 xn )
2300: \G unpacks the elements saved by to1
2301: dup @ swap over cells + swap
2302: 0 DO dup @ swap 1 cells - LOOP
2303: free throw ;
2304:
2305: : loop] branchto, dup <resolve tcell - (done) ;
2306:
2307: : skiploop] ?dup IF branchto, branchtoresolve, THEN ;
2308:
2309: >TARGET
2310:
2311: \ Structural Conditionals 12dec92py
2312:
2313: >TARGET
2314: Cond: AHEAD restrict? branchmark, ;Cond
2315: Cond: IF restrict? ?branchmark, ;Cond
2316: Cond: THEN restrict? sys? branchto, branchtoresolve, ;Cond
2317: Cond: ELSE restrict? sys? compile AHEAD swap compile THEN ;Cond
2318:
2319: Cond: BEGIN restrict? branchtomark, ;Cond
2320: Cond: WHILE restrict? sys? compile IF swap ;Cond
2321: Cond: AGAIN restrict? sys? branch, ;Cond
2322: Cond: UNTIL restrict? sys? ?branch, ;Cond
2323: Cond: REPEAT restrict? over 0= ?struc compile AGAIN compile THEN ;Cond
2324:
2325: Cond: CASE restrict? 0 ;Cond
2326: Cond: OF restrict? 1+ >r compile over compile =
2327: compile IF compile drop r> ;Cond
2328: Cond: ENDOF restrict? >r compile ELSE r> ;Cond
2329: Cond: ENDCASE restrict? compile drop 0 ?DO compile THEN LOOP ;Cond
2330:
2331: \ Structural Conditionals 12dec92py
2332:
2333: :noname \ ?? i think 0 is too much! jaw
2334: 0 compile (do)
2335: branchtomark, 2 to1 ;
2336: IS do, ( -- target-addr )
2337:
2338: \ :noname
2339: \ compile 2dup compile = compile IF
2340: \ compile 2drop compile ELSE
2341: \ compile (do) branchtomark, 2 to1 ;
2342: \ IS ?do,
2343:
2344: :noname
2345: 0 compile (?do) ?domark, (leave)
2346: branchtomark, 2 to1 ;
2347: IS ?do, ( -- target-addr )
2348: :noname compile (for) branchtomark, ;
2349: IS for, ( -- target-addr )
2350: :noname 1to compile (loop) loop] compile unloop skiploop] ;
2351: IS loop, ( target-addr -- )
2352: :noname 1to compile (+loop) loop] compile unloop skiploop] ;
2353: IS +loop, ( target-addr -- )
2354: :noname compile (next) loop] compile unloop ;
2355: IS next, ( target-addr -- )
2356:
2357: Cond: DO restrict? do, ;Cond
2358: Cond: ?DO restrict? ?do, ;Cond
2359: Cond: FOR restrict? for, ;Cond
2360:
2361: Cond: LOOP restrict? sys? loop, ;Cond
2362: Cond: +LOOP restrict? sys? +loop, ;Cond
2363: Cond: NEXT restrict? sys? next, ;Cond
2364:
2365: \ String words 23feb93py
2366:
2367: : ," [char] " parse T string, align H ;
2368:
2369: Cond: ." restrict? compile (.") T ," H ;Cond
2370: Cond: S" restrict? compile (S") T ," H ;Cond
2371: Cond: ABORT" restrict? compile (ABORT") T ," H ;Cond
2372:
2373: Cond: IS T ' >body H compile ALiteral compile ! ;Cond
2374: : IS T >address ' >body ! H ;
2375: Cond: TO T ' >body H compile ALiteral compile ! ;Cond
2376: : TO T ' >body ! H ;
2377:
2378: Cond: defers T ' >body @ compile, H ;Cond
2379: : on T -1 swap ! H ;
2380: : off T 0 swap ! H ;
2381:
2382: \ LINKED ERR" ENV" 2ENV" 18may93jaw
2383:
2384: \ linked list primitive
2385: : linked X here over X @ X A, swap X ! ;
2386: : chained T linked A, H ;
2387:
2388: : err" s" ErrLink linked" evaluate T , H
2389: [char] " parse T string, align H ;
2390:
2391: : env" [char] " parse s" EnvLink linked" evaluate
2392: T string, align , H ;
2393:
2394: : 2env" [char] " parse s" EnvLink linked" evaluate
2395: here >r T string, align , , H
2396: r> dup T c@ H 80 and swap T c! H ;
2397:
2398: \ compile must be last 22feb93py
2399:
2400: Cond: compile ( -- ) restrict? \ name
2401: bl word gfind dup 0= ABORT" CROSS: Can't compile"
2402: 0> IF gexecute
2403: ELSE dup >magic @ <imm> =
2404: IF gexecute
2405: ELSE compile (compile) addr, THEN THEN ;Cond
2406:
2407: Cond: postpone ( -- ) restrict? \ name
2408: bl word gfind dup 0= ABORT" CROSS: Can't compile"
2409: 0> IF gexecute
2410: ELSE dup >magic @ <imm> =
2411: IF gexecute
2412: ELSE compile (compile) addr, THEN THEN ;Cond
2413:
2414: \ save-cross 17mar93py
2415:
2416: hex
2417:
2418: >CROSS
2419: Create magic s" Gforth2x" here over allot swap move
2420:
2421: bigendian 1+ \ strangely, in magic big=0, little=1
2422: tcell 1 = 0 and or
2423: tcell 2 = 2 and or
2424: tcell 4 = 4 and or
2425: tcell 8 = 6 and or
2426: tchar 1 = 00 and or
2427: tchar 2 = 28 and or
2428: tchar 4 = 50 and or
2429: tchar 8 = 78 and or
2430: magic 7 + c!
2431:
2432: : save-cross ( "image-name" "binary-name" -- )
2433: bl parse ." Saving to " 2dup type cr
2434: w/o bin create-file throw >r
2435: TNIL IF
2436: s" #! " r@ write-file throw
2437: bl parse r@ write-file throw
2438: s" --image-file" r@ write-file throw
2439: #lf r@ emit-file throw
2440: r@ dup file-position throw drop 8 mod 8 swap ( file-id limit index )
2441: ?do
2442: bl over emit-file throw
2443: loop
2444: drop
2445: magic 8 r@ write-file throw \ write magic
2446: ELSE
2447: bl parse 2drop
2448: THEN
2449: image @ there
2450: r@ write-file throw \ write image
2451: TNIL IF
2452: bit$ @ there 1- tcell>bit rshift 1+
2453: r@ write-file throw \ write tags
2454: THEN
2455: r> close-file throw ;
2456:
2457: : save-region ( addr len -- )
2458: bl parse w/o bin create-file throw >r
2459: swap >image swap r@ write-file throw
2460: r> close-file throw ;
2461:
2462: \ \ minimal definitions
2463:
2464: >MINIMAL also minimal
2465:
2466: \ Usefull words 13feb93py
2467:
2468: : KB 400 * ;
2469:
2470: \ \ [IF] [ELSE] [THEN] ... 14sep97jaw
2471:
2472: \ it is useful to define our own structures and not to rely
2473: \ on the words in the compiler
2474: \ The words in the compiler might be defined with vocabularies
2475: \ this doesn't work with our self-made compile-loop
2476:
2477: Create parsed 20 chars allot \ store word we parsed
2478:
2479: : upcase
2480: parsed count bounds
2481: ?DO I c@ toupper I c! LOOP ;
2482:
2483: : [ELSE]
2484: 1 BEGIN
2485: BEGIN bl word count dup WHILE
2486: comment? 20 umin parsed place upcase parsed count
2487: 2dup s" [IF]" compare 0= >r
2488: 2dup s" [IFUNDEF]" compare 0= >r
2489: 2dup s" [IFDEF]" compare 0= r> or r> or
2490: IF 2drop 1+
2491: ELSE 2dup s" [ELSE]" compare 0=
2492: IF 2drop 1- dup
2493: IF 1+
2494: THEN
2495: ELSE
2496: 2dup s" [ENDIF]" compare 0= >r
2497: s" [THEN]" compare 0= r> or
2498: IF 1- THEN
2499: THEN
2500: THEN
2501: ?dup 0= ?EXIT
2502: REPEAT
2503: 2drop refill 0=
2504: UNTIL drop ; immediate
2505:
2506: : [THEN] ( -- ) ; immediate
2507:
2508: : [ENDIF] ( -- ) ; immediate
2509:
2510: : [IF] ( flag -- )
2511: 0= IF postpone [ELSE] THEN ; immediate
2512:
2513: Cond: [IF] postpone [IF] ;Cond
2514: Cond: [THEN] postpone [THEN] ;Cond
2515: Cond: [ELSE] postpone [ELSE] ;Cond
2516:
2517: \ define new [IFDEF] and [IFUNDEF] 20may93jaw
2518:
2519: : defined? tdefined? ;
2520: : needed? needed? ;
2521: : doer? doer? ;
2522:
2523: \ we want to use IFDEF on compiler directives (e.g. E?) in the source, too
2524:
2525: : directive?
2526: bl word count [ ' target >wordlist ] literal search-wordlist
2527: dup IF nip THEN ;
2528:
2529: : [IFDEF] >in @ directive? swap >in !
2530: 0= IF tdefined? ELSE name 2drop true THEN
2531: postpone [IF] ;
2532:
2533: : [IFUNDEF] tdefined? 0= postpone [IF] ;
2534:
2535: Cond: [IFDEF] postpone [IFDEF] ;Cond
2536:
2537: Cond: [IFUNDEF] postpone [IFUNDEF] ;Cond
2538:
2539: \ C: \- \+ Conditional Compiling 09jun93jaw
2540:
2541: : C: >in @ tdefined? 0=
2542: IF >in ! X :
2543: ELSE drop
2544: BEGIN bl word dup c@
2545: IF count comment? s" ;" compare 0= ?EXIT
2546: ELSE refill 0= ABORT" CROSS: Out of Input while C:"
2547: THEN
2548: AGAIN
2549: THEN ;
2550:
2551: : d? d? ;
2552:
2553: \G doesn't skip line when debug switch is on
2554: : \D D? 0= IF postpone \ THEN ;
2555:
2556: \G interprets the line if word is not defined
2557: : \- tdefined? IF postpone \ THEN ;
2558:
2559: \G interprets the line if word is defined
2560: : \+ tdefined? 0= IF postpone \ THEN ;
2561:
2562: Cond: \- \- ;Cond
2563: Cond: \+ \+ ;Cond
2564: Cond: \D \D ;Cond
2565:
2566: : ?? bl word find IF execute ELSE drop 0 THEN ;
2567:
2568: : needed:
2569: \G defines ghost for words that we want to be compiled
2570: BEGIN >in @ bl word c@ WHILE >in ! ghost drop REPEAT drop ;
2571:
2572: \ words that should be in minimal
2573:
2574: create s-buffer 50 chars allot
2575:
2576: bigendian Constant bigendian
2577:
2578: : here there ;
2579: : equ constant ;
2580: : mark there constant ;
2581:
2582: \ compiler directives
2583: : >ram >ram ;
2584: : >rom >rom ;
2585: : >auto >auto ;
2586: : >tempdp >tempdp ;
2587: : tempdp> tempdp> ;
2588: : const constflag on ;
2589: : warnings name 3 = 0= twarnings ! drop ;
2590: : | ;
2591: \ : | NoHeaderFlag on ; \ This is broken (damages the last word)
2592:
2593: : save-cross save-cross ;
2594: : save-region save-region ;
2595: : tdump swap >image swap dump ;
2596:
2597: also forth
2598: [IFDEF] Label : Label defempty? Label ; [THEN]
2599: [IFDEF] start-macros : start-macros defempty? start-macros ; [THEN]
2600: \ [IFDEF] builttag : builttag builttag ; [THEN]
2601: previous
2602:
2603: : s" [char] " parse s-buffer place s-buffer count ; \ for environment?
2604: : + + ;
2605: : 1+ 1 + ;
2606: : 2+ 2 + ;
2607: : 1- 1- ;
2608: : - - ;
2609: : and and ;
2610: : or or ;
2611: : 2* 2* ;
2612: : * * ;
2613: : / / ;
2614: : dup dup ;
2615: : over over ;
2616: : swap swap ;
2617: : rot rot ;
2618: : drop drop ;
2619: : = = ;
2620: : 0= 0= ;
2621: : lshift lshift ;
2622: : 2/ 2/ ;
2623: : . . ;
2624:
2625: : all-words ['] forced? IS skip? ;
2626: : needed-words ['] needed? IS skip? ;
2627: : undef-words ['] defined2? IS skip? ;
2628: : skipdef skipdef ;
2629:
2630: : \ postpone \ ; immediate
2631: : \G T-\G ; immediate
2632: : ( postpone ( ; immediate
2633: : include bl word count included ;
2634: : require require ;
2635: : .( [char] ) parse type ;
2636: : ." [char] " parse type ;
2637: : cr cr ;
2638:
2639: : times 0 ?DO dup X c, LOOP drop ; \ used for space table creation
2640:
2641: \ only forth also cross also minimal definitions order
2642:
2643: \ cross-compiler words
2644:
2645: : decimal decimal ;
2646: : hex hex ;
2647:
2648: \ : tudp X tudp ;
2649: \ : tup X tup ;
2650:
2651: : doc-off false to-doc ! ;
2652: : doc-on true to-doc ! ;
2653:
2654: [IFDEF] dbg : dbg dbg ; [THEN]
2655:
2656: \ for debugging...
2657: : order order ;
2658: : hwords words ;
2659: : words also ghosts words previous ;
2660: : .s .s ;
2661: : bye bye ;
2662:
2663: \ turnkey direction
2664: : H forth ; immediate
2665: : T minimal ; immediate
2666: : G ghosts ; immediate
2667:
2668: : turnkey
2669: \GFORTH 0 set-order also ghosts
2670: \ANSI [ ' ghosts >wordlist ] Literal 1 set-order
2671: also target definitions
2672: also Minimal also ;
2673:
2674: \ these ones are pefered:
2675:
2676: : lock turnkey ;
2677: : unlock previous forth also cross ;
2678:
2679: \ also minimal
2680: : [[ also unlock ;
2681: : ]] previous previous also also ;
2682:
2683: unlock definitions also minimal
2684: : lock lock ;
2685: lock
2686:
2687: \ load cross compiler extension defined in mach file
2688:
2689: UNLOCK >CROSS
2690:
2691: [IFDEF] extend-cross extend-cross [THEN]
2692:
2693: LOCK
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>