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"
107 } [ create-vocab drop ] each
110 : lookup-type-number ( word -- n )
111 global [ target-word ] bind type-number ;
113 : register-builtin ( class -- )
114 [ dup lookup-type-number "type" set-word-prop ]
115 [ dup "type" word-prop builtins get set-nth ]
116 [ f f f builtin-class define-class ]
119 : prepare-slots ( slots -- slots' )
120 [ [ dup pair? [ first2 create ] when ] map ] map ;
122 : define-builtin-slots ( class slots -- )
123 prepare-slots make-slots 1 finalize-slots
124 [ "slots" set-word-prop ] [ define-accessors ] 2bi ;
126 : define-builtin ( symbol slotspec -- )
127 [ [ define-builtin-predicate ] keep ] dip define-builtin-slots ;
129 "fixnum" "math" create register-builtin
130 "bignum" "math" create register-builtin
131 "tuple" "kernel" create register-builtin
132 "float" "math" create register-builtin
133 "f" "syntax" lookup register-builtin
134 "array" "arrays" create register-builtin
135 "wrapper" "kernel" create register-builtin
136 "callstack" "kernel" create register-builtin
137 "string" "strings" create register-builtin
138 "quotation" "quotations" create register-builtin
139 "dll" "alien" create register-builtin
140 "alien" "alien" create register-builtin
141 "word" "words" create register-builtin
142 "byte-array" "byte-arrays" create register-builtin
144 ! We need this before defining c-ptr below
145 "f" "syntax" lookup { } define-builtin
147 "f" "syntax" create [ not ] "predicate" set-word-prop
148 "f?" "syntax" vocab-words delete-at
151 "c-ptr" "alien" create [
152 "alien" "alien" lookup ,
153 "f" "syntax" lookup ,
154 "byte-array" "byte-arrays" lookup ,
155 ] { } make define-union-class
157 ! A predicate class used for declarations
158 "array-capacity" "sequences.private" create
159 "fixnum" "math" lookup
162 bootstrap-max-array-capacity <fake-bignum> [ fixnum<= ] curry ,
165 define-predicate-class
167 "array-capacity" "sequences.private" lookup
168 [ >fixnum ] bootstrap-max-array-capacity <fake-bignum> [ fixnum-bitand ] curry append
169 "coercer" set-word-prop
171 ! Catch-all class for providing a default method.
172 "object" "kernel" create
173 [ f f { } intersection-class define-class ]
174 [ [ drop t ] "predicate" set-word-prop ]
177 "object?" "kernel" vocab-words delete-at
179 ! Class of objects with object tag
180 "hi-tag" "kernel.private" create
181 builtins get num-tags get tail define-union-class
183 ! Empty class with no instances
184 "null" "kernel" create
185 [ f { } f union-class define-class ]
186 [ [ drop f ] "predicate" set-word-prop ]
189 "null?" "kernel" vocab-words delete-at
191 "fixnum" "math" create { } define-builtin
192 "fixnum" "math" create ">fixnum" "math" create 1quotation "coercer" set-word-prop
194 "bignum" "math" create { } define-builtin
195 "bignum" "math" create ">bignum" "math" create 1quotation "coercer" set-word-prop
197 "float" "math" create { } define-builtin
198 "float" "math" create ">float" "math" create 1quotation "coercer" set-word-prop
200 "array" "arrays" create {
201 { "length" { "array-capacity" "sequences.private" } read-only }
204 "wrapper" "kernel" create {
205 { "wrapped" read-only }
208 "string" "strings" create {
209 { "length" { "array-capacity" "sequences.private" } read-only }
213 "quotation" "quotations" create {
214 { "array" { "array" "arrays" } read-only }
219 "dll" "alien" create {
220 { "path" { "byte-array" "byte-arrays" } read-only }
223 "alien" "alien" create {
224 { "underlying" { "c-ptr" "alien" } read-only }
228 "word" "words" create {
229 { "hashcode" { "fixnum" "math" } }
232 { "def" { "quotation" "quotations" } initial: [ ] }
236 { "counter" { "fixnum" "math" } }
237 { "sub-primitive" read-only }
240 "byte-array" "byte-arrays" create {
241 { "length" { "array-capacity" "sequences.private" } read-only }
244 "callstack" "kernel" create { } define-builtin
246 "tuple" "kernel" create
247 [ { } define-builtin ]
248 [ define-tuple-layout ]
251 ! Create special tombstone values
252 "tombstone" "hashtables.private" create
254 { "state" } define-tuple-class
256 "((empty))" "hashtables.private" create
257 "tombstone" "hashtables.private" lookup f
258 2array >tuple 1quotation (( -- value )) define-inline
260 "((tombstone))" "hashtables.private" create
261 "tombstone" "hashtables.private" lookup t
262 2array >tuple 1quotation (( -- value )) define-inline
265 "curry" "kernel" create
270 } prepare-slots define-tuple-class
272 "curry" "kernel" lookup
274 [ f "inline" set-word-prop ]
279 callable instance-check-quot %
285 (( obj quot -- curry )) define-declared
287 "compose" "kernel" create
290 { "first" read-only }
291 { "second" read-only }
292 } prepare-slots define-tuple-class
294 "compose" "kernel" lookup
296 [ f "inline" set-word-prop ]
301 callable instance-check-quot [ dip ] curry %
302 callable instance-check-quot %
308 (( quot1 quot2 -- compose )) define-declared
310 ! Sub-primitive words
311 : make-sub-primitive ( word vocab effect -- )
312 [ create dup 1quotation ] dip define-declared ;
315 { "(execute)" "kernel.private" (( word -- )) }
316 { "(call)" "kernel.private" (( quot -- )) }
317 { "both-fixnums?" "math.private" (( x y -- ? )) }
318 { "fixnum+fast" "math.private" (( x y -- z )) }
319 { "fixnum-fast" "math.private" (( x y -- z )) }
320 { "fixnum*fast" "math.private" (( x y -- z )) }
321 { "fixnum-bitand" "math.private" (( x y -- z )) }
322 { "fixnum-bitor" "math.private" (( x y -- z )) }
323 { "fixnum-bitxor" "math.private" (( x y -- z )) }
324 { "fixnum-bitnot" "math.private" (( x -- y )) }
325 { "fixnum-mod" "math.private" (( x y -- z )) }
326 { "fixnum-shift-fast" "math.private" (( x y -- z )) }
327 { "fixnum/i-fast" "math.private" (( x y -- z )) }
328 { "fixnum/mod-fast" "math.private" (( x y -- z w )) }
329 { "fixnum<" "math.private" (( x y -- ? )) }
330 { "fixnum<=" "math.private" (( x y -- z )) }
331 { "fixnum>" "math.private" (( x y -- ? )) }
332 { "fixnum>=" "math.private" (( x y -- ? )) }
333 { "drop" "kernel" (( x -- )) }
334 { "2drop" "kernel" (( x y -- )) }
335 { "3drop" "kernel" (( x y z -- )) }
336 { "dup" "kernel" (( x -- x x )) }
337 { "2dup" "kernel" (( x y -- x y x y )) }
338 { "3dup" "kernel" (( x y z -- x y z x y z )) }
339 { "rot" "kernel" (( x y z -- y z x )) }
340 { "-rot" "kernel" (( x y z -- z x y )) }
341 { "dupd" "kernel" (( x y -- x x y )) }
342 { "swapd" "kernel" (( x y z -- y x z )) }
343 { "nip" "kernel" (( x y -- y )) }
344 { "2nip" "kernel" (( x y z -- z )) }
345 { "tuck" "kernel" (( x y -- y x y )) }
346 { "over" "kernel" (( x y -- x y x )) }
347 { "pick" "kernel" (( x y z -- x y z x )) }
348 { "swap" "kernel" (( x y -- y x )) }
349 { "eq?" "kernel" (( obj1 obj2 -- ? )) }
350 { "tag" "kernel.private" (( object -- n )) }
351 { "slot" "slots.private" (( obj m -- value )) }
352 { "get-local" "locals.backend" (( n -- obj )) }
353 { "load-local" "locals.backend" (( obj -- )) }
354 { "drop-locals" "locals.backend" (( n -- )) }
355 { "mega-cache-lookup" "generic.single.private" (( methods index cache -- )) }
356 } [ first3 make-sub-primitive ] each
359 : make-primitive ( word vocab n effect -- )
361 [ create dup reset-word ] dip
362 [ do-primitive ] curry
363 ] dip define-declared ;
366 { "bignum>fixnum" "math.private" (( x -- y )) }
367 { "float>fixnum" "math.private" (( x -- y )) }
368 { "fixnum>bignum" "math.private" (( x -- y )) }
369 { "float>bignum" "math.private" (( x -- y )) }
370 { "fixnum>float" "math.private" (( x -- y )) }
371 { "bignum>float" "math.private" (( x -- y )) }
372 { "(string>float)" "math.parser.private" (( str -- n/f )) }
373 { "(float>string)" "math.parser.private" (( n -- str )) }
374 { "float>bits" "math" (( x -- n )) }
375 { "double>bits" "math" (( x -- n )) }
376 { "bits>float" "math" (( n -- x )) }
377 { "bits>double" "math" (( n -- x )) }
378 { "fixnum+" "math.private" (( x y -- z )) }
379 { "fixnum-" "math.private" (( x y -- z )) }
380 { "fixnum*" "math.private" (( x y -- z )) }
381 { "fixnum/i" "math.private" (( x y -- z )) }
382 { "fixnum/mod" "math.private" (( x y -- z w )) }
383 { "fixnum-shift" "math.private" (( x y -- z )) }
384 { "bignum=" "math.private" (( x y -- ? )) }
385 { "bignum+" "math.private" (( x y -- z )) }
386 { "bignum-" "math.private" (( x y -- z )) }
387 { "bignum*" "math.private" (( x y -- z )) }
388 { "bignum/i" "math.private" (( x y -- z )) }
389 { "bignum-mod" "math.private" (( x y -- z )) }
390 { "bignum/mod" "math.private" (( x y -- z w )) }
391 { "bignum-bitand" "math.private" (( x y -- z )) }
392 { "bignum-bitor" "math.private" (( x y -- z )) }
393 { "bignum-bitxor" "math.private" (( x y -- z )) }
394 { "bignum-bitnot" "math.private" (( x -- y )) }
395 { "bignum-shift" "math.private" (( x y -- z )) }
396 { "bignum<" "math.private" (( x y -- ? )) }
397 { "bignum<=" "math.private" (( x y -- ? )) }
398 { "bignum>" "math.private" (( x y -- ? )) }
399 { "bignum>=" "math.private" (( x y -- ? )) }
400 { "bignum-bit?" "math.private" (( n x -- ? )) }
401 { "bignum-log2" "math.private" (( x -- n )) }
402 { "byte-array>bignum" "math" (( x -- y )) }
403 { "float=" "math.private" (( x y -- ? )) }
404 { "float+" "math.private" (( x y -- z )) }
405 { "float-" "math.private" (( x y -- z )) }
406 { "float*" "math.private" (( x y -- z )) }
407 { "float/f" "math.private" (( x y -- z )) }
408 { "float-mod" "math.private" (( x y -- z )) }
409 { "float<" "math.private" (( x y -- ? )) }
410 { "float<=" "math.private" (( x y -- ? )) }
411 { "float>" "math.private" (( x y -- ? )) }
412 { "float>=" "math.private" (( x y -- ? )) }
413 { "float-u<" "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 { "<word>" "words" (( name vocab -- word )) }
418 { "word-xt" "words" (( word -- start end )) }
419 { "getenv" "kernel.private" (( n -- obj )) }
420 { "setenv" "kernel.private" (( obj n -- )) }
421 { "(exists?)" "io.files.private" (( path -- ? )) }
422 { "gc" "memory" (( -- )) }
423 { "gc-stats" "memory" f }
424 { "(save-image)" "memory.private" (( path -- )) }
425 { "(save-image-and-exit)" "memory.private" (( path -- )) }
426 { "datastack" "kernel" (( -- ds )) }
427 { "retainstack" "kernel" (( -- rs )) }
428 { "callstack" "kernel" (( -- cs )) }
429 { "set-datastack" "kernel" (( ds -- )) }
430 { "set-retainstack" "kernel" (( rs -- )) }
431 { "set-callstack" "kernel" (( cs -- )) }
432 { "exit" "system" (( n -- )) }
433 { "data-room" "memory" (( -- cards decks generations )) }
434 { "code-room" "memory" (( -- code-total code-used code-free largest-free-block )) }
435 { "micros" "system" (( -- us )) }
436 { "modify-code-heap" "compiler.units" (( alist -- )) }
437 { "(dlopen)" "alien.libraries" (( path -- dll )) }
438 { "(dlsym)" "alien.libraries" (( name dll -- alien )) }
439 { "dlclose" "alien.libraries" (( dll -- )) }
440 { "<byte-array>" "byte-arrays" (( n -- byte-array )) }
441 { "(byte-array)" "byte-arrays" (( n -- byte-array )) }
442 { "<displaced-alien>" "alien" (( displacement c-ptr -- alien )) }
443 { "alien-signed-cell" "alien.accessors" (( c-ptr n -- value )) }
444 { "set-alien-signed-cell" "alien.accessors" (( value c-ptr n -- )) }
445 { "alien-unsigned-cell" "alien.accessors" (( c-ptr n -- value )) }
446 { "set-alien-unsigned-cell" "alien.accessors" (( value c-ptr n -- )) }
447 { "alien-signed-8" "alien.accessors" (( c-ptr n -- value )) }
448 { "set-alien-signed-8" "alien.accessors" (( value c-ptr n -- )) }
449 { "alien-unsigned-8" "alien.accessors" (( c-ptr n -- value )) }
450 { "set-alien-unsigned-8" "alien.accessors" (( value c-ptr n -- )) }
451 { "alien-signed-4" "alien.accessors" (( c-ptr n -- value )) }
452 { "set-alien-signed-4" "alien.accessors" (( value c-ptr n -- )) }
453 { "alien-unsigned-4" "alien.accessors" (( c-ptr n -- value )) }
454 { "set-alien-unsigned-4" "alien.accessors" (( value c-ptr n -- )) }
455 { "alien-signed-2" "alien.accessors" (( c-ptr n -- value )) }
456 { "set-alien-signed-2" "alien.accessors" (( value c-ptr n -- )) }
457 { "alien-unsigned-2" "alien.accessors" (( c-ptr n -- value )) }
458 { "set-alien-unsigned-2" "alien.accessors" (( value c-ptr n -- )) }
459 { "alien-signed-1" "alien.accessors" (( c-ptr n -- value )) }
460 { "set-alien-signed-1" "alien.accessors" (( value c-ptr n -- )) }
461 { "alien-unsigned-1" "alien.accessors" (( c-ptr n -- value )) }
462 { "set-alien-unsigned-1" "alien.accessors" (( value c-ptr n -- )) }
463 { "alien-float" "alien.accessors" (( c-ptr n -- value )) }
464 { "set-alien-float" "alien.accessors" (( value c-ptr n -- )) }
465 { "alien-double" "alien.accessors" (( c-ptr n -- value )) }
466 { "set-alien-double" "alien.accessors" (( value c-ptr n -- )) }
467 { "alien-cell" "alien.accessors" (( c-ptr n -- value )) }
468 { "set-alien-cell" "alien.accessors" (( value c-ptr n -- )) }
469 { "alien-address" "alien" (( c-ptr -- addr )) }
470 { "set-slot" "slots.private" (( value obj n -- )) }
471 { "string-nth" "strings.private" (( n string -- ch )) }
472 { "set-string-nth-fast" "strings.private" (( ch n string -- )) }
473 { "set-string-nth-slow" "strings.private" (( ch n string -- )) }
474 { "resize-array" "arrays" (( n array -- newarray )) }
475 { "resize-string" "strings" (( n str -- newstr )) }
476 { "<array>" "arrays" (( n elt -- array )) }
477 { "begin-scan" "memory" (( -- )) }
478 { "next-object" "memory" (( -- obj )) }
479 { "end-scan" "memory" (( -- )) }
480 { "size" "memory" (( obj -- n )) }
481 { "die" "kernel" (( -- )) }
482 { "(fopen)" "io.streams.c" (( path mode -- alien )) }
483 { "fgetc" "io.streams.c" (( alien -- ch/f )) }
484 { "fread" "io.streams.c" (( n alien -- str/f )) }
485 { "fputc" "io.streams.c" (( ch alien -- )) }
486 { "fwrite" "io.streams.c" (( string alien -- )) }
487 { "fflush" "io.streams.c" (( alien -- )) }
488 { "fseek" "io.streams.c" (( alien offset whence -- )) }
489 { "fclose" "io.streams.c" (( alien -- )) }
490 { "<wrapper>" "kernel" (( obj -- wrapper )) }
491 { "(clone)" "kernel" (( obj -- newobj )) }
492 { "<string>" "strings" (( n ch -- string )) }
493 { "array>quotation" "quotations.private" (( array -- quot )) }
494 { "quotation-xt" "quotations" (( quot -- xt )) }
495 { "<tuple>" "classes.tuple.private" (( layout -- tuple )) }
496 { "profiling" "tools.profiler.private" (( ? -- )) }
497 { "become" "kernel.private" (( old new -- )) }
498 { "(sleep)" "threads.private" (( us -- )) }
499 { "<tuple-boa>" "classes.tuple.private" (( ... layout -- tuple )) }
500 { "callstack>array" "kernel" (( callstack -- array )) }
501 { "innermost-frame-executing" "kernel.private" (( callstack -- obj )) }
502 { "innermost-frame-scan" "kernel.private" (( callstack -- n )) }
503 { "set-innermost-frame-quot" "kernel.private" (( n callstack -- )) }
504 { "call-clear" "kernel" (( quot -- )) }
505 { "resize-byte-array" "byte-arrays" (( n byte-array -- newbyte-array )) }
506 { "dll-valid?" "alien.libraries" (( dll -- ? )) }
507 { "unimplemented" "kernel.private" (( -- * )) }
508 { "gc-reset" "memory" (( -- )) }
509 { "jit-compile" "quotations" (( quot -- )) }
510 { "load-locals" "locals.backend" (( ... n -- )) }
511 { "check-datastack" "kernel.private" (( array in# out# -- ? )) }
512 { "inline-cache-miss" "generic.single.private" (( generic methods index cache -- )) }
513 { "inline-cache-miss-tail" "generic.single.private" (( generic methods index cache -- )) }
514 { "mega-cache-miss" "generic.single.private" (( methods index cache -- method )) }
515 { "lookup-method" "generic.single.private" (( object methods -- method )) }
516 { "reset-dispatch-stats" "generic.single" (( -- )) }
517 { "dispatch-stats" "generic.single" (( -- stats )) }
518 { "reset-inline-cache-stats" "generic.single" (( -- )) }
519 { "inline-cache-stats" "generic.single" (( -- stats )) }
520 { "optimized?" "words" (( word -- ? )) }
521 { "quot-compiled?" "quotations" (( quot -- ? )) }
522 { "vm-ptr" "vm" (( -- ptr )) }
523 } [ [ first3 ] dip swap make-primitive ] each-index
526 "build" "kernel" create build 1 + [ ] curry (( -- n )) define-declared