fix instruction positioning
use at as at and only compute difference when needed (to determine if there is a jump in binary) Easier to understand that way
This commit is contained in:
parent
bc1e29e4f6
commit
776a97986d
@ -17,14 +17,16 @@ module Parfait
|
|||||||
end
|
end
|
||||||
def extend_to(total_size)
|
def extend_to(total_size)
|
||||||
if total_size > self.data_length
|
if total_size > self.data_length
|
||||||
unless @next
|
extend_one unless @next
|
||||||
@next = BinaryCode.new(1)
|
|
||||||
#puts "extending #{total_size - data_length} in #{self}"
|
|
||||||
Risc::Position.reset(self) if Risc::Position.set?(self)
|
|
||||||
end
|
|
||||||
@next.extend_to(total_size - data_length)
|
@next.extend_to(total_size - data_length)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
def extend_one
|
||||||
|
@next = BinaryCode.new(1)
|
||||||
|
#puts "extending #{total_size - data_length} in #{self}"
|
||||||
|
Risc::Position.reset(self) if Risc::Position.set?(self)
|
||||||
|
end
|
||||||
|
|
||||||
def to_s
|
def to_s
|
||||||
"BinaryCode #{Risc::Position.set?(self) ? Risc::Position.get(self): self.object_id.to_s(16)}"
|
"BinaryCode #{Risc::Position.set?(self) ? Risc::Position.get(self): self.object_id.to_s(16)}"
|
||||||
end
|
end
|
||||||
|
@ -112,7 +112,7 @@ module Risc
|
|||||||
next unless method.is_a? Parfait::TypedMethod
|
next unless method.is_a? Parfait::TypedMethod
|
||||||
before = at
|
before = at
|
||||||
Position.set( method.binary , at , method)
|
Position.set( method.binary , at , method)
|
||||||
Position.set( method.cpu_instructions, 3 , method.binary)
|
Position.set( method.cpu_instructions, at + 12 , method.binary)
|
||||||
# before = at
|
# before = at
|
||||||
# nekst = method.binary
|
# nekst = method.binary
|
||||||
# while(nekst)
|
# while(nekst)
|
||||||
|
@ -65,7 +65,7 @@ module Risc
|
|||||||
when Arm::Instruction , Risc::Label
|
when Arm::Instruction , Risc::Label
|
||||||
InstructionPosition.new(object,at , extra)
|
InstructionPosition.new(object,at , extra)
|
||||||
else
|
else
|
||||||
ObjectPosition.new(at)
|
ObjectPosition.new(at,object)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,10 +1,16 @@
|
|||||||
module Risc
|
module Risc
|
||||||
module Position
|
module Position
|
||||||
|
|
||||||
|
# BinaryCodes form a linked list
|
||||||
|
#
|
||||||
|
# We want to keep all code for a method continous, so we propagate Positions
|
||||||
|
#
|
||||||
class CodePosition < ObjectPosition
|
class CodePosition < ObjectPosition
|
||||||
|
|
||||||
attr_reader :code , :method
|
attr_reader :code , :method
|
||||||
|
|
||||||
def initialize(code, pos , method)
|
def initialize(code, pos , method)
|
||||||
super(pos)
|
super(pos,code)
|
||||||
@code = code
|
@code = code
|
||||||
@method = method
|
@method = method
|
||||||
end
|
end
|
||||||
|
@ -1,23 +1,37 @@
|
|||||||
module Risc
|
module Risc
|
||||||
module Position
|
module Position
|
||||||
|
|
||||||
|
# Instructions are also a linked list, but their position is not really
|
||||||
|
# the position of the object.
|
||||||
|
# Rather it is the position of the assembled code in the binary.
|
||||||
|
# (Luckily arm is sane, so this is realtively simple)
|
||||||
|
#
|
||||||
|
# Really we only need to calculate Positions at a jump, so between the
|
||||||
|
# Jump and the label it jumps too. The other instructions are "just" fill.
|
||||||
|
# But off course we need to propagate positions to get it right.
|
||||||
|
#
|
||||||
|
# Assembled instructions are kept in BinaryCode objects.
|
||||||
|
# When propagating positions we have to see that the next position assembles into
|
||||||
|
# the same BinaryCode, or else move it and the code along
|
||||||
|
#
|
||||||
class InstructionPosition < ObjectPosition
|
class InstructionPosition < ObjectPosition
|
||||||
attr_reader :instruction , :binary
|
attr_reader :instruction , :binary
|
||||||
def initialize(instruction, pos , binary)
|
def initialize(instruction, pos , binary)
|
||||||
raise "not set " unless binary
|
raise "not set " unless binary
|
||||||
super(pos)
|
super(pos, instruction)
|
||||||
@instruction = instruction
|
@instruction = instruction
|
||||||
@binary = binary
|
@binary = binary
|
||||||
end
|
end
|
||||||
|
|
||||||
def init(at)
|
def init(at)
|
||||||
|
diff = at - Position.get(@binary).at
|
||||||
|
if( diff % 60 == 13*4)
|
||||||
|
@binary.extend_one unless @binary.next
|
||||||
|
@binary = @binary.next
|
||||||
|
raise "end of line " unless @binary
|
||||||
|
at = Position.get(@binary).at + 3*4
|
||||||
|
end
|
||||||
return unless instruction.next
|
return unless instruction.next
|
||||||
at += instruction.byte_length
|
at += instruction.byte_length
|
||||||
bin = binary
|
|
||||||
if( 12 == at % 60)
|
|
||||||
at = 12
|
|
||||||
bin = binary.next
|
|
||||||
end
|
|
||||||
Position.set(instruction.next, at , binary)
|
Position.set(instruction.next, at , binary)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
module Risc
|
module Risc
|
||||||
module Position
|
module Position
|
||||||
class ObjectPosition
|
class ObjectPosition
|
||||||
attr_reader :at
|
attr_reader :at , :object
|
||||||
|
|
||||||
def initialize( at )
|
def initialize( at , object)
|
||||||
@at = at
|
@at = at
|
||||||
raise "not int #{self}-#{at}" unless @at.is_a?(Integer)
|
raise "not int #{self}-#{at}" unless @at.is_a?(Integer)
|
||||||
end
|
end
|
||||||
|
@ -72,7 +72,7 @@ module Risc
|
|||||||
def write_any( obj )
|
def write_any( obj )
|
||||||
write_any_log( obj , "Write")
|
write_any_log( obj , "Write")
|
||||||
if @stream.length != Position.get(obj).at
|
if @stream.length != Position.get(obj).at
|
||||||
puts "Write #{obj.class}:0x#{obj.object_id.to_s(16)} at 0x#{stream_position.to_s(16)} not #{Position.get(obj)}"
|
#puts "Write #{obj.class}:0x#{obj.object_id.to_s(16)} at 0x#{stream_position.to_s(16)} not #{Position.get(obj)}"
|
||||||
end
|
end
|
||||||
write_any_out(obj)
|
write_any_out(obj)
|
||||||
write_any_log( obj , "Wrote")
|
write_any_log( obj , "Wrote")
|
||||||
|
@ -5,9 +5,16 @@ require_relative "../helper"
|
|||||||
# tests are named as per assembler code, ie test_mov testing mov instruction
|
# tests are named as per assembler code, ie test_mov testing mov instruction
|
||||||
|
|
||||||
module Arm
|
module Arm
|
||||||
|
class FakeBin
|
||||||
|
def byte_length
|
||||||
|
4
|
||||||
|
end
|
||||||
|
end
|
||||||
module ArmHelper
|
module ArmHelper
|
||||||
def setup
|
def setup
|
||||||
@machine = Arm::ArmMachine
|
@machine = Arm::ArmMachine
|
||||||
|
@binary = FakeBin.new
|
||||||
|
Risc::Position.set(@binary , 0)
|
||||||
end
|
end
|
||||||
|
|
||||||
# code is what the generator spits out, at least one instruction worth (.first)
|
# code is what the generator spits out, at least one instruction worth (.first)
|
||||||
|
@ -19,8 +19,9 @@ module Arm
|
|||||||
Risc.machine.boot
|
Risc.machine.boot
|
||||||
bin = Parfait::BinaryCode.new(1)
|
bin = Parfait::BinaryCode.new(1)
|
||||||
Risc::Position.set(bin , 0x20)
|
Risc::Position.set(bin , 0x20)
|
||||||
|
Risc::Position.set(@binary , 0)
|
||||||
code = @machine.call( bin ,{} )#this jumps to the next instruction
|
code = @machine.call( bin ,{} )#this jumps to the next instruction
|
||||||
Risc::Position.set(code , 0, 1)
|
Risc::Position.set(code , 0, @binary)
|
||||||
assert_code code , :call, [0x09,0x0,0x0,0xeb]
|
assert_code code , :call, [0x09,0x0,0x0,0xeb]
|
||||||
end
|
end
|
||||||
def test_swi
|
def test_swi
|
||||||
|
@ -82,7 +82,7 @@ module Arm
|
|||||||
end
|
end
|
||||||
def test_too_big_add
|
def test_too_big_add
|
||||||
code = @machine.add :r1 , :r1, 0x222
|
code = @machine.add :r1 , :r1, 0x222
|
||||||
Risc::Position.set(code,0,1)
|
Risc::Position.set(code,0,@binary)
|
||||||
# add 0x02 (first instruction) and then 0x220 shifted
|
# add 0x02 (first instruction) and then 0x220 shifted
|
||||||
assert_code code , :add , [0x02,0x1c,0x91,0xe2] #e2 91 1e 02
|
assert_code code , :add , [0x02,0x1c,0x91,0xe2] #e2 91 1e 02
|
||||||
# added extra instruction to add "extra"
|
# added extra instruction to add "extra"
|
||||||
@ -91,13 +91,13 @@ module Arm
|
|||||||
|
|
||||||
def label( pos = 0x22 + 8)
|
def label( pos = 0x22 + 8)
|
||||||
l = Risc.label("some" , "Label")
|
l = Risc.label("some" , "Label")
|
||||||
Risc::Position.set(l,pos , 1)
|
Risc::Position.set(l,pos , @binary)
|
||||||
l
|
l
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_move_object
|
def test_move_object
|
||||||
code = @machine.add( :r1 , label)
|
code = @machine.add( :r1 , label)
|
||||||
Risc::Position.set(code,0,0)
|
Risc::Position.set(code,0,@binary)
|
||||||
assert_code code , :add , [0x22,0x10,0x9f,0xe2] #e2 9f 10 22
|
assert_code code , :add , [0x22,0x10,0x9f,0xe2] #e2 9f 10 22
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -22,7 +22,7 @@ module Arm
|
|||||||
end
|
end
|
||||||
def test_mov_big
|
def test_mov_big
|
||||||
code = @machine.mov :r0, 0x222 # is not 8 bit and can't be rotated by the arm system in one instruction
|
code = @machine.mov :r0, 0x222 # is not 8 bit and can't be rotated by the arm system in one instruction
|
||||||
Risc::Position.set(code,0,1)
|
Risc::Position.set(code,0,@binary)
|
||||||
# mov 512(0x200) = e3 a0 0c 02 add 34(0x22) = e2 90 00 22
|
# mov 512(0x200) = e3 a0 0c 02 add 34(0x22) = e2 90 00 22
|
||||||
assert_code code , :mov , [ 0x02,0x0c,0xb0,0xe3]
|
assert_code code , :mov , [ 0x02,0x0c,0xb0,0xe3]
|
||||||
assert_code code.next , :add , [ 0x22,0x00,0x90,0xe2]
|
assert_code code.next , :add , [ 0x22,0x00,0x90,0xe2]
|
||||||
|
@ -7,6 +7,7 @@ module Risc
|
|||||||
def setup
|
def setup
|
||||||
Risc.machine.boot
|
Risc.machine.boot
|
||||||
@binary = Parfait::BinaryCode.new(1)
|
@binary = Parfait::BinaryCode.new(1)
|
||||||
|
Position.set(@binary , 0)
|
||||||
@label = Label.new("hi","ho")
|
@label = Label.new("hi","ho")
|
||||||
end
|
end
|
||||||
def test_set_instr
|
def test_set_instr
|
||||||
|
@ -6,21 +6,21 @@ module Risc
|
|||||||
class TestPositionBasic < MiniTest::Test
|
class TestPositionBasic < MiniTest::Test
|
||||||
|
|
||||||
def test_creation_ok
|
def test_creation_ok
|
||||||
assert ObjectPosition.new(0)
|
assert ObjectPosition.new(0,self)
|
||||||
end
|
end
|
||||||
def test_creation_fail
|
def test_creation_fail
|
||||||
assert_raises {Position.new("0")}
|
assert_raises {Position.new("0")}
|
||||||
end
|
end
|
||||||
def test_add
|
def test_add
|
||||||
res = ObjectPosition.new(0) + 5
|
res = ObjectPosition.new(0,self) + 5
|
||||||
assert_equal 5 , res
|
assert_equal 5 , res
|
||||||
end
|
end
|
||||||
def test_sub
|
def test_sub
|
||||||
res = ObjectPosition.new(5) - 1
|
res = ObjectPosition.new(5,self) - 1
|
||||||
assert_equal 4 , res
|
assert_equal 4 , res
|
||||||
end
|
end
|
||||||
def test_sub_pos
|
def test_sub_pos
|
||||||
res = ObjectPosition.new(5) - ObjectPosition.new(1)
|
res = ObjectPosition.new(5,self) - ObjectPosition.new(1,self)
|
||||||
assert_equal 4 , res
|
assert_equal 4 , res
|
||||||
end
|
end
|
||||||
def test_set
|
def test_set
|
||||||
|
@ -39,7 +39,7 @@ module Risc
|
|||||||
assert_equal Parfait::BinaryCode , @machine.binary_init.class
|
assert_equal Parfait::BinaryCode , @machine.binary_init.class
|
||||||
end
|
end
|
||||||
def test_has_jump
|
def test_has_jump
|
||||||
assert_equal "ea0011f4" , @machine.binary_init.get_word(1).to_s(16)
|
assert_equal "ea000fb4" , @machine.binary_init.get_word(1).to_s(16)
|
||||||
end
|
end
|
||||||
def test_pos_bin
|
def test_pos_bin
|
||||||
assert_equal "0x0" , Position.get(@machine.binary_init).to_s
|
assert_equal "0x0" , Position.get(@machine.binary_init).to_s
|
||||||
@ -48,7 +48,7 @@ module Risc
|
|||||||
assert_equal 12 , Position.get(@machine.cpu_init).at
|
assert_equal 12 , Position.get(@machine.cpu_init).at
|
||||||
end
|
end
|
||||||
def test_cpu_at
|
def test_cpu_at
|
||||||
assert_equal 3 , Position.get(@machine.cpu_init.first).at
|
assert_equal "0x3ee4" , Position.get(@machine.cpu_init.first).to_s
|
||||||
end
|
end
|
||||||
def test_cpu_bin
|
def test_cpu_bin
|
||||||
assert_equal "0x3ed8" , Position.get(Position.get(@machine.cpu_init.first).binary).to_s
|
assert_equal "0x3ed8" , Position.get(Position.get(@machine.cpu_init.first).binary).to_s
|
||||||
|
Loading…
x
Reference in New Issue
Block a user