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 ;
11 : 2over ( x y z -- x y z x y ) pick pick ; inline
13 : clear ( -- ) { } set-datastack ;
16 GENERIC: call ( callable -- )
18 GENERIC: execute ( word -- )
20 GENERIC: ?execute ( word -- value )
26 : ? ( ? true false -- true/false )
27 #! 'if' and '?' can be defined in terms of each other
28 #! because the JIT special-cases an 'if' preceeded by
29 #! two literal quotations.
30 rot [ drop ] [ nip ] if ; inline
32 : if ( ..a ? true: ( ..a -- ..b ) false: ( ..a -- ..b ) -- ..b ) ? call ;
35 : unless ( ..a ? false: ( ..a -- ..a ) -- ..a )
36 swap [ drop ] [ call ] if ; inline
38 : when ( ..a ? true: ( ..a -- ..a ) -- ..a )
39 swap [ call ] [ drop ] if ; inline
42 : if* ( ..a ? true: ( ..a ? -- ..b ) false: ( ..a -- ..b ) -- ..b )
43 pick [ drop call ] [ 2nip call ] if ; inline
45 : when* ( ..a ? true: ( ..a ? -- ..a ) -- ..a )
46 over [ call ] [ 2drop ] if ; inline
48 : unless* ( ..a ? false: ( ..a -- ..a x ) -- ..a x )
49 over [ drop ] [ nip call ] if ; inline
52 : ?if ( ..a default cond true: ( ..a cond -- ..b ) false: ( ..a default -- ..b ) -- ..b )
53 pick [ drop [ drop ] 2dip call ] [ 2nip call ] if ; inline
56 ! Not declared inline because the compiler special-cases them
58 : dip ( x quot -- x ) swap [ call ] dip ;
60 : 2dip ( x y quot -- x y ) swap [ dip ] dip ;
62 : 3dip ( x y z quot -- x y z ) swap [ 2dip ] dip ;
64 : 4dip ( w x y z quot -- w x y z ) swap [ 3dip ] dip ; inline
67 : keep ( ..a x quot: ( ..a x -- ..b ) -- ..b x )
68 over [ call ] dip ; inline
70 : 2keep ( ..a x y quot: ( ..a x y -- ..b ) -- ..b x y )
71 [ 2dup ] dip 2dip ; inline
73 : 3keep ( ..a x y z quot: ( ..a x y z -- ..b ) -- ..b x y z )
74 [ 3dup ] dip 3dip ; inline
78 [ keep ] dip call ; inline
81 [ [ keep ] dip keep ] dip call ; inline
85 [ 2keep ] dip call ; inline
87 : 2tri ( x y p q r -- )
88 [ [ 2keep ] dip 2keep ] dip call ; inline
91 : 3bi ( x y z p q -- )
92 [ 3keep ] dip call ; inline
94 : 3tri ( x y z p q r -- )
95 [ [ 3keep ] dip 3keep ] dip call ; inline
99 [ dip ] dip call ; inline
101 : tri* ( x y z p q r -- )
102 [ [ 2dip ] dip dip ] dip call ; inline
105 : 2bi* ( w x y z p q -- )
106 [ 2dip ] dip call ; inline
108 : 2tri* ( u v w x y z p q r -- )
109 [ 4dip ] 2dip 2bi* ; inline
112 : bi@ ( x y quot -- )
115 : tri@ ( x y z quot -- )
116 dup dup tri* ; inline
119 : 2bi@ ( w x y z quot -- )
122 : 2tri@ ( u v w x y z quot -- )
123 dup dup 2tri* ; inline
126 : 2curry ( obj1 obj2 quot -- curry )
129 : 3curry ( obj1 obj2 obj3 quot -- curry )
130 curry curry curry ; inline
132 : with ( param obj quot -- obj curry )
133 swapd [ swapd call ] 2curry ; inline
135 : prepose ( quot1 quot2 -- compose )
136 swap compose ; inline
141 : [curry] ( quot -- quot' ) [ curry ] curry ; inline
145 : bi-curry ( x p q -- p' q' ) [ [curry] ] bi@ bi ; inline
147 : tri-curry ( x p q r -- p' q' r' ) [ [curry] ] tri@ tri ; inline
149 : bi-curry* ( x y p q -- p' q' ) [ [curry] ] bi@ bi* ; inline
151 : tri-curry* ( x y z p q r -- p' q' r' ) [ [curry] ] tri@ tri* ; inline
153 : bi-curry@ ( x y q -- p' q' ) [curry] bi@ ; inline
155 : tri-curry@ ( x y z q -- p' q' r' ) [curry] tri@ ; inline
158 UNION: boolean POSTPONE: t POSTPONE: f ;
160 : >boolean ( obj -- ? ) [ t ] [ f ] if ; inline
162 : not ( obj -- ? ) [ f ] [ t ] if ; inline
164 : and ( obj1 obj2 -- ? ) over ? ; inline
166 : or ( obj1 obj2 -- ? ) dupd ? ; inline
168 : xor ( obj1 obj2 -- ? ) [ f swap ? ] when* ; inline
170 : both? ( x y quot -- ? ) bi@ and ; inline
172 : either? ( x y quot -- ? ) bi@ or ; inline
174 : most ( x y quot -- z ) 2keep ? ; inline
177 : loop ( ... pred: ( ... -- ... ? ) -- ... )
178 [ call ] keep [ loop ] curry when ; inline recursive
180 : do ( pred body -- pred body )
183 : while ( ..a pred: ( ..a -- ..b ? ) body: ( ..b -- ..a ) -- ..b )
184 swap do compose [ loop ] curry when ; inline
186 : until ( ..a pred: ( ..a -- ..b ? ) body: ( ..b -- ..a ) -- ..b )
187 [ [ not ] compose ] dip while ; inline
190 GENERIC: hashcode* ( depth obj -- code )
192 M: object hashcode* 2drop 0 ; inline
194 M: f hashcode* 2drop 31337 ; inline
196 : hashcode ( obj -- code ) 3 swap hashcode* ; inline
198 : identity-hashcode ( obj -- code )
200 dup tag 1 eq? [ drop 0 ] [
201 dup (identity-hashcode) dup 0 eq? [
202 drop dup compute-identity-hashcode
208 GENERIC: equal? ( obj1 obj2 -- ? )
210 M: object equal? 2drop f ; inline
212 TUPLE: identity-tuple ;
214 M: identity-tuple equal? 2drop f ; inline
216 M: identity-tuple hashcode* nip identity-hashcode ; inline
218 : = ( obj1 obj2 -- ? )
219 2dup eq? [ 2drop t ] [
220 2dup both-fixnums? [ 2drop f ] [ equal? ] if
223 : same? ( x y quot -- ? ) bi@ = ; inline
225 GENERIC: clone ( obj -- cloned )
227 M: object clone ; inline
229 M: callstack clone (clone) ; inline
232 GENERIC: new ( class -- tuple )
234 GENERIC: boa ( slots... class -- tuple )
236 ! Error handling -- defined early so that other files can
237 ! throw errors before continuations are loaded
238 GENERIC: throw ( error -- * )
240 ERROR: assert got expect ;
242 : assert= ( a b -- ) 2dup = [ 2drop ] [ assert ] if ;
246 : declare ( spec -- ) drop ;
248 : do-primitive ( number -- ) "Improper primitive call" throw ;
250 ! Special object count and identifiers must be kept in sync with:
252 ! basis/bootstrap/image/image.factor
254 CONSTANT: special-object-count 80
256 CONSTANT: OBJ-WALKER-HOOK 3
258 CONSTANT: OBJ-CALLCC-1 4
260 CONSTANT: ERROR-HANDLER-QUOT 5
261 CONSTANT: OBJ-ERROR 6
263 CONSTANT: OBJ-CELL-SIZE 7
267 CONSTANT: OBJ-ARGS 10
268 CONSTANT: OBJ-STDIN 11
269 CONSTANT: OBJ-STDOUT 12
271 CONSTANT: OBJ-IMAGE 13
272 CONSTANT: OBJ-EXECUTABLE 14
274 CONSTANT: OBJ-EMBEDDED 15
275 CONSTANT: OBJ-EVAL-CALLBACK 16
276 CONSTANT: OBJ-YIELD-CALLBACK 17
277 CONSTANT: OBJ-SLEEP-CALLBACK 18
279 CONSTANT: OBJ-STARTUP-QUOT 20
280 CONSTANT: OBJ-GLOBAL 21
281 CONSTANT: OBJ-SHUTDOWN-QUOT 22
283 CONSTANT: JIT-PROLOG 23
284 CONSTANT: JIT-PRIMITIVE-WORD 24
285 CONSTANT: JIT-PRIMITIVE 25
286 CONSTANT: JIT-WORD-JUMP 26
287 CONSTANT: JIT-WORD-CALL 27
288 CONSTANT: JIT-IF-WORD 28
290 CONSTANT: JIT-SAFEPOINT 30
291 CONSTANT: JIT-EPILOG 31
292 CONSTANT: JIT-RETURN 32
293 CONSTANT: JIT-PROFILING 33
294 CONSTANT: JIT-PUSH-IMMEDIATE 34
295 CONSTANT: JIT-DIP-WORD 35
297 CONSTANT: JIT-2DIP-WORD 37
298 CONSTANT: JIT-2DIP 38
299 CONSTANT: JIT-3DIP-WORD 39
300 CONSTANT: JIT-3DIP 40
301 CONSTANT: JIT-EXECUTE 41
302 CONSTANT: JIT-DECLARE-WORD 42
304 CONSTANT: C-TO-FACTOR-WORD 43
305 CONSTANT: LAZY-JIT-COMPILE-WORD 44
306 CONSTANT: UNWIND-NATIVE-FRAMES-WORD 45
307 CONSTANT: GET-FPU-STATE-WORD 46
308 CONSTANT: SET-FPU-STATE-WORD 47
309 CONSTANT: SIGNAL-HANDLER-WORD 48
310 CONSTANT: LEAF-SIGNAL-HANDLER-WORD 49
311 CONSTANT: FFI-SIGNAL-HANDLER-WORD 50
312 CONSTANT: FFI-LEAF-SIGNAL-HANDLER-WORD 51
314 CONSTANT: REDEFINITION-COUNTER 52
316 CONSTANT: CALLBACK-STUB 53
318 CONSTANT: PIC-LOAD 54
320 CONSTANT: PIC-TUPLE 56
321 CONSTANT: PIC-CHECK-TAG 57
322 CONSTANT: PIC-CHECK-TUPLE 58
324 CONSTANT: PIC-MISS-WORD 60
325 CONSTANT: PIC-MISS-TAIL-WORD 61
327 CONSTANT: MEGA-LOOKUP 62
328 CONSTANT: MEGA-LOOKUP-WORD 63
329 CONSTANT: MEGA-MISS-WORD 64
331 CONSTANT: OBJ-UNDEFINED 65
333 CONSTANT: OBJ-STDERR 66
335 CONSTANT: OBJ-STAGE2 67
337 CONSTANT: OBJ-CURRENT-THREAD 68
339 CONSTANT: OBJ-THREADS 69
340 CONSTANT: OBJ-RUN-QUEUE 70
341 CONSTANT: OBJ-SLEEP-QUEUE 71
343 CONSTANT: OBJ-VM-COMPILER 72
345 CONSTANT: OBJ-WAITING-CALLBACKS 73
347 CONSTANT: OBJ-SIGNAL-PIPE 74
349 ! Context object count and identifiers must be kept in sync with:
352 CONSTANT: context-object-count 10
354 CONSTANT: CONTEXT-OBJ-NAMESTACK 0
355 CONSTANT: CONTEXT-OBJ-CATCHSTACK 1
356 CONSTANT: CONTEXT-OBJ-CONTEXT 2
357 CONSTANT: CONTEXT-OBJ-IN-CALLBACK-P 3