tighter integration with factory methods for adding code

define methods to collapse the code Register.
in add_code Register.factory_method
most instructions done, except op and branch that are rare
This commit is contained in:
Torsten Ruger
2016-12-28 20:37:54 +02:00
parent 8cfcb84c8d
commit e551732f18
13 changed files with 79 additions and 73 deletions

View File

@ -135,9 +135,9 @@ module Typed
name = "#{method.for_type.name}.#{method.name}"
@method.instructions = Register.label(source, name)
@current = enter = method.instructions
add_code Register.label( source, "return #{name}")
add_label( source, "return #{name}")
#load the return address into pc, affecting return. (other cpus have commands for this, but not arm)
add_code Register.function_return( source , Register.message_reg , Register.resolve_to_index(:message , :return_address) )
add_function_return( source , Register.message_reg , Register.resolve_to_index(:message , :return_address) )
@current = enter
self
end
@ -157,6 +157,13 @@ module Typed
self
end
[:label, :reg_to_slot , :slot_to_reg , :load_constant, :function_return ,
:transfer , :reg_to_slot , :byte_to_reg , :reg_to_byte].each do |method|
define_method("add_#{method}".to_sym) do |*args|
add_code Register.send( method , *args )
end
end
# require a (temporary) register. code must give this back with release_reg
def use_reg( type , value = nil )
raise "Not type #{type.inspect}" unless type.is_a?(Symbol) or type.is_a?(Parfait::Type)

View File

@ -17,8 +17,8 @@ module Typed
value_type = @method.locals_type( index )
end
raise "Type mismatch for #{type} access #{value.type}!=#{value_type}" unless value.type == value_type
add_code Register.slot_to_reg(statement , :message , type , named_list )
add_code Register.reg_to_slot(statement , value , named_list , index + 1 ) # one for type
add_slot_to_reg(statement , :message , type , named_list )
add_reg_to_slot(statement , value , named_list , index + 1 ) # one for type
end
# ensure the name given is not space and raise exception otherwise

View File

@ -13,25 +13,25 @@ module Typed
def on_IntegerExpression expression
int = expression.value
reg = use_reg :Integer , int
add_code Register.load_constant( expression, int , reg )
add_load_constant( expression, int , reg )
return reg
end
def on_TrueExpression expression
reg = use_reg :Boolean
add_code Register.load_constant( expression, true , reg )
add_load_constant( expression, true , reg )
return reg
end
def on_FalseExpression expression
reg = use_reg :Boolean
add_code Register.load_constant( expression, false , reg )
add_load_constant( expression, false , reg )
return reg
end
def on_NilExpression expression
reg = use_reg :NilClass
add_code Register.load_constant( expression, nil , reg )
add_load_constant( expression, nil , reg )
return reg
end
@ -39,7 +39,7 @@ module Typed
value = Parfait.new_word expression.value.to_sym
reg = use_reg :Word
Register.machine.constants << value
add_code Register.load_constant( expression, value , reg )
add_load_constant( expression, value , reg )
return reg
end
@ -48,7 +48,7 @@ module Typed
clazz = Parfait::Space.object_space.get_class_by_name! name
raise "No such class #{name}" unless clazz
reg = use_reg :MetaClass , clazz
add_code Register.load_constant( expression, clazz , reg )
add_load_constant( expression, clazz , reg )
return reg
end

View File

@ -7,7 +7,7 @@ module Typed
reset_regs
#move the new message (that we need to populate to make a call) to std register
new_message = Register.resolve_to_register(:new_message)
add_code Register.slot_to_reg(statement, :message , :next_message , new_message )
add_slot_to_reg(statement, :message , :next_message , new_message )
me = get_me( statement )
type = get_my_type(me)
@ -15,7 +15,7 @@ module Typed
raise "Method not implemented #{type.inspect}.#{statement.name}" unless method
# move our receiver there
add_code Register.reg_to_slot( statement , me , :new_message , :receiver)
add_reg_to_slot( statement , me , :new_message , :receiver)
set_message_details(method , statement , statement.arguments)
set_arguments(method , statement.arguments)
@ -25,7 +25,7 @@ module Typed
# the effect of the method is that the NewMessage Return slot will be filled, return it
# but move it into a register too
add_code Register.slot_to_reg(statement, :new_message , :return_value , ret )
add_slot_to_reg(statement, :new_message , :return_value , ret )
ret
end
@ -36,7 +36,7 @@ module Typed
me = process( statement.receiver )
else
me = use_reg @method.for_type
add_code Register.slot_to_reg(statement, :message , :receiver , me )
add_slot_to_reg(statement, :message , :receiver , me )
end
me
end
@ -58,22 +58,22 @@ module Typed
def set_message_details( method , name_s , arguments )
name = name_s.name
name_tmp = use_reg(:Word)
add_code Register.load_constant("#{name} load method name", name , name_tmp)
add_code Register.reg_to_slot( "#{name} store method name" , name_tmp , :new_message , :name)
add_load_constant("#{name} load method name", name , name_tmp)
add_reg_to_slot( "#{name} store method name" , name_tmp , :new_message , :name)
# next arg and local types
args_reg = use_reg(:Type , method.arguments )
list_reg = use_reg(:NamedList , arguments )
add_code Register.load_constant("#{name} load methods", method , args_reg)
add_load_constant("#{name} load methods", method , args_reg)
args_type_index = method.get_type().variable_index(:arguments)
raise args_type_index.to_s unless args_type_index == 6
add_code Register.slot_to_reg( "#{name} get args type from method" , args_reg , args_type_index , args_reg )
add_code Register.slot_to_reg( "#{name} get args from method" , :new_message , :arguments , list_reg )
add_code Register.reg_to_slot( "#{name} store args type in args" , args_reg , list_reg , 1 )
add_slot_to_reg( "#{name} get args type from method" , args_reg , args_type_index , args_reg )
add_slot_to_reg( "#{name} get args from method" , :new_message , :arguments , list_reg )
add_reg_to_slot( "#{name} store args type in args" , args_reg , list_reg , 1 )
#FIXME need to set type of locals too. sama sama
# len_tmp = use_reg(:Integer , arguments.to_a.length )
# add_code Register.load_constant(name_s, arguments.to_a.length , len_tmp)
# add_code Register.reg_to_slot( name_s , len_tmp , :new_message , :indexed_length)
# add_load_constant(name_s, arguments.to_a.length , len_tmp)
# add_reg_to_slot( name_s , len_tmp , :new_message , :indexed_length)
end
def set_arguments( method , arguments )
@ -89,10 +89,9 @@ module Typed
#FIXME definately needs some tests
raise "TypeMismatch calling with #{val.type} , instead of #{arg_type.type_at(i + 2)}" if val.type != arg_type.type_at(i + 2)
list_reg = use_reg(:NamedList , arguments )
add_code Register.slot_to_reg( "Set arg #{i}:#{arg}" , :new_message , :arguments , list_reg )
add_slot_to_reg( "Set arg #{i}:#{arg}" , :new_message , :arguments , list_reg )
# which we load int the new_message at the argument's index
set = Register.reg_to_slot( arg , val , list_reg , i + 2 ) #one for type and one for ruby
add_code set
add_reg_to_slot( arg , val , list_reg , i + 2 ) #one for type and one for ruby
end
end
end

View File

@ -15,7 +15,7 @@ module Typed
raise "no such field:#{field_name} for class #{type.inspect}" unless index
value = use_reg(type.type_at(index))
add_code Register.slot_to_reg(statement , receiver , index, value)
add_slot_to_reg(statement , receiver , index, value)
value
end

View File

@ -15,8 +15,8 @@ module Typed
named_list = use_reg :NamedList
ret = use_reg @method.argument_type(index)
#puts "For #{name} at #{index} got #{@method.arguments.inspect}"
add_code Register.slot_to_reg("#{statement} load args" , :message , :arguments, named_list )
add_code Register.slot_to_reg("#{statement} load #{name}" , named_list , index + 1, ret )
add_slot_to_reg("#{statement} load args" , :message , :arguments, named_list )
add_slot_to_reg("#{statement} load #{name}" , named_list , index + 1, ret )
return ret
end
# or a local so it is in the named_list
@ -30,28 +30,28 @@ module Typed
index = @method.has_local( name )
raise "must define variable '#{name}' before using it" unless index
named_list = use_reg :NamedList
add_code Register.slot_to_reg("#{name} load locals" , :message , :locals , named_list )
add_slot_to_reg("#{name} load locals" , :message , :locals , named_list )
ret = use_reg @method.locals_type( index )
add_code Register.slot_to_reg("#{name} load from locals" , named_list , index + 1, ret )
add_slot_to_reg("#{name} load from locals" , named_list , index + 1, ret )
return ret
end
def handle_special_self(statement)
ret = use_reg @type
add_code Register.slot_to_reg("#{statement} load self" , :message , :receiver , ret )
add_slot_to_reg("#{statement} load self" , :message , :receiver , ret )
return ret
end
def handle_special_space(statement)
space = Parfait::Space.object_space
reg = use_reg :Space , space
add_code Register.load_constant( "#{statement} load space", space , reg )
add_load_constant( "#{statement} load space", space , reg )
return reg
end
def handle_special_message(statement)
reg = use_reg :Message
add_code Register.transfer( "#{statement} load message", Register.message_reg , reg )
add_transfer( "#{statement} load message", Register.message_reg , reg )
return reg
end
end #module

View File

@ -3,7 +3,7 @@ module Typed
def on_ReturnStatement statement
reg = process(statement.return_value)
add_code Register.reg_to_slot( statement, reg , :message , :return_value)
add_reg_to_slot( statement, reg , :message , :return_value)
nil # statements don't return
end
end