]> gitweb.factorcode.org Git - factor.git/blob - core/kernel/kernel.factor
constants for special object hardcoded literals
[factor.git] / core / kernel / kernel.factor
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 ;
4 IN: kernel
5
6 DEFER: dip
7 DEFER: 2dip
8 DEFER: 3dip
9
10 ! Stack stuff
11 : 2over ( x y z -- x y z x y ) pick pick ; inline
12
13 : clear ( -- ) { } set-datastack ;
14
15 ! Combinators
16 GENERIC: call ( callable -- )
17
18 GENERIC: execute ( word -- )
19
20 GENERIC: ?execute ( word -- value )
21
22 M: object ?execute ;
23
24 DEFER: if
25
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
31
32 : if ( ..a ? true: ( ..a -- ..b ) false: ( ..a -- ..b ) -- ..b ) ? call ;
33
34 ! Single branch
35 : unless ( ..a ? false: ( ..a -- ..a ) -- ..a )
36     swap [ drop ] [ call ] if ; inline
37
38 : when ( ..a ? true: ( ..a -- ..a ) -- ..a )
39     swap [ call ] [ drop ] if ; inline
40
41 ! Anaphoric
42 : if* ( ..a ? true: ( ..a ? -- ..b ) false: ( ..a -- ..b ) -- ..b )
43     pick [ drop call ] [ 2nip call ] if ; inline
44
45 : when* ( ..a ? true: ( ..a ? -- ..a ) -- ..a )
46     over [ call ] [ 2drop ] if ; inline
47
48 : unless* ( ..a ? false: ( ..a -- ..a x ) -- ..a x )
49     over [ drop ] [ nip call ] if ; inline
50
51 ! Default
52 : ?if ( ..a default cond true: ( ..a cond -- ..b ) false: ( ..a default -- ..b ) -- ..b )
53     pick [ drop [ drop ] 2dip call ] [ 2nip call ] if ; inline
54
55 ! Dippers.
56 ! Not declared inline because the compiler special-cases them
57
58 : dip ( x quot -- x ) swap [ call ] dip ;
59
60 : 2dip ( x y quot -- x y ) swap [ dip ] dip ;
61
62 : 3dip ( x y z quot -- x y z ) swap [ 2dip ] dip ;
63
64 : 4dip ( w x y z quot -- w x y z ) swap [ 3dip ] dip ; inline
65
66 ! Keepers
67 : keep ( ..a x quot: ( ..a x -- ..b ) -- ..b x )
68     over [ call ] dip ; inline
69
70 : 2keep ( ..a x y quot: ( ..a x y -- ..b ) -- ..b x y )
71     [ 2dup ] dip 2dip ; inline
72
73 : 3keep ( ..a x y z quot: ( ..a x y z -- ..b ) -- ..b x y z )
74     [ 3dup ] dip 3dip ; inline
75
76 ! Cleavers
77 : bi ( x p q -- )
78     [ keep ] dip call ; inline
79
80 : tri ( x p q r -- )
81     [ [ keep ] dip keep ] dip call ; inline
82
83 ! Double cleavers
84 : 2bi ( x y p q -- )
85     [ 2keep ] dip call ; inline
86
87 : 2tri ( x y p q r -- )
88     [ [ 2keep ] dip 2keep ] dip call ; inline
89
90 ! Triple cleavers
91 : 3bi ( x y z p q -- )
92     [ 3keep ] dip call ; inline
93
94 : 3tri ( x y z p q r -- )
95     [ [ 3keep ] dip 3keep ] dip call ; inline
96
97 ! Spreaders
98 : bi* ( x y p q -- )
99     [ dip ] dip call ; inline
100
101 : tri* ( x y z p q r -- )
102     [ [ 2dip ] dip dip ] dip call ; inline
103
104 ! Double spreaders
105 : 2bi* ( w x y z p q -- )
106     [ 2dip ] dip call ; inline
107
108 : 2tri* ( u v w x y z p q r -- )
109     [ 4dip ] 2dip 2bi* ; inline
110
111 ! Appliers
112 : bi@ ( x y quot -- )
113     dup bi* ; inline
114
115 : tri@ ( x y z quot -- )
116     dup dup tri* ; inline
117
118 ! Double appliers
119 : 2bi@ ( w x y z quot -- )
120     dup 2bi* ; inline
121
122 : 2tri@ ( u v w x y z quot -- )
123     dup dup 2tri* ; inline
124
125 ! Quotation building
126 : 2curry ( obj1 obj2 quot -- curry )
127     curry curry ; inline
128
129 : 3curry ( obj1 obj2 obj3 quot -- curry )
130     curry curry curry ; inline
131
132 : with ( param obj quot -- obj curry )
133     swapd [ swapd call ] 2curry ; inline
134
135 : prepose ( quot1 quot2 -- compose )
136     swap compose ; inline
137
138 ! Curried cleavers
139 <PRIVATE
140
141 : [curry] ( quot -- quot' ) [ curry ] curry ; inline
142
143 PRIVATE>
144
145 : bi-curry ( x p q -- p' q' ) [ [curry] ] bi@ bi ; inline
146
147 : tri-curry ( x p q r -- p' q' r' ) [ [curry] ] tri@ tri ; inline
148
149 : bi-curry* ( x y p q -- p' q' ) [ [curry] ] bi@ bi* ; inline
150
151 : tri-curry* ( x y z p q r -- p' q' r' ) [ [curry] ] tri@ tri* ; inline
152
153 : bi-curry@ ( x y q -- p' q' ) [curry] bi@ ; inline
154
155 : tri-curry@ ( x y z q -- p' q' r' ) [curry] tri@ ; inline
156
157 ! Booleans
158 UNION: boolean POSTPONE: t POSTPONE: f ;
159
160 : >boolean ( obj -- ? ) [ t ] [ f ] if ; inline
161
162 : not ( obj -- ? ) [ f ] [ t ] if ; inline
163
164 : and ( obj1 obj2 -- ? ) over ? ; inline
165
166 : or ( obj1 obj2 -- ? ) dupd ? ; inline
167
168 : xor ( obj1 obj2 -- ? ) [ f swap ? ] when* ; inline
169
170 : both? ( x y quot -- ? ) bi@ and ; inline
171
172 : either? ( x y quot -- ? ) bi@ or ; inline
173
174 : most ( x y quot -- z ) 2keep ? ; inline
175
176 ! Loops
177 : loop ( ... pred: ( ... -- ... ? ) -- ... )
178     [ call ] keep [ loop ] curry when ; inline recursive
179
180 : do ( pred body -- pred body )
181     dup 2dip ; inline
182
183 : while ( ..a pred: ( ..a -- ..b ? ) body: ( ..b -- ..a ) -- ..b )
184     swap do compose [ loop ] curry when ; inline
185
186 : until ( ..a pred: ( ..a -- ..b ? ) body: ( ..b -- ..a ) -- ..b )
187     [ [ not ] compose ] dip while ; inline
188
189 ! Object protocol
190 GENERIC: hashcode* ( depth obj -- code )
191
192 M: object hashcode* 2drop 0 ; inline
193
194 M: f hashcode* 2drop 31337 ; inline
195
196 : hashcode ( obj -- code ) 3 swap hashcode* ; inline
197
198 : identity-hashcode ( obj -- code )
199     dup tag 0 eq? [
200         dup tag 1 eq? [ drop 0 ] [
201             dup (identity-hashcode) dup 0 eq? [
202                 drop dup compute-identity-hashcode
203                 (identity-hashcode)
204             ] [ nip ] if
205         ] if
206     ] unless ; inline
207
208 GENERIC: equal? ( obj1 obj2 -- ? )
209
210 M: object equal? 2drop f ; inline
211
212 TUPLE: identity-tuple ;
213
214 M: identity-tuple equal? 2drop f ; inline
215
216 M: identity-tuple hashcode* nip identity-hashcode ; inline
217
218 : = ( obj1 obj2 -- ? )
219     2dup eq? [ 2drop t ] [
220         2dup both-fixnums? [ 2drop f ] [ equal? ] if
221     ] if ; inline
222
223 GENERIC: clone ( obj -- cloned )
224
225 M: object clone ; inline
226
227 M: callstack clone (clone) ; inline
228
229 ! Tuple construction
230 GENERIC: new ( class -- tuple )
231
232 GENERIC: boa ( slots... class -- tuple )
233
234 ! Error handling -- defined early so that other files can
235 ! throw errors before continuations are loaded
236 GENERIC: throw ( error -- * )
237
238 ERROR: assert got expect ;
239
240 : assert= ( a b -- ) 2dup = [ 2drop ] [ assert ] if ;
241
242 <PRIVATE
243
244 : declare ( spec -- ) drop ;
245
246 : do-primitive ( number -- ) "Improper primitive call" throw ;
247
248 ! Special object count and identifiers must be kept in sync with:
249 !   vm/objects.hpp
250 !   basis/bootstrap/image/image.factor
251
252 CONSTANT: special-object-count 80
253
254 CONSTANT: OBJ-WALKER-HOOK 3
255
256 CONSTANT: OBJ-CALLCC-1 4
257
258 CONSTANT: ERROR-HANDLER-QUOT 5
259 CONSTANT: OBJ-ERROR 6
260
261 CONSTANT: OBJ-CELL-SIZE 7
262 CONSTANT: OBJ-CPU 8
263 CONSTANT: OBJ-OS 9
264
265 CONSTANT: OBJ-ARGS 10
266 CONSTANT: OBJ-STDIN 11
267 CONSTANT: OBJ-STDOUT 12
268
269 CONSTANT: OBJ-IMAGE 13
270 CONSTANT: OBJ-EXECUTABLE 14
271
272 CONSTANT: OBJ-EMBEDDED 15
273 CONSTANT: OBJ-EVAL-CALLBACK 16
274 CONSTANT: OBJ-YIELD-CALLBACK 17
275 CONSTANT: OBJ-SLEEP-CALLBACK 18
276
277 CONSTANT: OBJ-STARTUP-QUOT 20
278 CONSTANT: OBJ-GLOBAL 21
279 CONSTANT: OBJ-SHUTDOWN-QUOT 22
280
281 CONSTANT: JIT-PROLOG 23
282 CONSTANT: JIT-PRIMITIVE-WORD 24
283 CONSTANT: JIT-PRIMITIVE 25
284 CONSTANT: JIT-WORD-JUMP 26
285 CONSTANT: JIT-WORD-CALL 27
286 CONSTANT: JIT-IF-WORD 28
287 CONSTANT: JIT-IF 29
288 CONSTANT: JIT-EPILOG 30
289 CONSTANT: JIT-RETURN 31
290 CONSTANT: JIT-PROFILING 32
291 CONSTANT: JIT-PUSH-IMMEDIATE 33
292 CONSTANT: JIT-DIP-WORD 34
293 CONSTANT: JIT-DIP 35
294 CONSTANT: JIT-2DIP-WORD 36
295 CONSTANT: JIT-2DIP 37
296 CONSTANT: JIT-3DIP-WORD 38
297 CONSTANT: JIT-3DIP 39
298 CONSTANT: JIT-EXECUTE 40
299 CONSTANT: JIT-DECLARE-WORD 41
300
301 CONSTANT: C-TO-FACTOR-WORD 42
302 CONSTANT: LAZY-JIT-COMPILE-WORD 43
303 CONSTANT: UNWIND-NATIVE-FRAMES-WORD 44
304 CONSTANT: GET-FPU-STATE-WORD 45
305 CONSTANT: SET-FPU-STATE-WORD 46
306 CONSTANT: SIGNAL-HANDLER-WORD 47
307 CONSTANT: LEAF-SIGNAL-HANDLER-WORD 48
308
309 CONSTANT: REDEFINITION-COUNTER 49
310
311 CONSTANT: CALLBACK-STUB 50
312
313 CONSTANT: PIC-LOAD 51
314 CONSTANT: PIC-TAG 52
315 CONSTANT: PIC-TUPLE 53
316 CONSTANT: PIC-CHECK-TAG 54
317 CONSTANT: PIC-CHECK-TUPLE 55
318 CONSTANT: PIC-HIT 56
319 CONSTANT: PIC-MISS-WORD 57
320 CONSTANT: PIC-MISS-TAIL-WORD 58
321
322 CONSTANT: MEGA-LOOKUP 59
323 CONSTANT: MEGA-LOOKUP-WORD 60
324 CONSTANT: MEGA-MISS-WORD 61
325
326 CONSTANT: OBJ-UNDEFINED 62
327
328 CONSTANT: OBJ-STDERR 63
329
330 CONSTANT: OBJ-STAGE2 64
331
332 CONSTANT: OBJ-CURRENT-THREAD 65
333
334 CONSTANT: OBJ-THREADS 66
335 CONSTANT: OBJ-RUN-QUEUE 67
336 CONSTANT: OBJ-SLEEP-QUEUE 68
337
338 CONSTANT: OBJ-VM-COMPILER 69
339
340 CONSTANT: OBJ-WAITING-CALLBACKS 70
341
342 ! Context object count and identifiers must be kept in sync with:
343 !   vm/contexts.hpp
344
345 CONSTANT: context-object-count 10
346
347 CONSTANT: CONTEXT-OBJ-NAMESTACK 0
348 CONSTANT: CONTEXT-OBJ-CATCHSTACK 1
349 CONSTANT: CONTEXT-OBJ-CONTEXT 2
350 CONSTANT: CONTEXT-OBJ-IN-CALLBACK-P 3
351
352 PRIVATE>