Class: Ruby2CExtension::Plugins::CaseOptimize

Inherits:
Ruby2CExtension::Plugin show all
Includes:
Tools::EnsureNodeTypeMixin
Defined in:
lib/ruby2cext/plugins/case_optimize.rb

Instance Attribute Summary

Attributes inherited from Ruby2CExtension::Plugin

#compiler

Instance Method Summary collapse

Methods included from Tools::EnsureNodeTypeMixin

#ensure_node_type

Methods inherited from Ruby2CExtension::Plugin

#global_c_code, #init_c_code

Constructor Details

#initialize(compiler) ⇒ CaseOptimize

Returns a new instance of CaseOptimize.



8
9
10
11
12
13
# File 'lib/ruby2cext/plugins/case_optimize.rb', line 8

def initialize(compiler)
  super
  compiler.add_preprocessor(:case) { |cfun, node|
    handle_case(cfun, node.last) || node
  }
end

Instance Method Details

#fixed_immediate?(node) ⇒ Boolean

Returns:



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ruby2cext/plugins/case_optimize.rb', line 19

def fixed_immediate?(node)
  # checks if the node is optimizable and if yes returns the equivalent C value
  if fixnum?(node)
    "LONG2FIX(#{node.last[:lit].inspect})"
  elsif Array === node
    case node.first
    when :nil
      "Qnil"
    when :true
      "Qtrue"
    when :false
      "Qfalse"
    else
      false
    end
  else
    false
  end
end

#fixnum?(node) ⇒ Boolean

Returns:



15
16
17
# File 'lib/ruby2cext/plugins/case_optimize.rb', line 15

def fixnum?(node)
  Array === node && node.first == :lit && Fixnum === node.last[:lit]
end

#handle_case(cfun, hash) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/ruby2cext/plugins/case_optimize.rb', line 39

def handle_case(cfun, hash)
  cur_when = hash[:body]
  fallback_whens = []
  opt_cases = []
  while cur_when.first == :when
    ensure_node_type(head = cur_when.last[:head], :array)
    cases = head.last.map { |wn| fixed_immediate?(wn) }
    break unless cases.all?
    case_c_code = cases.map { |c| "case #{c}:" }.join("\n")
    fixnum_whens = head.last.select { |wn| fixnum?(wn) }
    unless fixnum_whens.empty?
      goto_label = compiler.un("case_opt_label")
      case_c_code << "\n#{goto_label}:"
      fallback_whens << [:when, {
        :body => "Qnil;\ngoto #{goto_label}", # TODO: evil, depends on impl. details of comp_case/handle_when
        :head => [:array, fixnum_whens]
      }]
    end
    opt_cases << [case_c_code, cur_when.last[:body]]
    cur_when = cur_when.last[:next] || [:nil, {}]
  end
  return nil if opt_cases.empty? # nothing to optimize
  rest = cur_when
  if rest.first == :when # some whens are left construct a complete new case node
    rest = [:case, {:head => "case_opt_val", :body => rest}]
  end
  cfun.instance_eval {
    c_scope_res {
      l "VALUE case_opt_val;"
      l "case_opt_val = #{comp(hash[:head])};"
      l "switch (case_opt_val) {"
      opt_cases.each { |(case_code, body_node)|
        l case_code
        assign_res(comp(body_node))
        l "break;"
      }
      l "default:"
      if fallback_whens.empty?
        assign_res(comp(rest))
      else
        # link the fallback_whens
        fallback_whens.each_with_index { |wn, i|
          fallback_whens[i - 1].last[:next] = wn if i > 0
        }
        fallback_whens.last.last[:next] = "Qundef"
        c_if("!FIXNUM_P(case_opt_val)") {
          assign_res(comp_case({:head => "case_opt_val", :body => fallback_whens.first}))
        }
        c_else {
          assign_res("Qundef")
        }
        c_if("res == Qundef") {
          assign_res(comp(rest))
        }
      end
      l "}"
      "res"
    }
  }
end