Class: Ruby2CExtension::Plugins::CaseOptimize
- Inherits:
-
Ruby2CExtension::Plugin
- Object
- Ruby2CExtension::Plugin
- Ruby2CExtension::Plugins::CaseOptimize
- Includes:
- Tools::EnsureNodeTypeMixin
- Defined in:
- lib/ruby2cext/plugins/case_optimize.rb
Instance Attribute Summary
Attributes inherited from Ruby2CExtension::Plugin
Instance Method Summary collapse
- #fixed_immediate?(node) ⇒ Boolean
- #fixnum?(node) ⇒ Boolean
- #handle_case(cfun, hash) ⇒ Object
-
#initialize(compiler) ⇒ CaseOptimize
constructor
A new instance of CaseOptimize.
Methods included from Tools::EnsureNodeTypeMixin
Methods inherited from Ruby2CExtension::Plugin
Constructor Details
#initialize(compiler) ⇒ CaseOptimize
Returns a new instance of CaseOptimize.
11 12 13 14 15 16 |
# File 'lib/ruby2cext/plugins/case_optimize.rb', line 11 def initialize(compiler) super compiler.add_preprocessor(:case) { |cfun, node| handle_case(cfun, node.last) || node } end |
Instance Method Details
#fixed_immediate?(node) ⇒ Boolean
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/ruby2cext/plugins/case_optimize.rb', line 22 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
18 19 20 |
# File 'lib/ruby2cext/plugins/case_optimize.rb', line 18 def fixnum?(node) Array === node && node.first == :lit && Fixnum === node.last[:lit] end |
#handle_case(cfun, hash) ⇒ Object
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 99 100 101 |
# File 'lib/ruby2cext/plugins/case_optimize.rb', line 42 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 |