-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
295 lines (292 loc) · 10.5 KB
/
lib.rs
File metadata and controls
295 lines (292 loc) · 10.5 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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
use cranelift_codegen::entity::EntityRef;
use cranelift_codegen::ir::entities::FuncRef;
use cranelift_codegen::ir::function::DisplayFunction;
use cranelift_codegen::ir::immediates::Offset32;
use cranelift_codegen::ir::types::*;
use cranelift_codegen::ir::AbiParam;
use cranelift_codegen::ir::MemFlags;
use cranelift_codegen::ir::{Function, InstBuilder, Signature, UserFuncName, Value};
use cranelift_codegen::isa::CallConv;
use cranelift_codegen::settings;
use cranelift_codegen::verifier::verify_function;
use cranelift_frontend::*;
use cranelift_frontend::{FunctionBuilder, FunctionBuilderContext};
use cranelift_module::Module;
use datatable::DataTable;
use oir::Oir;
use perror::*;
use scopetable::ScopeTable;
use symtable::SymTable;
use types::*;
// Function Intermediate Representation
pub struct Fir {
variables: u32,
sym: SymTable,
}
impl Fir {
pub fn refresh(&mut self) -> () {
self.variables = 0;
self.sym = SymTable::new()
}
pub fn new(variables: u32, sym: SymTable) -> Self {
Fir { variables, sym }
}
pub fn run(
&mut self,
func_def: &FunctionInitialize,
ctx: &mut FunctionBuilderContext,
namespace: u32,
index: u32,
dtbl: &DataTable,
scopes: &Vec<ScopeTable>,
types: &Vec<TypeTree>,
oir: &mut Oir,
) -> Function {
let sig = Signature::new(CallConv::Cold);
let name = UserFuncName::user(namespace, index);
// todo:: types need to be worked out, params and returns defined
let mut func = Function::with_name_signature(name, sig);
let mut builder = FunctionBuilder::new(&mut func, ctx);
let root_block = builder.create_block();
// todo:: this is the issue with function arguments not working simple repr add case
func_def.args.iter().for_each(|x| {
let z = self
.recurse(*x, &mut builder, dtbl, scopes, types, oir)
.unwrap();
builder.func.signature.params.push(AbiParam::new(I64));
//let res = builder.block_params(root_block)[z.as_u32() as usize];
});
builder.func.signature.returns.push(AbiParam::new(I64));
builder.append_block_params_for_function_params(root_block);
builder.switch_to_block(root_block);
let _result = self.recurse(func_def.block, &mut builder, dtbl, scopes, types, oir);
builder.seal_block(root_block);
builder.finalize();
func
}
pub fn handle_arg_init(
&mut self,
op: &SymbolInit,
builder: &mut FunctionBuilder,
dtbl: &DataTable,
scopes: &Vec<ScopeTable>,
types: &Vec<TypeTree>,
oir: &mut Oir,
) -> ResultFir<Variable> {
let result = self.add_var();
self.sym.table.insert(op.ident.clone(), result.as_u32());
Ok(result)
}
pub fn handle_const_init(
&mut self,
op: &Initialization,
builder: &mut FunctionBuilder,
dtbl: &DataTable,
scopes: &Vec<ScopeTable>,
types: &Vec<TypeTree>,
oir: &mut Oir,
) -> ResultFir<Variable> {
let result = self.add_var();
builder.declare_var(result, I64);
let temp = self
.recurse(op.right, builder, dtbl, scopes, types, oir)
.unwrap();
// todo:: optimization: not all paths need declare var if value is only ever read. or something similar, this statement is in the same ballpark, but might not be totally correct
let x = builder.use_var(temp);
let tt = types.get(op.left as usize).unwrap();
self.sym
.table
.insert(tt.into_symbol_init().ident.clone(), temp.as_u32());
builder.def_var(temp, x);
Ok(temp)
}
pub fn handle_invoke(
&mut self,
op: &Invoke,
builder: &mut FunctionBuilder,
dtbl: &DataTable,
scopes: &Vec<ScopeTable>,
types: &Vec<TypeTree>,
oir: &mut Oir,
) -> ResultFir<Variable> {
let args: Vec<Value> = op
.args
.iter()
.map(|x| {
let result = self.recurse(*x, builder, dtbl, scopes, types, oir).unwrap();
return builder.use_var(result).clone();
})
.collect::<Vec<Value>>();
// todo:: get this correct with funcref. on how to get this from slt?
let call = builder.ins().call(FuncRef::from_u32(0), args.as_slice());
let result = self.add_var();
builder.declare_var(result, I64);
builder.def_var(result, builder.inst_results(call)[0]);
Ok(result)
}
pub fn handle_block(
&mut self,
op: &Block,
builder: &mut FunctionBuilder,
dtbl: &DataTable,
scopes: &Vec<ScopeTable>,
types: &Vec<TypeTree>,
oir: &mut Oir,
) -> ResultFir<Variable> {
let temp: Vec<Variable> = op
.exprs
.iter()
.map(|x| {
return self.recurse(*x, builder, dtbl, scopes, types, oir).unwrap();
})
.collect();
Ok(*temp.last().unwrap())
}
pub fn handle_ret_void(&mut self, builder: &mut FunctionBuilder) -> ResultFir<Variable> {
builder.ins().return_(&[]);
Ok(Variable::from_u32(0))
}
pub fn handle_ret(
&mut self,
op: &UnaryOp,
builder: &mut FunctionBuilder,
dtbl: &DataTable,
scopes: &Vec<ScopeTable>,
types: &Vec<TypeTree>,
oir: &mut Oir,
) -> ResultFir<Variable> {
let temp = self
.recurse(op.val, builder, dtbl, scopes, types, oir)
.unwrap();
let arg = builder.use_var(temp);
builder.ins().return_(&[arg]);
Ok(temp)
}
pub fn handle_sym_access(
&mut self,
op: &SymbolAccess,
dtbl: &DataTable,
scopes: &Vec<ScopeTable>,
types: &Vec<TypeTree>,
oir: &mut Oir,
builder: &mut FunctionBuilder,
) -> ResultFir<Variable> {
let sym = self.sym.table.get(&op.ident);
if let Some(s) = sym {
return Ok(Variable::from_u32(*s));
}
let id = dtbl.table.get(&op.ident).unwrap();
let gv = oir.obj_mod.declare_data_in_func(*id, builder.func);
let val = builder.ins().global_value(I64, gv);
let result = self.add_var();
builder.declare_var(result, I64);
let mem = MemFlags::new();
let loaded = builder.ins().load(I64, mem, val, Offset32::new(0));
builder.def_var(result, loaded);
Ok(result)
}
pub fn handle_u64(&mut self, num: u64, builder: &mut FunctionBuilder) -> ResultFir<Variable> {
let result = self.add_var();
builder.declare_var(result, I64);
let temp = builder
.ins()
.iconst(I64, i64::from_ne_bytes(num.to_ne_bytes()));
builder.def_var(result, temp);
Ok(result)
}
pub fn handle_i64(&mut self, num: i64, builder: &mut FunctionBuilder) -> ResultFir<Variable> {
let result = self.add_var();
builder.declare_var(result, I64);
let temp = builder.ins().iconst(I64, num);
builder.def_var(result, temp);
Ok(result)
}
pub fn handle_minus(
&mut self,
num: &BinaryOp,
builder: &mut FunctionBuilder,
dtbl: &DataTable,
scopes: &Vec<ScopeTable>,
types: &Vec<TypeTree>,
oir: &mut Oir,
) -> ResultFir<Variable> {
let result = self.add_var();
builder.declare_var(result, I64);
let left = self
.recurse(num.left, builder, dtbl, scopes, types, oir)
.unwrap();
let right = self
.recurse(num.right, builder, dtbl, scopes, types, oir)
.unwrap();
let arg1 = builder.use_var(left);
let arg2 = builder.use_var(right);
let temp = builder.ins().isub(arg1, arg2);
builder.def_var(result, temp);
Ok(result)
}
pub fn handle_plus(
&mut self,
num: &BinaryOp,
builder: &mut FunctionBuilder,
dtbl: &DataTable,
scopes: &Vec<ScopeTable>,
types: &Vec<TypeTree>,
oir: &mut Oir,
) -> ResultFir<Variable> {
let result = self.add_var();
builder.declare_var(result, I64);
let left = self
.recurse(num.left, builder, dtbl, scopes, types, oir)
.unwrap();
let right = self
.recurse(num.right, builder, dtbl, scopes, types, oir)
.unwrap();
let arg1 = builder.use_var(left);
let arg2 = builder.use_var(right);
let temp = builder.ins().iadd(arg1, arg2);
builder.def_var(result, temp);
Ok(result)
}
pub fn recurse(
&mut self,
idx: TypeTreeIndex,
builder: &mut FunctionBuilder,
dtbl: &DataTable,
scopes: &Vec<ScopeTable>,
types: &Vec<TypeTree>,
oir: &mut Oir,
) -> ResultFir<Variable> {
let expr = types.get(idx as usize).unwrap();
match expr {
TypeTree::Block(op) => self.handle_block(&op, builder, dtbl, scopes, types, oir),
TypeTree::Invoke(op) => self.handle_invoke(&op, builder, dtbl, scopes, types, oir),
TypeTree::Plus(op) => self.handle_plus(&op, builder, dtbl, scopes, types, oir),
TypeTree::Minus(op) => self.handle_minus(&op, builder, dtbl, scopes, types, oir),
TypeTree::Return(op) => self.handle_ret(&op, builder, dtbl, scopes, types, oir),
TypeTree::ReturnVoid(_) => self.handle_ret_void(builder),
TypeTree::ConstInit(op) => {
self.handle_const_init(&op, builder, dtbl, scopes, types, oir)
}
TypeTree::ArgInit(op) => self.handle_arg_init(&op, builder, dtbl, scopes, types, oir),
TypeTree::SymbolAccess(op) => {
self.handle_sym_access(&op, dtbl, scopes, types, oir, builder)
}
TypeTree::U64(op) => self.handle_u64(*op, builder),
TypeTree::I64(op) => self.handle_i64(*op, builder),
_ => panic!("developer error unexpected expression {:?}", expr),
}
}
pub fn get_ir(self, func: &Function) -> Result<DisplayFunction> {
let flags = settings::Flags::new(settings::builder());
let res = verify_function(func, &flags);
match res {
Err(error) => panic!("get_ir: {}", error),
_ => Ok(func.display()),
}
}
pub fn add_var(&mut self) -> Variable {
let temp = Variable::new(usize::try_from(self.variables).unwrap());
self.variables += 1;
temp
}
}