move return address generation to factory

removes the list from space
adds a ReturnAddress factory instead
and uses these throughout
This commit is contained in:
Torsten Ruger 2018-08-29 21:02:49 +03:00
parent f993ccefe3
commit c983dcf0eb
7 changed files with 25 additions and 35 deletions

View File

@ -1,13 +1,12 @@
# A Space is a collection of pages. It stores objects, the data for the objects, # The Space is the root object we work off, the only singleton in the parfait world
# not references. See Page for more detail. #
# Space stores the types, classes, factories and singleton objects (true/false/nil)
# Pages are stored by the object size they represent in a hash. #
# The Space is booted at compile time, a process outside the scope of Parfait(in parfait_boot)
# Space and Page work together in making *new* objects available. # Then it is used during compilation and later serialized into the resulting binary
# "New" is slightly misleading in that normal operation only ever #
# recycles objects. #
module Parfait module Parfait
# Make the object space globally available # Make the object space globally available
def self.object_space def self.object_space
@ -32,8 +31,7 @@ module Parfait
class Space < Object class Space < Object
attr :type, :classes , :types , :factories attr :type, :classes , :types , :factories
attr :next_message , :next_address attr :next_message , :messages
attr :messages, :addresses
attr :true_object , :false_object , :nil_object attr :true_object , :false_object , :nil_object
def initialize( classes ) def initialize( classes )
@ -43,8 +41,8 @@ module Parfait
add_type(cl.instance_type) add_type(cl.instance_type)
end end
self.factories = Dictionary.new self.factories = Dictionary.new
factories[ :Integer ] = Factory.new( classes[:Integer].instance_type) factories[ :Integer ] = Factory.new( classes[:Integer].instance_type).get_more
400.times { self.addresses = ReturnAddress.new(0,self.addresses) } factories[ :ReturnAddress ] = Factory.new( classes[:ReturnAddress].instance_type).get_more
message = Message.new(nil) message = Message.new(nil)
50.times do 50.times do
self.messages = Message.new( message ) self.messages = Message.new( message )
@ -52,7 +50,6 @@ module Parfait
message = self.messages message = self.messages
end end
self.next_message = self.messages self.next_message = self.messages
self.next_address = self.addresses
self.true_object = Parfait::TrueClass.new self.true_object = Parfait::TrueClass.new
self.false_object = Parfait::FalseClass.new self.false_object = Parfait::FalseClass.new
self.nil_object = Parfait::NilClass.new self.nil_object = Parfait::NilClass.new
@ -77,13 +74,6 @@ module Parfait
factories[name].get_next_object factories[name].get_next_object
end end
# hand out a return address for use as constant the address is added
def get_address
addr = next_address
self.next_address = next_address.next_integer
addr
end
# yield each type in the space # yield each type in the space
def each_type def each_type
types.values.each do |type| types.values.each do |type|

View File

@ -65,7 +65,7 @@ module Risc
# An integer is plucked from object_space abd added to the machine constant pool # An integer is plucked from object_space abd added to the machine constant pool
# if none was given # if none was given
def self.label( source , name , position = nil , nekst = nil) def self.label( source , name , position = nil , nekst = nil)
position = Parfait.object_space.get_address unless position position = Parfait.object_space.get_next_for(:ReturnAddress) unless position
Label.new( source , name , position, nekst ) Label.new( source , name , position, nekst )
end end
end end

View File

@ -160,12 +160,11 @@ module Parfait
NamedList: {}, NamedList: {},
NilClass: {}, NilClass: {},
Object: {}, Object: {},
Factory: { for_type: :Type , next_object: :Object , last_object: :Object , Factory: { for_type: :Type , next_object: :Object ,
reserve: :Object , attribute_name: :Word }, reserve: :Object , attribute_name: :Word },
ReturnAddress: {next_integer: :ReturnAddress}, ReturnAddress: {next_integer: :ReturnAddress},
Space: {classes: :Dictionary , types: :Dictionary , factories: :Dictionary, Space: {classes: :Dictionary , types: :Dictionary , factories: :Dictionary,
next_message: :Message , messages: :Message , next_message: :Message , messages: :Message ,
next_address: :ReturnAddress ,addresses: :ReturnAddress ,
true_object: :TrueClass, false_object: :FalseClass , nil_object: :NilClass}, true_object: :TrueClass, false_object: :FalseClass , nil_object: :NilClass},
TrueClass: {}, TrueClass: {},
Type: {names: :List , types: :List , Type: {names: :List , types: :List ,

View File

@ -11,7 +11,7 @@ module Parfait
end end
def test_space_length def test_space_length
assert_equal 11 , @space.get_type.instance_length , @space.get_type.inspect assert_equal 9 , @space.get_type.instance_length , @space.get_type.inspect
end end
def test_singletons def test_singletons
assert @space.true_object , "No truth" assert @space.true_object , "No truth"
@ -92,7 +92,7 @@ module Parfait
assert_equal Dictionary , @space.factories.class assert_equal Dictionary , @space.factories.class
end end
def test_factory_length def test_factory_length
assert_equal 1 , @space.factories.length assert_equal 2 , @space.factories.length
end end
def test_has_integer_factory def test_has_integer_factory
ints = @space.get_factory_for(:Integer) ints = @space.get_factory_for(:Integer)
@ -110,20 +110,21 @@ module Parfait
assert_equal Parfait::Integer , nekst.class assert_equal Parfait::Integer , nekst.class
end end
def test_has_addresses def test_has_addresses
assert_equal Parfait::ReturnAddress , @space.next_address.class ret = @space.get_next_for(:ReturnAddress)
assert_equal 0 , @space.next_address.value assert_equal Parfait::ReturnAddress , ret.class
assert_nil ret.value
end end
def test_has_next_address def test_has_next_address
assert_equal Parfait::ReturnAddress , @space.next_address.next_integer.class assert_equal Parfait::ReturnAddress , @space.get_next_for(:ReturnAddress).class
end end
def test_address_count def test_address_count
addr = @space.addresses addr = @space.get_next_for(:ReturnAddress)
count = 0 count = 0
while(addr) while(addr)
count += 1 count += 1
addr = addr.next_integer addr = addr.next_integer
end end
assert_equal 400, count assert_equal 1014, count
end end
def test_messages def test_messages
mess = @space.messages mess = @space.messages

View File

@ -27,7 +27,7 @@ module Parfait
assert_equal Parfait::Space , space.class assert_equal Parfait::Space , space.class
type = space.get_type type = space.get_type
assert_equal Parfait::Type , type.class assert_equal Parfait::Type , type.class
assert_equal 11 , type.names.get_length assert_equal 9 , type.names.get_length
assert_equal type.object_class.class , Parfait::Class assert_equal type.object_class.class , Parfait::Class
assert_equal type.object_class.name , :Space assert_equal type.object_class.name , :Space
end end

View File

@ -54,7 +54,7 @@ module Risc
end end
def test_pc1 def test_pc1
@interpreter.tick @interpreter.tick
assert_equal 41592 , @interpreter.pc assert_equal 49992 , @interpreter.pc
end end
def test_tick2 def test_tick2
@interpreter.tick @interpreter.tick
@ -68,7 +68,7 @@ module Risc
def test_pc2 def test_pc2
@interpreter.tick @interpreter.tick
@interpreter.tick @interpreter.tick
assert_equal 41596 , @interpreter.pc assert_equal 49996 , @interpreter.pc
end end
def test_tick_14_jump def test_tick_14_jump
14.times {@interpreter.tick} 14.times {@interpreter.tick}

View File

@ -25,7 +25,7 @@ module Risc
assert_equal 0 , Position.get(@linker.cpu_init).at assert_equal 0 , Position.get(@linker.cpu_init).at
end end
def test_cpu_at def test_cpu_at
assert_equal "0xb12c" , Position.get(@linker.cpu_init.first).to_s assert_equal "0xd1fc" , Position.get(@linker.cpu_init.first).to_s
end end
def test_cpu_label def test_cpu_label
assert_equal Position , Position.get(@linker.cpu_init.first).class assert_equal Position , Position.get(@linker.cpu_init.first).class