rubyx/lib/arm/arm_machine.rb

94 lines
3.0 KiB
Ruby
Raw Normal View History

2014-05-21 18:43:46 +02:00
require "vm/register_machine"
2014-05-03 21:18:04 +02:00
require_relative "stack_instruction"
require_relative "logic_instruction"
require_relative "move_instruction"
require_relative "compare_instruction"
2014-05-03 21:18:04 +02:00
require_relative "memory_instruction"
require_relative "call_instruction"
require_relative "constants"
module Arm
2014-05-21 18:43:46 +02:00
class ArmMachine < Vm::RegisterMachine
2014-05-18 11:34:53 +02:00
def integer_less_or_equal block , left , right
block << cmp( left , right )
2014-05-22 20:38:57 +02:00
Vm::BranchCondition.new :le
end
def integer_greater_than block , left , right
block << cmp( left , right )
Vm::BranchCondition.new :gt
end
2014-05-18 11:34:53 +02:00
def integer_plus block , result , left , right
block << add( result , left , right )
2014-05-14 21:04:03 +02:00
result
end
2014-05-18 11:34:53 +02:00
def integer_minus block , result , left , right
block << sub( result , left , right )
2014-05-14 21:04:03 +02:00
result
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
2014-05-18 11:34:53 +02:00
into << call( call.function )
2014-05-19 10:28:37 +02:00
raise "No return type for #{call.function.name}" unless call.function.return_type
call.function.return_type
end
2014-05-03 17:51:47 +02:00
def main_start entry
2014-05-18 11:30:49 +02:00
entry << mov( :fp , 0 )
2014-05-03 17:51:47 +02:00
end
def main_exit exit
syscall(exit , 1)
2014-05-14 21:04:03 +02:00
exit
2014-05-03 21:18:04 +02:00
end
def function_entry block, f_name
2014-05-18 11:34:53 +02:00
block << push( [:lr] )
end
def function_exit entry , f_name
2014-05-18 11:34:53 +02:00
entry << pop( [:pc] )
end
# 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
2014-05-18 11:30:49 +02:00
mov( :r2 , :r1 )
mov( :r1 , :r0 )
mov( :r0 , 1 ) # 1 == stdout
2014-05-16 22:33:25 +02:00
end
syscall( block , 4 )
2014-05-06 20:36:28 +02:00
end
# the number (a Vm::integer) is (itself) divided by 10, ie overwritten by the result
# and the remainder is overwritten (ie an out argument)
# not really a function, more a macro,
def div10 block, number , remainder
# 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)
tmp = function.new_local
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
end
def syscall block , num
2014-05-18 11:30:49 +02:00
block << mov( :r7 , num )
2014-05-18 11:34:53 +02:00
block << swi( 0 )
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
end
end