Class: Hornetseye::GCCFunction

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of GCCFunction.



52
53
54
55
56
57
58
59
# File 'lib/multiarray/gccfunction.rb', line 52

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

.run(block) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/multiarray/gccfunction.rb', line 24

def run( block )
  keys, values, term = block.strip
  labels = Hash[ *keys.zip( ( 0 ... keys.size ).to_a ).flatten ]
  method_name = ( '_' + term.descriptor( labels ) ).
                      tr( '(),+\-*/%.@?~&|^<=>',
                          '0123\456789ABCDEFGH' )
  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 = ( 0 ... keys.size ).collect do |i|
        { keys[i] => function.param( i ) }
      end.inject( {} ) { |a,b| a.merge b }
      Hornetseye::lazy do
        term.subst( term_subst ).demand
      end
      Thread.current[ :function ] = nil
      function.insn_return
      function.compile
    end
  end
  args = values.collect { |arg| arg.values }.flatten
  GCCCache.send method_name, *args
end

Instance Method Details

#<<(str) ⇒ Object



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

def <<( str )
  @context << str
  self
end

#call(*args) ⇒ Object



95
96
97
# File 'lib/multiarray/gccfunction.rb', line 95

def call( *args )
  @context.send @method_name, *args.collect { |v| v.get }
end

#compileObject



61
62
63
64
65
# File 'lib/multiarray/gccfunction.rb', line 61

def compile
  self << '}'
  @context.compile
  self
end

#id(prefix) ⇒ Object



67
68
69
70
# File 'lib/multiarray/gccfunction.rb', line 67

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

#indentObject



78
79
80
# File 'lib/multiarray/gccfunction.rb', line 78

def indent
  '  ' * @indent
end

#indent_offset(offset) ⇒ Object



82
83
84
# File 'lib/multiarray/gccfunction.rb', line 82

def indent_offset( offset )
  @indent += offset
end

#insn_return(value = nil) ⇒ Object



99
100
101
# File 'lib/multiarray/gccfunction.rb', line 99

def insn_return( value = nil )
  self << "#{indent}return#{ value ? ' ' + value.get.to_s : '' };\n"
end

#param(i) ⇒ Object



86
87
88
89
90
91
92
93
# File 'lib/multiarray/gccfunction.rb', line 86

def param( i )
  offset = ( 0 ... i ).inject( 0 ) do |s,idx|
    s + GCCType.new( @param_types[ idx ] ).identifiers.size
  end
  args = ( 0 ... GCCType.new( @param_types[ i ] ).identifiers.size ).
    collect { |idx| GCCValue.new self, "param#{ offset + idx }" }
  @param_types[ i ].construct *args
end

#variable(typecode, prefix) ⇒ Object



72
73
74
75
76
# File 'lib/multiarray/gccfunction.rb', line 72

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