Class: MilkTea::ControlFlow::ConstantPropagation
- Inherits:
-
Object
- Object
- MilkTea::ControlFlow::ConstantPropagation
- Defined in:
- lib/milk_tea/core/control_flow/constant_propagation.rb
Defined Under Namespace
Constant Summary collapse
- UNDEF =
:undef- NAC =
:nac
Class Method Summary collapse
- .constant_value_of(expr, in_state, binding_resolution: nil, strict_binding_ids: false) ⇒ Object
- .eval_assignment_const(statement, in_state, binding_resolution:, strict_binding_ids:) ⇒ Object
- .eval_const(statement, _write, in_state, binding_resolution:, strict_binding_ids:) ⇒ Object
- .eval_expr_const(expr, state, binding_resolution:, strict_binding_ids:) ⇒ Object
- .identifier_key(identifier_expression, binding_resolution:, strict_binding_ids:) ⇒ Object
- .join_lattice(a, b) ⇒ Object
- .solve(graph, binding_resolution: nil, strict_binding_ids: false) ⇒ Object
Class Method Details
.constant_value_of(expr, in_state, binding_resolution: nil, strict_binding_ids: false) ⇒ Object
124 125 126 127 |
# File 'lib/milk_tea/core/control_flow/constant_propagation.rb', line 124 def self.constant_value_of(expr, in_state, binding_resolution: nil, strict_binding_ids: false) result = send(:eval_expr_const, expr, in_state, binding_resolution:, strict_binding_ids:) result.is_a?(ConstVal) ? result.value : nil end |
.eval_assignment_const(statement, in_state, binding_resolution:, strict_binding_ids:) ⇒ Object
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 102 103 |
# File 'lib/milk_tea/core/control_flow/constant_propagation.rb', line 74 def self.eval_assignment_const(statement, in_state, binding_resolution:, strict_binding_ids:) return NAC unless statement.target.is_a?(AST::Identifier) rhs = eval_expr_const(statement.value, in_state, binding_resolution:, strict_binding_ids:) return NAC unless rhs.is_a?(ConstVal) return rhs if statement.operator == "=" lhs = eval_expr_const(statement.target, in_state, binding_resolution:, strict_binding_ids:) return NAC unless lhs.is_a?(ConstVal) begin v = case statement.operator when "+=" then lhs.value + rhs.value when "-=" then lhs.value - rhs.value when "*=" then lhs.value * rhs.value when "/=" then rhs.value.zero? ? (return NAC) : lhs.value / rhs.value when "%=" then rhs.value.zero? ? (return NAC) : lhs.value % rhs.value when "&=" then lhs.value & rhs.value when "|=" then lhs.value | rhs.value when "^=" then lhs.value ^ rhs.value when "<<=" then lhs.value << rhs.value when ">>=" then lhs.value >> rhs.value else return NAC end ConstVal.new(v) rescue ZeroDivisionError, RangeError NAC end end |
.eval_const(statement, _write, in_state, binding_resolution:, strict_binding_ids:) ⇒ Object
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/milk_tea/core/control_flow/constant_propagation.rb', line 63 def self.eval_const(statement, _write, in_state, binding_resolution:, strict_binding_ids:) case statement when AST::LocalDecl eval_expr_const(statement.value, in_state, binding_resolution:, strict_binding_ids:) when AST::Assignment eval_assignment_const(statement, in_state, binding_resolution:, strict_binding_ids:) else NAC end end |
.eval_expr_const(expr, state, binding_resolution:, strict_binding_ids:) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/milk_tea/core/control_flow/constant_propagation.rb', line 105 def self.eval_expr_const(expr, state, binding_resolution:, strict_binding_ids:) return NAC if expr.nil? value = ConstEval.evaluate( expr, resolve_identifier: lambda do |identifier_expression| key = identifier_key(identifier_expression, binding_resolution:, strict_binding_ids:) next unless key state_value = state[key] state_value.is_a?(ConstVal) ? state_value.value : nil end, resolve_member_access: nil, ) value.nil? ? NAC : ConstVal.new(value) rescue CompileTime::Error NAC end |
.identifier_key(identifier_expression, binding_resolution:, strict_binding_ids:) ⇒ Object
129 130 131 132 133 134 135 136 137 |
# File 'lib/milk_tea/core/control_flow/constant_propagation.rb', line 129 def self.identifier_key(identifier_expression, binding_resolution:, strict_binding_ids:) if binding_resolution && (id = binding_resolution.identifier_binding_ids[identifier_expression.object_id]) return id end return nil if strict_binding_ids identifier_expression.name end |
.join_lattice(a, b) ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/milk_tea/core/control_flow/constant_propagation.rb', line 55 def self.join_lattice(a, b) return b if a == UNDEF return a if b == UNDEF return NAC if a == NAC || b == NAC a == b ? a : NAC end |
.solve(graph, binding_resolution: nil, strict_binding_ids: false) ⇒ Object
23 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 49 50 51 52 53 |
# File 'lib/milk_tea/core/control_flow/constant_propagation.rb', line 23 def self.solve(graph, binding_resolution: nil, strict_binding_ids: false) result = Dataflow.solve( graph, direction: :forward, initial: -> { {} }, join: lambda do |states| return {} if states.empty? states.reduce do |acc, state| keys = acc.keys | state.keys keys.each_with_object({}) do |k, merged| a = acc[k] || UNDEF b = state[k] || UNDEF merged[k] = join_lattice(a, b) end end end, transfer: lambda do |node, in_state| out = in_state.dup node.writes_info.each do |write| key = write[:binding_key] val = eval_const(node.statement, write, in_state, binding_resolution:, strict_binding_ids:) out[key] = val end out end, boundary_in: { graph.entry_id => {} } ) Result.new(in_states: result.in_states, out_states: result.out_states) end |