]> gitweb.factorcode.org Git - factor.git/blob - vm/callbacks.cpp
VM: Refactor callbacks to Factor style
[factor.git] / vm / callbacks.cpp
1 #include "master.hpp"
2
3 namespace factor {
4
5 callback_heap::callback_heap(cell size, factor_vm* parent_)
6     : seg(new segment(size, true)), here(seg->start), parent(parent_) {}
7
8 callback_heap::~callback_heap() {
9   delete seg;
10   seg = NULL;
11 }
12
13 void factor_vm::init_callbacks(cell size) {
14   callbacks = new callback_heap(size, this);
15 }
16
17 bool callback_heap::setup_seh_p() {
18 #if defined(WINDOWS) && defined(FACTOR_X86)
19   return true;
20 #else
21   return false;
22 #endif
23 }
24
25 bool callback_heap::return_takes_param_p() {
26 #if defined(FACTOR_X86) || defined(FACTOR_AMD64)
27   return true;
28 #else
29   return false;
30 #endif
31 }
32
33 instruction_operand callback_heap::callback_operand(code_block* stub,
34                                                     cell index) {
35   tagged<array> code_template(parent->special_objects[CALLBACK_STUB]);
36   tagged<byte_array> relocation_template(
37       array_nth(code_template.untagged(), 0));
38
39   relocation_entry entry(relocation_template->data<relocation_entry>()[index]);
40   return instruction_operand(entry, stub, 0);
41 }
42
43 void callback_heap::store_callback_operand(code_block* stub, cell index) {
44   parent->store_external_address(callback_operand(stub, index));
45 }
46
47 void callback_heap::store_callback_operand(code_block* stub, cell index,
48                                            cell value) {
49   callback_operand(stub, index).store_value(value);
50 }
51
52 void callback_heap::update(code_block* stub) {
53   store_callback_operand(stub, setup_seh_p() ? 2 : 1,
54                          (cell) callback_entry_point(stub));
55   stub->flush_icache();
56 }
57
58 code_block* callback_heap::add(cell owner, cell return_rewind) {
59   tagged<array> code_template(parent->special_objects[CALLBACK_STUB]);
60   tagged<byte_array> insns(array_nth(code_template.untagged(), 1));
61   cell size = array_capacity(insns.untagged());
62
63   cell bump = align(size + sizeof(code_block), data_alignment);
64   if (here + bump > seg->end)
65     fatal_error("Out of callback space", 0);
66
67   free_heap_block* free_block = (free_heap_block*)here;
68   free_block->make_free(bump);
69   here += bump;
70
71   code_block* stub = (code_block*)free_block;
72   stub->owner = owner;
73   stub->parameters = false_object;
74   stub->relocation = false_object;
75
76   memcpy(stub->entry_point(), insns->data<void>(), size);
77
78   /* Store VM pointer */
79   store_callback_operand(stub, 0, (cell) parent);
80
81   cell index;
82
83   if (setup_seh_p()) {
84     store_callback_operand(stub, 1);
85     index = 1;
86   } else
87     index = 0;
88
89   /* Store VM pointer */
90   store_callback_operand(stub, index + 2, (cell) parent);
91
92   /* On x86, the RET instruction takes an argument which depends on
93      the callback's calling convention */
94   if (return_takes_param_p())
95     store_callback_operand(stub, index + 3, return_rewind);
96
97   update(stub);
98
99   return stub;
100 }
101
102 struct callback_updater {
103   callback_heap* callbacks;
104
105   explicit callback_updater(callback_heap* callbacks_)
106       : callbacks(callbacks_) {}
107
108   void operator()(code_block* stub) { callbacks->update(stub); }
109 };
110
111 void callback_heap::update() {
112   callback_updater updater(this);
113   each_callback(updater);
114 }
115
116 /* Allocates memory */
117 void factor_vm::primitive_callback() {
118   cell return_rewind = to_cell(ctx->pop());
119   tagged<word> w(ctx->pop());
120
121   w.untag_check(this);
122
123   void* func = callbacks->add(w.value(), return_rewind)->entry_point();
124   CODE_TO_FUNCTION_POINTER_CALLBACK(this, func);
125   ctx->push(allot_alien(func));
126 }
127
128 }