]> gitweb.factorcode.org Git - factor.git/blob - vm/callbacks.cpp
6c8165f5c40807dcd41a91e9a7e8fe092c7fec14
[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_entry_point(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->entry_point(),insns->data<void>(),size);
64
65         /* Store VM pointer */
66         store_callback_operand(stub,0,(cell)parent);
67         store_callback_operand(stub,2,(cell)parent);
68
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);
73 #endif
74
75         update(stub);
76
77         return stub;
78 }
79
80 struct callback_updater {
81         callback_heap *callbacks;
82
83         explicit callback_updater(callback_heap *callbacks_) : callbacks(callbacks_) {}
84
85         void operator()(code_block *stub)
86         {
87                 callbacks->update(stub);
88         }
89 };
90
91 void callback_heap::update()
92 {
93         callback_updater updater(this);
94         each_callback(updater);
95 }
96
97 void factor_vm::primitive_callback()
98 {
99         cell return_rewind = to_cell(ctx->pop());
100         tagged<word> w(ctx->pop());
101
102         w.untag_check(this);
103         ctx->push(allot_alien(callbacks->add(w.value(),return_rewind)->entry_point()));
104 }
105
106 }