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