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

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 Element element = type.element;
2188 use(input); 2188 use(input);
2189 js.PropertyAccess field = 2189 js.PropertyAccess field =
2190 new js.PropertyAccess.field(pop(), compiler.namer.operatorIs(element)); 2190 new js.PropertyAccess.field(pop(), compiler.namer.operatorIs(element));
2191 if (backend.emitter.nativeEmitter.requiresNativeIsCheck(element)) { 2191 if (backend.emitter.nativeEmitter.requiresNativeIsCheck(element)) {
2192 push(new js.Call(field, <js.Expression>[])); 2192 push(new js.Call(field, <js.Expression>[]));
2193 if (negative) push(new js.Prefix('!', pop())); 2193 if (negative) push(new js.Prefix('!', pop()));
2194 } else { 2194 } else {
2195 // We always negate at least once so that the result is boolified. 2195 // We always negate at least once so that the result is boolified.
2196 push(new js.Prefix('!', field)); 2196 push(new js.Prefix('!', field));
2197 // If the result is not negated, put another '!' in front. 2197 // If the result is not negated, put another '!' in front.
2198 if (!negative) push(new js.Prefix('!', pop())); 2198 if (!negative) push(new js.Prefix('!', pop()));
2199 } 2199 }
2200 } 2200 }
2201 2201
2202 void handleStringSupertypeCheck(HInstruction input, Element element) { 2202 void handleStringSupertypeCheck(HInstruction input, Type type) {
2203 // Make sure List and String don't share supertypes, otherwise we 2203 // Make sure List and String don't share supertypes, otherwise we
2204 // would need to check for List too. 2204 // would need to check for List too.
2205 assert(element !== compiler.listClass 2205 assert(type.element !== compiler.listClass
2206 && !Elements.isListSupertype(element, compiler)); 2206 && !Elements.isListSupertype(type.element, compiler));
2207 checkString(input, '==='); 2207 checkString(input, '===');
2208 js.Expression stringTest = pop(); 2208 js.Expression stringTest = pop();
2209 checkObject(input, '==='); 2209 checkObject(input, '===');
2210 js.Expression objectTest = pop(); 2210 js.Expression objectTest = pop();
2211 checkType(input, element); 2211 checkType(input, type);
2212 push(new js.Binary('||', 2212 push(new js.Binary('||',
2213 stringTest, 2213 stringTest,
2214 new js.Binary('&&', objectTest, pop()))); 2214 new js.Binary('&&', objectTest, pop())));
2215 } 2215 }
2216 2216
2217 void handleListOrSupertypeCheck(HInstruction input, Element element) { 2217 void handleListOrSupertypeCheck(HInstruction input, Type type) {
2218 // Make sure List and String don't share supertypes, otherwise we 2218 // Make sure List and String don't share supertypes, otherwise we
2219 // would need to check for String too. 2219 // would need to check for String too.
2220 assert(element !== compiler.stringClass 2220 assert(type.element !== compiler.stringClass
2221 && !Elements.isStringSupertype(element, compiler)); 2221 && !Elements.isStringSupertype(type.element, compiler));
2222 checkObject(input, '==='); 2222 checkObject(input, '===');
2223 js.Expression objectTest = pop(); 2223 js.Expression objectTest = pop();
2224 checkArray(input, '==='); 2224 checkArray(input, '===');
2225 js.Expression arrayTest = pop(); 2225 js.Expression arrayTest = pop();
2226 checkType(input, element); 2226 checkType(input, type);
2227 push(new js.Binary('&&', 2227 push(new js.Binary('&&',
2228 objectTest, 2228 objectTest,
2229 new js.Binary('||', arrayTest, pop()))); 2229 new js.Binary('||', arrayTest, pop())));
2230 } 2230 }
2231 2231
2232 void visitIs(HIs node) { 2232 void visitIs(HIs node) {
2233 Type type = node.typeExpression; 2233 Type type = node.typeExpression;
2234 Element element = type.element; 2234 Element element = type.element;
2235 if (element.kind === ElementKind.TYPE_VARIABLE) { 2235 if (element.kind === ElementKind.TYPE_VARIABLE) {
2236 compiler.unimplemented("visitIs for type variables", instruction: node); 2236 compiler.unimplemented("visitIs for type variables", instruction: node);
(...skipping 14 matching lines...) Expand all
2251 } else if (element == compiler.doubleClass) { 2251 } else if (element == compiler.doubleClass) {
2252 checkDouble(input, '==='); 2252 checkDouble(input, '===');
2253 attachLocationToLast(node); 2253 attachLocationToLast(node);
2254 } else if (element == compiler.numClass) { 2254 } else if (element == compiler.numClass) {
2255 checkNum(input, '==='); 2255 checkNum(input, '===');
2256 attachLocationToLast(node); 2256 attachLocationToLast(node);
2257 } else if (element == compiler.boolClass) { 2257 } else if (element == compiler.boolClass) {
2258 checkBool(input, '==='); 2258 checkBool(input, '===');
2259 attachLocationToLast(node); 2259 attachLocationToLast(node);
2260 } else if (element == compiler.functionClass) { 2260 } else if (element == compiler.functionClass) {
2261 checkFunction(input, element); 2261 checkFunction(input, type);
2262 attachLocationToLast(node); 2262 attachLocationToLast(node);
2263 } else if (element == compiler.intClass) { 2263 } else if (element == compiler.intClass) {
2264 checkNum(input, '==='); 2264 checkNum(input, '===');
2265 js.Expression numTest = pop(); 2265 js.Expression numTest = pop();
2266 checkInt(input, '==='); 2266 checkInt(input, '===');
2267 push(new js.Binary('&&', numTest, pop()), node); 2267 push(new js.Binary('&&', numTest, pop()), node);
2268 } else if (Elements.isStringSupertype(element, compiler)) { 2268 } else if (Elements.isStringSupertype(element, compiler)) {
2269 handleStringSupertypeCheck(input, element); 2269 handleStringSupertypeCheck(input, type);
2270 attachLocationToLast(node); 2270 attachLocationToLast(node);
2271 } else if (element === compiler.listClass 2271 } else if (element === compiler.listClass
2272 || Elements.isListSupertype(element, compiler)) { 2272 || Elements.isListSupertype(element, compiler)) {
2273 handleListOrSupertypeCheck(input, element); 2273 handleListOrSupertypeCheck(input, type);
2274 attachLocationToLast(node); 2274 attachLocationToLast(node);
2275 } else if (types[input].canBePrimitive() || types[input].canBeNull()) { 2275 } else if (types[input].canBePrimitive() || types[input].canBeNull()) {
2276 checkObject(input, '==='); 2276 checkObject(input, '===');
2277 js.Expression objectTest = pop(); 2277 js.Expression objectTest = pop();
2278 checkType(input, element); 2278 checkType(input, type);
2279 push(new js.Binary('&&', objectTest, pop()), node); 2279 push(new js.Binary('&&', objectTest, pop()), node);
2280 } else { 2280 } else {
2281 checkType(input, element); 2281 checkType(input, type);
2282 attachLocationToLast(node); 2282 attachLocationToLast(node);
2283 } 2283 }
2284 if (node.hasTypeInfo()) { 2284 if (node.hasTypeInfo()) {
2285 InterfaceType interfaceType = type; 2285 InterfaceType interfaceType = type;
2286 ClassElement cls = type.element; 2286 ClassElement cls = type.element;
2287 Link<Type> arguments = interfaceType.arguments; 2287 Link<Type> arguments = interfaceType.arguments;
2288 js.Expression result = pop(); 2288 js.Expression result = pop();
2289 for (TypeVariableType typeVariable in cls.typeVariables) { 2289 for (TypeVariableType typeVariable in cls.typeVariables) {
2290 use(node.typeInfoCall); 2290 use(node.typeInfoCall);
2291 // TODO(johnniwinther): Retrieve the type name properly and not through 2291 // 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"), 2329 const SourceString("listSuperNativeTypeCast"),
2330 "listSuperTypeCheck": 2330 "listSuperTypeCheck":
2331 const SourceString("listSuperTypeCast"), 2331 const SourceString("listSuperTypeCast"),
2332 "callTypeCheck": 2332 "callTypeCheck":
2333 const SourceString("callTypeCast"), 2333 const SourceString("callTypeCast"),
2334 "propertyTypeCheck": 2334 "propertyTypeCheck":
2335 const SourceString("propertyTypeCast") 2335 const SourceString("propertyTypeCast")
2336 }; 2336 };
2337 2337
2338 if (node.isChecked) { 2338 if (node.isChecked) {
2339 Element element = node.type.computeType(compiler).element; 2339 Type type = node.type.computeType(compiler);
2340 world.registerIsCheck(element); 2340 Element element = type.element;
2341 SourceString helper; 2341 SourceString helper;
2342 String additionalArgument; 2342 String additionalArgument;
2343 bool nativeCheck = 2343 bool nativeCheck =
2344 backend.emitter.nativeEmitter.requiresNativeIsCheck(element); 2344 backend.emitter.nativeEmitter.requiresNativeIsCheck(element);
2345 2345
2346 if (node.isArgumentTypeCheck) { 2346 if (node.isArgumentTypeCheck) {
2347 if (element == compiler.intClass) { 2347 if (element == compiler.intClass) {
2348 checkInt(node.checkedInput, '!=='); 2348 checkInt(node.checkedInput, '!==');
2349 } else { 2349 } else {
2350 assert(element == compiler.numClass); 2350 assert(element == compiler.numClass);
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
2473 } else { 2473 } else {
2474 bailoutTarget = new js.VariableUse(namer.isolateBailoutAccess(element)); 2474 bailoutTarget = new js.VariableUse(namer.isolateBailoutAccess(element));
2475 } 2475 }
2476 js.Call call = new js.Call(bailoutTarget, arguments); 2476 js.Call call = new js.Call(bailoutTarget, arguments);
2477 attachLocation(call, guard); 2477 attachLocation(call, guard);
2478 return new js.Return(call); 2478 return new js.Return(call);
2479 } 2479 }
2480 2480
2481 void visitTypeGuard(HTypeGuard node) { 2481 void visitTypeGuard(HTypeGuard node) {
2482 HInstruction input = node.guarded; 2482 HInstruction input = node.guarded;
2483 Element indexingBehavior = compiler.jsIndexingBehaviorInterface; 2483 Type indexingBehavior =
2484 compiler.jsIndexingBehaviorInterface.computeType(compiler);
2484 if (node.isInteger(types)) { 2485 if (node.isInteger(types)) {
2485 // if (input is !int) bailout 2486 // if (input is !int) bailout
2486 checkInt(input, '!=='); 2487 checkInt(input, '!==');
2487 pushStatement(new js.If.then(pop(), bailout(node, 'Not an integer')), 2488 pushStatement(new js.If.then(pop(), bailout(node, 'Not an integer')),
2488 node); 2489 node);
2489 } else if (node.isNumber(types)) { 2490 } else if (node.isNumber(types)) {
2490 // if (input is !num) bailout 2491 // if (input is !num) bailout
2491 checkNum(input, '!=='); 2492 checkNum(input, '!==');
2492 pushStatement(new js.If.then(pop(), bailout(node, 'Not a number')), node); 2493 pushStatement(new js.If.then(pop(), bailout(node, 'Not a number')), node);
2493 } else if (node.isBoolean(types)) { 2494 } else if (node.isBoolean(types)) {
(...skipping 431 matching lines...) Expand 10 before | Expand all | Expand 10 after
2925 if (leftType.canBeNull() && rightType.canBeNull()) { 2926 if (leftType.canBeNull() && rightType.canBeNull()) {
2926 if (left.isConstantNull() || right.isConstantNull() || 2927 if (left.isConstantNull() || right.isConstantNull() ||
2927 (leftType.isPrimitive() && leftType == rightType)) { 2928 (leftType.isPrimitive() && leftType == rightType)) {
2928 return '=='; 2929 return '==';
2929 } 2930 }
2930 return null; 2931 return null;
2931 } else { 2932 } else {
2932 return '==='; 2933 return '===';
2933 } 2934 }
2934 } 2935 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698