almost rewritten the call site
statements resolve to nothing and use all registers expressions return register and allocate those with use_reg
This commit is contained in:
parent
82d6ebf392
commit
5b95319191
@ -39,10 +39,19 @@ module Phisol
|
|||||||
return reg
|
return reg
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# releasing a register (accuired by use_reg) makes it available for use again
|
||||||
|
# thus avoiding possibly using too many registers
|
||||||
def release_reg reg
|
def release_reg reg
|
||||||
last = @regs.pop
|
last = @regs.pop
|
||||||
raise "released register in wrong order, expect #{last} but was #{reg}" if reg != last
|
raise "released register in wrong order, expect #{last} but was #{reg}" if reg != last
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# reset the registers to be used. Start at r4 for next usage.
|
||||||
|
# Every statement starts with this, meaning each statement may use all registers, but none
|
||||||
|
# get saved. Statements have affect on objects.
|
||||||
|
def reset_regs
|
||||||
|
@regs.clear
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -5,38 +5,40 @@ module Phisol
|
|||||||
name , arguments , receiver = *statement
|
name , arguments , receiver = *statement
|
||||||
name = name.to_a.first
|
name = name.to_a.first
|
||||||
raise "not inside method " unless @method
|
raise "not inside method " unless @method
|
||||||
|
reset_regs
|
||||||
if receiver
|
if receiver
|
||||||
me = process( receiver.to_a.first )
|
me = process( receiver.to_a.first )
|
||||||
else
|
else
|
||||||
raise "revisit"
|
if @method.for_class.name == :Integer
|
||||||
if @method.class.name == :Integer
|
type = :int
|
||||||
me = Virtual::Self.new :int
|
|
||||||
else
|
else
|
||||||
me = Virtual::Self.new :ref
|
type = :ref
|
||||||
end
|
end
|
||||||
|
me = Register.self_reg type
|
||||||
end
|
end
|
||||||
## need two step process, compile and save to frame
|
#move the new message (that we need to populate to make a call) to std register
|
||||||
# then move from frame to new message
|
new_message = Register.resolve_to_register(:new_message)
|
||||||
# load the new_message from message by index, simple get_slot
|
@method.source.add_code Register.get_slot(@method, :message , :next_message , new_message )
|
||||||
new_message = Register.get_slot(@method, :message , :next_message , Register.resolve_to_register(:new_message))
|
# move our receiver there
|
||||||
@method.source.add_code new_message
|
@method.source.add_code Register.set_slot( statement , me , :new_message , :receiver)
|
||||||
@method.source.add_code Virtual::Set.new( me , Virtual::NewSelf.new(me.type))
|
# load method name and set to new message (for exceptions/debug)
|
||||||
@method.source.add_code Virtual::Set.new( name.to_sym , Virtual::NewMessageName.new(:int))
|
name_tmp = use_reg(:ref)
|
||||||
compiled_args = []
|
@method.source.add_code Register::LoadConstant.new(statement, name , name_tmp)
|
||||||
|
@method.source.add_code Register.set_slot( statement , name_tmp , :new_message , :name)
|
||||||
|
# next arguments. reset tmp regs for each and load result into new_message
|
||||||
arguments.to_a.each_with_index do |arg , i|
|
arguments.to_a.each_with_index do |arg , i|
|
||||||
#compile in the running method, ie before passing control
|
reset_regs
|
||||||
|
# processing should return the register with the value
|
||||||
val = process( arg)
|
val = process( arg)
|
||||||
# move the compiled value to it's slot in the new message
|
raise "Not register #{val}" unless val.is_a?(Register::RegisterValue)
|
||||||
# + 1 as this is a ruby 0-start , but 0 is the last message ivar.
|
# which we load int the new_message at the argument's index
|
||||||
# so the next free is +1
|
@method.source.add_code Register.set_slot( statement , val , :new_message , i + 1)
|
||||||
to = Virtual::NewArgSlot.new(i + 1 ,val.type , val)
|
|
||||||
# (doing this immediately, not after the loop, so if it's a return it won't get overwritten)
|
|
||||||
@method.source.add_code Virtual::Set.new( val , to )
|
|
||||||
compiled_args << to
|
|
||||||
end
|
end
|
||||||
#method.source.add_code Virtual::MessageSend.new(name , me , compiled_args) #and pass control
|
|
||||||
|
# now we have to resolve the method name (+ receiver) into a callable method
|
||||||
method = nil
|
method = nil
|
||||||
if(me.value)
|
if(me.value)
|
||||||
|
raise "branch should only be optiisation, revit"
|
||||||
me = me.value
|
me = me.value
|
||||||
if( me.is_a? Parfait::Class )
|
if( me.is_a? Parfait::Class )
|
||||||
raise "unimplemented #{code} me is #{me}"
|
raise "unimplemented #{code} me is #{me}"
|
||||||
@ -67,9 +69,11 @@ module Phisol
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
raise "Method not implemented #{me.value}.#{name}" unless method
|
raise "Method not implemented #{me.value}.#{name}" unless method
|
||||||
|
ret = use_reg( method.source.return_type )
|
||||||
# the effect of the method is that the NewMessage Return slot will be filled, return it
|
# the effect of the method is that the NewMessage Return slot will be filled, return it
|
||||||
# (this is what is moved _inside_ above loop for such statements that are calls (or constants))
|
# but move it into a register too
|
||||||
Virtual::Return.new( method.source.return_type )
|
@method.source.add_code Register.get_slot(@method, :message , :return_value , ret )
|
||||||
|
ret
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
48
test/compiler/test_call.rb
Normal file
48
test/compiler/test_call.rb
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
require_relative "compiler_helper"
|
||||||
|
|
||||||
|
module Virtual
|
||||||
|
class TestFields < MiniTest::Test
|
||||||
|
include CompilerHelper
|
||||||
|
|
||||||
|
def setup
|
||||||
|
Virtual.machine.boot
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_call_main_plain
|
||||||
|
@root = :call_site
|
||||||
|
@string_input = <<HERE
|
||||||
|
main()
|
||||||
|
HERE
|
||||||
|
@output = Register::RegisterValue
|
||||||
|
check
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_call_main_int
|
||||||
|
@root = :call_site
|
||||||
|
@string_input = <<HERE
|
||||||
|
main(1)
|
||||||
|
HERE
|
||||||
|
@output = Register::RegisterValue
|
||||||
|
check
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_call_main_string
|
||||||
|
@root = :call_site
|
||||||
|
@string_input = <<HERE
|
||||||
|
main("1")
|
||||||
|
HERE
|
||||||
|
@output = Register::RegisterValue
|
||||||
|
check
|
||||||
|
end
|
||||||
|
|
||||||
|
def ttest_call_main_op
|
||||||
|
Virtual.machine.space.get_main.ensure_local(:bar , :int)
|
||||||
|
@root = :call_site
|
||||||
|
@string_input = <<HERE
|
||||||
|
main( bar )
|
||||||
|
HERE
|
||||||
|
@output = Register::RegisterValue
|
||||||
|
check
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue
Block a user