Class: Hornetseye::GCCFunction
Overview
Class representing a compiled function
Class Method Summary collapse
-
.compile(term, *keys) ⇒ String
Compile a block of Ruby code if not compiled already.
-
.run(block) ⇒ Object
Compile a block of Ruby code if not compiled already and run it.
Instance Method Summary collapse
-
#<<(str) ⇒ GCCFunction
Add instructions to C function.
-
#call(*args) ⇒ Object
Call the native method.
-
#compile ⇒ GCCFunction
Close the function and compile it.
-
#id(prefix) ⇒ String
Create a new identifier unique to this function.
-
#indent ⇒ String
Auxiliary method for code intendation.
-
#indent_offset(offset) ⇒ Integer
Increase/decrease amount of indentation.
-
#initialize(context, method_name, *param_types) ⇒ GCCFunction
constructor
Constructor.
-
#insn_return(value = nil) ⇒ GCCFunction
Add return instruction to native method.
-
#params ⇒ Array<Node>
Retrieve all parameters.
-
#variable(typecode, prefix) ⇒ GCCValue
Create a new C variable of given type.
Constructor Details
#initialize(context, method_name, *param_types) ⇒ GCCFunction
Constructor
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/multiarray/gccfunction.rb', line 72 def initialize( context, method_name, *param_types ) context.function method_name, *param_types.collect { |t| GCCType.new t } @context = context @method_name = method_name @param_types = param_types @indent = 1 @ids = 0 @c_decl = '' @c_body = '' end |
Class Method Details
.compile(term, *keys) ⇒ String
Compile a block of Ruby code if not compiled already
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/multiarray/gccfunction.rb', line 45 def compile(term, *keys) labels = Hash[*keys.zip((0 ... keys.size).to_a).flatten] method_name = ('_' + term.descriptor(labels)).method_name unless GCCCache.respond_to? method_name GCCContext.build do |context| function = GCCFunction.new context, method_name, *keys.collect { |var| var. } Thread.current[:function] = function term_subst = Hash[ *keys.zip(function.params).flatten ] Hornetseye::lazy { term.subst(term_subst).demand } Thread.current[:function] = nil function.insn_return function.compile end end method_name end |
Instance Method Details
#<<(str) ⇒ GCCFunction
Add instructions to C function
187 188 189 190 |
# File 'lib/multiarray/gccfunction.rb', line 187 def <<( str ) @c_body << str self end |
#call(*args) ⇒ Object
Call the native method
165 166 167 |
# File 'lib/multiarray/gccfunction.rb', line 165 def call( *args ) @context.send @method_name, *args.collect { |v| v.get } end |
#compile ⇒ GCCFunction
Close the function and compile it
88 89 90 91 92 93 |
# File 'lib/multiarray/gccfunction.rb', line 88 def compile @context << @c_decl << @c_body @context << '}' @context.compile self end |
#id(prefix) ⇒ String
Create a new identifier unique to this function
102 103 104 105 |
# File 'lib/multiarray/gccfunction.rb', line 102 def id( prefix ) @ids += 1 "%s%02d"% [ prefix, @ids ] end |
#indent ⇒ String
Auxiliary method for code intendation
126 127 128 |
# File 'lib/multiarray/gccfunction.rb', line 126 def indent ' ' * @indent end |
#indent_offset(offset) ⇒ Integer
Increase/decrease amount of indentation
137 138 139 |
# File 'lib/multiarray/gccfunction.rb', line 137 def indent_offset( offset ) @indent += offset end |
#insn_return(value = nil) ⇒ GCCFunction
Add return instruction to native method
176 177 178 |
# File 'lib/multiarray/gccfunction.rb', line 176 def insn_return( value = nil ) self << "#{indent}return#{ value ? ' ' + value.get.to_s : '' };\n" end |
#params ⇒ Array<Node>
Retrieve all parameters
146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/multiarray/gccfunction.rb', line 146 def params idx = 0 @param_types.collect do |param_type| args = GCCType.new( param_type ).identifiers.collect do arg = GCCValue.new self, "param#{idx}" idx += 1 arg end param_type.construct *args end end |