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
use cranelift_codegen::binemit;
use cranelift_codegen::ir;
use cranelift_codegen::ir::ExternalName;
use cranelift_codegen::isa;
use cranelift_codegen::{CodegenError, Context};
use cranelift_entity::PrimaryMap;
use cranelift_wasm::{DefinedFuncIndex, FuncIndex, FuncTranslator, WasmError};
use func_environ::{
get_func_name, get_imported_memory32_grow_name, get_imported_memory32_size_name,
get_memory32_grow_name, get_memory32_size_name, FuncEnvironment,
};
use module::Module;
use std::vec::Vec;
#[derive(Debug)]
pub struct Compilation {
pub functions: PrimaryMap<DefinedFuncIndex, Vec<u8>>,
}
impl Compilation {
pub fn new(functions: PrimaryMap<DefinedFuncIndex, Vec<u8>>) -> Self {
Self { functions }
}
}
pub struct RelocSink {
pub func_relocs: Vec<Relocation>,
}
impl binemit::RelocSink for RelocSink {
fn reloc_ebb(
&mut self,
_offset: binemit::CodeOffset,
_reloc: binemit::Reloc,
_ebb_offset: binemit::CodeOffset,
) {
panic!("ebb headers not yet implemented");
}
fn reloc_external(
&mut self,
offset: binemit::CodeOffset,
reloc: binemit::Reloc,
name: &ExternalName,
addend: binemit::Addend,
) {
let reloc_target = if *name == get_memory32_grow_name() {
RelocationTarget::Memory32Grow
} else if *name == get_imported_memory32_grow_name() {
RelocationTarget::ImportedMemory32Grow
} else if *name == get_memory32_size_name() {
RelocationTarget::Memory32Size
} else if *name == get_imported_memory32_size_name() {
RelocationTarget::ImportedMemory32Size
} else if let ExternalName::User { namespace, index } = *name {
debug_assert!(namespace == 0);
RelocationTarget::UserFunc(FuncIndex::from_u32(index))
} else if let ExternalName::LibCall(libcall) = *name {
RelocationTarget::LibCall(libcall)
} else {
panic!("unrecognized external name")
};
self.func_relocs.push(Relocation {
reloc,
reloc_target,
offset,
addend,
});
}
fn reloc_jt(
&mut self,
_offset: binemit::CodeOffset,
_reloc: binemit::Reloc,
_jt: ir::JumpTable,
) {
panic!("jump tables not yet implemented");
}
}
impl RelocSink {
pub fn new() -> Self {
Self {
func_relocs: Vec::new(),
}
}
}
#[derive(Debug, Clone)]
pub struct Relocation {
pub reloc: binemit::Reloc,
pub reloc_target: RelocationTarget,
pub offset: binemit::CodeOffset,
pub addend: binemit::Addend,
}
#[derive(Debug, Copy, Clone)]
pub enum RelocationTarget {
UserFunc(FuncIndex),
LibCall(ir::LibCall),
Memory32Grow,
ImportedMemory32Grow,
Memory32Size,
ImportedMemory32Size,
}
pub type Relocations = PrimaryMap<DefinedFuncIndex, Vec<Relocation>>;
pub fn compile_module<'data, 'module>(
module: &'module Module,
function_body_inputs: &PrimaryMap<DefinedFuncIndex, &'data [u8]>,
isa: &isa::TargetIsa,
) -> Result<(Compilation, Relocations), CompileError> {
let mut functions = PrimaryMap::new();
let mut relocations = PrimaryMap::new();
for (i, input) in function_body_inputs.iter() {
let func_index = module.func_index(i);
let mut context = Context::new();
context.func.name = get_func_name(func_index);
context.func.signature = module.signatures[module.functions[func_index]].clone();
let mut trans = FuncTranslator::new();
trans
.translate(
input,
&mut context.func,
&mut FuncEnvironment::new(isa, module),
)
.map_err(CompileError::Wasm)?;
let mut code_buf: Vec<u8> = Vec::new();
let mut reloc_sink = RelocSink::new();
let mut trap_sink = binemit::NullTrapSink {};
context
.compile_and_emit(isa, &mut code_buf, &mut reloc_sink, &mut trap_sink)
.map_err(CompileError::Codegen)?;
functions.push(code_buf);
relocations.push(reloc_sink.func_relocs);
}
Ok((Compilation::new(functions), relocations))
}
#[derive(Fail, Debug)]
pub enum CompileError {
#[fail(display = "WebAssembly translation error: {}", _0)]
Wasm(WasmError),
#[fail(display = "Compilation error: {}", _0)]
Codegen(CodegenError),
}