2015-07-30 18:18:12 +02:00
|
|
|
|
|
|
|
require_relative "eventable"
|
|
|
|
|
|
|
|
module Interpreter
|
|
|
|
class Interpreter
|
|
|
|
# fire events for changed pc and register contents
|
|
|
|
include Eventable
|
|
|
|
|
|
|
|
# current instruction or pc
|
|
|
|
attr_reader :instruction
|
|
|
|
|
|
|
|
# current instruction or pc
|
|
|
|
attr_reader :clock
|
|
|
|
|
|
|
|
# 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, 16
|
|
|
|
attr_reader :registers
|
|
|
|
|
|
|
|
# collect the output
|
|
|
|
attr_reader :stdout
|
|
|
|
|
|
|
|
attr_reader :state
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
@state = "runnnig"
|
|
|
|
@stdout = ""
|
|
|
|
@registers = {}
|
|
|
|
@clock = 0
|
2015-08-08 23:52:27 +02:00
|
|
|
(0...12).each do |r|
|
2015-07-30 18:18:12 +02:00
|
|
|
set_register "r#{r}".to_sym , "r#{r}:unknown"
|
|
|
|
end
|
|
|
|
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
|
|
|
|
trigger(:block_changed , old , bl)
|
|
|
|
set_instruction bl.codes.first
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_instruction i
|
|
|
|
@state = "exited" unless i
|
|
|
|
return if @instruction == i
|
|
|
|
old = @instruction
|
|
|
|
@instruction = i
|
|
|
|
trigger(:instruction_changed, old , i)
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_register( reg )
|
2015-10-10 20:38:55 +02:00
|
|
|
reg = reg.symbol if reg.is_a? Register::RegisterValue
|
|
|
|
raise "Not a register #{reg}" unless Register::RegisterValue.look_like_reg(reg)
|
2015-07-30 18:18:12 +02:00
|
|
|
@registers[reg]
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_register reg , val
|
|
|
|
old = get_register( reg ) # also ensures format
|
|
|
|
return if old === val
|
2015-10-10 20:38:55 +02:00
|
|
|
reg = reg.symbol if reg.is_a? Register::RegisterValue
|
2015-07-30 18:18:12 +02:00
|
|
|
@registers[reg] = val
|
|
|
|
trigger(:register_changed, reg , old , val)
|
|
|
|
end
|
|
|
|
|
|
|
|
def tick
|
|
|
|
return unless @instruction
|
|
|
|
@clock += 1
|
|
|
|
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
|
|
|
|
|
|
|
|
def object_for reg
|
|
|
|
id = get_register(reg)
|
|
|
|
Virtual.machine.objects[id]
|
|
|
|
end
|
|
|
|
|
|
|
|
# Instruction interpretation starts here
|
|
|
|
def execute_Branch
|
|
|
|
target = @instruction.block
|
|
|
|
set_block target
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2015-10-07 09:05:34 +02:00
|
|
|
def execute_IsZeroBranch
|
|
|
|
puts @instruction.inspect
|
|
|
|
target = @instruction.block
|
|
|
|
set_block target
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2015-07-30 18:18:12 +02:00
|
|
|
def execute_LoadConstant
|
|
|
|
to = @instruction.register
|
2015-08-08 23:52:27 +02:00
|
|
|
value = @instruction.constant
|
|
|
|
value = value.object_id unless value.is_a?(Fixnum)
|
2015-07-30 18:18:12 +02:00
|
|
|
set_register( to , value )
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute_GetSlot
|
|
|
|
object = object_for( @instruction.array )
|
|
|
|
value = object.internal_object_get( @instruction.index )
|
2015-09-27 21:56:20 +02:00
|
|
|
value = value.object_id unless value.is_a? Fixnum
|
2015-07-30 18:18:12 +02:00
|
|
|
set_register( @instruction.register , value )
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute_SetSlot
|
|
|
|
value = object_for( @instruction.register )
|
|
|
|
object = object_for( @instruction.array )
|
|
|
|
object.internal_object_set( @instruction.index , value )
|
2015-08-23 02:14:16 +02:00
|
|
|
trigger(:object_changed, @instruction.array , @instruction.index)
|
2015-07-30 18:18:12 +02:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
raise "save return has nothing to save" unless @link
|
2015-08-23 02:14:16 +02:00
|
|
|
trigger(:object_changed, @instruction.register , @instruction.index )
|
2015-07-30 18:18:12 +02:00
|
|
|
object.internal_object_set @instruction.index , @link
|
|
|
|
@link = nil
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
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.class}:#{str.object_id}" unless str.is_a? Symbol
|
|
|
|
@stdout += str.to_s
|
|
|
|
when :exit
|
|
|
|
set_instruction(nil)
|
|
|
|
return false
|
|
|
|
else
|
|
|
|
raise "un-implemented syscall #{name}"
|
|
|
|
end
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute_FunctionReturn
|
|
|
|
object = object_for( @instruction.register )
|
|
|
|
#wouldn't need to assign to link, but makes testing 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-08-07 15:46:55 +02:00
|
|
|
|
|
|
|
def execute_OperatorInstruction
|
2015-10-07 09:05:34 +02:00
|
|
|
left = get_register(@instruction.left)
|
|
|
|
rr = @instruction.right
|
|
|
|
right = get_register(rr)
|
2015-10-09 17:06:00 +02:00
|
|
|
case @instruction.operator.to_s
|
2015-10-15 08:32:47 +02:00
|
|
|
when "+"
|
2015-08-08 23:52:27 +02:00
|
|
|
result = left + right
|
2015-10-07 09:05:34 +02:00
|
|
|
when "/"
|
|
|
|
result = left / right
|
|
|
|
when "-"
|
|
|
|
result = left - right
|
|
|
|
when "<"
|
|
|
|
result = left < right
|
|
|
|
when "=="
|
|
|
|
result = left == right
|
2015-08-07 15:46:55 +02:00
|
|
|
else
|
2015-10-09 17:06:00 +02:00
|
|
|
raise "unimplemented '#{@instruction.operator}' #{@instruction}"
|
2015-08-07 15:46:55 +02:00
|
|
|
end
|
2015-10-07 09:05:34 +02:00
|
|
|
puts "#{@instruction} == #{result}"
|
2015-10-15 08:32:47 +02:00
|
|
|
right = set_register(@instruction.left , result)
|
2015-08-07 15:46:55 +02:00
|
|
|
true
|
|
|
|
end
|
2015-07-30 18:18:12 +02:00
|
|
|
end
|
|
|
|
end
|