move all arm instructions to own folder and fold inheritance

This commit is contained in:
Torsten Ruger 2014-10-02 22:28:34 +03:00
parent 9923eb0b07
commit 1be71918a5
7 changed files with 158 additions and 36 deletions

27
lib/arm/instruction.rb Normal file
View File

@ -0,0 +1,27 @@
module Arm
# The arm machine has following instruction classes
# - Memory
# - Stack
# - Logic
# - Math
# - Control/Compare
# - Move
# - Call class Instruction
class Instruction
def initialize options
@attributes = options
end
attr_reader :attributes
def opcode
@attributes[:opcode]
end
end
end
require_relative "constants"
require_relative "instructions/call_instruction"
require_relative "instructions/compare_instruction"
require_relative "instructions/logic_instruction"
require_relative "instructions/memory_instruction"
require_relative "instructions/move_instruction"
require_relative "instructions/stack_instruction"

View File

@ -1,5 +1,3 @@
require_relative "nodes"
module Arm
# There are only three call instructions in arm branch (b), call (bl) and syscall (swi)
@ -11,12 +9,21 @@ module Arm
# swi (SoftWareInterrupt) or system call is how we call the kernel.
# in Arm the register layout is different and so we have to place the syscall code into register 7
# Registers 0-6 hold the call values as for a normal c call
class CallInstruction < ::Register::CallInstruction
class CallInstruction < Instruction
include Arm::Constants
def initialize(first, attributes)
super(first , attributes)
super(attributes)
@first = first
opcode = @attributes[:opcode].to_s
if opcode.length == 3 and opcode[0] == "b"
@attributes[:condition_code] = opcode[1,2].to_sym
@attributes[:opcode] = :b
end
if opcode.length == 6 and opcode[0] == "c"
@attributes[:condition_code] = opcode[4,2].to_sym
@attributes[:opcode] = :call
end
@attributes[:update_status] = 0
@attributes[:condition_code] = :al if @attributes[:condition_code] == nil
end
@ -64,5 +71,22 @@ module Arm
end
end
end#class
def uses
if opcode == :call
@first.args.collect {|arg| arg.register }
else
[]
end
end
def assigns
if opcode == :call
[RegisterReference.new(RegisterMachine.instance.return_register)]
else
[]
end
end
def to_s
"#{opcode} #{@first.to_asm} #{super}"
end
end
end

View File

@ -1,9 +1,11 @@
module Arm
class CompareInstruction < Register::CompareInstruction
class CompareInstruction < Instruction
include Arm::Constants
def initialize(left , right , attributes)
super(left , right, attributes)
super(attributes)
@left = left
@right = right.is_a?(Fixnum) ? IntegerConstant.new(right) : right
@attributes[:condition_code] = :al if @attributes[:condition_code] == nil
@operand = 0
@immediate = 0
@ -87,5 +89,17 @@ module Arm
raise "Not integer #{val}:#{val.class} #{inspect}" unless val.is_a? Fixnum
val << by
end
def uses
ret = [@left.register ]
ret << @right.register unless @right.is_a? Constant
ret
end
def assigns
[]
end
def to_s
"#{opcode} #{@left.to_asm} , #{@right.to_asm} #{super}"
end
end
end

View File

@ -1,10 +1,16 @@
module Arm
class LogicInstruction < Register::LogicInstruction
class LogicInstruction < Instruction
include Arm::Constants
# result = left op right
#
# Logic instruction are your basic operator implementation. But unlike the (normal) code we write
# these Instructions must have "place" to write their results. Ie when you write 4 + 5 in ruby
# the result is sort of up in the air, but with Instructions the result must be assigned
def initialize(result , left , right , attributes = {})
super(result ,left , right , attributes)
super(attributes)
@result = result
@left = left
@right = right.is_a?(Fixnum) ? Virtual::IntegerConstant.new(right) : right
@attributes[:update_status] = 0 if @attributes[:update_status] == nil
@attributes[:condition_code] = :al if @attributes[:condition_code] == nil
@operand = 0
@ -13,6 +19,7 @@ module Arm
@immediate = 0
end
attr_accessor :result , :left , :right
def assemble(io)
# don't overwrite instance variables, to make assembly repeatable
left = @left
@ -64,5 +71,15 @@ module Arm
raise "Not integer #{val}:#{val.class} #{inspect}" unless val.is_a? Fixnum
val << by
end
def uses
ret = []
ret << @left.register if @left and not @left.is_a? Constant
ret << @right.register if @right and not @right.is_a?(Constant)
ret
end
def assigns
[@result.register]
end
end
end

View File

@ -1,13 +1,14 @@
require_relative "nodes"
module Arm
# ADDRESSING MODE 2
# Implemented: immediate offset with offset=0
class MemoryInstruction < ::Register::MemoryInstruction
include Arm::Constants
def initialize(result , left , right = nil , attributes = {})
super(result , left , right , attributes)
class MemoryInstruction < Instruction
include Arm::Constants
def initialize result , left , right = nil , attributes = {}
super(attributes)
@result = result
@left = left
@right = right
@attributes[:update_status] = 0 if @attributes[:update_status] == nil
@attributes[:condition_code] = :al if @attributes[:condition_code] == nil
@operand = 0
@ -98,5 +99,14 @@ module Arm
raise "Not integer #{val}:#{val.class} #{inspect}" unless val.is_a? Fixnum
val << by
end
def uses
ret = [@left.register ]
ret << @right.register unless @right.nil?
ret
end
def assigns
[@result.register]
end
end
end

View File

@ -1,10 +1,12 @@
module Arm
class MoveInstruction < Register::MoveInstruction
class MoveInstruction < Instruction
include Arm::Constants
def initialize(to , from , attributes)
super(to , from , attributes)
def initialize to , from , options = {}
super(options)
@to = to
@from = from.is_a?(Fixnum) ? Virtual::IntegerConstant.new(from) : from
raise "move must have from set #{inspect}" unless from
@attributes[:update_status] = 0 if @attributes[:update_status] == nil
@attributes[:condition_code] = :al if @attributes[:condition_code] == nil
@attributes[:opcode] = attributes[:opcode]
@ -15,6 +17,7 @@ module Arm
@from = Virtual::IntegerConstant.new( @from ) if( @from.is_a? Fixnum )
@extra = nil
end
attr_accessor :to , :from
# arm intructions are pretty sensible, and always 4 bytes (thumb not supported)
# but not all constants fit into the part of the instruction that is left after the instruction code,
@ -103,5 +106,12 @@ module Arm
raise "Not integer #{val}:#{val.class} in #{inspect}" unless val.is_a? Fixnum
val << by
end
def uses
@from.is_a?(Constant) ? [] : [@from.register]
end
def assigns
[@to.register]
end
end
end

View File

@ -1,12 +1,12 @@
require_relative "constants"
module Arm
# ADDRESSING MODE 4
class StackInstruction < Register::StackInstruction
class StackInstruction < Instruction
include Arm::Constants
def initialize(first , attributes)
super(first , attributes)
super(attributes)
@first = first
@attributes[:update_status] = 0 if @attributes[:update_status] == nil
@attributes[:condition_code] = :al if @attributes[:condition_code] == nil
@attributes[:opcode] = attributes[:opcode]
@ -56,5 +56,25 @@ module Arm
val |= (cond << 16+4+ 1+1+1+1 +2+2)
io.write_uint32 val
end
def is_push?
opcode == :push
end
def is_pop?
!is_push?
end
def uses
is_push? ? regs : []
end
def assigns
is_pop? ? regs : []
end
def regs
@first
end
def to_s
"#{opcode} [#{@first.collect {|f| f.to_asm}.join(',') }] #{super}"
end
end
end