1 ! Copyright (C) 2004, 2009 Slava Pestov.
2 ! See https://factorcode.org/license.txt for BSD license.
10 BUILTIN: wrapper { wrapped read-only } ;
12 PRIMITIVE: -rot ( x y z -- z x y )
13 PRIMITIVE: dup ( x -- x x )
14 PRIMITIVE: dupd ( x y -- x x y )
15 PRIMITIVE: drop ( x -- )
16 PRIMITIVE: nip ( x y -- y )
17 PRIMITIVE: over ( x y -- x y x )
18 PRIMITIVE: pick ( x y z -- x y z x )
19 PRIMITIVE: rot ( x y z -- y z x )
20 PRIMITIVE: swap ( x y -- y x )
21 PRIMITIVE: swapd ( x y z -- y x z )
22 PRIMITIVE: 2drop ( x y -- )
23 PRIMITIVE: 2dup ( x y -- x y x y )
24 PRIMITIVE: 2nip ( x y z -- z )
25 PRIMITIVE: 3drop ( x y z -- )
26 PRIMITIVE: 3dup ( x y z -- x y z x y z )
27 PRIMITIVE: 4drop ( w x y z -- )
28 PRIMITIVE: 4dup ( w x y z -- w x y z w x y z )
30 PRIMITIVE: (clone) ( obj -- newobj )
31 PRIMITIVE: eq? ( obj1 obj2 -- ? )
32 PRIMITIVE: <wrapper> ( obj -- wrapper )
34 PRIMITIVE: callstack>array ( callstack -- array )
37 PRIMITIVE: (call) ( quot -- )
38 PRIMITIVE: (execute) ( word -- )
39 PRIMITIVE: (identity-hashcode) ( obj -- code )
40 PRIMITIVE: become ( old new -- )
41 PRIMITIVE: c-to-factor ( -- )
42 PRIMITIVE: callstack-bounds ( -- start end )
43 PRIMITIVE: check-datastack ( array in# out# -- ? )
44 PRIMITIVE: compute-identity-hashcode ( obj -- )
45 PRIMITIVE: context-object ( n -- obj )
46 PRIMITIVE: fpu-state ( -- )
47 PRIMITIVE: innermost-frame-executing ( callstack -- obj )
48 PRIMITIVE: innermost-frame-scan ( callstack -- n )
49 PRIMITIVE: lazy-jit-compile ( -- )
50 PRIMITIVE: leaf-signal-handler ( -- )
51 PRIMITIVE: set-callstack ( callstack -- * )
52 PRIMITIVE: set-context-object ( obj n -- )
53 PRIMITIVE: set-datastack ( array -- )
54 PRIMITIVE: set-fpu-state ( -- )
55 PRIMITIVE: set-innermost-frame-quotation ( n callstack -- )
56 PRIMITIVE: set-retainstack ( array -- )
57 PRIMITIVE: set-special-object ( obj n -- )
58 PRIMITIVE: signal-handler ( -- )
59 PRIMITIVE: special-object ( n -- obj )
60 PRIMITIVE: strip-stack-traces ( -- )
61 PRIMITIVE: tag ( object -- n )
62 PRIMITIVE: unwind-native-frames ( -- )
70 : 2over ( x y z -- x y z x y ) pick pick ; inline
72 : clear ( -- ) { } set-datastack ;
75 GENERIC: call ( callable -- )
77 GENERIC: execute ( word -- )
81 : ? ( ? true false -- true/false )
82 ! 'if' and '?' can be defined in terms of each other
83 ! because the JIT special-cases an 'if' preceded by
84 ! two literal quotations.
85 rot [ drop ] [ nip ] if ; inline
87 : if ( ..a ? true: ( ..a -- ..b ) false: ( ..a -- ..b ) -- ..b ) ? call ;
90 : unless ( ..a ? false: ( ..a -- ..a ) -- ..a )
91 swap [ drop ] [ call ] if ; inline
93 : when ( ..a ? true: ( ..a -- ..a ) -- ..a )
94 swap [ call ] [ drop ] if ; inline
97 : if* ( ..a ? true: ( ..a ? -- ..b ) false: ( ..a -- ..b ) -- ..b )
98 pick [ drop call ] [ 2nip call ] if ; inline
100 : when* ( ..a ? true: ( ..a ? -- ..a ) -- ..a )
101 over [ call ] [ 2drop ] if ; inline
103 : unless* ( ..a ? false: ( ..a -- ..a x ) -- ..a x )
104 over [ drop ] [ nip call ] if ; inline
106 : transmute* ( obj quot: ( obj -- obj/f ) -- new/old changed? )
107 over [ call ] dip over [ drop t ] [ nip f ] if ; inline
109 : transmute ( obj quot: ( obj -- new/old ) -- new/old ) transmute* drop ; inline
111 : ?transmute ( obj/f quot -- obj' ) dupd when ; inline
115 : ?when ( ..a obj cond: ( ..a obj -- obj/f ) true: ( ..a cond -- ..b ) -- ..b )
116 [ transmute* ] dip when ; inline
118 : ?unless ( ..a obj cond: ( ..a obj -- obj/f ) false: ( ..a default -- ..b ) -- ..b )
119 [ transmute* ] dip unless ; inline
121 : ?if ( ..a obj cond true: ( ..a cond -- ..b ) false: ( ..a default -- ..b ) -- ..b )
122 [ transmute* ] 2dip if ; inline
125 ! Not declared inline because the compiler special-cases them
127 : dip ( x quot -- x ) swap [ call ] dip ;
129 : 2dip ( x y quot -- x y ) swap [ dip ] dip ;
131 : 3dip ( x y z quot -- x y z ) swap [ 2dip ] dip ;
133 : 4dip ( w x y z quot -- w x y z ) swap [ 3dip ] dip ; inline
136 : tuck ( x y -- y x y ) dup -rot ; inline
138 : spin ( x y z -- z y x ) -rot swap ; inline
140 : rotd ( w x y z -- x y w z ) [ rot ] dip ; inline
142 : -rotd ( w x y z -- y w x z ) [ -rot ] dip ; inline
144 : roll ( w x y z -- x y z w ) rotd swap ; inline
146 : -roll ( w x y z -- z w x y ) swap -rotd ; inline
148 : nipd ( x y z -- y z ) [ nip ] dip ; inline
150 : overd ( x y z -- x y x z ) [ over ] dip ; inline
152 : pickd ( w x y z -- w x y w z ) [ pick ] dip ; inline
154 : 2nipd ( w x y z -- y z ) [ 2drop ] 2dip ; inline
156 : 3nipd ( v w x y z -- y z ) [ 3drop ] 2dip ; inline
158 : 3nip ( w x y z -- z ) 2nip nip ; inline
160 : 4nip ( v w x y z -- z ) 2nip 2nip ; inline
162 : 5nip ( u v w x y z -- z ) 3nip 2nip ; inline
164 : 5drop ( v w x y z -- ) 4drop drop ; inline
166 : reach ( w x y z -- w x y z w ) [ pick ] dip swap ; inline
169 : keep ( ..a x quot: ( ..a x -- ..b ) -- ..b x )
170 over [ call ] dip ; inline
172 : 2keep ( ..a x y quot: ( ..a x y -- ..b ) -- ..b x y )
173 [ 2dup ] dip 2dip ; inline
175 : 3keep ( ..a x y z quot: ( ..a x y z -- ..b ) -- ..b x y z )
176 [ 3dup ] dip 3dip ; inline
178 : 4keep ( ..a w x y z quot: ( ..a w x y z -- ..b ) -- ..b w x y z )
179 [ 4dup ] dip 4dip ; inline
181 : keepd ( ..a x y quot: ( ..a x y -- ..b ) -- ..b x )
184 : keepdd ( ..a x y z quot: ( ..a x y z -- ..b ) -- ..b x )
187 : 2keepd ( ..a x y z quot: ( ..a x y z -- ..b ) -- ..b x y )
192 [ keep ] dip call ; inline
195 [ [ keep ] dip keep ] dip call ; inline
199 [ 2keep ] dip call ; inline
201 : 2tri ( x y p q r -- )
202 [ [ 2keep ] dip 2keep ] dip call ; inline
205 : 3bi ( x y z p q -- )
206 [ 3keep ] dip call ; inline
208 : 3tri ( x y z p q r -- )
209 [ [ 3keep ] dip 3keep ] dip call ; inline
213 [ dip ] dip call ; inline
215 : tri* ( x y z p q r -- )
216 [ [ 2dip ] dip dip ] dip call ; inline
219 : 2bi* ( w x y z p q -- )
220 [ 2dip ] dip call ; inline
222 : 2tri* ( u v w x y z p q r -- )
223 [ 4dip ] 2dip 2bi* ; inline
226 : bi@ ( x y quot -- )
229 : tri@ ( x y z quot -- )
230 dup dup tri* ; inline
233 : 2bi@ ( w x y z quot -- )
236 : 2tri@ ( u v w x y z quot -- )
237 dup dup 2tri* ; inline
240 : 2curry ( obj1 obj2 quot -- curried )
243 : 3curry ( obj1 obj2 obj3 quot -- curried )
244 curry curry curry ; inline
246 : with ( param obj quot -- obj curried )
247 swapd [ swapd call ] 2curry ; inline
249 : 2with ( param1 param2 obj quot -- obj curried )
252 : withd ( param obj quot -- obj curried )
253 swapd [ -rotd call ] 2curry ; inline
255 : prepose ( quot1 quot2 -- composed )
256 swap compose ; inline
261 : currier ( quot -- quot' ) [ curry ] curry ; inline
265 : bi-curry ( x p q -- p' q' ) [ currier ] bi@ bi ; inline
267 : tri-curry ( x p q r -- p' q' r' ) [ currier ] tri@ tri ; inline
269 : bi-curry* ( x y p q -- p' q' ) [ currier ] bi@ bi* ; inline
271 : tri-curry* ( x y z p q r -- p' q' r' ) [ currier ] tri@ tri* ; inline
273 : bi-curry@ ( x y q -- p' q' ) currier bi@ ; inline
275 : tri-curry@ ( x y z q -- p' q' r' ) currier tri@ ; inline
278 UNION: boolean POSTPONE: t POSTPONE: f ;
280 : >boolean ( obj -- ? ) [ t ] [ f ] if ; inline
282 : not ( obj -- ? ) [ f ] [ t ] if ; inline
284 : and ( obj1 obj2 -- ? ) over ? ; inline
286 : or ( obj1 obj2 -- ? ) dupd ? ; inline
288 : or* ( obj1 obj2 -- obj2/obj1 second? ) [ nip t ] [ f ] if* ; inline
290 : xor ( obj1 obj2 -- ? ) [ f swap ? ] when* ; inline
292 : both? ( x y quot -- ? ) bi@ and ; inline
294 : either? ( x y quot -- ? ) bi@ or ; inline
296 : most ( x y quot -- z ) 2keep ? ; inline
298 : negate ( quot -- quot' ) [ not ] compose ; inline
301 : loop ( ... pred: ( ... -- ... ? ) -- ... )
302 [ call ] keep [ loop ] curry when ; inline recursive
304 : do ( pred body -- pred body )
307 : while ( ..a pred: ( ..a -- ..b ? ) body: ( ..b -- ..a ) -- ..b )
308 swap do compose [ loop ] curry when ; inline
310 : while* ( ..a pred: ( ..a -- ..b ? ) body: ( ..b ? -- ..a ) -- ..b )
311 [ [ dup ] compose ] dip while drop ; inline
313 : until ( ..a pred: ( ..a -- ..b ? ) body: ( ..b -- ..a ) -- ..b )
314 [ negate ] dip while ; inline
317 GENERIC: hashcode* ( depth obj -- code ) flushable
319 M: object hashcode* 2drop 0 ; inline
321 M: f hashcode* 2drop 31337 ; inline
323 : hashcode ( obj -- code ) 3 swap hashcode* ; inline
325 GENERIC: equal? ( obj1 obj2 -- ? )
327 M: object equal? 2drop f ; inline
329 TUPLE: identity-tuple ;
331 M: identity-tuple equal? 2drop f ; inline
333 : identity-hashcode ( obj -- code )
335 dup tag 1 eq? [ drop 0 ] [
336 dup (identity-hashcode) dup 0 eq? [
337 drop dup compute-identity-hashcode
343 M: identity-tuple hashcode* nip identity-hashcode ; inline
345 : = ( obj1 obj2 -- ? )
346 2dup eq? [ 2drop t ] [
347 2dup both-fixnums? [ 2drop f ] [ equal? ] if
350 : same? ( x y quot -- ? ) bi@ = ; inline
352 GENERIC: clone ( obj -- cloned )
354 M: object clone ; inline
356 M: callstack clone (clone) ; inline
359 GENERIC: new ( class -- tuple )
361 GENERIC: boa ( slots... class -- tuple )
363 ! Error handling -- defined early so that other files can
364 ! throw errors before continuations are loaded
365 GENERIC: throw ( error -- * )
367 ERROR: assert got expect ;
369 : assert= ( a b -- ) 2dup = [ 2drop ] [ assert ] if ;
373 : declare ( spec -- ) drop ;
375 : do-primitive ( number -- ) "Improper primitive call" throw ;
377 ! Special object count and identifiers must be kept in sync with:
379 CONSTANT: special-object-count 85
381 CONSTANT: OBJ-WALKER-HOOK 3
383 CONSTANT: OBJ-CALLCC-1 4
385 CONSTANT: ERROR-HANDLER-QUOT 5
387 CONSTANT: OBJ-CELL-SIZE 7
391 CONSTANT: OBJ-ARGS 10
392 CONSTANT: OBJ-STDIN 11
393 CONSTANT: OBJ-STDOUT 12
395 CONSTANT: OBJ-IMAGE 13
396 CONSTANT: OBJ-EXECUTABLE 14
398 CONSTANT: OBJ-EMBEDDED 15
399 CONSTANT: OBJ-EVAL-CALLBACK 16
400 CONSTANT: OBJ-YIELD-CALLBACK 17
401 CONSTANT: OBJ-SLEEP-CALLBACK 18
403 CONSTANT: OBJ-STARTUP-QUOT 20
404 CONSTANT: OBJ-GLOBAL 21
405 CONSTANT: OBJ-SHUTDOWN-QUOT 22
407 CONSTANT: JIT-PROLOG 23
408 CONSTANT: JIT-PRIMITIVE-WORD 24
409 CONSTANT: JIT-PRIMITIVE 25
410 CONSTANT: JIT-WORD-JUMP 26
411 CONSTANT: JIT-WORD-CALL 27
412 CONSTANT: JIT-IF-WORD 28
414 CONSTANT: JIT-SAFEPOINT 30
415 CONSTANT: JIT-EPILOG 31
416 CONSTANT: JIT-RETURN 32
417 CONSTANT: JIT-UNUSED 33
418 CONSTANT: JIT-PUSH-LITERAL 34
419 CONSTANT: JIT-DIP-WORD 35
421 CONSTANT: JIT-2DIP-WORD 37
422 CONSTANT: JIT-2DIP 38
423 CONSTANT: JIT-3DIP-WORD 39
424 CONSTANT: JIT-3DIP 40
425 CONSTANT: JIT-EXECUTE 41
426 CONSTANT: JIT-DECLARE-WORD 42
428 CONSTANT: C-TO-FACTOR-WORD 43
429 CONSTANT: LAZY-JIT-COMPILE-WORD 44
430 CONSTANT: UNWIND-NATIVE-FRAMES-WORD 45
431 CONSTANT: GET-FPU-STATE-WORD 46
432 CONSTANT: SET-FPU-STATE-WORD 47
433 CONSTANT: SIGNAL-HANDLER-WORD 48
434 CONSTANT: LEAF-SIGNAL-HANDLER-WORD 49
435 CONSTANT: WIN-EXCEPTION-HANDLER 50
437 CONSTANT: OBJ-SAMPLE-CALLSTACKS 51
439 CONSTANT: REDEFINITION-COUNTER 52
441 CONSTANT: CALLBACK-STUB 53
443 CONSTANT: PIC-LOAD 54
445 CONSTANT: PIC-TUPLE 56
446 CONSTANT: PIC-CHECK-TAG 57
447 CONSTANT: PIC-CHECK-TUPLE 58
449 CONSTANT: PIC-MISS-WORD 60
450 CONSTANT: PIC-MISS-TAIL-WORD 61
452 CONSTANT: MEGA-LOOKUP 62
453 CONSTANT: MEGA-LOOKUP-WORD 63
454 CONSTANT: MEGA-MISS-WORD 64
456 CONSTANT: OBJ-UNDEFINED 65
458 CONSTANT: OBJ-STDERR 66
460 CONSTANT: OBJ-STAGE2 67
462 CONSTANT: OBJ-CURRENT-THREAD 68
464 CONSTANT: OBJ-THREADS 69
465 CONSTANT: OBJ-RUN-QUEUE 70
466 CONSTANT: OBJ-SLEEP-QUEUE 71
468 CONSTANT: OBJ-VM-COMPILER 72
470 CONSTANT: OBJ-WAITING-CALLBACKS 73
472 CONSTANT: OBJ-SIGNAL-PIPE 74
474 CONSTANT: OBJ-VM-COMPILE-TIME 75
476 CONSTANT: OBJ-VM-VERSION 76
477 CONSTANT: OBJ-VM-GIT-LABEL 77
479 CONSTANT: OBJ-CANONICAL-TRUE 78
481 CONSTANT: OBJ-BIGNUM-ZERO 79
482 CONSTANT: OBJ-BIGNUM-POS-ONE 80
483 CONSTANT: OBJ-BIGNUM-NEG-ONE 81
485 ! Context object count and identifiers must be kept in sync with:
488 CONSTANT: context-object-count 4
490 CONSTANT: CONTEXT-OBJ-NAMESTACK 0
491 CONSTANT: CONTEXT-OBJ-CATCHSTACK 1
492 CONSTANT: CONTEXT-OBJ-CONTEXT 2
493 CONSTANT: CONTEXT-OBJ-IN-CALLBACK-P 3
495 ! Runtime errors must be kept in sync with:
496 ! basis/debugger/debugger.factor
499 ! VM adds this to kernel errors, so that user-space can identify them.
500 CONSTANT: KERNEL-ERROR 0xfac7
502 CONSTANT: kernel-error-count 20
504 CONSTANT: ERROR-EXPIRED 0
506 CONSTANT: ERROR-NOT-IMPLEMENTED 2
507 CONSTANT: ERROR-TYPE 3
508 CONSTANT: ERROR-DIVIDE-BY-ZERO 4
509 CONSTANT: ERROR-SIGNAL 5
510 CONSTANT: ERROR-ARRAY-SIZE 6
511 CONSTANT: ERROR-OUT-OF-FIXNUM-RANGE 7
512 CONSTANT: ERROR-FFI 8
513 CONSTANT: ERROR-UNDEFINED-SYMBOL 9
514 CONSTANT: ERROR-DATASTACK-UNDERFLOW 10
515 CONSTANT: ERROR-DATASTACK-OVERFLOW 11
516 CONSTANT: ERROR-RETAINSTACK-UNDERFLOW 12
517 CONSTANT: ERROR-RETAINSTACK-OVERFLOW 13
518 CONSTANT: ERROR-CALLSTACK-UNDERFLOW 14
519 CONSTANT: ERROR-CALLSTACK-OVERFLOW 15
520 CONSTANT: ERROR-MEMORY 16
521 CONSTANT: ERROR-FP-TRAP 17
522 CONSTANT: ERROR-INTERRUPT 18
523 CONSTANT: ERROR-CALLBACK-SPACE-OVERFLOW 19
525 PRIMITIVE: callstack-for ( context -- array )
526 PRIMITIVE: retainstack-for ( context -- array )
527 PRIMITIVE: datastack-for ( context -- array )
529 : context ( -- context )
530 CONTEXT-OBJ-CONTEXT context-object ; inline
534 : get-callstack ( -- callstack )
535 context callstack-for ; inline
537 : get-datastack ( -- array )
538 context datastack-for ; inline
540 : get-retainstack ( -- array )
541 context retainstack-for ; inline