1 ! Copyright (C) 2004, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 IN: math DEFER: <= DEFER: - ! for bootstrap since math uses kernel
4 USING: kernel.private slots.private math.private ;
9 BUILTIN: wrapper { wrapped read-only } ;
11 PRIMITIVE: -rot ( x y z -- z x y )
12 PRIMITIVE: dup ( x -- x x )
13 PRIMITIVE: dupd ( x y -- x x y )
14 PRIMITIVE: drop ( x -- )
15 PRIMITIVE: nip ( x y -- y )
16 PRIMITIVE: over ( x y -- x y x )
17 PRIMITIVE: pick ( x y z -- x y z x )
18 PRIMITIVE: rot ( x y z -- y z x )
19 PRIMITIVE: swap ( x y -- y x )
20 PRIMITIVE: swapd ( x y z -- y x z )
21 PRIMITIVE: 2drop ( x y -- )
22 PRIMITIVE: 2dup ( x y -- x y x y )
23 PRIMITIVE: 2nip ( x y z -- z )
24 PRIMITIVE: 3drop ( x y z -- )
25 PRIMITIVE: 3dup ( x y z -- x y z x y z )
26 PRIMITIVE: 4drop ( w x y z -- )
27 PRIMITIVE: 4dup ( w x y z -- w x y z w x y z )
29 PRIMITIVE: (clone) ( obj -- newobj )
30 PRIMITIVE: eq? ( obj1 obj2 -- ? )
31 PRIMITIVE: <wrapper> ( obj -- wrapper )
33 PRIMITIVE: callstack>array ( callstack -- array )
36 PRIMITIVE: (call) ( quot -- )
37 PRIMITIVE: (execute) ( word -- )
38 PRIMITIVE: (identity-hashcode) ( obj -- code )
39 PRIMITIVE: become ( old new -- )
40 PRIMITIVE: c-to-factor ( -- )
41 PRIMITIVE: callstack-bounds ( -- start end )
42 PRIMITIVE: check-datastack ( array in# out# -- ? )
43 PRIMITIVE: compute-identity-hashcode ( obj -- )
44 PRIMITIVE: context-object ( n -- obj )
45 PRIMITIVE: fpu-state ( -- )
46 PRIMITIVE: innermost-frame-executing ( callstack -- obj )
47 PRIMITIVE: innermost-frame-scan ( callstack -- n )
48 PRIMITIVE: lazy-jit-compile ( -- )
49 PRIMITIVE: leaf-signal-handler ( -- )
50 PRIMITIVE: set-callstack ( callstack -- * )
51 PRIMITIVE: set-context-object ( obj n -- )
52 PRIMITIVE: set-datastack ( array -- )
53 PRIMITIVE: set-fpu-state ( -- )
54 PRIMITIVE: set-innermost-frame-quotation ( n callstack -- )
55 PRIMITIVE: set-retainstack ( array -- )
56 PRIMITIVE: set-special-object ( obj n -- )
57 PRIMITIVE: signal-handler ( -- )
58 PRIMITIVE: special-object ( n -- obj )
59 PRIMITIVE: strip-stack-traces ( -- )
60 PRIMITIVE: tag ( object -- n )
61 PRIMITIVE: unwind-native-frames ( -- )
69 : 2over ( x y z -- x y z x y ) pick pick ; inline
71 : clear ( -- ) { } set-datastack ;
74 GENERIC: call ( callable -- )
76 GENERIC: execute ( word -- )
80 : ? ( ? true false -- true/false )
81 ! 'if' and '?' can be defined in terms of each other
82 ! because the JIT special-cases an 'if' preceded by
83 ! two literal quotations.
84 rot [ drop ] [ nip ] if ; inline
86 : if ( ..a ? true: ( ..a -- ..b ) false: ( ..a -- ..b ) -- ..b ) ? call ;
89 : unless ( ..a ? false: ( ..a -- ..a ) -- ..a )
90 swap [ drop ] [ call ] if ; inline
92 : when ( ..a ? true: ( ..a -- ..a ) -- ..a )
93 swap [ call ] [ drop ] if ; inline
96 : if* ( ..a ? true: ( ..a ? -- ..b ) false: ( ..a -- ..b ) -- ..b )
97 pick [ drop call ] [ 2nip call ] if ; inline
99 : when* ( ..a ? true: ( ..a ? -- ..a ) -- ..a )
100 over [ call ] [ 2drop ] if ; inline
102 : unless* ( ..a ? false: ( ..a -- ..a x ) -- ..a x )
103 over [ drop ] [ nip call ] if ; inline
106 : ?if ( ..a default cond true: ( ..a cond -- ..b ) false: ( ..a default -- ..b ) -- ..b )
107 pick [ drop [ drop ] 2dip call ] [ 2nip call ] if ; inline
110 ! Not declared inline because the compiler special-cases them
112 : dip ( x quot -- x ) swap [ call ] dip ;
114 : 2dip ( x y quot -- x y ) swap [ dip ] dip ;
116 : 3dip ( x y z quot -- x y z ) swap [ 2dip ] dip ;
118 : 4dip ( w x y z quot -- w x y z ) swap [ 3dip ] dip ; inline
121 : tuck ( x y -- y x y ) dup -rot ; inline
123 : spin ( x y z -- z y x ) -rot swap ; inline
125 : rotd ( w x y z -- x y w z ) [ rot ] dip ; inline
127 : -rotd ( w x y z -- y w x z ) [ -rot ] dip ; inline
129 : roll ( w x y z -- x y z w ) rotd swap ; inline
131 : -roll ( w x y z -- z w x y ) swap -rotd ; inline
133 : nipd ( x y z -- y z ) [ nip ] dip ; inline
135 : overd ( x y z -- x y x z ) [ over ] dip ; inline
137 : pickd ( w x y z -- w x y w z ) [ pick ] dip ; inline
139 : 2nipd ( w x y z -- y z ) [ 2drop ] 2dip ; inline
141 : 3nipd ( v w x y z -- y z ) [ 3drop ] 2dip ; inline
143 : 3nip ( w x y z -- z ) 2nip nip ; inline
145 : 4nip ( v w x y z -- z ) 2nip 2nip ; inline
147 : 5nip ( u v w x y z -- z ) 3nip 2nip ; inline
149 : 5drop ( v w x y z -- ) 4drop drop ; inline
151 : reach ( w x y z -- w x y z w ) [ pick ] dip swap ; inline
154 : keep ( ..a x quot: ( ..a x -- ..b ) -- ..b x )
155 over [ call ] dip ; inline
157 : 2keep ( ..a x y quot: ( ..a x y -- ..b ) -- ..b x y )
158 [ 2dup ] dip 2dip ; inline
160 : 3keep ( ..a x y z quot: ( ..a x y z -- ..b ) -- ..b x y z )
161 [ 3dup ] dip 3dip ; inline
163 : 4keep ( ..a w x y z quot: ( ..a w x y z -- ..b ) -- ..b w x y z )
164 [ 4dup ] dip 4dip ; inline
166 : keepd ( ..a x y quot: ( ..a x y -- ..b x ) -- ..b x )
169 : keepdd ( ..a x y z quot: ( ..a x y z -- ..b x ) -- ..b x )
172 : 2keepd ( ..a x y z quot: ( ..a x y z -- ..b x y ) -- ..b x y )
177 [ keep ] dip call ; inline
180 [ [ keep ] dip keep ] dip call ; inline
184 [ 2keep ] dip call ; inline
186 : 2tri ( x y p q r -- )
187 [ [ 2keep ] dip 2keep ] dip call ; inline
190 : 3bi ( x y z p q -- )
191 [ 3keep ] dip call ; inline
193 : 3tri ( x y z p q r -- )
194 [ [ 3keep ] dip 3keep ] dip call ; inline
198 [ dip ] dip call ; inline
200 : tri* ( x y z p q r -- )
201 [ [ 2dip ] dip dip ] dip call ; inline
204 : 2bi* ( w x y z p q -- )
205 [ 2dip ] dip call ; inline
207 : 2tri* ( u v w x y z p q r -- )
208 [ 4dip ] 2dip 2bi* ; inline
211 : bi@ ( x y quot -- )
214 : tri@ ( x y z quot -- )
215 dup dup tri* ; inline
218 : 2bi@ ( w x y z quot -- )
221 : 2tri@ ( u v w x y z quot -- )
222 dup dup 2tri* ; inline
225 : 2curry ( obj1 obj2 quot -- curried )
228 : 3curry ( obj1 obj2 obj3 quot -- curried )
229 curry curry curry ; inline
231 : with ( param obj quot -- obj curried )
232 swapd [ swapd call ] 2curry ; inline
234 : 2with ( param1 param2 obj quot -- obj curried )
237 : prepose ( quot1 quot2 -- composed )
238 swap compose ; inline
243 : currier ( quot -- quot' ) [ curry ] curry ; inline
247 : bi-curry ( x p q -- p' q' ) [ currier ] bi@ bi ; inline
249 : tri-curry ( x p q r -- p' q' r' ) [ currier ] tri@ tri ; inline
251 : bi-curry* ( x y p q -- p' q' ) [ currier ] bi@ bi* ; inline
253 : tri-curry* ( x y z p q r -- p' q' r' ) [ currier ] tri@ tri* ; inline
255 : bi-curry@ ( x y q -- p' q' ) currier bi@ ; inline
257 : tri-curry@ ( x y z q -- p' q' r' ) currier tri@ ; inline
260 UNION: boolean POSTPONE: t POSTPONE: f ;
262 : >boolean ( obj -- ? ) [ t ] [ f ] if ; inline
264 : not ( obj -- ? ) [ f ] [ t ] if ; inline
266 : and ( obj1 obj2 -- ? ) over ? ; inline
268 : or ( obj1 obj2 -- ? ) dupd ? ; inline
270 : xor ( obj1 obj2 -- ? ) [ f swap ? ] when* ; inline
272 : both? ( x y quot -- ? ) bi@ and ; inline
274 : either? ( x y quot -- ? ) bi@ or ; inline
276 : most ( x y quot -- z ) 2keep ? ; inline
279 : loop ( ... pred: ( ... -- ... ? ) -- ... )
280 [ call ] keep [ loop ] curry when ; inline recursive
282 : do ( pred body -- pred body )
285 : while ( ..a pred: ( ..a -- ..b ? ) body: ( ..b -- ..a ) -- ..b )
286 swap do compose [ loop ] curry when ; inline
288 : while* ( ..a pred: ( ..a -- ..b ? ) body: ( ..b ? -- ..a ) -- ..b )
289 [ [ dup ] compose ] dip while drop ; inline
291 : until ( ..a pred: ( ..a -- ..b ? ) body: ( ..b -- ..a ) -- ..b )
292 [ [ not ] compose ] dip while ; inline
295 GENERIC: hashcode* ( depth obj -- code )
297 M: object hashcode* 2drop 0 ; inline
299 M: f hashcode* 2drop 31337 ; inline
301 : hashcode ( obj -- code ) 3 swap hashcode* ; inline
305 : recursive-hashcode ( n obj quot -- code )
306 pick 0 <= [ 3drop 0 ] [ [ 1 - ] 2dip call ] if ; inline
308 GENERIC: equal? ( obj1 obj2 -- ? )
310 M: object equal? 2drop f ; inline
312 TUPLE: identity-tuple ;
314 M: identity-tuple equal? 2drop f ; inline
316 : identity-hashcode ( obj -- code )
318 dup tag 1 eq? [ drop 0 ] [
319 dup (identity-hashcode) dup 0 eq? [
320 drop dup compute-identity-hashcode
326 M: identity-tuple hashcode* nip identity-hashcode ; inline
328 : = ( obj1 obj2 -- ? )
329 2dup eq? [ 2drop t ] [
330 2dup both-fixnums? [ 2drop f ] [ equal? ] if
333 : same? ( x y quot -- ? ) bi@ = ; inline
335 GENERIC: clone ( obj -- cloned )
337 M: object clone ; inline
339 M: callstack clone (clone) ; inline
342 GENERIC: new ( class -- tuple )
344 GENERIC: boa ( slots... class -- tuple )
346 ! Error handling -- defined early so that other files can
347 ! throw errors before continuations are loaded
348 GENERIC: throw ( error -- * )
350 ERROR: assert got expect ;
352 : assert= ( a b -- ) 2dup = [ 2drop ] [ assert ] if ;
356 : declare ( spec -- ) drop ;
358 : do-primitive ( number -- ) "Improper primitive call" throw ;
360 ! Special object count and identifiers must be kept in sync with:
362 CONSTANT: special-object-count 85
364 CONSTANT: OBJ-WALKER-HOOK 3
366 CONSTANT: OBJ-CALLCC-1 4
368 CONSTANT: ERROR-HANDLER-QUOT 5
370 CONSTANT: OBJ-CELL-SIZE 7
374 CONSTANT: OBJ-ARGS 10
375 CONSTANT: OBJ-STDIN 11
376 CONSTANT: OBJ-STDOUT 12
378 CONSTANT: OBJ-IMAGE 13
379 CONSTANT: OBJ-EXECUTABLE 14
381 CONSTANT: OBJ-EMBEDDED 15
382 CONSTANT: OBJ-EVAL-CALLBACK 16
383 CONSTANT: OBJ-YIELD-CALLBACK 17
384 CONSTANT: OBJ-SLEEP-CALLBACK 18
386 CONSTANT: OBJ-STARTUP-QUOT 20
387 CONSTANT: OBJ-GLOBAL 21
388 CONSTANT: OBJ-SHUTDOWN-QUOT 22
390 CONSTANT: JIT-PROLOG 23
391 CONSTANT: JIT-PRIMITIVE-WORD 24
392 CONSTANT: JIT-PRIMITIVE 25
393 CONSTANT: JIT-WORD-JUMP 26
394 CONSTANT: JIT-WORD-CALL 27
395 CONSTANT: JIT-IF-WORD 28
397 CONSTANT: JIT-SAFEPOINT 30
398 CONSTANT: JIT-EPILOG 31
399 CONSTANT: JIT-RETURN 32
400 CONSTANT: JIT-UNUSED 33
401 CONSTANT: JIT-PUSH-LITERAL 34
402 CONSTANT: JIT-DIP-WORD 35
404 CONSTANT: JIT-2DIP-WORD 37
405 CONSTANT: JIT-2DIP 38
406 CONSTANT: JIT-3DIP-WORD 39
407 CONSTANT: JIT-3DIP 40
408 CONSTANT: JIT-EXECUTE 41
409 CONSTANT: JIT-DECLARE-WORD 42
411 CONSTANT: C-TO-FACTOR-WORD 43
412 CONSTANT: LAZY-JIT-COMPILE-WORD 44
413 CONSTANT: UNWIND-NATIVE-FRAMES-WORD 45
414 CONSTANT: GET-FPU-STATE-WORD 46
415 CONSTANT: SET-FPU-STATE-WORD 47
416 CONSTANT: SIGNAL-HANDLER-WORD 48
417 CONSTANT: LEAF-SIGNAL-HANDLER-WORD 49
418 CONSTANT: WIN-EXCEPTION-HANDLER 50
420 CONSTANT: OBJ-SAMPLE-CALLSTACKS 51
422 CONSTANT: REDEFINITION-COUNTER 52
424 CONSTANT: CALLBACK-STUB 53
426 CONSTANT: PIC-LOAD 54
428 CONSTANT: PIC-TUPLE 56
429 CONSTANT: PIC-CHECK-TAG 57
430 CONSTANT: PIC-CHECK-TUPLE 58
432 CONSTANT: PIC-MISS-WORD 60
433 CONSTANT: PIC-MISS-TAIL-WORD 61
435 CONSTANT: MEGA-LOOKUP 62
436 CONSTANT: MEGA-LOOKUP-WORD 63
437 CONSTANT: MEGA-MISS-WORD 64
439 CONSTANT: OBJ-UNDEFINED 65
441 CONSTANT: OBJ-STDERR 66
443 CONSTANT: OBJ-STAGE2 67
445 CONSTANT: OBJ-CURRENT-THREAD 68
447 CONSTANT: OBJ-THREADS 69
448 CONSTANT: OBJ-RUN-QUEUE 70
449 CONSTANT: OBJ-SLEEP-QUEUE 71
451 CONSTANT: OBJ-VM-COMPILER 72
453 CONSTANT: OBJ-WAITING-CALLBACKS 73
455 CONSTANT: OBJ-SIGNAL-PIPE 74
457 CONSTANT: OBJ-VM-COMPILE-TIME 75
459 CONSTANT: OBJ-VM-VERSION 76
460 CONSTANT: OBJ-VM-GIT-LABEL 77
462 CONSTANT: OBJ-CANONICAL-TRUE 78
464 CONSTANT: OBJ-BIGNUM-ZERO 79
465 CONSTANT: OBJ-BIGNUM-POS-ONE 80
466 CONSTANT: OBJ-BIGNUM-NEG-ONE 81
468 ! Context object count and identifiers must be kept in sync with:
471 CONSTANT: context-object-count 4
473 CONSTANT: CONTEXT-OBJ-NAMESTACK 0
474 CONSTANT: CONTEXT-OBJ-CATCHSTACK 1
475 CONSTANT: CONTEXT-OBJ-CONTEXT 2
476 CONSTANT: CONTEXT-OBJ-IN-CALLBACK-P 3
478 ! Runtime errors must be kept in sync with:
479 ! basis/debugger/debugger.factor
482 ! VM adds this to kernel errors, so that user-space can identify them.
483 CONSTANT: KERNEL-ERROR 0xfac7
485 CONSTANT: kernel-error-count 20
487 CONSTANT: ERROR-EXPIRED 0
489 CONSTANT: ERROR-NOT-IMPLEMENTED 2
490 CONSTANT: ERROR-TYPE 3
491 CONSTANT: ERROR-DIVIDE-BY-ZERO 4
492 CONSTANT: ERROR-SIGNAL 5
493 CONSTANT: ERROR-ARRAY-SIZE 6
494 CONSTANT: ERROR-OUT-OF-FIXNUM-RANGE 7
495 CONSTANT: ERROR-FFI 8
496 CONSTANT: ERROR-UNDEFINED-SYMBOL 9
497 CONSTANT: ERROR-DATASTACK-UNDERFLOW 10
498 CONSTANT: ERROR-DATASTACK-OVERFLOW 11
499 CONSTANT: ERROR-RETAINSTACK-UNDERFLOW 12
500 CONSTANT: ERROR-RETAINSTACK-OVERFLOW 13
501 CONSTANT: ERROR-CALLSTACK-UNDERFLOW 14
502 CONSTANT: ERROR-CALLSTACK-OVERFLOW 15
503 CONSTANT: ERROR-MEMORY 16
504 CONSTANT: ERROR-FP-TRAP 17
505 CONSTANT: ERROR-INTERRUPT 18
506 CONSTANT: ERROR-CALLBACK-SPACE-OVERFLOW 19
508 PRIMITIVE: callstack-for ( context -- array )
509 PRIMITIVE: retainstack-for ( context -- array )
510 PRIMITIVE: datastack-for ( context -- array )
512 : context ( -- context )
513 CONTEXT-OBJ-CONTEXT context-object ; inline
517 : get-callstack ( -- callstack )
518 context callstack-for ; inline
520 : get-datastack ( -- array )
521 context datastack-for ; inline
523 : get-retainstack ( -- array )
524 context retainstack-for ; inline