|
@@ -106,10 +106,8 @@ impl Filter {
|
|
|
/// LOADED and LOADED_EXTERNAL register has a unique address.
|
|
|
fn assign_unique_register_addresses(&mut self) -> bool {
|
|
|
let mut has_changed = false;
|
|
|
- let mut counter: usize = self.opcodes.len();
|
|
|
-
|
|
|
+ let mut counter: usize = 0;
|
|
|
let mut register_new_mapping = HashMap::<_, Register>::new();
|
|
|
-
|
|
|
let mut new_mapping = |register_new_mapping: &mut HashMap<Register, Register>,
|
|
|
current: &Register|
|
|
|
-> Register {
|
|
@@ -132,11 +130,16 @@ impl Filter {
|
|
|
| OpCode::CPY(set, reg1)
|
|
|
| OpCode::NEG(set, reg1)
|
|
|
| OpCode::MOV(set, reg1) => {
|
|
|
- *reg1 = register_new_mapping[reg1];
|
|
|
- *set = new_mapping(&mut register_new_mapping, set);
|
|
|
+ *reg1 = register_new_mapping[reg1].clone();
|
|
|
+
|
|
|
+ if let Some(new_set) = register_new_mapping.get(set) {
|
|
|
+ *set = new_set.clone()
|
|
|
+ } else {
|
|
|
+ *set = new_mapping(&mut register_new_mapping, set);
|
|
|
+ }
|
|
|
}
|
|
|
OpCode::HLT(reg1) | OpCode::JEQ(reg1, _) | OpCode::JNE(reg1, _) => {
|
|
|
- *reg1 = register_new_mapping[reg1];
|
|
|
+ *reg1 = register_new_mapping[reg1].clone();
|
|
|
}
|
|
|
OpCode::ADD(set, reg1, reg2)
|
|
|
| OpCode::MUL(set, reg1, reg2)
|
|
@@ -152,9 +155,13 @@ impl Filter {
|
|
|
| OpCode::AND(set, reg1, reg2)
|
|
|
| OpCode::EQ(set, reg1, reg2)
|
|
|
| OpCode::OR(set, reg1, reg2) => {
|
|
|
- *reg1 = register_new_mapping[reg1];
|
|
|
- *reg2 = register_new_mapping[reg2];
|
|
|
- *set = new_mapping(&mut register_new_mapping, set);
|
|
|
+ *reg1 = register_new_mapping[reg1].clone();
|
|
|
+ *reg2 = register_new_mapping[reg2].clone();
|
|
|
+ if let Some(new_set) = register_new_mapping.get(set) {
|
|
|
+ *set = new_set.clone()
|
|
|
+ } else {
|
|
|
+ *set = new_mapping(&mut register_new_mapping, set);
|
|
|
+ }
|
|
|
}
|
|
|
OpCode::LABEL(_) | OpCode::JMP(_) => {}
|
|
|
});
|
|
@@ -301,6 +308,7 @@ mod test {
|
|
|
r#"
|
|
|
WHERE
|
|
|
$foo = 3 + 2 * 4 / 2 * 298210 + $bar
|
|
|
+ AND 5 = 5
|
|
|
"#,
|
|
|
)
|
|
|
.unwrap();
|