Module: LLVM

Defined in:
lib/llvm/config.rb,
lib/llvm.rb,
lib/llvm/core.rb,
lib/llvm/lljit.rb,
lib/llvm/linker.rb,
lib/llvm/target.rb,
lib/llvm/support.rb,
lib/llvm/version.rb,
lib/llvm/analysis.rb,
lib/llvm/core/type.rb,
lib/llvm/core/value.rb,
lib/llvm/core/module.rb,
lib/llvm/core_ffi_v2.rb,
lib/llvm/core/bitcode.rb,
lib/llvm/core/builder.rb,
lib/llvm/core/context.rb,
lib/llvm/pass_builder.rb,
lib/llvm/core/attribute.rb,
lib/llvm/transforms/ipo.rb,
lib/llvm/execution_engine.rb,
lib/llvm/transforms/utils.rb,
lib/llvm/core/pass_manager.rb,
lib/llvm/transforms/scalar.rb,
lib/llvm/transforms/vectorize.rb,
lib/llvm/transforms/pass_manager_builder.rb

Overview

typed: strict

Defined Under Namespace

Modules: C, CONFIG, PointerIdentity, Support Classes: Alloca, Argument, Attribute, BasicBlock, Builder, CallInst, Constant, ConstantArray, ConstantExpr, ConstantInt, ConstantNull, ConstantReal, ConstantStruct, ConstantVector, Context, DeprecationError, ExecutionEngine, Function, FunctionPassManager, FunctionType, GenericValue, GlobalAlias, GlobalValue, GlobalVariable, IndirectBr, Instruction, IntType, InvokeInst, LLJit, MCJITCompiler, MemoryBuffer, Module, PassBuilder, PassManager, PassManagerBuilder, Phi, Poison, RealType, StructType, SwitchInst, Target, TargetDataLayout, TargetMachine, Type, User, Value

Constant Summary collapse

LLVM_VERSION =
"21"
LLVM_REQUIRED_VERSION =
"21.1.0"
RUBY_LLVM_VERSION =
"21.1.0"
Float =

for compatibility

LLVM::Type.float.freeze
Double =

: LLVM::RealType

LLVM::Type.double.freeze
Int1 =

: LLVM::IntType

const_get(:Int1)
Int8 =

: LLVM::IntType

const_get(:Int8)
Int16 =

: LLVM::IntType

const_get(:Int16)
Int32 =

: LLVM::IntType

const_get(:Int32)
Int64 =

: LLVM::IntType

const_get(:Int64)
Int =
const_get(:"Int#{bits}")
TRUE =

Boolean values

::LLVM::Int1.from_i(-1)
FALSE =
::LLVM::Int1.from_i(0)
JITCompiler =
MCJITCompiler

Class Method Summary collapse

Class Method Details

.Array(ty, sz = 0) ⇒ Object

Shortcut to Type.array. : (LLVM::Type, ?Integer) -> LLVM::Type



415
416
417
# File 'lib/llvm/core/type.rb', line 415

def Array(ty, sz = 0)
  LLVM::Type.array(ty, sz)
end

.const_missing(const) ⇒ Object

creates LLVM::Int1, LLVM::Int64, etc



685
686
687
688
689
690
691
692
693
694
695
# File 'lib/llvm/core/value.rb', line 685

def self.const_missing(const)
  case const.to_s
  when /Int(\d+)/
    width = Regexp.last_match(1).to_i
    name = "Int#{width}"
    value = LLVM::Type.integer(width).freeze
    const_set(name, value)
  else
    super
  end
end

.double(value = nil) ⇒ Object

: (?Numeric?) -> (ConstantReal | RealType)



470
471
472
473
# File 'lib/llvm/core/type.rb', line 470

def double(value = nil)
  type = LLVM::Type.double
  value ? type.from_f(value.to_f) : type
end

.Double(value) ⇒ Object

for compatibility Create a double LLVM::ContantReal from a Ruby Float (value). This will always return ConstantReal because value will always be Numeric : (Numeric) -> ConstantReal



498
499
500
# File 'lib/llvm/core/type.rb', line 498

def Double(value)
  double(value) #: as ConstantReal
end

.float(value = nil) ⇒ Object

: (?Numeric?) -> (ConstantReal | RealType)



476
477
478
479
# File 'lib/llvm/core/type.rb', line 476

def float(value = nil)
  type = LLVM::Type.float
  value ? type.from_f(value.to_f) : type
end

.Float(value) ⇒ Object

for compatibility Create a float LLVM::ContantReal from a Ruby Float (value). This will always return ConstantReal because value will always be Numeric : (Numeric) -> ConstantReal



490
491
492
# File 'lib/llvm/core/type.rb', line 490

def Float(value)
  float(value) #: as ConstantReal
end

.Function(argtypes, rettype, options = {}) ⇒ Object

Shortcut to Type.function. : ([Type] | [], Type, ?Hash[untyped, untyped]) -> FunctionType



433
434
435
# File 'lib/llvm/core/type.rb', line 433

def Function(argtypes, rettype, options = {})
  LLVM::Type.function(argtypes, rettype, options)
end

.i(width, value = nil) ⇒ Object

: (Integer, ?(Numeric | String)?) -> (ConstantInt | IntType)



464
465
466
467
# File 'lib/llvm/core/type.rb', line 464

def i(width, value = nil)
  type = LLVM::Type.i(width)
  value ? type.from_i(value) : type
end

.init_jit(*args) ⇒ Object

A shorthand for LLVM::Target.init_native



10
11
12
13
14
# File 'lib/llvm/target.rb', line 10

def self.init_jit(*args)
  LLVM::Target.init_native(
    *args #: as untyped
  )
end

.init_x86Object

Deprecated.

Use LLVM.init_jit or LLVM::Target.init(‘X86’).



17
18
19
20
# File 'lib/llvm/target.rb', line 17

def self.init_x86
  warn "LLVM.init_x86 is deprecated. Use LLVM.init_jit or LLVM::Target.init('X86')."
  LLVM::Target.init('X86')
end

.Int(val) ⇒ Object

Creates a LLVM Int (subclass of ConstantInt) at the NATIVE_INT_SIZE from a integer (val).



708
709
710
711
712
713
714
715
716
717
# File 'lib/llvm/core/value.rb', line 708

def self.Int(val)
  case val
  when LLVM::ConstantInt then val
  when Integer then Int.from_i(val)
  when Value
    return val if val.type.kind == :integer
    raise "value not of integer type: #{val.type.kind}"
  else raise "can't make an LLVM::ConstantInt from #{val.class.name}"
  end
end

.load_library(libname) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/llvm/support.rb', line 36

def self.load_library(libname)
  if C.load_library_permanently(libname) != 0
    raise "LLVM::Support.load_library failed"
  end

  nil
end

.make_generic_value(ty, val) ⇒ Object

: (LLVM::Type, untyped) -> GenericValue



340
341
342
343
344
345
346
347
348
349
# File 'lib/llvm/execution_engine.rb', line 340

def make_generic_value(ty, val)
  case ty.kind
  when :double  then GenericValue.from_d(val)
  when :float   then GenericValue.from_f(val)
  when :pointer then GenericValue.from_value_ptr(val)
  when :integer then GenericValue.from_i(val, :type => ty)
  else
    raise ArgumentError, "Unsupported type #{ty.kind}."
  end
end

.Pointer(ty = nil) ⇒ Object

Shortcut to Type.pointer. : (?Type?) -> Type`



421
422
423
# File 'lib/llvm/core/type.rb', line 421

def Pointer(ty = nil)
  LLVM::Type.pointer(ty)
end

.ptrObject

: -> Type



482
483
484
# File 'lib/llvm/core/type.rb', line 482

def ptr
  LLVM::Type.pointer
end

.Struct(*elt_types) ⇒ Object

Shortcut to Type.struct. : (*untyped) -> LLVM::Type



439
440
441
442
443
444
445
446
447
448
449
450
# File 'lib/llvm/core/type.rb', line 439

def Struct(*elt_types)
  name = if elt_types.last.is_a? String
    elt_types.pop
  else
    nil
  end
  LLVM::Type.struct(
    elt_types, #: as [LLVM::Type]
    false,
    name
  )
end

.Type(ty) ⇒ Object

Creates a Type from the given object. : (LLVM::Type | LLVM::Value) -> LLVM::Type



404
405
406
407
408
409
410
411
# File 'lib/llvm/core/type.rb', line 404

def Type(ty)
  case ty
  when LLVM::Type
    ty
  else
    ty.type
  end
end

.Vector(ty, sz) ⇒ Object

Shortcut to Type.vector. : (LLVM::Type, Integer) -> LLVM::Type



427
428
429
# File 'lib/llvm/core/type.rb', line 427

def Vector(ty, sz)
  LLVM::Type.vector(ty, sz)
end

.voidObject

: -> Type



459
460
461
# File 'lib/llvm/core/type.rb', line 459

def void
  LLVM::Type.void
end

.VoidObject

Shortcut to Type.void. : -> Type



454
455
456
# File 'lib/llvm/core/type.rb', line 454

def Void
  LLVM::Type.void
end

.with_error_output {|FFI::MemoryPointer| ... } ⇒ nil

Same as #with_message_output, but raises a RuntimeError with the resulting message.

: { (FFI::MemoryPointer) -> Integer } -> String?

Yields:

  • (FFI::MemoryPointer)

Returns:

  • (nil)


42
43
44
45
46
# File 'lib/llvm/core.rb', line 42

def self.with_error_output(&blk)
  error = with_message_output(&blk)

  raise error unless error.nil?
end

.with_message_output {|FFI::MemoryPointer| ... } ⇒ String?

Yields a pointer suitable for storing an LLVM output message. If the message pointer is non-NULL (an error has happened), converts the result to a string and returns it. Otherwise, returns nil.

: { (FFI::MemoryPointer) -> Integer } -> String?

Yields:

  • (FFI::MemoryPointer)

Returns:

  • (String, nil)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/llvm/core.rb', line 17

def self.with_message_output(&)
  message = nil #: String?

  FFI::MemoryPointer.new(FFI.type_size(:pointer)) do |str|
    result = yield str

    msg_ptr = str.read_pointer

    if result != 0
      raise "Error is signalled, but msg_ptr is null" if msg_ptr.null?

      message = msg_ptr.read_string
      C.dispose_message msg_ptr
    end
  end

  message
end