Class: LLVM::Target

Inherits:
Object
  • Object
show all
Includes:
PointerIdentity
Defined in:
lib/llvm/target.rb

Overview

You need to call Target.init for a target to be usable.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PointerIdentity

#==, #eql?, #hash, #to_ptr

Class Method Details

.by_name(name) ⇒ Target

Fetch a target by its name.

Returns:



95
96
97
98
99
# File 'lib/llvm/target.rb', line 95

def self.by_name(name)
  each do |target|
    return target if target.name == name
  end
end

.each {|Target| ... } ⇒ Object

Enumerate all initialized targets.

Yields:



81
82
83
84
85
86
87
88
89
90
# File 'lib/llvm/target.rb', line 81

def self.each(&block)
  return to_enum(:each) if block.nil?

  target = C.get_first_target
  until target.null?
    yield from_ptr(target)

    target = C.get_next_target(target)
  end
end

.from_ptr(ptr) ⇒ Object



104
105
106
107
108
# File 'lib/llvm/target.rb', line 104

def self.from_ptr(ptr)
  target = allocate
  target.instance_variable_set :@ptr, ptr
  target
end

.init(target, asm_printer = false) ⇒ Object

Initializes target target; in particular, TargetInfo, Target and TargetMC.

Parameters:

  • target (String)

    Target name in LLVM format, e.g. “X86”, “ARM” or “PowerPC”.

  • asm_printer (true, false) (defaults to: false)

    Initialize corresponding AsmPrinter.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/llvm/target.rb', line 23

def self.init(target, asm_printer = false)
  C.module_eval do
    attach_function :"initialize_target_info_#{target}",
        :"LLVMInitialize#{target}TargetInfo", [], :void
    attach_function :"initialize_target_#{target}",
        :"LLVMInitialize#{target}Target", [], :void
    attach_function :"initialize_target_#{target}_mc",
        :"LLVMInitialize#{target}TargetMC", [], :void

    attach_function :"initialize_#{target}_asm_printer",
        :"LLVMInitialize#{target}AsmPrinter", [], :void
    attach_function :"initialize_#{target}_asm_parser",
        :"LLVMInitialize#{target}AsmParser", [], :void
    attach_function :"initialize_#{target}_disassembler",
        :"LLVMInitialize#{target}Disassembler", [], :void
  end

  begin
    %W(initialize_target_info_#{target}
       initialize_target_#{target}
       initialize_target_#{target}_mc).each do |init|
      C.send init
    end
  rescue FFI::NotFoundError
    raise ArgumentError, "LLVM target #{target} is not linked in. Try `llvm-config-#{LLVM_VERSION} --targets-built'."
  end

  begin
    C.send :"initialize_#{target}_asm_printer" if asm_printer
  rescue FFI::NotFoundError => e
    raise ArgumentError, "LLVM target #{target} does not implement an ASM routime: #{e.message}"
  end
end

.init_all(asm_printer = false) ⇒ Object

Initializes all available targets.

Parameters:

  • asm_printer (true, false) (defaults to: false)

    Initialize corresponding AsmPrinters.



60
61
62
63
64
65
66
# File 'lib/llvm/target.rb', line 60

def self.init_all(asm_printer = false)
  Support::C.initialize_all_target_infos
  Support::C.initialize_all_targets
  Support::C.initialize_all_target_mcs

  Support::C.initialize_all_asm_printers if asm_printer
end

.init_native(asm_printer = true) ⇒ Object

Initializes native target. Useful for JIT applications.

Parameters:

  • asm_printer (true, false) (defaults to: true)

    Initialize corresponding AsmPrinter. True by default, as this is required for MCJIT to function.



72
73
74
75
76
# File 'lib/llvm/target.rb', line 72

def self.init_native(asm_printer = true)
  Support::C.initialize_native_target

  Support::C.initialize_native_asm_printer if asm_printer
end

Instance Method Details

#asm_backend?Boolean

Returns if the target has an ASM backend (required for emitting output).

Returns:

  • (Boolean)


135
136
137
# File 'lib/llvm/target.rb', line 135

def asm_backend?
  !C.target_has_asm_backend(self).zero?
end

#create_machine(triple, cpu = "", features = "", opt_level = :default, reloc = :default, code_model = :default) ⇒ TargetMachine

Constructs a TargetMachine.

Parameters:

  • triple (String)

    Target triple

  • cpu (String) (defaults to: "")

    Target CPU

  • features (String) (defaults to: "")

    Target feature string

  • opt_level (Symbol) (defaults to: :default)

    :none, :less, :default, :aggressive

  • reloc (Symbol) (defaults to: :default)

    :default, :static, :pic, :dynamic_no_pic

  • code_model (Symbol) (defaults to: :default)

    :default, :jit_default, :small, :kernel, :medium, :large

Returns:



148
149
150
151
152
# File 'lib/llvm/target.rb', line 148

def create_machine(triple, cpu = "", features = "",
                   opt_level = :default, reloc = :default, code_model = :default)
  TargetMachine.from_ptr(C.create_target_machine(self,
        triple, cpu, features, opt_level, reloc, code_model))
end

#descriptionString

Returns the description of the target.

Returns:

  • (String)


120
121
122
# File 'lib/llvm/target.rb', line 120

def description
  C.get_target_description(self)
end

#jit?Boolean

Returns if the target has a JIT.

Returns:

  • (Boolean)


125
126
127
# File 'lib/llvm/target.rb', line 125

def jit?
  !C.target_has_jit(self).zero?
end

#nameString

Returns the name of the target.

Returns:

  • (String)


113
114
115
# File 'lib/llvm/target.rb', line 113

def name
  C.get_target_name(self)
end

#target_machine?Boolean

Returns if the target has a TargetMachine associated.

Returns:

  • (Boolean)


130
131
132
# File 'lib/llvm/target.rb', line 130

def target_machine?
  !C.target_has_target_machine(self).zero?
end