2009-10-16 12:39:22 -04:00
|
|
|
#include "master.hpp"
|
|
|
|
|
|
|
|
namespace factor
|
|
|
|
{
|
|
|
|
|
2009-10-18 21:31:59 -04:00
|
|
|
callback_heap::callback_heap(cell size, factor_vm *parent_) :
|
2009-10-16 12:39:22 -04:00
|
|
|
seg(new segment(size,true)),
|
|
|
|
here(seg->start),
|
2009-10-18 21:31:59 -04:00
|
|
|
parent(parent_) {}
|
2009-10-16 12:39:22 -04:00
|
|
|
|
|
|
|
callback_heap::~callback_heap()
|
|
|
|
{
|
|
|
|
delete seg;
|
|
|
|
seg = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void factor_vm::init_callbacks(cell size)
|
|
|
|
{
|
|
|
|
callbacks = new callback_heap(size,this);
|
|
|
|
}
|
|
|
|
|
2009-12-02 01:48:41 -05:00
|
|
|
void callback_heap::update(code_block *stub)
|
2009-10-16 12:39:22 -04:00
|
|
|
{
|
2009-10-23 01:33:53 -04:00
|
|
|
tagged<array> code_template(parent->special_objects[CALLBACK_STUB]);
|
2009-10-16 12:39:22 -04:00
|
|
|
|
|
|
|
cell rel_class = untag_fixnum(array_nth(code_template.untagged(),1));
|
2009-12-02 01:48:41 -05:00
|
|
|
cell rel_type = untag_fixnum(array_nth(code_template.untagged(),2));
|
2009-10-16 12:39:22 -04:00
|
|
|
cell offset = untag_fixnum(array_nth(code_template.untagged(),3));
|
|
|
|
|
2009-12-02 01:48:41 -05:00
|
|
|
relocation_entry rel(
|
|
|
|
(relocation_type)rel_type,
|
|
|
|
(relocation_class)rel_class,
|
|
|
|
offset);
|
2009-10-16 12:39:22 -04:00
|
|
|
|
2009-12-02 01:48:41 -05:00
|
|
|
instruction_operand op(rel,stub,0);
|
|
|
|
op.store_value((cell)callback_xt(stub));
|
|
|
|
|
|
|
|
stub->flush_icache();
|
2009-10-16 12:39:22 -04:00
|
|
|
}
|
|
|
|
|
2009-12-02 01:48:41 -05:00
|
|
|
code_block *callback_heap::add(cell owner)
|
2009-10-16 12:39:22 -04:00
|
|
|
{
|
2009-10-23 01:33:53 -04:00
|
|
|
tagged<array> code_template(parent->special_objects[CALLBACK_STUB]);
|
2009-10-16 12:39:22 -04:00
|
|
|
tagged<byte_array> insns(array_nth(code_template.untagged(),0));
|
|
|
|
cell size = array_capacity(insns.untagged());
|
|
|
|
|
2009-12-02 01:48:41 -05:00
|
|
|
cell bump = align(size + sizeof(code_block),data_alignment);
|
2009-10-16 12:39:22 -04:00
|
|
|
if(here + bump > seg->end) fatal_error("Out of callback space",0);
|
|
|
|
|
2009-12-02 01:48:41 -05:00
|
|
|
free_heap_block *free_block = (free_heap_block *)here;
|
|
|
|
free_block->make_free(bump);
|
2009-10-16 12:39:22 -04:00
|
|
|
here += bump;
|
|
|
|
|
2009-12-02 01:48:41 -05:00
|
|
|
code_block *stub = (code_block *)free_block;
|
|
|
|
stub->owner = owner;
|
2009-12-02 05:28:15 -05:00
|
|
|
stub->parameters = false_object;
|
2009-12-02 01:48:41 -05:00
|
|
|
stub->relocation = false_object;
|
|
|
|
|
|
|
|
memcpy(stub->xt(),insns->data<void>(),size);
|
2009-10-16 12:39:22 -04:00
|
|
|
update(stub);
|
|
|
|
|
|
|
|
return stub;
|
|
|
|
}
|
|
|
|
|
2009-12-02 04:31:57 -05:00
|
|
|
struct callback_updater {
|
|
|
|
callback_heap *callbacks;
|
|
|
|
|
|
|
|
explicit callback_updater(callback_heap *callbacks_) : callbacks(callbacks_) {}
|
|
|
|
|
|
|
|
void operator()(code_block *stub)
|
|
|
|
{
|
|
|
|
callbacks->update(stub);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void callback_heap::update()
|
|
|
|
{
|
|
|
|
callback_updater updater(this);
|
|
|
|
each_callback(updater);
|
|
|
|
}
|
|
|
|
|
2009-10-16 12:39:22 -04:00
|
|
|
void factor_vm::primitive_callback()
|
|
|
|
{
|
|
|
|
tagged<word> w(dpop());
|
|
|
|
w.untag_check(this);
|
2009-12-02 01:48:41 -05:00
|
|
|
box_alien(callbacks->add(w.value())->xt());
|
2009-10-16 12:39:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|