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