2021SC@SDUSC
对GlobalTables做一个简单的分析。
1、GlobalTables定义和实现在src/maple_ir/include/global_tables.h和src/maple_ir/src/global_tables.cpp之中。
2、GlobalTables的成员函数主要是从GlobalTables::globalTables获取自己需要的数据,具体代码如下:
static StringTable<std::string, GStrIdx> &GetStrTable() {
return globalTables.gStringTable;
}
static StringTable<std::string, UStrIdx> &GetUStrTable() {
return globalTables.uStrTable;
}
static StringTable<std::u16string, U16StrIdx> &GetU16StrTable() {
return globalTables.u16StringTable;
}
static TypeTable &GetTypeTable() {
return globalTables.typeTable;
}
static FPConstTable &GetFpConstTable() {
return *(globalTables.fpConstTablePtr);
}
static STypeNameTable &GetTypeNameTable() {
return globalTables.typeNameTable;
}
static FunctionTable &GetFunctionTable() {
return globalTables.functionTable;
}
static GSymbolTable &GetGsymTable() {
return globalTables.gSymbolTable;
}
static ConstPool &GetConstPool() {
return globalTables.constPool;
}
3、globalTables是GlobalTables的一个静态成员变量,其定义如下:
static GlobalTables globalTables;
所以,才会出现
void KlassHierarchy::AddKlasses() {
for (MIRType *type : GlobalTables::GetTypeTable().GetTypeTable()) {
这样的引用。直接通过GlobalTables::GetTypeTable()获取globalTables.typeTable:
static TypeTable &GetTypeTable() {
return globalTables.typeTable;
}
而globalTables.typeTable是 TypeTable类型的。TypeTable和GlobalTables定义在同个文件,其GetTypeTable的实现如下:
std::vector<MIRType*> &GetTypeTable() {
return typeTable;
}
所以, GlobalTables::GetTypeTable().GetTypeTable()最终返回了std::vector<MIRType*>,这也就解释了AddKlasses中的使用方式。
4、GlobalTables的成员变量主要有以下这些:
TypeTable typeTable;
STypeNameTable typeNameTable;
FunctionTable functionTable;
GSymbolTable gSymbolTable;
ConstPool constPool;
std::unique_ptr fpConstTablePtr;
StringTable<std::string, GStrIdx> gStringTable;
StringTable<std::string, UStrIdx> uStrTable;
StringTable<std::u16string, U16StrIdx> u16StringTable;
static GlobalTables globalTables;
这里涉及到的TypeTable、STypeNameTable、FunctionTable、GSymbolTable 和ConstPool 类,都是在GlobalTables的同个文件中定义的。
5、TypeTable 类中比较重要的是它的成员变量:
std::vector<MIRType*> typeTable;
它内部的成员函数,也多是围绕该成员变量的操作。
6、STypeNameTable类主要是用来存储类和接口类型。其重要的成员变量是:
std::unordered_map<GStrIdx, TyIdx, GStrIdxHash> gStrIdxToTyIdxMap;
7、FunctionTable是一个用来存储MIRFunction的类。其重要的成员变量是:
std::vector<MIRFunction*> funcTable; // index is PUIdx
8、GSymbolTable 是一个用来存储MIRSymbol的类。其重要的成员变量是:
// hash table mapping string index to st index
std::unordered_map<GStrIdx, StIdx, GStrIdxHash> strIdxToStIdxMap;
std::vector<MIRSymbol*> symbolTable; // map symbol idx to symbol node
9、ConstPool类主要是用来存储MIRConst的类。其重要的成员变量是:
protected:
std::unordered_map<GStrIdx, MIRConst*, GStrIdxHash> constMap;
std::set importedLiteralNames;
private:
std::unordered_map<std::u16string, MIRSymbol*> constU16StringPool;
参考自:小乖他爹