diff --git a/.ruby-version b/.ruby-version index 3f684d2d..00355e29 100644 --- a/.ruby-version +++ b/.ruby-version @@ -1 +1 @@ -2.3.4 +2.3.7 diff --git a/lib/arm/instructions/call_instruction.rb b/lib/arm/instructions/call_instruction.rb index 520b5a99..19c776e1 100644 --- a/lib/arm/instructions/call_instruction.rb +++ b/lib/arm/instructions/call_instruction.rb @@ -11,7 +11,7 @@ module Arm # Riscs 0-6 hold the call values as for a normal c call class CallInstruction < Instruction attr_reader :first - + def initialize(first, attributes) super(nil, nil) @attributes = attributes @@ -51,8 +51,8 @@ module Arm when Parfait::BinaryCode # But, for methods, this happens to be the size of the object header, # so there it balances out, but not blocks - # have to use the code, not the mthod object for methods - arg = Positioned.position(@first) - Positioned.position(self) + # have to use the code, not the method object for methods + arg = Positioned.position(@first) - Positioned.position(self) + 4 else arg = @first end diff --git a/lib/arm/instructions/instruction.rb b/lib/arm/instructions/instruction.rb index 6029c784..b6b0392b 100644 --- a/lib/arm/instructions/instruction.rb +++ b/lib/arm/instructions/instruction.rb @@ -21,11 +21,16 @@ module Arm ret end - def set_position( position ) + def set_position( position , count ) Positioned.set_position(self,position) position += byte_length if self.next - self.next.set_position( position ) + count += 1 #assumes 4 byte instructions, as does the whole setup + if( 0 == count % 12) # 12 is the amount of instructions that fit into a BinaryCode + count = 0 + position += 12 # 12=3*4 , 3 for marker,type,next words to jump over + end + self.next.set_position( position , count ) else position end diff --git a/lib/risc/instructions/label.rb b/lib/risc/instructions/label.rb index 625950d4..027fbb0e 100644 --- a/lib/risc/instructions/label.rb +++ b/lib/risc/instructions/label.rb @@ -45,9 +45,9 @@ module Risc end # labels have the same position as their next - def set_position( position ) + def set_position( position , count = 0) Positioned.set_position(self,position) - self.next.set_position(position) if self.next + self.next.set_position(position,count) if self.next end # shame we need this, just for logging diff --git a/lib/risc/machine.rb b/lib/risc/machine.rb index fd63cdb0..8a503b86 100644 --- a/lib/risc/machine.rb +++ b/lib/risc/machine.rb @@ -14,7 +14,7 @@ module Risc class Machine include Logging - log_level :info + log_level :debug def initialize @booted = false @@ -77,9 +77,8 @@ module Risc def position_all translate_arm unless @translated #need the initial jump at 0 and then functions - cpu_init.set_position( 0 ) - #Positioned.set_position(cpu_init.first , 0) Positioned.set_position(binary_init,0) + cpu_init.set_position( 12 ,0) @code_start = position_objects( binary_init.padded_length ) # and then everything code position_code @@ -93,7 +92,9 @@ module Risc objects.each do | id , objekt| next if objekt.is_a?( Parfait::BinaryCode) or objekt.is_a?( Risc::Label ) Positioned.set_position(objekt,at) + before = at at += objekt.padded_length + log.debug "Object #{objekt.class}:#{before.to_s(16)} len: #{(at - before).to_s(16)}" end at end @@ -109,8 +110,7 @@ module Risc at = @code_start objects.each do |id , method| next unless method.is_a? Parfait::TypedMethod - log.debug "POS1 #{method.name}:#{at.to_s(16)}" - method.cpu_instructions.set_position( at ) + method.cpu_instructions.set_position( at + 12) before = at nekst = method.binary while(nekst) @@ -118,7 +118,8 @@ module Risc at += nekst.padded_length nekst = nekst.next end - log.debug "POS2 #{method.name}:#{at.to_s(16)} len: #{(at - before).to_s(16)}" + log.debug "Method #{method.name}:#{before.to_s(16)} len: #{(at - before).to_s(16)}" + log.debug "Instructions #{method.cpu_instructions.object_id.to_s(16)}:#{(before+12).to_s(16)}" end at end @@ -136,6 +137,7 @@ module Risc return do_create_binary rescue LinkException not_ok += 1 + log.debug "Relink #{not_ok}" position_code end end @@ -150,6 +152,7 @@ module Risc writer = BinaryWriter.new(method.binary) writer.assemble(method.cpu_instructions) end + log.debug "BinaryInit #{cpu_init.first.object_id.to_s(16)}" BinaryWriter.new(binary_init).assemble(cpu_init) end diff --git a/lib/risc/text_writer.rb b/lib/risc/text_writer.rb index c5f8fe69..d2d76473 100644 --- a/lib/risc/text_writer.rb +++ b/lib/risc/text_writer.rb @@ -6,6 +6,11 @@ module Risc # This class serves to write all the objects of the machine (wich also contain the code) # into one stream or binary text object. This is then written to an ELF text section. # + # A word about positions: The c world has a thing called position independent code, and + # baically we follw that idea. Code (ie jumps and constant loads) are all relative. + # But we have pointers. In C the linker takes care of bending those, we have to + # do that ourselves, in write_ref. That's why we need the load adddess and basically + # we just add it to pointers. class TextWriter include Logging diff --git a/test/arm/test_call.rb b/test/arm/test_call.rb index 6d4e2bc2..5a05c2ec 100644 --- a/test/arm/test_call.rb +++ b/test/arm/test_call.rb @@ -21,7 +21,7 @@ module Arm Positioned.set_position(bin , 0x20) code = @machine.call( bin ,{} )#this jumps to the next instruction Positioned.set_position(code , 0) - assert_code code , :call, [0x08,0x0,0x0,0xeb] + assert_code code , :call, [0x09,0x0,0x0,0xeb] end def test_swi code = @machine.swi( 0x05 ) diff --git a/test/arm/test_logic.rb b/test/arm/test_logic.rb index d3794357..867c3a1b 100644 --- a/test/arm/test_logic.rb +++ b/test/arm/test_logic.rb @@ -99,7 +99,7 @@ module Arm def test_move_object code = @machine.add( :r1 , label) - code.set_position(0) + code.set_position(0,0) assert_code code , :add , [0x22,0x10,0x9f,0xe2] #e2 9f 10 22 end diff --git a/test/risc/test_positioned.rb b/test/risc/test_positioned.rb index 48f166cf..9f0e514b 100644 --- a/test/risc/test_positioned.rb +++ b/test/risc/test_positioned.rb @@ -37,6 +37,6 @@ class TestPositioned < MiniTest::Test def test_pos_arm mov = Arm::ArmMachine.mov :r1, 128 - mov.set_position(0) + mov.set_position(0,0) end end