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