renove Node class as it wasnt doing anything

This commit is contained in:
Torsten Ruger 2014-04-23 12:38:38 +03:00
parent 8955cf31da
commit 1423b8a845
8 changed files with 30 additions and 33 deletions

View File

@ -17,24 +17,24 @@ module Asm
rfp sl fp ip sp lr pc rfp sl fp ip sp lr pc
).each { |reg| ).each { |reg|
define_method(reg) { define_method(reg) {
Asm::RegisterNode.new(reg) Asm::Register.new(reg)
} }
} }
def instruction(name, *args) def instruction(name, *args)
node = Asm::InstructionNode.new node = Asm::Instruction.new
node.opcode = name.to_s node.opcode = name.to_s
node.args = [] node.args = []
args.each { |arg| args.each { |arg|
if (arg.is_a?(Asm::RegisterNode)) if (arg.is_a?(Asm::Register))
node.args << arg node.args << arg
elsif (arg.is_a?(Integer)) elsif (arg.is_a?(Integer))
node.args << Asm::NumLiteralNode.new(arg) node.args << Asm::NumLiteral.new(arg)
elsif (arg.is_a?(String)) elsif (arg.is_a?(String))
node.args << add_string(arg) node.args << add_string(arg)
elsif (arg.is_a?(Symbol)) elsif (arg.is_a?(Symbol))
node.args << Asm::LabelRefNode.new(arg.to_s) node.args << Asm::Label.new(arg.to_s)
elsif (arg.is_a?(Asm::Arm::GeneratorLabel)) elsif (arg.is_a?(Asm::Arm::GeneratorLabel))
node.args << arg node.args << arg
else else

View File

@ -97,15 +97,15 @@ module Asm
builder.assemble io, as builder.assemble io, as
when :b, :bl when :b, :bl
arg = args[0] arg = args[0]
if (arg.is_a?(Asm::NumLiteralNode)) if (arg.is_a?(Asm::NumLiteral))
jmp_val = arg.value >> 2 jmp_val = arg.value >> 2
packed = [jmp_val].pack('l') packed = [jmp_val].pack('l')
# signed 32-bit, condense to 24-bit # signed 32-bit, condense to 24-bit
# TODO add check that the value fits into 24 bits # TODO add check that the value fits into 24 bits
io << packed[0,3] io << packed[0,3]
elsif (arg.is_a?(Asm::LabelObject) or arg.is_a?(Asm::LabelRefNode)) elsif (arg.is_a?(Asm::LabelObject) or arg.is_a?(Asm::Label))
#not yet tested/supported #not yet tested/supported
# arg = @ast_asm.object_for_label(arg.label, self) if arg.is_a?(Asm::LabelRefNode) # arg = @ast_asm.object_for_label(arg.label, self) if arg.is_a?(Asm::Label)
# as.add_relocation(io.tell, arg, Asm::Arm::R_ARM_PC24, RelocHandler) # as.add_relocation(io.tell, arg, Asm::Arm::R_ARM_PC24, RelocHandler)
#write 0 "for now" and let relocation happen #write 0 "for now" and let relocation happen
io << "\x00\x00\x00" io << "\x00\x00\x00"
@ -115,7 +115,7 @@ module Asm
io.write_uint8 OPCODES[opcode] | (COND_CODES[@cond] << 4) io.write_uint8 OPCODES[opcode] | (COND_CODES[@cond] << 4)
when :swi when :swi
arg = args[0] arg = args[0]
if (arg.is_a?(Asm::NumLiteralNode)) if (arg.is_a?(Asm::NumLiteral))
packed = [arg.value].pack('L')[0,3] packed = [arg.value].pack('L')[0,3]
io << packed io << packed
io.write_uint8 0b1111 | (COND_CODES[@cond] << 4) io.write_uint8 0b1111 | (COND_CODES[@cond] << 4)

View File

@ -36,7 +36,7 @@ module Asm
OPC_STACK = 0b10 OPC_STACK = 0b10
def reg_ref(arg) def reg_ref(arg)
if (not arg.is_a?(Asm::RegisterNode)) if (not arg.is_a?(Asm::Register))
raise Asm::AssemblyError.new('argument must be a register', arg) raise Asm::AssemblyError.new('argument must be a register', arg)
end end

View File

@ -33,7 +33,7 @@ module Asm
@pre_post_index = 0 @pre_post_index = 0
@w = 0 @w = 0
@operand = 0 @operand = 0
if (arg.is_a?(Asm::RegisterNode)) if (arg.is_a?(Asm::Register))
@rn = reg_ref(arg) @rn = reg_ref(arg)
if(arg.offset != 0) if(arg.offset != 0)
@operand = arg.offset @operand = arg.offset
@ -48,7 +48,7 @@ module Asm
raise Asm::AssemblyError.new('reference offset too large/small (max 4095)', argr.right) raise Asm::AssemblyError.new('reference offset too large/small (max 4095)', argr.right)
end end
end end
elsif (arg.is_a?(Asm::LabelRefNode) or arg.is_a?(Asm::NumLiteralNode)) elsif (arg.is_a?(Asm::Label) or arg.is_a?(Asm::NumLiteral))
@pre_post_index = 1 @pre_post_index = 1
@rn = 15 # pc @rn = 15 # pc
@use_addrtable_reloc = true @use_addrtable_reloc = true
@ -72,10 +72,10 @@ module Asm
# move towards simpler model # move towards simpler model
if (@use_addrtable_reloc) if (@use_addrtable_reloc)
# closest_addrtable = Asm::Arm.closest_addrtable(as) # closest_addrtable = Asm::Arm.closest_addrtable(as)
if (@addrtable_reloc_target.is_a?(Asm::LabelRefNode)) if (@addrtable_reloc_target.is_a?(Asm::Label))
obj = generator.object_for_label(@addrtable_reloc_target.label, inst) obj = generator.object_for_label(@addrtable_reloc_target.label, inst)
# ref_label = closest_addrtable.add_label(obj) # ref_label = closest_addrtable.add_label(obj)
elsif (@addrtable_reloc_target.is_a?(Asm::NumLiteralNode)) elsif (@addrtable_reloc_target.is_a?(Asm::NumLiteral))
# ref_label = closest_addrtable.add_const(@addrtable_reloc_target.value) # ref_label = closest_addrtable.add_const(@addrtable_reloc_target.value)
end end
as.add_relocation io.tell, ref_label, Asm::Arm::R_ARM_PC12, as.add_relocation io.tell, ref_label, Asm::Arm::R_ARM_PC12,

View File

@ -46,12 +46,12 @@ module Asm
def build_operand(arg , position = 0) def build_operand(arg , position = 0)
#position only needed for calculating relative addresses to data objects #position only needed for calculating relative addresses to data objects
#there is a design stink here which makes my head ache. But shanti shanti #there is a design stink here which makes my head ache. But shanti shanti
if arg.is_a?(Asm::StringNode) if arg.is_a?(Asm::StringLiteral)
# do pc relative addressing with the difference to the instuction # do pc relative addressing with the difference to the instuction
# 8 is for the funny pipeline adjustment (ie oc pointing to fetch and not execute) # 8 is for the funny pipeline adjustment (ie oc pointing to fetch and not execute)
arg = Asm::NumLiteralNode.new( arg.position - position - 8 ) arg = Asm::NumLiteral.new( arg.position - position - 8 )
end end
if (arg.is_a?(Asm::NumLiteralNode)) if (arg.is_a?(Asm::NumLiteral))
if (arg.value.fits_u8?) if (arg.value.fits_u8?)
# no shifting needed # no shifting needed
@operand = arg.value @operand = arg.value
@ -62,10 +62,10 @@ module Asm
else else
raise Asm::AssemblyError.new(Asm::ERRSTR_NUMERIC_TOO_LARGE, arg) raise Asm::AssemblyError.new(Asm::ERRSTR_NUMERIC_TOO_LARGE, arg)
end end
elsif (arg.is_a?(Asm::RegisterNode)) elsif (arg.is_a?(Asm::Register))
@operand = reg_ref(arg) @operand = reg_ref(arg)
@i = 0 @i = 0
elsif (arg.is_a?(Asm::ShiftNode)) elsif (arg.is_a?(Asm::Shift))
rm_ref = reg_ref(arg.argument) rm_ref = reg_ref(arg.argument)
@i = 0 @i = 0
shift_op = {'lsl' => 0b000, 'lsr' => 0b010, 'asr' => 0b100, shift_op = {'lsl' => 0b000, 'lsr' => 0b010, 'asr' => 0b100,
@ -76,12 +76,12 @@ module Asm
end end
arg1 = arg.value arg1 = arg.value
if (arg1.is_a?(Asm::NumLiteralNode)) if (arg1.is_a?(Asm::NumLiteral))
if (arg1.value >= 32) if (arg1.value >= 32)
raise Asm::AssemblyError.new('cannot shift by more than 31', arg1) raise Asm::AssemblyError.new('cannot shift by more than 31', arg1)
end end
shift_imm = arg1.value shift_imm = arg1.value
elsif (arg1.is_a?(Asm::RegisterNode)) elsif (arg1.is_a?(Asm::Register))
shift_op |= 0x1; shift_op |= 0x1;
shift_imm = reg_ref(arg1) << 1 shift_imm = reg_ref(arg1) << 1
elsif (arg.type == 'rrx') elsif (arg.type == 'rrx')

View File

@ -16,7 +16,7 @@ module Asm
def add_string str def add_string str
value = @string_table[str] value = @string_table[str]
return value if value return value if value
data = Asm::StringNode.new(str) data = Asm::StringLiteral.new(str)
@string_table[str] = data @string_table[str] = data
end end

View File

@ -1,13 +1,10 @@
module Asm module Asm
class Node class Instruction
end
class InstructionNode < Node
attr_accessor :opcode, :args attr_accessor :opcode, :args
end end
class ShiftNode < Node class Shift
attr_accessor :type, :value, :argument attr_accessor :type, :value, :argument
end end
@ -15,7 +12,7 @@ module Asm
# but also refer to an address. In other words they can be an operand for instructions. # but also refer to an address. In other words they can be an operand for instructions.
# Arm has addressing modes abound, and so can add to a register before actually using it # Arm has addressing modes abound, and so can add to a register before actually using it
# If can actually shift or indeed shift what it adds, but not implemented # If can actually shift or indeed shift what it adds, but not implemented
class RegisterNode < Node class Register
attr_accessor :name , :offset attr_accessor :name , :offset
def initialize name def initialize name
@name = name @name = name
@ -32,22 +29,22 @@ module Asm
#maybe not used at all as code_gen::instruction raises if used. #maybe not used at all as code_gen::instruction raises if used.
# instead now using Arrays # instead now using Arrays
class RegisterListNode < Node class RegisterList
attr_accessor :registers attr_accessor :registers
def initialize regs def initialize regs
@registers = regs @registers = regs
regs.each{ |reg| raise "not a reg #{sym} , #{reg}" unless reg.is_a?(Asm::RegisterNode) } regs.each{ |reg| raise "not a reg #{sym} , #{reg}" unless reg.is_a?(Asm::Register) }
end end
end end
class NumLiteralNode < Node class NumLiteral
attr_accessor :value attr_accessor :value
def initialize val def initialize val
@value = val @value = val
end end
end end
class LabelRefNode < Node class Label
attr_accessor :label, :label_object attr_accessor :label, :label_object
def initialize label , object = nil def initialize label , object = nil
@label = label @label = label

View File

@ -1,5 +1,5 @@
module Asm module Asm
class StringNode class StringLiteral
def initialize(str) def initialize(str)
#align #align
length = str.length length = str.length