[gforth] / gforth / kernel / comp.fs  

gforth: gforth/kernel/comp.fs


1 : pazsan 1.1 \ compiler definitions 14sep97jaw
2 :    
3 : anton 1.106 \ Copyright (C) 1995,1996,1997,1998,2000,2003,2004,2005,2006,2007,2008,2009,2010 Free Software Foundation, Inc.
4 : anton 1.6
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 : anton 1.93 \ as published by the Free Software Foundation, either version 3
10 : anton 1.6 \ 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 : anton 1.93 \ along with this program. If not, see http://www.gnu.org/licenses/.
19 : anton 1.6
20 : pazsan 1.1 \ \ Revisions-Log
21 :    
22 :     \ put in seperate file 14sep97jaw
23 :    
24 :     \ \ here allot , c, A, 17dec92py
25 :    
26 : jwilke 1.14 [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 : pazsan 1.1 : allot ( n -- ) \ core
37 : anton 1.23 \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 : anton 1.12 here +
44 :     dup 1- usable-dictionary-end forthstart within -8 and throw
45 :     dp ! ;
46 : jwilke 1.14 [THEN]
47 : pazsan 1.1
48 : crook 1.22 : c, ( c -- ) \ core c-comma
49 : crook 1.15 \G Reserve data space for one char and store @i{c} in the space.
50 : pazsan 1.110 here 1 chars allot c! ;
51 : pazsan 1.1
52 : crook 1.22 : , ( w -- ) \ core comma
53 : crook 1.15 \G Reserve data space for one cell and store @i{w} in the space.
54 : pazsan 1.110 here cell allot ! ;
55 : pazsan 1.1
56 :     : 2, ( w1 w2 -- ) \ gforth
57 : crook 1.15 \G Reserve data space for two cells and store the double @i{w1
58 : anton 1.24 \G w2} there, @i{w2} first (lower address).
59 : pazsan 1.110 here 2 cells allot 2! ;
60 : pazsan 1.1
61 :     \ : aligned ( addr -- addr' ) \ core
62 :     \ [ cell 1- ] Literal + [ -1 cells ] Literal and ;
63 :    
64 :     : align ( -- ) \ core
65 : crook 1.15 \G If the data-space pointer is not aligned, reserve enough space to align it.
66 : pazsan 1.1 here dup aligned swap ?DO bl c, LOOP ;
67 :    
68 : crook 1.22 \ : faligned ( addr -- f-addr ) \ float f-aligned
69 : pazsan 1.1 \ [ 1 floats 1- ] Literal + [ -1 floats ] Literal and ;
70 :    
71 : crook 1.22 : falign ( -- ) \ float f-align
72 : crook 1.15 \G If the data-space pointer is not float-aligned, reserve
73 :     \G enough space to align it.
74 : pazsan 1.1 here dup faligned swap
75 :     ?DO
76 :     bl c,
77 :     LOOP ;
78 :    
79 : anton 1.9 : maxalign ( -- ) \ gforth
80 : anton 1.23 \G Align data-space pointer for all alignment requirements.
81 : pazsan 1.1 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 : anton 1.24 \G Align data-space pointer for code field requirements (i.e., such
89 :     \G that the corresponding body is maxaligned).
90 : pazsan 1.1
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 : pazsan 1.110 dup alias-mask or c,
104 :     here swap chars dup allot move ;
105 : pazsan 1.1
106 : anton 1.30 : longstring, ( c-addr u -- ) \ gforth
107 : anton 1.55 \G puts down string as longcstring
108 : anton 1.30 dup , here swap chars dup allot move ;
109 :    
110 : pazsan 1.99 [IFDEF] prelude-mask
111 : anton 1.96 variable next-prelude
112 :    
113 :     : prelude, ( -- )
114 :     next-prelude @ if
115 :     align next-prelude @ ,
116 :     then ;
117 : pazsan 1.99 [THEN]
118 : anton 1.96
119 : pazsan 1.1 : header, ( c-addr u -- ) \ gforth
120 :     name-too-long?
121 : anton 1.53 dup max-name-length @ max max-name-length !
122 : pazsan 1.99 [ [IFDEF] prelude-mask ] prelude, [ [THEN] ]
123 : pazsan 1.1 align here last !
124 :     current @ 1 or A, \ link field; before revealing, it contains the
125 :     \ tagged reveal-into wordlist
126 : pazsan 1.110 longstring, alias-mask lastflags cset
127 :     next-prelude @ 0<> prelude-mask and lastflags cset
128 :     next-prelude off
129 : pazsan 1.83 cfalign ;
130 : pazsan 1.1
131 : pazsan 1.107 defer (header)
132 :     defer header ( -- ) \ gforth
133 :     ' (header) IS header
134 :    
135 : pazsan 1.1 : input-stream-header ( "name" -- )
136 : pazsan 1.80 parse-name name-too-short? header, ;
137 : pazsan 1.1
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 : anton 1.32 2variable nextname-string
145 : pazsan 1.1
146 :     : nextname-header ( -- )
147 : anton 1.32 nextname-string 2@ header,
148 :     nextname-string free-mem-var
149 : pazsan 1.1 input-stream ;
150 :    
151 :     \ the next name is given in the string
152 :    
153 :     : nextname ( c-addr u -- ) \ gforth
154 : anton 1.19 \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 : pazsan 1.1 name-too-long?
157 : anton 1.32 nextname-string free-mem-var
158 :     save-mem nextname-string 2!
159 : pazsan 1.1 ['] nextname-header IS (header) ;
160 :    
161 :     : noname-header ( -- )
162 :     0 last ! cfalign
163 :     input-stream ;
164 :    
165 :     : noname ( -- ) \ gforth
166 : anton 1.19 \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 : anton 1.56 \g be given by @code{latestxt}.
169 : pazsan 1.1 ['] noname-header IS (header) ;
170 :    
171 : anton 1.56 : latestxt ( -- xt ) \ gforth
172 : crook 1.15 \G @i{xt} is the execution token of the last word defined.
173 : crook 1.13 \ The main purpose of this word is to get the xt of words defined using noname
174 : pazsan 1.1 lastcfa @ ;
175 :    
176 : anton 1.56 ' 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 : pazsan 1.1 \ \ literals 17dec92py
185 :    
186 :     : Literal ( compilation n -- ; run-time -- n ) \ core
187 : anton 1.27 \G Compilation semantics: compile the run-time semantics.@*
188 :     \G Run-time Semantics: push @i{n}.@*
189 :     \G Interpretation semantics: undefined.
190 : pazsan 1.110 postpone lit , ; immediate restrict
191 : pazsan 1.1
192 : anton 1.72 : 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 : pazsan 1.1 : ALiteral ( compilation addr -- ; run-time -- addr ) \ gforth
198 : pazsan 1.110 postpone lit A, ; immediate restrict
199 : pazsan 1.1
200 : pazsan 1.70 Defer char@ ( addr u -- char addr' u' )
201 :     :noname over c@ -rot 1 /string ; IS char@
202 :    
203 : crook 1.8 : char ( '<spaces>ccc' -- c ) \ core
204 : crook 1.15 \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 : pazsan 1.87 parse-name char@ 2drop ;
207 : pazsan 1.1
208 : crook 1.8 : [char] ( compilation '<spaces>ccc' -- ; run-time -- c ) \ core bracket-char
209 : crook 1.10 \G Compilation: skip leading spaces. Parse the string
210 : crook 1.15 \G @i{ccc}. Run-time: return @i{c}, the display code
211 :     \G representing the first character of @i{ccc}. Interpretation
212 : crook 1.10 \G semantics for this word are undefined.
213 : pazsan 1.1 char postpone Literal ; immediate restrict
214 :    
215 :     \ \ threading 17mar93py
216 :    
217 : pazsan 1.111 has? ec 0= [IF]
218 :     ' noop Alias recurse
219 :     \g Call the current definition.
220 :     unlock tlastcfa @ lock AConstant lastcfa
221 :     [ELSE]
222 :     : recurse ( compilation -- ; run-time ?? -- ?? ) \ core
223 :     \g Call the current definition.
224 :     latestxt compile, ; immediate restrict
225 :     [THEN]
226 :    
227 : pazsan 1.1 : cfa, ( code-address -- ) \ gforth cfa-comma
228 :     here
229 :     dup lastcfa !
230 : pazsan 1.110 0 A, 0 ,
231 : pazsan 1.84 code-address! ;
232 : pazsan 1.1
233 : jwilke 1.14 [IFUNDEF] compile,
234 : anton 1.34 defer compile, ( xt -- ) \ core-ext compile-comma
235 :     \G Compile the word represented by the execution token @i{xt}
236 :     \G into the current definition.
237 :    
238 :     ' , is compile,
239 : jwilke 1.14 [THEN]
240 : pazsan 1.1
241 : anton 1.40 defer basic-block-end ( -- )
242 :    
243 : anton 1.60 :noname ( -- )
244 :     0 compile-prim1 ;
245 :     is basic-block-end
246 : anton 1.40
247 : pazsan 1.99 has? primcentric [IF]
248 :     has? peephole [IF]
249 :     \ dynamic only
250 :     : peephole-compile, ( xt -- )
251 :     \ compile xt, appending its code to the current dynamic superinstruction
252 :     here swap , compile-prim1 ;
253 :     [ELSE]
254 :     : peephole-compile, ( xt -- addr ) @ , ;
255 :     [THEN]
256 : anton 1.40
257 : anton 1.35 : compile-to-prims, ( xt -- )
258 :     \G compile xt to use primitives (and their peephole optimization)
259 :     \G instead of ","-ing the xt.
260 :     \ !! all POSTPONEs here postpone primitives; this can be optimized
261 : pazsan 1.39 dup >does-code if
262 : anton 1.66 ['] does-exec peephole-compile, , EXIT
263 :     \ dup >body POSTPONE literal ['] call peephole-compile, >does-code , EXIT
264 : anton 1.35 then
265 :     dup >code-address CASE
266 : pazsan 1.91 dovalue: OF >body ['] lit@ peephole-compile, , EXIT ENDOF
267 :     docon: OF >body @ ['] lit peephole-compile, , EXIT ENDOF
268 : anton 1.66 \ docon: OF >body POSTPONE literal ['] @ peephole-compile, EXIT ENDOF
269 : anton 1.42 \ docon is also used by VALUEs, so don't @ at compile time
270 : anton 1.66 docol: OF >body ['] call peephole-compile, , EXIT ENDOF
271 :     dovar: OF >body ['] lit peephole-compile, , EXIT ENDOF
272 :     douser: OF >body @ ['] useraddr peephole-compile, , EXIT ENDOF
273 :     dodefer: OF >body ['] lit-perform peephole-compile, , EXIT ENDOF
274 :     dofield: OF >body @ ['] lit+ peephole-compile, , EXIT ENDOF
275 : anton 1.103 \ dofield: OF >body @ POSTPONE literal ['] + peephole-compile, EXIT ENDOF
276 : dvdkhlng 1.100 doabicode: OF >body ['] abi-call peephole-compile, , EXIT ENDOF
277 : anton 1.103 do;abicode: OF ['] ;abi-code-exec peephole-compile, , EXIT ENDOF
278 : anton 1.104 \ code words and ;code-defined words (code words could be
279 :     \ optimized, if we could identify them):
280 : anton 1.105 dup in-dictionary? IF ( xt code-address )
281 :     over >body = if ( xt )
282 :     \ definitely a CODE word
283 :     peephole-compile, EXIT THEN
284 :     \ not sure, might be a ;CODE word
285 :     ['] lit-execute peephole-compile, , EXIT
286 : anton 1.104 \ drop POSTPONE literal ['] execute peephole-compile, EXIT
287 :     THEN
288 : anton 1.35 ENDCASE
289 : anton 1.49 peephole-compile, ;
290 : anton 1.35
291 :     ' compile-to-prims, IS compile,
292 : pazsan 1.36 [ELSE]
293 :     ' , is compile,
294 :     [THEN]
295 : anton 1.34
296 : pazsan 1.1 : !does ( addr -- ) \ gforth store-does
297 : anton 1.56 latestxt does-code! ;
298 : pazsan 1.1
299 : pazsan 1.61 : (compile) ( -- ) \ gforth-obsolete: dummy
300 : anton 1.63 true abort" (compile) doesn't work, use POSTPONE instead" ;
301 : pazsan 1.1
302 :     \ \ ticks
303 :    
304 : anton 1.90 : name>comp ( nt -- w xt ) \ gforth name-to-comp
305 : crook 1.15 \G @i{w xt} is the compilation token for the word @i{nt}.
306 : pazsan 1.1 (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 : crook 1.15 \g @i{xt} represents @i{name}'s interpretation
318 :     \g semantics. Perform @code{-14 throw} if the word has no
319 : pazsan 1.1 \g interpretation semantics.
320 :     ' postpone ALiteral ; immediate restrict
321 :    
322 : anton 1.5 : COMP' ( "name" -- w xt ) \ gforth comp-tick
323 : crook 1.15 \g Compilation token @i{w xt} represents @i{name}'s compilation semantics.
324 : pazsan 1.1 (') name>comp ;
325 :    
326 :     : [COMP'] ( compilation "name" -- ; run-time -- w xt ) \ gforth bracket-comp-tick
327 : crook 1.15 \g Compilation token @i{w xt} represents @i{name}'s compilation semantics.
328 : pazsan 1.1 COMP' swap POSTPONE Aliteral POSTPONE ALiteral ; immediate restrict
329 :    
330 : jwilke 1.18 : postpone, ( w xt -- ) \ gforth postpone-comma
331 : anton 1.26 \g Compile the compilation semantics represented by the
332 :     \g compilation token @i{w xt}.
333 : jwilke 1.18 dup ['] execute =
334 :     if
335 :     drop compile,
336 :     else
337 : pazsan 1.61 swap POSTPONE aliteral compile,
338 : jwilke 1.18 then ;
339 :    
340 : pazsan 1.109 has? recognizer [IF]
341 :     include ./recognizer.fs
342 :     [ELSE]
343 : jwilke 1.18 : POSTPONE ( "name" -- ) \ core
344 :     \g Compiles the compilation semantics of @i{name}.
345 : anton 1.48 COMP' postpone, ; immediate
346 : pazsan 1.109 [THEN]
347 : jwilke 1.18
348 : pazsan 1.1 \ \ compiler loop
349 :    
350 : pazsan 1.109 has? recognizer 0= [IF]
351 : anton 1.72 : compiler1 ( c-addr u -- ... xt )
352 : pazsan 1.99 2dup find-name [ [IFDEF] prelude-mask ] run-prelude [ [THEN] ] dup
353 : pazsan 1.1 if ( c-addr u nt )
354 : anton 1.72 nip nip name>comp
355 : pazsan 1.1 else
356 :     drop
357 : anton 1.72 2dup 2>r snumber? dup
358 : pazsan 1.1 IF
359 :     0>
360 :     IF
361 : anton 1.72 ['] 2literal
362 :     ELSE
363 :     ['] literal
364 : pazsan 1.1 THEN
365 : anton 1.72 2rdrop
366 : pazsan 1.1 ELSE
367 : anton 1.72 drop 2r> compiler-notfound1
368 : pazsan 1.1 THEN
369 :     then ;
370 :    
371 : crook 1.22 : [ ( -- ) \ core left-bracket
372 : crook 1.8 \G Enter interpretation state. Immediate word.
373 : anton 1.72 ['] interpreter1 IS parser1 state off ; immediate
374 : pazsan 1.1
375 :     : ] ( -- ) \ core right-bracket
376 : crook 1.8 \G Enter compilation state.
377 : anton 1.72 ['] compiler1 IS parser1 state on ;
378 : pazsan 1.109 [THEN]
379 : pazsan 1.1
380 :     \ \ Strings 22feb93py
381 :    
382 : anton 1.44 : S, ( addr u -- )
383 : anton 1.45 \ allot string as counted string
384 : pazsan 1.110 here over char+ allot place align ;
385 : anton 1.45
386 :     : mem, ( addr u -- )
387 :     \ allot the memory block HERE (do alignment yourself)
388 : pazsan 1.110 here over allot swap move ;
389 : pazsan 1.1
390 : anton 1.44 : ," ( "string"<"> -- )
391 :     [char] " parse s, ;
392 : pazsan 1.1
393 :     \ \ Header states 23feb93py
394 :    
395 : pazsan 1.83 \ problematic only for big endian machines
396 :    
397 : pazsan 1.1 : cset ( bmask c-addr -- )
398 : anton 1.30 tuck @ or swap ! ;
399 : pazsan 1.1
400 :     : creset ( bmask c-addr -- )
401 : anton 1.30 tuck @ swap invert and swap ! ;
402 : pazsan 1.1
403 :     : ctoggle ( bmask c-addr -- )
404 : anton 1.30 tuck @ xor swap ! ;
405 : pazsan 1.1
406 :     : lastflags ( -- c-addr )
407 :     \ the address of the flags byte in the last header
408 :     \ aborts if the last defined word was headerless
409 : anton 1.56 latest dup 0= abort" last word was headerless" cell+ ;
410 : pazsan 1.1
411 :     : immediate ( -- ) \ core
412 : crook 1.10 \G Make the compilation semantics of a word be to @code{execute}
413 :     \G the execution semantics.
414 : pazsan 1.110 immediate-mask lastflags cset ;
415 : pazsan 1.1
416 :     : restrict ( -- ) \ gforth
417 : crook 1.10 \G A synonym for @code{compile-only}
418 : pazsan 1.110 restrict-mask lastflags cset ;
419 : jwilke 1.18
420 : pazsan 1.1 ' restrict alias compile-only ( -- ) \ gforth
421 : crook 1.10 \G Remove the interpretation semantics of a word.
422 : pazsan 1.1
423 :     \ \ Create Variable User Constant 17mar93py
424 :    
425 : crook 1.15 : Alias ( xt "name" -- ) \ gforth
426 : pazsan 1.1 Header reveal
427 :     alias-mask lastflags creset
428 :     dup A, lastcfa ! ;
429 :    
430 :     : Create ( "name" -- ) \ core
431 :     Header reveal dovar: cfa, ;
432 :    
433 : pazsan 1.108 : buffer: ( u "name" -- ) \ core ext
434 :     Create allot ;
435 :    
436 : pazsan 1.1 : Variable ( "name" -- ) \ core
437 :     Create 0 , ;
438 :    
439 :     : AVariable ( "name" -- ) \ gforth
440 :     Create 0 A, ;
441 :    
442 : crook 1.22 : 2Variable ( "name" -- ) \ double two-variable
443 : pazsan 1.85 Create 0 , 0 , ;
444 : pazsan 1.1
445 : crook 1.21 : uallot ( n -- ) \ gforth
446 :     udp @ swap udp +! ;
447 : pazsan 1.1
448 :     : User ( "name" -- ) \ gforth
449 :     Header reveal douser: cfa, cell uallot , ;
450 :    
451 :     : AUser ( "name" -- ) \ gforth
452 :     User ;
453 :    
454 : pazsan 1.110 : (Constant) Header reveal docon: cfa, ;
455 : pazsan 1.1
456 : pazsan 1.110 : (Value) Header reveal dovalue: cfa, ;
457 : pazsan 1.76
458 : pazsan 1.1 : Constant ( w "name" -- ) \ core
459 : crook 1.15 \G Define a constant @i{name} with value @i{w}.
460 : pazsan 1.1 \G
461 : crook 1.15 \G @i{name} execution: @i{-- w}
462 : pazsan 1.1 (Constant) , ;
463 :    
464 :     : AConstant ( addr "name" -- ) \ gforth
465 :     (Constant) A, ;
466 :    
467 :     : Value ( w "name" -- ) \ core-ext
468 : pazsan 1.76 (Value) , ;
469 : pazsan 1.1
470 : jwilke 1.37 : AValue ( w "name" -- ) \ core-ext
471 : pazsan 1.76 (Value) A, ;
472 : jwilke 1.37
473 : crook 1.22 : 2Constant ( w1 w2 "name" -- ) \ double two-constant
474 : pazsan 1.1 Create ( w1 w2 "name" -- )
475 :     2,
476 :     DOES> ( -- w1 w2 )
477 :     2@ ;
478 : pazsan 1.110
479 :     : (Field) Header reveal dofield: cfa, ;
480 : pazsan 1.39
481 :     \ \ interpret/compile:
482 :    
483 :     struct
484 :     >body
485 :     cell% field interpret/compile-int
486 :     cell% field interpret/compile-comp
487 :     end-struct interpret/compile-struct
488 :    
489 :     : interpret/compile: ( interp-xt comp-xt "name" -- ) \ gforth
490 :     Create immediate swap A, A,
491 :     DOES>
492 :     abort" executed primary cfa of an interpret/compile: word" ;
493 :     \ state @ IF cell+ THEN perform ;
494 :    
495 : pazsan 1.1 \ IS Defer What's Defers TO 24feb93py
496 :    
497 : anton 1.68 defer defer-default ( -- )
498 : anton 1.69 ' abort is defer-default
499 :     \ default action for deferred words (overridden by a warning later)
500 : anton 1.67
501 : pazsan 1.1 : Defer ( "name" -- ) \ gforth
502 : anton 1.67 \G Define a deferred word @i{name}; its execution semantics can be
503 :     \G set with @code{defer!} or @code{is} (and they have to, before first
504 :     \G executing @i{name}.
505 : pazsan 1.1 Header Reveal dodefer: cfa,
506 : pazsan 1.110 ['] defer-default A, ;
507 : pazsan 1.1
508 : anton 1.67 : defer@ ( xt-deferred -- xt ) \ gforth defer-fetch
509 :     \G @i{xt} represents the word currently associated with the deferred
510 :     \G word @i{xt-deferred}.
511 : pazsan 1.110 >body @ ;
512 : anton 1.67
513 : anton 1.25 : Defers ( compilation "name" -- ; run-time ... -- ... ) \ gforth
514 :     \G Compiles the present contents of the deferred word @i{name}
515 :     \G into the current definition. I.e., this produces static
516 :     \G binding as if @i{name} was not deferred.
517 : anton 1.67 ' defer@ compile, ; immediate
518 : jwilke 1.18
519 : anton 1.103 : does>-like ( xt -- )
520 :     \ xt ( addr -- ) is !does or !;abi-code etc, addr is the address
521 :     \ that should be stored right after the code address.
522 :     >r ;-hook ?struc
523 : pazsan 1.110 exit-like
524 : anton 1.103 here [ has? peephole [IF] ] 5 [ [ELSE] ] 4 [ [THEN] ] cells +
525 :     postpone aliteral r> compile, [compile] exit
526 :     [ has? peephole [IF] ] finish-code [ [THEN] ]
527 :     defstart ;
528 :    
529 : jwilke 1.18 :noname
530 : anton 1.101 here !does ]
531 : jwilke 1.18 defstart :-hook ;
532 :     :noname
533 : anton 1.103 ['] !does does>-like :-hook ;
534 : jwilke 1.18 interpret/compile: DOES> ( compilation colon-sys1 -- colon-sys2 ; run-time nest-sys -- ) \ core does
535 :    
536 : anton 1.73 : defer! ( xt xt-deferred -- ) \ gforth defer-store
537 : anton 1.67 \G Changes the @code{defer}red word @var{xt-deferred} to execute @var{xt}.
538 : pazsan 1.110 >body ! ;
539 : anton 1.67
540 : anton 1.20 : <IS> ( "name" xt -- ) \ gforth
541 :     \g Changes the @code{defer}red word @var{name} to execute @var{xt}.
542 : anton 1.67 ' defer! ;
543 : anton 1.20
544 :     : [IS] ( compilation "name" -- ; run-time xt -- ) \ gforth bracket-is
545 :     \g At run-time, changes the @code{defer}red word @var{name} to
546 :     \g execute @var{xt}.
547 : anton 1.67 ' postpone ALiteral postpone defer! ; immediate restrict
548 : jwilke 1.18
549 : anton 1.20 ' <IS>
550 : jwilke 1.18 ' [IS]
551 : anton 1.67 interpret/compile: IS ( compilation/interpretation "name-deferred" -- ; run-time xt -- ) \ gforth
552 :     \G Changes the @code{defer}red word @var{name} to execute @var{xt}.
553 :     \G Its compilation semantics parses at compile time.
554 : jwilke 1.18
555 : anton 1.20 ' <IS>
556 :     ' [IS]
557 :     interpret/compile: TO ( w "name" -- ) \ core-ext
558 : jwilke 1.18
559 :     : interpret/compile? ( xt -- flag )
560 :     >does-code ['] DOES> >does-code = ;
561 : pazsan 1.1
562 :     \ \ : ; 24feb93py
563 :    
564 :     defer :-hook ( sys1 -- sys2 )
565 :    
566 :     defer ;-hook ( sys2 -- sys1 )
567 :    
568 : jwilke 1.16 0 Constant defstart
569 :    
570 : anton 1.7 : (:noname) ( -- colon-sys )
571 :     \ common factor of : and :noname
572 : anton 1.34 docol: cfa,
573 : anton 1.40 defstart ] :-hook ;
574 : anton 1.7
575 : pazsan 1.1 : : ( "name" -- colon-sys ) \ core colon
576 : anton 1.7 Header (:noname) ;
577 :    
578 :     : :noname ( -- xt colon-sys ) \ core-ext colon-no-name
579 :     0 last !
580 :     cfalign here (:noname) ;
581 : pazsan 1.1
582 :     : ; ( compilation colon-sys -- ; run-time nest-sys ) \ core semicolon
583 : pazsan 1.57 ;-hook ?struc [compile] exit
584 :     [ has? peephole [IF] ] finish-code [ [THEN] ]
585 :     reveal postpone [ ; immediate restrict
586 : pazsan 1.1
587 :     \ \ Search list handling: reveal words, recursive 23feb93py
588 :    
589 :     : last? ( -- false / nfa nfa )
590 : anton 1.56 latest ?dup ;
591 : pazsan 1.1
592 : pazsan 1.83 Variable warnings ( -- addr ) \ gforth
593 :     G -1 warnings T !
594 :    
595 : pazsan 1.1 : (reveal) ( nt wid -- )
596 : pazsan 1.3 wordlist-id dup >r
597 : pazsan 1.1 @ over ( name>link ) !
598 :     r> ! ;
599 :    
600 :     \ make entry in wordlist-map
601 :     ' (reveal) f83search reveal-method !
602 :    
603 :     : check-shadow ( addr count wid -- )
604 : pazsan 1.2 \G prints a warning if the string is already present in the wordlist
605 :     >r 2dup 2dup r> (search-wordlist) warnings @ and ?dup if
606 :     >stderr
607 :     ." redefined " name>string 2dup type
608 : anton 1.43 str= 0= if
609 : pazsan 1.2 ." with " type
610 :     else
611 :     2drop
612 :     then
613 :     space space EXIT
614 :     then
615 :     2drop 2drop ;
616 : pazsan 1.1
617 :     : reveal ( -- ) \ gforth
618 :     last?
619 :     if \ the last word has a header
620 :     dup ( name>link ) @ 1 and
621 :     if \ it is still hidden
622 :     dup ( name>link ) @ 1 xor ( nt wid )
623 :     2dup >r name>string r> check-shadow ( nt wid )
624 : pazsan 1.83 dup wordlist-map @ reveal-method perform
625 : pazsan 1.1 else
626 :     drop
627 :     then
628 :     then ;
629 :    
630 :     : rehash ( wid -- )
631 :     dup wordlist-map @ rehash-method perform ;
632 :    
633 :     ' reveal alias recursive ( compilation -- ; run-time -- ) \ gforth
634 : crook 1.10 \g Make the current definition visible, enabling it to call itself
635 : pazsan 1.1 \g recursively.
636 :     immediate restrict

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help