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

Side by Side Diff: sdk/lib/_internal/compiler/implementation/constant_system_dart.dart

Issue 614993002: Rename Constant to ConstantValue and ConstExp to ConstantExpression. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 2 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
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 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 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. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 part of dart2js; 5 part of dart2js;
6 6
7 const DART_CONSTANT_SYSTEM = const DartConstantSystem(); 7 const DART_CONSTANT_SYSTEM = const DartConstantSystem();
8 8
9 class BitNotOperation implements UnaryOperation { 9 class BitNotOperation implements UnaryOperation {
10 final String name = '~'; 10 final String name = '~';
11 const BitNotOperation(); 11 const BitNotOperation();
12 Constant fold(Constant constant) { 12 ConstantValue fold(ConstantValue constant) {
13 if (constant.isInt) { 13 if (constant.isInt) {
14 IntConstant intConstant = constant; 14 IntConstantValue intConstant = constant;
15 return DART_CONSTANT_SYSTEM.createInt(~intConstant.value); 15 return DART_CONSTANT_SYSTEM.createInt(~intConstant.primitiveValue);
16 } 16 }
17 return null; 17 return null;
18 } 18 }
19 } 19 }
20 20
21 class NegateOperation implements UnaryOperation { 21 class NegateOperation implements UnaryOperation {
22 final String name = 'negate'; 22 final String name = 'negate';
23 const NegateOperation(); 23 const NegateOperation();
24 Constant fold(Constant constant) { 24 ConstantValue fold(ConstantValue constant) {
25 if (constant.isInt) { 25 if (constant.isInt) {
26 IntConstant intConstant = constant; 26 IntConstantValue intConstant = constant;
27 return DART_CONSTANT_SYSTEM.createInt(-intConstant.value); 27 return DART_CONSTANT_SYSTEM.createInt(-intConstant.primitiveValue);
28 } 28 }
29 if (constant.isDouble) { 29 if (constant.isDouble) {
30 DoubleConstant doubleConstant = constant; 30 DoubleConstantValue doubleConstant = constant;
31 return DART_CONSTANT_SYSTEM.createDouble(-doubleConstant.value); 31 return DART_CONSTANT_SYSTEM.createDouble(-doubleConstant.primitiveValue);
32 } 32 }
33 return null; 33 return null;
34 } 34 }
35 } 35 }
36 36
37 class NotOperation implements UnaryOperation { 37 class NotOperation implements UnaryOperation {
38 final String name = '!'; 38 final String name = '!';
39 const NotOperation(); 39 const NotOperation();
40 Constant fold(Constant constant) { 40 ConstantValue fold(ConstantValue constant) {
41 if (constant.isBool) { 41 if (constant.isBool) {
42 BoolConstant boolConstant = constant; 42 BoolConstantValue boolConstant = constant;
43 return DART_CONSTANT_SYSTEM.createBool(!boolConstant.value); 43 return DART_CONSTANT_SYSTEM.createBool(!boolConstant.primitiveValue);
44 } 44 }
45 return null; 45 return null;
46 } 46 }
47 } 47 }
48 48
49 /** 49 /**
50 * Operations that only work if both arguments are integers. 50 * Operations that only work if both arguments are integers.
51 */ 51 */
52 abstract class BinaryBitOperation implements BinaryOperation { 52 abstract class BinaryBitOperation implements BinaryOperation {
53 const BinaryBitOperation(); 53 const BinaryBitOperation();
54 Constant fold(Constant left, Constant right) { 54 ConstantValue fold(ConstantValue left, ConstantValue right) {
55 if (left.isInt && right.isInt) { 55 if (left.isInt && right.isInt) {
56 IntConstant leftInt = left; 56 IntConstantValue leftInt = left;
57 IntConstant rightInt = right; 57 IntConstantValue rightInt = right;
58 int resultValue = foldInts(leftInt.value, rightInt.value); 58 int resultValue =
59 foldInts(leftInt.primitiveValue, rightInt.primitiveValue);
59 if (resultValue == null) return null; 60 if (resultValue == null) return null;
60 return DART_CONSTANT_SYSTEM.createInt(resultValue); 61 return DART_CONSTANT_SYSTEM.createInt(resultValue);
61 } 62 }
62 return null; 63 return null;
63 } 64 }
64 65
65 int foldInts(int left, int right); 66 int foldInts(int left, int right);
66 } 67 }
67 68
68 class BitOrOperation extends BinaryBitOperation { 69 class BitOrOperation extends BinaryBitOperation {
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 const ShiftRightOperation(); 104 const ShiftRightOperation();
104 int foldInts(int left, int right) { 105 int foldInts(int left, int right) {
105 if (right < 0) return null; 106 if (right < 0) return null;
106 return left >> right; 107 return left >> right;
107 } 108 }
108 apply(left, right) => left >> right; 109 apply(left, right) => left >> right;
109 } 110 }
110 111
111 abstract class BinaryBoolOperation implements BinaryOperation { 112 abstract class BinaryBoolOperation implements BinaryOperation {
112 const BinaryBoolOperation(); 113 const BinaryBoolOperation();
113 Constant fold(Constant left, Constant right) { 114 ConstantValue fold(ConstantValue left, ConstantValue right) {
114 if (left.isBool && right.isBool) { 115 if (left.isBool && right.isBool) {
115 BoolConstant leftBool = left; 116 BoolConstantValue leftBool = left;
116 BoolConstant rightBool = right; 117 BoolConstantValue rightBool = right;
117 bool resultValue = foldBools(leftBool.value, rightBool.value); 118 bool resultValue =
119 foldBools(leftBool.primitiveValue, rightBool.primitiveValue);
118 return DART_CONSTANT_SYSTEM.createBool(resultValue); 120 return DART_CONSTANT_SYSTEM.createBool(resultValue);
119 } 121 }
120 return null; 122 return null;
121 } 123 }
122 124
123 bool foldBools(bool left, bool right); 125 bool foldBools(bool left, bool right);
124 } 126 }
125 127
126 class BooleanAndOperation extends BinaryBoolOperation { 128 class BooleanAndOperation extends BinaryBoolOperation {
127 final String name = '&&'; 129 final String name = '&&';
128 const BooleanAndOperation(); 130 const BooleanAndOperation();
129 bool foldBools(bool left, bool right) => left && right; 131 bool foldBools(bool left, bool right) => left && right;
130 apply(left, right) => left && right; 132 apply(left, right) => left && right;
131 } 133 }
132 134
133 class BooleanOrOperation extends BinaryBoolOperation { 135 class BooleanOrOperation extends BinaryBoolOperation {
134 final String name = '||'; 136 final String name = '||';
135 const BooleanOrOperation(); 137 const BooleanOrOperation();
136 bool foldBools(bool left, bool right) => left || right; 138 bool foldBools(bool left, bool right) => left || right;
137 apply(left, right) => left || right; 139 apply(left, right) => left || right;
138 } 140 }
139 141
140 abstract class ArithmeticNumOperation implements BinaryOperation { 142 abstract class ArithmeticNumOperation implements BinaryOperation {
141 const ArithmeticNumOperation(); 143 const ArithmeticNumOperation();
142 Constant fold(Constant left, Constant right) { 144 ConstantValue fold(ConstantValue left, ConstantValue right) {
143 if (left.isNum && right.isNum) { 145 if (left.isNum && right.isNum) {
144 NumConstant leftNum = left; 146 NumConstantValue leftNum = left;
145 NumConstant rightNum = right; 147 NumConstantValue rightNum = right;
146 num foldedValue; 148 num foldedValue;
147 if (left.isInt && right.isInt) { 149 if (left.isInt && right.isInt) {
148 foldedValue = foldInts(leftNum.value, rightNum.value); 150 foldedValue = foldInts(leftNum.primitiveValue, rightNum.primitiveValue);
149 } else { 151 } else {
150 foldedValue = foldNums(leftNum.value, rightNum.value); 152 foldedValue = foldNums(leftNum.primitiveValue, rightNum.primitiveValue);
151 } 153 }
152 // A division by 0 means that we might not have a folded value. 154 // A division by 0 means that we might not have a folded value.
153 if (foldedValue == null) return null; 155 if (foldedValue == null) return null;
154 if (left.isInt && right.isInt && !isDivide() || 156 if (left.isInt && right.isInt && !isDivide() ||
155 isTruncatingDivide()) { 157 isTruncatingDivide()) {
156 assert(foldedValue is int); 158 assert(foldedValue is int);
157 return DART_CONSTANT_SYSTEM.createInt(foldedValue); 159 return DART_CONSTANT_SYSTEM.createInt(foldedValue);
158 } else { 160 } else {
159 return DART_CONSTANT_SYSTEM.createDouble(foldedValue); 161 return DART_CONSTANT_SYSTEM.createDouble(foldedValue);
160 } 162 }
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 final String name = '/'; 215 final String name = '/';
214 const DivideOperation(); 216 const DivideOperation();
215 num foldNums(num left, num right) => left / right; 217 num foldNums(num left, num right) => left / right;
216 bool isDivide() => true; 218 bool isDivide() => true;
217 apply(left, right) => left / right; 219 apply(left, right) => left / right;
218 } 220 }
219 221
220 class AddOperation implements BinaryOperation { 222 class AddOperation implements BinaryOperation {
221 final String name = '+'; 223 final String name = '+';
222 const AddOperation(); 224 const AddOperation();
223 Constant fold(Constant left, Constant right) { 225 ConstantValue fold(ConstantValue left, ConstantValue right) {
224 if (left.isInt && right.isInt) { 226 if (left.isInt && right.isInt) {
225 IntConstant leftInt = left; 227 IntConstantValue leftInt = left;
226 IntConstant rightInt = right; 228 IntConstantValue rightInt = right;
227 int result = leftInt.value + rightInt.value; 229 int result = leftInt.primitiveValue + rightInt.primitiveValue;
228 return DART_CONSTANT_SYSTEM.createInt(result); 230 return DART_CONSTANT_SYSTEM.createInt(result);
229 } else if (left.isNum && right.isNum) { 231 } else if (left.isNum && right.isNum) {
230 NumConstant leftNum = left; 232 NumConstantValue leftNum = left;
231 NumConstant rightNum = right; 233 NumConstantValue rightNum = right;
232 double result = leftNum.value + rightNum.value; 234 double result = leftNum.primitiveValue + rightNum.primitiveValue;
233 return DART_CONSTANT_SYSTEM.createDouble(result); 235 return DART_CONSTANT_SYSTEM.createDouble(result);
234 } else { 236 } else {
235 return null; 237 return null;
236 } 238 }
237 } 239 }
238 apply(left, right) => left + right; 240 apply(left, right) => left + right;
239 } 241 }
240 242
241 abstract class RelationalNumOperation implements BinaryOperation { 243 abstract class RelationalNumOperation implements BinaryOperation {
242 const RelationalNumOperation(); 244 const RelationalNumOperation();
243 Constant fold(Constant left, Constant right) { 245 ConstantValue fold(ConstantValue left, ConstantValue right) {
244 if (!left.isNum || !right.isNum) return null; 246 if (!left.isNum || !right.isNum) return null;
245 NumConstant leftNum = left; 247 NumConstantValue leftNum = left;
246 NumConstant rightNum = right; 248 NumConstantValue rightNum = right;
247 bool foldedValue = foldNums(leftNum.value, rightNum.value); 249 bool foldedValue =
250 foldNums(leftNum.primitiveValue, rightNum.primitiveValue);
248 assert(foldedValue != null); 251 assert(foldedValue != null);
249 return DART_CONSTANT_SYSTEM.createBool(foldedValue); 252 return DART_CONSTANT_SYSTEM.createBool(foldedValue);
250 } 253 }
251 254
252 bool foldNums(num left, num right); 255 bool foldNums(num left, num right);
253 } 256 }
254 257
255 class LessOperation extends RelationalNumOperation { 258 class LessOperation extends RelationalNumOperation {
256 final String name = '<'; 259 final String name = '<';
257 const LessOperation(); 260 const LessOperation();
(...skipping 18 matching lines...) Expand all
276 class GreaterEqualOperation extends RelationalNumOperation { 279 class GreaterEqualOperation extends RelationalNumOperation {
277 final String name = '>='; 280 final String name = '>=';
278 const GreaterEqualOperation(); 281 const GreaterEqualOperation();
279 bool foldNums(num left, num right) => left >= right; 282 bool foldNums(num left, num right) => left >= right;
280 apply(left, right) => left >= right; 283 apply(left, right) => left >= right;
281 } 284 }
282 285
283 class EqualsOperation implements BinaryOperation { 286 class EqualsOperation implements BinaryOperation {
284 final String name = '=='; 287 final String name = '==';
285 const EqualsOperation(); 288 const EqualsOperation();
286 Constant fold(Constant left, Constant right) { 289 ConstantValue fold(ConstantValue left, ConstantValue right) {
287 if (left.isNum && right.isNum) { 290 if (left.isNum && right.isNum) {
288 // Numbers need to be treated specially because: NaN != NaN, -0.0 == 0.0, 291 // Numbers need to be treated specially because: NaN != NaN, -0.0 == 0.0,
289 // and 1 == 1.0. 292 // and 1 == 1.0.
290 NumConstant leftNum = left; 293 NumConstantValue leftNum = left;
291 NumConstant rightNum = right; 294 NumConstantValue rightNum = right;
292 bool result = leftNum.value == rightNum.value; 295 bool result = leftNum.primitiveValue == rightNum.primitiveValue;
293 return DART_CONSTANT_SYSTEM.createBool(result); 296 return DART_CONSTANT_SYSTEM.createBool(result);
294 } 297 }
295 if (left.isConstructedObject) { 298 if (left.isConstructedObject) {
296 // Unless we know that the user-defined object does not implement the 299 // Unless we know that the user-defined object does not implement the
297 // equality operator we cannot fold here. 300 // equality operator we cannot fold here.
298 return null; 301 return null;
299 } 302 }
300 return DART_CONSTANT_SYSTEM.createBool(left == right); 303 return DART_CONSTANT_SYSTEM.createBool(left == right);
301 } 304 }
302 apply(left, right) => left == right; 305 apply(left, right) => left == right;
303 } 306 }
304 307
305 class IdentityOperation implements BinaryOperation { 308 class IdentityOperation implements BinaryOperation {
306 final String name = '==='; 309 final String name = '===';
307 const IdentityOperation(); 310 const IdentityOperation();
308 BoolConstant fold(Constant left, Constant right) { 311 BoolConstantValue fold(ConstantValue left, ConstantValue right) {
309 // In order to preserve runtime semantics which says that NaN !== NaN don't 312 // In order to preserve runtime semantics which says that NaN !== NaN don't
310 // constant fold NaN === NaN. Otherwise the output depends on inlined 313 // constant fold NaN === NaN. Otherwise the output depends on inlined
311 // variables and other optimizations. 314 // variables and other optimizations.
312 if (left.isNaN && right.isNaN) return null; 315 if (left.isNaN && right.isNaN) return null;
313 return DART_CONSTANT_SYSTEM.createBool(left == right); 316 return DART_CONSTANT_SYSTEM.createBool(left == right);
314 } 317 }
315 apply(left, right) => identical(left, right); 318 apply(left, right) => identical(left, right);
316 } 319 }
317 320
318 /** 321 /**
(...skipping 21 matching lines...) Expand all
340 final multiply = const MultiplyOperation(); 343 final multiply = const MultiplyOperation();
341 final negate = const NegateOperation(); 344 final negate = const NegateOperation();
342 final not = const NotOperation(); 345 final not = const NotOperation();
343 final shiftLeft = const ShiftLeftOperation(); 346 final shiftLeft = const ShiftLeftOperation();
344 final shiftRight = const ShiftRightOperation(); 347 final shiftRight = const ShiftRightOperation();
345 final subtract = const SubtractOperation(); 348 final subtract = const SubtractOperation();
346 final truncatingDivide = const TruncatingDivideOperation(); 349 final truncatingDivide = const TruncatingDivideOperation();
347 350
348 const DartConstantSystem(); 351 const DartConstantSystem();
349 352
350 IntConstant createInt(int i) => new IntConstant(i); 353 IntConstantValue createInt(int i) => new IntConstantValue(i);
351 DoubleConstant createDouble(double d) => new DoubleConstant(d); 354 DoubleConstantValue createDouble(double d) => new DoubleConstantValue(d);
352 StringConstant createString(DartString string) => new StringConstant(string); 355 StringConstantValue createString(DartString string) {
353 BoolConstant createBool(bool value) => new BoolConstant(value); 356 return new StringConstantValue(string);
354 NullConstant createNull() => new NullConstant(); 357 }
355 MapConstant createMap(Compiler compiler, InterfaceType type, 358 BoolConstantValue createBool(bool value) => new BoolConstantValue(value);
356 List<Constant> keys, List<Constant> values) { 359 NullConstantValue createNull() => new NullConstantValue();
357 return new MapConstant(type, keys, values); 360 MapConstantValue createMap(Compiler compiler,
361 InterfaceType type,
362 List<ConstantValue> keys,
363 List<ConstantValue> values) {
364 return new MapConstantValue(type, keys, values);
358 } 365 }
359 366
360 bool isInt(Constant constant) => constant.isInt; 367 bool isInt(ConstantValue constant) => constant.isInt;
361 bool isDouble(Constant constant) => constant.isDouble; 368 bool isDouble(ConstantValue constant) => constant.isDouble;
362 bool isString(Constant constant) => constant.isString; 369 bool isString(ConstantValue constant) => constant.isString;
363 bool isBool(Constant constant) => constant.isBool; 370 bool isBool(ConstantValue constant) => constant.isBool;
364 bool isNull(Constant constant) => constant.isNull; 371 bool isNull(ConstantValue constant) => constant.isNull;
365 372
366 bool isSubtype(Compiler compiler, DartType s, DartType t) { 373 bool isSubtype(Compiler compiler, DartType s, DartType t) {
367 return compiler.types.isSubtype(s, t); 374 return compiler.types.isSubtype(s, t);
368 } 375 }
369 } 376 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698