2015-05-26 19:17:43 +02:00
|
|
|
module Parfait
|
2015-06-03 09:01:59 +02:00
|
|
|
|
2018-03-26 13:04:13 +02:00
|
|
|
# A typed method object is a description of the method, it's name etc
|
|
|
|
#
|
|
|
|
# But the code that the method represents, the binary, is held as an array
|
|
|
|
# in these. As Objects are fixed size (this one 16 words), we use linked list
|
|
|
|
# and as the last code of each link is a jump to the next link.
|
|
|
|
#
|
2019-08-22 11:26:40 +02:00
|
|
|
class BinaryCode < Data32
|
2019-09-09 19:26:54 +02:00
|
|
|
attr_reader :type, :next_code
|
2018-03-25 17:22:02 +02:00
|
|
|
|
2018-05-28 14:09:59 +02:00
|
|
|
def self.type_length
|
|
|
|
2 #type + next (could get from space, maybe later)
|
|
|
|
end
|
|
|
|
def self.byte_offset
|
|
|
|
self.type_length * 4 # size of type * word_size (4)
|
|
|
|
end
|
2018-05-28 10:45:04 +02:00
|
|
|
def self.data_length
|
2018-05-28 17:20:09 +02:00
|
|
|
self.memory_size - self.type_length - 1 #one for the jump
|
2018-05-28 10:45:04 +02:00
|
|
|
end
|
|
|
|
def data_length
|
|
|
|
self.class.data_length
|
|
|
|
end
|
|
|
|
def byte_length
|
|
|
|
4*data_length
|
|
|
|
end
|
|
|
|
|
2018-03-25 17:22:02 +02:00
|
|
|
def initialize(total_size)
|
|
|
|
super()
|
2019-09-09 19:26:54 +02:00
|
|
|
@next_code = nil
|
2018-05-23 20:35:22 +02:00
|
|
|
extend_to(total_size )
|
2018-05-28 17:20:09 +02:00
|
|
|
(0 ... data_length).each{ |index| set_word(index , 0) }
|
|
|
|
set_last(0)
|
2018-03-28 11:49:17 +02:00
|
|
|
end
|
2019-09-09 19:26:54 +02:00
|
|
|
|
2018-03-28 11:49:17 +02:00
|
|
|
def extend_to(total_size)
|
2018-05-28 17:20:09 +02:00
|
|
|
return if total_size < data_length
|
2018-08-11 18:15:34 +02:00
|
|
|
extend_one() unless next_code
|
|
|
|
next_code.extend_to(total_size - data_length)
|
2018-03-25 17:22:02 +02:00
|
|
|
end
|
2018-05-28 17:20:09 +02:00
|
|
|
|
2018-05-23 20:35:22 +02:00
|
|
|
def extend_one()
|
2019-09-09 19:26:54 +02:00
|
|
|
@next_code = BinaryCode.new(1)
|
2018-06-07 18:26:02 +02:00
|
|
|
Risc::Position.get(self).trigger_inserted if Risc::Position.set?(self)
|
2018-05-11 17:36:45 +02:00
|
|
|
end
|
2018-05-14 11:38:44 +02:00
|
|
|
|
2018-06-05 23:53:41 +02:00
|
|
|
def ensure_next
|
2018-08-11 18:15:34 +02:00
|
|
|
extend_one unless next_code
|
|
|
|
next_code
|
2018-06-05 23:53:41 +02:00
|
|
|
end
|
|
|
|
|
2018-06-09 16:41:39 +02:00
|
|
|
def last_code
|
|
|
|
last = self
|
2018-08-11 18:15:34 +02:00
|
|
|
last = last.next_code while(last.next_code)
|
2018-06-09 16:41:39 +02:00
|
|
|
last
|
|
|
|
end
|
|
|
|
|
2018-05-28 10:45:04 +02:00
|
|
|
def each_block( &block )
|
2018-05-13 16:21:48 +02:00
|
|
|
block.call( self )
|
2018-08-11 18:15:34 +02:00
|
|
|
next_code.each_block( &block ) if next_code
|
2018-05-13 16:21:48 +02:00
|
|
|
end
|
|
|
|
|
2015-05-28 20:10:27 +02:00
|
|
|
def to_s
|
2018-05-09 19:36:49 +02:00
|
|
|
"BinaryCode #{Risc::Position.set?(self) ? Risc::Position.get(self): self.object_id.to_s(16)}"
|
2018-03-25 17:22:02 +02:00
|
|
|
end
|
2018-05-09 19:36:49 +02:00
|
|
|
|
2018-05-28 10:45:04 +02:00
|
|
|
def each_word( all = true)
|
2018-05-14 11:38:44 +02:00
|
|
|
index = 0
|
2018-05-28 17:20:09 +02:00
|
|
|
while( index < data_length)
|
2018-04-01 11:13:14 +02:00
|
|
|
yield get_word(index)
|
2018-03-29 16:38:59 +02:00
|
|
|
index += 1
|
|
|
|
end
|
2018-05-28 17:20:09 +02:00
|
|
|
yield( get_last ) if all
|
2018-03-29 16:38:59 +02:00
|
|
|
end
|
2018-05-28 10:45:04 +02:00
|
|
|
|
2018-03-26 17:14:52 +02:00
|
|
|
def set_word(index , word)
|
2018-05-14 11:38:44 +02:00
|
|
|
raise "invalid index #{index}" if index < 0
|
2018-05-28 17:20:09 +02:00
|
|
|
if index >= data_length
|
2018-08-11 18:15:34 +02:00
|
|
|
#raise "invalid index #{index}" unless next
|
2018-04-03 14:07:36 +02:00
|
|
|
extend_to( index )
|
2018-08-11 18:15:34 +02:00
|
|
|
next_code.set_word( index - data_length , word)
|
2018-05-28 14:09:59 +02:00
|
|
|
else
|
2018-05-28 17:20:09 +02:00
|
|
|
set_internal_word(index + BinaryCode.type_length , word)
|
2018-03-26 17:14:52 +02:00
|
|
|
end
|
|
|
|
end
|
2018-05-28 17:20:09 +02:00
|
|
|
def get_last()
|
|
|
|
get_internal_word(data_length + BinaryCode.type_length)
|
|
|
|
end
|
2018-05-28 10:45:04 +02:00
|
|
|
def set_last(word)
|
2018-05-28 17:20:09 +02:00
|
|
|
set_internal_word(data_length + BinaryCode.type_length , word)
|
2018-05-28 10:45:04 +02:00
|
|
|
end
|
2018-03-26 17:14:52 +02:00
|
|
|
def get_word(index)
|
2018-05-14 11:38:44 +02:00
|
|
|
raise "invalid index #{index}" if index < 0
|
2018-05-28 17:20:09 +02:00
|
|
|
if index >= data_length
|
2018-08-11 18:15:34 +02:00
|
|
|
raise "invalid index #{index}" unless next_code
|
|
|
|
return next_code.get_word( index - data_length)
|
2018-03-26 17:14:52 +02:00
|
|
|
end
|
2018-05-28 17:20:09 +02:00
|
|
|
get_internal_word(index + BinaryCode.type_length)
|
2018-03-26 17:14:52 +02:00
|
|
|
end
|
2018-03-26 13:04:13 +02:00
|
|
|
def set_char(index , char)
|
|
|
|
if index >= byte_length
|
2018-08-11 18:15:34 +02:00
|
|
|
#puts "Pass it on #{index} for #{self.object_id}:#{next_code.object_id}"
|
|
|
|
return next_code.set_char( index - byte_length , char )
|
2018-03-25 17:22:02 +02:00
|
|
|
end
|
|
|
|
word_index = (index - 1) / 4 + 2
|
|
|
|
old = get_internal_word( word_index )
|
2018-03-26 13:04:13 +02:00
|
|
|
old = old && char << ((index-1)%4)
|
|
|
|
set_internal_word(word_index , char)
|
2018-03-25 17:22:02 +02:00
|
|
|
end
|
|
|
|
def total_byte_length(start = 0 )
|
2019-09-09 19:26:54 +02:00
|
|
|
start += byte_length
|
2018-08-11 18:15:34 +02:00
|
|
|
return start unless self.next_code
|
2019-09-09 19:26:54 +02:00
|
|
|
@next_code.total_byte_length(start)
|
2015-05-28 20:10:27 +02:00
|
|
|
end
|
2015-05-26 19:17:43 +02:00
|
|
|
end
|
|
|
|
end
|