(semi) proper fix for large moves, integers or virtual constants(was buggy)

This commit is contained in:
Torsten Ruger 2014-09-30 12:07:21 +03:00
parent 527e591e75
commit fba66371c0
2 changed files with 44 additions and 20 deletions

View File

@ -16,7 +16,7 @@ module Arm
@extra = nil @extra = nil
end end
# arm intrucions are pretty sensible, and always 4 bytes (thumb not supported) # arm intructions are pretty sensible, and always 4 bytes (thumb not supported)
# but not all constants fit into the part of the instruction that is left after the instruction code, # but not all constants fit into the part of the instruction that is left after the instruction code,
# so large moves have to be split into two instructions. # so large moves have to be split into two instructions.
# we handle this "transparently", just this instruction looks longer # we handle this "transparently", just this instruction looks longer
@ -39,8 +39,11 @@ module Arm
# 8 is for the funny pipeline adjustment (ie pc pointing to fetch and not execute) # 8 is for the funny pipeline adjustment (ie pc pointing to fetch and not execute)
right = Virtual::IntegerConstant.new( r_pos - self.position - 8 ) right = Virtual::IntegerConstant.new( r_pos - self.position - 8 )
#puts "Position #{r_pos} from #{self.position} = #{right}" #puts "Position #{r_pos} from #{self.position} = #{right}"
right = Virtual::IntegerConstant.new(r_pos) if right.integer > 255 if right.integer > 255
rn = :pc right = Virtual::IntegerConstant.new(r_pos)
else
rn = :pc
end
end end
if (right.is_a?(Virtual::IntegerConstant)) if (right.is_a?(Virtual::IntegerConstant))
if (right.fits_u8?) if (right.fits_u8?)
@ -52,26 +55,29 @@ module Arm
immediate = 1 immediate = 1
else else
# unfortunately i was wrong in thinking the pi is armv7. The good news is the code below implements # unfortunately i was wrong in thinking the pi is armv7. The good news is the code below implements
# the movw (armv7) instruciton and works # the movw instruction (armv7 for moving a word) and works
#armv7 raise "Too big #{right.integer} " if (right.integer >> 16) > 0 #armv7 raise "Too big #{right.integer} " if (right.integer >> 16) > 0
#armv7 operand = (right.integer & 0xFFF) #armv7 operand = (right.integer & 0xFFF)
#armv7 immediate = 1 #armv7 immediate = 1
#armv7 rn = (right.integer >> 12) #armv7 rn = (right.integer >> 12)
# a little STRANGE, that the armv7 movw (move a 2 byte word) is an old test opcode, but there it is # a little STRANGE, that the armv7 movw (move a 2 byte word) is an old test opcode, but there it is
#armv7 @attributes[:opcode] = :tst #armv7 @attributes[:opcode] = :tst
raise "No negatives implemented " if right.integer < 0 raise "No negatives implemented #{right} " if right.integer < 0
# and so it continues: when we notice that the const doesn't fit, first time we raise an # and so it continues: when we notice that the const doesn't fit, first time we raise an
# error,but set the extra flag, to say the instruction is now 8 bytes # error,but set the extra flag, to say the instruction is now 8 bytes
# then on subsequent assemlies we can assemble # then on subsequent assemblies we can assemble
unless @extra unless @extra
@extra = 1 @extra = 1
raise ::Register::LinkException.new("cannot fit numeric literal argument in operand #{right.inspect}") raise ::Register::LinkException.new("cannot fit numeric literal argument in operand #{right.inspect}")
end end
# now we can do the actual breaking of instruction # now we can do the actual breaking of instruction, by splitting the operand
operand = (right.integer / 256 ) first = Virtual::IntegerConstant.new(right.integer & 0xFFFFFF00)
operand = calculate_u8_with_rr( first )
raise "no fit for #{right}" unless operand
immediate = 1 immediate = 1
raise "only simple 2 byte implemented #{self.inspect}" if operand > 255 @extra = ::Register::RegisterMachine.instance.add( to , to , (right.integer & 0xFF) )
@extra = ::Register::RegisterMachine.instance.add( to , to , (right.integer && 0xFF) , shift_lsr: 8) #TODO: this is still a hack, as it does not encode all possible values. The way it _should_ be done
# is to check that the first part is doabe with u8_with_rr AND leaves a u8 remainder
end end
elsif (right.is_a?(Symbol) or right.is_a?(::Register::RegisterReference)) elsif (right.is_a?(Symbol) or right.is_a?(::Register::RegisterReference))
operand = reg_code(right) operand = reg_code(right)
@ -94,7 +100,7 @@ module Arm
# by now we have the extra add so assemble that # by now we have the extra add so assemble that
if(@extra) if(@extra)
@extra.assemble(io) @extra.assemble(io)
#puts "Assemble extra at #{self.position.to_s(16)}" #puts "Assemble extra at #{val.to_s(16)}"
end end
end end
def shift val , by def shift val , by

View File

@ -4,12 +4,12 @@ class TestMoves < MiniTest::Test
include ArmHelper include ArmHelper
def test_mov def test_mov
code = @machine.mov :r0, 5 code = @machine.mov :r1, 5
assert_code code , :mov , [0x05,0x00,0xa0,0xe3] #e3 a0 10 05 assert_code code , :mov , [0x05,0x10,0xa0,0xe3] #e3 a0 10 05
end end
def test_mov_pc def test_mov_pc
code = @machine.mov :pc, 6 code = @machine.mov :pc, 5
assert_code code , :mov , [0x06,0xf0,0xa0,0xe3] #e3 a0 f0 06 assert_code code , :mov , [0x05,0xf0,0xa0,0xe3] #e3 a0 f0 06
end end
def test_mov_256 def test_mov_256
code = @machine.mov :r1, 256 code = @machine.mov :r1, 256
@ -20,10 +20,10 @@ class TestMoves < MiniTest::Test
assert_code code , :mov , [0x80,0x10,0xa0,0xe3] #e3 a0 10 80 assert_code code , :mov , [0x80,0x10,0xa0,0xe3] #e3 a0 10 80
end end
def test_mov_big def test_mov_big
code = @machine.mov :r0, 0x222 code = @machine.mov :r0, 0x222 # is not 8 bit and can't be rotated by the arm system in one instruction
code.set_position(0) code.set_position(0)
begin #TODO use compiler to confirm codes here: this is just what passes begin # mov 512(0x200) = e3 a0 0c 02 add 34(0x22) = e2 80 00 22
assert_code code , :mov , [ 0x02,0x00,0xa0,0xe3 , 0xff,0x04,0x80,0xe2] assert_code code , :mov , [ 0x02,0x0c,0xa0,0xe3 , 0x22,0x00,0x80,0xe2]
rescue Register::LinkException rescue Register::LinkException
retry retry
end end
@ -32,4 +32,22 @@ class TestMoves < MiniTest::Test
code = @machine.mvn :r1, 5 code = @machine.mvn :r1, 5
assert_code code , :mvn , [0x05,0x10,0xe0,0xe3] #e3 e0 10 05 assert_code code , :mvn , [0x05,0x10,0xe0,0xe3] #e3 e0 10 05
end end
def test_constant_small # like test_mov
const = Virtual::ObjectConstant.new
const.set_position( 13 ) # 13 = 5 + 8 , 8 for the arm pipeline offset, gets subtracted
code = @machine.mov :r1 , const
code.set_position(0)
assert_code code , :mov , [0x05,0x10,0xaf,0xe3] #e3 ef 10 05
end
def test_constant_big # like test_mov_big
const = Virtual::ObjectConstant.new
const.set_position( 0x222 )
code = @machine.mov :r0 , const
code.set_position(0)
begin # mov 512(0x200) = e3 a0 0c 02 add 34(0x22) = e2 80 00 22
assert_code code , :mov , [ 0x02,0x0c,0xa0,0xe3 , 0x22,0x00,0x80,0xe2]
rescue Register::LinkException
retry
end
end
end end