2014-05-13 15:24:19 +02:00
|
|
|
module Vm
|
|
|
|
|
|
|
|
# constants are the stuff that you embedd in the program as numbers or strings.
|
|
|
|
# Another way to think about them is as Operands, they have no seperate "identity"
|
|
|
|
# and usually end up embedded in the instructions. ie your basic foo + 4 will encode
|
|
|
|
# the 4 in the instruction opcode. The 4 is not accessible anywhere else.
|
|
|
|
# When it should be usable in other forms, the constant must become a Value first
|
2014-05-21 18:41:51 +02:00
|
|
|
class Constant < Code
|
2014-05-13 15:24:19 +02:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2014-06-07 16:59:44 +02:00
|
|
|
# another abstract "marker" class (so we can check for it)
|
|
|
|
# derived classes are Boot/Meta Clas and StringConstant
|
|
|
|
class ObjectConstant < Constant
|
|
|
|
end
|
2014-05-13 15:24:19 +02:00
|
|
|
|
|
|
|
class IntegerConstant < Constant
|
2014-05-13 17:21:24 +02:00
|
|
|
def initialize int
|
2014-05-13 15:24:19 +02:00
|
|
|
@integer = int
|
|
|
|
end
|
|
|
|
attr_reader :integer
|
2014-06-05 17:17:00 +02:00
|
|
|
def value
|
|
|
|
@integer
|
|
|
|
end
|
2014-06-12 20:40:25 +02:00
|
|
|
def to_asm
|
|
|
|
@integer.to_s
|
|
|
|
end
|
2014-05-13 15:24:19 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# The name really says it all.
|
|
|
|
# The only interesting thing is storage.
|
|
|
|
# Currently string are stored "inline" , ie in the code segment.
|
|
|
|
# Mainly because that works an i aint no elf expert.
|
|
|
|
|
2014-06-07 16:59:44 +02:00
|
|
|
class StringConstant < ObjectConstant
|
2014-05-13 15:24:19 +02:00
|
|
|
attr_reader :string
|
|
|
|
# currently aligned to 4 (ie padded with 0) and off course 0 at the end
|
|
|
|
def initialize str
|
2014-06-03 21:16:57 +02:00
|
|
|
str = str.to_s if str.is_a? Symbol
|
2014-05-13 15:24:19 +02:00
|
|
|
length = str.length
|
|
|
|
# rounding up to the next 4 (always adding one for zero pad)
|
|
|
|
pad = ((length / 4 ) + 1 ) * 4 - length
|
|
|
|
raise "#{pad} #{self}" unless pad >= 1
|
2014-05-28 13:27:37 +02:00
|
|
|
@string = str + " " * pad
|
2014-05-13 15:24:19 +02:00
|
|
|
end
|
|
|
|
|
2014-05-19 14:44:12 +02:00
|
|
|
def result= value
|
|
|
|
class_for(MoveInstruction).new(value , self , :opcode => :mov)
|
2014-05-13 15:24:19 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# the strings length plus padding
|
|
|
|
def length
|
|
|
|
string.length
|
|
|
|
end
|
|
|
|
|
|
|
|
# just writing the string
|
|
|
|
def assemble(io)
|
|
|
|
io << string
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|