[gforth] / gforth / kernel / comp.fs  

gforth: gforth/kernel/comp.fs


1 : pazsan 1.1 \ compiler definitions 14sep97jaw
2 :    
3 : anton 1.74 \ Copyright (C) 1995,1996,1997,1998,2000,2003,2004,2005 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 :     \ 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 : anton 1.29 \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
20 : anton 1.6
21 : pazsan 1.1 \ \ Revisions-Log
22 :    
23 :     \ put in seperate file 14sep97jaw
24 :    
25 :     \ \ here allot , c, A, 17dec92py
26 :    
27 : jwilke 1.14 [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 : pazsan 1.1 : allot ( n -- ) \ core
38 : anton 1.23 \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 : anton 1.12 here +
45 :     dup 1- usable-dictionary-end forthstart within -8 and throw
46 :     dp ! ;
47 : jwilke 1.14 [THEN]
48 : pazsan 1.1
49 : crook 1.22 : c, ( c -- ) \ core c-comma
50 : crook 1.15 \G Reserve data space for one char and store @i{c} in the space.
51 : pazsan 1.84 here 1 chars allot [ has? flash [IF] ] flashc! [ [ELSE] ] c! [ [THEN] ] ;
52 : pazsan 1.1
53 : crook 1.22 : , ( w -- ) \ core comma
54 : crook 1.15 \G Reserve data space for one cell and store @i{w} in the space.
55 : pazsan 1.84 here cell allot [ has? flash [IF] ] flash! [ [ELSE] ] ! [ [THEN] ] ;
56 : pazsan 1.1
57 :     : 2, ( w1 w2 -- ) \ gforth
58 : crook 1.15 \G Reserve data space for two cells and store the double @i{w1
59 : anton 1.24 \G w2} there, @i{w2} first (lower address).
60 : pazsan 1.84 here 2 cells allot [ has? flash [IF] ] tuck flash! cell+ flash!
61 :     [ [ELSE] ] 2! [ [THEN] ] ;
62 : pazsan 1.1
63 :     \ : aligned ( addr -- addr' ) \ core
64 :     \ [ cell 1- ] Literal + [ -1 cells ] Literal and ;
65 :    
66 :     : align ( -- ) \ core
67 : crook 1.15 \G If the data-space pointer is not aligned, reserve enough space to align it.
68 : pazsan 1.1 here dup aligned swap ?DO bl c, LOOP ;
69 :    
70 : crook 1.22 \ : faligned ( addr -- f-addr ) \ float f-aligned
71 : pazsan 1.1 \ [ 1 floats 1- ] Literal + [ -1 floats ] Literal and ;
72 :    
73 : crook 1.22 : falign ( -- ) \ float f-align
74 : crook 1.15 \G If the data-space pointer is not float-aligned, reserve
75 :     \G enough space to align it.
76 : pazsan 1.1 here dup faligned swap
77 :     ?DO
78 :     bl c,
79 :     LOOP ;
80 :    
81 : anton 1.9 : maxalign ( -- ) \ gforth
82 : anton 1.23 \G Align data-space pointer for all alignment requirements.
83 : pazsan 1.1 here dup maxaligned swap
84 :     ?DO
85 :     bl c,
86 :     LOOP ;
87 :    
88 :     \ the code field is aligned if its body is maxaligned
89 :     ' maxalign Alias cfalign ( -- ) \ gforth
90 : anton 1.24 \G Align data-space pointer for code field requirements (i.e., such
91 :     \G that the corresponding body is maxaligned).
92 : pazsan 1.1
93 :     ' , alias A, ( addr -- ) \ gforth
94 :    
95 :     ' NOOP ALIAS const
96 :    
97 :     \ \ Header 23feb93py
98 :    
99 :     \ input-stream, nextname and noname are quite ugly (passing
100 :     \ information through global variables), but they are useful for dealing
101 :     \ with existing/independent defining words
102 :    
103 :     defer (header)
104 :     defer header ( -- ) \ gforth
105 :     ' (header) IS header
106 :    
107 :     : string, ( c-addr u -- ) \ gforth
108 :     \G puts down string as cstring
109 : pazsan 1.83 dup [ has? rom [IF] ] $E0 [ [ELSE] ] alias-mask [ [THEN] ] or c,
110 : pazsan 1.84 [ has? flash [IF] ]
111 :     bounds ?DO I c@ c, LOOP
112 :     [ [ELSE] ]
113 :     here swap chars dup allot move
114 :     [ [THEN] ] ;
115 : pazsan 1.1
116 : anton 1.30 : longstring, ( c-addr u -- ) \ gforth
117 : anton 1.55 \G puts down string as longcstring
118 : anton 1.30 dup , here swap chars dup allot move ;
119 :    
120 : pazsan 1.1 : header, ( c-addr u -- ) \ gforth
121 :     name-too-long?
122 : anton 1.53 dup max-name-length @ max max-name-length !
123 : pazsan 1.1 align here last !
124 : pazsan 1.84 [ has? flash [IF] ]
125 : pazsan 1.83 -1 A,
126 :     [ [ELSE] ]
127 : pazsan 1.1 current @ 1 or A, \ link field; before revealing, it contains the
128 :     \ tagged reveal-into wordlist
129 : pazsan 1.83 [ [THEN] ]
130 : pazsan 1.78 [ has? f83headerstring [IF] ]
131 :     string,
132 :     [ [ELSE] ]
133 : pazsan 1.83 longstring, alias-mask lastflags cset
134 : pazsan 1.78 [ [THEN] ]
135 : pazsan 1.83 cfalign ;
136 : pazsan 1.1
137 :     : input-stream-header ( "name" -- )
138 : pazsan 1.80 parse-name name-too-short? header, ;
139 : pazsan 1.1
140 :     : input-stream ( -- ) \ general
141 :     \G switches back to getting the name from the input stream ;
142 :     ['] input-stream-header IS (header) ;
143 :    
144 :     ' input-stream-header IS (header)
145 :    
146 : anton 1.32 2variable nextname-string
147 : pazsan 1.1
148 : pazsan 1.33 has? OS [IF]
149 : pazsan 1.1 : nextname-header ( -- )
150 : anton 1.32 nextname-string 2@ header,
151 :     nextname-string free-mem-var
152 : pazsan 1.1 input-stream ;
153 : pazsan 1.33 [THEN]
154 : pazsan 1.1
155 :     \ the next name is given in the string
156 :    
157 : pazsan 1.33 has? OS [IF]
158 : pazsan 1.1 : nextname ( c-addr u -- ) \ gforth
159 : anton 1.19 \g The next defined word will have the name @var{c-addr u}; the
160 :     \g defining word will leave the input stream alone.
161 : pazsan 1.1 name-too-long?
162 : anton 1.32 nextname-string free-mem-var
163 :     save-mem nextname-string 2!
164 : pazsan 1.1 ['] nextname-header IS (header) ;
165 : pazsan 1.33 [THEN]
166 : pazsan 1.1
167 :     : noname-header ( -- )
168 :     0 last ! cfalign
169 :     input-stream ;
170 :    
171 :     : noname ( -- ) \ gforth
172 : anton 1.19 \g The next defined word will be anonymous. The defining word will
173 :     \g leave the input stream alone. The xt of the defined word will
174 : anton 1.56 \g be given by @code{latestxt}.
175 : pazsan 1.1 ['] noname-header IS (header) ;
176 :    
177 : anton 1.56 : latestxt ( -- xt ) \ gforth
178 : crook 1.15 \G @i{xt} is the execution token of the last word defined.
179 : crook 1.13 \ The main purpose of this word is to get the xt of words defined using noname
180 : pazsan 1.1 lastcfa @ ;
181 :    
182 : anton 1.56 ' latestxt alias lastxt \ gforth-obsolete
183 :     \G old name for @code{latestxt}.
184 :    
185 :     : latest ( -- nt ) \ gforth
186 :     \G @var{nt} is the name token of the last word defined; it is 0 if the
187 :     \G last word has no name.
188 :     last @ ;
189 :    
190 : pazsan 1.1 \ \ literals 17dec92py
191 :    
192 :     : Literal ( compilation n -- ; run-time -- n ) \ core
193 : anton 1.27 \G Compilation semantics: compile the run-time semantics.@*
194 :     \G Run-time Semantics: push @i{n}.@*
195 :     \G Interpretation semantics: undefined.
196 : jwilke 1.14 [ [IFDEF] lit, ]
197 :     lit,
198 :     [ [ELSE] ]
199 : anton 1.27 postpone lit ,
200 : jwilke 1.14 [ [THEN] ] ; immediate restrict
201 : pazsan 1.1
202 : anton 1.72 : 2Literal ( compilation w1 w2 -- ; run-time -- w1 w2 ) \ double two-literal
203 :     \G Compile appropriate code such that, at run-time, @i{w1 w2} are
204 :     \G placed on the stack. Interpretation semantics are undefined.
205 :     swap postpone Literal postpone Literal ; immediate restrict
206 :    
207 : pazsan 1.1 : ALiteral ( compilation addr -- ; run-time -- addr ) \ gforth
208 : jwilke 1.14 [ [IFDEF] alit, ]
209 :     alit,
210 :     [ [ELSE] ]
211 :     postpone lit A,
212 :     [ [THEN] ] ; immediate restrict
213 : pazsan 1.1
214 : pazsan 1.70 Defer char@ ( addr u -- char addr' u' )
215 :     :noname over c@ -rot 1 /string ; IS char@
216 :    
217 : crook 1.8 : char ( '<spaces>ccc' -- c ) \ core
218 : crook 1.15 \G Skip leading spaces. Parse the string @i{ccc} and return @i{c}, the
219 :     \G display code representing the first character of @i{ccc}.
220 : pazsan 1.70 bl word count char@ 2drop ;
221 : pazsan 1.1
222 : crook 1.8 : [char] ( compilation '<spaces>ccc' -- ; run-time -- c ) \ core bracket-char
223 : crook 1.10 \G Compilation: skip leading spaces. Parse the string
224 : crook 1.15 \G @i{ccc}. Run-time: return @i{c}, the display code
225 :     \G representing the first character of @i{ccc}. Interpretation
226 : crook 1.10 \G semantics for this word are undefined.
227 : pazsan 1.1 char postpone Literal ; immediate restrict
228 :    
229 :     \ \ threading 17mar93py
230 :    
231 :     : cfa, ( code-address -- ) \ gforth cfa-comma
232 :     here
233 :     dup lastcfa !
234 : pazsan 1.84 [ has? rom [IF] ] 2 cells allot [ [ELSE] ] 0 A, 0 , [ [THEN] ]
235 :     code-address! ;
236 : pazsan 1.1
237 : jwilke 1.14 [IFUNDEF] compile,
238 : anton 1.34 defer compile, ( xt -- ) \ core-ext compile-comma
239 :     \G Compile the word represented by the execution token @i{xt}
240 :     \G into the current definition.
241 :    
242 :     ' , is compile,
243 : jwilke 1.14 [THEN]
244 : pazsan 1.1
245 : pazsan 1.80 has? ec 0= [IF]
246 : anton 1.40 defer basic-block-end ( -- )
247 :    
248 : anton 1.60 :noname ( -- )
249 :     0 compile-prim1 ;
250 :     is basic-block-end
251 : pazsan 1.80 [THEN]
252 : anton 1.40
253 : pazsan 1.36 has? peephole [IF]
254 : anton 1.40
255 :     \ dynamic only
256 : anton 1.49 : peephole-compile, ( xt -- )
257 :     \ compile xt, appending its code to the current dynamic superinstruction
258 :     here swap , compile-prim1 ;
259 : anton 1.38
260 : anton 1.35 : compile-to-prims, ( xt -- )
261 :     \G compile xt to use primitives (and their peephole optimization)
262 :     \G instead of ","-ing the xt.
263 :     \ !! all POSTPONEs here postpone primitives; this can be optimized
264 : pazsan 1.39 dup >does-code if
265 : anton 1.66 ['] does-exec peephole-compile, , EXIT
266 :     \ dup >body POSTPONE literal ['] call peephole-compile, >does-code , EXIT
267 : anton 1.35 then
268 :     dup >code-address CASE
269 : anton 1.66 docon: OF >body ['] lit@ peephole-compile, , EXIT ENDOF
270 :     \ docon: OF >body POSTPONE literal ['] @ peephole-compile, EXIT ENDOF
271 : anton 1.42 \ docon is also used by VALUEs, so don't @ at compile time
272 : anton 1.66 docol: OF >body ['] call peephole-compile, , EXIT ENDOF
273 :     dovar: OF >body ['] lit peephole-compile, , EXIT ENDOF
274 :     douser: OF >body @ ['] useraddr peephole-compile, , EXIT ENDOF
275 :     dodefer: OF >body ['] lit-perform peephole-compile, , EXIT ENDOF
276 :     dofield: OF >body @ ['] lit+ peephole-compile, , EXIT ENDOF
277 :     \ dofield: OF >body @ POSTPONE literal ['] + peephole-compile, EXIT ENDOF
278 : anton 1.48 \ code words and ;code-defined words (code words could be optimized):
279 : anton 1.66 dup in-dictionary? IF drop POSTPONE literal ['] execute peephole-compile, EXIT THEN
280 : anton 1.35 ENDCASE
281 : anton 1.49 peephole-compile, ;
282 : anton 1.35
283 :     ' compile-to-prims, IS compile,
284 : pazsan 1.36 [ELSE]
285 :     ' , is compile,
286 :     [THEN]
287 : anton 1.34
288 : pazsan 1.1 : !does ( addr -- ) \ gforth store-does
289 : anton 1.56 latestxt does-code! ;
290 : pazsan 1.1
291 : anton 1.65 \ !! unused, but ifdefed/gosted in some places
292 : pazsan 1.1 : (does>) ( R: addr -- )
293 : anton 1.62 r> cfaligned /does-handler + !does ; \ !! no gforth-native
294 : pazsan 1.1
295 : anton 1.64 : (does>2) ( addr -- )
296 :     cfaligned /does-handler + !does ;
297 :    
298 : pazsan 1.1 : dodoes, ( -- )
299 :     cfalign here /does-handler allot does-handler! ;
300 :    
301 : pazsan 1.61 : (compile) ( -- ) \ gforth-obsolete: dummy
302 : anton 1.63 true abort" (compile) doesn't work, use POSTPONE instead" ;
303 : pazsan 1.1
304 :     \ \ ticks
305 :    
306 :     : name>comp ( nt -- w xt ) \ gforth
307 : crook 1.15 \G @i{w xt} is the compilation token for the word @i{nt}.
308 : pazsan 1.1 (name>comp)
309 :     1 = if
310 :     ['] execute
311 :     else
312 :     ['] compile,
313 :     then ;
314 :    
315 :     : [(')] ( compilation "name" -- ; run-time -- nt ) \ gforth bracket-paren-tick
316 :     (') postpone ALiteral ; immediate restrict
317 :    
318 :     : ['] ( compilation. "name" -- ; run-time. -- xt ) \ core bracket-tick
319 : crook 1.15 \g @i{xt} represents @i{name}'s interpretation
320 :     \g semantics. Perform @code{-14 throw} if the word has no
321 : pazsan 1.1 \g interpretation semantics.
322 :     ' postpone ALiteral ; immediate restrict
323 :    
324 : anton 1.5 : COMP' ( "name" -- w xt ) \ gforth comp-tick
325 : crook 1.15 \g Compilation token @i{w xt} represents @i{name}'s compilation semantics.
326 : pazsan 1.1 (') name>comp ;
327 :    
328 :     : [COMP'] ( compilation "name" -- ; run-time -- w xt ) \ gforth bracket-comp-tick
329 : crook 1.15 \g Compilation token @i{w xt} represents @i{name}'s compilation semantics.
330 : pazsan 1.1 COMP' swap POSTPONE Aliteral POSTPONE ALiteral ; immediate restrict
331 :    
332 : jwilke 1.18 : postpone, ( w xt -- ) \ gforth postpone-comma
333 : anton 1.26 \g Compile the compilation semantics represented by the
334 :     \g compilation token @i{w xt}.
335 : jwilke 1.18 dup ['] execute =
336 :     if
337 :     drop compile,
338 :     else
339 : pazsan 1.61 swap POSTPONE aliteral compile,
340 : jwilke 1.18 then ;
341 :    
342 :     : POSTPONE ( "name" -- ) \ core
343 :     \g Compiles the compilation semantics of @i{name}.
344 : anton 1.48 COMP' postpone, ; immediate
345 : jwilke 1.18
346 : pazsan 1.1 \ \ recurse 17may93jaw
347 :    
348 :     : recurse ( compilation -- ; run-time ?? -- ?? ) \ core
349 : crook 1.10 \g Call the current definition.
350 : anton 1.56 latestxt compile, ; immediate restrict
351 : pazsan 1.1
352 :     \ \ compiler loop
353 :    
354 : anton 1.72 : compiler1 ( c-addr u -- ... xt )
355 : pazsan 1.1 2dup find-name dup
356 :     if ( c-addr u nt )
357 : anton 1.72 nip nip name>comp
358 : pazsan 1.1 else
359 :     drop
360 : anton 1.72 2dup 2>r snumber? dup
361 : pazsan 1.1 IF
362 :     0>
363 :     IF
364 : anton 1.72 ['] 2literal
365 :     ELSE
366 :     ['] literal
367 : pazsan 1.1 THEN
368 : anton 1.72 2rdrop
369 : pazsan 1.1 ELSE
370 : anton 1.72 drop 2r> compiler-notfound1
371 : pazsan 1.1 THEN
372 :     then ;
373 :    
374 : crook 1.22 : [ ( -- ) \ core left-bracket
375 : crook 1.8 \G Enter interpretation state. Immediate word.
376 : anton 1.72 ['] interpreter1 IS parser1 state off ; immediate
377 : pazsan 1.1
378 :     : ] ( -- ) \ core right-bracket
379 : crook 1.8 \G Enter compilation state.
380 : anton 1.72 ['] compiler1 IS parser1 state on ;
381 : pazsan 1.1
382 :     \ \ Strings 22feb93py
383 :    
384 : anton 1.44 : S, ( addr u -- )
385 : anton 1.45 \ allot string as counted string
386 : pazsan 1.84 [ has? flash [IF] ]
387 :     dup c, bounds ?DO I c@ c, LOOP
388 :     [ [ELSE] ]
389 :     here over char+ allot place align
390 :     [ [THEN] ] ;
391 : anton 1.45
392 :     : mem, ( addr u -- )
393 :     \ allot the memory block HERE (do alignment yourself)
394 : pazsan 1.84 [ has? flash [IF] ]
395 :     bounds ?DO I c@ c, LOOP
396 :     [ [ELSE] ]
397 :     here over allot swap move
398 :     [ [THEN] ] ;
399 : pazsan 1.1
400 : anton 1.44 : ," ( "string"<"> -- )
401 :     [char] " parse s, ;
402 : pazsan 1.1
403 :     \ \ Header states 23feb93py
404 :    
405 : pazsan 1.83 \ problematic only for big endian machines
406 :    
407 :     has? f83headerstring [IF]
408 :     : cset ( bmask c-addr -- )
409 :     tuck c@ or swap c! ;
410 :    
411 :     : creset ( bmask c-addr -- )
412 :     tuck c@ swap invert and swap c! ;
413 :    
414 :     : ctoggle ( bmask c-addr -- )
415 :     tuck c@ xor swap c! ;
416 :     [ELSE]
417 : pazsan 1.1 : cset ( bmask c-addr -- )
418 : anton 1.30 tuck @ or swap ! ;
419 : pazsan 1.1
420 :     : creset ( bmask c-addr -- )
421 : anton 1.30 tuck @ swap invert and swap ! ;
422 : pazsan 1.1
423 :     : ctoggle ( bmask c-addr -- )
424 : anton 1.30 tuck @ xor swap ! ;
425 : pazsan 1.83 [THEN]
426 : pazsan 1.1
427 :     : lastflags ( -- c-addr )
428 :     \ the address of the flags byte in the last header
429 :     \ aborts if the last defined word was headerless
430 : anton 1.56 latest dup 0= abort" last word was headerless" cell+ ;
431 : pazsan 1.1
432 :     : immediate ( -- ) \ core
433 : crook 1.10 \G Make the compilation semantics of a word be to @code{execute}
434 :     \G the execution semantics.
435 : pazsan 1.83 immediate-mask lastflags [ has? rom [IF] ] creset [ [ELSE] ] cset [ [THEN] ] ;
436 : pazsan 1.1
437 :     : restrict ( -- ) \ gforth
438 : crook 1.10 \G A synonym for @code{compile-only}
439 : pazsan 1.83 restrict-mask lastflags [ has? rom [IF] ] creset [ [ELSE] ] cset [ [THEN] ] ;
440 : jwilke 1.18
441 : pazsan 1.1 ' restrict alias compile-only ( -- ) \ gforth
442 : crook 1.10 \G Remove the interpretation semantics of a word.
443 : pazsan 1.1
444 :     \ \ Create Variable User Constant 17mar93py
445 :    
446 : crook 1.15 : Alias ( xt "name" -- ) \ gforth
447 : pazsan 1.1 Header reveal
448 :     alias-mask lastflags creset
449 :     dup A, lastcfa ! ;
450 :    
451 :     doer? :dovar [IF]
452 :    
453 :     : Create ( "name" -- ) \ core
454 :     Header reveal dovar: cfa, ;
455 :     [ELSE]
456 :    
457 :     : Create ( "name" -- ) \ core
458 :     Header reveal here lastcfa ! 0 A, 0 , DOES> ;
459 :     [THEN]
460 :    
461 :     : Variable ( "name" -- ) \ core
462 :     Create 0 , ;
463 :    
464 :     : AVariable ( "name" -- ) \ gforth
465 :     Create 0 A, ;
466 :    
467 : crook 1.22 : 2Variable ( "name" -- ) \ double two-variable
468 : pazsan 1.1 create 0 , 0 , ;
469 :    
470 : pazsan 1.75 has? no-userspace 0= [IF]
471 : crook 1.21 : uallot ( n -- ) \ gforth
472 :     udp @ swap udp +! ;
473 : pazsan 1.1
474 :     doer? :douser [IF]
475 :    
476 :     : User ( "name" -- ) \ gforth
477 :     Header reveal douser: cfa, cell uallot , ;
478 :    
479 :     : AUser ( "name" -- ) \ gforth
480 :     User ;
481 :     [ELSE]
482 :    
483 :     : User Create cell uallot , DOES> @ up @ + ;
484 :    
485 :     : AUser User ;
486 : pazsan 1.75 [THEN]
487 : pazsan 1.1 [THEN]
488 :    
489 :     doer? :docon [IF]
490 :     : (Constant) Header reveal docon: cfa, ;
491 :     [ELSE]
492 :     : (Constant) Create DOES> @ ;
493 :     [THEN]
494 :    
495 : pazsan 1.76 doer? :dovalue [IF]
496 :     : (Value) Header reveal dovalue: cfa, ;
497 :     [ELSE]
498 :     has? rom [IF]
499 :     : (Value) Create DOES> @ @ ;
500 :     [ELSE]
501 :     : (Value) Create DOES> @ ;
502 :     [THEN]
503 :     [THEN]
504 :    
505 : pazsan 1.1 : Constant ( w "name" -- ) \ core
506 : crook 1.15 \G Define a constant @i{name} with value @i{w}.
507 : pazsan 1.1 \G
508 : crook 1.15 \G @i{name} execution: @i{-- w}
509 : pazsan 1.1 (Constant) , ;
510 :    
511 :     : AConstant ( addr "name" -- ) \ gforth
512 :     (Constant) A, ;
513 :    
514 : pazsan 1.84 has? flash [IF]
515 :     : Value ( w "name" -- ) \ core-ext
516 :     (Value) dpp @ >r here cell allot >r
517 :     ram here >r , r> r> flash! r> dpp ! ;
518 :    
519 :     ' Value alias AValue
520 :     [ELSE]
521 : pazsan 1.1 : Value ( w "name" -- ) \ core-ext
522 : pazsan 1.76 (Value) , ;
523 : pazsan 1.1
524 : jwilke 1.37 : AValue ( w "name" -- ) \ core-ext
525 : pazsan 1.76 (Value) A, ;
526 : pazsan 1.84 [THEN]
527 : jwilke 1.37
528 : crook 1.22 : 2Constant ( w1 w2 "name" -- ) \ double two-constant
529 : pazsan 1.1 Create ( w1 w2 "name" -- )
530 :     2,
531 :     DOES> ( -- w1 w2 )
532 :     2@ ;
533 :    
534 :     doer? :dofield [IF]
535 :     : (Field) Header reveal dofield: cfa, ;
536 :     [ELSE]
537 :     : (Field) Create DOES> @ + ;
538 :     [THEN]
539 : pazsan 1.39
540 :     \ \ interpret/compile:
541 :    
542 :     struct
543 :     >body
544 :     cell% field interpret/compile-int
545 :     cell% field interpret/compile-comp
546 :     end-struct interpret/compile-struct
547 :    
548 :     : interpret/compile: ( interp-xt comp-xt "name" -- ) \ gforth
549 :     Create immediate swap A, A,
550 :     DOES>
551 :     abort" executed primary cfa of an interpret/compile: word" ;
552 :     \ state @ IF cell+ THEN perform ;
553 :    
554 : pazsan 1.1 \ IS Defer What's Defers TO 24feb93py
555 :    
556 : anton 1.68 defer defer-default ( -- )
557 : anton 1.69 ' abort is defer-default
558 :     \ default action for deferred words (overridden by a warning later)
559 : anton 1.67
560 : pazsan 1.1 doer? :dodefer [IF]
561 :    
562 :     : Defer ( "name" -- ) \ gforth
563 : anton 1.67 \G Define a deferred word @i{name}; its execution semantics can be
564 :     \G set with @code{defer!} or @code{is} (and they have to, before first
565 :     \G executing @i{name}.
566 : pazsan 1.1 Header Reveal dodefer: cfa,
567 : anton 1.67 ['] defer-default A, ;
568 : jwilke 1.18
569 : pazsan 1.1 [ELSE]
570 :    
571 : pazsan 1.77 has? rom [IF]
572 :     : Defer ( "name" -- ) \ gforth
573 :     Create ['] defer-default A,
574 :     DOES> @ @ execute ;
575 :     [ELSE]
576 :     : Defer ( "name" -- ) \ gforth
577 :     Create ['] defer-default A,
578 :     DOES> @ execute ;
579 :     [THEN]
580 : pazsan 1.1 [THEN]
581 :    
582 : anton 1.67 : defer@ ( xt-deferred -- xt ) \ gforth defer-fetch
583 :     \G @i{xt} represents the word currently associated with the deferred
584 :     \G word @i{xt-deferred}.
585 :     >body @ ;
586 :    
587 : anton 1.25 : Defers ( compilation "name" -- ; run-time ... -- ... ) \ gforth
588 :     \G Compiles the present contents of the deferred word @i{name}
589 :     \G into the current definition. I.e., this produces static
590 :     \G binding as if @i{name} was not deferred.
591 : anton 1.67 ' defer@ compile, ; immediate
592 : jwilke 1.18
593 :     :noname
594 :     dodoes, here !does ]
595 :     defstart :-hook ;
596 :     :noname
597 :     ;-hook ?struc
598 :     [ has? xconds [IF] ] exit-like [ [THEN] ]
599 : pazsan 1.84 here [ has? peephole [IF] ] 5 [ [ELSE] ] 4 [ [THEN] ] cells +
600 :     postpone aliteral postpone (does>2) [compile] exit
601 : pazsan 1.80 [ has? peephole [IF] ] finish-code [ [THEN] ] dodoes,
602 : jwilke 1.18 defstart :-hook ;
603 :     interpret/compile: DOES> ( compilation colon-sys1 -- colon-sys2 ; run-time nest-sys -- ) \ core does
604 :    
605 : anton 1.73 : defer! ( xt xt-deferred -- ) \ gforth defer-store
606 : anton 1.67 \G Changes the @code{defer}red word @var{xt-deferred} to execute @var{xt}.
607 :     >body ! ;
608 :    
609 : anton 1.20 : <IS> ( "name" xt -- ) \ gforth
610 :     \g Changes the @code{defer}red word @var{name} to execute @var{xt}.
611 : anton 1.67 ' defer! ;
612 : anton 1.20
613 :     : [IS] ( compilation "name" -- ; run-time xt -- ) \ gforth bracket-is
614 :     \g At run-time, changes the @code{defer}red word @var{name} to
615 :     \g execute @var{xt}.
616 : anton 1.67 ' postpone ALiteral postpone defer! ; immediate restrict
617 : jwilke 1.18
618 : anton 1.20 ' <IS>
619 : jwilke 1.18 ' [IS]
620 : anton 1.67 interpret/compile: IS ( compilation/interpretation "name-deferred" -- ; run-time xt -- ) \ gforth
621 :     \G Changes the @code{defer}red word @var{name} to execute @var{xt}.
622 :     \G Its compilation semantics parses at compile time.
623 : jwilke 1.18
624 : anton 1.20 ' <IS>
625 :     ' [IS]
626 :     interpret/compile: TO ( w "name" -- ) \ core-ext
627 : jwilke 1.18
628 :     : interpret/compile? ( xt -- flag )
629 :     >does-code ['] DOES> >does-code = ;
630 : pazsan 1.1
631 :     \ \ : ; 24feb93py
632 :    
633 :     defer :-hook ( sys1 -- sys2 )
634 :    
635 :     defer ;-hook ( sys2 -- sys1 )
636 :    
637 : jwilke 1.16 0 Constant defstart
638 :    
639 : jwilke 1.14 [IFDEF] docol,
640 :     : (:noname) ( -- colon-sys )
641 :     \ common factor of : and :noname
642 : anton 1.34 docol, ]comp
643 : jwilke 1.14 [ELSE]
644 : anton 1.7 : (:noname) ( -- colon-sys )
645 :     \ common factor of : and :noname
646 : anton 1.34 docol: cfa,
647 : jwilke 1.14 [THEN]
648 : anton 1.40 defstart ] :-hook ;
649 : anton 1.7
650 : pazsan 1.1 : : ( "name" -- colon-sys ) \ core colon
651 : anton 1.7 Header (:noname) ;
652 :    
653 :     : :noname ( -- xt colon-sys ) \ core-ext colon-no-name
654 :     0 last !
655 :     cfalign here (:noname) ;
656 : pazsan 1.1
657 : jwilke 1.14 [IFDEF] fini,
658 :     : ; ( compilation colon-sys -- ; run-time nest-sys ) \ core semicolon
659 :     ;-hook ?struc fini, comp[ reveal postpone [ ; immediate restrict
660 :     [ELSE]
661 : pazsan 1.1 : ; ( compilation colon-sys -- ; run-time nest-sys ) \ core semicolon
662 : pazsan 1.57 ;-hook ?struc [compile] exit
663 :     [ has? peephole [IF] ] finish-code [ [THEN] ]
664 :     reveal postpone [ ; immediate restrict
665 : jwilke 1.14 [THEN]
666 : pazsan 1.1
667 :     \ \ Search list handling: reveal words, recursive 23feb93py
668 :    
669 :     : last? ( -- false / nfa nfa )
670 : anton 1.56 latest ?dup ;
671 : pazsan 1.1
672 : pazsan 1.83 Variable warnings ( -- addr ) \ gforth
673 :     G -1 warnings T !
674 :    
675 :     has? ec [IF]
676 :     : reveal ( -- ) \ gforth
677 :     last?
678 :     if \ the last word has a header
679 :     dup ( name>link ) @ -1 =
680 :     if \ it is still hidden
681 : pazsan 1.84 current @ dup >r @ over
682 :     [ has? flash [IF] ] flash! [ [ELSE] ] ! [ [THEN] ] r> !
683 : pazsan 1.83 else
684 :     drop
685 :     then
686 :     then ;
687 :     [ELSE]
688 : pazsan 1.1 : (reveal) ( nt wid -- )
689 : pazsan 1.3 wordlist-id dup >r
690 : pazsan 1.1 @ over ( name>link ) !
691 :     r> ! ;
692 :    
693 :     \ make entry in wordlist-map
694 :     ' (reveal) f83search reveal-method !
695 :    
696 :     : check-shadow ( addr count wid -- )
697 : pazsan 1.2 \G prints a warning if the string is already present in the wordlist
698 :     >r 2dup 2dup r> (search-wordlist) warnings @ and ?dup if
699 :     >stderr
700 :     ." redefined " name>string 2dup type
701 : anton 1.43 str= 0= if
702 : pazsan 1.2 ." with " type
703 :     else
704 :     2drop
705 :     then
706 :     space space EXIT
707 :     then
708 :     2drop 2drop ;
709 : pazsan 1.1
710 :     : reveal ( -- ) \ gforth
711 :     last?
712 :     if \ the last word has a header
713 :     dup ( name>link ) @ 1 and
714 :     if \ it is still hidden
715 :     dup ( name>link ) @ 1 xor ( nt wid )
716 :     2dup >r name>string r> check-shadow ( nt wid )
717 : pazsan 1.83 dup wordlist-map @ reveal-method perform
718 : pazsan 1.1 else
719 :     drop
720 :     then
721 :     then ;
722 :    
723 :     : rehash ( wid -- )
724 :     dup wordlist-map @ rehash-method perform ;
725 : pazsan 1.80 [THEN]
726 : pazsan 1.1
727 :     ' reveal alias recursive ( compilation -- ; run-time -- ) \ gforth
728 : crook 1.10 \g Make the current definition visible, enabling it to call itself
729 : pazsan 1.1 \g recursively.
730 :     immediate restrict

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help