Skip to content

Commit 0e09faf

Browse files
authored
Use comparison operators for constant folding (#720)
1 parent eaec666 commit 0e09faf

2 files changed

Lines changed: 20 additions & 64 deletions

File tree

Src/IronPython/Compiler/Ast/BinaryExpression.Generated.cs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ internal override ConstantExpression ConstantFold() {
4242
case PythonOperator.BitwiseAnd: return new ConstantExpression(Int32Ops.BitwiseAnd((Int32)constLeft.Value, (Int32)constRight.Value));
4343
case PythonOperator.BitwiseOr: return new ConstantExpression(Int32Ops.BitwiseOr((Int32)constLeft.Value, (Int32)constRight.Value));
4444
case PythonOperator.ExclusiveOr: return new ConstantExpression(Int32Ops.ExclusiveOr((Int32)constLeft.Value, (Int32)constRight.Value));
45-
case PythonOperator.LessThan: return new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(Int32Ops.Compare((Int32)constLeft.Value, (Int32)constRight.Value) < 0));
46-
case PythonOperator.GreaterThan: return new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(Int32Ops.Compare((Int32)constLeft.Value, (Int32)constRight.Value) > 0));
47-
case PythonOperator.LessThanOrEqual: return new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(Int32Ops.Compare((Int32)constLeft.Value, (Int32)constRight.Value) <= 0));
48-
case PythonOperator.GreaterThanOrEqual: return new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(Int32Ops.Compare((Int32)constLeft.Value, (Int32)constRight.Value) >= 0));
49-
case PythonOperator.Equals: return new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(Int32Ops.Compare((Int32)constLeft.Value, (Int32)constRight.Value) == 0));
50-
case PythonOperator.NotEquals: return new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(Int32Ops.Compare((Int32)constLeft.Value, (Int32)constRight.Value) != 0));
45+
case PythonOperator.LessThan: return new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject((Int32)constLeft.Value < (Int32)constRight.Value));
46+
case PythonOperator.GreaterThan: return new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject((Int32)constLeft.Value > (Int32)constRight.Value));
47+
case PythonOperator.LessThanOrEqual: return new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject((Int32)constLeft.Value <= (Int32)constRight.Value));
48+
case PythonOperator.GreaterThanOrEqual: return new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject((Int32)constLeft.Value >= (Int32)constRight.Value));
49+
case PythonOperator.Equals: return new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject((Int32)constLeft.Value == (Int32)constRight.Value));
50+
case PythonOperator.NotEquals: return new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject((Int32)constLeft.Value != (Int32)constRight.Value));
5151
}
5252
}
5353
if (constLeft.Value.GetType() == typeof(Double)) {
@@ -59,12 +59,12 @@ internal override ConstantExpression ConstantFold() {
5959
case PythonOperator.FloorDivide: return new ConstantExpression(DoubleOps.FloorDivide((Double)constLeft.Value, (Double)constRight.Value));
6060
case PythonOperator.TrueDivide: return new ConstantExpression(DoubleOps.TrueDivide((Double)constLeft.Value, (Double)constRight.Value));
6161
case PythonOperator.Mod: return new ConstantExpression(DoubleOps.Mod((Double)constLeft.Value, (Double)constRight.Value));
62-
case PythonOperator.LessThan: return new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(DoubleOps.Compare((Double)constLeft.Value, (Double)constRight.Value) < 0));
63-
case PythonOperator.GreaterThan: return new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(DoubleOps.Compare((Double)constLeft.Value, (Double)constRight.Value) > 0));
64-
case PythonOperator.LessThanOrEqual: return new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(DoubleOps.Compare((Double)constLeft.Value, (Double)constRight.Value) <= 0));
65-
case PythonOperator.GreaterThanOrEqual: return new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(DoubleOps.Compare((Double)constLeft.Value, (Double)constRight.Value) >= 0));
66-
case PythonOperator.Equals: return new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(DoubleOps.Compare((Double)constLeft.Value, (Double)constRight.Value) == 0));
67-
case PythonOperator.NotEquals: return new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(DoubleOps.Compare((Double)constLeft.Value, (Double)constRight.Value) != 0));
62+
case PythonOperator.LessThan: return new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject((Double)constLeft.Value < (Double)constRight.Value));
63+
case PythonOperator.GreaterThan: return new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject((Double)constLeft.Value > (Double)constRight.Value));
64+
case PythonOperator.LessThanOrEqual: return new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject((Double)constLeft.Value <= (Double)constRight.Value));
65+
case PythonOperator.GreaterThanOrEqual: return new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject((Double)constLeft.Value >= (Double)constRight.Value));
66+
case PythonOperator.Equals: return new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject((Double)constLeft.Value == (Double)constRight.Value));
67+
case PythonOperator.NotEquals: return new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject((Double)constLeft.Value != (Double)constRight.Value));
6868
}
6969
}
7070
if (constLeft.Value.GetType() == typeof(BigInteger)) {
@@ -81,12 +81,12 @@ internal override ConstantExpression ConstantFold() {
8181
case PythonOperator.BitwiseAnd: return new ConstantExpression(BigIntegerOps.BitwiseAnd((BigInteger)constLeft.Value, (BigInteger)constRight.Value));
8282
case PythonOperator.BitwiseOr: return new ConstantExpression(BigIntegerOps.BitwiseOr((BigInteger)constLeft.Value, (BigInteger)constRight.Value));
8383
case PythonOperator.ExclusiveOr: return new ConstantExpression(BigIntegerOps.ExclusiveOr((BigInteger)constLeft.Value, (BigInteger)constRight.Value));
84-
case PythonOperator.LessThan: return new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(BigIntegerOps.Compare((BigInteger)constLeft.Value, (BigInteger)constRight.Value) < 0));
85-
case PythonOperator.GreaterThan: return new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(BigIntegerOps.Compare((BigInteger)constLeft.Value, (BigInteger)constRight.Value) > 0));
86-
case PythonOperator.LessThanOrEqual: return new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(BigIntegerOps.Compare((BigInteger)constLeft.Value, (BigInteger)constRight.Value) <= 0));
87-
case PythonOperator.GreaterThanOrEqual: return new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(BigIntegerOps.Compare((BigInteger)constLeft.Value, (BigInteger)constRight.Value) >= 0));
88-
case PythonOperator.Equals: return new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(BigIntegerOps.Compare((BigInteger)constLeft.Value, (BigInteger)constRight.Value) == 0));
89-
case PythonOperator.NotEquals: return new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(BigIntegerOps.Compare((BigInteger)constLeft.Value, (BigInteger)constRight.Value) != 0));
84+
case PythonOperator.LessThan: return new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject((BigInteger)constLeft.Value < (BigInteger)constRight.Value));
85+
case PythonOperator.GreaterThan: return new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject((BigInteger)constLeft.Value > (BigInteger)constRight.Value));
86+
case PythonOperator.LessThanOrEqual: return new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject((BigInteger)constLeft.Value <= (BigInteger)constRight.Value));
87+
case PythonOperator.GreaterThanOrEqual: return new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject((BigInteger)constLeft.Value >= (BigInteger)constRight.Value));
88+
case PythonOperator.Equals: return new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject((BigInteger)constLeft.Value == (BigInteger)constRight.Value));
89+
case PythonOperator.NotEquals: return new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject((BigInteger)constLeft.Value != (BigInteger)constRight.Value));
9090
}
9191
}
9292
if (constLeft.Value.GetType() == typeof(Complex)) {

Src/Scripts/generate_ops.py

Lines changed: 2 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -148,45 +148,6 @@ def genStringOperatorToSymbol(self, cw):
148148
cw.writeline("case PythonOperationKind.Reverse%s: return \"__r%s__\";" % (titleName, self.name))
149149
cw.writeline("case PythonOperationKind.InPlace%s: return \"__i%s__\";" % (titleName, self.name))
150150

151-
def genOldStyleOp(self, cw):
152-
if self.isCompare(): return
153-
154-
cw.writeline('[return: MaybeNotImplemented]')
155-
if self.dotnetOp:
156-
cw.enter_block("public static object operator %s([NotNull]OldInstance self, object other)" % self.symbol)
157-
else:
158-
cw.writeline('[SpecialName]')
159-
cw.enter_block("public static object %s([NotNull]OldInstance self, object other)" % self.title_name())
160-
cw.writeline('object res = InvokeOne(self, other, \"__%s__\");' % self.name)
161-
cw.writeline('if (res != NotImplementedType.Value) return res;')
162-
163-
cw.writeline()
164-
cw.writeline("OldInstance otherOc = other as OldInstance;")
165-
cw.enter_block("if (otherOc != null)")
166-
cw.writeline('return InvokeOne(otherOc, self, \"__r%s__\");' % self.name)
167-
cw.exit_block() # end of otherOc != null
168-
169-
cw.writeline("return NotImplementedType.Value;")
170-
cw.exit_block() # end method
171-
cw.writeline()
172-
173-
cw.writeline('[return: MaybeNotImplemented]')
174-
if self.dotnetOp:
175-
cw.enter_block("public static object operator %s(object other, [NotNull]OldInstance self)" % self.symbol)
176-
else:
177-
cw.writeline('[SpecialName]')
178-
cw.enter_block("public static object %s(object other, [NotNull]OldInstance self)" % self.title_name())
179-
cw.writeline("return InvokeOne(self, other, \"__r%s__\");" % self.name)
180-
cw.exit_block() # end method
181-
cw.writeline()
182-
183-
cw.writeline('[return: MaybeNotImplemented]')
184-
cw.writeline('[SpecialName]')
185-
cw.enter_block("public object InPlace%s(object other)" % self.title_name())
186-
cw.writeline("return InvokeOne(this, other, \"__i%s__\");" % self.name)
187-
cw.exit_block() # end method
188-
cw.writeline()
189-
190151
def genWeakRefOperatorNames(self, cw):
191152
cw.writeline('[SlotField] public static PythonTypeSlot __%s__ = new SlotWrapper(\"__%s__\", ProxyType);' % (self.name, self.name))
192153

@@ -210,8 +171,8 @@ def genConstantFolding(self, cw, type):
210171
if type == 'Complex' and (self.clrName in ['Mod', 'FloorDivide']):
211172
return
212173
if self.isCompare():
213-
if type != 'Complex' and self.symbol != '<>':
214-
cw.writeline('case PythonOperator.%s: return new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(%sOps.Compare((%s)constLeft.Value, (%s)constRight.Value) %s 0));' % (self.clrName, type, type, type, self.symbol))
174+
if type != 'Complex':
175+
cw.writeline('case PythonOperator.%s: return new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject((%s)constLeft.Value %s (%s)constRight.Value));' % (self.clrName, type, self.symbol, type))
215176
elif (type !='Double' and type != 'Complex') or not self.is_bitwise():
216177
cw.writeline('case PythonOperator.%s: return new ConstantExpression(%sOps.%s((%s)constLeft.Value, (%s)constRight.Value));' % (self.clrName, type, self.clrName, type, type))
217178

@@ -534,11 +495,6 @@ def weakrefCallabelProxy_operators(cw):
534495
if op.is_comparison(): continue
535496
op.genWeakRefCallableProxyOperatorNames(cw)
536497

537-
def oldinstance_operators(cw):
538-
for op in ops:
539-
if not isinstance(op, Operator): continue
540-
op.genOldStyleOp(cw)
541-
542498
def operator_reversal(cw):
543499
for op in ops:
544500
if not isinstance(op, Operator): continue

0 commit comments

Comments
 (0)