Class: MrbParser::IrepRecord

Inherits:
Object
  • Object
show all
Defined in:
lib/mrb_parser/irep_record.rb

Constant Summary collapse

IREP_TT_STRING =
0
IREP_TT_FIXNUM =
1
IREP_TT_FLOAT =
2
MRB_DUMP_NULL_SYM_LEN =
0xFFFF

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeIrepRecord

Returns a new instance of IrepRecord.



17
18
# File 'lib/mrb_parser/irep_record.rb', line 17

def initialize
end

Instance Attribute Details

#debug_infoObject

Returns the value of attribute debug_info.



12
13
14
# File 'lib/mrb_parser/irep_record.rb', line 12

def debug_info
  @debug_info
end

#poolObject (readonly)

Returns the value of attribute pool.



15
16
17
# File 'lib/mrb_parser/irep_record.rb', line 15

def pool
  @pool
end

#recsObject (readonly)

Returns the value of attribute recs.



13
14
15
# File 'lib/mrb_parser/irep_record.rb', line 13

def recs
  @recs
end

#symsObject (readonly)

Returns the value of attribute syms.



14
15
16
# File 'lib/mrb_parser/irep_record.rb', line 14

def syms
  @syms
end

Instance Method Details

#dump(n = 2) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/mrb_parser/irep_record.rb', line 82

def dump(n = 2)
  code_dump = MrbParser::CodeDump.new(self)
  printf_indent n, "*** IREP RECORD ***\n"
  printf_indent n, "locals: %d\n", @nlocals
  printf_indent n, "regs  : %d\n", @nregs
  printf_indent n, "iseqs : %d\n", @iseq.size
  @iseq.each_with_index do |code, i|
    printf_indent n, "  code: %08x ", code
    code_dump.dump(code, i)
  end
  printf_indent n, "pools : %d\n", @pool.size
  @pool.each do |pool|
    printf_indent n, "  pool: %s\n", pool.inspect
  end
  printf_indent n, "syms  : %d\n", @syms.size
  @syms.each do |item|
    printf_indent n, "  sym: %s\n", item.inspect
  end
  printf_indent n, "ireps : %d\n", @recs.size
  @recs.each do |rec|
    rec.dump(n+2)
  end
  printf_indent n, "*** ***\n"
end

#parse_iseq(parser) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/mrb_parser/irep_record.rb', line 36

def parse_iseq(parser)
  ilen = parser.read_uint32
  @iseq = []
  ilen.times do |i|
    @iseq << parser.read_uint32
  end
end

#parse_pool(parser) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/mrb_parser/irep_record.rb', line 44

def parse_pool(parser)
  plen = parser.read_uint32
  @pool = []
  plen.times do |i|
    type = parser.read_uint8
    str = parser.read_n16string
    case type
    when IREP_TT_FIXNUM
      @pool[i] = Integer(str)
    when IREP_TT_FLOAT
      @pool[i] = Float(str)
    when IREP_TT_STRING
      @pool[i] = str
    else
      @pool[i] = nil
    end
  end
end

#parse_record(parser) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/mrb_parser/irep_record.rb', line 20

def parse_record(parser)
  @section_size = parser.read_uint32
  @nlocals = parser.read_uint16
  @nregs = parser.read_uint16
  rlen = parser.read_uint16
  parse_iseq(parser)
  parse_pool(parser)
  parse_symbol(parser)
  @recs = []
  rlen.times do |i|
    rec = IrepRecord.new()
    @recs[i] = rec.parse_record(parser)
  end
  self
end

#parse_symbol(parser) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/mrb_parser/irep_record.rb', line 63

def parse_symbol(parser)
  slen = parser.read_uint32
  @syms = []
  slen.times do |i|
    len = parser.read_uint16
    if len == MRB_DUMP_NULL_SYM_LEN
      @syms[i] = nil
      next
    end
    @syms[i] = parser.read_chars(len)
    terminater = parser.read_uint8 ## skip NULL byte
  end
end

#printf_indent(n, *args) ⇒ Object



77
78
79
80
# File 'lib/mrb_parser/irep_record.rb', line 77

def printf_indent(n, *args)
  print " "*n
  printf *args
end