extract padding functions to module
and finally TEST them its especially the brain bending stuff that needs tests
This commit is contained in:
parent
4ddfcc4900
commit
30d9aaf61b
@ -9,6 +9,7 @@ module Register
|
|||||||
# functions on the objects, but now it has gone to a visitor pattern.
|
# functions on the objects, but now it has gone to a visitor pattern.
|
||||||
|
|
||||||
class Assembler
|
class Assembler
|
||||||
|
include Padding
|
||||||
TYPE_REF = 0
|
TYPE_REF = 0
|
||||||
TYPE_INT = 1
|
TYPE_INT = 1
|
||||||
TYPE_BITS = 4
|
TYPE_BITS = 4
|
||||||
@ -49,9 +50,12 @@ module Register
|
|||||||
|
|
||||||
def assemble
|
def assemble
|
||||||
# must be same order as link
|
# must be same order as link
|
||||||
|
|
||||||
begin
|
begin
|
||||||
link
|
link
|
||||||
@machine.objects.each do |objekt|
|
all= @machine.objects.sort{|a,b| a.position <=> b.position}
|
||||||
|
all.each do |objekt|
|
||||||
|
puts "Linked #{objekt.class}(#{objekt.object_id.to_s(16)}) at #{objekt.position.to_s(16)} / #{objekt.mem_length.to_s(16)}"
|
||||||
objekt.position
|
objekt.position
|
||||||
end
|
end
|
||||||
# first we need to create the binary code for the methods
|
# first we need to create the binary code for the methods
|
||||||
@ -76,6 +80,7 @@ module Register
|
|||||||
assemble_any( objekt )
|
assemble_any( objekt )
|
||||||
end
|
end
|
||||||
rescue LinkException
|
rescue LinkException
|
||||||
|
puts "RELINK"
|
||||||
# knowing that we fix the problem, we hope to get away with retry.
|
# knowing that we fix the problem, we hope to get away with retry.
|
||||||
retry
|
retry
|
||||||
end
|
end
|
||||||
@ -111,9 +116,9 @@ module Register
|
|||||||
end
|
end
|
||||||
|
|
||||||
def assemble_any obj
|
def assemble_any obj
|
||||||
puts "Assemble #{obj.class} at stream #{(@stream.length).to_s(16)} pos:#{obj.position.to_s(16)} , len:#{obj.mem_length}"
|
puts "Assemble #{obj.class}(#{obj.object_id.to_s(16)}) at stream #{(@stream.length).to_s(16)} pos:#{obj.position.to_s(16)} , len:#{obj.mem_length}"
|
||||||
if @stream.length != obj.position
|
if @stream.length != obj.position
|
||||||
raise "Assemble #{obj.class} at #{@stream.length.to_s(16)} not #{obj.position.to_s(16)}"
|
raise "Assemble #{obj.class} #{obj.object_id.to_s(16)} at #{@stream.length.to_s(16)} not #{obj.position.to_s(16)}"
|
||||||
end
|
end
|
||||||
clazz = obj.class.name.split("::").last
|
clazz = obj.class.name.split("::").last
|
||||||
send("assemble_#{clazz}".to_sym , obj)
|
send("assemble_#{clazz}".to_sym , obj)
|
||||||
@ -149,7 +154,8 @@ module Register
|
|||||||
puts "Nil for #{object.class}.#{var}" unless inst
|
puts "Nil for #{object.class}.#{var}" unless inst
|
||||||
write_ref_for(inst)
|
write_ref_for(inst)
|
||||||
end
|
end
|
||||||
pad_after( layout.get_length * 4 )
|
puts "layout leng=#{layout.get_length.to_s(16)} mem_len=#{layout.mem_length.to_s(16)}"
|
||||||
|
pad_after( layout.mem_length )
|
||||||
object.position
|
object.position
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -185,6 +191,7 @@ module Register
|
|||||||
end
|
end
|
||||||
|
|
||||||
def assemble_Method(method)
|
def assemble_Method(method)
|
||||||
|
raise "no"
|
||||||
count = method.info.blocks.inject(0) { |c , block| c += block.mem_length }
|
count = method.info.blocks.inject(0) { |c , block| c += block.mem_length }
|
||||||
word = (count+7) / 32 # all object are multiple of 8 words (7 for header)
|
word = (count+7) / 32 # all object are multiple of 8 words (7 for header)
|
||||||
raise "Method too long, splitting not implemented #{method.name}/#{count}" if word > 15
|
raise "Method too long, splitting not implemented #{method.name}/#{count}" if word > 15
|
||||||
@ -236,26 +243,12 @@ module Register
|
|||||||
@stream.write_sint32 object.position
|
@stream.write_sint32 object.position
|
||||||
end
|
end
|
||||||
|
|
||||||
# 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
|
|
||||||
|
|
||||||
# pad_after is always in bytes and pads (writes 0's) up to the next 8 word boundary
|
# pad_after is always in bytes and pads (writes 0's) up to the next 8 word boundary
|
||||||
def pad_after length
|
def pad_after length
|
||||||
pad = padded(length) - length - 8 # for header, type and layout
|
pad = padding_for(lenght)
|
||||||
pad.times do
|
pad.times do
|
||||||
@stream.write_uint8(0)
|
@stream.write_uint8(0)
|
||||||
end
|
end
|
||||||
#puts "padded #{length} with #{pad} stream pos #{@stream.length.to_s(16)}"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
@ -2,6 +2,7 @@ require "parfait"
|
|||||||
require "virtual/machine"
|
require "virtual/machine"
|
||||||
#if we are in the ruby run-time / generating an executable
|
#if we are in the ruby run-time / generating an executable
|
||||||
require "virtual/positioned"
|
require "virtual/positioned"
|
||||||
|
require "virtual/padding"
|
||||||
require "virtual/parfait_adapter"
|
require "virtual/parfait_adapter"
|
||||||
|
|
||||||
require "virtual/compiler"
|
require "virtual/compiler"
|
||||||
|
@ -140,12 +140,6 @@ module Virtual
|
|||||||
padded(l)
|
padded(l)
|
||||||
end
|
end
|
||||||
|
|
||||||
def padded len
|
|
||||||
a = 32 * (1 + (len + 7)/32 )
|
|
||||||
#puts "#{a} for #{len}"
|
|
||||||
a
|
|
||||||
end
|
|
||||||
|
|
||||||
# position of the function is the position of the entry block, is where we call
|
# position of the function is the position of the entry block, is where we call
|
||||||
def set_position at
|
def set_position at
|
||||||
at += 8 #for the 2 header words
|
at += 8 #for the 2 header words
|
||||||
|
22
lib/virtual/padding.rb
Normal file
22
lib/virtual/padding.rb
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
|
||||||
|
module Padding
|
||||||
|
|
||||||
|
# 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
|
||||||
|
|
||||||
|
def padding_for length
|
||||||
|
pad = padded(length) - length - 8 # for header, type and layout
|
||||||
|
puts "padded #{length.to_s(16)} with #{pad.to_s(16)} stream pos #{@stream.length.to_s(16)}"
|
||||||
|
pad
|
||||||
|
end
|
||||||
|
end
|
@ -39,18 +39,6 @@ module FakeMem
|
|||||||
end
|
end
|
||||||
@position = pos
|
@position = pos
|
||||||
end
|
end
|
||||||
# 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
|
|
||||||
end
|
end
|
||||||
module Virtual
|
module Virtual
|
||||||
def self.new_list array
|
def self.new_list array
|
||||||
@ -110,7 +98,8 @@ module Parfait
|
|||||||
# These are the same functions that Builtin implements at run-time
|
# These are the same functions that Builtin implements at run-time
|
||||||
class Object
|
class Object
|
||||||
include FakeMem
|
include FakeMem
|
||||||
|
include Padding
|
||||||
|
|
||||||
# these internal functions are _really_ internal
|
# these internal functions are _really_ internal
|
||||||
# they respresent the smallest code needed to build larger functionality
|
# they respresent the smallest code needed to build larger functionality
|
||||||
# but should _never_ be used outside parfait. in fact that should be impossible
|
# but should _never_ be used outside parfait. in fact that should be impossible
|
||||||
|
@ -15,16 +15,5 @@ module Positioned
|
|||||||
end
|
end
|
||||||
@position = pos
|
@position = pos
|
||||||
end
|
end
|
||||||
# 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
|
|
||||||
end
|
end
|
||||||
|
38
test/virtual/test_padding.rb
Normal file
38
test/virtual/test_padding.rb
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
require_relative "../helper"
|
||||||
|
|
||||||
|
class Padded
|
||||||
|
include Padding
|
||||||
|
end
|
||||||
|
|
||||||
|
class TestPadding < MiniTest::Test
|
||||||
|
|
||||||
|
def setup
|
||||||
|
@pad = Padded.new
|
||||||
|
end
|
||||||
|
def test_small
|
||||||
|
[6,20,23,24].each do |p|
|
||||||
|
assert_equal 32 , @pad.padded(p) , "Expecting 32 for #{p}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
def test_large
|
||||||
|
[26,40,53,56].each do |p|
|
||||||
|
assert_equal 64 , @pad.padded(p) , "Expecting 64 for #{p}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class TestPositioning < MiniTest::Test
|
||||||
|
def test_list1
|
||||||
|
list = Virtual.new_list([1])
|
||||||
|
assert_equal 32 , list.mem_length
|
||||||
|
end
|
||||||
|
def test_list5
|
||||||
|
list = Virtual.new_list([1,2,3,4,5])
|
||||||
|
assert_equal 32 , list.mem_length
|
||||||
|
end
|
||||||
|
def test_layout
|
||||||
|
layout = Parfait::Layout.new Object
|
||||||
|
layout.push 5
|
||||||
|
assert_equal 32 , layout.mem_length
|
||||||
|
end
|
||||||
|
end
|
Loading…
x
Reference in New Issue
Block a user