1: \ compiler definitions 14sep97jaw
2:
3: \ Copyright (C) 1995,1996,1997,1998,2000,2003,2004,2005,2006,2007,2008,2009,2010,2011 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 3
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, see http://www.gnu.org/licenses/.
19:
20: \ \ Revisions-Log
21:
22: \ put in seperate file 14sep97jaw
23:
24: \ \ here allot , c, A, 17dec92py
25:
26: [IFUNDEF] allot
27: [IFUNDEF] forthstart
28: : allot ( n -- ) \ core
29: dup unused u> -8 and throw
30: dp +! ;
31: [THEN]
32: [THEN]
33:
34: \ we default to this version if we have nothing else 05May99jaw
35: [IFUNDEF] allot
36: : allot ( n -- ) \ core
37: \G Reserve @i{n} address units of data space without
38: \G initialization. @i{n} is a signed number, passing a negative
39: \G @i{n} releases memory. In ANS Forth you can only deallocate
40: \G memory from the current contiguous region in this way. In
41: \G Gforth you can deallocate anything in this way but named words.
42: \G The system does not check this restriction.
43: here +
44: dup 1- usable-dictionary-end forthstart within -8 and throw
45: dp ! ;
46: [THEN]
47:
48: : c, ( c -- ) \ core c-comma
49: \G Reserve data space for one char and store @i{c} in the space.
50: here 1 chars allot c! ;
51:
52: : , ( w -- ) \ core comma
53: \G Reserve data space for one cell and store @i{w} in the space.
54: here cell allot ! ;
55:
56: : 2, ( w1 w2 -- ) \ gforth
57: \G Reserve data space for two cells and store the double @i{w1
58: \G w2} there, @i{w2} first (lower address).
59: here 2 cells allot 2! ;
60:
61: \ : aligned ( addr -- addr' ) \ core
62: \ [ cell 1- ] Literal + [ -1 cells ] Literal and ;
63:
64: : align ( -- ) \ core
65: \G If the data-space pointer is not aligned, reserve enough space to align it.
66: here dup aligned swap ?DO bl c, LOOP ;
67:
68: \ : faligned ( addr -- f-addr ) \ float f-aligned
69: \ [ 1 floats 1- ] Literal + [ -1 floats ] Literal and ;
70:
71: : falign ( -- ) \ float f-align
72: \G If the data-space pointer is not float-aligned, reserve
73: \G enough space to align it.
74: here dup faligned swap
75: ?DO
76: bl c,
77: LOOP ;
78:
79: : maxalign ( -- ) \ gforth
80: \G Align data-space pointer for all alignment requirements.
81: here dup maxaligned swap
82: ?DO
83: bl c,
84: LOOP ;
85:
86: \ the code field is aligned if its body is maxaligned
87: ' maxalign Alias cfalign ( -- ) \ gforth
88: \G Align data-space pointer for code field requirements (i.e., such
89: \G that the corresponding body is maxaligned).
90:
91: ' , alias A, ( addr -- ) \ gforth
92:
93: ' NOOP ALIAS const
94:
95: \ \ Header 23feb93py
96:
97: \ input-stream, nextname and noname are quite ugly (passing
98: \ information through global variables), but they are useful for dealing
99: \ with existing/independent defining words
100:
101: : string, ( c-addr u -- ) \ gforth
102: \G puts down string as cstring
103: dup alias-mask or c,
104: here swap chars dup allot move ;
105:
106: : longstring, ( c-addr u -- ) \ gforth
107: \G puts down string as longcstring
108: dup , here swap chars dup allot move ;
109:
110: [IFDEF] prelude-mask
111: variable next-prelude
112:
113: : prelude, ( -- )
114: next-prelude @ if
115: align next-prelude @ ,
116: then ;
117: [THEN]
118:
119: : header, ( c-addr u -- ) \ gforth
120: name-too-long?
121: dup max-name-length @ max max-name-length !
122: [ [IFDEF] prelude-mask ] prelude, [ [THEN] ]
123: align here last !
124: current @ 1 or A, \ link field; before revealing, it contains the
125: \ tagged reveal-into wordlist
126: longstring, alias-mask lastflags cset
127: next-prelude @ 0<> prelude-mask and lastflags cset
128: next-prelude off
129: cfalign ;
130:
131: defer (header)
132: defer header ( -- ) \ gforth
133: ' (header) IS header
134:
135: : input-stream-header ( "name" -- )
136: parse-name name-too-short? header, ;
137:
138: : input-stream ( -- ) \ general
139: \G switches back to getting the name from the input stream ;
140: ['] input-stream-header IS (header) ;
141:
142: ' input-stream-header IS (header)
143:
144: 2variable nextname-string
145:
146: : nextname-header ( -- )
147: nextname-string 2@ header,
148: nextname-string free-mem-var
149: input-stream ;
150:
151: \ the next name is given in the string
152:
153: : nextname ( c-addr u -- ) \ gforth
154: \g The next defined word will have the name @var{c-addr u}; the
155: \g defining word will leave the input stream alone.
156: name-too-long?
157: nextname-string free-mem-var
158: save-mem nextname-string 2!
159: ['] nextname-header IS (header) ;
160:
161: : noname-header ( -- )
162: 0 last ! cfalign
163: input-stream ;
164:
165: : noname ( -- ) \ gforth
166: \g The next defined word will be anonymous. The defining word will
167: \g leave the input stream alone. The xt of the defined word will
168: \g be given by @code{latestxt}.
169: ['] noname-header IS (header) ;
170:
171: : latestxt ( -- xt ) \ gforth
172: \G @i{xt} is the execution token of the last word defined.
173: \ The main purpose of this word is to get the xt of words defined using noname
174: lastcfa @ ;
175:
176: ' latestxt alias lastxt \ gforth-obsolete
177: \G old name for @code{latestxt}.
178:
179: : latest ( -- nt ) \ gforth
180: \G @var{nt} is the name token of the last word defined; it is 0 if the
181: \G last word has no name.
182: last @ ;
183:
184: \ \ literals 17dec92py
185:
186: : Literal ( compilation n -- ; run-time -- n ) \ core
187: \G Compilation semantics: compile the run-time semantics.@*
188: \G Run-time Semantics: push @i{n}.@*
189: \G Interpretation semantics: undefined.
190: postpone lit , ; immediate restrict
191:
192: : 2Literal ( compilation w1 w2 -- ; run-time -- w1 w2 ) \ double two-literal
193: \G Compile appropriate code such that, at run-time, @i{w1 w2} are
194: \G placed on the stack. Interpretation semantics are undefined.
195: swap postpone Literal postpone Literal ; immediate restrict
196:
197: : ALiteral ( compilation addr -- ; run-time -- addr ) \ gforth
198: postpone lit A, ; 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: parse-name 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: has? ec 0= [IF]
218: ' noop Alias recurse
219: \g Call the current definition.
220: unlock tlastcfa @ lock AConstant lastcfa
221: \ this is the alias pointer in the recurse header, named lastcfa.
222: \ changing lastcfa now changes where recurse aliases to
223: \ it's always an alias of the current definition
224: \ it won't work in a flash/rom environment, therefore for Gforth EC
225: \ we stick to the traditional implementation
226: [ELSE]
227: : recurse ( compilation -- ; run-time ?? -- ?? ) \ core
228: \g Call the current definition.
229: latestxt compile, ; immediate restrict
230: [THEN]
231:
232: : cfa, ( code-address -- ) \ gforth cfa-comma
233: here
234: dup lastcfa !
235: 0 A, 0 ,
236: code-address! ;
237:
238: [IFUNDEF] compile,
239: defer compile, ( xt -- ) \ core-ext compile-comma
240: \G Compile the word represented by the execution token @i{xt}
241: \G into the current definition.
242:
243: ' , is compile,
244: [THEN]
245:
246: defer basic-block-end ( -- )
247:
248: :noname ( -- )
249: 0 compile-prim1 ;
250: is basic-block-end
251:
252: has? primcentric [IF]
253: has? peephole [IF]
254: \ dynamic only
255: : peephole-compile, ( xt -- )
256: \ compile xt, appending its code to the current dynamic superinstruction
257: here swap , compile-prim1 ;
258: [ELSE]
259: : peephole-compile, ( xt -- addr ) @ , ;
260: [THEN]
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: ['] does-exec peephole-compile, , EXIT
268: \ dup >body POSTPONE literal ['] call peephole-compile, >does-code , EXIT
269: then
270: dup >code-address CASE
271: dovalue: OF >body ['] lit@ peephole-compile, , EXIT ENDOF
272: docon: OF >body @ ['] lit peephole-compile, , EXIT ENDOF
273: \ docon: OF >body POSTPONE literal ['] @ peephole-compile, EXIT ENDOF
274: \ docon is also used by VALUEs, so don't @ at compile time
275: docol: OF >body ['] call peephole-compile, , EXIT ENDOF
276: dovar: OF >body ['] lit peephole-compile, , EXIT ENDOF
277: douser: OF >body @ ['] useraddr peephole-compile, , EXIT ENDOF
278: dodefer: OF >body ['] lit-perform peephole-compile, , EXIT ENDOF
279: dofield: OF >body @ ['] lit+ peephole-compile, , EXIT ENDOF
280: \ dofield: OF >body @ POSTPONE literal ['] + peephole-compile, EXIT ENDOF
281: doabicode: OF >body ['] abi-call peephole-compile, , EXIT ENDOF
282: do;abicode: OF ['] ;abi-code-exec peephole-compile, , EXIT ENDOF
283: \ code words and ;code-defined words (code words could be
284: \ optimized, if we could identify them):
285: dup in-dictionary? IF ( xt code-address )
286: over >body = if ( xt )
287: \ definitely a CODE word
288: peephole-compile, EXIT THEN
289: \ not sure, might be a ;CODE word
290: ['] lit-execute peephole-compile, , EXIT
291: \ drop POSTPONE literal ['] execute peephole-compile, EXIT
292: THEN
293: ENDCASE
294: peephole-compile, ;
295:
296: ' compile-to-prims, IS compile,
297: [ELSE]
298: ' , is compile,
299: [THEN]
300:
301: : !does ( addr -- ) \ gforth store-does
302: latestxt does-code! ;
303:
304: : (compile) ( -- ) \ gforth-obsolete: dummy
305: true abort" (compile) doesn't work, use POSTPONE instead" ;
306:
307: \ \ ticks
308:
309: : name>comp ( nt -- w xt ) \ gforth name-to-comp
310: \G @i{w xt} is the compilation token for the word @i{nt}.
311: (name>comp)
312: 1 = if
313: ['] execute
314: else
315: ['] compile,
316: then ;
317:
318: : [(')] ( compilation "name" -- ; run-time -- nt ) \ gforth bracket-paren-tick
319: (') postpone ALiteral ; immediate restrict
320:
321: : ['] ( compilation. "name" -- ; run-time. -- xt ) \ core bracket-tick
322: \g @i{xt} represents @i{name}'s interpretation
323: \g semantics. Perform @code{-14 throw} if the word has no
324: \g interpretation semantics.
325: ' postpone ALiteral ; immediate restrict
326:
327: : COMP' ( "name" -- w xt ) \ gforth comp-tick
328: \g Compilation token @i{w xt} represents @i{name}'s compilation semantics.
329: (') name>comp ;
330:
331: : [COMP'] ( compilation "name" -- ; run-time -- w xt ) \ gforth bracket-comp-tick
332: \g Compilation token @i{w xt} represents @i{name}'s compilation semantics.
333: COMP' swap POSTPONE Aliteral POSTPONE ALiteral ; immediate restrict
334:
335: : postpone, ( w xt -- ) \ gforth postpone-comma
336: \g Compile the compilation semantics represented by the
337: \g compilation token @i{w xt}.
338: dup ['] execute =
339: if
340: drop compile,
341: else
342: swap POSTPONE aliteral compile,
343: then ;
344:
345: has? recognizer [IF]
346: include ./recognizer.fs
347: [ELSE]
348: : POSTPONE ( "name" -- ) \ core
349: \g Compiles the compilation semantics of @i{name}.
350: COMP' postpone, ; immediate
351: [THEN]
352:
353: \ \ compiler loop
354:
355: has? recognizer 0= [IF]
356: : compiler1 ( c-addr u -- ... xt )
357: 2dup find-name [ [IFDEF] prelude-mask ] run-prelude [ [THEN] ] dup
358: if ( c-addr u nt )
359: nip nip name>comp
360: else
361: drop
362: 2dup 2>r snumber? dup
363: IF
364: 0>
365: IF
366: ['] 2literal
367: ELSE
368: ['] literal
369: THEN
370: 2rdrop
371: ELSE
372: drop 2r> compiler-notfound1
373: THEN
374: then ;
375:
376: : [ ( -- ) \ core left-bracket
377: \G Enter interpretation state. Immediate word.
378: ['] interpreter1 IS parser1 state off ; immediate
379:
380: : ] ( -- ) \ core right-bracket
381: \G Enter compilation state.
382: ['] compiler1 IS parser1 state on ;
383: [THEN]
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: \ problematic only for big endian machines
401:
402: : cset ( bmask c-addr -- )
403: tuck @ or swap ! ;
404:
405: : creset ( bmask c-addr -- )
406: tuck @ swap invert and swap ! ;
407:
408: : ctoggle ( bmask c-addr -- )
409: tuck @ xor swap ! ;
410:
411: : lastflags ( -- c-addr )
412: \ the address of the flags byte in the last header
413: \ aborts if the last defined word was headerless
414: latest dup 0= abort" last word was headerless" cell+ ;
415:
416: : immediate ( -- ) \ core
417: \G Make the compilation semantics of a word be to @code{execute}
418: \G the execution semantics.
419: immediate-mask lastflags cset ;
420:
421: : restrict ( -- ) \ gforth
422: \G A synonym for @code{compile-only}
423: restrict-mask lastflags cset ;
424:
425: ' restrict alias compile-only ( -- ) \ gforth
426: \G Remove the interpretation semantics of a word.
427:
428: \ \ Create Variable User Constant 17mar93py
429:
430: : Alias ( xt "name" -- ) \ gforth
431: Header reveal
432: alias-mask lastflags creset
433: dup A, lastcfa ! ;
434:
435: : Create ( "name" -- ) \ core
436: Header reveal dovar: cfa, ;
437:
438: : buffer: ( u "name" -- ) \ core ext
439: Create allot ;
440:
441: : Variable ( "name" -- ) \ core
442: Create 0 , ;
443:
444: : AVariable ( "name" -- ) \ gforth
445: Create 0 A, ;
446:
447: : 2Variable ( "name" -- ) \ double two-variable
448: Create 0 , 0 , ;
449:
450: : uallot ( n -- ) \ gforth
451: udp @ swap udp +! ;
452:
453: : User ( "name" -- ) \ gforth
454: Header reveal douser: cfa, cell uallot , ;
455:
456: : AUser ( "name" -- ) \ gforth
457: User ;
458:
459: : (Constant) Header reveal docon: cfa, ;
460:
461: : (Value) Header reveal dovalue: cfa, ;
462:
463: : Constant ( w "name" -- ) \ core
464: \G Define a constant @i{name} with value @i{w}.
465: \G
466: \G @i{name} execution: @i{-- w}
467: (Constant) , ;
468:
469: : AConstant ( addr "name" -- ) \ gforth
470: (Constant) A, ;
471:
472: : Value ( w "name" -- ) \ core-ext
473: (Value) , ;
474:
475: : AValue ( w "name" -- ) \ core-ext
476: (Value) A, ;
477:
478: : 2Constant ( w1 w2 "name" -- ) \ double two-constant
479: Create ( w1 w2 "name" -- )
480: 2,
481: DOES> ( -- w1 w2 )
482: 2@ ;
483:
484: : (Field) Header reveal dofield: cfa, ;
485:
486: \ \ interpret/compile:
487:
488: struct
489: >body
490: cell% field interpret/compile-int
491: cell% field interpret/compile-comp
492: end-struct interpret/compile-struct
493:
494: : interpret/compile: ( interp-xt comp-xt "name" -- ) \ gforth
495: Create immediate swap A, A,
496: DOES>
497: abort" executed primary cfa of an interpret/compile: word" ;
498: \ state @ IF cell+ THEN perform ;
499:
500: \ IS Defer What's Defers TO 24feb93py
501:
502: defer defer-default ( -- )
503: ' abort is defer-default
504: \ default action for deferred words (overridden by a warning later)
505:
506: : Defer ( "name" -- ) \ gforth
507: \G Define a deferred word @i{name}; its execution semantics can be
508: \G set with @code{defer!} or @code{is} (and they have to, before first
509: \G executing @i{name}.
510: Header Reveal dodefer: cfa,
511: ['] defer-default A, ;
512:
513: : defer@ ( xt-deferred -- xt ) \ gforth defer-fetch
514: \G @i{xt} represents the word currently associated with the deferred
515: \G word @i{xt-deferred}.
516: >body @ ;
517:
518: : Defers ( compilation "name" -- ; run-time ... -- ... ) \ gforth
519: \G Compiles the present contents of the deferred word @i{name}
520: \G into the current definition. I.e., this produces static
521: \G binding as if @i{name} was not deferred.
522: ' defer@ compile, ; immediate
523:
524: : does>-like ( xt -- )
525: \ xt ( addr -- ) is !does or !;abi-code etc, addr is the address
526: \ that should be stored right after the code address.
527: >r ;-hook ?struc
528: exit-like
529: here [ has? peephole [IF] ] 5 [ [ELSE] ] 4 [ [THEN] ] cells +
530: postpone aliteral r> compile, [compile] exit
531: [ has? peephole [IF] ] finish-code [ [THEN] ]
532: defstart ;
533:
534: :noname
535: here !does ]
536: defstart :-hook ;
537: :noname
538: ['] !does does>-like :-hook ;
539: interpret/compile: DOES> ( compilation colon-sys1 -- colon-sys2 ; run-time nest-sys -- ) \ core does
540:
541: : defer! ( xt xt-deferred -- ) \ gforth defer-store
542: \G Changes the @code{defer}red word @var{xt-deferred} to execute @var{xt}.
543: >body ! ;
544:
545: : <IS> ( "name" xt -- ) \ gforth
546: \g Changes the @code{defer}red word @var{name} to execute @var{xt}.
547: ' defer! ;
548:
549: : [IS] ( compilation "name" -- ; run-time xt -- ) \ gforth bracket-is
550: \g At run-time, changes the @code{defer}red word @var{name} to
551: \g execute @var{xt}.
552: ' postpone ALiteral postpone defer! ; immediate restrict
553:
554: ' <IS>
555: ' [IS]
556: interpret/compile: IS ( compilation/interpretation "name-deferred" -- ; run-time xt -- ) \ gforth
557: \G Changes the @code{defer}red word @var{name} to execute @var{xt}.
558: \G Its compilation semantics parses at compile time.
559:
560: ' <IS>
561: ' [IS]
562: interpret/compile: TO ( w "name" -- ) \ core-ext
563:
564: : interpret/compile? ( xt -- flag )
565: >does-code ['] DOES> >does-code = ;
566:
567: \ \ : ; 24feb93py
568:
569: defer :-hook ( sys1 -- sys2 )
570:
571: defer ;-hook ( sys2 -- sys1 )
572:
573: 0 Constant defstart
574:
575: : (:noname) ( -- colon-sys )
576: \ common factor of : and :noname
577: docol: cfa,
578: defstart ] :-hook ;
579:
580: : : ( "name" -- colon-sys ) \ core colon
581: Header (:noname) ;
582:
583: : :noname ( -- xt colon-sys ) \ core-ext colon-no-name
584: 0 last !
585: cfalign here (:noname) ;
586:
587: : ; ( compilation colon-sys -- ; run-time nest-sys ) \ core semicolon
588: ;-hook ?struc [compile] exit
589: [ has? peephole [IF] ] finish-code [ [THEN] ]
590: reveal postpone [ ; immediate restrict
591:
592: \ \ Search list handling: reveal words, recursive 23feb93py
593:
594: : last? ( -- false / nfa nfa )
595: latest ?dup ;
596:
597: Variable warnings ( -- addr ) \ gforth
598: G -1 warnings T !
599:
600: : (reveal) ( nt wid -- )
601: wordlist-id dup >r
602: @ over ( name>link ) !
603: r> ! ;
604:
605: \ make entry in wordlist-map
606: ' (reveal) f83search reveal-method !
607:
608: : check-shadow ( addr count wid -- )
609: \G prints a warning if the string is already present in the wordlist
610: >r 2dup 2dup r> (search-wordlist) warnings @ and ?dup if
611: >stderr
612: ." redefined " name>string 2dup type
613: str= 0= if
614: ." with " type
615: else
616: 2drop
617: then
618: space space EXIT
619: then
620: 2drop 2drop ;
621:
622: : reveal ( -- ) \ gforth
623: last?
624: if \ the last word has a header
625: dup ( name>link ) @ 1 and
626: if \ it is still hidden
627: dup ( name>link ) @ 1 xor ( nt wid )
628: 2dup >r name>string r> check-shadow ( nt wid )
629: dup wordlist-map @ reveal-method perform
630: else
631: drop
632: then
633: then ;
634:
635: : rehash ( wid -- )
636: dup wordlist-map @ rehash-method perform ;
637:
638: ' reveal alias recursive ( compilation -- ; run-time -- ) \ gforth
639: \g Make the current definition visible, enabling it to call itself
640: \g recursively.
641: immediate restrict
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>