unite the two resolve_to_index functions

This commit is contained in:
Torsten Ruger 2018-04-05 20:10:00 +03:00
parent f4ce6d6253
commit f09086e524
11 changed files with 105 additions and 46 deletions

View File

@ -25,4 +25,7 @@ guard :minitest do # with Minitest::Unit
watch(%r{^lib/vool/statements/send_statement.rb}) {
[ Dir["test/vool/to_mom/send/test_*.rb"] , "test/vool/statements/test_send_statement.rb"] }
#message setup
watch(%r{^lib/mom/instruction/message_setup.rb}) { Dir["test/mom/send/test_setup*.rb"] }
end

View File

@ -43,10 +43,14 @@ module Mom
return risc
end
private
def source
"method setup "
end
# get the method from method_source into the given register
# return instruction that do this
# return instructions to do this
def move_method_to(compiler, register)
Risc.load_constant("message setup move method" , @method_source ,register)
Risc.load_constant(source + " move method" , @method_source ,register)
end
# get the next message from space and unlink it there
@ -54,7 +58,11 @@ module Mom
# use given message regster
# return instructions to do this
def get_message_to( compiler , message)
Risc.load_constant("message setup move method" , @method_source ,message)
space = compiler.use_reg(:Space)
risc = Risc.load_constant("message setup move method" , Parfait.object_space ,space)
risc << Risc.slot_to_reg(source + "get next message" , space , :first_message , message)
next_message = compiler.use_reg( :Message )
risc << Risc.slot_to_reg(source + "get next message" , message , :next_message , next_message)
end
end

View File

@ -46,13 +46,13 @@ module Mom
new_left = compiler.use_reg( :Object )
const << Risc::SlotToReg.new( original_source , left ,left_index, new_left)
left = new_left
left_index = SlotLoad.resolve_to_index(left_slots[0] , left_slots[1] ,compiler)
left_index = Risc.resolve_to_index(left_slots[0] , left_slots[1] ,compiler)
if left_slots.length > 2
#same again, once more updating target
new_left = compiler.use_reg( :Object )
const << Risc::SlotToReg.new( original_source , left ,left_index, new_left)
left = new_left
left_index = SlotLoad.resolve_to_index(left_slots[1] , left_slots[2] ,compiler)
left_index = Risc.resolve_to_index(left_slots[1] , left_slots[2] ,compiler)
end
raise "more slots not implemented #{left_slots}" if left_slots.length > 3
end
@ -67,27 +67,6 @@ module Mom
compiler.reset_regs
return const
end
def self.resolve_to_index(object , variable_name ,compiler)
return variable_name if variable_name.is_a?(Integer)
case object
when :frame
type = compiler.method.frame_type
when :message , :next_message , :caller
type = Parfait.object_space.get_class_by_name(:Message).instance_type
when :arguments
type = compiler.method.arguments_type
when :receiver
type = compiler.method.for_type
when Parfait::Object
type = Parfait.object_space.get_class_by_name( object.class.name.split("::").last.to_sym).instance_type
else
raise "Not implemented/found object #{object}"
end
index = type.variable_index(variable_name)
raise "Index not found for #{variable_name} in #{object} of type #{type}" unless index
return index
end
end
class SlotDefinition
@ -116,7 +95,7 @@ module Mom
const = Risc.load_constant(instruction, known_object , right)
if slots.length > 0
# desctructively replace the existing value to be loaded if more slots
index = SlotLoad.resolve_to_index(known_object , slots[0] ,compiler)
index = Risc.resolve_to_index(known_object , slots[0] ,compiler)
const << Risc::SlotToReg.new( instruction , right ,index, right)
end
when Symbol
@ -127,7 +106,7 @@ module Mom
end
if slots.length > 1
# desctructively replace the existing value to be loaded if more slots
index = SlotLoad.resolve_to_index(slots[0] , slots[1] ,compiler)
index = Risc.resolve_to_index(slots[0] , slots[1] ,compiler)
const << Risc::SlotToReg.new( instruction , right ,index, right)
raise "more slots not implemented #{slots}" if slots.length > 2
end

View File

@ -48,7 +48,7 @@ module Risc
Parfait::NamedList.type_for({}) , Parfait::NamedList.type_for({}))
space_reg = compiler.use_reg(:Space) #Set up the Space as self upon init
compiler.add_load_constant("__init__ load Space", Parfait.object_space , space_reg)
message_ind = Risc.resolve_to_index( :space , :first_message )
message_ind = Risc.resolve_to_index( :Space , :first_message )
#load the first_message (instance of space)
compiler.add_slot_to_reg( "__init__ load 1st message" , space_reg , message_ind , :message)
compiler.add_mom( Mom::MessageSetup.new(Parfait.object_space.get_main))

View File

@ -62,11 +62,11 @@ module Risc
method = compiler.use_reg( :TypedMethod )
method_name = compiler.use_reg( :Word )
space = compiler.use_reg( :Space )
methods_index = Risc.resolve_to_index(:type , :methods)
next_index = Risc.resolve_to_index(:typed_method , :next_method)
name_index = Risc.resolve_to_index(:typed_method , :name)
binary_index = Risc.resolve_to_index(:typed_method , :binary)
nil_index = Risc.resolve_to_index(:space , :nil_object)
methods_index = Risc.resolve_to_index(:Type , :methods)
next_index = Risc.resolve_to_index(:TypedMethod , :next_method)
name_index = Risc.resolve_to_index(:TypedMethod , :name)
binary_index = Risc.resolve_to_index(:TypedMethod , :binary)
nil_index = Risc.resolve_to_index(:Space , :nil_object)
while_start = Risc.label( source , source + "while_start_#{object_id}")
exit_label = Risc.label( source , source + "exit_label_#{object_id}")
false_label = Risc.label( source , source + "fal_label_#{object_id}")

View File

@ -16,7 +16,7 @@ module Risc
# Produce a SlotToReg instruction.
# Array and to are registers or symbols that can be transformed to a register by resolve_to_register
# index resolves with resolve_to_index.
def self.slot_to_reg source , array , index , to
def self.slot_to_reg( source , array , index , to)
index = resolve_to_index( array , index)
array = resolve_to_register( array )
to = resolve_to_register( to )

View File

@ -82,14 +82,31 @@ module Risc
# By looking up the class and the type for that class, we can resolve the instance
# variable name to an index.
# The class can be mapped to a register, and so we get a memory address (reg+index)
def self.resolve_to_index( clazz_name , instance_name )
return instance_name unless instance_name.is_a? Symbol
real_name = clazz_name.to_s.split('_').collect{|p|p.capitalize}.join.to_sym
clazz = Parfait.object_space.get_class_by_name(real_name)
raise "Class name not given #{real_name} #{clazz_name} #{instance_name}" unless clazz
index = clazz.instance_type.variable_index( instance_name )
raise "Instance name=#{instance_name} not found on #{real_name}:#{clazz.instance_type}" unless index.is_a?(Numeric)
return index # the type word is at index 0, but type is a list and starts at 1 == type
# Third arg, compiler, is only needed to resolve receiver/arguments/frame
def self.resolve_to_index(object , variable_name ,compiler = nil)
return variable_name if variable_name.is_a?(Integer) or variable_name.is_a?(RiscValue)
object = object.type if object.is_a?(RiscValue)
case object
when :frame
type = compiler.method.frame_type
when :message , :next_message , :caller
type = Parfait.object_space.get_class_by_name(:Message).instance_type
when :arguments
type = compiler.method.arguments_type
when :receiver
type = compiler.method.for_type
when Parfait::Object
type = Parfait.object_space.get_class_by_name( object.class.name.split("::").last.to_sym).instance_type
when Symbol
clazz = Parfait.object_space.get_class_by_name(object)
raise "Not implemented/found object #{object}:#{object.class}" unless clazz
type = clazz.instance_type
else
raise "Not implemented/found object #{object}:#{object.class}"
end
index = type.variable_index(variable_name)
raise "Index not found for #{variable_name} in #{object} of type #{type}" unless index
return index
end
# if a symbol is given, it may be the message or the new_message.

View File

@ -1,9 +1,7 @@
require_relative '../helper'
module Risc
module Statements
include AST::Sexp
include CleanCompile
def setup

View File

@ -0,0 +1,39 @@
require_relative "../helper"
module Risc
class TestMessageSetupSimple < MiniTest::Test
include Statements
include Assertions
def setup
super
@input = "5.mod4"
@expect = [LoadConstant, LoadConstant, SlotToReg, SlotToReg, LoadConstant,
SlotToReg, RegToSlot, LoadConstant, SlotToReg, RegToSlot,
SlotToReg, LoadConstant, FunctionCall, Label]
@produced = produce_body
end
def test_send_instructions
assert_nil msg = check_nil , msg
end
def test_load_method
method = @produced
assert_load( method, Parfait::TypedMethod ,:r1)
assert_equal :mod4 , method.constant.name
end
def test_load_space
space = @produced.next(1)
assert_load( space , Parfait::Space , :r3 )
end
def test_load_message
sl = @produced.next( 2 )
assert_slot_to_reg( sl , :r3 , 4 , :r2 )
end
def test_load_next_message
sl = @produced.next( 3 )
assert_slot_to_reg( sl , :r2 , 2 , :r4 )
end
end
end

View File

@ -0,0 +1,15 @@
module Risc
module Assertions
def assert_slot_to_reg( slot , array = nil, index = nil , register = nil)
assert_equal SlotToReg , slot.class
assert_equal( array , slot.array.symbol) if array
assert_equal( index , slot.index) if index
assert_equal( register , slot.register.symbol) if register
end
def assert_load(load , clazz = nil , register = nil)
assert_equal LoadConstant , load.class
assert_equal( clazz , load.constant.class) if clazz
assert_equal( register , load.register.symbol) if register
end
end
end