| 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 | 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 2198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2209 | 2209 |
| 2210 void checkFixedArray(HInstruction input) { | 2210 void checkFixedArray(HInstruction input) { |
| 2211 checkFieldExists(input, 'fixed\$length'); | 2211 checkFieldExists(input, 'fixed\$length'); |
| 2212 } | 2212 } |
| 2213 | 2213 |
| 2214 void checkNull(HInstruction input) { | 2214 void checkNull(HInstruction input) { |
| 2215 use(input); | 2215 use(input); |
| 2216 push(new js.Binary('==', pop(), new js.LiteralNull())); | 2216 push(new js.Binary('==', pop(), new js.LiteralNull())); |
| 2217 } | 2217 } |
| 2218 | 2218 |
| 2219 void checkFunction(HInstruction input, Element element) { | 2219 void checkFunction(HInstruction input, DartType type) { |
| 2220 checkTypeOf(input, '===', 'function'); | 2220 checkTypeOf(input, '===', 'function'); |
| 2221 js.Expression functionTest = pop(); | 2221 js.Expression functionTest = pop(); |
| 2222 checkObject(input, '==='); | 2222 checkObject(input, '==='); |
| 2223 js.Expression objectTest = pop(); | 2223 js.Expression objectTest = pop(); |
| 2224 checkType(input, element); | 2224 checkType(input, type); |
| 2225 push(new js.Binary('||', | 2225 push(new js.Binary('||', |
| 2226 functionTest, | 2226 functionTest, |
| 2227 new js.Binary('&&', objectTest, pop()))); | 2227 new js.Binary('&&', objectTest, pop()))); |
| 2228 } | 2228 } |
| 2229 | 2229 |
| 2230 void checkType(HInstruction input, Element element, [bool negative = false]) { | 2230 void checkType(HInstruction input, DartType type, [bool negative = false]) { |
| 2231 world.registerIsCheck(element); | 2231 Element element = type.element; |
| 2232 use(input); | 2232 use(input); |
| 2233 js.PropertyAccess field = | 2233 js.PropertyAccess field = |
| 2234 new js.PropertyAccess.field(pop(), compiler.namer.operatorIs(element)); | 2234 new js.PropertyAccess.field(pop(), compiler.namer.operatorIs(element)); |
| 2235 if (backend.emitter.nativeEmitter.requiresNativeIsCheck(element)) { | 2235 if (backend.emitter.nativeEmitter.requiresNativeIsCheck(element)) { |
| 2236 push(new js.Call(field, <js.Expression>[])); | 2236 push(new js.Call(field, <js.Expression>[])); |
| 2237 if (negative) push(new js.Prefix('!', pop())); | 2237 if (negative) push(new js.Prefix('!', pop())); |
| 2238 } else { | 2238 } else { |
| 2239 // We always negate at least once so that the result is boolified. | 2239 // We always negate at least once so that the result is boolified. |
| 2240 push(new js.Prefix('!', field)); | 2240 push(new js.Prefix('!', field)); |
| 2241 // If the result is not negated, put another '!' in front. | 2241 // If the result is not negated, put another '!' in front. |
| 2242 if (!negative) push(new js.Prefix('!', pop())); | 2242 if (!negative) push(new js.Prefix('!', pop())); |
| 2243 } | 2243 } |
| 2244 } | 2244 } |
| 2245 | 2245 |
| 2246 void handleStringSupertypeCheck(HInstruction input, Element element) { | 2246 void handleStringSupertypeCheck(HInstruction input, DartType type) { |
| 2247 // Make sure List and String don't share supertypes, otherwise we | 2247 // Make sure List and String don't share supertypes, otherwise we |
| 2248 // would need to check for List too. | 2248 // would need to check for List too. |
| 2249 assert(element !== compiler.listClass | 2249 assert(type.element !== compiler.listClass |
| 2250 && !Elements.isListSupertype(element, compiler)); | 2250 && !Elements.isListSupertype(type.element, compiler)); |
| 2251 checkString(input, '==='); | 2251 checkString(input, '==='); |
| 2252 js.Expression stringTest = pop(); | 2252 js.Expression stringTest = pop(); |
| 2253 checkObject(input, '==='); | 2253 checkObject(input, '==='); |
| 2254 js.Expression objectTest = pop(); | 2254 js.Expression objectTest = pop(); |
| 2255 checkType(input, element); | 2255 checkType(input, type); |
| 2256 push(new js.Binary('||', | 2256 push(new js.Binary('||', |
| 2257 stringTest, | 2257 stringTest, |
| 2258 new js.Binary('&&', objectTest, pop()))); | 2258 new js.Binary('&&', objectTest, pop()))); |
| 2259 } | 2259 } |
| 2260 | 2260 |
| 2261 void handleListOrSupertypeCheck(HInstruction input, Element element) { | 2261 void handleListOrSupertypeCheck(HInstruction input, DartType type) { |
| 2262 // Make sure List and String don't share supertypes, otherwise we | 2262 // Make sure List and String don't share supertypes, otherwise we |
| 2263 // would need to check for String too. | 2263 // would need to check for String too. |
| 2264 assert(element !== compiler.stringClass | 2264 assert(type.element !== compiler.stringClass |
| 2265 && !Elements.isStringSupertype(element, compiler)); | 2265 && !Elements.isStringSupertype(type.element, compiler)); |
| 2266 checkObject(input, '==='); | 2266 checkObject(input, '==='); |
| 2267 js.Expression objectTest = pop(); | 2267 js.Expression objectTest = pop(); |
| 2268 checkArray(input, '==='); | 2268 checkArray(input, '==='); |
| 2269 js.Expression arrayTest = pop(); | 2269 js.Expression arrayTest = pop(); |
| 2270 checkType(input, element); | 2270 checkType(input, type); |
| 2271 push(new js.Binary('&&', | 2271 push(new js.Binary('&&', |
| 2272 objectTest, | 2272 objectTest, |
| 2273 new js.Binary('||', arrayTest, pop()))); | 2273 new js.Binary('||', arrayTest, pop()))); |
| 2274 } | 2274 } |
| 2275 | 2275 |
| 2276 void visitIs(HIs node) { | 2276 void visitIs(HIs node) { |
| 2277 DartType type = node.typeExpression; | 2277 DartType type = node.typeExpression; |
| 2278 Element element = type.element; | 2278 Element element = type.element; |
| 2279 if (element.kind === ElementKind.TYPE_VARIABLE) { | 2279 if (element.kind === ElementKind.TYPE_VARIABLE) { |
| 2280 compiler.unimplemented("visitIs for type variables", instruction: node); | 2280 compiler.unimplemented("visitIs for type variables", instruction: node); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 2295 } else if (element == compiler.doubleClass) { | 2295 } else if (element == compiler.doubleClass) { |
| 2296 checkDouble(input, '==='); | 2296 checkDouble(input, '==='); |
| 2297 attachLocationToLast(node); | 2297 attachLocationToLast(node); |
| 2298 } else if (element == compiler.numClass) { | 2298 } else if (element == compiler.numClass) { |
| 2299 checkNum(input, '==='); | 2299 checkNum(input, '==='); |
| 2300 attachLocationToLast(node); | 2300 attachLocationToLast(node); |
| 2301 } else if (element == compiler.boolClass) { | 2301 } else if (element == compiler.boolClass) { |
| 2302 checkBool(input, '==='); | 2302 checkBool(input, '==='); |
| 2303 attachLocationToLast(node); | 2303 attachLocationToLast(node); |
| 2304 } else if (element == compiler.functionClass) { | 2304 } else if (element == compiler.functionClass) { |
| 2305 checkFunction(input, element); | 2305 checkFunction(input, type); |
| 2306 attachLocationToLast(node); | 2306 attachLocationToLast(node); |
| 2307 } else if (element == compiler.intClass) { | 2307 } else if (element == compiler.intClass) { |
| 2308 checkNum(input, '==='); | 2308 checkNum(input, '==='); |
| 2309 js.Expression numTest = pop(); | 2309 js.Expression numTest = pop(); |
| 2310 checkInt(input, '==='); | 2310 checkInt(input, '==='); |
| 2311 push(new js.Binary('&&', numTest, pop()), node); | 2311 push(new js.Binary('&&', numTest, pop()), node); |
| 2312 } else if (Elements.isStringSupertype(element, compiler)) { | 2312 } else if (Elements.isStringSupertype(element, compiler)) { |
| 2313 handleStringSupertypeCheck(input, element); | 2313 handleStringSupertypeCheck(input, type); |
| 2314 attachLocationToLast(node); | 2314 attachLocationToLast(node); |
| 2315 } else if (element === compiler.listClass | 2315 } else if (element === compiler.listClass |
| 2316 || Elements.isListSupertype(element, compiler)) { | 2316 || Elements.isListSupertype(element, compiler)) { |
| 2317 handleListOrSupertypeCheck(input, element); | 2317 handleListOrSupertypeCheck(input, type); |
| 2318 attachLocationToLast(node); | 2318 attachLocationToLast(node); |
| 2319 } else if (types[input].canBePrimitive() || types[input].canBeNull()) { | 2319 } else if (types[input].canBePrimitive() || types[input].canBeNull()) { |
| 2320 checkObject(input, '==='); | 2320 checkObject(input, '==='); |
| 2321 js.Expression objectTest = pop(); | 2321 js.Expression objectTest = pop(); |
| 2322 checkType(input, element); | 2322 checkType(input, type); |
| 2323 push(new js.Binary('&&', objectTest, pop()), node); | 2323 push(new js.Binary('&&', objectTest, pop()), node); |
| 2324 } else { | 2324 } else { |
| 2325 checkType(input, element); | 2325 checkType(input, type); |
| 2326 attachLocationToLast(node); | 2326 attachLocationToLast(node); |
| 2327 } | 2327 } |
| 2328 if (node.hasTypeInfo()) { | 2328 if (node.hasTypeInfo()) { |
| 2329 InterfaceType interfaceType = type; | 2329 InterfaceType interfaceType = type; |
| 2330 ClassElement cls = type.element; | 2330 ClassElement cls = type.element; |
| 2331 Link<DartType> arguments = interfaceType.arguments; | 2331 Link<DartType> arguments = interfaceType.arguments; |
| 2332 js.Expression result = pop(); | 2332 js.Expression result = pop(); |
| 2333 for (TypeVariableType typeVariable in cls.typeVariables) { | 2333 for (TypeVariableType typeVariable in cls.typeVariables) { |
| 2334 use(node.typeInfoCall); | 2334 use(node.typeInfoCall); |
| 2335 // TODO(johnniwinther): Retrieve the type name properly and not through | 2335 // TODO(johnniwinther): Retrieve the type name properly and not through |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2375 const SourceString("listSuperTypeCast"), | 2375 const SourceString("listSuperTypeCast"), |
| 2376 "callTypeCheck": | 2376 "callTypeCheck": |
| 2377 const SourceString("callTypeCast"), | 2377 const SourceString("callTypeCast"), |
| 2378 "propertyTypeCheck": | 2378 "propertyTypeCheck": |
| 2379 const SourceString("propertyTypeCast") | 2379 const SourceString("propertyTypeCast") |
| 2380 }; | 2380 }; |
| 2381 | 2381 |
| 2382 if (node.isChecked) { | 2382 if (node.isChecked) { |
| 2383 DartType type = node.type.computeType(compiler); | 2383 DartType type = node.type.computeType(compiler); |
| 2384 Element element = type.element; | 2384 Element element = type.element; |
| 2385 world.registerIsCheck(element); | 2385 world.registerIsCheck(type); |
| 2386 | 2386 |
| 2387 if (node.isArgumentTypeCheck) { | 2387 if (node.isArgumentTypeCheck) { |
| 2388 if (element == compiler.intClass) { | 2388 if (element == compiler.intClass) { |
| 2389 checkInt(node.checkedInput, '!=='); | 2389 checkInt(node.checkedInput, '!=='); |
| 2390 } else { | 2390 } else { |
| 2391 assert(element == compiler.numClass); | 2391 assert(element == compiler.numClass); |
| 2392 checkNum(node.checkedInput, '!=='); | 2392 checkNum(node.checkedInput, '!=='); |
| 2393 } | 2393 } |
| 2394 js.Expression test = pop(); | 2394 js.Expression test = pop(); |
| 2395 js.Block oldContainer = currentContainer; | 2395 js.Block oldContainer = currentContainer; |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2480 assert(!element.isField()); | 2480 assert(!element.isField()); |
| 2481 bailoutTarget = new js.VariableUse(namer.isolateBailoutAccess(element)); | 2481 bailoutTarget = new js.VariableUse(namer.isolateBailoutAccess(element)); |
| 2482 } | 2482 } |
| 2483 js.Call call = new js.Call(bailoutTarget, arguments); | 2483 js.Call call = new js.Call(bailoutTarget, arguments); |
| 2484 attachLocation(call, guard); | 2484 attachLocation(call, guard); |
| 2485 return new js.Return(call); | 2485 return new js.Return(call); |
| 2486 } | 2486 } |
| 2487 | 2487 |
| 2488 void visitTypeGuard(HTypeGuard node) { | 2488 void visitTypeGuard(HTypeGuard node) { |
| 2489 HInstruction input = node.guarded; | 2489 HInstruction input = node.guarded; |
| 2490 Element indexingBehavior = compiler.jsIndexingBehaviorInterface; | 2490 DartType indexingBehavior = |
| 2491 backend.jsIndexingBehaviorInterface.computeType(compiler); |
| 2491 if (node.isInteger(types)) { | 2492 if (node.isInteger(types)) { |
| 2492 // if (input is !int) bailout | 2493 // if (input is !int) bailout |
| 2493 checkInt(input, '!=='); | 2494 checkInt(input, '!=='); |
| 2494 js.Statement then = bailout(node, 'Not an integer'); | 2495 js.Statement then = bailout(node, 'Not an integer'); |
| 2495 pushStatement(new js.If.noElse(pop(), then), node); | 2496 pushStatement(new js.If.noElse(pop(), then), node); |
| 2496 } else if (node.isNumber(types)) { | 2497 } else if (node.isNumber(types)) { |
| 2497 // if (input is !num) bailout | 2498 // if (input is !num) bailout |
| 2498 checkNum(input, '!=='); | 2499 checkNum(input, '!=='); |
| 2499 js.Statement then = bailout(node, 'Not a number'); | 2500 js.Statement then = bailout(node, 'Not a number'); |
| 2500 pushStatement(new js.If.noElse(pop(), then), node); | 2501 pushStatement(new js.If.noElse(pop(), then), node); |
| (...skipping 433 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2934 if (leftType.canBeNull() && rightType.canBeNull()) { | 2935 if (leftType.canBeNull() && rightType.canBeNull()) { |
| 2935 if (left.isConstantNull() || right.isConstantNull() || | 2936 if (left.isConstantNull() || right.isConstantNull() || |
| 2936 (leftType.isPrimitive() && leftType == rightType)) { | 2937 (leftType.isPrimitive() && leftType == rightType)) { |
| 2937 return '=='; | 2938 return '=='; |
| 2938 } | 2939 } |
| 2939 return null; | 2940 return null; |
| 2940 } else { | 2941 } else { |
| 2941 return '==='; | 2942 return '==='; |
| 2942 } | 2943 } |
| 2943 } | 2944 } |
| OLD | NEW |