Index: lib/compiler/implementation/javascript_operations.dart |
diff --git a/lib/compiler/implementation/javascript_operations.dart b/lib/compiler/implementation/javascript_operations.dart |
new file mode 100644 |
index 0000000000000000000000000000000000000000..1dc088e892ba4d388bd5d17e3a97a9a552efd99d |
--- /dev/null |
+++ b/lib/compiler/implementation/javascript_operations.dart |
@@ -0,0 +1,171 @@ |
+// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
+// for details. All rights reserved. Use of this source code is governed by a |
+// BSD-style license that can be found in the LICENSE file. |
+ |
+class JavaScriptBitNotOperation extends BitNotOperation { |
+ static const int BITS32 = 0xFFFFFFFF; |
+ |
+ const JavaScriptBitNotOperation(); |
+ Constant fold(Constant constant) { |
+ // In JavaScript we don't check for -0 and treat it as if it was zero. |
+ if (constant.isMinusZero()) constant = new IntConstant(0); |
+ if (constant.isInt()) { |
+ IntConstant intConstant = constant; |
+ // We convert the result of bit-operations to 32 bit unsigned integers. |
+ return new IntConstant((~intConstant.value) & BITS32); |
+ } |
+ return null; |
+ } |
+} |
+ |
+/** |
+ * In JavaScript we truncate the result to an unsigned 32 bit integer. Also, -0 |
+ * is treated as if it was the integer 0. |
+ */ |
+class JavaScriptBinaryBitOperation implements BinaryOperation { |
+ static const int BITS32 = 0xFFFFFFFF; |
ngeoffray
2012/08/17 08:32:59
Share it with JavaScriptBitNotOperation?
floitsch
2012/09/03 14:34:52
Done.
|
+ |
+ final BinaryBitOperation dartBitOperation; |
+ const JavaScriptBinaryBitOperation(this.dartBitOperation); |
+ |
+ bool isUserDefinable() => dartBitOperation.isUserDefinable(); |
+ SourceString get name() => dartBitOperation.name; |
+ |
+ Constant fold(Constant left, Constant right) { |
+ // In JavaScript we don't check for -0 and treat it as if it was zero. |
+ if (left.isMinusZero()) left = new IntConstant(0); |
+ if (right.isMinusZero()) right = new IntConstant(0); |
+ IntConstant result = dartBitOperation.fold(left, right); |
+ if (result != null) { |
+ // We convert the result of bit-operations to 32 bit unsigned integers. |
+ int clampedValue = result.value & BITS32; |
+ if (clampedValue != result.value) { |
+ result = new IntConstant(clampedValue); |
+ } |
+ } |
+ return result; |
+ } |
+} |
+ |
+class JavaScriptNegateOperation implements UnaryOperation { |
+ final NegateOperation dartNegateOperation = const NegateOperation(); |
+ const JavaScriptNegateOperation(); |
+ |
+ bool isUserDefinable() => dartNegateOperation.isUserDefinable(); |
+ SourceString get name() => dartNegateOperation.name; |
+ |
+ Constant fold(Constant constant) { |
+ if (constant.isInt()) { |
+ IntConstant intConstant = constant; |
+ if (intConstant.value == 0) return new DoubleConstant(-0.0); |
+ } |
+ return dartNegateOperation.fold(constant); |
+ } |
+} |
+ |
+class JavaScriptBinaryArithmeticOperation implements BinaryOperation { |
+ final BinaryOperation dartArithmeticOperation; |
+ const JavaScriptBinaryArithmeticOperation(this.dartArithmeticOperation); |
+ |
+ bool isUserDefinable() => dartArithmeticOperation.isUserDefinable(); |
+ SourceString get name() => dartArithmeticOperation.name; |
+ |
+ /** |
+ * Returns true if the given [value] fits into a double without losing |
+ * precision. |
+ */ |
+ bool integerFitsIntoDouble(int value) { |
+ // The maximum integer value a double can represent without losing |
+ // precision. |
+ final int BITS53 = 0x1FFFFFFFFFFFFF; |
+ |
+ int absValue = value.abs(); |
+ return (absValue & BITS53) == absValue; |
+ } |
+ |
+ Constant fold(Constant left, Constant right) { |
+ Constant result = dartArithmeticOperation.fold(left, right); |
+ if (result == null) return result; |
+ if (result.isInt()) { |
+ // TODO(floitsch): make sure that the constant folding operation has the |
+ // same semantics as during runtime. |
+ IntConstant intResult = result; |
+ int intValue = intResult.value; |
+ if (!integerFitsIntoDouble(intValue)) { |
+ return new DoubleConstant(intValue.toDouble()); |
+ } |
+ } else if (result.isDouble()) { |
+ DoubleConstant doubleResult = result; |
+ double doubleValue = doubleResult.value; |
+ if (!doubleValue.isInfinite() && !doubleValue.isNaN()) { |
+ int intValue = doubleValue.toInt(); |
+ if (intValue == doubleValue && integerFitsIntoDouble(intValue)) { |
+ return new IntConstant(intValue); |
+ } |
+ } |
+ } |
+ return result; |
+ } |
+} |
+ |
+class JavaScriptIdentityOperation implements BinaryOperation { |
+ final IdentityOperation dartIdentityOperation = const IdentityOperation(); |
+ |
+ const JavaScriptIdentityOperation(); |
+ |
+ bool isUserDefinable() => dartIdentityOperation.isUserDefinable(); |
+ SourceString get name() => dartIdentityOperation.name; |
+ |
+ BoolConstant fold(Constant left, Constant right) { |
+ BoolConstant result = dartIdentityOperation.fold(left, right); |
+ if (result == null || result.value) return result; |
+ // In JavaScript -0.0 === 0 and all doubles are equal to their integer |
+ // values. |
+ if (left.isNum() && right.isNum()) { |
+ NumConstant leftNum = left; |
+ NumConstant rightNum = right; |
+ double leftDouble = leftNum.value.toDouble(); |
+ double rightDouble = rightNum.value.toDouble(); |
+ return new BoolConstant(leftDouble == rightDouble); |
+ } |
+ return result; |
+ } |
+} |
+ |
+/** |
+ * Folding operations following the semantics for Dart code that has been |
+ * compiled to JavaScript. |
+ */ |
+class JavaScriptFoldingOperations implements FoldingOperations { |
+ final add = const JavaScriptBinaryArithmeticOperation(const AddOperation()); |
+ final bitAnd = const JavaScriptBinaryBitOperation(const BitAndOperation()); |
+ final bitNot = const JavaScriptBitNotOperation(); |
+ final bitOr = const JavaScriptBinaryBitOperation(const BitOrOperation()); |
+ final bitXor = const JavaScriptBinaryBitOperation(const BitXorOperation()); |
+ final booleanAnd = const BooleanAndOperation(); |
+ final booleanOr = const BooleanOrOperation(); |
+ final divide = |
+ const JavaScriptBinaryArithmeticOperation(const DivideOperation()); |
+ final equal = const EqualsOperation(); |
+ final greaterEqual = const GreaterEqualOperation(); |
+ final greater = const GreaterOperation(); |
+ final identity = const JavaScriptIdentityOperation(); |
+ final lessEqual = const LessEqualOperation(); |
+ final less = const LessOperation(); |
+ final modulo = |
+ const JavaScriptBinaryArithmeticOperation(const ModuloOperation()); |
+ final multiply = |
+ const JavaScriptBinaryArithmeticOperation(const MultiplyOperation()); |
+ final negate = const JavaScriptNegateOperation(); |
+ final not = const NotOperation(); |
+ final shiftLeft = |
+ const JavaScriptBinaryBitOperation(const ShiftLeftOperation()); |
+ final shiftRight = |
+ const JavaScriptBinaryBitOperation(const ShiftRightOperation()); |
+ final subtract = |
+ const JavaScriptBinaryArithmeticOperation(const SubtractOperation()); |
+ final truncatingDivide = const JavaScriptBinaryArithmeticOperation( |
+ const TruncatingDivideOperation()); |
+ |
+ const JavaScriptFoldingOperations(); |
+} |