create one file per instruction
This commit is contained in:
parent
4f1508ca61
commit
1feed6af44
@ -1,20 +1,20 @@
|
|||||||
require_relative "object"
|
require_relative "object"
|
||||||
|
|
||||||
module Virtual
|
module Virtual
|
||||||
|
|
||||||
# Instruction is an abstract for all the code of the object-machine.
|
# Instruction is an abstract for all the code of the object-machine.
|
||||||
# Derived classes make up the actual functionality of the machine.
|
# Derived classes make up the actual functionality of the machine.
|
||||||
# All functions on the machine are captured as instances of instructions
|
# All functions on the machine are captured as instances of instructions
|
||||||
#
|
#
|
||||||
# It is actually the point of the virtual machine layer to express oo functionality in the set of instructions,
|
# It is actually the point of the virtual machine layer to express oo functionality in the set of instructions,
|
||||||
# thus defining a minimal set of instructions needed to implement oo.
|
# thus defining a minimal set of instructions needed to implement oo.
|
||||||
|
|
||||||
# This is partly because jumping over this layer and doing in straight in assember was too big a step
|
# This is partly because jumping over this layer and doing in straight in assember was too big a step
|
||||||
class Instruction < Virtual::Object
|
class Instruction < Virtual::Object
|
||||||
|
|
||||||
# simple thought: don't recurse for Blocks, just check their names
|
# simple thought: don't recurse for Blocks, just check their names
|
||||||
def == other
|
def == other
|
||||||
return false unless other.class == self.class
|
return false unless other.class == self.class
|
||||||
Sof::Util.attributes(self).each do |a|
|
Sof::Util.attributes(self).each do |a|
|
||||||
begin
|
begin
|
||||||
left = send(a)
|
left = send(a)
|
||||||
@ -26,7 +26,7 @@ module Virtual
|
|||||||
rescue NoMethodError
|
rescue NoMethodError
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
return false unless left.class == right.class
|
return false unless left.class == right.class
|
||||||
if( left.is_a? Block)
|
if( left.is_a? Block)
|
||||||
return false unless left.name == right.name
|
return false unless left.name == right.name
|
||||||
else
|
else
|
||||||
@ -39,6 +39,13 @@ module Virtual
|
|||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
require_relative "instructions/access"
|
require_relative "instructions/branch"
|
||||||
require_relative "instructions/control"
|
require_relative "instructions/halt"
|
||||||
require_relative "instructions/messaging"
|
require_relative "instructions/instance_get"
|
||||||
|
require_relative "instructions/message_send"
|
||||||
|
require_relative "instructions/method_call"
|
||||||
|
require_relative "instructions/method_enter"
|
||||||
|
require_relative "instructions/method_return"
|
||||||
|
require_relative "instructions/new_frame"
|
||||||
|
require_relative "instructions/new_message"
|
||||||
|
require_relative "instructions/set"
|
||||||
|
14
lib/virtual/instructions/branch.rb
Normal file
14
lib/virtual/instructions/branch.rb
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
module Virtual
|
||||||
|
|
||||||
|
|
||||||
|
# a branch must branch to a block. This is an abstract class, names indicate the actual test
|
||||||
|
class Branch < Instruction
|
||||||
|
def initialize to
|
||||||
|
@to = to
|
||||||
|
end
|
||||||
|
attr_reader :to
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
require_relative "is_true_branch"
|
||||||
|
require_relative "unconditional_branch"
|
@ -1,40 +0,0 @@
|
|||||||
module Virtual
|
|
||||||
|
|
||||||
|
|
||||||
# the first instruction we need is to stop. Off course in a real machine this would be a syscall, but that is just
|
|
||||||
# an implementation (in a programm it would be a function).
|
|
||||||
# But in a virtual machine, not only do we need this instruction,
|
|
||||||
# it is indeed the first instruction as just this instruction is the smallest possible programm for the machine.
|
|
||||||
# As such it is the next instruction for any first instruction that we generate.
|
|
||||||
class Halt < Instruction
|
|
||||||
end
|
|
||||||
|
|
||||||
# MethodCall involves shuffling some registers about and doing a machine call
|
|
||||||
class MethodCall < Instruction
|
|
||||||
def initialize method
|
|
||||||
@method = method
|
|
||||||
end
|
|
||||||
attr_reader :method
|
|
||||||
end
|
|
||||||
|
|
||||||
# also the return shuffles our objects beck before actually transferring control
|
|
||||||
class MethodReturn < Instruction
|
|
||||||
end
|
|
||||||
|
|
||||||
# a branch must branch to a block. This is an abstract class, names indicate the actual test
|
|
||||||
class Branch < Instruction
|
|
||||||
def initialize to
|
|
||||||
@to = to
|
|
||||||
end
|
|
||||||
attr_reader :to
|
|
||||||
end
|
|
||||||
|
|
||||||
# implicit means there is no explcit test involved.
|
|
||||||
# normal ruby rules are false and nil are false, EVERYTHING else is true (and that includes 0)
|
|
||||||
class IsTrueBranch < Branch
|
|
||||||
end
|
|
||||||
|
|
||||||
class UnconditionalBranch < Branch
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
11
lib/virtual/instructions/halt.rb
Normal file
11
lib/virtual/instructions/halt.rb
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
module Virtual
|
||||||
|
|
||||||
|
|
||||||
|
# the first instruction we need is to stop. Off course in a real machine this would be a syscall, but that is just
|
||||||
|
# an implementation (in a programm it would be a function).
|
||||||
|
# But in a virtual machine, not only do we need this instruction,
|
||||||
|
# it is indeed the first instruction as just this instruction is the smallest possible programm for the machine.
|
||||||
|
# As such it is the next instruction for any first instruction that we generate.
|
||||||
|
class Halt < Instruction
|
||||||
|
end
|
||||||
|
end
|
11
lib/virtual/instructions/instance_get.rb
Normal file
11
lib/virtual/instructions/instance_get.rb
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
module Virtual
|
||||||
|
|
||||||
|
# Get a instance variable by _name_ . So we have to resolve the name to an index to trnsform into a Slot
|
||||||
|
# The slot may the be used in a set on left or right hand. The transformation is done by GetImplementation
|
||||||
|
class InstanceGet < Instruction
|
||||||
|
def initialize name
|
||||||
|
@name = name.to_sym
|
||||||
|
end
|
||||||
|
attr_reader :name
|
||||||
|
end
|
||||||
|
end
|
8
lib/virtual/instructions/is_true_branch.rb
Normal file
8
lib/virtual/instructions/is_true_branch.rb
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
module Virtual
|
||||||
|
|
||||||
|
# implicit means there is no explcit test involved.
|
||||||
|
# normal ruby rules are false and nil are false, EVERYTHING else is true (and that includes 0)
|
||||||
|
class IsTrueBranch < Branch
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
12
lib/virtual/instructions/message_send.rb
Normal file
12
lib/virtual/instructions/message_send.rb
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
module Virtual
|
||||||
|
|
||||||
|
class MessageSend < Instruction
|
||||||
|
def initialize name , me , args = []
|
||||||
|
@name = name.to_sym
|
||||||
|
@me = me
|
||||||
|
@args = args
|
||||||
|
end
|
||||||
|
attr_reader :name , :me , :args
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
@ -1,21 +0,0 @@
|
|||||||
module Virtual
|
|
||||||
|
|
||||||
# following classes are stubs. currently in brainstorming mode, so anything may change anytime
|
|
||||||
class MethodEnter < Instruction
|
|
||||||
end
|
|
||||||
|
|
||||||
class NewMessage < Instruction
|
|
||||||
end
|
|
||||||
class NewFrame < Instruction
|
|
||||||
end
|
|
||||||
|
|
||||||
class MessageSend < Instruction
|
|
||||||
def initialize name , me , args = []
|
|
||||||
@name = name.to_sym
|
|
||||||
@me = me
|
|
||||||
@args = args
|
|
||||||
end
|
|
||||||
attr_reader :name , :me , :args
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
12
lib/virtual/instructions/method_call.rb
Normal file
12
lib/virtual/instructions/method_call.rb
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
module Virtual
|
||||||
|
|
||||||
|
|
||||||
|
# MethodCall involves shuffling some registers about and doing a machine call
|
||||||
|
class MethodCall < Instruction
|
||||||
|
def initialize method
|
||||||
|
@method = method
|
||||||
|
end
|
||||||
|
attr_reader :method
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
7
lib/virtual/instructions/method_enter.rb
Normal file
7
lib/virtual/instructions/method_enter.rb
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
module Virtual
|
||||||
|
|
||||||
|
# following classes are stubs. currently in brainstorming mode, so anything may change anytime
|
||||||
|
class MethodEnter < Instruction
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
8
lib/virtual/instructions/method_return.rb
Normal file
8
lib/virtual/instructions/method_return.rb
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
module Virtual
|
||||||
|
|
||||||
|
|
||||||
|
# also the return shuffles our objects beck before actually transferring control
|
||||||
|
class MethodReturn < Instruction
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
6
lib/virtual/instructions/new_frame.rb
Normal file
6
lib/virtual/instructions/new_frame.rb
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
module Virtual
|
||||||
|
|
||||||
|
class NewFrame < Instruction
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
5
lib/virtual/instructions/new_message.rb
Normal file
5
lib/virtual/instructions/new_message.rb
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
module Virtual
|
||||||
|
|
||||||
|
class NewMessage < Instruction
|
||||||
|
end
|
||||||
|
end
|
@ -2,7 +2,7 @@ module Virtual
|
|||||||
|
|
||||||
# class for Set instructions, A set is basically a mem move.
|
# class for Set instructions, A set is basically a mem move.
|
||||||
# to and from are indexes into the known objects(frame,message,self and new_message), these are represented as slots
|
# to and from are indexes into the known objects(frame,message,self and new_message), these are represented as slots
|
||||||
# (see there)
|
# (see there)
|
||||||
# from may be a Constant (Object,Integer,String,Class)
|
# from may be a Constant (Object,Integer,String,Class)
|
||||||
class Set < Instruction
|
class Set < Instruction
|
||||||
def initialize to , from
|
def initialize to , from
|
||||||
@ -14,12 +14,4 @@ module Virtual
|
|||||||
attr_reader :to , :from
|
attr_reader :to , :from
|
||||||
end
|
end
|
||||||
|
|
||||||
# Get a instance variable by _name_ . So we have to resolve the name to an index to trnsform into a Slot
|
|
||||||
# The slot may the be used in a set on left or right hand. The transformation is done by GetImplementation
|
|
||||||
class InstanceGet < Instruction
|
|
||||||
def initialize name
|
|
||||||
@name = name.to_sym
|
|
||||||
end
|
|
||||||
attr_reader :name
|
|
||||||
end
|
|
||||||
end
|
end
|
6
lib/virtual/instructions/unconditional_branch.rb
Normal file
6
lib/virtual/instructions/unconditional_branch.rb
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
module Virtual
|
||||||
|
|
||||||
|
class UnconditionalBranch < Branch
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
Loading…
Reference in New Issue
Block a user