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