|
| 1 | +//extern crate sha2; |
| 2 | +//extern crate ripemd160; |
| 3 | +//extern crate digest; |
| 4 | + |
| 5 | +//use std::io; |
| 6 | +//use std::mem; |
| 7 | +//use digest::Digest; |
| 8 | +//use sha2::Sha256; |
| 9 | +//use ripemd160::Ripemd160; |
| 10 | + |
| 11 | +use crate::compiler::*; |
| 12 | + |
| 13 | +pub struct VM<'borrow_code_lifetime> { |
| 14 | + stack: &'borrow_code_lifetime mut Vec<i32>, |
| 15 | + codes: &'borrow_code_lifetime Vec<i32>, |
| 16 | + pc: usize, |
| 17 | +} |
| 18 | + |
| 19 | +impl<'borrow_code_lifetime> VM<'borrow_code_lifetime> { |
| 20 | + pub fn new(codes: &'borrow_code_lifetime Vec<i32>, stack: &'borrow_code_lifetime mut Vec<i32>, pc: usize) -> VM<'borrow_code_lifetime>{ |
| 21 | + VM { |
| 22 | + codes: codes, |
| 23 | + stack: stack, |
| 24 | + pc: pc, |
| 25 | + } |
| 26 | + } |
| 27 | + pub fn dump(&self) { |
| 28 | + println!("pc:{}", self.pc); |
| 29 | + print!("stack: ["); |
| 30 | + for value in &mut self.stack.iter() { |
| 31 | + print!("{:#x}({}), ", value,value); |
| 32 | + } |
| 33 | + println!("]"); |
| 34 | + |
| 35 | + print!("codes: ["); |
| 36 | + for code in Compiler::new().uncompile(&self.codes) { |
| 37 | + print!("{}, ",code); |
| 38 | + } |
| 39 | + println!("]"); |
| 40 | + } |
| 41 | + pub fn run(&mut self) { |
| 42 | + while self.codes.len() > self.pc { |
| 43 | + let halt = self.step(); |
| 44 | + if halt == 1 { |
| 45 | + break; |
| 46 | + } |
| 47 | + } |
| 48 | + //self.dump(); |
| 49 | + } |
| 50 | + pub fn step(&mut self) -> i32{ |
| 51 | + |
| 52 | + let compiler = Compiler::new(); |
| 53 | + if self.codes[self.pc] == compiler.compile_single("OP_0") { self.op_pushnumber(0); } |
| 54 | + else if self.codes[self.pc] == compiler.compile_single("OP_1NEGATE") { self.op_pushnumber(-1); } |
| 55 | + else if self.codes[self.pc] == compiler.compile_single("OP_1") { self.op_pushnumber(1); } |
| 56 | + else if self.codes[self.pc] == compiler.compile_single("OP_2") { self.op_pushnumber(2); } |
| 57 | + else if self.codes[self.pc] == compiler.compile_single("OP_3") { self.op_pushnumber(3); } |
| 58 | + else if self.codes[self.pc] == compiler.compile_single("OP_4") { self.op_pushnumber(4); } |
| 59 | + else if self.codes[self.pc] == compiler.compile_single("OP_5") { self.op_pushnumber(5); } |
| 60 | + else if self.codes[self.pc] == compiler.compile_single("OP_6") { self.op_pushnumber(6); } |
| 61 | + else if self.codes[self.pc] == compiler.compile_single("OP_7") { self.op_pushnumber(7); } |
| 62 | + else if self.codes[self.pc] == compiler.compile_single("OP_8") { self.op_pushnumber(8); } |
| 63 | + else if self.codes[self.pc] == compiler.compile_single("OP_9") { self.op_pushnumber(9); } |
| 64 | + else if self.codes[self.pc] == compiler.compile_single("OP_10") { self.op_pushnumber(10); } |
| 65 | + else if self.codes[self.pc] == compiler.compile_single("OP_11") { self.op_pushnumber(11); } |
| 66 | + else if self.codes[self.pc] == compiler.compile_single("OP_12") { self.op_pushnumber(12); } |
| 67 | + else if self.codes[self.pc] == compiler.compile_single("OP_13") { self.op_pushnumber(13); } |
| 68 | + else if self.codes[self.pc] == compiler.compile_single("OP_14") { self.op_pushnumber(14); } |
| 69 | + else if self.codes[self.pc] == compiler.compile_single("OP_15") { self.op_pushnumber(15); } |
| 70 | + else if self.codes[self.pc] == compiler.compile_single("OP_16") { self.op_pushnumber(16); } |
| 71 | + else if self.codes[self.pc] == compiler.compile_single("OP_NOP") { self.op_nop(); } |
| 72 | + else if self.codes[self.pc] == compiler.compile_single("OP_DUP") { self.op_dup(); } |
| 73 | + else if self.codes[self.pc] == compiler.compile_single("OP_IF") { self.op_if(); } |
| 74 | + else if self.codes[self.pc] == compiler.compile_single("OP_ENDIF") { return 1; } |
| 75 | + else if self.codes[self.pc] == compiler.compile_single("OP_ELSE") { return 1; } |
| 76 | + else if self.codes[self.pc] == compiler.compile_single("OP_HASH160") { self.op_hash160(); } |
| 77 | + else { panic!("[VM] The opcode {:#x} is not implemented yet,", self.codes[self.pc]); } |
| 78 | + |
| 79 | + return 0; |
| 80 | + } |
| 81 | + pub fn run_nothing(&mut self){ |
| 82 | + loop { |
| 83 | + let compiler = Compiler::new(); |
| 84 | + if self.codes[self.pc] == compiler.compile_single("OP_IF") { self.op_if(); } |
| 85 | + else if self.codes[self.pc] == compiler.compile_single("OP_ENDIF"){ break; } |
| 86 | + else if self.codes[self.pc] == compiler.compile_single("OP_ELSE"){ break; } |
| 87 | + else { self.pc += 1; } |
| 88 | + } |
| 89 | + } |
| 90 | + pub fn op_pushnumber(&mut self, num: i32){ |
| 91 | + self.stack.push(num); |
| 92 | + self.pc += 1; |
| 93 | + } |
| 94 | + pub fn op_dup(&mut self){ |
| 95 | + |
| 96 | + let num: i32 = match self.stack.pop() { |
| 97 | + Some(num) => num, |
| 98 | + None => panic!("stack is empty."), |
| 99 | + }; |
| 100 | + |
| 101 | + self.stack.push(num); |
| 102 | + self.stack.push(num); |
| 103 | + self.pc += 1; |
| 104 | + } |
| 105 | + pub fn op_nop(&mut self){ |
| 106 | + self.pc += 1; |
| 107 | + } |
| 108 | + pub fn op_if(&mut self){ |
| 109 | + |
| 110 | + let bool = self.stack.pop(); |
| 111 | + if bool.unwrap() != 0 { // run from OP_IF to OP_ELSE |
| 112 | + |
| 113 | + // run from next to OP_IF until OP_ELSE or OP_END |
| 114 | + let mut if_vm = VM::new(self.codes, self.stack, self.pc + 1); |
| 115 | + if_vm.run(); |
| 116 | + |
| 117 | + // check weither OP_ELSE or OP_END |
| 118 | + if if_vm.codes[if_vm.pc] == Compiler::new().compile_single("OP_ELSE") { |
| 119 | + if_vm.pc += 1; // first opcode next to OP_ELSE |
| 120 | + if_vm.run_nothing(); // skip from OP_ELSE to OP_ENDIF |
| 121 | + } |
| 122 | + self.pc = if_vm.pc + 1; // get pc at next to OP_END or OP_ELSE |
| 123 | + |
| 124 | + } else { // run from OP_ELSE to OP_END |
| 125 | + |
| 126 | + // skip from next to OP_IF until OP_ELSE or OP_END |
| 127 | + let mut if_vm = VM::new(self.codes, self.stack, self.pc + 1); |
| 128 | + if_vm.run_nothing(); |
| 129 | + |
| 130 | + // check weither OP_ELSE or OP_END |
| 131 | + if if_vm.codes[if_vm.pc] == Compiler::new().compile_single("OP_ELSE") { |
| 132 | + if_vm.pc += 1; // first opcode next to OP_ELSE |
| 133 | + if_vm.run(); // skip from OP_IF to OP_ELSE |
| 134 | + } |
| 135 | + self.pc = if_vm.pc + 1; // get pc at next to OP_END or OP_ELSE |
| 136 | + } |
| 137 | + |
| 138 | + //panic!("debug"); |
| 139 | + } |
| 140 | + pub fn op_hash160(&mut self){ |
| 141 | + |
| 142 | + //let value = self.stack.pop().unwrap(); |
| 143 | + |
| 144 | + //let sha256hash = Sha256::digest(&value.to_be_bytes()); |
| 145 | + //let ripemd160hash = Ripemd160::digest(sha256hash.as_slice()); |
| 146 | + |
| 147 | + |
| 148 | + //self.stack.push(ripemd160hash.as_slice()); |
| 149 | + } |
| 150 | +} |
0 commit comments