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