From 009c0895f4eca48f55d1655c067f25c53bd8caed Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Thu, 15 May 2014 21:35:45 +0300 Subject: [PATCH] change right syntax to 1.9 style, looks a little nicer --- lib/arm/arm_machine.rb | 42 +++++++++++++++++------------------ lib/arm/memory_instruction.rb | 40 ++++++++++++++++----------------- test/test_arm.rb | 42 +++++++++++++++++------------------ test/test_small_program.rb | 10 ++++----- 4 files changed, 66 insertions(+), 68 deletions(-) diff --git a/lib/arm/arm_machine.rb b/lib/arm/arm_machine.rb index 840c71fa..95499e84 100644 --- a/lib/arm/arm_machine.rb +++ b/lib/arm/arm_machine.rb @@ -11,46 +11,46 @@ module Arm class ArmMachine < Vm::CMachine def integer_less_or_equal block , left , right - block.add_code cmp( left , :right => right ) + block.add_code cmp( left , right: right ) Vm::Bool.new end def integer_plus block , result , left , right - block.add_code add( result , :right => left , :extra => right ) + block.add_code add( result , right: left , :extra => right ) result end def integer_minus block , result , left , right - block.add_code sub( result , :right => left , :extra => right ) + block.add_code sub( result , right: left , :extra => right ) result end def integer_load block , left , right - block.add_code mov( left , :right => right ) + block.add_code mov( left , right: right ) left end def integer_move block , left , right - block.add_code mov( left , :right => right ) + block.add_code mov( left , right: right ) left end def string_load block , str_lit , reg block.add_code add( "r#{reg}".to_sym , :extra => str_lit ) #right is pc, implicit #second arg is a hack to get the stringlength without coding - block.add_code mov( "r#{reg+1}".to_sym , :right => str_lit.length ) + block.add_code mov( "r#{reg+1}".to_sym , right: str_lit.length ) str_lit end def function_call into , call raise "Not CallSite #{call.inspect}" unless call.is_a? Vm::CallSite raise "Not linked #{call.inspect}" unless call.function - into.add_code call( call.function ) + into.add_code call( call.function , {}) call.function.return_type end def main_start entry - entry.add_code mov( :fp , :right => 0 ) + entry.add_code mov( :fp , right: 0 ) end def main_exit exit syscall(exit , 1) @@ -61,14 +61,14 @@ module Arm block end def function_exit entry , f_name - entry.add_code mov( :pc , :right => :lr ) + entry.add_code mov( :pc , right: :lr ) end # assumes string in r0 and r1 and moves them along for the syscall def write_stdout block - block.add_code mov( :r2 , :right => :r1 ) - block.add_code mov( :r1 , :right => :r0 ) - block.add_code mov( :r0 , :right => 1 ) # 1 == stdout + block.add_code mov( :r2 , right: :r1 ) + block.add_code mov( :r1 , right: :r0 ) + block.add_code mov( :r0 , right: 1 ) # 1 == stdout syscall( block , 4 ) end @@ -80,23 +80,23 @@ module Arm tos = Vm::Block.new("integer_to_s") # need to create a block to jump to block.add_code(tos) # and then use the new block to add code #STMFD sp!, {r9, r10, lr} #function entry save working regs (for recursion) - tos.add_code( push :regs => [:lr ]) #and the return address. + tos.add_code push( [:lr ] , {} ) #and the return address. # MOV r9, r1 # preserve arguments over following # MOV r10, r2 # function calls # pin data, ie no saving - remainder = Vm::Integer.new( number.register + 2) + remainder = Vm::Integer.new( number.register + 1) # BL udiv10 # r1 = r1 / 10 div10( tos , number , remainder ) # ADD r10, r10, 48 #'0' # make char out of digit (by using ascii encoding) - tos.add_code( add( left: remainder , right: remainder , extra: 48 )) + tos.add_code add( remainder , right: remainder , extra: 48 ) #STRB r10, [r1], 1 # store digit at end of buffer - tos.add_code( strb( left: string , right: remainder )) #and increment TODO check + tos.add_code strb( remainder , right: string ) #and increment TODO check # CMP r1, #0 # quotient non-zero? - tos.add_code( cmp left: number , right: 0 ) + tos.add_code cmp( number , right: 0 ) #BLNE utoa # conditional recursive call to utoa - tos.add_code( callne( left: tos )) + tos.add_code callne( tos , {} ) #LDMFD sp!, {r9, r10, pc} # function exit - restore and return - tos.add_code( pop regs: [:pc]) + tos.add_code pop( [:pc] , {} ) end private @@ -110,7 +110,7 @@ module Arm # takes argument in r1 # returns quotient in r1, remainder in r2 # SUB r2, r1, #10 # keep (x-10) for later - block.add_code sub( remainder , :right => number , :extra => 10 ) + block.add_code sub( remainder , right: number , :extra => 10 ) # SUB r1, r1, r1, lsr #2 # ADD r1, r1, r1, lsr #4 # ADD r1, r1, r1, lsr #8 @@ -124,7 +124,7 @@ module Arm end def syscall block , num - block.add_code mov( :r7 , :right => num ) + block.add_code mov( :r7 , right: num ) block.add_code swi( 0 , {}) Vm::Integer.new(0) #small todo, is this actually correct for all (that they return int) end diff --git a/lib/arm/memory_instruction.rb b/lib/arm/memory_instruction.rb index 6177cf53..82516e1a 100644 --- a/lib/arm/memory_instruction.rb +++ b/lib/arm/memory_instruction.rb @@ -15,8 +15,6 @@ module Arm @pre_post_index = 0 #P flag @add_offset = 0 #U flag @is_load = opcode.to_s[0] == "l" ? 1 : 0 #L (load) flag - @rn = :r0 # register zero = zero bit pattern - @rd = :r0 # register zero = zero bit pattern end # attr_accessor :i, :pre_post_index, :add_offset, :byte_access, :w, :is_load, :rn, :rd @@ -27,13 +25,8 @@ module Arm # Build representation for target address def build - if( @is_load ) - @rd = @left - arg = @attributes[:right] - else #store - @rd = @attributes[:right] - arg = @left - end + arg = @attributes[:right] + arg = "r#{arg.register}".to_sym if( arg.is_a? Vm::Word ) #str / ldr are _serious instructions. With BIG possibilities not half are implemented if (arg.is_a?(Symbol)) #symbol is register @rn = arg @@ -79,19 +72,24 @@ module Arm w = 0 #W flag byte_access = opcode.to_s[-1] == "b" ? 1 : 0 #B (byte) flag instuction_class = 0b01 # OPC_MEMORY_ACCESS - val = @operand.is_a?(Symbol) ? reg_code(@operand) : @operand - val |= (reg_code(@rd) << 12 ) - val |= (reg_code(@rn) << 12+4) #16 - val |= (@is_load << 12+4 +4) - val |= (w << 12+4 +4+1) - val |= (byte_access << 12+4 +4+1+1) - val |= (@add_offset << 12+4 +4+1+1+1) - val |= (@pre_post_index << 12+4 +4+1+1+1+1)#24 - val |= (i << 12+4 +4+1+1+1+1 +1) - val |= (instuction_class<<12+4 +4+1+1+1+1 +1+1) - val |= (cond_bit_code << 12+4 +4+1+1+1+1 +1+1+2) + val = @operand + val = reg_code(@operand) if @operand.is_a?(Symbol) + val = shift(val , 0 ) # for the test + val |= shift(reg_code(@left) , 12 ) + val |= shift(reg_code(@rn) , 12+4) #16 + val |= shift(@is_load , 12+4 +4) + val |= shift(w , 12+4 +4+1) + val |= shift(byte_access , 12+4 +4+1+1) + val |= shift(@add_offset , 12+4 +4+1+1+1) + val |= shift(@pre_post_index , 12+4 +4+1+1+1+1)#24 + val |= shift(i , 12+4 +4+1+1+1+1 +1) + val |= shift(instuction_class,12+4 +4+1+1+1+1 +1+1) + val |= shift(cond_bit_code , 12+4 +4+1+1+1+1 +1+1+2) io.write_uint32 val - + end + def shift val , by + raise "Not integer #{val}:#{val.class} #{inspect}" unless val.is_a? Fixnum + val << by end end end \ No newline at end of file diff --git a/test/test_arm.rb b/test/test_arm.rb index 9567d6a0..057210a9 100644 --- a/test/test_arm.rb +++ b/test/test_arm.rb @@ -27,15 +27,15 @@ class TestArmAsm < MiniTest::Test end end def test_adc - code = @machine.adc :r1, :right => :r3, :extra => :r5 + code = @machine.adc :r1, right: :r3, :extra => :r5 assert_code code , :adc , [0x05,0x10,0xa3,0xe0] #e0 a3 10 05 end def test_add - code = @machine.add :r1 , :right => :r1, :extra => :r3 + code = @machine.add :r1 , right: :r1, :extra => :r3 assert_code code , :add , [0x03,0x10,0x81,0xe0] #e0 81 10 03 end def test_and # inst eval doesn't really work with and - code = @machine.and( :r1 , :right => :r2 , :extra => :r3) + code = @machine.and( :r1 , right: :r2 , :extra => :r3) assert_code code , :and , [0x03,0x10,0x02,0xe0] #e0 01 10 03 end def test_b @@ -50,35 +50,35 @@ class TestArmAsm < MiniTest::Test assert_code code , :call, [0xff,0xff,0xff,0xeb] #ea ff ff fe end def test_bic - code = @machine.bic :r2 , :right => :r2 , :extra => :r3 + code = @machine.bic :r2 , right: :r2 , :extra => :r3 assert_code code , :bic , [0x03,0x20,0xc2,0xe1] #e3 c2 20 44 end def test_cmn - code = @machine.cmn :r1 , :right => :r2 + code = @machine.cmn :r1 , right: :r2 assert_code code , :cmn , [0x02,0x00,0x71,0xe1] #e1 71 00 02 end def test_cmp - code = @machine.cmp :r1 , :right => :r2 + code = @machine.cmp :r1 , right: :r2 assert_code code , :cmp , [0x02,0x00,0x51,0xe1] #e1 51 00 02 end def test_eor - code = @machine.eor :r2 , :right => :r2 , :extra => :r3 + code = @machine.eor :r2 , right: :r2 , :extra => :r3 assert_code code , :eor , [0x03,0x20,0x22,0xe0] #e0 22 20 03 end def test_ldr - code = @machine.ldr :r0, :right => :r0 + code = @machine.ldr :r0, right: :r0 assert_code code, :ldr , [0x00,0x00,0x90,0xe5] #e5 90 00 00 end def test_ldr2 - code = @machine.ldr :r0, :right => :r0 , :offset => 4 + code = @machine.ldr :r0, right: :r0 , :offset => 4 assert_code code, :ldr , [0x04,0x00,0x90,0xe5] #e5 90 00 04 end def test_ldrb - code = @machine.ldrb :r0, :right => :r0 + code = @machine.ldrb :r0, right: :r0 assert_code code, :ldrb , [0x00,0x00,0xd0,0xe5] #e5 d0 00 00 end def test_orr - code = @machine.orr :r2 , :right => :r2 , :extra => :r3 + code = @machine.orr :r2 , right: :r2 , :extra => :r3 assert_code code , :orr , [0x03,0x20,0x82,0xe1] #e1 82 20 03 end def test_push @@ -90,27 +90,27 @@ class TestArmAsm < MiniTest::Test assert_code code , :pop , [0x00,0x80,0xbd,0xe8] #e8 bd 80 00 end def test_rsb - code = @machine.rsb :r1 , :right => :r2 , :extra => :r3 + code = @machine.rsb :r1 , right: :r2 , :extra => :r3 assert_code code , :rsb , [0x03,0x10,0x62,0xe0]#e0 62 10 03 end def test_rsc - code = @machine.rsc :r2 , :right => :r3 , :extra => :r4 + code = @machine.rsc :r2 , right: :r3 , :extra => :r4 assert_code code , :rsc , [0x04,0x20,0xe3,0xe0]#e0 e3 20 04 end def test_sbc - code = @machine.sbc :r3, :right => :r4 , :extra => :r5 + code = @machine.sbc :r3, right: :r4 , :extra => :r5 assert_code code , :sbc , [0x05,0x30,0xc4,0xe0]#e0 c4 30 05 end def test_str - code = @machine.str :r0, :right => :r0 + code = @machine.str :r0, right: :r0 assert_code code, :str , [0x00,0x00,0x80,0xe5] #e5 81 00 00 end def test_strb - code = @machine.strb :r0, :right => :r0 + code = @machine.strb :r0, right: :r0 assert_code code, :strb , [0x00,0x00,0xc0,0xe5] #e5 c0 00 00 end def test_sub - code = @machine.sub :r2, :right => :r0, :extra => 1 + code = @machine.sub :r2, right: :r0, :extra => 1 assert_code code, :sub , [0x01,0x20,0x40,0xe2] #e2 40 20 01 end def test_swi @@ -118,19 +118,19 @@ class TestArmAsm < MiniTest::Test assert_code code , :swi , [0x05,0x00,0x00,0xef]#ef 00 00 05 end def test_teq - code = @machine.teq :r1 , :right => :r2 + code = @machine.teq :r1 , right: :r2 assert_code code , :teq , [0x02,0x00,0x31,0xe1] #e1 31 00 02 end def test_tst - code = @machine.tst :r1 , :right => :r2 + code = @machine.tst :r1 , right: :r2 assert_code code , :tst , [0x02,0x00,0x11,0xe1] #e1 11 00 02 end def test_mov - code = @machine.mov :r0, :right => 5 + code = @machine.mov :r0, right: 5 assert_code code , :mov , [0x05,0x00,0xa0,0xe3] #e3 a0 10 05 end def test_mvn - code = @machine.mvn :r1, :right => 5 + code = @machine.mvn :r1, right: 5 assert_code code , :mvn , [0x05,0x10,0xe0,0xe3] #e3 e0 10 05 end end diff --git a/test/test_small_program.rb b/test/test_small_program.rb index 73afa447..43781a9f 100644 --- a/test/test_small_program.rb +++ b/test/test_small_program.rb @@ -14,11 +14,11 @@ class TestSmallProg < MiniTest::Test def test_loop @program.main.instance_eval do - mov :r0, :right => 5 #1 + mov :r0, right: 5 #1 start = Vm::Block.new("start") add_code start start.instance_eval do - sub :r0, :right => :r0, :extra => 1 , :update_status_flag => 1 #2 + sub :r0, right: :r0, :extra => 1 , :update_status_flag => 1 #2 bne start ,{} #3 end end @@ -29,10 +29,10 @@ class TestSmallProg < MiniTest::Test hello = Vm::StringConstant.new "Hello Raisa\n" @program.add_object hello @program.main.instance_eval do - mov :r7, :right => 4 # 4 == write - mov :r0 , :right => 1 # stdout + mov :r7, right: 4 # 4 == write + mov :r0 , right: 1 # stdout add :r1 , :extra => hello # address of "hello Raisa" - mov :r2 , :right => hello.length + mov :r2 , right: hello.length swi 0 , {} #software interupt, ie kernel syscall end write(7 + hello.length/4 + 1 , 'hello')