1: \ compiler definitions 14sep97jaw
2:
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:
21: \ \ Revisions-Log
22:
23: \ put in seperate file 14sep97jaw
24:
25: \ \ here allot , c, A, 17dec92py
26:
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
37: : allot ( n -- ) \ core
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.
44: here +
45: dup 1- usable-dictionary-end forthstart within -8 and throw
46: dp ! ;
47: [THEN]
48:
49: : c, ( c -- ) \ core c-comma
50: \G Reserve data space for one char and store @i{c} in the space.
51: here 1 chars allot c! ;
52:
53: : , ( w -- ) \ core comma
54: \G Reserve data space for one cell and store @i{w} in the space.
55: here cell allot ! ;
56:
57: : 2, ( w1 w2 -- ) \ gforth
58: \G Reserve data space for two cells and store the double @i{w1
59: \G w2} there, @i{w2} first (lower address).
60: here 2 cells allot 2! ;
61:
62: \ : aligned ( addr -- addr' ) \ core
63: \ [ cell 1- ] Literal + [ -1 cells ] Literal and ;
64:
65: : align ( -- ) \ core
66: \G If the data-space pointer is not aligned, reserve enough space to align it.
67: here dup aligned swap ?DO bl c, LOOP ;
68:
69: \ : faligned ( addr -- f-addr ) \ float f-aligned
70: \ [ 1 floats 1- ] Literal + [ -1 floats ] Literal and ;
71:
72: : falign ( -- ) \ float f-align
73: \G If the data-space pointer is not float-aligned, reserve
74: \G enough space to align it.
75: here dup faligned swap
76: ?DO
77: bl c,
78: LOOP ;
79:
80: : maxalign ( -- ) \ gforth
81: \G Align data-space pointer for all alignment requirements.
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
89: \G Align data-space pointer for code field requirements (i.e., such
90: \G that the corresponding body is maxaligned).
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
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.
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
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}.
152: ['] noname-header IS (header) ;
153:
154: : lastxt ( -- xt ) \ gforth
155: \G @i{xt} is the execution token of the last word defined.
156: \ The main purpose of this word is to get the xt of words defined using noname
157: lastcfa @ ;
158:
159: \ \ literals 17dec92py
160:
161: : Literal ( compilation n -- ; run-time -- n ) \ core
162: \G Compile appropriate code such that, at run-time, @i{n} is placed
163: \G on the stack. Interpretation semantics are undefined.
164: [ [IFDEF] lit, ]
165: lit,
166: [ [ELSE] ]
167: postpone lit ,
168: [ [THEN] ] ; immediate restrict
169:
170: : ALiteral ( compilation addr -- ; run-time -- addr ) \ gforth
171: [ [IFDEF] alit, ]
172: alit,
173: [ [ELSE] ]
174: postpone lit A,
175: [ [THEN] ] ; immediate restrict
176:
177: : char ( '<spaces>ccc' -- c ) \ core
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}.
180: bl word char+ c@ ;
181:
182: : [char] ( compilation '<spaces>ccc' -- ; run-time -- c ) \ core bracket-char
183: \G Compilation: skip leading spaces. Parse the string
184: \G @i{ccc}. Run-time: return @i{c}, the display code
185: \G representing the first character of @i{ccc}. Interpretation
186: \G semantics for this word are undefined.
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:
196: [IFUNDEF] compile,
197: : compile, ( xt -- ) \ core-ext compile-comma
198: \G Compile the word represented by the execution token @i{xt}
199: \G into the current definition.
200: A, ;
201: [THEN]
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
218: \G @i{w xt} is the compilation token for the word @i{nt}.
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
230: \g @i{xt} represents @i{name}'s interpretation
231: \g semantics. Perform @code{-14 throw} if the word has no
232: \g interpretation semantics.
233: ' postpone ALiteral ; immediate restrict
234:
235: : COMP' ( "name" -- w xt ) \ gforth comp-tick
236: \g Compilation token @i{w xt} represents @i{name}'s compilation semantics.
237: (') name>comp ;
238:
239: : [COMP'] ( compilation "name" -- ; run-time -- w xt ) \ gforth bracket-comp-tick
240: \g Compilation token @i{w xt} represents @i{name}'s compilation semantics.
241: COMP' swap POSTPONE Aliteral POSTPONE ALiteral ; immediate restrict
242:
243: : postpone, ( w xt -- ) \ gforth postpone-comma
244: \g Compile the compilation semantics represented by the
245: \g compilation token @i{w xt}.
246: dup ['] execute =
247: if
248: drop compile,
249: else
250: dup ['] compile, =
251: if
252: drop POSTPONE (compile) a,
253: else
254: swap POSTPONE aliteral compile,
255: then
256: then ;
257:
258: : POSTPONE ( "name" -- ) \ core
259: \g Compiles the compilation semantics of @i{name}.
260: COMP' postpone, ; immediate restrict
261:
262: \ \ recurse 17may93jaw
263:
264: : recurse ( compilation -- ; run-time ?? -- ?? ) \ core
265: \g Call the current definition.
266: lastxt compile, ; immediate restrict
267:
268: \ \ compiler loop
269:
270: : compiler ( c-addr u -- )
271: 2dup find-name dup
272: if ( c-addr u nt )
273: nip nip name>comp execute
274: else
275: drop
276: 2dup snumber? dup
277: IF
278: 0>
279: IF
280: swap postpone Literal
281: THEN
282: postpone Literal
283: 2drop
284: ELSE
285: drop compiler-notfound
286: THEN
287: then ;
288:
289: : [ ( -- ) \ core left-bracket
290: \G Enter interpretation state. Immediate word.
291: ['] interpreter IS parser state off ; immediate
292:
293: : ] ( -- ) \ core right-bracket
294: \G Enter compilation state.
295: ['] compiler IS parser state on ;
296:
297: \ \ Strings 22feb93py
298:
299: : ," ( "string"<"> -- ) [char] " parse
300: here over char+ allot place align ;
301:
302: : SLiteral ( Compilation c-addr1 u ; run-time -- c-addr2 u ) \ string
303: \G Compilation: compile the string specified by @i{c-addr1},
304: \G @i{u} into the current definition. Run-time: return
305: \G @i{c-addr2 u} describing the address and length of the
306: \G string.
307: postpone (S") here over char+ allot place align ;
308: immediate restrict
309:
310: \ \ abort" 22feb93py
311:
312: : abort" ( compilation 'ccc"' -- ; run-time f -- ) \ core,exception-ext abort-quote
313: \G If any bit of @i{f} is non-zero, perform the function of @code{-2 throw},
314: \G displaying the string @i{ccc} if there is no exception frame on the
315: \G exception stack.
316: postpone (abort") ," ; immediate restrict
317:
318: \ \ Header states 23feb93py
319:
320: : cset ( bmask c-addr -- )
321: tuck c@ or swap c! ;
322:
323: : creset ( bmask c-addr -- )
324: tuck c@ swap invert and swap c! ;
325:
326: : ctoggle ( bmask c-addr -- )
327: tuck c@ xor swap c! ;
328:
329: : lastflags ( -- c-addr )
330: \ the address of the flags byte in the last header
331: \ aborts if the last defined word was headerless
332: last @ dup 0= abort" last word was headerless" cell+ ;
333:
334: : immediate ( -- ) \ core
335: \G Make the compilation semantics of a word be to @code{execute}
336: \G the execution semantics.
337: immediate-mask lastflags cset ;
338:
339: : restrict ( -- ) \ gforth
340: \G A synonym for @code{compile-only}
341: restrict-mask lastflags cset ;
342:
343: ' restrict alias compile-only ( -- ) \ gforth
344: \G Remove the interpretation semantics of a word.
345:
346: \ \ Create Variable User Constant 17mar93py
347:
348: : Alias ( xt "name" -- ) \ gforth
349: Header reveal
350: alias-mask lastflags creset
351: dup A, lastcfa ! ;
352:
353: doer? :dovar [IF]
354:
355: : Create ( "name" -- ) \ core
356: Header reveal dovar: cfa, ;
357: [ELSE]
358:
359: : Create ( "name" -- ) \ core
360: Header reveal here lastcfa ! 0 A, 0 , DOES> ;
361: [THEN]
362:
363: : Variable ( "name" -- ) \ core
364: Create 0 , ;
365:
366: : AVariable ( "name" -- ) \ gforth
367: Create 0 A, ;
368:
369: : 2Variable ( "name" -- ) \ double two-variable
370: create 0 , 0 , ;
371:
372: : uallot ( n -- ) \ gforth
373: udp @ swap udp +! ;
374:
375: doer? :douser [IF]
376:
377: : User ( "name" -- ) \ gforth
378: Header reveal douser: cfa, cell uallot , ;
379:
380: : AUser ( "name" -- ) \ gforth
381: User ;
382: [ELSE]
383:
384: : User Create cell uallot , DOES> @ up @ + ;
385:
386: : AUser User ;
387: [THEN]
388:
389: doer? :docon [IF]
390: : (Constant) Header reveal docon: cfa, ;
391: [ELSE]
392: : (Constant) Create DOES> @ ;
393: [THEN]
394:
395: : Constant ( w "name" -- ) \ core
396: \G Define a constant @i{name} with value @i{w}.
397: \G
398: \G @i{name} execution: @i{-- w}
399: (Constant) , ;
400:
401: : AConstant ( addr "name" -- ) \ gforth
402: (Constant) A, ;
403:
404: : Value ( w "name" -- ) \ core-ext
405: (Constant) , ;
406:
407: : 2Constant ( w1 w2 "name" -- ) \ double two-constant
408: Create ( w1 w2 "name" -- )
409: 2,
410: DOES> ( -- w1 w2 )
411: 2@ ;
412:
413: doer? :dofield [IF]
414: : (Field) Header reveal dofield: cfa, ;
415: [ELSE]
416: : (Field) Create DOES> @ + ;
417: [THEN]
418: \ IS Defer What's Defers TO 24feb93py
419:
420: doer? :dodefer [IF]
421:
422: : Defer ( "name" -- ) \ gforth
423: \ !! shouldn't it be initialized with abort or something similar?
424: Header Reveal dodefer: cfa,
425: ['] noop A, ;
426:
427: [ELSE]
428:
429: : Defer ( "name" -- ) \ gforth
430: Create ['] noop A,
431: DOES> @ execute ;
432:
433: [THEN]
434:
435: : Defers ( compilation "name" -- ; run-time ... -- ... ) \ gforth
436: \G Compiles the present contents of the deferred word @i{name}
437: \G into the current definition. I.e., this produces static
438: \G binding as if @i{name} was not deferred.
439: ' >body @ compile, ; immediate
440:
441: :noname
442: dodoes, here !does ]
443: defstart :-hook ;
444: :noname
445: ;-hook ?struc
446: [ has? xconds [IF] ] exit-like [ [THEN] ]
447: postpone (does>) dodoes,
448: defstart :-hook ;
449: interpret/compile: DOES> ( compilation colon-sys1 -- colon-sys2 ; run-time nest-sys -- ) \ core does
450:
451: : <IS> ( "name" xt -- ) \ gforth
452: \g Changes the @code{defer}red word @var{name} to execute @var{xt}.
453: ' >body ! ;
454:
455: : [IS] ( compilation "name" -- ; run-time xt -- ) \ gforth bracket-is
456: \g At run-time, changes the @code{defer}red word @var{name} to
457: \g execute @var{xt}.
458: ' >body postpone ALiteral postpone ! ; immediate restrict
459:
460: ' <IS>
461: ' [IS]
462: interpret/compile: IS ( xt "name" -- ) \ gforth
463: \G A combined word made up from @code{<IS>} and @code{[IS]}.
464:
465: ' <IS>
466: ' [IS]
467: interpret/compile: TO ( w "name" -- ) \ core-ext
468:
469: :noname ' >body @ ;
470: :noname ' >body postpone ALiteral postpone @ ;
471: interpret/compile: What's ( interpretation "name" -- xt; compilation "name" -- ; run-time -- xt ) \ gforth
472: \G @i{Xt} is the XT that is currently assigned to @i{name}.
473:
474: \ \ interpret/compile:
475:
476: struct
477: >body
478: cell% field interpret/compile-int
479: cell% field interpret/compile-comp
480: end-struct interpret/compile-struct
481:
482: : interpret/compile: ( interp-xt comp-xt "name" -- ) \ gforth
483: Create immediate swap A, A,
484: DOES>
485: abort" executed primary cfa of an interpret/compile: word" ;
486: \ state @ IF cell+ THEN perform ;
487:
488: : interpret/compile? ( xt -- flag )
489: >does-code ['] DOES> >does-code = ;
490:
491: \ \ : ; 24feb93py
492:
493: defer :-hook ( sys1 -- sys2 )
494:
495: defer ;-hook ( sys2 -- sys1 )
496:
497: 0 Constant defstart
498:
499: [IFDEF] docol,
500: : (:noname) ( -- colon-sys )
501: \ common factor of : and :noname
502: docol, ]comp defstart ] :-hook ;
503: [ELSE]
504: : (:noname) ( -- colon-sys )
505: \ common factor of : and :noname
506: docol: cfa, defstart ] :-hook ;
507: [THEN]
508:
509: : : ( "name" -- colon-sys ) \ core colon
510: Header (:noname) ;
511:
512: : :noname ( -- xt colon-sys ) \ core-ext colon-no-name
513: 0 last !
514: cfalign here (:noname) ;
515:
516: [IFDEF] fini,
517: : ; ( compilation colon-sys -- ; run-time nest-sys ) \ core semicolon
518: ;-hook ?struc fini, comp[ reveal postpone [ ; immediate restrict
519: [ELSE]
520: : ; ( compilation colon-sys -- ; run-time nest-sys ) \ core semicolon
521: ;-hook ?struc postpone exit reveal postpone [ ; immediate restrict
522: [THEN]
523:
524: \ \ Search list handling: reveal words, recursive 23feb93py
525:
526: : last? ( -- false / nfa nfa )
527: last @ ?dup ;
528:
529: : (reveal) ( nt wid -- )
530: wordlist-id dup >r
531: @ over ( name>link ) !
532: r> ! ;
533:
534: \ make entry in wordlist-map
535: ' (reveal) f83search reveal-method !
536:
537: Variable warnings ( -- addr ) \ gforth
538: G -1 warnings T !
539:
540: : check-shadow ( addr count wid -- )
541: \G prints a warning if the string is already present in the wordlist
542: >r 2dup 2dup r> (search-wordlist) warnings @ and ?dup if
543: >stderr
544: ." redefined " name>string 2dup type
545: compare 0<> if
546: ." with " type
547: else
548: 2drop
549: then
550: space space EXIT
551: then
552: 2drop 2drop ;
553:
554: : reveal ( -- ) \ gforth
555: last?
556: if \ the last word has a header
557: dup ( name>link ) @ 1 and
558: if \ it is still hidden
559: dup ( name>link ) @ 1 xor ( nt wid )
560: 2dup >r name>string r> check-shadow ( nt wid )
561: dup wordlist-map @ reveal-method perform
562: else
563: drop
564: then
565: then ;
566:
567: : rehash ( wid -- )
568: dup wordlist-map @ rehash-method perform ;
569:
570: ' reveal alias recursive ( compilation -- ; run-time -- ) \ gforth
571: \g Make the current definition visible, enabling it to call itself
572: \g recursively.
573: immediate restrict
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>