Annotation of gforth/kernel/comp.fs, revision 1.30
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:
1.30 ! anton 110: : longstring, ( c-addr u -- ) \ gforth
! 111: \G puts down string as cstring
! 112: dup , here swap chars dup allot move ;
! 113:
1.1 pazsan 114: : header, ( c-addr u -- ) \ gforth
115: name-too-long?
116: align here last !
117: current @ 1 or A, \ link field; before revealing, it contains the
118: \ tagged reveal-into wordlist
1.30 ! anton 119: longstring, cfalign
1.1 pazsan 120: alias-mask lastflags cset ;
121:
122: : input-stream-header ( "name" -- )
123: name name-too-short? header, ;
124:
125: : input-stream ( -- ) \ general
126: \G switches back to getting the name from the input stream ;
127: ['] input-stream-header IS (header) ;
128:
129: ' input-stream-header IS (header)
130:
131: \ !! make that a 2variable
132: create nextname-buffer 32 chars allot
133:
134: : nextname-header ( -- )
135: nextname-buffer count header,
136: input-stream ;
137:
138: \ the next name is given in the string
139:
140: : nextname ( c-addr u -- ) \ gforth
1.19 anton 141: \g The next defined word will have the name @var{c-addr u}; the
142: \g defining word will leave the input stream alone.
1.30 ! anton 143: dup 31 u> -&19 and throw \ !! make buffer variable-sized
1.1 pazsan 144: name-too-long?
145: nextname-buffer c! ( c-addr )
146: nextname-buffer count move
147: ['] nextname-header IS (header) ;
148:
149: : noname-header ( -- )
150: 0 last ! cfalign
151: input-stream ;
152:
153: : noname ( -- ) \ gforth
1.19 anton 154: \g The next defined word will be anonymous. The defining word will
155: \g leave the input stream alone. The xt of the defined word will
156: \g be given by @code{lastxt}.
1.1 pazsan 157: ['] noname-header IS (header) ;
158:
159: : lastxt ( -- xt ) \ gforth
1.15 crook 160: \G @i{xt} is the execution token of the last word defined.
1.13 crook 161: \ The main purpose of this word is to get the xt of words defined using noname
1.1 pazsan 162: lastcfa @ ;
163:
164: \ \ literals 17dec92py
165:
166: : Literal ( compilation n -- ; run-time -- n ) \ core
1.27 anton 167: \G Compilation semantics: compile the run-time semantics.@*
168: \G Run-time Semantics: push @i{n}.@*
169: \G Interpretation semantics: undefined.
1.14 jwilke 170: [ [IFDEF] lit, ]
171: lit,
172: [ [ELSE] ]
1.27 anton 173: postpone lit ,
1.14 jwilke 174: [ [THEN] ] ; immediate restrict
1.1 pazsan 175:
176: : ALiteral ( compilation addr -- ; run-time -- addr ) \ gforth
1.14 jwilke 177: [ [IFDEF] alit, ]
178: alit,
179: [ [ELSE] ]
180: postpone lit A,
181: [ [THEN] ] ; immediate restrict
1.1 pazsan 182:
1.8 crook 183: : char ( '<spaces>ccc' -- c ) \ core
1.15 crook 184: \G Skip leading spaces. Parse the string @i{ccc} and return @i{c}, the
185: \G display code representing the first character of @i{ccc}.
1.1 pazsan 186: bl word char+ c@ ;
187:
1.8 crook 188: : [char] ( compilation '<spaces>ccc' -- ; run-time -- c ) \ core bracket-char
1.10 crook 189: \G Compilation: skip leading spaces. Parse the string
1.15 crook 190: \G @i{ccc}. Run-time: return @i{c}, the display code
191: \G representing the first character of @i{ccc}. Interpretation
1.10 crook 192: \G semantics for this word are undefined.
1.1 pazsan 193: char postpone Literal ; immediate restrict
194:
195: \ \ threading 17mar93py
196:
197: : cfa, ( code-address -- ) \ gforth cfa-comma
198: here
199: dup lastcfa !
200: 0 A, 0 , code-address! ;
201:
1.14 jwilke 202: [IFUNDEF] compile,
1.1 pazsan 203: : compile, ( xt -- ) \ core-ext compile-comma
1.26 anton 204: \G Compile the word represented by the execution token @i{xt}
1.15 crook 205: \G into the current definition.
1.1 pazsan 206: A, ;
1.14 jwilke 207: [THEN]
1.1 pazsan 208:
209: : !does ( addr -- ) \ gforth store-does
210: lastxt does-code! ;
211:
212: : (does>) ( R: addr -- )
213: r> cfaligned /does-handler + !does ;
214:
215: : dodoes, ( -- )
216: cfalign here /does-handler allot does-handler! ;
217:
218: : (compile) ( -- ) \ gforth
219: r> dup cell+ >r @ compile, ;
220:
221: \ \ ticks
222:
223: : name>comp ( nt -- w xt ) \ gforth
1.15 crook 224: \G @i{w xt} is the compilation token for the word @i{nt}.
1.1 pazsan 225: (name>comp)
226: 1 = if
227: ['] execute
228: else
229: ['] compile,
230: then ;
231:
232: : [(')] ( compilation "name" -- ; run-time -- nt ) \ gforth bracket-paren-tick
233: (') postpone ALiteral ; immediate restrict
234:
235: : ['] ( compilation. "name" -- ; run-time. -- xt ) \ core bracket-tick
1.15 crook 236: \g @i{xt} represents @i{name}'s interpretation
237: \g semantics. Perform @code{-14 throw} if the word has no
1.1 pazsan 238: \g interpretation semantics.
239: ' postpone ALiteral ; immediate restrict
240:
1.5 anton 241: : COMP' ( "name" -- w xt ) \ gforth comp-tick
1.15 crook 242: \g Compilation token @i{w xt} represents @i{name}'s compilation semantics.
1.1 pazsan 243: (') name>comp ;
244:
245: : [COMP'] ( compilation "name" -- ; run-time -- w xt ) \ gforth bracket-comp-tick
1.15 crook 246: \g Compilation token @i{w xt} represents @i{name}'s compilation semantics.
1.1 pazsan 247: COMP' swap POSTPONE Aliteral POSTPONE ALiteral ; immediate restrict
248:
1.18 jwilke 249: : postpone, ( w xt -- ) \ gforth postpone-comma
1.26 anton 250: \g Compile the compilation semantics represented by the
251: \g compilation token @i{w xt}.
1.18 jwilke 252: dup ['] execute =
253: if
254: drop compile,
255: else
256: dup ['] compile, =
257: if
258: drop POSTPONE (compile) a,
259: else
260: swap POSTPONE aliteral compile,
261: then
262: then ;
263:
264: : POSTPONE ( "name" -- ) \ core
265: \g Compiles the compilation semantics of @i{name}.
266: COMP' postpone, ; immediate restrict
267:
1.1 pazsan 268: \ \ recurse 17may93jaw
269:
270: : recurse ( compilation -- ; run-time ?? -- ?? ) \ core
1.10 crook 271: \g Call the current definition.
1.1 pazsan 272: lastxt compile, ; immediate restrict
273:
274: \ \ compiler loop
275:
276: : compiler ( c-addr u -- )
277: 2dup find-name dup
278: if ( c-addr u nt )
279: nip nip name>comp execute
280: else
281: drop
282: 2dup snumber? dup
283: IF
284: 0>
285: IF
286: swap postpone Literal
287: THEN
288: postpone Literal
289: 2drop
290: ELSE
291: drop compiler-notfound
292: THEN
293: then ;
294:
1.22 crook 295: : [ ( -- ) \ core left-bracket
1.8 crook 296: \G Enter interpretation state. Immediate word.
1.1 pazsan 297: ['] interpreter IS parser state off ; immediate
298:
299: : ] ( -- ) \ core right-bracket
1.8 crook 300: \G Enter compilation state.
1.1 pazsan 301: ['] compiler IS parser state on ;
302:
303: \ \ Strings 22feb93py
304:
305: : ," ( "string"<"> -- ) [char] " parse
306: here over char+ allot place align ;
307:
308: : SLiteral ( Compilation c-addr1 u ; run-time -- c-addr2 u ) \ string
1.15 crook 309: \G Compilation: compile the string specified by @i{c-addr1},
310: \G @i{u} into the current definition. Run-time: return
311: \G @i{c-addr2 u} describing the address and length of the
1.10 crook 312: \G string.
1.1 pazsan 313: postpone (S") here over char+ allot place align ;
314: immediate restrict
315:
316: \ \ abort" 22feb93py
317:
318: : abort" ( compilation 'ccc"' -- ; run-time f -- ) \ core,exception-ext abort-quote
1.15 crook 319: \G If any bit of @i{f} is non-zero, perform the function of @code{-2 throw},
320: \G displaying the string @i{ccc} if there is no exception frame on the
1.10 crook 321: \G exception stack.
1.1 pazsan 322: postpone (abort") ," ; immediate restrict
323:
324: \ \ Header states 23feb93py
325:
326: : cset ( bmask c-addr -- )
1.30 ! anton 327: tuck @ or swap ! ;
1.1 pazsan 328:
329: : creset ( bmask c-addr -- )
1.30 ! anton 330: tuck @ swap invert and swap ! ;
1.1 pazsan 331:
332: : ctoggle ( bmask c-addr -- )
1.30 ! anton 333: tuck @ xor swap ! ;
1.1 pazsan 334:
335: : lastflags ( -- c-addr )
336: \ the address of the flags byte in the last header
337: \ aborts if the last defined word was headerless
338: last @ dup 0= abort" last word was headerless" cell+ ;
339:
340: : immediate ( -- ) \ core
1.10 crook 341: \G Make the compilation semantics of a word be to @code{execute}
342: \G the execution semantics.
1.1 pazsan 343: immediate-mask lastflags cset ;
344:
345: : restrict ( -- ) \ gforth
1.10 crook 346: \G A synonym for @code{compile-only}
1.1 pazsan 347: restrict-mask lastflags cset ;
1.18 jwilke 348:
1.1 pazsan 349: ' restrict alias compile-only ( -- ) \ gforth
1.10 crook 350: \G Remove the interpretation semantics of a word.
1.1 pazsan 351:
352: \ \ Create Variable User Constant 17mar93py
353:
1.15 crook 354: : Alias ( xt "name" -- ) \ gforth
1.1 pazsan 355: Header reveal
356: alias-mask lastflags creset
357: dup A, lastcfa ! ;
358:
359: doer? :dovar [IF]
360:
361: : Create ( "name" -- ) \ core
362: Header reveal dovar: cfa, ;
363: [ELSE]
364:
365: : Create ( "name" -- ) \ core
366: Header reveal here lastcfa ! 0 A, 0 , DOES> ;
367: [THEN]
368:
369: : Variable ( "name" -- ) \ core
370: Create 0 , ;
371:
372: : AVariable ( "name" -- ) \ gforth
373: Create 0 A, ;
374:
1.22 crook 375: : 2Variable ( "name" -- ) \ double two-variable
1.1 pazsan 376: create 0 , 0 , ;
377:
1.21 crook 378: : uallot ( n -- ) \ gforth
379: udp @ swap udp +! ;
1.1 pazsan 380:
381: doer? :douser [IF]
382:
383: : User ( "name" -- ) \ gforth
384: Header reveal douser: cfa, cell uallot , ;
385:
386: : AUser ( "name" -- ) \ gforth
387: User ;
388: [ELSE]
389:
390: : User Create cell uallot , DOES> @ up @ + ;
391:
392: : AUser User ;
393: [THEN]
394:
395: doer? :docon [IF]
396: : (Constant) Header reveal docon: cfa, ;
397: [ELSE]
398: : (Constant) Create DOES> @ ;
399: [THEN]
400:
401: : Constant ( w "name" -- ) \ core
1.15 crook 402: \G Define a constant @i{name} with value @i{w}.
1.1 pazsan 403: \G
1.15 crook 404: \G @i{name} execution: @i{-- w}
1.1 pazsan 405: (Constant) , ;
406:
407: : AConstant ( addr "name" -- ) \ gforth
408: (Constant) A, ;
409:
410: : Value ( w "name" -- ) \ core-ext
411: (Constant) , ;
412:
1.22 crook 413: : 2Constant ( w1 w2 "name" -- ) \ double two-constant
1.1 pazsan 414: Create ( w1 w2 "name" -- )
415: 2,
416: DOES> ( -- w1 w2 )
417: 2@ ;
418:
419: doer? :dofield [IF]
420: : (Field) Header reveal dofield: cfa, ;
421: [ELSE]
422: : (Field) Create DOES> @ + ;
423: [THEN]
424: \ IS Defer What's Defers TO 24feb93py
425:
426: doer? :dodefer [IF]
427:
428: : Defer ( "name" -- ) \ gforth
429: \ !! shouldn't it be initialized with abort or something similar?
430: Header Reveal dodefer: cfa,
431: ['] noop A, ;
1.18 jwilke 432:
1.1 pazsan 433: [ELSE]
434:
435: : Defer ( "name" -- ) \ gforth
436: Create ['] noop A,
437: DOES> @ execute ;
1.18 jwilke 438:
1.1 pazsan 439: [THEN]
440:
1.25 anton 441: : Defers ( compilation "name" -- ; run-time ... -- ... ) \ gforth
442: \G Compiles the present contents of the deferred word @i{name}
443: \G into the current definition. I.e., this produces static
444: \G binding as if @i{name} was not deferred.
1.1 pazsan 445: ' >body @ compile, ; immediate
1.18 jwilke 446:
447: :noname
448: dodoes, here !does ]
449: defstart :-hook ;
450: :noname
451: ;-hook ?struc
452: [ has? xconds [IF] ] exit-like [ [THEN] ]
453: postpone (does>) dodoes,
454: defstart :-hook ;
455: interpret/compile: DOES> ( compilation colon-sys1 -- colon-sys2 ; run-time nest-sys -- ) \ core does
456:
1.20 anton 457: : <IS> ( "name" xt -- ) \ gforth
458: \g Changes the @code{defer}red word @var{name} to execute @var{xt}.
459: ' >body ! ;
460:
461: : [IS] ( compilation "name" -- ; run-time xt -- ) \ gforth bracket-is
462: \g At run-time, changes the @code{defer}red word @var{name} to
463: \g execute @var{xt}.
1.18 jwilke 464: ' >body postpone ALiteral postpone ! ; immediate restrict
465:
1.20 anton 466: ' <IS>
1.18 jwilke 467: ' [IS]
468: interpret/compile: IS ( xt "name" -- ) \ gforth
1.21 crook 469: \G A combined word made up from @code{<IS>} and @code{[IS]}.
1.18 jwilke 470:
1.20 anton 471: ' <IS>
472: ' [IS]
473: interpret/compile: TO ( w "name" -- ) \ core-ext
1.18 jwilke 474:
475: :noname ' >body @ ;
476: :noname ' >body postpone ALiteral postpone @ ;
1.25 anton 477: interpret/compile: What's ( interpretation "name" -- xt; compilation "name" -- ; run-time -- xt ) \ gforth
478: \G @i{Xt} is the XT that is currently assigned to @i{name}.
1.18 jwilke 479:
480: \ \ interpret/compile:
481:
482: struct
483: >body
484: cell% field interpret/compile-int
485: cell% field interpret/compile-comp
486: end-struct interpret/compile-struct
487:
488: : interpret/compile: ( interp-xt comp-xt "name" -- ) \ gforth
489: Create immediate swap A, A,
490: DOES>
491: abort" executed primary cfa of an interpret/compile: word" ;
492: \ state @ IF cell+ THEN perform ;
493:
494: : interpret/compile? ( xt -- flag )
495: >does-code ['] DOES> >does-code = ;
1.1 pazsan 496:
497: \ \ : ; 24feb93py
498:
499: defer :-hook ( sys1 -- sys2 )
500:
501: defer ;-hook ( sys2 -- sys1 )
502:
1.16 jwilke 503: 0 Constant defstart
504:
1.14 jwilke 505: [IFDEF] docol,
506: : (:noname) ( -- colon-sys )
507: \ common factor of : and :noname
508: docol, ]comp defstart ] :-hook ;
509: [ELSE]
1.7 anton 510: : (:noname) ( -- colon-sys )
511: \ common factor of : and :noname
512: docol: cfa, defstart ] :-hook ;
1.14 jwilke 513: [THEN]
1.7 anton 514:
1.1 pazsan 515: : : ( "name" -- colon-sys ) \ core colon
1.7 anton 516: Header (:noname) ;
517:
518: : :noname ( -- xt colon-sys ) \ core-ext colon-no-name
519: 0 last !
520: cfalign here (:noname) ;
1.1 pazsan 521:
1.14 jwilke 522: [IFDEF] fini,
523: : ; ( compilation colon-sys -- ; run-time nest-sys ) \ core semicolon
524: ;-hook ?struc fini, comp[ reveal postpone [ ; immediate restrict
525: [ELSE]
1.1 pazsan 526: : ; ( compilation colon-sys -- ; run-time nest-sys ) \ core semicolon
527: ;-hook ?struc postpone exit reveal postpone [ ; immediate restrict
1.14 jwilke 528: [THEN]
1.1 pazsan 529:
530: \ \ Search list handling: reveal words, recursive 23feb93py
531:
532: : last? ( -- false / nfa nfa )
533: last @ ?dup ;
534:
535: : (reveal) ( nt wid -- )
1.3 pazsan 536: wordlist-id dup >r
1.1 pazsan 537: @ over ( name>link ) !
538: r> ! ;
539:
540: \ make entry in wordlist-map
541: ' (reveal) f83search reveal-method !
542:
543: Variable warnings ( -- addr ) \ gforth
544: G -1 warnings T !
545:
546: : check-shadow ( addr count wid -- )
1.2 pazsan 547: \G prints a warning if the string is already present in the wordlist
548: >r 2dup 2dup r> (search-wordlist) warnings @ and ?dup if
549: >stderr
550: ." redefined " name>string 2dup type
551: compare 0<> if
552: ." with " type
553: else
554: 2drop
555: then
556: space space EXIT
557: then
558: 2drop 2drop ;
1.1 pazsan 559:
560: : reveal ( -- ) \ gforth
561: last?
562: if \ the last word has a header
563: dup ( name>link ) @ 1 and
564: if \ it is still hidden
565: dup ( name>link ) @ 1 xor ( nt wid )
566: 2dup >r name>string r> check-shadow ( nt wid )
567: dup wordlist-map @ reveal-method perform
568: else
569: drop
570: then
571: then ;
572:
573: : rehash ( wid -- )
574: dup wordlist-map @ rehash-method perform ;
575:
576: ' reveal alias recursive ( compilation -- ; run-time -- ) \ gforth
1.10 crook 577: \g Make the current definition visible, enabling it to call itself
1.1 pazsan 578: \g recursively.
579: immediate restrict
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>