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