-
Notifications
You must be signed in to change notification settings - Fork 595
Expand file tree
/
Copy pathcompiler.rs
More file actions
399 lines (375 loc) · 14.2 KB
/
compiler.rs
File metadata and controls
399 lines (375 loc) · 14.2 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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
mod operand_collector;
use crate::{
bit_traits::bits_needed_for,
instructions::{AvmInstruction, AvmOperand},
opcodes::AvmOpcode,
utils::{UnresolvedPCLocation, make_operand, make_unresolved_pc},
};
use fxhash::FxHashMap as HashMap;
use operand_collector::OperandCollector;
use super::parser::{Label, Mnemonic, ParsedOpcode};
pub(crate) use operand_collector::SCRATCH_SPACE_START;
pub(crate) struct CompiledProcedure {
pub instructions: Vec<AvmInstruction>,
// Map of instruction label to local avm pc
pub locations: HashMap<Label, usize>,
// Maps instructions with unresolved PCs to their target label
pub unresolved_jumps: HashMap<UnresolvedPCLocation, Label>,
pub instructions_size: usize,
}
impl CompiledProcedure {
fn add_instruction(&mut self, instruction: AvmInstruction, label: Option<Label>) {
if let Some(label) = label {
self.locations.insert(label, self.instructions_size);
}
self.instructions_size += instruction.size();
self.instructions.push(instruction);
}
fn add_unresolved_jump(&mut self, immediate_index: usize, target: Label) {
self.unresolved_jumps.insert(
UnresolvedPCLocation { instruction_index: self.instructions.len(), immediate_index },
target,
);
}
}
pub(crate) fn compile(parsed_opcodes: Vec<ParsedOpcode>) -> Result<CompiledProcedure, String> {
let mut result = CompiledProcedure {
instructions: Vec::with_capacity(parsed_opcodes.len()),
locations: HashMap::default(),
unresolved_jumps: HashMap::default(),
instructions_size: 0,
};
for parsed_opcode in parsed_opcodes.into_iter() {
let mnemonic = parsed_opcode.mnemonic;
compile_opcode(parsed_opcode, &mut result)
.map_err(|err| format!("Error compiling opcode {:?}: {}", mnemonic, err))?;
}
Ok(result)
}
fn compile_opcode(
parsed_opcode: ParsedOpcode,
result: &mut CompiledProcedure,
) -> Result<(), String> {
let label = parsed_opcode.label.clone();
let mnemonic = parsed_opcode.mnemonic;
let mut collector = OperandCollector::new(parsed_opcode);
match mnemonic {
Mnemonic::ADD
| Mnemonic::SUB
| Mnemonic::MUL
| Mnemonic::FDIV
| Mnemonic::DIV
| Mnemonic::AND
| Mnemonic::OR
| Mnemonic::XOR
| Mnemonic::SHL
| Mnemonic::SHR
| Mnemonic::EQ
| Mnemonic::LT
| Mnemonic::LTE => {
compile_binary_instruction(mnemonic, label, collector, result)?;
}
Mnemonic::SET => {
collector.memory_address_operand()?;
collector.numeric_operand()?;
collector.with_tag()?;
let collection = collector.finish()?;
let dest_address = collection.operands[0];
let immediate_value = collection.immediates[0].unwrap_numeric();
let bits_needed_val = bits_needed_for(&immediate_value);
let bits_needed_mem =
if bits_needed_val >= 16 { 16 } else { bits_needed_for(&dest_address) };
assert!(bits_needed_mem <= 16);
let bits_needed_opcode = bits_needed_val.max(bits_needed_mem);
let set_opcode = match bits_needed_opcode {
8 => AvmOpcode::SET_8,
16 => AvmOpcode::SET_16,
32 => AvmOpcode::SET_32,
64 => AvmOpcode::SET_64,
128 => AvmOpcode::SET_128,
254 => AvmOpcode::SET_FF,
_ => panic!("Invalid bits needed for opcode: {}", bits_needed_opcode),
};
result.add_instruction(
AvmInstruction {
opcode: set_opcode,
addressing_mode: Some(build_addressing_mode(collection.addressing_mode)),
operands: vec![make_operand(bits_needed_mem, &dest_address)],
immediates: vec![make_operand(bits_needed_opcode, &immediate_value)],
tag: collection.tag,
},
label,
);
}
Mnemonic::JUMP => {
collector.label_operand()?;
let collection = collector.finish()?;
result.add_unresolved_jump(0, collection.immediates[0].unwrap_label());
result.add_instruction(
AvmInstruction {
opcode: AvmOpcode::JUMP_32,
immediates: vec![make_unresolved_pc()],
..Default::default()
},
label,
);
}
Mnemonic::JUMPI => {
collector.memory_address_operand()?;
collector.label_operand()?;
let collection = collector.finish()?;
result.add_unresolved_jump(0, collection.immediates[0].unwrap_label());
result.add_instruction(
AvmInstruction {
opcode: AvmOpcode::JUMPI_32,
addressing_mode: Some(build_addressing_mode(collection.addressing_mode)),
operands: vec![make_operand(16, &collection.operands[0])],
immediates: vec![make_unresolved_pc()],
..Default::default()
},
label,
);
}
Mnemonic::NOT => {
collector.memory_address_operand()?;
collector.memory_address_operand()?;
let collection = collector.finish()?;
let bits_needed = collection.operands.iter().map(bits_needed_for).max().unwrap();
assert!(
bits_needed == 8 || bits_needed == 16,
"NOT opcodes only support 8 or 16 bit encodings, got: {}",
bits_needed
);
result.add_instruction(
AvmInstruction {
opcode: if bits_needed == 8 { AvmOpcode::NOT_8 } else { AvmOpcode::NOT_16 },
addressing_mode: Some(build_addressing_mode(collection.addressing_mode)),
operands: collection
.operands
.iter()
.map(|operand| make_operand(bits_needed, operand))
.collect(),
..Default::default()
},
label,
);
}
Mnemonic::CAST => {
collector.memory_address_operand()?;
collector.memory_address_operand()?;
collector.with_tag()?;
let collection = collector.finish()?;
let bits_needed = collection.operands.iter().map(bits_needed_for).max().unwrap();
let avm_opcode = match bits_needed {
8 => AvmOpcode::CAST_8,
16 => AvmOpcode::CAST_16,
_ => {
panic!("CAST only supports 8 and 16 bit encodings, needed {}", bits_needed)
}
};
result.add_instruction(
AvmInstruction {
opcode: avm_opcode,
addressing_mode: Some(build_addressing_mode(collection.addressing_mode)),
operands: collection
.operands
.iter()
.map(|operand| make_operand(bits_needed, operand))
.collect(),
tag: collection.tag,
..Default::default()
},
label,
);
}
Mnemonic::MOV => {
collector.memory_address_operand()?;
collector.memory_address_operand()?;
let collection = collector.finish()?;
let bits_needed = collection.operands.iter().map(bits_needed_for).max().unwrap();
let mov_opcode = match bits_needed {
8 => AvmOpcode::MOV_8,
16 => AvmOpcode::MOV_16,
_ => panic!("MOV operands must fit in 16 bits but needed {}", bits_needed),
};
result.add_instruction(
AvmInstruction {
opcode: mov_opcode,
addressing_mode: Some(build_addressing_mode(collection.addressing_mode)),
operands: collection
.operands
.iter()
.map(|operand| make_operand(bits_needed, operand))
.collect(),
..Default::default()
},
label,
);
}
Mnemonic::INTERNALRETURN => {
collector.finish()?;
result.add_instruction(
AvmInstruction { opcode: AvmOpcode::INTERNALRETURN, ..Default::default() },
label,
);
}
Mnemonic::ECADD => {
collector.memory_address_operand()?; // p1 x
collector.memory_address_operand()?; // p1 y
collector.memory_address_operand()?; // p2 x
collector.memory_address_operand()?; // p2 y
collector.memory_address_operand()?; // result
let collection = collector.finish()?;
result.add_instruction(
AvmInstruction {
opcode: AvmOpcode::ECADD,
addressing_mode: Some(build_addressing_mode(collection.addressing_mode)),
operands: collection
.operands
.into_iter()
.map(|operand| AvmOperand::U16 { value: operand as u16 })
.collect(),
..Default::default()
},
label,
);
}
Mnemonic::TORADIXBE => {
collector.memory_address_operand()?; // input
collector.memory_address_operand()?; // radix
collector.memory_address_operand()?; // num_limbs
collector.memory_address_operand()?; // output_bits
collector.memory_address_operand()?; // output
let collection = collector.finish()?;
result.add_instruction(
AvmInstruction {
opcode: AvmOpcode::TORADIXBE,
addressing_mode: Some(build_addressing_mode(collection.addressing_mode)),
operands: collection
.operands
.into_iter()
.map(|operand| AvmOperand::U16 { value: operand as u16 })
.collect(),
..Default::default()
},
label,
);
}
};
Ok(())
}
fn compile_binary_instruction(
mnemonic: Mnemonic,
label: Option<Label>,
mut collector: OperandCollector,
result: &mut CompiledProcedure,
) -> Result<(), String> {
collector.memory_address_operand()?;
collector.memory_address_operand()?;
collector.memory_address_operand()?;
let collection = collector.finish()?;
let bits_needed = collection.operands.iter().map(bits_needed_for).max().unwrap();
assert!(
bits_needed == 8 || bits_needed == 16,
"Binary opcodes only support 8 or 16 bit encodings, got: {}",
bits_needed
);
let avm_opcode = match mnemonic {
Mnemonic::ADD => match bits_needed {
8 => AvmOpcode::ADD_8,
16 => AvmOpcode::ADD_16,
_ => unreachable!(),
},
Mnemonic::SUB => match bits_needed {
8 => AvmOpcode::SUB_8,
16 => AvmOpcode::SUB_16,
_ => unreachable!(),
},
Mnemonic::MUL => match bits_needed {
8 => AvmOpcode::MUL_8,
16 => AvmOpcode::MUL_16,
_ => unreachable!(),
},
Mnemonic::FDIV => match bits_needed {
8 => AvmOpcode::FDIV_8,
16 => AvmOpcode::FDIV_16,
_ => unreachable!(),
},
Mnemonic::DIV => match bits_needed {
8 => AvmOpcode::DIV_8,
16 => AvmOpcode::DIV_16,
_ => unreachable!(),
},
Mnemonic::AND => match bits_needed {
8 => AvmOpcode::AND_8,
16 => AvmOpcode::AND_16,
_ => unreachable!(),
},
Mnemonic::OR => match bits_needed {
8 => AvmOpcode::OR_8,
16 => AvmOpcode::OR_16,
_ => unreachable!(),
},
Mnemonic::XOR => match bits_needed {
8 => AvmOpcode::XOR_8,
16 => AvmOpcode::XOR_16,
_ => unreachable!(),
},
Mnemonic::SHL => match bits_needed {
8 => AvmOpcode::SHL_8,
16 => AvmOpcode::SHL_16,
_ => unreachable!(),
},
Mnemonic::SHR => match bits_needed {
8 => AvmOpcode::SHR_8,
16 => AvmOpcode::SHR_16,
_ => unreachable!(),
},
Mnemonic::EQ => match bits_needed {
8 => AvmOpcode::EQ_8,
16 => AvmOpcode::EQ_16,
_ => unreachable!(),
},
Mnemonic::LT => match bits_needed {
8 => AvmOpcode::LT_8,
16 => AvmOpcode::LT_16,
_ => unreachable!(),
},
Mnemonic::LTE => match bits_needed {
8 => AvmOpcode::LTE_8,
16 => AvmOpcode::LTE_16,
_ => unreachable!(),
},
_ => unreachable!("Invalid binary opcode: {:?}", mnemonic),
};
result.add_instruction(
AvmInstruction {
opcode: avm_opcode,
addressing_mode: Some(build_addressing_mode(collection.addressing_mode)),
operands: collection
.operands
.iter()
.map(|operand| make_operand(bits_needed, operand))
.collect(),
..Default::default()
},
label,
);
Ok(())
}
fn build_addressing_mode(addressing_mode: Vec<bool>) -> AvmOperand {
let num_operands = addressing_mode.len();
assert!(num_operands <= 8, "Too many operands for building addressing mode bytes");
let mut result = 0;
for (i, is_indirect) in addressing_mode.into_iter().enumerate() {
if is_indirect {
// No relative, so we only operate on even bits
result |= 1 << (i * 2);
}
}
if num_operands <= 4 {
AvmOperand::U8 { value: result as u8 }
} else {
AvmOperand::U16 { value: result as u16 }
}
}