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 void callback_heap::store_callback_operand(code_block *stub, cell index, cell value)
24 tagged<array> code_template(parent->special_objects[CALLBACK_STUB]);
26 cell rel_class = untag_fixnum(array_nth(code_template.untagged(),3 * index + 1));
27 cell rel_type = untag_fixnum(array_nth(code_template.untagged(),3 * index + 2));
28 cell offset = untag_fixnum(array_nth(code_template.untagged(),3 * index + 3));
31 (relocation_type)rel_type,
32 (relocation_class)rel_class,
35 instruction_operand op(rel,stub,0);
36 op.store_value(value);
39 void callback_heap::update(code_block *stub)
46 store_callback_operand(stub,index,(cell)callback_entry_point(stub));
50 code_block *callback_heap::add(cell owner, cell return_rewind)
52 tagged<array> code_template(parent->special_objects[CALLBACK_STUB]);
53 tagged<byte_array> insns(array_nth(code_template.untagged(),0));
54 cell size = array_capacity(insns.untagged());
56 cell bump = align(size + sizeof(code_block),data_alignment);
57 if(here + bump > seg->end) fatal_error("Out of callback space",0);
59 free_heap_block *free_block = (free_heap_block *)here;
60 free_block->make_free(bump);
63 code_block *stub = (code_block *)free_block;
65 stub->parameters = false_object;
66 stub->relocation = false_object;
68 memcpy(stub->entry_point(),insns->data<void>(),size);
70 /* Store VM pointer */
71 store_callback_operand(stub,0,(cell)parent);
74 store_callback_operand(stub,1,(cell)&exception_handler);
80 /* Store VM pointer */
81 store_callback_operand(stub,index + 2,(cell)parent);
83 /* On x86, the RET instruction takes an argument which depends on
84 the callback's calling convention */
85 #if defined(FACTOR_X86) || defined(FACTOR_AMD64)
86 store_callback_operand(stub,index + 3,return_rewind);
94 struct callback_updater {
95 callback_heap *callbacks;
97 explicit callback_updater(callback_heap *callbacks_) : callbacks(callbacks_) {}
99 void operator()(code_block *stub)
101 callbacks->update(stub);
105 void callback_heap::update()
107 callback_updater updater(this);
108 each_callback(updater);
111 void factor_vm::primitive_callback()
113 cell return_rewind = to_cell(ctx->pop());
114 tagged<word> w(ctx->pop());
117 ctx->push(allot_alien(callbacks->add(w.value(),return_rewind)->entry_point()));