rubyx/lib/vm/values.rb

147 lines
4.3 KiB
Ruby
Raw Normal View History

require_relative "code"
2014-04-28 21:08:09 +02:00
module Vm
2014-05-02 07:02:25 +02:00
# Values represent the information as it is processed. Different subclasses for different types,
# each type with different operations.
# The oprerations on values is what makes a machine do things. Operations are captured as
# subclasses of Instruction and saved to Blocks
2014-05-02 07:02:25 +02:00
# Values are immutable! (that's why they are called values)
# Operations on values _always_ produce new values (conceptionally)
2014-05-03 14:13:44 +02:00
# Values are a way to reason about (create/validate) instructions.
# In fact a linked lists of values is created by invoking instructions
# the linked list goes from value to instruction to value, backwards
2014-05-02 07:02:25 +02:00
# Word Values are what fits in a register. Derived classes
# Float, Reference , Integer(s) must fit the same registers
# just a base class for data. not sure how this will be usefull (may just have read too much llvm)
class Value
def class_for clazz
2014-05-21 18:43:46 +02:00
RegisterMachine.instance.class_for(clazz)
2014-05-10 09:59:56 +02:00
end
2014-05-02 07:02:25 +02:00
end
# Just a nice way to write branches
2014-05-22 20:38:57 +02:00
class BranchCondition < Value
2014-05-22 20:38:57 +02:00
def initialize operator
@operator = operator
end
attr_accessor :operator
#needed to check the opposite, ie not true
def not_operator
case @operator
when :le
:gt
when :gt
:le
when :lt
:ge
when :eq
:ne
2014-05-22 20:38:57 +02:00
else
raise "no implemented #{@operator}"
end
end
end
# This is what it is when we don't know what it is.
# Must be promoted to A Word-Value to to anything
# remembering that our oo machine is typed, no overloading or stuff
2014-05-02 07:02:25 +02:00
class Word < Value
attr_accessor :register
def inspect
2014-05-20 10:03:18 +02:00
self.class.name + "(r#{register})"
end
def to_s
inspect
end
def initialize reg
@register = reg
2014-05-22 20:55:17 +02:00
raise inspect if reg == nil
2014-05-05 08:35:40 +02:00
end
def length
4
end
2014-04-28 21:08:09 +02:00
end
2014-05-02 07:02:25 +02:00
class Unsigned < Word
def plus block , unsigned
2014-05-21 18:43:46 +02:00
RegisterMachine.instance.unsigned_plus self , unsigned
2014-05-02 07:02:25 +02:00
end
end
class Integer < Word
2014-05-02 07:02:25 +02:00
# part of the dsl.
# Gets called with either fixnum/IntegerConstant or an Instruction (usually logic, iw add...)
# For instructions we flip, ie call the assign on the instruction
# but for constants we have to create instruction first (mov)
def assign other
other = Vm::IntegerConstant.new(other) if other.is_a? Fixnum
2014-05-21 11:42:54 +02:00
if other.is_a?(Vm::IntegerConstant) or other.is_a?(Vm::Integer)
class_for(MoveInstruction).new( self , other , :opcode => :mov)
elsif other.is_a?(Vm::StringConstant) # pc relative addressing
class_for(LogicInstruction).new(self , other , nil , opcode: :add)
2014-05-21 11:42:54 +02:00
else
other.assign(self)
end
end
def less_or_equal block , right
2014-05-21 18:43:46 +02:00
RegisterMachine.instance.integer_less_or_equal block , self , right
2014-05-06 20:36:28 +02:00
end
2014-05-28 19:10:16 +02:00
def greater_or_equal block , right
RegisterMachine.instance.integer_greater_or_equal block , self , right
end
2014-05-22 20:38:57 +02:00
def greater_than block , right
RegisterMachine.instance.integer_greater_than block , self , right
end
def less_than block , right
RegisterMachine.instance.integer_less_than block , self , right
end
2014-05-28 19:10:16 +02:00
def equals block , right
RegisterMachine.instance.integer_equals block , self , right
end
2014-05-21 11:42:54 +02:00
def == other
code = class_for(CompareInstruction).new(self , other , opcode: :cmp)
end
2014-05-19 10:28:13 +02:00
def + other
class_for(LogicInstruction).new(nil , self , other , opcode: :add)
end
def - other
2014-05-21 11:42:54 +02:00
class_for(LogicInstruction).new(nil , self , other , opcode: :sub )#, update_status: 1 )
2014-05-19 10:28:13 +02:00
end
def plus block , first , right
2014-05-21 18:43:46 +02:00
RegisterMachine.instance.integer_plus block , self , first , right
end
def minus block , first , right
2014-05-21 18:43:46 +02:00
RegisterMachine.instance.integer_minus block , self , first , right
end
def load block , right
if(right.is_a? IntegerConstant)
block.mov( self , right ) #move the value
elsif right.is_a? StringConstant
block.add( self , right , nil) #move the address, by "adding" to pc, ie pc relative
block.mov( Integer.new(register+1) , right.length ) #and the length HACK TODO
else
raise "unknown #{right.inspect}"
end
2014-05-21 20:15:23 +02:00
self
2014-05-03 14:13:44 +02:00
end
2014-05-14 11:02:54 +02:00
def move block , right
2014-05-21 20:15:23 +02:00
block.mov( self , right )
self
2014-05-14 11:02:54 +02:00
end
2014-05-02 07:02:25 +02:00
end
end
require_relative "constants"