Class: Evoasm::Kernel

Inherits:
FFI::AutoPointer
  • Object
show all
Defined in:
lib/evoasm/kernel.rb,
lib/evoasm/kernel/io.rb

Overview

Represents a kernel comprising one ore multiple kernels

Defined Under Namespace

Classes: IO, Input, Output

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.wrap(ptr) ⇒ Object



12
13
14
# File 'lib/evoasm/kernel.rb', line 12

def self.wrap(ptr)
  new ptr
end

Instance Method Details

#disassemble(frame = false, format: false) ⇒ String+

Disassembles the whole kernel

Parameters:

  • frame (Bool) (defaults to: false)

    whether to include the stack frame and

  • format (Bool)

    whether to format the assembly

Returns:

  • (String, Array<String>)

    the formatted assembly as string if format is set, an array of address, opcode, operands triples otherwise.



164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/evoasm/kernel.rb', line 164

def disassemble(frame = false, format: false)
  code_ptr_ptr = FFI::MemoryPointer.new :pointer
  code_len = Libevoasm.kernel_get_code self, frame, code_ptr_ptr
  code_ptr = code_ptr_ptr.read_pointer
  code = code_ptr.read_string(code_len)

  disasm = X64.disassemble code, code_ptr.address

  if format
    format_disassembly disasm
  else
    disasm
  end
end

#disassemble_kernel(kernel_index) ⇒ String

Gives the disassembly for the specified kernel

Parameters:

  • kernel_index (Integer)

    index of kernel to disassemble

Returns:

  • (String)

    disassembly



97
98
99
100
101
102
103
104
# File 'lib/evoasm/kernel.rb', line 97

def disassemble_kernel(kernel_index)
  code_ptr_ptr = FFI::MemoryPointer.new :pointer
  code_len = Libevoasm.kernel_get_kernel_code self, kernel_index, code_ptr_ptr
  code_ptr = code_ptr_ptr.read_pointer
  code = code_ptr.read_string(code_len)

  X64.disassemble code, code_ptr.address
end

#disassemble_kernelsArray<String>

Gives the disassembly for all kernels in the kernel

Returns:

  • (Array<String>)

    array of disassembly



108
109
110
111
112
# File 'lib/evoasm/kernel.rb', line 108

def disassemble_kernels
  Array.new(size) do |kernel_index|
    disassemble_kernel kernel_index
  end
end

#eliminate_intronsKernel

Eliminates intron instructions (instructions without effect)

Returns:

  • (Kernel)

    a new kernel with introns eliminated



85
86
87
88
89
90
91
92
# File 'lib/evoasm/kernel.rb', line 85

def eliminate_introns
  kernel = Libevoasm.kernel_alloc
  unless Libevoasm.kernel_elim_introns self, kernel
    raise Error.last
  end

  self.class.wrap kernel
end

#input_arityInteger

Gives the input arity, i.e. the number of arguments

Returns:

  • (Integer)

    arity



31
32
33
# File 'lib/evoasm/kernel.rb', line 31

def input_arity
  Libevoasm.kernel_get_input_arity self
end

#input_mappingHash<Symbol, Integer>

Gives the mapping of input arguments to input registers of this kernel

Returns:

  • (Hash<Symbol, Integer>)

    input mapping



138
139
140
141
142
# File 'lib/evoasm/kernel.rb', line 138

def input_mapping
  io_registers(true).each_with_object({}) do |reg, mapping|
    mapping[reg] = Libevoasm.kernel_get_reg_input_mapping(self, reg)
  end
end

#input_registersArray<Symbol>

Gives the input registers of this kernel

Returns:

  • (Array<Symbol>)

    input registers



132
133
134
# File 'lib/evoasm/kernel.rb', line 132

def input_registers
  io_registers true
end

#input_typesArray<Symbol>

Gives the kernel's input type

Examples:

kernel.input_type #=> [:i64x1]

Returns:

  • (Array<Symbol>)

    the input type



45
46
47
48
49
# File 'lib/evoasm/kernel.rb', line 45

def input_types
  Array.new(self.input_arity) do |index|
    Libevoasm.kernel_get_input_type self, index
  end
end

#output_arityInteger

Gives the output arity, i.e. the number of return values

Returns:

  • (Integer)

    arity



37
38
39
# File 'lib/evoasm/kernel.rb', line 37

def output_arity
  Libevoasm.kernel_get_output_arity self
end

#output_registersArray<Symbol>

Gives the output registers of this kernel

Returns:

  • (Array<Symbol>)

    output registers



146
147
148
149
150
151
# File 'lib/evoasm/kernel.rb', line 146

def output_registers
  reg_enum_type = Libevoasm.enum_type(:x64_reg_id)
  Array.new(Libevoasm.kernel_get_arity self) do |index|
    reg_enum_type[Libevoasm.kernel_get_output_reg(self, index)]
  end
end

#output_typesArray<Symbol>

Gives the kernel's output type

Examples:

kernel.output_type #=> [:i64x1]

Returns:

  • (Array<Symbol>)

    the output type



55
56
57
58
59
# File 'lib/evoasm/kernel.rb', line 55

def output_types
  Array.new(self.output_arity) do |index|
    Libevoasm.kernel_get_output_type self, index
  end
end

#run(*input_tuple) ⇒ Array

Runs the kernel with the given input

Parameters:

  • input_tuple (Array)

    an input tuple

Returns:

  • (Array)

    the output tuple corresponding to the given input



25
26
27
# File 'lib/evoasm/kernel.rb', line 25

def run(*input_tuple)
  run_all(input_tuple).first
end

#run_all(*input_examples) ⇒ Array

Like #run, but runs multiple input tuples at once

Parameters:

  • input_examples (Array)

    an array of input tuples

Returns:

  • (Array)

    an array of output tuples



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/evoasm/kernel.rb', line 64

def run_all(*input_examples)
  input = Kernel::Input.new(input_examples, self.input_types)

  output_ptr = Libevoasm.kernel_io_alloc
  success = Libevoasm.kernel_run self, input, output_ptr

  unless success
    raise Error.last
  end

  Kernel::Output.new(output_ptr, self.output_types).to_a
end

#sizeInteger

Gives the size of the kernel as the number of instructions

Returns:

  • (Integer)

    size



79
80
81
# File 'lib/evoasm/kernel.rb', line 79

def size
  Libevoasm.kernel_get_size self
end