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

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

Issue 681543002: Constant fold codeUnitAt via constant system (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Add language test Created 6 years, 1 month 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 = '~';
(...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after
311 BoolConstantValue fold(ConstantValue left, ConstantValue right) { 311 BoolConstantValue fold(ConstantValue left, ConstantValue right) {
312 // 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
313 // constant fold NaN === NaN. Otherwise the output depends on inlined 313 // constant fold NaN === NaN. Otherwise the output depends on inlined
314 // variables and other optimizations. 314 // variables and other optimizations.
315 if (left.isNaN && right.isNaN) return null; 315 if (left.isNaN && right.isNaN) return null;
316 return DART_CONSTANT_SYSTEM.createBool(left == right); 316 return DART_CONSTANT_SYSTEM.createBool(left == right);
317 } 317 }
318 apply(left, right) => identical(left, right); 318 apply(left, right) => identical(left, right);
319 } 319 }
320 320
321 abstract class CodeUnitAtOperation implements BinaryOperation {
322 final String name = 'charCodeAt';
323 const CodeUnitAtOperation();
324 apply(left, right) => left.codeUnitAt(right);
325 }
326
327 class CodeUnitAtConstantOperation extends CodeUnitAtOperation {
328 const CodeUnitAtConstantOperation();
329 ConstantValue fold(ConstantValue left, ConstantValue right) {
330 // 'a'.codeUnitAt(0) is not a constant expression.
331 return null;
332 }
333 }
334
335 class CodeUnitAtRuntimeOperation extends CodeUnitAtOperation {
336 const CodeUnitAtRuntimeOperation();
337 IntConstantValue fold(ConstantValue left, ConstantValue right) {
338 if (left.isString && right.isInt) {
339 DartString dartString = left.primitiveValue;
340 int index = right.primitiveValue;
341 if (index < 0 || index >= dartString.length) return null;
342 String string = dartString.slowToString();
343 int value = string.codeUnitAt(index);
344 return DART_CONSTANT_SYSTEM.createInt(value);
345 }
346 return null;
347 }
348 }
349
321 /** 350 /**
322 * A constant system implementing the Dart semantics. This system relies on 351 * A constant system implementing the Dart semantics. This system relies on
323 * the underlying runtime-system. That is, if dart2js is run in an environment 352 * the underlying runtime-system. That is, if dart2js is run in an environment
324 * that doesn't correctly implement Dart's semantics this constant system will 353 * that doesn't correctly implement Dart's semantics this constant system will
325 * not return the correct values. 354 * not return the correct values.
326 */ 355 */
327 class DartConstantSystem extends ConstantSystem { 356 class DartConstantSystem extends ConstantSystem {
328 final add = const AddOperation(); 357 final add = const AddOperation();
329 final bitAnd = const BitAndOperation(); 358 final bitAnd = const BitAndOperation();
330 final bitNot = const BitNotOperation(); 359 final bitNot = const BitNotOperation();
331 final bitOr = const BitOrOperation(); 360 final bitOr = const BitOrOperation();
332 final bitXor = const BitXorOperation(); 361 final bitXor = const BitXorOperation();
333 final booleanAnd = const BooleanAndOperation(); 362 final booleanAnd = const BooleanAndOperation();
334 final booleanOr = const BooleanOrOperation(); 363 final booleanOr = const BooleanOrOperation();
335 final divide = const DivideOperation(); 364 final divide = const DivideOperation();
336 final equal = const EqualsOperation(); 365 final equal = const EqualsOperation();
337 final greaterEqual = const GreaterEqualOperation(); 366 final greaterEqual = const GreaterEqualOperation();
338 final greater = const GreaterOperation(); 367 final greater = const GreaterOperation();
339 final identity = const IdentityOperation(); 368 final identity = const IdentityOperation();
340 final lessEqual = const LessEqualOperation(); 369 final lessEqual = const LessEqualOperation();
341 final less = const LessOperation(); 370 final less = const LessOperation();
342 final modulo = const ModuloOperation(); 371 final modulo = const ModuloOperation();
343 final multiply = const MultiplyOperation(); 372 final multiply = const MultiplyOperation();
344 final negate = const NegateOperation(); 373 final negate = const NegateOperation();
345 final not = const NotOperation(); 374 final not = const NotOperation();
346 final shiftLeft = const ShiftLeftOperation(); 375 final shiftLeft = const ShiftLeftOperation();
347 final shiftRight = const ShiftRightOperation(); 376 final shiftRight = const ShiftRightOperation();
348 final subtract = const SubtractOperation(); 377 final subtract = const SubtractOperation();
349 final truncatingDivide = const TruncatingDivideOperation(); 378 final truncatingDivide = const TruncatingDivideOperation();
379 final codeUnitAt = const CodeUnitAtConstantOperation();
350 380
351 const DartConstantSystem(); 381 const DartConstantSystem();
352 382
353 IntConstantValue createInt(int i) => new IntConstantValue(i); 383 IntConstantValue createInt(int i) => new IntConstantValue(i);
354 DoubleConstantValue createDouble(double d) => new DoubleConstantValue(d); 384 DoubleConstantValue createDouble(double d) => new DoubleConstantValue(d);
355 StringConstantValue createString(DartString string) { 385 StringConstantValue createString(DartString string) {
356 return new StringConstantValue(string); 386 return new StringConstantValue(string);
357 } 387 }
358 BoolConstantValue createBool(bool value) => new BoolConstantValue(value); 388 BoolConstantValue createBool(bool value) => new BoolConstantValue(value);
359 NullConstantValue createNull() => new NullConstantValue(); 389 NullConstantValue createNull() => new NullConstantValue();
360 MapConstantValue createMap(Compiler compiler, 390 MapConstantValue createMap(Compiler compiler,
361 InterfaceType type, 391 InterfaceType type,
362 List<ConstantValue> keys, 392 List<ConstantValue> keys,
363 List<ConstantValue> values) { 393 List<ConstantValue> values) {
364 return new MapConstantValue(type, keys, values); 394 return new MapConstantValue(type, keys, values);
365 } 395 }
366 396
367 bool isInt(ConstantValue constant) => constant.isInt; 397 bool isInt(ConstantValue constant) => constant.isInt;
368 bool isDouble(ConstantValue constant) => constant.isDouble; 398 bool isDouble(ConstantValue constant) => constant.isDouble;
369 bool isString(ConstantValue constant) => constant.isString; 399 bool isString(ConstantValue constant) => constant.isString;
370 bool isBool(ConstantValue constant) => constant.isBool; 400 bool isBool(ConstantValue constant) => constant.isBool;
371 bool isNull(ConstantValue constant) => constant.isNull; 401 bool isNull(ConstantValue constant) => constant.isNull;
372 402
373 bool isSubtype(Compiler compiler, DartType s, DartType t) { 403 bool isSubtype(Compiler compiler, DartType s, DartType t) {
374 return compiler.types.isSubtype(s, t); 404 return compiler.types.isSubtype(s, t);
375 } 405 }
376 } 406 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698