2014-05-03 14:13:15 +02:00
|
|
|
require "vm/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-03 14:13:15 +02:00
|
|
|
|
|
|
|
module Arm
|
|
|
|
class ArmMachine < Vm::Machine
|
|
|
|
|
2014-05-03 21:18:04 +02:00
|
|
|
# defines a method in the current class, with the name inst (first erg)
|
|
|
|
# the method instantiates an instruction of the given class which gets passed a single hash as arg
|
|
|
|
|
|
|
|
# gets called for every "standard" instruction.
|
|
|
|
# may be used for machine specific ones too
|
|
|
|
def define_instruction inst , clazz
|
|
|
|
super
|
|
|
|
return
|
2014-05-07 13:58:38 +02:00
|
|
|
# need to use create_method and move to attributes hash
|
2014-05-03 21:18:04 +02:00
|
|
|
define_method("#{inst}s") do |*args|
|
|
|
|
instruction clazz , inst , :al , 1 , *args
|
|
|
|
end
|
|
|
|
ArmMachine::COND_CODES.keys.each do |suffix|
|
2014-05-07 13:58:38 +02:00
|
|
|
define_method("#{inst}#{suffix}") do |attributes|
|
2014-05-03 21:18:04 +02:00
|
|
|
instruction clazz , inst , suffix , 0 , *args
|
|
|
|
end
|
2014-05-07 13:58:38 +02:00
|
|
|
define_method("#{inst}s#{suffix}") do |attributes|
|
2014-05-03 21:18:04 +02:00
|
|
|
instruction clazz , inst , suffix , 1 , *args
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-05-05 21:21:11 +02:00
|
|
|
|
2014-05-05 08:35:40 +02:00
|
|
|
def word_load value , reg
|
2014-05-10 16:55:02 +02:00
|
|
|
raise "not a register :#{reg}:" unless reg.class == Symbol
|
2014-05-06 11:42:43 +02:00
|
|
|
mov( :left => reg , :right => value )
|
2014-05-03 14:13:15 +02:00
|
|
|
end
|
2014-05-06 20:36:28 +02:00
|
|
|
def string_load str_lit , reg
|
|
|
|
[ add( :left => "r#{reg}".to_sym , :extra => str_lit ) , #right is pc, implicit
|
|
|
|
#second arg is a hack to get the stringlength without coding
|
|
|
|
mov( :left => "r#{reg+1}".to_sym , :right => str_lit.length ) ]
|
|
|
|
end
|
|
|
|
|
2014-05-05 08:35:40 +02:00
|
|
|
def function_call call
|
|
|
|
raise "Not FunctionCall #{call.inspect}" unless call.is_a? Vm::FunctionCall
|
2014-05-06 20:36:28 +02:00
|
|
|
call.args.each do | arg |
|
|
|
|
end
|
2014-05-05 21:21:11 +02:00
|
|
|
bl( :left => call.function )
|
2014-05-03 14:13:15 +02:00
|
|
|
end
|
2014-05-03 17:51:47 +02:00
|
|
|
|
2014-05-06 11:42:43 +02:00
|
|
|
def main_start
|
2014-05-05 23:12:04 +02:00
|
|
|
entry = Vm::Block.new("main_entry")
|
|
|
|
entry.add_code mov( :left => :fp , :right => 0 )
|
2014-05-03 17:51:47 +02:00
|
|
|
end
|
|
|
|
def main_exit
|
2014-05-05 23:12:04 +02:00
|
|
|
entry = Vm::Block.new("main_exit")
|
2014-05-06 20:36:28 +02:00
|
|
|
entry.add_code syscall(1)
|
2014-05-03 21:18:04 +02:00
|
|
|
end
|
2014-05-06 11:42:43 +02:00
|
|
|
def function_entry f_name
|
|
|
|
entry = Vm::Block.new("#{f_name}_entry")
|
2014-05-10 14:47:27 +02:00
|
|
|
# entry.add_code push( :regs => [:lr] )
|
2014-05-06 11:42:43 +02:00
|
|
|
end
|
|
|
|
def function_exit f_name
|
|
|
|
entry = Vm::Block.new("#{f_name}_exit")
|
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-06 20:36:28 +02:00
|
|
|
def putstring
|
|
|
|
put = Vm::Block.new("putstring_code")
|
|
|
|
# should be another level of indirection, ie write(io,str)
|
|
|
|
put.add_code mov( :left => :r2 , :right => :r1 )
|
|
|
|
put.add_code mov( :left => :r1 , :right => :r0 )
|
|
|
|
put.add_code mov( :left => :r0 , :right => 1 ) #stdout
|
|
|
|
put.add_code syscall(4)
|
|
|
|
end
|
|
|
|
private
|
2014-05-03 21:18:04 +02:00
|
|
|
def syscall num
|
2014-05-05 23:12:04 +02:00
|
|
|
[mov( :left => :r7 , :right => num ) , swi( :left => 0 )]
|
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
|