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