passing compiler to to_mom, not method

To be able to delegate scope (block/method) things later
This commit is contained in:
Torsten Ruger 2018-07-05 14:02:38 +03:00
parent 16d91f24ce
commit 3f80953385
15 changed files with 78 additions and 74 deletions

View File

@ -30,8 +30,8 @@ module Parfait
def compiler_for(for_type) def compiler_for(for_type)
typed_method = create_typed_method(for_type) typed_method = create_typed_method(for_type)
head = source.to_mom( typed_method )
compiler = Risc::MethodCompiler.new( typed_method ) compiler = Risc::MethodCompiler.new( typed_method )
head = source.to_mom( compiler )
compiler.add_mom(head) compiler.add_mom(head)
compiler compiler
end end

View File

@ -31,9 +31,9 @@ module Vool
statements statements
end end
def chain_assign(assign , method) def chain_assign(assign , compiler)
return assign unless @value.is_a?(SendStatement) return assign unless @value.is_a?(SendStatement)
@value.to_mom(method) << assign @value.to_mom(compiler) << assign
end end
def each(&block) def each(&block)
@ -54,10 +54,10 @@ module Vool
return IvarAssignment.new(@name , @value) return IvarAssignment.new(@name , @value)
end end
def to_mom( method ) def to_mom( compiler )
to = Mom::SlotDefinition.new(:message ,[ :receiver , @name]) to = Mom::SlotDefinition.new(:message ,[ :receiver , @name])
from = @value.slot_definition(method) from = @value.slot_definition(compiler)
return chain_assign( Mom::SlotLoad.new(to,from) , method) return chain_assign( Mom::SlotLoad.new(to,from) , compiler)
end end
end end

View File

@ -10,7 +10,7 @@ module Vool
def initialize(value) def initialize(value)
@value = value @value = value
end end
def slot_definition(method) def slot_definition(compiler)
return Mom::SlotDefinition.new(Mom::IntegerConstant.new(@value) , []) return Mom::SlotDefinition.new(Mom::IntegerConstant.new(@value) , [])
end end
def ct_type def ct_type
@ -35,7 +35,7 @@ module Vool
def ct_type def ct_type
Parfait.object_space.get_class_by_name(:True).instance_type Parfait.object_space.get_class_by_name(:True).instance_type
end end
def slot_definition(method) def slot_definition(compiler)
return Mom::SlotDefinition.new(Parfait.object_space.true_object , []) return Mom::SlotDefinition.new(Parfait.object_space.true_object , [])
end end
def to_s(depth = 0) def to_s(depth = 0)
@ -46,7 +46,7 @@ module Vool
def ct_type def ct_type
Parfait.object_space.get_class_by_name(:False).instance_type Parfait.object_space.get_class_by_name(:False).instance_type
end end
def slot_definition(method) def slot_definition(compiler)
return Mom::SlotDefinition.new(Parfait.object_space.false_object , []) return Mom::SlotDefinition.new(Parfait.object_space.false_object , [])
end end
def to_s(depth = 0) def to_s(depth = 0)
@ -57,7 +57,7 @@ module Vool
def ct_type def ct_type
Parfait.object_space.get_class_by_name(:Nil).instance_type Parfait.object_space.get_class_by_name(:Nil).instance_type
end end
def slot_definition(method) def slot_definition(compiler)
return Mom::SlotDefinition.new(Parfait.object_space.nil_object , []) return Mom::SlotDefinition.new(Parfait.object_space.nil_object , [])
end end
def to_s(depth = 0) def to_s(depth = 0)
@ -69,8 +69,8 @@ module Vool
def initialize(type = nil) def initialize(type = nil)
@my_type = type @my_type = type
end end
def slot_definition(in_method) def slot_definition(compiler)
@my_type = in_method.for_type @my_type = compiler.method.for_type
Mom::SlotDefinition.new(:message , [:receiver]) Mom::SlotDefinition.new(:message , [:receiver])
end end
def ct_type def ct_type
@ -90,7 +90,7 @@ module Vool
def initialize(value) def initialize(value)
@value = value @value = value
end end
def slot_definition(method) def slot_definition(compiler)
return Mom::SlotDefinition.new(Mom::StringConstant.new(@value),[]) return Mom::SlotDefinition.new(Mom::StringConstant.new(@value),[])
end end
def ct_type def ct_type

View File

@ -8,8 +8,11 @@ module Vool
@clazz = clazz @clazz = clazz
end end
def to_mom( _ ) def to_mom( compiler )
raise "should not be called (call create_objects)" # raise "should not be called (call create_objects)"
end
def slot_definition(compiler)
return Mom::SlotDefinition.new(Mom::IntegerConstant.new(1) , [])
end end
def each(&block) def each(&block)

View File

@ -20,31 +20,31 @@ module Vool
rest rest
end end
def to_mom( method ) def to_mom( compiler )
if_false ? full_if(method) : simple_if(method) if_false ? full_if(compiler) : simple_if(compiler)
end end
def simple_if(method) def simple_if(compiler)
true_label = Mom::Label.new( "true_label_#{object_id.to_s(16)}") true_label = Mom::Label.new( "true_label_#{object_id.to_s(16)}")
merge_label = Mom::Label.new( "merge_label_#{object_id.to_s(16)}") merge_label = Mom::Label.new( "merge_label_#{object_id.to_s(16)}")
head = Mom::TruthCheck.new(condition.slot_definition(method) , merge_label) head = Mom::TruthCheck.new(condition.slot_definition(compiler) , merge_label)
head << true_label head << true_label
head << if_true.to_mom(method) head << if_true.to_mom(compiler)
head << merge_label head << merge_label
end end
def full_if(method) def full_if(compiler)
true_label = Mom::Label.new( "true_label_#{object_id.to_s(16)}") true_label = Mom::Label.new( "true_label_#{object_id.to_s(16)}")
false_label = Mom::Label.new( "false_label_#{object_id.to_s(16)}") false_label = Mom::Label.new( "false_label_#{object_id.to_s(16)}")
merge_label = Mom::Label.new( "merge_label_#{object_id.to_s(16)}") merge_label = Mom::Label.new( "merge_label_#{object_id.to_s(16)}")
head = Mom::TruthCheck.new(condition.slot_definition(method) , false_label) head = Mom::TruthCheck.new(condition.slot_definition(compiler) , false_label)
head << true_label head << true_label
head << if_true.to_mom(method) head << if_true.to_mom(compiler)
head << Mom::Jump.new(merge_label) head << Mom::Jump.new(merge_label)
head << false_label head << false_label
head << if_false.to_mom(method) head << if_false.to_mom(compiler)
head << merge_label head << merge_label
end end

View File

@ -2,15 +2,15 @@ module Vool
class LocalAssignment < Assignment class LocalAssignment < Assignment
def to_mom( method ) def to_mom( compiler )
if method.arguments_type.variable_index(@name) if compiler.method.arguments_type.variable_index(@name)
type = :arguments type = :arguments
else else
type = :frame type = :frame
end end
to = Mom::SlotDefinition.new(:message ,[ type , @name]) to = Mom::SlotDefinition.new(:message ,[ type , @name])
from = @value.slot_definition(method) from = @value.slot_definition(compiler)
return chain_assign( Mom::SlotLoad.new(to,from) , method) return chain_assign( Mom::SlotLoad.new(to,from) , compiler)
end end
end end

View File

@ -24,8 +24,9 @@ module Vool
# To return form a method in mom instructions we only need to do two things: # To return form a method in mom instructions we only need to do two things:
# - store the given return value, this is a SlotMove # - store the given return value, this is a SlotMove
# - activate return sequence (reinstantiate old message and jump to return address) # - activate return sequence (reinstantiate old message and jump to return address)
def to_mom( method ) def to_mom( compiler )
ret = Mom::SlotLoad.new( [:message , :return_value] , @return_value.slot_definition(method) ) ret = Mom::SlotLoad.new( [:message , :return_value] ,
@return_value.slot_definition(compiler) )
ret << Mom::ReturnSequence.new ret << Mom::ReturnSequence.new
end end

View File

@ -60,7 +60,7 @@ module Vool
@arguments.each do |arg| @arguments.each do |arg|
block.call(arg) block.call(arg)
end end
self.block.each(block) if self.block self.block.each(&block) if self.block
end end
# lazy init this, to keep the dependency (which goes to parfait and booting) at bay # lazy init this, to keep the dependency (which goes to parfait and booting) at bay
@ -71,62 +71,62 @@ module Vool
# A Send breaks down to 2 steps: # A Send breaks down to 2 steps:
# - Setting up the next message, with receiver, arguments, and (importantly) return address # - Setting up the next message, with receiver, arguments, and (importantly) return address
# - a CachedCall , or a SimpleCall, depending on wether the receiver type can be determined # - a CachedCall , or a SimpleCall, depending on wether the receiver type can be determined
def to_mom( in_method ) def to_mom( compiler )
@parfait_block = self.block.to_mom(in_method) if self.block @parfait_block = self.block.to_mom(compiler) if self.block
@receiver = SelfExpression.new(in_method.for_type) if @receiver.is_a?(SelfExpression) @receiver = SelfExpression.new(compiler.method.for_type) if @receiver.is_a?(SelfExpression)
if(@receiver.ct_type) if(@receiver.ct_type)
simple_call(in_method) simple_call(compiler)
else else
cached_call(in_method) cached_call(compiler)
end end
end end
# When used as right hand side, this tells what data to move to get the result into # When used as right hand side, this tells what data to move to get the result into
# a varaible. It is (off course) the return value of the message # a varaible. It is (off course) the return value of the message
def slot_definition(in_method) def slot_definition(compiler)
Mom::SlotDefinition.new(:message ,[ :return_value]) Mom::SlotDefinition.new(:message ,[ :return_value])
end end
def message_setup(in_method,called_method) def message_setup(compiler,called_method)
setup = Mom::MessageSetup.new( called_method ) setup = Mom::MessageSetup.new( called_method )
mom_receive = @receiver.slot_definition(in_method) mom_receive = @receiver.slot_definition(compiler)
arg_target = [:message , :next_message , :arguments] arg_target = [:message , :next_message , :arguments]
args = [] args = []
@arguments.each_with_index do |arg , index| # +1 because of type @arguments.each_with_index do |arg , index| # +1 because of type
args << Mom::SlotLoad.new( arg_target + [index + 1] , arg.slot_definition(in_method)) args << Mom::SlotLoad.new( arg_target + [index + 1] , arg.slot_definition(compiler))
end end
setup << Mom::ArgumentTransfer.new( mom_receive , args ) setup << Mom::ArgumentTransfer.new( mom_receive , args )
end end
def simple_call(in_method) def simple_call(compiler)
type = @receiver.ct_type type = @receiver.ct_type
called_method = type.resolve_method(@name) called_method = type.resolve_method(@name)
raise "No method #{@name} for #{type}" unless called_method raise "No method #{@name} for #{type}" unless called_method
message_setup(in_method,called_method) << Mom::SimpleCall.new(called_method) message_setup(compiler,called_method) << Mom::SimpleCall.new(called_method)
end end
# this breaks cleanly into two parts: # this breaks cleanly into two parts:
# - check the cached type and if neccessary update # - check the cached type and if neccessary update
# - call the cached method # - call the cached method
def cached_call(in_method) def cached_call(compiler)
cache_check(in_method) << call_cached_method(in_method) cache_check(compiler) << call_cached_method(compiler)
end end
# check that current type is the cached type # check that current type is the cached type
# if not, change and find method for the type (simple_call to resolve_method) # if not, change and find method for the type (simple_call to resolve_method)
# conceptually easy in ruby, but we have to compile that "easy" ruby # conceptually easy in ruby, but we have to compile that "easy" ruby
def cache_check(in_method) def cache_check(compiler)
ok = Mom::Label.new("cache_ok_#{self.object_id}") ok = Mom::Label.new("cache_ok_#{self.object_id}")
check = build_condition(ok, in_method) # if cached_type != current_type check = build_condition(ok, compiler) # if cached_type != current_type
check << Mom::SlotLoad.new([dynamic_call.cache_entry, :cached_type] , receiver_type_definition(in_method)) check << Mom::SlotLoad.new([dynamic_call.cache_entry, :cached_type] , receiver_type_definition(compiler))
check << Mom::ResolveMethod.new( @name , dynamic_call.cache_entry ) check << Mom::ResolveMethod.new( @name , dynamic_call.cache_entry )
check << ok check << ok
end end
# to call the method (that we know now to be in the cache), we move the method # to call the method (that we know now to be in the cache), we move the method
# to reg1, do the setup (very similar to static) and call # to reg1, do the setup (very similar to static) and call
def call_cached_method(in_method) def call_cached_method(compiler)
message_setup(in_method,dynamic_call.cache_entry) << dynamic_call message_setup(compiler,dynamic_call.cache_entry) << dynamic_call
end end
def to_s(depth = 0) def to_s(depth = 0)
@ -135,14 +135,14 @@ module Vool
end end
private private
def receiver_type_definition(in_method) def receiver_type_definition(compiler)
defi = @receiver.slot_definition(in_method) defi = @receiver.slot_definition(compiler)
defi.slots << :type defi.slots << :type
defi defi
end end
def build_condition(ok_label, in_method) def build_condition(ok_label, compiler)
cached_type = Mom::SlotDefinition.new(dynamic_call.cache_entry , [:cached_type]) cached_type = Mom::SlotDefinition.new(dynamic_call.cache_entry , [:cached_type])
current_type = receiver_type_definition(in_method) current_type = receiver_type_definition(compiler)
Mom::NotSameCheck.new(cached_type , current_type, ok_label) Mom::NotSameCheck.new(cached_type , current_type, ok_label)
end end
end end

View File

@ -52,13 +52,13 @@ module Vool
def normalize def normalize
raise "should not be normalized #{self}" raise "should not be normalized #{self}"
end end
def to_mom(method) def to_mom(compiler)
raise "should not be momed #{self}" raise "should not be momed #{self}"
end end
# for loading into a lot, return the "slot_definition" that can be passed to # for loading into a lot, return the "slot_definition" that can be passed to
# SlotLoad. # SlotLoad.
def slot_definition(method) def slot_definition(compiler)
raise "not iplemented in #{self}" raise "not iplemented in #{self}"
end end

View File

@ -29,12 +29,12 @@ module Vool
end end
# create mom instructions # create mom instructions
def to_mom( method ) def to_mom( compiler )
raise "Empty list ? #{statements.length}" if empty? raise "Empty list ? #{statements.length}" if empty?
stats = @statements.dup stats = @statements.dup
flat = stats.shift.to_mom(method) flat = stats.shift.to_mom(compiler)
while( nekst = stats.shift ) while( nekst = stats.shift )
flat.append nekst.to_mom(method) flat.append nekst.to_mom(compiler)
end end
flat flat
end end

View File

@ -10,8 +10,8 @@ module Vool
class LocalVariable < Expression class LocalVariable < Expression
include Named include Named
def slot_definition(method) def slot_definition(compiler)
if method.arguments_type.variable_index(@name) if compiler.method.arguments_type.variable_index(@name)
type = :arguments type = :arguments
else else
type = :frame type = :frame
@ -25,7 +25,7 @@ module Vool
class InstanceVariable < Expression class InstanceVariable < Expression
include Named include Named
def slot_definition(method) def slot_definition(compiler)
Mom::SlotDefinition.new(:message , [ :receiver , @name] ) Mom::SlotDefinition.new(:message , [ :receiver , @name] )
end end
# used to collect type information # used to collect type information

View File

@ -16,13 +16,13 @@ module Vool
WhileStatement.new(cond , @body.normalize , rest) WhileStatement.new(cond , @body.normalize , rest)
end end
def to_mom( method ) def to_mom( compiler )
merge_label = Mom::Label.new( "merge_label_#{object_id.to_s(16)}") merge_label = Mom::Label.new( "merge_label_#{object_id.to_s(16)}")
cond_label = Mom::Label.new( "cond_label_#{object_id.to_s(16)}") cond_label = Mom::Label.new( "cond_label_#{object_id.to_s(16)}")
codes = cond_label codes = cond_label
codes << @hoisted.to_mom(method) if @hoisted codes << @hoisted.to_mom(compiler) if @hoisted
codes << Mom::TruthCheck.new(condition.slot_definition(method) , merge_label) codes << Mom::TruthCheck.new(condition.slot_definition(compiler) , merge_label)
codes << @body.to_mom(method) codes << @body.to_mom(compiler)
codes << Mom::Jump.new(cond_label) codes << Mom::Jump.new(cond_label)
codes << merge_label codes << merge_label
end end

View File

@ -46,11 +46,11 @@ module Vool
# A Send breaks down to 2 steps: # A Send breaks down to 2 steps:
# - Setting up the next message, with receiver, arguments, and (importantly) return address # - Setting up the next message, with receiver, arguments, and (importantly) return address
# - a SimpleCall, # - a SimpleCall,
def to_mom( in_method ) def to_mom( compiler )
@parfait_block = @block.to_mom(in_method) if @block @parfait_block = @block.to_mom(compiler) if @block
@receiver = SelfExpression.new(in_method.for_type) if @receiver.is_a?(SelfExpression) @receiver = SelfExpression.new(compiler.for_type) if @receiver.is_a?(SelfExpression)
if(@receiver.ct_type) if(@receiver.ct_type)
simple_call(in_method) simple_call(compiler)
else else
raise "ERROR" raise "ERROR"
end end
@ -58,17 +58,17 @@ module Vool
# When used as right hand side, this tells what data to move to get the result into # When used as right hand side, this tells what data to move to get the result into
# a varaible. It is (off course) the return value of the message # a varaible. It is (off course) the return value of the message
def slot_definition(in_method) def slot_definition(compiler)
Mom::SlotDefinition.new(:message ,[ :return_value]) Mom::SlotDefinition.new(:message ,[ :return_value])
end end
def message_setup(in_method,called_method) def message_setup(in_method,called_method)
setup = Mom::MessageSetup.new( called_method ) setup = Mom::MessageSetup.new( called_method )
mom_receive = @receiver.slot_definition(in_method) mom_receive = @receiver.slot_definition(compiler)
arg_target = [:message , :next_message , :arguments] arg_target = [:message , :next_message , :arguments]
args = [] args = []
@arguments.each_with_index do |arg , index| # +1 because of type @arguments.each_with_index do |arg , index| # +1 because of type
args << Mom::SlotLoad.new( arg_target + [index + 1] , arg.slot_definition(in_method)) args << Mom::SlotLoad.new( arg_target + [index + 1] , arg.slot_definition(compiler))
end end
setup << Mom::ArgumentTransfer.new( mom_receive , args ) setup << Mom::ArgumentTransfer.new( mom_receive , args )
end end

View File

@ -35,7 +35,7 @@ module MomCompile
assert_equal Mom::MomCompiler , ret.class assert_equal Mom::MomCompiler , ret.class
compiler = ret.method_compilers.find{|c| c.method.name == :main and c.method.for_type.object_class.name == :Test} compiler = ret.method_compilers.find{|c| c.method.name == :main and c.method.for_type.object_class.name == :Test}
assert_equal Risc::MethodCompiler , compiler.class assert_equal Risc::MethodCompiler , compiler.class
@method.source.to_mom( compiler.method ) @method.source.to_mom( compiler )
end end
def compile_mom(input) def compile_mom(input)

View File

@ -11,7 +11,7 @@ module Vool
end end
def test_is_compiler def test_is_compiler
assert_equal Mom::MomCompiler , @ret.class # assert_equal Mom::MomCompiler , @ret.class
end end
def est_has_compilers def est_has_compilers