From 285a88b59f60cc8a57165adfb6acc2a97a7c48ce Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Mon, 30 Jul 2018 10:23:42 +0300 Subject: [PATCH] generalize assemblers to use callables not just methods, they are almost the same anyway --- lib/elf/object_writer.rb | 2 +- lib/risc/assembler.rb | 8 ++++---- lib/risc/linker.rb | 14 +++++++------- lib/risc/text_writer.rb | 2 +- test/mom/helper.rb | 2 +- test/risc/test_linker1.rb | 2 +- test/rubyx/test_rubyx_compiler2.rb | 2 +- 7 files changed, 16 insertions(+), 16 deletions(-) diff --git a/lib/elf/object_writer.rb b/lib/elf/object_writer.rb index 75b9ba4a..a8e86c74 100644 --- a/lib/elf/object_writer.rb +++ b/lib/elf/object_writer.rb @@ -24,7 +24,7 @@ module Elf # for debug add labels for labels @linker.assemblers.each do |asm| - meth = asm.method + meth = asm.callable asm.instructions.each do |label| next unless label.is_a?(Risc::Label) add_symbol "#{meth.self_type.name}@#{meth.name}:Label=#{label.name}" , Risc::Position.get(label).at diff --git a/lib/risc/assembler.rb b/lib/risc/assembler.rb index 3841437e..094fb2bf 100644 --- a/lib/risc/assembler.rb +++ b/lib/risc/assembler.rb @@ -1,12 +1,12 @@ module Risc class Assembler - attr_reader :method , :instructions + attr_reader :callable , :instructions - def initialize( method , instructions) - @method = method + def initialize( callable , instructions) + @callable = callable @instructions = instructions total = instructions.total_byte_length / 4 + 1 - method.binary.extend_to( total ) + callable.binary.extend_to( total ) end end end diff --git a/lib/risc/linker.rb b/lib/risc/linker.rb index bdf5e743..05791a3e 100644 --- a/lib/risc/linker.rb +++ b/lib/risc/linker.rb @@ -82,13 +82,13 @@ module Risc # start at code_start. def position_code(code_start) assemblers.each do |asm| - Position.log.debug "Method start #{code_start.to_s(16)} #{asm.method.name}" - code_pos = CodeListener.init(asm.method.binary, platform) + Position.log.debug "Method start #{code_start.to_s(16)} #{asm.callable.name}" + code_pos = CodeListener.init(asm.callable.binary, platform) instructions = asm.instructions - InstructionListener.init( instructions, asm.method.binary) + InstructionListener.init( instructions, asm.callable.binary) code_pos.position_listener( LabelListener.new(instructions)) code_pos.set(code_start) - code_start = Position.get(asm.method.binary.last_code).next_slot + code_start = Position.get(asm.callable.binary.last_code).next_slot end end @@ -109,7 +109,7 @@ module Risc def assemble assemblers.each do |asm| - writer = BinaryWriter.new(asm.method.binary) + writer = BinaryWriter.new(asm.callable.binary) writer.assemble(asm.instructions) end end @@ -119,8 +119,8 @@ module Risc # risc_init is a branch to the __init__ method # def cpu_init_init - init = assemblers.find {|asm| asm.method.name == :__init__} - risc_init = Branch.new( "__initial_branch__" , init.method.binary ) + init = assemblers.find {|asm| asm.callable.name == :__init__} + risc_init = Branch.new( "__initial_branch__" , init.callable.binary ) @platform.translator.translate(risc_init) end diff --git a/lib/risc/text_writer.rb b/lib/risc/text_writer.rb index f0492f1f..653ebd92 100644 --- a/lib/risc/text_writer.rb +++ b/lib/risc/text_writer.rb @@ -60,7 +60,7 @@ module Risc # Really like any other object, it's just about the ordering def write_code @linker.assemblers.each do |asm| - asm.method.each_binary do |code| + asm.callable.each_binary do |code| write_any(code) end end diff --git a/test/mom/helper.rb b/test/mom/helper.rb index efec686c..cbd0a38b 100644 --- a/test/mom/helper.rb +++ b/test/mom/helper.rb @@ -30,7 +30,7 @@ module Risc def produce_instructions assert @expect , "No output given" linker = RubyX::RubyXCompiler.new(as_test_main).ruby_to_risc(:interpreter) - compiler = linker.assemblers.find{|c| c.method.name == :main and c.method.self_type.object_class.name == :Test} + compiler = linker.assemblers.find{|c| c.callable.name == :main and c.callable.self_type.object_class.name == :Test} compiler.instructions end def check_nil diff --git a/test/risc/test_linker1.rb b/test/risc/test_linker1.rb index 0f9fae6d..18e4b5b4 100644 --- a/test/risc/test_linker1.rb +++ b/test/risc/test_linker1.rb @@ -14,7 +14,7 @@ module Risc end end def test_one_main - mains = @linker.assemblers.find_all{|asm| asm.method.name == :main } + mains = @linker.assemblers.find_all{|asm| asm.callable.name == :main } assert_equal 1 , mains.length end def test_assembler_num diff --git a/test/rubyx/test_rubyx_compiler2.rb b/test/rubyx/test_rubyx_compiler2.rb index 50cfe56d..373d8fa1 100644 --- a/test/rubyx/test_rubyx_compiler2.rb +++ b/test/rubyx/test_rubyx_compiler2.rb @@ -14,7 +14,7 @@ module RubyX assert_equal Risc::Linker , @linker.class end def test_method - assert_equal :main , @linker.assemblers.first.method.name + assert_equal :main , @linker.assemblers.first.callable.name end end end