Annotation of gforth/kernel/comp.fs, revision 1.29
1.1 pazsan 1: \ compiler definitions 14sep97jaw
2:
1.28 anton 3: \ Copyright (C) 1995,1996,1997,1998,2000 Free Software Foundation, Inc.
1.6 anton 4:
5: \ This file is part of Gforth.
6:
7: \ Gforth is free software; you can redistribute it and/or
8: \ modify it under the terms of the GNU General Public License
9: \ as published by the Free Software Foundation; either version 2
10: \ of the License, or (at your option) any later version.
11:
12: \ This program is distributed in the hope that it will be useful,
13: \ but WITHOUT ANY WARRANTY; without even the implied warranty of
14: \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: \ GNU General Public License for more details.
16:
17: \ You should have received a copy of the GNU General Public License
18: \ along with this program; if not, write to the Free Software
1.29 ! anton 19: \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
1.6 anton 20:
1.1 pazsan 21: \ \ Revisions-Log
22:
23: \ put in seperate file 14sep97jaw
24:
25: \ \ here allot , c, A, 17dec92py
26:
1.14 jwilke 27: [IFUNDEF] allot
28: [IFUNDEF] forthstart
29: : allot ( n -- ) \ core
30: dup unused u> -8 and throw
31: dp +! ;
32: [THEN]
33: [THEN]
34:
35: \ we default to this version if we have nothing else 05May99jaw
36: [IFUNDEF] allot
1.1 pazsan 37: : allot ( n -- ) \ core
1.23 anton 38: \G Reserve @i{n} address units of data space without
39: \G initialization. @i{n} is a signed number, passing a negative
40: \G @i{n} releases memory. In ANS Forth you can only deallocate
41: \G memory from the current contiguous region in this way. In
42: \G Gforth you can deallocate anything in this way but named words.
43: \G The system does not check this restriction.
1.12 anton 44: here +
45: dup 1- usable-dictionary-end forthstart within -8 and throw
46: dp ! ;
1.14 jwilke 47: [THEN]
1.1 pazsan 48:
1.22 crook 49: : c, ( c -- ) \ core c-comma
1.15 crook 50: \G Reserve data space for one char and store @i{c} in the space.
1.1 pazsan 51: here 1 chars allot c! ;
52:
1.22 crook 53: : , ( w -- ) \ core comma
1.15 crook 54: \G Reserve data space for one cell and store @i{w} in the space.
1.1 pazsan 55: here cell allot ! ;
56:
57: : 2, ( w1 w2 -- ) \ gforth
1.15 crook 58: \G Reserve data space for two cells and store the double @i{w1
1.24 anton 59: \G w2} there, @i{w2} first (lower address).
1.1 pazsan 60: here 2 cells allot 2! ;
61:
62: \ : aligned ( addr -- addr' ) \ core
63: \ [ cell 1- ] Literal + [ -1 cells ] Literal and ;
64:
65: : align ( -- ) \ core
1.15 crook 66: \G If the data-space pointer is not aligned, reserve enough space to align it.
1.1 pazsan 67: here dup aligned swap ?DO bl c, LOOP ;
68:
1.22 crook 69: \ : faligned ( addr -- f-addr ) \ float f-aligned
1.1 pazsan 70: \ [ 1 floats 1- ] Literal + [ -1 floats ] Literal and ;
71:
1.22 crook 72: : falign ( -- ) \ float f-align
1.15 crook 73: \G If the data-space pointer is not float-aligned, reserve
74: \G enough space to align it.
1.1 pazsan 75: here dup faligned swap
76: ?DO
77: bl c,
78: LOOP ;
79:
1.9 anton 80: : maxalign ( -- ) \ gforth
1.23 anton 81: \G Align data-space pointer for all alignment requirements.
1.1 pazsan 82: here dup maxaligned swap
83: ?DO
84: bl c,
85: LOOP ;
86:
87: \ the code field is aligned if its body is maxaligned
88: ' maxalign Alias cfalign ( -- ) \ gforth
1.24 anton 89: \G Align data-space pointer for code field requirements (i.e., such
90: \G that the corresponding body is maxaligned).
1.1 pazsan 91:
92: ' , alias A, ( addr -- ) \ gforth
93:
94: ' NOOP ALIAS const
95:
96: \ \ Header 23feb93py
97:
98: \ input-stream, nextname and noname are quite ugly (passing
99: \ information through global variables), but they are useful for dealing
100: \ with existing/independent defining words
101:
102: defer (header)
103: defer header ( -- ) \ gforth
104: ' (header) IS header
105:
106: : string, ( c-addr u -- ) \ gforth
107: \G puts down string as cstring
108: dup c, here swap chars dup allot move ;
109:
110: : header, ( c-addr u -- ) \ gforth
111: name-too-long?
112: align here last !
113: current @ 1 or A, \ link field; before revealing, it contains the
114: \ tagged reveal-into wordlist
115: string, cfalign
116: alias-mask lastflags cset ;
117:
118: : input-stream-header ( "name" -- )
119: name name-too-short? header, ;
120:
121: : input-stream ( -- ) \ general
122: \G switches back to getting the name from the input stream ;
123: ['] input-stream-header IS (header) ;
124:
125: ' input-stream-header IS (header)
126:
127: \ !! make that a 2variable
128: create nextname-buffer 32 chars allot
129:
130: : nextname-header ( -- )
131: nextname-buffer count header,
132: input-stream ;
133:
134: \ the next name is given in the string
135:
136: : nextname ( c-addr u -- ) \ gforth
1.19 anton 137: \g The next defined word will have the name @var{c-addr u}; the
138: \g defining word will leave the input stream alone.
1.1 pazsan 139: name-too-long?
140: nextname-buffer c! ( c-addr )
141: nextname-buffer count move
142: ['] nextname-header IS (header) ;
143:
144: : noname-header ( -- )
145: 0 last ! cfalign
146: input-stream ;
147:
148: : noname ( -- ) \ gforth
1.19 anton 149: \g The next defined word will be anonymous. The defining word will
150: \g leave the input stream alone. The xt of the defined word will
151: \g be given by @code{lastxt}.
1.1 pazsan 152: ['] noname-header IS (header) ;
153:
154: : lastxt ( -- xt ) \ gforth
1.15 crook 155: \G @i{xt} is the execution token of the last word defined.
1.13 crook 156: \ The main purpose of this word is to get the xt of words defined using noname
1.1 pazsan 157: lastcfa @ ;
158:
159: \ \ literals 17dec92py
160:
161: : Literal ( compilation n -- ; run-time -- n ) \ core
1.27 anton 162: \G Compilation semantics: compile the run-time semantics.@*
163: \G Run-time Semantics: push @i{n}.@*
164: \G Interpretation semantics: undefined.
1.14 jwilke 165: [ [IFDEF] lit, ]
166: lit,
167: [ [ELSE] ]
1.27 anton 168: postpone lit ,
1.14 jwilke 169: [ [THEN] ] ; immediate restrict
1.1 pazsan 170:
171: : ALiteral ( compilation addr -- ; run-time -- addr ) \ gforth
1.14 jwilke 172: [ [IFDEF] alit, ]
173: alit,
174: [ [ELSE] ]
175: postpone lit A,
176: [ [THEN] ] ; immediate restrict
1.1 pazsan 177:
1.8 crook 178: : char ( '<spaces>ccc' -- c ) \ core
1.15 crook 179: \G Skip leading spaces. Parse the string @i{ccc} and return @i{c}, the
180: \G display code representing the first character of @i{ccc}.
1.1 pazsan 181: bl word char+ c@ ;
182:
1.8 crook 183: : [char] ( compilation '<spaces>ccc' -- ; run-time -- c ) \ core bracket-char
1.10 crook 184: \G Compilation: skip leading spaces. Parse the string
1.15 crook 185: \G @i{ccc}. Run-time: return @i{c}, the display code
186: \G representing the first character of @i{ccc}. Interpretation
1.10 crook 187: \G semantics for this word are undefined.
1.1 pazsan 188: char postpone Literal ; immediate restrict
189:
190: \ \ threading 17mar93py
191:
192: : cfa, ( code-address -- ) \ gforth cfa-comma
193: here
194: dup lastcfa !
195: 0 A, 0 , code-address! ;
196:
1.14 jwilke 197: [IFUNDEF] compile,
1.1 pazsan 198: : compile, ( xt -- ) \ core-ext compile-comma
1.26 anton 199: \G Compile the word represented by the execution token @i{xt}
1.15 crook 200: \G into the current definition.
1.1 pazsan 201: A, ;
1.14 jwilke 202: [THEN]
1.1 pazsan 203:
204: : !does ( addr -- ) \ gforth store-does
205: lastxt does-code! ;
206:
207: : (does>) ( R: addr -- )
208: r> cfaligned /does-handler + !does ;
209:
210: : dodoes, ( -- )
211: cfalign here /does-handler allot does-handler! ;
212:
213: : (compile) ( -- ) \ gforth
214: r> dup cell+ >r @ compile, ;
215:
216: \ \ ticks
217:
218: : name>comp ( nt -- w xt ) \ gforth
1.15 crook 219: \G @i{w xt} is the compilation token for the word @i{nt}.
1.1 pazsan 220: (name>comp)
221: 1 = if
222: ['] execute
223: else
224: ['] compile,
225: then ;
226:
227: : [(')] ( compilation "name" -- ; run-time -- nt ) \ gforth bracket-paren-tick
228: (') postpone ALiteral ; immediate restrict
229:
230: : ['] ( compilation. "name" -- ; run-time. -- xt ) \ core bracket-tick
1.15 crook 231: \g @i{xt} represents @i{name}'s interpretation
232: \g semantics. Perform @code{-14 throw} if the word has no
1.1 pazsan 233: \g interpretation semantics.
234: ' postpone ALiteral ; immediate restrict
235:
1.5 anton 236: : COMP' ( "name" -- w xt ) \ gforth comp-tick
1.15 crook 237: \g Compilation token @i{w xt} represents @i{name}'s compilation semantics.
1.1 pazsan 238: (') name>comp ;
239:
240: : [COMP'] ( compilation "name" -- ; run-time -- w xt ) \ gforth bracket-comp-tick
1.15 crook 241: \g Compilation token @i{w xt} represents @i{name}'s compilation semantics.
1.1 pazsan 242: COMP' swap POSTPONE Aliteral POSTPONE ALiteral ; immediate restrict
243:
1.18 jwilke 244: : postpone, ( w xt -- ) \ gforth postpone-comma
1.26 anton 245: \g Compile the compilation semantics represented by the
246: \g compilation token @i{w xt}.
1.18 jwilke 247: dup ['] execute =
248: if
249: drop compile,
250: else
251: dup ['] compile, =
252: if
253: drop POSTPONE (compile) a,
254: else
255: swap POSTPONE aliteral compile,
256: then
257: then ;
258:
259: : POSTPONE ( "name" -- ) \ core
260: \g Compiles the compilation semantics of @i{name}.
261: COMP' postpone, ; immediate restrict
262:
1.1 pazsan 263: \ \ recurse 17may93jaw
264:
265: : recurse ( compilation -- ; run-time ?? -- ?? ) \ core
1.10 crook 266: \g Call the current definition.
1.1 pazsan 267: lastxt compile, ; immediate restrict
268:
269: \ \ compiler loop
270:
271: : compiler ( c-addr u -- )
272: 2dup find-name dup
273: if ( c-addr u nt )
274: nip nip name>comp execute
275: else
276: drop
277: 2dup snumber? dup
278: IF
279: 0>
280: IF
281: swap postpone Literal
282: THEN
283: postpone Literal
284: 2drop
285: ELSE
286: drop compiler-notfound
287: THEN
288: then ;
289:
1.22 crook 290: : [ ( -- ) \ core left-bracket
1.8 crook 291: \G Enter interpretation state. Immediate word.
1.1 pazsan 292: ['] interpreter IS parser state off ; immediate
293:
294: : ] ( -- ) \ core right-bracket
1.8 crook 295: \G Enter compilation state.
1.1 pazsan 296: ['] compiler IS parser state on ;
297:
298: \ \ Strings 22feb93py
299:
300: : ," ( "string"<"> -- ) [char] " parse
301: here over char+ allot place align ;
302:
303: : SLiteral ( Compilation c-addr1 u ; run-time -- c-addr2 u ) \ string
1.15 crook 304: \G Compilation: compile the string specified by @i{c-addr1},
305: \G @i{u} into the current definition. Run-time: return
306: \G @i{c-addr2 u} describing the address and length of the
1.10 crook 307: \G string.
1.1 pazsan 308: postpone (S") here over char+ allot place align ;
309: immediate restrict
310:
311: \ \ abort" 22feb93py
312:
313: : abort" ( compilation 'ccc"' -- ; run-time f -- ) \ core,exception-ext abort-quote
1.15 crook 314: \G If any bit of @i{f} is non-zero, perform the function of @code{-2 throw},
315: \G displaying the string @i{ccc} if there is no exception frame on the
1.10 crook 316: \G exception stack.
1.1 pazsan 317: postpone (abort") ," ; immediate restrict
318:
319: \ \ Header states 23feb93py
320:
321: : cset ( bmask c-addr -- )
322: tuck c@ or swap c! ;
323:
324: : creset ( bmask c-addr -- )
325: tuck c@ swap invert and swap c! ;
326:
327: : ctoggle ( bmask c-addr -- )
328: tuck c@ xor swap c! ;
329:
330: : lastflags ( -- c-addr )
331: \ the address of the flags byte in the last header
332: \ aborts if the last defined word was headerless
333: last @ dup 0= abort" last word was headerless" cell+ ;
334:
335: : immediate ( -- ) \ core
1.10 crook 336: \G Make the compilation semantics of a word be to @code{execute}
337: \G the execution semantics.
1.1 pazsan 338: immediate-mask lastflags cset ;
339:
340: : restrict ( -- ) \ gforth
1.10 crook 341: \G A synonym for @code{compile-only}
1.1 pazsan 342: restrict-mask lastflags cset ;
1.18 jwilke 343:
1.1 pazsan 344: ' restrict alias compile-only ( -- ) \ gforth
1.10 crook 345: \G Remove the interpretation semantics of a word.
1.1 pazsan 346:
347: \ \ Create Variable User Constant 17mar93py
348:
1.15 crook 349: : Alias ( xt "name" -- ) \ gforth
1.1 pazsan 350: Header reveal
351: alias-mask lastflags creset
352: dup A, lastcfa ! ;
353:
354: doer? :dovar [IF]
355:
356: : Create ( "name" -- ) \ core
357: Header reveal dovar: cfa, ;
358: [ELSE]
359:
360: : Create ( "name" -- ) \ core
361: Header reveal here lastcfa ! 0 A, 0 , DOES> ;
362: [THEN]
363:
364: : Variable ( "name" -- ) \ core
365: Create 0 , ;
366:
367: : AVariable ( "name" -- ) \ gforth
368: Create 0 A, ;
369:
1.22 crook 370: : 2Variable ( "name" -- ) \ double two-variable
1.1 pazsan 371: create 0 , 0 , ;
372:
1.21 crook 373: : uallot ( n -- ) \ gforth
374: udp @ swap udp +! ;
1.1 pazsan 375:
376: doer? :douser [IF]
377:
378: : User ( "name" -- ) \ gforth
379: Header reveal douser: cfa, cell uallot , ;
380:
381: : AUser ( "name" -- ) \ gforth
382: User ;
383: [ELSE]
384:
385: : User Create cell uallot , DOES> @ up @ + ;
386:
387: : AUser User ;
388: [THEN]
389:
390: doer? :docon [IF]
391: : (Constant) Header reveal docon: cfa, ;
392: [ELSE]
393: : (Constant) Create DOES> @ ;
394: [THEN]
395:
396: : Constant ( w "name" -- ) \ core
1.15 crook 397: \G Define a constant @i{name} with value @i{w}.
1.1 pazsan 398: \G
1.15 crook 399: \G @i{name} execution: @i{-- w}
1.1 pazsan 400: (Constant) , ;
401:
402: : AConstant ( addr "name" -- ) \ gforth
403: (Constant) A, ;
404:
405: : Value ( w "name" -- ) \ core-ext
406: (Constant) , ;
407:
1.22 crook 408: : 2Constant ( w1 w2 "name" -- ) \ double two-constant
1.1 pazsan 409: Create ( w1 w2 "name" -- )
410: 2,
411: DOES> ( -- w1 w2 )
412: 2@ ;
413:
414: doer? :dofield [IF]
415: : (Field) Header reveal dofield: cfa, ;
416: [ELSE]
417: : (Field) Create DOES> @ + ;
418: [THEN]
419: \ IS Defer What's Defers TO 24feb93py
420:
421: doer? :dodefer [IF]
422:
423: : Defer ( "name" -- ) \ gforth
424: \ !! shouldn't it be initialized with abort or something similar?
425: Header Reveal dodefer: cfa,
426: ['] noop A, ;
1.18 jwilke 427:
1.1 pazsan 428: [ELSE]
429:
430: : Defer ( "name" -- ) \ gforth
431: Create ['] noop A,
432: DOES> @ execute ;
1.18 jwilke 433:
1.1 pazsan 434: [THEN]
435:
1.25 anton 436: : Defers ( compilation "name" -- ; run-time ... -- ... ) \ gforth
437: \G Compiles the present contents of the deferred word @i{name}
438: \G into the current definition. I.e., this produces static
439: \G binding as if @i{name} was not deferred.
1.1 pazsan 440: ' >body @ compile, ; immediate
1.18 jwilke 441:
442: :noname
443: dodoes, here !does ]
444: defstart :-hook ;
445: :noname
446: ;-hook ?struc
447: [ has? xconds [IF] ] exit-like [ [THEN] ]
448: postpone (does>) dodoes,
449: defstart :-hook ;
450: interpret/compile: DOES> ( compilation colon-sys1 -- colon-sys2 ; run-time nest-sys -- ) \ core does
451:
1.20 anton 452: : <IS> ( "name" xt -- ) \ gforth
453: \g Changes the @code{defer}red word @var{name} to execute @var{xt}.
454: ' >body ! ;
455:
456: : [IS] ( compilation "name" -- ; run-time xt -- ) \ gforth bracket-is
457: \g At run-time, changes the @code{defer}red word @var{name} to
458: \g execute @var{xt}.
1.18 jwilke 459: ' >body postpone ALiteral postpone ! ; immediate restrict
460:
1.20 anton 461: ' <IS>
1.18 jwilke 462: ' [IS]
463: interpret/compile: IS ( xt "name" -- ) \ gforth
1.21 crook 464: \G A combined word made up from @code{<IS>} and @code{[IS]}.
1.18 jwilke 465:
1.20 anton 466: ' <IS>
467: ' [IS]
468: interpret/compile: TO ( w "name" -- ) \ core-ext
1.18 jwilke 469:
470: :noname ' >body @ ;
471: :noname ' >body postpone ALiteral postpone @ ;
1.25 anton 472: interpret/compile: What's ( interpretation "name" -- xt; compilation "name" -- ; run-time -- xt ) \ gforth
473: \G @i{Xt} is the XT that is currently assigned to @i{name}.
1.18 jwilke 474:
475: \ \ interpret/compile:
476:
477: struct
478: >body
479: cell% field interpret/compile-int
480: cell% field interpret/compile-comp
481: end-struct interpret/compile-struct
482:
483: : interpret/compile: ( interp-xt comp-xt "name" -- ) \ gforth
484: Create immediate swap A, A,
485: DOES>
486: abort" executed primary cfa of an interpret/compile: word" ;
487: \ state @ IF cell+ THEN perform ;
488:
489: : interpret/compile? ( xt -- flag )
490: >does-code ['] DOES> >does-code = ;
1.1 pazsan 491:
492: \ \ : ; 24feb93py
493:
494: defer :-hook ( sys1 -- sys2 )
495:
496: defer ;-hook ( sys2 -- sys1 )
497:
1.16 jwilke 498: 0 Constant defstart
499:
1.14 jwilke 500: [IFDEF] docol,
501: : (:noname) ( -- colon-sys )
502: \ common factor of : and :noname
503: docol, ]comp defstart ] :-hook ;
504: [ELSE]
1.7 anton 505: : (:noname) ( -- colon-sys )
506: \ common factor of : and :noname
507: docol: cfa, defstart ] :-hook ;
1.14 jwilke 508: [THEN]
1.7 anton 509:
1.1 pazsan 510: : : ( "name" -- colon-sys ) \ core colon
1.7 anton 511: Header (:noname) ;
512:
513: : :noname ( -- xt colon-sys ) \ core-ext colon-no-name
514: 0 last !
515: cfalign here (:noname) ;
1.1 pazsan 516:
1.14 jwilke 517: [IFDEF] fini,
518: : ; ( compilation colon-sys -- ; run-time nest-sys ) \ core semicolon
519: ;-hook ?struc fini, comp[ reveal postpone [ ; immediate restrict
520: [ELSE]
1.1 pazsan 521: : ; ( compilation colon-sys -- ; run-time nest-sys ) \ core semicolon
522: ;-hook ?struc postpone exit reveal postpone [ ; immediate restrict
1.14 jwilke 523: [THEN]
1.1 pazsan 524:
525: \ \ Search list handling: reveal words, recursive 23feb93py
526:
527: : last? ( -- false / nfa nfa )
528: last @ ?dup ;
529:
530: : (reveal) ( nt wid -- )
1.3 pazsan 531: wordlist-id dup >r
1.1 pazsan 532: @ over ( name>link ) !
533: r> ! ;
534:
535: \ make entry in wordlist-map
536: ' (reveal) f83search reveal-method !
537:
538: Variable warnings ( -- addr ) \ gforth
539: G -1 warnings T !
540:
541: : check-shadow ( addr count wid -- )
1.2 pazsan 542: \G prints a warning if the string is already present in the wordlist
543: >r 2dup 2dup r> (search-wordlist) warnings @ and ?dup if
544: >stderr
545: ." redefined " name>string 2dup type
546: compare 0<> if
547: ." with " type
548: else
549: 2drop
550: then
551: space space EXIT
552: then
553: 2drop 2drop ;
1.1 pazsan 554:
555: : reveal ( -- ) \ gforth
556: last?
557: if \ the last word has a header
558: dup ( name>link ) @ 1 and
559: if \ it is still hidden
560: dup ( name>link ) @ 1 xor ( nt wid )
561: 2dup >r name>string r> check-shadow ( nt wid )
562: dup wordlist-map @ reveal-method perform
563: else
564: drop
565: then
566: then ;
567:
568: : rehash ( wid -- )
569: dup wordlist-map @ rehash-method perform ;
570:
571: ' reveal alias recursive ( compilation -- ; run-time -- ) \ gforth
1.10 crook 572: \g Make the current definition visible, enabling it to call itself
1.1 pazsan 573: \g recursively.
574: immediate restrict
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>