Skip to content
This repository has been archived by the owner on Apr 9, 2024. It is now read-only.

feat: Mov Opcode in Brillig #166

Merged
merged 5 commits into from
Mar 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion brillig_bytecode/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,18 @@ impl VM {
Opcode::Call => todo!(),
Opcode::Intrinsics => todo!(),
Opcode::Oracle { inputs, destination } => todo!(),
Opcode::Mov { destination, source } => todo!(),
Opcode::Mov { destination, source } => {
let source_value = self.registers.get(*source);

match destination {
RegisterMemIndex::Register(dest_index) => {
self.registers.set(*dest_index, source_value)
}
_ => return VMStatus::Failure, // TODO: add variants to VMStatus::Failure for more informed failures
}

self.increment_program_counter()
}
Opcode::Trap => VMStatus::Failure,
}
}
Expand Down Expand Up @@ -242,3 +253,27 @@ fn test_jmpifnot_opcode() {
let output_value = registers.get(RegisterMemIndex::Register(RegisterIndex(2)));
assert_eq!(output_value, Value::from(false));
}

#[test]
fn test_mov_opcode() {
let input_registers =
Registers::load(vec![Value::from(1u128), Value::from(2u128), Value::from(3u128)]);

let mov_opcode = Opcode::Mov {
destination: RegisterMemIndex::Register(RegisterIndex(2)),
source: RegisterMemIndex::Register(RegisterIndex(0)),
};

let mut vm = VM::new(input_registers, vec![mov_opcode]);

let status = vm.process_opcode();
assert_eq!(status, VMStatus::Halted);

let registers = vm.finish();

let destination_value = registers.get(RegisterMemIndex::Register(RegisterIndex(2)));
assert_eq!(destination_value, Value::from(1u128));

let source_value = registers.get(RegisterMemIndex::Register(RegisterIndex(0)));
assert_eq!(source_value, Value::from(1u128));
}