Class: RubyVM::RJIT::CPointer::BitField

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_vm/rjit/c_pointer.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(addr, width, offset) ⇒ BitField

Returns a new instance of BitField.

Parameters:



342
343
344
345
346
# File 'lib/ruby_vm/rjit/c_pointer.rb', line 342

def initialize(addr, width, offset)
  @addr = addr
  @width = width
  @offset = offset
end

Class Method Details

.define(width, offset) ⇒ Object

Parameters:



364
365
366
367
368
369
370
# File 'lib/ruby_vm/rjit/c_pointer.rb', line 364

def self.define(width, offset)
  Class.new(self) do
    define_method(:initialize) do |addr|
      super(addr, width, offset)
    end
  end
end

Instance Method Details

#*Object

Dereference



349
350
351
352
353
354
355
356
357
358
359
360
# File 'lib/ruby_vm/rjit/c_pointer.rb', line 349

def *
  byte = Fiddle::Pointer.new(@addr)[0, Fiddle::SIZEOF_CHAR].unpack('c').first
  if @width == 1
    bit = (1 & (byte >> @offset))
    bit == 1
  elsif @width <= 8 && @offset == 0
    bitmask = @width.times.map { |i| 1 << i }.sum
    byte & bitmask
  else
    raise NotImplementedError.new("not-implemented bit field access: width=#{@width} offset=#{@offset}")
  end
end