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