factor/core/bootstrap/primitives.factor

544 lines
15 KiB
Factor
Raw Normal View History

2008-01-06 13:17:50 -05:00
! Copyright (C) 2004, 2008 Slava Pestov.
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
USING: alien arrays byte-arrays generic hashtables
2008-11-23 05:22:38 -05:00
hashtables.private io kernel math math.private math.order
namespaces make parser sequences strings vectors words
quotations assocs layouts classes classes.builtin classes.tuple
classes.tuple.private kernel.private vocabs vocabs.loader
source-files definitions slots classes.union
classes.intersection classes.predicate compiler.units
bootstrap.image.private io.files accessors combinators ;
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
crossref off
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
2009-02-15 20:53:21 -05:00
"vocab:cpu/" architecture get {
2008-02-02 00:07:19 -05:00
{ "x86.32" "x86/32" }
2008-11-07 21:34:26 -05:00
{ "winnt-x86.64" "x86/64/winnt" }
{ "unix-x86.64" "x86/64/unix" }
2008-02-02 00:07:19 -05:00
{ "linux-ppc" "ppc/linux" }
{ "macosx-ppc" "ppc/macosx" }
{ "arm" "arm" }
} at "/bootstrap.factor" 3append 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" vocab vocab-words bootstrap-syntax set {
dictionary
new-classes
changed-definitions changed-generics
outdated-generics forgotten-definitions
root-cache source-files update-map implementors-map
} [ H{ } clone swap set ] each
2008-03-24 20:52:21 -04:00
init-caches
2008-02-26 04:30:11 -05:00
2008-03-20 16:30:59 -04:00
! Vocabulary for slot accessors
"accessors" create-vocab drop
2008-02-26 04:30:11 -05:00
! Trivial recompile hook. We don't want to touch the code heap
! during stage1 bootstrap, it would just waste time.
! SINGLETON: dummy-compiler
! M: dummy-compiler recompile drop { } ;
! dummy-compiler compiler-impl set
2008-02-26 04:30:11 -05:00
[ drop { } ] recompile-hook set
2008-01-30 00:13:47 -05:00
call
call
2007-09-20 18:09:08 -04:00
call
2008-03-26 04:57:48 -04:00
! After we execute bootstrap/layouts
num-types get f <array> builtins set
bootstrapping? on
2007-09-20 18:09:08 -04:00
! Create some empty vocabs where the below primitives and
! classes will go
{
"alien"
2008-01-31 21:11:46 -05:00
"alien.accessors"
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"
"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.private"
"memory"
"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"
"tools.profiler.private"
"words"
"words.private"
"vectors"
"vectors.private"
} [ 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 )
global [ target-word ] bind type-number ;
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' )
2008-06-29 22:37:57 -04:00
[ [ dup pair? [ first2 create ] 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 ( symbol slotspec -- )
[ [ define-builtin-predicate ] keep ] dip define-builtin-slots ;
2007-09-20 18:09:08 -04:00
2008-04-02 19:50:21 -04:00
"fixnum" "math" create register-builtin
"bignum" "math" create register-builtin
"tuple" "kernel" create register-builtin
"ratio" "math" create register-builtin
"float" "math" create register-builtin
"complex" "math" create register-builtin
"f" "syntax" lookup register-builtin
"array" "arrays" create register-builtin
"wrapper" "kernel" create register-builtin
"callstack" "kernel" create register-builtin
"string" "strings" create register-builtin
"quotation" "quotations" create register-builtin
"dll" "alien" create register-builtin
"alien" "alien" create register-builtin
"word" "words" create register-builtin
"byte-array" "byte-arrays" create register-builtin
2008-06-29 22:37:57 -04:00
! For predicate classes
"predicate-instance?" "classes.predicate" create drop
! We need this before defining c-ptr below
"f" "syntax" lookup { } define-builtin
"f" "syntax" create [ not ] "predicate" set-word-prop
"f?" "syntax" vocab-words delete-at
! Some unions
"integer" "math" create
"fixnum" "math" lookup
"bignum" "math" lookup
2array
define-union-class
"rational" "math" create
"integer" "math" lookup
"ratio" "math" lookup
2array
define-union-class
"real" "math" create
"rational" "math" lookup
"float" "math" lookup
2array
define-union-class
"c-ptr" "alien" create [
"alien" "alien" lookup ,
"f" "syntax" lookup ,
"byte-array" "byte-arrays" lookup ,
] { } make define-union-class
! A predicate class used for declarations
"array-capacity" "sequences.private" create
"fixnum" "math" lookup
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
"array-capacity" "sequences.private" lookup
[ >fixnum ] bootstrap-max-array-capacity <fake-bignum> [ fixnum-bitand ] curry append
"coercer" set-word-prop
2008-04-02 19:50:21 -04:00
! Catch-all class for providing a default method.
2008-05-11 02:37:37 -04:00
"object" "kernel" create
[ f f { } intersection-class define-class ]
[ [ drop t ] "predicate" set-word-prop ]
bi
"object?" "kernel" vocab-words delete-at
2008-04-02 19:50:21 -04:00
! Class of objects with object tag
"hi-tag" "kernel.private" create
builtins get num-tags get tail define-union-class
2008-04-02 19:50:21 -04:00
! Empty class with no instances
"null" "kernel" create
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 delete-at
2007-09-20 18:09:08 -04:00
2008-03-13 19:56:24 -04:00
"fixnum" "math" create { } define-builtin
2007-09-20 18:09:08 -04:00
"fixnum" "math" create ">fixnum" "math" create 1quotation "coercer" set-word-prop
2008-03-13 19:56:24 -04:00
"bignum" "math" create { } define-builtin
2007-09-20 18:09:08 -04:00
"bignum" "math" create ">bignum" "math" create 1quotation "coercer" set-word-prop
2008-03-13 19:56:24 -04:00
"ratio" "math" create {
2008-06-30 02:44:58 -04:00
{ "numerator" { "integer" "math" } read-only }
{ "denominator" { "integer" "math" } read-only }
2007-09-20 18:09:08 -04:00
} define-builtin
2008-03-13 19:56:24 -04:00
"float" "math" create { } define-builtin
2007-09-20 18:09:08 -04:00
"float" "math" create ">float" "math" create 1quotation "coercer" set-word-prop
2008-03-13 19:56:24 -04:00
"complex" "math" create {
2008-06-30 02:44:58 -04:00
{ "real" { "real" "math" } read-only }
{ "imaginary" { "real" "math" } read-only }
2007-09-20 18:09:08 -04:00
} define-builtin
2008-07-16 17:48:09 -04:00
"array" "arrays" create {
{ "length" { "array-capacity" "sequences.private" } read-only }
} define-builtin
2008-03-13 19:56:24 -04:00
"wrapper" "kernel" create {
2008-06-30 02:44:58 -04:00
{ "wrapped" read-only }
2007-09-20 18:09:08 -04:00
} define-builtin
2008-03-13 19:56:24 -04:00
"string" "strings" create {
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
2008-03-13 19:56:24 -04:00
"quotation" "quotations" create {
2008-06-30 02:44:58 -04:00
{ "array" { "array" "arrays" } read-only }
{ "compiled" read-only }
2007-09-20 18:09:08 -04:00
} define-builtin
2008-03-13 19:56:24 -04:00
"dll" "alien" create {
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
2008-03-13 19:56:24 -04:00
"alien" "alien" create {
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
2008-03-13 19:56:24 -04:00
"word" "words" create {
{ "hashcode" { "fixnum" "math" } }
"name"
"vocabulary"
2008-06-29 22:37:57 -04:00
{ "def" { "quotation" "quotations" } initial: [ ] }
"props"
{ "optimized" read-only }
{ "counter" { "fixnum" "math" } }
{ "sub-primitive" read-only }
2007-09-20 18:09:08 -04:00
} define-builtin
2008-07-16 17:48:09 -04:00
"byte-array" "byte-arrays" create {
{ "length" { "array-capacity" "sequences.private" } read-only }
} define-builtin
2007-09-20 18:09:08 -04:00
2008-03-13 19:56:24 -04:00
"callstack" "kernel" create { } define-builtin
2007-09-20 18:09:08 -04:00
2008-07-14 00:26:43 -04:00
"tuple" "kernel" create
[ { } define-builtin ]
[ define-tuple-layout ]
2008-09-03 04:46:56 -04:00
bi
2008-03-26 04:57:48 -04:00
2007-09-20 18:09:08 -04:00
! Create special tombstone values
2008-03-26 18:07:50 -04:00
"tombstone" "hashtables.private" create
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
"tombstone" "hashtables.private" lookup f
2array >tuple 1quotation (( -- value )) define-inline
2007-09-20 18:09:08 -04:00
"((tombstone))" "hashtables.private" create
"tombstone" "hashtables.private" lookup t
2array >tuple 1quotation (( -- value )) define-inline
2007-09-20 18:09:08 -04:00
2008-01-28 19:15:21 -05:00
! Some tuple classes
"curry" "kernel" create
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
2008-03-26 04:57:48 -04:00
"curry" "kernel" lookup
{
[ f "inline" set-word-prop ]
[ make-flushable ]
[ ]
[
[
callable instance-check-quot %
tuple-layout ,
\ <tuple-boa> ,
] [ ] make
]
} cleave
(( obj quot -- curry )) define-declared
2008-03-26 04:57:48 -04:00
"compose" "kernel" create
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
2008-03-26 04:57:48 -04:00
"compose" "kernel" lookup
{
[ f "inline" set-word-prop ]
[ make-flushable ]
[ ]
[
[
callable instance-check-quot [ dip ] curry %
callable instance-check-quot %
tuple-layout ,
\ <tuple-boa> ,
] [ ] make
]
} cleave
(( quot1 quot2 -- compose )) define-declared
2008-03-26 04:57:48 -04:00
! Sub-primitive words
: make-sub-primitive ( word vocab -- )
create
dup reset-word
dup 1quotation define ;
{
{ "(execute)" "words.private" }
{ "(call)" "kernel.private" }
{ "both-fixnums?" "math.private" }
{ "fixnum+fast" "math.private" }
{ "fixnum-fast" "math.private" }
{ "fixnum*fast" "math.private" }
{ "fixnum-bitand" "math.private" }
{ "fixnum-bitor" "math.private" }
{ "fixnum-bitxor" "math.private" }
{ "fixnum-bitnot" "math.private" }
{ "fixnum-mod" "math.private" }
{ "fixnum-shift-fast" "math.private" }
{ "fixnum/i-fast" "math.private" }
{ "fixnum/mod-fast" "math.private" }
{ "fixnum<" "math.private" }
{ "fixnum<=" "math.private" }
{ "fixnum>" "math.private" }
{ "fixnum>=" "math.private" }
{ "drop" "kernel" }
{ "2drop" "kernel" }
{ "3drop" "kernel" }
{ "dup" "kernel" }
{ "2dup" "kernel" }
{ "3dup" "kernel" }
{ "rot" "kernel" }
{ "-rot" "kernel" }
{ "dupd" "kernel" }
{ "swapd" "kernel" }
{ "nip" "kernel" }
{ "2nip" "kernel" }
{ "tuck" "kernel" }
{ "over" "kernel" }
{ "pick" "kernel" }
{ "swap" "kernel" }
{ "eq?" "kernel" }
{ "tag" "kernel.private" }
{ "slot" "slots.private" }
2008-10-23 06:49:32 -04:00
{ "get-local" "locals.backend" }
2008-12-17 20:17:37 -05:00
{ "load-local" "locals.backend" }
2008-10-23 06:49:32 -04:00
{ "drop-locals" "locals.backend" }
} [ make-sub-primitive ] assoc-each
2007-12-25 18:10:05 -05:00
! Primitive words
2007-12-26 21:37:18 -05:00
: make-primitive ( word vocab n -- )
[ create dup reset-word ] dip
[ do-primitive ] curry [ ] like define ;
2007-12-25 18:10:05 -05:00
{
{ "bignum>fixnum" "math.private" }
{ "float>fixnum" "math.private" }
{ "fixnum>bignum" "math.private" }
{ "float>bignum" "math.private" }
{ "fixnum>float" "math.private" }
{ "bignum>float" "math.private" }
{ "<ratio>" "math.private" }
{ "string>float" "math.private" }
{ "float>string" "math.private" }
{ "float>bits" "math" }
{ "double>bits" "math" }
{ "bits>float" "math" }
{ "bits>double" "math" }
{ "<complex>" "math.private" }
{ "fixnum+" "math.private" }
{ "fixnum-" "math.private" }
{ "fixnum*" "math.private" }
{ "fixnum/i" "math.private" }
{ "fixnum/mod" "math.private" }
{ "fixnum-shift" "math.private" }
{ "bignum=" "math.private" }
{ "bignum+" "math.private" }
{ "bignum-" "math.private" }
{ "bignum*" "math.private" }
{ "bignum/i" "math.private" }
{ "bignum-mod" "math.private" }
{ "bignum/mod" "math.private" }
{ "bignum-bitand" "math.private" }
{ "bignum-bitor" "math.private" }
{ "bignum-bitxor" "math.private" }
{ "bignum-bitnot" "math.private" }
{ "bignum-shift" "math.private" }
{ "bignum<" "math.private" }
{ "bignum<=" "math.private" }
{ "bignum>" "math.private" }
{ "bignum>=" "math.private" }
{ "bignum-bit?" "math.private" }
{ "bignum-log2" "math.private" }
{ "byte-array>bignum" "math" }
{ "float=" "math.private" }
{ "float+" "math.private" }
{ "float-" "math.private" }
{ "float*" "math.private" }
{ "float/f" "math.private" }
{ "float-mod" "math.private" }
{ "float<" "math.private" }
{ "float<=" "math.private" }
{ "float>" "math.private" }
{ "float>=" "math.private" }
2008-01-06 13:17:50 -05:00
{ "<word>" "words" }
2007-12-25 18:10:05 -05:00
{ "word-xt" "words" }
{ "getenv" "kernel.private" }
{ "setenv" "kernel.private" }
2008-03-20 00:29:19 -04:00
{ "(exists?)" "io.files.private" }
{ "gc" "memory" }
2008-05-07 18:42:41 -04:00
{ "gc-stats" "memory" }
2007-12-25 18:10:05 -05:00
{ "save-image" "memory" }
{ "save-image-and-exit" "memory" }
{ "datastack" "kernel" }
{ "retainstack" "kernel" }
{ "callstack" "kernel" }
{ "set-datastack" "kernel" }
{ "set-retainstack" "kernel" }
{ "set-callstack" "kernel" }
{ "exit" "system" }
{ "data-room" "memory" }
{ "code-room" "memory" }
{ "micros" "system" }
{ "modify-code-heap" "compiler.units" }
2007-12-25 18:10:05 -05:00
{ "dlopen" "alien" }
{ "dlsym" "alien" }
{ "dlclose" "alien" }
{ "<byte-array>" "byte-arrays" }
{ "(byte-array)" "byte-arrays" }
2007-12-25 18:10:05 -05:00
{ "<displaced-alien>" "alien" }
2008-01-31 21:11:46 -05:00
{ "alien-signed-cell" "alien.accessors" }
{ "set-alien-signed-cell" "alien.accessors" }
{ "alien-unsigned-cell" "alien.accessors" }
{ "set-alien-unsigned-cell" "alien.accessors" }
{ "alien-signed-8" "alien.accessors" }
{ "set-alien-signed-8" "alien.accessors" }
{ "alien-unsigned-8" "alien.accessors" }
{ "set-alien-unsigned-8" "alien.accessors" }
{ "alien-signed-4" "alien.accessors" }
{ "set-alien-signed-4" "alien.accessors" }
{ "alien-unsigned-4" "alien.accessors" }
{ "set-alien-unsigned-4" "alien.accessors" }
{ "alien-signed-2" "alien.accessors" }
{ "set-alien-signed-2" "alien.accessors" }
{ "alien-unsigned-2" "alien.accessors" }
{ "set-alien-unsigned-2" "alien.accessors" }
{ "alien-signed-1" "alien.accessors" }
{ "set-alien-signed-1" "alien.accessors" }
{ "alien-unsigned-1" "alien.accessors" }
{ "set-alien-unsigned-1" "alien.accessors" }
{ "alien-float" "alien.accessors" }
{ "set-alien-float" "alien.accessors" }
{ "alien-double" "alien.accessors" }
{ "set-alien-double" "alien.accessors" }
{ "alien-cell" "alien.accessors" }
{ "set-alien-cell" "alien.accessors" }
2007-12-25 18:10:05 -05:00
{ "(throw)" "kernel.private" }
{ "alien-address" "alien" }
{ "set-slot" "slots.private" }
2008-02-01 00:00:08 -05:00
{ "string-nth" "strings.private" }
{ "set-string-nth-fast" "strings.private" }
{ "set-string-nth-slow" "strings.private" }
2007-12-25 18:10:05 -05:00
{ "resize-array" "arrays" }
{ "resize-string" "strings" }
{ "<array>" "arrays" }
{ "begin-scan" "memory" }
{ "next-object" "memory" }
{ "end-scan" "memory" }
{ "size" "memory" }
{ "die" "kernel" }
{ "fopen" "io.streams.c" }
{ "fgetc" "io.streams.c" }
{ "fread" "io.streams.c" }
{ "fputc" "io.streams.c" }
2007-12-25 18:10:05 -05:00
{ "fwrite" "io.streams.c" }
{ "fflush" "io.streams.c" }
{ "fclose" "io.streams.c" }
{ "<wrapper>" "kernel" }
{ "(clone)" "kernel" }
{ "<string>" "strings" }
{ "array>quotation" "quotations.private" }
{ "quotation-xt" "quotations" }
{ "<tuple>" "classes.tuple.private" }
2007-12-25 18:10:05 -05:00
{ "profiling" "tools.profiler.private" }
{ "become" "kernel.private" }
{ "(sleep)" "threads.private" }
{ "<tuple-boa>" "classes.tuple.private" }
2007-12-25 18:10:05 -05:00
{ "callstack>array" "kernel" }
{ "innermost-frame-quot" "kernel.private" }
{ "innermost-frame-scan" "kernel.private" }
{ "set-innermost-frame-quot" "kernel.private" }
{ "call-clear" "kernel" }
2008-01-29 16:04:26 -05:00
{ "resize-byte-array" "byte-arrays" }
{ "dll-valid?" "alien" }
2008-04-07 00:31:53 -04:00
{ "unimplemented" "kernel.private" }
2008-05-08 18:04:28 -04:00
{ "gc-reset" "memory" }
{ "jit-compile" "quotations" }
{ "load-locals" "locals.backend" }
2007-12-25 18:10:05 -05:00
}
[ [ first2 ] dip make-primitive ] each-index
2007-12-25 18:10:05 -05:00
2007-09-20 18:09:08 -04:00
! Bump build number
"build" "kernel" create build 1+ [ ] curry (( -- n )) define-declared