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