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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help