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-13 17:21:24 +02:00
|
|
|
def integer_less_or_equal block , left , right
|
|
|
|
block.add_code cmp(:left => left , :right => right )
|
|
|
|
Vm::Bool.new
|
2014-05-13 15:24:19 +02:00
|
|
|
end
|
|
|
|
|
2014-05-13 17:21:24 +02:00
|
|
|
def integer_plus block , left , right
|
|
|
|
block.add_code add(:left => left , :right => right )
|
|
|
|
left
|
2014-05-13 15:24:19 +02:00
|
|
|
end
|
|
|
|
|
2014-05-13 17:21:24 +02:00
|
|
|
def integer_load block , left , right
|
|
|
|
reg = "r#{left.register}".to_sym
|
|
|
|
block.add_code mov( :left => reg , :right => right )
|
|
|
|
left
|
2014-05-03 14:13:15 +02:00
|
|
|
end
|
2014-05-13 17:21:24 +02:00
|
|
|
def string_load block , str_lit , reg
|
|
|
|
block.add_code add( :left => "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-13 17:21:24 +02:00
|
|
|
block.add_code mov( :left => "r#{reg+1}".to_sym , :right => str_lit.length )
|
|
|
|
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
|
|
|
|
into.add_code bl( :left => call.function )
|
|
|
|
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-05 23:12:04 +02:00
|
|
|
entry.add_code mov( :left => :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-03 21:18:04 +02:00
|
|
|
end
|
2014-05-13 17:21:24 +02:00
|
|
|
def function_entry block, f_name
|
|
|
|
# entry.add_code push( :regs => [:lr] )
|
|
|
|
block
|
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-06 20:36:28 +02:00
|
|
|
entry.add_code mov( :left => :pc , :right => :lr )
|
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
|
|
|
|
block.add_code mov( :left => :r2 , :right => :r1 )
|
|
|
|
block.add_code mov( :left => :r1 , :right => :r0 )
|
|
|
|
block.add_code mov( :left => :r0 , :right => 1 ) # 1 == stdout
|
|
|
|
syscall( block , 4 )
|
2014-05-06 20:36:28 +02:00
|
|
|
end
|
2014-05-13 20:06:12 +02:00
|
|
|
|
2014-05-06 20:36:28 +02:00
|
|
|
private
|
2014-05-13 20:06:12 +02:00
|
|
|
|
|
|
|
def syscall block , num
|
|
|
|
block.add_code mov( :left => :r7 , :right => num )
|
|
|
|
block.add_code swi( :left => 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
|
|
|
|
2014-05-03 14:13:15 +02:00
|
|
|
end
|
|
|
|
end
|