| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 library dart2js.ir_pickler; | 5 library dart2js.ir_pickler; |
| 6 | 6 |
| 7 import 'ir_nodes.dart' as ir; | 7 import 'ir_nodes.dart' as ir; |
| 8 import '../dart2jslib.dart' show | 8 import '../dart2jslib.dart' show |
| 9 Constant, FalseConstant, TrueConstant, IntConstant, DoubleConstant, | 9 Constant, FalseConstant, TrueConstant, IntConstant, DoubleConstant, |
| 10 StringConstant, NullConstant, ListConstant, MapConstant, | 10 StringConstant, NullConstant, ListConstant, MapConstant, |
| 11 InterceptorConstant, DummyConstant, FunctionConstant, TypeConstant, | 11 InterceptorConstant, DummyConstant, FunctionConstant, TypeConstant, |
| 12 ConstructedConstant, | 12 ConstructedConstant, |
| 13 ConstantVisitor, ConstantSystem, | 13 ConstantVisitor, ConstantSystem, |
| 14 Compiler, NO_LOCATION_SPANNABLE; | 14 Compiler, NO_LOCATION_SPANNABLE; |
| 15 import 'dart:typed_data' show ByteData, Endianness, Uint8List; | 15 import 'dart:typed_data' show ByteData, Endianness, Uint8List; |
| 16 import 'dart:convert' show UTF8; | 16 import 'dart:convert' show UTF8; |
| 17 import '../tree/tree.dart' as ast show | 17 import '../tree/tree.dart' as ast show |
| 18 DartString, LiteralDartString, RawSourceDartString, EscapedSourceDartString, | 18 DartString, LiteralDartString, RawSourceDartString, EscapedSourceDartString, |
| 19 ConsDartString; | 19 ConsDartString; |
| 20 import '../elements/elements.dart' show | 20 import '../elements/elements.dart' show |
| 21 Element, LibraryElement, FunctionElement; | 21 Element, LibraryElement, FunctionElement; |
| 22 import '../universe/universe.dart' show Selector, TypedSelector, SelectorKind; | 22 import '../universe/universe.dart' show Selector, TypedSelector, SelectorKind; |
| 23 import '../dart_types.dart' as types; |
| 23 | 24 |
| 24 part 'ir_unpickler.dart'; | 25 part 'ir_unpickler.dart'; |
| 25 | 26 |
| 26 /* The int(entries) counts expression nodes, which might potentially be | 27 /* The int(entries) counts expression nodes, which might potentially be |
| 27 * referred to in a back reference. | 28 * referred to in a back reference. |
| 28 * | 29 * |
| 29 * pickle ::= int(entries) function | 30 * pickle ::= int(entries) function |
| 30 * | 31 * |
| 31 * function ::= int(parameter count) {element(parameter)} node(body) | 32 * function ::= int(parameter count) {element(parameter)} node(body) |
| 32 * | 33 * |
| 33 * int ::= see [writeInt] for number encoding | 34 * int ::= see [writeInt] for number encoding |
| 34 * | 35 * |
| 35 * string ::= byte(STRING_ASCII) int(length) {byte(ascii)} | 36 * string ::= byte(STRING_ASCII) int(length) {byte(ascii)} |
| 36 * | byte(STRING_UTF8) int(length) {byte(utf8)} | 37 * | byte(STRING_UTF8) int(length) {byte(utf8)} |
| 37 * | 38 * |
| 38 * node ::= byte(NODE_CONSTANT) constant node(next) | 39 * node ::= byte(NODE_CONSTANT) constant node(next) |
| 39 * | byte(NODE_LET_CONT) int(parameter count) node(next) node(body) | 40 * | byte(NODE_LET_CONT) int(parameter count) node(next) node(body) |
| 40 * | byte(NODE_INVOKE_STATIC) element selector | 41 * | byte(NODE_INVOKE_STATIC) element selector |
| 41 * reference(continuation) {reference(argument)} | 42 * reference(continuation) {reference(argument)} |
| 43 * | byte(NODE_INVOKE_METHOD) reference(receiver) selector |
| 44 * reference(continuation) {reference(argument)} |
| 45 * | byte(NODE_INVOKE_CONSTRUCTOR) type element(target) |
| 46 * reference(continuation) {reference(argument)} |
| 42 * | byte(NODE_INVOKE_CONTINUATION) reference(continuation) | 47 * | byte(NODE_INVOKE_CONTINUATION) reference(continuation) |
| 43 * {reference(argument)} | 48 * {reference(argument)} |
| 44 * | 49 * |
| 45 * reference ::= int(indexDelta) | 50 * reference ::= int(indexDelta) |
| 46 * | 51 * |
| 47 * constant ::= byte(CONST_BOOL) byte(0 or 1) | 52 * constant ::= byte(CONST_BOOL) byte(0 or 1) |
| 48 * | byte(CONST_DOUBLE) byte{8} | 53 * | byte(CONST_DOUBLE) byte{8} |
| 49 * | byte(CONST_INT) int(value) | 54 * | byte(CONST_INT) int(value) |
| 50 * | byte(CONST_STRING_LITERAL) string | 55 * | byte(CONST_STRING_LITERAL) string |
| 51 * | byte(CONST_STRING_RAW) string int(length) | 56 * | byte(CONST_STRING_RAW) string int(length) |
| 52 * | byte(CONST_STRING_ESCAPED) string int(length) | 57 * | byte(CONST_STRING_ESCAPED) string int(length) |
| 53 * | byte(CONST_STRING_CONS) constant(left) constant(right) | 58 * | byte(CONST_STRING_CONS) constant(left) constant(right) |
| 54 * | byte(CONST_NULL) | 59 * | byte(CONST_NULL) |
| 55 * | 60 * |
| 56 * selector ::= byte(BACKREFERENCE) reference | 61 * selector ::= byte(BACKREFERENCE) reference |
| 57 * | byte(SELECTOR_UNTYPED) int(kind) string(name) element(library) | 62 * | byte(SELECTOR_UNTYPED) int(kind) string(name) element(library) |
| 58 * int(argumentsCount) int(namedArgumentsCount) | 63 * int(argumentsCount) int(namedArgumentsCount) |
| 59 * {string(parameterName)} | 64 * {string(parameterName)} |
| 60 * | 65 * |
| 61 * element ::= int(constantPoolIndex) | 66 * element ::= int(constantPoolIndex) |
| 67 * |
| 68 * type ::= int(constantPoolIndex) |
| 62 */ | 69 */ |
| 63 class Pickles { | 70 class Pickles { |
| 64 static const int BACKREFERENCE = 1; | 71 static const int BACKREFERENCE = 1; |
| 65 | 72 |
| 66 static const int STRING_ASCII = BACKREFERENCE + 1; | 73 static const int STRING_ASCII = BACKREFERENCE + 1; |
| 67 static const int STRING_UTF8 = STRING_ASCII + 1; | 74 static const int STRING_UTF8 = STRING_ASCII + 1; |
| 68 | 75 |
| 69 static const int FIRST_NODE_TAG = STRING_UTF8 + 1; | 76 static const int FIRST_NODE_TAG = STRING_UTF8 + 1; |
| 70 static const int NODE_CONSTANT = FIRST_NODE_TAG; | 77 static const int NODE_CONSTANT = FIRST_NODE_TAG; |
| 71 static const int NODE_IS_TRUE = NODE_CONSTANT + 1; | 78 static const int NODE_IS_TRUE = NODE_CONSTANT + 1; |
| 72 static const int NODE_LET_CONT = NODE_IS_TRUE + 1; | 79 static const int NODE_LET_CONT = NODE_IS_TRUE + 1; |
| 73 static const int NODE_INVOKE_STATIC = NODE_LET_CONT + 1; | 80 static const int NODE_INVOKE_STATIC = NODE_LET_CONT + 1; |
| 74 static const int NODE_INVOKE_CONTINUATION = NODE_INVOKE_STATIC + 1; | 81 static const int NODE_INVOKE_METHOD = NODE_INVOKE_STATIC + 1; |
| 82 static const int NODE_INVOKE_CONSTRUCTOR = NODE_INVOKE_METHOD + 1; |
| 83 static const int NODE_INVOKE_CONTINUATION = NODE_INVOKE_CONSTRUCTOR + 1; |
| 75 static const int NODE_BRANCH = NODE_INVOKE_CONTINUATION + 1; | 84 static const int NODE_BRANCH = NODE_INVOKE_CONTINUATION + 1; |
| 76 static const int LAST_NODE_TAG = NODE_BRANCH; | 85 static const int LAST_NODE_TAG = NODE_BRANCH; |
| 77 | 86 |
| 78 static const int FIRST_CONST_TAG = LAST_NODE_TAG + 1; | 87 static const int FIRST_CONST_TAG = LAST_NODE_TAG + 1; |
| 79 static const int CONST_BOOL = FIRST_CONST_TAG; | 88 static const int CONST_BOOL = FIRST_CONST_TAG; |
| 80 static const int CONST_INT = CONST_BOOL + 1; | 89 static const int CONST_INT = CONST_BOOL + 1; |
| 81 static const int CONST_DOUBLE = CONST_INT + 1; | 90 static const int CONST_DOUBLE = CONST_INT + 1; |
| 82 static const int CONST_STRING_LITERAL = CONST_DOUBLE + 1; | 91 static const int CONST_STRING_LITERAL = CONST_DOUBLE + 1; |
| 83 static const int CONST_STRING_RAW = CONST_STRING_LITERAL + 1; | 92 static const int CONST_STRING_RAW = CONST_STRING_LITERAL + 1; |
| 84 static const int CONST_STRING_ESCAPED = CONST_STRING_RAW + 1; | 93 static const int CONST_STRING_ESCAPED = CONST_STRING_RAW + 1; |
| (...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 330 } | 339 } |
| 331 } | 340 } |
| 332 | 341 |
| 333 void writeConstNull() { | 342 void writeConstNull() { |
| 334 writeByte(Pickles.CONST_NULL); | 343 writeByte(Pickles.CONST_NULL); |
| 335 } | 344 } |
| 336 | 345 |
| 337 void writeElement(Element element) { | 346 void writeElement(Element element) { |
| 338 writeInt(constantPool.add(element)); | 347 writeInt(constantPool.add(element)); |
| 339 } | 348 } |
| 349 |
| 350 void writeDartType(types.DartType type) { |
| 351 writeInt(constantPool.add(type)); |
| 352 } |
| 340 | 353 |
| 341 void writeSelector(Selector selector) { | 354 void writeSelector(Selector selector) { |
| 342 if (emitted.containsKey(selector)) { | 355 if (emitted.containsKey(selector)) { |
| 343 writeByte(Pickles.BACKREFERENCE); | 356 writeByte(Pickles.BACKREFERENCE); |
| 344 writeBackReference(selector); | 357 writeBackReference(selector); |
| 345 } else { | 358 } else { |
| 346 recordForBackReference(selector); | 359 recordForBackReference(selector); |
| 347 assert(selector is !TypedSelector); | 360 assert(selector is !TypedSelector); |
| 348 writeByte(Pickles.SELECTOR_UNTYPED); | 361 writeByte(Pickles.SELECTOR_UNTYPED); |
| 349 writeInt(selector.kind.hashCode); | 362 writeInt(selector.kind.hashCode); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 395 void visitInvokeStatic(ir.InvokeStatic node) { | 408 void visitInvokeStatic(ir.InvokeStatic node) { |
| 396 writeByte(Pickles.NODE_INVOKE_STATIC); | 409 writeByte(Pickles.NODE_INVOKE_STATIC); |
| 397 writeElement(node.target); | 410 writeElement(node.target); |
| 398 writeSelector(node.selector); | 411 writeSelector(node.selector); |
| 399 // TODO(lry): compact encoding when the arity of the selector and the | 412 // TODO(lry): compact encoding when the arity of the selector and the |
| 400 // arguments list are the same | 413 // arguments list are the same |
| 401 writeBackReference(node.continuation.definition); | 414 writeBackReference(node.continuation.definition); |
| 402 writeBackReferenceList(node.arguments.length, | 415 writeBackReferenceList(node.arguments.length, |
| 403 node.arguments.map((a) => a.definition)); | 416 node.arguments.map((a) => a.definition)); |
| 404 } | 417 } |
| 418 |
| 419 void visitInvokeMethod(ir.InvokeMethod node) { |
| 420 writeByte(Pickles.NODE_INVOKE_METHOD); |
| 421 writeBackReference(node.receiver.definition); |
| 422 writeSelector(node.selector); |
| 423 writeBackReference(node.continuation.definition); |
| 424 writeBackReferenceList(node.arguments.length, |
| 425 node.arguments.map((a) => a.definition)); |
| 426 } |
| 427 |
| 428 void visitInvokeConstructor(ir.InvokeConstructor node) { |
| 429 writeByte(Pickles.NODE_INVOKE_CONSTRUCTOR); |
| 430 writeDartType(node.type); |
| 431 writeElement(node.target); |
| 432 writeBackReference(node.continuation.definition); |
| 433 writeBackReferenceList(node.arguments.length, |
| 434 node.arguments.map((a) => a.definition)); |
| 435 } |
| 405 | 436 |
| 406 void visitInvokeContinuation(ir.InvokeContinuation node) { | 437 void visitInvokeContinuation(ir.InvokeContinuation node) { |
| 407 writeByte(Pickles.NODE_INVOKE_CONTINUATION); | 438 writeByte(Pickles.NODE_INVOKE_CONTINUATION); |
| 408 writeBackReference(node.continuation.definition); | 439 writeBackReference(node.continuation.definition); |
| 409 writeBackReferenceList(node.arguments.length, | 440 writeBackReferenceList(node.arguments.length, |
| 410 node.arguments.map((a) => a.definition)); | 441 node.arguments.map((a) => a.definition)); |
| 411 } | 442 } |
| 412 | 443 |
| 413 void visitBranch(ir.Branch node) { | 444 void visitBranch(ir.Branch node) { |
| 414 writeByte(Pickles.NODE_BRANCH); | 445 writeByte(Pickles.NODE_BRANCH); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 467 void visitList(ListConstant constant) => abort(constant); | 498 void visitList(ListConstant constant) => abort(constant); |
| 468 void visitMap(MapConstant constant) => abort(constant); | 499 void visitMap(MapConstant constant) => abort(constant); |
| 469 void visitInterceptor(InterceptorConstant constant) => abort(constant); | 500 void visitInterceptor(InterceptorConstant constant) => abort(constant); |
| 470 void visitDummy(DummyConstant constant) => abort(constant); | 501 void visitDummy(DummyConstant constant) => abort(constant); |
| 471 void visitFunction(FunctionConstant constant) => abort(constant); | 502 void visitFunction(FunctionConstant constant) => abort(constant); |
| 472 void visitType(TypeConstant constant) => abort(constant); | 503 void visitType(TypeConstant constant) => abort(constant); |
| 473 void visitConstructed(ConstructedConstant constant) => abort(constant); | 504 void visitConstructed(ConstructedConstant constant) => abort(constant); |
| 474 | 505 |
| 475 void abort(Constant value) => throw "Can not pickle constant $value"; | 506 void abort(Constant value) => throw "Can not pickle constant $value"; |
| 476 } | 507 } |
| OLD | NEW |