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
|
|
|
|
block.add_code cmp( first , right: 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
|
|
|
|
block.add_code add( result , right: first , :extra => 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
|
|
|
|
block.add_code sub( result , right: first , :extra => 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
|
|
|
|
block.add_code mov( first , right: right )
|
|
|
|
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
|
|
|
|
block.add_code mov( first , right: right )
|
|
|
|
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-15 18:41:51 +02:00
|
|
|
block.add_code 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-15 20:35:45 +02:00
|
|
|
block.add_code 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-15 20:35:45 +02:00
|
|
|
into.add_code 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-15 20:35:45 +02:00
|
|
|
entry.add_code 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
|
|
|
|
# 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-15 20:35:45 +02:00
|
|
|
entry.add_code mov( :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
|
2014-05-15 20:35:45 +02:00
|
|
|
block.add_code mov( :r2 , right: :r1 )
|
|
|
|
block.add_code mov( :r1 , right: :r0 )
|
|
|
|
block.add_code mov( :r0 , right: 1 ) # 1 == stdout
|
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-15 15:54:23 +02:00
|
|
|
# make a string out of the integer.
|
|
|
|
# as we don't have memory manegement yet, you have to pass the string in (ouch)
|
|
|
|
# in a weird twist the string is actually a string, while we actually use its address.
|
|
|
|
def integer_to_s block , string
|
|
|
|
number = Vm::Integer.new(0)
|
|
|
|
tos = Vm::Block.new("integer_to_s") # need to create a block to jump to
|
|
|
|
block.add_code(tos) # and then use the new block to add code
|
|
|
|
#STMFD sp!, {r9, r10, lr} #function entry save working regs (for recursion)
|
2014-05-15 20:35:45 +02:00
|
|
|
tos.add_code push( [:lr ] , {} ) #and the return address.
|
2014-05-15 15:54:23 +02:00
|
|
|
# MOV r9, r1 # preserve arguments over following
|
|
|
|
# MOV r10, r2 # function calls
|
|
|
|
# pin data, ie no saving
|
2014-05-15 20:35:45 +02:00
|
|
|
remainder = Vm::Integer.new( number.register + 1)
|
2014-05-15 15:54:23 +02:00
|
|
|
# BL udiv10 # r1 = r1 / 10
|
|
|
|
div10( tos , number , remainder )
|
|
|
|
# ADD r10, r10, 48 #'0' # make char out of digit (by using ascii encoding)
|
2014-05-15 20:35:45 +02:00
|
|
|
tos.add_code add( remainder , right: remainder , extra: 48 )
|
2014-05-15 15:54:23 +02:00
|
|
|
#STRB r10, [r1], 1 # store digit at end of buffer
|
2014-05-15 20:35:45 +02:00
|
|
|
tos.add_code strb( remainder , right: string ) #and increment TODO check
|
2014-05-15 15:54:23 +02:00
|
|
|
# CMP r1, #0 # quotient non-zero?
|
2014-05-15 20:35:45 +02:00
|
|
|
tos.add_code cmp( number , right: 0 )
|
2014-05-15 15:54:23 +02:00
|
|
|
#BLNE utoa # conditional recursive call to utoa
|
2014-05-15 20:35:45 +02:00
|
|
|
tos.add_code callne( tos , {} )
|
2014-05-15 15:54:23 +02:00
|
|
|
#LDMFD sp!, {r9, r10, pc} # function exit - restore and return
|
2014-05-15 20:35:45 +02:00
|
|
|
tos.add_code pop( [:pc] , {} )
|
2014-05-15 15:54:23 +02:00
|
|
|
end
|
|
|
|
|
2014-05-06 20:36:28 +02:00
|
|
|
private
|
2014-05-13 20:06:12 +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)
|
|
|
|
# not really a function, more a macro, hence private
|
|
|
|
def div10 block, number , remainder
|
|
|
|
|
|
|
|
# takes argument in r1
|
|
|
|
# returns quotient in r1, remainder in r2
|
|
|
|
# SUB r2, r1, #10 # keep (x-10) for later
|
2014-05-15 20:35:45 +02:00
|
|
|
block.add_code sub( remainder , right: number , :extra => 10 )
|
2014-05-16 09:42:25 +02:00
|
|
|
# SUB r1, r1, r1, lsr #2
|
|
|
|
block.add_code add( number , right: number , extra: number , shift_right: 4)
|
|
|
|
# ADD r1, r1, r1, lsr #4
|
|
|
|
# ADD r1, r1, r1, lsr #8
|
|
|
|
# ADD r1, r1, r1, lsr #16
|
|
|
|
# MOV r1, r1, lsr #3
|
|
|
|
# ADD r3, r1, r1, asl #2
|
|
|
|
# SUBS r2, r2, r3, asl #1 # calc (x-10) - (x/10)*10
|
|
|
|
# ADDPL r1, r1, #1 # fix-up quotient
|
|
|
|
# ADDMI r2, r2, #10 # fix-up remainder
|
|
|
|
# MOV pc, lr
|
2014-05-15 15:54:23 +02:00
|
|
|
end
|
|
|
|
|
2014-05-13 20:06:12 +02:00
|
|
|
def syscall block , num
|
2014-05-15 20:35:45 +02:00
|
|
|
block.add_code mov( :r7 , right: num )
|
2014-05-15 18:41:51 +02:00
|
|
|
block.add_code 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
|