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