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