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

Side by Side Diff: lib/compiler/implementation/ssa/codegen.dart

Issue 10911006: Collect the types used in is-checks in the resolver phase. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: 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
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 6
7 final JavaScriptBackend backend; 7 final JavaScriptBackend backend;
8 8
9 SsaCodeGeneratorTask(JavaScriptBackend backend) 9 SsaCodeGeneratorTask(JavaScriptBackend backend)
10 : this.backend = backend, 10 : this.backend = backend,
(...skipping 2154 matching lines...) Expand 10 before | Expand all | Expand 10 after
2165 2165
2166 void checkFixedArray(HInstruction input) { 2166 void checkFixedArray(HInstruction input) {
2167 checkFieldExists(input, 'fixed\$length'); 2167 checkFieldExists(input, 'fixed\$length');
2168 } 2168 }
2169 2169
2170 void checkNull(HInstruction input) { 2170 void checkNull(HInstruction input) {
2171 use(input); 2171 use(input);
2172 push(new js.Binary('==', pop(), new js.LiteralNull())); 2172 push(new js.Binary('==', pop(), new js.LiteralNull()));
2173 } 2173 }
2174 2174
2175 void checkFunction(HInstruction input, Element element) { 2175 void checkFunction(HInstruction input, Type type) {
2176 checkTypeOf(input, '===', 'function'); 2176 checkTypeOf(input, '===', 'function');
2177 js.Expression functionTest = pop(); 2177 js.Expression functionTest = pop();
2178 checkObject(input, '==='); 2178 checkObject(input, '===');
2179 js.Expression objectTest = pop(); 2179 js.Expression objectTest = pop();
2180 checkType(input, element); 2180 checkType(input, type);
2181 push(new js.Binary('||', 2181 push(new js.Binary('||',
2182 functionTest, 2182 functionTest,
2183 new js.Binary('&&', objectTest, pop()))); 2183 new js.Binary('&&', objectTest, pop())));
2184 } 2184 }
2185 2185
2186 void checkType(HInstruction input, Element element, [bool negative = false]) { 2186 void checkType(HInstruction input, Type type, [bool negative = false]) {
2187 world.registerIsCheck(element); 2187 if (!world.universe.isChecks.contains(type)) {
2188 compiler.internalError("expected $type in isChecks",
2189 instruction: input);
2190 }
2191 Element element = type.element;
2188 use(input); 2192 use(input);
2189 js.PropertyAccess field = 2193 js.PropertyAccess field =
2190 new js.PropertyAccess.field(pop(), compiler.namer.operatorIs(element)); 2194 new js.PropertyAccess.field(pop(), compiler.namer.operatorIs(element));
2191 if (backend.emitter.nativeEmitter.requiresNativeIsCheck(element)) { 2195 if (backend.emitter.nativeEmitter.requiresNativeIsCheck(element)) {
2192 push(new js.Call(field, <js.Expression>[])); 2196 push(new js.Call(field, <js.Expression>[]));
2193 if (negative) push(new js.Prefix('!', pop())); 2197 if (negative) push(new js.Prefix('!', pop()));
2194 } else { 2198 } else {
2195 // We always negate at least once so that the result is boolified. 2199 // We always negate at least once so that the result is boolified.
2196 push(new js.Prefix('!', field)); 2200 push(new js.Prefix('!', field));
2197 // If the result is not negated, put another '!' in front. 2201 // If the result is not negated, put another '!' in front.
2198 if (!negative) push(new js.Prefix('!', pop())); 2202 if (!negative) push(new js.Prefix('!', pop()));
2199 } 2203 }
2200 } 2204 }
2201 2205
2202 void handleStringSupertypeCheck(HInstruction input, Element element) { 2206 void handleStringSupertypeCheck(HInstruction input, Type type) {
2203 // Make sure List and String don't share supertypes, otherwise we 2207 // Make sure List and String don't share supertypes, otherwise we
2204 // would need to check for List too. 2208 // would need to check for List too.
2205 assert(element !== compiler.listClass 2209 assert(type.element !== compiler.listClass
2206 && !Elements.isListSupertype(element, compiler)); 2210 && !Elements.isListSupertype(type.element, compiler));
2207 checkString(input, '==='); 2211 checkString(input, '===');
2208 js.Expression stringTest = pop(); 2212 js.Expression stringTest = pop();
2209 checkObject(input, '==='); 2213 checkObject(input, '===');
2210 js.Expression objectTest = pop(); 2214 js.Expression objectTest = pop();
2211 checkType(input, element); 2215 checkType(input, type);
2212 push(new js.Binary('||', 2216 push(new js.Binary('||',
2213 stringTest, 2217 stringTest,
2214 new js.Binary('&&', objectTest, pop()))); 2218 new js.Binary('&&', objectTest, pop())));
2215 } 2219 }
2216 2220
2217 void handleListOrSupertypeCheck(HInstruction input, Element element) { 2221 void handleListOrSupertypeCheck(HInstruction input, Type type) {
2218 // Make sure List and String don't share supertypes, otherwise we 2222 // Make sure List and String don't share supertypes, otherwise we
2219 // would need to check for String too. 2223 // would need to check for String too.
2220 assert(element !== compiler.stringClass 2224 assert(type.element !== compiler.stringClass
2221 && !Elements.isStringSupertype(element, compiler)); 2225 && !Elements.isStringSupertype(type.element, compiler));
2222 checkObject(input, '==='); 2226 checkObject(input, '===');
2223 js.Expression objectTest = pop(); 2227 js.Expression objectTest = pop();
2224 checkArray(input, '==='); 2228 checkArray(input, '===');
2225 js.Expression arrayTest = pop(); 2229 js.Expression arrayTest = pop();
2226 checkType(input, element); 2230 checkType(input, type);
2227 push(new js.Binary('&&', 2231 push(new js.Binary('&&',
2228 objectTest, 2232 objectTest,
2229 new js.Binary('||', arrayTest, pop()))); 2233 new js.Binary('||', arrayTest, pop())));
2230 } 2234 }
2231 2235
2232 void visitIs(HIs node) { 2236 void visitIs(HIs node) {
2233 Type type = node.typeExpression; 2237 Type type = node.typeExpression;
2234 Element element = type.element; 2238 Element element = type.element;
2235 if (element.kind === ElementKind.TYPE_VARIABLE) { 2239 if (element.kind === ElementKind.TYPE_VARIABLE) {
2236 compiler.unimplemented("visitIs for type variables", instruction: node); 2240 compiler.unimplemented("visitIs for type variables", instruction: node);
(...skipping 14 matching lines...) Expand all
2251 } else if (element == compiler.doubleClass) { 2255 } else if (element == compiler.doubleClass) {
2252 checkDouble(input, '==='); 2256 checkDouble(input, '===');
2253 attachLocationToLast(node); 2257 attachLocationToLast(node);
2254 } else if (element == compiler.numClass) { 2258 } else if (element == compiler.numClass) {
2255 checkNum(input, '==='); 2259 checkNum(input, '===');
2256 attachLocationToLast(node); 2260 attachLocationToLast(node);
2257 } else if (element == compiler.boolClass) { 2261 } else if (element == compiler.boolClass) {
2258 checkBool(input, '==='); 2262 checkBool(input, '===');
2259 attachLocationToLast(node); 2263 attachLocationToLast(node);
2260 } else if (element == compiler.functionClass) { 2264 } else if (element == compiler.functionClass) {
2261 checkFunction(input, element); 2265 checkFunction(input, type);
2262 attachLocationToLast(node); 2266 attachLocationToLast(node);
2263 } else if (element == compiler.intClass) { 2267 } else if (element == compiler.intClass) {
2264 checkNum(input, '==='); 2268 checkNum(input, '===');
2265 js.Expression numTest = pop(); 2269 js.Expression numTest = pop();
2266 checkInt(input, '==='); 2270 checkInt(input, '===');
2267 push(new js.Binary('&&', numTest, pop()), node); 2271 push(new js.Binary('&&', numTest, pop()), node);
2268 } else if (Elements.isStringSupertype(element, compiler)) { 2272 } else if (Elements.isStringSupertype(element, compiler)) {
2269 handleStringSupertypeCheck(input, element); 2273 handleStringSupertypeCheck(input, type);
2270 attachLocationToLast(node); 2274 attachLocationToLast(node);
2271 } else if (element === compiler.listClass 2275 } else if (element === compiler.listClass
2272 || Elements.isListSupertype(element, compiler)) { 2276 || Elements.isListSupertype(element, compiler)) {
2273 handleListOrSupertypeCheck(input, element); 2277 handleListOrSupertypeCheck(input, type);
2274 attachLocationToLast(node); 2278 attachLocationToLast(node);
2275 } else if (types[input].canBePrimitive() || types[input].canBeNull()) { 2279 } else if (types[input].canBePrimitive() || types[input].canBeNull()) {
2276 checkObject(input, '==='); 2280 checkObject(input, '===');
2277 js.Expression objectTest = pop(); 2281 js.Expression objectTest = pop();
2278 checkType(input, element); 2282 checkType(input, type);
2279 push(new js.Binary('&&', objectTest, pop()), node); 2283 push(new js.Binary('&&', objectTest, pop()), node);
2280 } else { 2284 } else {
2281 checkType(input, element); 2285 checkType(input, type);
2282 attachLocationToLast(node); 2286 attachLocationToLast(node);
2283 } 2287 }
2284 if (node.hasTypeInfo()) { 2288 if (node.hasTypeInfo()) {
2285 InterfaceType interfaceType = type; 2289 InterfaceType interfaceType = type;
2286 ClassElement cls = type.element; 2290 ClassElement cls = type.element;
2287 Link<Type> arguments = interfaceType.arguments; 2291 Link<Type> arguments = interfaceType.arguments;
2288 js.Expression result = pop(); 2292 js.Expression result = pop();
2289 for (TypeVariableType typeVariable in cls.typeVariables) { 2293 for (TypeVariableType typeVariable in cls.typeVariables) {
2290 use(node.typeInfoCall); 2294 use(node.typeInfoCall);
2291 // TODO(johnniwinther): Retrieve the type name properly and not through 2295 // TODO(johnniwinther): Retrieve the type name properly and not through
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
2329 const SourceString("listSuperNativeTypeCast"), 2333 const SourceString("listSuperNativeTypeCast"),
2330 "listSuperTypeCheck": 2334 "listSuperTypeCheck":
2331 const SourceString("listSuperTypeCast"), 2335 const SourceString("listSuperTypeCast"),
2332 "callTypeCheck": 2336 "callTypeCheck":
2333 const SourceString("callTypeCast"), 2337 const SourceString("callTypeCast"),
2334 "propertyTypeCheck": 2338 "propertyTypeCheck":
2335 const SourceString("propertyTypeCast") 2339 const SourceString("propertyTypeCast")
2336 }; 2340 };
2337 2341
2338 if (node.isChecked) { 2342 if (node.isChecked) {
2339 Element element = node.type.computeType(compiler).element; 2343 Type type = node.type.computeType(compiler);
2340 world.registerIsCheck(element); 2344 if (!world.universe.isChecks.contains(type)) {
2345 compiler.internalError("expected $type in isChecks",
2346 instruction: node);
2347 }
2348 Element element = type.element;
2341 SourceString helper; 2349 SourceString helper;
2342 String additionalArgument; 2350 String additionalArgument;
2343 bool nativeCheck = 2351 bool nativeCheck =
2344 backend.emitter.nativeEmitter.requiresNativeIsCheck(element); 2352 backend.emitter.nativeEmitter.requiresNativeIsCheck(element);
2345 2353
2346 if (node.isArgumentTypeCheck) { 2354 if (node.isArgumentTypeCheck) {
2347 if (element == compiler.intClass) { 2355 if (element == compiler.intClass) {
2348 checkInt(node.checkedInput, '!=='); 2356 checkInt(node.checkedInput, '!==');
2349 } else { 2357 } else {
2350 assert(element == compiler.numClass); 2358 assert(element == compiler.numClass);
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
2473 } else { 2481 } else {
2474 bailoutTarget = new js.VariableUse(namer.isolateBailoutAccess(element)); 2482 bailoutTarget = new js.VariableUse(namer.isolateBailoutAccess(element));
2475 } 2483 }
2476 js.Call call = new js.Call(bailoutTarget, arguments); 2484 js.Call call = new js.Call(bailoutTarget, arguments);
2477 attachLocation(call, guard); 2485 attachLocation(call, guard);
2478 return new js.Return(call); 2486 return new js.Return(call);
2479 } 2487 }
2480 2488
2481 void visitTypeGuard(HTypeGuard node) { 2489 void visitTypeGuard(HTypeGuard node) {
2482 HInstruction input = node.guarded; 2490 HInstruction input = node.guarded;
2483 Element indexingBehavior = compiler.jsIndexingBehaviorInterface; 2491 Type indexingBehavior =
2492 compiler.jsIndexingBehaviorInterface.computeType(compiler);
2484 if (node.isInteger(types)) { 2493 if (node.isInteger(types)) {
2485 // if (input is !int) bailout 2494 // if (input is !int) bailout
2486 checkInt(input, '!=='); 2495 checkInt(input, '!==');
2487 pushStatement(new js.If.then(pop(), bailout(node, 'Not an integer')), 2496 pushStatement(new js.If.then(pop(), bailout(node, 'Not an integer')),
2488 node); 2497 node);
2489 } else if (node.isNumber(types)) { 2498 } else if (node.isNumber(types)) {
2490 // if (input is !num) bailout 2499 // if (input is !num) bailout
2491 checkNum(input, '!=='); 2500 checkNum(input, '!==');
2492 pushStatement(new js.If.then(pop(), bailout(node, 'Not a number')), node); 2501 pushStatement(new js.If.then(pop(), bailout(node, 'Not a number')), node);
2493 } else if (node.isBoolean(types)) { 2502 } else if (node.isBoolean(types)) {
(...skipping 431 matching lines...) Expand 10 before | Expand all | Expand 10 after
2925 if (leftType.canBeNull() && rightType.canBeNull()) { 2934 if (leftType.canBeNull() && rightType.canBeNull()) {
2926 if (left.isConstantNull() || right.isConstantNull() || 2935 if (left.isConstantNull() || right.isConstantNull() ||
2927 (leftType.isPrimitive() && leftType == rightType)) { 2936 (leftType.isPrimitive() && leftType == rightType)) {
2928 return '=='; 2937 return '==';
2929 } 2938 }
2930 return null; 2939 return null;
2931 } else { 2940 } else {
2932 return '==='; 2941 return '===';
2933 } 2942 }
2934 } 2943 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698