2014-05-21 19:43:46 +03:00
|
|
|
require "vm/register_machine"
|
2014-05-03 22:18:04 +03:00
|
|
|
require_relative "stack_instruction"
|
|
|
|
require_relative "logic_instruction"
|
2014-05-10 15:34:05 +03:00
|
|
|
require_relative "move_instruction"
|
|
|
|
require_relative "compare_instruction"
|
2014-05-03 22:18:04 +03:00
|
|
|
require_relative "memory_instruction"
|
|
|
|
require_relative "call_instruction"
|
2014-05-14 10:47:30 +03:00
|
|
|
require_relative "constants"
|
2014-05-03 15:13:15 +03:00
|
|
|
|
|
|
|
module Arm
|
2014-05-21 19:43:46 +03:00
|
|
|
class ArmMachine < Vm::RegisterMachine
|
2014-06-10 13:29:01 +03:00
|
|
|
# The constants are here for readablility, the code uses access functions below
|
|
|
|
RETURN_REG = :r0
|
|
|
|
TYPE_REG = :r1
|
|
|
|
RECEIVER_REG = :r2
|
|
|
|
|
|
|
|
def return_register
|
|
|
|
RETURN_REG
|
|
|
|
end
|
|
|
|
def type_register
|
|
|
|
TYPE_REG
|
|
|
|
end
|
|
|
|
def receiver_register
|
|
|
|
RECEIVER_REG
|
|
|
|
end
|
2014-05-13 16:24:19 +03:00
|
|
|
|
2014-05-25 08:43:07 +03:00
|
|
|
def integer_equals block , left , right
|
2014-06-10 23:57:56 +03:00
|
|
|
block.add_code cmp( left , right )
|
2014-05-25 08:43:07 +03:00
|
|
|
Vm::BranchCondition.new :eq
|
|
|
|
end
|
2014-05-18 12:34:53 +03:00
|
|
|
def integer_less_or_equal block , left , right
|
2014-06-10 23:57:56 +03:00
|
|
|
block.add_code cmp( left , right )
|
2014-05-22 21:38:57 +03:00
|
|
|
Vm::BranchCondition.new :le
|
|
|
|
end
|
2014-05-28 20:10:16 +03:00
|
|
|
def integer_greater_or_equal block , left , right
|
2014-06-10 23:57:56 +03:00
|
|
|
block.add_code cmp( left , right )
|
2014-05-28 20:10:16 +03:00
|
|
|
Vm::BranchCondition.new :ge
|
|
|
|
end
|
2014-05-24 10:41:57 +03:00
|
|
|
def integer_less_than block , left , right
|
2014-06-10 23:57:56 +03:00
|
|
|
block.add_code cmp( left , right )
|
2014-05-24 10:41:57 +03:00
|
|
|
Vm::BranchCondition.new :lt
|
|
|
|
end
|
2014-05-22 21:38:57 +03:00
|
|
|
def integer_greater_than block , left , right
|
2014-06-10 23:57:56 +03:00
|
|
|
block.add_code cmp( left , right )
|
2014-05-22 21:38:57 +03:00
|
|
|
Vm::BranchCondition.new :gt
|
2014-05-13 16:24:19 +03:00
|
|
|
end
|
|
|
|
|
2014-06-01 21:03:08 +03:00
|
|
|
# TODO wrong type, should be object_reference. But that needs the actual typing to work
|
|
|
|
def integer_at_index block , result ,left , right
|
2014-06-10 23:57:56 +03:00
|
|
|
block.add_code ldr( result , left , right )
|
2014-06-01 21:03:08 +03:00
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2014-05-18 12:34:53 +03:00
|
|
|
def integer_plus block , result , left , right
|
2014-06-10 23:57:56 +03:00
|
|
|
block.add_code add( result , left , right )
|
2014-05-14 22:04:03 +03:00
|
|
|
result
|
2014-05-13 16:24:19 +03:00
|
|
|
end
|
|
|
|
|
2014-05-18 12:34:53 +03:00
|
|
|
def integer_minus block , result , left , right
|
2014-06-10 23:57:56 +03:00
|
|
|
block.add_code sub( result , left , right )
|
2014-05-14 22:04:03 +03:00
|
|
|
result
|
2014-05-14 11:33:23 +03:00
|
|
|
end
|
2014-06-02 15:11:48 +03:00
|
|
|
def integer_left_shift block , result , left , right
|
2014-06-10 23:57:56 +03:00
|
|
|
block.add_code mov( result , left , shift_lsr: right )
|
2014-06-02 15:11:48 +03:00
|
|
|
result
|
|
|
|
end
|
2014-05-14 11:33:23 +03:00
|
|
|
|
2014-05-13 21:06:12 +03:00
|
|
|
def function_call into , call
|
2014-05-13 21:15:02 +03:00
|
|
|
raise "Not CallSite #{call.inspect}" unless call.is_a? Vm::CallSite
|
2014-05-13 21:06:12 +03:00
|
|
|
raise "Not linked #{call.inspect}" unless call.function
|
2014-06-10 23:57:56 +03:00
|
|
|
into.add_code call( call.function )
|
2014-05-19 11:28:37 +03:00
|
|
|
raise "No return type for #{call.function.name}" unless call.function.return_type
|
2014-05-13 21:06:12 +03:00
|
|
|
call.function.return_type
|
2014-05-03 15:13:15 +03:00
|
|
|
end
|
2014-05-03 18:51:47 +03:00
|
|
|
|
2014-06-11 00:38:46 +03:00
|
|
|
def main_start context
|
|
|
|
entry = Vm::Block.new("main_entry",nil,nil)
|
2014-06-10 23:57:56 +03:00
|
|
|
entry.do_add mov( :fp , 0 )
|
2014-06-12 21:27:47 +03:00
|
|
|
entry.do_add call( context.function )
|
2014-06-11 00:38:46 +03:00
|
|
|
entry
|
2014-05-03 18:51:47 +03:00
|
|
|
end
|
2014-06-11 00:38:46 +03:00
|
|
|
def main_exit context
|
|
|
|
exit = Vm::Block.new("main_exit",nil,nil)
|
2014-05-13 21:06:12 +03:00
|
|
|
syscall(exit , 1)
|
2014-05-14 22:04:03 +03:00
|
|
|
exit
|
2014-05-03 22:18:04 +03:00
|
|
|
end
|
2014-05-13 18:21:24 +03:00
|
|
|
def function_entry block, f_name
|
2014-06-10 23:57:56 +03:00
|
|
|
block.do_add push( [:lr] )
|
2014-06-11 00:38:46 +03:00
|
|
|
block
|
2014-05-06 12:42:43 +03:00
|
|
|
end
|
2014-05-13 18:21:24 +03:00
|
|
|
def function_exit entry , f_name
|
2014-06-10 23:57:56 +03:00
|
|
|
entry.do_add pop( [:pc] )
|
2014-06-11 00:38:46 +03:00
|
|
|
entry
|
2014-05-06 12:42:43 +03:00
|
|
|
end
|
2014-05-13 21:06:12 +03:00
|
|
|
|
2014-06-12 08:34:46 +03:00
|
|
|
# assumes string in standard receiver reg (r2) and moves them down for the syscall
|
|
|
|
def write_stdout function #, string
|
2014-06-10 23:57:56 +03:00
|
|
|
# TODO save and restore r0
|
2014-06-12 09:07:03 +03:00
|
|
|
function.mov( :r0 , 1 ) # 1 == stdout
|
2014-06-12 16:23:57 +03:00
|
|
|
function.mov( :r1 , receiver_register )
|
|
|
|
function.mov( receiver_register , :r3 )
|
2014-06-12 08:34:46 +03:00
|
|
|
syscall( function.insertion_point , 4 ) # 4 == write
|
2014-05-06 21:36:28 +03:00
|
|
|
end
|
2014-05-13 21:06:12 +03:00
|
|
|
|
2014-05-16 17:31:01 +03:00
|
|
|
|
2014-05-15 16:54:23 +03: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 23:24:19 +03:00
|
|
|
# not really a function, more a macro,
|
2014-06-11 00:38:46 +03:00
|
|
|
def div10 function, number , remainder
|
2014-05-16 23:24:19 +03: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-06-12 13:47:06 +03:00
|
|
|
tmp = function.new_local
|
2014-06-11 00:38:46 +03:00
|
|
|
function.instance_eval do
|
2014-05-18 12:18:57 +03: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)
|
|
|
|
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 23:33:25 +03:00
|
|
|
end
|
2014-05-15 16:54:23 +03:00
|
|
|
end
|
|
|
|
|
2014-05-13 21:06:12 +03:00
|
|
|
def syscall block , num
|
2014-06-12 08:34:46 +03:00
|
|
|
# This is very arm specific, syscall number is passed in r7, other arguments like a c call ie 0 and up
|
|
|
|
sys = Vm::Integer.new( Vm::RegisterUse.new(:r7) )
|
|
|
|
ret = Vm::Integer.new( Vm::RegisterUse.new(RETURN_REG) )
|
|
|
|
block.do_add mov( sys , num )
|
2014-06-10 23:57:56 +03:00
|
|
|
block.do_add swi( 0 )
|
2014-06-12 08:34:46 +03:00
|
|
|
#todo should write type into r1 according to syscall
|
|
|
|
ret
|
2014-05-03 18:51:47 +03:00
|
|
|
end
|
2014-05-06 21:36:28 +03:00
|
|
|
|
2014-05-03 15:13:15 +03:00
|
|
|
end
|
|
|
|
end
|