Class: RLTK::CG::TargetMachine

Inherits:
Object
  • Object
show all
Includes:
BindingClass
Defined in:
lib/rltk/cg/target.rb

Overview

This class represents a specific architecture that wil be targeted by LLVM’s compilation process.

Constant Summary collapse

CLASS_FINALIZER =

The Proc object called by the garbage collector to free resources used by LLVM.

Proc.new { |id| Bindings.dispose_target_machine(ptr) if ptr = ObjectSpace._id2ref(id).ptr }

Instance Attribute Summary

Attributes included from BindingClass

#ptr

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BindingClass

#==

Constructor Details

#initialize(target, mcpu = '', features = '', opt_level = :none, reloc_mode = :default, code_model = :default) ⇒ TargetMachine

Create a new object describing a target machine.

Parameters:

  • target (Target)

    Target description

  • mcpu (String) (defaults to: '')

    Specific CPU type to target

  • features (Array<String>, String) (defaults to: '')

    Features present for this target machine

  • opt_level (Symbol from _enum_code_gen_opt_level_) (defaults to: :none)

    Optimization level

  • reloc_mode (Symbol from _enum_reloc_mode_) (defaults to: :default)

    Code relocation model

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

    Code generation model

See Also:



152
153
154
155
156
157
158
159
160
161
# File 'lib/rltk/cg/target.rb', line 152

def initialize(target, mcpu = '', features = '', opt_level = :none, reloc_mode = :default, code_model = :default)
	# Convert the features parameter if necessary.
	features = TargetMachine.build_feature_string(features) if features.is_a?(Array)
	
	@ptr = Bindings.create_target_machine(target, target.triple.to_s, mcpu, features, opt_level, reloc_mode, code_model)
	
	# Define a finalizer to free the memory used by LLVM for
	# this target machine.
	ObjectSpace.define_finalizer(self, CLASS_FINALIZER)
end

Class Method Details

.hostTargetMachine

Returns TargetMachine representation of the host machine.

Returns:

  • (TargetMachine)

    TargetMachine representation of the host machine.



137
138
139
# File 'lib/rltk/cg/target.rb', line 137

def self.host
	@host ||= self.new(Target.host)
end

Instance Method Details

#cpuString

Returns Name of the target machine’s CPU.

Returns:

  • (String)

    Name of the target machine’s CPU



164
165
166
# File 'lib/rltk/cg/target.rb', line 164

def cpu
	Bindings.get_target_machine_cpu(@ptr)
end

#dataTargetData

Returns:



169
170
171
# File 'lib/rltk/cg/target.rb', line 169

def data
	TargetData.new(Bindings.get_target_machine_data(@pt))
end

#emit_module(mod, file_name, emit_type) ⇒ void

This method returns an undefined value.

Emit assembly or object code for the given module to the file specified.

Parameters:

  • mod (Module)

    Module to emit code for

  • file_name (String)

    File to emit code to

  • emit_type (:assembly, :object)

    Type of code to emit

Raises:

  • LLVM error message if unable to emite code for module



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/rltk/cg/target.rb', line 183

def emit_module(mod, file_name, emit_type)
	error  = FFI::MemoryPointer.new(:pointer)
	status = Bindings.target_machine_emit_to_file(@ptr, mod, file_name, emit_type, error)
	
	if not status.zero?
		errorp  = error.read_pointer
		message = errorp.null? ? 'Unknown' : errorp.read_string
	
		error.autorelease = false
	
		Bindings.dispose_message(error)
	
		raise "Error emiting code for module: #{message}"
	end
end

#feature_stringString

Returns Feature string for this target machine.

Returns:

  • (String)

    Feature string for this target machine



200
201
202
# File 'lib/rltk/cg/target.rb', line 200

def feature_string
	Bindings.get_target_machine_feature_string(@ptr)
end

#targetTarget

Returns:



205
206
207
# File 'lib/rltk/cg/target.rb', line 205

def target
	Target.new(Bindings.get_target_machine_target(@ptr))
end

#tripleTriple

Returns:



210
211
212
# File 'lib/rltk/cg/target.rb', line 210

def triple
	Triple.new(Bindings.get_target_machine_triple(@ptr))
end

#verbose_asm=(bool) ⇒ void

This method returns an undefined value.

Set verbose ASM property.

Parameters:

  • bool (Boolean)

    Verbose ASM or not



219
220
221
222
223
# File 'lib/rltk/cg/target.rb', line 219

def verbose_asm=(bool)
	@verbose_asm = bool
	
	Bindings.set_target_machine_asm_verbosity(@ptr, bool.to_i)
end

#verbose_asm?Boolean

Returns If this target machine should print verbose ASM.

Returns:

  • (Boolean)

    If this target machine should print verbose ASM



226
227
228
# File 'lib/rltk/cg/target.rb', line 226

def verbose_asm?
	@verbose_asm ||= false
end