[gforth] / gforth / kernel / comp.fs  

gforth: gforth/kernel/comp.fs


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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help