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