factor/library/bootstrap/image.factor

313 lines
7.2 KiB
Factor
Raw Normal View History

! Copyright (C) 2004, 2005 Slava Pestov.
! See http://factor.sf.net/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 (boot.image.{le32,le64,be32,be64}.
!
! 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.
2004-09-11 15:26:24 -04:00
IN: image
2005-09-11 21:18:19 -04:00
USING: arrays errors generic hashtables kernel lists
math namespaces parser prettyprint sequences
sequences-internals io strings vectors words ;
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
SYMBOL: 64-bits
2005-04-06 21:41:49 -04:00
: emit ( cell -- ) image get push ;
2004-11-26 22:23:57 -05: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
2004-07-24 15:11:55 -04:00
( Object memory )
2004-07-16 02:26:21 -04:00
: image-magic HEX: 0f0e0d0c ;
: image-version 0 ;
2005-08-07 00:00:57 -04:00
: cell 64-bits get 8 4 ? ;
: char 64-bits get 4 2 ? ;
2004-07-16 02:26:21 -04:00
: untag ( cell tag -- ) tag-mask bitnot bitand ;
: tag ( cell -- tag ) tag-mask bitand ;
2005-01-27 20:06:10 -05:00
: t-type 7 ; inline
: array-type 8 ; inline
: hashtable-type 10 ; inline
: vector-type 11 ; inline
: string-type 12 ; inline
: wrapper-type 14 ; inline
2005-01-27 20:06:10 -05:00
: word-type 17 ; inline
: tuple-type 18 ; inline
2004-08-26 19:37:22 -04:00
: immediate ( x tag -- tagged ) swap tag-bits shift bitor ;
: >header ( id -- tagged ) object-tag immediate ;
2004-07-16 02:26:21 -04:00
2004-07-24 15:11:55 -04:00
( Image header )
2004-07-16 02:26:21 -04:00
2005-08-07 00:00:57 -04:00
: base 1024 ;
2004-09-08 02:31:03 -04:00
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 ;
: boot-quot-offset 3 ;
: global-offset 4 ;
2005-05-10 22:30:58 -04:00
: t-offset 5 ;
: 0-offset 6 ;
: 1-offset 7 ;
: -1-offset 8 ;
: heap-size-offset 9 ;
: header-size 10 ;
2004-07-16 02:26:21 -04:00
GENERIC: ' ( obj -- ptr )
#! Write an object to the image.
2004-09-08 02:31:03 -04:00
( Allocator )
: here ( -- size )
image get length header-size - cell * base + ;
2004-09-08 02:31:03 -04:00
: here-as ( tag -- pointer )
here swap bitor ;
2004-07-16 02:26:21 -04:00
2004-12-19 03:04:03 -05:00
: align-here ( -- )
2004-09-08 02:31:03 -04:00
here 8 mod 4 = [ 0 emit ] when ;
2004-07-16 02:26:21 -04:00
2004-07-24 15:11:55 -04:00
( Fixnums )
2004-07-16 02:26:21 -04:00
2005-01-27 20:06:10 -05:00
: emit-fixnum ( n -- ) fixnum-tag immediate emit ;
M: fixnum ' ( n -- tagged ) fixnum-tag immediate ;
2004-07-16 02:26:21 -04:00
2004-08-06 02:51:32 -04:00
( Bignums )
M: bignum ' ( bignum -- tagged )
#! This can only emit 0, -1 and 1.
bignum-tag here-as >r
bignum-tag >header emit
2005-08-07 00:00:57 -04:00
{{
[[ 0 [ 1 0 ] ]]
[[ -1 [ 2 1 1 ] ]]
[[ 1 [ 2 0 1 ] ]]
2005-08-07 00:00:57 -04:00
}} hash unswons emit-fixnum emit-seq align-here r> ;
2004-08-06 02:51:32 -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
2004-09-08 02:31:03 -04:00
: begin ( -- ) 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 -- )
2005-08-07 00:00:57 -04:00
dup word-props ' >r
dup word-def ' >r
dup word-primitive ' >r
dup word-vocabulary ' >r
dup word-name ' >r
2005-08-07 00:00:57 -04:00
object-tag here-as over objects get set-hash
word-type >header emit
hashcode emit-fixnum
2005-08-07 00:00:57 -04:00
r> emit
r> emit
r> emit
r> emit
r> emit
0 emit ;
: word-error ( word msg -- )
2005-08-25 15:27:38 -04:00
[ % dup word-vocabulary % " " % word-name % ] "" make
2005-09-03 18:34:50 -04:00
throw ; inline
: transfer-word ( word -- word )
#! This is a hack. See doc/bootstrap.txt.
dup dup word-name swap word-vocabulary lookup
2005-03-21 14:39:46 -05:00
[ ] [ dup "Missing DEFER: " word-error ] ?ifte ;
2004-07-16 02:26:21 -04:00
2005-08-07 00:00:57 -04:00
: pooled-object ( object -- ptr ) objects get hash ;
2005-07-31 23:38:33 -04:00
2004-09-30 21:49:49 -04:00
: fixup-word ( word -- offset )
2005-08-07 00:00:57 -04:00
transfer-word dup pooled-object dup
[ nip ] [ "Not in image: " word-error ] ifte ;
2004-07-16 02:26:21 -04:00
: fixup-words ( -- )
2005-07-31 23:38:33 -04:00
image get [ dup word? [ fixup-word ] when ] nmap ;
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 )
wrapped '
object-tag here-as >r
wrapper-type >header emit
emit r> ;
2004-07-16 02:26:21 -04:00
2004-07-24 15:11:55 -04:00
( Conses )
2004-07-16 02:26:21 -04:00
M: cons ' ( c -- tagged )
uncons ' swap '
cons-tag here-as
-rot emit emit ;
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 -- )
big-endian get [ [ reverse-slice ] 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 )
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 )
2004-07-16 02:26:21 -04:00
object-tag here-as swap
2004-08-06 02:51:32 -04:00
string-type >header emit
dup length emit-fixnum
2005-01-27 20:06:10 -05:00
dup hashcode emit-fixnum
2005-07-31 23:38:33 -04:00
pack-string emit-chars
2004-12-19 03:04:03 -05:00
align-here ;
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 here-as >r
>header emit
2005-01-27 20:06:10 -05:00
dup length emit-fixnum
2005-07-31 23:38:33 -04:00
( elements -- ) emit-seq
2004-12-19 03:04:03 -05:00
align-here r> ;
2004-07-16 02:26:21 -04:00
M: tuple ' ( tuple -- pointer )
tuple>array tuple-type emit-array ;
2005-09-11 21:18:19 -04:00
M: array ' ( array -- pointer )
array-type emit-array ;
2005-08-07 00:00:57 -04:00
M: vector ' ( vector -- pointer )
dup array-type emit-array swap length
2004-07-16 02:26:21 -04:00
object-tag here-as >r
2004-08-06 02:51:32 -04:00
vector-type >header emit
2005-01-27 20:06:10 -05:00
emit-fixnum ( length )
2004-07-16 02:26:21 -04:00
emit ( array ptr )
2004-12-19 03:04:03 -05:00
align-here r> ;
2004-07-16 02:26:21 -04: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 )
dup underlying array-type emit-array
2005-08-07 00:00:57 -04:00
swap hash-size
2005-01-27 20:06:10 -05:00
object-tag here-as >r
hashtable-type >header emit
emit-fixnum ( length )
emit ( array ptr )
align-here r> ;
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, ( -- )
[
{ vocabularies typemap builtins } [ [ ] change ] 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
2005-08-07 00:00:57 -04:00
: heap-size image get length header-size - cell * ;
2004-12-27 22:58:43 -05:00
: end ( quot -- )
2005-07-31 23:38:33 -04:00
"Generating words..." print
words,
"Generating global namespace..." print
global,
2005-07-31 23:38:33 -04:00
"Generating boot quotation..." print
2004-12-27 22:58:43 -05:00
boot,
2005-07-31 23:38:33 -04:00
"Performing some word fixups..." print
2004-09-08 02:31:03 -04:00
fixup-words
2005-08-07 00:00:57 -04:00
heap-size heap-size-offset fixup ;
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 -- )
2005-08-07 00:00:57 -04:00
64-bits get 8 4 ? swap big-endian get [
[ swap >be write ] each-with
2004-07-17 18:35:09 -04:00
] [
[ swap >le write ] each-with
2004-07-17 18:35:09 -04:00
] ifte ;
2004-07-16 02:26:21 -04:00
: write-image ( image file -- )
2005-07-31 23:38:33 -04:00
"Writing image to " write dup write "..." print
2005-06-10 17:41:41 -04:00
<file-writer> [ (write-image) ] with-stream ;
2004-07-16 02:26:21 -04:00
2005-08-24 10:19:09 -04:00
: with-image ( quot -- image )
[
2005-08-24 10:19:09 -04:00
bootstrapping? on
2005-08-06 01:59:49 -04:00
800000 <vector> image set
2005-08-07 00:00:57 -04:00
20000 <hashtable> objects set
call
2005-08-07 00:00:57 -04:00
"Image length: " write image get length .
"Object cache size: " write objects get hash-size .
2004-11-26 22:23:57 -05:00
image get
\ word global remove-hash
namespace global [ "foobar" set ] bind
] with-scope ;
2004-07-16 02:26:21 -04:00
: make-image ( name -- )
2005-07-31 23:38:33 -04:00
#! Make a bootstrap image.
[
2005-08-24 10:19:09 -04:00
begin
"/library/bootstrap/boot-stage1.factor" run-resource
2005-08-24 10:19:09 -04:00
end
] with-image
swap write-image ;
: make-images ( -- )
2005-08-07 00:00:57 -04:00
64-bits off
big-endian off "boot.image.le32" make-image
big-endian on "boot.image.be32" make-image
64-bits on
big-endian off "boot.image.le64" make-image
big-endian on "boot.image.be64" make-image
64-bits off ;