adds message get and set instructions

This commit is contained in:
Torsten Ruger 2014-07-25 20:28:38 +03:00
parent 7df1490da8
commit 1a95835442
5 changed files with 39 additions and 10 deletions

View File

@ -51,7 +51,7 @@ Then a new Method receives the message, creates a Frame for local and temporary
The important thing here is that Messages and Frames are normal objects. The important thing here is that Messages and Frames are normal objects.
Anf interestingly we can partly use ruby to find the method, so in a way it is not just a top down transformation. but And interestingly we can partly use ruby to find the method, so in a way it is not just a top down transformation. but
the sending goes back up and then down again. the sending goes back up and then down again.
The Message object is the second parameter to the compile method, the run-time part as it were. The Message object is the second parameter to the compile method, the run-time part as it were.

View File

@ -103,12 +103,14 @@ module Virtual
class ImplicitBranch < Branch class ImplicitBranch < Branch
end end
# A note: future branch conditions include OverflowBranch and other non-c class MessageGet < Instruction
include Named
end
class FrameGet < Instruction class FrameGet < Instruction
include Named include Named
end end
class FrameSend < Instruction class MessageSend < Instruction
def initialize name , args = [] , nex = nil def initialize name , args = [] , nex = nil
super(nex) super(nex)
@name = name.to_sym @name = name.to_sym
@ -132,6 +134,18 @@ module Virtual
end end
end end
class MessageSet < Instruction
def initialize name , val , nex = nil
super(nex)
@name = name.to_sym
@value = val
end
attr_reader :name , :value
def attributes
[:name , :value] + super
end
end
class LoadSelf < Instruction class LoadSelf < Instruction
def initialize val , nex = nil def initialize val , nex = nil
super(nex) super(nex)

View File

@ -36,19 +36,27 @@ module Virtual
end end
# #
def compile_get method , name def compile_get method , name
if method.has_arg(name)
method.add MessageGet.new(name)
else
method.add FrameGet.new(name) method.add FrameGet.new(name)
end
method.get_var(name) method.get_var(name)
end end
def compile_send method , name , me , with = [] def compile_send method , name , me , with = []
method.add Virtual::LoadSelf.new(me) method.add Virtual::LoadSelf.new(me)
method.add FrameSend.new(name , with ) method.add MessageSend.new(name , with )
Return.new( method.return_type ) Return.new( method.return_type )
end end
def compile_set method , name , val def compile_set method , name , val
method.set_var(name,val) method.set_var(name,val)
if method.has_arg(name)
method.add MessageSet.new(name , val )
else
method.add FrameSet.new(name , val ) method.add FrameSet.new(name , val )
end
method.get_var(name) method.get_var(name)
end end
end end

View File

@ -49,6 +49,13 @@ module Virtual
var var
end end
# determine whether this method has an argument by the name
def has_arg name
name = name.to_sym
var = @args.find {|a| a.name == name }
var
end
def set_var name , var def set_var name , var
v = has_var name v = has_var name
if( v ) if( v )

View File

@ -30,7 +30,7 @@ def foo(x)
2 + 5 2 + 5
end end
HERE HERE
@output ="---RETURN_MARKER- !ruby/object:Virtual::MethodDefinitionRETURN_MARKER name: :fooRETURN_MARKER args:RETURN_MARKER - !ruby/object:Virtual::ArgumentRETURN_MARKER name: :xRETURN_MARKER type: !ruby/object:Virtual::Mystery {}RETURN_MARKER locals:RETURN_MARKER - !ruby/object:Virtual::LocalRETURN_MARKER name: :abbaRETURN_MARKER type: &1 !ruby/object:Virtual::IntegerConstantRETURN_MARKER integer: 5RETURN_MARKER tmps: []RETURN_MARKER receiver: !ruby/object:Virtual::SelfReferenceRETURN_MARKER clazz: RETURN_MARKER return_type: !ruby/object:Virtual::ReturnRETURN_MARKER name: :returnRETURN_MARKER type: !ruby/class 'Virtual::Mystery'RETURN_MARKER start: !ruby/object:Virtual::MethodEnterRETURN_MARKER next: !ruby/object:Virtual::FrameSetRETURN_MARKER next: !ruby/object:Virtual::LoadSelfRETURN_MARKER next: &2 !ruby/object:Virtual::FrameSendRETURN_MARKER next: !ruby/object:Virtual::MethodReturnRETURN_MARKER next: RETURN_MARKER name: :+RETURN_MARKER args:RETURN_MARKER - !ruby/object:Virtual::IntegerConstantRETURN_MARKER integer: 5RETURN_MARKER value: !ruby/object:Virtual::IntegerConstantRETURN_MARKER integer: 2RETURN_MARKER name: :abbaRETURN_MARKER value: *1RETURN_MARKER current: *2RETURN_MARKER" @output ="---RETURN_MARKER- !ruby/object:Virtual::MethodDefinitionRETURN_MARKER name: :fooRETURN_MARKER args:RETURN_MARKER - !ruby/object:Virtual::ArgumentRETURN_MARKER name: :xRETURN_MARKER type: !ruby/object:Virtual::Mystery {}RETURN_MARKER locals:RETURN_MARKER - !ruby/object:Virtual::LocalRETURN_MARKER name: :abbaRETURN_MARKER type: &1 !ruby/object:Virtual::IntegerConstantRETURN_MARKER integer: 5RETURN_MARKER tmps: []RETURN_MARKER receiver: !ruby/object:Virtual::SelfReferenceRETURN_MARKER clazz: RETURN_MARKER return_type: !ruby/object:Virtual::ReturnRETURN_MARKER name: :returnRETURN_MARKER type: !ruby/class 'Virtual::Mystery'RETURN_MARKER start: !ruby/object:Virtual::MethodEnterRETURN_MARKER next: !ruby/object:Virtual::FrameSetRETURN_MARKER next: !ruby/object:Virtual::LoadSelfRETURN_MARKER next: &2 !ruby/object:Virtual::MessageSendRETURN_MARKER next: !ruby/object:Virtual::MethodReturnRETURN_MARKER next: RETURN_MARKER name: :+RETURN_MARKER args:RETURN_MARKER - !ruby/object:Virtual::IntegerConstantRETURN_MARKER integer: 5RETURN_MARKER value: !ruby/object:Virtual::IntegerConstantRETURN_MARKER integer: 2RETURN_MARKER name: :abbaRETURN_MARKER value: *1RETURN_MARKER current: *2RETURN_MARKER"
check check
end end
@ -40,7 +40,7 @@ def foo()
2 + 5 2 + 5
end end
HERE HERE
@output = "---RETURN_MARKER- !ruby/object:Virtual::MethodDefinitionRETURN_MARKER name: :fooRETURN_MARKER args: []RETURN_MARKER locals: []RETURN_MARKER tmps: []RETURN_MARKER receiver: !ruby/object:Virtual::SelfReferenceRETURN_MARKER clazz: RETURN_MARKER return_type: !ruby/object:Virtual::ReturnRETURN_MARKER name: :returnRETURN_MARKER type: !ruby/class 'Virtual::Mystery'RETURN_MARKER start: !ruby/object:Virtual::MethodEnterRETURN_MARKER next: !ruby/object:Virtual::LoadSelfRETURN_MARKER next: &1 !ruby/object:Virtual::FrameSendRETURN_MARKER next: !ruby/object:Virtual::MethodReturnRETURN_MARKER next: RETURN_MARKER name: :+RETURN_MARKER args:RETURN_MARKER - !ruby/object:Virtual::IntegerConstantRETURN_MARKER integer: 5RETURN_MARKER value: !ruby/object:Virtual::IntegerConstantRETURN_MARKER integer: 2RETURN_MARKER current: *1RETURN_MARKER" @output = "---RETURN_MARKER- !ruby/object:Virtual::MethodDefinitionRETURN_MARKER name: :fooRETURN_MARKER args: []RETURN_MARKER locals: []RETURN_MARKER tmps: []RETURN_MARKER receiver: !ruby/object:Virtual::SelfReferenceRETURN_MARKER clazz: RETURN_MARKER return_type: !ruby/object:Virtual::ReturnRETURN_MARKER name: :returnRETURN_MARKER type: !ruby/class 'Virtual::Mystery'RETURN_MARKER start: !ruby/object:Virtual::MethodEnterRETURN_MARKER next: !ruby/object:Virtual::LoadSelfRETURN_MARKER next: &1 !ruby/object:Virtual::MessageSendRETURN_MARKER next: !ruby/object:Virtual::MethodReturnRETURN_MARKER next: RETURN_MARKER name: :+RETURN_MARKER args:RETURN_MARKER - !ruby/object:Virtual::IntegerConstantRETURN_MARKER integer: 5RETURN_MARKER value: !ruby/object:Virtual::IntegerConstantRETURN_MARKER integer: 2RETURN_MARKER current: *1RETURN_MARKER"
check check
end end
@ -68,7 +68,7 @@ def fibonaccit(n)
end end
end end
HERE HERE
@output = "---RETURN_MARKER- !ruby/object:Virtual::MethodDefinitionRETURN_MARKER name: :fibonaccitRETURN_MARKER args:RETURN_MARKER - !ruby/object:Virtual::ArgumentRETURN_MARKER name: :nRETURN_MARKER type: !ruby/object:Virtual::Mystery {}RETURN_MARKER locals:RETURN_MARKER - !ruby/object:Virtual::LocalRETURN_MARKER name: :aRETURN_MARKER type: &2 !ruby/object:Virtual::IntegerConstantRETURN_MARKER integer: 0RETURN_MARKER - &4 !ruby/object:Virtual::LocalRETURN_MARKER name: :someRETURN_MARKER type: &3 !ruby/object:Virtual::IntegerConstantRETURN_MARKER integer: 43RETURN_MARKER - &1 !ruby/object:Virtual::LocalRETURN_MARKER name: :otherRETURN_MARKER type: &5 !ruby/object:Virtual::ReturnRETURN_MARKER name: :returnRETURN_MARKER type: !ruby/class 'Virtual::Mystery'RETURN_MARKER tmps: []RETURN_MARKER receiver: !ruby/object:Virtual::SelfReferenceRETURN_MARKER clazz: RETURN_MARKER return_type: *1RETURN_MARKER start: !ruby/object:Virtual::MethodEnterRETURN_MARKER next: !ruby/object:Virtual::FrameSetRETURN_MARKER name: :aRETURN_MARKER value: *2RETURN_MARKER next: &6 !ruby/object:Virtual::LabelRETURN_MARKER name: while_startRETURN_MARKER next: !ruby/object:Virtual::FrameGetRETURN_MARKER name: :nRETURN_MARKER next: !ruby/object:Virtual::ImplicitBranchRETURN_MARKER name: :while_39010RETURN_MARKER other: &7 !ruby/object:Virtual::LabelRETURN_MARKER name: :while_39010RETURN_MARKER next: !ruby/object:Virtual::MethodReturnRETURN_MARKER next: RETURN_MARKER next: !ruby/object:Virtual::FrameSetRETURN_MARKER name: :someRETURN_MARKER value: *3RETURN_MARKER next: !ruby/object:Virtual::FrameGetRETURN_MARKER name: :someRETURN_MARKER next: !ruby/object:Virtual::LoadSelfRETURN_MARKER value: *4RETURN_MARKER next: !ruby/object:Virtual::FrameSendRETURN_MARKER name: :*RETURN_MARKER args:RETURN_MARKER - !ruby/object:Virtual::IntegerConstantRETURN_MARKER integer: 4RETURN_MARKER next: !ruby/object:Virtual::FrameSetRETURN_MARKER name: :otherRETURN_MARKER value: *5RETURN_MARKER next: *6RETURN_MARKER current: *7RETURN_MARKER" @output = "---RETURN_MARKER- !ruby/object:Virtual::MethodDefinitionRETURN_MARKER name: :fibonaccitRETURN_MARKER args:RETURN_MARKER - !ruby/object:Virtual::ArgumentRETURN_MARKER name: :nRETURN_MARKER type: !ruby/object:Virtual::Mystery {}RETURN_MARKER locals:RETURN_MARKER - !ruby/object:Virtual::LocalRETURN_MARKER name: :aRETURN_MARKER type: &2 !ruby/object:Virtual::IntegerConstantRETURN_MARKER integer: 0RETURN_MARKER - &4 !ruby/object:Virtual::LocalRETURN_MARKER name: :someRETURN_MARKER type: &3 !ruby/object:Virtual::IntegerConstantRETURN_MARKER integer: 43RETURN_MARKER - &1 !ruby/object:Virtual::LocalRETURN_MARKER name: :otherRETURN_MARKER type: &5 !ruby/object:Virtual::ReturnRETURN_MARKER name: :returnRETURN_MARKER type: !ruby/class 'Virtual::Mystery'RETURN_MARKER tmps: []RETURN_MARKER receiver: !ruby/object:Virtual::SelfReferenceRETURN_MARKER clazz: RETURN_MARKER return_type: *1RETURN_MARKER start: !ruby/object:Virtual::MethodEnterRETURN_MARKER next: !ruby/object:Virtual::FrameSetRETURN_MARKER name: :aRETURN_MARKER value: *2RETURN_MARKER next: &6 !ruby/object:Virtual::LabelRETURN_MARKER name: while_startRETURN_MARKER next: !ruby/object:Virtual::FrameGetRETURN_MARKER name: :nRETURN_MARKER next: !ruby/object:Virtual::ImplicitBranchRETURN_MARKER name: :while_39010RETURN_MARKER other: &7 !ruby/object:Virtual::LabelRETURN_MARKER name: :while_39010RETURN_MARKER next: !ruby/object:Virtual::MethodReturnRETURN_MARKER next: RETURN_MARKER next: !ruby/object:Virtual::FrameSetRETURN_MARKER name: :someRETURN_MARKER value: *3RETURN_MARKER next: !ruby/object:Virtual::FrameGetRETURN_MARKER name: :someRETURN_MARKER next: !ruby/object:Virtual::LoadSelfRETURN_MARKER value: *4RETURN_MARKER next: !ruby/object:Virtual::MessageSendRETURN_MARKER name: :*RETURN_MARKER args:RETURN_MARKER - !ruby/object:Virtual::IntegerConstantRETURN_MARKER integer: 4RETURN_MARKER next: !ruby/object:Virtual::FrameSetRETURN_MARKER name: :otherRETURN_MARKER value: *5RETURN_MARKER next: *6RETURN_MARKER current: *7RETURN_MARKER"
check check
end end
@ -93,7 +93,7 @@ def retvar(n)
end end
end end
HERE HERE
@output = "---RETURN_MARKER- !ruby/object:Virtual::MethodDefinitionRETURN_MARKER name: :retvarRETURN_MARKER args:RETURN_MARKER - &2 !ruby/object:Virtual::ArgumentRETURN_MARKER name: :nRETURN_MARKER type: !ruby/object:Virtual::Mystery {}RETURN_MARKER locals: []RETURN_MARKER tmps: []RETURN_MARKER receiver: !ruby/object:Virtual::SelfReferenceRETURN_MARKER clazz: RETURN_MARKER return_type: !ruby/object:Virtual::ReferenceRETURN_MARKER clazz: RETURN_MARKER start: !ruby/object:Virtual::MethodEnterRETURN_MARKER next: !ruby/object:Virtual::FrameGetRETURN_MARKER next: !ruby/object:Virtual::LoadSelfRETURN_MARKER next: !ruby/object:Virtual::FrameSendRETURN_MARKER next: !ruby/object:Virtual::ImplicitBranchRETURN_MARKER next: &1 !ruby/object:Virtual::LabelRETURN_MARKER next: RETURN_MARKER name: :if_merge_5246RETURN_MARKER name: :if_merge_5246RETURN_MARKER other: *1RETURN_MARKER name: :>RETURN_MARKER args:RETURN_MARKER - !ruby/object:Virtual::IntegerConstantRETURN_MARKER integer: 5RETURN_MARKER value: *2RETURN_MARKER name: :nRETURN_MARKER current: *1RETURN_MARKER" @output = "---RETURN_MARKER- !ruby/object:Virtual::MethodDefinitionRETURN_MARKER name: :retvarRETURN_MARKER args:RETURN_MARKER - &1 !ruby/object:Virtual::ArgumentRETURN_MARKER name: :nRETURN_MARKER type: !ruby/object:Virtual::Mystery {}RETURN_MARKER locals: []RETURN_MARKER tmps: []RETURN_MARKER receiver: !ruby/object:Virtual::SelfReferenceRETURN_MARKER clazz: RETURN_MARKER return_type: !ruby/object:Virtual::ReferenceRETURN_MARKER clazz: RETURN_MARKER start: !ruby/object:Virtual::MethodEnterRETURN_MARKER next: !ruby/object:Virtual::MessageGetRETURN_MARKER name: :nRETURN_MARKER next: !ruby/object:Virtual::LoadSelfRETURN_MARKER value: *1RETURN_MARKER next: !ruby/object:Virtual::MessageSendRETURN_MARKER name: :>RETURN_MARKER args:RETURN_MARKER - !ruby/object:Virtual::IntegerConstantRETURN_MARKER integer: 5RETURN_MARKER next: !ruby/object:Virtual::ImplicitBranchRETURN_MARKER name: :if_merge_5246RETURN_MARKER other: &2 !ruby/object:Virtual::LabelRETURN_MARKER name: :if_merge_5246RETURN_MARKER next: RETURN_MARKER next: *2RETURN_MARKER current: *2RETURN_MARKER"
check check
end end