rubyx/lib/arm/machine_code.rb

82 lines
2.9 KiB
Ruby
Raw Normal View History

2016-12-14 12:43:13 +01:00
module Arm
class MachineCode
2018-03-11 11:41:15 +01:00
def function_call( into , call )
raise "Not CallSite #{call.inspect}" unless call.is_a? Risc::CallSite
2016-12-14 12:43:13 +01:00
raise "Not linked #{call.inspect}" unless call.function
into.add_code call( call.function )
raise "No return type for #{call.function.name}" unless call.function.return_type
call.function.return_type
end
2018-03-11 11:41:15 +01:00
def main_start( context )
entry = Risc::Block.new("main_entry",nil,nil)
2016-12-14 12:43:13 +01:00
entry.add_code mov( :fp , 0 )
entry.add_code call( context.function )
entry
end
2018-03-11 11:41:15 +01:00
def main_exit( context )
exit = Risc::Block.new("main_exit",nil,nil)
2016-12-14 12:43:13 +01:00
syscall(exit , 1)
exit
end
2018-03-11 11:41:15 +01:00
def function_entry( block, f_name )
2016-12-14 12:43:13 +01:00
block.add_code push( [:lr] )
block
end
2018-03-11 11:41:15 +01:00
def function_exit( entry , f_name )
2016-12-14 12:43:13 +01:00
entry.add_code pop( [:pc] )
entry
end
# assumes string in standard receiver reg (r2) and moves them down for the syscall
2018-03-11 11:41:15 +01:00
def write_stdout( function ) #, string
2016-12-14 12:43:13 +01:00
# TODO save and restore r0
function.mov( :r0 , 1 ) # 1 == stdout
function.mov( :r1 , receiver_register )
function.mov( receiver_register , :r3 )
syscall( function.insertion_point , 4 ) # 4 == write
end
# stop, do not return
2018-03-11 11:41:15 +01:00
def exit( function )#, string
2016-12-14 12:43:13 +01:00
syscall( function.insertion_point , 1 ) # 1 == exit
end
# the number (a Risc::integer) is (itself) divided by 10, ie overwritten by the result
2016-12-14 12:43:13 +01:00
# and the remainder is overwritten (ie an out argument)
# not really a function, more a macro,
2018-03-11 11:41:15 +01:00
def div10( function, number , remainder )
2016-12-14 12:43:13 +01: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
tmp = function.new_local
function.instance_eval do
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 )
end
end
2018-03-11 11:41:15 +01:00
def syscall( block , num )
2016-12-14 12:43:13 +01:00
# This is very arm specific, syscall number is passed in r7,
# other arguments like a c call ie 0 and up
sys = Risc::Integer.new( Risc::RiscValue.new(SYSCALL_REG) )
ret = Risc::Integer.new( Risc::RiscValue.new(RETURN_REG) )
2016-12-14 12:43:13 +01:00
block.add_code mov( sys , num )
block.add_code swi( 0 )
#todo should write type into r1 according to syscall
ret
end
end
end