5 callback_heap::callback_heap(cell size, factor_vm* parent) {
6 seg = new segment(size, true);
8 fatal_error("Out of memory in callback_heap constructor", size);
9 allocator = new free_list_allocator<code_block>(size, seg->start);
10 this->parent = parent;
14 callback_heap::~callback_heap() {
22 void factor_vm::init_callbacks(cell size) {
23 callbacks = new callback_heap(size, this);
26 bool callback_heap::setup_seh_p() {
27 #if defined(WINDOWS) && defined(FACTOR_X86)
34 bool callback_heap::return_takes_param_p() {
35 #if defined(FACTOR_X86) || defined(FACTOR_AMD64)
42 instruction_operand callback_heap::callback_operand(code_block* stub,
44 tagged<array> code_template(parent->special_objects[CALLBACK_STUB]);
45 tagged<byte_array> relocation_template(
46 array_nth(code_template.untagged(), 0));
48 relocation_entry entry(relocation_template->data<relocation_entry>()[index]);
49 return instruction_operand(entry, stub, 0);
52 void callback_heap::store_callback_operand(code_block* stub, cell index) {
53 instruction_operand op = callback_operand(stub, index);
54 op.store_value(parent->compute_external_address(op));
57 void callback_heap::store_callback_operand(code_block* stub, cell index,
59 callback_operand(stub, index).store_value(value);
62 void callback_heap::update(code_block* stub) {
63 store_callback_operand(stub, setup_seh_p() ? 2 : 1,
64 callback_entry_point(stub));
68 code_block* callback_heap::add(cell owner, cell return_rewind) {
69 tagged<array> code_template(parent->special_objects[CALLBACK_STUB]);
70 tagged<byte_array> insns(array_nth(code_template.untagged(), 1));
71 cell size = array_capacity(insns.untagged());
73 cell bump = align(size + sizeof(code_block), data_alignment);
74 code_block* stub = allocator->allot(bump);
76 parent->general_error(ERROR_CALLBACK_SPACE_OVERFLOW,
81 stub->header = bump & ~7;
83 stub->parameters = false_object;
84 stub->relocation = false_object;
86 memcpy((void*)stub->entry_point(), insns->data<void>(), size);
88 /* Store VM pointer */
89 store_callback_operand(stub, 0, (cell)parent);
94 store_callback_operand(stub, 1);
99 /* Store VM pointer */
100 store_callback_operand(stub, index + 2, (cell) parent);
102 /* On x86, the RET instruction takes an argument which depends on
103 the callback's calling convention */
104 if (return_takes_param_p())
105 store_callback_operand(stub, index + 3, return_rewind);
112 void callback_heap::update() {
113 auto callback_updater = [&](code_block* stub, cell size) {
116 allocator->iterate(callback_updater);
119 /* Allocates memory (add(), allot_alien())*/
120 void factor_vm::primitive_callback() {
121 cell return_rewind = to_cell(ctx->pop());
122 tagged<word> w(ctx->pop());
126 cell func = callbacks->add(w.value(), return_rewind)->entry_point();
127 CODE_TO_FUNCTION_POINTER_CALLBACK(this, func);
128 ctx->push(allot_alien(func));
131 void factor_vm::primitive_free_callback() {
132 void* entry_point = alien_offset(ctx->pop());
133 code_block* stub = (code_block*)entry_point - 1;
134 callbacks->allocator->free(stub);
137 /* Allocates memory */
138 void factor_vm::primitive_callback_room() {
139 allocator_room room = callbacks->allocator->as_allocator_room();
140 ctx->push(tag<byte_array>(byte_array_from_value(&room)));