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 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.
43: here +
44: dup 1- usable-dictionary-end forthstart within -8 and throw
45: dp ! ;
46: [THEN]
47:
48: : c, ( c -- ) \ core
49: \G Reserve data space for one char and store @i{c} in the space.
50: here 1 chars allot c! ;
51:
52: : , ( w -- ) \ core
53: \G Reserve data space for one cell and store @i{w} in the space.
54: here cell allot ! ;
55:
56: : 2, ( w1 w2 -- ) \ gforth
57: \G Reserve data space for two cells and store the double @i{w1
58: \G w2} in the space.
59: here 2 cells allot 2! ;
60:
61: \ : aligned ( addr -- addr' ) \ core
62: \ [ cell 1- ] Literal + [ -1 cells ] Literal and ;
63:
64: : align ( -- ) \ core
65: \G If the data-space pointer is not aligned, reserve enough space to align it.
66: here dup aligned swap ?DO bl c, LOOP ;
67:
68: \ : faligned ( addr -- f-addr ) \ float
69: \ [ 1 floats 1- ] Literal + [ -1 floats ] Literal and ;
70:
71: : falign ( -- ) \ float
72: \G If the data-space pointer is not float-aligned, reserve
73: \G enough space to align it.
74: here dup faligned swap
75: ?DO
76: bl c,
77: LOOP ;
78:
79: : maxalign ( -- ) \ gforth
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
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.
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
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}.
148: ['] noname-header IS (header) ;
149:
150: : lastxt ( -- xt ) \ gforth
151: \G @i{xt} is the execution token of the last word defined.
152: \ The main purpose of this word is to get the xt of words defined using noname
153: lastcfa @ ;
154:
155: \ \ literals 17dec92py
156:
157: : Literal ( compilation n -- ; run-time -- n ) \ core
158: \G Compile appropriate code such that, at run-time, @i{n} is placed
159: \G on the stack. Interpretation semantics are undefined.
160: [ [IFDEF] lit, ]
161: lit,
162: [ [ELSE] ]
163: postpone lit ,
164: [ [THEN] ] ; immediate restrict
165:
166: : ALiteral ( compilation addr -- ; run-time -- addr ) \ gforth
167: [ [IFDEF] alit, ]
168: alit,
169: [ [ELSE] ]
170: postpone lit A,
171: [ [THEN] ] ; immediate restrict
172:
173: : char ( '<spaces>ccc' -- c ) \ core
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}.
176: bl word char+ c@ ;
177:
178: : [char] ( compilation '<spaces>ccc' -- ; run-time -- c ) \ core bracket-char
179: \G Compilation: skip leading spaces. Parse the string
180: \G @i{ccc}. Run-time: return @i{c}, the display code
181: \G representing the first character of @i{ccc}. Interpretation
182: \G semantics for this word are undefined.
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:
192: [IFUNDEF] compile,
193: : compile, ( xt -- ) \ core-ext compile-comma
194: \G Compile the word represented by the execution token, @i{xt},
195: \G into the current definition.
196: A, ;
197: [THEN]
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
214: \G @i{w xt} is the compilation token for the word @i{nt}.
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
226: \g @i{xt} represents @i{name}'s interpretation
227: \g semantics. Perform @code{-14 throw} if the word has no
228: \g interpretation semantics.
229: ' postpone ALiteral ; immediate restrict
230:
231: : COMP' ( "name" -- w xt ) \ gforth comp-tick
232: \g Compilation token @i{w xt} represents @i{name}'s compilation semantics.
233: (') name>comp ;
234:
235: : [COMP'] ( compilation "name" -- ; run-time -- w xt ) \ gforth bracket-comp-tick
236: \g Compilation token @i{w xt} represents @i{name}'s compilation semantics.
237: COMP' swap POSTPONE Aliteral POSTPONE ALiteral ; immediate restrict
238:
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:
257: \ \ recurse 17may93jaw
258:
259: : recurse ( compilation -- ; run-time ?? -- ?? ) \ core
260: \g Call the current definition.
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:
284: : [ ( -- ) \ core left-bracket
285: \G Enter interpretation state. Immediate word.
286: ['] interpreter IS parser state off ; immediate
287:
288: : ] ( -- ) \ core right-bracket
289: \G Enter compilation state.
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
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
301: \G string.
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
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
310: \G exception stack.
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
330: \G Make the compilation semantics of a word be to @code{execute}
331: \G the execution semantics.
332: immediate-mask lastflags cset ;
333:
334: : restrict ( -- ) \ gforth
335: \G A synonym for @code{compile-only}
336: restrict-mask lastflags cset ;
337:
338: ' restrict alias compile-only ( -- ) \ gforth
339: \G Remove the interpretation semantics of a word.
340:
341: \ \ Create Variable User Constant 17mar93py
342:
343: : Alias ( xt "name" -- ) \ gforth
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:
364: : 2Variable ( "name" -- ) \ double
365: create 0 , 0 , ;
366:
367: : uallot ( n -- ) udp @ swap udp +! ;
368:
369: doer? :douser [IF]
370:
371: : User ( "name" -- ) \ gforth
372: Header reveal douser: cfa, cell uallot , ;
373:
374: : AUser ( "name" -- ) \ gforth
375: User ;
376: [ELSE]
377:
378: : User Create cell uallot , DOES> @ up @ + ;
379:
380: : AUser User ;
381: [THEN]
382:
383: doer? :docon [IF]
384: : (Constant) Header reveal docon: cfa, ;
385: [ELSE]
386: : (Constant) Create DOES> @ ;
387: [THEN]
388:
389: : Constant ( w "name" -- ) \ core
390: \G Define a constant @i{name} with value @i{w}.
391: \G
392: \G @i{name} execution: @i{-- w}
393: (Constant) , ;
394:
395: : AConstant ( addr "name" -- ) \ gforth
396: (Constant) A, ;
397:
398: : Value ( w "name" -- ) \ core-ext
399: (Constant) , ;
400:
401: : 2Constant ( w1 w2 "name" -- ) \ double
402: Create ( w1 w2 "name" -- )
403: 2,
404: DOES> ( -- w1 w2 )
405: 2@ ;
406:
407: doer? :dofield [IF]
408: : (Field) Header reveal dofield: cfa, ;
409: [ELSE]
410: : (Field) Create DOES> @ + ;
411: [THEN]
412: \ IS Defer What's Defers TO 24feb93py
413:
414: doer? :dodefer [IF]
415:
416: : Defer ( "name" -- ) \ gforth
417: \ !! shouldn't it be initialized with abort or something similar?
418: Header Reveal dodefer: cfa,
419: ['] noop A, ;
420:
421: [ELSE]
422:
423: : Defer ( "name" -- ) \ gforth
424: Create ['] noop A,
425: DOES> @ execute ;
426:
427: [THEN]
428:
429: : Defers ( "name" -- ) \ gforth
430: ' >body @ compile, ; immediate
431:
432: :noname
433: dodoes, here !does ]
434: defstart :-hook ;
435: :noname
436: ;-hook ?struc
437: [ has? xconds [IF] ] exit-like [ [THEN] ]
438: postpone (does>) dodoes,
439: defstart :-hook ;
440: interpret/compile: DOES> ( compilation colon-sys1 -- colon-sys2 ; run-time nest-sys -- ) \ core does
441:
442: : <IS> ( "name" xt -- ) \ gforth
443: \g Changes the @code{defer}red word @var{name} to execute @var{xt}.
444: ' >body ! ;
445:
446: : [IS] ( compilation "name" -- ; run-time xt -- ) \ gforth bracket-is
447: \g At run-time, changes the @code{defer}red word @var{name} to
448: \g execute @var{xt}.
449: ' >body postpone ALiteral postpone ! ; immediate restrict
450:
451: ' <IS>
452: ' [IS]
453: interpret/compile: IS ( xt "name" -- ) \ gforth
454:
455: ' <IS>
456: ' [IS]
457: interpret/compile: TO ( w "name" -- ) \ core-ext
458:
459: :noname ' >body @ ;
460: :noname ' >body postpone ALiteral postpone @ ;
461: interpret/compile: What's ( "name" -- addr ) \ gforth
462:
463: \ \ interpret/compile:
464:
465: struct
466: >body
467: cell% field interpret/compile-int
468: cell% field interpret/compile-comp
469: end-struct interpret/compile-struct
470:
471: : interpret/compile: ( interp-xt comp-xt "name" -- ) \ gforth
472: Create immediate swap A, A,
473: DOES>
474: abort" executed primary cfa of an interpret/compile: word" ;
475: \ state @ IF cell+ THEN perform ;
476:
477: : interpret/compile? ( xt -- flag )
478: >does-code ['] DOES> >does-code = ;
479:
480: \ \ : ; 24feb93py
481:
482: defer :-hook ( sys1 -- sys2 )
483:
484: defer ;-hook ( sys2 -- sys1 )
485:
486: 0 Constant defstart
487:
488: [IFDEF] docol,
489: : (:noname) ( -- colon-sys )
490: \ common factor of : and :noname
491: docol, ]comp defstart ] :-hook ;
492: [ELSE]
493: : (:noname) ( -- colon-sys )
494: \ common factor of : and :noname
495: docol: cfa, defstart ] :-hook ;
496: [THEN]
497:
498: : : ( "name" -- colon-sys ) \ core colon
499: Header (:noname) ;
500:
501: : :noname ( -- xt colon-sys ) \ core-ext colon-no-name
502: 0 last !
503: cfalign here (:noname) ;
504:
505: [IFDEF] fini,
506: : ; ( compilation colon-sys -- ; run-time nest-sys ) \ core semicolon
507: ;-hook ?struc fini, comp[ reveal postpone [ ; immediate restrict
508: [ELSE]
509: : ; ( compilation colon-sys -- ; run-time nest-sys ) \ core semicolon
510: ;-hook ?struc postpone exit reveal postpone [ ; immediate restrict
511: [THEN]
512:
513: \ \ Search list handling: reveal words, recursive 23feb93py
514:
515: : last? ( -- false / nfa nfa )
516: last @ ?dup ;
517:
518: : (reveal) ( nt wid -- )
519: wordlist-id dup >r
520: @ over ( name>link ) !
521: r> ! ;
522:
523: \ make entry in wordlist-map
524: ' (reveal) f83search reveal-method !
525:
526: Variable warnings ( -- addr ) \ gforth
527: G -1 warnings T !
528:
529: : check-shadow ( addr count wid -- )
530: \G prints a warning if the string is already present in the wordlist
531: >r 2dup 2dup r> (search-wordlist) warnings @ and ?dup if
532: >stderr
533: ." redefined " name>string 2dup type
534: compare 0<> if
535: ." with " type
536: else
537: 2drop
538: then
539: space space EXIT
540: then
541: 2drop 2drop ;
542:
543: : reveal ( -- ) \ gforth
544: last?
545: if \ the last word has a header
546: dup ( name>link ) @ 1 and
547: if \ it is still hidden
548: dup ( name>link ) @ 1 xor ( nt wid )
549: 2dup >r name>string r> check-shadow ( nt wid )
550: dup wordlist-map @ reveal-method perform
551: else
552: drop
553: then
554: then ;
555:
556: : rehash ( wid -- )
557: dup wordlist-map @ rehash-method perform ;
558:
559: ' reveal alias recursive ( compilation -- ; run-time -- ) \ gforth
560: \g Make the current definition visible, enabling it to call itself
561: \g recursively.
562: immediate restrict
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>