]> gitweb.factorcode.org Git - factor.git/blob - core/kernel/kernel.factor
kernel: symbolic constants for the various kernel errors
[factor.git] / core / kernel / kernel.factor
1 ! Copyright (C) 2004, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel.private slots.private math.private ;
4 IN: kernel
5
6 BUILTIN: callstack ;
7 BUILTIN: tuple ;
8 BUILTIN: wrapper { wrapped read-only } ;
9
10 DEFER: dip
11 DEFER: 2dip
12 DEFER: 3dip
13
14 ! Stack stuff
15 : 2over ( x y z -- x y z x y ) pick pick ; inline
16
17 : clear ( -- ) { } set-datastack ;
18
19 ! Combinators
20 GENERIC: call ( callable -- )
21
22 GENERIC: execute ( word -- )
23
24 GENERIC: ?execute ( word -- value )
25
26 M: object ?execute ;
27
28 DEFER: if
29
30 : ? ( ? true false -- true/false )
31     #! 'if' and '?' can be defined in terms of each other
32     #! because the JIT special-cases an 'if' preceeded by
33     #! two literal quotations.
34     rot [ drop ] [ nip ] if ; inline
35
36 : if ( ..a ? true: ( ..a -- ..b ) false: ( ..a -- ..b ) -- ..b ) ? call ;
37
38 ! Single branch
39 : unless ( ..a ? false: ( ..a -- ..a ) -- ..a )
40     swap [ drop ] [ call ] if ; inline
41
42 : when ( ..a ? true: ( ..a -- ..a ) -- ..a )
43     swap [ call ] [ drop ] if ; inline
44
45 ! Anaphoric
46 : if* ( ..a ? true: ( ..a ? -- ..b ) false: ( ..a -- ..b ) -- ..b )
47     pick [ drop call ] [ 2nip call ] if ; inline
48
49 : when* ( ..a ? true: ( ..a ? -- ..a ) -- ..a )
50     over [ call ] [ 2drop ] if ; inline
51
52 : unless* ( ..a ? false: ( ..a -- ..a x ) -- ..a x )
53     over [ drop ] [ nip call ] if ; inline
54
55 ! Default
56 : ?if ( ..a default cond true: ( ..a cond -- ..b ) false: ( ..a default -- ..b ) -- ..b )
57     pick [ drop [ drop ] 2dip call ] [ 2nip call ] if ; inline
58
59 ! Dippers.
60 ! Not declared inline because the compiler special-cases them
61
62 : dip ( x quot -- x ) swap [ call ] dip ;
63
64 : 2dip ( x y quot -- x y ) swap [ dip ] dip ;
65
66 : 3dip ( x y z quot -- x y z ) swap [ 2dip ] dip ;
67
68 : 4dip ( w x y z quot -- w x y z ) swap [ 3dip ] dip ; inline
69
70 ! Keepers
71 : keep ( ..a x quot: ( ..a x -- ..b ) -- ..b x )
72     over [ call ] dip ; inline
73
74 : 2keep ( ..a x y quot: ( ..a x y -- ..b ) -- ..b x y )
75     [ 2dup ] dip 2dip ; inline
76
77 : 3keep ( ..a x y z quot: ( ..a x y z -- ..b ) -- ..b x y z )
78     [ 3dup ] dip 3dip ; inline
79
80 : 4keep ( ..a w x y z quot: ( ..a w x y z -- ..b ) -- ..b w x y z )
81     [ 4dup ] dip 4dip ; inline
82
83 ! Cleavers
84 : bi ( x p q -- )
85     [ keep ] dip call ; inline
86
87 : tri ( x p q r -- )
88     [ [ keep ] dip keep ] dip call ; inline
89
90 ! Double cleavers
91 : 2bi ( x y p q -- )
92     [ 2keep ] dip call ; inline
93
94 : 2tri ( x y p q r -- )
95     [ [ 2keep ] dip 2keep ] dip call ; inline
96
97 ! Triple cleavers
98 : 3bi ( x y z p q -- )
99     [ 3keep ] dip call ; inline
100
101 : 3tri ( x y z p q r -- )
102     [ [ 3keep ] dip 3keep ] dip call ; inline
103
104 ! Spreaders
105 : bi* ( x y p q -- )
106     [ dip ] dip call ; inline
107
108 : tri* ( x y z p q r -- )
109     [ [ 2dip ] dip dip ] dip call ; inline
110
111 ! Double spreaders
112 : 2bi* ( w x y z p q -- )
113     [ 2dip ] dip call ; inline
114
115 : 2tri* ( u v w x y z p q r -- )
116     [ 4dip ] 2dip 2bi* ; inline
117
118 ! Appliers
119 : bi@ ( x y quot -- )
120     dup bi* ; inline
121
122 : tri@ ( x y z quot -- )
123     dup dup tri* ; inline
124
125 ! Double appliers
126 : 2bi@ ( w x y z quot -- )
127     dup 2bi* ; inline
128
129 : 2tri@ ( u v w x y z quot -- )
130     dup dup 2tri* ; inline
131
132 ! Quotation building
133 : 2curry ( obj1 obj2 quot -- curry )
134     curry curry ; inline
135
136 : 3curry ( obj1 obj2 obj3 quot -- curry )
137     curry curry curry ; inline
138
139 : with ( param obj quot -- obj curry )
140     swapd [ swapd call ] 2curry ; inline
141
142 : prepose ( quot1 quot2 -- compose )
143     swap compose ; inline
144
145 ! Curried cleavers
146 <PRIVATE
147
148 : [curry] ( quot -- quot' ) [ curry ] curry ; inline
149
150 PRIVATE>
151
152 : bi-curry ( x p q -- p' q' ) [ [curry] ] bi@ bi ; inline
153
154 : tri-curry ( x p q r -- p' q' r' ) [ [curry] ] tri@ tri ; inline
155
156 : bi-curry* ( x y p q -- p' q' ) [ [curry] ] bi@ bi* ; inline
157
158 : tri-curry* ( x y z p q r -- p' q' r' ) [ [curry] ] tri@ tri* ; inline
159
160 : bi-curry@ ( x y q -- p' q' ) [curry] bi@ ; inline
161
162 : tri-curry@ ( x y z q -- p' q' r' ) [curry] tri@ ; inline
163
164 ! Booleans
165 UNION: boolean POSTPONE: t POSTPONE: f ;
166
167 : >boolean ( obj -- ? ) [ t ] [ f ] if ; inline
168
169 : not ( obj -- ? ) [ f ] [ t ] if ; inline
170
171 : and ( obj1 obj2 -- ? ) over ? ; inline
172
173 : or ( obj1 obj2 -- ? ) dupd ? ; inline
174
175 : xor ( obj1 obj2 -- ? ) [ f swap ? ] when* ; inline
176
177 : both? ( x y quot -- ? ) bi@ and ; inline
178
179 : either? ( x y quot -- ? ) bi@ or ; inline
180
181 : most ( x y quot -- z ) 2keep ? ; inline
182
183 ! Loops
184 : loop ( ... pred: ( ... -- ... ? ) -- ... )
185     [ call ] keep [ loop ] curry when ; inline recursive
186
187 : do ( pred body -- pred body )
188     dup 2dip ; inline
189
190 : while ( ..a pred: ( ..a -- ..b ? ) body: ( ..b -- ..a ) -- ..b )
191     swap do compose [ loop ] curry when ; inline
192
193 : until ( ..a pred: ( ..a -- ..b ? ) body: ( ..b -- ..a ) -- ..b )
194     [ [ not ] compose ] dip while ; inline
195
196 ! Object protocol
197 GENERIC: hashcode* ( depth obj -- code )
198
199 M: object hashcode* 2drop 0 ; inline
200
201 M: f hashcode* 2drop 31337 ; inline
202
203 : hashcode ( obj -- code ) 3 swap hashcode* ; inline
204
205 : identity-hashcode ( obj -- code )
206     dup tag 0 eq? [
207         dup tag 1 eq? [ drop 0 ] [
208             dup (identity-hashcode) dup 0 eq? [
209                 drop dup compute-identity-hashcode
210                 (identity-hashcode)
211             ] [ nip ] if
212         ] if
213     ] unless ; inline
214
215 GENERIC: equal? ( obj1 obj2 -- ? )
216
217 M: object equal? 2drop f ; inline
218
219 TUPLE: identity-tuple ;
220
221 M: identity-tuple equal? 2drop f ; inline
222
223 M: identity-tuple hashcode* nip identity-hashcode ; inline
224
225 : = ( obj1 obj2 -- ? )
226     2dup eq? [ 2drop t ] [
227         2dup both-fixnums? [ 2drop f ] [ equal? ] if
228     ] if ; inline
229
230 : same? ( x y quot -- ? ) bi@ = ; inline
231
232 GENERIC: clone ( obj -- cloned )
233
234 M: object clone ; inline
235
236 M: callstack clone (clone) ; inline
237
238 ! Tuple construction
239 GENERIC: new ( class -- tuple )
240
241 GENERIC: boa ( slots... class -- tuple )
242
243 ! Error handling -- defined early so that other files can
244 ! throw errors before continuations are loaded
245 GENERIC: throw ( error -- * )
246
247 ERROR: assert got expect ;
248
249 : assert= ( a b -- ) 2dup = [ 2drop ] [ assert ] if ;
250
251 <PRIVATE
252
253 : declare ( spec -- ) drop ;
254
255 : do-primitive ( number -- ) "Improper primitive call" throw ;
256
257 ! Special object count and identifiers must be kept in sync with:
258 !   vm/objects.hpp
259 !   basis/bootstrap/image/image.factor
260
261 CONSTANT: special-object-count 80
262
263 CONSTANT: OBJ-WALKER-HOOK 3
264
265 CONSTANT: OBJ-CALLCC-1 4
266
267 CONSTANT: ERROR-HANDLER-QUOT 5
268 CONSTANT: OBJ-ERROR 6
269
270 CONSTANT: OBJ-CELL-SIZE 7
271 CONSTANT: OBJ-CPU 8
272 CONSTANT: OBJ-OS 9
273
274 CONSTANT: OBJ-ARGS 10
275 CONSTANT: OBJ-STDIN 11
276 CONSTANT: OBJ-STDOUT 12
277
278 CONSTANT: OBJ-IMAGE 13
279 CONSTANT: OBJ-EXECUTABLE 14
280
281 CONSTANT: OBJ-EMBEDDED 15
282 CONSTANT: OBJ-EVAL-CALLBACK 16
283 CONSTANT: OBJ-YIELD-CALLBACK 17
284 CONSTANT: OBJ-SLEEP-CALLBACK 18
285
286 CONSTANT: OBJ-STARTUP-QUOT 20
287 CONSTANT: OBJ-GLOBAL 21
288 CONSTANT: OBJ-SHUTDOWN-QUOT 22
289
290 CONSTANT: JIT-PROLOG 23
291 CONSTANT: JIT-PRIMITIVE-WORD 24
292 CONSTANT: JIT-PRIMITIVE 25
293 CONSTANT: JIT-WORD-JUMP 26
294 CONSTANT: JIT-WORD-CALL 27
295 CONSTANT: JIT-IF-WORD 28
296 CONSTANT: JIT-IF 29
297 CONSTANT: JIT-SAFEPOINT 30
298 CONSTANT: JIT-EPILOG 31
299 CONSTANT: JIT-RETURN 32
300 CONSTANT: JIT-PROFILING 33
301 CONSTANT: JIT-PUSH-IMMEDIATE 34
302 CONSTANT: JIT-DIP-WORD 35
303 CONSTANT: JIT-DIP 36
304 CONSTANT: JIT-2DIP-WORD 37
305 CONSTANT: JIT-2DIP 38
306 CONSTANT: JIT-3DIP-WORD 39
307 CONSTANT: JIT-3DIP 40
308 CONSTANT: JIT-EXECUTE 41
309 CONSTANT: JIT-DECLARE-WORD 42
310
311 CONSTANT: C-TO-FACTOR-WORD 43
312 CONSTANT: LAZY-JIT-COMPILE-WORD 44
313 CONSTANT: UNWIND-NATIVE-FRAMES-WORD 45
314 CONSTANT: GET-FPU-STATE-WORD 46
315 CONSTANT: SET-FPU-STATE-WORD 47
316 CONSTANT: SIGNAL-HANDLER-WORD 48
317 CONSTANT: LEAF-SIGNAL-HANDLER-WORD 49
318 CONSTANT: FFI-SIGNAL-HANDLER-WORD 50
319 CONSTANT: FFI-LEAF-SIGNAL-HANDLER-WORD 51
320
321 CONSTANT: REDEFINITION-COUNTER 52
322
323 CONSTANT: CALLBACK-STUB 53
324
325 CONSTANT: PIC-LOAD 54
326 CONSTANT: PIC-TAG 55
327 CONSTANT: PIC-TUPLE 56
328 CONSTANT: PIC-CHECK-TAG 57
329 CONSTANT: PIC-CHECK-TUPLE 58
330 CONSTANT: PIC-HIT 59
331 CONSTANT: PIC-MISS-WORD 60
332 CONSTANT: PIC-MISS-TAIL-WORD 61
333
334 CONSTANT: MEGA-LOOKUP 62
335 CONSTANT: MEGA-LOOKUP-WORD 63
336 CONSTANT: MEGA-MISS-WORD 64
337
338 CONSTANT: OBJ-UNDEFINED 65
339
340 CONSTANT: OBJ-STDERR 66
341
342 CONSTANT: OBJ-STAGE2 67
343
344 CONSTANT: OBJ-CURRENT-THREAD 68
345
346 CONSTANT: OBJ-THREADS 69
347 CONSTANT: OBJ-RUN-QUEUE 70
348 CONSTANT: OBJ-SLEEP-QUEUE 71
349
350 CONSTANT: OBJ-VM-COMPILER 72
351
352 CONSTANT: OBJ-WAITING-CALLBACKS 73
353
354 CONSTANT: OBJ-SIGNAL-PIPE 74
355
356 ! Context object count and identifiers must be kept in sync with:
357 !   vm/contexts.hpp
358
359 CONSTANT: context-object-count 10
360
361 CONSTANT: CONTEXT-OBJ-NAMESTACK 0
362 CONSTANT: CONTEXT-OBJ-CATCHSTACK 1
363 CONSTANT: CONTEXT-OBJ-CONTEXT 2
364 CONSTANT: CONTEXT-OBJ-IN-CALLBACK-P 3
365
366 PRIVATE>
367
368 ! Runtime errors must be kept in sync with:
369 !   vm/errors.hpp
370 CONSTANT: kernel-error-count 19
371 CONSTANT: ERROR-EXPIRED 0
372 CONSTANT: ERROR-IO      1
373 CONSTANT: ERROR-NOT-IMPLEMENTED 2
374 CONSTANT: ERROR-TYPE 3
375 CONSTANT: ERROR-DIVIDE-BY-ZERO 4
376 CONSTANT: ERROR-SIGNAL 5
377 CONSTANT: ERROR-ARRAY-SIZE 6
378 CONSTANT: ERROR-C-STRING 7
379 CONSTANT: ERROR-FFI 8
380 CONSTANT: ERROR-UNDEFINED-SYMBOL 9
381 CONSTANT: ERROR-DATASTACK-UNDERFLOW 10
382 CONSTANT: ERROR-DATASTACK-OVERFLOW 11
383 CONSTANT: ERROR-RETAINSTACK-UNDERFLOW 12
384 CONSTANT: ERROR-RETAINSTACK-OVERFLOW 13
385 CONSTANT: ERROR-CALLSTACK-UNDERFLOW 14
386 CONSTANT: ERROR-CALLSTACK-OVERFLOW 15
387 CONSTANT: ERROR-MEMORY 16
388 CONSTANT: ERROR-FP-TRAP 17
389 CONSTANT: ERROR-INTERRUPT 18