Class: Instruction::Encoding

Inherits:
Object
  • Object
show all
Defined in:
lib/arch_obj_models/instruction.rb

Overview

represents an instruction encoding

Defined Under Namespace

Classes: Field

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(format, decode_vars) ⇒ Encoding

Returns a new instance of Encoding.

Parameters:

  • format (String)

    Format of the encoding, as 0’s, 1’s and -‘s (for decode variables)

  • decode_vars (Array<Hash<String,Object>>)

    List of decode variable defintions from the arch spec



519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
# File 'lib/arch_obj_models/instruction.rb', line 519

def initialize(format, decode_vars)
  @format = format

  @opcode_fields = []
  field_chars = []
  @format.chars.each_with_index do |c, idx|
    if c == "-"
      next if field_chars.empty?

      field_text = field_chars.join("")
      field_lsb = @format.size - idx
      field_msb = @format.size - idx - 1 + field_text.size
      @opcode_fields << Field.new(field_text, field_lsb..field_msb)

      field_chars.clear
      next
    else
      field_chars << c
    end
  end

  # add the least significant field
  unless field_chars.empty?
    field_text = field_chars.join("")
    @opcode_fields << Field.new(field_text, 0...field_text.size)
  end

  @decode_variables = []
  decode_vars&.each do |var|
    @decode_variables << DecodeVariable.new(self, var)
  end
end

Instance Attribute Details

#decode_variablesArray<DecodeVariable> (readonly)

Returns List of decode variables.

Returns:



486
487
488
# File 'lib/arch_obj_models/instruction.rb', line 486

def decode_variables
  @decode_variables
end

#formatString (readonly)

Returns format, as a string of 0,1 and -,.

Examples:

Format of ‘sd`

sd.format #=> '-----------------011-----0100011'

Returns:

  • (String)

    format, as a string of 0,1 and -,



478
479
480
# File 'lib/arch_obj_models/instruction.rb', line 478

def format
  @format
end

#opcode_fieldsArray<Field> (readonly)

Returns List of fields containing opcodes.

Examples:

opcode_fields of ‘sd`

sd.opcode_fields #=> [Field('011', ...), Field('0100011', ...)]

Returns:

  • (Array<Field>)

    List of fields containing opcodes



483
484
485
# File 'lib/arch_obj_models/instruction.rb', line 483

def opcode_fields
  @opcode_fields
end

Instance Method Details

#sizeInteger

Returns Size, in bits, of the encoding.

Returns:

  • (Integer)

    Size, in bits, of the encoding



553
554
555
# File 'lib/arch_obj_models/instruction.rb', line 553

def size
  @format.size
end