-
Notifications
You must be signed in to change notification settings - Fork 83
/
jitter.rs
222 lines (190 loc) · 6.34 KB
/
jitter.rs
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
//< ch-3
use std;
use std::cell::RefCell;
use std::rc::Rc;
use llvm_sys::prelude::LLVMValueRef;
use iron_llvm::{LLVMRef, LLVMRefCtor};
use iron_llvm::core;
use iron_llvm::core::types::{FunctionType, FunctionTypeRef, RealTypeCtor, RealTypeRef};
use iron_llvm::core::value::{Function, FunctionCtor, FunctionRef, Value};
use iron_llvm::execution_engine::{BindingSectionMemoryManagerBuilder, ExecutionEngine, MCJITBuilder};
use iron_llvm::execution_engine::execution_engine::FrozenModule;
use iron_llvm::support::add_symbol;
use builder;
//< jit-builtin
pub extern fn printd(x: f64) -> f64 {
println!("> {} <", x);
x
}
pub extern fn putchard(x: f64) -> f64 {
print!("{}", x as u8 as char);
x
}
pub fn init() {
unsafe {
add_symbol("printd", printd as *const ());
add_symbol("putchard", putchard as *const ());
}
}
//> jit-builtin
//< jit-jitter
pub trait JITter : builder::ModuleProvider {
// TODO: fix https://github.com/rust-lang/rust/issues/5665
fn get_module_provider(&mut self) -> &mut builder::ModuleProvider;
fn run_function(&mut self, f: LLVMValueRef) -> f64;
}
//> jit-jitter
//< jit-mc
struct ModulesContainer {
execution_engines: Vec<ExecutionEngine>,
modules: Vec<FrozenModule>
}
//> jit-mc
//< jit-mc-address
impl ModulesContainer {
fn get_function_address(&self, name: &str) -> u64 {
for ee in &self.execution_engines {
let addr = ee.get_function_address(name);
if addr != 0 {
return addr;
}
}
0
}
}
//> jit-mc-address
//< jit-mcjitter
pub struct MCJITter {
module_name: String,
current_module: core::Module,
function_passmanager: core::FunctionPassManager,
container: Rc<RefCell<ModulesContainer>>
}
//> jit-mcjitter
impl MCJITter {
//< jit-mcjitter-ctor
pub fn new(name: &str) -> MCJITter {
let (current_module, function_passmanager) = builder::new_module(name);
MCJITter {
module_name: String::from(name),
current_module: current_module,
function_passmanager: function_passmanager,
container: Rc::new(RefCell::new(ModulesContainer {
execution_engines: vec![],
modules: vec![]
}))
}
}
//> jit-mcjitter-ctor
//< jit-mcjitter-close-module
fn close_current_module(& mut self) {
//< jit-mcjitter-new-module
let (new_module, new_function_passmanager) = builder::new_module(&self.module_name);
self.function_passmanager = new_function_passmanager;
let current_module = std::mem::replace(&mut self.current_module, new_module);
//> jit-mcjitter-new-module
//< jit-mcjitter-mm
let container = self.container.clone();
let memory_manager = BindingSectionMemoryManagerBuilder::new()
.set_get_symbol_address(move |mut parent_mm, name| {
let addr = parent_mm.get_symbol_address(name);
if addr != 0 {
return addr;
}
container.borrow().get_function_address(name)
})
.create();
//> jit-mcjitter-mm
//< jit-mcjitter-ee
let (execution_engine, module) = match MCJITBuilder::new()
.set_mcjit_memory_manager(Box::new(memory_manager))
.create(current_module) {
Ok((ee, module)) => (ee, module),
Err(msg) => panic!(msg)
};
//> jit-mcjitter-ee
//< jit-mcjitter-container-update
self.container.borrow_mut().execution_engines.push(execution_engine);
self.container.borrow_mut().modules.push(module);
//> jit-mcjitter-container-update
}
//> jit-mcjitter-close-module
}
impl builder::ModuleProvider for MCJITter {
//< jit-mcjitter-mp-simple
fn dump(&self) {
for module in self.container.borrow().modules.iter() {
module.get().dump()
}
self.current_module.dump();
}
fn get_module(&mut self) -> &mut core::Module {
&mut self.current_module
}
//> jit-mcjitter-mp-simple
//< jit-mcjitter-mp-gf
fn get_function(&mut self, name: &str) -> Option<(FunctionRef, bool)> {
for module in &self.container.borrow().modules {
let funct = match module.get().get_function_by_name(name) {
Some(f) => {
f
},
None => continue
};
let proto = match self.current_module.get_function_by_name(name) {
Some(f) => {
if funct.count_basic_blocks() != 0 && f.count_basic_blocks() != 0 {
panic!("redefinition of function across modules")
}
f
},
None => {
// TODO: fix iron-llvm get_type
let fty = unsafe { FunctionTypeRef::from_ref(funct.get_type().to_ref()) };
let fty = unsafe { FunctionTypeRef::from_ref(fty.get_return_type().to_ref()) };
FunctionRef::new(&mut self.current_module, name, &fty)
}
};
if funct.count_basic_blocks() > 0 {
return Some((proto, true))
}
}
match self.current_module.get_function_by_name(name) {
Some(f) => Some((f, f.count_basic_blocks() > 0)),
None => None
}
}
//> jit-mcjitter-mp-gf
//< jit-mcjitter-mp-simple
fn get_pass_manager(&mut self) -> &mut core::FunctionPassManager {
&mut self.function_passmanager
}
//> jit-mcjitter-mp-simple
}
//< jit-mcjitter-jitter
impl JITter for MCJITter {
fn get_module_provider(&mut self) -> &mut builder::ModuleProvider {
self
}
fn run_function(&mut self, f: LLVMValueRef) -> f64 {
self.close_current_module();
let f = unsafe {FunctionRef::from_ref(f)};
let mut args = vec![];
let res = self.container.borrow()
.execution_engines.last().expect("MCJITter went crazy")
.run_function(&f, args.as_mut_slice());
let ty = RealTypeRef::get_double();
res.to_float(&ty)
}
}
//> jit-mcjitter-jitter
//< jit-smp
impl JITter for builder::SimpleModuleProvider {
fn get_module_provider(&mut self) -> &mut builder::ModuleProvider {
self
}
fn run_function(&mut self, _f: LLVMValueRef) -> f64 {
panic!("not implemented (and will not be)")
}
}
//> ch-3 jit-smp