mrb_parser 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b96059708452f131c2ef1c44c20b13c163b687f9
4
- data.tar.gz: 9df8a740b518b0b6cd628f9c026360213bd1c5a5
3
+ metadata.gz: 5956d0d53530db9bd2e95920a141cd2906befaba
4
+ data.tar.gz: c4354f5264d8e5da124aede37f115e738869f0c5
5
5
  SHA512:
6
- metadata.gz: fdc2cd80c1e26c8b5eee9191c628398d71f757512d2cf202c208ab571af24760609a25b5993b840a9662a262bea230bc8e7bdb7be5c5330782cdb834f51ece9c
7
- data.tar.gz: fe9035d908042c98ebc57d00d37ddc84da200a96cb81524b40118cf5ef7056e53b8a8cb9242cb30b3235604401394a1dfb247084bda47815d809789f0ed98650
6
+ metadata.gz: 33e69c1e59b29b96e17c9cfdcecd96dbea6e438ab095170a0b251c2e8b51ee7b81e6455dc17c74dc02f17279ae707634e197950a5458e7bb9bf133b5a90169ea
7
+ data.tar.gz: 721c5c11a7b37350f86dc8cd9fcb5b89a12be1efe039974adb88f978117e05bc6142dbb16e95ecdb19d0268f206b3da907e1751726b4bc160ed4765859f2f10c
data/README.md CHANGED
@@ -27,6 +27,125 @@ To show more information, with ``-d`` option:
27
27
 
28
28
  $ mrb_parser -d sample.mrb
29
29
 
30
+ Example:
31
+
32
+ $ cat hello.rb
33
+ def hello(msg)
34
+ p "Hello, #{msg}"
35
+ end
36
+
37
+ hello("world")
38
+ $ mrbc -g hello.rb
39
+ $ mrb_parser hello.mrb
40
+ *** BINARY HEADER ***
41
+ secID: RITE
42
+ ver : 0002
43
+ crc : 0xe756 (true)
44
+ size : 0x00000101
45
+ compiler:
46
+ name: MATZ
47
+ ver : 0000
48
+ *** ***
49
+ *** IREP SECTION ***
50
+ secID : IREP
51
+ size : 145
52
+ vm ver: 0000
53
+ *** IREP RECORD ***
54
+ locals: 1
55
+ regs : 3
56
+ iseqs : 7
57
+ code: 00800048
58
+ 000 OP_TCLASS R1
59
+ code: 010000c0
60
+ 001 OP_LAMBDA R2 I(+0) 1
61
+ code: 00800046
62
+ 002 OP_METHOD R1 :hello
63
+ code: 00800006
64
+ 003 OP_LOADSELF R1
65
+ code: 0100003d
66
+ 004 OP_STRING R2 world
67
+ code: 008000a0
68
+ 005 OP_SEND R1 :hello 1
69
+ code: 0000004a
70
+ 006 OP_STOP
71
+ pools : 1
72
+ pool: "world"
73
+ syms : 1
74
+ sym: "hello"
75
+ ireps : 1
76
+ *** IREP RECORD ***
77
+ locals: 3
78
+ regs : 6
79
+ iseqs : 7
80
+ code: 02000026
81
+ 000 OP_ENTER 1:0:0:0:0:0:0
82
+ code: 01800006
83
+ 001 OP_LOADSELF R3
84
+ code: 0200003d
85
+ 002 OP_STRING R4 Hello,
86
+ code: 02804001
87
+ 003 OP_MOVE R5 R1
88
+ code: 0201403e
89
+ 004 OP_STRCAT R4 R5
90
+ code: 018000a0
91
+ 005 OP_SEND R3 :p 1
92
+ code: 01800029
93
+ 006 OP_RETURN R3
94
+ pools : 2
95
+ pool: "Hello, "
96
+ pool: ""
97
+ syms : 1
98
+ sym: "p"
99
+ ireps : 0
100
+ *** ***
101
+ *** ***
102
+ *** ***
103
+ *** DEBUG SECTION ***
104
+ secID: DBG
105
+ size : 82
106
+ files: 1
107
+ filename: hello.rb
108
+ *** DEBUG INFO ***
109
+ count: 31
110
+ files: 1
111
+ *** DEBUG INFO FILE ***
112
+ start: 0
113
+ fname: hello.rb
114
+ line type: 0 (ARY)
115
+ lines: 7
116
+ line: 3
117
+ line: 3
118
+ line: 3
119
+ line: 5
120
+ line: 5
121
+ line: 5
122
+ line: 5
123
+ *** ***
124
+ *** DEBUG INFO ***
125
+ count: 31
126
+ files: 1
127
+ *** DEBUG INFO FILE ***
128
+ start: 0
129
+ fname: hello.rb
130
+ line type: 0 (ARY)
131
+ lines: 7
132
+ line: 3
133
+ line: 2
134
+ line: 2
135
+ line: 2
136
+ line: 2
137
+ line: 2
138
+ line: 2
139
+ *** ***
140
+ *** ***
141
+ *** ***
142
+ *** ***
143
+ *** END SECTION ***
144
+ secID: END
145
+ size : 8
146
+ *** ***
147
+
148
+
30
149
  ## TODO
31
150
 
32
151
  * support LINENO section (current implementation is broken)
@@ -4,24 +4,24 @@ class MrbParser
4
4
  MAXARG_Bx = 0xffff
5
5
  MAXARG_sBx = (MAXARG_Bx>>1) ## `sBx' is signed
6
6
 
7
- OP_NOP = 0 # /* */
8
- OP_MOVE = 1 # /* A B R(A) := R(B) */
9
- OP_LOADL = 2 # /* A Bx R(A) := Lit(Bx) */
10
- OP_LOADI = 3 # /* A sBx R(A) := sBx */
11
- OP_LOADSYM = 4 # /* A Bx R(A) := Sym(Bx) */
12
- OP_LOADNIL = 5 # /* A R(A) := nil */
7
+ OP_NOP = 0 # /* */
8
+ OP_MOVE = 1 # /* A B R(A) := R(B) */
9
+ OP_LOADL = 2 # /* A Bx R(A) := Lit(Bx) */
10
+ OP_LOADI = 3 # /* A sBx R(A) := sBx */
11
+ OP_LOADSYM = 4 # /* A Bx R(A) := Sym(Bx) */
12
+ OP_LOADNIL = 5 # /* A R(A) := nil */
13
13
  OP_LOADSELF = 6 # /* A R(A) := self */
14
- OP_LOADT = 7 # /* A R(A) := true */
15
- OP_LOADF = 8 # /* A R(A) := false */
16
-
17
- OP_GETGLOBAL = 9 # /* A Bx R(A) := getglobal(Sym(Bx)) */
18
- OP_SETGLOBAL =10 # /* A Bx setglobal(Sym(Bx), R(A)) */
19
- OP_GETSPECIAL=11 # /*A Bx R(A) := Special[Bx] */
20
- OP_SETSPECIAL=12 # /*A Bx Special[Bx] := R(A) */
21
- OP_GETIV = 13 # /* A Bx R(A) := ivget(Sym(Bx)) */
22
- OP_SETIV = 14 # /* A Bx ivset(Sym(Bx),R(A)) */
23
- OP_GETCV = 15 # /* A Bx R(A) := cvget(Sym(Bx)) */
24
- OP_SETCV = 16 # /* A Bx cvset(Sym(Bx),R(A)) */
14
+ OP_LOADT = 7 # /* A R(A) := true */
15
+ OP_LOADF = 8 # /* A R(A) := false */
16
+
17
+ OP_GETGLOBAL = 9 # /* A Bx R(A) := getglobal(Sym(Bx)) */
18
+ OP_SETGLOBAL =10 # /* A Bx setglobal(Sym(Bx), R(A)) */
19
+ OP_GETSPECIAL=11 # /* A Bx R(A) := Special[Bx] */
20
+ OP_SETSPECIAL=12 # /* A Bx Special[Bx] := R(A) */
21
+ OP_GETIV = 13 # /* A Bx R(A) := ivget(Sym(Bx)) */
22
+ OP_SETIV = 14 # /* A Bx ivset(Sym(Bx),R(A)) */
23
+ OP_GETCV = 15 # /* A Bx R(A) := cvget(Sym(Bx)) */
24
+ OP_SETCV = 16 # /* A Bx cvset(Sym(Bx),R(A)) */
25
25
  OP_GETCONST = 17 # /* A Bx R(A) := constget(Sym(Bx)) */
26
26
  OP_SETCONST = 18 # /* A Bx constset(Sym(Bx),R(A)) */
27
27
  OP_GETMCNST = 19 # /* A Bx R(A) := R(A)::Sym(B) */
@@ -29,73 +29,73 @@ class MrbParser
29
29
  OP_GETUPVAR = 21 # /* A B C R(A) := uvget(B,C) */
30
30
  OP_SETUPVAR = 22 # /* A B C uvset(B,C,R(A)) */
31
31
 
32
- OP_JMP = 23 # /* sBx pc+=sBx */
33
- OP_JMPIF = 24 # /* A sBx if R(A) pc+=sBx */
34
- OP_JMPNOT = 25 # /* A sBx if !R(A) pc+=sBx */
35
- OP_ONERR = 26 # /* sBx rescue_push(pc+sBx) */
36
- OP_RESCUE = 27 # /* A clear(exc); R(A) := exception (ignore when A=0) */
37
- OP_POPERR = 28 # /* A A.times{rescue_pop()} */
38
- OP_RAISE = 29 # /* A raise(R(A)) */
39
- OP_EPUSH = 30 # /* Bx ensure_push(SEQ[Bx]) */
40
- OP_EPOP = 31 # /* A A.times{ensure_pop().call} */
41
-
42
- OP_SEND = 32 # /* A B C R(A) := call(R(A),mSym(B),R(A+1),...,R(A+C)) */
43
- OP_SENDB = 33 # /* A B C R(A) := call(R(A),mSym(B),R(A+1),...,R(A+C),&R(A+C+1))*/
44
- OP_FSEND = 34 # /* A B C R(A) := fcall(R(A),mSym(B),R(A+1),...,R(A+C-1)) */
45
- OP_CALL = 35 # /* A B C R(A) := self.call(R(A),.., R(A+C)) */
46
- OP_SUPER = 36 # /* A B C R(A) := super(R(A+1),... ,R(A+C-1)) */
47
- OP_ARGARY = 37 # /* A Bx R(A) := argument array (16=6:1:5:4) */
48
- OP_ENTER = 38 # /* Ax arg setup according to flags (24=5:5:1:5:5:1:1) */
49
- OP_KARG = 39 # /* A B C R(A) := kdict[mSym(B)]; if C kdict.rm(mSym(B)) */
50
- OP_KDICT = 40 # /* A C R(A) := kdict */
51
-
52
- OP_RETURN = 41 # /* A B return R(A) (B=normal,in-block return/break) */
32
+ OP_JMP = 23 # /* sBx pc+=sBx */
33
+ OP_JMPIF = 24 # /* A sBx if R(A) pc+=sBx */
34
+ OP_JMPNOT = 25 # /* A sBx if !R(A) pc+=sBx */
35
+ OP_ONERR = 26 # /* sBx rescue_push(pc+sBx) */
36
+ OP_RESCUE = 27 # /* A clear(exc); R(A) := exception (ignore when A=0) */
37
+ OP_POPERR = 28 # /* A A.times{rescue_pop()} */
38
+ OP_RAISE = 29 # /* A raise(R(A)) */
39
+ OP_EPUSH = 30 # /* Bx ensure_push(SEQ[Bx]) */
40
+ OP_EPOP = 31 # /* A A.times{ensure_pop().call} */
41
+
42
+ OP_SEND = 32 # /* A B C R(A) := call(R(A),mSym(B),R(A+1),...,R(A+C)) */
43
+ OP_SENDB = 33 # /* A B C R(A) := call(R(A),mSym(B),R(A+1),...,R(A+C),&R(A+C+1))*/
44
+ OP_FSEND = 34 # /* A B C R(A) := fcall(R(A),mSym(B),R(A+1),...,R(A+C-1)) */
45
+ OP_CALL = 35 # /* A B C R(A) := self.call(R(A),.., R(A+C)) */
46
+ OP_SUPER = 36 # /* A B C R(A) := super(R(A+1),... ,R(A+C-1)) */
47
+ OP_ARGARY = 37 # /* A Bx R(A) := argument array (16=6:1:5:4) */
48
+ OP_ENTER = 38 # /* Ax arg setup according to flags (24=5:5:1:5:5:1:1) */
49
+ OP_KARG = 39 # /* A B C R(A) := kdict[mSym(B)]; if C kdict.rm(mSym(B)) */
50
+ OP_KDICT = 40 # /* A C R(A) := kdict */
51
+
52
+ OP_RETURN = 41 # /* A B return R(A) (B=normal,in-block return/break) */
53
53
  OP_TAILCALL = 42 # /* A B C return call(R(A),mSym(B),*R(C)) */
54
- OP_BLKPUSH = 43 # /* A Bx R(A) := block (16=6:1:5:4) */
55
-
56
- OP_ADD = 44 # /* A B C R(A) := R(A)+R(A+1) (mSyms[B]=:+,C=1) */
57
- OP_ADDI = 45 # /* A B C R(A) := R(A)+C (mSyms[B]=:+) */
58
- OP_SUB = 46 # /* A B C R(A) := R(A)-R(A+1) (mSyms[B]=:-,C=1) */
59
- OP_SUBI = 47 # /* A B C R(A) := R(A)-C (mSyms[B]=:-) */
60
- OP_MUL = 48 # /* A B C R(A) := R(A)*R(A+1) (mSyms[B]=:*,C=1) */
61
- OP_DIV = 49 # /* A B C R(A) := R(A)/R(A+1) (mSyms[B]=:/,C=1) */
62
- OP_EQ = 50 # /* A B C R(A) := R(A)==R(A+1) (mSyms[B]=:==,C=1) */
63
- OP_LT = 51 # /* A B C R(A) := R(A)<R(A+1) (mSyms[B]=:<,C=1) */
64
- OP_LE = 52 # /* A B C R(A) := R(A)<=R(A+1) (mSyms[B]=:<=,C=1) */
65
- OP_GT = 53 # /* A B C R(A) := R(A)>R(A+1) (mSyms[B]=:>,C=1) */
66
- OP_GE = 54 # /* A B C R(A) := R(A)>=R(A+1) (mSyms[B]=:>=,C=1) */
67
-
68
- OP_ARRAY = 55 # /* A B C R(A) := ary_new(R(B),R(B+1)..R(B+C)) */
69
- OP_ARYCAT = 56 # /* A B ary_cat(R(A),R(B)) */
70
- OP_ARYPUSH = 57 # /* A B ary_push(R(A),R(B)) */
71
- OP_AREF = 58 # /* A B C R(A) := R(B)[C] */
72
- OP_ASET = 59 # /* A B C R(B)[C] := R(A) */
73
- OP_APOST = 60 # /* A B C *R(A),R(A+1)..R(A+C) := R(A) */
74
-
75
- OP_STRING = 61 # /* A Bx R(A) := str_dup(Lit(Bx)) */
76
- OP_STRCAT = 62 # /* A B str_cat(R(A),R(B)) */
77
-
78
- OP_HASH = 63 # /* A B C R(A) := hash_new(R(B),R(B+1)..R(B+C)) */
79
- OP_LAMBDA = 64 # /* A Bz Cz R(A) := lambda(SEQ[Bz],Cm) */
80
- OP_RANGE = 65 # /* A B C R(A) := range_new(R(B),R(B+1),C) */
81
-
82
- OP_OCLASS = 66 # /* A R(A) := ::Object */
83
- OP_CLASS = 67 # /* A B R(A) := newclass(R(A),mSym(B),R(A+1)) */
84
- OP_MODULE = 68 # /* A B R(A) := newmodule(R(A),mSym(B)) */
85
- OP_EXEC = 69 # /* A Bx R(A) := blockexec(R(A),SEQ[Bx]) */
86
- OP_METHOD = 70 # /* A B R(A).newmethod(mSym(B),R(A+1)) */
87
- OP_SCLASS = 71 # /* A B R(A) := R(B).singleton_class */
88
- OP_TCLASS = 72 # /* A R(A) := target_class */
89
-
90
- OP_DEBUG = 73 # /* A print R(A) */
91
- OP_STOP = 74 # /* stop VM */
92
- OP_ERR = 75 # /* Bx raise RuntimeError with message Lit(Bx) */
93
-
94
- OP_RSVD1 = 76 # /* reserved instruction #1 */
95
- OP_RSVD2 = 77 # /* reserved instruction #2 */
96
- OP_RSVD3 = 78 # /* reserved instruction #3 */
97
- OP_RSVD4 = 79 # /* reserved instruction #4 */
98
- OP_RSVD5 = 80 # /* reserved instruction #5 */
54
+ OP_BLKPUSH = 43 # /* A Bx R(A) := block (16=6:1:5:4) */
55
+
56
+ OP_ADD = 44 # /* A B C R(A) := R(A)+R(A+1) (mSyms[B]=:+,C=1) */
57
+ OP_ADDI = 45 # /* A B C R(A) := R(A)+C (mSyms[B]=:+) */
58
+ OP_SUB = 46 # /* A B C R(A) := R(A)-R(A+1) (mSyms[B]=:-,C=1) */
59
+ OP_SUBI = 47 # /* A B C R(A) := R(A)-C (mSyms[B]=:-) */
60
+ OP_MUL = 48 # /* A B C R(A) := R(A)*R(A+1) (mSyms[B]=:*,C=1) */
61
+ OP_DIV = 49 # /* A B C R(A) := R(A)/R(A+1) (mSyms[B]=:/,C=1) */
62
+ OP_EQ = 50 # /* A B C R(A) := R(A)==R(A+1) (mSyms[B]=:==,C=1) */
63
+ OP_LT = 51 # /* A B C R(A) := R(A)<R(A+1) (mSyms[B]=:<,C=1) */
64
+ OP_LE = 52 # /* A B C R(A) := R(A)<=R(A+1) (mSyms[B]=:<=,C=1) */
65
+ OP_GT = 53 # /* A B C R(A) := R(A)>R(A+1) (mSyms[B]=:>,C=1) */
66
+ OP_GE = 54 # /* A B C R(A) := R(A)>=R(A+1) (mSyms[B]=:>=,C=1) */
67
+
68
+ OP_ARRAY = 55 # /* A B C R(A) := ary_new(R(B),R(B+1)..R(B+C)) */
69
+ OP_ARYCAT = 56 # /* A B ary_cat(R(A),R(B)) */
70
+ OP_ARYPUSH = 57 # /* A B ary_push(R(A),R(B)) */
71
+ OP_AREF = 58 # /* A B C R(A) := R(B)[C] */
72
+ OP_ASET = 59 # /* A B C R(B)[C] := R(A) */
73
+ OP_APOST = 60 # /* A B C *R(A),R(A+1)..R(A+C) := R(A) */
74
+
75
+ OP_STRING = 61 # /* A Bx R(A) := str_dup(Lit(Bx)) */
76
+ OP_STRCAT = 62 # /* A B str_cat(R(A),R(B)) */
77
+
78
+ OP_HASH = 63 # /* A B C R(A) := hash_new(R(B),R(B+1)..R(B+C)) */
79
+ OP_LAMBDA = 64 # /* A Bz Cz R(A) := lambda(SEQ[Bz],Cm) */
80
+ OP_RANGE = 65 # /* A B C R(A) := range_new(R(B),R(B+1),C) */
81
+
82
+ OP_OCLASS = 66 # /* A R(A) := ::Object */
83
+ OP_CLASS = 67 # /* A B R(A) := newclass(R(A),mSym(B),R(A+1)) */
84
+ OP_MODULE = 68 # /* A B R(A) := newmodule(R(A),mSym(B)) */
85
+ OP_EXEC = 69 # /* A Bx R(A) := blockexec(R(A),SEQ[Bx]) */
86
+ OP_METHOD = 70 # /* A B R(A).newmethod(mSym(B),R(A+1)) */
87
+ OP_SCLASS = 71 # /* A B R(A) := R(B).singleton_class */
88
+ OP_TCLASS = 72 # /* A R(A) := target_class */
89
+
90
+ OP_DEBUG = 73 # /* A print R(A) */
91
+ OP_STOP = 74 # /* stop VM */
92
+ OP_ERR = 75 # /* Bx raise RuntimeError with message Lit(Bx) */
93
+
94
+ OP_RSVD1 = 76 # /* reserved instruction #1 */
95
+ OP_RSVD2 = 77 # /* reserved instruction #2 */
96
+ OP_RSVD3 = 78 # /* reserved instruction #3 */
97
+ OP_RSVD4 = 79 # /* reserved instruction #4 */
98
+ OP_RSVD5 = 80 # /* reserved instruction #5 */
99
99
 
100
100
 
101
101
  OP_R_NORMAL = 0
@@ -86,8 +86,7 @@ class MrbParser
86
86
  printf_indent n, "regs : %d\n", @nregs
87
87
  printf_indent n, "iseqs : %d\n", @iseq.size
88
88
  @iseq.each_with_index do |code, i|
89
- printf_indent n, " code: %08x\n", code
90
- printf_indent n, " "
89
+ printf_indent n, " code: %08x ", code
91
90
  code_dump.dump(code, i)
92
91
  end
93
92
  printf_indent n, "pools : %d\n", @pool.size
@@ -1,3 +1,3 @@
1
1
  class MrbParser
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe MrbParser::CodeDump do
4
+ let(:irep) { double("irep_record") }
5
+ let(:dump) { MrbParser::CodeDump.new(irep) }
6
+
7
+ describe "#opcode" do
8
+ subject { dump.opcode(0x00c00003) }
9
+ it { should == MrbParser::CodeDump::OP_LOADI }
10
+ end
11
+
12
+ describe "#dump" do
13
+ it "get OP_LOADI from code 0x00c00003" do
14
+ tmp = $stdout
15
+ out = StringIO.new
16
+ $stdout = out
17
+ dump.dump(0x00c00003, 1)
18
+ $stdout = tmp
19
+ expect(out.string).to eq "001 OP_LOADI\tR1\t1\n"
20
+ end
21
+ end
22
+
23
+
24
+ end
25
+
data/spec/crc_spec.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'spec_helper'
2
+
3
+ describe MrbParser::CRC do
4
+ describe ".calc_crc_16_ccitt" do
5
+ it "takes valid CRC16 CCITT checksum" do
6
+ data = "123456789"
7
+ expect(MrbParser::CRC.calc_crc_16_ccitt(data,data.size,0)).to eq(48879)
8
+ end
9
+ end
10
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mrb_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - yamanekko
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-02 00:00:00.000000000 Z
11
+ date: 2014-04-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -82,6 +82,8 @@ files:
82
82
  - lib/mrb_parser/utils.rb
83
83
  - lib/mrb_parser/version.rb
84
84
  - mrb_parser.gemspec
85
+ - spec/code_dump_spec.rb
86
+ - spec/crc_spec.rb
85
87
  - spec/spec_helper.rb
86
88
  homepage: https://github.com/yamanekko/mrb_parser
87
89
  licenses:
@@ -103,9 +105,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
103
105
  version: '0'
104
106
  requirements: []
105
107
  rubyforge_project:
106
- rubygems_version: 2.2.0.rc.1
108
+ rubygems_version: 2.2.2
107
109
  signing_key:
108
110
  specification_version: 4
109
111
  summary: mrb (mruby bytecode) parser
110
112
  test_files:
113
+ - spec/code_dump_spec.rb
114
+ - spec/crc_spec.rb
111
115
  - spec/spec_helper.rb
116
+ has_rdoc: