Line | Branch | Exec | Source |
---|---|---|---|
1 | // Copyright (c) 2021-2024 ChilliBits. All rights reserved. | ||
2 | |||
3 | #include "IRGenerator.h" | ||
4 | |||
5 | #include <llvm/IR/Module.h> | ||
6 | |||
7 | #include <SourceFile.h> | ||
8 | #include <irgenerator/NameMangling.h> | ||
9 | |||
10 | namespace spice::compiler { | ||
11 | |||
12 | 249 | llvm::Constant *IRGenerator::generateTypeInfoName(StructBase *spiceStruct) const { | |
13 | // Resolve mangled type info name and mangled global name | ||
14 |
1/2✓ Branch 1 taken 249 times.
✗ Branch 2 not taken.
|
249 | const std::string globalName = NameMangling::mangleTypeInfoName(spiceStruct); |
15 | |||
16 | // Generate global string constant | ||
17 |
3/6✓ Branch 1 taken 249 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 249 times.
✗ Branch 5 not taken.
✓ Branch 8 taken 249 times.
✗ Branch 9 not taken.
|
249 | builder.CreateGlobalStringPtr(NameMangling::mangleTypeInfoValue(spiceStruct->name), globalName, 0, module); |
18 |
1/2✓ Branch 2 taken 249 times.
✗ Branch 3 not taken.
|
249 | llvm::GlobalVariable *global = module->getNamedGlobal(globalName); |
19 | |||
20 | // Set global attributes | ||
21 | 249 | global->setConstant(true); | |
22 | 249 | global->setDSOLocal(true); | |
23 | 249 | global->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::None); | |
24 |
1/2✓ Branch 1 taken 249 times.
✗ Branch 2 not taken.
|
249 | global->setLinkage(llvm::GlobalValue::ExternalLinkage); |
25 |
2/4✓ Branch 2 taken 249 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 249 times.
✗ Branch 6 not taken.
|
249 | global->setComdat(module->getOrInsertComdat(globalName)); |
26 | |||
27 | 249 | return spiceStruct->vTableData.typeInfoName = global; | |
28 | 249 | } | |
29 | |||
30 | 249 | llvm::Constant *IRGenerator::generateTypeInfo(StructBase *spiceStruct) const { | |
31 | // Generate type info name | ||
32 |
1/2✓ Branch 1 taken 249 times.
✗ Branch 2 not taken.
|
249 | llvm::Constant *typeInfoName = generateTypeInfoName(spiceStruct); |
33 | |||
34 | // Generate LLVM type for type info | ||
35 |
1/2✓ Branch 1 taken 249 times.
✗ Branch 2 not taken.
|
249 | const std::string mangledName = NameMangling::mangleTypeInfo(spiceStruct); |
36 |
1/2✓ Branch 1 taken 249 times.
✗ Branch 2 not taken.
|
249 | llvm::PointerType *ptrTy = builder.getPtrTy(); |
37 | |||
38 | 249 | QualTypeList interfaceTypes; | |
39 |
4/6✓ Branch 1 taken 249 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 249 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 141 times.
✓ Branch 7 taken 108 times.
|
249 | if (spiceStruct->entry->getQualType().is(TY_STRUCT)) { |
40 | 141 | const auto spiceStructEnsured = reinterpret_cast<Struct *>(spiceStruct); | |
41 |
1/2✓ Branch 1 taken 141 times.
✗ Branch 2 not taken.
|
141 | interfaceTypes = spiceStructEnsured->interfaceTypes; |
42 | } | ||
43 | |||
44 | // Build type info LLVM type | ||
45 |
1/2✓ Branch 1 taken 249 times.
✗ Branch 2 not taken.
|
498 | std::vector<llvm::Type *> typeInfoFieldTypes = {ptrTy, ptrTy}; |
46 |
2/2✓ Branch 1 taken 105 times.
✓ Branch 2 taken 249 times.
|
354 | for (size_t i = 0; i < interfaceTypes.size(); i++) |
47 |
1/2✓ Branch 1 taken 105 times.
✗ Branch 2 not taken.
|
105 | typeInfoFieldTypes.push_back(ptrTy); |
48 |
1/2✓ Branch 2 taken 249 times.
✗ Branch 3 not taken.
|
249 | spiceStruct->vTableData.typeInfoType = llvm::StructType::get(context, typeInfoFieldTypes); |
49 | |||
50 | // Generate type info values | ||
51 |
1/2✓ Branch 1 taken 249 times.
✗ Branch 2 not taken.
|
249 | llvm::Constant *typeInfoVTable = llvm::Constant::getNullValue(ptrTy); |
52 |
3/4✓ Branch 1 taken 249 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 213 times.
✓ Branch 4 taken 36 times.
|
498 | if (!sourceFile->isRttiRT()) { |
53 | // Add external global pointer for TypeInfo vtable | ||
54 |
2/4✓ Branch 1 taken 213 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 213 times.
|
213 | assert(sourceFile->isRuntimeModuleAvailable(RuntimeModule::RTTI_RT)); |
55 |
2/4✓ Branch 1 taken 213 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 213 times.
✗ Branch 5 not taken.
|
213 | const std::string typeInfoMangledName = NameMangling::mangleVTable("TypeInfo"); |
56 |
2/4✓ Branch 1 taken 213 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 213 times.
✗ Branch 6 not taken.
|
213 | llvm::Constant *typeInfoExtPtr = module->getOrInsertGlobal(typeInfoMangledName, builder.getPtrTy()); |
57 |
2/4✓ Branch 1 taken 213 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 213 times.
✗ Branch 5 not taken.
|
213 | typeInfoVTable = llvm::ConstantExpr::getInBoundsGetElementPtr(ptrTy, typeInfoExtPtr, builder.getInt64(2)); |
58 | 213 | } | |
59 | 249 | std::vector<llvm::Constant *> fieldValues; | |
60 |
1/2✓ Branch 1 taken 249 times.
✗ Branch 2 not taken.
|
249 | fieldValues.push_back(typeInfoVTable); |
61 |
1/2✓ Branch 1 taken 249 times.
✗ Branch 2 not taken.
|
249 | fieldValues.push_back(typeInfoName); |
62 |
2/2✓ Branch 5 taken 105 times.
✓ Branch 6 taken 249 times.
|
354 | for (const QualType &interfaceType : interfaceTypes) { |
63 |
1/2✓ Branch 1 taken 105 times.
✗ Branch 2 not taken.
|
105 | const Interface *interface = interfaceType.getInterface(nullptr); |
64 |
2/4✓ Branch 0 taken 105 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 105 times.
✗ Branch 3 not taken.
|
105 | assert(interface != nullptr && interface->vTableData.typeInfo != nullptr); |
65 |
1/2✓ Branch 1 taken 105 times.
✗ Branch 2 not taken.
|
105 | const std::string interfaceMangledName = NameMangling::mangleTypeInfo(interface); |
66 |
2/4✓ Branch 1 taken 105 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 105 times.
✗ Branch 6 not taken.
|
105 | llvm::Constant *global = module->getOrInsertGlobal(interfaceMangledName, builder.getPtrTy()); |
67 |
1/2✓ Branch 1 taken 105 times.
✗ Branch 2 not taken.
|
105 | fieldValues.push_back(global); |
68 | 105 | } | |
69 |
1/2✓ Branch 2 taken 249 times.
✗ Branch 3 not taken.
|
249 | llvm::Constant *typeInfo = llvm::ConstantStruct::get(spiceStruct->vTableData.typeInfoType, fieldValues); |
70 | |||
71 | // Generate global variable | ||
72 |
1/2✓ Branch 2 taken 249 times.
✗ Branch 3 not taken.
|
249 | module->getOrInsertGlobal(mangledName, spiceStruct->vTableData.typeInfoType); |
73 |
1/2✓ Branch 2 taken 249 times.
✗ Branch 3 not taken.
|
249 | llvm::GlobalVariable *global = module->getNamedGlobal(mangledName); |
74 |
1/2✓ Branch 1 taken 249 times.
✗ Branch 2 not taken.
|
249 | global->setInitializer(typeInfo); |
75 | |||
76 | // Set global attributes | ||
77 | 249 | global->setConstant(true); | |
78 | 249 | global->setDSOLocal(true); | |
79 | 249 | global->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::None); | |
80 |
1/2✓ Branch 1 taken 249 times.
✗ Branch 2 not taken.
|
249 | global->setLinkage(llvm::GlobalValue::ExternalLinkage); |
81 |
2/4✓ Branch 2 taken 249 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 249 times.
✗ Branch 6 not taken.
|
249 | global->setComdat(module->getOrInsertComdat(mangledName)); |
82 |
2/4✓ Branch 1 taken 249 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 249 times.
✗ Branch 5 not taken.
|
249 | global->setAlignment(llvm::MaybeAlign(8)); |
83 | |||
84 | 249 | return spiceStruct->vTableData.typeInfo = global; | |
85 | 249 | } | |
86 | |||
87 | 249 | llvm::Constant *IRGenerator::generateVTable(StructBase *spiceStruct) const { | |
88 | // Retrieve virtual method count | ||
89 |
1/2✓ Branch 1 taken 249 times.
✗ Branch 2 not taken.
|
249 | const std::vector<Function *> virtualMethods = spiceStruct->scope->getVirtualMethods(); |
90 | 249 | const size_t virtualMethodCount = virtualMethods.size(); | |
91 | 249 | const size_t arrayElementCount = virtualMethodCount + 2; // +2 for nullptr and TypeInfo | |
92 | |||
93 | // Generate type info data structures | ||
94 |
1/2✓ Branch 1 taken 249 times.
✗ Branch 2 not taken.
|
249 | generateTypeInfo(spiceStruct); |
95 | |||
96 | // Generate VTable type | ||
97 |
2/4✓ Branch 1 taken 249 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 249 times.
✗ Branch 5 not taken.
|
249 | llvm::ArrayType *vtableArrayTy = llvm::ArrayType::get(builder.getPtrTy(), arrayElementCount); |
98 |
1/2✓ Branch 2 taken 249 times.
✗ Branch 3 not taken.
|
249 | spiceStruct->vTableData.vtableType = llvm::StructType::get(context, vtableArrayTy, false); |
99 | |||
100 |
1/2✓ Branch 1 taken 249 times.
✗ Branch 2 not taken.
|
249 | const std::string mangledName = NameMangling::mangleVTable(spiceStruct); |
101 |
1/2✓ Branch 2 taken 249 times.
✗ Branch 3 not taken.
|
249 | module->getOrInsertGlobal(mangledName, spiceStruct->vTableData.vtableType); |
102 |
1/2✓ Branch 2 taken 249 times.
✗ Branch 3 not taken.
|
249 | llvm::GlobalVariable *global = module->getNamedGlobal(mangledName); |
103 | |||
104 | // Set global attributes | ||
105 | 249 | global->setConstant(true); | |
106 | 249 | global->setDSOLocal(true); | |
107 | 249 | global->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); | |
108 |
1/2✓ Branch 1 taken 249 times.
✗ Branch 2 not taken.
|
249 | global->setLinkage(llvm::GlobalValue::ExternalLinkage); |
109 |
2/4✓ Branch 2 taken 249 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 249 times.
✗ Branch 6 not taken.
|
249 | global->setComdat(module->getOrInsertComdat(mangledName)); |
110 |
2/4✓ Branch 1 taken 249 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 249 times.
✗ Branch 5 not taken.
|
249 | global->setAlignment(llvm::MaybeAlign(8)); |
111 | |||
112 | 249 | return spiceStruct->vTableData.vtable = global; | |
113 | 249 | } | |
114 | |||
115 | 249 | void IRGenerator::generateVTableInitializer(const StructBase *spiceStruct) const { | |
116 | // Retrieve virtual method count | ||
117 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 249 times.
|
249 | assert(spiceStruct->scope); |
118 |
1/2✓ Branch 1 taken 249 times.
✗ Branch 2 not taken.
|
249 | const std::vector<Function *> virtualMethods = spiceStruct->scope->getVirtualMethods(); |
119 | 249 | const size_t virtualMethodCount = virtualMethods.size(); | |
120 | 249 | const size_t arrayElementCount = virtualMethodCount + 2; // +2 for nullptr and TypeInfo | |
121 | |||
122 | // Generate VTable type | ||
123 |
1/2✓ Branch 1 taken 249 times.
✗ Branch 2 not taken.
|
249 | llvm::PointerType *ptrTy = builder.getPtrTy(); |
124 |
1/2✓ Branch 1 taken 249 times.
✗ Branch 2 not taken.
|
249 | llvm::ArrayType *vtableArrayTy = llvm::ArrayType::get(ptrTy, arrayElementCount); |
125 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 249 times.
|
249 | assert(spiceStruct->vTableData.vtableType); |
126 | |||
127 | // Generate VTable values | ||
128 | 249 | std::vector<llvm::Constant *> arrayValues; | |
129 |
2/4✓ Branch 1 taken 249 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 249 times.
✗ Branch 5 not taken.
|
249 | arrayValues.push_back(llvm::Constant::getNullValue(ptrTy)); // nullptr as safety guard |
130 |
1/2✓ Branch 1 taken 249 times.
✗ Branch 2 not taken.
|
249 | arrayValues.push_back(spiceStruct->vTableData.typeInfo); // TypeInfo to identify the type for the VTable |
131 |
2/2✓ Branch 4 taken 552 times.
✓ Branch 5 taken 249 times.
|
801 | for (Function *virtualMethod : virtualMethods) { |
132 |
3/4✓ Branch 0 taken 272 times.
✓ Branch 1 taken 280 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 272 times.
|
552 | assert(spiceStruct->scope->type == ScopeType::INTERFACE || virtualMethod->llvmFunction != nullptr); |
133 |
4/6✓ Branch 0 taken 272 times.
✓ Branch 1 taken 280 times.
✓ Branch 3 taken 280 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 552 times.
✗ Branch 7 not taken.
|
552 | arrayValues.push_back(virtualMethod->llvmFunction ? virtualMethod->llvmFunction : llvm::Constant::getNullValue(ptrTy)); |
134 | } | ||
135 | |||
136 | // Generate VTable struct | ||
137 | 249 | std::vector<llvm::Constant *> fieldValues; | |
138 |
2/4✓ Branch 2 taken 249 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 249 times.
✗ Branch 6 not taken.
|
249 | fieldValues.push_back(llvm::ConstantArray::get(vtableArrayTy, arrayValues)); |
139 |
1/2✓ Branch 2 taken 249 times.
✗ Branch 3 not taken.
|
249 | llvm::Constant *initializer = llvm::ConstantStruct::get(spiceStruct->vTableData.vtableType, fieldValues); |
140 | |||
141 |
1/2✓ Branch 1 taken 249 times.
✗ Branch 2 not taken.
|
249 | const std::string mangledName = NameMangling::mangleVTable(spiceStruct); |
142 |
1/2✓ Branch 2 taken 249 times.
✗ Branch 3 not taken.
|
249 | llvm::GlobalVariable *global = module->getNamedGlobal(mangledName); |
143 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 249 times.
|
249 | assert(global != nullptr); |
144 |
1/2✓ Branch 1 taken 249 times.
✗ Branch 2 not taken.
|
249 | global->setInitializer(initializer); |
145 | 249 | } | |
146 | |||
147 | } // namespace spice::compiler | ||
148 |