Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
|
ngeoffray
2012/09/05 11:20:36
Move this file to the backend?
floitsch
2012/09/05 16:12:01
Done.
| |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 const JAVA_SCRIPT_CONSTANT_SYSTEM = const JavaScriptConstantSystem(); | |
| 6 | |
| 7 class JavaScriptBitNotOperation extends BitNotOperation { | |
| 8 const JavaScriptBitNotOperation(); | |
|
ngeoffray
2012/09/05 11:20:36
Spaces please.
floitsch
2012/09/05 16:12:01
Done.
| |
| 9 Constant fold(Constant constant) { | |
| 10 if (JAVA_SCRIPT_CONSTANT_SYSTEM.isInt(constant)) { | |
| 11 // In JavaScript we don't check for -0 and treat it as if it was zero. | |
| 12 if (constant.isMinusZero()) constant = DART_CONSTANT_SYSTEM.createInt(0); | |
| 13 IntConstant intConstant = constant; | |
| 14 // We convert the result of bit-operations to 32 bit unsigned integers. | |
| 15 return JAVA_SCRIPT_CONSTANT_SYSTEM.createInt32(~intConstant.value); | |
| 16 } | |
| 17 return null; | |
| 18 } | |
| 19 } | |
| 20 | |
| 21 /** | |
| 22 * In JavaScript we truncate the result to an unsigned 32 bit integer. Also, -0 | |
| 23 * is treated as if it was the integer 0. | |
| 24 */ | |
| 25 class JavaScriptBinaryBitOperation implements BinaryOperation { | |
| 26 final BinaryBitOperation dartBitOperation; | |
| 27 const JavaScriptBinaryBitOperation(this.dartBitOperation); | |
| 28 | |
| 29 bool isUserDefinable() => dartBitOperation.isUserDefinable(); | |
| 30 SourceString get name() => dartBitOperation.name; | |
| 31 | |
| 32 Constant fold(Constant left, Constant right) { | |
| 33 // In JavaScript we don't check for -0 and treat it as if it was zero. | |
| 34 if (left.isMinusZero()) left = DART_CONSTANT_SYSTEM.createInt(0); | |
| 35 if (right.isMinusZero()) right = DART_CONSTANT_SYSTEM.createInt(0); | |
| 36 IntConstant result = dartBitOperation.fold(left, right); | |
| 37 if (result != null) { | |
| 38 // We convert the result of bit-operations to 32 bit unsigned integers. | |
| 39 return JAVA_SCRIPT_CONSTANT_SYSTEM.createInt32(result.value); | |
| 40 } | |
| 41 return result; | |
| 42 } | |
| 43 } | |
| 44 | |
| 45 class JavaScriptShiftRightOperation extends JavaScriptBinaryBitOperation { | |
| 46 const JavaScriptShiftRightOperation() : super(const ShiftRightOperation()); | |
| 47 | |
| 48 Constant fold(Constant left, Constant right) { | |
| 49 // Truncate the input value to 32 bits if necessary. | |
| 50 if (left.isInt()) { | |
| 51 IntConstant intConstant = left; | |
| 52 int value = intConstant.value; | |
| 53 int truncatedValue = value & JAVA_SCRIPT_CONSTANT_SYSTEM.BITS32; | |
| 54 // TODO(floitsch): we should treat the input to right shifts as unsigned. | |
| 55 | |
| 56 // Sign-extend. A 32 bit complement-two value x can be computed by: | |
| 57 // x_u - 2^32 (where x_u is its unsigned representation). | |
| 58 // Example: 0xFFFFFFFF - 0x100000000 => -1. | |
| 59 // We simply and with the sign-bit and multiply by two. If the sign-bit | |
| 60 // was set, then the result is 0. Otherwise it will become 2^32. | |
| 61 final int SIGN_BIT = 0x80000000; | |
| 62 truncatedValue -= 2 * (truncatedValue & SIGN_BIT); | |
| 63 if (value != truncatedValue) { | |
| 64 left = DART_CONSTANT_SYSTEM.createInt(truncatedValue); | |
| 65 } | |
| 66 } | |
| 67 return super.fold(left, right); | |
| 68 } | |
| 69 } | |
| 70 | |
| 71 class JavaScriptNegateOperation implements UnaryOperation { | |
| 72 final NegateOperation dartNegateOperation = const NegateOperation(); | |
| 73 const JavaScriptNegateOperation(); | |
| 74 | |
| 75 bool isUserDefinable() => dartNegateOperation.isUserDefinable(); | |
| 76 SourceString get name() => dartNegateOperation.name; | |
| 77 | |
| 78 Constant fold(Constant constant) { | |
| 79 if (constant.isInt()) { | |
| 80 IntConstant intConstant = constant; | |
| 81 if (intConstant.value == 0) { | |
| 82 return JAVA_SCRIPT_CONSTANT_SYSTEM.createDouble(-0.0); | |
| 83 } | |
| 84 } | |
| 85 return dartNegateOperation.fold(constant); | |
| 86 } | |
| 87 } | |
| 88 | |
| 89 class JavaScriptBinaryArithmeticOperation implements BinaryOperation { | |
| 90 final BinaryOperation dartArithmeticOperation; | |
| 91 const JavaScriptBinaryArithmeticOperation(this.dartArithmeticOperation); | |
| 92 | |
| 93 bool isUserDefinable() => dartArithmeticOperation.isUserDefinable(); | |
| 94 SourceString get name() => dartArithmeticOperation.name; | |
| 95 | |
| 96 Constant fold(Constant left, Constant right) { | |
| 97 Constant result = dartArithmeticOperation.fold(left, right); | |
| 98 if (result == null) return result; | |
| 99 return JAVA_SCRIPT_CONSTANT_SYSTEM.convertToJavaScriptConstant(result); | |
| 100 } | |
| 101 } | |
| 102 | |
| 103 class JavaScriptIdentityOperation implements BinaryOperation { | |
| 104 final IdentityOperation dartIdentityOperation = const IdentityOperation(); | |
| 105 | |
| 106 const JavaScriptIdentityOperation(); | |
| 107 | |
| 108 bool isUserDefinable() => dartIdentityOperation.isUserDefinable(); | |
| 109 SourceString get name() => dartIdentityOperation.name; | |
| 110 | |
| 111 BoolConstant fold(Constant left, Constant right) { | |
| 112 BoolConstant result = dartIdentityOperation.fold(left, right); | |
| 113 if (result == null || result.value) return result; | |
| 114 // In JavaScript -0.0 === 0 and all doubles are equal to their integer | |
| 115 // values. Furthermore NaN !== NaN. | |
| 116 if (left.isNum() && right.isNum()) { | |
| 117 NumConstant leftNum = left; | |
| 118 NumConstant rightNum = right; | |
| 119 double leftDouble = leftNum.value.toDouble(); | |
| 120 double rightDouble = rightNum.value.toDouble(); | |
| 121 return new BoolConstant(leftDouble == rightDouble); | |
| 122 } | |
| 123 return result; | |
| 124 } | |
| 125 } | |
| 126 | |
| 127 /** | |
| 128 * Constant system following the semantics for Dart code that has been | |
| 129 * compiled to JavaScript. | |
| 130 */ | |
| 131 class JavaScriptConstantSystem implements ConstantSystem { | |
| 132 const int BITS31 = 0x8FFFFFFF; | |
| 133 const int BITS32 = 0xFFFFFFFF; | |
| 134 // The maximum integer value a double can represent without losing | |
| 135 // precision. | |
| 136 const int BITS53 = 0x1FFFFFFFFFFFFF; | |
| 137 | |
| 138 final add = const JavaScriptBinaryArithmeticOperation(const AddOperation()); | |
| 139 final bitAnd = const JavaScriptBinaryBitOperation(const BitAndOperation()); | |
| 140 final bitNot = const JavaScriptBitNotOperation(); | |
| 141 final bitOr = const JavaScriptBinaryBitOperation(const BitOrOperation()); | |
| 142 final bitXor = const JavaScriptBinaryBitOperation(const BitXorOperation()); | |
| 143 final booleanAnd = const BooleanAndOperation(); | |
| 144 final booleanOr = const BooleanOrOperation(); | |
| 145 final divide = | |
| 146 const JavaScriptBinaryArithmeticOperation(const DivideOperation()); | |
| 147 final equal = const EqualsOperation(); | |
| 148 final greaterEqual = const GreaterEqualOperation(); | |
| 149 final greater = const GreaterOperation(); | |
| 150 final identity = const JavaScriptIdentityOperation(); | |
| 151 final lessEqual = const LessEqualOperation(); | |
| 152 final less = const LessOperation(); | |
| 153 final modulo = | |
| 154 const JavaScriptBinaryArithmeticOperation(const ModuloOperation()); | |
| 155 final multiply = | |
| 156 const JavaScriptBinaryArithmeticOperation(const MultiplyOperation()); | |
| 157 final negate = const JavaScriptNegateOperation(); | |
| 158 final not = const NotOperation(); | |
| 159 final shiftLeft = | |
| 160 const JavaScriptBinaryBitOperation(const ShiftLeftOperation()); | |
| 161 final shiftRight = const JavaScriptShiftRightOperation(); | |
| 162 final subtract = | |
| 163 const JavaScriptBinaryArithmeticOperation(const SubtractOperation()); | |
| 164 final truncatingDivide = const JavaScriptBinaryArithmeticOperation( | |
| 165 const TruncatingDivideOperation()); | |
| 166 | |
| 167 const JavaScriptConstantSystem(); | |
| 168 | |
| 169 /** | |
| 170 * Returns true if the given [value] fits into a double without losing | |
| 171 * precision. | |
| 172 */ | |
| 173 bool integerFitsIntoDouble(int value) { | |
| 174 int absValue = value.abs(); | |
| 175 return (absValue & BITS53) == absValue; | |
| 176 } | |
| 177 | |
| 178 NumConstant convertToJavaScriptConstant(NumConstant constant) { | |
| 179 if (constant.isInt()) { | |
| 180 IntConstant intConstant = constant; | |
| 181 int intValue = intConstant.value; | |
| 182 if (!integerFitsIntoDouble(intValue)) { | |
| 183 return new DoubleConstant(intValue.toDouble()); | |
| 184 } | |
| 185 } else if (constant.isDouble()) { | |
| 186 DoubleConstant doubleResult = constant; | |
| 187 double doubleValue = doubleResult.value; | |
| 188 if (!doubleValue.isInfinite() && !doubleValue.isNaN() && | |
| 189 !constant.isMinusZero()) { | |
| 190 int intValue = doubleValue.toInt(); | |
| 191 if (intValue == doubleValue && integerFitsIntoDouble(intValue)) { | |
| 192 return new IntConstant(intValue); | |
| 193 } | |
| 194 } | |
| 195 } | |
| 196 return constant; | |
| 197 } | |
| 198 | |
| 199 NumConstant createInt(int i) | |
| 200 => convertToJavaScriptConstant(new IntConstant(i)); | |
| 201 NumConstant createInt32(int i) => new IntConstant(i & BITS32); | |
| 202 NumConstant createDouble(double d) | |
| 203 => convertToJavaScriptConstant(new DoubleConstant(d)); | |
| 204 StringConstant createString(DartString string, Node diagnosticNode) | |
| 205 => new StringConstant(string, diagnosticNode); | |
| 206 BoolConstant createBool(bool value) => new BoolConstant(value); | |
| 207 NullConstant createNull() => new NullConstant(); | |
| 208 | |
| 209 // Integer checks don't verify that the number is not -0.0. | |
| 210 bool isInt(Constant constant) => constant.isInt() || constant.isMinusZero(); | |
| 211 bool isDouble(Constant constant) | |
| 212 => constant.isDouble() && !constant.isMinusZero(); | |
| 213 bool isString(Constant constant) => constant.isString(); | |
| 214 bool isBool(Constant constant) => constant.isBool(); | |
| 215 bool isNull(Constant constant) => constant.isNull(); | |
| 216 } | |
| OLD | NEW |