2014-09-16 15:06:56 +02:00
|
|
|
require_relative "type"
|
2015-05-12 08:54:36 +02:00
|
|
|
require "parfait"
|
2014-09-16 15:06:56 +02:00
|
|
|
|
2014-09-25 19:30:02 +02:00
|
|
|
module Positioned
|
|
|
|
def position
|
2014-10-07 11:01:33 +02:00
|
|
|
raise "position accessed but not set at #{length} for #{self.inspect[0...500]}" if @position == nil
|
2014-09-25 19:30:02 +02:00
|
|
|
@position
|
|
|
|
end
|
|
|
|
def set_position pos
|
2015-05-13 15:06:38 +02:00
|
|
|
# resetting of position used to be error, but since relink and dynamic instruction size it is ok.
|
|
|
|
# in measures (of 32)
|
2014-09-28 10:18:24 +02:00
|
|
|
if @position != nil and ((@position - pos).abs > 32)
|
2015-05-04 10:10:40 +02:00
|
|
|
raise "position set again #{pos}!=#{@position} for #{self}"
|
2014-09-25 19:30:02 +02:00
|
|
|
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
|
2015-05-04 10:10:40 +02:00
|
|
|
#
|
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
|
2015-05-12 14:36:44 +02:00
|
|
|
# Space ObjectSpace
|
2014-08-28 07:24:37 +02:00
|
|
|
# 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-30 11:08:12 +02:00
|
|
|
def to_s
|
2014-10-07 11:21:40 +02:00
|
|
|
inspect[0..300]
|
2014-09-30 11:08:12 +02:00
|
|
|
end
|
2014-09-16 16:16:56 +02:00
|
|
|
def mem_length
|
2014-10-05 00:13:57 +02:00
|
|
|
raise "abstract #{self.class}"
|
2014-09-16 16:16:56 +02:00
|
|
|
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
|
2015-05-04 10:10:40 +02:00
|
|
|
@@HASH.merge :keys => object.keys , :values => object.values
|
2014-09-16 15:06:56 +02:00
|
|
|
when Virtual::BootClass
|
|
|
|
@@CLAZZ
|
2015-05-12 14:36:44 +02:00
|
|
|
when Virtual::Space
|
2014-09-16 15:06:56 +02:00
|
|
|
@@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
|
2015-05-12 08:44:34 +02:00
|
|
|
::Parfait::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
|
2015-05-12 08:44:34 +02:00
|
|
|
::Parfait::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
|
2015-05-12 18:10:45 +02:00
|
|
|
Parfait::Dictionary.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
|
2015-05-12 18:10:45 +02:00
|
|
|
::Parfait::List.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
|
2015-05-13 15:06:38 +02:00
|
|
|
::Parfait::Word.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
|