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 ;
8 BUILTIN: wrapper { wrapped read-only } ;
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 )
28 PRIMITIVE: (clone) ( obj -- newobj )
29 PRIMITIVE: eq? ( obj1 obj2 -- ? )
30 PRIMITIVE: <wrapper> ( obj -- wrapper )
32 PRIMITIVE: callstack>array ( callstack -- array )
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 ( -- )
68 : 2over ( x y z -- x y z x y ) pick pick ; inline
70 : clear ( -- ) { } set-datastack ;
73 GENERIC: call ( callable -- )
75 GENERIC: execute ( word -- )
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
85 : if ( ..a ? true: ( ..a -- ..b ) false: ( ..a -- ..b ) -- ..b ) ? call ;
88 : unless ( ..a ? false: ( ..a -- ..a ) -- ..a )
89 swap [ drop ] [ call ] if ; inline
91 : when ( ..a ? true: ( ..a -- ..a ) -- ..a )
92 swap [ call ] [ drop ] if ; inline
95 : if* ( ..a ? true: ( ..a ? -- ..b ) false: ( ..a -- ..b ) -- ..b )
96 pick [ drop call ] [ 2nip call ] if ; inline
98 : when* ( ..a ? true: ( ..a ? -- ..a ) -- ..a )
99 over [ call ] [ 2drop ] if ; inline
101 : unless* ( ..a ? false: ( ..a -- ..a x ) -- ..a x )
102 over [ drop ] [ nip call ] if ; inline
105 : ?if ( ..a default cond true: ( ..a cond -- ..b ) false: ( ..a default -- ..b ) -- ..b )
106 pick [ drop [ drop ] 2dip call ] [ 2nip call ] if ; inline
109 ! Not declared inline because the compiler special-cases them
111 : dip ( x quot -- x ) swap [ call ] dip ;
113 : 2dip ( x y quot -- x y ) swap [ dip ] dip ;
115 : 3dip ( x y z quot -- x y z ) swap [ 2dip ] dip ;
117 : 4dip ( w x y z quot -- w x y z ) swap [ 3dip ] dip ; inline
120 : tuck ( x y -- y x y ) dup -rot ; inline
122 : spin ( x y z -- z y x ) -rot swap ; inline
124 : rotd ( w x y z -- x y w z ) [ rot ] dip ; inline
126 : -rotd ( w x y z -- w z x y ) [ -rot ] dip ; inline
128 : roll ( w x y z -- x y z w ) rotd swap ; inline
130 : -roll ( w x y z -- z w x y ) swap -rotd ; inline
132 : nipd ( x y z -- y z ) [ nip ] dip ; inline
134 : overd ( x y z -- x y x z ) [ over ] dip ; inline
136 : pickd ( w x y z -- w x y w z ) [ pick ] dip ; inline
138 : 2nipd ( w x y z -- y z ) [ 2drop ] 2dip ; inline
140 : 3nipd ( v w x y z -- y z ) [ 3drop ] 2dip ; inline
142 : 3nip ( w x y z -- z ) 2nip nip ; inline
144 : 4nip ( v w x y z -- z ) 2nip 2nip ; inline
146 : 5nip ( u v w x y z -- z ) 3nip 2nip ; inline
148 : 5drop ( v w x y z -- ) 4drop drop ; inline
150 : reach ( w x y z -- w x y z w ) [ pick ] dip swap ; inline
153 : keep ( ..a x quot: ( ..a x -- ..b ) -- ..b x )
154 over [ call ] dip ; inline
156 : 2keep ( ..a x y quot: ( ..a x y -- ..b ) -- ..b x y )
157 [ 2dup ] dip 2dip ; inline
159 : 3keep ( ..a x y z quot: ( ..a x y z -- ..b ) -- ..b x y z )
160 [ 3dup ] dip 3dip ; inline
162 : 4keep ( ..a w x y z quot: ( ..a w x y z -- ..b ) -- ..b w x y z )
163 [ 4dup ] dip 4dip ; inline
165 : keepd ( ..a x y quot: ( ..a x y -- ..b x ) -- ..b x )
168 : keepdd ( ..a x y z quot: ( ..a x y z -- ..b x ) -- ..b x )
171 : 2keepd ( ..a x y z quot: ( ..a x y z -- ..b x y ) -- ..b x y )
176 [ keep ] dip call ; inline
179 [ [ keep ] dip keep ] dip call ; inline
183 [ 2keep ] dip call ; inline
185 : 2tri ( x y p q r -- )
186 [ [ 2keep ] dip 2keep ] dip call ; inline
189 : 3bi ( x y z p q -- )
190 [ 3keep ] dip call ; inline
192 : 3tri ( x y z p q r -- )
193 [ [ 3keep ] dip 3keep ] dip call ; inline
197 [ dip ] dip call ; inline
199 : tri* ( x y z p q r -- )
200 [ [ 2dip ] dip dip ] dip call ; inline
203 : 2bi* ( w x y z p q -- )
204 [ 2dip ] dip call ; inline
206 : 2tri* ( u v w x y z p q r -- )
207 [ 4dip ] 2dip 2bi* ; inline
210 : bi@ ( x y quot -- )
213 : tri@ ( x y z quot -- )
214 dup dup tri* ; inline
217 : 2bi@ ( w x y z quot -- )
220 : 2tri@ ( u v w x y z quot -- )
221 dup dup 2tri* ; inline
224 : 2curry ( obj1 obj2 quot -- curried )
227 : 3curry ( obj1 obj2 obj3 quot -- curried )
228 curry curry curry ; inline
230 : with ( param obj quot -- obj curried )
231 swapd [ swapd call ] 2curry ; inline
233 : 2with ( param1 param2 obj quot -- obj curried )
236 : prepose ( quot1 quot2 -- composed )
237 swap compose ; inline
242 : currier ( quot -- quot' ) [ curry ] curry ; inline
246 : bi-curry ( x p q -- p' q' ) [ currier ] bi@ bi ; inline
248 : tri-curry ( x p q r -- p' q' r' ) [ currier ] tri@ tri ; inline
250 : bi-curry* ( x y p q -- p' q' ) [ currier ] bi@ bi* ; inline
252 : tri-curry* ( x y z p q r -- p' q' r' ) [ currier ] tri@ tri* ; inline
254 : bi-curry@ ( x y q -- p' q' ) currier bi@ ; inline
256 : tri-curry@ ( x y z q -- p' q' r' ) currier tri@ ; inline
259 UNION: boolean POSTPONE: t POSTPONE: f ;
261 : >boolean ( obj -- ? ) [ t ] [ f ] if ; inline
263 : not ( obj -- ? ) [ f ] [ t ] if ; inline
265 : and ( obj1 obj2 -- ? ) over ? ; inline
267 : or ( obj1 obj2 -- ? ) dupd ? ; inline
269 : xor ( obj1 obj2 -- ? ) [ f swap ? ] when* ; inline
271 : both? ( x y quot -- ? ) bi@ and ; inline
273 : either? ( x y quot -- ? ) bi@ or ; inline
275 : most ( x y quot -- z ) 2keep ? ; inline
278 : loop ( ... pred: ( ... -- ... ? ) -- ... )
279 [ call ] keep [ loop ] curry when ; inline recursive
281 : do ( pred body -- pred body )
284 : while ( ..a pred: ( ..a -- ..b ? ) body: ( ..b -- ..a ) -- ..b )
285 swap do compose [ loop ] curry when ; inline
287 : until ( ..a pred: ( ..a -- ..b ? ) body: ( ..b -- ..a ) -- ..b )
288 [ [ not ] compose ] dip while ; inline
291 GENERIC: hashcode* ( depth obj -- code )
293 M: object hashcode* 2drop 0 ; inline
295 M: f hashcode* 2drop 31337 ; inline
297 : hashcode ( obj -- code ) 3 swap hashcode* ; inline
299 : identity-hashcode ( obj -- code )
301 dup tag 1 eq? [ drop 0 ] [
302 dup (identity-hashcode) dup 0 eq? [
303 drop dup compute-identity-hashcode
309 GENERIC: equal? ( obj1 obj2 -- ? )
311 M: object equal? 2drop f ; inline
313 TUPLE: identity-tuple ;
315 M: identity-tuple equal? 2drop f ; inline
317 M: identity-tuple hashcode* nip identity-hashcode ; inline
319 : = ( obj1 obj2 -- ? )
320 2dup eq? [ 2drop t ] [
321 2dup both-fixnums? [ 2drop f ] [ equal? ] if
324 : same? ( x y quot -- ? ) bi@ = ; inline
326 GENERIC: clone ( obj -- cloned )
328 M: object clone ; inline
330 M: callstack clone (clone) ; inline
333 GENERIC: new ( class -- tuple )
335 GENERIC: boa ( slots... class -- tuple )
337 ! Error handling -- defined early so that other files can
338 ! throw errors before continuations are loaded
339 GENERIC: throw ( error -- * )
341 ERROR: assert got expect ;
343 : assert= ( a b -- ) 2dup = [ 2drop ] [ assert ] if ;
347 : declare ( spec -- ) drop ;
349 : do-primitive ( number -- ) "Improper primitive call" throw ;
351 ! Special object count and identifiers must be kept in sync with:
353 CONSTANT: special-object-count 85
355 CONSTANT: OBJ-WALKER-HOOK 3
357 CONSTANT: OBJ-CALLCC-1 4
359 CONSTANT: ERROR-HANDLER-QUOT 5
361 CONSTANT: OBJ-CELL-SIZE 7
365 CONSTANT: OBJ-ARGS 10
366 CONSTANT: OBJ-STDIN 11
367 CONSTANT: OBJ-STDOUT 12
369 CONSTANT: OBJ-IMAGE 13
370 CONSTANT: OBJ-EXECUTABLE 14
372 CONSTANT: OBJ-EMBEDDED 15
373 CONSTANT: OBJ-EVAL-CALLBACK 16
374 CONSTANT: OBJ-YIELD-CALLBACK 17
375 CONSTANT: OBJ-SLEEP-CALLBACK 18
377 CONSTANT: OBJ-STARTUP-QUOT 20
378 CONSTANT: OBJ-GLOBAL 21
379 CONSTANT: OBJ-SHUTDOWN-QUOT 22
381 CONSTANT: JIT-PROLOG 23
382 CONSTANT: JIT-PRIMITIVE-WORD 24
383 CONSTANT: JIT-PRIMITIVE 25
384 CONSTANT: JIT-WORD-JUMP 26
385 CONSTANT: JIT-WORD-CALL 27
386 CONSTANT: JIT-IF-WORD 28
388 CONSTANT: JIT-SAFEPOINT 30
389 CONSTANT: JIT-EPILOG 31
390 CONSTANT: JIT-RETURN 32
391 CONSTANT: JIT-UNUSED 33
392 CONSTANT: JIT-PUSH-LITERAL 34
393 CONSTANT: JIT-DIP-WORD 35
395 CONSTANT: JIT-2DIP-WORD 37
396 CONSTANT: JIT-2DIP 38
397 CONSTANT: JIT-3DIP-WORD 39
398 CONSTANT: JIT-3DIP 40
399 CONSTANT: JIT-EXECUTE 41
400 CONSTANT: JIT-DECLARE-WORD 42
402 CONSTANT: C-TO-FACTOR-WORD 43
403 CONSTANT: LAZY-JIT-COMPILE-WORD 44
404 CONSTANT: UNWIND-NATIVE-FRAMES-WORD 45
405 CONSTANT: GET-FPU-STATE-WORD 46
406 CONSTANT: SET-FPU-STATE-WORD 47
407 CONSTANT: SIGNAL-HANDLER-WORD 48
408 CONSTANT: LEAF-SIGNAL-HANDLER-WORD 49
409 CONSTANT: WIN-EXCEPTION-HANDLER 50
411 CONSTANT: OBJ-SAMPLE-CALLSTACKS 51
413 CONSTANT: REDEFINITION-COUNTER 52
415 CONSTANT: CALLBACK-STUB 53
417 CONSTANT: PIC-LOAD 54
419 CONSTANT: PIC-TUPLE 56
420 CONSTANT: PIC-CHECK-TAG 57
421 CONSTANT: PIC-CHECK-TUPLE 58
423 CONSTANT: PIC-MISS-WORD 60
424 CONSTANT: PIC-MISS-TAIL-WORD 61
426 CONSTANT: MEGA-LOOKUP 62
427 CONSTANT: MEGA-LOOKUP-WORD 63
428 CONSTANT: MEGA-MISS-WORD 64
430 CONSTANT: OBJ-UNDEFINED 65
432 CONSTANT: OBJ-STDERR 66
434 CONSTANT: OBJ-STAGE2 67
436 CONSTANT: OBJ-CURRENT-THREAD 68
438 CONSTANT: OBJ-THREADS 69
439 CONSTANT: OBJ-RUN-QUEUE 70
440 CONSTANT: OBJ-SLEEP-QUEUE 71
442 CONSTANT: OBJ-VM-COMPILER 72
444 CONSTANT: OBJ-WAITING-CALLBACKS 73
446 CONSTANT: OBJ-SIGNAL-PIPE 74
448 CONSTANT: OBJ-VM-COMPILE-TIME 75
450 CONSTANT: OBJ-VM-VERSION 76
451 CONSTANT: OBJ-VM-GIT-LABEL 77
453 CONSTANT: OBJ-CANONICAL-TRUE 78
455 CONSTANT: OBJ-BIGNUM-ZERO 79
456 CONSTANT: OBJ-BIGNUM-POS-ONE 80
457 CONSTANT: OBJ-BIGNUM-NEG-ONE 81
459 ! Context object count and identifiers must be kept in sync with:
462 CONSTANT: context-object-count 4
464 CONSTANT: CONTEXT-OBJ-NAMESTACK 0
465 CONSTANT: CONTEXT-OBJ-CATCHSTACK 1
466 CONSTANT: CONTEXT-OBJ-CONTEXT 2
467 CONSTANT: CONTEXT-OBJ-IN-CALLBACK-P 3
469 ! Runtime errors must be kept in sync with:
470 ! basis/debugger/debugger.factor
473 ! VM adds this to kernel errors, so that user-space can identify them.
474 CONSTANT: KERNEL-ERROR 0xfac7
476 CONSTANT: kernel-error-count 20
478 CONSTANT: ERROR-EXPIRED 0
480 CONSTANT: ERROR-NOT-IMPLEMENTED 2
481 CONSTANT: ERROR-TYPE 3
482 CONSTANT: ERROR-DIVIDE-BY-ZERO 4
483 CONSTANT: ERROR-SIGNAL 5
484 CONSTANT: ERROR-ARRAY-SIZE 6
485 CONSTANT: ERROR-OUT-OF-FIXNUM-RANGE 7
486 CONSTANT: ERROR-FFI 8
487 CONSTANT: ERROR-UNDEFINED-SYMBOL 9
488 CONSTANT: ERROR-DATASTACK-UNDERFLOW 10
489 CONSTANT: ERROR-DATASTACK-OVERFLOW 11
490 CONSTANT: ERROR-RETAINSTACK-UNDERFLOW 12
491 CONSTANT: ERROR-RETAINSTACK-OVERFLOW 13
492 CONSTANT: ERROR-CALLSTACK-UNDERFLOW 14
493 CONSTANT: ERROR-CALLSTACK-OVERFLOW 15
494 CONSTANT: ERROR-MEMORY 16
495 CONSTANT: ERROR-FP-TRAP 17
496 CONSTANT: ERROR-INTERRUPT 18
497 CONSTANT: ERROR-CALLBACK-SPACE-OVERFLOW 19
499 PRIMITIVE: callstack-for ( context -- array )
500 PRIMITIVE: retainstack-for ( context -- array )
501 PRIMITIVE: datastack-for ( context -- array )
503 : context ( -- context )
504 CONTEXT-OBJ-CONTEXT context-object ; inline
508 : get-callstack ( -- callstack )
509 context callstack-for ; inline
511 : get-datastack ( -- array )
512 context datastack-for ; inline
514 : get-retainstack ( -- array )
515 context retainstack-for ; inline