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