Skip to content
This repository was archived by the owner on Jan 21, 2023. It is now read-only.

Commit 969a826

Browse files
committed
split vm
1 parent d1d0cc9 commit 969a826

File tree

2 files changed

+154
-140
lines changed

2 files changed

+154
-140
lines changed

src/main.rs

Lines changed: 4 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -9,147 +9,11 @@ extern crate bimap;
99
//use sha2::Sha256;
1010
//use ripemd160::Ripemd160;
1111

12-
mod compiler;
13-
use crate::compiler::*;
14-
15-
struct VM<'borrow_code_lifetime> {
16-
stack: &'borrow_code_lifetime mut Vec<i32>,
17-
codes: &'borrow_code_lifetime Vec<i32>,
18-
pc: usize,
19-
}
20-
21-
impl<'borrow_code_lifetime> VM<'borrow_code_lifetime> {
22-
fn new(codes: &'borrow_code_lifetime Vec<i32>, stack: &'borrow_code_lifetime mut Vec<i32>, pc: usize) -> VM<'borrow_code_lifetime>{
23-
VM {
24-
codes: codes,
25-
stack: stack,
26-
pc: pc,
27-
}
28-
}
29-
fn dump(&self) {
30-
println!("pc:{}", self.pc);
31-
print!("stack: [");
32-
for value in &mut self.stack.iter() {
33-
print!("{:#x}({}), ", value,value);
34-
}
35-
println!("]");
36-
37-
print!("codes: [");
38-
for code in Compiler::new().uncompile(&self.codes) {
39-
print!("{}, ",code);
40-
}
41-
println!("]");
42-
}
43-
fn run(&mut self) {
44-
while self.codes.len() > self.pc {
45-
let halt = self.step();
46-
if halt == 1 {
47-
break;
48-
}
49-
}
50-
//self.dump();
51-
}
52-
fn step(&mut self) -> i32{
53-
54-
let compiler = Compiler::new();
55-
if self.codes[self.pc] == compiler.compile_single("OP_0") { self.op_pushnumber(0); }
56-
else if self.codes[self.pc] == compiler.compile_single("OP_1NEGATE") { self.op_pushnumber(-1); }
57-
else if self.codes[self.pc] == compiler.compile_single("OP_1") { self.op_pushnumber(1); }
58-
else if self.codes[self.pc] == compiler.compile_single("OP_2") { self.op_pushnumber(2); }
59-
else if self.codes[self.pc] == compiler.compile_single("OP_3") { self.op_pushnumber(3); }
60-
else if self.codes[self.pc] == compiler.compile_single("OP_4") { self.op_pushnumber(4); }
61-
else if self.codes[self.pc] == compiler.compile_single("OP_5") { self.op_pushnumber(5); }
62-
else if self.codes[self.pc] == compiler.compile_single("OP_6") { self.op_pushnumber(6); }
63-
else if self.codes[self.pc] == compiler.compile_single("OP_7") { self.op_pushnumber(7); }
64-
else if self.codes[self.pc] == compiler.compile_single("OP_8") { self.op_pushnumber(8); }
65-
else if self.codes[self.pc] == compiler.compile_single("OP_9") { self.op_pushnumber(9); }
66-
else if self.codes[self.pc] == compiler.compile_single("OP_10") { self.op_pushnumber(10); }
67-
else if self.codes[self.pc] == compiler.compile_single("OP_11") { self.op_pushnumber(11); }
68-
else if self.codes[self.pc] == compiler.compile_single("OP_12") { self.op_pushnumber(12); }
69-
else if self.codes[self.pc] == compiler.compile_single("OP_13") { self.op_pushnumber(13); }
70-
else if self.codes[self.pc] == compiler.compile_single("OP_14") { self.op_pushnumber(14); }
71-
else if self.codes[self.pc] == compiler.compile_single("OP_15") { self.op_pushnumber(15); }
72-
else if self.codes[self.pc] == compiler.compile_single("OP_16") { self.op_pushnumber(16); }
73-
else if self.codes[self.pc] == compiler.compile_single("OP_NOP") { self.op_nop(); }
74-
else if self.codes[self.pc] == compiler.compile_single("OP_DUP") { self.op_dup(); }
75-
else if self.codes[self.pc] == compiler.compile_single("OP_IF") { self.op_if(); }
76-
else if self.codes[self.pc] == compiler.compile_single("OP_ENDIF") { return 1; }
77-
else if self.codes[self.pc] == compiler.compile_single("OP_ELSE") { return 1; }
78-
else if self.codes[self.pc] == compiler.compile_single("OP_HASH160") { self.op_hash160(); }
79-
else { panic!("[VM] The opcode {:#x} is not implemented yet,", self.codes[self.pc]); }
80-
81-
return 0;
82-
}
83-
fn run_nothing(&mut self){
84-
loop {
85-
let compiler = Compiler::new();
86-
if self.codes[self.pc] == compiler.compile_single("OP_IF") { self.op_if(); }
87-
else if self.codes[self.pc] == compiler.compile_single("OP_ENDIF"){ break; }
88-
else if self.codes[self.pc] == compiler.compile_single("OP_ELSE"){ break; }
89-
else { self.pc += 1; }
90-
}
91-
}
92-
fn op_pushnumber(&mut self, num: i32){
93-
self.stack.push(num);
94-
self.pc += 1;
95-
}
96-
fn op_dup(&mut self){
97-
98-
let num: i32 = match self.stack.pop() {
99-
Some(num) => num,
100-
None => panic!("stack is empty."),
101-
};
102-
103-
self.stack.push(num);
104-
self.stack.push(num);
105-
self.pc += 1;
106-
}
107-
fn op_nop(&mut self){
108-
self.pc += 1;
109-
}
110-
fn op_if(&mut self){
111-
112-
let bool = self.stack.pop();
113-
if bool.unwrap() != 0 { // run from OP_IF to OP_ELSE
114-
115-
// run from next to OP_IF until OP_ELSE or OP_END
116-
let mut if_vm = VM::new(self.codes, self.stack, self.pc + 1);
117-
if_vm.run();
12+
pub mod compiler;
13+
pub mod vm;
11814

119-
// check weither OP_ELSE or OP_END
120-
if if_vm.codes[if_vm.pc] == Compiler::new().compile_single("OP_ELSE") {
121-
if_vm.pc += 1; // first opcode next to OP_ELSE
122-
if_vm.run_nothing(); // skip from OP_ELSE to OP_ENDIF
123-
}
124-
self.pc = if_vm.pc + 1; // get pc at next to OP_END or OP_ELSE
125-
126-
} else { // run from OP_ELSE to OP_END
127-
128-
// skip from next to OP_IF until OP_ELSE or OP_END
129-
let mut if_vm = VM::new(self.codes, self.stack, self.pc + 1);
130-
if_vm.run_nothing();
131-
132-
// check weither OP_ELSE or OP_END
133-
if if_vm.codes[if_vm.pc] == Compiler::new().compile_single("OP_ELSE") {
134-
if_vm.pc += 1; // first opcode next to OP_ELSE
135-
if_vm.run(); // skip from OP_IF to OP_ELSE
136-
}
137-
self.pc = if_vm.pc + 1; // get pc at next to OP_END or OP_ELSE
138-
}
139-
140-
//panic!("debug");
141-
}
142-
fn op_hash160(&mut self){
143-
144-
//let value = self.stack.pop().unwrap();
145-
146-
//let sha256hash = Sha256::digest(&value.to_be_bytes());
147-
//let ripemd160hash = Ripemd160::digest(sha256hash.as_slice());
148-
149-
150-
//self.stack.push(ripemd160hash.as_slice());
151-
}
152-
}
15+
use crate::compiler::*;
16+
use crate::vm::*;
15317

15418
fn main() {
15519

src/vm.rs

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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

Comments
 (0)