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.order namespaces make
5 parser sequences strings vectors words quotations assocs layouts
6 classes classes.builtin classes.tuple classes.tuple.private
7 kernel.private vocabs vocabs.loader source-files definitions
8 slots classes.union classes.intersection classes.predicate
9 compiler.units bootstrap.image.private io.files accessors
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
74 "classes.tuple.private"
77 "continuations.private"
104 "tools.profiler.private"
109 } [ create-vocab drop ] each
112 : define-builtin-predicate ( class -- )
113 dup class>type [ builtin-instance? ] curry define-predicate ;
115 : lookup-type-number ( word -- n )
116 global [ target-word ] bind type-number ;
118 : register-builtin ( class -- )
119 [ dup lookup-type-number "type" set-word-prop ]
120 [ dup "type" word-prop builtins get set-nth ]
121 [ f f f builtin-class define-class ]
124 : prepare-slots ( slots -- slots' )
125 [ [ dup pair? [ first2 create ] when ] map ] map ;
127 : define-builtin-slots ( class slots -- )
128 prepare-slots make-slots 1 finalize-slots
129 [ "slots" set-word-prop ] [ define-accessors ] 2bi ;
131 : define-builtin ( symbol slotspec -- )
132 [ [ define-builtin-predicate ] keep ] dip define-builtin-slots ;
134 "fixnum" "math" create register-builtin
135 "bignum" "math" create register-builtin
136 "tuple" "kernel" create register-builtin
137 "ratio" "math" create register-builtin
138 "float" "math" create register-builtin
139 "complex" "math" create register-builtin
140 "f" "syntax" lookup register-builtin
141 "array" "arrays" create register-builtin
142 "wrapper" "kernel" create register-builtin
143 "callstack" "kernel" create register-builtin
144 "string" "strings" create register-builtin
145 "quotation" "quotations" create register-builtin
146 "dll" "alien" create register-builtin
147 "alien" "alien" create register-builtin
148 "word" "words" create register-builtin
149 "byte-array" "byte-arrays" create register-builtin
151 ! For predicate classes
152 "predicate-instance?" "classes.predicate" create drop
154 ! We need this before defining c-ptr below
155 "f" "syntax" lookup { } define-builtin
157 "f" "syntax" create [ not ] "predicate" set-word-prop
158 "f?" "syntax" vocab-words delete-at
161 "integer" "math" create
162 "fixnum" "math" lookup
163 "bignum" "math" lookup
167 "rational" "math" create
168 "integer" "math" lookup
169 "ratio" "math" lookup
174 "rational" "math" lookup
175 "float" "math" lookup
179 "c-ptr" "alien" create [
180 "alien" "alien" lookup ,
181 "f" "syntax" lookup ,
182 "byte-array" "byte-arrays" lookup ,
183 ] { } make define-union-class
185 ! A predicate class used for declarations
186 "array-capacity" "sequences.private" create
187 "fixnum" "math" lookup
188 0 bootstrap-max-array-capacity <fake-bignum> [ between? ] 2curry
189 define-predicate-class
191 ! Catch-all class for providing a default method.
192 "object" "kernel" create
193 [ f f { } intersection-class define-class ]
194 [ [ drop t ] "predicate" set-word-prop ]
197 "object?" "kernel" vocab-words delete-at
199 ! Class of objects with object tag
200 "hi-tag" "kernel.private" create
201 builtins get num-tags get tail define-union-class
203 ! Empty class with no instances
204 "null" "kernel" create
205 [ f { } f union-class define-class ]
206 [ [ drop f ] "predicate" set-word-prop ]
209 "null?" "kernel" vocab-words delete-at
211 "fixnum" "math" create { } define-builtin
212 "fixnum" "math" create ">fixnum" "math" create 1quotation "coercer" set-word-prop
214 "bignum" "math" create { } define-builtin
215 "bignum" "math" create ">bignum" "math" create 1quotation "coercer" set-word-prop
217 "ratio" "math" create {
218 { "numerator" { "integer" "math" } read-only }
219 { "denominator" { "integer" "math" } read-only }
222 "float" "math" create { } define-builtin
223 "float" "math" create ">float" "math" create 1quotation "coercer" set-word-prop
225 "complex" "math" create {
226 { "real" { "real" "math" } read-only }
227 { "imaginary" { "real" "math" } read-only }
230 "array" "arrays" create {
231 { "length" { "array-capacity" "sequences.private" } read-only }
234 "wrapper" "kernel" create {
235 { "wrapped" read-only }
238 "string" "strings" create {
239 { "length" { "array-capacity" "sequences.private" } read-only }
243 "quotation" "quotations" create {
244 { "array" { "array" "arrays" } read-only }
245 { "compiled" read-only }
248 "dll" "alien" create {
249 { "path" { "byte-array" "byte-arrays" } read-only }
252 "alien" "alien" create {
253 { "underlying" { "c-ptr" "alien" } read-only }
257 "word" "words" create {
258 { "hashcode" { "fixnum" "math" } }
261 { "def" { "quotation" "quotations" } initial: [ ] }
263 { "compiled" read-only }
264 { "counter" { "fixnum" "math" } }
265 { "sub-primitive" read-only }
268 "byte-array" "byte-arrays" create {
269 { "length" { "array-capacity" "sequences.private" } read-only }
272 "callstack" "kernel" create { } define-builtin
274 "tuple" "kernel" create
275 [ { } define-builtin ]
276 [ define-tuple-layout ]
279 ! Create special tombstone values
280 "tombstone" "hashtables.private" create
282 { "state" } define-tuple-class
284 "((empty))" "hashtables.private" create
285 "tombstone" "hashtables.private" lookup f
286 2array >tuple 1quotation define-inline
288 "((tombstone))" "hashtables.private" create
289 "tombstone" "hashtables.private" lookup t
290 2array >tuple 1quotation define-inline
293 "curry" "kernel" create
298 } prepare-slots define-tuple-class
300 "curry" "kernel" lookup
302 [ f "inline" set-word-prop ]
307 callable instance-check-quot %
313 (( obj quot -- curry )) define-declared
315 "compose" "kernel" create
318 { "first" read-only }
319 { "second" read-only }
320 } prepare-slots define-tuple-class
322 "compose" "kernel" lookup
324 [ f "inline" set-word-prop ]
329 callable instance-check-quot [ dip ] curry %
330 callable instance-check-quot %
336 (( quot1 quot2 -- compose )) define-declared
338 ! Sub-primitive words
339 : make-sub-primitive ( word vocab -- )
342 dup 1quotation define ;
345 { "(execute)" "words.private" }
346 { "(call)" "kernel.private" }
347 { "fixnum+fast" "math.private" }
348 { "fixnum-fast" "math.private" }
349 { "fixnum*fast" "math.private" }
350 { "fixnum-bitand" "math.private" }
351 { "fixnum-bitor" "math.private" }
352 { "fixnum-bitxor" "math.private" }
353 { "fixnum-bitnot" "math.private" }
354 { "fixnum-mod" "math.private" }
355 { "fixnum-shift-fast" "math.private" }
356 { "fixnum/i-fast" "math.private" }
357 { "fixnum/mod-fast" "math.private" }
358 { "fixnum<" "math.private" }
359 { "fixnum<=" "math.private" }
360 { "fixnum>" "math.private" }
361 { "fixnum>=" "math.private" }
381 { "tag" "kernel.private" }
382 { "slot" "slots.private" }
383 { "get-local" "locals.backend" }
384 { "drop-locals" "locals.backend" }
385 } [ make-sub-primitive ] assoc-each
388 : make-primitive ( word vocab n -- )
389 [ create dup reset-word ] dip
390 [ do-primitive ] curry [ ] like define ;
393 { "bignum>fixnum" "math.private" }
394 { "float>fixnum" "math.private" }
395 { "fixnum>bignum" "math.private" }
396 { "float>bignum" "math.private" }
397 { "fixnum>float" "math.private" }
398 { "bignum>float" "math.private" }
399 { "<ratio>" "math.private" }
400 { "string>float" "math.private" }
401 { "float>string" "math.private" }
402 { "float>bits" "math" }
403 { "double>bits" "math" }
404 { "bits>float" "math" }
405 { "bits>double" "math" }
406 { "<complex>" "math.private" }
407 { "fixnum+" "math.private" }
408 { "fixnum-" "math.private" }
409 { "fixnum*" "math.private" }
410 { "fixnum/i" "math.private" }
411 { "fixnum/mod" "math.private" }
412 { "fixnum-shift" "math.private" }
413 { "bignum=" "math.private" }
414 { "bignum+" "math.private" }
415 { "bignum-" "math.private" }
416 { "bignum*" "math.private" }
417 { "bignum/i" "math.private" }
418 { "bignum-mod" "math.private" }
419 { "bignum/mod" "math.private" }
420 { "bignum-bitand" "math.private" }
421 { "bignum-bitor" "math.private" }
422 { "bignum-bitxor" "math.private" }
423 { "bignum-bitnot" "math.private" }
424 { "bignum-shift" "math.private" }
425 { "bignum<" "math.private" }
426 { "bignum<=" "math.private" }
427 { "bignum>" "math.private" }
428 { "bignum>=" "math.private" }
429 { "bignum-bit?" "math.private" }
430 { "bignum-log2" "math.private" }
431 { "byte-array>bignum" "math" }
432 { "float=" "math.private" }
433 { "float+" "math.private" }
434 { "float-" "math.private" }
435 { "float*" "math.private" }
436 { "float/f" "math.private" }
437 { "float-mod" "math.private" }
438 { "float<" "math.private" }
439 { "float<=" "math.private" }
440 { "float>" "math.private" }
441 { "float>=" "math.private" }
443 { "word-xt" "words" }
444 { "getenv" "kernel.private" }
445 { "setenv" "kernel.private" }
446 { "(exists?)" "io.files.private" }
448 { "gc-stats" "memory" }
449 { "save-image" "memory" }
450 { "save-image-and-exit" "memory" }
451 { "datastack" "kernel" }
452 { "retainstack" "kernel" }
453 { "callstack" "kernel" }
454 { "set-datastack" "kernel" }
455 { "set-retainstack" "kernel" }
456 { "set-callstack" "kernel" }
458 { "data-room" "memory" }
459 { "code-room" "memory" }
460 { "millis" "system" }
461 { "modify-code-heap" "compiler.units" }
464 { "dlclose" "alien" }
465 { "<byte-array>" "byte-arrays" }
466 { "<displaced-alien>" "alien" }
467 { "alien-signed-cell" "alien.accessors" }
468 { "set-alien-signed-cell" "alien.accessors" }
469 { "alien-unsigned-cell" "alien.accessors" }
470 { "set-alien-unsigned-cell" "alien.accessors" }
471 { "alien-signed-8" "alien.accessors" }
472 { "set-alien-signed-8" "alien.accessors" }
473 { "alien-unsigned-8" "alien.accessors" }
474 { "set-alien-unsigned-8" "alien.accessors" }
475 { "alien-signed-4" "alien.accessors" }
476 { "set-alien-signed-4" "alien.accessors" }
477 { "alien-unsigned-4" "alien.accessors" }
478 { "set-alien-unsigned-4" "alien.accessors" }
479 { "alien-signed-2" "alien.accessors" }
480 { "set-alien-signed-2" "alien.accessors" }
481 { "alien-unsigned-2" "alien.accessors" }
482 { "set-alien-unsigned-2" "alien.accessors" }
483 { "alien-signed-1" "alien.accessors" }
484 { "set-alien-signed-1" "alien.accessors" }
485 { "alien-unsigned-1" "alien.accessors" }
486 { "set-alien-unsigned-1" "alien.accessors" }
487 { "alien-float" "alien.accessors" }
488 { "set-alien-float" "alien.accessors" }
489 { "alien-double" "alien.accessors" }
490 { "set-alien-double" "alien.accessors" }
491 { "alien-cell" "alien.accessors" }
492 { "set-alien-cell" "alien.accessors" }
493 { "(throw)" "kernel.private" }
494 { "alien-address" "alien" }
495 { "set-slot" "slots.private" }
496 { "string-nth" "strings.private" }
497 { "set-string-nth" "strings.private" }
498 { "resize-array" "arrays" }
499 { "resize-string" "strings" }
500 { "<array>" "arrays" }
501 { "begin-scan" "memory" }
502 { "next-object" "memory" }
503 { "end-scan" "memory" }
506 { "fopen" "io.streams.c" }
507 { "fgetc" "io.streams.c" }
508 { "fread" "io.streams.c" }
509 { "fputc" "io.streams.c" }
510 { "fwrite" "io.streams.c" }
511 { "fflush" "io.streams.c" }
512 { "fclose" "io.streams.c" }
513 { "<wrapper>" "kernel" }
514 { "(clone)" "kernel" }
515 { "<string>" "strings" }
516 { "array>quotation" "quotations.private" }
517 { "quotation-xt" "quotations" }
518 { "<tuple>" "classes.tuple.private" }
519 { "profiling" "tools.profiler.private" }
520 { "become" "kernel.private" }
521 { "(sleep)" "threads.private" }
522 { "<tuple-boa>" "classes.tuple.private" }
523 { "callstack>array" "kernel" }
524 { "innermost-frame-quot" "kernel.private" }
525 { "innermost-frame-scan" "kernel.private" }
526 { "set-innermost-frame-quot" "kernel.private" }
527 { "call-clear" "kernel" }
528 { "resize-byte-array" "byte-arrays" }
529 { "dll-valid?" "alien" }
530 { "unimplemented" "kernel.private" }
531 { "gc-reset" "memory" }
533 [ [ first2 ] dip make-primitive ] each-index
536 "build" "kernel" create build 1+ 1quotation define