]> gitweb.factorcode.org Git - factor.git/blob - core/kernel/kernel.factor
Revert "VM: removing the unused callstack-bounds primitive"
[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 PRIMITIVE: -rot ( x y z -- z x y )
11 PRIMITIVE: dup ( x -- x x )
12 PRIMITIVE: dupd ( x y -- x x y )
13 PRIMITIVE: drop ( x -- )
14 PRIMITIVE: nip ( x y -- y )
15 PRIMITIVE: over ( x y -- x y x )
16 PRIMITIVE: pick ( x y z -- x y z x )
17 PRIMITIVE: rot ( x y z -- y z x )
18 PRIMITIVE: swap ( x y -- y x )
19 PRIMITIVE: swapd ( x y z -- y x z )
20 PRIMITIVE: 2drop ( x y -- )
21 PRIMITIVE: 2dup ( x y -- x y x y )
22 PRIMITIVE: 2nip ( x y z -- z )
23 PRIMITIVE: 3drop ( x y z -- )
24 PRIMITIVE: 3dup ( x y z -- x y z x y z )
25 PRIMITIVE: 4drop ( w x y z -- )
26 PRIMITIVE: 4dup ( w x y z -- w x y z w x y z )
27
28 PRIMITIVE: (clone) ( obj -- newobj )
29 PRIMITIVE: eq? ( obj1 obj2 -- ? )
30 PRIMITIVE: <wrapper> ( obj -- wrapper )
31 PRIMITIVE: die ( -- )
32 PRIMITIVE: callstack>array ( callstack -- array )
33
34 <PRIVATE
35 PRIMITIVE: (call) ( quot -- )
36 PRIMITIVE: (execute) ( word -- )
37 PRIMITIVE: (identity-hashcode) ( obj -- code )
38 PRIMITIVE: become ( old new -- )
39 PRIMITIVE: c-to-factor ( -- )
40 PRIMITIVE: callstack-bounds ( -- start end )
41 PRIMITIVE: check-datastack ( array in# out# -- ? )
42 PRIMITIVE: compute-identity-hashcode ( obj -- )
43 PRIMITIVE: context-object ( n -- obj )
44 PRIMITIVE: fpu-state ( -- )
45 PRIMITIVE: innermost-frame-executing ( callstack -- obj )
46 PRIMITIVE: innermost-frame-scan ( callstack -- n )
47 PRIMITIVE: lazy-jit-compile ( -- )
48 PRIMITIVE: leaf-signal-handler ( -- )
49 PRIMITIVE: set-callstack ( callstack -- * )
50 PRIMITIVE: set-context-object ( obj n -- )
51 PRIMITIVE: set-datastack ( array -- )
52 PRIMITIVE: set-fpu-state ( -- )
53 PRIMITIVE: set-innermost-frame-quotation ( n callstack -- )
54 PRIMITIVE: set-retainstack ( array -- )
55 PRIMITIVE: set-special-object ( obj n -- )
56 PRIMITIVE: signal-handler ( -- )
57 PRIMITIVE: special-object ( n -- obj )
58 PRIMITIVE: strip-stack-traces ( -- )
59 PRIMITIVE: tag ( object -- n )
60 PRIMITIVE: unwind-native-frames ( -- )
61 PRIVATE>
62
63 DEFER: dip
64 DEFER: 2dip
65 DEFER: 3dip
66
67 ! Stack stuff
68 : 2over ( x y z -- x y z x y ) pick pick ; inline
69
70 : clear ( -- ) { } set-datastack ;
71
72 ! Combinators
73 GENERIC: call ( callable -- )
74
75 GENERIC: execute ( word -- )
76
77 DEFER: if
78
79 : ? ( ? true false -- true/false )
80     ! 'if' and '?' can be defined in terms of each other
81     ! because the JIT special-cases an 'if' preceded by
82     ! two literal quotations.
83     rot [ drop ] [ nip ] if ; inline
84
85 : if ( ..a ? true: ( ..a -- ..b ) false: ( ..a -- ..b ) -- ..b ) ? call ;
86
87 ! Single branch
88 : unless ( ..a ? false: ( ..a -- ..a ) -- ..a )
89     swap [ drop ] [ call ] if ; inline
90
91 : when ( ..a ? true: ( ..a -- ..a ) -- ..a )
92     swap [ call ] [ drop ] if ; inline
93
94 ! Anaphoric
95 : if* ( ..a ? true: ( ..a ? -- ..b ) false: ( ..a -- ..b ) -- ..b )
96     pick [ drop call ] [ 2nip call ] if ; inline
97
98 : when* ( ..a ? true: ( ..a ? -- ..a ) -- ..a )
99     over [ call ] [ 2drop ] if ; inline
100
101 : unless* ( ..a ? false: ( ..a -- ..a x ) -- ..a x )
102     over [ drop ] [ nip call ] if ; inline
103
104 ! Default
105 : ?if ( ..a default cond true: ( ..a cond -- ..b ) false: ( ..a default -- ..b ) -- ..b )
106     pick [ drop [ drop ] 2dip call ] [ 2nip call ] if ; inline
107
108 ! Dippers.
109 ! Not declared inline because the compiler special-cases them
110
111 : dip ( x quot -- x ) swap [ call ] dip ;
112
113 : 2dip ( x y quot -- x y ) swap [ dip ] dip ;
114
115 : 3dip ( x y z quot -- x y z ) swap [ 2dip ] dip ;
116
117 : 4dip ( w x y z quot -- w x y z ) swap [ 3dip ] dip ; inline
118
119 ! Keepers
120 : keep ( ..a x quot: ( ..a x -- ..b ) -- ..b x )
121     over [ call ] dip ; inline
122
123 : 2keep ( ..a x y quot: ( ..a x y -- ..b ) -- ..b x y )
124     [ 2dup ] dip 2dip ; inline
125
126 : 3keep ( ..a x y z quot: ( ..a x y z -- ..b ) -- ..b x y z )
127     [ 3dup ] dip 3dip ; inline
128
129 : 4keep ( ..a w x y z quot: ( ..a w x y z -- ..b ) -- ..b w x y z )
130     [ 4dup ] dip 4dip ; inline
131
132 ! Cleavers
133 : bi ( x p q -- )
134     [ keep ] dip call ; inline
135
136 : tri ( x p q r -- )
137     [ [ keep ] dip keep ] dip call ; inline
138
139 ! Double cleavers
140 : 2bi ( x y p q -- )
141     [ 2keep ] dip call ; inline
142
143 : 2tri ( x y p q r -- )
144     [ [ 2keep ] dip 2keep ] dip call ; inline
145
146 ! Triple cleavers
147 : 3bi ( x y z p q -- )
148     [ 3keep ] dip call ; inline
149
150 : 3tri ( x y z p q r -- )
151     [ [ 3keep ] dip 3keep ] dip call ; inline
152
153 ! Spreaders
154 : bi* ( x y p q -- )
155     [ dip ] dip call ; inline
156
157 : tri* ( x y z p q r -- )
158     [ [ 2dip ] dip dip ] dip call ; inline
159
160 ! Double spreaders
161 : 2bi* ( w x y z p q -- )
162     [ 2dip ] dip call ; inline
163
164 : 2tri* ( u v w x y z p q r -- )
165     [ 4dip ] 2dip 2bi* ; inline
166
167 ! Appliers
168 : bi@ ( x y quot -- )
169     dup bi* ; inline
170
171 : tri@ ( x y z quot -- )
172     dup dup tri* ; inline
173
174 ! Double appliers
175 : 2bi@ ( w x y z quot -- )
176     dup 2bi* ; inline
177
178 : 2tri@ ( u v w x y z quot -- )
179     dup dup 2tri* ; inline
180
181 ! Quotation building
182 : 2curry ( obj1 obj2 quot -- curried )
183     curry curry ; inline
184
185 : 3curry ( obj1 obj2 obj3 quot -- curried )
186     curry curry curry ; inline
187
188 : with ( param obj quot -- obj curried )
189     swapd [ swapd call ] 2curry ; inline
190
191 : 2with ( param1 param2 obj quot -- obj curried )
192     with with ; inline
193
194 : prepose ( quot1 quot2 -- composed )
195     swap compose ; inline
196
197 ! Curried cleavers
198 <PRIVATE
199
200 : currier ( quot -- quot' ) [ curry ] curry ; inline
201
202 PRIVATE>
203
204 : bi-curry ( x p q -- p' q' ) [ currier ] bi@ bi ; inline
205
206 : tri-curry ( x p q r -- p' q' r' ) [ currier ] tri@ tri ; inline
207
208 : bi-curry* ( x y p q -- p' q' ) [ currier ] bi@ bi* ; inline
209
210 : tri-curry* ( x y z p q r -- p' q' r' ) [ currier ] tri@ tri* ; inline
211
212 : bi-curry@ ( x y q -- p' q' ) currier bi@ ; inline
213
214 : tri-curry@ ( x y z q -- p' q' r' ) currier tri@ ; inline
215
216 ! Booleans
217 UNION: boolean POSTPONE: t POSTPONE: f ;
218
219 : >boolean ( obj -- ? ) [ t ] [ f ] if ; inline
220
221 : not ( obj -- ? ) [ f ] [ t ] if ; inline
222
223 : and ( obj1 obj2 -- ? ) over ? ; inline
224
225 : or ( obj1 obj2 -- ? ) dupd ? ; inline
226
227 : xor ( obj1 obj2 -- ? ) [ f swap ? ] when* ; inline
228
229 : both? ( x y quot -- ? ) bi@ and ; inline
230
231 : either? ( x y quot -- ? ) bi@ or ; inline
232
233 : most ( x y quot -- z ) 2keep ? ; inline
234
235 ! Loops
236 : loop ( ... pred: ( ... -- ... ? ) -- ... )
237     [ call ] keep [ loop ] curry when ; inline recursive
238
239 : do ( pred body -- pred body )
240     dup 2dip ; inline
241
242 : while ( ..a pred: ( ..a -- ..b ? ) body: ( ..b -- ..a ) -- ..b )
243     swap do compose [ loop ] curry when ; inline
244
245 : until ( ..a pred: ( ..a -- ..b ? ) body: ( ..b -- ..a ) -- ..b )
246     [ [ not ] compose ] dip while ; inline
247
248 ! Object protocol
249 GENERIC: hashcode* ( depth obj -- code )
250
251 M: object hashcode* 2drop 0 ; inline
252
253 M: f hashcode* 2drop 31337 ; inline
254
255 : hashcode ( obj -- code ) 3 swap hashcode* ; inline
256
257 : identity-hashcode ( obj -- code )
258     dup tag 0 eq? [
259         dup tag 1 eq? [ drop 0 ] [
260             dup (identity-hashcode) dup 0 eq? [
261                 drop dup compute-identity-hashcode
262                 (identity-hashcode)
263             ] [ nip ] if
264         ] if
265     ] unless ; inline
266
267 GENERIC: equal? ( obj1 obj2 -- ? )
268
269 M: object equal? 2drop f ; inline
270
271 TUPLE: identity-tuple ;
272
273 M: identity-tuple equal? 2drop f ; inline
274
275 M: identity-tuple hashcode* nip identity-hashcode ; inline
276
277 : = ( obj1 obj2 -- ? )
278     2dup eq? [ 2drop t ] [
279         2dup both-fixnums? [ 2drop f ] [ equal? ] if
280     ] if ; inline
281
282 : same? ( x y quot -- ? ) bi@ = ; inline
283
284 GENERIC: clone ( obj -- cloned )
285
286 M: object clone ; inline
287
288 M: callstack clone (clone) ; inline
289
290 ! Tuple construction
291 GENERIC: new ( class -- tuple )
292
293 GENERIC: boa ( slots... class -- tuple )
294
295 ! Error handling -- defined early so that other files can
296 ! throw errors before continuations are loaded
297 GENERIC: throw ( error -- * )
298
299 ERROR: assert got expect ;
300
301 : assert= ( a b -- ) 2dup = [ 2drop ] [ assert ] if ;
302
303 <PRIVATE
304
305 : declare ( spec -- ) drop ;
306
307 : do-primitive ( number -- ) "Improper primitive call" throw ;
308
309 ! Special object count and identifiers must be kept in sync with:
310 !   vm/objects.hpp
311 CONSTANT: special-object-count 85
312
313 CONSTANT: OBJ-WALKER-HOOK 3
314
315 CONSTANT: OBJ-CALLCC-1 4
316
317 CONSTANT: ERROR-HANDLER-QUOT 5
318
319 CONSTANT: OBJ-CELL-SIZE 7
320 CONSTANT: OBJ-CPU 8
321 CONSTANT: OBJ-OS 9
322
323 CONSTANT: OBJ-ARGS 10
324 CONSTANT: OBJ-STDIN 11
325 CONSTANT: OBJ-STDOUT 12
326
327 CONSTANT: OBJ-IMAGE 13
328 CONSTANT: OBJ-EXECUTABLE 14
329
330 CONSTANT: OBJ-EMBEDDED 15
331 CONSTANT: OBJ-EVAL-CALLBACK 16
332 CONSTANT: OBJ-YIELD-CALLBACK 17
333 CONSTANT: OBJ-SLEEP-CALLBACK 18
334
335 CONSTANT: OBJ-STARTUP-QUOT 20
336 CONSTANT: OBJ-GLOBAL 21
337 CONSTANT: OBJ-SHUTDOWN-QUOT 22
338
339 CONSTANT: JIT-PROLOG 23
340 CONSTANT: JIT-PRIMITIVE-WORD 24
341 CONSTANT: JIT-PRIMITIVE 25
342 CONSTANT: JIT-WORD-JUMP 26
343 CONSTANT: JIT-WORD-CALL 27
344 CONSTANT: JIT-IF-WORD 28
345 CONSTANT: JIT-IF 29
346 CONSTANT: JIT-SAFEPOINT 30
347 CONSTANT: JIT-EPILOG 31
348 CONSTANT: JIT-RETURN 32
349 CONSTANT: JIT-UNUSED 33
350 CONSTANT: JIT-PUSH-LITERAL 34
351 CONSTANT: JIT-DIP-WORD 35
352 CONSTANT: JIT-DIP 36
353 CONSTANT: JIT-2DIP-WORD 37
354 CONSTANT: JIT-2DIP 38
355 CONSTANT: JIT-3DIP-WORD 39
356 CONSTANT: JIT-3DIP 40
357 CONSTANT: JIT-EXECUTE 41
358 CONSTANT: JIT-DECLARE-WORD 42
359
360 CONSTANT: C-TO-FACTOR-WORD 43
361 CONSTANT: LAZY-JIT-COMPILE-WORD 44
362 CONSTANT: UNWIND-NATIVE-FRAMES-WORD 45
363 CONSTANT: GET-FPU-STATE-WORD 46
364 CONSTANT: SET-FPU-STATE-WORD 47
365 CONSTANT: SIGNAL-HANDLER-WORD 48
366 CONSTANT: LEAF-SIGNAL-HANDLER-WORD 49
367 CONSTANT: WIN-EXCEPTION-HANDLER 50
368
369 CONSTANT: OBJ-SAMPLE-CALLSTACKS 51
370
371 CONSTANT: REDEFINITION-COUNTER 52
372
373 CONSTANT: CALLBACK-STUB 53
374
375 CONSTANT: PIC-LOAD 54
376 CONSTANT: PIC-TAG 55
377 CONSTANT: PIC-TUPLE 56
378 CONSTANT: PIC-CHECK-TAG 57
379 CONSTANT: PIC-CHECK-TUPLE 58
380 CONSTANT: PIC-HIT 59
381 CONSTANT: PIC-MISS-WORD 60
382 CONSTANT: PIC-MISS-TAIL-WORD 61
383
384 CONSTANT: MEGA-LOOKUP 62
385 CONSTANT: MEGA-LOOKUP-WORD 63
386 CONSTANT: MEGA-MISS-WORD 64
387
388 CONSTANT: OBJ-UNDEFINED 65
389
390 CONSTANT: OBJ-STDERR 66
391
392 CONSTANT: OBJ-STAGE2 67
393
394 CONSTANT: OBJ-CURRENT-THREAD 68
395
396 CONSTANT: OBJ-THREADS 69
397 CONSTANT: OBJ-RUN-QUEUE 70
398 CONSTANT: OBJ-SLEEP-QUEUE 71
399
400 CONSTANT: OBJ-VM-COMPILER 72
401
402 CONSTANT: OBJ-WAITING-CALLBACKS 73
403
404 CONSTANT: OBJ-SIGNAL-PIPE 74
405
406 CONSTANT: OBJ-VM-COMPILE-TIME 75
407
408 CONSTANT: OBJ-VM-VERSION 76
409 CONSTANT: OBJ-VM-GIT-LABEL 77
410
411 CONSTANT: OBJ-CANONICAL-TRUE 78
412
413 CONSTANT: OBJ-BIGNUM-ZERO 79
414 CONSTANT: OBJ-BIGNUM-POS-ONE 80
415 CONSTANT: OBJ-BIGNUM-NEG-ONE 81
416
417 ! Context object count and identifiers must be kept in sync with:
418 !   vm/contexts.hpp
419
420 CONSTANT: context-object-count 4
421
422 CONSTANT: CONTEXT-OBJ-NAMESTACK 0
423 CONSTANT: CONTEXT-OBJ-CATCHSTACK 1
424 CONSTANT: CONTEXT-OBJ-CONTEXT 2
425 CONSTANT: CONTEXT-OBJ-IN-CALLBACK-P 3
426
427 ! Runtime errors must be kept in sync with:
428 !   basis/debugger/debugger.factor
429 !   vm/errors.hpp
430
431 ! VM adds this to kernel errors, so that user-space can identify them.
432 CONSTANT: KERNEL-ERROR 0xfac7
433
434 CONSTANT: kernel-error-count 20
435
436 CONSTANT: ERROR-EXPIRED 0
437 CONSTANT: ERROR-IO      1
438 CONSTANT: ERROR-NOT-IMPLEMENTED 2
439 CONSTANT: ERROR-TYPE 3
440 CONSTANT: ERROR-DIVIDE-BY-ZERO 4
441 CONSTANT: ERROR-SIGNAL 5
442 CONSTANT: ERROR-ARRAY-SIZE 6
443 CONSTANT: ERROR-OUT-OF-FIXNUM-RANGE 7
444 CONSTANT: ERROR-FFI 8
445 CONSTANT: ERROR-UNDEFINED-SYMBOL 9
446 CONSTANT: ERROR-DATASTACK-UNDERFLOW 10
447 CONSTANT: ERROR-DATASTACK-OVERFLOW 11
448 CONSTANT: ERROR-RETAINSTACK-UNDERFLOW 12
449 CONSTANT: ERROR-RETAINSTACK-OVERFLOW 13
450 CONSTANT: ERROR-CALLSTACK-UNDERFLOW 14
451 CONSTANT: ERROR-CALLSTACK-OVERFLOW 15
452 CONSTANT: ERROR-MEMORY 16
453 CONSTANT: ERROR-FP-TRAP 17
454 CONSTANT: ERROR-INTERRUPT 18
455 CONSTANT: ERROR-CALLBACK-SPACE-OVERFLOW 19
456
457 PRIMITIVE: callstack-for ( context -- array )
458 PRIMITIVE: retainstack-for ( context -- array )
459 PRIMITIVE: datastack-for ( context -- array )
460
461 : context ( -- context )
462     CONTEXT-OBJ-CONTEXT context-object ; inline
463
464 PRIVATE>
465
466 : get-callstack ( -- callstack )
467     context callstack-for ; inline
468
469 : get-datastack ( -- array )
470     context datastack-for ; inline
471
472 : get-retainstack ( -- array )
473     context retainstack-for ; inline