Class: Hornetseye::GCCFunction

Inherits:
Object
  • Object
show all
Defined in:
lib/multiarray/gccfunction.rb

Overview

Class representing a compiled function

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, method_name, *param_types) ⇒ GCCFunction

Constructor

Parameters:

  • context (GCContext)

    Context object for compiling function.

  • method_name (String)

    Unique method name.

  • param_types (Array<Class>)

    Native types of parameters.



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

Parameters:

  • term (Node)

    Stripped expression to compile.

  • keys (Array<Variable>)

    Variables for performing substitutions on term.

Returns:

  • (String)

    Unique method name of compiled function.



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.meta }
      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

.run(block) ⇒ Object

Compile a block of Ruby code if not compiled already and run it

Parameters:

  • block (Node)

    Expression to compile and run.

Returns:

  • (Object)

    Result returned by the compiled function.



32
33
34
35
# File 'lib/multiarray/gccfunction.rb', line 32

def run( block )
  keys, values, term = block.strip
  GCCCache.send compile(term, *keys), *values.collect { |arg| arg.values }.flatten
end

Instance Method Details

#<<(str) ⇒ GCCFunction

Add instructions to C function

Parameters:

  • str (String)

    C code fragment.

Returns:



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

Parameters:

  • args (Array<Node>)

    Arguments of method call.

Returns:

  • The return value of 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

#compileGCCFunction

Close the function and compile it

Returns:



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

Parameters:

  • prefix (String)

    Prefix for constructing the identifier.

Returns:

  • (String)

    A new identifier.



102
103
104
105
# File 'lib/multiarray/gccfunction.rb', line 102

def id( prefix )
  @ids += 1
  "%s%02d"% [ prefix, @ids ]
end

#indentString

Auxiliary method for code intendation

Returns:

  • (String)

    String to use for indentation.



126
127
128
# File 'lib/multiarray/gccfunction.rb', line 126

def indent
  '  ' * @indent
end

#indent_offset(offset) ⇒ Integer

Increase/decrease amount of indentation

Parameters:

  • offset (Integer)

    Offset to add to current amount of indentation.

Returns:

  • (Integer)

    Resulting 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

Parameters:

  • value (Object, NilClass) (defaults to: nil)

    Return value or nil.

Returns:



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

#paramsArray<Node>

Retrieve all parameters

Returns:

  • (Array<Node>)

    Objects for handling the 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

#variable(typecode, prefix) ⇒ GCCValue

Create a new C variable of given type

Parameters:

  • typecode (Class)

    Native type of variable.

  • prefix (String)

    Prefix for creating variable name.

Returns:

  • (GCCValue)

    GCC value object refering to the C variable.



115
116
117
118
119
# File 'lib/multiarray/gccfunction.rb', line 115

def variable( typecode, prefix )
  retval = GCCValue.new( self, id( prefix ) )
  @c_decl << "  #{GCCType.new( typecode ).identifier} #{retval};\n"
  retval
end