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 |
# 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 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
184 185 186 187 |
# File 'lib/multiarray/gccfunction.rb', line 184 def <<( str ) @context << str self end |
#call(*args) ⇒ Object
Call the native method
162 163 164 |
# File 'lib/multiarray/gccfunction.rb', line 162 def call( *args ) @context.send @method_name, *args.collect { |v| v.get } end |
#compile ⇒ GCCFunction
Close the function and compile it
86 87 88 89 90 |
# File 'lib/multiarray/gccfunction.rb', line 86 def compile self << '}' @context.compile self end |
#id(prefix) ⇒ String
Create a new identifier unique to this function
99 100 101 102 |
# File 'lib/multiarray/gccfunction.rb', line 99 def id( prefix ) @ids += 1 "%s%02d"% [ prefix, @ids ] end |
#indent ⇒ String
Auxiliary method for code intendation
123 124 125 |
# File 'lib/multiarray/gccfunction.rb', line 123 def indent ' ' * @indent end |
#indent_offset(offset) ⇒ Integer
Increase/decrease amount of indentation
134 135 136 |
# File 'lib/multiarray/gccfunction.rb', line 134 def indent_offset( offset ) @indent += offset end |
#insn_return(value = nil) ⇒ GCCFunction
Add return instruction to native method
173 174 175 |
# File 'lib/multiarray/gccfunction.rb', line 173 def insn_return( value = nil ) self << "#{indent}return#{ value ? ' ' + value.get.to_s : '' };\n" end |
#params ⇒ Array<Node>
Retrieve all parameters
143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/multiarray/gccfunction.rb', line 143 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 |
#variable(typecode, prefix) ⇒ GCCValue
Create a new C variable of given type
112 113 114 115 116 |
# File 'lib/multiarray/gccfunction.rb', line 112 def variable( typecode, prefix ) retval = GCCValue.new( self, id( prefix ) ) self << "#{indent}#{GCCType.new( typecode ).identifier} #{retval};\n" retval end |