diff --git a/lib/asm/arm/arm_assembler.rb b/lib/asm/arm_assembler.rb similarity index 84% rename from lib/asm/arm/arm_assembler.rb rename to lib/asm/arm_assembler.rb index ffca9844..6701605d 100644 --- a/lib/asm/arm/arm_assembler.rb +++ b/lib/asm/arm_assembler.rb @@ -1,14 +1,13 @@ require 'asm/assembler' -require 'asm/arm/arm_assembler' -require 'asm/arm/instruction' -require 'asm/arm/generator_label' +require 'asm/arm_assembler' +require 'asm/instruction' +require 'asm/generator_label' require 'asm/nodes' require 'stream_reader' require 'stringio' -require "asm/string_node" +require "asm/string_literal" module Asm - module Arm class ArmAssembler < Asm::Assembler @@ -22,27 +21,26 @@ module Asm } def instruction(name, *args) - node = Asm::Instruction.new - node.opcode = name.to_s - node.args = [] + opcode = name.to_s + arg_nodes = [] args.each { |arg| if (arg.is_a?(Asm::Register)) - node.args << arg + arg_nodes << arg elsif (arg.is_a?(Integer)) - node.args << Asm::NumLiteral.new(arg) + arg_nodes << Asm::NumLiteral.new(arg) elsif (arg.is_a?(String)) - node.args << add_string(arg) + arg_nodes << add_string(arg) elsif (arg.is_a?(Symbol)) - node.args << Asm::Label.new(arg.to_s) - elsif (arg.is_a?(Asm::Arm::GeneratorLabel)) - node.args << arg + arg_nodes << Asm::Label.new(arg.to_s) + elsif (arg.is_a?(Asm::GeneratorLabel)) + arg_nodes << arg else raise 'Invalid argument `%s\' for instruction' % arg.inspect end } - add_value Asm::Arm::Instruction.new(node) + add_value Asm::Instruction.new(opcode , arg_nodes) end %w(adc add and bic eor orr rsb rsc sbc sub mov mvn cmn cmp teq tst b bl bx @@ -119,6 +117,5 @@ module Asm raise 'unknown relocation type' end end - end end diff --git a/lib/asm/assembler.rb b/lib/asm/assembler.rb index b2f14409..0aaae2b0 100644 --- a/lib/asm/assembler.rb +++ b/lib/asm/assembler.rb @@ -32,7 +32,7 @@ module Asm end def label - label = Asm::Arm::GeneratorLabel.new(self) + label = Asm::GeneratorLabel.new(self) @labels << label label end diff --git a/lib/asm/arm/generator_label.rb b/lib/asm/generator_label.rb similarity index 79% rename from lib/asm/arm/generator_label.rb rename to lib/asm/generator_label.rb index 16eab7b6..a15b81ea 100644 --- a/lib/asm/arm/generator_label.rb +++ b/lib/asm/generator_label.rb @@ -1,6 +1,6 @@ require "asm/label_object" -class Asm::Arm::GeneratorLabel < Asm::LabelObject +class Asm::GeneratorLabel < Asm::LabelObject def initialize(asm) @asm = asm end diff --git a/lib/asm/arm/instruction.rb b/lib/asm/instruction.rb similarity index 91% rename from lib/asm/arm/instruction.rb rename to lib/asm/instruction.rb index e7098ef0..4e2cbc75 100644 --- a/lib/asm/arm/instruction.rb +++ b/lib/asm/instruction.rb @@ -1,20 +1,16 @@ require "asm/assembly_error" -require "asm/arm/instruction_tools" -require "asm/arm/normal_builder" -require "asm/arm/memory_access_builder" -require "asm/arm/stack_builder" +require "asm/instruction_tools" +require "asm/normal_builder" +require "asm/memory_access_builder" +require "asm/stack_builder" module Asm - module Arm class Instruction include InstructionTools COND_POSTFIXES = Regexp.union(%w(eq ne cs cc mi pl vs vc hi ls ge lt gt le al)).source - def initialize(node) - @node = node - opcode = node.opcode - args = node.args + def initialize(opcode , args) opcode = opcode.downcase @cond = :al @@ -106,7 +102,7 @@ module Asm elsif (arg.is_a?(Asm::LabelObject) or arg.is_a?(Asm::Label)) #not yet tested/supported # 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::R_ARM_PC24, RelocHandler) #write 0 "for now" and let relocation happen io << "\x00\x00\x00" else @@ -123,9 +119,8 @@ module Asm raise Asm::AssemblyError.new(Asm::ERRSTR_INVALID_ARG, arg) end else - raise Asm::AssemblyError.new("unknown instruction #{opcode}", @node) + raise Asm::AssemblyError.new("unknown instruction #{opcode}", self) end end end - end end \ No newline at end of file diff --git a/lib/asm/arm/instruction_tools.rb b/lib/asm/instruction_tools.rb similarity index 98% rename from lib/asm/arm/instruction_tools.rb rename to lib/asm/instruction_tools.rb index 09f5babd..e46f7b8a 100644 --- a/lib/asm/arm/instruction_tools.rb +++ b/lib/asm/instruction_tools.rb @@ -1,5 +1,5 @@ module Asm - module Arm + module InstructionTools OPCODES = { :adc => 0b0101, :add => 0b0100, @@ -55,5 +55,4 @@ module Asm ref end end - end end \ No newline at end of file diff --git a/lib/asm/arm/memory_access_builder.rb b/lib/asm/memory_access_builder.rb similarity index 93% rename from lib/asm/arm/memory_access_builder.rb rename to lib/asm/memory_access_builder.rb index 47620641..620e27a7 100644 --- a/lib/asm/arm/memory_access_builder.rb +++ b/lib/asm/memory_access_builder.rb @@ -1,11 +1,10 @@ require "asm/nodes" module Asm - module Arm # ADDRESSING MODE 2 # Implemented: immediate offset with offset=0 class MemoryAccessBuilder - include Asm::Arm::InstructionTools + include Asm::InstructionTools def initialize(inst_class, byte_access, load_store) @cond = 0b1110 @@ -78,11 +77,10 @@ module Asm elsif (@addrtable_reloc_target.is_a?(Asm::NumLiteral)) # ref_label = closest_addrtable.add_const(@addrtable_reloc_target.value) end - as.add_relocation io.tell, ref_label, Asm::Arm::R_ARM_PC12, - Asm::Arm::Instruction::RelocHandler + as.add_relocation io.tell, ref_label, Asm::R_ARM_PC12, + Asm::Instruction::RelocHandler end io.write_uint32 val end end - end end \ No newline at end of file diff --git a/lib/asm/nodes.rb b/lib/asm/nodes.rb index 4f4c84c7..bc6c99fc 100644 --- a/lib/asm/nodes.rb +++ b/lib/asm/nodes.rb @@ -1,9 +1,5 @@ module Asm - class Instruction - attr_accessor :opcode, :args - end - class Shift attr_accessor :type, :value, :argument end diff --git a/lib/asm/arm/normal_builder.rb b/lib/asm/normal_builder.rb similarity index 98% rename from lib/asm/arm/normal_builder.rb rename to lib/asm/normal_builder.rb index 9bef2955..94f665fe 100644 --- a/lib/asm/arm/normal_builder.rb +++ b/lib/asm/normal_builder.rb @@ -1,9 +1,8 @@ module Asm - module Arm # ADDRESSING MODE 1 # Complete! class NormalBuilder - include Asm::Arm::InstructionTools + include Asm::InstructionTools def initialize(inst_class, opcode, s) @cond = 0b1110 @@ -102,6 +101,4 @@ module Asm io.write_uint32 val end end - - end end \ No newline at end of file diff --git a/lib/asm/arm/stack_builder.rb b/lib/asm/stack_builder.rb similarity index 91% rename from lib/asm/arm/stack_builder.rb rename to lib/asm/stack_builder.rb index ee409fa3..4056ef59 100644 --- a/lib/asm/arm/stack_builder.rb +++ b/lib/asm/stack_builder.rb @@ -1,12 +1,11 @@ module Asm - module Arm # ADDRESSING MODE 4 class StackBuilder - include Asm::Arm::InstructionTools + include Asm::InstructionTools def initialize(pre_post, up_down, write, store_load) @cond = 0b1110 - @inst_class = Asm::Arm::Instruction::OPC_STACK + @inst_class = Asm::Instruction::OPC_STACK @pre_post_index = 0 @up_down = 0 @s = 0 @@ -43,5 +42,4 @@ module Asm io.write_uint32 val end end - end end diff --git a/test/helper.rb b/test/helper.rb index 12e5a7c5..bf3b7e6c 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -30,4 +30,4 @@ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'test')) require 'crystal' require 'asm/object_writer' -require "asm/arm/arm_assembler" +require "asm/arm_assembler" diff --git a/test/small_program_test.rb b/test/small_program_test.rb index e9946c26..313b6bdf 100644 --- a/test/small_program_test.rb +++ b/test/small_program_test.rb @@ -9,7 +9,7 @@ require_relative 'helper' class TestSmallProg < MiniTest::Test # need a code generator, for arm def setup - @generator = Asm::Arm::ArmAssembler.new + @generator = Asm::ArmAssembler.new end def test_loop diff --git a/test/test_crystal.rb b/test/test_crystal.rb index 80093169..dc2fc45f 100644 --- a/test/test_crystal.rb +++ b/test/test_crystal.rb @@ -1,5 +1,5 @@ require_relative 'helper' -require "asm/arm/arm_assembler" +require "asm/arm_assembler" # try to test that the generation of basic instructions works # one instruction at a time, reverse testing from objdump --demangle -Sfghxp @@ -9,7 +9,7 @@ require "asm/arm/arm_assembler" class TestArmAsm < MiniTest::Test # need a code generator, for arm def setup - @assembler = Asm::Arm::ArmAssembler.new + @assembler = Asm::ArmAssembler.new end # code is what the generator spits out, at least one instruction worth (.first)