Skip to content

Commit d84a149

Browse files
committed
refactor integers in bitstream
1 parent d9c898c commit d84a149

File tree

2 files changed

+89
-59
lines changed

2 files changed

+89
-59
lines changed

source/lib/ast/BitcodeWriter.cpp

Lines changed: 67 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ struct RecordIdToIndexFunctor
4141
}
4242
};
4343

44+
//------------------------------------------------
45+
//
46+
// Abbrev
47+
//
48+
//------------------------------------------------
49+
4450
using AbbrevDsc = void (*)(std::shared_ptr<llvm::BitCodeAbbrev>& Abbrev);
4551

4652
static void AbbrevGen(
@@ -51,24 +57,32 @@ static void AbbrevGen(
5157
Abbrev->Add(Op);
5258
}
5359

54-
static void BoolAbbrev(
60+
static void Integer32Abbrev(
5561
std::shared_ptr<llvm::BitCodeAbbrev>& Abbrev)
5662
{
5763
AbbrevGen(Abbrev, {
58-
// 0. Boolean
64+
// 0. 32-bit signed or unsigned integer
5965
llvm::BitCodeAbbrevOp(
60-
llvm::BitCodeAbbrevOp::Fixed,
61-
BitCodeConstants::BoolSize) });
66+
llvm::BitCodeAbbrevOp::Fixed, 32) });
6267
}
6368

64-
static void IntAbbrev(
69+
static void Integer16Abbrev(
6570
std::shared_ptr<llvm::BitCodeAbbrev>& Abbrev)
6671
{
6772
AbbrevGen(Abbrev, {
68-
// 0. Fixed-size integer
73+
// 0. 16-bit signed or unsigned integer
74+
llvm::BitCodeAbbrevOp(
75+
llvm::BitCodeAbbrevOp::Fixed, 16) });
76+
}
77+
78+
static void BoolAbbrev(
79+
std::shared_ptr<llvm::BitCodeAbbrev>& Abbrev)
80+
{
81+
AbbrevGen(Abbrev, {
82+
// 0. Boolean
6983
llvm::BitCodeAbbrevOp(
7084
llvm::BitCodeAbbrevOp::Fixed,
71-
BitCodeConstants::IntSize) });
85+
BitCodeConstants::BoolSize) });
7286
}
7387

7488
static void SymbolIDAbbrev(
@@ -182,16 +196,16 @@ RecordIdNameMap = []()
182196
// There is no init-list constructor for the IndexedMap, so have to
183197
// improvise
184198
static const std::vector<std::pair<RecordId, RecordIdDsc>> Inits = {
185-
{VERSION, {"Version", &IntAbbrev}},
186-
{JAVADOC_LIST_KIND, {"JavadocListKind", &IntAbbrev}},
187-
{JAVADOC_NODE_KIND, {"JavadocNodeKind", &IntAbbrev}},
199+
{VERSION, {"Version", &Integer32Abbrev}},
200+
{JAVADOC_LIST_KIND, {"JavadocListKind", &Integer32Abbrev}},
201+
{JAVADOC_NODE_KIND, {"JavadocNodeKind", &Integer32Abbrev}},
188202
{JAVADOC_NODE_STRING, {"JavadocNodeString", &StringAbbrev}},
189-
{JAVADOC_NODE_STYLE, {"JavadocNodeStyle", &IntAbbrev}},
190-
{JAVADOC_NODE_ADMONISH, {"JavadocNodeAdmonish", &IntAbbrev}},
203+
{JAVADOC_NODE_STYLE, {"JavadocNodeStyle", &Integer32Abbrev}},
204+
{JAVADOC_NODE_ADMONISH, {"JavadocNodeAdmonish", &Integer32Abbrev}},
191205
{FIELD_TYPE_NAME, {"Name", &StringAbbrev}},
192206
{FIELD_DEFAULT_VALUE, {"DefaultValue", &StringAbbrev}},
193207
{MEMBER_TYPE_NAME, {"Name", &StringAbbrev}},
194-
{MEMBER_TYPE_ACCESS, {"Access", &IntAbbrev}},
208+
{MEMBER_TYPE_ACCESS, {"Access", &Integer32Abbrev}},
195209
{NAMESPACE_USR, {"USR", &SymbolIDAbbrev}},
196210
{NAMESPACE_NAME, {"Name", &StringAbbrev}},
197211
{ENUM_USR, {"USR", &SymbolIDAbbrev}},
@@ -206,25 +220,25 @@ RecordIdNameMap = []()
206220
{RECORD_NAME, {"Name", &StringAbbrev}},
207221
{RECORD_DEFLOCATION, {"DefLocation", &LocationAbbrev}},
208222
{RECORD_LOCATION, {"Location", &LocationAbbrev}},
209-
{RECORD_TAG_TYPE, {"TagType", &IntAbbrev}},
223+
{RECORD_TAG_TYPE, {"TagType", &Integer32Abbrev}},
210224
{RECORD_IS_TYPE_DEF, {"IsTypeDef", &BoolAbbrev}},
211225
{BASE_RECORD_USR, {"USR", &SymbolIDAbbrev}},
212226
{BASE_RECORD_NAME, {"Name", &StringAbbrev}},
213-
{BASE_RECORD_TAG_TYPE, {"TagType", &IntAbbrev}},
227+
{BASE_RECORD_TAG_TYPE, {"TagType", &Integer32Abbrev}},
214228
{BASE_RECORD_IS_VIRTUAL, {"IsVirtual", &BoolAbbrev}},
215-
{BASE_RECORD_ACCESS, {"Access", &IntAbbrev}},
229+
{BASE_RECORD_ACCESS, {"Access", &Integer32Abbrev}},
216230
{BASE_RECORD_IS_PARENT, {"IsParent", &BoolAbbrev}},
217231
{FUNCTION_USR, {"USR", &SymbolIDAbbrev}},
218232
{FUNCTION_NAME, {"Name", &StringAbbrev}},
219233
{FUNCTION_DEFLOCATION, {"DefLocation", &LocationAbbrev}},
220234
{FUNCTION_LOCATION, {"Location", &LocationAbbrev}},
221-
{FUNCTION_ACCESS, {"Access", &IntAbbrev}},
235+
{FUNCTION_ACCESS, {"Access", &Integer32Abbrev}},
222236
{FUNCTION_IS_METHOD, {"IsMethod", &BoolAbbrev}},
223-
{FUNCTION_BITS, {"Specs", &IntAbbrev}},
237+
{FUNCTION_BITS, {"Specs", &Integer16Abbrev}},
224238
{REFERENCE_USR, {"USR", &SymbolIDAbbrev}},
225239
{REFERENCE_NAME, {"Name", &StringAbbrev}},
226-
{REFERENCE_TYPE, {"RefType", &IntAbbrev}},
227-
{REFERENCE_FIELD, {"Field", &IntAbbrev}},
240+
{REFERENCE_TYPE, {"RefType", &Integer32Abbrev}},
241+
{REFERENCE_FIELD, {"Field", &Integer32Abbrev}},
228242
{TEMPLATE_PARAM_CONTENTS, {"Contents", &StringAbbrev}},
229243
{TEMPLATE_SPECIALIZATION_OF, {"SpecializationOf", &SymbolIDAbbrev}},
230244
{TYPEDEF_USR, {"USR", &SymbolIDAbbrev}},
@@ -726,17 +740,46 @@ emitAbbrev(
726740

727741
//------------------------------------------------
728742
//
729-
// emitRecord
743+
// Records
730744
//
731745
//------------------------------------------------
732746

733-
template<class Enum, class>
747+
// 16 and 32 bit integers
748+
template<class Integer>
749+
requires std::is_integral_v<Integer>
750+
void
751+
BitcodeWriter::
752+
emitRecord(
753+
Integer Value, RecordId ID)
754+
{
755+
Assert(RecordIdNameMap[ID]);
756+
if constexpr(sizeof(Integer) == 4)
757+
{
758+
Assert(RecordIdNameMap[ID].Abbrev == &Integer32Abbrev);
759+
}
760+
else if constexpr(sizeof(Integer) == 2)
761+
{
762+
Assert(RecordIdNameMap[ID].Abbrev == &Integer16Abbrev);
763+
}
764+
else
765+
{
766+
static_assert("can't use Integer type");
767+
}
768+
if (!prepRecordData(ID, Value))
769+
return;
770+
Record.push_back(static_cast<RecordValue>(Value));
771+
Stream.EmitRecordWithAbbrev(Abbrevs.get(ID), Record);
772+
}
773+
774+
// enumerations
775+
template<class Enum>
776+
requires std::is_enum_v<Enum>
734777
void
735778
BitcodeWriter::
736779
emitRecord(
737-
Enum value, RecordId ID)
780+
Enum Value, RecordId ID)
738781
{
739-
emitRecord(static_cast<std::underlying_type_t<Enum>>(value), ID);
782+
emitRecord(static_cast<std::underlying_type_t<Enum>>(Value), ID);
740783
}
741784

742785
void
@@ -803,34 +846,6 @@ emitRecord(
803846
Stream.EmitRecordWithAbbrev(Abbrevs.get(ID), Record);
804847
}
805848

806-
void
807-
BitcodeWriter::
808-
emitRecord(
809-
int Val, RecordId ID)
810-
{
811-
Assert(RecordIdNameMap[ID] && "Unknown RecordId.");
812-
Assert(RecordIdNameMap[ID].Abbrev == &IntAbbrev && "Abbrev type mismatch.");
813-
if (!prepRecordData(ID, Val))
814-
return;
815-
// FIXME: Assert that the integer is of the appropriate size.
816-
Record.push_back(Val);
817-
Stream.EmitRecordWithAbbrev(Abbrevs.get(ID), Record);
818-
}
819-
820-
void
821-
BitcodeWriter::
822-
emitRecord(
823-
unsigned Val, RecordId ID)
824-
{
825-
Assert(RecordIdNameMap[ID] && "Unknown RecordId.");
826-
Assert(RecordIdNameMap[ID].Abbrev == &IntAbbrev && "Abbrev type mismatch.");
827-
if (!prepRecordData(ID, Val))
828-
return;
829-
Assert(Val < (1U << BitCodeConstants::IntSize));
830-
Record.push_back(Val);
831-
Stream.EmitRecordWithAbbrev(Abbrevs.get(ID), Record);
832-
}
833-
834849
void
835850
BitcodeWriter::
836851
emitRecord(

source/lib/ast/BitcodeWriter.hpp

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,27 @@ class BitcodeWriter
7070
void emitBlockID(BlockId ID);
7171
void emitBlockInfo(BlockId BID, const std::vector<RecordId> &RIDs);
7272

73-
// Emission of individual record types.
73+
//--------------------------------------------
74+
//
75+
// Records
76+
//
77+
//--------------------------------------------
78+
79+
template<class Integer>
80+
requires
81+
std::is_integral_v<Integer> &&
82+
(sizeof(Integer) > 4)
83+
void emitRecord(Integer Value, RecordId ID) = delete;
7484

75-
template<class Enum, class =
76-
std::enable_if_t<std::is_enum_v<Enum>>>
77-
void emitRecord(Enum value, RecordId ID);
85+
template<class Integer>
86+
requires std::is_integral_v<Integer>
87+
void emitRecord(Integer Value, RecordId ID);
88+
89+
template<class Enum>
90+
requires std::is_enum_v<Enum>
91+
void emitRecord(Enum Value, RecordId ID);
7892

7993
void emitRecord(bool Value, RecordId ID);
80-
void emitRecord(int Value, RecordId ID);
81-
void emitRecord(unsigned Value, RecordId ID);
8294
void emitRecord(StringRef Str, RecordId ID);
8395
void emitRecord(SymbolID const& Str, RecordId ID);
8496
void emitRecord(Location const& Loc, RecordId ID);
@@ -87,6 +99,8 @@ class BitcodeWriter
8799

88100
bool prepRecordData(RecordId ID, bool ShouldEmit = true);
89101

102+
//--------------------------------------------
103+
90104
// Emission of appropriate abbreviation type.
91105
void emitAbbrev(RecordId ID, BlockId Block);
92106

@@ -135,8 +149,9 @@ class BitcodeWriter
135149
// to this + 1. Longest is currently
136150
// `MemberTypeBlock` at 15 chars.
137151
//
152+
using RecordValue = std::uint32_t;
138153
SmallVector<
139-
uint32_t,
154+
RecordValue,
140155
BitCodeConstants::RecordSize> Record;
141156
llvm::BitstreamWriter& Stream;
142157
AbbreviationMap Abbrevs;

0 commit comments

Comments
 (0)