Annotation of gforth/kernel/basics.fs, revision 1.65
1.1 anton 1: \ kernel.fs GForth kernel 17dec92py
2:
1.64 anton 3: \ Copyright (C) 1995,1998,2000,2003,2004,2005,2006,2007,2008 Free Software Foundation, Inc.
1.1 anton 4:
5: \ This file is part of Gforth.
6:
7: \ Gforth is free software; you can redistribute it and/or
8: \ modify it under the terms of the GNU General Public License
1.62 anton 9: \ as published by the Free Software Foundation, either version 3
1.1 anton 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
1.62 anton 18: \ along with this program. If not, see http://www.gnu.org/licenses/.
1.1 anton 19:
20: \ Idea and implementation: Bernd Paysan (py)
21:
1.16 jwilke 22: \ Needs:
23:
24: require ./vars.fs
1.32 anton 25: require ../compat/strcomp.fs
1.16 jwilke 26:
27: hex
1.1 anton 28:
29: \ labels for some code addresses
30:
31: \- NIL NIL AConstant NIL \ gforth
32:
33: \ Aliases
34:
1.4 jwilke 35: [IFUNDEF] r@
1.1 anton 36: ' i Alias r@ ( -- w ; R: w -- w ) \ core r-fetch
1.4 jwilke 37: [THEN]
1.1 anton 38:
39: \ !! this is machine-dependent, but works on all but the strangest machines
40:
1.23 anton 41: : maxaligned ( addr1 -- addr2 ) \ gforth
42: \G @i{addr2} is the first address after @i{addr1} that satisfies
43: \G all alignment restrictions.
1.1 anton 44: [ /maxalign 1 - ] Literal + [ 0 /maxalign - ] Literal and ;
1.23 anton 45: \ !! machine-dependent and won't work if "0 >body" <> "0 >body
46: \G maxaligned"
1.1 anton 47: ' maxaligned Alias cfaligned ( addr1 -- addr2 ) \ gforth
1.23 anton 48: \G @i{addr2} is the first address after @i{addr1} that is aligned for
49: \G a code field (i.e., such that the corresponding body is maxaligned).
1.1 anton 50:
51: : chars ( n1 -- n2 ) \ core
1.23 anton 52: \G @i{n2} is the number of address units of @i{n1} chars.""
1.1 anton 53: ; immediate
54:
55:
56: \ : A! ( addr1 addr2 -- ) \ gforth
57: \ dup relon ! ;
58: \ : A, ( addr -- ) \ gforth
59: \ here cell allot A! ;
60: ' ! alias A! ( addr1 addr2 -- ) \ gforth
61:
1.2 anton 62: \ UNUSED 17may93jaw
63:
1.41 pazsan 64: has? ec [IF]
1.30 jwilke 65: unlock ram-dictionary borders nip lock
66: AConstant dictionary-end
1.4 jwilke 67: [ELSE]
1.35 pazsan 68: has? header [IF]
69: : dictionary-end ( -- addr )
70: forthstart [ 3 cells image-header + ] Aliteral @ + ;
71: [ELSE]
72: : forthstart 0 ;
73: : dictionary-end ( -- addr )
74: forthstart [ has? kernel-size ] Literal + ;
75: [THEN]
1.4 jwilke 76: [THEN]
1.2 anton 77:
1.14 anton 78: : usable-dictionary-end ( -- addr )
79: dictionary-end [ word-pno-size pad-minsize + ] Literal - ;
80:
1.2 anton 81: : unused ( -- u ) \ core-ext
1.13 crook 82: \G Return the amount of free space remaining (in address units) in
83: \G the region addressed by @code{here}.
1.14 anton 84: usable-dictionary-end here - ;
1.2 anton 85:
1.65 ! pazsan 86: has? ec has? primcentric 0= and [IF]
1.40 pazsan 87: : in-dictionary? ( x -- f )
1.41 pazsan 88: dictionary-end u< ;
1.40 pazsan 89: [ELSE]
1.34 anton 90: : in-dictionary? ( x -- f )
91: forthstart dictionary-end within ;
1.40 pazsan 92: [THEN]
1.34 anton 93:
1.1 anton 94: \ here is used for pad calculation!
95:
96: : dp ( -- addr ) \ gforth
97: dpp @ ;
1.13 crook 98: : here ( -- addr ) \ core
99: \G Return the address of the next free location in data space.
1.1 anton 100: dp @ ;
101:
102: \ on off 23feb93py
103:
1.4 jwilke 104: \ on is used by docol:
1.15 crook 105: : on ( a-addr -- ) \ gforth
106: \G Set the (value of the) variable at @i{a-addr} to @code{true}.
1.1 anton 107: true swap ! ;
1.15 crook 108: : off ( a-addr -- ) \ gforth
109: \G Set the (value of the) variable at @i{a-addr} to @code{false}.
1.1 anton 110: false swap ! ;
111:
112: \ dabs roll 17may93jaw
113:
1.24 anton 114: : dabs ( d -- ud ) \ double d-abs
1.1 anton 115: dup 0< IF dnegate THEN ;
116:
117: : roll ( x0 x1 .. xn n -- x1 .. xn x0 ) \ core-ext
1.47 anton 118: \ dup 1+ pick >r
119: \ cells sp@ cell+ dup cell+ rot move drop r> ;
120: dup 0<= if
121: drop
122: else
1.54 pazsan 123: swap >r 1- recurse r> swap
1.47 anton 124: then ;
1.1 anton 125:
126: \ place bounds 13feb93py
127:
128: : place ( addr len to -- ) \ gforth
129: over >r rot over 1+ r> move c! ;
1.27 anton 130: : bounds ( addr u -- addr+u addr ) \ gforth
131: \G Given a memory block represented by starting address @i{addr}
132: \G and length @i{u} in aus, produce the end address @i{addr+u} and
133: \G the start address in the right order for @code{u+do} or
134: \G @code{?do}.
1.1 anton 135: over + swap ;
136:
137: \ (word) 22feb93py
138:
139: : scan ( addr1 n1 char -- addr2 n2 ) \ gforth
1.43 anton 140: \G skip all characters not equal to char
1.1 anton 141: >r
142: BEGIN
143: dup
144: WHILE
145: over c@ r@ <>
146: WHILE
147: 1 /string
148: REPEAT THEN
149: rdrop ;
150: : skip ( addr1 n1 char -- addr2 n2 ) \ gforth
1.43 anton 151: \G skip all characters equal to char
1.1 anton 152: >r
153: BEGIN
154: dup
155: WHILE
156: over c@ r@ =
157: WHILE
158: 1 /string
159: REPEAT THEN
160: rdrop ;
161:
162: \ digit? 17dec92py
163:
164: : digit? ( char -- digit true/ false ) \ gforth
165: toupper [char] 0 - dup 9 u> IF
1.16 jwilke 166: [ char A char 9 1 + - ] literal -
1.1 anton 167: dup 9 u<= IF
168: drop false EXIT
169: THEN
170: THEN
171: dup base @ u>= IF
172: drop false EXIT
173: THEN
174: true ;
175:
176: : accumulate ( +d0 addr digit - +d1 addr )
1.40 pazsan 177: swap >r swap base @ um* drop rot base @ um* d+ r> ;
1.1 anton 178:
1.18 crook 179: : >number ( ud1 c-addr1 u1 -- ud2 c-addr2 u2 ) \ core to-number
1.22 anton 180: \G Attempt to convert the character string @var{c-addr1 u1} to an
1.13 crook 181: \G unsigned number in the current number base. The double
182: \G @var{ud1} accumulates the result of the conversion to form
183: \G @var{ud2}. Conversion continues, left-to-right, until the whole
184: \G string is converted or a character that is not convertable in
185: \G the current number base is encountered (including + or -). For
186: \G each convertable character, @var{ud1} is first multiplied by
187: \G the value in @code{BASE} and then incremented by the value
188: \G represented by the character. @var{c-addr2} is the location of
189: \G the first unconverted character (past the end of the string if
190: \G the whole string was converted). @var{u2} is the number of
191: \G unconverted characters in the string. Overflow is not detected.
1.1 anton 192: 0
193: ?DO
194: count digit?
195: WHILE
196: accumulate
197: LOOP
198: 0
199: ELSE
200: 1- I' I -
201: UNLOOP
202: THEN ;
203:
204: \ s>d um/mod 21mar93py
205:
206: : s>d ( n -- d ) \ core s-to-d
207: dup 0< ;
208:
209: : ud/mod ( ud1 u2 -- urem udquot ) \ gforth
210: >r 0 r@ um/mod r> swap >r
211: um/mod r> ;
212:
213: \ catch throw 23feb93py
214:
1.5 jwilke 215: has? glocals [IF]
1.12 crook 216: : lp@ ( -- addr ) \ gforth lp-fetch
1.1 anton 217: laddr# [ 0 , ] ;
218: [THEN]
219:
1.59 pazsan 220: has? os 0= [IF]
1.52 pazsan 221: : catch ( ... xt -- ... 0 )
222: handler @ >r sp@ >r
223: rp@ handler ! execute 0 r> drop r> handler ! ;
224: : throw ( error -- error ) dup 0= IF drop EXIT THEN
225: handler @ rp! r> swap >r sp! r> r> handler ! ;
226: [ELSE]
1.17 anton 227: defer catch ( x1 .. xn xt -- y1 .. ym 0 / z1 .. zn error ) \ exception
1.24 anton 228: \G @code{Executes} @i{xt}. If execution returns normally,
229: \G @code{catch} pushes 0 on the stack. If execution returns through
230: \G @code{throw}, all the stacks are reset to the depth on entry to
231: \G @code{catch}, and the TOS (the @i{xt} position) is replaced with
232: \G the throw code.
233:
1.17 anton 234: :noname ( ... xt -- ... 0 )
235: execute 0 ;
236: is catch
1.1 anton 237:
1.24 anton 238: defer throw ( y1 .. ym nerror -- y1 .. ym / z1 .. zn error ) \ exception
239: \G If @i{nerror} is 0, drop it and continue. Otherwise, transfer
240: \G control to the next dynamically enclosing exception handler, reset
241: \G the stacks accordingly, and push @i{nerror}.
242:
243: :noname ( y1 .. ym error -- y1 .. ym / z1 .. zn error )
1.19 anton 244: ?dup if
1.35 pazsan 245: [ has? header [IF] here image-header 9 cells + ! [THEN] ]
1.60 pazsan 246: cr DoError cr
1.21 pazsan 247: [ has? file [IF] ] script? IF 1 (bye) ELSE quit THEN
248: [ [ELSE] ] quit [ [THEN] ]
1.19 anton 249: then ;
1.56 pazsan 250: is throw
1.52 pazsan 251: [THEN]
1.19 anton 252:
1.1 anton 253: \ (abort")
254:
1.33 anton 255: : c(abort") ( c-addr -- )
256: "error ! -2 throw ;
257:
1.1 anton 258: : (abort")
259: "lit >r
260: IF
261: r> "error ! -2 throw
262: THEN
263: rdrop ;
1.6 pazsan 264:
265: : abort ( ?? -- ?? ) \ core,exception-ext
1.12 crook 266: \G @code{-1 throw}.
1.6 pazsan 267: -1 throw ;
1.1 anton 268:
269: \ ?stack 23feb93py
270:
271: : ?stack ( ?? -- ?? ) \ gforth
1.3 jwilke 272: sp@ sp0 @ u> IF -4 throw THEN
1.5 jwilke 273: [ has? floating [IF] ]
1.3 jwilke 274: fp@ fp0 @ u> IF -&45 throw THEN
1.1 anton 275: [ [THEN] ]
276: ;
277: \ ?stack should be code -- it touches an empty stack!
278:
279: \ DEPTH 9may93jaw
280:
1.9 crook 281: : depth ( -- +n ) \ core depth
1.12 crook 282: \G @var{+n} is the number of values that were on the data stack before
283: \G @var{+n} itself was placed on the stack.
1.3 jwilke 284: sp@ sp0 @ swap - cell / ;
1.9 crook 285:
286: : clearstack ( ... -- ) \ gforth clear-stack
1.42 anton 287: \G remove and discard all/any items from the data stack.
1.3 jwilke 288: sp0 @ sp! ;
1.42 anton 289:
290: : clearstacks ( ... -- ) \ gforth clear-stacks
291: \G empty data and FP stack
1.49 pazsan 292: clearstack
293: [ has? floating [IF] ]
294: fp0 @ fp!
295: [ [THEN] ]
296: ;
1.1 anton 297:
298: \ Strings 22feb93py
299:
300: : "lit ( -- addr )
301: r> r> dup count + aligned >r swap >r ;
302:
303: \ HEX DECIMAL 2may93jaw
304:
305: : decimal ( -- ) \ core
1.63 anton 306: \G Set @code{base} to &10 (decimal). Don't use @code{decimal}, use
1.58 anton 307: \G @code{base-execute} instead.
1.1 anton 308: a base ! ;
309: : hex ( -- ) \ core-ext
1.58 anton 310: \G Set @code{base} to &16 (hexadecimal). Don't use @code{hex},
311: \G use @code{base-execute} instead.
1.1 anton 312: 10 base ! ;
313:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>