2014-05-13 16:06:42 +02:00
|
|
|
require "vm/c_machine"
|
2014-05-03 21:18:04 +02:00
|
|
|
require_relative "stack_instruction"
|
|
|
|
require_relative "logic_instruction"
|
2014-05-10 14:34:05 +02:00
|
|
|
require_relative "move_instruction"
|
|
|
|
require_relative "compare_instruction"
|
2014-05-03 21:18:04 +02:00
|
|
|
require_relative "memory_instruction"
|
|
|
|
require_relative "call_instruction"
|
2014-05-14 09:47:30 +02:00
|
|
|
require_relative "constants"
|
2014-05-03 14:13:15 +02:00
|
|
|
|
|
|
|
module Arm
|
2014-05-13 16:06:42 +02:00
|
|
|
class ArmMachine < Vm::CMachine
|
2014-05-13 15:24:19 +02:00
|
|
|
|
2014-05-16 09:42:25 +02:00
|
|
|
def integer_less_or_equal block , first , right
|
2014-05-18 11:18:57 +02:00
|
|
|
block << cmp( first , right )
|
2014-05-13 17:21:24 +02:00
|
|
|
Vm::Bool.new
|
2014-05-13 15:24:19 +02:00
|
|
|
end
|
|
|
|
|
2014-05-16 09:42:25 +02:00
|
|
|
def integer_plus block , result , first , right
|
2014-05-18 11:18:57 +02:00
|
|
|
block << add( result , first , right )
|
2014-05-14 21:04:03 +02:00
|
|
|
result
|
2014-05-13 15:24:19 +02:00
|
|
|
end
|
|
|
|
|
2014-05-16 09:42:25 +02:00
|
|
|
def integer_minus block , result , first , right
|
2014-05-18 11:18:57 +02:00
|
|
|
block << sub( result , first , right )
|
2014-05-14 21:04:03 +02:00
|
|
|
result
|
2014-05-14 10:33:23 +02:00
|
|
|
end
|
|
|
|
|
2014-05-16 09:42:25 +02:00
|
|
|
def integer_load block , first , right
|
2014-05-18 11:18:57 +02:00
|
|
|
block << mov( first , right )
|
2014-05-16 09:42:25 +02:00
|
|
|
first
|
2014-05-03 14:13:15 +02:00
|
|
|
end
|
2014-05-14 11:02:54 +02:00
|
|
|
|
2014-05-16 09:42:25 +02:00
|
|
|
def integer_move block , first , right
|
2014-05-18 11:18:57 +02:00
|
|
|
block << mov( first , right )
|
2014-05-16 09:42:25 +02:00
|
|
|
first
|
2014-05-14 11:02:54 +02:00
|
|
|
end
|
|
|
|
|
2014-05-13 17:21:24 +02:00
|
|
|
def string_load block , str_lit , reg
|
2014-05-18 09:15:43 +02:00
|
|
|
block << add( "r#{reg}".to_sym , :extra => str_lit ) #right is pc, implicit
|
2014-05-06 20:36:28 +02:00
|
|
|
#second arg is a hack to get the stringlength without coding
|
2014-05-18 09:15:43 +02:00
|
|
|
block << mov( "r#{reg+1}".to_sym , right: str_lit.length )
|
2014-05-13 17:21:24 +02:00
|
|
|
str_lit
|
2014-05-06 20:36:28 +02:00
|
|
|
end
|
|
|
|
|
2014-05-13 20:06:12 +02:00
|
|
|
def function_call into , call
|
2014-05-13 20:15:02 +02:00
|
|
|
raise "Not CallSite #{call.inspect}" unless call.is_a? Vm::CallSite
|
2014-05-13 20:06:12 +02:00
|
|
|
raise "Not linked #{call.inspect}" unless call.function
|
2014-05-18 09:15:43 +02:00
|
|
|
into << call( call.function , {})
|
2014-05-13 20:06:12 +02:00
|
|
|
call.function.return_type
|
2014-05-03 14:13:15 +02:00
|
|
|
end
|
2014-05-03 17:51:47 +02:00
|
|
|
|
2014-05-13 17:21:24 +02:00
|
|
|
def main_start entry
|
2014-05-18 09:15:43 +02:00
|
|
|
entry << mov( :fp , right: 0 )
|
2014-05-03 17:51:47 +02:00
|
|
|
end
|
2014-05-13 17:21:24 +02:00
|
|
|
def main_exit exit
|
2014-05-13 20:06:12 +02:00
|
|
|
syscall(exit , 1)
|
2014-05-14 21:04:03 +02:00
|
|
|
exit
|
2014-05-03 21:18:04 +02:00
|
|
|
end
|
2014-05-13 17:21:24 +02:00
|
|
|
def function_entry block, f_name
|
2014-05-18 09:15:43 +02:00
|
|
|
block << push( [:lr] , {})
|
2014-05-06 11:42:43 +02:00
|
|
|
end
|
2014-05-13 17:21:24 +02:00
|
|
|
def function_exit entry , f_name
|
2014-05-18 09:15:43 +02:00
|
|
|
entry << pop( [:pc] , {})
|
2014-05-06 11:42:43 +02:00
|
|
|
end
|
2014-05-13 20:06:12 +02:00
|
|
|
|
|
|
|
# assumes string in r0 and r1 and moves them along for the syscall
|
|
|
|
def write_stdout block
|
2014-05-16 22:33:25 +02:00
|
|
|
block.instance_eval do
|
|
|
|
mov( :r2 , right: :r1 )
|
|
|
|
mov( :r1 , right: :r0 )
|
|
|
|
mov( :r0 , right: 1 ) # 1 == stdout
|
|
|
|
end
|
2014-05-13 20:06:12 +02:00
|
|
|
syscall( block , 4 )
|
2014-05-06 20:36:28 +02:00
|
|
|
end
|
2014-05-13 20:06:12 +02:00
|
|
|
|
2014-05-16 16:31:01 +02:00
|
|
|
|
2014-05-15 15:54:23 +02:00
|
|
|
# the number (a Vm::integer) is (itself) divided by 10, ie overwritten by the result
|
|
|
|
# and the remainder is overwritten (ie an out argument)
|
2014-05-16 22:24:19 +02:00
|
|
|
# not really a function, more a macro,
|
2014-05-15 15:54:23 +02:00
|
|
|
def div10 block, number , remainder
|
2014-05-16 22:24:19 +02:00
|
|
|
# Note about division: devision is MUCH more expensive than one would have thought
|
|
|
|
# And coding it is a bit of a mind leap: it's all about finding a a result that gets the
|
|
|
|
# remainder smaller than an int. i'll post some links sometime. This is from the arm manual
|
2014-05-16 22:33:25 +02:00
|
|
|
block.instance_eval do
|
2014-05-18 11:18:57 +02:00
|
|
|
sub( remainder , number , 10 )
|
|
|
|
sub( number , number , number , shift_lsr: 2)
|
|
|
|
add( number , number , number , shift_lsr: 4)
|
|
|
|
add( number , number , number , shift_lsr: 8)
|
|
|
|
add( number , number , number , shift_lsr: 16)
|
|
|
|
mov( number , number , shift_lsr: 3)
|
2014-05-16 22:33:25 +02:00
|
|
|
tmp = Vm::Integer.new( remainder.register + 1)
|
2014-05-18 11:18:57 +02:00
|
|
|
add( tmp , number , number , shift_lsl: 2)
|
|
|
|
sub( remainder , remainder , tmp , shift_lsl: 1 , update_status: 1)
|
|
|
|
add( number , number, 1 , condition_code: :pl )
|
|
|
|
add( remainder , remainder , 10 , condition_code: :mi )
|
2014-05-16 22:33:25 +02:00
|
|
|
end
|
2014-05-15 15:54:23 +02:00
|
|
|
end
|
|
|
|
|
2014-05-13 20:06:12 +02:00
|
|
|
def syscall block , num
|
2014-05-18 09:15:43 +02:00
|
|
|
block << mov( :r7 , right: num )
|
|
|
|
block << swi( 0 , {})
|
2014-05-13 20:06:12 +02:00
|
|
|
Vm::Integer.new(0) #small todo, is this actually correct for all (that they return int)
|
2014-05-03 17:51:47 +02:00
|
|
|
end
|
2014-05-06 20:36:28 +02:00
|
|
|
|
2014-05-03 14:13:15 +02:00
|
|
|
end
|
|
|
|
end
|