fixed layouts

also moved objects to machine, from space
space has a list of objects, but implicit,
not as an explicit array
This commit is contained in:
Torsten Ruger 2015-05-31 13:02:29 +03:00
parent aaa206fbca
commit 03bdc16810
8 changed files with 69 additions and 75 deletions

View File

@ -18,7 +18,7 @@ module Elf
@object.add_section @text @object.add_section @text
@object_machine.run_passes @object_machine.run_passes
assembler = Register::Assembler.new(@object_machine.space) assembler = Register::Assembler.new(@object_machine)
set_text assembler.assemble set_text assembler.assemble
# for debug add labels to the block positions # for debug add labels to the block positions

View File

@ -54,9 +54,14 @@ module Parfait
internal_object_set(LAYOUT_INDEX , layout) internal_object_set(LAYOUT_INDEX , layout)
end end
# so we can keep the raise in get_layout
def has_layout?
! internal_object_get(LAYOUT_INDEX).nil?
end
def get_layout() def get_layout()
l = internal_object_get(LAYOUT_INDEX) l = internal_object_get(LAYOUT_INDEX)
raise "No layout #{self.class}:#{self.to_s} #{self.object_id}" unless l raise "No layout #{self.class}:#{self.class} #{self.object_id}" unless l
return l return l
end end

View File

@ -27,13 +27,8 @@ module Parfait
super() super()
Parfait::Space.set_object_space self Parfait::Space.set_object_space self
@classes = Parfait::Dictionary.new_object @classes = Parfait::Dictionary.new_object
# this is like asking for troubles, but if the space instance is not registered
# and the @classes up, one can not register classes.
# all is good after this init
#global objects (data)
@objects = Parfait::List.new_object
end end
attr_reader :classes , :objects , :frames, :messages, :next_message , :next_frame attr_reader :classes , :frames, :messages, :next_message , :next_frame
# need a two phase init for the object space (and generally parfait) because the space # need a two phase init for the object space (and generally parfait) because the space
# is an interconnected graph, so not everthing is ready # is an interconnected graph, so not everthing is ready
@ -50,25 +45,6 @@ module Parfait
@next_frame = @frames.first @next_frame = @frames.first
end end
# double check that all objects dependents are really in the space too (debugging)
def double_check
@objects.each do |o|
check o
end
end
# private
def check object , recurse = true
raise "No good #{self.class}" unless @objects.include? object
puts "#{object.class}"
puts "#{object}" if object.class == Parfait::Word
check object.get_layout
return unless recurse
object.get_layout.each do |name|
check name , false
inst = object.instance_variable_get "@#{name}".to_sym
check inst , false
end
end
@@object_space = nil @@object_space = nil
# Make the object space globally available # Make the object space globally available
def self.object_space def self.object_space
@ -79,12 +55,6 @@ module Parfait
@@object_space = space @@object_space = space
end end
# Objects are data and get assembled after functions
def add_object o
return if @objects.include?(o)
@objects.push o
end
def get_main def get_main
kernel = get_class_by_name "Object" kernel = get_class_by_name "Object"
kernel.get_instance_method "main" kernel.get_instance_method "main"

View File

@ -1,7 +1,7 @@
module Register module Register
class LinkException < Exception class LinkException < Exception
end end
# Assemble the object space into a binary. # Assemble the object machine into a binary.
# Link first to get positions, then assemble # Link first to get positions, then assemble
# The link function determines the length of an object and the assemble actually # The link function determines the length of an object and the assemble actually
@ -14,28 +14,27 @@ module Register
TYPE_BITS = 4 TYPE_BITS = 4
TYPE_LENGTH = 6 TYPE_LENGTH = 6
def initialize space def initialize machine
@space = space @machine = machine
end end
attr_reader :objects
def link def link
# want to have the methods first in the executable # want to have the methods first in the executable
# so first we determine the code length for the methods and set the # so first we determine the code length for the methods and set the
# binary code (array) to right length # binary code (array) to right length
@space.objects.each do |objekt| @machine.objects.each do |objekt|
next unless objekt.is_a? Parfait::Method next unless objekt.is_a? Parfait::Method
objekt.code.set_length(objekt.info.mem_length / 4 , 0) objekt.code.set_length(objekt.info.mem_length / 4 , 0)
end end
at = 0 at = 0
# then we make sure we really get the binary codes first # then we make sure we really get the binary codes first
@space.objects.each do |objekt| @machine.objects.each do |objekt|
next unless objekt.is_a? Parfait::BinaryCode next unless objekt.is_a? Parfait::BinaryCode
objekt.set_position at objekt.set_position at
at += objekt.mem_length at += objekt.mem_length
end end
# and then everything else # and then everything else
@space.objects.each do | objekt| @machine.objects.each do | objekt|
# have to tell the code that will be assembled where it is to # have to tell the code that will be assembled where it is to
# get the jumps/calls right # get the jumps/calls right
if objekt.is_a? Parfait::Method if objekt.is_a? Parfait::Method
@ -52,23 +51,23 @@ module Register
begin begin
link link
# first we need to create the binary code for the methods # first we need to create the binary code for the methods
@space.objects.each do |objekt| @machine.objects.each do |objekt|
next unless objekt.is_a? Parfait::Method next unless objekt.is_a? Parfait::Method
assemble_binary_method(objekt) assemble_binary_method(objekt)
end end
@stream = StringIO.new @stream = StringIO.new
#TODOmid , main = @objects.find{|k,objekt| objekt.is_a?(Virtual::CompiledMethod) and (objekt.name == :__init__ )} #TODOmid , main = @objects.find{|k,objekt| objekt.is_a?(Virtual::CompiledMethod) and (objekt.name == :__init__ )}
# initial_jump = @space.init # initial_jump = @machine.init
# initial_jump.codes.each do |code| # initial_jump.codes.each do |code|
# code.assemble( @stream ) # code.assemble( @stream )
# end # end
# then write the methods to file # then write the methods to file
@space.objects.each do |objekt| @machine.objects.each do |objekt|
next unless objekt.is_a? Parfait::BinaryCode next unless objekt.is_a? Parfait::BinaryCode
assemble_any( objekt ) assemble_any( objekt )
end end
# and then the rest of the object space # and then the rest of the object machine
@space.objects.each do | objekt| @machine.objects.each do | objekt|
next if objekt.is_a? Parfait::BinaryCode next if objekt.is_a? Parfait::BinaryCode
assemble_any( objekt ) assemble_any( objekt )
end end
@ -150,8 +149,8 @@ module Register
assemble_self( hash , [ hash.keys , hash.values ] ) assemble_self( hash , [ hash.keys , hash.values ] )
end end
def assemble_Space(space) def assemble_Space(machine)
assemble_self(space , [space.classes,space.objects, space.symbols,space.messages,space.next_message,space.next_frame] ) assemble_self(machine , [machine.classes,machine.messages,machine.next_message,machine.next_frame] )
end end
def assemble_Class(clazz) def assemble_Class(clazz)

View File

@ -27,7 +27,7 @@ module Virtual
"List" => [] , "List" => [] ,
"Message" => [], "Message" => [],
"BinaryCode" => [], "BinaryCode" => [],
"Space" => ["classes","objects","frames","messages","next_message","next_frame"], "Space" => ["classes","frames","messages","next_message","next_frame"],
"Frame" => ["locals" , "tmps" ], "Frame" => ["locals" , "tmps" ],
"Layout" => ["object_class"] , "Layout" => ["object_class"] ,
"Class" => ["object_layout"], "Class" => ["object_layout"],
@ -65,19 +65,17 @@ module Virtual
class_mappings["Kernel"] = value_classes[2] #need for further booting class_mappings["Kernel"] = value_classes[2] #need for further booting
class_mappings["Object"] = value_classes[3] #need for further booting class_mappings["Object"] = value_classes[3] #need for further booting
# add space and instances which get created before the objects list
[@space,@space.classes,@space.classes.keys, @space.classes.values,@space.objects].each do |o|
@space.add_object o
end
@space.late_init @space.late_init
# add_object @space
values.each {|v| v.init_layout } values.each {|v| v.init_layout }
# now update the layout on all objects created so far, # now update the layout on all objects created so far,
# go through objects in space # go through objects in space
@space.objects.each do | o | @objects.each do | o |
o.init_layout o.init_layout
end end
@space.double_check
boot_functions! boot_functions!
end end

View File

@ -38,8 +38,9 @@ module Virtual
def initialize def initialize
@parser = Parser::Salama.new @parser = Parser::Salama.new
@passes = [ "Virtual::SendImplementation" ] @passes = [ "Virtual::SendImplementation" ]
@objects = []
end end
attr_reader :passes , :space , :class_mappings , :init attr_reader :passes , :space , :class_mappings , :init , :objects
def run_passes def run_passes
Minimizer.new.run Minimizer.new.run
@ -62,6 +63,34 @@ module Virtual
end end
end end
# double check that all objects dependents are really in the space too (debugging)
def double_check
@objects.each do |o|
check o
end
end
# Objects are data and get assembled after functions
def add_object o
return false if @objects.include?(o)
@objects.push o
true
end
# private
def check object , recurse = true
raise "No good #{object.class}" unless @objects.include? object
puts "#{object.class}"
puts "#{object}" if object.class == Parfait::Word
check object.get_layout
return unless recurse
object.get_layout.each do |name|
check name , false
inst = object.instance_variable_get "@#{name}".to_sym
check inst , false
end
end
# Passes may be added to by anyone who wants # Passes may be added to by anyone who wants
# This is intentionally quite flexible, though one sometimes has to watch the order of them # This is intentionally quite flexible, though one sometimes has to watch the order of them
# most ordering is achieved by ordering the requires and using add_pass # most ordering is achieved by ordering the requires and using add_pass

View File

@ -10,12 +10,7 @@ module FakeMem
@memory = [0,nil] @memory = [0,nil]
@position = nil @position = nil
@length = -1 @length = -1
if Parfait::Space.object_space and Parfait::Space.object_space.objects # Virtual::Machine.instance.add_object self
Parfait::Space.object_space.add_object self
else
# Note: the else is handled in boot, by ading the space "by hand", as it slips though
# puts "Got away #{self.class}"
end
if Virtual::Machine.instance.class_mappings if Virtual::Machine.instance.class_mappings
init_layout init_layout
else else

View File

@ -1,24 +1,22 @@
module Virtual module Virtual
# garbage collect anything that is in the space but not reachable from init # collect anything that is in the space but and reachable from init
class Collector class Collector
def run def run
@keepers = [] # init= Parfait::Space.object_space.get_class_by_name("Kernel").get_instance_method "__init__"
init= Parfait::Space.object_space.get_class_by_name("Kernel").get_instance_method "__init__" keep Parfait::Space.object_space
keep init
end end
def keep object def keep object
return if @keepers.include? object return if object.nil?
layout = object.get_layout return unless Machine.instance.add_object object
begin # puts "adding #{object.class}"
puts "Object #{object.class} #{Parfait::Space.object_space.objects.include?(object)}" unless object.has_layout?
puts "Object #{layout.object_id} #{Parfait::Space.object_space.objects.include?(layout)}" object.init_layout
keep layout
rescue => e
puts "for #{object.name}"
raise e
end end
layout = object.get_layout
puts "Layout #{layout.get_object_class.name} #{Machine.instance.objects.include?(layout)}"
keep layout
layout.each do |name| layout.each do |name|
inst = object.instance_variable_get "@#{name}".to_sym inst = object.instance_variable_get "@#{name}".to_sym
keep inst keep inst