2014-04-25 17:37:19 +02:00
|
|
|
require_relative 'call_instruction'
|
|
|
|
require_relative 'stack_instruction'
|
|
|
|
require_relative 'logic_instruction'
|
|
|
|
require_relative 'memory_instruction'
|
2014-04-23 12:52:34 +02:00
|
|
|
|
2014-04-25 11:28:23 +02:00
|
|
|
module Asm
|
|
|
|
|
2014-04-25 17:37:19 +02:00
|
|
|
class Code ; end
|
|
|
|
|
|
|
|
# A Block is the smalles unit of code, a list of instructions as it were
|
|
|
|
# It is also a point to jump/branch to. An address in the final stream.
|
|
|
|
# To allow for forward branches creation does not fix the position. Either set or assembling does that.
|
|
|
|
|
|
|
|
# Blocks are also used to create instructions, and so Block has functions for every cpu instruction
|
|
|
|
# and to make using the apu function easier, there are functions that create registers as well
|
|
|
|
class Block < Code
|
2014-04-25 20:08:19 +02:00
|
|
|
extend Forwardable # forward block call back to program
|
|
|
|
def_delegator :@program, :block
|
2014-04-25 17:37:19 +02:00
|
|
|
|
|
|
|
def initialize(asm)
|
|
|
|
super()
|
|
|
|
@codes = []
|
|
|
|
@position = 0
|
2014-04-25 20:08:19 +02:00
|
|
|
@program = asm
|
2014-04-23 12:52:34 +02:00
|
|
|
end
|
|
|
|
|
2014-04-25 17:37:19 +02:00
|
|
|
ArmMachine::REGISTERS.each do |reg , number|
|
|
|
|
define_method(reg) { Asm::Register.new(reg , number) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def instruction(clazz, opcode , condition_code , update_status , *args)
|
|
|
|
arg_nodes = []
|
|
|
|
args.each do |arg|
|
|
|
|
if (arg.is_a?(Asm::Register))
|
|
|
|
arg_nodes << arg
|
|
|
|
elsif (arg.is_a?(Integer))
|
|
|
|
arg_nodes << Asm::NumLiteral.new(arg)
|
|
|
|
elsif (arg.is_a?(String))
|
2014-04-25 20:08:19 +02:00
|
|
|
arg_nodes << @program.add_string(arg)
|
|
|
|
elsif (arg.is_a?(Asm::Block))
|
2014-04-25 17:37:19 +02:00
|
|
|
arg_nodes << arg
|
|
|
|
else
|
|
|
|
raise "Invalid argument #{arg.inspect} for instruction"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
add_code clazz.new(opcode , condition_code , update_status , arg_nodes)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def self.define_instruction(inst , clazz )
|
|
|
|
define_method(inst) do |*args|
|
|
|
|
instruction clazz , inst , :al , 0 , *args
|
|
|
|
end
|
|
|
|
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 |*args|
|
|
|
|
instruction clazz , inst , suffix , 0 , *args
|
|
|
|
end
|
|
|
|
define_method("#{inst}s#{suffix}") do |*args|
|
|
|
|
instruction clazz , inst , suffix , 1 , *args
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
[:push, :pop].each do |inst|
|
|
|
|
define_instruction(inst , StackInstruction)
|
|
|
|
end
|
|
|
|
|
|
|
|
[:adc, :add, :and, :bic, :eor, :orr, :rsb, :rsc, :sbc, :sub].each do |inst|
|
|
|
|
define_instruction(inst , LogicInstruction)
|
|
|
|
end
|
|
|
|
[:mov, :mvn].each do |inst|
|
|
|
|
define_instruction(inst , MoveInstruction)
|
|
|
|
end
|
|
|
|
[:cmn, :cmp, :teq, :tst].each do |inst|
|
|
|
|
define_instruction(inst , CompareInstruction)
|
|
|
|
end
|
|
|
|
[:strb, :str , :ldrb, :ldr].each do |inst|
|
|
|
|
define_instruction(inst , MemoryInstruction)
|
|
|
|
end
|
|
|
|
[:b, :bl , :swi].each do |inst|
|
|
|
|
define_instruction(inst , CallInstruction)
|
|
|
|
end
|
|
|
|
|
|
|
|
# setting a block fixes it's position in the stream.
|
|
|
|
# For backwards jumps, positions of blocks are known at creation, but for forward off course not.
|
|
|
|
# So then one can create a block, branch to it and set it later.
|
2014-04-25 11:28:23 +02:00
|
|
|
def set!
|
2014-04-25 20:08:19 +02:00
|
|
|
@program.add_block self
|
2014-04-25 11:28:23 +02:00
|
|
|
self
|
2014-04-23 12:52:34 +02:00
|
|
|
end
|
2014-04-25 11:28:23 +02:00
|
|
|
|
2014-04-25 20:08:19 +02:00
|
|
|
# length of the codes. In arm it would be the length * 4
|
|
|
|
# (strings are stored globally in the Program)
|
2014-04-23 12:52:34 +02:00
|
|
|
def length
|
2014-04-25 20:08:19 +02:00
|
|
|
@codes.inject(0) {| sum , item | sum + item.length}
|
2014-04-25 17:37:19 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def add_code(kode)
|
|
|
|
kode.at(@position)
|
|
|
|
length = kode.length
|
|
|
|
@position += length
|
|
|
|
@codes << kode
|
2014-04-23 12:52:34 +02:00
|
|
|
end
|
|
|
|
|
2014-04-25 10:56:53 +02:00
|
|
|
def assemble(io)
|
2014-04-25 17:37:19 +02:00
|
|
|
@codes.each do |obj|
|
|
|
|
obj.assemble io
|
|
|
|
end
|
2014-04-23 12:52:34 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|