unite the two resolve_to_index functions
This commit is contained in:
parent
f4ce6d6253
commit
f09086e524
@ -25,4 +25,7 @@ guard :minitest do # with Minitest::Unit
|
|||||||
watch(%r{^lib/vool/statements/send_statement.rb}) {
|
watch(%r{^lib/vool/statements/send_statement.rb}) {
|
||||||
[ Dir["test/vool/to_mom/send/test_*.rb"] , "test/vool/statements/test_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
|
end
|
||||||
|
@ -43,10 +43,14 @@ module Mom
|
|||||||
return risc
|
return risc
|
||||||
end
|
end
|
||||||
private
|
private
|
||||||
|
def source
|
||||||
|
"method setup "
|
||||||
|
end
|
||||||
|
|
||||||
# get the method from method_source into the given register
|
# 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)
|
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
|
end
|
||||||
|
|
||||||
# get the next message from space and unlink it there
|
# get the next message from space and unlink it there
|
||||||
@ -54,7 +58,11 @@ module Mom
|
|||||||
# use given message regster
|
# use given message regster
|
||||||
# return instructions to do this
|
# return instructions to do this
|
||||||
def get_message_to( compiler , message)
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -46,13 +46,13 @@ module Mom
|
|||||||
new_left = compiler.use_reg( :Object )
|
new_left = compiler.use_reg( :Object )
|
||||||
const << Risc::SlotToReg.new( original_source , left ,left_index, new_left)
|
const << Risc::SlotToReg.new( original_source , left ,left_index, new_left)
|
||||||
left = 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
|
if left_slots.length > 2
|
||||||
#same again, once more updating target
|
#same again, once more updating target
|
||||||
new_left = compiler.use_reg( :Object )
|
new_left = compiler.use_reg( :Object )
|
||||||
const << Risc::SlotToReg.new( original_source , left ,left_index, new_left)
|
const << Risc::SlotToReg.new( original_source , left ,left_index, new_left)
|
||||||
left = 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
|
end
|
||||||
raise "more slots not implemented #{left_slots}" if left_slots.length > 3
|
raise "more slots not implemented #{left_slots}" if left_slots.length > 3
|
||||||
end
|
end
|
||||||
@ -67,27 +67,6 @@ module Mom
|
|||||||
compiler.reset_regs
|
compiler.reset_regs
|
||||||
return const
|
return const
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
class SlotDefinition
|
class SlotDefinition
|
||||||
@ -116,7 +95,7 @@ module Mom
|
|||||||
const = Risc.load_constant(instruction, known_object , right)
|
const = Risc.load_constant(instruction, known_object , right)
|
||||||
if slots.length > 0
|
if slots.length > 0
|
||||||
# desctructively replace the existing value to be loaded if more slots
|
# 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)
|
const << Risc::SlotToReg.new( instruction , right ,index, right)
|
||||||
end
|
end
|
||||||
when Symbol
|
when Symbol
|
||||||
@ -127,7 +106,7 @@ module Mom
|
|||||||
end
|
end
|
||||||
if slots.length > 1
|
if slots.length > 1
|
||||||
# desctructively replace the existing value to be loaded if more slots
|
# 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)
|
const << Risc::SlotToReg.new( instruction , right ,index, right)
|
||||||
raise "more slots not implemented #{slots}" if slots.length > 2
|
raise "more slots not implemented #{slots}" if slots.length > 2
|
||||||
end
|
end
|
||||||
|
@ -48,7 +48,7 @@ module Risc
|
|||||||
Parfait::NamedList.type_for({}) , Parfait::NamedList.type_for({}))
|
Parfait::NamedList.type_for({}) , Parfait::NamedList.type_for({}))
|
||||||
space_reg = compiler.use_reg(:Space) #Set up the Space as self upon init
|
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)
|
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)
|
#load the first_message (instance of space)
|
||||||
compiler.add_slot_to_reg( "__init__ load 1st message" , space_reg , message_ind , :message)
|
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))
|
compiler.add_mom( Mom::MessageSetup.new(Parfait.object_space.get_main))
|
||||||
|
@ -62,11 +62,11 @@ module Risc
|
|||||||
method = compiler.use_reg( :TypedMethod )
|
method = compiler.use_reg( :TypedMethod )
|
||||||
method_name = compiler.use_reg( :Word )
|
method_name = compiler.use_reg( :Word )
|
||||||
space = compiler.use_reg( :Space )
|
space = compiler.use_reg( :Space )
|
||||||
methods_index = Risc.resolve_to_index(:type , :methods)
|
methods_index = Risc.resolve_to_index(:Type , :methods)
|
||||||
next_index = Risc.resolve_to_index(:typed_method , :next_method)
|
next_index = Risc.resolve_to_index(:TypedMethod , :next_method)
|
||||||
name_index = Risc.resolve_to_index(:typed_method , :name)
|
name_index = Risc.resolve_to_index(:TypedMethod , :name)
|
||||||
binary_index = Risc.resolve_to_index(:typed_method , :binary)
|
binary_index = Risc.resolve_to_index(:TypedMethod , :binary)
|
||||||
nil_index = Risc.resolve_to_index(:space , :nil_object)
|
nil_index = Risc.resolve_to_index(:Space , :nil_object)
|
||||||
while_start = Risc.label( source , source + "while_start_#{object_id}")
|
while_start = Risc.label( source , source + "while_start_#{object_id}")
|
||||||
exit_label = Risc.label( source , source + "exit_label_#{object_id}")
|
exit_label = Risc.label( source , source + "exit_label_#{object_id}")
|
||||||
false_label = Risc.label( source , source + "fal_label_#{object_id}")
|
false_label = Risc.label( source , source + "fal_label_#{object_id}")
|
||||||
|
@ -16,7 +16,7 @@ module Risc
|
|||||||
# Produce a SlotToReg instruction.
|
# Produce a SlotToReg instruction.
|
||||||
# Array and to are registers or symbols that can be transformed to a register by resolve_to_register
|
# Array and to are registers or symbols that can be transformed to a register by resolve_to_register
|
||||||
# index resolves with resolve_to_index.
|
# 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)
|
index = resolve_to_index( array , index)
|
||||||
array = resolve_to_register( array )
|
array = resolve_to_register( array )
|
||||||
to = resolve_to_register( to )
|
to = resolve_to_register( to )
|
||||||
|
@ -82,14 +82,31 @@ module Risc
|
|||||||
# By looking up the class and the type for that class, we can resolve the instance
|
# By looking up the class and the type for that class, we can resolve the instance
|
||||||
# variable name to an index.
|
# variable name to an index.
|
||||||
# The class can be mapped to a register, and so we get a memory address (reg+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 )
|
# Third arg, compiler, is only needed to resolve receiver/arguments/frame
|
||||||
return instance_name unless instance_name.is_a? Symbol
|
def self.resolve_to_index(object , variable_name ,compiler = nil)
|
||||||
real_name = clazz_name.to_s.split('_').collect{|p|p.capitalize}.join.to_sym
|
return variable_name if variable_name.is_a?(Integer) or variable_name.is_a?(RiscValue)
|
||||||
clazz = Parfait.object_space.get_class_by_name(real_name)
|
object = object.type if object.is_a?(RiscValue)
|
||||||
raise "Class name not given #{real_name} #{clazz_name} #{instance_name}" unless clazz
|
case object
|
||||||
index = clazz.instance_type.variable_index( instance_name )
|
when :frame
|
||||||
raise "Instance name=#{instance_name} not found on #{real_name}:#{clazz.instance_type}" unless index.is_a?(Numeric)
|
type = compiler.method.frame_type
|
||||||
return index # the type word is at index 0, but type is a list and starts at 1 == 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
|
end
|
||||||
|
|
||||||
# if a symbol is given, it may be the message or the new_message.
|
# if a symbol is given, it may be the message or the new_message.
|
||||||
|
@ -1,9 +1,7 @@
|
|||||||
require_relative '../helper'
|
require_relative '../helper'
|
||||||
|
|
||||||
module Risc
|
module Risc
|
||||||
|
|
||||||
module Statements
|
module Statements
|
||||||
include AST::Sexp
|
|
||||||
include CleanCompile
|
include CleanCompile
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
|
39
test/mom/send/test_setup_simple.rb
Normal file
39
test/mom/send/test_setup_simple.rb
Normal 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
|
15
test/support/risc_assert.rb
Normal file
15
test/support/risc_assert.rb
Normal 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
|
Loading…
Reference in New Issue
Block a user