1 ! Copyright (C) 2004, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 IN: math DEFER: <= DEFER: - ! for bootstrap
11 BUILTIN: wrapper { wrapped read-only } ;
13 PRIMITIVE: -rot ( x y z -- z x y )
14 PRIMITIVE: dup ( x -- x x )
15 PRIMITIVE: dupd ( x y -- x x y )
16 PRIMITIVE: drop ( x -- )
17 PRIMITIVE: nip ( x y -- y )
18 PRIMITIVE: over ( x y -- x y x )
19 PRIMITIVE: pick ( x y z -- x y z x )
20 PRIMITIVE: rot ( x y z -- y z x )
21 PRIMITIVE: swap ( x y -- y x )
22 PRIMITIVE: swapd ( x y z -- y x z )
23 PRIMITIVE: 2drop ( x y -- )
24 PRIMITIVE: 2dup ( x y -- x y x y )
25 PRIMITIVE: 2nip ( x y z -- z )
26 PRIMITIVE: 3drop ( x y z -- )
27 PRIMITIVE: 3dup ( x y z -- x y z x y z )
28 PRIMITIVE: 4drop ( w x y z -- )
29 PRIMITIVE: 4dup ( w x y z -- w x y z w x y z )
31 PRIMITIVE: (clone) ( obj -- newobj )
32 PRIMITIVE: eq? ( obj1 obj2 -- ? )
33 PRIMITIVE: <wrapper> ( obj -- wrapper )
35 PRIMITIVE: callstack>array ( callstack -- array )
38 PRIMITIVE: (call) ( quot -- )
39 PRIMITIVE: (execute) ( word -- )
40 PRIMITIVE: (identity-hashcode) ( obj -- code )
41 PRIMITIVE: become ( old new -- )
42 PRIMITIVE: c-to-factor ( -- )
43 PRIMITIVE: callstack-bounds ( -- start end )
44 PRIMITIVE: check-datastack ( array in# out# -- ? )
45 PRIMITIVE: compute-identity-hashcode ( obj -- )
46 PRIMITIVE: context-object ( n -- obj )
47 PRIMITIVE: fpu-state ( -- )
48 PRIMITIVE: innermost-frame-executing ( callstack -- obj )
49 PRIMITIVE: innermost-frame-scan ( callstack -- n )
50 PRIMITIVE: lazy-jit-compile ( -- )
51 PRIMITIVE: leaf-signal-handler ( -- )
52 PRIMITIVE: set-callstack ( callstack -- * )
53 PRIMITIVE: set-context-object ( obj n -- )
54 PRIMITIVE: set-datastack ( array -- )
55 PRIMITIVE: set-fpu-state ( -- )
56 PRIMITIVE: set-innermost-frame-quotation ( n callstack -- )
57 PRIMITIVE: set-retainstack ( array -- )
58 PRIMITIVE: set-special-object ( obj n -- )
59 PRIMITIVE: signal-handler ( -- )
60 PRIMITIVE: special-object ( n -- obj )
61 PRIMITIVE: strip-stack-traces ( -- )
62 PRIMITIVE: tag ( object -- n )
63 PRIMITIVE: unwind-native-frames ( -- )
71 : 2over ( x y z -- x y z x y ) pick pick ; inline
73 : clear ( -- ) { } set-datastack ;
76 GENERIC: call ( callable -- )
78 GENERIC: execute ( word -- )
82 : ? ( ? true false -- true/false )
83 ! 'if' and '?' can be defined in terms of each other
84 ! because the JIT special-cases an 'if' preceded by
85 ! two literal quotations.
86 rot [ drop ] [ nip ] if ; inline
88 : if ( ..a ? true: ( ..a -- ..b ) false: ( ..a -- ..b ) -- ..b ) ? call ;
91 : unless ( ..a ? false: ( ..a -- ..a ) -- ..a )
92 swap [ drop ] [ call ] if ; inline
94 : when ( ..a ? true: ( ..a -- ..a ) -- ..a )
95 swap [ call ] [ drop ] if ; inline
98 : if* ( ..a ? true: ( ..a ? -- ..b ) false: ( ..a -- ..b ) -- ..b )
99 pick [ drop call ] [ 2nip call ] if ; inline
101 : when* ( ..a ? true: ( ..a ? -- ..a ) -- ..a )
102 over [ call ] [ 2drop ] if ; inline
104 : unless* ( ..a ? false: ( ..a -- ..a x ) -- ..a x )
105 over [ drop ] [ nip call ] if ; inline
108 : ?if ( ..a default cond true: ( ..a cond -- ..b ) false: ( ..a default -- ..b ) -- ..b )
109 pick [ drop [ drop ] 2dip call ] [ 2nip call ] if ; inline
112 ! Not declared inline because the compiler special-cases them
114 : dip ( x quot -- x ) swap [ call ] dip ;
116 : 2dip ( x y quot -- x y ) swap [ dip ] dip ;
118 : 3dip ( x y z quot -- x y z ) swap [ 2dip ] dip ;
120 : 4dip ( w x y z quot -- w x y z ) swap [ 3dip ] dip ; inline
123 : tuck ( x y -- y x y ) dup -rot ; inline
125 : spin ( x y z -- z y x ) -rot swap ; inline
127 : rotd ( w x y z -- x y w z ) [ rot ] dip ; inline
129 : -rotd ( w x y z -- y w x z ) [ -rot ] dip ; inline
131 : roll ( w x y z -- x y z w ) rotd swap ; inline
133 : -roll ( w x y z -- z w x y ) swap -rotd ; inline
135 : nipd ( x y z -- y z ) [ nip ] dip ; inline
137 : overd ( x y z -- x y x z ) [ over ] dip ; inline
139 : pickd ( w x y z -- w x y w z ) [ pick ] dip ; inline
141 : 2nipd ( w x y z -- y z ) [ 2drop ] 2dip ; inline
143 : 3nipd ( v w x y z -- y z ) [ 3drop ] 2dip ; inline
145 : 3nip ( w x y z -- z ) 2nip nip ; inline
147 : 4nip ( v w x y z -- z ) 2nip 2nip ; inline
149 : 5nip ( u v w x y z -- z ) 3nip 2nip ; inline
151 : 5drop ( v w x y z -- ) 4drop drop ; inline
153 : reach ( w x y z -- w x y z w ) [ pick ] dip swap ; inline
156 : keep ( ..a x quot: ( ..a x -- ..b ) -- ..b x )
157 over [ call ] dip ; inline
159 : 2keep ( ..a x y quot: ( ..a x y -- ..b ) -- ..b x y )
160 [ 2dup ] dip 2dip ; inline
162 : 3keep ( ..a x y z quot: ( ..a x y z -- ..b ) -- ..b x y z )
163 [ 3dup ] dip 3dip ; inline
165 : 4keep ( ..a w x y z quot: ( ..a w x y z -- ..b ) -- ..b w x y z )
166 [ 4dup ] dip 4dip ; inline
168 : keepd ( ..a x y quot: ( ..a x y -- ..b ) -- ..b x )
171 : keepdd ( ..a x y z quot: ( ..a x y z -- ..b ) -- ..b x )
174 : 2keepd ( ..a x y z quot: ( ..a x y z -- ..b ) -- ..b x y )
179 [ keep ] dip call ; inline
182 [ [ keep ] dip keep ] dip call ; inline
186 [ 2keep ] dip call ; inline
188 : 2tri ( x y p q r -- )
189 [ [ 2keep ] dip 2keep ] dip call ; inline
192 : 3bi ( x y z p q -- )
193 [ 3keep ] dip call ; inline
195 : 3tri ( x y z p q r -- )
196 [ [ 3keep ] dip 3keep ] dip call ; inline
200 [ dip ] dip call ; inline
202 : tri* ( x y z p q r -- )
203 [ [ 2dip ] dip dip ] dip call ; inline
206 : 2bi* ( w x y z p q -- )
207 [ 2dip ] dip call ; inline
209 : 2tri* ( u v w x y z p q r -- )
210 [ 4dip ] 2dip 2bi* ; inline
213 : bi@ ( x y quot -- )
216 : tri@ ( x y z quot -- )
217 dup dup tri* ; inline
220 : 2bi@ ( w x y z quot -- )
223 : 2tri@ ( u v w x y z quot -- )
224 dup dup 2tri* ; inline
227 : 2curry ( obj1 obj2 quot -- curried )
230 : 3curry ( obj1 obj2 obj3 quot -- curried )
231 curry curry curry ; inline
233 : with ( param obj quot -- obj curried )
234 swapd [ swapd call ] 2curry ; inline
236 : 2with ( param1 param2 obj quot -- obj curried )
239 : prepose ( quot1 quot2 -- composed )
240 swap compose ; inline
245 : currier ( quot -- quot' ) [ curry ] curry ; inline
249 : bi-curry ( x p q -- p' q' ) [ currier ] bi@ bi ; inline
251 : tri-curry ( x p q r -- p' q' r' ) [ currier ] tri@ tri ; inline
253 : bi-curry* ( x y p q -- p' q' ) [ currier ] bi@ bi* ; inline
255 : tri-curry* ( x y z p q r -- p' q' r' ) [ currier ] tri@ tri* ; inline
257 : bi-curry@ ( x y q -- p' q' ) currier bi@ ; inline
259 : tri-curry@ ( x y z q -- p' q' r' ) currier tri@ ; inline
262 UNION: boolean POSTPONE: t POSTPONE: f ;
264 : >boolean ( obj -- ? ) [ t ] [ f ] if ; inline
266 : not ( obj -- ? ) [ f ] [ t ] if ; inline
268 : and ( obj1 obj2 -- ? ) over ? ; inline
270 : or ( obj1 obj2 -- ? ) dupd ? ; inline
272 : xor ( obj1 obj2 -- ? ) [ f swap ? ] when* ; inline
274 : both? ( x y quot -- ? ) bi@ and ; inline
276 : either? ( x y quot -- ? ) bi@ or ; inline
278 : most ( x y quot -- z ) 2keep ? ; inline
280 : negate ( quot -- quot' ) [ not ] compose ; inline
283 : loop ( ... pred: ( ... -- ... ? ) -- ... )
284 [ call ] keep [ loop ] curry when ; inline recursive
286 : do ( pred body -- pred body )
289 : while ( ..a pred: ( ..a -- ..b ? ) body: ( ..b -- ..a ) -- ..b )
290 swap do compose [ loop ] curry when ; inline
292 : while* ( ..a pred: ( ..a -- ..b ? ) body: ( ..b ? -- ..a ) -- ..b )
293 [ [ dup ] compose ] dip while drop ; inline
295 : until ( ..a pred: ( ..a -- ..b ? ) body: ( ..b -- ..a ) -- ..b )
296 [ negate ] dip while ; inline
299 GENERIC: hashcode* ( depth obj -- code ) flushable
301 M: object hashcode* 2drop 0 ; inline
303 M: f hashcode* 2drop 31337 ; inline
305 : hashcode ( obj -- code ) 3 swap hashcode* ; inline
307 : recursive-hashcode ( n obj quot -- code )
308 pick 0 <= [ 3drop 0 ] [ [ 1 - ] 2dip call ] if ; inline
310 GENERIC: equal? ( obj1 obj2 -- ? )
312 M: object equal? 2drop f ; inline
314 TUPLE: identity-tuple ;
316 M: identity-tuple equal? 2drop f ; inline
318 : identity-hashcode ( obj -- code )
320 dup tag 1 eq? [ drop 0 ] [
321 dup (identity-hashcode) dup 0 eq? [
322 drop dup compute-identity-hashcode
328 M: identity-tuple hashcode* nip identity-hashcode ; inline
330 : = ( obj1 obj2 -- ? )
331 2dup eq? [ 2drop t ] [
332 2dup both-fixnums? [ 2drop f ] [ equal? ] if
335 : same? ( x y quot -- ? ) bi@ = ; inline
337 GENERIC: clone ( obj -- cloned )
339 M: object clone ; inline
341 M: callstack clone (clone) ; inline
344 GENERIC: new ( class -- tuple )
346 GENERIC: boa ( slots... class -- tuple )
348 ! Error handling -- defined early so that other files can
349 ! throw errors before continuations are loaded
350 GENERIC: throw ( error -- * )
352 ERROR: assert got expect ;
354 : assert= ( a b -- ) 2dup = [ 2drop ] [ assert ] if ;
358 : declare ( spec -- ) drop ;
360 : do-primitive ( number -- ) "Improper primitive call" throw ;
362 ! Special object count and identifiers must be kept in sync with:
364 CONSTANT: special-object-count 85
366 CONSTANT: OBJ-WALKER-HOOK 3
368 CONSTANT: OBJ-CALLCC-1 4
370 CONSTANT: ERROR-HANDLER-QUOT 5
372 CONSTANT: OBJ-CELL-SIZE 7
376 CONSTANT: OBJ-ARGS 10
377 CONSTANT: OBJ-STDIN 11
378 CONSTANT: OBJ-STDOUT 12
380 CONSTANT: OBJ-IMAGE 13
381 CONSTANT: OBJ-EXECUTABLE 14
383 CONSTANT: OBJ-EMBEDDED 15
384 CONSTANT: OBJ-EVAL-CALLBACK 16
385 CONSTANT: OBJ-YIELD-CALLBACK 17
386 CONSTANT: OBJ-SLEEP-CALLBACK 18
388 CONSTANT: OBJ-STARTUP-QUOT 20
389 CONSTANT: OBJ-GLOBAL 21
390 CONSTANT: OBJ-SHUTDOWN-QUOT 22
392 CONSTANT: JIT-PROLOG 23
393 CONSTANT: JIT-PRIMITIVE-WORD 24
394 CONSTANT: JIT-PRIMITIVE 25
395 CONSTANT: JIT-WORD-JUMP 26
396 CONSTANT: JIT-WORD-CALL 27
397 CONSTANT: JIT-IF-WORD 28
399 CONSTANT: JIT-SAFEPOINT 30
400 CONSTANT: JIT-EPILOG 31
401 CONSTANT: JIT-RETURN 32
402 CONSTANT: JIT-UNUSED 33
403 CONSTANT: JIT-PUSH-LITERAL 34
404 CONSTANT: JIT-DIP-WORD 35
406 CONSTANT: JIT-2DIP-WORD 37
407 CONSTANT: JIT-2DIP 38
408 CONSTANT: JIT-3DIP-WORD 39
409 CONSTANT: JIT-3DIP 40
410 CONSTANT: JIT-EXECUTE 41
411 CONSTANT: JIT-DECLARE-WORD 42
413 CONSTANT: C-TO-FACTOR-WORD 43
414 CONSTANT: LAZY-JIT-COMPILE-WORD 44
415 CONSTANT: UNWIND-NATIVE-FRAMES-WORD 45
416 CONSTANT: GET-FPU-STATE-WORD 46
417 CONSTANT: SET-FPU-STATE-WORD 47
418 CONSTANT: SIGNAL-HANDLER-WORD 48
419 CONSTANT: LEAF-SIGNAL-HANDLER-WORD 49
420 CONSTANT: WIN-EXCEPTION-HANDLER 50
422 CONSTANT: OBJ-SAMPLE-CALLSTACKS 51
424 CONSTANT: REDEFINITION-COUNTER 52
426 CONSTANT: CALLBACK-STUB 53
428 CONSTANT: PIC-LOAD 54
430 CONSTANT: PIC-TUPLE 56
431 CONSTANT: PIC-CHECK-TAG 57
432 CONSTANT: PIC-CHECK-TUPLE 58
434 CONSTANT: PIC-MISS-WORD 60
435 CONSTANT: PIC-MISS-TAIL-WORD 61
437 CONSTANT: MEGA-LOOKUP 62
438 CONSTANT: MEGA-LOOKUP-WORD 63
439 CONSTANT: MEGA-MISS-WORD 64
441 CONSTANT: OBJ-UNDEFINED 65
443 CONSTANT: OBJ-STDERR 66
445 CONSTANT: OBJ-STAGE2 67
447 CONSTANT: OBJ-CURRENT-THREAD 68
449 CONSTANT: OBJ-THREADS 69
450 CONSTANT: OBJ-RUN-QUEUE 70
451 CONSTANT: OBJ-SLEEP-QUEUE 71
453 CONSTANT: OBJ-VM-COMPILER 72
455 CONSTANT: OBJ-WAITING-CALLBACKS 73
457 CONSTANT: OBJ-SIGNAL-PIPE 74
459 CONSTANT: OBJ-VM-COMPILE-TIME 75
461 CONSTANT: OBJ-VM-VERSION 76
462 CONSTANT: OBJ-VM-GIT-LABEL 77
464 CONSTANT: OBJ-CANONICAL-TRUE 78
466 CONSTANT: OBJ-BIGNUM-ZERO 79
467 CONSTANT: OBJ-BIGNUM-POS-ONE 80
468 CONSTANT: OBJ-BIGNUM-NEG-ONE 81
470 ! Context object count and identifiers must be kept in sync with:
473 CONSTANT: context-object-count 4
475 CONSTANT: CONTEXT-OBJ-NAMESTACK 0
476 CONSTANT: CONTEXT-OBJ-CATCHSTACK 1
477 CONSTANT: CONTEXT-OBJ-CONTEXT 2
478 CONSTANT: CONTEXT-OBJ-IN-CALLBACK-P 3
480 ! Runtime errors must be kept in sync with:
481 ! basis/debugger/debugger.factor
484 ! VM adds this to kernel errors, so that user-space can identify them.
485 CONSTANT: KERNEL-ERROR 0xfac7
487 CONSTANT: kernel-error-count 20
489 CONSTANT: ERROR-EXPIRED 0
491 CONSTANT: ERROR-NOT-IMPLEMENTED 2
492 CONSTANT: ERROR-TYPE 3
493 CONSTANT: ERROR-DIVIDE-BY-ZERO 4
494 CONSTANT: ERROR-SIGNAL 5
495 CONSTANT: ERROR-ARRAY-SIZE 6
496 CONSTANT: ERROR-OUT-OF-FIXNUM-RANGE 7
497 CONSTANT: ERROR-FFI 8
498 CONSTANT: ERROR-UNDEFINED-SYMBOL 9
499 CONSTANT: ERROR-DATASTACK-UNDERFLOW 10
500 CONSTANT: ERROR-DATASTACK-OVERFLOW 11
501 CONSTANT: ERROR-RETAINSTACK-UNDERFLOW 12
502 CONSTANT: ERROR-RETAINSTACK-OVERFLOW 13
503 CONSTANT: ERROR-CALLSTACK-UNDERFLOW 14
504 CONSTANT: ERROR-CALLSTACK-OVERFLOW 15
505 CONSTANT: ERROR-MEMORY 16
506 CONSTANT: ERROR-FP-TRAP 17
507 CONSTANT: ERROR-INTERRUPT 18
508 CONSTANT: ERROR-CALLBACK-SPACE-OVERFLOW 19
510 PRIMITIVE: callstack-for ( context -- array )
511 PRIMITIVE: retainstack-for ( context -- array )
512 PRIMITIVE: datastack-for ( context -- array )
514 : context ( -- context )
515 CONTEXT-OBJ-CONTEXT context-object ; inline
519 : get-callstack ( -- callstack )
520 context callstack-for ; inline
522 : get-datastack ( -- array )
523 context datastack-for ; inline
525 : get-retainstack ( -- array )
526 context retainstack-for ; inline