forked from factor/factor
-
Notifications
You must be signed in to change notification settings - Fork 3
/
jit.cpp
154 lines (134 loc) · 3.78 KB
/
jit.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "master.hpp"
namespace factor
{
/* Simple code generator used by:
- quotation compiler (quotations.cpp),
- megamorphic caches (dispatch.cpp),
- polymorphic inline caches (inline_cache.cpp) */
/* Allocates memory */
jit::jit(code_block_type type_, cell owner_, factor_vm *vm)
: type(type_),
owner(owner_,vm),
code(vm),
relocation(vm),
parameters(vm),
literals(vm),
computing_offset_p(false),
position(0),
offset(0),
parent(vm)
{
fixnum old_count = atomic::fetch_add(&parent->current_jit_count, 1);
FACTOR_ASSERT(old_count >= 0);
(void)old_count;
}
jit::~jit()
{
fixnum old_count = atomic::fetch_subtract(&parent->current_jit_count, 1);
FACTOR_ASSERT(old_count >= 1);
(void)old_count;
}
void jit::emit_relocation(cell relocation_template_)
{
data_root<byte_array> relocation_template(relocation_template_,parent);
cell capacity = array_capacity(relocation_template.untagged())
/ sizeof(relocation_entry);
relocation_entry *relocations = relocation_template->data<relocation_entry>();
for(cell i = 0; i < capacity; i++)
{
relocation_entry entry = relocations[i];
relocation_entry new_entry(entry.rel_type(), entry.rel_class(),
entry.rel_offset() + code.count);
relocation.append_bytes(&new_entry,sizeof(relocation_entry));
}
}
/* Allocates memory */
void jit::emit(cell code_template_)
{
data_root<array> code_template(code_template_,parent);
emit_relocation(array_nth(code_template.untagged(),0));
data_root<byte_array> insns(array_nth(code_template.untagged(),1),parent);
if(computing_offset_p)
{
cell size = array_capacity(insns.untagged());
if(offset == 0)
{
position--;
computing_offset_p = false;
}
else if(offset < size)
{
position++;
computing_offset_p = false;
}
else
offset -= size;
}
code.append_byte_array(insns.value());
}
/* Allocates memory */
void jit::emit_with_literal(cell code_template_, cell argument_) {
data_root<array> code_template(code_template_,parent);
data_root<object> argument(argument_,parent);
literal(argument.value());
emit(code_template.value());
}
/* Allocates memory */
void jit::emit_with_parameter(cell code_template_, cell argument_) {
data_root<array> code_template(code_template_,parent);
data_root<object> argument(argument_,parent);
parameter(argument.value());
emit(code_template.value());
}
/* Allocates memory */
bool jit::emit_subprimitive(cell word_, bool tail_call_p, bool stack_frame_p)
{
data_root<word> word(word_,parent);
data_root<array> code_template(word->subprimitive,parent);
parameters.append(untag<array>(array_nth(code_template.untagged(),0)));
literals.append(untag<array>(array_nth(code_template.untagged(),1)));
emit(array_nth(code_template.untagged(),2));
if(array_capacity(code_template.untagged()) == 5)
{
if(tail_call_p)
{
if(stack_frame_p) emit(parent->special_objects[JIT_EPILOG]);
emit(array_nth(code_template.untagged(),4));
return true;
}
else
emit(array_nth(code_template.untagged(),3));
}
return false;
}
/* Facility to convert compiled code offsets to quotation offsets.
Call jit_compute_offset() with the compiled code offset, then emit
code, and at the end jit->position is the quotation position. */
void jit::compute_position(cell offset_)
{
computing_offset_p = true;
position = 0;
offset = offset_;
}
/* Allocates memory */
code_block *jit::to_code_block(cell frame_size)
{
/* Emit dummy GC info */
code.grow_bytes(alignment_for(code.count + 4,data_alignment));
u32 dummy_gc_info = 0;
code.append_bytes(&dummy_gc_info,sizeof(u32));
code.trim();
relocation.trim();
parameters.trim();
literals.trim();
return parent->add_code_block(
type,
code.elements.value(),
false_object, /* no labels */
owner.value(),
relocation.elements.value(),
parameters.elements.value(),
literals.elements.value(),
frame_size);
}
}