rubyx/lib/vm/instruction.rb

56 lines
1.5 KiB
Ruby
Raw Normal View History

2014-05-03 21:18:04 +02:00
require_relative "code"
require "support/hash_attributes"
2014-05-02 07:02:25 +02:00
module Vm
2014-05-03 14:13:44 +02:00
2014-05-03 21:18:04 +02:00
# Because the idea of what one instruction does, does not always map one to one to real machine
2014-05-03 14:13:44 +02:00
# instructions, and instruction may link to another instruction thus creating an arbitrary list
# to get the job (the original instruciton) done
2014-05-03 21:18:04 +02:00
2014-05-03 14:13:44 +02:00
# Admittately it would be simpler just to create the (abstract) instructions and let the machine
# encode them into what-ever is neccessary, but this approach leaves more possibility to
# optimize the actual instruction stream (not just the crystal instruction stream). Makes sense?
# We have basic classes (literally) of instructions
# - Memory
# - Stack
# - Logic
# - Math
# - Control/Compare
# - Move
# - Call
# Instruction derives from Code, for the assembly api
2014-05-03 21:18:04 +02:00
2014-05-03 14:13:44 +02:00
class Instruction < Code
2014-05-03 21:18:04 +02:00
# Make hash attributes to object attributes
include Support::HashAttributes
2014-05-03 14:13:44 +02:00
2014-05-03 21:18:04 +02:00
def initialize options
2014-05-05 14:59:29 +02:00
@attributes = options
2014-05-03 21:18:04 +02:00
end
2014-05-02 07:02:25 +02:00
end
2014-05-03 14:13:44 +02:00
class StackInstruction < Instruction
end
class MemoryInstruction < Instruction
end
class LogicInstruction < Instruction
end
class MathInstruction < Instruction
end
class CompareInstruction < Instruction
end
class MoveInstruction < Instruction
end
class CallInstruction < Instruction
def initialize options
super
opcode = @attributes[:opcode].to_s
if opcode.length == 3 and opcode[0] == "b"
@attributes[:condition_code] = opcode[1,2].to_sym
@attributes[:opcode] = :b
end
end
2014-05-03 14:13:44 +02:00
end
2014-05-02 07:02:25 +02:00
end