Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 import '../closure.dart'; | |
| 6 import '../common.dart'; | |
| 7 import '../common/codegen.dart' show CodegenRegistry; | |
| 5 import '../compiler.dart'; | 8 import '../compiler.dart'; |
| 9 import '../dart_types.dart'; | |
| 6 import '../elements/elements.dart'; | 10 import '../elements/elements.dart'; |
| 7 import '../io/source_information.dart'; | 11 import '../io/source_information.dart'; |
| 8 import '../js_backend/js_backend.dart'; | 12 import '../js_backend/js_backend.dart'; |
| 9 import '../resolution/tree_elements.dart'; | 13 import '../resolution/tree_elements.dart'; |
| 10 import '../tree/tree.dart' as ast; | 14 import '../tree/tree.dart' as ast; |
| 11 import '../types/types.dart'; | 15 import '../types/types.dart'; |
| 16 import '../universe/call_structure.dart' show CallStructure; | |
| 17 import '../universe/use.dart' show TypeUse; | |
| 18 import '../world.dart' show ClosedWorld; | |
| 12 import 'jump_handler.dart'; | 19 import 'jump_handler.dart'; |
| 13 import 'locals_handler.dart'; | 20 import 'locals_handler.dart'; |
| 14 import 'nodes.dart'; | 21 import 'nodes.dart'; |
| 15 import 'ssa_branch_builder.dart'; | 22 import 'ssa_branch_builder.dart'; |
| 23 import 'type_builder.dart'; | |
| 16 | 24 |
| 17 /// Base class for objects that build up an SSA graph. | 25 /// Base class for objects that build up an SSA graph. |
| 18 /// | 26 /// |
| 19 /// This contains helpers for building the graph and tracking information about | 27 /// This contains helpers for building the graph and tracking information about |
| 20 /// the current state of the graph being built. | 28 /// the current state of the graph being built. |
| 21 abstract class GraphBuilder { | 29 abstract class GraphBuilder { |
| 22 /// Holds the resulting SSA graph. | 30 /// Holds the resulting SSA graph. |
| 23 final HGraph graph = new HGraph(); | 31 final HGraph graph = new HGraph(); |
| 24 | 32 |
| 25 // TODO(het): remove this | 33 // TODO(het): remove this |
| 26 /// A reference to the compiler. | 34 /// A reference to the compiler. |
| 27 Compiler compiler; | 35 Compiler compiler; |
| 28 | 36 |
| 29 /// The JavaScript backend we are targeting in this compilation. | 37 /// The JavaScript backend we are targeting in this compilation. |
| 30 JavaScriptBackend get backend; | 38 JavaScriptBackend get backend; |
| 31 | 39 |
| 32 /// The tree elements for the element being built into an SSA graph. | 40 /// The tree elements for the element being built into an SSA graph. |
| 33 TreeElements get elements; | 41 TreeElements get elements; |
| 34 | 42 |
| 43 CodegenRegistry get registry; | |
|
Siggi Cherem (dart-lang)
2016/11/14 18:30:21
remove field & import above? (it appears it's no l
Emily Fortuna
2016/11/14 19:03:39
It's used in type_builder.dart
| |
| 44 | |
| 35 /// Used to track the locals while building the graph. | 45 /// Used to track the locals while building the graph. |
| 36 LocalsHandler localsHandler; | 46 LocalsHandler localsHandler; |
| 37 | 47 |
| 38 /// A stack of instructions. | 48 /// A stack of instructions. |
| 39 /// | 49 /// |
| 40 /// We build the SSA graph by simulating a stack machine. | 50 /// We build the SSA graph by simulating a stack machine. |
| 41 List<HInstruction> stack = <HInstruction>[]; | 51 List<HInstruction> stack = <HInstruction>[]; |
| 42 | 52 |
| 43 /// The count of nested loops we are currently building. | 53 /// The count of nested loops we are currently building. |
| 44 /// | 54 /// |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 175 | 185 |
| 176 HSubGraphBlockInformation wrapStatementGraph(SubGraph statements) { | 186 HSubGraphBlockInformation wrapStatementGraph(SubGraph statements) { |
| 177 if (statements == null) return null; | 187 if (statements == null) return null; |
| 178 return new HSubGraphBlockInformation(statements); | 188 return new HSubGraphBlockInformation(statements); |
| 179 } | 189 } |
| 180 | 190 |
| 181 HSubExpressionBlockInformation wrapExpressionGraph(SubExpression expression) { | 191 HSubExpressionBlockInformation wrapExpressionGraph(SubExpression expression) { |
| 182 if (expression == null) return null; | 192 if (expression == null) return null; |
| 183 return new HSubExpressionBlockInformation(expression); | 193 return new HSubExpressionBlockInformation(expression); |
| 184 } | 194 } |
| 195 | |
| 196 HInstruction buildFunctionType(FunctionType type) { | |
| 197 type.accept( | |
| 198 new ReifiedTypeRepresentationBuilder(compiler.closedWorld), this); | |
| 199 return pop(); | |
| 200 } | |
| 201 | |
| 202 HInstruction buildFunctionTypeConversion( | |
| 203 HInstruction original, DartType type, int kind); | |
| 204 | |
| 205 /// Returns the current source element. | |
| 206 /// | |
| 207 /// The returned element is a declaration element. | |
| 208 Element get sourceElement; | |
| 209 | |
| 210 // TODO(karlklose): this is needed to avoid a bug where the resolved type is | |
| 211 // not stored on a type annotation in the closure translator. Remove when | |
| 212 // fixed. | |
| 213 bool hasDirectLocal(Local local) { | |
| 214 return !localsHandler.isAccessedDirectly(local) || | |
| 215 localsHandler.directLocals[local] != null; | |
| 216 } | |
| 217 | |
| 218 /// The element for which this SSA builder is being used. | |
| 219 Element get targetElement; | |
| 220 TypeBuilder get typeBuilder; | |
| 185 } | 221 } |
| 222 | |
| 223 class ReifiedTypeRepresentationBuilder | |
| 224 implements DartTypeVisitor<dynamic, GraphBuilder> { | |
| 225 final ClosedWorld closedWorld; | |
| 226 | |
| 227 ReifiedTypeRepresentationBuilder(this.closedWorld); | |
| 228 | |
| 229 void visit(DartType type, GraphBuilder builder) => type.accept(this, builder); | |
| 230 | |
| 231 void visitVoidType(VoidType type, GraphBuilder builder) { | |
| 232 ClassElement cls = builder.backend.helpers.VoidRuntimeType; | |
| 233 builder.push(new HVoidType(type, new TypeMask.exact(cls, closedWorld))); | |
| 234 } | |
| 235 | |
| 236 void visitTypeVariableType(TypeVariableType type, GraphBuilder builder) { | |
| 237 ClassElement cls = builder.backend.helpers.RuntimeType; | |
| 238 TypeMask instructionType = new TypeMask.subclass(cls, closedWorld); | |
| 239 if (!builder.sourceElement.enclosingElement.isClosure && | |
| 240 builder.sourceElement.isInstanceMember) { | |
| 241 HInstruction receiver = builder.localsHandler.readThis(); | |
| 242 builder.push(new HReadTypeVariable(type, receiver, instructionType)); | |
| 243 } else { | |
| 244 builder.push(new HReadTypeVariable.noReceiver( | |
| 245 type, | |
| 246 builder.typeBuilder | |
| 247 .addTypeVariableReference(type, builder.sourceElement), | |
| 248 instructionType)); | |
| 249 } | |
| 250 } | |
| 251 | |
| 252 void visitFunctionType(FunctionType type, GraphBuilder builder) { | |
| 253 type.returnType.accept(this, builder); | |
| 254 HInstruction returnType = builder.pop(); | |
| 255 List<HInstruction> inputs = <HInstruction>[returnType]; | |
| 256 | |
| 257 for (DartType parameter in type.parameterTypes) { | |
| 258 parameter.accept(this, builder); | |
| 259 inputs.add(builder.pop()); | |
| 260 } | |
| 261 | |
| 262 for (DartType parameter in type.optionalParameterTypes) { | |
| 263 parameter.accept(this, builder); | |
| 264 inputs.add(builder.pop()); | |
| 265 } | |
| 266 | |
| 267 List<DartType> namedParameterTypes = type.namedParameterTypes; | |
| 268 List<String> names = type.namedParameters; | |
| 269 for (int index = 0; index < names.length; index++) { | |
| 270 ast.DartString dartString = new ast.DartString.literal(names[index]); | |
| 271 inputs.add(builder.graph.addConstantString(dartString, builder.compiler)); | |
| 272 namedParameterTypes[index].accept(this, builder); | |
| 273 inputs.add(builder.pop()); | |
| 274 } | |
| 275 | |
| 276 ClassElement cls = builder.backend.helpers.RuntimeFunctionType; | |
| 277 builder.push( | |
| 278 new HFunctionType(inputs, type, new TypeMask.exact(cls, closedWorld))); | |
| 279 } | |
| 280 | |
| 281 void visitMalformedType(MalformedType type, GraphBuilder builder) { | |
| 282 visitDynamicType(const DynamicType(), builder); | |
| 283 } | |
| 284 | |
| 285 void visitStatementType(StatementType type, GraphBuilder builder) { | |
| 286 throw 'not implemented visitStatementType($type)'; | |
| 287 } | |
| 288 | |
| 289 void visitInterfaceType(InterfaceType type, GraphBuilder builder) { | |
| 290 List<HInstruction> inputs = <HInstruction>[]; | |
| 291 for (DartType typeArgument in type.typeArguments) { | |
| 292 typeArgument.accept(this, builder); | |
| 293 inputs.add(builder.pop()); | |
| 294 } | |
| 295 ClassElement cls; | |
| 296 if (type.typeArguments.isEmpty) { | |
| 297 cls = builder.backend.helpers.RuntimeTypePlain; | |
| 298 } else { | |
| 299 cls = builder.backend.helpers.RuntimeTypeGeneric; | |
| 300 } | |
| 301 builder.push( | |
| 302 new HInterfaceType(inputs, type, new TypeMask.exact(cls, closedWorld))); | |
| 303 } | |
| 304 | |
| 305 void visitTypedefType(TypedefType type, GraphBuilder builder) { | |
| 306 DartType unaliased = type.unaliased; | |
| 307 if (unaliased is TypedefType) throw 'unable to unalias $type'; | |
| 308 unaliased.accept(this, builder); | |
| 309 } | |
| 310 | |
| 311 void visitDynamicType(DynamicType type, GraphBuilder builder) { | |
| 312 JavaScriptBackend backend = builder.compiler.backend; | |
| 313 ClassElement cls = backend.helpers.DynamicRuntimeType; | |
| 314 builder.push(new HDynamicType(type, new TypeMask.exact(cls, closedWorld))); | |
| 315 } | |
| 316 } | |
| OLD | NEW |