[gforth] / gforth / cross.fs  

gforth: gforth/cross.fs


1 : anton 1.1 \ CROSS.FS The Cross-Compiler 06oct92py
2 :     \ Idea and implementation: Bernd Paysan (py)
3 : anton 1.30
4 :     \ Copyright (C) 1995 Free Software Foundation, Inc.
5 :    
6 :     \ This file is part of Gforth.
7 :    
8 :     \ Gforth is free software; you can redistribute it and/or
9 :     \ modify it under the terms of the GNU General Public License
10 :     \ as published by the Free Software Foundation; either version 2
11 :     \ of the License, or (at your option) any later version.
12 :    
13 :     \ This program is distributed in the hope that it will be useful,
14 :     \ but WITHOUT ANY WARRANTY; without even the implied warranty of
15 :     \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 :     \ GNU General Public License for more details.
17 :    
18 :     \ You should have received a copy of the GNU General Public License
19 :     \ along with this program; if not, write to the Free Software
20 :     \ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 : anton 1.1
22 :     \ Log:
23 :     \ changed in ; [ to state off 12may93jaw
24 :     \ included place +place 12may93jaw
25 :     \ for a created word (variable, constant...)
26 :     \ is now an alias in the target voabulary.
27 :     \ this means it is no longer necessary to
28 :     \ switch between vocabularies for variable
29 :     \ initialization 12may93jaw
30 :     \ discovered error in DOES>
31 :     \ replaced !does with (;code) 16may93jaw
32 :     \ made complete redesign and
33 :     \ introduced two vocs method
34 :     \ to be asure that the right words
35 :     \ are found 08jun93jaw
36 :     \ btw: ! works not with 16 bit
37 :     \ targets 09jun93jaw
38 :     \ added: 2user and value 11jun93jaw
39 :    
40 : pazsan 1.9 \ include other.fs \ ansforth extentions for cross
41 : anton 1.1
42 : pazsan 1.23 : string, ( c-addr u -- )
43 :     \ puts down string as cstring
44 :     dup c, here swap chars dup allot move ;
45 :     ' falign Alias cfalign
46 : pazsan 1.5 : comment? ( c-addr u -- c-addr u )
47 :     2dup s" (" compare 0=
48 :     IF postpone (
49 :     ELSE 2dup s" \" compare 0= IF postpone \ THEN
50 :     THEN ;
51 :    
52 : anton 1.1 decimal
53 :    
54 :     \ Begin CROSS COMPILER:
55 :    
56 :     \ GhostNames 9may93jaw
57 :     \ second name source to search trough list
58 :    
59 :     VARIABLE GhostNames
60 :     0 GhostNames !
61 :     : GhostName ( -- addr )
62 : anton 1.22 here GhostNames @ , GhostNames ! here 0 ,
63 :     bl word count
64 :     \ 2dup type space
65 :     string, cfalign ;
66 : anton 1.1
67 :     hex
68 :    
69 :    
70 :     Vocabulary Cross
71 :     Vocabulary Target
72 :     Vocabulary Ghosts
73 :     VOCABULARY Minimal
74 :     only Forth also Target also also
75 :     definitions Forth
76 :    
77 :     : T previous Cross also Target ; immediate
78 :     : G Ghosts ; immediate
79 :     : H previous Forth also Cross ; immediate
80 :    
81 :     forth definitions
82 :    
83 :     : T previous Cross also Target ; immediate
84 :     : G Ghosts ; immediate
85 :    
86 :     : >cross also Cross definitions previous ;
87 :     : >target also Target definitions previous ;
88 :     : >minimal also Minimal definitions previous ;
89 :    
90 :     H
91 :    
92 :     >CROSS
93 :    
94 : pazsan 1.43 \ Parameter for target systems 06oct92py
95 :    
96 :     mach-file count included
97 :    
98 :     also Forth definitions
99 :    
100 :     [IFDEF] asm-include asm-include [THEN]
101 :    
102 :     previous
103 : pazsan 1.46 hex
104 : pazsan 1.43
105 :     >CROSS
106 :    
107 : anton 1.1 \ Variables 06oct92py
108 :    
109 :     Variable image
110 :     Variable tlast NIL tlast ! \ Last name field
111 :     Variable tlastcfa \ Last code field
112 :     Variable tdoes \ Resolve does> calls
113 :     Variable bit$
114 :     Variable tdp
115 :     : there tdp @ ;
116 :    
117 : pazsan 1.19 \ Create additional parameters 19jan95py
118 :    
119 :     T
120 :     cell Constant tcell
121 :     cell<< Constant tcell<<
122 :     cell>bit Constant tcell>bit
123 :     bits/byte Constant tbits/byte
124 :     float Constant tfloat
125 :     1 bits/byte lshift Constant maxbyte
126 :     H
127 :    
128 : anton 1.1 >TARGET
129 :    
130 :     \ Byte ordering and cell size 06oct92py
131 :    
132 : pazsan 1.19 : cell+ tcell + ;
133 :     : cells tcell<< lshift ;
134 : anton 1.1 : chars ;
135 : pazsan 1.19 : floats tfloat * ;
136 : anton 1.6
137 : anton 1.1 >CROSS
138 : pazsan 1.19 : cell/ tcell<< rshift ;
139 : anton 1.1 >TARGET
140 :     20 CONSTANT bl
141 :     -1 Constant NIL
142 :    
143 :     >CROSS
144 :    
145 : pazsan 1.20 bigendian
146 :     [IF]
147 :     : T! ( n addr -- ) >r s>d r> tcell bounds swap 1-
148 :     DO maxbyte ud/mod rot I c! -1 +LOOP 2drop ;
149 :     : T@ ( addr -- n ) >r 0 0 r> tcell bounds
150 :     DO maxbyte * swap maxbyte um* rot + swap I c@ + swap LOOP d>s ;
151 : pazsan 1.19 [ELSE]
152 : pazsan 1.20 : T! ( n addr -- ) >r s>d r> tcell bounds
153 :     DO maxbyte ud/mod rot I c! LOOP 2drop ;
154 :     : T@ ( addr -- n ) >r 0 0 r> tcell bounds swap 1-
155 :     DO maxbyte * swap maxbyte um* rot + swap I c@ + swap -1 +LOOP d>s ;
156 : anton 1.1 [THEN]
157 :    
158 :     \ Memory initialisation 05dec92py
159 :     \ Fixed bug in else part 11may93jaw
160 :    
161 :     [IFDEF] Memory \ Memory is a bigFORTH feature
162 : pazsan 1.5 also Memory
163 : anton 1.1 : initmem ( var len -- )
164 :     2dup swap handle! >r @ r> erase ;
165 : pazsan 1.5 toss
166 : anton 1.1 [ELSE]
167 :     : initmem ( var len -- )
168 :     tuck allocate abort" CROSS: No memory for target"
169 :     ( len var adr ) dup rot !
170 :     ( len adr ) swap erase ;
171 :     [THEN]
172 :    
173 :     \ MakeKernal 12dec92py
174 :    
175 :     >MINIMAL
176 : pazsan 1.39 : makekernel ( targetsize -- targetsize )
177 : anton 1.1 bit$ over 1- cell>bit rshift 1+ initmem
178 :     image over initmem tdp off ;
179 :    
180 :     >CROSS
181 :     \ Bit string manipulation 06oct92py
182 :     \ 9may93jaw
183 :     CREATE Bittable 80 c, 40 c, 20 c, 10 c, 8 c, 4 c, 2 c, 1 c,
184 :     : bits ( n -- n ) chars Bittable + c@ ;
185 :    
186 :     : >bit ( addr n -- c-addr mask ) 8 /mod rot + swap bits ;
187 :     : +bit ( addr n -- ) >bit over c@ or swap c! ;
188 : pazsan 1.4 : -bit ( addr n -- ) >bit invert over c@ and swap c! ;
189 : anton 1.1 : relon ( taddr -- ) bit$ @ swap cell/ +bit ;
190 : pazsan 1.4 : reloff ( taddr -- ) bit$ @ swap cell/ -bit ;
191 : anton 1.1
192 :     \ Target memory access 06oct92py
193 :    
194 :     : align+ ( taddr -- rest )
195 :     cell tuck 1- and - [ cell 1- ] Literal and ;
196 : anton 1.22 : cfalign+ ( taddr -- rest )
197 : pazsan 1.39 \ see kernel.fs:cfaligned
198 : pazsan 1.43 /maxalign tuck 1- and - [ /maxalign 1- ] Literal and ;
199 : anton 1.1
200 :     >TARGET
201 :     : aligned ( taddr -- ta-addr ) dup align+ + ;
202 :     \ assumes cell alignment granularity (as GNU C)
203 :    
204 : anton 1.22 : cfaligned ( taddr1 -- taddr2 )
205 : pazsan 1.39 \ see kernel.fs
206 : anton 1.22 dup cfalign+ + ;
207 :    
208 : anton 1.1 >CROSS
209 :     : >image ( taddr -- absaddr ) image @ + ;
210 :     >TARGET
211 : pazsan 1.19 : @ ( taddr -- w ) >image t@ ;
212 :     : ! ( w taddr -- ) >image t! ;
213 : anton 1.1 : c@ ( taddr -- char ) >image c@ ;
214 :     : c! ( char taddr -- ) >image c! ;
215 : anton 1.7 : 2@ ( taddr -- x1 x2 ) T dup cell+ @ swap @ H ;
216 :     : 2! ( x1 x2 taddr -- ) T swap over ! cell+ ! H ;
217 : anton 1.1
218 :     \ Target compilation primitives 06oct92py
219 :     \ included A! 16may93jaw
220 :    
221 :     : here ( -- there ) there ;
222 :     : allot ( n -- ) tdp +! ;
223 :     : , ( w -- ) T here H cell T allot ! H ;
224 :     : c, ( char -- ) T here 1 allot c! H ;
225 :     : align ( -- ) T here H align+ 0 ?DO bl T c, H LOOP ;
226 : anton 1.22 : cfalign ( -- )
227 :     T here H cfalign+ 0 ?DO bl T c, H LOOP ;
228 : anton 1.1
229 :     : A! dup relon T ! H ;
230 :     : A, ( w -- ) T here H relon T , H ;
231 :    
232 :     >CROSS
233 :    
234 :     \ threading modell 13dec92py
235 :    
236 :     >TARGET
237 :     : >body ( cfa -- pfa ) T cell+ cell+ H ;
238 :     >CROSS
239 :    
240 :     \ Ghost Builder 06oct92py
241 :    
242 :     \ <T T> new version with temp variable 10may93jaw
243 :    
244 :     VARIABLE VocTemp
245 :    
246 :     : <T get-current VocTemp ! also Ghosts definitions ;
247 :     : T> previous VocTemp @ set-current ;
248 :    
249 : pazsan 1.46 hex
250 : anton 1.1 4711 Constant <fwd> 4712 Constant <res>
251 : pazsan 1.42 4713 Constant <imm> 4714 Constant <do:>
252 : anton 1.1
253 :     \ iForth makes only immediate directly after create
254 :     \ make atonce trick! ?
255 :    
256 :     Variable atonce atonce off
257 :    
258 :     : NoExec true ABORT" CROSS: Don't execute ghost" ;
259 :    
260 :     : GhostHeader <fwd> , 0 , ['] NoExec , ;
261 :    
262 : pazsan 1.43 : >magic ;
263 :     : >link cell+ ;
264 :     : >exec cell+ cell+ ;
265 : anton 1.1 : >end 3 cells + ;
266 :    
267 : pazsan 1.11 Variable last-ghost
268 : anton 1.1 : Make-Ghost ( "name" -- ghost )
269 :     >in @ GhostName swap >in !
270 :     <T Create atonce @ IF immediate atonce off THEN
271 :     here tuck swap ! ghostheader T>
272 : pazsan 1.11 DOES> dup last-ghost ! >exec @ execute ;
273 : anton 1.1
274 :     \ ghost words 14oct92py
275 :     \ changed: 10may93py/jaw
276 :    
277 :     : gfind ( string -- ghost true/1 / string false )
278 :     \ searches for string in word-list ghosts
279 : pazsan 1.5 dup count [ ' ghosts >body ] ALiteral search-wordlist
280 : anton 1.38 dup IF >r >body nip r> THEN ;
281 : anton 1.1
282 :     VARIABLE Already
283 :    
284 :     : ghost ( "name" -- ghost )
285 :     Already off
286 : pazsan 1.13 >in @ bl word gfind IF Already on nip EXIT THEN
287 : anton 1.1 drop >in ! Make-Ghost ;
288 :    
289 :     \ resolve 14oct92py
290 :    
291 :     : resolve-loop ( ghost tcfa -- ghost tcfa )
292 :     >r dup >link @
293 :     BEGIN dup WHILE dup T @ H r@ rot T ! H REPEAT drop r> ;
294 :    
295 :     \ exists 9may93jaw
296 :    
297 :     : exists ( ghost tcfa -- )
298 :     over GhostNames
299 :     BEGIN @ dup
300 :     WHILE 2dup cell+ @ =
301 :     UNTIL
302 : pazsan 1.18 2 cells + count cr ." CROSS: Exists: " type 4 spaces drop
303 : anton 1.1 swap cell+ !
304 : pazsan 1.24 ELSE true abort" CROSS: Ghostnames inconsistent "
305 : anton 1.1 THEN ;
306 :    
307 :     : resolve ( ghost tcfa -- )
308 :     over >magic @ <fwd> <> IF exists EXIT THEN
309 :     resolve-loop over >link ! <res> swap >magic ! ;
310 :    
311 :     \ gexecute ghost, 01nov92py
312 :    
313 :     : do-forward ( ghost -- )
314 :     >link dup @ there rot ! T A, H ;
315 :     : do-resolve ( ghost -- )
316 :     >link @ T A, H ;
317 :    
318 :     : gexecute ( ghost -- ) dup @
319 :     <fwd> = IF do-forward ELSE do-resolve THEN ;
320 :     : ghost, ghost gexecute ;
321 :    
322 :     \ .unresolved 11may93jaw
323 :    
324 :     variable ResolveFlag
325 :    
326 :     \ ?touched 11may93jaw
327 :    
328 :     : ?touched ( ghost -- flag ) dup >magic @ <fwd> = swap >link @
329 :     0 <> and ;
330 :    
331 :     : ?resolved ( ghostname -- )
332 :     dup cell+ @ ?touched
333 :     IF cell+ cell+ count cr type ResolveFlag on ELSE drop THEN ;
334 :    
335 :     >MINIMAL
336 :     : .unresolved ( -- )
337 :     ResolveFlag off cr ." Unresolved: "
338 :     Ghostnames
339 :     BEGIN @ dup
340 :     WHILE dup ?resolved
341 : anton 1.10 REPEAT drop ResolveFlag @
342 :     IF
343 : pazsan 1.11 abort" Unresolved words!"
344 : anton 1.10 ELSE
345 :     ." Nothing!"
346 :     THEN
347 :     cr ;
348 : anton 1.1
349 :     >CROSS
350 :     \ Header states 12dec92py
351 :    
352 :     : flag! ( 8b -- ) tlast @ dup >r T c@ xor r> c! H ;
353 :    
354 :     VARIABLE ^imm
355 :    
356 :     >TARGET
357 : anton 1.36 : immediate 40 flag!
358 : pazsan 1.18 ^imm @ @ dup <imm> = IF drop EXIT THEN
359 : anton 1.1 <res> <> ABORT" CROSS: Cannot immediate a unresolved word"
360 :     <imm> ^imm @ ! ;
361 : anton 1.36 : restrict 20 flag! ;
362 : anton 1.1 >CROSS
363 :    
364 :     \ ALIAS2 ansforth conform alias 9may93jaw
365 :    
366 :     : ALIAS2 create here 0 , DOES> @ execute ;
367 :     \ usage:
368 : pazsan 1.18 \ ' <name> alias2 bla !
369 : anton 1.1
370 :     \ Target Header Creation 01nov92py
371 :    
372 :     : string, ( addr count -- )
373 : pazsan 1.28 dup T c, H bounds ?DO I c@ T c, H LOOP ;
374 : anton 1.22 : name, ( "name" -- ) bl word count string, T cfalign H ;
375 : anton 1.1 : view, ( -- ) ( dummy ) ;
376 :    
377 : pazsan 1.25 \ Target Document Creation (goes to crossdoc.fd) 05jul95py
378 :    
379 :     s" crossdoc.fd" r/w create-file throw value doc-file-id
380 :     \ contains the file-id of the documentation file
381 :    
382 : pazsan 1.40 : T-\G ( -- )
383 : pazsan 1.25 source >in @ /string doc-file-id write-line throw
384 : pazsan 1.40 postpone \ ;
385 : pazsan 1.25
386 : pazsan 1.39 Variable to-doc to-doc on
387 : pazsan 1.25
388 :     : cross-doc-entry ( -- )
389 :     to-doc @ tlast @ 0<> and \ not an anonymous (i.e. noname) header
390 :     IF
391 :     s" " doc-file-id write-line throw
392 :     s" make-doc " doc-file-id write-file throw
393 :     tlast @ >image count $1F and doc-file-id write-file throw
394 :     >in @
395 :     [char] ( parse 2drop
396 :     [char] ) parse doc-file-id write-file throw
397 :     s" )" doc-file-id write-file throw
398 :     [char] \ parse 2drop
399 : pazsan 1.40 T-\G
400 : pazsan 1.25 >in !
401 : pazsan 1.39 THEN ;
402 : pazsan 1.25
403 : pazsan 1.28 \ Target TAGS creation
404 :    
405 : pazsan 1.39 s" kernel.TAGS" r/w create-file throw value tag-file-id
406 : pazsan 1.28 \ contains the file-id of the tags file
407 :    
408 :     Create tag-beg 2 c, 7F c, bl c,
409 :     Create tag-end 2 c, bl c, 01 c,
410 :     Create tag-bof 1 c, 0C c,
411 :    
412 :     2variable last-loadfilename 0 0 last-loadfilename 2!
413 :    
414 :     : put-load-file-name ( -- )
415 :     loadfilename 2@ last-loadfilename 2@ d<>
416 :     IF
417 :     tag-bof count tag-file-id write-line throw
418 : anton 1.31 sourcefilename 2dup
419 : pazsan 1.28 tag-file-id write-file throw
420 :     last-loadfilename 2!
421 :     s" ,0" tag-file-id write-line throw
422 :     THEN ;
423 :    
424 :     : cross-tag-entry ( -- )
425 :     tlast @ 0<> \ not an anonymous (i.e. noname) header
426 :     IF
427 :     put-load-file-name
428 :     source >in @ min tag-file-id write-file throw
429 :     tag-beg count tag-file-id write-file throw
430 :     tlast @ >image count $1F and tag-file-id write-file throw
431 :     tag-end count tag-file-id write-file throw
432 : anton 1.31 base @ decimal sourceline# 0 <# #s #> tag-file-id write-file throw
433 : pazsan 1.28 \ >in @ 0 <# #s [char] , hold #> tag-file-id write-line throw
434 :     s" ,0" tag-file-id write-line throw
435 :     base !
436 :     THEN ;
437 :    
438 : pazsan 1.43 \ Check for words
439 :    
440 :     Defer skip? ' false IS skip?
441 :    
442 :     : defined? ( -- flag ) \ name
443 :     ghost >magic @ <fwd> <> ;
444 :    
445 :     : needed? ( -- flag ) \ name
446 :     ghost dup >magic @ <fwd> =
447 :     IF >link @ 0<> ELSE drop false THEN ;
448 :    
449 : pazsan 1.44 : doer? ( -- flag ) \ name
450 :     ghost >magic @ <do:> = ;
451 :    
452 : pazsan 1.43 : skip-defs ( -- )
453 :     BEGIN refill WHILE source -trailing nip 0= UNTIL THEN ;
454 :    
455 : pazsan 1.28 \ Target header creation
456 :    
457 : anton 1.1 VARIABLE CreateFlag CreateFlag off
458 :    
459 : pazsan 1.43 : (Theader ( "name" -- ghost )
460 :     \ >in @ bl word count type 2 spaces >in !
461 :     T align H view,
462 : anton 1.1 tlast @ dup 0> IF T 1 cells - THEN A, H there tlast !
463 :     >in @ name, >in ! T here H tlastcfa !
464 :     CreateFlag @ IF
465 : pazsan 1.18 >in @ alias2 swap >in ! \ create alias in target
466 :     >in @ ghost swap >in !
467 :     swap also ghosts ' previous swap ! \ tick ghost and store in alias
468 :     CreateFlag off
469 : anton 1.1 ELSE ghost THEN
470 :     dup >magic ^imm ! \ a pointer for immediate
471 :     Already @ IF dup >end tdoes !
472 :     ELSE 0 tdoes ! THEN
473 : pazsan 1.25 80 flag!
474 : pazsan 1.28 cross-doc-entry cross-tag-entry ;
475 : anton 1.1
476 :     VARIABLE ;Resolve 1 cells allot
477 :    
478 : pazsan 1.11 : Theader ( "name" -- ghost )
479 :     (THeader dup there resolve 0 ;Resolve ! ;
480 : anton 1.1
481 :     >TARGET
482 :     : Alias ( cfa -- ) \ name
483 : pazsan 1.43 >in @ skip? IF 2drop EXIT THEN >in !
484 :     dup 0< has-prims 0= and
485 :     IF
486 :     ." needs prim: " >in @ bl word count type >in ! cr
487 :     THEN
488 :     (THeader over resolve T A, H 80 flag! ;
489 : pazsan 1.42 : Alias: ( cfa -- ) \ name
490 : pazsan 1.43 >in @ skip? IF 2drop EXIT THEN >in !
491 :     dup 0< has-prims 0= and
492 :     IF
493 :     ." needs doer: " >in @ bl word count type >in ! cr
494 :     THEN
495 :     ghost tuck swap resolve <do:> swap >magic ! ;
496 : anton 1.1 >CROSS
497 :    
498 :     \ Conditionals and Comments 11may93jaw
499 :    
500 :     : ;Cond
501 :     postpone ;
502 :     swap ! ; immediate
503 :    
504 :     : Cond: ( -- ) \ name {code } ;
505 :     atonce on
506 :     ghost
507 :     >exec
508 :     :NONAME ;
509 :    
510 :     : restrict? ( -- )
511 :     \ aborts on interprete state - ae
512 :     state @ 0= ABORT" CROSS: Restricted" ;
513 :    
514 :     : Comment ( -- )
515 :     >in @ atonce on ghost swap >in ! ' swap >exec ! ;
516 :    
517 :     Comment ( Comment \
518 :    
519 :     \ Predefined ghosts 12dec92py
520 :    
521 :     ghost 0= drop
522 :     ghost branch ghost ?branch 2drop
523 :     ghost (do) ghost (?do) 2drop
524 :     ghost (for) drop
525 :     ghost (loop) ghost (+loop) 2drop
526 :     ghost (next) drop
527 : pazsan 1.2 ghost unloop ghost ;S 2drop
528 : anton 1.1 ghost lit ghost (compile) ghost ! 2drop drop
529 : anton 1.29 ghost (does>) ghost noop 2drop
530 : anton 1.1 ghost (.") ghost (S") ghost (ABORT") 2drop drop
531 : pazsan 1.41 ghost ' drop
532 : pazsan 1.42 ghost :docol ghost :doesjump ghost :dodoes 2drop drop
533 : pazsan 1.45 ghost over ghost = ghost drop 2drop drop
534 : anton 1.1
535 :     \ compile 10may93jaw
536 :    
537 :     : compile ( -- ) \ name
538 :     restrict?
539 : pazsan 1.13 bl word gfind dup 0= ABORT" CROSS: Can't compile "
540 : anton 1.1 0> ( immediate? )
541 :     IF >exec @ compile,
542 :     ELSE postpone literal postpone gexecute THEN ;
543 :     immediate
544 :    
545 : pazsan 1.42 \ generic threading modell
546 :     : docol, ( -- ) compile :docol T 0 , H ;
547 :    
548 : pazsan 1.46 : dodoes, ( -- ) T cfalign H compile :doesjump T 0 , H ;
549 : pazsan 1.42
550 : pazsan 1.43 [IFUNDEF] (code)
551 :     Defer (code)
552 :     Defer (end-code)
553 :     [THEN]
554 :    
555 : anton 1.1 >TARGET
556 : pazsan 1.43 : Code
557 :     (THeader there resolve
558 : pazsan 1.46 [ has-prims 0= [IF] ITC [ELSE] true [THEN] ] [IF]
559 :     there 2 T cells H + T a, 0 , H
560 :     [THEN]
561 : pazsan 1.43 depth (code) ;
562 :    
563 :     : Code:
564 :     ghost dup there resolve <do:> swap >magic !
565 :     depth (code) ;
566 :    
567 :     : end-code
568 :     depth ?dup IF 1- <> ABORT" CROSS: Stack changed"
569 :     ELSE true ABORT" CROSS: Stack empty" THEN
570 :     (end-code) ;
571 :    
572 : pazsan 1.13 : ' ( -- cfa ) bl word gfind 0= ABORT" CROSS: undefined "
573 : anton 1.1 dup >magic @ <fwd> = ABORT" CROSS: forward " >link @ ;
574 :    
575 :     Cond: ['] compile lit ghost gexecute ;Cond
576 : anton 1.14
577 :     Cond: chars ;Cond
578 : anton 1.1
579 :     >CROSS
580 :     \ tLiteral 12dec92py
581 :    
582 :     : lit, ( n -- ) compile lit T , H ;
583 :     : alit, ( n -- ) compile lit T A, H ;
584 :    
585 :     >TARGET
586 : pazsan 1.40 Cond: \G T-\G ;Cond
587 :    
588 : anton 1.1 Cond: Literal ( n -- ) restrict? lit, ;Cond
589 :     Cond: ALiteral ( n -- ) restrict? alit, ;Cond
590 :    
591 :     : Char ( "<char>" -- ) bl word char+ c@ ;
592 :     Cond: [Char] ( "<char>" -- ) restrict? Char lit, ;Cond
593 :    
594 : pazsan 1.43 \ some special literals 27jan97jaw
595 :    
596 :     Cond: MAXU
597 :     restrict? compile lit
598 :     tcell 0 ?DO FF T c, H LOOP ;Cond
599 :    
600 :     Cond: MINI
601 :     restrict? compile lit
602 :     bigendian IF
603 :     80 T c, H tcell 1 ?DO 0 T c, H LOOP
604 :     ELSE
605 :     tcell 1 ?DO 0 T c, H LOOP 80 T c, H
606 :     THEN
607 :     ;Cond
608 :    
609 :     Cond: MAXI
610 :     restrict? compile lit
611 :     bigendian IF
612 :     7F T c, H tcell 1 ?DO FF T c, H LOOP
613 :     ELSE
614 :     tcell 1 ?DO FF T c, H LOOP 7F T c, H
615 :     THEN
616 :     ;Cond
617 :    
618 : anton 1.1 >CROSS
619 :     \ Target compiling loop 12dec92py
620 :     \ ">tib trick thrown out 10may93jaw
621 :     \ number? defined at the top 11may93jaw
622 :    
623 :     \ compiled word might leave items on stack!
624 :     : tcom ( in name -- )
625 :     gfind ?dup IF 0> IF nip >exec @ execute
626 :     ELSE nip gexecute THEN EXIT THEN
627 :     number? dup IF 0> IF swap lit, THEN lit, drop
628 :     ELSE 2drop >in !
629 :     ghost gexecute THEN ;
630 :    
631 :     >TARGET
632 :     \ : ; DOES> 13dec92py
633 :     \ ] 9may93py/jaw
634 :    
635 :     : ] state on
636 :     BEGIN
637 : pazsan 1.13 BEGIN >in @ bl word
638 : anton 1.1 dup c@ 0= WHILE 2drop refill 0=
639 :     ABORT" CROSS: End of file while target compiling"
640 :     REPEAT
641 :     tcom
642 :     state @
643 :     0=
644 :     UNTIL ;
645 :    
646 :     \ by the way: defining a second interpreter (a compiler-)loop
647 :     \ is not allowed if a system should be ans conform
648 :    
649 :     : : ( -- colon-sys ) \ Name
650 : pazsan 1.43 >in @ skip? IF drop skip-defs EXIT THEN >in !
651 : anton 1.1 (THeader ;Resolve ! there ;Resolve cell+ !
652 :     docol, depth T ] H ;
653 :    
654 : pazsan 1.37 : :noname ( -- colon-sys )
655 : pazsan 1.46 T cfalign H there docol, depth T ] H ;
656 : pazsan 1.37
657 : pazsan 1.2 Cond: EXIT ( -- ) restrict? compile ;S ;Cond
658 : anton 1.6
659 :     Cond: ?EXIT ( -- ) 1 abort" CROSS: using ?exit" ;Cond
660 : pazsan 1.2
661 : anton 1.1 Cond: ; ( -- ) restrict?
662 :     depth ?dup IF 1- <> ABORT" CROSS: Stack changed"
663 :     ELSE true ABORT" CROSS: Stack empty" THEN
664 : pazsan 1.2 compile ;S state off
665 : anton 1.1 ;Resolve @
666 :     IF ;Resolve @ ;Resolve cell+ @ resolve THEN
667 :     ;Cond
668 :     Cond: [ restrict? state off ;Cond
669 :    
670 :     >CROSS
671 : pazsan 1.42 : !does
672 :     tlastcfa @ dup there >r tdp ! compile :dodoes r> tdp ! T cell+ ! H ;
673 : anton 1.1
674 :     >TARGET
675 :     Cond: DOES> restrict?
676 : pazsan 1.46 compile (does>) dodoes, tdoes @ ?dup IF @ T here H resolve THEN
677 : anton 1.1 ;Cond
678 :     : DOES> dodoes, T here H !does depth T ] H ;
679 :    
680 :     >CROSS
681 :     \ Creation 01nov92py
682 :    
683 :     \ Builder 11may93jaw
684 :    
685 :     : Builder ( Create do: "name" -- )
686 :     >in @ alias2 swap dup >in ! >r >r
687 :     Make-Ghost rot swap >exec ! ,
688 :     r> r> >in !
689 : pazsan 1.11 also ghosts ' previous swap ! ;
690 :     \ DOES> dup >exec @ execute ;
691 : anton 1.1
692 :     : gdoes, ( ghost -- ) >end @ dup >magic @ <fwd> <>
693 : pazsan 1.42 IF
694 :     dup >magic @ <do:> =
695 :     IF gexecute T 0 , H EXIT THEN
696 :     THEN
697 :     compile :dodoes gexecute T here H cell - reloff ;
698 : anton 1.1
699 : pazsan 1.11 : TCreate ( -- )
700 :     last-ghost @
701 : anton 1.1 CreateFlag on
702 : pazsan 1.11 Theader >r dup gdoes,
703 :     >end @ >exec @ r> >exec ! ;
704 : anton 1.1
705 :     : Build: ( -- [xt] [colon-sys] )
706 :     :noname postpone TCreate ;
707 :    
708 :     : gdoes> ( ghost -- addr flag )
709 : pazsan 1.11 last-ghost @
710 : anton 1.1 state @ IF gexecute true EXIT THEN
711 :     cell+ @ T >body H false ;
712 :    
713 :     \ DO: ;DO 11may93jaw
714 :     \ changed to ?EXIT 10may93jaw
715 :    
716 :     : DO: ( -- addr [xt] [colon-sys] )
717 :     here ghostheader
718 : pazsan 1.11 :noname postpone gdoes> postpone ?EXIT ;
719 : anton 1.1
720 : pazsan 1.42 : by: ( -- addr [xt] [colon-sys] ) \ name
721 :     ghost
722 :     :noname postpone gdoes> postpone ?EXIT ;
723 :    
724 : anton 1.1 : ;DO ( addr [xt] [colon-sys] -- )
725 :     postpone ; ( S addr xt )
726 :     over >exec ! ; immediate
727 :    
728 :     : by ( -- addr ) \ Name
729 :     ghost >end @ ;
730 :    
731 :     >TARGET
732 :     \ Variables and Constants 05dec92py
733 :    
734 :     Build: ;
735 : pazsan 1.42 by: :dovar ( ghost -- addr ) ;DO
736 : anton 1.1 Builder Create
737 :    
738 :     Build: T 0 , H ;
739 :     by Create
740 :     Builder Variable
741 :    
742 :     Build: T 0 A, H ;
743 :     by Create
744 :     Builder AVariable
745 :    
746 : pazsan 1.3 \ User variables 04may94py
747 :    
748 :     >CROSS
749 :     Variable tup 0 tup !
750 :     Variable tudp 0 tudp !
751 :     : u, ( n -- udp )
752 :     tup @ tudp @ + T ! H
753 : pazsan 1.19 tudp @ dup T cell+ H tudp ! ;
754 : pazsan 1.3 : au, ( n -- udp )
755 :     tup @ tudp @ + T A! H
756 : pazsan 1.19 tudp @ dup T cell+ H tudp ! ;
757 : pazsan 1.3 >TARGET
758 :    
759 :     Build: T 0 u, , H ;
760 : pazsan 1.42 by: :douser ( ghost -- up-addr ) T @ H tup @ + ;DO
761 : anton 1.1 Builder User
762 :    
763 : pazsan 1.3 Build: T 0 u, , 0 u, drop H ;
764 :     by User
765 : anton 1.1 Builder 2User
766 :    
767 : pazsan 1.3 Build: T 0 au, , H ;
768 :     by User
769 : anton 1.1 Builder AUser
770 :    
771 : pazsan 1.44 Build: ( n -- ) ;
772 :     by: :docon ( ghost -- n ) T @ H ;DO
773 :     Builder (Constant)
774 :    
775 : anton 1.1 Build: ( n -- ) T , H ;
776 : pazsan 1.44 by (Constant)
777 : anton 1.1 Builder Constant
778 :    
779 :     Build: ( n -- ) T A, H ;
780 : pazsan 1.44 by (Constant)
781 : anton 1.1 Builder AConstant
782 :    
783 : pazsan 1.24 Build: ( d -- ) T , , H ;
784 :     DO: ( ghost -- d ) T dup cell+ @ swap @ H ;DO
785 :     Builder 2Constant
786 :    
787 : pazsan 1.45 Build: T , H ;
788 : pazsan 1.44 by (Constant)
789 : anton 1.1 Builder Value
790 :    
791 : pazsan 1.45 Build: T A, H ;
792 : pazsan 1.44 by (Constant)
793 : pazsan 1.32 Builder AValue
794 :    
795 : anton 1.1 Build: ( -- ) compile noop ;
796 : pazsan 1.42 by: :dodefer ( ghost -- ) ABORT" CROSS: Don't execute" ;DO
797 : anton 1.1 Builder Defer
798 : pazsan 1.37
799 :     Build: ( inter comp -- ) swap T immediate A, A, H ;
800 :     DO: ( ghost -- ) ABORT" CROSS: Don't execute" ;DO
801 : anton 1.38 Builder interpret/compile:
802 : pazsan 1.24
803 :     \ Sturctures 23feb95py
804 :    
805 :     >CROSS
806 :     : nalign ( addr1 n -- addr2 )
807 :     \ addr2 is the aligned version of addr1 wrt the alignment size n
808 :     1- tuck + swap invert and ;
809 :     >TARGET
810 :    
811 : pazsan 1.44 Build: ;
812 :     by: :dofield T @ H + ;DO
813 :     Builder (Field)
814 :    
815 : pazsan 1.24 Build: >r rot r@ nalign dup T , H ( align1 size offset )
816 :     + swap r> nalign ;
817 : pazsan 1.44 by (Field)
818 : pazsan 1.24 Builder Field
819 :    
820 :     : struct T 0 1 chars H ;
821 :     : end-struct T 2Constant H ;
822 :    
823 :     : cells: ( n -- size align )
824 :     T cells 1 cells H ;
825 :    
826 :     \ ' 2Constant Alias2 end-struct
827 :     \ 0 1 T Chars H 2Constant struct
828 : anton 1.1
829 :     \ structural conditionals 17dec92py
830 :    
831 :     >CROSS
832 :     : ?struc ( flag -- ) ABORT" CROSS: unstructured " ;
833 :     : sys? ( sys -- sys ) dup 0= ?struc ;
834 :     : >mark ( -- sys ) T here 0 , H ;
835 :     : >resolve ( sys -- ) T here over - swap ! H ;
836 :     : <resolve ( sys -- ) T here - , H ;
837 :     >TARGET
838 :    
839 :     \ Structural Conditionals 12dec92py
840 :    
841 :     Cond: BUT restrict? sys? swap ;Cond
842 :     Cond: YET restrict? sys? dup ;Cond
843 :    
844 :     >CROSS
845 :     Variable tleavings
846 :     >TARGET
847 :    
848 :     Cond: DONE ( addr -- ) restrict? tleavings @
849 :     BEGIN 2dup u> 0= WHILE dup T @ H swap >resolve REPEAT
850 :     tleavings ! drop ;Cond
851 :    
852 :     >CROSS
853 :     : (leave T here H tleavings @ T , H tleavings ! ;
854 :     >TARGET
855 :    
856 :     Cond: LEAVE restrict? compile branch (leave ;Cond
857 :     Cond: ?LEAVE restrict? compile 0= compile ?branch (leave ;Cond
858 :    
859 :     \ Structural Conditionals 12dec92py
860 :    
861 :     Cond: AHEAD restrict? compile branch >mark ;Cond
862 :     Cond: IF restrict? compile ?branch >mark ;Cond
863 :     Cond: THEN restrict? sys? dup T @ H ?struc >resolve ;Cond
864 :     Cond: ELSE restrict? sys? compile AHEAD swap compile THEN ;Cond
865 :    
866 :     Cond: BEGIN restrict? T here H ;Cond
867 :     Cond: WHILE restrict? sys? compile IF swap ;Cond
868 :     Cond: AGAIN restrict? sys? compile branch <resolve ;Cond
869 :     Cond: UNTIL restrict? sys? compile ?branch <resolve ;Cond
870 :     Cond: REPEAT restrict? over 0= ?struc compile AGAIN compile THEN ;Cond
871 : pazsan 1.45
872 :     Cond: CASE restrict? 0 ;Cond
873 :     Cond: OF restrict? 1+ >r compile over compile = compile IF compile drop
874 :     r> ;Cond
875 :     Cond: ENDOF restrict? >r compile ELSE r> ;Cond
876 :     Cond: ENDCASE restrict? compile drop 0 ?DO compile THEN LOOP ;Cond
877 : anton 1.1
878 :     \ Structural Conditionals 12dec92py
879 :    
880 :     Cond: DO restrict? compile (do) T here H ;Cond
881 :     Cond: ?DO restrict? compile (?do) (leave T here H ;Cond
882 :     Cond: FOR restrict? compile (for) T here H ;Cond
883 :    
884 :     >CROSS
885 :     : loop] dup <resolve cell - compile DONE compile unloop ;
886 :     >TARGET
887 :    
888 :     Cond: LOOP restrict? sys? compile (loop) loop] ;Cond
889 :     Cond: +LOOP restrict? sys? compile (+loop) loop] ;Cond
890 :     Cond: NEXT restrict? sys? compile (next) loop] ;Cond
891 :    
892 :     \ String words 23feb93py
893 :    
894 :     : ," [char] " parse string, T align H ;
895 :    
896 :     Cond: ." restrict? compile (.") T ," H ;Cond
897 :     Cond: S" restrict? compile (S") T ," H ;Cond
898 :     Cond: ABORT" restrict? compile (ABORT") T ," H ;Cond
899 :    
900 :     Cond: IS T ' >body H compile ALiteral compile ! ;Cond
901 :     : IS T ' >body ! H ;
902 : pazsan 1.9 Cond: TO T ' >body H compile ALiteral compile ! ;Cond
903 :     : TO T ' >body ! H ;
904 : anton 1.1
905 :     \ LINKED ERR" ENV" 2ENV" 18may93jaw
906 :    
907 :     \ linked list primitive
908 :     : linked T here over @ A, swap ! H ;
909 :    
910 :     : err" s" ErrLink linked" evaluate T , H
911 :     [char] " parse string, T align H ;
912 :    
913 :     : env" [char] " parse s" EnvLink linked" evaluate
914 :     string, T align , H ;
915 :    
916 :     : 2env" [char] " parse s" EnvLink linked" evaluate
917 :     here >r string, T align , , H
918 :     r> dup T c@ H 80 and swap T c! H ;
919 :    
920 :     \ compile must be last 22feb93py
921 :    
922 :     Cond: compile ( -- ) restrict? \ name
923 : pazsan 1.13 bl word gfind dup 0= ABORT" CROSS: Can't compile"
924 : anton 1.1 0> IF gexecute
925 :     ELSE dup >magic @ <imm> =
926 :     IF gexecute
927 :     ELSE compile (compile) gexecute THEN THEN ;Cond
928 :    
929 :     Cond: postpone ( -- ) restrict? \ name
930 : pazsan 1.13 bl word gfind dup 0= ABORT" CROSS: Can't compile"
931 : anton 1.1 0> IF gexecute
932 :     ELSE dup >magic @ <imm> =
933 :     IF gexecute
934 :     ELSE compile (compile) gexecute THEN THEN ;Cond
935 :    
936 :     >MINIMAL
937 :     also minimal
938 :     \ Usefull words 13feb93py
939 :    
940 :     : KB 400 * ;
941 :    
942 :     \ define new [IFDEF] and [IFUNDEF] 20may93jaw
943 :    
944 : pazsan 1.43 : defined? defined? ;
945 : pazsan 1.44 : needed? needed? ;
946 :     : doer? doer? ;
947 : anton 1.1
948 : pazsan 1.43 : [IFDEF] defined? postpone [IF] ;
949 :     : [IFUNDEF] defined? 0= postpone [IF] ;
950 : anton 1.1
951 :     \ C: \- \+ Conditional Compiling 09jun93jaw
952 :    
953 : pazsan 1.43 : C: >in @ defined? 0=
954 : anton 1.1 IF >in ! T : H
955 :     ELSE drop
956 :     BEGIN bl word dup c@
957 :     IF count comment? s" ;" compare 0= ?EXIT
958 :     ELSE refill 0= ABORT" CROSS: Out of Input while C:"
959 :     THEN
960 :     AGAIN
961 :     THEN ;
962 :    
963 :     also minimal
964 :    
965 : pazsan 1.43 : \- defined? IF postpone \ THEN ;
966 :     : \+ defined? 0= IF postpone \ THEN ;
967 : anton 1.1
968 :     : [IF] postpone [IF] ;
969 :     : [THEN] postpone [THEN] ;
970 :     : [ELSE] postpone [ELSE] ;
971 :    
972 :     Cond: [IF] [IF] ;Cond
973 :     Cond: [IFDEF] [IFDEF] ;Cond
974 :     Cond: [IFUNDEF] [IFUNDEF] ;Cond
975 :     Cond: [THEN] [THEN] ;Cond
976 :     Cond: [ELSE] [ELSE] ;Cond
977 :    
978 :     \ save-cross 17mar93py
979 :    
980 :     \ i'm not interested in bigforth features this time 10may93jaw
981 :     \ [IFDEF] file
982 :     \ also file
983 :     \ [THEN]
984 :     \ included throw after create-file 11may93jaw
985 :    
986 : anton 1.12 bigendian Constant bigendian
987 : anton 1.1
988 : anton 1.34 Create magic s" Gforth10" here over allot swap move
989 : pazsan 1.26
990 : anton 1.35 char 1 bigendian + cell + magic 7 + c!
991 : pazsan 1.26
992 : anton 1.34 : save-cross ( "image-name" "binary-name" -- )
993 :     bl parse ." Saving to " 2dup type cr
994 : anton 1.1 w/o bin create-file throw >r
995 : pazsan 1.43 NIL IF
996 :     s" #! " r@ write-file throw
997 :     bl parse r@ write-file throw
998 :     s" -i" r@ write-file throw
999 :     #lf r@ emit-file throw
1000 :     r@ dup file-position throw drop 8 mod 8 swap ( file-id limit index )
1001 :     ?do
1002 :     bl over emit-file throw
1003 :     loop
1004 :     drop
1005 :     magic 8 r@ write-file throw \ write magic
1006 :     ELSE
1007 :     bl parse 2drop
1008 :     THEN
1009 : pazsan 1.16 image @ there r@ write-file throw \ write image
1010 : pazsan 1.43 NIL IF
1011 :     bit$ @ there 1- cell>bit rshift 1+
1012 : pazsan 1.16 r@ write-file throw \ write tags
1013 : pazsan 1.43 THEN
1014 : anton 1.1 r> close-file throw ;
1015 :    
1016 :     \ words that should be in minimal
1017 :    
1018 : pazsan 1.43 : here there ;
1019 :     also forth [IFDEF] Label : Label Label ; [THEN] previous
1020 :     : + + ;
1021 :     : or or ;
1022 :     : 1- 1- ;
1023 :     : - - ;
1024 :     : 2* 2* ;
1025 :     : * * ;
1026 :     : / / ;
1027 :     : dup dup ;
1028 :     : over over ;
1029 :     : swap swap ;
1030 :     : rot rot ;
1031 :     : drop drop ;
1032 :     : = = ;
1033 :     : 0= 0= ;
1034 :     : lshift lshift ;
1035 :     : 2/ 2/ ;
1036 : pazsan 1.19 : . . ;
1037 : pazsan 1.42
1038 :     mach-file count included
1039 : anton 1.1
1040 : pazsan 1.43 : all-words ['] false IS skip? ;
1041 :     : needed-words ['] needed? IS skip? ;
1042 :     : undef-words ['] defined? IS skip? ;
1043 : anton 1.1
1044 : pazsan 1.40 : \ postpone \ ; immediate
1045 :     : ( postpone ( ; immediate
1046 : anton 1.1 : include bl word count included ;
1047 :     : .( [char] ) parse type ;
1048 :     : cr cr ;
1049 :    
1050 :     : times 0 ?DO dup T c, H LOOP drop ; \ used for space table creation
1051 :     only forth also minimal definitions
1052 :    
1053 :     \ cross-compiler words
1054 :    
1055 :     : decimal decimal ;
1056 :     : hex hex ;
1057 :    
1058 : pazsan 1.3 : tudp T tudp H ;
1059 : pazsan 1.39 : tup T tup H ;
1060 :    
1061 :     : doc-off false T to-doc H ! ;
1062 :     : doc-on true T to-doc H ! ;
1063 :    
1064 :     minimal
1065 : anton 1.1
1066 :     \ for debugging...
1067 :     : order order ;
1068 :     : words words ;
1069 :     : .s .s ;
1070 :    
1071 :     : bye bye ;
1072 :    
1073 :     \ turnkey direction
1074 :     : H forth ; immediate
1075 :     : T minimal ; immediate
1076 :     : G ghosts ; immediate
1077 :    
1078 :     : turnkey 0 set-order also Target definitions
1079 :     also Minimal also ;
1080 :    
1081 :     \ these ones are pefered:
1082 :    
1083 :     : lock turnkey ;
1084 :     : unlock forth also cross ;
1085 :    
1086 :     unlock definitions also minimal
1087 :     : lock lock ;
1088 :     lock

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help