]> gitweb.factorcode.org Git - factor.git/blob - vm/callbacks.cpp
Change how non-volatile register preservation is done in alien callbacks, with the...
[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::update(code_block *stub)
23 {
24         tagged<array> code_template(parent->special_objects[CALLBACK_STUB]);
25
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));
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((cell)callback_xt(stub));
37
38         stub->flush_icache();
39 }
40
41 code_block *callback_heap::add(cell owner, cell return_rewind)
42 {
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());
46
47         cell bump = align(size + sizeof(code_block),data_alignment);
48         if(here + bump > seg->end) fatal_error("Out of callback space",0);
49
50         free_heap_block *free_block = (free_heap_block *)here;
51         free_block->make_free(bump);
52         here += bump;
53
54         code_block *stub = (code_block *)free_block;
55         stub->owner = owner;
56         stub->parameters = false_object;
57         stub->relocation = false_object;
58
59         memcpy(stub->xt(),insns->data<void>(),size);
60
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)
64         {
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));
68
69                 relocation_entry rel(
70                         (relocation_type)rel_type,
71                         (relocation_class)rel_class,
72                         offset);
73
74                 instruction_operand op(rel,stub,0);
75                 op.store_value(return_rewind);
76         }
77
78         update(stub);
79
80         return stub;
81 }
82
83 struct callback_updater {
84         callback_heap *callbacks;
85
86         explicit callback_updater(callback_heap *callbacks_) : callbacks(callbacks_) {}
87
88         void operator()(code_block *stub)
89         {
90                 callbacks->update(stub);
91         }
92 };
93
94 void callback_heap::update()
95 {
96         callback_updater updater(this);
97         each_callback(updater);
98 }
99
100 void factor_vm::primitive_callback()
101 {
102         cell return_rewind = to_cell(ctx->pop());
103         tagged<word> w(ctx->pop());
104
105         w.untag_check(this);
106         ctx->push(allot_alien(callbacks->add(w.value(),return_rewind)->xt()));
107 }
108
109 }