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: defer 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:
210: ' , is compile,
211: [THEN]
212:
213: has? peephole [IF]
214: : peephole-compile, ( xt -- )
215: \ compile xt, appending its code to the current dynamic superinstruction
216: compile-prim , ;
217:
218: \ : peephole-compile, ( xt -- )
219: \ \ compile xt, possibly combining it with the previous compiled xt
220: \ \ into a superinstruction (static superinstructions)
221: \ last-compiled @ ?dup if
222: \ @ over peeptable peephole-opt ?dup if
223: \ last-compiled @ ! drop EXIT
224: \ then
225: \ then
226: \ here last-compiled !
227: \ dyn-compile, ;
228:
229: : compile-to-prims, ( xt -- )
230: \G compile xt to use primitives (and their peephole optimization)
231: \G instead of ","-ing the xt.
232: \ !! all POSTPONEs here postpone primitives; this can be optimized
233: dup >does-code if
234: POSTPONE does-exec , EXIT
235: then
236: dup >code-address CASE
237: docon: OF >body POSTPONE lit@ , EXIT ENDOF
238: \ docon is also used by VALUEs, so don't @ at compile time
239: docol: OF >body POSTPONE call , EXIT ENDOF
240: dovar: OF >body POSTPONE literal EXIT ENDOF
241: douser: OF >body @ POSTPONE useraddr , EXIT ENDOF
242: dodefer: OF >body POSTPONE lit-perform , EXIT
243: ENDOF
244: dofield: OF >body @ POSTPONE lit+ , EXIT ENDOF
245: ENDCASE
246: peephole-compile, ;
247:
248: ' compile-to-prims, IS compile,
249: [ELSE]
250: ' , is compile,
251: [THEN]
252:
253: : !does ( addr -- ) \ gforth store-does
254: lastxt does-code! ;
255:
256: : (does>) ( R: addr -- )
257: r> cfaligned /does-handler + !does ;
258:
259: : dodoes, ( -- )
260: cfalign here /does-handler allot does-handler! ;
261:
262: : (compile) ( -- ) \ gforth
263: r> dup cell+ >r @ compile, ;
264:
265: \ \ ticks
266:
267: : name>comp ( nt -- w xt ) \ gforth
268: \G @i{w xt} is the compilation token for the word @i{nt}.
269: (name>comp)
270: 1 = if
271: ['] execute
272: else
273: ['] compile,
274: then ;
275:
276: : [(')] ( compilation "name" -- ; run-time -- nt ) \ gforth bracket-paren-tick
277: (') postpone ALiteral ; immediate restrict
278:
279: : ['] ( compilation. "name" -- ; run-time. -- xt ) \ core bracket-tick
280: \g @i{xt} represents @i{name}'s interpretation
281: \g semantics. Perform @code{-14 throw} if the word has no
282: \g interpretation semantics.
283: ' postpone ALiteral ; immediate restrict
284:
285: : COMP' ( "name" -- w xt ) \ gforth comp-tick
286: \g Compilation token @i{w xt} represents @i{name}'s compilation semantics.
287: (') name>comp ;
288:
289: : [COMP'] ( compilation "name" -- ; run-time -- w xt ) \ gforth bracket-comp-tick
290: \g Compilation token @i{w xt} represents @i{name}'s compilation semantics.
291: COMP' swap POSTPONE Aliteral POSTPONE ALiteral ; immediate restrict
292:
293: : postpone, ( w xt -- ) \ gforth postpone-comma
294: \g Compile the compilation semantics represented by the
295: \g compilation token @i{w xt}.
296: dup ['] execute =
297: if
298: drop compile,
299: else
300: dup ['] compile, =
301: if
302: drop POSTPONE (compile) a,
303: else
304: swap POSTPONE aliteral compile,
305: then
306: then ;
307:
308: : POSTPONE ( "name" -- ) \ core
309: \g Compiles the compilation semantics of @i{name}.
310: COMP' postpone, ; immediate restrict
311:
312: \ \ recurse 17may93jaw
313:
314: : recurse ( compilation -- ; run-time ?? -- ?? ) \ core
315: \g Call the current definition.
316: lastxt compile, ; immediate restrict
317:
318: \ \ compiler loop
319:
320: : compiler ( c-addr u -- )
321: 2dup find-name dup
322: if ( c-addr u nt )
323: nip nip name>comp execute
324: else
325: drop
326: 2dup snumber? dup
327: IF
328: 0>
329: IF
330: swap postpone Literal
331: THEN
332: postpone Literal
333: 2drop
334: ELSE
335: drop compiler-notfound
336: THEN
337: then ;
338:
339: : [ ( -- ) \ core left-bracket
340: \G Enter interpretation state. Immediate word.
341: ['] interpreter IS parser state off ; immediate
342:
343: : ] ( -- ) \ core right-bracket
344: \G Enter compilation state.
345: ['] compiler IS parser state on ;
346:
347: \ \ Strings 22feb93py
348:
349: : ," ( "string"<"> -- ) [char] " parse
350: here over char+ allot place align ;
351:
352: : SLiteral ( Compilation c-addr1 u ; run-time -- c-addr2 u ) \ string
353: \G Compilation: compile the string specified by @i{c-addr1},
354: \G @i{u} into the current definition. Run-time: return
355: \G @i{c-addr2 u} describing the address and length of the
356: \G string.
357: postpone (S") here over char+ allot place align ;
358: immediate restrict
359:
360: \ \ abort" 22feb93py
361:
362: : abort" ( compilation 'ccc"' -- ; run-time f -- ) \ core,exception-ext abort-quote
363: \G If any bit of @i{f} is non-zero, perform the function of @code{-2 throw},
364: \G displaying the string @i{ccc} if there is no exception frame on the
365: \G exception stack.
366: postpone (abort") ," ; immediate restrict
367:
368: \ \ Header states 23feb93py
369:
370: : cset ( bmask c-addr -- )
371: tuck @ or swap ! ;
372:
373: : creset ( bmask c-addr -- )
374: tuck @ swap invert and swap ! ;
375:
376: : ctoggle ( bmask c-addr -- )
377: tuck @ xor swap ! ;
378:
379: : lastflags ( -- c-addr )
380: \ the address of the flags byte in the last header
381: \ aborts if the last defined word was headerless
382: last @ dup 0= abort" last word was headerless" cell+ ;
383:
384: : immediate ( -- ) \ core
385: \G Make the compilation semantics of a word be to @code{execute}
386: \G the execution semantics.
387: immediate-mask lastflags cset ;
388:
389: : restrict ( -- ) \ gforth
390: \G A synonym for @code{compile-only}
391: restrict-mask lastflags cset ;
392:
393: ' restrict alias compile-only ( -- ) \ gforth
394: \G Remove the interpretation semantics of a word.
395:
396: \ \ Create Variable User Constant 17mar93py
397:
398: : Alias ( xt "name" -- ) \ gforth
399: Header reveal
400: alias-mask lastflags creset
401: dup A, lastcfa ! ;
402:
403: doer? :dovar [IF]
404:
405: : Create ( "name" -- ) \ core
406: Header reveal dovar: cfa, ;
407: [ELSE]
408:
409: : Create ( "name" -- ) \ core
410: Header reveal here lastcfa ! 0 A, 0 , DOES> ;
411: [THEN]
412:
413: : Variable ( "name" -- ) \ core
414: Create 0 , ;
415:
416: : AVariable ( "name" -- ) \ gforth
417: Create 0 A, ;
418:
419: : 2Variable ( "name" -- ) \ double two-variable
420: create 0 , 0 , ;
421:
422: : uallot ( n -- ) \ gforth
423: udp @ swap udp +! ;
424:
425: doer? :douser [IF]
426:
427: : User ( "name" -- ) \ gforth
428: Header reveal douser: cfa, cell uallot , ;
429:
430: : AUser ( "name" -- ) \ gforth
431: User ;
432: [ELSE]
433:
434: : User Create cell uallot , DOES> @ up @ + ;
435:
436: : AUser User ;
437: [THEN]
438:
439: doer? :docon [IF]
440: : (Constant) Header reveal docon: cfa, ;
441: [ELSE]
442: : (Constant) Create DOES> @ ;
443: [THEN]
444:
445: : Constant ( w "name" -- ) \ core
446: \G Define a constant @i{name} with value @i{w}.
447: \G
448: \G @i{name} execution: @i{-- w}
449: (Constant) , ;
450:
451: : AConstant ( addr "name" -- ) \ gforth
452: (Constant) A, ;
453:
454: : Value ( w "name" -- ) \ core-ext
455: (Constant) , ;
456:
457: : AValue ( w "name" -- ) \ core-ext
458: (Constant) A, ;
459:
460: : 2Constant ( w1 w2 "name" -- ) \ double two-constant
461: Create ( w1 w2 "name" -- )
462: 2,
463: DOES> ( -- w1 w2 )
464: 2@ ;
465:
466: doer? :dofield [IF]
467: : (Field) Header reveal dofield: cfa, ;
468: [ELSE]
469: : (Field) Create DOES> @ + ;
470: [THEN]
471:
472: \ \ interpret/compile:
473:
474: struct
475: >body
476: cell% field interpret/compile-int
477: cell% field interpret/compile-comp
478: end-struct interpret/compile-struct
479:
480: : interpret/compile: ( interp-xt comp-xt "name" -- ) \ gforth
481: Create immediate swap A, A,
482: DOES>
483: abort" executed primary cfa of an interpret/compile: word" ;
484: \ state @ IF cell+ THEN perform ;
485:
486: \ IS Defer What's Defers TO 24feb93py
487:
488: doer? :dodefer [IF]
489:
490: : Defer ( "name" -- ) \ gforth
491: \ !! shouldn't it be initialized with abort or something similar?
492: Header Reveal dodefer: cfa,
493: ['] noop A, ;
494:
495: [ELSE]
496:
497: : Defer ( "name" -- ) \ gforth
498: Create ['] noop A,
499: DOES> @ execute ;
500:
501: [THEN]
502:
503: : Defers ( compilation "name" -- ; run-time ... -- ... ) \ gforth
504: \G Compiles the present contents of the deferred word @i{name}
505: \G into the current definition. I.e., this produces static
506: \G binding as if @i{name} was not deferred.
507: ' >body @ compile, ; immediate
508:
509: :noname
510: dodoes, here !does ]
511: defstart :-hook ;
512: :noname
513: ;-hook ?struc
514: [ has? xconds [IF] ] exit-like [ [THEN] ]
515: postpone (does>) dodoes,
516: defstart :-hook ;
517: interpret/compile: DOES> ( compilation colon-sys1 -- colon-sys2 ; run-time nest-sys -- ) \ core does
518:
519: : <IS> ( "name" xt -- ) \ gforth
520: \g Changes the @code{defer}red word @var{name} to execute @var{xt}.
521: ' >body ! ;
522:
523: : [IS] ( compilation "name" -- ; run-time xt -- ) \ gforth bracket-is
524: \g At run-time, changes the @code{defer}red word @var{name} to
525: \g execute @var{xt}.
526: ' >body postpone ALiteral postpone ! ; immediate restrict
527:
528: ' <IS>
529: ' [IS]
530: interpret/compile: IS ( xt "name" -- ) \ gforth
531: \G A combined word made up from @code{<IS>} and @code{[IS]}.
532:
533: ' <IS>
534: ' [IS]
535: interpret/compile: TO ( w "name" -- ) \ core-ext
536:
537: :noname ' >body @ ;
538: :noname ' >body postpone ALiteral postpone @ ;
539: interpret/compile: What's ( interpretation "name" -- xt; compilation "name" -- ; run-time -- xt ) \ gforth
540: \G @i{Xt} is the XT that is currently assigned to @i{name}.
541:
542: : interpret/compile? ( xt -- flag )
543: >does-code ['] DOES> >does-code = ;
544:
545: \ \ : ; 24feb93py
546:
547: defer :-hook ( sys1 -- sys2 )
548:
549: defer ;-hook ( sys2 -- sys1 )
550:
551: 0 Constant defstart
552:
553: [IFDEF] docol,
554: : (:noname) ( -- colon-sys )
555: \ common factor of : and :noname
556: docol, ]comp
557: [ELSE]
558: : (:noname) ( -- colon-sys )
559: \ common factor of : and :noname
560: docol: cfa,
561: [THEN]
562: 0 last-compiled ! defstart ] :-hook ;
563:
564: : : ( "name" -- colon-sys ) \ core colon
565: Header (:noname) ;
566:
567: : :noname ( -- xt colon-sys ) \ core-ext colon-no-name
568: 0 last !
569: cfalign here (:noname) ;
570:
571: [IFDEF] fini,
572: : ; ( compilation colon-sys -- ; run-time nest-sys ) \ core semicolon
573: ;-hook ?struc fini, comp[ reveal postpone [ ; immediate restrict
574: [ELSE]
575: : ; ( compilation colon-sys -- ; run-time nest-sys ) \ core semicolon
576: ;-hook ?struc [compile] exit reveal postpone [ ; immediate restrict
577: [THEN]
578:
579: \ \ Search list handling: reveal words, recursive 23feb93py
580:
581: : last? ( -- false / nfa nfa )
582: last @ ?dup ;
583:
584: : (reveal) ( nt wid -- )
585: wordlist-id dup >r
586: @ over ( name>link ) !
587: r> ! ;
588:
589: \ make entry in wordlist-map
590: ' (reveal) f83search reveal-method !
591:
592: Variable warnings ( -- addr ) \ gforth
593: G -1 warnings T !
594:
595: : check-shadow ( addr count wid -- )
596: \G prints a warning if the string is already present in the wordlist
597: >r 2dup 2dup r> (search-wordlist) warnings @ and ?dup if
598: >stderr
599: ." redefined " name>string 2dup type
600: compare 0<> if
601: ." with " type
602: else
603: 2drop
604: then
605: space space EXIT
606: then
607: 2drop 2drop ;
608:
609: : reveal ( -- ) \ gforth
610: last?
611: if \ the last word has a header
612: dup ( name>link ) @ 1 and
613: if \ it is still hidden
614: dup ( name>link ) @ 1 xor ( nt wid )
615: 2dup >r name>string r> check-shadow ( nt wid )
616: dup wordlist-map @ reveal-method perform
617: else
618: drop
619: then
620: then ;
621:
622: : rehash ( wid -- )
623: dup wordlist-map @ rehash-method perform ;
624:
625: ' reveal alias recursive ( compilation -- ; run-time -- ) \ gforth
626: \g Make the current definition visible, enabling it to call itself
627: \g recursively.
628: immediate restrict
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>