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
extern crate mrubyedge;
use mrubyedge::rite::insn::Fetched;
use mrubyedge::rite::insn::OpCode;
use mrubyedge::yamrb::*;
use value::RSym;
//
// This is a simple example of a new VM from realworld mrbc result.
// file: examples/add.rb
// 1 000 LOADI_1 R1 (1) ; R1:x
// 2 002 LOADI_2 R2 (2) ; R2:y
// 3 004 MOVE R4 R1 ; R1:x
// 3 007 MOVE R5 R2 ; R2:y
// 3 010 ADD R4 R5
// 3 012 SSEND R3 :puts n=1
// 3 016 RETURN R3
// 3 018 STOP
//
fn main() {
let irep = vm::IREP {
__id: 0,
nlocals: 0,
nregs: 7,
rlen: 0,
code: vec![
op::Op {
code: OpCode::LOADI_1,
operand: Fetched::B(1),
pos: 0,
len: 2,
},
op::Op {
code: OpCode::LOADI_2,
operand: Fetched::B(2),
pos: 2,
len: 2,
},
op::Op {
code: OpCode::MOVE,
operand: Fetched::BB(4, 1),
pos: 4,
len: 3,
},
op::Op {
code: OpCode::MOVE,
operand: Fetched::BB(5, 2),
pos: 7,
len: 3,
},
op::Op {
code: OpCode::ADD,
operand: Fetched::B(4),
pos: 10,
len: 2,
},
op::Op {
code: OpCode::SSEND,
operand: Fetched::BBB(3, 0, 1),
pos: 12,
len: 4,
},
op::Op {
code: OpCode::RETURN,
operand: Fetched::B(3),
pos: 16,
len: 2,
},
op::Op {
code: OpCode::STOP,
operand: Fetched::Z,
pos: 18,
len: 1,
},
],
syms: vec![RSym::new("puts".to_string())],
pool: Vec::new(),
reps: Vec::new(),
lv: None,
catch_target_pos: Vec::new(),
};
let mut vm = vm::VM::new_by_raw_irep(irep);
let ret = vm.run().unwrap();
dbg!(ret);
}