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