]> gitweb.factorcode.org Git - factor.git/blob - core/kernel/kernel.factor
82e8553f5f48bef03b3047e6c364d314e0652914
[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 : prepose ( quot1 quot2 -- composed )
240     swap compose ; inline
241
242 ! Curried cleavers
243 <PRIVATE
244
245 : currier ( quot -- quot' ) [ curry ] curry ; inline
246
247 PRIVATE>
248
249 : bi-curry ( x p q -- p' q' ) [ currier ] bi@ bi ; inline
250
251 : tri-curry ( x p q r -- p' q' r' ) [ currier ] tri@ tri ; inline
252
253 : bi-curry* ( x y p q -- p' q' ) [ currier ] bi@ bi* ; inline
254
255 : tri-curry* ( x y z p q r -- p' q' r' ) [ currier ] tri@ tri* ; inline
256
257 : bi-curry@ ( x y q -- p' q' ) currier bi@ ; inline
258
259 : tri-curry@ ( x y z q -- p' q' r' ) currier tri@ ; inline
260
261 ! Booleans
262 UNION: boolean POSTPONE: t POSTPONE: f ;
263
264 : >boolean ( obj -- ? ) [ t ] [ f ] if ; inline
265
266 : not ( obj -- ? ) [ f ] [ t ] if ; inline
267
268 : and ( obj1 obj2 -- ? ) over ? ; inline
269
270 : or ( obj1 obj2 -- ? ) dupd ? ; inline
271
272 : xor ( obj1 obj2 -- ? ) [ f swap ? ] when* ; inline
273
274 : both? ( x y quot -- ? ) bi@ and ; inline
275
276 : either? ( x y quot -- ? ) bi@ or ; inline
277
278 : most ( x y quot -- z ) 2keep ? ; inline
279
280 : negate ( quot -- quot' ) [ not ] compose ; inline
281
282 ! Loops
283 : loop ( ... pred: ( ... -- ... ? ) -- ... )
284     [ call ] keep [ loop ] curry when ; inline recursive
285
286 : do ( pred body -- pred body )
287     dup 2dip ; inline
288
289 : while ( ..a pred: ( ..a -- ..b ? ) body: ( ..b -- ..a ) -- ..b )
290     swap do compose [ loop ] curry when ; inline
291
292 : while* ( ..a pred: ( ..a -- ..b ? ) body: ( ..b ? -- ..a ) -- ..b )
293     [ [ dup ] compose ] dip while drop ; inline
294
295 : until ( ..a pred: ( ..a -- ..b ? ) body: ( ..b -- ..a ) -- ..b )
296     [ negate ] dip while ; inline
297
298 ! Object protocol
299 GENERIC: hashcode* ( depth obj -- code ) flushable
300
301 M: object hashcode* 2drop 0 ; inline
302
303 M: f hashcode* 2drop 31337 ; inline
304
305 : hashcode ( obj -- code ) 3 swap hashcode* ; inline
306
307 : recursive-hashcode ( n obj quot -- code )
308     pick 0 <= [ 3drop 0 ] [ [ 1 - ] 2dip call ] if ; inline
309
310 GENERIC: equal? ( obj1 obj2 -- ? )
311
312 M: object equal? 2drop f ; inline
313
314 TUPLE: identity-tuple ;
315
316 M: identity-tuple equal? 2drop f ; inline
317
318 : identity-hashcode ( obj -- code )
319     dup tag 0 eq? [
320         dup tag 1 eq? [ drop 0 ] [
321             dup (identity-hashcode) dup 0 eq? [
322                 drop dup compute-identity-hashcode
323                 (identity-hashcode)
324             ] [ nip ] if
325         ] if
326     ] unless ; inline
327
328 M: identity-tuple hashcode* nip identity-hashcode ; inline
329
330 : = ( obj1 obj2 -- ? )
331     2dup eq? [ 2drop t ] [
332         2dup both-fixnums? [ 2drop f ] [ equal? ] if
333     ] if ; inline
334
335 : same? ( x y quot -- ? ) bi@ = ; inline
336
337 GENERIC: clone ( obj -- cloned )
338
339 M: object clone ; inline
340
341 M: callstack clone (clone) ; inline
342
343 ! Tuple construction
344 GENERIC: new ( class -- tuple )
345
346 GENERIC: boa ( slots... class -- tuple )
347
348 ! Error handling -- defined early so that other files can
349 ! throw errors before continuations are loaded
350 GENERIC: throw ( error -- * )
351
352 ERROR: assert got expect ;
353
354 : assert= ( a b -- ) 2dup = [ 2drop ] [ assert ] if ;
355
356 <PRIVATE
357
358 : declare ( spec -- ) drop ;
359
360 : do-primitive ( number -- ) "Improper primitive call" throw ;
361
362 ! Special object count and identifiers must be kept in sync with:
363 !   vm/objects.hpp
364 CONSTANT: special-object-count 85
365
366 CONSTANT: OBJ-WALKER-HOOK 3
367
368 CONSTANT: OBJ-CALLCC-1 4
369
370 CONSTANT: ERROR-HANDLER-QUOT 5
371
372 CONSTANT: OBJ-CELL-SIZE 7
373 CONSTANT: OBJ-CPU 8
374 CONSTANT: OBJ-OS 9
375
376 CONSTANT: OBJ-ARGS 10
377 CONSTANT: OBJ-STDIN 11
378 CONSTANT: OBJ-STDOUT 12
379
380 CONSTANT: OBJ-IMAGE 13
381 CONSTANT: OBJ-EXECUTABLE 14
382
383 CONSTANT: OBJ-EMBEDDED 15
384 CONSTANT: OBJ-EVAL-CALLBACK 16
385 CONSTANT: OBJ-YIELD-CALLBACK 17
386 CONSTANT: OBJ-SLEEP-CALLBACK 18
387
388 CONSTANT: OBJ-STARTUP-QUOT 20
389 CONSTANT: OBJ-GLOBAL 21
390 CONSTANT: OBJ-SHUTDOWN-QUOT 22
391
392 CONSTANT: JIT-PROLOG 23
393 CONSTANT: JIT-PRIMITIVE-WORD 24
394 CONSTANT: JIT-PRIMITIVE 25
395 CONSTANT: JIT-WORD-JUMP 26
396 CONSTANT: JIT-WORD-CALL 27
397 CONSTANT: JIT-IF-WORD 28
398 CONSTANT: JIT-IF 29
399 CONSTANT: JIT-SAFEPOINT 30
400 CONSTANT: JIT-EPILOG 31
401 CONSTANT: JIT-RETURN 32
402 CONSTANT: JIT-UNUSED 33
403 CONSTANT: JIT-PUSH-LITERAL 34
404 CONSTANT: JIT-DIP-WORD 35
405 CONSTANT: JIT-DIP 36
406 CONSTANT: JIT-2DIP-WORD 37
407 CONSTANT: JIT-2DIP 38
408 CONSTANT: JIT-3DIP-WORD 39
409 CONSTANT: JIT-3DIP 40
410 CONSTANT: JIT-EXECUTE 41
411 CONSTANT: JIT-DECLARE-WORD 42
412
413 CONSTANT: C-TO-FACTOR-WORD 43
414 CONSTANT: LAZY-JIT-COMPILE-WORD 44
415 CONSTANT: UNWIND-NATIVE-FRAMES-WORD 45
416 CONSTANT: GET-FPU-STATE-WORD 46
417 CONSTANT: SET-FPU-STATE-WORD 47
418 CONSTANT: SIGNAL-HANDLER-WORD 48
419 CONSTANT: LEAF-SIGNAL-HANDLER-WORD 49
420 CONSTANT: WIN-EXCEPTION-HANDLER 50
421
422 CONSTANT: OBJ-SAMPLE-CALLSTACKS 51
423
424 CONSTANT: REDEFINITION-COUNTER 52
425
426 CONSTANT: CALLBACK-STUB 53
427
428 CONSTANT: PIC-LOAD 54
429 CONSTANT: PIC-TAG 55
430 CONSTANT: PIC-TUPLE 56
431 CONSTANT: PIC-CHECK-TAG 57
432 CONSTANT: PIC-CHECK-TUPLE 58
433 CONSTANT: PIC-HIT 59
434 CONSTANT: PIC-MISS-WORD 60
435 CONSTANT: PIC-MISS-TAIL-WORD 61
436
437 CONSTANT: MEGA-LOOKUP 62
438 CONSTANT: MEGA-LOOKUP-WORD 63
439 CONSTANT: MEGA-MISS-WORD 64
440
441 CONSTANT: OBJ-UNDEFINED 65
442
443 CONSTANT: OBJ-STDERR 66
444
445 CONSTANT: OBJ-STAGE2 67
446
447 CONSTANT: OBJ-CURRENT-THREAD 68
448
449 CONSTANT: OBJ-THREADS 69
450 CONSTANT: OBJ-RUN-QUEUE 70
451 CONSTANT: OBJ-SLEEP-QUEUE 71
452
453 CONSTANT: OBJ-VM-COMPILER 72
454
455 CONSTANT: OBJ-WAITING-CALLBACKS 73
456
457 CONSTANT: OBJ-SIGNAL-PIPE 74
458
459 CONSTANT: OBJ-VM-COMPILE-TIME 75
460
461 CONSTANT: OBJ-VM-VERSION 76
462 CONSTANT: OBJ-VM-GIT-LABEL 77
463
464 CONSTANT: OBJ-CANONICAL-TRUE 78
465
466 CONSTANT: OBJ-BIGNUM-ZERO 79
467 CONSTANT: OBJ-BIGNUM-POS-ONE 80
468 CONSTANT: OBJ-BIGNUM-NEG-ONE 81
469
470 ! Context object count and identifiers must be kept in sync with:
471 !   vm/contexts.hpp
472
473 CONSTANT: context-object-count 4
474
475 CONSTANT: CONTEXT-OBJ-NAMESTACK 0
476 CONSTANT: CONTEXT-OBJ-CATCHSTACK 1
477 CONSTANT: CONTEXT-OBJ-CONTEXT 2
478 CONSTANT: CONTEXT-OBJ-IN-CALLBACK-P 3
479
480 ! Runtime errors must be kept in sync with:
481 !   basis/debugger/debugger.factor
482 !   vm/errors.hpp
483
484 ! VM adds this to kernel errors, so that user-space can identify them.
485 CONSTANT: KERNEL-ERROR 0xfac7
486
487 CONSTANT: kernel-error-count 20
488
489 CONSTANT: ERROR-EXPIRED 0
490 CONSTANT: ERROR-IO      1
491 CONSTANT: ERROR-NOT-IMPLEMENTED 2
492 CONSTANT: ERROR-TYPE 3
493 CONSTANT: ERROR-DIVIDE-BY-ZERO 4
494 CONSTANT: ERROR-SIGNAL 5
495 CONSTANT: ERROR-ARRAY-SIZE 6
496 CONSTANT: ERROR-OUT-OF-FIXNUM-RANGE 7
497 CONSTANT: ERROR-FFI 8
498 CONSTANT: ERROR-UNDEFINED-SYMBOL 9
499 CONSTANT: ERROR-DATASTACK-UNDERFLOW 10
500 CONSTANT: ERROR-DATASTACK-OVERFLOW 11
501 CONSTANT: ERROR-RETAINSTACK-UNDERFLOW 12
502 CONSTANT: ERROR-RETAINSTACK-OVERFLOW 13
503 CONSTANT: ERROR-CALLSTACK-UNDERFLOW 14
504 CONSTANT: ERROR-CALLSTACK-OVERFLOW 15
505 CONSTANT: ERROR-MEMORY 16
506 CONSTANT: ERROR-FP-TRAP 17
507 CONSTANT: ERROR-INTERRUPT 18
508 CONSTANT: ERROR-CALLBACK-SPACE-OVERFLOW 19
509
510 PRIMITIVE: callstack-for ( context -- array )
511 PRIMITIVE: retainstack-for ( context -- array )
512 PRIMITIVE: datastack-for ( context -- array )
513
514 : context ( -- context )
515     CONTEXT-OBJ-CONTEXT context-object ; inline
516
517 PRIVATE>
518
519 : get-callstack ( -- callstack )
520     context callstack-for ; inline
521
522 : get-datastack ( -- array )
523     context datastack-for ; inline
524
525 : get-retainstack ( -- array )
526     context retainstack-for ; inline