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

Side by Side Diff: frog/leg/ssa/codegen.dart

Issue 9773026: Add a method on native classes for is checks. Reduces the code for generating is checks. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 9 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
« frog/leg/native_emitter.dart ('K') | « frog/leg/native_emitter.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 class SsaCodeGeneratorTask extends CompilerTask { 5 class SsaCodeGeneratorTask extends CompilerTask {
6 SsaCodeGeneratorTask(Compiler compiler) : super(compiler); 6 SsaCodeGeneratorTask(Compiler compiler) : super(compiler);
7 String get name() => 'SSA code generator'; 7 String get name() => 'SSA code generator';
8 8
9 String generate(WorkItem work, HGraph graph) { 9 String generate(WorkItem work, HGraph graph) {
10 return measure(() { 10 return measure(() {
(...skipping 1109 matching lines...) Expand 10 before | Expand all | Expand 10 after
1120 buffer.add(" || "); 1120 buffer.add(" || ");
1121 beginExpression(JSPrecedence.LOGICAL_AND_PRECEDENCE); 1121 beginExpression(JSPrecedence.LOGICAL_AND_PRECEDENCE);
1122 checkObject(input, '==='); 1122 checkObject(input, '===');
1123 buffer.add(" && "); 1123 buffer.add(" && ");
1124 checkType(input, element); 1124 checkType(input, element);
1125 endExpression(JSPrecedence.LOGICAL_AND_PRECEDENCE); 1125 endExpression(JSPrecedence.LOGICAL_AND_PRECEDENCE);
1126 endExpression(JSPrecedence.LOGICAL_OR_PRECEDENCE); 1126 endExpression(JSPrecedence.LOGICAL_OR_PRECEDENCE);
1127 } 1127 }
1128 1128
1129 void checkType(HInstruction input, Element element) { 1129 void checkType(HInstruction input, Element element) {
1130 buffer.add('!!'); 1130 bool requiresNativeIsCheck =
1131 compiler.emitter.nativeEmitter.requiresNativeIsCheck(element);
1132 if (!requiresNativeIsCheck) buffer.add('!!');
1131 use(input, JSPrecedence.MEMBER_PRECEDENCE); 1133 use(input, JSPrecedence.MEMBER_PRECEDENCE);
1132 buffer.add('.'); 1134 buffer.add('.');
1133 buffer.add(compiler.namer.operatorIs(element)); 1135 buffer.add(compiler.namer.operatorIs(element));
1136 if (requiresNativeIsCheck) buffer.add('()');
1137 }
1138
1139 void handleStringSupertypeCheck(HInstruction input, Element element) {
1140 assert(element !== compiler.listClass
1141 && Elements.isListSupertype(element, compiler));
floitsch 2012/03/26 20:32:59 I don't understand this assert.
ngeoffray 2012/03/27 10:48:53 It's to make sure List and String don't share supe
floitsch 2012/03/28 21:19:01 You also added a "!" ;)
ngeoffray 2012/03/29 08:27:16 Busted :)
1142 beginExpression(JSPrecedence.LOGICAL_OR_PRECEDENCE);
1143 checkString(input, '===');
1144 buffer.add(' || ');
1145 beginExpression(JSPrecedence.LOGICAL_AND_PRECEDENCE);
1146 checkObject(input, '===');
1147 buffer.add(' && ');
1148 checkType(input, element);
1149 endExpression(JSPrecedence.LOGICAL_AND_PRECEDENCE);
1150 endExpression(JSPrecedence.LOGICAL_OR_PRECEDENCE);
1151 }
1152
1153 void handleListOrSupertypeCheck(HInstruction input, Element element) {
1154 beginExpression(JSPrecedence.LOGICAL_AND_PRECEDENCE);
1155 checkObject(input, '===');
1156 buffer.add(' && (');
1157 beginExpression(JSPrecedence.LOGICAL_OR_PRECEDENCE);
1158 checkArray(input, '===');
1159 buffer.add(' || ');
1160 checkType(input, element);
1161 buffer.add(')');
1162 endExpression(JSPrecedence.LOGICAL_OR_PRECEDENCE);
1163 endExpression(JSPrecedence.LOGICAL_AND_PRECEDENCE);
1134 } 1164 }
1135 1165
1136 void visitIs(HIs node) { 1166 void visitIs(HIs node) {
1137 Element element = node.typeExpression; 1167 Element element = node.typeExpression;
1138 if (element.kind === ElementKind.TYPE_VARIABLE) { 1168 if (element.kind === ElementKind.TYPE_VARIABLE) {
1139 compiler.unimplemented("visitIs for type variables"); 1169 compiler.unimplemented("visitIs for type variables");
1140 } 1170 }
1141 compiler.registerIsCheck(element); 1171 compiler.registerIsCheck(element);
1142 LibraryElement coreLibrary = compiler.coreLibrary; 1172 LibraryElement coreLibrary = compiler.coreLibrary;
1143 ClassElement objectClass = coreLibrary.find(const SourceString('Object')); 1173 ClassElement objectClass = coreLibrary.find(const SourceString('Object'));
1144 HInstruction input = node.expression; 1174 HInstruction input = node.expression;
1145 if (node.nullOk) { 1175 if (node.nullOk) {
1146 beginExpression(JSPrecedence.LOGICAL_OR_PRECEDENCE); 1176 beginExpression(JSPrecedence.LOGICAL_OR_PRECEDENCE);
1147 checkNull(input); 1177 checkNull(input);
1148 buffer.add(' || '); 1178 buffer.add(' || ');
1149 } 1179 }
1180
1150 if (element === objectClass || element === compiler.dynamicClass) { 1181 if (element === objectClass || element === compiler.dynamicClass) {
1151 // The constant folder also does this optimization, but we make 1182 // The constant folder also does this optimization, but we make
1152 // it safe by assuming it may have not run. 1183 // it safe by assuming it may have not run.
1153 buffer.add('true'); 1184 buffer.add('true');
1154 } else if (element == compiler.stringClass) { 1185 } else if (element == compiler.stringClass) {
1155 checkString(input, '==='); 1186 checkString(input, '===');
1156 } else if (element == compiler.doubleClass) { 1187 } else if (element == compiler.doubleClass) {
1157 checkDouble(input, '==='); 1188 checkDouble(input, '===');
1158 } else if (element == compiler.numClass) { 1189 } else if (element == compiler.numClass) {
1159 checkNum(input, '==='); 1190 checkNum(input, '===');
1160 } else if (element == compiler.boolClass) { 1191 } else if (element == compiler.boolClass) {
1161 checkBool(input, '==='); 1192 checkBool(input, '===');
1162 } else if (element == compiler.functionClass) { 1193 } else if (element == compiler.functionClass) {
1163 checkFunction(input, element); 1194 checkFunction(input, element);
1164 } else if (element == compiler.intClass) { 1195 } else if (element == compiler.intClass) {
1165 beginExpression(JSPrecedence.LOGICAL_AND_PRECEDENCE); 1196 beginExpression(JSPrecedence.LOGICAL_AND_PRECEDENCE);
1166 checkNum(input, '==='); 1197 checkNum(input, '===');
1167 buffer.add(' && '); 1198 buffer.add(' && ');
1168 checkInt(input, '==='); 1199 checkInt(input, '===');
1169 endExpression(JSPrecedence.LOGICAL_AND_PRECEDENCE); 1200 endExpression(JSPrecedence.LOGICAL_AND_PRECEDENCE);
1201 } else if (Elements.isStringSupertype(element, compiler)) {
1202 handleStringSupertypeCheck(input, element);
1203 } else if (element === compiler.listClass
1204 || Elements.isListSupertype(element, compiler)) {
1205 handleListOrSupertypeCheck(input, element);
1170 } else { 1206 } else {
1171 beginExpression(JSPrecedence.LOGICAL_AND_PRECEDENCE); 1207 beginExpression(JSPrecedence.LOGICAL_AND_PRECEDENCE);
1172 if (Elements.isStringSupertype(element, compiler)) {
1173 checkString(input, '===');
1174 buffer.add(' || ');
1175 }
1176 checkObject(input, '==='); 1208 checkObject(input, '===');
1177 buffer.add(' && '); 1209 buffer.add(' && ');
1178 int precedence = JSPrecedence.PREFIX_PRECEDENCE; 1210 checkType(input, element);
1179 bool endParen = false;
1180 if (element === compiler.listClass
1181 || Elements.isListSupertype(element, compiler)) {
1182 buffer.add("(");
1183 endParen = true;
1184 beginExpression(JSPrecedence.LOGICAL_OR_PRECEDENCE);
1185 checkArray(input, '===');
1186 buffer.add(' || ');
1187 precedence = JSPrecedence.LOGICAL_OR_PRECEDENCE;
1188 } else if (element.isClass() && (element.dynamic.isNative()
1189 || isSupertypeOfNativeClass(element))) {
1190 buffer.add("(");
1191 endParen = true;
1192 } else {
1193 beginExpression(precedence);
1194 }
1195 checkType(input, node.typeExpression);
1196 if (element.isClass() && (element.dynamic.isNative()
1197 || isSupertypeOfNativeClass(element))) {
1198 buffer.add(' || ');
1199 beginExpression(JSPrecedence.LOGICAL_AND_PRECEDENCE);
1200 // First check if the object is not a Dart object. If the
1201 // object is a Dart object, we know the property check was
1202 // sufficient.
1203 compiler.registerIsCheck(objectClass);
1204 buffer.add('!');
1205 use(input, JSPrecedence.MEMBER_PRECEDENCE);
1206 buffer.add('.');
1207 buffer.add(compiler.namer.operatorIs(objectClass));
1208 buffer.add(' && ');
1209 buffer.add(compiler.emitter.nativeEmitter.dynamicIsCheckName);
1210 buffer.add('(');
1211 use(input, JSPrecedence.MEMBER_PRECEDENCE);
1212 buffer.add(", '${compiler.namer.operatorIs(node.typeExpression)}')");
1213 endExpression(JSPrecedence.LOGICAL_AND_PRECEDENCE);
1214 }
1215 endExpression(precedence);
1216 if (endParen) buffer.add(')');
1217 endExpression(JSPrecedence.LOGICAL_AND_PRECEDENCE); 1211 endExpression(JSPrecedence.LOGICAL_AND_PRECEDENCE);
1218 } 1212 }
1213
1219 if (node.nullOk) { 1214 if (node.nullOk) {
1220 endExpression(JSPrecedence.LOGICAL_OR_PRECEDENCE); 1215 endExpression(JSPrecedence.LOGICAL_OR_PRECEDENCE);
1221 } 1216 }
1222 } 1217 }
1223
1224 bool isSupertypeOfNativeClass(Element element) {
1225 if (element.isTypeVariable()) {
1226 compiler.cancel("Is check for type variable", element: work.element);
1227 return false;
1228 }
1229 if (element.computeType(compiler) is FunctionType) return false;
1230
1231 if (!element.isClass()) {
1232 compiler.cancel("Is check does not handle element", element: element);
1233 return false;
1234 }
1235
1236 List<ClassElement> subtypes =
1237 compiler.emitter.nativeEmitter.subtypes[element];
1238 if (subtypes === null) return false;
1239 return true;
1240 }
1241 } 1218 }
1242 1219
1243 class SsaOptimizedCodeGenerator extends SsaCodeGenerator { 1220 class SsaOptimizedCodeGenerator extends SsaCodeGenerator {
1244 final List<HTypeGuard> guards; 1221 final List<HTypeGuard> guards;
1245 int state = 0; 1222 int state = 0;
1246 1223
1247 SsaOptimizedCodeGenerator(compiler, work, parameters, parameterNames) 1224 SsaOptimizedCodeGenerator(compiler, work, parameters, parameterNames)
1248 : super(compiler, work, parameters, parameterNames), 1225 : super(compiler, work, parameters, parameterNames),
1249 guards = <HTypeGuard>[]; 1226 guards = <HTypeGuard>[];
1250 1227
(...skipping 334 matching lines...) Expand 10 before | Expand all | Expand 10 after
1585 startBailoutSwitch(); 1562 startBailoutSwitch();
1586 } 1563 }
1587 } 1564 }
1588 1565
1589 void endElse(HIf node) { 1566 void endElse(HIf node) {
1590 if (node.elseBlock.hasBailouts()) { 1567 if (node.elseBlock.hasBailouts()) {
1591 endBailoutSwitch(); 1568 endBailoutSwitch();
1592 } 1569 }
1593 } 1570 }
1594 } 1571 }
OLDNEW
« frog/leg/native_emitter.dart ('K') | « frog/leg/native_emitter.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698