2014-04-14 20:52:16 +02:00
|
|
|
require 'asm/arm/arm_assembler'
|
|
|
|
require 'asm/arm/instruction'
|
2014-04-17 14:35:55 +02:00
|
|
|
require_relative 'generator_label'
|
2014-04-16 11:10:58 +02:00
|
|
|
require 'asm/nodes'
|
2014-04-14 20:52:16 +02:00
|
|
|
require 'stream_reader'
|
2014-04-14 17:09:56 +02:00
|
|
|
require 'stringio'
|
2014-04-20 01:28:57 +02:00
|
|
|
require "asm/data_object"
|
2014-04-14 17:09:56 +02:00
|
|
|
|
2014-04-16 11:02:28 +02:00
|
|
|
class Asm::Arm::CodeGenerator
|
|
|
|
def initialize
|
|
|
|
@asm = Asm::Assembler.new
|
|
|
|
@externs = []
|
|
|
|
end
|
2014-04-14 17:09:56 +02:00
|
|
|
|
2014-04-16 11:02:28 +02:00
|
|
|
def data(str)
|
|
|
|
@asm.add_object Asm::DataObject.new(str)
|
|
|
|
end
|
2014-04-14 17:09:56 +02:00
|
|
|
|
2014-04-16 11:02:28 +02:00
|
|
|
%w(r0 r1 r2 r3 r4 r5 r6 r7 r8 r9 r10 r11 r12
|
|
|
|
r13 r14 r15 a1 a2 a3 a4 v1 v2 v3 v4 v5 v6
|
|
|
|
rfp sl fp ip sp lr pc
|
|
|
|
).each { |reg|
|
|
|
|
define_method(reg) {
|
|
|
|
[:reg, reg]
|
|
|
|
}
|
|
|
|
}
|
2014-04-14 17:09:56 +02:00
|
|
|
|
2014-04-16 11:02:28 +02:00
|
|
|
def instruction(name, *args)
|
|
|
|
node = Asm::InstructionNode.new
|
|
|
|
node.opcode = name.to_s
|
|
|
|
node.args = []
|
2014-04-14 17:09:56 +02:00
|
|
|
|
2014-04-16 11:02:28 +02:00
|
|
|
args.each { |arg|
|
|
|
|
if (arg.is_a?(Array))
|
|
|
|
if (arg[0] == :reg)
|
2014-04-21 16:35:38 +02:00
|
|
|
node.args << Asm::RegisterNode.new(arg[1])
|
2014-04-14 17:09:56 +02:00
|
|
|
end
|
2014-04-16 11:02:28 +02:00
|
|
|
elsif (arg.is_a?(Integer))
|
2014-04-21 16:35:38 +02:00
|
|
|
node.args << Asm::NumLiteralNode.new(arg)
|
2014-04-16 11:02:28 +02:00
|
|
|
elsif (arg.is_a?(Symbol))
|
2014-04-21 16:35:38 +02:00
|
|
|
node.args << Asm::LabelRefNode.new(arg.to_s)
|
2014-04-17 14:35:55 +02:00
|
|
|
elsif (arg.is_a?(Asm::Arm::GeneratorLabel) or arg.is_a?(Asm::Arm::GeneratorExternLabel))
|
2014-04-16 11:02:28 +02:00
|
|
|
node.args << arg
|
|
|
|
else
|
|
|
|
raise 'Invalid argument `%s\' for instruction' % arg.inspect
|
2014-04-14 17:09:56 +02:00
|
|
|
end
|
2014-04-16 11:02:28 +02:00
|
|
|
}
|
2014-04-14 17:09:56 +02:00
|
|
|
|
2014-04-16 11:02:28 +02:00
|
|
|
@asm.add_object Asm::Arm::Instruction.new(node)
|
|
|
|
end
|
2014-04-14 17:09:56 +02:00
|
|
|
|
2014-04-20 22:48:04 +02:00
|
|
|
%w(adc add and bic eor orr rsb rsc sbc sub mov mvn cmn cmp teq tst b bl bx
|
|
|
|
push pop swi str strb ldr ldrb
|
2014-04-16 11:02:28 +02:00
|
|
|
).each { |inst|
|
|
|
|
define_method(inst) { |*args|
|
|
|
|
instruction inst.to_sym, *args
|
|
|
|
}
|
|
|
|
define_method(inst+'s') { |*args|
|
|
|
|
instruction (inst+'s').to_sym, *args
|
|
|
|
}
|
|
|
|
%w(al eq ne cs mi hi cc pl ls vc lt le ge gt vs
|
|
|
|
).each { |cond_suffix|
|
|
|
|
define_method(inst+cond_suffix) { |*args|
|
|
|
|
instruction (inst+cond_suffix).to_sym, *args
|
|
|
|
}
|
|
|
|
define_method(inst+'s'+cond_suffix) { |*args|
|
|
|
|
instruction (inst+'s'+cond_suffix).to_sym, *args
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-04-14 17:09:56 +02:00
|
|
|
|
2014-04-16 11:02:28 +02:00
|
|
|
def label
|
2014-04-17 14:35:55 +02:00
|
|
|
Asm::Arm::GeneratorLabel.new(@asm)
|
2014-04-16 11:02:28 +02:00
|
|
|
end
|
2014-04-14 17:09:56 +02:00
|
|
|
|
2014-04-16 11:02:28 +02:00
|
|
|
def label!
|
2014-04-17 14:35:55 +02:00
|
|
|
lbl = Asm::Arm::GeneratorLabel.new(@asm)
|
2014-04-16 11:02:28 +02:00
|
|
|
lbl.set!
|
|
|
|
lbl
|
|
|
|
end
|
2014-04-14 17:09:56 +02:00
|
|
|
|
2014-04-16 11:02:28 +02:00
|
|
|
def extern(sym)
|
|
|
|
if (lbl = @externs.find { |extern| extern.name == sym })
|
|
|
|
lbl
|
|
|
|
else
|
2014-04-20 01:28:57 +02:00
|
|
|
@externs << lbl = Asm::Arm::GeneratorExternLabel.new(sym)
|
2014-04-16 11:02:28 +02:00
|
|
|
@asm.add_object lbl
|
|
|
|
lbl
|
2014-04-14 17:09:56 +02:00
|
|
|
end
|
|
|
|
end
|
2014-04-16 11:02:28 +02:00
|
|
|
|
|
|
|
def assemble
|
|
|
|
io = StringIO.new
|
|
|
|
@asm.assemble(io)
|
|
|
|
io.string
|
|
|
|
end
|
|
|
|
|
|
|
|
def relocations
|
|
|
|
@asm.relocations
|
|
|
|
end
|
|
|
|
end
|