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