6 callback_heap::callback_heap(cell size, factor_vm *parent_) :
7 seg(new segment(size,true)),
11 callback_heap::~callback_heap()
17 void factor_vm::init_callbacks(cell size)
19 callbacks = new callback_heap(size,this);
22 bool callback_heap::setup_seh_p()
24 #if defined(WINDOWS) && defined(FACTOR_X86)
31 bool callback_heap::return_takes_param_p()
33 #if defined(FACTOR_X86) || defined(FACTOR_AMD64)
40 instruction_operand callback_heap::callback_operand(code_block *stub, cell index)
42 tagged<array> code_template(parent->special_objects[CALLBACK_STUB]);
43 tagged<byte_array> relocation_template(array_nth(code_template.untagged(),0));
45 relocation_entry entry(relocation_template->data<relocation_entry>()[index]);
46 return instruction_operand(entry,stub,0);
49 void callback_heap::store_callback_operand(code_block *stub, cell index)
51 parent->store_external_address(callback_operand(stub,index));
54 void callback_heap::store_callback_operand(code_block *stub, cell index, cell value)
56 callback_operand(stub,index).store_value(value);
59 void callback_heap::update(code_block *stub)
61 store_callback_operand(stub,setup_seh_p() ? 2 : 1,(cell)callback_entry_point(stub));
65 code_block *callback_heap::add(cell owner, cell return_rewind)
67 tagged<array> code_template(parent->special_objects[CALLBACK_STUB]);
68 tagged<byte_array> insns(array_nth(code_template.untagged(),1));
69 cell size = array_capacity(insns.untagged());
71 cell bump = align(size + sizeof(code_block),data_alignment);
72 if(here + bump > seg->end) fatal_error("Out of callback space",0);
74 free_heap_block *free_block = (free_heap_block *)here;
75 free_block->make_free(bump);
78 code_block *stub = (code_block *)free_block;
80 stub->parameters = false_object;
81 stub->relocation = false_object;
83 memcpy(stub->entry_point(),insns->data<void>(),size);
85 /* Store VM pointer */
86 store_callback_operand(stub,0,(cell)parent);
92 store_callback_operand(stub,1);
98 /* Store VM pointer */
99 store_callback_operand(stub,index + 2,(cell)parent);
101 /* On x86, the RET instruction takes an argument which depends on
102 the callback's calling convention */
103 if(return_takes_param_p())
104 store_callback_operand(stub,index + 3,return_rewind);
111 struct callback_updater {
112 callback_heap *callbacks;
114 explicit callback_updater(callback_heap *callbacks_) : callbacks(callbacks_) {}
116 void operator()(code_block *stub)
118 callbacks->update(stub);
122 void callback_heap::update()
124 callback_updater updater(this);
125 each_callback(updater);
128 /* Allocates memory */
129 void factor_vm::primitive_callback()
131 cell return_rewind = to_cell(ctx->pop());
132 tagged<word> w(ctx->pop());
136 void* func = callbacks->add(w.value(),return_rewind)->entry_point();
137 CODE_TO_FUNCTION_POINTER_CALLBACK(this, func);
138 ctx->push(allot_alien(func));