e6df473647
Before instance variables were used to store the actual data While this worked it was a double mental loop and (more importantly) did not allow the same memory access Ie, in the interpreter the types for args had to be set correctly But they don't need to be until we walk the list, when we can get the types from the method. In short, now the fake memory may be used as memory, by indexing into it We now have to use attr reader/writers that map to the fake memory And while this is possible, it means a lot more self. than i would like Also the compiler whill have to undo those attr to instance acess at some point
42 lines
1.1 KiB
Ruby
42 lines
1.1 KiB
Ruby
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 , :object
|
|
def initialize(object,from , size)
|
|
@object = object
|
|
@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} in #{object.class}" if index < 0
|
|
raise "index too big #{index} >= #{size} #{object.class}" if index >= size
|
|
end
|
|
end
|
|
end
|