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::update(code_block *stub)
24 tagged<array> code_template(parent->special_objects[CALLBACK_STUB]);
26 cell rel_class = untag_fixnum(array_nth(code_template.untagged(),1));
27 cell rel_type = untag_fixnum(array_nth(code_template.untagged(),2));
28 cell offset = untag_fixnum(array_nth(code_template.untagged(),3));
31 (relocation_type)rel_type,
32 (relocation_class)rel_class,
35 instruction_operand op(rel,stub,0);
36 op.store_value((cell)callback_xt(stub));
41 code_block *callback_heap::add(cell owner, cell return_rewind)
43 tagged<array> code_template(parent->special_objects[CALLBACK_STUB]);
44 tagged<byte_array> insns(array_nth(code_template.untagged(),0));
45 cell size = array_capacity(insns.untagged());
47 cell bump = align(size + sizeof(code_block),data_alignment);
48 if(here + bump > seg->end) fatal_error("Out of callback space",0);
50 free_heap_block *free_block = (free_heap_block *)here;
51 free_block->make_free(bump);
54 code_block *stub = (code_block *)free_block;
56 stub->parameters = false_object;
57 stub->relocation = false_object;
59 memcpy(stub->xt(),insns->data<void>(),size);
61 /* On x86, the RET instruction takes an argument which depends on
62 the callback's calling convention */
63 if(array_capacity(code_template.untagged()) == 7)
65 cell rel_class = untag_fixnum(array_nth(code_template.untagged(),4));
66 cell rel_type = untag_fixnum(array_nth(code_template.untagged(),5));
67 cell offset = untag_fixnum(array_nth(code_template.untagged(),6));
70 (relocation_type)rel_type,
71 (relocation_class)rel_class,
74 instruction_operand op(rel,stub,0);
75 op.store_value(return_rewind);
83 struct callback_updater {
84 callback_heap *callbacks;
86 explicit callback_updater(callback_heap *callbacks_) : callbacks(callbacks_) {}
88 void operator()(code_block *stub)
90 callbacks->update(stub);
94 void callback_heap::update()
96 callback_updater updater(this);
97 each_callback(updater);
100 void factor_vm::primitive_callback()
102 cell return_rewind = to_cell(ctx->pop());
103 tagged<word> w(ctx->pop());
106 ctx->push(allot_alien(callbacks->add(w.value(),return_rewind)->xt()));