Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(18)

Side by Side Diff: lib/compiler/implementation/js_backend/constant_system_javascript.dart

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

Powered by Google App Engine
This is Rietveld 408576698