factor/core/bootstrap/primitives.factor

332 lines
8.3 KiB
Factor
Raw Normal View History

! Copyright (C) 2004, 2010 Slava Pestov.
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
USING: arrays assocs bootstrap.image.primitives
bootstrap.image.private classes classes.builtin classes.intersection
classes.predicate classes.private classes.singleton classes.tuple
classes.tuple.private classes.union combinators compiler.units io
kernel kernel.private layouts make math math.private namespaces parser
quotations sequences slots source-files splitting vocabs vocabs.loader
words ;
2008-03-20 16:30:59 -04:00
IN: bootstrap.primitives
2007-09-20 18:09:08 -04:00
"* Creating primitives and basic runtime structures..." print flush
2007-09-20 18:09:08 -04:00
H{ } clone sub-primitives set
2009-02-15 20:53:21 -05:00
"vocab:bootstrap/syntax.factor" parse-file
2007-12-24 17:18:26 -05:00
: asm-file ( arch -- file )
"-" split reverse "." join
"vocab:bootstrap/assembler/" ".factor" surround ;
architecture get asm-file parse-file
2009-02-15 20:53:21 -05:00
"vocab:bootstrap/layouts/layouts.factor" parse-file
2008-01-30 00:13:47 -05:00
! Now we have ( syntax-quot arch-quot layouts-quot ) on the stack
! Bring up a bare cross-compiling vocabulary.
"syntax" lookup-vocab vocab-words-assoc bootstrap-syntax set
2010-01-27 23:30:35 -05:00
H{ } clone dictionary set
H{ } clone root-cache set
H{ } clone source-files set
H{ } clone update-map set
H{ } clone implementors-map set
2008-02-26 04:30:11 -05:00
2010-01-27 23:30:35 -05:00
init-caches
2008-03-20 16:30:59 -04:00
bootstrapping? on
call( -- ) ! layouts quot
call( -- ) ! arch quot
2007-09-20 18:09:08 -04:00
2010-01-27 23:30:35 -05:00
! Vocabulary for slot accessors
"accessors" create-vocab drop
2008-03-26 04:57:48 -04:00
! After we execute bootstrap/layouts
num-types get f <array> builtins set
2009-11-13 09:05:02 -05:00
[
call( -- ) ! syntax-quot
2010-01-27 23:30:35 -05:00
! create-word some empty vocabs where the below primitives and
2007-09-20 18:09:08 -04:00
! classes will go
{
"alien"
2008-01-31 21:11:46 -05:00
"alien.accessors"
2009-03-26 00:00:19 -04:00
"alien.libraries"
"alien.private"
2007-09-20 18:09:08 -04:00
"arrays"
"byte-arrays"
"classes.private"
"classes.tuple"
"classes.tuple.private"
2008-06-29 22:37:57 -04:00
"classes.predicate"
"compiler.units"
2007-09-20 18:09:08 -04:00
"continuations.private"
2009-04-28 17:58:05 -04:00
"generic.single"
2009-04-24 21:43:01 -04:00
"generic.single.private"
2007-09-20 18:09:08 -04:00
"growable"
"hashtables"
"hashtables.private"
"io"
"io.files"
"io.files.private"
"io.streams.c"
2008-10-23 06:49:32 -04:00
"locals.backend"
2007-09-20 18:09:08 -04:00
"kernel"
"kernel.private"
"math"
"math.parser.private"
2007-09-20 18:09:08 -04:00
"math.private"
"memory"
"memory.private"
2007-09-20 18:09:08 -04:00
"quotations"
"quotations.private"
"sbufs"
"sbufs.private"
"scratchpad"
"sequences"
"sequences.private"
"slots.private"
"strings"
"strings.private"
"system"
2008-03-06 21:44:52 -05:00
"system.private"
2007-09-20 18:09:08 -04:00
"threads.private"
2009-11-05 02:07:59 -05:00
"tools.dispatch.private"
"tools.memory.private"
"tools.profiler.sampling.private"
2007-09-20 18:09:08 -04:00
"words"
"words.private"
2007-09-20 18:09:08 -04:00
"vectors"
"vectors.private"
2009-09-02 05:43:21 -04:00
"vm"
} [ create-vocab drop ] each
2007-09-20 18:09:08 -04:00
2007-12-25 18:10:05 -05:00
! Builtin classes
2007-09-20 18:09:08 -04:00
: lookup-type-number ( word -- n )
2011-10-19 13:40:12 -04:00
[ target-word ] with-global type-number ;
2007-09-20 18:09:08 -04:00
2008-03-13 19:56:24 -04:00
: register-builtin ( class -- )
2008-03-31 02:19:21 -04:00
[ dup lookup-type-number "type" set-word-prop ]
[ dup "type" word-prop builtins get set-nth ]
2008-05-11 00:59:02 -04:00
[ f f f builtin-class define-class ]
2008-04-02 19:50:21 -04:00
tri ;
2008-03-13 19:56:24 -04:00
: prepare-slots ( slots -- slots' )
[ [ dup pair? [ first2 create-word ] when ] map ] map ;
: define-builtin-slots ( class slots -- )
2008-07-14 00:26:43 -04:00
prepare-slots make-slots 1 finalize-slots
[ "slots" set-word-prop ] [ define-accessors ] 2bi ;
2008-03-13 19:56:24 -04:00
: define-builtin-predicate ( class -- )
dup class>type [ eq? ] curry [ tag ] prepend define-predicate ;
2008-03-13 19:56:24 -04:00
: define-builtin ( symbol slotspec -- )
[ [ define-builtin-predicate ] keep ] dip define-builtin-slots ;
2007-09-20 18:09:08 -04:00
{
{ "alien" "alien" }
{ "array" "arrays" }
{ "bignum" "math" }
{ "byte-array" "byte-arrays" }
{ "callstack" "kernel" }
{ "dll" "alien" }
{ "fixnum" "math" }
{ "float" "math" }
{ "quotation" "quotations" }
{ "string" "strings" }
{ "tuple" "kernel" }
{ "word" "words" }
{ "wrapper" "kernel" }
} [ create-word register-builtin ] assoc-each
2011-11-06 18:57:24 -05:00
"f" "syntax" lookup-word register-builtin
! We need this before defining c-ptr below
2011-11-06 18:57:24 -05:00
"f" "syntax" lookup-word { } define-builtin
"f" "syntax" create-word [ not ] "predicate" set-word-prop
"f?" "syntax" vocab-words-assoc delete-at
2011-11-06 18:57:24 -05:00
"t" "syntax" lookup-word define-singleton-class
! Some unions
"c-ptr" "alien" create-word [
2011-11-06 18:57:24 -05:00
"alien" "alien" lookup-word ,
"f" "syntax" lookup-word ,
"byte-array" "byte-arrays" lookup-word ,
] { } make define-union-class
"integer" "math" create-word
"fixnum" "math" lookup-word "bignum" "math" lookup-word 2array
define-union-class
! Two predicate classes used for declarations.
"array-capacity" "sequences.private" create-word
2011-11-06 18:57:24 -05:00
"fixnum" "math" lookup-word
2008-11-23 05:22:38 -05:00
[
[ dup 0 fixnum>= ] %
bootstrap-max-array-capacity <fake-bignum> [ fixnum<= ] curry ,
[ [ drop f ] if ] %
] [ ] make
define-predicate-class
2011-11-06 18:57:24 -05:00
"array-capacity" "sequences.private" lookup-word
[ >fixnum ] bootstrap-max-array-capacity <fake-bignum> [ fixnum-bitand ] curry append
"coercer" set-word-prop
"integer-array-capacity" "sequences.private" create-word
"integer" "math" lookup-word
[
[ dup 0 >= ] %
bootstrap-max-array-capacity <fake-bignum> [ <= ] curry ,
[ [ drop f ] if ] %
] [ ] make
define-predicate-class
2008-04-02 19:50:21 -04:00
! Catch-all class for providing a default method.
"object" "kernel" create-word
2008-05-11 02:37:37 -04:00
[ f f { } intersection-class define-class ]
[ [ drop t ] "predicate" set-word-prop ]
bi
"object?" "kernel" vocab-words-assoc delete-at
2008-04-02 19:50:21 -04:00
! Empty class with no instances
"null" "kernel" create-word
2008-05-11 00:59:02 -04:00
[ f { } f union-class define-class ]
[ [ drop f ] "predicate" set-word-prop ]
bi
"null?" "kernel" vocab-words-assoc delete-at
2007-09-20 18:09:08 -04:00
"fixnum" "math" create-word { } define-builtin
"fixnum" "math" create-word "integer>fixnum-strict" "math" create-word 1quotation "coercer" set-word-prop
2007-09-20 18:09:08 -04:00
"bignum" "math" create-word { } define-builtin
"bignum" "math" create-word ">bignum" "math" create-word 1quotation "coercer" set-word-prop
2007-09-20 18:09:08 -04:00
"float" "math" create-word { } define-builtin
"float" "math" create-word ">float" "math" create-word 1quotation "coercer" set-word-prop
2007-09-20 18:09:08 -04:00
"array" "arrays" create-word {
2008-07-16 17:48:09 -04:00
{ "length" { "array-capacity" "sequences.private" } read-only }
} define-builtin
2008-03-13 19:56:24 -04:00
"wrapper" "kernel" create-word {
2008-06-30 02:44:58 -04:00
{ "wrapped" read-only }
2007-09-20 18:09:08 -04:00
} define-builtin
"string" "strings" create-word {
2008-06-30 02:44:58 -04:00
{ "length" { "array-capacity" "sequences.private" } read-only }
"aux"
2007-09-20 18:09:08 -04:00
} define-builtin
"quotation" "quotations" create-word {
2008-06-30 02:44:58 -04:00
{ "array" { "array" "arrays" } read-only }
2009-03-16 21:11:36 -04:00
"cached-effect"
"cache-counter"
2007-09-20 18:09:08 -04:00
} define-builtin
"dll" "alien" create-word {
2008-06-30 02:44:58 -04:00
{ "path" { "byte-array" "byte-arrays" } read-only }
} define-builtin
2007-09-20 18:09:08 -04:00
"alien" "alien" create-word {
2008-06-30 02:44:58 -04:00
{ "underlying" { "c-ptr" "alien" } read-only }
"expired"
} define-builtin
2007-09-20 18:09:08 -04:00
"word" "words" create-word {
{ "hashcode" { "fixnum" "math" } }
"name"
"vocabulary"
2008-06-29 22:37:57 -04:00
{ "def" { "quotation" "quotations" } initial: [ ] }
"props"
"pic-def"
"pic-tail-def"
{ "sub-primitive" read-only }
2007-09-20 18:09:08 -04:00
} define-builtin
"byte-array" "byte-arrays" create-word {
2008-07-16 17:48:09 -04:00
{ "length" { "array-capacity" "sequences.private" } read-only }
} define-builtin
2007-09-20 18:09:08 -04:00
"callstack" "kernel" create-word { } define-builtin
2007-09-20 18:09:08 -04:00
"tuple" "kernel" create-word
2008-07-14 00:26:43 -04:00
[ { } define-builtin ]
[ define-tuple-layout ]
2008-09-03 04:46:56 -04:00
bi
2008-03-26 04:57:48 -04:00
! create-word special tombstone values
"tombstone" "hashtables.private" create-word
2008-04-14 06:27:04 -04:00
tuple
2008-09-03 04:46:56 -04:00
{ "state" } define-tuple-class
2007-09-20 18:09:08 -04:00
"+empty+" "hashtables.private" create-word
{ f } "tombstone" "hashtables.private" lookup-word
slots>tuple 1quotation ( -- value ) define-inline
2007-09-20 18:09:08 -04:00
"+tombstone+" "hashtables.private" create-word
{ t } "tombstone" "hashtables.private" lookup-word
slots>tuple 1quotation ( -- value ) define-inline
2007-09-20 18:09:08 -04:00
2008-01-28 19:15:21 -05:00
! Some tuple classes
"curried" "kernel" create-word
2008-04-14 06:27:04 -04:00
tuple
{
2008-06-30 02:44:58 -04:00
{ "obj" read-only }
{ "quot" read-only }
} prepare-slots define-tuple-class
"curry" "kernel" create-word
{
[ f "inline" set-word-prop ]
[ make-flushable ]
} cleave
"curry" "kernel" lookup-word
[
callable instance-check-quot %
"curried" "kernel" lookup-word tuple-layout ,
\ <tuple-boa> ,
] [ ] make
( obj quot -- curry ) define-declared
2008-03-26 04:57:48 -04:00
"composed" "kernel" create-word
2008-04-14 06:27:04 -04:00
tuple
{
2008-06-30 02:44:58 -04:00
{ "first" read-only }
{ "second" read-only }
} prepare-slots define-tuple-class
"compose" "kernel" create-word
{
[ f "inline" set-word-prop ]
[ make-flushable ]
} cleave
"compose" "kernel" lookup-word
[
callable instance-check-quot [ dip ] curry %
callable instance-check-quot %
"composed" "kernel" lookup-word tuple-layout ,
\ <tuple-boa> ,
] [ ] make
( quot1 quot2 -- compose ) define-declared
2008-03-26 04:57:48 -04:00
"* Declaring primitives..." print flush
all-primitives create-primitives
2007-12-25 18:10:05 -05:00
2007-09-20 18:09:08 -04:00
! Bump build number
"build" "kernel" create-word build 1 + [ ] curry ( -- n ) define-declared
2009-11-13 09:05:02 -05:00
] with-compilation-unit