]> gitweb.factorcode.org Git - factor.git/blob - core/kernel/kernel.factor
VM: the context_object_count must be wrong, there are only 4 of them
[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 BUILTIN: callstack ;
7 BUILTIN: tuple ;
8 BUILTIN: wrapper { wrapped read-only } ;
9
10 PRIMITIVE: -rot ( x y z -- z x y )
11 PRIMITIVE: dup ( x -- x x )
12 PRIMITIVE: dupd ( x y -- x x y )
13 PRIMITIVE: drop ( x -- )
14 PRIMITIVE: nip ( x y -- y )
15 PRIMITIVE: over ( x y -- x y x )
16 PRIMITIVE: pick ( x y z -- x y z x )
17 PRIMITIVE: rot ( x y z -- y z x )
18 PRIMITIVE: swap ( x y -- y x )
19 PRIMITIVE: swapd ( x y z -- y x z )
20 PRIMITIVE: 2drop ( x y -- )
21 PRIMITIVE: 2dup ( x y -- x y x y )
22 PRIMITIVE: 2nip ( x y z -- z )
23 PRIMITIVE: 3drop ( x y z -- )
24 PRIMITIVE: 3dup ( x y z -- x y z x y z )
25 PRIMITIVE: 4drop ( w x y z -- )
26 PRIMITIVE: 4dup ( w x y z -- w x y z w x y z )
27
28 PRIMITIVE: (clone) ( obj -- newobj )
29 PRIMITIVE: eq? ( obj1 obj2 -- ? )
30 PRIMITIVE: <wrapper> ( obj -- wrapper )
31 PRIMITIVE: get-datastack ( -- array )
32 PRIMITIVE: get-callstack ( -- callstack )
33 PRIMITIVE: get-retainstack ( -- array )
34 PRIMITIVE: die ( -- )
35 PRIMITIVE: callstack>array ( callstack -- array )
36
37 <PRIVATE
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: ffi-leaf-signal-handler ( -- )
48 PRIMITIVE: ffi-signal-handler ( -- )
49 PRIMITIVE: fpu-state ( -- )
50 PRIMITIVE: innermost-frame-executing ( callstack -- obj )
51 PRIMITIVE: innermost-frame-scan ( callstack -- n )
52 PRIMITIVE: lazy-jit-compile ( -- )
53 PRIMITIVE: leaf-signal-handler ( -- )
54 PRIMITIVE: set-callstack ( callstack -- * )
55 PRIMITIVE: set-context-object ( obj n -- )
56 PRIMITIVE: set-datastack ( array -- )
57 PRIMITIVE: set-fpu-state ( -- )
58 PRIMITIVE: set-innermost-frame-quotation ( n callstack -- )
59 PRIMITIVE: set-retainstack ( array -- )
60 PRIMITIVE: set-special-object ( obj n -- )
61 PRIMITIVE: signal-handler ( -- )
62 PRIMITIVE: special-object ( n -- obj )
63 PRIMITIVE: strip-stack-traces ( -- )
64 PRIMITIVE: tag ( object -- n )
65 PRIMITIVE: unimplemented ( -- * )
66 PRIMITIVE: unwind-native-frames ( -- )
67 PRIVATE>
68
69 DEFER: dip
70 DEFER: 2dip
71 DEFER: 3dip
72
73 ! Stack stuff
74 : 2over ( x y z -- x y z x y ) pick pick ; inline
75
76 : clear ( -- ) { } set-datastack ;
77
78 ! Combinators
79 GENERIC: call ( callable -- )
80
81 GENERIC: execute ( word -- )
82
83 DEFER: if
84
85 : ? ( ? true false -- true/false )
86     #! 'if' and '?' can be defined in terms of each other
87     #! because the JIT special-cases an 'if' preceeded by
88     #! two literal quotations.
89     rot [ drop ] [ nip ] if ; inline
90
91 : if ( ..a ? true: ( ..a -- ..b ) false: ( ..a -- ..b ) -- ..b ) ? call ;
92
93 ! Single branch
94 : unless ( ..a ? false: ( ..a -- ..a ) -- ..a )
95     swap [ drop ] [ call ] if ; inline
96
97 : when ( ..a ? true: ( ..a -- ..a ) -- ..a )
98     swap [ call ] [ drop ] if ; inline
99
100 ! Anaphoric
101 : if* ( ..a ? true: ( ..a ? -- ..b ) false: ( ..a -- ..b ) -- ..b )
102     pick [ drop call ] [ 2nip call ] if ; inline
103
104 : when* ( ..a ? true: ( ..a ? -- ..a ) -- ..a )
105     over [ call ] [ 2drop ] if ; inline
106
107 : unless* ( ..a ? false: ( ..a -- ..a x ) -- ..a x )
108     over [ drop ] [ nip call ] if ; inline
109
110 ! Default
111 : ?if ( ..a default cond true: ( ..a cond -- ..b ) false: ( ..a default -- ..b ) -- ..b )
112     pick [ drop [ drop ] 2dip call ] [ 2nip call ] if ; inline
113
114 ! Dippers.
115 ! Not declared inline because the compiler special-cases them
116
117 : dip ( x quot -- x ) swap [ call ] dip ;
118
119 : 2dip ( x y quot -- x y ) swap [ dip ] dip ;
120
121 : 3dip ( x y z quot -- x y z ) swap [ 2dip ] dip ;
122
123 : 4dip ( w x y z quot -- w x y z ) swap [ 3dip ] dip ; inline
124
125 ! Keepers
126 : keep ( ..a x quot: ( ..a x -- ..b ) -- ..b x )
127     over [ call ] dip ; inline
128
129 : 2keep ( ..a x y quot: ( ..a x y -- ..b ) -- ..b x y )
130     [ 2dup ] dip 2dip ; inline
131
132 : 3keep ( ..a x y z quot: ( ..a x y z -- ..b ) -- ..b x y z )
133     [ 3dup ] dip 3dip ; inline
134
135 : 4keep ( ..a w x y z quot: ( ..a w x y z -- ..b ) -- ..b w x y z )
136     [ 4dup ] dip 4dip ; inline
137
138 ! Cleavers
139 : bi ( x p q -- )
140     [ keep ] dip call ; inline
141
142 : tri ( x p q r -- )
143     [ [ keep ] dip keep ] dip call ; inline
144
145 ! Double cleavers
146 : 2bi ( x y p q -- )
147     [ 2keep ] dip call ; inline
148
149 : 2tri ( x y p q r -- )
150     [ [ 2keep ] dip 2keep ] dip call ; inline
151
152 ! Triple cleavers
153 : 3bi ( x y z p q -- )
154     [ 3keep ] dip call ; inline
155
156 : 3tri ( x y z p q r -- )
157     [ [ 3keep ] dip 3keep ] dip call ; inline
158
159 ! Spreaders
160 : bi* ( x y p q -- )
161     [ dip ] dip call ; inline
162
163 : tri* ( x y z p q r -- )
164     [ [ 2dip ] dip dip ] dip call ; inline
165
166 ! Double spreaders
167 : 2bi* ( w x y z p q -- )
168     [ 2dip ] dip call ; inline
169
170 : 2tri* ( u v w x y z p q r -- )
171     [ 4dip ] 2dip 2bi* ; inline
172
173 ! Appliers
174 : bi@ ( x y quot -- )
175     dup bi* ; inline
176
177 : tri@ ( x y z quot -- )
178     dup dup tri* ; inline
179
180 ! Double appliers
181 : 2bi@ ( w x y z quot -- )
182     dup 2bi* ; inline
183
184 : 2tri@ ( u v w x y z quot -- )
185     dup dup 2tri* ; inline
186
187 ! Quotation building
188 : 2curry ( obj1 obj2 quot -- curry )
189     curry curry ; inline
190
191 : 3curry ( obj1 obj2 obj3 quot -- curry )
192     curry curry curry ; inline
193
194 : with ( param obj quot -- obj curry )
195     swapd [ swapd call ] 2curry ; inline
196
197 : 2with ( param1 param2 obj quot -- obj curry )
198     with with ; inline
199
200 : prepose ( quot1 quot2 -- compose )
201     swap compose ; inline
202
203 ! Curried cleavers
204 <PRIVATE
205
206 : [curry] ( quot -- quot' ) [ curry ] curry ; inline
207
208 PRIVATE>
209
210 : bi-curry ( x p q -- p' q' ) [ [curry] ] bi@ bi ; inline
211
212 : tri-curry ( x p q r -- p' q' r' ) [ [curry] ] tri@ tri ; inline
213
214 : bi-curry* ( x y p q -- p' q' ) [ [curry] ] bi@ bi* ; inline
215
216 : tri-curry* ( x y z p q r -- p' q' r' ) [ [curry] ] tri@ tri* ; inline
217
218 : bi-curry@ ( x y q -- p' q' ) [curry] bi@ ; inline
219
220 : tri-curry@ ( x y z q -- p' q' r' ) [curry] tri@ ; inline
221
222 ! Booleans
223 UNION: boolean POSTPONE: t POSTPONE: f ;
224
225 : >boolean ( obj -- ? ) [ t ] [ f ] if ; inline
226
227 : not ( obj -- ? ) [ f ] [ t ] if ; inline
228
229 : and ( obj1 obj2 -- ? ) over ? ; inline
230
231 : or ( obj1 obj2 -- ? ) dupd ? ; inline
232
233 : xor ( obj1 obj2 -- ? ) [ f swap ? ] when* ; inline
234
235 : both? ( x y quot -- ? ) bi@ and ; inline
236
237 : either? ( x y quot -- ? ) bi@ or ; inline
238
239 : most ( x y quot -- z ) 2keep ? ; inline
240
241 ! Loops
242 : loop ( ... pred: ( ... -- ... ? ) -- ... )
243     [ call ] keep [ loop ] curry when ; inline recursive
244
245 : do ( pred body -- pred body )
246     dup 2dip ; inline
247
248 : while ( ..a pred: ( ..a -- ..b ? ) body: ( ..b -- ..a ) -- ..b )
249     swap do compose [ loop ] curry when ; inline
250
251 : until ( ..a pred: ( ..a -- ..b ? ) body: ( ..b -- ..a ) -- ..b )
252     [ [ not ] compose ] dip while ; inline
253
254 ! Object protocol
255 GENERIC: hashcode* ( depth obj -- code )
256
257 M: object hashcode* 2drop 0 ; inline
258
259 M: f hashcode* 2drop 31337 ; inline
260
261 : hashcode ( obj -- code ) 3 swap hashcode* ; inline
262
263 : identity-hashcode ( obj -- code )
264     dup tag 0 eq? [
265         dup tag 1 eq? [ drop 0 ] [
266             dup (identity-hashcode) dup 0 eq? [
267                 drop dup compute-identity-hashcode
268                 (identity-hashcode)
269             ] [ nip ] if
270         ] if
271     ] unless ; inline
272
273 GENERIC: equal? ( obj1 obj2 -- ? )
274
275 M: object equal? 2drop f ; inline
276
277 TUPLE: identity-tuple ;
278
279 M: identity-tuple equal? 2drop f ; inline
280
281 M: identity-tuple hashcode* nip identity-hashcode ; inline
282
283 : = ( obj1 obj2 -- ? )
284     2dup eq? [ 2drop t ] [
285         2dup both-fixnums? [ 2drop f ] [ equal? ] if
286     ] if ; inline
287
288 : same? ( x y quot -- ? ) bi@ = ; inline
289
290 GENERIC: clone ( obj -- cloned )
291
292 M: object clone ; inline
293
294 M: callstack clone (clone) ; inline
295
296 ! Tuple construction
297 GENERIC: new ( class -- tuple )
298
299 GENERIC: boa ( slots... class -- tuple )
300
301 ! Error handling -- defined early so that other files can
302 ! throw errors before continuations are loaded
303 GENERIC: throw ( error -- * )
304
305 ERROR: assert got expect ;
306
307 : assert= ( a b -- ) 2dup = [ 2drop ] [ assert ] if ;
308
309 <PRIVATE
310
311 : declare ( spec -- ) drop ;
312
313 : do-primitive ( number -- ) "Improper primitive call" throw ;
314
315 ! Special object count and identifiers must be kept in sync with:
316 !   vm/objects.hpp
317 !   basis/bootstrap/image/image.factor
318
319 CONSTANT: special-object-count 80
320
321 CONSTANT: OBJ-WALKER-HOOK 3
322
323 CONSTANT: OBJ-CALLCC-1 4
324
325 CONSTANT: ERROR-HANDLER-QUOT 5
326 CONSTANT: OBJ-ERROR 6
327
328 CONSTANT: OBJ-CELL-SIZE 7
329 CONSTANT: OBJ-CPU 8
330 CONSTANT: OBJ-OS 9
331
332 CONSTANT: OBJ-ARGS 10
333 CONSTANT: OBJ-STDIN 11
334 CONSTANT: OBJ-STDOUT 12
335
336 CONSTANT: OBJ-IMAGE 13
337 CONSTANT: OBJ-EXECUTABLE 14
338
339 CONSTANT: OBJ-EMBEDDED 15
340 CONSTANT: OBJ-EVAL-CALLBACK 16
341 CONSTANT: OBJ-YIELD-CALLBACK 17
342 CONSTANT: OBJ-SLEEP-CALLBACK 18
343
344 CONSTANT: OBJ-STARTUP-QUOT 20
345 CONSTANT: OBJ-GLOBAL 21
346 CONSTANT: OBJ-SHUTDOWN-QUOT 22
347
348 CONSTANT: JIT-PROLOG 23
349 CONSTANT: JIT-PRIMITIVE-WORD 24
350 CONSTANT: JIT-PRIMITIVE 25
351 CONSTANT: JIT-WORD-JUMP 26
352 CONSTANT: JIT-WORD-CALL 27
353 CONSTANT: JIT-IF-WORD 28
354 CONSTANT: JIT-IF 29
355 CONSTANT: JIT-SAFEPOINT 30
356 CONSTANT: JIT-EPILOG 31
357 CONSTANT: JIT-RETURN 32
358 CONSTANT: JIT-PROFILING 33
359 CONSTANT: JIT-PUSH-IMMEDIATE 34
360 CONSTANT: JIT-DIP-WORD 35
361 CONSTANT: JIT-DIP 36
362 CONSTANT: JIT-2DIP-WORD 37
363 CONSTANT: JIT-2DIP 38
364 CONSTANT: JIT-3DIP-WORD 39
365 CONSTANT: JIT-3DIP 40
366 CONSTANT: JIT-EXECUTE 41
367 CONSTANT: JIT-DECLARE-WORD 42
368
369 CONSTANT: C-TO-FACTOR-WORD 43
370 CONSTANT: LAZY-JIT-COMPILE-WORD 44
371 CONSTANT: UNWIND-NATIVE-FRAMES-WORD 45
372 CONSTANT: GET-FPU-STATE-WORD 46
373 CONSTANT: SET-FPU-STATE-WORD 47
374 CONSTANT: SIGNAL-HANDLER-WORD 48
375 CONSTANT: LEAF-SIGNAL-HANDLER-WORD 49
376 CONSTANT: FFI-SIGNAL-HANDLER-WORD 50
377 CONSTANT: FFI-LEAF-SIGNAL-HANDLER-WORD 51
378
379 CONSTANT: REDEFINITION-COUNTER 52
380
381 CONSTANT: CALLBACK-STUB 53
382
383 CONSTANT: PIC-LOAD 54
384 CONSTANT: PIC-TAG 55
385 CONSTANT: PIC-TUPLE 56
386 CONSTANT: PIC-CHECK-TAG 57
387 CONSTANT: PIC-CHECK-TUPLE 58
388 CONSTANT: PIC-HIT 59
389 CONSTANT: PIC-MISS-WORD 60
390 CONSTANT: PIC-MISS-TAIL-WORD 61
391
392 CONSTANT: MEGA-LOOKUP 62
393 CONSTANT: MEGA-LOOKUP-WORD 63
394 CONSTANT: MEGA-MISS-WORD 64
395
396 CONSTANT: OBJ-UNDEFINED 65
397
398 CONSTANT: OBJ-STDERR 66
399
400 CONSTANT: OBJ-STAGE2 67
401
402 CONSTANT: OBJ-CURRENT-THREAD 68
403
404 CONSTANT: OBJ-THREADS 69
405 CONSTANT: OBJ-RUN-QUEUE 70
406 CONSTANT: OBJ-SLEEP-QUEUE 71
407
408 CONSTANT: OBJ-VM-COMPILER 72
409
410 CONSTANT: OBJ-WAITING-CALLBACKS 73
411
412 CONSTANT: OBJ-SIGNAL-PIPE 74
413
414 CONSTANT: OBJ-VM-COMPILE-TIME 75
415
416 CONSTANT: OBJ-VM-VERSION 76
417 CONSTANT: OBJ-VM-GIT-LABEL 77
418
419 ! Context object count and identifiers must be kept in sync with:
420 !   vm/contexts.hpp
421
422 CONSTANT: context-object-count 4
423
424 CONSTANT: CONTEXT-OBJ-NAMESTACK 0
425 CONSTANT: CONTEXT-OBJ-CATCHSTACK 1
426 CONSTANT: CONTEXT-OBJ-CONTEXT 2
427 CONSTANT: CONTEXT-OBJ-IN-CALLBACK-P 3
428
429 ! Runtime errors must be kept in sync with:
430 !   basis/debugger/debugger.factor
431 !   vm/errors.hpp
432
433 CONSTANT: kernel-error-count 21
434
435 CONSTANT: ERROR-EXPIRED 0
436 CONSTANT: ERROR-IO      1
437 CONSTANT: ERROR-NOT-IMPLEMENTED 2
438 CONSTANT: ERROR-TYPE 3
439 CONSTANT: ERROR-DIVIDE-BY-ZERO 4
440 CONSTANT: ERROR-SIGNAL 5
441 CONSTANT: ERROR-ARRAY-SIZE 6
442 CONSTANT: ERROR-OUT-OF-FIXNUM-RANGE 7
443 CONSTANT: ERROR-C-STRING 8
444 CONSTANT: ERROR-FFI 9
445 CONSTANT: ERROR-UNDEFINED-SYMBOL 10
446 CONSTANT: ERROR-DATASTACK-UNDERFLOW 11
447 CONSTANT: ERROR-DATASTACK-OVERFLOW 12
448 CONSTANT: ERROR-RETAINSTACK-UNDERFLOW 13
449 CONSTANT: ERROR-RETAINSTACK-OVERFLOW 14
450 CONSTANT: ERROR-CALLSTACK-UNDERFLOW 15
451 CONSTANT: ERROR-CALLSTACK-OVERFLOW 16
452 CONSTANT: ERROR-MEMORY 17
453 CONSTANT: ERROR-FP-TRAP 18
454 CONSTANT: ERROR-INTERRUPT 19
455 CONSTANT: ERROR-CALLBACK-SPACE-OVERFLOW 20
456
457 PRIVATE>