-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
52 lines (48 loc) · 1.73 KB
/
lib.rs
File metadata and controls
52 lines (48 loc) · 1.73 KB
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
use cranelift_codegen::ir::Function;
use cranelift_codegen::settings::*;
use cranelift_codegen::Context;
use cranelift_module::DataId;
use cranelift_module::{DataDescription, Linkage, Module};
use cranelift_object::{ObjectBuilder, ObjectModule};
pub struct ObjectSource {
pub obj_mod: ObjectModule,
pub data: DataDescription,
pub name: String,
}
impl ObjectSource {
pub fn new(obj_name: &str) -> ObjectSource {
let settings = builder();
let flags = Flags::new(settings);
let isa_builder = cranelift_native::builder().unwrap();
let isa = isa_builder.finish(flags).unwrap();
let obj_builder =
ObjectBuilder::new(isa, obj_name, cranelift_module::default_libcall_names()).unwrap();
ObjectSource {
obj_mod: ObjectModule::new(obj_builder),
data: DataDescription::new(),
name: obj_name.to_string(),
}
}
pub fn add_const_data(&mut self, name: &str, contents: Vec<u8>) -> DataId {
self.data.define(contents.into_boxed_slice());
let id = self
.obj_mod
.declare_data(name, Linkage::Export, false, false)
.unwrap();
self.obj_mod.define_data(id, &self.data).unwrap();
return id;
}
pub fn add_fn(&mut self, name: &str, func: Function) -> () {
let func_id = self
.obj_mod
.declare_function(name, Linkage::Export, &func.signature)
.unwrap();
let mut ctx = Context::for_function(func);
self.obj_mod.define_function(func_id, &mut ctx).unwrap();
}
pub fn flush_self(self) -> Vec<u8> {
let object_product = self.obj_mod.finish();
let bytes = object_product.emit().unwrap();
return bytes;
}
}