2014-05-03 14:13:15 +02:00
|
|
|
require "vm/machine"
|
2014-05-03 21:18:04 +02:00
|
|
|
require_relative "instruction"
|
|
|
|
require_relative "stack_instruction"
|
|
|
|
require_relative "logic_instruction"
|
|
|
|
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
|
|
|
|
# need to use create_method and move to options hash
|
|
|
|
define_method("#{inst}s") do |*args|
|
|
|
|
instruction clazz , inst , :al , 1 , *args
|
|
|
|
end
|
|
|
|
ArmMachine::COND_CODES.keys.each do |suffix|
|
|
|
|
define_method("#{inst}#{suffix}") do |options|
|
|
|
|
instruction clazz , inst , suffix , 0 , *args
|
|
|
|
end
|
|
|
|
define_method("#{inst}s#{suffix}") do |options|
|
|
|
|
instruction clazz , inst , suffix , 1 , *args
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-05 08:35:40 +02:00
|
|
|
def word_load value , reg
|
|
|
|
e = Vm::Block.new("load_#{value}")
|
|
|
|
e.add_code( MoveInstruction.new( :left => reg , :right => value ) )
|
2014-05-03 14:13:15 +02:00
|
|
|
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-05 10:03:43 +02:00
|
|
|
bl( :function => call.function )
|
2014-05-03 14:13:15 +02:00
|
|
|
end
|
2014-05-03 17:51:47 +02:00
|
|
|
|
|
|
|
def main_entry
|
2014-05-05 10:03:43 +02:00
|
|
|
mov( :left => :fp , :right => 0 )
|
2014-05-03 17:51:47 +02:00
|
|
|
end
|
|
|
|
def main_exit
|
2014-05-05 10:03:43 +02:00
|
|
|
syscall(0)
|
2014-05-03 21:18:04 +02:00
|
|
|
end
|
|
|
|
def syscall num
|
2014-05-05 10:03:43 +02:00
|
|
|
mov( :left => 7 , :right => num )
|
|
|
|
swi( {} )
|
2014-05-03 17:51:47 +02:00
|
|
|
end
|
2014-05-03 14:13:15 +02:00
|
|
|
end
|
|
|
|
end
|