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