Index: pkg/kernel/lib/binary/ast_to_binary.dart |
diff --git a/pkg/kernel/lib/binary/ast_to_binary.dart b/pkg/kernel/lib/binary/ast_to_binary.dart |
index 0b6c21f0fb49805322aea0ea46ccb0f0dbbbaf46..f962f916ae18ab98558ebab7584ebdd6db1adee5 100644 |
--- a/pkg/kernel/lib/binary/ast_to_binary.dart |
+++ b/pkg/kernel/lib/binary/ast_to_binary.dart |
@@ -26,6 +26,9 @@ class BinaryPrinter extends Visitor { |
final BufferedSink _sink; |
+ int _binaryOffsetForSourceTable = -1; |
+ int _binaryOffsetForLinkTable = -1; |
+ |
/// Create a printer that writes to the given [sink]. |
/// |
/// The BinaryPrinter will use its own buffer, so the [sink] does not need |
@@ -65,6 +68,13 @@ class BinaryPrinter extends Visitor { |
} |
} |
+ void writeUInt32(int value) { |
+ writeByte((value >> 24) & 0xFF); |
+ writeByte((value >> 16) & 0xFF); |
+ writeByte((value >> 8) & 0xFF); |
+ writeByte(value & 0xFF); |
+ } |
+ |
void writeMagicWord(int value) { |
Kevin Millikin (Google)
2017/06/29 12:27:36
Why replace readMagicWord with readUInt32 but not
jensj
2017/06/30 05:51:17
Done.
|
writeByte((value >> 24) & 0xFF); |
writeByte((value >> 16) & 0xFF); |
@@ -131,6 +141,7 @@ class BinaryPrinter extends Visitor { |
} |
void writeLinkTable(Program program) { |
+ _binaryOffsetForLinkTable = _sink.flushedLength + _sink.length; |
List<CanonicalName> list = <CanonicalName>[]; |
void visitCanonicalName(CanonicalName node) { |
node.index = list.length; |
@@ -181,6 +192,10 @@ class BinaryPrinter extends Visitor { |
writeLinkTable(program); |
writeLibraries(program); |
writeMemberReference(program.mainMethod, allowNull: true); |
+ |
+ // Fixed-size ints at the end. |
+ writeFixedProgramFooter(program, program.libraries); |
Kevin Millikin (Google)
2017/06/29 12:27:36
Adopt a uniform terminology. In the comments this
jensj
2017/06/30 05:51:17
I have uniformly renamed to "program index".
|
+ |
_flush(); |
} |
@@ -194,7 +209,25 @@ class BinaryPrinter extends Visitor { |
writeList(program.libraries, writeNode); |
} |
+ void writeFixedProgramFooter(Program program, List<Library> libraries) { |
+ // Fixed-size ints at the end. |
+ writeUInt32(_binaryOffsetForSourceTable); |
+ writeUInt32(_binaryOffsetForLinkTable); |
+ |
+ CanonicalName main = getCanonicalNameOfMember(program.mainMethod); |
+ if (main == null) { |
+ writeUInt32(0); |
+ } else { |
+ writeUInt32(main.index + 1); |
+ } |
+ for (Library library in libraries) { |
+ writeUInt32(library.binaryOffset); |
+ } |
+ writeUInt32(libraries.length); |
+ } |
+ |
void writeUriToSource(Program program) { |
+ _binaryOffsetForSourceTable = _sink.flushedLength + _sink.length; |
program.uriToSource.keys.forEach((uri) { |
_sourceUriIndexer.put(uri); |
}); |
@@ -280,6 +313,7 @@ class BinaryPrinter extends Visitor { |
visitLibrary(Library node) { |
insideExternalLibrary = node.isExternal; |
+ node.binaryOffset = _sink.flushedLength + _sink.length; |
writeByte(insideExternalLibrary ? 1 : 0); |
writeCanonicalNameReference(getCanonicalNameOfLibrary(node)); |
writeStringReference(node.name ?? ''); |
@@ -1145,8 +1179,14 @@ class LibraryFilteringBinaryPrinter extends BinaryPrinter { |
writeStringTable(stringIndexer); |
writeUriToSource(program); |
writeLinkTable(program); |
- writeList(program.libraries.where(predicate).toList(), writeNode); |
+ final List<Library> filteredLibraries = |
+ program.libraries.where(predicate).toList(); |
+ writeList(filteredLibraries, writeNode); |
writeMemberReference(program.mainMethod, allowNull: true); |
+ |
+ // Fixed-size ints at the end. |
+ writeFixedProgramFooter(program, filteredLibraries); |
+ |
_flush(); |
} |
} |