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