5 callback_heap::callback_heap(cell size, factor_vm* parent)
6 : seg(new segment(size, true)), here(seg->start), parent(parent) {}
8 callback_heap::~callback_heap() {
13 void factor_vm::init_callbacks(cell size) {
14 callbacks = new callback_heap(size, this);
17 bool callback_heap::setup_seh_p() {
18 #if defined(WINDOWS) && defined(FACTOR_X86)
25 bool callback_heap::return_takes_param_p() {
26 #if defined(FACTOR_X86) || defined(FACTOR_AMD64)
33 instruction_operand callback_heap::callback_operand(code_block* stub,
35 tagged<array> code_template(parent->special_objects[CALLBACK_STUB]);
36 tagged<byte_array> relocation_template(
37 array_nth(code_template.untagged(), 0));
39 relocation_entry entry(relocation_template->data<relocation_entry>()[index]);
40 return instruction_operand(entry, stub, 0);
43 void callback_heap::store_callback_operand(code_block* stub, cell index) {
44 parent->store_external_address(callback_operand(stub, index));
47 void callback_heap::store_callback_operand(code_block* stub, cell index,
49 callback_operand(stub, index).store_value(value);
52 void callback_heap::update(code_block* stub) {
53 store_callback_operand(stub, setup_seh_p() ? 2 : 1,
54 (cell)callback_entry_point(stub));
58 code_block* callback_heap::add(cell owner, cell return_rewind) {
59 tagged<array> code_template(parent->special_objects[CALLBACK_STUB]);
60 tagged<byte_array> insns(array_nth(code_template.untagged(), 1));
61 cell size = array_capacity(insns.untagged());
63 cell bump = align(size + sizeof(code_block), data_alignment);
64 if (here + bump > seg->end)
65 fatal_error("Out of callback space", 0);
67 free_heap_block* free_block = (free_heap_block*)here;
68 free_block->make_free(bump);
71 code_block* stub = (code_block*)free_block;
73 stub->parameters = false_object;
74 stub->relocation = false_object;
76 memcpy(stub->entry_point(), insns->data<void>(), size);
78 /* Store VM pointer */
79 store_callback_operand(stub, 0, (cell)parent);
84 store_callback_operand(stub, 1);
89 /* Store VM pointer */
90 store_callback_operand(stub, index + 2, (cell) parent);
92 /* On x86, the RET instruction takes an argument which depends on
93 the callback's calling convention */
94 if (return_takes_param_p())
95 store_callback_operand(stub, index + 3, return_rewind);
102 struct callback_updater {
103 callback_heap* callbacks;
105 explicit callback_updater(callback_heap* callbacks)
106 : callbacks(callbacks) {}
108 void operator()(code_block* stub) { callbacks->update(stub); }
111 void callback_heap::update() {
112 callback_updater updater(this);
113 each_callback(updater);
116 /* Allocates memory */
117 void factor_vm::primitive_callback() {
118 cell return_rewind = to_cell(ctx->pop());
119 tagged<word> w(ctx->pop());
123 void* func = callbacks->add(w.value(), return_rewind)->entry_point();
124 CODE_TO_FUNCTION_POINTER_CALLBACK(this, func);
125 ctx->push(allot_alien(func));