factor/library/bootstrap/image.factor

352 lines
8.5 KiB
Factor
Raw Normal View History

! Copyright (C) 2004, 2006 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
2004-07-16 02:26:21 -04:00
2004-10-27 23:13:00 -04:00
! This library allows one to generate a new set of bootstrap
! images.
2004-10-27 23:13:00 -04:00
!
! It does this by parsing the set of source files needed to
! generate the minimal image, and writing the cons cells, words,
! strings etc to the image file in the CFactor object memory
! format.
USING: alien arrays errors generic hashtables
2006-05-17 14:55:46 -04:00
hashtables-internals help io kernel kernel-internals math
namespaces parser prettyprint sequences sequences-internals
strings vectors words ;
2005-12-02 02:25:44 -05:00
IN: image
( Constants )
: image-magic HEX: 0f0e0d0c ; inline
: image-version 0 ; inline
: char bootstrap-cell 2 /i ; inline
: untag ( cell tag -- ) tag-mask bitnot bitand ; inline
: tag ( cell -- tag ) tag-mask bitand ; inline
2006-05-16 16:50:51 -04:00
: array-type 8 ; inline
: hashtable-type 10 ; inline
: vector-type 11 ; inline
: string-type 12 ; inline
: sbuf-type 13 ; inline
2006-05-18 01:08:09 -04:00
: quotation-type 14 ; inline
: dll-type 15 ; inline
: alien-type 16 ; inline
2006-05-16 16:50:51 -04:00
: tuple-type 17 ; inline
: byte-array-type 18 ; inline
2006-05-28 17:31:54 -04:00
: base 1024 ; inline
2006-05-28 17:31:54 -04:00
: boot-quot-offset 3 ; inline
: global-offset 4 ; inline
: t-offset 5 ; inline
: 0-offset 6 ; inline
: 1-offset 7 ; inline
: -1-offset 8 ; inline
: heap-size-offset 9 ; inline
: header-size 10 ; inline
2004-11-26 22:23:57 -05:00
! The image being constructed; a vector of word-size integers
SYMBOL: image
2005-08-07 00:00:57 -04:00
! Object cache
SYMBOL: objects
! Image output format
SYMBOL: big-endian
! Bootstrap architecture name
SYMBOL: architecture
2005-08-07 00:00:57 -04:00
2005-04-06 21:41:49 -04:00
: emit ( cell -- ) image get push ;
2004-11-26 22:23:57 -05:00
: d>w/w ( d -- w w )
2005-10-08 01:15:14 -04:00
dup HEX: ffffffff bitand swap -32 shift HEX: ffffffff bitand ;
2005-09-16 22:47:28 -04:00
: emit-64 ( cell -- )
bootstrap-cell 8 = [
2005-09-16 22:47:28 -04:00
emit
] [
d>w/w big-endian get [ swap ] unless emit emit
2005-09-24 15:21:17 -04:00
] if ;
2005-09-16 22:47:28 -04:00
2005-07-31 23:38:33 -04:00
: emit-seq ( seq -- ) image get swap nappend ;
2005-04-06 21:41:49 -04:00
: fixup ( value offset -- ) image get set-nth ;
2004-07-16 02:26:21 -04:00
: here ( -- size )
image get length header-size - bootstrap-cells base + ;
2004-07-16 02:26:21 -04:00
: here-as ( tag -- pointer ) here swap bitor ;
2004-07-16 02:26:21 -04:00
: align-here ( -- )
here 8 mod 4 = [ 0 emit ] when ;
2004-07-16 02:26:21 -04:00
: emit-fixnum ( n -- ) fixnum-tag tag-address emit ;
2004-07-16 02:26:21 -04:00
: emit-object ( header tag quot -- addr )
swap here-as >r swap tag-header emit call align-here r> ;
2004-07-24 15:11:55 -04:00
( Image header )
2004-07-16 02:26:21 -04:00
: header ( -- )
image-magic emit
image-version emit
2004-09-08 02:31:03 -04:00
( relocation base at end of header ) base emit
2004-07-16 02:26:21 -04:00
( bootstrap quotation set later ) 0 emit
( global namespace set later ) 0 emit
2005-05-10 22:30:58 -04:00
( pointer to t object ) 0 emit
( pointer to bignum 0 ) 0 emit
( pointer to bignum 1 ) 0 emit
( pointer to bignum -1 ) 0 emit
2004-07-16 02:26:21 -04:00
( size of heap set later ) 0 emit ;
GENERIC: ' ( obj -- ptr )
#! Write an object to the image.
2004-08-06 02:51:32 -04:00
( Bignums )
: bignum-bits bootstrap-cell-bits 2 - ;
2005-10-08 01:15:14 -04:00
: bignum-radix bignum-bits 1 swap shift 1- ;
: (bignum>seq) ( n -- )
2006-01-28 15:49:31 -05:00
dup zero? [
2005-10-08 01:15:14 -04:00
drop
] [
dup bignum-radix bitand ,
bignum-bits neg shift (bignum>seq)
] if ;
: bignum>seq ( n -- seq )
#! n is positive or zero.
[ (bignum>seq) ] { } make ;
2005-10-08 01:15:14 -04:00
: emit-bignum ( n -- )
[ 0 < 1 0 ? ] keep abs bignum>seq
dup length 1+ emit-fixnum
swap emit emit-seq ;
M: bignum ' ( bignum -- tagged )
#! This can only emit 0, -1 and 1.
bignum-tag bignum-tag [ emit-bignum ] emit-object ;
2004-08-06 02:51:32 -04:00
2006-05-14 20:05:57 -04:00
( Fixnums )
M: fixnum ' ( n -- tagged )
#! When generating a 32-bit image on a 64-bit system,
#! some fixnums should be bignums.
dup most-negative-fixnum most-positive-fixnum between?
[ fixnum-tag tag-address ] [ >bignum ' ] if ;
2006-05-14 20:05:57 -04:00
2005-09-16 22:47:28 -04:00
( Floats )
M: float ' ( float -- tagged )
float-tag float-tag [
align-here double>bits emit-64
] emit-object ;
2005-09-16 22:47:28 -04:00
2004-07-24 15:11:55 -04:00
( Special objects )
2004-07-16 02:26:21 -04:00
! Padded with fixnums for 8-byte alignment
2005-09-09 17:32:38 -04:00
: t, t t-offset fixup ;
M: f ' ( obj -- ptr )
#! f is #define F RETAG(0,OBJECT_TYPE)
drop object-tag ;
2004-07-16 02:26:21 -04:00
2005-05-10 22:30:58 -04:00
: 0, 0 >bignum ' 0-offset fixup ;
: 1, 1 >bignum ' 1-offset fixup ;
: -1, -1 >bignum ' -1-offset fixup ;
2004-08-29 01:04:42 -04:00
2004-07-24 15:11:55 -04:00
( Beginning of the image )
2005-01-27 20:06:10 -05:00
! The image begins with the header, then T,
2004-08-29 01:04:42 -04:00
! and the bignums 0, 1, and -1.
2004-07-16 02:26:21 -04:00
: begin-image ( -- ) header t, 0, 1, -1, ;
2004-07-16 02:26:21 -04:00
2004-07-24 15:11:55 -04:00
( Words )
2004-07-16 02:26:21 -04:00
2005-07-31 23:38:33 -04:00
: emit-word ( word -- )
[
dup hashcode ' ,
dup word-name ' ,
dup word-vocabulary ' ,
dup word-primitive ' ,
dup word-def ' ,
dup word-props ' ,
0 ,
] { } make
2006-05-18 01:08:09 -04:00
word-tag word-tag [ emit-seq ] emit-object
swap objects get set-hash ;
: word-error ( word msg -- )
2005-09-17 22:25:18 -04:00
[ % dup word-vocabulary % " " % word-name % ] "" make throw ;
: transfer-word ( word -- word )
#! This is a hack. See doc/bootstrap.txt.
dup target-word [ ] [ "Missing DEFER: " word-error ] ?if ;
2004-07-16 02:26:21 -04:00
2004-09-30 21:49:49 -04:00
: fixup-word ( word -- offset )
2006-05-28 17:31:54 -04:00
transfer-word dup objects get hash
[ ] [ "Not in image: " word-error ] ?if ;
2004-07-16 02:26:21 -04:00
: fixup-words ( -- )
image get [ dup word? [ fixup-word ] when ] inject ;
2004-07-16 02:26:21 -04:00
2005-08-07 00:00:57 -04:00
M: word ' ( word -- pointer ) ;
( Wrappers )
M: wrapper ' ( wrapper -- pointer )
2006-05-18 01:08:09 -04:00
wrapped ' wrapper-tag wrapper-tag [ emit ] emit-object ;
2004-07-16 02:26:21 -04:00
2006-05-16 16:50:51 -04:00
( Ratios and complexes )
2005-09-16 22:47:28 -04:00
: emit-pair
[ [ emit ] 2apply ] emit-object ;
M: ratio ' ( c -- tagged )
>fraction [ ' ] 2apply ratio-tag ratio-tag emit-pair ;
2005-09-16 22:47:28 -04:00
M: complex ' ( c -- tagged )
>rect [ ' ] 2apply complex-tag complex-tag emit-pair ;
2004-07-16 02:26:21 -04:00
2004-07-24 15:11:55 -04:00
( Strings )
2004-07-16 02:26:21 -04:00
2005-07-31 23:38:33 -04:00
: emit-chars ( seq -- )
2006-05-14 23:25:34 -04:00
big-endian get [ [ <reversed> ] map ] unless
2005-07-31 23:38:33 -04:00
[ 0 [ swap 16 shift + ] reduce emit ] each ;
2004-07-16 02:26:21 -04:00
2005-07-31 23:38:33 -04:00
: pack-string ( string -- seq )
2005-09-16 22:47:28 -04:00
dup length 1+ char align CHAR: \0 pad-right char swap group ;
2004-07-16 02:26:21 -04:00
2005-07-31 23:38:33 -04:00
: emit-string ( string -- ptr )
string-type object-tag [
dup length emit-fixnum
dup hashcode emit-fixnum
pack-string emit-chars
] emit-object ;
2004-07-16 02:26:21 -04:00
M: string ' ( string -- pointer )
2004-07-16 02:26:21 -04:00
#! We pool strings so that each string is only written once
#! to the image
2005-08-07 00:00:57 -04:00
objects get [ emit-string ] cache ;
2004-07-16 02:26:21 -04:00
2004-07-24 15:11:55 -04:00
( Arrays and vectors )
2004-07-16 02:26:21 -04:00
: emit-array ( list type -- pointer )
>r [ ' ] map r> object-tag [
dup length emit-fixnum
( elements -- ) emit-seq
] emit-object ;
2004-07-16 02:26:21 -04:00
2005-11-27 17:45:48 -05:00
: transfer-tuple ( tuple -- tuple )
tuple>array
dup first transfer-word 0 pick set-nth
>tuple ;
2005-11-27 17:45:48 -05:00
M: tuple ' ( tuple -- pointer )
2005-11-27 17:45:48 -05:00
transfer-tuple
objects get [ tuple>array tuple-type emit-array ] cache ;
2005-09-11 21:18:19 -04:00
M: array ' ( array -- pointer )
array-type emit-array ;
2006-05-17 14:55:46 -04:00
M: quotation ' ( array -- pointer )
quotation-type emit-array ;
2006-05-16 16:50:51 -04:00
2005-08-07 00:00:57 -04:00
M: vector ' ( vector -- pointer )
2005-11-27 17:45:48 -05:00
dup underlying ' swap length
vector-type object-tag [
emit-fixnum ( length )
emit ( array ptr )
] emit-object ;
2004-07-16 02:26:21 -04:00
2006-01-02 00:51:03 -05:00
M: sbuf ' ( sbuf -- pointer )
dup underlying ' swap length
sbuf-type object-tag [
emit-fixnum ( length )
emit ( array ptr )
] emit-object ;
2006-01-02 00:51:03 -05:00
2005-08-07 00:00:57 -04:00
( Hashes )
2004-12-27 22:58:43 -05:00
2005-08-07 00:00:57 -04:00
M: hashtable ' ( hashtable -- pointer )
[ hash-array ' ] keep
hashtable-type object-tag [
dup hash-count emit-fixnum
hash-deleted emit-fixnum
emit ( array ptr )
] emit-object ;
2005-01-27 20:06:10 -05:00
2004-07-24 15:11:55 -04:00
( End of the image )
2004-07-16 02:26:21 -04:00
2005-07-31 23:38:33 -04:00
: words, ( -- )
all-words [ emit-word ] each ;
2004-07-16 02:26:21 -04:00
: global, ( -- )
[
2005-12-25 22:18:25 -05:00
{
vocabularies typemap builtins c-types crossref
2006-06-22 22:36:56 -04:00
articles parent-graph term-index
2006-05-28 17:31:54 -04:00
} [ dup get swap bootstrap-word set ] each
] make-hash '
global-offset fixup ;
2004-11-26 22:23:57 -05:00
2005-08-22 16:01:13 -04:00
: boot, ( quot -- ) ' boot-quot-offset fixup ;
2004-07-16 02:26:21 -04:00
: heap-size image get length header-size - bootstrap-cells ;
2005-08-07 00:00:57 -04:00
: end-image ( quot -- )
2005-12-16 21:12:35 -05:00
"Generating words..." print flush
2005-07-31 23:38:33 -04:00
words,
2005-12-16 21:12:35 -05:00
"Generating global namespace..." print flush
global,
2005-12-16 21:12:35 -05:00
"Generating boot quotation..." print flush
2004-12-27 22:58:43 -05:00
boot,
2005-12-16 21:12:35 -05:00
"Performing some word fixups..." print flush
2004-09-08 02:31:03 -04:00
fixup-words
heap-size heap-size-offset fixup
"Image length: " write image get length .
"Object cache size: " write objects get hash-size .
\ word global remove-hash ;
2004-07-16 02:26:21 -04:00
2004-07-24 15:11:55 -04:00
( Image output )
2004-07-16 02:26:21 -04:00
2005-06-10 17:41:41 -04:00
: (write-image) ( image -- )
bootstrap-cell swap big-endian get [
[ swap >be write ] each-with
2004-07-17 18:35:09 -04:00
] [
[ swap >le write ] each-with
2005-09-24 15:21:17 -04:00
] if ;
2004-07-17 18:35:09 -04:00
2005-12-14 20:29:32 -05:00
: image-name
"boot.image." architecture get append resource-path ;
2005-12-14 20:29:32 -05:00
: write-image ( image -- )
2005-12-16 21:12:35 -05:00
"Writing image to " write dup write "..." print flush
2005-06-10 17:41:41 -04:00
<file-writer> [ (write-image) ] with-stream ;
2004-07-16 02:26:21 -04:00
: prepare-profile ( arch -- )
"/library/bootstrap/profile-" swap ".factor" append3
run-resource ;
2004-07-16 02:26:21 -04:00
: prepare-image ( arch -- )
bootstrapping? on dup architecture set prepare-profile
800000 <vector> image set 20000 <hashtable> objects set ;
2005-12-14 20:29:32 -05:00
: make-image ( architecture -- )
[
prepare-image
begin-image
"/library/bootstrap/boot-stage1.factor" run-resource
end-image
2005-12-14 20:29:32 -05:00
image get image-name write-image
] with-scope ;
: make-images ( -- )
2006-05-05 02:08:37 -04:00
{ "x86" "pentium4" "ppc" "amd64" } [ make-image ] each ;