2014-09-16 15:06:56 +02:00
|
|
|
require_relative "type"
|
2014-09-24 17:25:18 +02:00
|
|
|
require "parfait/message"
|
2014-09-16 15:06:56 +02:00
|
|
|
|
2014-09-25 19:30:02 +02:00
|
|
|
module Positioned
|
|
|
|
def position
|
|
|
|
raise "position accessed but not set at #{length} for #{self.inspect}" if @position == nil
|
|
|
|
@position
|
|
|
|
end
|
|
|
|
def set_position pos
|
|
|
|
# resetting of position used to be error, but since relink and dynamic instruction size it is ok. in measures
|
|
|
|
if @position != nil and ((@position - pos).abs > 15)
|
|
|
|
raise "position set again #{pos}!=#{@position} for #{self}"
|
|
|
|
end
|
|
|
|
@position = pos
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-06-26 17:39:02 +02:00
|
|
|
module Virtual
|
|
|
|
# our machine is made up of objects, some of which are code, some data
|
|
|
|
#
|
|
|
|
# during compilation objects are module Virtual objects, but during execution they are not scoped
|
|
|
|
#
|
2014-08-28 07:24:37 +02:00
|
|
|
# So compiling/linking/assembly turns ::virtual objects into binary that represents ruby objects at runtime
|
|
|
|
# The equivalence is listed below (i'll try and work on clearer correspondence later)
|
|
|
|
# ::Virtual Runtime / parfait
|
|
|
|
# Object Object
|
|
|
|
# BootClass Class
|
|
|
|
# MetaClass self/Object
|
|
|
|
# BootSpace ObjectSpace
|
|
|
|
# CompiledMethod Function
|
|
|
|
# (ruby)Array Array
|
|
|
|
# String String
|
2014-06-26 17:39:02 +02:00
|
|
|
class Object
|
2014-09-25 19:30:02 +02:00
|
|
|
include Positioned
|
2014-09-16 15:06:56 +02:00
|
|
|
def initialize
|
2014-09-16 16:16:56 +02:00
|
|
|
@position = nil
|
2014-09-16 15:06:56 +02:00
|
|
|
@length = -1
|
2014-06-26 17:39:02 +02:00
|
|
|
end
|
2014-09-16 16:16:56 +02:00
|
|
|
attr_accessor :length , :layout
|
2014-07-14 10:29:38 +02:00
|
|
|
def inspect
|
2014-08-22 09:21:12 +02:00
|
|
|
Sof::Writer.write(self)
|
2014-07-14 10:29:38 +02:00
|
|
|
end
|
2014-09-16 16:16:56 +02:00
|
|
|
def mem_length
|
|
|
|
raise "abstract #{self}"
|
|
|
|
end
|
2014-09-16 15:06:56 +02:00
|
|
|
@@EMPTY = { :names => [] , :types => []}
|
|
|
|
def layout
|
|
|
|
raise "Find me #{self}"
|
|
|
|
self.class.layout
|
|
|
|
end
|
|
|
|
def self.layout
|
|
|
|
@@EMPTY
|
|
|
|
end
|
|
|
|
# class variables to have _identical_ objects passed back (stops recursion)
|
|
|
|
@@ARRAY = { :names => [] , :types => []}
|
|
|
|
# @@HASH = { :names => [:keys,:values] , :types => [Virtual::Reference,Virtual::Reference]}
|
|
|
|
# @@CLAZZ = { :names => [:name , :super_class_name , :instance_methods] , :types => [Virtual::Reference,Virtual::Reference,Virtual::Reference]}
|
|
|
|
# @@SPACE = { :names => [:classes,:objects] , :types => [Virtual::Reference,Virtual::Reference]}
|
|
|
|
|
|
|
|
def layout_for(object)
|
|
|
|
case object
|
|
|
|
when Array , Symbol , String , Virtual::CompiledMethod , Virtual::Block , Virtual::StringConstant
|
|
|
|
@@ARRAY
|
|
|
|
when Hash
|
|
|
|
@@HASH.merge :keys => object.keys , :values => object.values
|
|
|
|
when Virtual::BootClass
|
|
|
|
@@CLAZZ
|
|
|
|
when Virtual::BootSpace
|
|
|
|
@@SPACE
|
|
|
|
else
|
|
|
|
raise "linker encounters unknown class #{object.class}"
|
|
|
|
end
|
|
|
|
end
|
2014-09-16 16:16:56 +02:00
|
|
|
# objects only come in lengths of multiple of 8 words
|
|
|
|
# but there is a constant overhead of 2 words, one for type, one for layout
|
|
|
|
# and as we would have to subtract 1 to make it work without overhead, we now have to add 7
|
|
|
|
def padded len
|
|
|
|
a = 32 * (1 + (len + 7)/32 )
|
|
|
|
#puts "#{a} for #{len}"
|
|
|
|
a
|
|
|
|
end
|
|
|
|
|
|
|
|
def padded_words words
|
|
|
|
padded(words*4) # 4 == word length, a constant waiting for a home
|
|
|
|
end
|
2014-09-16 15:06:56 +02:00
|
|
|
end
|
|
|
|
end
|
2014-09-24 17:25:18 +02:00
|
|
|
::Message.class_eval do
|
2014-09-25 19:30:02 +02:00
|
|
|
include Positioned
|
2014-09-24 17:25:18 +02:00
|
|
|
def layout
|
|
|
|
Virtual::Object.layout
|
|
|
|
end
|
|
|
|
def mem_length
|
|
|
|
Virtual::Object.new.padded_words(2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
::Frame.class_eval do
|
2014-09-25 19:30:02 +02:00
|
|
|
include Positioned
|
2014-09-24 17:25:18 +02:00
|
|
|
def layout
|
|
|
|
Virtual::Object.layout
|
|
|
|
end
|
|
|
|
def mem_length
|
|
|
|
Virtual::Object.new.padded_words(2)
|
|
|
|
end
|
|
|
|
end
|
2014-09-16 15:06:56 +02:00
|
|
|
Parfait::Hash.class_eval do
|
2014-09-25 19:30:02 +02:00
|
|
|
include Positioned
|
2014-09-17 11:04:54 +02:00
|
|
|
HASH = { :names => [:keys,:values] , :types => [Virtual::Reference,Virtual::Reference]}
|
2014-09-16 15:06:56 +02:00
|
|
|
def layout
|
2014-09-17 11:04:54 +02:00
|
|
|
HASH
|
2014-09-16 15:06:56 +02:00
|
|
|
end
|
2014-09-16 16:16:56 +02:00
|
|
|
def mem_length
|
|
|
|
Virtual::Object.new.padded_words(2)
|
|
|
|
end
|
2014-09-16 15:06:56 +02:00
|
|
|
end
|
|
|
|
Array.class_eval do
|
2014-09-25 19:30:02 +02:00
|
|
|
include Positioned
|
2014-09-16 15:06:56 +02:00
|
|
|
def layout
|
|
|
|
Virtual::Object.layout
|
|
|
|
end
|
2014-09-16 16:16:56 +02:00
|
|
|
def mem_length
|
|
|
|
Virtual::Object.new.padded_words(length())
|
|
|
|
end
|
2014-09-16 15:06:56 +02:00
|
|
|
end
|
|
|
|
Symbol.class_eval do
|
2014-09-25 19:30:02 +02:00
|
|
|
include Positioned
|
2014-09-16 16:16:56 +02:00
|
|
|
def layout
|
|
|
|
Virtual::Object.layout
|
|
|
|
end
|
|
|
|
def mem_length
|
|
|
|
Virtual::Object.new.padded(1 + to_s.length())
|
|
|
|
end
|
|
|
|
end
|
|
|
|
String.class_eval do
|
2014-09-25 19:30:02 +02:00
|
|
|
include Positioned
|
2014-09-16 15:06:56 +02:00
|
|
|
def layout
|
|
|
|
Virtual::Object.layout
|
2014-06-26 17:39:02 +02:00
|
|
|
end
|
2014-09-16 16:16:56 +02:00
|
|
|
def mem_length
|
|
|
|
Virtual::Object.new.padded(1 + length())
|
|
|
|
end
|
2014-06-26 17:39:02 +02:00
|
|
|
end
|