]> gitweb.factorcode.org Git - factor.git/blob - vm/callbacks.cpp
Merge branch 'master' of git://factorcode.org/git/factor
[factor.git] / vm / callbacks.cpp
1 #include "master.hpp"
2
3 namespace factor
4 {
5
6 callback_heap::callback_heap(cell size, factor_vm *parent_) :
7         seg(new segment(size,true)),
8         here(seg->start),
9         parent(parent_) {}
10
11 callback_heap::~callback_heap()
12 {
13         delete seg;
14         seg = NULL;
15 }
16
17 void factor_vm::init_callbacks(cell size)
18 {
19         callbacks = new callback_heap(size,this);
20 }
21
22 void callback_heap::store_callback_operand(code_block *stub, cell index, cell value)
23 {
24         tagged<array> code_template(parent->special_objects[CALLBACK_STUB]);
25
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));
29
30         relocation_entry rel(
31                 (relocation_type)rel_type,
32                 (relocation_class)rel_class,
33                 offset);
34
35         instruction_operand op(rel,stub,0);
36         op.store_value(value);
37 }
38
39 void callback_heap::update(code_block *stub)
40 {
41         store_callback_operand(stub,1,(cell)callback_xt(stub));
42         stub->flush_icache();
43 }
44
45 code_block *callback_heap::add(cell owner, cell return_rewind)
46 {
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());
50
51         cell bump = align(size + sizeof(code_block),data_alignment);
52         if(here + bump > seg->end) fatal_error("Out of callback space",0);
53
54         free_heap_block *free_block = (free_heap_block *)here;
55         free_block->make_free(bump);
56         here += bump;
57
58         code_block *stub = (code_block *)free_block;
59         stub->owner = owner;
60         stub->parameters = false_object;
61         stub->relocation = false_object;
62
63         memcpy(stub->xt(),insns->data<void>(),size);
64
65         /* Store VM pointer */
66         store_callback_operand(stub,0,(cell)parent);
67
68         /* On x86, the RET instruction takes an argument which depends on
69         the callback's calling convention */
70 #if defined(FACTOR_X86) || defined(FACTOR_AMD64)
71         store_callback_operand(stub,2,return_rewind);
72 #endif
73
74         update(stub);
75
76         return stub;
77 }
78
79 struct callback_updater {
80         callback_heap *callbacks;
81
82         explicit callback_updater(callback_heap *callbacks_) : callbacks(callbacks_) {}
83
84         void operator()(code_block *stub)
85         {
86                 callbacks->update(stub);
87         }
88 };
89
90 void callback_heap::update()
91 {
92         callback_updater updater(this);
93         each_callback(updater);
94 }
95
96 void factor_vm::primitive_callback()
97 {
98         cell return_rewind = to_cell(ctx->pop());
99         tagged<word> w(ctx->pop());
100
101         w.untag_check(this);
102         ctx->push(allot_alien(callbacks->add(w.value(),return_rewind)->xt()));
103 }
104
105 }