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
use cranelift_codegen::binemit::{Addend, CodeOffset};
use cranelift_codegen::entity::PrimaryMap;
use cranelift_codegen::ir;
use std::boxed::Box;
use std::vec::Vec;
#[derive(PartialEq, Eq, Debug)]
pub enum Init {
Uninitialized,
Zeros {
size: usize,
},
Bytes {
contents: Box<[u8]>,
},
}
impl Init {
pub fn size(&self) -> usize {
match *self {
Self::Uninitialized => panic!("data size not initialized yet"),
Self::Zeros { size } => size,
Self::Bytes { ref contents } => contents.len(),
}
}
}
pub struct DataDescription {
pub init: Init,
pub function_decls: PrimaryMap<ir::FuncRef, ir::ExternalName>,
pub data_decls: PrimaryMap<ir::GlobalValue, ir::ExternalName>,
pub function_relocs: Vec<(CodeOffset, ir::FuncRef)>,
pub data_relocs: Vec<(CodeOffset, ir::GlobalValue, Addend)>,
}
pub struct DataContext {
description: DataDescription,
}
impl DataContext {
pub fn new() -> Self {
Self {
description: DataDescription {
init: Init::Uninitialized,
function_decls: PrimaryMap::new(),
data_decls: PrimaryMap::new(),
function_relocs: vec![],
data_relocs: vec![],
},
}
}
pub fn clear(&mut self) {
self.description.init = Init::Uninitialized;
self.description.function_decls.clear();
self.description.data_decls.clear();
self.description.function_relocs.clear();
self.description.data_relocs.clear();
}
pub fn define_zeroinit(&mut self, size: usize) {
debug_assert_eq!(self.description.init, Init::Uninitialized);
self.description.init = Init::Zeros { size };
}
pub fn define(&mut self, contents: Box<[u8]>) {
debug_assert_eq!(self.description.init, Init::Uninitialized);
self.description.init = Init::Bytes { contents };
}
pub fn import_function(&mut self, name: ir::ExternalName) -> ir::FuncRef {
self.description.function_decls.push(name)
}
pub fn import_global_value(&mut self, name: ir::ExternalName) -> ir::GlobalValue {
self.description.data_decls.push(name)
}
pub fn write_function_addr(&mut self, offset: CodeOffset, func: ir::FuncRef) {
self.description.function_relocs.push((offset, func))
}
pub fn write_data_addr(&mut self, offset: CodeOffset, data: ir::GlobalValue, addend: Addend) {
self.description.data_relocs.push((offset, data, addend))
}
pub fn description(&self) -> &DataDescription {
debug_assert!(
self.description.init != Init::Uninitialized,
"data must be initialized first"
);
&self.description
}
}
#[cfg(test)]
mod tests {
use super::{DataContext, Init};
use cranelift_codegen::ir;
#[test]
fn basic_data_context() {
let mut data_ctx = DataContext::new();
{
let description = &data_ctx.description;
assert_eq!(description.init, Init::Uninitialized);
assert!(description.function_decls.is_empty());
assert!(description.data_decls.is_empty());
assert!(description.function_relocs.is_empty());
assert!(description.data_relocs.is_empty());
}
data_ctx.define_zeroinit(256);
let _func_a = data_ctx.import_function(ir::ExternalName::user(0, 0));
let func_b = data_ctx.import_function(ir::ExternalName::user(0, 1));
let func_c = data_ctx.import_function(ir::ExternalName::user(1, 0));
let _data_a = data_ctx.import_global_value(ir::ExternalName::user(2, 2));
let data_b = data_ctx.import_global_value(ir::ExternalName::user(2, 3));
data_ctx.write_function_addr(8, func_b);
data_ctx.write_function_addr(16, func_c);
data_ctx.write_data_addr(32, data_b, 27);
{
let description = data_ctx.description();
assert_eq!(description.init, Init::Zeros { size: 256 });
assert_eq!(description.function_decls.len(), 3);
assert_eq!(description.data_decls.len(), 2);
assert_eq!(description.function_relocs.len(), 2);
assert_eq!(description.data_relocs.len(), 1);
}
data_ctx.clear();
{
let description = &data_ctx.description;
assert_eq!(description.init, Init::Uninitialized);
assert!(description.function_decls.is_empty());
assert!(description.data_decls.is_empty());
assert!(description.function_relocs.is_empty());
assert!(description.data_relocs.is_empty());
}
let contents = vec![33, 34, 35, 36];
let contents_clone = contents.clone();
data_ctx.define(contents.into_boxed_slice());
{
let description = data_ctx.description();
assert_eq!(
description.init,
Init::Bytes {
contents: contents_clone.into_boxed_slice()
}
);
assert_eq!(description.function_decls.len(), 0);
assert_eq!(description.data_decls.len(), 0);
assert_eq!(description.function_relocs.len(), 0);
assert_eq!(description.data_relocs.len(), 0);
}
}
}