1: \ kernel.fs GForth kernel 17dec92py
2:
3: \ Copyright (C) 1995,1998,2000,2003,2004,2005,2006,2007,2008,2010 Free Software Foundation, Inc.
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
9: \ as published by the Free Software Foundation, either version 3
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, see http://www.gnu.org/licenses/.
19:
20: \ Idea and implementation: Bernd Paysan (py)
21:
22: \ Needs:
23:
24: require ./vars.fs
25: require ../compat/strcomp.fs
26:
27: hex
28:
29: \ labels for some code addresses
30:
31: \- NIL NIL AConstant NIL \ gforth
32:
33: \ Aliases
34:
35: [IFUNDEF] r@
36: ' i Alias r@ ( -- w ; R: w -- w ) \ core r-fetch
37: [THEN]
38:
39: \ !! this is machine-dependent, but works on all but the strangest machines
40:
41: : maxaligned ( addr1 -- addr2 ) \ gforth
42: \G @i{addr2} is the first address after @i{addr1} that satisfies
43: \G all alignment restrictions.
44: [ /maxalign 1 - ] Literal + [ 0 /maxalign - ] Literal and ;
45: \ !! machine-dependent and won't work if "0 >body" <> "0 >body
46: \G maxaligned"
47: ' maxaligned Alias cfaligned ( addr1 -- addr2 ) \ gforth
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).
50:
51: : chars ( n1 -- n2 ) \ core
52: \G @i{n2} is the number of address units of @i{n1} chars.""
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:
62: \ UNUSED 17may93jaw
63:
64: has? ec [IF]
65: unlock ram-dictionary borders nip lock
66: AConstant dictionary-end
67: [ELSE]
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]
76: [THEN]
77:
78: : usable-dictionary-end ( -- addr )
79: dictionary-end [ word-pno-size pad-minsize + ] Literal - ;
80:
81: : unused ( -- u ) \ core-ext
82: \G Return the amount of free space remaining (in address units) in
83: \G the region addressed by @code{here}.
84: usable-dictionary-end here - ;
85:
86: has? ec has? primcentric 0= and [IF]
87: : in-dictionary? ( x -- f )
88: dictionary-end u< ;
89: [ELSE]
90: : in-dictionary? ( x -- f )
91: forthstart dictionary-end within ;
92: [THEN]
93:
94: \ here is used for pad calculation!
95:
96: : dp ( -- addr ) \ gforth
97: dpp @ ;
98: : here ( -- addr ) \ core
99: \G Return the address of the next free location in data space.
100: dp @ ;
101:
102: \ on off 23feb93py
103:
104: \ on is used by docol:
105: : on ( a-addr -- ) \ gforth
106: \G Set the (value of the) variable at @i{a-addr} to @code{true}.
107: true swap ! ;
108: : off ( a-addr -- ) \ gforth
109: \G Set the (value of the) variable at @i{a-addr} to @code{false}.
110: false swap ! ;
111:
112: \ dabs roll 17may93jaw
113:
114: : dabs ( d -- ud ) \ double d-abs
115: dup 0< IF dnegate THEN ;
116:
117: : roll ( x0 x1 .. xn n -- x1 .. xn x0 ) \ core-ext
118: \ dup 1+ pick >r
119: \ cells sp@ cell+ dup cell+ rot move drop r> ;
120: dup 0<= if
121: drop
122: else
123: swap >r 1- recurse r> swap
124: then ;
125:
126: \ place bounds 13feb93py
127:
128: : place ( addr len to -- ) \ gforth
129: over >r rot over 1+ r> move c! ;
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}.
135: over + swap ;
136:
137: \ (word) 22feb93py
138:
139: : scan ( addr1 n1 char -- addr2 n2 ) \ gforth
140: \G skip all characters not equal to char
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
151: \G skip all characters equal to char
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
166: [ char A char 9 1 + - ] literal -
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 )
177: swap >r swap base @ um* drop rot base @ um* d+ r> ;
178:
179: : >number ( ud1 c-addr1 u1 -- ud2 c-addr2 u2 ) \ core to-number
180: \G Attempt to convert the character string @var{c-addr1 u1} to an
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.
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:
215: has? glocals [IF]
216: : lp@ ( -- addr ) \ gforth lp-fetch
217: laddr# [ 0 , ] ;
218: [THEN]
219:
220: has? os 0= [IF]
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]
227: defer catch ( x1 .. xn xt -- y1 .. ym 0 / z1 .. zn error ) \ exception
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:
234: :noname ( ... xt -- ... 0 )
235: execute 0 ;
236: is catch
237:
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 )
244: ?dup if
245: [ has? header [IF] here image-header 9 cells + ! [THEN] ]
246: cr DoError cr
247: [ has? file [IF] ] script? IF 1 (bye) ELSE quit THEN
248: [ [ELSE] ] quit [ [THEN] ]
249: then ;
250: is throw
251: [THEN]
252:
253: \ (abort")
254:
255: : c(abort") ( c-addr -- )
256: "error ! -2 throw ;
257:
258: : (abort")
259: "lit >r
260: IF
261: r> "error ! -2 throw
262: THEN
263: rdrop ;
264:
265: : abort ( ?? -- ?? ) \ core,exception-ext
266: \G @code{-1 throw}.
267: -1 throw ;
268:
269: \ ?stack 23feb93py
270:
271: : ?stack ( ?? -- ?? ) \ gforth
272: sp@ sp0 @ u> IF -4 throw THEN
273: [ has? floating [IF] ]
274: fp@ fp0 @ u> IF -&45 throw THEN
275: [ [THEN] ]
276: ;
277: \ ?stack should be code -- it touches an empty stack!
278:
279: \ DEPTH 9may93jaw
280:
281: : depth ( -- +n ) \ core depth
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.
284: sp@ sp0 @ swap - cell / ;
285:
286: : clearstack ( ... -- ) \ gforth clear-stack
287: \G remove and discard all/any items from the data stack.
288: sp0 @ sp! ;
289:
290: : clearstacks ( ... -- ) \ gforth clear-stacks
291: \G empty data and FP stack
292: clearstack
293: [ has? floating [IF] ]
294: fp0 @ fp!
295: [ [THEN] ]
296: ;
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
306: \G Set @code{base} to &10 (decimal). Don't use @code{decimal}, use
307: \G @code{base-execute} instead.
308: a base ! ;
309: : hex ( -- ) \ core-ext
310: \G Set @code{base} to &16 (hexadecimal). Don't use @code{hex},
311: \G use @code{base-execute} instead.
312: 10 base ! ;
313:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>