namespace factor
{
-code_heap::code_heap(bool secure_gc, cell size) :
- heap<heap_block,code_heap_layout>(secure_gc,size,true) {}
+code_heap::code_heap(cell size)
+{
+ if(size > ((u64)1 << (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 code_heap constructor",size);
+
+ cell start = seg->start + getpagesize() + seh_area_size;
+
+ allocator = new free_list_allocator<code_block>(seg->end - start,start);
+
+ /* See os-windows-x86.64.cpp for seh_area usage */
+ safepoint_page = (void *)seg->start;
+ seh_area = (char *)seg->start + getpagesize();
+}
+
+code_heap::~code_heap()
+{
+ delete allocator;
+ allocator = NULL;
+ delete seg;
+ seg = NULL;
+}
void code_heap::write_barrier(code_block *compiled)
{
points_to_aging.clear();
}
-bool code_heap::needs_fixup_p(code_block *compiled)
+bool code_heap::uninitialized_p(code_block *compiled)
{
- return needs_fixup.count(compiled) > 0;
+ return uninitialized_blocks.count(compiled) > 0;
}
-void code_heap::code_heap_free(code_block *compiled)
+bool code_heap::marked_p(code_block *compiled)
{
- points_to_nursery.erase(compiled);
- points_to_aging.erase(compiled);
- needs_fixup.erase(compiled);
- heap_free(compiled);
+ return allocator->state.marked_p(compiled);
}
-/* Allocate a code heap during startup */
-void factor_vm::init_code_heap(cell size)
+void code_heap::set_marked_p(code_block *compiled)
{
- code = new code_heap(secure_gc,size);
+ allocator->state.set_marked_p(compiled);
}
-bool factor_vm::in_code_heap_p(cell ptr)
+void code_heap::clear_mark_bits()
{
- return (ptr >= code->seg->start && ptr <= code->seg->end);
+ allocator->state.clear_mark_bits();
}
-/* Compile a word definition with the non-optimizing compiler. Allocates memory */
-void factor_vm::jit_compile_word(cell word_, cell def_, bool relocate)
+void code_heap::free(code_block *compiled)
{
- gc_root<word> word(word_,this);
- gc_root<quotation> def(def_,this);
-
- jit_compile(def.value(),relocate);
-
- word->code = def->code;
+ assert(!uninitialized_p(compiled));
+ points_to_nursery.erase(compiled);
+ points_to_aging.erase(compiled);
+ allocator->free(compiled);
+}
- 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);
+void code_heap::flush_icache()
+{
+ factor::flush_icache(seg->start,seg->size);
}
-struct word_updater {
- factor_vm *parent;
+struct address_finder {
+ cell address;
+ code_block *found_code_block;
- explicit word_updater(factor_vm *parent_) : parent(parent_) {}
- void operator()(code_block *compiled, cell size)
+ address_finder(cell address)
+ : address(address), found_code_block(NULL) {}
+
+ void operator()(code_block *block, cell size)
{
- parent->update_word_references(compiled);
+ if ((cell)block->entry_point() <= address
+ && address - (cell)block->entry_point() < block->size())
+ {
+ assert(found_code_block == NULL);
+ found_code_block = block;
+ }
}
};
-/* Update pointers to words referenced from all code blocks. Only after
-defining a new word. */
-void factor_vm::update_code_heap_words()
+code_block *code_heap::code_block_for_address(cell address)
{
- word_updater updater(this);
- iterate_code_heap(updater);
+ address_finder finder(address);
+ allocator->iterate(finder);
+ return finder.found_code_block;
}
-/* 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 {
+/* Allocate a code heap during startup */
+void factor_vm::init_code_heap(cell size)
+{
+ code = new code_heap(size);
+}
+
+struct word_updater {
factor_vm *parent;
+ bool reset_inline_caches;
- word_and_literal_code_heap_updater(factor_vm *parent_) : parent(parent_) {}
+ word_updater(factor_vm *parent_, bool reset_inline_caches_) :
+ parent(parent_), reset_inline_caches(reset_inline_caches_) {}
- void operator()(heap_block *block, cell size)
+ void operator()(code_block *compiled, cell size)
{
- parent->update_code_block_words_and_literals((code_block *)block);
+ parent->update_word_references(compiled,reset_inline_caches);
}
};
-void factor_vm::update_code_heap_words_and_literals()
+/* Update pointers to words referenced from all code blocks.
+Only needed after redefining an existing word.
+If generic words were redefined, inline caches need to be reset. */
+void factor_vm::update_code_heap_words(bool reset_inline_caches)
{
- word_and_literal_code_heap_updater updater(this);
- code->sweep_heap(updater);
+ word_updater updater(this,reset_inline_caches);
+ each_code_block(updater);
}
-/* 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;
+/* Fix up new words only.
+Fast path for compilation units that only define new words. */
+void factor_vm::initialize_code_blocks()
+{
+ std::map<code_block *, cell>::const_iterator iter = code->uninitialized_blocks.begin();
+ std::map<code_block *, cell>::const_iterator end = code->uninitialized_blocks.end();
- code_heap_relocator(factor_vm *parent_) : parent(parent_) {}
+ for(; iter != end; iter++)
+ initialize_code_block(iter->first,iter->second);
- void operator()(code_block *block, cell size)
- {
- parent->relocate_code_block(block);
- }
-};
-
-void factor_vm::relocate_code_heap()
-{
- code_heap_relocator relocator(this);
- code_heap_iterator<code_heap_relocator> iter(relocator);
- code->sweep_heap(iter);
+ code->uninitialized_blocks.clear();
}
void factor_vm::primitive_modify_code_heap()
{
- gc_root<array> alist(dpop(),this);
+ bool reset_inline_caches = to_boolean(ctx->pop());
+ bool update_existing_words = to_boolean(ctx->pop());
+ data_root<array> alist(ctx->pop(),this);
cell count = array_capacity(alist.untagged());
if(count == 0)
return;
- cell i;
- for(i = 0; i < count; i++)
+ for(cell i = 0; i < count; i++)
{
- gc_root<array> pair(array_nth(alist.untagged(),i),this);
+ data_root<array> pair(array_nth(alist.untagged(),i),this);
- gc_root<word> word(array_nth(pair.untagged(),0),this);
- gc_root<object> data(array_nth(pair.untagged(),1),this);
+ data_root<word> word(array_nth(pair.untagged(),0),this);
+ data_root<object> data(array_nth(pair.untagged(),1),this);
switch(data.type())
{
case ARRAY_TYPE:
{
array *compiled_data = data.as<array>().untagged();
- cell owner = array_nth(compiled_data,0);
+ cell parameters = 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);
code_block_optimized,
code,
labels,
- owner,
+ word.value(),
relocation,
+ parameters,
literals);
- word->code = compiled;
+ word->entry_point = compiled->entry_point();
}
break;
default:
critical_error("Expected a quotation or an array",data.value());
break;
}
-
- update_word_xt(word.value());
}
- update_code_heap_words();
-}
-
-/* Push the free space and total size of the code heap */
-void factor_vm::primitive_code_room()
-{
- cell used, total_free, max_free;
- code->heap_usage(&used,&total_free,&max_free);
- dpush(tag_fixnum(code->seg->size / 1024));
- dpush(tag_fixnum(used / 1024));
- dpush(tag_fixnum(total_free / 1024));
- dpush(tag_fixnum(max_free / 1024));
-}
-
-code_block *code_heap::forward_code_block(code_block *compiled)
-{
- return (code_block *)state->forward_block(compiled);
+ if(update_existing_words)
+ update_code_heap_words(reset_inline_caches);
+ else
+ initialize_code_blocks();
}
-struct callframe_forwarder {
- factor_vm *parent;
-
- explicit callframe_forwarder(factor_vm *parent_) : parent(parent_) {}
-
- void operator()(stack_frame *frame)
- {
- cell offset = (cell)FRAME_RETURN_ADDRESS(frame,parent) - (cell)frame->xt;
-
- code_block *forwarded = parent->code->forward_code_block(parent->frame_code(frame));
- frame->xt = forwarded->xt();
-
- FRAME_RETURN_ADDRESS(frame,parent) = (void *)((cell)frame->xt + offset);
- }
-};
-
-void factor_vm::forward_object_xts()
+code_heap_room factor_vm::code_room()
{
- begin_scan();
-
- cell obj;
-
- while(to_boolean(obj = next_object()))
- {
- switch(tagged<object>(obj).type())
- {
- case WORD_TYPE:
- {
- word *w = untag<word>(obj);
-
- if(w->code)
- w->code = code->forward_code_block(w->code);
- if(w->profiling)
- w->profiling = code->forward_code_block(w->profiling);
+ code_heap_room room;
- update_word_xt(obj);
- }
- break;
- case QUOTATION_TYPE:
- {
- quotation *quot = untag<quotation>(obj);
-
- if(quot->code)
- {
- quot->code = code->forward_code_block(quot->code);
- set_quot_xt(quot,quot->code);
- }
- }
- break;
- case CALLSTACK_TYPE:
- {
- callstack *stack = untag<callstack>(obj);
- callframe_forwarder forwarder(this);
- iterate_callstack_object(stack,forwarder);
- }
- break;
- default:
- break;
- }
- }
+ 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();
- end_scan();
+ return room;
}
-void factor_vm::forward_context_xts()
+void factor_vm::primitive_code_room()
{
- callframe_forwarder forwarder(this);
- iterate_active_frames(forwarder);
+ code_heap_room room = code_room();
+ ctx->push(tag<byte_array>(byte_array_from_value(&room)));
}
-struct callback_forwarder {
- code_heap *code;
- callback_heap *callbacks;
-
- callback_forwarder(code_heap *code_, callback_heap *callbacks_) :
- code(code_), callbacks(callbacks_) {}
+struct stack_trace_stripper {
+ explicit stack_trace_stripper() {}
- void operator()(callback *stub)
+ void operator()(code_block *compiled, cell size)
{
- stub->compiled = code->forward_code_block(stub->compiled);
- callbacks->update(stub);
+ compiled->owner = false_object;
}
};
-void factor_vm::forward_callback_xts()
-{
- callback_forwarder forwarder(code,callbacks);
- callbacks->iterate(forwarder);
-}
-
-/* Move all free space to the end of the code heap. Live blocks must be marked
-on entry to this function. XTs in code blocks must be updated after this
-function returns. */
-void factor_vm::compact_code_heap(bool trace_contexts_p)
+void factor_vm::primitive_strip_stack_traces()
{
- /* Figure out where blocks are going to go */
- code->state->compute_forwarding();
-
- /* Update references to the code heap from the data heap */
- forward_object_xts();
- if(trace_contexts_p)
- {
- forward_context_xts();
- forward_callback_xts();
- }
-
- /* Move code blocks and update references amongst them (this requires
- that the data heap is up to date since relocation looks up object XTs) */
- code_heap_relocator relocator(this);
- code_heap_iterator<code_heap_relocator> iter(relocator);
- code->compact_heap(iter);
+ stack_trace_stripper stripper;
+ each_code_block(stripper);
}
-struct stack_trace_stripper {
- explicit stack_trace_stripper() {}
+struct code_block_accumulator {
+ std::vector<cell> objects;
void operator()(code_block *compiled, cell size)
{
- compiled->owner = false_object;
+ objects.push_back(compiled->owner);
+ objects.push_back(compiled->parameters);
+ objects.push_back(compiled->relocation);
+
+ objects.push_back(tag_fixnum(compiled->type()));
+ objects.push_back(tag_fixnum(compiled->size()));
+
+ /* Note: the entry point is always a multiple of the heap
+ alignment (16 bytes). We cannot allocate while iterating
+ through the code heap, so it is not possible to call
+ from_unsigned_cell() here. It is OK, however, to add it as
+ if it were a fixnum, and have library code shift it to the
+ left by 4. */
+ cell entry_point = (cell)compiled->entry_point();
+ assert((entry_point & (data_alignment - 1)) == 0);
+ assert((entry_point & TAG_MASK) == FIXNUM_TYPE);
+ objects.push_back(entry_point);
}
};
-void factor_vm::primitive_strip_stack_traces()
+cell factor_vm::code_blocks()
{
- stack_trace_stripper stripper;
- iterate_code_heap(stripper);
+ code_block_accumulator accum;
+ each_code_block(accum);
+ return std_vector_to_array(accum.objects);
+}
+
+void factor_vm::primitive_code_blocks()
+{
+ ctx->push(code_blocks());
}
}