<commit_before>require 'simple_asm/inst_factory'
require 'simple_asm/inst_arithmetic'
require 'simple_asm/inst_load_store'
require 'simple_asm/inst_li_branch'
require 'simple_asm/inst_zero'
require 'simple_asm/inst_jmp'

module SimpleAsm
  class Simple
    class << self
      REGISTER_SIZE = 8

      def define(name, symbols)
        define_method name do |*args|
          factory_args = Hash[*symbols.zip(args).flatten(1)]
          add_inst(InstFactory.create(name, factory_args))
        end
      end

      def use(inst_class)
        InstFactory.register(inst_class)

        inst_class.args_to_names_map.each do |args, names|
          names.each do |name|
            self.define(name, args)
          end
        end
      end

      def define_registers
        (0..REGISTER_SIZE-1).each do |i|
          name = ('r' + i.to_s).to_sym
          define_method name do
            return i
          end
        end
      end

      def define_label
        define_method(:label) do |name|
          @labels[name] = @insts.length
        end
      end

      def define_exts
        define_registers
        define_label
        self.define(:zero, [])
        InstFactory.register(InstZero)
      end


      def define_function(name, &block)
        define_method(name, &block)
      end
    end

    use InstArithmetic
    use InstLoadStore
    use InstLiBranch
    use InstJmp

    define_exts

    def initialize(&block)
      @insts = []
      @labels = {}
      self.instance_eval(&block) if block
    end

    def to_s
      preassemble
      @insts.map{|inst| inst.to_s }.join("\n")
    end

    def to_a
      preassemble
      @insts
    end

    def to_mif(depth=2048)
      preassemble
      mif_text = <<-MIF
WIDTH=16;
DEPTH=#{depth};

ADDRESS_RADIX=HEX;
DATA_RADIX=DEC;

CONTENT BEGIN
      MIF

      @insts.each_with_index do |inst, index|

        mif_text << "\t#{'%03x' % index}  :   #{binary_to_decimal(inst.to_s)};\n"
      end

      mif_text << "\t[#{'%03x' % @insts.length}..#{'%03x' % depth}]  :   0;\n"

      mif_text << "END;\n"
    end

    private
    def add_inst(inst)
      @insts << inst
    end

    def preassemble<commit_msg>2/16進数形式で出力できるように<commit_after>require 'simple_asm/inst_factory'
require 'simple_asm/inst_arithmetic'
require 'simple_asm/inst_load_store'
require 'simple_asm/inst_li_branch'
require 'simple_asm/inst_zero'
require 'simple_asm/inst_jmp'

module SimpleAsm
  class Simple
    class << self
      REGISTER_SIZE = 8

      def define(name, symbols)
        define_method name do |*args|
          factory_args = Hash[*symbols.zip(args).flatten(1)]
          add_inst(InstFactory.create(name, factory_args))
        end
      end

      def use(inst_class)
        InstFactory.register(inst_class)

        inst_class.args_to_names_map.each do |args, names|
          names.each do |name|
            self.define(name, args)
          end
        end
      end

      def define_registers
        (0..REGISTER_SIZE-1).each do |i|
          name = ('r' + i.to_s).to_sym
          define_method name do
            return i
          end
        end
      end

      def define_label
        define_method(:label) do |name|
          @labels[name] = @insts.length
        end
      end

      def define_exts
        define_registers
        define_label
        self.define(:zero, [])
        InstFactory.register(InstZero)
      end


      def define_function(name, &block)
        define_method(name, &block)
      end
    end

    use InstArithmetic
    use InstLoadStore
    use InstLiBranch
    use InstJmp

    define_exts

    def initialize(&block)
      @insts = []
      @labels = {}
      self.instance_eval(&block) if block
    end

    def to_s
      preassemble
      @insts.map{|inst| inst.to_s }.join("\n")
    end

    def to_a
      preassemble
      @insts
    end

    def to_mif(depth=2048)
      preassemble
      mif_text = <<-MIF
WIDTH=16;
DEPTH=#{depth};

ADDRESS_RADIX=HEX;
DATA_RADIX=DEC;

CONTENT BEGIN
      MIF

      @insts.each_with_index do |inst, index|

        mif_text << "\t#{'%03x' % index}  :   #{binary_to_decimal(inst.to_s)};\n"
      end

      mif_text << "\t[#{'%03x' % @insts.length}..#{'%03x' % depth}]  :   0;\n"

      mif_text << "END;\n"
    end

    def to_binary_mif(depth=2048)
      preassemble
      mif_text = <<-MIF
WIDTH=16;
DEPTH=#{depth};

ADDRESS_RADIX=HEX;
DATA_RADIX=BIN;

CONTENT BEGIN
      MIF

      @insts.each_with_index do |inst, index|

        mif_text << "\t#{'%03x' % index}  :   #{inst.to_s};\n"
      end

      mif_text << "\t[#{'%03x' % @insts.length}..#{'%03x' % depth}]  :   0;\n"

      mif_text << "END;\n"
    end


    def to_hex_mif(depth=2048)
      preassemble
      mif_text = <<-MIF
WIDTH=16;
DEPTH=#{depth};

ADDRESS_RADIX=HEX;
DATA_RADIX=HEX;

CONTENT BEGIN
      MIF

      @insts.each_with_index do |inst, index|

        mif_text << "\t#{'%03x' % index}  :   #{inst.to_s.to_i(2).to_s(16)};\n"
      end

      mif_text << "\t[#{'%03x' % @insts.length}..#{'%03x' % depth}]  :   0;\n"

      mif_text << "END;\n"
    end


    private
    def add_inst(inst)
      @insts << inst
    end

    def preassemble
{'fraction_non_alphanumeric': 0.1159504452187379, 'fraction_numerical': 0.012775842044134728, 'mean_word_length': 2.0977218225419665, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4356021', 'n_tokens_mistral': 1946, 'n_tokens_neox': 1849, 'n_words': 363}