2014-07-14 13:29:33 +02:00
|
|
|
require_relative "object"
|
|
|
|
|
2014-06-26 17:39:02 +02:00
|
|
|
module Virtual
|
2014-06-25 01:47:59 +02:00
|
|
|
|
2014-08-13 10:59:51 +02:00
|
|
|
# Instruction is an abstract for all the code of the object-machine.
|
|
|
|
# Derived classes make up the actual functionality of the machine.
|
2014-06-25 01:47:59 +02:00
|
|
|
# All functions on the machine are captured as instances of instructions
|
2014-06-26 17:39:02 +02:00
|
|
|
#
|
2014-08-13 10:59:51 +02:00
|
|
|
# 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-26 17:39:02 +02:00
|
|
|
|
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
|
2014-07-14 13:29:33 +02:00
|
|
|
class Instruction < Virtual::Object
|
2014-08-13 10:59:51 +02:00
|
|
|
|
2014-07-17 23:29:45 +02:00
|
|
|
# simple thought: don't recurse for labels, just check their names
|
|
|
|
def == other
|
|
|
|
return false unless other.class == self.class
|
|
|
|
attributes.each do |a|
|
|
|
|
left = send(a)
|
|
|
|
right = other.send(a)
|
|
|
|
return false unless left.class == right.class
|
2014-08-13 10:59:51 +02:00
|
|
|
if( left.is_a? Block)
|
2014-07-17 23:29:45 +02:00
|
|
|
return false unless left.name == right.name
|
|
|
|
else
|
|
|
|
return false unless left == right
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
2014-08-13 10:59:51 +02:00
|
|
|
|
2014-06-25 01:47:59 +02:00
|
|
|
end
|
|
|
|
|
2014-07-16 20:16:08 +02:00
|
|
|
module Named
|
2014-08-13 10:59:51 +02:00
|
|
|
def initialize name
|
2014-07-16 20:16:08 +02:00
|
|
|
@name = name
|
|
|
|
end
|
|
|
|
attr_reader :name
|
|
|
|
def attributes
|
2014-08-13 10:59:51 +02:00
|
|
|
[:name ]
|
2014-07-16 20:16:08 +02:00
|
|
|
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.
|
2014-07-16 19:16:40 +02:00
|
|
|
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
|
2014-07-16 19:16:40 +02:00
|
|
|
class MethodEnter < Instruction
|
2014-07-10 16:14:38 +02:00
|
|
|
end
|
2014-07-18 10:56:46 +02:00
|
|
|
class MethodReturn < Instruction
|
|
|
|
end
|
2014-07-10 16:14:38 +02:00
|
|
|
|
2014-08-13 19:05:32 +02:00
|
|
|
# a branch must branch to a block. This is an abstract class, names indicate the actual test
|
2014-07-16 19:16:40 +02:00
|
|
|
class Branch < Instruction
|
2014-08-13 19:05:32 +02:00
|
|
|
def initialize to
|
|
|
|
@to = to
|
2014-07-16 19:16:40 +02:00
|
|
|
end
|
2014-08-13 19:05:32 +02:00
|
|
|
attr_reader :to
|
2014-07-16 19:16:40 +02:00
|
|
|
def attributes
|
2014-08-13 19:05:32 +02:00
|
|
|
[:to]
|
2014-07-16 19:16:40 +02:00
|
|
|
end
|
|
|
|
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
|
|
|
|
|
2014-08-13 19:05:32 +02:00
|
|
|
class UnconditionalBranch < Branch
|
|
|
|
end
|
|
|
|
|
2014-07-25 19:28:38 +02:00
|
|
|
class MessageGet < Instruction
|
|
|
|
include Named
|
|
|
|
end
|
2014-07-10 16:14:38 +02:00
|
|
|
class FrameGet < Instruction
|
2014-07-16 20:16:08 +02:00
|
|
|
include Named
|
2014-07-10 16:14:38 +02:00
|
|
|
end
|
|
|
|
|
2014-07-25 19:28:38 +02:00
|
|
|
class MessageSend < Instruction
|
2014-08-13 10:59:51 +02:00
|
|
|
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
|
2014-07-14 20:28:21 +02:00
|
|
|
def attributes
|
2014-08-13 10:59:51 +02:00
|
|
|
[:name , :args ]
|
2014-07-10 16:14:38 +02:00
|
|
|
end
|
2014-07-14 20:28:21 +02:00
|
|
|
end
|
|
|
|
|
2014-07-15 17:27:13 +02:00
|
|
|
class FrameSet < Instruction
|
2014-08-13 10:59:51 +02:00
|
|
|
def initialize name , val
|
2014-07-14 20:28:21 +02:00
|
|
|
@name = name.to_sym
|
|
|
|
@value = val
|
|
|
|
end
|
|
|
|
attr_reader :name , :value
|
|
|
|
def attributes
|
2014-08-13 10:59:51 +02:00
|
|
|
[:name , :value]
|
2014-07-15 17:27:13 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-07-25 19:28:38 +02:00
|
|
|
class MessageSet < Instruction
|
2014-08-13 10:59:51 +02:00
|
|
|
def initialize name , val
|
2014-07-25 19:28:38 +02:00
|
|
|
@name = name.to_sym
|
|
|
|
@value = val
|
|
|
|
end
|
|
|
|
attr_reader :name , :value
|
|
|
|
def attributes
|
2014-08-13 10:59:51 +02:00
|
|
|
[:name , :value]
|
2014-07-25 19:28:38 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-07-15 17:27:13 +02:00
|
|
|
class LoadSelf < Instruction
|
2014-08-13 10:59:51 +02:00
|
|
|
def initialize val
|
2014-07-15 17:27:13 +02:00
|
|
|
@value = val
|
|
|
|
end
|
|
|
|
attr_reader :value
|
|
|
|
def attributes
|
2014-08-13 10:59:51 +02:00
|
|
|
[:value]
|
2014-07-10 16:14:38 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-07-14 20:28:21 +02:00
|
|
|
class ObjectGet < Instruction
|
2014-07-16 20:16:08 +02:00
|
|
|
include Named
|
2014-07-14 20:28:21 +02:00
|
|
|
end
|
2014-06-25 01:47:59 +02:00
|
|
|
end
|