cython-devel
changeset 4009:1c7d838997ed
code cleanup in ConstantFolding transform to make boolean handling less error prone
| author | Stefan Behnel <scoder@users.berlios.de> |
|---|---|
| date | Thu Nov 25 07:49:03 2010 +0100 (2 years ago) |
| parents | b8653f818cc5 |
| children | d94ea2350269 |
| files | Cython/Compiler/Optimize.py |
line diff
1.1 --- a/Cython/Compiler/Optimize.py Thu Nov 25 07:44:59 2010 +0100
1.2 +++ b/Cython/Compiler/Optimize.py Thu Nov 25 07:49:03 2010 +0100
1.3 @@ -2970,12 +2970,23 @@
1.4 self._calculate_const(node)
1.5 return node
1.6
1.7 - def visit_UnaryMinusNode(self, node):
1.8 + def visit_UnopNode(self, node):
1.9 self._calculate_const(node)
1.10 if node.constant_result is ExprNodes.not_a_constant:
1.11 return node
1.12 if not node.operand.is_literal:
1.13 return node
1.14 + if isinstance(node.operand, ExprNodes.BoolNode):
1.15 + return ExprNodes.IntNode(node.pos, value = str(node.constant_result),
1.16 + type = PyrexTypes.c_int_type,
1.17 + constant_result = node.constant_result)
1.18 + if node.operator == '+':
1.19 + return self._handle_UnaryPlusNode(node)
1.20 + elif node.operator == '-':
1.21 + return self._handle_UnaryMinusNode(node)
1.22 + return node
1.23 +
1.24 + def _handle_UnaryMinusNode(self, node):
1.25 if isinstance(node.operand, ExprNodes.LongNode):
1.26 return ExprNodes.LongNode(node.pos, value = '-' + node.operand.value,
1.27 constant_result = node.constant_result)
1.28 @@ -2983,11 +2994,6 @@
1.29 # this is a safe operation
1.30 return ExprNodes.FloatNode(node.pos, value = '-' + node.operand.value,
1.31 constant_result = node.constant_result)
1.32 - if isinstance(node.operand, ExprNodes.BoolNode):
1.33 - # not important at all, but simplifies the code below
1.34 - return ExprNodes.IntNode(node.pos, value = str(node.constant_result),
1.35 - type = PyrexTypes.c_int_type,
1.36 - constant_result = node.constant_result)
1.37 node_type = node.operand.type
1.38 if node_type.is_int and node_type.signed or \
1.39 isinstance(node.operand, ExprNodes.IntNode) and node_type.is_pyobject:
1.40 @@ -2997,10 +3003,7 @@
1.41 constant_result = node.constant_result)
1.42 return node
1.43
1.44 - def visit_UnaryPlusNode(self, node):
1.45 - self._calculate_const(node)
1.46 - if node.constant_result is ExprNodes.not_a_constant:
1.47 - return node
1.48 + def _handle_UnaryPlusNode(self, node):
1.49 if node.constant_result == node.operand.constant_result:
1.50 return node.operand
1.51 return node
1.52 @@ -3026,12 +3029,13 @@
1.53 return node
1.54 if isinstance(node.constant_result, float):
1.55 return node
1.56 - if not node.operand1.is_literal or not node.operand2.is_literal:
1.57 + operand1, operand2 = node.operand1, node.operand2
1.58 + if not operand1.is_literal or not operand2.is_literal:
1.59 return node
1.60
1.61 # now inject a new constant node with the calculated value
1.62 try:
1.63 - type1, type2 = node.operand1.type, node.operand2.type
1.64 + type1, type2 = operand1.type, operand2.type
1.65 if type1 is None or type2 is None:
1.66 return node
1.67 except AttributeError:
1.68 @@ -3041,14 +3045,14 @@
1.69 widest_type = PyrexTypes.widest_numeric_type(type1, type2)
1.70 else:
1.71 widest_type = PyrexTypes.py_object_type
1.72 - target_class = self._widest_node_class(node.operand1, node.operand2)
1.73 + target_class = self._widest_node_class(operand1, operand2)
1.74 if target_class is None:
1.75 return node
1.76 elif target_class is ExprNodes.IntNode:
1.77 - unsigned = getattr(node.operand1, 'unsigned', '') and \
1.78 - getattr(node.operand2, 'unsigned', '')
1.79 - longness = "LL"[:max(len(getattr(node.operand1, 'longness', '')),
1.80 - len(getattr(node.operand2, 'longness', '')))]
1.81 + unsigned = getattr(operand1, 'unsigned', '') and \
1.82 + getattr(operand2, 'unsigned', '')
1.83 + longness = "LL"[:max(len(getattr(operand1, 'longness', '')),
1.84 + len(getattr(operand2, 'longness', '')))]
1.85 new_node = ExprNodes.IntNode(pos=node.pos,
1.86 unsigned = unsigned, longness = longness,
1.87 value = str(node.constant_result),
