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