Class: Evoasm::X64::Operand

Inherits:
FFI::Pointer show all
Defined in:
lib/evoasm/x64/operand.rb

Overview

Represents a formal instruction operand

Constant Summary

INVALID_IMMEDIATE =
-1

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from FFI::Pointer

#to_ptr

Instance Attribute Details

#instructionX64::Instruction (readonly)

Returns the instruction this operand belongs to

Returns:



9
10
11
# File 'lib/evoasm/x64/operand.rb', line 9

def instruction
  @instruction
end

Instance Method Details

#explicit?Boolean

Returns whether this operand is explicit

Returns:

  • (Boolean)


62
63
64
# File 'lib/evoasm/x64/operand.rb', line 62

def explicit?
  !implicit?
end

#immediateInteger?

Returns the operand's immediate (e.g. if implicit) if available

Returns:

  • (Integer, nil)

    the operand's immediate or nil if there is none



100
101
102
103
104
105
106
107
# File 'lib/evoasm/x64/operand.rb', line 100

def immediate
  if type == :imm
    imm = Libevoasm.x64_operand_get_imm self
    imm == INVALID_IMMEDIATE ? nil : imm
  else
    nil
  end
end

#implicit?Boolean

Returns whether this operand is implicit

Returns:

  • (Boolean)


44
45
46
# File 'lib/evoasm/x64/operand.rb', line 44

def implicit?
  Libevoasm.x64_operand_is_implicit self
end

#indexObject



153
154
155
156
157
# File 'lib/evoasm/x64/operand.rb', line 153

def index
  index = Libevoasm.x64_inst_get_operand_idx @instruction, self
  return nil if index == -1
  index
end

#index_register_sizeInteger?

Returns the operand's index register size (e.g. in VSIB instructions)

Returns:

  • (Integer, nil)

    the operand's index registert size or nil if this operand does not hold a index register



171
172
173
174
175
176
177
# File 'lib/evoasm/x64/operand.rb', line 171

def index_register_size
  if type == :vsib
    convert_size Libevoasm.x64_operand_get_index_reg_size(self)
  else
    nil
  end
end

#inspect(parameters = nil) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/evoasm/x64/operand.rb', line 48

def inspect(parameters = nil)
  info = case type
         when :reg, :rm
           "#{register_type}:#{register parameters}"
         when :imm
           "imm:#{immediate}"
         when :mem
           'mem'
         end

  "#<#{self.class.inspect} #{index} #{info}>"
end

#maybe_written?Boolean

Returns whether this operand is possibliy written (e.g. in a conditional move instruction)

Returns:

  • (Boolean)


34
35
36
# File 'lib/evoasm/x64/operand.rb', line 34

def maybe_written?
  Libevoasm.x64_operand_is_maybe_written self
end

#memory_sizeInteger?

Returns the operand's memory size (e.g. in R/M instructions)

Returns:

  • (Integer, nil)

    the operand's memory size or nil if this operand is not a memory operand



181
182
183
184
185
186
187
# File 'lib/evoasm/x64/operand.rb', line 181

def memory_size
  if type == :rm || type == :mem || type == :vsib
    convert_size Libevoasm.x64_operand_get_mem_size(self)
  else
    nil
  end
end

#mnemonic?Boolean

Returns whether this operand is part of the instruction mnemonic

Returns:

  • (Boolean)


39
40
41
# File 'lib/evoasm/x64/operand.rb', line 39

def mnemonic?
  Libevoasm.x64_operand_is_mnem self
end

#parameterX64::Parameter

Gives the parameter affecting this operand

Returns:

  • (X64::Parameter)

    the parameter



19
20
21
# File 'lib/evoasm/x64/operand.rb', line 19

def parameter
  @instruction.parameters[Libevoasm.x64_operand_get_param_idx self]
end

#read?Boolean

Returns whether this operand is read

Returns:

  • (Boolean)


24
25
26
# File 'lib/evoasm/x64/operand.rb', line 24

def read?
  Libevoasm.x64_operand_is_read self
end

#register(parameters = nil) ⇒ Symbol?

Returns the operand's register (e.g. if implicit) if available

Returns:

  • (Symbol, nil)

    the operand's register or nil if there is none



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/evoasm/x64/operand.rb', line 74

def register(parameters = nil)
  if type == :rm || type == :reg
    reg_id = Libevoasm.x64_operand_get_reg_id self
    if reg_id == :none
      if parameters
        parameter_name = parameter&.name
        if parameter_name
          parameters[parameter_name]
        else
          nil
        end
      else
        nil
      end
    else
      reg_id
    end
  else
    nil
  end
end

#register_sizeInteger?

Returns the operand's register size

Returns:

  • (Integer, nil)

    the operand's registert size or nil if this operand does not hold a register



161
162
163
164
165
166
167
# File 'lib/evoasm/x64/operand.rb', line 161

def register_size
  if type == :rm || type == :reg
    convert_size Libevoasm.x64_operand_get_reg_size(self)
  else
    nil
  end
end

#register_typeSymbol?

Returns the operand's register type (e.g. if implicit) if available

Returns:

  • (Symbol, nil)

    the operand's register type or nil if there is none



111
112
113
114
115
116
117
118
# File 'lib/evoasm/x64/operand.rb', line 111

def register_type
  if type == :rm || type == :reg
    reg_type = Libevoasm.x64_operand_get_reg_type self
    reg_type == :none ? nil : reg_type
  else
    nil
  end
end

#sizeInteger

Gives the operand's size

Returns:

  • (Integer)

    the operand size in bits



122
123
124
# File 'lib/evoasm/x64/operand.rb', line 122

def size
  convert_size Libevoasm.x64_operand_get_size(self)
end

#type:rm, ...

Returns the operand type

Returns:

  • (:rm, :imm, :reg)

    the operand type



68
69
70
# File 'lib/evoasm/x64/operand.rb', line 68

def type
  Libevoasm.x64_operand_get_type self
end

#word(mode, parameters = nil, mask: false) ⇒ Symbol

Gives the operand's word

Parameters:

  • mode (:read, :write)

    the access mode

  • parameters (Parameters) (defaults to: nil)

    parameters

Returns:

  • (Symbol)

    the operand word



130
131
132
133
134
135
136
137
138
139
# File 'lib/evoasm/x64/operand.rb', line 130

def word(mode, parameters = nil, mask: false)
  word = Libevoasm.x64_operand_get_word(self, @instruction, parameters, mode == :read)
  if word && mask
    mask = Bitmap.new 512
    Libevoasm.x64_operand_word_get_mask(word, mask)
    mask
  else
    word
  end
end

#word_mask(mode, parameters = nil) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
# File 'lib/evoasm/x64/operand.rb', line 141

def word_mask(mode, parameters = nil)
  word = Libevoasm.x64_operand_get_word(self, @instruction, parameters, mode == :read)
  if word && word != :none
    #mask = Bitmap.new 512
    mask = FFI::MemoryPointer.new :uint64, 8
    Libevoasm.x64_operand_word_get_mask(word, mask)
    mask.read_array_of_uint64 8
  else
    nil
  end
end

#written?Boolean

Returns whether this operand is written

Returns:

  • (Boolean)


29
30
31
# File 'lib/evoasm/x64/operand.rb', line 29

def written?
  Libevoasm.x64_operand_is_written self
end