1 ! Copyright (C) 2004, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: alien arrays byte-arrays generic hashtables
4 hashtables.private io kernel math math.private math.order
5 namespaces make parser sequences strings vectors words
6 quotations assocs layouts classes classes.builtin classes.tuple
7 classes.tuple.private kernel.private vocabs vocabs.loader
8 source-files definitions slots classes.union
9 classes.intersection classes.predicate compiler.units
10 bootstrap.image.private io.files accessors combinators ;
11 IN: bootstrap.primitives
13 "Creating primitives and basic runtime structures..." print flush
15 H{ } clone sub-primitives set
17 "vocab:bootstrap/syntax.factor" parse-file
19 "vocab:cpu/" architecture get {
21 { "winnt-x86.64" "x86/64/winnt" }
22 { "unix-x86.64" "x86/64/unix" }
23 { "linux-ppc" "ppc/linux" }
24 { "macosx-ppc" "ppc/macosx" }
26 } ?at [ "Bad architecture: " prepend throw ] unless
27 "/bootstrap.factor" 3append parse-file
29 "vocab:bootstrap/layouts/layouts.factor" parse-file
31 ! Now we have ( syntax-quot arch-quot layouts-quot ) on the stack
33 ! Bring up a bare cross-compiling vocabulary.
34 "syntax" vocab vocab-words bootstrap-syntax set {
37 changed-definitions changed-generics changed-effects
38 outdated-generics forgotten-definitions
39 root-cache source-files update-map implementors-map
40 } [ H{ } clone swap set ] each
44 ! Vocabulary for slot accessors
45 "accessors" create-vocab drop
47 dummy-compiler compiler-impl set
53 ! After we execute bootstrap/layouts
54 num-types get f <array> builtins set
58 ! Create some empty vocabs where the below primitives and
68 "classes.tuple.private"
71 "continuations.private"
73 "generic.single.private"
102 "tools.profiler.private"
108 } [ create-vocab drop ] each
111 : lookup-type-number ( word -- n )
112 global [ target-word ] bind type-number ;
114 : register-builtin ( class -- )
115 [ dup lookup-type-number "type" set-word-prop ]
116 [ dup "type" word-prop builtins get set-nth ]
117 [ f f f builtin-class define-class ]
120 : prepare-slots ( slots -- slots' )
121 [ [ dup pair? [ first2 create ] when ] map ] map ;
123 : define-builtin-slots ( class slots -- )
124 prepare-slots make-slots 1 finalize-slots
125 [ "slots" set-word-prop ] [ define-accessors ] 2bi ;
127 : define-builtin ( symbol slotspec -- )
128 [ [ define-builtin-predicate ] keep ] dip define-builtin-slots ;
130 "fixnum" "math" create register-builtin
131 "bignum" "math" create register-builtin
132 "tuple" "kernel" create register-builtin
133 "float" "math" create register-builtin
134 "f" "syntax" lookup register-builtin
135 "array" "arrays" create register-builtin
136 "wrapper" "kernel" create register-builtin
137 "callstack" "kernel" create register-builtin
138 "string" "strings" create register-builtin
139 "quotation" "quotations" create register-builtin
140 "dll" "alien" create register-builtin
141 "alien" "alien" create register-builtin
142 "word" "words" create register-builtin
143 "byte-array" "byte-arrays" create register-builtin
145 ! We need this before defining c-ptr below
146 "f" "syntax" lookup { } define-builtin
148 "f" "syntax" create [ not ] "predicate" set-word-prop
149 "f?" "syntax" vocab-words delete-at
152 "c-ptr" "alien" create [
153 "alien" "alien" lookup ,
154 "f" "syntax" lookup ,
155 "byte-array" "byte-arrays" lookup ,
156 ] { } make define-union-class
158 ! A predicate class used for declarations
159 "array-capacity" "sequences.private" create
160 "fixnum" "math" lookup
163 bootstrap-max-array-capacity <fake-bignum> [ fixnum<= ] curry ,
166 define-predicate-class
168 "array-capacity" "sequences.private" lookup
169 [ >fixnum ] bootstrap-max-array-capacity <fake-bignum> [ fixnum-bitand ] curry append
170 "coercer" set-word-prop
172 ! Catch-all class for providing a default method.
173 "object" "kernel" create
174 [ f f { } intersection-class define-class ]
175 [ [ drop t ] "predicate" set-word-prop ]
178 "object?" "kernel" vocab-words delete-at
180 ! Class of objects with object tag
181 "hi-tag" "kernel.private" create
182 builtins get num-tags get tail define-union-class
184 ! Empty class with no instances
185 "null" "kernel" create
186 [ f { } f union-class define-class ]
187 [ [ drop f ] "predicate" set-word-prop ]
190 "null?" "kernel" vocab-words delete-at
192 "fixnum" "math" create { } define-builtin
193 "fixnum" "math" create ">fixnum" "math" create 1quotation "coercer" set-word-prop
195 "bignum" "math" create { } define-builtin
196 "bignum" "math" create ">bignum" "math" create 1quotation "coercer" set-word-prop
198 "float" "math" create { } define-builtin
199 "float" "math" create ">float" "math" create 1quotation "coercer" set-word-prop
201 "array" "arrays" create {
202 { "length" { "array-capacity" "sequences.private" } read-only }
205 "wrapper" "kernel" create {
206 { "wrapped" read-only }
209 "string" "strings" create {
210 { "length" { "array-capacity" "sequences.private" } read-only }
214 "quotation" "quotations" create {
215 { "array" { "array" "arrays" } read-only }
220 "dll" "alien" create {
221 { "path" { "byte-array" "byte-arrays" } read-only }
224 "alien" "alien" create {
225 { "underlying" { "c-ptr" "alien" } read-only }
229 "word" "words" create {
230 { "hashcode" { "fixnum" "math" } }
233 { "def" { "quotation" "quotations" } initial: [ ] }
237 { "counter" { "fixnum" "math" } }
238 { "sub-primitive" read-only }
241 "byte-array" "byte-arrays" create {
242 { "length" { "array-capacity" "sequences.private" } read-only }
245 "callstack" "kernel" create { } define-builtin
247 "tuple" "kernel" create
248 [ { } define-builtin ]
249 [ define-tuple-layout ]
252 ! Create special tombstone values
253 "tombstone" "hashtables.private" create
255 { "state" } define-tuple-class
257 "((empty))" "hashtables.private" create
258 "tombstone" "hashtables.private" lookup f
259 2array >tuple 1quotation (( -- value )) define-inline
261 "((tombstone))" "hashtables.private" create
262 "tombstone" "hashtables.private" lookup t
263 2array >tuple 1quotation (( -- value )) define-inline
266 "curry" "kernel" create
271 } prepare-slots define-tuple-class
273 "curry" "kernel" lookup
275 [ f "inline" set-word-prop ]
280 callable instance-check-quot %
286 (( obj quot -- curry )) define-declared
288 "compose" "kernel" create
291 { "first" read-only }
292 { "second" read-only }
293 } prepare-slots define-tuple-class
295 "compose" "kernel" lookup
297 [ f "inline" set-word-prop ]
302 callable instance-check-quot [ dip ] curry %
303 callable instance-check-quot %
309 (( quot1 quot2 -- compose )) define-declared
311 ! Sub-primitive words
312 : make-sub-primitive ( word vocab effect -- )
313 [ create dup 1quotation ] dip define-declared ;
316 { "(execute)" "kernel.private" (( word -- )) }
317 { "(call)" "kernel.private" (( quot -- )) }
318 { "both-fixnums?" "math.private" (( x y -- ? )) }
319 { "fixnum+fast" "math.private" (( x y -- z )) }
320 { "fixnum-fast" "math.private" (( x y -- z )) }
321 { "fixnum*fast" "math.private" (( x y -- z )) }
322 { "fixnum-bitand" "math.private" (( x y -- z )) }
323 { "fixnum-bitor" "math.private" (( x y -- z )) }
324 { "fixnum-bitxor" "math.private" (( x y -- z )) }
325 { "fixnum-bitnot" "math.private" (( x -- y )) }
326 { "fixnum-mod" "math.private" (( x y -- z )) }
327 { "fixnum-shift-fast" "math.private" (( x y -- z )) }
328 { "fixnum/i-fast" "math.private" (( x y -- z )) }
329 { "fixnum/mod-fast" "math.private" (( x y -- z w )) }
330 { "fixnum<" "math.private" (( x y -- ? )) }
331 { "fixnum<=" "math.private" (( x y -- z )) }
332 { "fixnum>" "math.private" (( x y -- ? )) }
333 { "fixnum>=" "math.private" (( x y -- ? )) }
334 { "drop" "kernel" (( x -- )) }
335 { "2drop" "kernel" (( x y -- )) }
336 { "3drop" "kernel" (( x y z -- )) }
337 { "dup" "kernel" (( x -- x x )) }
338 { "2dup" "kernel" (( x y -- x y x y )) }
339 { "3dup" "kernel" (( x y z -- x y z x y z )) }
340 { "rot" "kernel" (( x y z -- y z x )) }
341 { "-rot" "kernel" (( x y z -- z x y )) }
342 { "dupd" "kernel" (( x y -- x x y )) }
343 { "swapd" "kernel" (( x y z -- y x z )) }
344 { "nip" "kernel" (( x y -- y )) }
345 { "2nip" "kernel" (( x y z -- z )) }
346 { "tuck" "kernel" (( x y -- y x y )) }
347 { "over" "kernel" (( x y -- x y x )) }
348 { "pick" "kernel" (( x y z -- x y z x )) }
349 { "swap" "kernel" (( x y -- y x )) }
350 { "eq?" "kernel" (( obj1 obj2 -- ? )) }
351 { "tag" "kernel.private" (( object -- n )) }
352 { "slot" "slots.private" (( obj m -- value )) }
353 { "get-local" "locals.backend" (( n -- obj )) }
354 { "load-local" "locals.backend" (( obj -- )) }
355 { "drop-locals" "locals.backend" (( n -- )) }
356 { "mega-cache-lookup" "generic.single.private" (( methods index cache -- )) }
357 } [ first3 make-sub-primitive ] each
360 : make-primitive ( word vocab n effect -- )
362 [ create dup reset-word ] dip
363 [ do-primitive ] curry
364 ] dip define-declared ;
367 { "bignum>fixnum" "math.private" (( x -- y )) }
368 { "float>fixnum" "math.private" (( x -- y )) }
369 { "fixnum>bignum" "math.private" (( x -- y )) }
370 { "float>bignum" "math.private" (( x -- y )) }
371 { "fixnum>float" "math.private" (( x -- y )) }
372 { "bignum>float" "math.private" (( x -- y )) }
373 { "(string>float)" "math.parser.private" (( str -- n/f )) }
374 { "(float>string)" "math.parser.private" (( n -- str )) }
375 { "float>bits" "math" (( x -- n )) }
376 { "double>bits" "math" (( x -- n )) }
377 { "bits>float" "math" (( n -- x )) }
378 { "bits>double" "math" (( n -- x )) }
379 { "fixnum+" "math.private" (( x y -- z )) }
380 { "fixnum-" "math.private" (( x y -- z )) }
381 { "fixnum*" "math.private" (( x y -- z )) }
382 { "fixnum/i" "math.private" (( x y -- z )) }
383 { "fixnum/mod" "math.private" (( x y -- z w )) }
384 { "fixnum-shift" "math.private" (( x y -- z )) }
385 { "bignum=" "math.private" (( x y -- ? )) }
386 { "bignum+" "math.private" (( x y -- z )) }
387 { "bignum-" "math.private" (( x y -- z )) }
388 { "bignum*" "math.private" (( x y -- z )) }
389 { "bignum/i" "math.private" (( x y -- z )) }
390 { "bignum-mod" "math.private" (( x y -- z )) }
391 { "bignum/mod" "math.private" (( x y -- z w )) }
392 { "bignum-bitand" "math.private" (( x y -- z )) }
393 { "bignum-bitor" "math.private" (( x y -- z )) }
394 { "bignum-bitxor" "math.private" (( x y -- z )) }
395 { "bignum-bitnot" "math.private" (( x -- y )) }
396 { "bignum-shift" "math.private" (( x y -- z )) }
397 { "bignum<" "math.private" (( x y -- ? )) }
398 { "bignum<=" "math.private" (( x y -- ? )) }
399 { "bignum>" "math.private" (( x y -- ? )) }
400 { "bignum>=" "math.private" (( x y -- ? )) }
401 { "bignum-bit?" "math.private" (( n x -- ? )) }
402 { "bignum-log2" "math.private" (( x -- n )) }
403 { "byte-array>bignum" "math" (( x -- y )) }
404 { "float=" "math.private" (( x y -- ? )) }
405 { "float+" "math.private" (( x y -- z )) }
406 { "float-" "math.private" (( x y -- z )) }
407 { "float*" "math.private" (( x y -- z )) }
408 { "float/f" "math.private" (( x y -- z )) }
409 { "float-mod" "math.private" (( x y -- z )) }
410 { "float<" "math.private" (( x y -- ? )) }
411 { "float<=" "math.private" (( x y -- ? )) }
412 { "float>" "math.private" (( x y -- ? )) }
413 { "float>=" "math.private" (( x y -- ? )) }
414 { "float-u<" "math.private" (( x y -- ? )) }
415 { "float-u<=" "math.private" (( x y -- ? )) }
416 { "float-u>" "math.private" (( x y -- ? )) }
417 { "float-u>=" "math.private" (( x y -- ? )) }
418 { "(word)" "words.private" (( name vocab -- word )) }
419 { "word-xt" "words" (( word -- start end )) }
420 { "getenv" "kernel.private" (( n -- obj )) }
421 { "setenv" "kernel.private" (( obj n -- )) }
422 { "(exists?)" "io.files.private" (( path -- ? )) }
423 { "minor-gc" "memory" (( -- )) }
424 { "gc" "memory" (( -- )) }
425 { "compact-gc" "memory" (( -- )) }
426 { "gc-stats" "memory" f }
427 { "(save-image)" "memory.private" (( path -- )) }
428 { "(save-image-and-exit)" "memory.private" (( path -- )) }
429 { "datastack" "kernel" (( -- ds )) }
430 { "retainstack" "kernel" (( -- rs )) }
431 { "callstack" "kernel" (( -- cs )) }
432 { "set-datastack" "kernel" (( ds -- )) }
433 { "set-retainstack" "kernel" (( rs -- )) }
434 { "set-callstack" "kernel" (( cs -- )) }
435 { "(exit)" "system" (( n -- )) }
436 { "data-room" "memory" (( -- cards decks generations )) }
437 { "code-room" "memory" (( -- code-total code-used code-free largest-free-block )) }
438 { "micros" "system" (( -- us )) }
439 { "modify-code-heap" "compiler.units" (( alist -- )) }
440 { "(dlopen)" "alien.libraries" (( path -- dll )) }
441 { "(dlsym)" "alien.libraries" (( name dll -- alien )) }
442 { "dlclose" "alien.libraries" (( dll -- )) }
443 { "<byte-array>" "byte-arrays" (( n -- byte-array )) }
444 { "(byte-array)" "byte-arrays" (( n -- byte-array )) }
445 { "<displaced-alien>" "alien" (( displacement c-ptr -- alien )) }
446 { "alien-signed-cell" "alien.accessors" (( c-ptr n -- value )) }
447 { "set-alien-signed-cell" "alien.accessors" (( value c-ptr n -- )) }
448 { "alien-unsigned-cell" "alien.accessors" (( c-ptr n -- value )) }
449 { "set-alien-unsigned-cell" "alien.accessors" (( value c-ptr n -- )) }
450 { "alien-signed-8" "alien.accessors" (( c-ptr n -- value )) }
451 { "set-alien-signed-8" "alien.accessors" (( value c-ptr n -- )) }
452 { "alien-unsigned-8" "alien.accessors" (( c-ptr n -- value )) }
453 { "set-alien-unsigned-8" "alien.accessors" (( value c-ptr n -- )) }
454 { "alien-signed-4" "alien.accessors" (( c-ptr n -- value )) }
455 { "set-alien-signed-4" "alien.accessors" (( value c-ptr n -- )) }
456 { "alien-unsigned-4" "alien.accessors" (( c-ptr n -- value )) }
457 { "set-alien-unsigned-4" "alien.accessors" (( value c-ptr n -- )) }
458 { "alien-signed-2" "alien.accessors" (( c-ptr n -- value )) }
459 { "set-alien-signed-2" "alien.accessors" (( value c-ptr n -- )) }
460 { "alien-unsigned-2" "alien.accessors" (( c-ptr n -- value )) }
461 { "set-alien-unsigned-2" "alien.accessors" (( value c-ptr n -- )) }
462 { "alien-signed-1" "alien.accessors" (( c-ptr n -- value )) }
463 { "set-alien-signed-1" "alien.accessors" (( value c-ptr n -- )) }
464 { "alien-unsigned-1" "alien.accessors" (( c-ptr n -- value )) }
465 { "set-alien-unsigned-1" "alien.accessors" (( value c-ptr n -- )) }
466 { "alien-float" "alien.accessors" (( c-ptr n -- value )) }
467 { "set-alien-float" "alien.accessors" (( value c-ptr n -- )) }
468 { "alien-double" "alien.accessors" (( c-ptr n -- value )) }
469 { "set-alien-double" "alien.accessors" (( value c-ptr n -- )) }
470 { "alien-cell" "alien.accessors" (( c-ptr n -- value )) }
471 { "set-alien-cell" "alien.accessors" (( value c-ptr n -- )) }
472 { "alien-address" "alien" (( c-ptr -- addr )) }
473 { "set-slot" "slots.private" (( value obj n -- )) }
474 { "string-nth" "strings.private" (( n string -- ch )) }
475 { "set-string-nth-fast" "strings.private" (( ch n string -- )) }
476 { "set-string-nth-slow" "strings.private" (( ch n string -- )) }
477 { "resize-array" "arrays" (( n array -- newarray )) }
478 { "resize-string" "strings" (( n str -- newstr )) }
479 { "<array>" "arrays" (( n elt -- array )) }
480 { "begin-scan" "memory" (( -- )) }
481 { "next-object" "memory" (( -- obj )) }
482 { "end-scan" "memory" (( -- )) }
483 { "size" "memory" (( obj -- n )) }
484 { "die" "kernel" (( -- )) }
485 { "(fopen)" "io.streams.c" (( path mode -- alien )) }
486 { "fgetc" "io.streams.c" (( alien -- ch/f )) }
487 { "fread" "io.streams.c" (( n alien -- str/f )) }
488 { "fputc" "io.streams.c" (( ch alien -- )) }
489 { "fwrite" "io.streams.c" (( string alien -- )) }
490 { "fflush" "io.streams.c" (( alien -- )) }
491 { "ftell" "io.streams.c" (( alien -- n )) }
492 { "fseek" "io.streams.c" (( alien offset whence -- )) }
493 { "fclose" "io.streams.c" (( alien -- )) }
494 { "<wrapper>" "kernel" (( obj -- wrapper )) }
495 { "(clone)" "kernel" (( obj -- newobj )) }
496 { "<string>" "strings" (( n ch -- string )) }
497 { "array>quotation" "quotations.private" (( array -- quot )) }
498 { "quotation-xt" "quotations" (( quot -- xt )) }
499 { "<tuple>" "classes.tuple.private" (( layout -- tuple )) }
500 { "profiling" "tools.profiler.private" (( ? -- )) }
501 { "become" "kernel.private" (( old new -- )) }
502 { "(sleep)" "threads.private" (( us -- )) }
503 { "<tuple-boa>" "classes.tuple.private" (( ... layout -- tuple )) }
504 { "callstack>array" "kernel" (( callstack -- array )) }
505 { "innermost-frame-executing" "kernel.private" (( callstack -- obj )) }
506 { "innermost-frame-scan" "kernel.private" (( callstack -- n )) }
507 { "set-innermost-frame-quot" "kernel.private" (( n callstack -- )) }
508 { "call-clear" "kernel" (( quot -- )) }
509 { "resize-byte-array" "byte-arrays" (( n byte-array -- newbyte-array )) }
510 { "dll-valid?" "alien.libraries" (( dll -- ? )) }
511 { "unimplemented" "kernel.private" (( -- * )) }
512 { "gc-reset" "memory" (( -- )) }
513 { "jit-compile" "quotations" (( quot -- )) }
514 { "load-locals" "locals.backend" (( ... n -- )) }
515 { "check-datastack" "kernel.private" (( array in# out# -- ? )) }
516 { "inline-cache-miss" "generic.single.private" (( generic methods index cache -- )) }
517 { "inline-cache-miss-tail" "generic.single.private" (( generic methods index cache -- )) }
518 { "mega-cache-miss" "generic.single.private" (( methods index cache -- method )) }
519 { "lookup-method" "generic.single.private" (( object methods -- method )) }
520 { "reset-dispatch-stats" "generic.single" (( -- )) }
521 { "dispatch-stats" "generic.single" (( -- stats )) }
522 { "reset-inline-cache-stats" "generic.single" (( -- )) }
523 { "inline-cache-stats" "generic.single" (( -- stats )) }
524 { "optimized?" "words" (( word -- ? )) }
525 { "quot-compiled?" "quotations" (( quot -- ? )) }
526 { "vm-ptr" "vm" (( -- ptr )) }
527 { "strip-stack-traces" "kernel.private" (( -- )) }
528 { "<callback>" "alien" (( word -- alien )) }
529 } [ [ first3 ] dip swap make-primitive ] each-index
532 "build" "kernel" create build 1 + [ ] curry (( -- n )) define-declared