rubyx-debugger/app/main/controllers/interpreter.rb

167 lines
4.1 KiB
Ruby
Raw Normal View History

2015-07-23 12:15:44 +02:00
2015-07-29 19:50:39 +02:00
require_relative "eventable"
2015-07-23 12:15:44 +02:00
class Interpreter
2015-07-26 17:27:12 +02:00
# fire events for changed pc and register contents
include Eventable
2015-07-23 12:15:44 +02:00
2015-07-26 17:27:12 +02:00
# current instruction or pc
attr_reader :instruction
# an (arm style) link register. store the return address to return to
attr_reader :link
# current executing block. since this is not a hardware simulator this is luxury
attr_reader :block
# the registers, 12
2015-07-26 12:59:58 +02:00
attr_reader :registers
2015-07-26 17:27:12 +02:00
# collect the output
2015-07-26 12:59:58 +02:00
attr_reader :stdout
2015-07-23 12:15:44 +02:00
2015-07-28 15:16:09 +02:00
attr_reader :state
2015-07-23 12:15:44 +02:00
def initialize
2015-07-28 15:16:09 +02:00
@state = "runnnig"
2015-07-26 12:59:58 +02:00
@stdout = ""
2015-07-24 16:21:13 +02:00
@registers = {}
2015-07-29 15:39:15 +02:00
(0...16).each do |r|
set_register "r#{r}".to_sym , "r#{r}:unknown"
2015-07-24 16:21:13 +02:00
end
2015-07-23 12:15:44 +02:00
end
def start bl
set_block bl
end
def set_block bl
return if @block == bl
raise "Error, nil block" unless bl
old = @block
@block = bl
2015-07-26 08:04:57 +02:00
trigger(:block_changed , old , bl)
set_instruction bl.codes.first
end
def set_instruction i
2015-07-28 15:16:09 +02:00
@state = "exited" unless i
return if @instruction == i
old = @instruction
@instruction = i
trigger(:instruction_changed, old , i)
end
def get_register( reg )
2015-07-24 16:21:13 +02:00
reg = reg.symbol if reg.is_a? Register::RegisterReference
raise "Not a register #{reg}" unless Register::RegisterReference.look_like_reg(reg)
@registers[reg]
end
def set_register reg , val
old = get_register( reg ) # also ensures format
return if old === val
2015-07-24 16:21:13 +02:00
reg = reg.symbol if reg.is_a? Register::RegisterReference
@registers[reg] = val
trigger(:register_changed, reg , old , val)
end
def tick
2015-07-26 17:27:12 +02:00
return unless @instruction
name = @instruction.class.name.split("::").last
fetch = send "execute_#{name}"
return unless fetch
fetch_next_intruction
end
def fetch_next_intruction
if(@instruction != @block.codes.last)
set_instruction @block.codes[ @block.codes.index(@instruction) + 1]
else
next_b = @block.method.source.blocks.index(@block) + 1
set_block @block.method.source.blocks[next_b]
end
end
2015-07-24 20:15:47 +02:00
def object_for reg
id = get_register(reg)
2015-07-26 12:59:58 +02:00
Virtual.machine.objects[id]
2015-07-24 20:15:47 +02:00
end
2015-07-26 12:59:58 +02:00
2015-07-24 20:15:47 +02:00
# Instruction interpretation starts here
def execute_Branch
target = @instruction.block
set_block target
false
end
2015-07-26 08:04:57 +02:00
def execute_LoadConstant
to = @instruction.register
value = @instruction.constant.object_id
set_register( to , value )
true
end
2015-07-26 08:04:57 +02:00
2015-07-24 20:15:47 +02:00
def execute_GetSlot
object = object_for( @instruction.array )
value = object.internal_object_get( @instruction.index )
2015-07-26 08:04:57 +02:00
value = value.object_id unless value.is_a? Integer
2015-07-24 20:15:47 +02:00
set_register( @instruction.register , value )
true
end
2015-07-26 08:04:57 +02:00
2015-07-24 20:15:47 +02:00
def execute_SetSlot
2015-07-26 12:59:58 +02:00
value = object_for( @instruction.register )
object = object_for( @instruction.array )
2015-07-24 20:15:47 +02:00
object.internal_object_set( @instruction.index , value )
2015-07-30 13:00:49 +02:00
trigger(:object_changed, @instruction.array )
2015-07-24 20:15:47 +02:00
true
end
2015-07-26 08:04:57 +02:00
def execute_RegisterTransfer
value = get_register @instruction.from
set_register @instruction.to , value
true
end
def execute_FunctionCall
@link = [@block , @instruction]
next_block = @instruction.method.source.blocks.first
set_block next_block
false
end
def execute_SaveReturn
object = object_for @instruction.register
2015-07-26 12:59:58 +02:00
raise "save return has nothing to save" unless @link
2015-07-29 15:39:15 +02:00
trigger(:object_changed, @instruction.register )
2015-07-26 08:04:57 +02:00
object.internal_object_set @instruction.index , @link
true
end
2015-07-26 12:59:58 +02:00
def execute_Syscall
name = @instruction.name
case name
when :putstring
str = object_for( :r1 ) # should test length, ie r2
raise "NO string for putstring #{str}" unless str.is_a? Symbol
@stdout += str.to_s
2015-07-26 17:27:12 +02:00
when :exit
2015-07-28 15:16:09 +02:00
set_instruction(nil)
2015-07-26 17:27:12 +02:00
return false
2015-07-26 12:59:58 +02:00
else
raise "un-implemented syscall #{name}"
end
true
end
2015-07-26 17:27:12 +02:00
def execute_FunctionReturn
object = object_for( @instruction.register )
#wouldn't need to assign to link, but makes tsting easier
@link = object.internal_object_get( @instruction.index )
@block , @instruction = @link
# we jump back to the call instruction. so it is as if the call never happened and we continue
true
end
2015-07-23 12:15:44 +02:00
end