2014-10-03 09:25:10 +02:00
|
|
|
require_relative "instruction"
|
|
|
|
|
|
|
|
module Arm
|
2015-05-04 22:03:52 +02:00
|
|
|
|
|
|
|
# A Machines main responsibility in the framework is to instantiate Instructions
|
2014-10-03 09:25:10 +02:00
|
|
|
|
|
|
|
# Value functions are mapped to machines by concatenating the values class name + the methd name
|
|
|
|
# Example: IntegerValue.plus( value ) -> Machine.signed_plus (value )
|
2015-05-04 22:03:52 +02:00
|
|
|
|
2014-10-05 00:12:16 +02:00
|
|
|
# Also, shortcuts are created to easily instantiate Instruction objects.
|
2014-10-03 09:25:10 +02:00
|
|
|
# Example: pop -> StackInstruction.new( {:opcode => :pop}.merge(options) )
|
2015-05-30 11:20:39 +02:00
|
|
|
# Instructions work with options, so you can pass anything in, and the only thing the functions
|
|
|
|
# does is save you typing the clazz.new. It passes the function name as the :opcode
|
2015-05-04 22:03:52 +02:00
|
|
|
|
2014-10-03 09:25:10 +02:00
|
|
|
class ArmMachine
|
2015-05-04 22:03:52 +02:00
|
|
|
|
2014-10-03 09:25:10 +02:00
|
|
|
# conditions specify all the possibilities for branches. Branches are b + condition
|
2015-05-04 22:03:52 +02:00
|
|
|
# Example: beq means brach if equal.
|
2014-10-03 09:25:10 +02:00
|
|
|
# :al means always, so bal is an unconditional branch (but b() also works)
|
2015-05-30 11:20:39 +02:00
|
|
|
CONDITIONS = [:al ,:eq ,:ne ,:lt ,:le ,:ge,:gt ,:cs ,:mi ,:hi ,:cc ,:pl,:ls ,:vc ,:vs]
|
2015-05-04 22:03:52 +02:00
|
|
|
|
2014-10-03 09:25:10 +02:00
|
|
|
# here we create the shortcuts for the "standard" instructions, see above
|
|
|
|
# Derived machines may use own instructions and define functions for them if so desired
|
2014-10-04 16:34:51 +02:00
|
|
|
def self.init
|
2014-10-03 09:25:10 +02:00
|
|
|
[:push, :pop].each do |inst|
|
|
|
|
define_instruction_one(inst , StackInstruction)
|
|
|
|
end
|
|
|
|
[:adc, :add, :and, :bic, :eor, :orr, :rsb, :rsc, :sbc, :sub].each do |inst|
|
|
|
|
define_instruction_three(inst , LogicInstruction)
|
|
|
|
end
|
|
|
|
[:mov, :mvn].each do |inst|
|
|
|
|
define_instruction_two(inst , MoveInstruction)
|
|
|
|
end
|
|
|
|
[:cmn, :cmp, :teq, :tst].each do |inst|
|
|
|
|
define_instruction_two(inst , CompareInstruction)
|
|
|
|
end
|
|
|
|
[:strb, :str , :ldrb, :ldr].each do |inst|
|
|
|
|
define_instruction_three(inst , MemoryInstruction)
|
|
|
|
end
|
|
|
|
[:b, :call , :swi].each do |inst|
|
|
|
|
define_instruction_one(inst , CallInstruction)
|
|
|
|
end
|
2015-05-04 22:03:52 +02:00
|
|
|
# create all possible brach instructions, but the CallInstruction demangles the
|
2014-10-03 09:25:10 +02:00
|
|
|
# code, and has opcode set to :b and :condition_code set to the condition
|
|
|
|
CONDITIONS.each do |suffix|
|
|
|
|
define_instruction_one("b#{suffix}".to_sym , CallInstruction)
|
|
|
|
define_instruction_one("call#{suffix}".to_sym , CallInstruction)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-10-04 16:34:51 +02:00
|
|
|
def self.create_method(name, &block)
|
2014-10-03 09:25:10 +02:00
|
|
|
self.class.send(:define_method, name , &block)
|
|
|
|
end
|
|
|
|
|
2014-10-04 16:34:51 +02:00
|
|
|
def self.class_for clazz
|
2014-10-03 09:25:10 +02:00
|
|
|
my_module = self.class.name.split("::").first
|
|
|
|
clazz_name = clazz.name.split("::").last
|
|
|
|
if(my_module != Register )
|
|
|
|
module_class = eval("#{my_module}::#{clazz_name}") rescue nil
|
|
|
|
clazz = module_class if module_class
|
|
|
|
end
|
|
|
|
clazz
|
|
|
|
end
|
2014-10-04 16:34:51 +02:00
|
|
|
|
2014-10-03 09:25:10 +02:00
|
|
|
#defining the instruction (opcode, symbol) as an given class.
|
|
|
|
# the class is a Register::Instruction derived base class and to create machine specific function
|
2015-05-04 22:03:52 +02:00
|
|
|
# an actual machine must create derived classes (from this base class)
|
2014-10-03 09:25:10 +02:00
|
|
|
# These instruction classes must follow a naming pattern and take a hash in the contructor
|
|
|
|
# Example, a mov() opcode instantiates a Register::MoveInstruction
|
2015-05-30 11:20:39 +02:00
|
|
|
# for an Arm machine, a class Arm::MoveInstruction < Register::MoveInstruction exists, and it
|
|
|
|
# will be used to define the mov on an arm machine.
|
2015-05-04 22:03:52 +02:00
|
|
|
# This methods picks up that derived class and calls a define_instruction methods that can
|
|
|
|
# be overriden in subclasses
|
2014-10-04 16:34:51 +02:00
|
|
|
def self.define_instruction_one(inst , clazz , defaults = {} )
|
|
|
|
clazz = class_for(clazz)
|
2014-10-03 09:25:10 +02:00
|
|
|
create_method(inst) do |first , options = nil|
|
|
|
|
options = {} if options == nil
|
|
|
|
options.merge defaults
|
|
|
|
options[:opcode] = inst
|
2015-06-01 07:34:17 +02:00
|
|
|
first = Register::RegisterReference.convert(first)
|
2014-10-03 09:25:10 +02:00
|
|
|
clazz.new(first , options)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# same for two args (left right, from to etc)
|
2014-10-04 16:34:51 +02:00
|
|
|
def self.define_instruction_two(inst , clazz , defaults = {} )
|
2014-10-03 09:25:10 +02:00
|
|
|
clazz = self.class_for(clazz)
|
|
|
|
create_method(inst) do |left ,right , options = nil|
|
|
|
|
options = {} if options == nil
|
|
|
|
options.merge defaults
|
2015-06-01 07:34:17 +02:00
|
|
|
left = Register::RegisterReference.convert(left)
|
|
|
|
right = Register::RegisterReference.convert(right)
|
2014-10-03 09:25:10 +02:00
|
|
|
options[:opcode] = inst
|
|
|
|
clazz.new(left , right ,options)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# same for three args (result = left right,)
|
2014-10-04 16:34:51 +02:00
|
|
|
def self.define_instruction_three(inst , clazz , defaults = {} )
|
2014-10-03 09:25:10 +02:00
|
|
|
clazz = self.class_for(clazz)
|
|
|
|
create_method(inst) do |result , left ,right = nil , options = nil|
|
|
|
|
options = {} if options == nil
|
|
|
|
options.merge defaults
|
|
|
|
options[:opcode] = inst
|
2015-06-01 07:34:17 +02:00
|
|
|
result = Register::RegisterReference.convert(result)
|
|
|
|
left = Register::RegisterReference.convert(left)
|
|
|
|
right = Register::RegisterReference.convert(right)
|
2014-10-03 09:25:10 +02:00
|
|
|
clazz.new(result, left , right ,options)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-10-04 16:34:51 +02:00
|
|
|
Arm::ArmMachine.init
|
2014-10-03 13:52:47 +02:00
|
|
|
require_relative "passes/call_implementation"
|
2015-07-17 12:21:57 +02:00
|
|
|
require_relative "passes/branch_implementation"
|
2015-06-24 15:07:27 +02:00
|
|
|
require_relative "passes/syscall_implementation"
|
2014-10-05 00:12:16 +02:00
|
|
|
require_relative "passes/save_implementation"
|
|
|
|
require_relative "passes/transfer_implementation"
|
|
|
|
require_relative "passes/get_implementation"
|
|
|
|
require_relative "passes/set_implementation"
|
|
|
|
require_relative "passes/return_implementation"
|
|
|
|
require_relative "passes/constant_implementation"
|