start on fake memory
get index checks on the array access of parfait
This commit is contained in:
parent
be1bc63ff2
commit
6c06f61ab8
@ -18,4 +18,3 @@ require_relative "parfait/cache_entry"
|
||||
require_relative "parfait/message"
|
||||
require_relative "parfait/named_list"
|
||||
require_relative "parfait/space"
|
||||
require_relative "parfait/symbol_adapter"
|
||||
|
@ -23,7 +23,6 @@ module Parfait
|
||||
class DataObject < Object
|
||||
def initialize
|
||||
super
|
||||
@memory = []
|
||||
end
|
||||
def data_length
|
||||
raise "called #{self}"
|
||||
@ -31,23 +30,12 @@ module Parfait
|
||||
def data_start
|
||||
return get_type.get_length
|
||||
end
|
||||
|
||||
# 0 -based index
|
||||
def get_internal_word(index)
|
||||
return super if index < data_start
|
||||
@memory[index]
|
||||
end
|
||||
|
||||
# 1 -based index
|
||||
def set_internal_word(index , value)
|
||||
return super if index < data_start
|
||||
raise "Word[#{index}] = nil" if( value.nil? )
|
||||
@memory[index] = value
|
||||
value
|
||||
end
|
||||
end
|
||||
|
||||
class Data4 < DataObject
|
||||
def self.memory_size
|
||||
4
|
||||
end
|
||||
def data_length
|
||||
4
|
||||
end
|
||||
@ -57,6 +45,9 @@ module Parfait
|
||||
end
|
||||
|
||||
class Data8 < DataObject
|
||||
def self.memory_size
|
||||
8
|
||||
end
|
||||
def data_length
|
||||
8
|
||||
end
|
||||
@ -65,6 +56,9 @@ module Parfait
|
||||
end
|
||||
end
|
||||
class Data16 < DataObject
|
||||
def self.memory_size
|
||||
16
|
||||
end
|
||||
def data_length
|
||||
16
|
||||
end
|
||||
|
@ -1,16 +0,0 @@
|
||||
|
||||
class Symbol
|
||||
|
||||
def has_type?
|
||||
true
|
||||
end
|
||||
def get_type
|
||||
l = Parfait.object_space.classes[:Word].instance_type
|
||||
#puts "LL #{l.class}"
|
||||
l
|
||||
end
|
||||
def padded_length
|
||||
Padding.padded( to_s.length + 4)
|
||||
end
|
||||
|
||||
end
|
@ -22,8 +22,8 @@ end
|
||||
require_relative "risc/padding"
|
||||
require_relative "risc/position"
|
||||
require_relative "risc/platform"
|
||||
|
||||
require "parfait"
|
||||
require_relative "risc/parfait_adapter"
|
||||
require_relative "risc/machine"
|
||||
require_relative "risc/method_compiler"
|
||||
|
||||
|
40
lib/risc/fake_memory.rb
Normal file
40
lib/risc/fake_memory.rb
Normal file
@ -0,0 +1,40 @@
|
||||
module Risc
|
||||
# simulate memory during compile time.
|
||||
#
|
||||
# Memory comes in chunks, power of 2 chunks actually.
|
||||
#
|
||||
# Currently typed instance variables map to ruby instance variables and so do not
|
||||
# end up in memory. Memory being only for indexed word aligned access.
|
||||
#
|
||||
# Parfait really does everything else, apart from the internal_get/set
|
||||
# And our fake memory (other than hte previously used array, does bound check)
|
||||
class FakeMemory
|
||||
attr_reader :min
|
||||
def initialize(from , size)
|
||||
@min = from
|
||||
@memory = Array.new(size)
|
||||
raise "only multiples of 2 !#{size}" unless size == 2**(Math.log2(size).to_i)
|
||||
end
|
||||
def set(index , value)
|
||||
range_check(index)
|
||||
@memory[index] = value
|
||||
value
|
||||
end
|
||||
alias :[]= :set
|
||||
|
||||
def get(index)
|
||||
range_check(index)
|
||||
@memory[index]
|
||||
end
|
||||
alias :[] :get
|
||||
|
||||
def size
|
||||
@memory.length
|
||||
end
|
||||
|
||||
def range_check(index)
|
||||
raise "index too low #{index} < #{min}" if index < min
|
||||
raise "index too big #{index} < #{min}" if index >= size
|
||||
end
|
||||
end
|
||||
end
|
43
lib/risc/parfait_adapter.rb
Normal file
43
lib/risc/parfait_adapter.rb
Normal file
@ -0,0 +1,43 @@
|
||||
require_relative "fake_memory"
|
||||
|
||||
module Parfait
|
||||
class DataObject < Object
|
||||
|
||||
def self.allocate
|
||||
r = super
|
||||
puts "#{self.memory_size}"
|
||||
r.instance_variable_set(:@memory , [])
|
||||
r
|
||||
end
|
||||
|
||||
# 0 -based index
|
||||
def get_internal_word(index)
|
||||
return super if index < data_start
|
||||
@memory[index]
|
||||
end
|
||||
|
||||
# 1 -based index
|
||||
def set_internal_word(index , value)
|
||||
return super if index < data_start
|
||||
raise "Word[#{index}] = nil" if( value.nil? )
|
||||
@memory[index] = value
|
||||
value
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class Symbol
|
||||
|
||||
def has_type?
|
||||
true
|
||||
end
|
||||
def get_type
|
||||
l = Parfait.object_space.classes[:Word].instance_type
|
||||
#puts "LL #{l.class}"
|
||||
l
|
||||
end
|
||||
def padded_length
|
||||
Padding.padded( to_s.length + 4)
|
||||
end
|
||||
|
||||
end
|
50
test/risc/test_fake_memory.rb
Normal file
50
test/risc/test_fake_memory.rb
Normal file
@ -0,0 +1,50 @@
|
||||
require_relative "../helper"
|
||||
|
||||
module Risc
|
||||
class TestFakeMemory < MiniTest::Test
|
||||
def setup
|
||||
@fake = FakeMemory.new(2,16)
|
||||
end
|
||||
def test_init
|
||||
assert @fake
|
||||
end
|
||||
def test_size
|
||||
assert_equal 16 , @fake.size
|
||||
end
|
||||
def test_not_instantiates
|
||||
assert_raises {FakeMemory.new(1,7)}
|
||||
end
|
||||
def test_access_fail_big
|
||||
assert_raises {@fake.set(20 , 12)}
|
||||
end
|
||||
def test_access_fail_small
|
||||
assert_raises {@fake.set(1 , 12)}
|
||||
end
|
||||
def test_access_fail_minus
|
||||
assert_raises {@fake.set(-1 , 12)}
|
||||
end
|
||||
def test_set
|
||||
assert_equal 12 , @fake.set(2 , 12)
|
||||
end
|
||||
def test_set_arr
|
||||
assert_equal 12 , @fake[2] = 12
|
||||
end
|
||||
def test_get_no_init
|
||||
assert_nil @fake.get(2)
|
||||
end
|
||||
def test_get_arr
|
||||
assert_nil @fake[2]
|
||||
end
|
||||
def test_get_set
|
||||
@fake[2] = 12
|
||||
assert_equal 12 , @fake[2]
|
||||
end
|
||||
def test_set_all
|
||||
(2...16).each{ |i| @fake[i] = i * 2}
|
||||
assert_equal 4 , @fake[2]
|
||||
assert_equal 30 , @fake[15]
|
||||
assert_equal 16 , @fake.size
|
||||
end
|
||||
|
||||
end
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user