Class: FastRuby::Context

Inherits:
Object show all
Defined in:
lib/fastruby/translator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeContext



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/fastruby/translator.rb', line 36

def initialize
  @infer_lvar_map = Hash.new
  @extra_code = ""
  @options = {}

  extra_code << '#include "node.h"
  '

  extra_code << "static VALUE _rb_gvar_set(void* ge,VALUE value) {
    rb_gvar_set((struct global_entry*)ge,value);
    return value;
  }
  "

  extra_code << "static VALUE _rb_ivar_set(VALUE recv,ID idvar, VALUE value) {
    rb_ivar_set(recv,idvar,value);
    return value;
  }
  "

  extra_code << "static VALUE _lvar_assing(VALUE* destination,VALUE value) {
    *destination = value;
    return value;
  }
  "

end

Instance Attribute Details

#alt_method_nameObject

Returns the value of attribute alt_method_name.



29
30
31
# File 'lib/fastruby/translator.rb', line 29

def alt_method_name
  @alt_method_name
end

#extra_codeObject (readonly)

Returns the value of attribute extra_code.



33
34
35
# File 'lib/fastruby/translator.rb', line 33

def extra_code
  @extra_code
end

#infer_lvar_mapObject

Returns the value of attribute infer_lvar_map.



28
29
30
# File 'lib/fastruby/translator.rb', line 28

def infer_lvar_map
  @infer_lvar_map
end

#infer_selfObject

Returns the value of attribute infer_self.



32
33
34
# File 'lib/fastruby/translator.rb', line 32

def infer_self
  @infer_self
end

#localsObject

Returns the value of attribute locals.



30
31
32
# File 'lib/fastruby/translator.rb', line 30

def locals
  @locals
end

#optionsObject

Returns the value of attribute options.



31
32
33
# File 'lib/fastruby/translator.rb', line 31

def options
  @options
end

#yield_signatureObject (readonly)

Returns the value of attribute yield_signature.



34
35
36
# File 'lib/fastruby/translator.rb', line 34

def yield_signature
  @yield_signature
end

Instance Method Details

#anonymous_function(method) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/fastruby/translator.rb', line 73

def anonymous_function(method)

  name = "anonymous" + rand(10000000).to_s
  extra_code << method.call(name)

  name
end

#directive(tree) ⇒ Object



820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
# File 'lib/fastruby/translator.rb', line 820

def directive(tree)
  recv = tree[1]
  mname = tree[2]
  args = tree[3]

  if mname == :infer
    return to_c(recv)
  elsif mname == :lvar_type
    lvar_name = args[1][1] || args[1][2]
    lvar_type = eval(args[2][1].to_s)

    @infer_lvar_map[lvar_name] = lvar_type
    return ""
  elsif mname == :block_given?
    return "#{locals_accessor}block_function_address == 0 ? Qfalse : Qtrue"
  elsif mname == :inline_c
    code = args[1][1]

    caller_code = proc { |name| "
       static VALUE #{name}(VALUE param) {
        #{@locals_struct} *plocals = (void*)param;
        #{code};
        return Qnil;
      }
     "
    }

    return anonymous_function(caller_code)+"((VALUE)plocals)"
  else
    nil
  end
end

#infer_type(recv) ⇒ Object



794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
# File 'lib/fastruby/translator.rb', line 794

def infer_type(recv)
  if recv[0] == :call
    if recv[2] == :infer
      eval(recv[3].last.last.to_s)
    end
  elsif recv[0] == :lvar
    @infer_lvar_map[recv[1]]
  elsif recv[0] == :self
    @infer_self
  elsif recv[0] == :str or recv[0] == :lit
    recv[1].class
  else
    nil
  end
end

#locals_accessorObject



525
526
527
# File 'lib/fastruby/translator.rb', line 525

def locals_accessor
  "plocals->"
end

#locals_pointerObject



529
530
531
# File 'lib/fastruby/translator.rb', line 529

def locals_pointer
  "plocals"
end

#on_blockObject



64
65
66
# File 'lib/fastruby/translator.rb', line 64

def on_block
  yield
end

#to_c(tree) ⇒ Object



68
69
70
71
# File 'lib/fastruby/translator.rb', line 68

def to_c(tree)
  return "Qnil" unless tree
  send("to_c_" + tree[0].to_s, tree);
end

#to_c_and(tree) ⇒ Object



588
589
590
# File 'lib/fastruby/translator.rb', line 588

def to_c_and(tree)
  "(RTEST(#{to_c tree[1]}) && RTEST(#{to_c tree[2]})) ? Qtrue : Qfalse"
end

#to_c_array(tree) ⇒ Object



455
456
457
458
459
460
461
462
# File 'lib/fastruby/translator.rb', line 455

def to_c_array(tree)
  if tree.size > 1
    strargs = tree[1..-1].map{|subtree| to_c subtree}.join(",")
    "rb_ary_new3(#{tree.size-1}, #{strargs})"
  else
    "rb_ary_new3(0)"
  end
end

#to_c_attrasgn(tree) ⇒ Object



85
86
87
# File 'lib/fastruby/translator.rb', line 85

def to_c_attrasgn(tree)
  to_c_call(tree)
end

#to_c_block(tree) ⇒ Object



390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# File 'lib/fastruby/translator.rb', line 390

def to_c_block(tree)

  str = ""
  str = tree[1..-2].map{ |subtree|
    to_c(subtree)
  }.join(";")

  if tree[-1][0] != :return
    str = str + ";last_expression = #{to_c(tree[-1])};"
  else
    str = str + ";#{to_c(tree[-1])};"
  end

  caller_code = proc { |name| "
    static VALUE #{name}(VALUE param) {
      #{@locals_struct} *plocals = (void*)param;
      VALUE last_expression;

      #{str}

      return last_expression;
      }
    "
  }

  anonymous_function(caller_code) + "((VALUE)#{locals_pointer})"
end

#to_c_call(tree) ⇒ Object



631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
# File 'lib/fastruby/translator.rb', line 631

def to_c_call(tree)
  recv = tree[1]
  mname = tree[2]
  args = tree[3]

  directive_code = directive(tree)
  if directive_code
    return directive_code
  end

  strargs = args[1..-1].map{|arg| to_c arg}.join(",")

  argnum = args.size - 1

  recv = recv || s(:self)

  recvtype = infer_type(recv)

  if recvtype

    address = nil
    mobject = nil

    inference_complete = true
    signature = [recvtype]

    args[1..-1].each do |arg|
      argtype = infer_type(arg)
      if argtype
        signature << argtype
      else
        inference_complete = false
      end
    end

    convention = nil

    if recvtype.respond_to? :method_tree and inference_complete

      if recvtype.method_tree[tree[2]]
        mobject = recvtype.build(signature, tree[2])
        convention = :fastruby
      else
        mobject = recvtype.instance_method(tree[2])
        convention = :cruby
      end
    else
      mobject = recvtype.instance_method(tree[2])
      convention = :cruby
    end

    address = getaddress(mobject)
    len = getlen(mobject)

    extraargs = ""
    extraargs = ", Qfalse" if convention == :fastruby

    if address then
      if argnum == 0
        value_cast = "VALUE"
        value_cast = value_cast + ", VALUE" if convention == :fastruby

        if convention == :fastruby
          "((VALUE(*)(#{value_cast}))0x#{address.to_s(16)})(#{to_c recv}, Qfalse)"
        else

          str_incall_args = nil
          if len == -1
            str_incall_args = "0, (VALUE[]){}, recv"
            value_cast = "int,VALUE*,VALUE"
          elsif len == -2
            str_incall_args = "recv, rb_ary_new4(#{})"
            value_cast = "VALUE,VALUE"
          else
            str_incall_args = "recv"
          end

          wrapper_func = proc { |name| "
            static VALUE #{name}(VALUE recv) {
              // call to #{recvtype}##{mname}
              if (rb_block_given_p()) {
                // no passing block, recall
                return rb_funcall(recv, #{tree[2].to_i}, 0);
              } else {
                return ((VALUE(*)(#{value_cast}))0x#{address.to_s(16)})(#{str_incall_args});
              }
            }
          " }

          anonymous_function(wrapper_func) + "(#{to_c(recv)})"

        end
      else
        value_cast = ( ["VALUE"]*(args.size) ).join(",")
        value_cast = value_cast + ", VALUE" if convention == :fastruby

        wrapper_func = nil
        if convention == :fastruby
          "((VALUE(*)(#{value_cast}))0x#{address.to_s(16)})(#{to_c recv}, Qfalse, #{strargs})"
        else

          str_incall_args = nil
          if len == -1
            str_incall_args = "#{argnum}, (VALUE[]){#{ (1..argnum).map{|x| "_arg"+x.to_s }.join(",")}}, recv"
            value_cast = "int,VALUE*,VALUE"
          elsif len == -2
            str_incall_args = "recv, rb_ary_new4(#{ (1..argnum).map{|x| "_arg"+x.to_s }.join(",")})"
            value_cast = "VALUE,VALUE"
          else
            str_incall_args = "recv, #{ (1..argnum).map{|x| "_arg"+x.to_s }.join(",")}"
          end

          wrapper_func = proc { |name| "
            static VALUE #{name}(VALUE recv, #{ (1..argnum).map{|x| "VALUE _arg"+x.to_s }.join(",")} ) {
              // call to #{recvtype}##{mname}
              if (rb_block_given_p()) {
                // no passing block, recall
                return rb_funcall(recv, #{tree[2].to_i}, #{argnum}, #{ (1..argnum).map{|x| "_arg"+x.to_s }.join(",")});
              } else {
                return ((VALUE(*)(#{value_cast}))0x#{address.to_s(16)})(#{str_incall_args});
              }
            }
          " }

          anonymous_function(wrapper_func) + "(#{to_c(recv)}, #{strargs})"
        end
      end
    else

      if argnum == 0
        "rb_funcall(#{to_c recv}, #{tree[2].to_i}, 0)"
      else
        "rb_funcall(#{to_c recv}, #{tree[2].to_i}, #{argnum}, #{strargs} )"
      end
    end

  else
    if argnum == 0
      "rb_funcall(#{to_c recv}, #{tree[2].to_i}, 0)"
    else
      "rb_funcall(#{to_c recv}, #{tree[2].to_i}, #{argnum}, #{strargs} )"
    end
  end
end

#to_c_defn(tree) ⇒ Object



464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
# File 'lib/fastruby/translator.rb', line 464

def to_c_defn(tree)
  method_name = tree[1]
  args_tree = tree[2]

  impl_tree = tree[3][1]

  @locals_struct = "struct {
    #{@locals.map{|l| "VALUE #{l};\n"}.join}
    #{args_tree[1..-1].map{|arg| "VALUE #{arg};\n"}.join};
    void* block_function_address;
    VALUE block_function_param;
    }"

  @block_struct = "struct {
    void* block_function_address;
    void* block_function_param;
  }"

  str_impl = ""
  # if impl_tree is a block, implement the last node with a return
  if impl_tree[0] == :block
    str_impl = to_c impl_tree
  else
    if impl_tree[0] != :return
      str_impl = str_impl + ";last_expression = #{to_c(impl_tree)};"
    else
      str_impl = str_impl + ";#{to_c(impl_tree)};"
    end
  end

  strargs = if args_tree.size > 1
    "VALUE block, #{args_tree[1..-1].map{|arg| "VALUE #{arg}" }.join(",") }"
  else
    "VALUE block"
  end

  "VALUE #{@alt_method_name || method_name}(#{strargs}) {
    #{@locals_struct} locals;
    #{@locals_struct} *plocals = (void*)&locals;
    #{@block_struct} *pblock;
    VALUE last_expression;

    #{args_tree[1..-1].map { |arg|
      "locals.#{arg} = #{arg};\n"
    }.join("") }

    locals.self = self;

    pblock = (void*)block;
    if (pblock) {
      locals.block_function_address = pblock->block_function_address;
      locals.block_function_param = (VALUE)pblock->block_function_param;
    } else {
      locals.block_function_address = 0;
      locals.block_function_param = Qnil;
    }

    return #{str_impl};
  }"
end

#to_c_dot2(tree) ⇒ Object



81
82
83
# File 'lib/fastruby/translator.rb', line 81

def to_c_dot2(tree)
  "rb_range_new(#{to_c tree[1]}, #{to_c tree[2]},0)"
end

#to_c_false(tree) ⇒ Object



580
581
582
# File 'lib/fastruby/translator.rb', line 580

def to_c_false(tree)
  "Qfalse"
end

#to_c_gasgn(tree) ⇒ Object



537
538
539
# File 'lib/fastruby/translator.rb', line 537

def to_c_gasgn(tree)
  "_rb_gvar_set((void*)0x#{global_entry(tree[1]).to_s(16)}, #{to_c tree[2]})"
end

#to_c_gvar(tree) ⇒ Object



533
534
535
# File 'lib/fastruby/translator.rb', line 533

def to_c_gvar(tree)
  "rb_gvar_get((struct global_entry*)0x#{global_entry(tree[1]).to_s(16)})"
end

#to_c_hash(tree) ⇒ Object



434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
# File 'lib/fastruby/translator.rb', line 434

def to_c_hash(tree)

  hash_aset_code = ""
  (0..(tree.size-3)/2).each do |i|
    strkey = to_c tree[1 + i * 2]
    strvalue = to_c tree[2 + i * 2]
    hash_aset_code << "rb_hash_aset(hash, #{strkey}, #{strvalue});"
  end

  wrapper_func = proc { |name| "
    static VALUE #{name}(VALUE value_params) {
      #{@locals_struct} *plocals = (void*)value_params;
      VALUE hash = rb_hash_new();
      #{hash_aset_code}
      return hash;
    }
  " }

  anonymous_function(wrapper_func) + "((VALUE)#{locals_pointer})"
end

#to_c_iasgn(tree) ⇒ Object



545
546
547
# File 'lib/fastruby/translator.rb', line 545

def to_c_iasgn(tree)
  "_rb_ivar_set(#{locals_accessor}self,#{tree[1].to_i},#{to_c tree[2]})"
end

#to_c_if(tree) ⇒ Object



600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
# File 'lib/fastruby/translator.rb', line 600

def to_c_if(tree)
  condition_tree = tree[1]
  impl_tree = tree[2]
  else_tree = tree[3]
  code = "if (RTEST(#{to_c condition_tree})) {
    last_expression = #{to_c impl_tree};
  }
  "

  if (else_tree)
    code = code + " else {
      last_expression = #{to_c else_tree};
    }
    "
  end

  caller_code = proc { |name| "
    static VALUE #{name}(VALUE param) {
      #{@locals_struct} *plocals = (void*)param;
      VALUE last_expression = Qnil;

      #{code};

      return last_expression;
      }
    "
  }

  anonymous_function(caller_code) + "((VALUE)#{locals_pointer})"
end

#to_c_iter(tree) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/fastruby/translator.rb', line 89

def to_c_iter(tree)

  call_tree = tree[1]
  args_tree = tree[2]
  recv_tree = call_tree[1]

  directive_code = directive(call_tree)
  if directive_code
    return directive_code
  end

  other_call_tree = call_tree.dup
  other_call_tree[1] = s(:lvar, :arg)

  call_args_tree = call_tree[3]

  caller_code = nil

  str_lvar_initialization = @locals_struct + " *plocals;
                            plocals = (void*)param;"

  recvtype = infer_type(recv_tree || s(:self))

  address = nil
  mobject = nil
  len = nil

  convention = :ruby

  extra_inference = {}

  if recvtype

    inference_complete = true
    signature = [recvtype]

    call_args_tree[1..-1].each do |arg|
      argtype = infer_type(arg)
      if argtype
        signature << argtype
      else
        inference_complete = false
      end
    end

    convention = nil

    if recvtype.respond_to? :method_tree and inference_complete

      if recvtype.method_tree[call_tree[2]]
        mobject = recvtype.build(signature, call_tree[2])
        yield_signature = mobject.yield_signature

        if not args_tree
        elsif args_tree.first == :lasgn
          if yield_signature[0]
          extra_inference[args_tree.last] = yield_signature[0]
          end
        elsif args_tree.first == :masgn
          yield_args = args_tree[1][1..-1].map(&:last)
          (0...yield_signature.size-1).each do |i|
            extra_inference[yield_args[i]] = yield_signature[i]
          end
        end

        convention = :fastruby
      else
        mobject = recvtype.instance_method(call_tree[2])
        convention = :cruby
      end
    else
      mobject = recvtype.instance_method(call_tree[2])
      convention = :cruby
    end

    address = getaddress(mobject)
    len = getlen(mobject)

    unless address
      convention = :ruby
    end

  end

  anonymous_impl = tree[3]

  str_lvar_initialization = @locals_struct + " *plocals;
                              plocals = (void*)param;"

  str_arg_initialization = ""

  str_impl = ""

  with_extra_inference(extra_inference) do
      # if impl_tree is a block, implement the last node with a return
      if anonymous_impl
        if anonymous_impl[0] == :block
          str_impl = anonymous_impl[1..-2].map{ |subtree|
            to_c(subtree)
          }.join(";")

          if anonymous_impl[-1][0] != :return
            str_impl = str_impl + ";last_expression = (#{to_c(anonymous_impl[-1])});"
          else
            str_impl = str_impl + ";#{to_c(anonymous_impl[-1])};"
          end
        else
          if anonymous_impl[0] != :return
            str_impl = str_impl + ";last_expression = (#{to_c(anonymous_impl)});"
          else
            str_impl = str_impl + ";#{to_c(anonymous_impl)};"
          end
        end
      else
        str_impl = "last_expression = Qnil;"
      end

  end

  if convention == :ruby or convention == :cruby

    if call_args_tree.size > 1

      str_called_code_args = call_args_tree[1..-1].map{ |subtree| to_c subtree }.join(",")
      str_recv = to_c recv_tree

      str_recv = "plocals->self" unless recv_tree

        caller_code = proc { |name| "
          static VALUE #{name}(VALUE param) {
            // call to #{call_tree[2]}

            #{str_lvar_initialization}
            return rb_funcall(#{str_recv}, #{call_tree[2].to_i}, #{call_args_tree.size-1}, #{str_called_code_args});
          }
        "
        }

    else
      str_recv = to_c recv_tree
      str_recv = "plocals->self" unless recv_tree

        caller_code = proc { |name| "
          static VALUE #{name}(VALUE param) {
            // call to #{call_tree[2]}
            #{str_lvar_initialization}
            return rb_funcall(#{str_recv}, #{call_tree[2].to_i}, 0);
          }
        "
        }
    end

    if not args_tree
      str_arg_initialization = ""
    elsif args_tree.first == :lasgn
      str_arg_initialization = "plocals->#{args_tree[1]} = arg;"
    elsif args_tree.first == :masgn
      arguments = args_tree[1][1..-1].map(&:last)

      (0..arguments.size-1).each do |i|
        str_arg_initialization << "plocals->#{arguments[i]} = rb_ary_entry(arg,#{i});\n"
      end
    end

    str_arg_initialization

    block_code = proc { |name| "
      static VALUE #{name}(VALUE arg, VALUE param) {
        // block for call to #{call_tree[2]}
        VALUE last_expression = Qnil;

        #{str_lvar_initialization};
        #{str_arg_initialization}
        #{str_impl}

        return last_expression;
      }
    "
    }

    "rb_iterate(#{anonymous_function(caller_code)}, (VALUE)#{locals_pointer}, #{anonymous_function(block_code)}, (VALUE)#{locals_pointer})"
  elsif convention == :fastruby

    str_arg_initialization = ""

    if not args_tree
      str_arg_initialization = ""
    elsif args_tree.first == :lasgn
      str_arg_initialization = "plocals->#{args_tree[1]} = argv[0];"
    elsif args_tree.first == :masgn
      arguments = args_tree[1][1..-1].map(&:last)

      (0..arguments.size-1).each do |i|
        str_arg_initialization << "plocals->#{arguments[i]} = #{i} < argc ? argv[#{i}] : Qnil;\n"
      end
    end

    block_code = proc { |name| "
      static VALUE #{name}(int argc, VALUE* argv, VALUE param) {
        // block for call to #{call_tree[2]}
        VALUE last_expression = Qnil;

        #{str_lvar_initialization};
        #{str_arg_initialization}
        #{str_impl}

        return last_expression;
      }
    "
    }


    str_recv = "plocals->self"

    if recv_tree
       str_recv = to_c recv_tree
    end

    if call_args_tree.size > 1
      value_cast = ( ["VALUE"]*(call_tree[3].size) ).join(",")
      value_cast = value_cast + ", VALUE" if convention == :fastruby

      str_called_code_args = call_tree[3][1..-1].map{|subtree| to_c subtree}.join(",")

        caller_code = proc { |name| "
          static VALUE #{name}(VALUE param) {
            #{@block_struct} block;

            block.block_function_address = (void*)#{anonymous_function(block_code)};
            block.block_function_param = (void*)param;

            // call to #{call_tree[2]}

            #{str_lvar_initialization}

            return ((VALUE(*)(#{value_cast}))0x#{address.to_s(16)})(#{str_recv}, (VALUE)&block, #{str_called_code_args});
          }
        "
        }

    else
        caller_code = proc { |name| "
          static VALUE #{name}(VALUE param) {
            #{@block_struct} block;

            block.block_function_address = (void*)#{anonymous_function(block_code)};
            block.block_function_param = (void*)param;

            // call to #{call_tree[2]}
            #{str_lvar_initialization}

            return ((VALUE(*)(VALUE,VALUE))0x#{address.to_s(16)})(#{str_recv}, (VALUE)&block);
          }
        "
        }
    end

    "#{anonymous_function(caller_code)}((VALUE)#{locals_pointer})"
  end
end

#to_c_ivar(tree) ⇒ Object



541
542
543
# File 'lib/fastruby/translator.rb', line 541

def to_c_ivar(tree)
  "rb_ivar_get(#{locals_accessor}self,#{tree[1].to_i})"
end

#to_c_lasgn(tree) ⇒ Object



549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
# File 'lib/fastruby/translator.rb', line 549

def to_c_lasgn(tree)
  if options[:validate_lvar_types]
    klass = @infer_lvar_map[tree[1]]
    if klass

      verify_type_function = proc { |name| "
        static VALUE #{name}(VALUE arg) {
          if (CLASS_OF(arg)!=#{klass.internal_value}) rb_raise(#{TypeMismatchAssignmentException.internal_value}, \"Illegal assignment at runtime (type mismatch)\");
          return arg;
        }
      "
      }


      "_lvar_assing(&#{locals_accessor}#{tree[1]}, #{anonymous_function(verify_type_function)}(#{to_c tree[2]}))"
    else
      "_lvar_assing(&#{locals_accessor}#{tree[1]},#{to_c tree[2]})"
    end
  else
    "_lvar_assing(&#{locals_accessor}#{tree[1]},#{to_c tree[2]})"
  end
end

#to_c_lit(tree) ⇒ Object



422
423
424
# File 'lib/fastruby/translator.rb', line 422

def to_c_lit(tree)
  "(VALUE)#{tree[1].internal_value}"
end

#to_c_lvar(tree) ⇒ Object



572
573
574
# File 'lib/fastruby/translator.rb', line 572

def to_c_lvar(tree)
  locals_accessor + tree[1].to_s
end

#to_c_nil(tree) ⇒ Object



426
427
428
# File 'lib/fastruby/translator.rb', line 426

def to_c_nil(tree)
  "Qnil"
end

#to_c_not(tree) ⇒ Object



596
597
598
# File 'lib/fastruby/translator.rb', line 596

def to_c_not(tree)
  "RTEST(#{to_c tree[1]}) ? Qfalse : Qtrue"
end

#to_c_or(tree) ⇒ Object



592
593
594
# File 'lib/fastruby/translator.rb', line 592

def to_c_or(tree)
  "(RTEST(#{to_c tree[1]}) || RTEST(#{to_c tree[2]})) ? Qtrue : Qfalse"
end

#to_c_return(tree) ⇒ Object



418
419
420
# File 'lib/fastruby/translator.rb', line 418

def to_c_return(tree)
  "return #{to_c(tree[1])};\n"
end

#to_c_self(tree) ⇒ Object



576
577
578
# File 'lib/fastruby/translator.rb', line 576

def to_c_self(tree)
  locals_accessor + "self"
end

#to_c_str(tree) ⇒ Object



430
431
432
# File 'lib/fastruby/translator.rb', line 430

def to_c_str(tree)
  "(VALUE)#{tree[1].internal_value}"
end

#to_c_true(tree) ⇒ Object



584
585
586
# File 'lib/fastruby/translator.rb', line 584

def to_c_true(tree)
  "Qtrue"
end

#to_c_while(tree) ⇒ Object



776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
# File 'lib/fastruby/translator.rb', line 776

def to_c_while(tree)
  caller_code = proc { |name| "
    static VALUE #{name}(VALUE param) {
      #{@locals_struct} *plocals = (void*)param;
      VALUE last_expression;

      while (#{to_c tree[1]}) {
        #{to_c tree[2]};
      }

      return Qnil;
      }
    "
  }

  anonymous_function(caller_code) + "((VALUE)#{locals_pointer})"
end

#to_c_yield(tree) ⇒ Object



350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# File 'lib/fastruby/translator.rb', line 350

def to_c_yield(tree)

  block_code = proc { |name| "
    static VALUE #{name}(VALUE locals_param, VALUE* block_args) {

      #{@locals_struct} *plocals;
      plocals = (void*)locals_param;

      if (plocals->block_function_address == 0) {
        rb_raise(rb_eLocalJumpError, \"no block given\");
      } else {
        return ((VALUE(*)(int,VALUE*,VALUE))plocals->block_function_address)(#{tree.size-1}, block_args, plocals->block_function_param);
      }
    }
  "
  }

  new_yield_signature = tree[1..-1].map{|subtree| infer_type subtree}
  # merge the new_yield_signature with the new
  if @yield_signature
    if new_yield_signature.size == @yield_signature.size
      (0..new_yield_signature.size-1).each do |i|
        if @yield_signature[i] != new_yield_signature[i]
          @yield_signature[i] = nil
        end
      end
    else
      @yield_signature = new_yield_signature.map{|x| nil}
    end
  else
    @yield_signature = new_yield_signature
  end

  if tree.size > 1
    anonymous_function(block_code)+"((VALUE)#{locals_pointer}, (VALUE[]){#{tree[1..-1].map{|subtree| to_c subtree}.join(",")}})"
  else
    anonymous_function(block_code)+"((VALUE)#{locals_pointer}, (VALUE[]){})"
  end
end

#with_extra_inference(extra_inference) ⇒ Object



810
811
812
813
814
815
816
817
818
# File 'lib/fastruby/translator.rb', line 810

def with_extra_inference(extra_inference)
  previous_infer_lvar_map = @infer_lvar_map
  begin
    @infer_lvar_map = @infer_lvar_map.merge(extra_inference)
    yield
  ensure
    @infer_lvar_map = previous_infer_lvar_map
  end
end