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]);
44 cell rel_class = untag_fixnum(array_nth(code_template.untagged(),3 * index + 1));
45 cell rel_type = untag_fixnum(array_nth(code_template.untagged(),3 * index + 2));
46 cell offset = untag_fixnum(array_nth(code_template.untagged(),3 * index + 3));
49 (relocation_type)rel_type,
50 (relocation_class)rel_class,
53 instruction_operand op(rel,stub,0);
58 void callback_heap::store_callback_operand(code_block *stub, cell index)
60 parent->store_external_address(callback_operand(stub,index));
63 void callback_heap::store_callback_operand(code_block *stub, cell index, cell value)
65 callback_operand(stub,index).store_value(value);
68 void callback_heap::update(code_block *stub)
70 store_callback_operand(stub,setup_seh_p() ? 2 : 1,(cell)callback_entry_point(stub));
74 code_block *callback_heap::add(cell owner, cell return_rewind)
76 tagged<array> code_template(parent->special_objects[CALLBACK_STUB]);
77 tagged<byte_array> insns(array_nth(code_template.untagged(),0));
78 cell size = array_capacity(insns.untagged());
80 cell bump = align(size + sizeof(code_block),data_alignment);
81 if(here + bump > seg->end) fatal_error("Out of callback space",0);
83 free_heap_block *free_block = (free_heap_block *)here;
84 free_block->make_free(bump);
87 code_block *stub = (code_block *)free_block;
89 stub->parameters = false_object;
90 stub->relocation = false_object;
92 memcpy(stub->entry_point(),insns->data<void>(),size);
94 /* Store VM pointer */
95 store_callback_operand(stub,0,(cell)parent);
101 store_callback_operand(stub,1);
107 /* Store VM pointer */
108 store_callback_operand(stub,index + 2,(cell)parent);
110 /* On x86, the RET instruction takes an argument which depends on
111 the callback's calling convention */
112 if(return_takes_param_p())
113 store_callback_operand(stub,index + 3,return_rewind);
120 struct callback_updater {
121 callback_heap *callbacks;
123 explicit callback_updater(callback_heap *callbacks_) : callbacks(callbacks_) {}
125 void operator()(code_block *stub)
127 callbacks->update(stub);
131 void callback_heap::update()
133 callback_updater updater(this);
134 each_callback(updater);
137 void factor_vm::primitive_callback()
139 cell return_rewind = to_cell(ctx->pop());
140 tagged<word> w(ctx->pop());
143 ctx->push(allot_alien(callbacks->add(w.value(),return_rewind)->entry_point()));