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)
41 store_callback_operand(stub,1,(cell)callback_entry_point(stub));
45 code_block *callback_heap::add(cell owner, cell return_rewind)
47 tagged<array> code_template(parent->special_objects[CALLBACK_STUB]);
48 tagged<byte_array> insns(array_nth(code_template.untagged(),0));
49 cell size = array_capacity(insns.untagged());
51 cell bump = align(size + sizeof(code_block),data_alignment);
52 if(here + bump > seg->end) fatal_error("Out of callback space",0);
54 free_heap_block *free_block = (free_heap_block *)here;
55 free_block->make_free(bump);
58 code_block *stub = (code_block *)free_block;
60 stub->parameters = false_object;
61 stub->relocation = false_object;
63 memcpy(stub->entry_point(),insns->data<void>(),size);
65 /* Store VM pointer */
66 store_callback_operand(stub,0,(cell)parent);
67 store_callback_operand(stub,2,(cell)parent);
69 /* On x86, the RET instruction takes an argument which depends on
70 the callback's calling convention */
71 #if defined(FACTOR_X86) || defined(FACTOR_AMD64)
72 store_callback_operand(stub,3,return_rewind);
80 struct callback_updater {
81 callback_heap *callbacks;
83 explicit callback_updater(callback_heap *callbacks_) : callbacks(callbacks_) {}
85 void operator()(code_block *stub)
87 callbacks->update(stub);
91 void callback_heap::update()
93 callback_updater updater(this);
94 each_callback(updater);
97 void factor_vm::primitive_callback()
99 cell return_rewind = to_cell(ctx->pop());
100 tagged<word> w(ctx->pop());
103 ctx->push(allot_alien(callbacks->add(w.value(),return_rewind)->entry_point()));