| 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 2167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2178 | 2178 |
| 2179 void checkFixedArray(HInstruction input) { | 2179 void checkFixedArray(HInstruction input) { |
| 2180 checkFieldExists(input, 'fixed\$length'); | 2180 checkFieldExists(input, 'fixed\$length'); |
| 2181 } | 2181 } |
| 2182 | 2182 |
| 2183 void checkNull(HInstruction input) { | 2183 void checkNull(HInstruction input) { |
| 2184 use(input); | 2184 use(input); |
| 2185 push(new js.Binary('==', pop(), new js.LiteralNull())); | 2185 push(new js.Binary('==', pop(), new js.LiteralNull())); |
| 2186 } | 2186 } |
| 2187 | 2187 |
| 2188 void checkFunction(HInstruction input, Element element) { | 2188 void checkFunction(HInstruction input, DartType type) { |
| 2189 checkTypeOf(input, '===', 'function'); | 2189 checkTypeOf(input, '===', 'function'); |
| 2190 js.Expression functionTest = pop(); | 2190 js.Expression functionTest = pop(); |
| 2191 checkObject(input, '==='); | 2191 checkObject(input, '==='); |
| 2192 js.Expression objectTest = pop(); | 2192 js.Expression objectTest = pop(); |
| 2193 checkType(input, element); | 2193 checkType(input, type); |
| 2194 push(new js.Binary('||', | 2194 push(new js.Binary('||', |
| 2195 functionTest, | 2195 functionTest, |
| 2196 new js.Binary('&&', objectTest, pop()))); | 2196 new js.Binary('&&', objectTest, pop()))); |
| 2197 } | 2197 } |
| 2198 | 2198 |
| 2199 void checkType(HInstruction input, Element element, [bool negative = false]) { | 2199 void checkType(HInstruction input, DartType type, [bool negative = false]) { |
| 2200 world.registerIsCheck(element); | 2200 Element element = type.element; |
| 2201 use(input); | 2201 use(input); |
| 2202 js.PropertyAccess field = | 2202 js.PropertyAccess field = |
| 2203 new js.PropertyAccess.field(pop(), compiler.namer.operatorIs(element)); | 2203 new js.PropertyAccess.field(pop(), compiler.namer.operatorIs(element)); |
| 2204 if (backend.emitter.nativeEmitter.requiresNativeIsCheck(element)) { | 2204 if (backend.emitter.nativeEmitter.requiresNativeIsCheck(element)) { |
| 2205 push(new js.Call(field, <js.Expression>[])); | 2205 push(new js.Call(field, <js.Expression>[])); |
| 2206 if (negative) push(new js.Prefix('!', pop())); | 2206 if (negative) push(new js.Prefix('!', pop())); |
| 2207 } else { | 2207 } else { |
| 2208 // We always negate at least once so that the result is boolified. | 2208 // We always negate at least once so that the result is boolified. |
| 2209 push(new js.Prefix('!', field)); | 2209 push(new js.Prefix('!', field)); |
| 2210 // If the result is not negated, put another '!' in front. | 2210 // If the result is not negated, put another '!' in front. |
| 2211 if (!negative) push(new js.Prefix('!', pop())); | 2211 if (!negative) push(new js.Prefix('!', pop())); |
| 2212 } | 2212 } |
| 2213 } | 2213 } |
| 2214 | 2214 |
| 2215 void handleStringSupertypeCheck(HInstruction input, Element element) { | 2215 void handleStringSupertypeCheck(HInstruction input, DartType type) { |
| 2216 // Make sure List and String don't share supertypes, otherwise we | 2216 // Make sure List and String don't share supertypes, otherwise we |
| 2217 // would need to check for List too. | 2217 // would need to check for List too. |
| 2218 assert(element !== compiler.listClass | 2218 assert(type.element !== compiler.listClass |
| 2219 && !Elements.isListSupertype(element, compiler)); | 2219 && !Elements.isListSupertype(type.element, compiler)); |
| 2220 checkString(input, '==='); | 2220 checkString(input, '==='); |
| 2221 js.Expression stringTest = pop(); | 2221 js.Expression stringTest = 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 stringTest, | 2226 stringTest, |
| 2227 new js.Binary('&&', objectTest, pop()))); | 2227 new js.Binary('&&', objectTest, pop()))); |
| 2228 } | 2228 } |
| 2229 | 2229 |
| 2230 void handleListOrSupertypeCheck(HInstruction input, Element element) { | 2230 void handleListOrSupertypeCheck(HInstruction input, DartType type) { |
| 2231 // Make sure List and String don't share supertypes, otherwise we | 2231 // Make sure List and String don't share supertypes, otherwise we |
| 2232 // would need to check for String too. | 2232 // would need to check for String too. |
| 2233 assert(element !== compiler.stringClass | 2233 assert(type.element !== compiler.stringClass |
| 2234 && !Elements.isStringSupertype(element, compiler)); | 2234 && !Elements.isStringSupertype(type.element, compiler)); |
| 2235 checkObject(input, '==='); | 2235 checkObject(input, '==='); |
| 2236 js.Expression objectTest = pop(); | 2236 js.Expression objectTest = pop(); |
| 2237 checkArray(input, '==='); | 2237 checkArray(input, '==='); |
| 2238 js.Expression arrayTest = pop(); | 2238 js.Expression arrayTest = pop(); |
| 2239 checkType(input, element); | 2239 checkType(input, type); |
| 2240 push(new js.Binary('&&', | 2240 push(new js.Binary('&&', |
| 2241 objectTest, | 2241 objectTest, |
| 2242 new js.Binary('||', arrayTest, pop()))); | 2242 new js.Binary('||', arrayTest, pop()))); |
| 2243 } | 2243 } |
| 2244 | 2244 |
| 2245 void visitIs(HIs node) { | 2245 void visitIs(HIs node) { |
| 2246 DartType type = node.typeExpression; | 2246 DartType type = node.typeExpression; |
| 2247 Element element = type.element; | 2247 Element element = type.element; |
| 2248 if (element.kind === ElementKind.TYPE_VARIABLE) { | 2248 if (element.kind === ElementKind.TYPE_VARIABLE) { |
| 2249 compiler.unimplemented("visitIs for type variables", instruction: node); | 2249 compiler.unimplemented("visitIs for type variables", instruction: node); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 2264 } else if (element == compiler.doubleClass) { | 2264 } else if (element == compiler.doubleClass) { |
| 2265 checkDouble(input, '==='); | 2265 checkDouble(input, '==='); |
| 2266 attachLocationToLast(node); | 2266 attachLocationToLast(node); |
| 2267 } else if (element == compiler.numClass) { | 2267 } else if (element == compiler.numClass) { |
| 2268 checkNum(input, '==='); | 2268 checkNum(input, '==='); |
| 2269 attachLocationToLast(node); | 2269 attachLocationToLast(node); |
| 2270 } else if (element == compiler.boolClass) { | 2270 } else if (element == compiler.boolClass) { |
| 2271 checkBool(input, '==='); | 2271 checkBool(input, '==='); |
| 2272 attachLocationToLast(node); | 2272 attachLocationToLast(node); |
| 2273 } else if (element == compiler.functionClass) { | 2273 } else if (element == compiler.functionClass) { |
| 2274 checkFunction(input, element); | 2274 checkFunction(input, type); |
| 2275 attachLocationToLast(node); | 2275 attachLocationToLast(node); |
| 2276 } else if (element == compiler.intClass) { | 2276 } else if (element == compiler.intClass) { |
| 2277 checkNum(input, '==='); | 2277 checkNum(input, '==='); |
| 2278 js.Expression numTest = pop(); | 2278 js.Expression numTest = pop(); |
| 2279 checkInt(input, '==='); | 2279 checkInt(input, '==='); |
| 2280 push(new js.Binary('&&', numTest, pop()), node); | 2280 push(new js.Binary('&&', numTest, pop()), node); |
| 2281 } else if (Elements.isStringSupertype(element, compiler)) { | 2281 } else if (Elements.isStringSupertype(element, compiler)) { |
| 2282 handleStringSupertypeCheck(input, element); | 2282 handleStringSupertypeCheck(input, type); |
| 2283 attachLocationToLast(node); | 2283 attachLocationToLast(node); |
| 2284 } else if (element === compiler.listClass | 2284 } else if (element === compiler.listClass |
| 2285 || Elements.isListSupertype(element, compiler)) { | 2285 || Elements.isListSupertype(element, compiler)) { |
| 2286 handleListOrSupertypeCheck(input, element); | 2286 handleListOrSupertypeCheck(input, type); |
| 2287 attachLocationToLast(node); | 2287 attachLocationToLast(node); |
| 2288 } else if (types[input].canBePrimitive() || types[input].canBeNull()) { | 2288 } else if (types[input].canBePrimitive() || types[input].canBeNull()) { |
| 2289 checkObject(input, '==='); | 2289 checkObject(input, '==='); |
| 2290 js.Expression objectTest = pop(); | 2290 js.Expression objectTest = pop(); |
| 2291 checkType(input, element); | 2291 checkType(input, type); |
| 2292 push(new js.Binary('&&', objectTest, pop()), node); | 2292 push(new js.Binary('&&', objectTest, pop()), node); |
| 2293 } else { | 2293 } else { |
| 2294 checkType(input, element); | 2294 checkType(input, type); |
| 2295 attachLocationToLast(node); | 2295 attachLocationToLast(node); |
| 2296 } | 2296 } |
| 2297 if (node.hasTypeInfo()) { | 2297 if (node.hasTypeInfo()) { |
| 2298 InterfaceType interfaceType = type; | 2298 InterfaceType interfaceType = type; |
| 2299 ClassElement cls = type.element; | 2299 ClassElement cls = type.element; |
| 2300 Link<DartType> arguments = interfaceType.arguments; | 2300 Link<DartType> arguments = interfaceType.arguments; |
| 2301 js.Expression result = pop(); | 2301 js.Expression result = pop(); |
| 2302 for (TypeVariableType typeVariable in cls.typeVariables) { | 2302 for (TypeVariableType typeVariable in cls.typeVariables) { |
| 2303 use(node.typeInfoCall); | 2303 use(node.typeInfoCall); |
| 2304 // TODO(johnniwinther): Retrieve the type name properly and not through | 2304 // TODO(johnniwinther): Retrieve the type name properly and not through |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2344 const SourceString("listSuperTypeCast"), | 2344 const SourceString("listSuperTypeCast"), |
| 2345 "callTypeCheck": | 2345 "callTypeCheck": |
| 2346 const SourceString("callTypeCast"), | 2346 const SourceString("callTypeCast"), |
| 2347 "propertyTypeCheck": | 2347 "propertyTypeCheck": |
| 2348 const SourceString("propertyTypeCast") | 2348 const SourceString("propertyTypeCast") |
| 2349 }; | 2349 }; |
| 2350 | 2350 |
| 2351 if (node.isChecked) { | 2351 if (node.isChecked) { |
| 2352 DartType type = node.type.computeType(compiler); | 2352 DartType type = node.type.computeType(compiler); |
| 2353 Element element = type.element; | 2353 Element element = type.element; |
| 2354 world.registerIsCheck(element); | 2354 world.registerIsCheck(type); |
| 2355 | 2355 |
| 2356 if (node.isArgumentTypeCheck) { | 2356 if (node.isArgumentTypeCheck) { |
| 2357 if (element == compiler.intClass) { | 2357 if (element == compiler.intClass) { |
| 2358 checkInt(node.checkedInput, '!=='); | 2358 checkInt(node.checkedInput, '!=='); |
| 2359 } else { | 2359 } else { |
| 2360 assert(element == compiler.numClass); | 2360 assert(element == compiler.numClass); |
| 2361 checkNum(node.checkedInput, '!=='); | 2361 checkNum(node.checkedInput, '!=='); |
| 2362 } | 2362 } |
| 2363 js.Expression test = pop(); | 2363 js.Expression test = pop(); |
| 2364 js.Block oldContainer = currentContainer; | 2364 js.Block oldContainer = currentContainer; |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2448 } else { | 2448 } else { |
| 2449 bailoutTarget = new js.VariableUse(namer.isolateBailoutAccess(element)); | 2449 bailoutTarget = new js.VariableUse(namer.isolateBailoutAccess(element)); |
| 2450 } | 2450 } |
| 2451 js.Call call = new js.Call(bailoutTarget, arguments); | 2451 js.Call call = new js.Call(bailoutTarget, arguments); |
| 2452 attachLocation(call, guard); | 2452 attachLocation(call, guard); |
| 2453 return new js.Return(call); | 2453 return new js.Return(call); |
| 2454 } | 2454 } |
| 2455 | 2455 |
| 2456 void visitTypeGuard(HTypeGuard node) { | 2456 void visitTypeGuard(HTypeGuard node) { |
| 2457 HInstruction input = node.guarded; | 2457 HInstruction input = node.guarded; |
| 2458 Element indexingBehavior = compiler.jsIndexingBehaviorInterface; | 2458 DartType indexingBehavior = |
| 2459 compiler.jsIndexingBehaviorInterface.computeType(compiler); |
| 2459 if (node.isInteger(types)) { | 2460 if (node.isInteger(types)) { |
| 2460 // if (input is !int) bailout | 2461 // if (input is !int) bailout |
| 2461 checkInt(input, '!=='); | 2462 checkInt(input, '!=='); |
| 2462 pushStatement(new js.If.then(pop(), bailout(node, 'Not an integer')), | 2463 pushStatement(new js.If.then(pop(), bailout(node, 'Not an integer')), |
| 2463 node); | 2464 node); |
| 2464 } else if (node.isNumber(types)) { | 2465 } else if (node.isNumber(types)) { |
| 2465 // if (input is !num) bailout | 2466 // if (input is !num) bailout |
| 2466 checkNum(input, '!=='); | 2467 checkNum(input, '!=='); |
| 2467 pushStatement(new js.If.then(pop(), bailout(node, 'Not a number')), node); | 2468 pushStatement(new js.If.then(pop(), bailout(node, 'Not a number')), node); |
| 2468 } else if (node.isBoolean(types)) { | 2469 } else if (node.isBoolean(types)) { |
| (...skipping 431 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2900 if (leftType.canBeNull() && rightType.canBeNull()) { | 2901 if (leftType.canBeNull() && rightType.canBeNull()) { |
| 2901 if (left.isConstantNull() || right.isConstantNull() || | 2902 if (left.isConstantNull() || right.isConstantNull() || |
| 2902 (leftType.isPrimitive() && leftType == rightType)) { | 2903 (leftType.isPrimitive() && leftType == rightType)) { |
| 2903 return '=='; | 2904 return '=='; |
| 2904 } | 2905 } |
| 2905 return null; | 2906 return null; |
| 2906 } else { | 2907 } else { |
| 2907 return '==='; | 2908 return '==='; |
| 2908 } | 2909 } |
| 2909 } | 2910 } |
| OLD | NEW |