rubyx/lib/virtual/instruction.rb

94 lines
2.9 KiB
Ruby
Raw Normal View History

require_relative "object"
module Virtual
# Instruction is an abstract for all the code of the object-machine.
# Derived classes make up the actual functionality of the machine.
# 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,
# thus defining a minimal set of instructions needed to implement oo.
2014-06-29 18:05:35 +02:00
# This is partly because jumping over this layer and doing in straight in assember was too big a step
class Instruction < Virtual::Object
# simple thought: don't recurse for Blocks, just check their names
def == other
return false unless other.class == self.class
Sof::Util.attributes(self).each do |a|
begin
left = send(a)
rescue NoMethodError
next # not using instance variables that are not defined as attr_readers for equality
end
begin
right = other.send(a)
rescue NoMethodError
return false
end
return false unless left.class == right.class
if( left.is_a? Block)
return false unless left.name == right.name
else
return false unless left == right
end
end
return true
end
end
2014-06-29 18:05:35 +02:00
# 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
2014-06-29 18:05:35 +02:00
end
2014-07-10 16:14:38 +02:00
# following classes are stubs. currently in brainstorming mode, so anything may change anytime
class MethodEnter < Instruction
2014-07-10 16:14:38 +02:00
end
class MethodReturn < Instruction
end
2014-07-10 16:14:38 +02:00
# 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 ImplicitBranch < Branch
end
class UnconditionalBranch < Branch
end
2014-07-25 19:28:38 +02:00
class MessageSend < Instruction
def initialize name , args = []
2014-07-10 16:14:38 +02:00
@name = name.to_sym
2014-07-13 15:00:48 +02:00
@args = args
2014-07-10 16:14:38 +02:00
end
2014-07-13 15:00:48 +02:00
attr_reader :name , :args
end
# 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), or from may be a constant
class Set < Instruction
def initialize to , from
@to = to
@from = from
2014-07-15 17:27:13 +02:00
end
attr_reader :to , :from
2014-07-10 16:14:38 +02:00
end
2014-08-20 21:33:08 +02:00
class ObjectGet < Instruction
def initialize name
@name = name.to_sym
end
attr_reader :name
end
end