change right syntax to 1.9 style, looks a little nicer

This commit is contained in:
Torsten Ruger
2014-05-15 21:35:45 +03:00
parent a0f0d08e81
commit 009c0895f4
4 changed files with 66 additions and 68 deletions

View File

@ -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

View File

@ -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