1 ! Copyright (C) 2004, 2008 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
17 H{ } clone sub-primitives set
19 "resource:core/bootstrap/syntax.factor" parse-file
21 "resource:basis/cpu/" architecture get {
23 { "winnt-x86.64" "x86/64/winnt" }
24 { "unix-x86.64" "x86/64/unix" }
25 { "linux-ppc" "ppc/linux" }
26 { "macosx-ppc" "ppc/macosx" }
28 } at "/bootstrap.factor" 3append parse-file
30 "resource:core/bootstrap/layouts/layouts.factor" parse-file
32 ! Now we have ( syntax-quot arch-quot layouts-quot ) on the stack
34 ! Bring up a bare cross-compiling vocabulary.
35 "syntax" vocab vocab-words bootstrap-syntax set {
38 changed-definitions changed-generics
39 remake-generics forgotten-definitions
40 root-cache source-files update-map implementors-map
41 } [ H{ } clone swap set ] each
45 ! Vocabulary for slot accessors
46 "accessors" create-vocab drop
48 ! Trivial recompile hook. We don't want to touch the code heap
49 ! during stage1 bootstrap, it would just waste time.
50 [ drop { } ] recompile-hook set
56 ! After we execute bootstrap/layouts
57 num-types get f <array> builtins set
61 ! Create some empty vocabs where the below primitives and
70 "classes.tuple.private"
73 "continuations.private"
100 "tools.profiler.private"
105 } [ create-vocab drop ] each
108 : lookup-type-number ( word -- n )
109 global [ target-word ] bind type-number ;
111 : register-builtin ( class -- )
112 [ dup lookup-type-number "type" set-word-prop ]
113 [ dup "type" word-prop builtins get set-nth ]
114 [ f f f builtin-class define-class ]
117 : prepare-slots ( slots -- slots' )
118 [ [ dup pair? [ first2 create ] when ] map ] map ;
120 : define-builtin-slots ( class slots -- )
121 prepare-slots make-slots 1 finalize-slots
122 [ "slots" set-word-prop ] [ define-accessors ] 2bi ;
124 : define-builtin ( symbol slotspec -- )
125 [ [ define-builtin-predicate ] keep ] dip define-builtin-slots ;
127 "fixnum" "math" create register-builtin
128 "bignum" "math" create register-builtin
129 "tuple" "kernel" create register-builtin
130 "ratio" "math" create register-builtin
131 "float" "math" create register-builtin
132 "complex" "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 ! For predicate classes
145 "predicate-instance?" "classes.predicate" create drop
147 ! We need this before defining c-ptr below
148 "f" "syntax" lookup { } define-builtin
150 "f" "syntax" create [ not ] "predicate" set-word-prop
151 "f?" "syntax" vocab-words delete-at
154 "integer" "math" create
155 "fixnum" "math" lookup
156 "bignum" "math" lookup
160 "rational" "math" create
161 "integer" "math" lookup
162 "ratio" "math" lookup
167 "rational" "math" lookup
168 "float" "math" lookup
172 "c-ptr" "alien" create [
173 "alien" "alien" lookup ,
174 "f" "syntax" lookup ,
175 "byte-array" "byte-arrays" lookup ,
176 ] { } make define-union-class
178 ! A predicate class used for declarations
179 "array-capacity" "sequences.private" create
180 "fixnum" "math" lookup
183 bootstrap-max-array-capacity <fake-bignum> [ fixnum<= ] curry ,
186 define-predicate-class
188 "array-capacity" "sequences.private" lookup
189 [ >fixnum ] bootstrap-max-array-capacity <fake-bignum> [ fixnum-bitand ] curry append
190 "coercer" set-word-prop
192 ! Catch-all class for providing a default method.
193 "object" "kernel" create
194 [ f f { } intersection-class define-class ]
195 [ [ drop t ] "predicate" set-word-prop ]
198 "object?" "kernel" vocab-words delete-at
200 ! Class of objects with object tag
201 "hi-tag" "kernel.private" create
202 builtins get num-tags get tail define-union-class
204 ! Empty class with no instances
205 "null" "kernel" create
206 [ f { } f union-class define-class ]
207 [ [ drop f ] "predicate" set-word-prop ]
210 "null?" "kernel" vocab-words delete-at
212 "fixnum" "math" create { } define-builtin
213 "fixnum" "math" create ">fixnum" "math" create 1quotation "coercer" set-word-prop
215 "bignum" "math" create { } define-builtin
216 "bignum" "math" create ">bignum" "math" create 1quotation "coercer" set-word-prop
218 "ratio" "math" create {
219 { "numerator" { "integer" "math" } read-only }
220 { "denominator" { "integer" "math" } read-only }
223 "float" "math" create { } define-builtin
224 "float" "math" create ">float" "math" create 1quotation "coercer" set-word-prop
226 "complex" "math" create {
227 { "real" { "real" "math" } read-only }
228 { "imaginary" { "real" "math" } read-only }
231 "array" "arrays" create {
232 { "length" { "array-capacity" "sequences.private" } read-only }
235 "wrapper" "kernel" create {
236 { "wrapped" read-only }
239 "string" "strings" create {
240 { "length" { "array-capacity" "sequences.private" } read-only }
244 "quotation" "quotations" create {
245 { "array" { "array" "arrays" } read-only }
246 { "compiled" read-only }
249 "dll" "alien" create {
250 { "path" { "byte-array" "byte-arrays" } read-only }
253 "alien" "alien" create {
254 { "underlying" { "c-ptr" "alien" } read-only }
258 "word" "words" create {
259 { "hashcode" { "fixnum" "math" } }
262 { "def" { "quotation" "quotations" } initial: [ ] }
264 { "optimized" read-only }
265 { "counter" { "fixnum" "math" } }
266 { "sub-primitive" read-only }
269 "byte-array" "byte-arrays" create {
270 { "length" { "array-capacity" "sequences.private" } read-only }
273 "callstack" "kernel" create { } define-builtin
275 "tuple" "kernel" create
276 [ { } define-builtin ]
277 [ define-tuple-layout ]
280 ! Create special tombstone values
281 "tombstone" "hashtables.private" create
283 { "state" } define-tuple-class
285 "((empty))" "hashtables.private" create
286 "tombstone" "hashtables.private" lookup f
287 2array >tuple 1quotation (( -- value )) define-inline
289 "((tombstone))" "hashtables.private" create
290 "tombstone" "hashtables.private" lookup t
291 2array >tuple 1quotation (( -- value )) define-inline
294 "curry" "kernel" create
299 } prepare-slots define-tuple-class
301 "curry" "kernel" lookup
303 [ f "inline" set-word-prop ]
308 callable instance-check-quot %
314 (( obj quot -- curry )) define-declared
316 "compose" "kernel" create
319 { "first" read-only }
320 { "second" read-only }
321 } prepare-slots define-tuple-class
323 "compose" "kernel" lookup
325 [ f "inline" set-word-prop ]
330 callable instance-check-quot [ dip ] curry %
331 callable instance-check-quot %
337 (( quot1 quot2 -- compose )) define-declared
339 ! Sub-primitive words
340 : make-sub-primitive ( word vocab -- )
343 dup 1quotation define ;
346 { "(execute)" "words.private" }
347 { "(call)" "kernel.private" }
348 { "both-fixnums?" "math.private" }
349 { "fixnum+fast" "math.private" }
350 { "fixnum-fast" "math.private" }
351 { "fixnum*fast" "math.private" }
352 { "fixnum-bitand" "math.private" }
353 { "fixnum-bitor" "math.private" }
354 { "fixnum-bitxor" "math.private" }
355 { "fixnum-bitnot" "math.private" }
356 { "fixnum-mod" "math.private" }
357 { "fixnum-shift-fast" "math.private" }
358 { "fixnum/i-fast" "math.private" }
359 { "fixnum/mod-fast" "math.private" }
360 { "fixnum<" "math.private" }
361 { "fixnum<=" "math.private" }
362 { "fixnum>" "math.private" }
363 { "fixnum>=" "math.private" }
381 { "tag" "kernel.private" }
382 { "slot" "slots.private" }
383 { "get-local" "locals.backend" }
384 { "load-local" "locals.backend" }
385 { "drop-locals" "locals.backend" }
386 } [ make-sub-primitive ] assoc-each
389 : make-primitive ( word vocab n -- )
390 [ create dup reset-word ] dip
391 [ do-primitive ] curry [ ] like define ;
394 { "bignum>fixnum" "math.private" }
395 { "float>fixnum" "math.private" }
396 { "fixnum>bignum" "math.private" }
397 { "float>bignum" "math.private" }
398 { "fixnum>float" "math.private" }
399 { "bignum>float" "math.private" }
400 { "<ratio>" "math.private" }
401 { "string>float" "math.private" }
402 { "float>string" "math.private" }
403 { "float>bits" "math" }
404 { "double>bits" "math" }
405 { "bits>float" "math" }
406 { "bits>double" "math" }
407 { "<complex>" "math.private" }
408 { "fixnum+" "math.private" }
409 { "fixnum-" "math.private" }
410 { "fixnum*" "math.private" }
411 { "fixnum/i" "math.private" }
412 { "fixnum/mod" "math.private" }
413 { "fixnum-shift" "math.private" }
414 { "bignum=" "math.private" }
415 { "bignum+" "math.private" }
416 { "bignum-" "math.private" }
417 { "bignum*" "math.private" }
418 { "bignum/i" "math.private" }
419 { "bignum-mod" "math.private" }
420 { "bignum/mod" "math.private" }
421 { "bignum-bitand" "math.private" }
422 { "bignum-bitor" "math.private" }
423 { "bignum-bitxor" "math.private" }
424 { "bignum-bitnot" "math.private" }
425 { "bignum-shift" "math.private" }
426 { "bignum<" "math.private" }
427 { "bignum<=" "math.private" }
428 { "bignum>" "math.private" }
429 { "bignum>=" "math.private" }
430 { "bignum-bit?" "math.private" }
431 { "bignum-log2" "math.private" }
432 { "byte-array>bignum" "math" }
433 { "float=" "math.private" }
434 { "float+" "math.private" }
435 { "float-" "math.private" }
436 { "float*" "math.private" }
437 { "float/f" "math.private" }
438 { "float-mod" "math.private" }
439 { "float<" "math.private" }
440 { "float<=" "math.private" }
441 { "float>" "math.private" }
442 { "float>=" "math.private" }
444 { "word-xt" "words" }
445 { "getenv" "kernel.private" }
446 { "setenv" "kernel.private" }
447 { "(exists?)" "io.files.private" }
449 { "gc-stats" "memory" }
450 { "save-image" "memory" }
451 { "save-image-and-exit" "memory" }
452 { "datastack" "kernel" }
453 { "retainstack" "kernel" }
454 { "callstack" "kernel" }
455 { "set-datastack" "kernel" }
456 { "set-retainstack" "kernel" }
457 { "set-callstack" "kernel" }
459 { "data-room" "memory" }
460 { "code-room" "memory" }
461 { "micros" "system" }
462 { "modify-code-heap" "compiler.units" }
465 { "dlclose" "alien" }
466 { "<byte-array>" "byte-arrays" }
467 { "(byte-array)" "byte-arrays" }
468 { "<displaced-alien>" "alien" }
469 { "alien-signed-cell" "alien.accessors" }
470 { "set-alien-signed-cell" "alien.accessors" }
471 { "alien-unsigned-cell" "alien.accessors" }
472 { "set-alien-unsigned-cell" "alien.accessors" }
473 { "alien-signed-8" "alien.accessors" }
474 { "set-alien-signed-8" "alien.accessors" }
475 { "alien-unsigned-8" "alien.accessors" }
476 { "set-alien-unsigned-8" "alien.accessors" }
477 { "alien-signed-4" "alien.accessors" }
478 { "set-alien-signed-4" "alien.accessors" }
479 { "alien-unsigned-4" "alien.accessors" }
480 { "set-alien-unsigned-4" "alien.accessors" }
481 { "alien-signed-2" "alien.accessors" }
482 { "set-alien-signed-2" "alien.accessors" }
483 { "alien-unsigned-2" "alien.accessors" }
484 { "set-alien-unsigned-2" "alien.accessors" }
485 { "alien-signed-1" "alien.accessors" }
486 { "set-alien-signed-1" "alien.accessors" }
487 { "alien-unsigned-1" "alien.accessors" }
488 { "set-alien-unsigned-1" "alien.accessors" }
489 { "alien-float" "alien.accessors" }
490 { "set-alien-float" "alien.accessors" }
491 { "alien-double" "alien.accessors" }
492 { "set-alien-double" "alien.accessors" }
493 { "alien-cell" "alien.accessors" }
494 { "set-alien-cell" "alien.accessors" }
495 { "(throw)" "kernel.private" }
496 { "alien-address" "alien" }
497 { "set-slot" "slots.private" }
498 { "string-nth" "strings.private" }
499 { "set-string-nth-fast" "strings.private" }
500 { "set-string-nth-slow" "strings.private" }
501 { "resize-array" "arrays" }
502 { "resize-string" "strings" }
503 { "<array>" "arrays" }
504 { "begin-scan" "memory" }
505 { "next-object" "memory" }
506 { "end-scan" "memory" }
509 { "fopen" "io.streams.c" }
510 { "fgetc" "io.streams.c" }
511 { "fread" "io.streams.c" }
512 { "fputc" "io.streams.c" }
513 { "fwrite" "io.streams.c" }
514 { "fflush" "io.streams.c" }
515 { "fclose" "io.streams.c" }
516 { "<wrapper>" "kernel" }
517 { "(clone)" "kernel" }
518 { "<string>" "strings" }
519 { "array>quotation" "quotations.private" }
520 { "quotation-xt" "quotations" }
521 { "<tuple>" "classes.tuple.private" }
522 { "profiling" "tools.profiler.private" }
523 { "become" "kernel.private" }
524 { "(sleep)" "threads.private" }
525 { "<tuple-boa>" "classes.tuple.private" }
526 { "callstack>array" "kernel" }
527 { "innermost-frame-quot" "kernel.private" }
528 { "innermost-frame-scan" "kernel.private" }
529 { "set-innermost-frame-quot" "kernel.private" }
530 { "call-clear" "kernel" }
531 { "resize-byte-array" "byte-arrays" }
532 { "dll-valid?" "alien" }
533 { "unimplemented" "kernel.private" }
534 { "gc-reset" "memory" }
535 { "jit-compile" "quotations" }
536 { "load-locals" "locals.backend" }
538 [ [ first2 ] dip make-primitive ] each-index
541 "build" "kernel" create build 1+ 1quotation define