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
36 H{ } clone dictionary set
37 H{ } clone new-classes set
38 H{ } clone changed-definitions set
39 H{ } clone changed-generics set
40 H{ } clone remake-generics set
41 H{ } clone forgotten-definitions set
42 H{ } clone root-cache set
43 H{ } clone source-files set
44 H{ } clone update-map set
45 H{ } clone implementors-map set
48 ! Vocabulary for slot accessors
49 "accessors" create-vocab drop
51 ! Trivial recompile hook. We don't want to touch the code heap
52 ! during stage1 bootstrap, it would just waste time.
53 [ drop { } ] recompile-hook set
59 ! After we execute bootstrap/layouts
60 num-types get f <array> builtins set
64 ! Create some empty vocabs where the below primitives and
73 "classes.tuple.private"
76 "continuations.private"
103 "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 "ratio" "math" create register-builtin
134 "float" "math" create register-builtin
135 "complex" "math" create register-builtin
136 "f" "syntax" lookup register-builtin
137 "array" "arrays" create register-builtin
138 "wrapper" "kernel" create register-builtin
139 "callstack" "kernel" create register-builtin
140 "string" "strings" create register-builtin
141 "quotation" "quotations" create register-builtin
142 "dll" "alien" create register-builtin
143 "alien" "alien" create register-builtin
144 "word" "words" create register-builtin
145 "byte-array" "byte-arrays" create register-builtin
147 ! For predicate classes
148 "predicate-instance?" "classes.predicate" create drop
150 ! We need this before defining c-ptr below
151 "f" "syntax" lookup { } define-builtin
153 "f" "syntax" create [ not ] "predicate" set-word-prop
154 "f?" "syntax" vocab-words delete-at
157 "integer" "math" create
158 "fixnum" "math" lookup
159 "bignum" "math" lookup
163 "rational" "math" create
164 "integer" "math" lookup
165 "ratio" "math" lookup
170 "rational" "math" lookup
171 "float" "math" lookup
175 "c-ptr" "alien" create [
176 "alien" "alien" lookup ,
177 "f" "syntax" lookup ,
178 "byte-array" "byte-arrays" lookup ,
179 ] { } make define-union-class
181 ! A predicate class used for declarations
182 "array-capacity" "sequences.private" create
183 "fixnum" "math" lookup
186 bootstrap-max-array-capacity <fake-bignum> [ fixnum<= ] curry ,
189 define-predicate-class
191 "array-capacity" "sequences.private" lookup
192 [ >fixnum ] bootstrap-max-array-capacity <fake-bignum> [ fixnum-bitand ] curry append
193 "coercer" set-word-prop
195 ! Catch-all class for providing a default method.
196 "object" "kernel" create
197 [ f f { } intersection-class define-class ]
198 [ [ drop t ] "predicate" set-word-prop ]
201 "object?" "kernel" vocab-words delete-at
203 ! Class of objects with object tag
204 "hi-tag" "kernel.private" create
205 builtins get num-tags get tail define-union-class
207 ! Empty class with no instances
208 "null" "kernel" create
209 [ f { } f union-class define-class ]
210 [ [ drop f ] "predicate" set-word-prop ]
213 "null?" "kernel" vocab-words delete-at
215 "fixnum" "math" create { } define-builtin
216 "fixnum" "math" create ">fixnum" "math" create 1quotation "coercer" set-word-prop
218 "bignum" "math" create { } define-builtin
219 "bignum" "math" create ">bignum" "math" create 1quotation "coercer" set-word-prop
221 "ratio" "math" create {
222 { "numerator" { "integer" "math" } read-only }
223 { "denominator" { "integer" "math" } read-only }
226 "float" "math" create { } define-builtin
227 "float" "math" create ">float" "math" create 1quotation "coercer" set-word-prop
229 "complex" "math" create {
230 { "real" { "real" "math" } read-only }
231 { "imaginary" { "real" "math" } read-only }
234 "array" "arrays" create {
235 { "length" { "array-capacity" "sequences.private" } read-only }
238 "wrapper" "kernel" create {
239 { "wrapped" read-only }
242 "string" "strings" create {
243 { "length" { "array-capacity" "sequences.private" } read-only }
247 "quotation" "quotations" create {
248 { "array" { "array" "arrays" } read-only }
249 { "compiled" read-only }
252 "dll" "alien" create {
253 { "path" { "byte-array" "byte-arrays" } read-only }
256 "alien" "alien" create {
257 { "underlying" { "c-ptr" "alien" } read-only }
261 "word" "words" create {
262 { "hashcode" { "fixnum" "math" } }
265 { "def" { "quotation" "quotations" } initial: [ ] }
267 { "compiled" read-only }
268 { "counter" { "fixnum" "math" } }
269 { "sub-primitive" read-only }
272 "byte-array" "byte-arrays" create {
273 { "length" { "array-capacity" "sequences.private" } read-only }
276 "callstack" "kernel" create { } define-builtin
278 "tuple" "kernel" create
279 [ { } define-builtin ]
280 [ define-tuple-layout ]
283 ! Create special tombstone values
284 "tombstone" "hashtables.private" create
286 { "state" } define-tuple-class
288 "((empty))" "hashtables.private" create
289 "tombstone" "hashtables.private" lookup f
290 2array >tuple 1quotation (( -- value )) define-inline
292 "((tombstone))" "hashtables.private" create
293 "tombstone" "hashtables.private" lookup t
294 2array >tuple 1quotation (( -- value )) define-inline
297 "curry" "kernel" create
302 } prepare-slots define-tuple-class
304 "curry" "kernel" lookup
306 [ f "inline" set-word-prop ]
311 callable instance-check-quot %
317 (( obj quot -- curry )) define-declared
319 "compose" "kernel" create
322 { "first" read-only }
323 { "second" read-only }
324 } prepare-slots define-tuple-class
326 "compose" "kernel" lookup
328 [ f "inline" set-word-prop ]
333 callable instance-check-quot [ dip ] curry %
334 callable instance-check-quot %
340 (( quot1 quot2 -- compose )) define-declared
342 ! Sub-primitive words
343 : make-sub-primitive ( word vocab -- )
346 dup 1quotation define ;
349 { "(execute)" "words.private" }
350 { "(call)" "kernel.private" }
351 { "both-fixnums?" "math.private" }
352 { "fixnum+fast" "math.private" }
353 { "fixnum-fast" "math.private" }
354 { "fixnum*fast" "math.private" }
355 { "fixnum-bitand" "math.private" }
356 { "fixnum-bitor" "math.private" }
357 { "fixnum-bitxor" "math.private" }
358 { "fixnum-bitnot" "math.private" }
359 { "fixnum-mod" "math.private" }
360 { "fixnum-shift-fast" "math.private" }
361 { "fixnum/i-fast" "math.private" }
362 { "fixnum/mod-fast" "math.private" }
363 { "fixnum<" "math.private" }
364 { "fixnum<=" "math.private" }
365 { "fixnum>" "math.private" }
366 { "fixnum>=" "math.private" }
384 { "tag" "kernel.private" }
385 { "slot" "slots.private" }
386 { "get-local" "locals.backend" }
387 { "load-local" "locals.backend" }
388 { "drop-locals" "locals.backend" }
389 } [ make-sub-primitive ] assoc-each
392 : make-primitive ( word vocab n -- )
393 [ create dup reset-word ] dip
394 [ do-primitive ] curry [ ] like define ;
397 { "bignum>fixnum" "math.private" }
398 { "float>fixnum" "math.private" }
399 { "fixnum>bignum" "math.private" }
400 { "float>bignum" "math.private" }
401 { "fixnum>float" "math.private" }
402 { "bignum>float" "math.private" }
403 { "<ratio>" "math.private" }
404 { "string>float" "math.private" }
405 { "float>string" "math.private" }
406 { "float>bits" "math" }
407 { "double>bits" "math" }
408 { "bits>float" "math" }
409 { "bits>double" "math" }
410 { "<complex>" "math.private" }
411 { "fixnum+" "math.private" }
412 { "fixnum-" "math.private" }
413 { "fixnum*" "math.private" }
414 { "fixnum/i" "math.private" }
415 { "fixnum/mod" "math.private" }
416 { "fixnum-shift" "math.private" }
417 { "bignum=" "math.private" }
418 { "bignum+" "math.private" }
419 { "bignum-" "math.private" }
420 { "bignum*" "math.private" }
421 { "bignum/i" "math.private" }
422 { "bignum-mod" "math.private" }
423 { "bignum/mod" "math.private" }
424 { "bignum-bitand" "math.private" }
425 { "bignum-bitor" "math.private" }
426 { "bignum-bitxor" "math.private" }
427 { "bignum-bitnot" "math.private" }
428 { "bignum-shift" "math.private" }
429 { "bignum<" "math.private" }
430 { "bignum<=" "math.private" }
431 { "bignum>" "math.private" }
432 { "bignum>=" "math.private" }
433 { "bignum-bit?" "math.private" }
434 { "bignum-log2" "math.private" }
435 { "byte-array>bignum" "math" }
436 { "float=" "math.private" }
437 { "float+" "math.private" }
438 { "float-" "math.private" }
439 { "float*" "math.private" }
440 { "float/f" "math.private" }
441 { "float-mod" "math.private" }
442 { "float<" "math.private" }
443 { "float<=" "math.private" }
444 { "float>" "math.private" }
445 { "float>=" "math.private" }
447 { "word-xt" "words" }
448 { "getenv" "kernel.private" }
449 { "setenv" "kernel.private" }
450 { "(exists?)" "io.files.private" }
452 { "gc-stats" "memory" }
453 { "save-image" "memory" }
454 { "save-image-and-exit" "memory" }
455 { "datastack" "kernel" }
456 { "retainstack" "kernel" }
457 { "callstack" "kernel" }
458 { "set-datastack" "kernel" }
459 { "set-retainstack" "kernel" }
460 { "set-callstack" "kernel" }
462 { "data-room" "memory" }
463 { "code-room" "memory" }
464 { "micros" "system" }
465 { "modify-code-heap" "compiler.units" }
468 { "dlclose" "alien" }
469 { "<byte-array>" "byte-arrays" }
470 { "(byte-array)" "byte-arrays" }
471 { "<displaced-alien>" "alien" }
472 { "alien-signed-cell" "alien.accessors" }
473 { "set-alien-signed-cell" "alien.accessors" }
474 { "alien-unsigned-cell" "alien.accessors" }
475 { "set-alien-unsigned-cell" "alien.accessors" }
476 { "alien-signed-8" "alien.accessors" }
477 { "set-alien-signed-8" "alien.accessors" }
478 { "alien-unsigned-8" "alien.accessors" }
479 { "set-alien-unsigned-8" "alien.accessors" }
480 { "alien-signed-4" "alien.accessors" }
481 { "set-alien-signed-4" "alien.accessors" }
482 { "alien-unsigned-4" "alien.accessors" }
483 { "set-alien-unsigned-4" "alien.accessors" }
484 { "alien-signed-2" "alien.accessors" }
485 { "set-alien-signed-2" "alien.accessors" }
486 { "alien-unsigned-2" "alien.accessors" }
487 { "set-alien-unsigned-2" "alien.accessors" }
488 { "alien-signed-1" "alien.accessors" }
489 { "set-alien-signed-1" "alien.accessors" }
490 { "alien-unsigned-1" "alien.accessors" }
491 { "set-alien-unsigned-1" "alien.accessors" }
492 { "alien-float" "alien.accessors" }
493 { "set-alien-float" "alien.accessors" }
494 { "alien-double" "alien.accessors" }
495 { "set-alien-double" "alien.accessors" }
496 { "alien-cell" "alien.accessors" }
497 { "set-alien-cell" "alien.accessors" }
498 { "(throw)" "kernel.private" }
499 { "alien-address" "alien" }
500 { "set-slot" "slots.private" }
501 { "string-nth" "strings.private" }
502 { "set-string-nth-fast" "strings.private" }
503 { "set-string-nth-slow" "strings.private" }
504 { "resize-array" "arrays" }
505 { "resize-string" "strings" }
506 { "<array>" "arrays" }
507 { "begin-scan" "memory" }
508 { "next-object" "memory" }
509 { "end-scan" "memory" }
512 { "fopen" "io.streams.c" }
513 { "fgetc" "io.streams.c" }
514 { "fread" "io.streams.c" }
515 { "fputc" "io.streams.c" }
516 { "fwrite" "io.streams.c" }
517 { "fflush" "io.streams.c" }
518 { "fclose" "io.streams.c" }
519 { "<wrapper>" "kernel" }
520 { "(clone)" "kernel" }
521 { "<string>" "strings" }
522 { "array>quotation" "quotations.private" }
523 { "quotation-xt" "quotations" }
524 { "<tuple>" "classes.tuple.private" }
525 { "profiling" "tools.profiler.private" }
526 { "become" "kernel.private" }
527 { "(sleep)" "threads.private" }
528 { "<tuple-boa>" "classes.tuple.private" }
529 { "callstack>array" "kernel" }
530 { "innermost-frame-quot" "kernel.private" }
531 { "innermost-frame-scan" "kernel.private" }
532 { "set-innermost-frame-quot" "kernel.private" }
533 { "call-clear" "kernel" }
534 { "resize-byte-array" "byte-arrays" }
535 { "dll-valid?" "alien" }
536 { "unimplemented" "kernel.private" }
537 { "gc-reset" "memory" }
538 { "jit-compile" "quotations" }
539 { "load-locals" "locals.backend" }
541 [ [ first2 ] dip make-primitive ] each-index
544 "build" "kernel" create build 1+ 1quotation define