2009-10-16 12:39:22 -04:00
|
|
|
#include "master.hpp"
|
|
|
|
|
2013-05-11 21:47:11 -04:00
|
|
|
namespace factor {
|
|
|
|
|
2016-04-24 08:19:35 -04:00
|
|
|
bool return_takes_param_p() {
|
|
|
|
#if defined(FACTOR_X86) || defined(FACTOR_AMD64)
|
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-09-09 08:45:01 -04:00
|
|
|
callback_heap::callback_heap(cell size, factor_vm* parent) {
|
|
|
|
seg = new segment(size, true);
|
|
|
|
if (!seg)
|
|
|
|
fatal_error("Out of memory in callback_heap constructor", size);
|
2014-09-09 11:56:56 -04:00
|
|
|
allocator = new free_list_allocator<code_block>(size, seg->start);
|
2014-09-09 08:45:01 -04:00
|
|
|
this->parent = parent;
|
|
|
|
|
|
|
|
}
|
2013-05-11 21:47:11 -04:00
|
|
|
|
|
|
|
callback_heap::~callback_heap() {
|
2014-09-09 08:45:01 -04:00
|
|
|
delete allocator;
|
|
|
|
allocator = NULL;
|
2013-05-11 21:47:11 -04:00
|
|
|
delete seg;
|
|
|
|
seg = NULL;
|
2009-10-16 12:39:22 -04:00
|
|
|
}
|
|
|
|
|
2013-05-11 21:47:11 -04:00
|
|
|
instruction_operand callback_heap::callback_operand(code_block* stub,
|
|
|
|
cell index) {
|
|
|
|
tagged<array> code_template(parent->special_objects[CALLBACK_STUB]);
|
|
|
|
tagged<byte_array> relocation_template(
|
|
|
|
array_nth(code_template.untagged(), 0));
|
2009-10-16 12:39:22 -04:00
|
|
|
|
2013-05-11 21:47:11 -04:00
|
|
|
relocation_entry entry(relocation_template->data<relocation_entry>()[index]);
|
|
|
|
return instruction_operand(entry, stub, 0);
|
2010-04-04 17:46:36 -04:00
|
|
|
}
|
|
|
|
|
2013-05-11 21:47:11 -04:00
|
|
|
void callback_heap::store_callback_operand(code_block* stub, cell index,
|
|
|
|
cell value) {
|
2015-08-30 06:21:20 -04:00
|
|
|
instruction_operand op = callback_operand(stub, index);
|
|
|
|
op.store_value(value);
|
2010-01-10 07:20:32 -05:00
|
|
|
}
|
2009-12-02 01:48:41 -05:00
|
|
|
|
2013-05-11 21:47:11 -04:00
|
|
|
void callback_heap::update(code_block* stub) {
|
2016-11-08 20:07:56 -05:00
|
|
|
word* w = untag<word>(stub->owner);
|
2015-08-30 20:10:05 -04:00
|
|
|
store_callback_operand(stub, 1, w->entry_point);
|
2013-05-11 21:47:11 -04:00
|
|
|
stub->flush_icache();
|
2009-10-16 12:39:22 -04:00
|
|
|
}
|
|
|
|
|
2013-05-11 21:47:11 -04:00
|
|
|
code_block* callback_heap::add(cell owner, cell return_rewind) {
|
2016-08-21 10:26:04 -04:00
|
|
|
// code_template is a 2-tuple where the first element contains the
|
|
|
|
// relocations and the second a byte array of compiled assembly
|
|
|
|
// code. The code assumes that there are four relocations on x86 and
|
|
|
|
// three on ppc.
|
2013-05-11 21:47:11 -04:00
|
|
|
tagged<array> code_template(parent->special_objects[CALLBACK_STUB]);
|
|
|
|
tagged<byte_array> insns(array_nth(code_template.untagged(), 1));
|
|
|
|
cell size = array_capacity(insns.untagged());
|
2009-10-16 12:39:22 -04:00
|
|
|
|
2013-05-11 21:47:11 -04:00
|
|
|
cell bump = align(size + sizeof(code_block), data_alignment);
|
2014-09-09 11:56:56 -04:00
|
|
|
code_block* stub = allocator->allot(bump);
|
|
|
|
if (!stub) {
|
2014-09-09 06:44:01 -04:00
|
|
|
parent->general_error(ERROR_CALLBACK_SPACE_OVERFLOW,
|
|
|
|
false_object,
|
|
|
|
false_object);
|
|
|
|
}
|
2014-09-09 11:56:56 -04:00
|
|
|
stub->header = bump & ~7;
|
2013-05-11 21:47:11 -04:00
|
|
|
stub->owner = owner;
|
|
|
|
stub->parameters = false_object;
|
|
|
|
stub->relocation = false_object;
|
2009-12-02 01:48:41 -05:00
|
|
|
|
2015-01-07 05:51:19 -05:00
|
|
|
memcpy((void*)stub->entry_point(), insns->data<void>(), size);
|
2010-01-02 07:03:30 -05:00
|
|
|
|
2016-08-21 10:26:04 -04:00
|
|
|
// Store VM pointer in two relocations.
|
2013-05-13 00:53:47 -04:00
|
|
|
store_callback_operand(stub, 0, (cell)parent);
|
2015-08-30 20:10:05 -04:00
|
|
|
store_callback_operand(stub, 2, (cell)parent);
|
2010-01-10 07:20:32 -05:00
|
|
|
|
2016-08-21 10:26:04 -04:00
|
|
|
// On x86, the RET instruction takes an argument which depends on
|
|
|
|
// the callback's calling convention
|
2013-05-11 21:47:11 -04:00
|
|
|
if (return_takes_param_p())
|
2015-08-30 06:21:20 -04:00
|
|
|
store_callback_operand(stub, 3, return_rewind);
|
2010-01-02 07:03:30 -05:00
|
|
|
|
2013-05-11 21:47:11 -04:00
|
|
|
update(stub);
|
|
|
|
return stub;
|
2009-10-16 12:39:22 -04:00
|
|
|
}
|
|
|
|
|
2016-08-21 10:26:04 -04:00
|
|
|
// Allocates memory (add(), allot_alien())
|
2013-05-11 21:47:11 -04:00
|
|
|
void factor_vm::primitive_callback() {
|
|
|
|
cell return_rewind = to_cell(ctx->pop());
|
|
|
|
tagged<word> w(ctx->pop());
|
2016-11-08 20:07:56 -05:00
|
|
|
check_tagged(w);
|
2011-05-20 18:11:50 -04:00
|
|
|
|
2015-01-07 05:51:19 -05:00
|
|
|
cell func = callbacks->add(w.value(), return_rewind)->entry_point();
|
2013-05-11 21:47:11 -04:00
|
|
|
CODE_TO_FUNCTION_POINTER_CALLBACK(this, func);
|
2015-08-18 02:18:41 -04:00
|
|
|
ctx->push(allot_alien(func));
|
2009-10-16 12:39:22 -04:00
|
|
|
}
|
|
|
|
|
2014-09-15 12:20:22 -04:00
|
|
|
void factor_vm::primitive_free_callback() {
|
|
|
|
void* entry_point = alien_offset(ctx->pop());
|
|
|
|
code_block* stub = (code_block*)entry_point - 1;
|
|
|
|
callbacks->allocator->free(stub);
|
|
|
|
}
|
|
|
|
|
2016-08-21 10:26:04 -04:00
|
|
|
// Allocates memory
|
2014-09-10 11:01:48 -04:00
|
|
|
void factor_vm::primitive_callback_room() {
|
|
|
|
allocator_room room = callbacks->allocator->as_allocator_room();
|
|
|
|
ctx->push(tag<byte_array>(byte_array_from_value(&room)));
|
|
|
|
}
|
|
|
|
|
2009-10-16 12:39:22 -04:00
|
|
|
}
|