| OLD | NEW |
| 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 Loading... |
| 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 // Make sure List and String don't share supertypes, otherwise we |
| 1141 // would need to check for List too. |
| 1142 assert(element !== compiler.listClass |
| 1143 && !Elements.isListSupertype(element, compiler)); |
| 1144 beginExpression(JSPrecedence.LOGICAL_OR_PRECEDENCE); |
| 1145 checkString(input, '==='); |
| 1146 buffer.add(' || '); |
| 1147 beginExpression(JSPrecedence.LOGICAL_AND_PRECEDENCE); |
| 1148 checkObject(input, '==='); |
| 1149 buffer.add(' && '); |
| 1150 checkType(input, element); |
| 1151 endExpression(JSPrecedence.LOGICAL_AND_PRECEDENCE); |
| 1152 endExpression(JSPrecedence.LOGICAL_OR_PRECEDENCE); |
| 1153 } |
| 1154 |
| 1155 void handleListOrSupertypeCheck(HInstruction input, Element element) { |
| 1156 // Make sure List and String don't share supertypes, otherwise we |
| 1157 // would need to check for String too. |
| 1158 assert(element !== compiler.stringClass |
| 1159 && !Elements.isStringSupertype(element, compiler)); |
| 1160 beginExpression(JSPrecedence.LOGICAL_AND_PRECEDENCE); |
| 1161 checkObject(input, '==='); |
| 1162 buffer.add(' && ('); |
| 1163 beginExpression(JSPrecedence.LOGICAL_OR_PRECEDENCE); |
| 1164 checkArray(input, '==='); |
| 1165 buffer.add(' || '); |
| 1166 checkType(input, element); |
| 1167 buffer.add(')'); |
| 1168 endExpression(JSPrecedence.LOGICAL_OR_PRECEDENCE); |
| 1169 endExpression(JSPrecedence.LOGICAL_AND_PRECEDENCE); |
| 1134 } | 1170 } |
| 1135 | 1171 |
| 1136 void visitIs(HIs node) { | 1172 void visitIs(HIs node) { |
| 1137 Element element = node.typeExpression; | 1173 Element element = node.typeExpression; |
| 1138 if (element.kind === ElementKind.TYPE_VARIABLE) { | 1174 if (element.kind === ElementKind.TYPE_VARIABLE) { |
| 1139 compiler.unimplemented("visitIs for type variables"); | 1175 compiler.unimplemented("visitIs for type variables"); |
| 1140 } | 1176 } |
| 1141 compiler.registerIsCheck(element); | 1177 compiler.registerIsCheck(element); |
| 1142 LibraryElement coreLibrary = compiler.coreLibrary; | 1178 LibraryElement coreLibrary = compiler.coreLibrary; |
| 1143 ClassElement objectClass = coreLibrary.find(const SourceString('Object')); | 1179 ClassElement objectClass = coreLibrary.find(const SourceString('Object')); |
| 1144 HInstruction input = node.expression; | 1180 HInstruction input = node.expression; |
| 1145 if (node.nullOk) { | 1181 if (node.nullOk) { |
| 1146 beginExpression(JSPrecedence.LOGICAL_OR_PRECEDENCE); | 1182 beginExpression(JSPrecedence.LOGICAL_OR_PRECEDENCE); |
| 1147 checkNull(input); | 1183 checkNull(input); |
| 1148 buffer.add(' || '); | 1184 buffer.add(' || '); |
| 1149 } | 1185 } |
| 1186 |
| 1150 if (element === objectClass || element === compiler.dynamicClass) { | 1187 if (element === objectClass || element === compiler.dynamicClass) { |
| 1151 // The constant folder also does this optimization, but we make | 1188 // The constant folder also does this optimization, but we make |
| 1152 // it safe by assuming it may have not run. | 1189 // it safe by assuming it may have not run. |
| 1153 buffer.add('true'); | 1190 buffer.add('true'); |
| 1154 } else if (element == compiler.stringClass) { | 1191 } else if (element == compiler.stringClass) { |
| 1155 checkString(input, '==='); | 1192 checkString(input, '==='); |
| 1156 } else if (element == compiler.doubleClass) { | 1193 } else if (element == compiler.doubleClass) { |
| 1157 checkDouble(input, '==='); | 1194 checkDouble(input, '==='); |
| 1158 } else if (element == compiler.numClass) { | 1195 } else if (element == compiler.numClass) { |
| 1159 checkNum(input, '==='); | 1196 checkNum(input, '==='); |
| 1160 } else if (element == compiler.boolClass) { | 1197 } else if (element == compiler.boolClass) { |
| 1161 checkBool(input, '==='); | 1198 checkBool(input, '==='); |
| 1162 } else if (element == compiler.functionClass) { | 1199 } else if (element == compiler.functionClass) { |
| 1163 checkFunction(input, element); | 1200 checkFunction(input, element); |
| 1164 } else if (element == compiler.intClass) { | 1201 } else if (element == compiler.intClass) { |
| 1165 beginExpression(JSPrecedence.LOGICAL_AND_PRECEDENCE); | 1202 beginExpression(JSPrecedence.LOGICAL_AND_PRECEDENCE); |
| 1166 checkNum(input, '==='); | 1203 checkNum(input, '==='); |
| 1167 buffer.add(' && '); | 1204 buffer.add(' && '); |
| 1168 checkInt(input, '==='); | 1205 checkInt(input, '==='); |
| 1169 endExpression(JSPrecedence.LOGICAL_AND_PRECEDENCE); | 1206 endExpression(JSPrecedence.LOGICAL_AND_PRECEDENCE); |
| 1207 } else if (Elements.isStringSupertype(element, compiler)) { |
| 1208 handleStringSupertypeCheck(input, element); |
| 1209 } else if (element === compiler.listClass |
| 1210 || Elements.isListSupertype(element, compiler)) { |
| 1211 handleListOrSupertypeCheck(input, element); |
| 1170 } else { | 1212 } else { |
| 1171 beginExpression(JSPrecedence.LOGICAL_AND_PRECEDENCE); | 1213 beginExpression(JSPrecedence.LOGICAL_AND_PRECEDENCE); |
| 1172 if (Elements.isStringSupertype(element, compiler)) { | |
| 1173 checkString(input, '==='); | |
| 1174 buffer.add(' || '); | |
| 1175 } | |
| 1176 checkObject(input, '==='); | 1214 checkObject(input, '==='); |
| 1177 buffer.add(' && '); | 1215 buffer.add(' && '); |
| 1178 int precedence = JSPrecedence.PREFIX_PRECEDENCE; | 1216 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); | 1217 endExpression(JSPrecedence.LOGICAL_AND_PRECEDENCE); |
| 1218 } | 1218 } |
| 1219 |
| 1219 if (node.nullOk) { | 1220 if (node.nullOk) { |
| 1220 endExpression(JSPrecedence.LOGICAL_OR_PRECEDENCE); | 1221 endExpression(JSPrecedence.LOGICAL_OR_PRECEDENCE); |
| 1221 } | 1222 } |
| 1222 } | 1223 } |
| 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 compiler.registerStaticUse(compiler.findHelper( | |
| 1240 const SourceString('dynamicIsCheck'))); | |
| 1241 return true; | |
| 1242 } | |
| 1243 } | 1224 } |
| 1244 | 1225 |
| 1245 class SsaOptimizedCodeGenerator extends SsaCodeGenerator { | 1226 class SsaOptimizedCodeGenerator extends SsaCodeGenerator { |
| 1246 final List<HTypeGuard> guards; | 1227 final List<HTypeGuard> guards; |
| 1247 int state = 0; | 1228 int state = 0; |
| 1248 | 1229 |
| 1249 SsaOptimizedCodeGenerator(compiler, work, parameters, parameterNames) | 1230 SsaOptimizedCodeGenerator(compiler, work, parameters, parameterNames) |
| 1250 : super(compiler, work, parameters, parameterNames), | 1231 : super(compiler, work, parameters, parameterNames), |
| 1251 guards = <HTypeGuard>[]; | 1232 guards = <HTypeGuard>[]; |
| 1252 | 1233 |
| (...skipping 334 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1587 startBailoutSwitch(); | 1568 startBailoutSwitch(); |
| 1588 } | 1569 } |
| 1589 } | 1570 } |
| 1590 | 1571 |
| 1591 void endElse(HIf node) { | 1572 void endElse(HIf node) { |
| 1592 if (node.elseBlock.hasBailouts()) { | 1573 if (node.elseBlock.hasBailouts()) { |
| 1593 endBailoutSwitch(); | 1574 endBailoutSwitch(); |
| 1594 } | 1575 } |
| 1595 } | 1576 } |
| 1596 } | 1577 } |
| OLD | NEW |