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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help