factor/vm/code_heap.cpp

234 lines
5.1 KiB
C++
Raw Normal View History

2009-05-02 05:04:19 -04:00
#include "master.hpp"
2009-05-04 02:46:13 -04:00
namespace factor
{
code_heap::code_heap(cell size)
{
if(size > (1L << (sizeof(cell) * 8 - 6))) fatal_error("Heap too large",size);
seg = new segment(align_page(size),true);
if(!seg) fatal_error("Out of memory in heap allocator",size);
allocator = new free_list_allocator<code_block>(size,seg->start);
}
code_heap::~code_heap()
{
delete allocator;
allocator = NULL;
delete seg;
seg = NULL;
}
2009-10-06 02:42:17 -04:00
void code_heap::write_barrier(code_block *compiled)
{
points_to_nursery.insert(compiled);
points_to_aging.insert(compiled);
}
2009-10-16 03:55:02 -04:00
void code_heap::clear_remembered_set()
{
points_to_nursery.clear();
points_to_aging.clear();
}
bool code_heap::needs_fixup_p(code_block *compiled)
{
return needs_fixup.count(compiled) > 0;
}
bool code_heap::marked_p(code_block *compiled)
{
return allocator->state.marked_p(compiled);
}
void code_heap::set_marked_p(code_block *compiled)
{
allocator->state.set_marked_p(compiled);
}
void code_heap::clear_mark_bits()
{
allocator->state.clear_mark_bits();
}
void code_heap::code_heap_free(code_block *compiled)
{
points_to_nursery.erase(compiled);
points_to_aging.erase(compiled);
needs_fixup.erase(compiled);
allocator->free(compiled);
}
2009-05-02 05:04:19 -04:00
/* Allocate a code heap during startup */
2009-09-23 14:05:46 -04:00
void factor_vm::init_code_heap(cell size)
2009-05-02 05:04:19 -04:00
{
code = new code_heap(size);
2009-05-02 05:04:19 -04:00
}
2009-09-23 14:05:46 -04:00
bool factor_vm::in_code_heap_p(cell ptr)
2009-05-02 05:04:19 -04:00
{
return (ptr >= code->seg->start && ptr <= code->seg->end);
2009-05-02 05:04:19 -04:00
}
/* Compile a word definition with the non-optimizing compiler. Allocates memory */
2009-09-23 14:05:46 -04:00
void factor_vm::jit_compile_word(cell word_, cell def_, bool relocate)
2009-05-02 05:04:19 -04:00
{
data_root<word> word(word_,this);
data_root<quotation> def(def_,this);
2009-05-02 05:04:19 -04:00
2009-05-02 10:19:09 -04:00
jit_compile(def.value(),relocate);
word->code = def->code;
2009-05-02 05:04:19 -04:00
if(to_boolean(word->pic_def)) jit_compile(word->pic_def,relocate);
if(to_boolean(word->pic_tail_def)) jit_compile(word->pic_tail_def,relocate);
2009-05-02 05:04:19 -04:00
}
2009-10-06 02:42:17 -04:00
struct word_updater {
factor_vm *parent;
2009-10-06 02:42:17 -04:00
explicit word_updater(factor_vm *parent_) : parent(parent_) {}
void operator()(code_block *compiled, cell size)
2009-10-06 02:42:17 -04:00
{
parent->update_word_references(compiled);
2009-10-06 02:42:17 -04:00
}
};
2009-05-02 05:04:19 -04:00
/* Update pointers to words referenced from all code blocks. Only after
defining a new word. */
2009-09-23 14:05:46 -04:00
void factor_vm::update_code_heap_words()
2009-08-17 16:37:08 -04:00
{
word_updater updater(this);
iterate_code_heap(updater);
2009-08-17 16:37:08 -04:00
}
/* After a full GC that did not grow the heap, we have to update references
to literals and other words. */
struct word_and_literal_code_heap_updater {
factor_vm *parent;
explicit word_and_literal_code_heap_updater(factor_vm *parent_) : parent(parent_) {}
void operator()(code_block *block, cell size)
{
parent->update_code_block_words_and_literals(block);
}
};
void factor_vm::update_code_heap_words_and_literals()
{
current_gc->event->started_code_sweep();
word_and_literal_code_heap_updater updater(this);
code->allocator->sweep(updater);
current_gc->event->ended_code_sweep();
}
/* After growing the heap, we have to perform a full relocation to update
references to card and deck arrays. */
struct code_heap_relocator {
factor_vm *parent;
explicit code_heap_relocator(factor_vm *parent_) : parent(parent_) {}
void operator()(code_block *block, cell size)
{
parent->relocate_code_block(block);
}
};
void factor_vm::relocate_code_heap()
{
code_heap_relocator relocator(this);
code->allocator->sweep(relocator);
}
void factor_vm::primitive_modify_code_heap()
2009-05-02 05:04:19 -04:00
{
data_root<array> alist(dpop(),this);
2009-05-02 10:19:09 -04:00
2009-05-04 05:50:24 -04:00
cell count = array_capacity(alist.untagged());
2009-05-02 05:04:19 -04:00
if(count == 0)
return;
2009-05-04 05:50:24 -04:00
cell i;
2009-05-02 05:04:19 -04:00
for(i = 0; i < count; i++)
{
data_root<array> pair(array_nth(alist.untagged(),i),this);
2009-05-02 05:04:19 -04:00
data_root<word> word(array_nth(pair.untagged(),0),this);
data_root<object> data(array_nth(pair.untagged(),1),this);
2009-05-02 05:04:19 -04:00
2009-05-02 10:19:09 -04:00
switch(data.type())
2009-05-02 05:04:19 -04:00
{
2009-05-02 10:19:09 -04:00
case QUOTATION_TYPE:
jit_compile_word(word.value(),data.value(),false);
break;
case ARRAY_TYPE:
{
array *compiled_data = data.as<array>().untagged();
cell owner = array_nth(compiled_data,0);
cell literals = array_nth(compiled_data,1);
cell relocation = array_nth(compiled_data,2);
cell labels = array_nth(compiled_data,3);
cell code = array_nth(compiled_data,4);
code_block *compiled = add_code_block(
code_block_optimized,
code,
labels,
owner,
relocation,
literals);
word->code = compiled;
}
2009-05-02 10:19:09 -04:00
break;
default:
critical_error("Expected a quotation or an array",data.value());
break;
2009-05-02 05:04:19 -04:00
}
update_word_xt(word.untagged());
2009-05-02 05:04:19 -04:00
}
update_code_heap_words();
}
code_heap_room factor_vm::code_room()
2009-05-02 05:04:19 -04:00
{
code_heap_room room;
room.size = code->allocator->size;
room.occupied_space = code->allocator->occupied_space();
room.total_free = code->allocator->free_space();
room.contiguous_free = code->allocator->largest_free_block();
room.free_block_count = code->allocator->free_block_count();
return room;
}
void factor_vm::primitive_code_room()
{
code_heap_room room = code_room();
dpush(tag<byte_array>(byte_array_from_value(&room)));
2009-05-02 05:04:19 -04:00
}
struct stack_trace_stripper {
explicit stack_trace_stripper() {}
void operator()(code_block *compiled, cell size)
{
compiled->owner = false_object;
}
};
void factor_vm::primitive_strip_stack_traces()
{
stack_trace_stripper stripper;
iterate_code_heap(stripper);
}
2009-05-04 02:46:13 -04:00
}