forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeserializeSIL.h
194 lines (157 loc) · 7.28 KB
/
DeserializeSIL.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
//===--- DeserializeSIL.h - Read SIL ----------------------------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "SILFormat.h"
#include "swift/SIL/SILModule.h"
#include "swift/Serialization/ModuleFile.h"
#include "swift/Serialization/SerializedSILLoader.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/Support/SaveAndRestore.h"
namespace llvm {
template <typename Info> class OnDiskIterableChainedHashTable;
}
namespace swift {
class SILDeserializer {
using TypeID = serialization::TypeID;
ModuleFile *MF;
SILModule &SILMod;
DeserializationNotificationHandlerSet *Callback;
/// The cursor used to lazily load SILFunctions.
llvm::BitstreamCursor SILCursor;
llvm::BitstreamCursor SILIndexCursor;
class FuncTableInfo;
using SerializedFuncTable =
llvm::OnDiskIterableChainedHashTable<FuncTableInfo>;
std::unique_ptr<SerializedFuncTable> FuncTable;
std::vector<ModuleFile::PartiallySerialized<SILFunction*>> Funcs;
std::unique_ptr<SerializedFuncTable> VTableList;
std::vector<ModuleFile::Serialized<SILVTable*>> VTables;
std::unique_ptr<SerializedFuncTable> GlobalVarList;
std::vector<ModuleFile::Serialized<SILGlobalVariable*>> GlobalVars;
std::unique_ptr<SerializedFuncTable> WitnessTableList;
std::vector<ModuleFile::PartiallySerialized<SILWitnessTable *>>
WitnessTables;
std::unique_ptr<SerializedFuncTable> DefaultWitnessTableList;
std::vector<ModuleFile::PartiallySerialized<SILDefaultWitnessTable *>>
DefaultWitnessTables;
std::vector<ModuleFile::PartiallySerialized<SILProperty *>>
Properties;
/// A declaration will only
llvm::DenseMap<NormalProtocolConformance *, SILWitnessTable *>
ConformanceToWitnessTableMap;
/// Data structures used to perform name lookup for local values.
llvm::DenseMap<uint32_t, ValueBase*> LocalValues;
llvm::DenseMap<uint32_t, ValueBase*> ForwardLocalValues;
serialization::ValueID LastValueID = 0;
/// Data structures used to perform lookup of basic blocks.
llvm::DenseMap<unsigned, SILBasicBlock*> BlocksByID;
llvm::DenseMap<SILBasicBlock*, unsigned> UndefinedBlocks;
unsigned BasicBlockID = 0;
/// Return the SILBasicBlock of a given ID.
SILBasicBlock *getBBForReference(SILFunction *Fn, unsigned ID);
SILBasicBlock *getBBForDefinition(SILFunction *Fn, SILBasicBlock *Prev,
unsigned ID);
/// Read a SIL function.
SILFunction *readSILFunction(serialization::DeclID, SILFunction *InFunc,
StringRef Name, bool declarationOnly,
bool errorIfEmptyBody = true);
/// Read a SIL function.
llvm::Expected<SILFunction *>
readSILFunctionChecked(serialization::DeclID, SILFunction *InFunc,
StringRef Name, bool declarationOnly,
bool errorIfEmptyBody = true);
/// Read a SIL basic block within a given SIL function.
SILBasicBlock *readSILBasicBlock(SILFunction *Fn,
SILBasicBlock *Prev,
SmallVectorImpl<uint64_t> &scratch);
/// Read a SIL instruction within a given SIL basic block.
bool readSILInstruction(SILFunction *Fn, SILBasicBlock *BB,
SILBuilder &Builder,
unsigned RecordKind,
SmallVectorImpl<uint64_t> &scratch);
/// Read the SIL function table.
std::unique_ptr<SerializedFuncTable>
readFuncTable(ArrayRef<uint64_t> fields, StringRef blobData);
/// When an instruction or block argument is defined, this method is used to
/// register it and update our symbol table.
void setLocalValue(ValueBase *Value, serialization::ValueID Id);
/// Get a reference to a local value with the specified ID and type.
SILValue getLocalValue(serialization::ValueID Id,
SILType Type);
SILFunction *getFuncForReference(StringRef Name, SILType Ty);
SILFunction *getFuncForReference(StringRef Name);
SILVTable *readVTable(serialization::DeclID);
SILGlobalVariable *getGlobalForReference(StringRef Name);
SILGlobalVariable *readGlobalVar(StringRef Name);
SILWitnessTable *readWitnessTable(serialization::DeclID,
SILWitnessTable *existingWt);
SILProperty *readProperty(serialization::DeclID);
SILDefaultWitnessTable *
readDefaultWitnessTable(serialization::DeclID,
SILDefaultWitnessTable *existingWt);
Optional<KeyPathPatternComponent>
readKeyPathComponent(ArrayRef<uint64_t> ListOfValues, unsigned &nextValue);
public:
Identifier getModuleIdentifier() const {
return MF->getAssociatedModule()->getName();
}
FileUnit *getFile() const {
return MF->getFile();
}
SILFunction *lookupSILFunction(SILFunction *InFunc);
SILFunction *lookupSILFunction(StringRef Name,
bool declarationOnly = false);
bool hasSILFunction(StringRef Name, Optional<SILLinkage> Linkage = None);
SILVTable *lookupVTable(Identifier Name);
SILWitnessTable *lookupWitnessTable(SILWitnessTable *wt);
SILDefaultWitnessTable *
lookupDefaultWitnessTable(SILDefaultWitnessTable *wt);
/// Invalidate all cached SILFunctions.
void invalidateFunctionCache();
/// Invalidate a specific cached SILFunction.
bool invalidateFunction(SILFunction *F);
/// Deserialize all SILFunctions, VTables, WitnessTables, and
/// DefaultWitnessTables inside the module, and add them to SILMod.
///
/// TODO: Globals.
void getAll(bool UseCallback = true) {
llvm::SaveAndRestore<DeserializationNotificationHandlerSet *> SaveCB(
Callback);
if (!UseCallback)
Callback = nullptr;
getAllSILFunctions();
getAllSILGlobalVariables();
getAllVTables();
getAllWitnessTables();
getAllDefaultWitnessTables();
getAllProperties();
}
/// Deserialize all SILFunctions inside the module and add them to SILMod.
void getAllSILFunctions();
/// Deserialize all SILGlobalVariables inside the module and add them to
/// SILMod.
void getAllSILGlobalVariables();
/// Deserialize all VTables inside the module and add them to SILMod.
void getAllVTables();
/// Deserialize all WitnessTables inside the module and add them to SILMod.
void getAllWitnessTables();
/// Deserialize all DefaultWitnessTables inside the module and add them
/// to SILMod.
void getAllDefaultWitnessTables();
/// Deserialize all Property descriptors inside the module and add them
/// to SILMod.
void getAllProperties();
SILDeserializer(ModuleFile *MF, SILModule &M,
DeserializationNotificationHandlerSet *callback);
// Out of line to avoid instantiation OnDiskChainedHashTable here.
~SILDeserializer();
};
} // end namespace swift