Simplify code heap layout

db4
Slava Pestov 2008-01-16 15:45:04 -05:00
parent fcc7d584e6
commit c6bc074472
15 changed files with 33 additions and 93 deletions

View File

@ -19,4 +19,4 @@ IN: compiler.constants
: class-hash-offset bootstrap-cell object tag-number - ; : class-hash-offset bootstrap-cell object tag-number - ;
: word-xt-offset 8 bootstrap-cells object tag-number - ; : word-xt-offset 8 bootstrap-cells object tag-number - ;
: word-code-offset 9 bootstrap-cells object tag-number - ; : word-code-offset 9 bootstrap-cells object tag-number - ;
: compiled-header-size 8 bootstrap-cells ; : compiled-header-size 4 bootstrap-cells ;

View File

@ -44,7 +44,7 @@ words kernel math effects definitions compiler.units ;
[ [
[ ] [ init-templates ] unit-test [ ] [ init-templates ] unit-test
[ ] [ \ + init-generator ] unit-test [ ] [ init-generator ] unit-test
[ t ] [ [ end-basic-block ] { } make empty? ] unit-test [ t ] [ [ end-basic-block ] { } make empty? ] unit-test

View File

@ -72,8 +72,6 @@ HOOK: %jump-dispatch compiler-backend ( -- )
HOOK: %dispatch-label compiler-backend ( word -- ) HOOK: %dispatch-label compiler-backend ( word -- )
HOOK: %end-dispatch compiler-backend ( label -- )
! Return to caller ! Return to caller
HOOK: %return compiler-backend ( -- ) HOOK: %return compiler-backend ( -- )

View File

@ -144,9 +144,6 @@ M: ppc-backend %jump-dispatch ( -- )
M: ppc-backend %dispatch-label ( word -- ) M: ppc-backend %dispatch-label ( word -- )
0 , rc-absolute-cell rel-word ; 0 , rc-absolute-cell rel-word ;
M: ppc-backend %end-dispatch ( label -- )
resolve-label ;
M: ppc-backend %return ( -- ) %epilogue-later BLR ; M: ppc-backend %return ( -- ) %epilogue-later BLR ;
M: ppc-backend %unwind drop %return ; M: ppc-backend %unwind drop %return ;

View File

@ -13,13 +13,6 @@ HELP: add-literal
{ $values { "obj" object } { "n" integer } } { $values { "obj" object } { "n" integer } }
{ $description "Adds a literal to the " { $link literal-table } ", if it is not already there, and outputs the index of the literal in the table. This literal can then be used as an argument for a " { $link rt-literal } " relocation with " { $link rel-fixup } "." } ; { $description "Adds a literal to the " { $link literal-table } ", if it is not already there, and outputs the index of the literal in the table. This literal can then be used as an argument for a " { $link rt-literal } " relocation with " { $link rel-fixup } "." } ;
HELP: word-table
{ $var-description "Holds a vector of words called from the currently compiling word." } ;
HELP: add-word
{ $values { "word" word } { "n" integer } }
{ $description "Adds a word to the " { $link word-table } ", if it is not already there, and outputs the index of the word in the table. This literal can then be used as an argument for a " { $link rt-xt } " relocation with " { $link rel-fixup } "." } ;
HELP: string>symbol HELP: string>symbol
{ $values { "str" string } { "alien" alien } } { $values { "str" string } { "alien" alien } }
{ $description "Converts the string to a format which is a valid symbol name for the Factor VM's compiled code linker. By performing this conversion ahead of time, the image loader can run without allocating memory." { $description "Converts the string to a format which is a valid symbol name for the Factor VM's compiled code linker. By performing this conversion ahead of time, the image loader can run without allocating memory."

View File

@ -1,4 +1,4 @@
! Copyright (C) 2007 Slava Pestov. ! Copyright (C) 2007, 2008 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: arrays generic assocs hashtables USING: arrays generic assocs hashtables
kernel kernel.private math namespaces sequences words kernel kernel.private math namespaces sequences words
@ -110,10 +110,6 @@ SYMBOL: literal-table
: add-literal ( obj -- n ) literal-table get push-new* ; : add-literal ( obj -- n ) literal-table get push-new* ;
SYMBOL: word-table
: add-word ( word -- n ) word-table get push-new* ;
: string>symbol ( str -- alien ) : string>symbol ( str -- alien )
wince? [ string>u16-alien ] [ string>char-alien ] if ; wince? [ string>u16-alien ] [ string>char-alien ] if ;
@ -125,10 +121,8 @@ SYMBOL: word-table
add-dlsym-literals add-dlsym-literals
r> r> rt-dlsym rel-fixup ; r> r> rt-dlsym rel-fixup ;
: rel-dispatch ( word-table# class -- ) rt-dispatch rel-fixup ;
: rel-word ( word class -- ) : rel-word ( word class -- )
>r add-word r> rt-xt rel-fixup ; >r add-literal r> rt-xt rel-fixup ;
: rel-primitive ( word class -- ) : rel-primitive ( word class -- )
>r word-def first r> rt-primitive rel-fixup ; >r word-def first r> rt-primitive rel-fixup ;

View File

@ -1,22 +1,20 @@
! Copyright (C) 2004, 2007 Slava Pestov. ! Copyright (C) 2004, 2008 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: arrays assocs classes combinators cpu.architecture USING: arrays assocs classes combinators cpu.architecture
effects generator.fixup generator.registers generic hashtables effects generator.fixup generator.registers generic hashtables
inference inference.backend inference.dataflow io kernel inference inference.backend inference.dataflow io kernel
kernel.private layouts math namespaces optimizer prettyprint kernel.private layouts math namespaces optimizer prettyprint
quotations sequences system threads words ; quotations sequences system threads words vectors ;
IN: generator IN: generator
SYMBOL: compile-queue SYMBOL: compile-queue
SYMBOL: compiled SYMBOL: compiled
: 5array 3array >r 2array r> append ;
: begin-compiling ( word -- ) : begin-compiling ( word -- )
f swap compiled get set-at ; f swap compiled get set-at ;
: finish-compiling ( word literals words relocation labels code -- ) : finish-compiling ( word literals relocation labels code -- )
5array swap compiled get set-at ; 4array swap compiled get set-at ;
: queue-compile ( word -- ) : queue-compile ( word -- )
{ {
@ -38,20 +36,18 @@ SYMBOL: current-label-start
: compiled-stack-traces? ( -- ? ) 36 getenv ; : compiled-stack-traces? ( -- ? ) 36 getenv ;
: init-generator ( compiling -- ) : init-generator ( -- )
V{ } clone literal-table set compiled-stack-traces?
V{ } clone word-table set compiling-word get f ?
compiled-stack-traces? swap f ? 1vector literal-table set ;
literal-table get push ;
: generate-1 ( word label node quot -- ) : generate-1 ( word label node quot -- )
pick begin-compiling [ pick begin-compiling [
roll compiling-word set roll compiling-word set
pick compiling-label set pick compiling-label set
compiling-word get init-generator init-generator
call call
literal-table get >array literal-table get >array
word-table get >array
] { } make fixup finish-compiling ; ] { } make fixup finish-compiling ;
GENERIC: generate-node ( node -- next ) GENERIC: generate-node ( node -- next )
@ -182,7 +178,7 @@ M: #dispatch generate-node
%jump-dispatch dispatch-branches %jump-dispatch dispatch-branches
] [ ] [
0 frame-required 0 frame-required
%call-dispatch >r dispatch-branches r> %end-dispatch %call-dispatch >r dispatch-branches r> resolve-label
] if ] if
init-templates iterate-next ; init-templates iterate-next ;

View File

@ -245,17 +245,13 @@ void iterate_code_heap(CODE_HEAP_ITERATOR iter)
/* Copy all literals referenced from a code block to newspace */ /* Copy all literals referenced from a code block to newspace */
void collect_literals_step(F_COMPILED *compiled, CELL code_start, void collect_literals_step(F_COMPILED *compiled, CELL code_start,
CELL reloc_start, CELL literals_start, CELL words_start, CELL words_end) CELL reloc_start, CELL literals_start)
{ {
CELL scan; CELL scan;
CELL literal_end = literals_start + compiled->literals_length; CELL literal_end = literals_start + compiled->literals_length;
for(scan = literals_start; scan < literal_end; scan += CELLS) for(scan = literals_start; scan < literal_end; scan += CELLS)
copy_handle((CELL*)scan); copy_handle((CELL*)scan);
for(scan = words_start; scan < words_end; scan += CELLS)
copy_handle((CELL*)scan);
} }
/* Copy literals referenced from all code blocks to newspace */ /* Copy literals referenced from all code blocks to newspace */

View File

@ -48,17 +48,15 @@ INLINE F_BLOCK *next_block(F_HEAP *heap, F_BLOCK *block)
F_HEAP code_heap; F_HEAP code_heap;
typedef void (*CODE_HEAP_ITERATOR)(F_COMPILED *compiled, CELL code_start, typedef void (*CODE_HEAP_ITERATOR)(F_COMPILED *compiled, CELL code_start,
CELL reloc_start, CELL literals_start, CELL words_start, CELL words_end); CELL reloc_start, CELL literals_start);
INLINE void iterate_code_heap_step(F_COMPILED *compiled, CODE_HEAP_ITERATOR iter) INLINE void iterate_code_heap_step(F_COMPILED *compiled, CODE_HEAP_ITERATOR iter)
{ {
CELL code_start = (CELL)(compiled + 1); CELL code_start = (CELL)(compiled + 1);
CELL reloc_start = code_start + compiled->code_length; CELL reloc_start = code_start + compiled->code_length;
CELL literals_start = reloc_start + compiled->reloc_length; CELL literals_start = reloc_start + compiled->reloc_length;
CELL words_start = literals_start + compiled->literals_length;
CELL words_end = words_start + compiled->words_length;
iter(compiled,code_start,reloc_start,literals_start,words_start,words_end); iter(compiled,code_start,reloc_start,literals_start);
} }
INLINE F_BLOCK *compiled_to_block(F_COMPILED *compiled) INLINE F_BLOCK *compiled_to_block(F_COMPILED *compiled)

View File

@ -38,7 +38,7 @@ void *get_rel_symbol(F_REL *rel, CELL literals_start)
/* Compute an address to store at a relocation */ /* Compute an address to store at a relocation */
INLINE CELL compute_code_rel(F_REL *rel, INLINE CELL compute_code_rel(F_REL *rel,
CELL code_start, CELL literals_start, CELL words_start) CELL code_start, CELL literals_start)
{ {
switch(REL_TYPE(rel)) switch(REL_TYPE(rel))
{ {
@ -48,10 +48,8 @@ INLINE CELL compute_code_rel(F_REL *rel,
return (CELL)get_rel_symbol(rel,literals_start); return (CELL)get_rel_symbol(rel,literals_start);
case RT_LITERAL: case RT_LITERAL:
return CREF(literals_start,REL_ARGUMENT(rel)); return CREF(literals_start,REL_ARGUMENT(rel));
case RT_DISPATCH:
return CREF(words_start,REL_ARGUMENT(rel));
case RT_XT: case RT_XT:
return (CELL)untag_word(get(CREF(words_start,REL_ARGUMENT(rel))))->xt; return (CELL)untag_word(get(CREF(literals_start,REL_ARGUMENT(rel))))->xt;
case RT_HERE: case RT_HERE:
return rel->offset + code_start; return rel->offset + code_start;
case RT_LABEL: case RT_LABEL:
@ -127,7 +125,7 @@ void apply_relocation(CELL class, CELL offset, F_FIXNUM absolute_value)
/* Perform all fixups on a code block */ /* Perform all fixups on a code block */
void relocate_code_block(F_COMPILED *relocating, CELL code_start, void relocate_code_block(F_COMPILED *relocating, CELL code_start,
CELL reloc_start, CELL literals_start, CELL words_start, CELL words_end) CELL reloc_start, CELL literals_start)
{ {
if(reloc_start != literals_start) if(reloc_start != literals_start)
{ {
@ -138,8 +136,8 @@ void relocate_code_block(F_COMPILED *relocating, CELL code_start,
{ {
CELL offset = rel->offset + code_start; CELL offset = rel->offset + code_start;
F_FIXNUM absolute_value = compute_code_rel(rel, F_FIXNUM absolute_value = compute_code_rel(
code_start,literals_start,words_start); rel,code_start,literals_start);
apply_relocation(REL_CLASS(rel),offset,absolute_value); apply_relocation(REL_CLASS(rel),offset,absolute_value);
@ -228,27 +226,23 @@ F_COMPILED *add_compiled_block(
F_ARRAY *code, F_ARRAY *code,
F_ARRAY *labels, F_ARRAY *labels,
F_ARRAY *relocation, F_ARRAY *relocation,
F_ARRAY *words,
F_ARRAY *literals) F_ARRAY *literals)
{ {
CELL code_format = compiled_code_format(); CELL code_format = compiled_code_format();
CELL code_length = align8(array_capacity(code) * code_format); CELL code_length = align8(array_capacity(code) * code_format);
CELL rel_length = array_capacity(relocation) * sizeof(unsigned int); CELL rel_length = array_capacity(relocation) * sizeof(unsigned int);
CELL words_length = (words ? array_capacity(words) * CELLS : 0);
CELL literals_length = array_capacity(literals) * CELLS; CELL literals_length = array_capacity(literals) * CELLS;
REGISTER_UNTAGGED(code); REGISTER_UNTAGGED(code);
REGISTER_UNTAGGED(labels); REGISTER_UNTAGGED(labels);
REGISTER_UNTAGGED(relocation); REGISTER_UNTAGGED(relocation);
REGISTER_UNTAGGED(words);
REGISTER_UNTAGGED(literals); REGISTER_UNTAGGED(literals);
CELL here = allot_code_block(sizeof(F_COMPILED) + code_length CELL here = allot_code_block(sizeof(F_COMPILED) + code_length
+ rel_length + literals_length + words_length); + rel_length + literals_length);
UNREGISTER_UNTAGGED(literals); UNREGISTER_UNTAGGED(literals);
UNREGISTER_UNTAGGED(words);
UNREGISTER_UNTAGGED(relocation); UNREGISTER_UNTAGGED(relocation);
UNREGISTER_UNTAGGED(labels); UNREGISTER_UNTAGGED(labels);
UNREGISTER_UNTAGGED(code); UNREGISTER_UNTAGGED(code);
@ -259,7 +253,6 @@ F_COMPILED *add_compiled_block(
header->code_length = code_length; header->code_length = code_length;
header->reloc_length = rel_length; header->reloc_length = rel_length;
header->literals_length = literals_length; header->literals_length = literals_length;
header->words_length = words_length;
here += sizeof(F_COMPILED); here += sizeof(F_COMPILED);
@ -277,13 +270,6 @@ F_COMPILED *add_compiled_block(
deposit_objects(here,literals); deposit_objects(here,literals);
here += literals_length; here += literals_length;
/* words */
if(words)
{
deposit_objects(here,words);
here += words_length;
}
/* fixup labels */ /* fixup labels */
if(labels) if(labels)
fixup_labels(labels,code_format,code_start); fixup_labels(labels,code_format,code_start);
@ -347,10 +333,9 @@ DEFINE_PRIMITIVE(modify_code_heap)
F_ARRAY *compiled_code = untag_array(data); F_ARRAY *compiled_code = untag_array(data);
F_ARRAY *literals = untag_array(array_nth(compiled_code,0)); F_ARRAY *literals = untag_array(array_nth(compiled_code,0));
F_ARRAY *words = untag_array(array_nth(compiled_code,1)); F_ARRAY *relocation = untag_array(array_nth(compiled_code,1));
F_ARRAY *relocation = untag_array(array_nth(compiled_code,2)); F_ARRAY *labels = untag_array(array_nth(compiled_code,2));
F_ARRAY *labels = untag_array(array_nth(compiled_code,3)); F_ARRAY *code = untag_array(array_nth(compiled_code,3));
F_ARRAY *code = untag_array(array_nth(compiled_code,4));
REGISTER_UNTAGGED(alist); REGISTER_UNTAGGED(alist);
REGISTER_UNTAGGED(word); REGISTER_UNTAGGED(word);
@ -360,7 +345,6 @@ DEFINE_PRIMITIVE(modify_code_heap)
code, code,
labels, labels,
relocation, relocation,
words,
literals); literals);
UNREGISTER_UNTAGGED(word); UNREGISTER_UNTAGGED(word);

View File

@ -54,7 +54,7 @@ typedef struct {
} F_REL; } F_REL;
void relocate_code_block(F_COMPILED *relocating, CELL code_start, void relocate_code_block(F_COMPILED *relocating, CELL code_start,
CELL reloc_start, CELL literals_start, CELL words_start, CELL words_end); CELL reloc_start, CELL literals_start);
void default_word_code(F_WORD *word, bool relocate); void default_word_code(F_WORD *word, bool relocate);
@ -65,7 +65,6 @@ F_COMPILED *add_compiled_block(
F_ARRAY *code, F_ARRAY *code,
F_ARRAY *labels, F_ARRAY *labels,
F_ARRAY *rel, F_ARRAY *rel,
F_ARRAY *words,
F_ARRAY *literals); F_ARRAY *literals);
CELL compiled_code_format(void); CELL compiled_code_format(void);

View File

@ -179,7 +179,7 @@ void fixup_word(F_WORD *word)
{ {
code_fixup((CELL)&word->code); code_fixup((CELL)&word->code);
if(word->profiling) code_fixup((CELL)&word->profiling); if(word->profiling) code_fixup((CELL)&word->profiling);
update_word_xt(word); code_fixup((CELL)&word->xt);
} }
} }
@ -262,7 +262,7 @@ void relocate_data()
} }
void fixup_code_block(F_COMPILED *relocating, CELL code_start, void fixup_code_block(F_COMPILED *relocating, CELL code_start,
CELL reloc_start, CELL literals_start, CELL words_start, CELL words_end) CELL reloc_start, CELL literals_start)
{ {
/* relocate literal table data */ /* relocate literal table data */
CELL scan; CELL scan;
@ -271,14 +271,8 @@ void fixup_code_block(F_COMPILED *relocating, CELL code_start,
for(scan = literals_start; scan < literal_end; scan += CELLS) for(scan = literals_start; scan < literal_end; scan += CELLS)
data_fixup((CELL*)scan); data_fixup((CELL*)scan);
for(scan = words_start; scan < words_end; scan += CELLS)
data_fixup((CELL*)scan);
if(reloc_start != literals_start) if(reloc_start != literals_start)
{ relocate_code_block(relocating,code_start,reloc_start,literals_start);
relocate_code_block(relocating,code_start,reloc_start,
literals_start,words_start,words_end);
}
} }
void relocate_code() void relocate_code()

View File

@ -151,8 +151,6 @@ typedef struct
CELL code_length; /* # bytes */ CELL code_length; /* # bytes */
CELL reloc_length; /* # bytes */ CELL reloc_length; /* # bytes */
CELL literals_length; /* # bytes */ CELL literals_length; /* # bytes */
CELL words_length; /* # bytes */
CELL padding[3];
} F_COMPILED; } F_COMPILED;
/* Assembly code makes assumptions about the layout of this struct */ /* Assembly code makes assumptions about the layout of this struct */

View File

@ -25,7 +25,6 @@ F_COMPILED *compile_profiling_stub(F_WORD *word)
untag_object(code), untag_object(code),
NULL, /* no labels */ NULL, /* no labels */
untag_object(relocation), untag_object(relocation),
NULL, /* no words */
untag_object(literals)); untag_object(literals));
} }

View File

@ -116,9 +116,6 @@ void jit_compile(CELL quot, bool relocate)
GROWABLE_ARRAY(literals); GROWABLE_ARRAY(literals);
REGISTER_ROOT(literals); REGISTER_ROOT(literals);
GROWABLE_ARRAY(words);
REGISTER_ROOT(words);
GROWABLE_ADD(literals,stack_traces_p() ? quot : F); GROWABLE_ADD(literals,stack_traces_p() ? quot : F);
bool stack_frame = jit_stack_frame_p(untag_object(array)); bool stack_frame = jit_stack_frame_p(untag_object(array));
@ -144,19 +141,19 @@ void jit_compile(CELL quot, bool relocate)
current stack frame. */ current stack frame. */
word = untag_object(obj); word = untag_object(obj);
GROWABLE_ADD(words,array_nth(untag_object(array),i)); GROWABLE_ADD(literals,array_nth(untag_object(array),i));
if(i == length - 1) if(i == length - 1)
{ {
if(stack_frame) if(stack_frame)
EMIT(JIT_EPILOG,0); EMIT(JIT_EPILOG,0);
EMIT(JIT_WORD_JUMP,words_count - 1); EMIT(JIT_WORD_JUMP,literals_count - 1);
tail_call = true; tail_call = true;
} }
else else
EMIT(JIT_WORD_CALL,words_count - 1); EMIT(JIT_WORD_CALL,literals_count - 1);
break; break;
case WRAPPER_TYPE: case WRAPPER_TYPE:
wrapper = untag_object(obj); wrapper = untag_object(obj);
@ -220,14 +217,12 @@ void jit_compile(CELL quot, bool relocate)
GROWABLE_TRIM(code); GROWABLE_TRIM(code);
GROWABLE_TRIM(relocation); GROWABLE_TRIM(relocation);
GROWABLE_TRIM(literals); GROWABLE_TRIM(literals);
GROWABLE_TRIM(words);
F_COMPILED *compiled = add_compiled_block( F_COMPILED *compiled = add_compiled_block(
QUOTATION_TYPE, QUOTATION_TYPE,
untag_object(code), untag_object(code),
NULL, NULL,
untag_object(relocation), untag_object(relocation),
untag_object(words),
untag_object(literals)); untag_object(literals));
set_quot_xt(untag_object(quot),compiled); set_quot_xt(untag_object(quot),compiled);
@ -235,7 +230,6 @@ void jit_compile(CELL quot, bool relocate)
if(relocate) if(relocate)
iterate_code_heap_step(compiled,relocate_code_block); iterate_code_heap_step(compiled,relocate_code_block);
UNREGISTER_ROOT(words);
UNREGISTER_ROOT(literals); UNREGISTER_ROOT(literals);
UNREGISTER_ROOT(relocation); UNREGISTER_ROOT(relocation);
UNREGISTER_ROOT(code); UNREGISTER_ROOT(code);