Class: RLTK::CG::ExecutionEngine Abstract

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

Overview

This class is abstract.

Implemented by Interpreter and JITCompiler.

The ExecutionEngine class and its subclasses execute code from the provided module, as well as providing a PassManager and FunctionPassManager for optimizing modules.

Direct Known Subclasses

Interpreter, JITCompiler

Instance Attribute Summary collapse

Attributes included from BindingClass

#ptr

Instance Method Summary collapse

Methods included from BindingClass

#==

Methods included from AbstractClass

included

Constructor Details

#initialize(mod, &block) ⇒ ExecutionEngine

Create a new execution engine.

Parameters:

  • mod (Module)

    Module to be executed.

  • block (Proc)

    Block used by subclass constructors. Don’t use this parameter.

Raises:

  • (RuntimeError)

    An error is raised if something went horribly wrong inside LLVM during the creation of this engine.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rltk/cg/execution_engine.rb', line 41

def initialize(mod, &block)
	check_type(mod, Module, 'mod')
	
	block = Proc.new { |ptr, error| Bindings.create_execution_engine_for_module(ptr, mod, error) } if block == nil
	
	ptr		= FFI::MemoryPointer.new(:pointer)
	error	= FFI::MemoryPointer.new(:pointer)
	status	= block.call(ptr, error)
	
	if status.zero?
		@ptr		= ptr.read_pointer
		@module	= mod
		
		# Associate this engine with the provided module.
		@module.engine = self

	else
		errorp  = error.read_pointer
		message = errorp.null? ? 'Unknown' : errorp.read_string

		error.autorelease = false

		Bindings.dispose_message(error)

		raise "Error creating execution engine: #{message}"
	end
end

Instance Attribute Details

#moduleModule (readonly)

Returns:



33
34
35
# File 'lib/rltk/cg/execution_engine.rb', line 33

def module
  @module
end

Instance Method Details

#disposevoid

This method returns an undefined value.

Frees the resources used by LLVM for this execution engine..



72
73
74
75
76
77
78
# File 'lib/rltk/cg/execution_engine.rb', line 72

def dispose
	if @ptr
		Bindings.dispose_execution_engine(@ptr)
		
		@ptr = nil
	end
end

#pointer_to_global(global) ⇒ FFI::Pointer

Builds a pointer to a global value.

Parameters:

  • global (GlobalValue)

    Value you want a pointer to.

Returns:

  • (FFI::Pointer)


85
86
87
# File 'lib/rltk/cg/execution_engine.rb', line 85

def pointer_to_global(global)
	Bindings.get_pointer_to_global(@ptr, global)
end

#run_function(fun, *args) ⇒ GenericValue Also known as: run

Execute a function in the engine’s module with the given arguments. The arguments may be either GnericValue objects or any object that can be turned into a GenericValue.

Parameters:

  • fun (Function)

    Function object to be executed.

  • args (Array<GenericValue, Object>)

    Arguments to be passed to the function.

Returns:



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/rltk/cg/execution_engine.rb', line 97

def run_function(fun, *args)
	new_values = Array.new
	
	new_args =
	fun.params.zip(args).map do |param, arg|
		if arg.is_a?(GenericValue)
			arg
			
		else
			returning(GenericValue.new(arg)) { |val| new_values << val }
		end
	end
	
	args_ptr = FFI::MemoryPointer.new(:pointer, args.length)
	args_ptr.write_array_of_pointer(new_args)
	
	returning(GenericValue.new(Bindings.run_function(@ptr, fun, args.length, args_ptr))) do
		new_values.each { |val| val.dispose }
	end
end

#run_function_as_main(fun, *args) ⇒ GenericValue Also known as: run_main

Execute a function in the engine’s module with the given arguments as the main function of a program.

Parameters:

  • fun (Function)

    Function object to be executed.

  • args (Array<String>)

    Arguments to be passed to the function.

Returns:



126
127
128
129
130
131
132
133
134
135
136
# File 'lib/rltk/cg/execution_engine.rb', line 126

def run_function_as_main(fun, *args)
	# Prepare the ARGV parameter.
	argv = FFI::MemoryPointer.new(:pointer, argc)
	argv.write_array_of_pointer(args.map { |str| FFI::MemoryPointer.from_string(str) })
	
	# Prepare the ENV parameter.
	env = FFI::MemoryPointer.new(:pointer, ENV.size)
	env.write_array_of_pointer(ENV.to_a.map { |pair| FFI::MemoryPointer.from_string(pair[0] + '=' + pair[1]) })
	
	GenericValue.new(Bindings.run_function_as_main(@ptr, fun, args.length, argv, env))
end

#target_dataTargetData

Returns Information about the target architecture for this execution engine.

Returns:

  • (TargetData)

    Information about the target architecture for this execution engine.



140
141
142
# File 'lib/rltk/cg/execution_engine.rb', line 140

def target_data
	TargetData.new(Bindings.get_execution_engine_target_data(@ptr))
end