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

Side by Side Diff: pkg/kernel/lib/binary/ast_from_binary.dart

Issue 2790073004: Restructure the Kernel string table. (Closed)
Patch Set: Created 3 years, 8 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
OLDNEW
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 library kernel.ast_from_binary; 4 library kernel.ast_from_binary;
5 5
6 import 'dart:convert'; 6 import 'dart:convert';
7 import 'dart:typed_data'; 7 import 'dart:typed_data';
8 8
9 import '../ast.dart'; 9 import '../ast.dart';
10 import '../transformations/flags.dart'; 10 import '../transformations/flags.dart';
(...skipping 12 matching lines...) Expand all
23 23
24 class BinaryBuilder { 24 class BinaryBuilder {
25 final List<VariableDeclaration> variableStack = <VariableDeclaration>[]; 25 final List<VariableDeclaration> variableStack = <VariableDeclaration>[];
26 final List<LabeledStatement> labelStack = <LabeledStatement>[]; 26 final List<LabeledStatement> labelStack = <LabeledStatement>[];
27 int labelStackBase = 0; 27 int labelStackBase = 0;
28 final List<SwitchCase> switchCaseStack = <SwitchCase>[]; 28 final List<SwitchCase> switchCaseStack = <SwitchCase>[];
29 final List<TypeParameter> typeParameterStack = <TypeParameter>[]; 29 final List<TypeParameter> typeParameterStack = <TypeParameter>[];
30 final String filename; 30 final String filename;
31 final List<int> _bytes; 31 final List<int> _bytes;
32 int _byteIndex = 0; 32 int _byteIndex = 0;
33 List<String> _stringTable; 33 final List<String> _stringTable = <String>[];
34 List<String> _sourceUriTable; 34 final List<String> _sourceUriTable = <String>[];
35 List<CanonicalName> _linkTable; 35 List<CanonicalName> _linkTable;
36 int _transformerFlags = 0; 36 int _transformerFlags = 0;
37 Library _currentLibrary; 37 Library _currentLibrary;
38 38
39 // If something goes wrong, this list should indicate what library, 39 // If something goes wrong, this list should indicate what library,
40 // class, and member was being built. 40 // class, and member was being built.
41 List<String> debugPath = <String>[]; 41 List<String> debugPath = <String>[];
42 42
43 bool _isReadingLibraryImplementation = false; 43 bool _isReadingLibraryImplementation = false;
44 44
(...skipping 30 matching lines...) Expand all
75 readByte(); 75 readByte();
76 } 76 }
77 77
78 List<int> readUtf8Bytes() { 78 List<int> readUtf8Bytes() {
79 List<int> bytes = new Uint8List(readUInt()); 79 List<int> bytes = new Uint8List(readUInt());
80 bytes.setRange(0, bytes.length, _bytes, _byteIndex); 80 bytes.setRange(0, bytes.length, _bytes, _byteIndex);
81 _byteIndex += bytes.length; 81 _byteIndex += bytes.length;
82 return bytes; 82 return bytes;
83 } 83 }
84 84
85 String readStringEntry() { 85 String readStringEntry(int numBytes) {
86 int numBytes = readUInt();
87 // Utf8Decoder will skip leading BOM characters, but we must preserve them. 86 // Utf8Decoder will skip leading BOM characters, but we must preserve them.
88 // Collect leading BOMs before passing the bytes onto Utf8Decoder. 87 // Collect leading BOMs before passing the bytes onto Utf8Decoder.
89 int numByteOrderMarks = 0; 88 int numByteOrderMarks = 0;
90 while (_byteIndex + 2 < _bytes.length && 89 while (_byteIndex + 2 < _bytes.length &&
91 _bytes[_byteIndex] == 0xef && 90 _bytes[_byteIndex] == 0xef &&
92 _bytes[_byteIndex + 1] == 0xbb && 91 _bytes[_byteIndex + 1] == 0xbb &&
93 _bytes[_byteIndex + 2] == 0xbf) { 92 _bytes[_byteIndex + 2] == 0xbf) {
94 ++numByteOrderMarks; 93 ++numByteOrderMarks;
95 _byteIndex += 3; 94 _byteIndex += 3;
96 numBytes -= 3; 95 numBytes -= 3;
97 } 96 }
98 String string = 97 String string =
99 const Utf8Decoder().convert(_bytes, _byteIndex, _byteIndex + numBytes); 98 const Utf8Decoder().convert(_bytes, _byteIndex, _byteIndex + numBytes);
100 _byteIndex += numBytes; 99 _byteIndex += numBytes;
101 if (numByteOrderMarks > 0) { 100 if (numByteOrderMarks > 0) {
102 return '\ufeff' * numByteOrderMarks + string; 101 return '\ufeff' * numByteOrderMarks + string;
103 } 102 }
104 return string; 103 return string;
105 } 104 }
106 105
107 void readStringTable() { 106 void readStringTable(List<String> table) {
107 // Read the table of end offsets.
108 int length = readUInt(); 108 int length = readUInt();
109 _stringTable = new List<String>(length); 109 List<int> endOffsets = new List<int>(length);
110 for (int i = 0; i < length; ++i) { 110 for (int i = 0; i < length; ++i) {
111 _stringTable[i] = readStringEntry(); 111 endOffsets[i] = readUInt();
112 }
113 // Read the UTF-8 encoded strings.
114 table.length = length;
115 int startOffset = 0;
116 for (int i = 0; i < length; ++i) {
117 table[i] = readStringEntry(endOffsets[i] - startOffset);
118 startOffset = endOffsets[i];
112 } 119 }
113 } 120 }
114 121
115 String readUriReference() { 122 String readUriReference() {
116 return _sourceUriTable[readUInt()]; 123 return _sourceUriTable[readUInt()];
117 } 124 }
118 125
119 void readSourceUriTable() { 126 void readSourceUriTable() {
jensj 2017/04/04 09:42:16 This method isn't called anymore is it? It doesn't
Kevin Millikin (Google) 2017/04/04 11:02:58 You are right. I've removed it.
120 int length = readUInt(); 127 int length = readUInt();
121 _sourceUriTable = new List<String>(length); 128 _sourceUriTable = new List<String>(length);
122 for (int i = 0; i < length; ++i) { 129 for (int i = 0; i < length; ++i) {
123 _sourceUriTable[i] = readStringEntry(); 130 int numBytes = readUInt();
131 _sourceUriTable[i] = readStringEntry(numBytes);
124 } 132 }
125 } 133 }
126 134
127 String readStringReference() { 135 String readStringReference() {
128 return _stringTable[readUInt()]; 136 return _stringTable[readUInt()];
129 } 137 }
130 138
131 String readStringOrNullIfEmpty() { 139 String readStringOrNullIfEmpty() {
132 var string = readStringReference(); 140 var string = readStringReference();
133 return string.isEmpty ? null : string; 141 return string.isEmpty ? null : string;
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
243 throw 'Unrecognized bytes following program data'; 251 throw 'Unrecognized bytes following program data';
244 } 252 }
245 } 253 }
246 254
247 void _readOneProgram(Program program) { 255 void _readOneProgram(Program program) {
248 int magic = readMagicWord(); 256 int magic = readMagicWord();
249 if (magic != Tag.ProgramFile) { 257 if (magic != Tag.ProgramFile) {
250 throw fail('This is not a binary dart file. ' 258 throw fail('This is not a binary dart file. '
251 'Magic number was: ${magic.toRadixString(16)}'); 259 'Magic number was: ${magic.toRadixString(16)}');
252 } 260 }
253 readStringTable(); 261 readStringTable(_stringTable);
254 Map<String, Source> uriToSource = readUriToSource(); 262 Map<String, Source> uriToSource = readUriToSource();
255 program.uriToSource.addAll(uriToSource); 263 program.uriToSource.addAll(uriToSource);
256 readLinkTable(program.root); 264 readLinkTable(program.root);
257 int numberOfLibraries = readUInt(); 265 int numberOfLibraries = readUInt();
258 List<Library> libraries = new List<Library>(numberOfLibraries); 266 List<Library> libraries = new List<Library>(numberOfLibraries);
259 for (int i = 0; i < numberOfLibraries; ++i) { 267 for (int i = 0; i < numberOfLibraries; ++i) {
260 libraries[i] = readLibrary(program); 268 libraries[i] = readLibrary(program);
261 } 269 }
262 var mainMethod = readMemberReference(allowNull: true); 270 var mainMethod = readMemberReference(allowNull: true);
263 program.mainMethodName ??= mainMethod; 271 program.mainMethodName ??= mainMethod;
264 } 272 }
265 273
266 Map<String, Source> readUriToSource() { 274 Map<String, Source> readUriToSource() {
267 readSourceUriTable(); 275 readStringTable(_sourceUriTable);
268 int length = _sourceUriTable.length; 276 int length = _sourceUriTable.length;
269 Map<String, Source> uriToSource = <String, Source>{}; 277 Map<String, Source> uriToSource = <String, Source>{};
270 for (int i = 0; i < length; ++i) { 278 for (int i = 0; i < length; ++i) {
271 String uri = _sourceUriTable[i]; 279 String uri = _sourceUriTable[i];
272 List<int> sourceCode = readUtf8Bytes(); 280 List<int> sourceCode = readUtf8Bytes();
273 int lineCount = readUInt(); 281 int lineCount = readUInt();
274 List<int> lineStarts = new List<int>(lineCount); 282 List<int> lineStarts = new List<int>(lineCount);
275 int previousLineStart = 0; 283 int previousLineStart = 0;
276 for (int j = 0; j < lineCount; ++j) { 284 for (int j = 0; j < lineCount; ++j) {
277 int lineStart = readUInt() + previousLineStart; 285 int lineStart = readUInt() + previousLineStart;
(...skipping 847 matching lines...) Expand 10 before | Expand all | Expand 10 after
1125 ..fileOffset = offset 1133 ..fileOffset = offset
1126 ..fileEqualsOffset = fileEqualsOffset; 1134 ..fileEqualsOffset = fileEqualsOffset;
1127 } 1135 }
1128 1136
1129 int readOffset() { 1137 int readOffset() {
1130 // Offset is saved as unsigned, 1138 // Offset is saved as unsigned,
1131 // but actually ranges from -1 and up (thus the -1) 1139 // but actually ranges from -1 and up (thus the -1)
1132 return readUInt() - 1; 1140 return readUInt() - 1;
1133 } 1141 }
1134 } 1142 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698