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