Skip to content

Commit 6ad56c6

Browse files
jensjohaCommit Queue
authored andcommitted
[kernel] Remove NNBD compilation mode dummy from kernel binary
Also clean up some of the magic constants around this. TEST=CI Change-Id: Ie02df874cb30761d5ea08ae52c1507cf25318280 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/416940 Commit-Queue: Jens Johansen <[email protected]> Reviewed-by: Johnni Winther <[email protected]> Reviewed-by: Alexander Markov <[email protected]>
1 parent d361b8f commit 6ad56c6

File tree

9 files changed

+57
-60
lines changed

9 files changed

+57
-60
lines changed

pkg/front_end/test/binary_md_dill_reader.dart

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import "dart:math" as math;
66

7+
import "package:kernel/binary/tag.dart" show numberOfFixedFields;
8+
79
class BinaryMdDillReader {
810
final String _binaryMdContent;
911

@@ -121,8 +123,7 @@ class BinaryMdDillReader {
121123
numLibs = _peekUint32();
122124

123125
// Skip to the start of the index.
124-
_binaryOffset = _dillContent.length -
125-
((numLibs + 1) + 12 /* number of fixed fields */) * 4;
126+
_binaryOffset = _dillContent.length - numberOfFixedFields(numLibs) * 4;
126127

127128
// Read index.
128129
binaryOffsetForSourceTable = _peekUint32();
@@ -143,7 +144,6 @@ class BinaryMdDillReader {
143144
_binaryOffset += 4;
144145
mainMethodReference = _peekUint32();
145146
_binaryOffset += 4;
146-
/*int compilationMode = */ _peekUint32();
147147

148148
_binaryOffset = binaryOffsetForStringTable;
149149
var saved = readingInstructions["ComponentFile"]!;
@@ -157,8 +157,7 @@ class BinaryMdDillReader {
157157
Map componentFile = _readBinary("ComponentFile", "");
158158
if (_binaryOffset != _dillContent.length) {
159159
throw "Didn't read the entire binary: "
160-
"Only read $_binaryOffset of ${_dillContent.length} bytes. "
161-
"($componentFile)";
160+
"Only read $_binaryOffset of ${_dillContent.length} bytes.";
162161
}
163162
if (verboseLevel > 0) {
164163
print("Successfully read the dill file.");
@@ -537,8 +536,8 @@ class BinaryMdDillReader {
537536
} else if (what == "ComponentIndex" &&
538537
instruction == "Byte[] 8bitAlignment;") {
539538
// Special-case 8-byte alignment.
540-
int sizeWithoutPadding = _binaryOffset +
541-
((numLibs + 1) + 10 /* number of fixed fields */) * 4;
539+
int sizeWithoutPadding =
540+
_binaryOffset + numberOfFixedFields(numLibs) * 4;
542541
int padding = 8 - sizeWithoutPadding % 8;
543542
if (padding == 8) padding = 0;
544543
_binaryOffset += padding;

pkg/kernel/binary.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ type CanonicalName {
147147

148148
type ComponentFile {
149149
UInt32 magic = 0x90ABCDEF;
150-
UInt32 formatVersion = 123;
150+
UInt32 formatVersion = 124;
151151
Byte[10] shortSdkHash;
152152
List<String> problemsAsJson; // Described in problems.md.
153153
Library[] libraries;
@@ -188,7 +188,6 @@ type ComponentIndex {
188188
UInt32 binaryOffsetForStringTable;
189189
UInt32 binaryOffsetForStartOfComponentIndex;
190190
UInt32 mainMethodReference; // This is a ProcedureReference with a fixed-size integer.
191-
UInt32 _dummy; // TODO(jensj): Previously the component mode. Remove this.
192191
UInt32[libraryCount + 1] libraryOffsets;
193192
UInt32 libraryCount;
194193
UInt32 componentFileSizeInBytes;

pkg/kernel/lib/binary/ast_from_binary.dart

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@ class CompilationModeError {
6767
}
6868

6969
class _ComponentIndex {
70-
static const int numberOfFixedFields = 12;
71-
7270
final int binaryOffsetForSourceTable;
7371
final int binaryOffsetForCanonicalNames;
7472
final int binaryOffsetForMetadataPayloads;
@@ -790,8 +788,7 @@ class BinaryBuilder {
790788
}
791789

792790
// Skip to the start of the index.
793-
_byteOffset -=
794-
((libraryCount + 1) + _ComponentIndex.numberOfFixedFields) * 4;
791+
_byteOffset -= numberOfFixedFields(libraryCount) * 4;
795792

796793
// Now read the component index.
797794
int binaryOffsetForSourceTable = _componentStartOffset + readUint32();
@@ -805,8 +802,6 @@ class BinaryBuilder {
805802
int binaryOffsetForStartOfComponentIndex =
806803
_componentStartOffset + readUint32();
807804
int mainMethodReference = readUint32();
808-
// TODO(jensj): Previously the component mode. Remove this.
809-
readUint32();
810805
for (int i = 0; i < libraryCount + 1; ++i) {
811806
libraryOffsets[i] = _componentStartOffset + readUint32();
812807
}

pkg/kernel/lib/binary/ast_to_binary.dart

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -805,19 +805,18 @@ class BinaryPrinter implements Visitor<void>, BinarySink {
805805
// is added before component index.
806806
const int kernelFileAlignment = 8;
807807

808-
// Keep this in sync with number of writeUInt32 below.
809-
int numComponentIndexEntries = 10 + libraryOffsets.length + 3;
810808
int componentIndexOffset = getBufferOffset();
811-
812-
int unalignedSize = componentIndexOffset + numComponentIndexEntries * 4;
809+
int unalignedSize =
810+
componentIndexOffset + numberOfFixedFields(libraryOffsets.length) * 4;
813811
int padding =
814812
((unalignedSize + kernelFileAlignment - 1) & -kernelFileAlignment) -
815813
unalignedSize;
816814
for (int i = 0; i < padding; ++i) {
817815
writeByte(0);
818816
}
819817

820-
// Fixed-size ints at the end used as an index.
818+
// Fixed-size ints at the end used as an index. Including main there's
819+
// [fixedFieldsBeforeLibraries] fields.
821820
assert(_binaryOffsetForSourceTable >= 0);
822821
writeUInt32(_binaryOffsetForSourceTable);
823822
assert(_binaryOffsetForConstantTable >= 0);
@@ -843,16 +842,17 @@ class BinaryPrinter implements Visitor<void>, BinarySink {
843842
_ensureCanonicalName(getNonNullableMemberReferenceGetter(mainMethod));
844843
writeUInt32(main.index + 1);
845844
}
846-
// TODO(jensj): Previously the component mode. Remove this.
847-
writeUInt32(0);
848845

846+
// Offset for the libraries.
849847
assert(libraryOffsets.length == libraries.length);
850848
for (int offset in libraryOffsets) {
851849
writeUInt32(offset);
852850
}
853-
writeUInt32(_binaryOffsetForSourceTable); // end of last library.
854-
writeUInt32(libraries.length);
851+
// and the end of the last library.
852+
writeUInt32(_binaryOffsetForSourceTable);
855853

854+
// And an additional [fixedFieldsAfterLibraries] fields.
855+
writeUInt32(libraries.length);
856856
writeUInt32(getBufferOffset() + 4); // total size.
857857
}
858858

pkg/kernel/lib/binary/tag.dart

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ class Tag {
226226
/// Internal version of kernel binary format.
227227
/// Bump it when making incompatible changes in kernel binaries.
228228
/// Keep in sync with runtime/vm/kernel_binary.h, pkg/kernel/binary.md.
229-
static const int BinaryFormatVersion = 123;
229+
static const int BinaryFormatVersion = 124;
230230
}
231231

232232
abstract class ConstantTag {
@@ -274,3 +274,14 @@ bool isValidSdkHash(String sdkHash) {
274274
expectedSdkHash == sdkHashNull ||
275275
sdkHash == expectedSdkHash);
276276
}
277+
278+
/// These should match with what is written in
279+
/// BinaryPrinter.writeComponentIndex.
280+
const int fixedFieldsBeforeLibraries = 9;
281+
const int fixedFieldsAfterLibraries = 2;
282+
int numberOfFixedFields(int numberOfLibraries) {
283+
return fixedFieldsBeforeLibraries +
284+
numberOfLibraries +
285+
1 +
286+
fixedFieldsAfterLibraries;
287+
}

runtime/vm/compiler/frontend/kernel_translation_helper.cc

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3095,34 +3095,22 @@ TokenPosition KernelReaderHelper::ReadPosition() {
30953095

30963096
intptr_t KernelReaderHelper::SourceTableSize() {
30973097
AlternativeReadingScope alt(&reader_);
3098-
intptr_t library_count = reader_.ReadFromIndexNoReset(
3099-
reader_.size(), LibraryCountFieldCountFromEnd, 1, 0);
3098+
intptr_t library_count = reader_.ReadSingleFieldFromIndexNoReset(
3099+
reader_.size(), KernelFixedFieldsAfterLibraries);
31003100

3101-
const intptr_t count_from_first_library_offset =
3102-
SourceTableFieldCountFromFirstLibraryOffset;
3103-
3104-
intptr_t source_table_offset = reader_.ReadFromIndexNoReset(
3105-
reader_.size(),
3106-
LibraryCountFieldCountFromEnd + 1 + library_count + 1 +
3107-
count_from_first_library_offset,
3108-
1, 0);
3101+
intptr_t source_table_offset = reader_.ReadSingleFieldFromIndexNoReset(
3102+
reader_.size(), KernelNumberOfFixedFields(library_count));
31093103
SetOffset(source_table_offset); // read source table offset.
31103104
return reader_.ReadUInt32(); // read source table size.
31113105
}
31123106

31133107
intptr_t KernelReaderHelper::GetOffsetForSourceInfo(intptr_t index) {
31143108
AlternativeReadingScope alt(&reader_);
3115-
intptr_t library_count = reader_.ReadFromIndexNoReset(
3116-
reader_.size(), LibraryCountFieldCountFromEnd, 1, 0);
3117-
3118-
const intptr_t count_from_first_library_offset =
3119-
SourceTableFieldCountFromFirstLibraryOffset;
3109+
intptr_t library_count = reader_.ReadSingleFieldFromIndexNoReset(
3110+
reader_.size(), KernelFixedFieldsAfterLibraries);
31203111

3121-
intptr_t source_table_offset = reader_.ReadFromIndexNoReset(
3122-
reader_.size(),
3123-
LibraryCountFieldCountFromEnd + 1 + library_count + 1 +
3124-
count_from_first_library_offset,
3125-
1, 0);
3112+
intptr_t source_table_offset = reader_.ReadSingleFieldFromIndexNoReset(
3113+
reader_.size(), KernelNumberOfFixedFields(library_count));
31263114
intptr_t next_field_offset = reader_.ReadUInt32();
31273115
SetOffset(source_table_offset);
31283116
intptr_t size = reader_.ReadUInt32(); // read source table size.

runtime/vm/kernel_binary.cc

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -154,15 +154,10 @@ std::unique_ptr<Program> Program::ReadFrom(Reader* reader, const char** error) {
154154
program->single_program_ = subprogram_count == 1;
155155

156156
// Read backwards at the end.
157-
program->library_count_ = reader->ReadFromIndexNoReset(
158-
reader->size_, LibraryCountFieldCountFromEnd, 1, 0);
159-
intptr_t count_from_first_library_offset =
160-
SourceTableFieldCountFromFirstLibraryOffset;
161-
program->source_table_offset_ = reader->ReadFromIndexNoReset(
162-
reader->size_,
163-
LibraryCountFieldCountFromEnd + 1 + program->library_count_ + 1 +
164-
count_from_first_library_offset,
165-
1, 0);
157+
program->library_count_ = reader->ReadSingleFieldFromIndexNoReset(
158+
reader->size_, KernelFixedFieldsAfterLibraries);
159+
program->source_table_offset_ = reader->ReadSingleFieldFromIndexNoReset(
160+
reader->size_, KernelNumberOfFixedFields(program->library_count_));
166161
program->constant_table_offset_ = reader->ReadUInt32();
167162
reader->ReadUInt32(); // offset for constant table index.
168163
program->name_table_offset_ = reader->ReadUInt32();
@@ -174,8 +169,6 @@ std::unique_ptr<Program> Program::ReadFrom(Reader* reader, const char** error) {
174169
program->component_index_offset_ = reader->ReadUInt32();
175170

176171
program->main_method_reference_ = NameIndex(reader->ReadUInt32() - 1);
177-
// TODO(jensj): Remove this.
178-
reader->ReadUInt32(); // Read and ignore NNBD compilation mode.
179172

180173
return program;
181174
}

runtime/vm/kernel_binary.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace kernel {
1818
// package:kernel/binary.md.
1919

2020
static const uint32_t kMagicProgramFile = 0x90ABCDEFu;
21-
static const uint32_t kSupportedKernelFormatVersion = 123;
21+
static const uint32_t kSupportedKernelFormatVersion = 124;
2222

2323
// Keep in sync with package:kernel/lib/binary/tag.dart
2424
#define KERNEL_TAG_LIST(V) \
@@ -271,9 +271,15 @@ enum class FunctionAccessKind {
271271
};
272272

273273
static constexpr int SpecializedIntLiteralBias = 3;
274-
static constexpr int LibraryCountFieldCountFromEnd = 1;
275274
static constexpr int KernelFormatVersionOffset = 4;
276-
static constexpr int SourceTableFieldCountFromFirstLibraryOffset = 9;
275+
276+
// These should be kept in sync with the constants in kernels tag.dart.
277+
static constexpr int KernelFixedFieldsBeforeLibraries = 9;
278+
static constexpr int KernelFixedFieldsAfterLibraries = 2;
279+
static inline int KernelNumberOfFixedFields(int numberOfLibraries) {
280+
return KernelFixedFieldsBeforeLibraries + numberOfLibraries + 1 +
281+
KernelFixedFieldsAfterLibraries;
282+
}
277283

278284
static constexpr int HeaderSize = 8; // 'magic', 'formatVersion'.
279285

@@ -310,6 +316,12 @@ class Reader : public ValueObject {
310316
return ReadUInt32();
311317
}
312318

319+
uint32_t ReadSingleFieldFromIndexNoReset(intptr_t end_offset,
320+
intptr_t fields_before) {
321+
offset_ = end_offset - fields_before * 4;
322+
return ReadUInt32();
323+
}
324+
313325
uint32_t ReadUInt32() {
314326
uint32_t value = ReadUInt32At(offset_);
315327
offset_ += 4;

runtime/vm/kernel_loader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ class KernelLoader : public ValueObject {
273273
intptr_t library_offset(intptr_t index) {
274274
kernel::Reader reader(program_->binary());
275275
return reader.ReadFromIndexNoReset(reader.size(),
276-
LibraryCountFieldCountFromEnd + 1,
276+
KernelFixedFieldsAfterLibraries,
277277
program_->library_count() + 1, index);
278278
}
279279

0 commit comments

Comments
 (0)