]> gitweb.factorcode.org Git - factor.git/blob - vm/callbacks.cpp
Get green threads working on Windows
[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 #ifdef WIN32
42         cell index = 2;
43 #else
44         cell index = 1;
45 #endif
46         store_callback_operand(stub,index,(cell)callback_entry_point(stub));
47         stub->flush_icache();
48 }
49
50 code_block *callback_heap::add(cell owner, cell return_rewind)
51 {
52         tagged<array> code_template(parent->special_objects[CALLBACK_STUB]);
53         tagged<byte_array> insns(array_nth(code_template.untagged(),0));
54         cell size = array_capacity(insns.untagged());
55
56         cell bump = align(size + sizeof(code_block),data_alignment);
57         if(here + bump > seg->end) fatal_error("Out of callback space",0);
58
59         free_heap_block *free_block = (free_heap_block *)here;
60         free_block->make_free(bump);
61         here += bump;
62
63         code_block *stub = (code_block *)free_block;
64         stub->owner = owner;
65         stub->parameters = false_object;
66         stub->relocation = false_object;
67
68         memcpy(stub->entry_point(),insns->data<void>(),size);
69
70         /* Store VM pointer */
71         store_callback_operand(stub,0,(cell)parent);
72
73 #ifdef WIN32
74         store_callback_operand(stub,1,(cell)&exception_handler);
75         cell index = 1;
76 #else
77         cell index = 0;
78 #endif
79
80         /* Store VM pointer */
81         store_callback_operand(stub,index + 2,(cell)parent);
82
83         /* On x86, the RET instruction takes an argument which depends on
84         the callback's calling convention */
85 #if defined(FACTOR_X86) || defined(FACTOR_AMD64)
86         store_callback_operand(stub,index + 3,return_rewind);
87 #endif
88
89         update(stub);
90
91         return stub;
92 }
93
94 struct callback_updater {
95         callback_heap *callbacks;
96
97         explicit callback_updater(callback_heap *callbacks_) : callbacks(callbacks_) {}
98
99         void operator()(code_block *stub)
100         {
101                 callbacks->update(stub);
102         }
103 };
104
105 void callback_heap::update()
106 {
107         callback_updater updater(this);
108         each_callback(updater);
109 }
110
111 void factor_vm::primitive_callback()
112 {
113         cell return_rewind = to_cell(ctx->pop());
114         tagged<word> w(ctx->pop());
115
116         w.untag_check(this);
117         ctx->push(allot_alien(callbacks->add(w.value(),return_rewind)->entry_point()));
118 }
119
120 }