Class: Disasm

Inherits:
Object
  • Object
show all
Defined in:
lib/rmarshal/disasm.rb

Instance Method Summary collapse

Constructor Details

#initialize(code, consts, varnames, names) ⇒ Disasm

Returns a new instance of Disasm.



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/rmarshal/disasm.rb', line 4

def initialize(code, consts, varnames, names)
	@code = code
	@consts = consts
	@varnames = varnames
	@names = names
	@insts = []
	@off = 0
	
	while @off != @code.size
		@insts[@insts.size] = parse
	end
end

Instance Method Details

#byteObject



18
19
20
21
22
# File 'lib/rmarshal/disasm.rb', line 18

def byte()
	val = @code[@off].ord
	@off += 1
	val
end

#instsObject



17
# File 'lib/rmarshal/disasm.rb', line 17

def insts() @insts end

#parseObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rmarshal/disasm.rb', line 29

def parse
	off = @off
	opcd = byte
	if not $opcodes.has_key? opcd
		raise "Unknown opcode #{opcd}"
	end
	
	name, arg = $opcodes[opcd]
	if arg == nil
		[off, name]
	else
		val = short
		arg = case arg
				when :const then @consts[val]
				when :name then @names[val]
				when :jrel then @off + val
				when :local then @varnames[val]
				else val
			end
		
		[off, name, arg]
	end
end

#shortObject



23
24
25
26
27
# File 'lib/rmarshal/disasm.rb', line 23

def short()
	val = @code[@off...@off+2].unpack('s')[0]
	@off += 2
	val
end