GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 100.0% 7 / 0 / 7
Functions: -% 0 / 0 / 0
Branches: 41.6% 94 / 0 / 226

src/irgenerator/IRGenerator.h
Line Branch Exec Source
1 // Copyright (c) 2021-2026 ChilliBits. All rights reserved.
2
3 #pragma once
4
5 #include <stack>
6 #include <unordered_map>
7
8 #include <CompilerPass.h>
9 #include <ast/ASTNodes.h>
10 #include <ast/ParallelizableASTVisitor.h>
11 #include <irgenerator/DebugInfoGenerator.h>
12 #include <irgenerator/MetadataGenerator.h>
13 #include <irgenerator/OpRuleConversionManager.h>
14 #include <irgenerator/StdFunctionManager.h>
15 #include <model/StructBase.h>
16 #include <symboltablebuilder/Scope.h>
17 #include <util/DeferredLogic.h>
18
19 namespace spice::compiler {
20
21 // Forward declarations
22 class ExprNode;
23 class Function;
24
25 const char *const ANON_GLOBAL_STRING_NAME = "anon.string.";
26 const char *const ANON_GLOBAL_ARRAY_NAME = "anon.array.";
27 const char *const CAPTURES_PARAM_NAME = "captures";
28 extern const std::string PRODUCER_STRING;
29
30 enum class Likelihood : uint8_t {
31 UNSPECIFIED,
32 LIKELY,
33 UNLIKELY,
34 };
35
36 // Forward declarations
37 class SourceFile;
38
39 class IRGenerator final : CompilerPass, public ParallelizableASTVisitor {
40 public:
41 // Type definitions
42 using ParamInfoList = std::vector<std::pair<std::string, const SymbolTableEntry *>>;
43
44 // Constructors
45 IRGenerator(GlobalResourceManager &resourceManager, SourceFile *sourceFile);
46
47 // Friend classes
48 friend class StdFunctionManager;
49 friend class OpRuleConversionManager;
50 friend class DebugInfoGenerator;
51 friend class MetadataGenerator;
52 friend class ScopeHandle;
53
54 // Visitor methods
55 // Top level definitions
56 std::any visitEntry(const EntryNode *node) override;
57 std::any visitMainFctDef(const MainFctDefNode *node) override;
58 std::any visitFctDef(const FctDefNode *node) override;
59 std::any visitProcDef(const ProcDefNode *node) override;
60 std::any visitStructDef(const StructDefNode *node) override;
61 std::any visitInterfaceDef(const InterfaceDefNode *node) override;
62 std::any visitEnumDef(const EnumDefNode *node) override;
63 std::any visitGenericTypeDef(const GenericTypeDefNode *node) override;
64 std::any visitAliasDef(const AliasDefNode *node) override;
65 std::any visitGlobalVarDef(const GlobalVarDefNode *node) override;
66 std::any visitExtDecl(const ExtDeclNode *node) override;
67 // Control structures
68 std::any visitUnsafeBlockDef(const UnsafeBlockNode *node) override;
69 std::any visitForLoop(const ForLoopNode *node) override;
70 std::any visitForeachLoop(const ForeachLoopNode *node) override;
71 std::any visitWhileLoop(const WhileLoopNode *node) override;
72 std::any visitDoWhileLoop(const DoWhileLoopNode *node) override;
73 std::any visitIfStmt(const IfStmtNode *node) override;
74 std::any visitElseStmt(const ElseStmtNode *node) override;
75 std::any visitSwitchStmt(const SwitchStmtNode *node) override;
76 std::any visitCaseBranch(const CaseBranchNode *node) override;
77 std::any visitDefaultBranch(const DefaultBranchNode *node) override;
78 std::any visitAssertStmt(const AssertStmtNode *node) override;
79 std::any visitAnonymousBlockStmt(const AnonymousBlockStmtNode *node) override;
80 // Statements
81 std::any visitStmtLst(const StmtLstNode *node) override;
82 std::any visitTypeAltsLst(const TypeAltsLstNode *node) override;
83 std::any visitDeclStmt(const DeclStmtNode *node) override;
84 std::any visitQualifierLst(const QualifierLstNode *node) override;
85 std::any visitModAttr(const ModAttrNode *node) override;
86 std::any visitTopLevelDefinitionAttr(const TopLevelDefAttrNode *node) override;
87 std::any visitCaseConstant(const CaseConstantNode *node) override;
88 std::any visitReturnStmt(const ReturnStmtNode *node) override;
89 std::any visitBreakStmt(const BreakStmtNode *node) override;
90 std::any visitContinueStmt(const ContinueStmtNode *node) override;
91 std::any visitFallthroughStmt(const FallthroughStmtNode *node) override;
92 // Expressions
93 std::any visitAssignExpr(const AssignExprNode *node) override;
94 std::any visitTernaryExpr(const TernaryExprNode *node) override;
95 std::any visitLogicalOrExpr(const LogicalOrExprNode *node) override;
96 std::any visitLogicalAndExpr(const LogicalAndExprNode *node) override;
97 std::any visitBitwiseOrExpr(const BitwiseOrExprNode *node) override;
98 std::any visitBitwiseXorExpr(const BitwiseXorExprNode *node) override;
99 std::any visitBitwiseAndExpr(const BitwiseAndExprNode *node) override;
100 std::any visitEqualityExpr(const EqualityExprNode *node) override;
101 std::any visitRelationalExpr(const RelationalExprNode *node) override;
102 std::any visitShiftExpr(const ShiftExprNode *node) override;
103 std::any visitAdditiveExpr(const AdditiveExprNode *node) override;
104 std::any visitMultiplicativeExpr(const MultiplicativeExprNode *node) override;
105 std::any visitCastExpr(const CastExprNode *node) override;
106 std::any visitPrefixUnaryExpr(const PrefixUnaryExprNode *node) override;
107 std::any visitPostfixUnaryExpr(const PostfixUnaryExprNode *node) override;
108 std::any visitAtomicExpr(const AtomicExprNode *node) override;
109 // Values and types
110 std::any visitValue(const ValueNode *node) override;
111 std::any visitConstant(const ConstantNode *node) override;
112 std::any visitFctCall(const FctCallNode *node) override;
113 std::any visitArrayInitialization(const ArrayInitializationNode *node) override;
114 std::any visitStructInstantiation(const StructInstantiationNode *node) override;
115 std::any visitLambdaFunc(const LambdaFuncNode *node) override;
116 std::any visitLambdaProc(const LambdaProcNode *node) override;
117 std::any visitLambdaExpr(const LambdaExprNode *node) override;
118 std::any visitDataType(const DataTypeNode *node) override;
119
120 // Public methods
121
20/44
✓ Branch 8 → 9 taken 139 times.
✗ Branch 8 → 61 not taken.
✓ Branch 11 → 12 taken 223 times.
✗ Branch 11 → 65 not taken.
✓ Branch 14 → 15 taken 1004 times.
✗ Branch 14 → 90 not taken.
✓ Branch 15 → 16 taken 223 times.
✗ Branch 15 → 88 not taken.
✓ Branch 19 → 20 taken 1004 times.
✗ Branch 19 → 117 not taken.
✓ Branch 21 → 22 taken 1892 times.
✗ Branch 21 → 252 not taken.
✓ Branch 28 → 29 taken 1 time.
✗ Branch 28 → 97 not taken.
✓ Branch 35 → 36 taken 1836 times.
✗ Branch 35 → 79 not taken.
✓ Branch 39 → 40 taken 1836 times.
✗ Branch 39 → 88 not taken.
✓ Branch 43 → 44 taken 1 time.
✗ Branch 43 → 104 not taken.
✓ Branch 68 → 69 taken 2 times.
✗ Branch 68 → 127 not taken.
✓ Branch 79 → 80 taken 1206 times.
✗ Branch 79 → 144 not taken.
✗ Branch 98 → 99 not taken.
✗ Branch 98 → 129 not taken.
✗ Branch 103 → 104 not taken.
✗ Branch 103 → 133 not taken.
✓ Branch 107 → 108 taken 355 times.
✗ Branch 107 → 138 not taken.
✓ Branch 112 → 113 taken 355 times.
✗ Branch 112 → 142 not taken.
✓ Branch 113 → 114 taken 2 times.
✗ Branch 113 → 223 not taken.
✓ Branch 116 → 117 taken 8947 times.
✗ Branch 116 → 464 not taken.
✓ Branch 120 → 121 taken 8947 times.
✗ Branch 120 → 581 not taken.
✓ Branch 137 → 138 taken 2 times.
✗ Branch 137 → 230 not taken.
✓ Branch 403 → 404 taken 768 times.
✗ Branch 403 → 572 not taken.
✓ Branch 408 → 409 taken 768 times.
✗ Branch 408 → 579 not taken.
48995 llvm::AllocaInst *insertAlloca(llvm::Type *llvmType, const std::string &varName = "");
122
4/14
✓ Branch 46 → 47 taken 2 times.
✗ Branch 46 → 159 not taken.
✗ Branch 48 → 49 not taken.
✗ Branch 48 → 82 not taken.
✓ Branch 75 → 76 taken 14 times.
✗ Branch 75 → 172 not taken.
✓ Branch 76 → 77 taken 18944 times.
✗ Branch 76 → 264 not taken.
✓ Branch 108 → 109 taken 6 times.
✗ Branch 108 → 186 not taken.
✗ Branch 134 → 135 not taken.
✗ Branch 134 → 200 not taken.
✗ Branch 139 → 140 not taken.
✗ Branch 139 → 204 not taken.
56898 llvm::AllocaInst *insertAlloca(const QualType &qualType, const std::string &varName = "");
123 llvm::LoadInst *insertLoad(llvm::Type *llvmType, llvm::Value *ptr, bool isVolatile = false,
124
26/59
✓ Branch 12 → 13 taken 14667 times.
✗ Branch 12 → 37 not taken.
✓ Branch 18 → 19 taken 1635 times.
✗ Branch 18 → 36 not taken.
✗ Branch 18 → 102 not taken.
✓ Branch 20 → 21 taken 139 times.
✗ Branch 20 → 74 not taken.
✓ Branch 24 → 25 taken 16 times.
✗ Branch 24 → 54 not taken.
✓ Branch 28 → 29 taken 2258 times.
✗ Branch 28 → 58 not taken.
✗ Branch 28 → 136 not taken.
✓ Branch 30 → 31 taken 2228 times.
✗ Branch 30 → 106 not taken.
✓ Branch 33 → 34 taken 1326 times.
✗ Branch 33 → 147 not taken.
✓ Branch 45 → 46 taken 7 times.
✗ Branch 45 → 86 not taken.
✓ Branch 49 → 50 taken 34 times.
✗ Branch 49 → 115 not taken.
✓ Branch 51 → 52 taken 303 times.
✗ Branch 51 → 258 not taken.
✓ Branch 53 → 54 taken 81 times.
✗ Branch 53 → 116 not taken.
✓ Branch 55 → 56 taken 292 times.
✗ Branch 55 → 92 not taken.
✓ Branch 73 → 74 taken 2238 times.
✗ Branch 73 → 142 not taken.
✓ Branch 76 → 77 taken 168 times.
✗ Branch 76 → 176 not taken.
✓ Branch 77 → 78 taken 2238 times.
✗ Branch 77 → 155 not taken.
✓ Branch 82 → 83 taken 312 times.
✗ Branch 82 → 159 not taken.
✗ Branch 82 → 160 not taken.
✓ Branch 97 → 98 taken 40 times.
✗ Branch 97 → 189 not taken.
✓ Branch 101 → 102 taken 3 times.
✗ Branch 101 → 172 not taken.
✓ Branch 104 → 105 taken 2703 times.
✗ Branch 104 → 174 not taken.
✓ Branch 125 → 126 taken 378 times.
✗ Branch 125 → 190 not taken.
✓ Branch 131 → 132 taken 378 times.
✗ Branch 131 → 195 not taken.
✗ Branch 138 → 139 not taken.
✗ Branch 138 → 223 not taken.
✓ Branch 139 → 140 taken 8 times.
✗ Branch 139 → 186 not taken.
✗ Branch 143 → 144 not taken.
✗ Branch 143 → 229 not taken.
✓ Branch 193 → 194 taken 947 times.
✗ Branch 193 → 297 not taken.
✓ Branch 196 → 197 taken 47 times.
✗ Branch 196 → 504 not taken.
✓ Branch 198 → 199 taken 947 times.
✗ Branch 198 → 301 not taken.
✓ Branch 200 → 201 taken 47 times.
✗ Branch 200 → 508 not taken.
85512 const std::string &varName = "") const;
125 llvm::LoadInst *insertLoad(const QualType &qualType, llvm::Value *ptr, bool isVolatile = false,
126
3/14
✓ Branch 5 → 6 taken 63810 times.
✗ Branch 5 → 21 not taken.
✗ Branch 8 → 9 not taken.
✗ Branch 8 → 59 not taken.
✓ Branch 9 → 10 taken 63810 times.
✗ Branch 9 → 25 not taken.
✗ Branch 12 → 13 not taken.
✗ Branch 12 → 63 not taken.
✓ Branch 27 → 28 taken 127738 times.
✗ Branch 27 → 42 not taken.
✗ Branch 37 → 38 not taken.
✗ Branch 37 → 75 not taken.
✗ Branch 41 → 42 not taken.
✗ Branch 41 → 79 not taken.
574644 const std::string &varName = "");
127 llvm::StoreInst *insertStore(llvm::Value *val, llvm::Value *ptr, bool isVolatile = false) const;
128 void insertStore(llvm::Value *val, llvm::Value *ptr, const QualType &qualType, bool isVolatile = false);
129 llvm::Value *insertInBoundsGEP(llvm::Type *type, llvm::Value *basePtr, llvm::ArrayRef<llvm::Value *> indices,
130
9/21
✓ Branch 28 → 29 taken 221 times.
✗ Branch 28 → 68 not taken.
✗ Branch 33 → 34 not taken.
✗ Branch 33 → 101 not taken.
✓ Branch 38 → 39 taken 1326 times.
✗ Branch 38 → 143 not taken.
✓ Branch 43 → 44 taken 1515 times.
✗ Branch 43 → 147 not taken.
✗ Branch 43 → 192 not taken.
✓ Branch 54 → 55 taken 5998 times.
✗ Branch 54 → 201 not taken.
✓ Branch 74 → 75 taken 2 times.
✗ Branch 74 → 135 not taken.
✓ Branch 86 → 87 taken 3 times.
✗ Branch 86 → 143 not taken.
✓ Branch 91 → 92 taken 78 times.
✗ Branch 91 → 448 not taken.
✓ Branch 102 → 103 taken 2 times.
✗ Branch 102 → 271 not taken.
✓ Branch 107 → 108 taken 2 times.
✗ Branch 107 → 275 not taken.
23457 const std::string &varName = "") const;
131
24/50
✓ Branch 36 → 37 taken 932 times.
✗ Branch 36 → 64 not taken.
✓ Branch 37 → 38 taken 700 times.
✗ Branch 37 → 125 not taken.
✗ Branch 37 → 148 not taken.
✓ Branch 47 → 48 taken 81 times.
✗ Branch 47 → 110 not taken.
✓ Branch 51 → 52 taken 7 times.
✗ Branch 51 → 89 not taken.
✓ Branch 56 → 57 taken 37 times.
✗ Branch 56 → 121 not taken.
✓ Branch 60 → 61 taken 37 times.
✗ Branch 60 → 125 not taken.
✓ Branch 62 → 63 taken 7759 times.
✗ Branch 62 → 435 not taken.
✓ Branch 63 → 64 taken 297 times.
✗ Branch 63 → 98 not taken.
✓ Branch 67 → 68 taken 2238 times.
✗ Branch 67 → 136 not taken.
✓ Branch 73 → 74 taken 75 times.
✗ Branch 73 → 134 not taken.
✓ Branch 74 → 75 taken 823 times.
✗ Branch 74 → 95 not taken.
✓ Branch 78 → 79 taken 823 times.
✗ Branch 78 → 99 not taken.
✓ Branch 80 → 81 taken 75 times.
✗ Branch 80 → 140 not taken.
✓ Branch 84 → 85 taken 425 times.
✓ Branch 84 → 88 taken 33 times.
✗ Branch 84 → 182 not taken.
✓ Branch 90 → 91 taken 1416 times.
✗ Branch 90 → 150 not taken.
✗ Branch 90 → 165 not taken.
✗ Branch 90 → 166 not taken.
✓ Branch 92 → 93 taken 75 times.
✗ Branch 92 → 146 not taken.
✗ Branch 94 → 95 not taken.
✗ Branch 94 → 161 not taken.
✓ Branch 108 → 109 taken 1338 times.
✗ Branch 108 → 156 not taken.
✓ Branch 112 → 113 taken 9482 times.
✓ Branch 112 → 115 taken 967 times.
✗ Branch 112 → 180 not taken.
✓ Branch 116 → 117 taken 5227 times.
✓ Branch 116 → 119 taken 3884 times.
✓ Branch 126 → 127 taken 100 times.
✗ Branch 126 → 470 not taken.
✓ Branch 132 → 133 taken 108 times.
✗ Branch 132 → 180 not taken.
✗ Branch 132 → 476 not taken.
76665 llvm::Value *insertStructGEP(llvm::Type *type, llvm::Value *basePtr, unsigned index, const std::string &varName = "") const;
132 llvm::Value *resolveValue(const ExprNode *node);
133 llvm::Value *resolveValue(const ExprNode *node, LLVMExprResult &exprResult);
134 llvm::Value *resolveValue(const QualType &qualType, LLVMExprResult &exprResult);
135 llvm::Value *resolveAddress(const ASTNode *node);
136 llvm::Value *resolveAddress(LLVMExprResult &exprResult);
137 [[nodiscard]] llvm::Constant *getDefaultValueForSymbolType(const QualType &symbolType);
138 [[nodiscard]] static std::string getIRString(llvm::Module *llvmModule, const CliOptions &cliOptions);
139 // Address management for symbol table entries
140 [[nodiscard]] llvm::Value *getAddress(const SymbolTableEntry *entry);
141 void updateAddress(const SymbolTableEntry *entry, llvm::Value *address);
142 void pushAddress(const SymbolTableEntry *entry, llvm::Value *address);
143 void popAddress(const SymbolTableEntry *entry);
144 // LLVM function management for Spice functions
145 [[nodiscard]] llvm::Function *getLLVMFunction(const Function *spiceFunc);
146 void setLLVMFunction(const Function *spiceFunc, llvm::Function *llvmFunction);
147
148 // Builtin function handlers
149 std::any visitBuiltinCall(const FctCallNode *node);
150 std::any visitBuiltinPrintfCall(const FctCallNode *node);
151 std::any visitBuiltinLenCall(const FctCallNode *node);
152 std::any visitBuiltinPanicCall(const FctCallNode *node);
153 std::any visitBuiltinSyscallCall(const FctCallNode *node);
154 std::any visitBuiltinNewCall(const FctCallNode *node);
155 std::any visitBuiltinPlacementNewCall(const FctCallNode *node);
156
157 private:
158 // Private methods
159 llvm::Constant *getConst(const CompileTimeValue &compileTimeValue, const QualType &type, const ASTNode *node) const;
160
8/24
✓ Branch 62 → 63 taken 460 times.
✗ Branch 62 → 149 not taken.
✗ Branch 62 → 155 not taken.
✗ Branch 62 → 156 not taken.
✗ Branch 62 → 186 not taken.
✓ Branch 66 → 67 taken 460 times.
✗ Branch 66 → 167 not taken.
✗ Branch 66 → 175 not taken.
✗ Branch 66 → 195 not taken.
✗ Branch 66 → 229 not taken.
✓ Branch 85 → 86 taken 1080 times.
✗ Branch 85 → 139 not taken.
✗ Branch 85 → 141 not taken.
✓ Branch 89 → 90 taken 1080 times.
✗ Branch 89 → 155 not taken.
✗ Branch 89 → 157 not taken.
✓ Branch 100 → 101 taken 11295 times.
✗ Branch 100 → 197 not taken.
✓ Branch 104 → 105 taken 11295 times.
✗ Branch 104 → 214 not taken.
✓ Branch 122 → 123 taken 21964 times.
✗ Branch 122 → 260 not taken.
✓ Branch 126 → 127 taken 21964 times.
✗ Branch 126 → 301 not taken.
104397 llvm::BasicBlock *createBlock(const std::string &blockName = "") const;
161 void switchToBlock(llvm::BasicBlock *block, llvm::Function *parentFct = nullptr);
162 void terminateBlock(const StmtLstNode *stmtLstNode);
163 void insertJump(llvm::BasicBlock *targetBlock);
164 void insertCondJump(llvm::Value *condition, llvm::BasicBlock *trueBlock, llvm::BasicBlock *falseBlock,
165 Likelihood likelihood = Likelihood::UNSPECIFIED);
166 void verifyFunction(const llvm::Function *fct, const CodeLoc &codeLoc) const;
167 void verifyModule(const CodeLoc &codeLoc) const;
168 LLVMExprResult doAssignment(const ASTNode *lhsNode, const ExprNode *rhsNode, const ASTNode *node);
169 LLVMExprResult doAssignment(llvm::Value *lhsAddress, const SymbolTableEntry *lhsEntry, const ExprNode *rhsNode,
170 const ASTNode *node, bool isDecl = false);
171 LLVMExprResult doAssignment(llvm::Value *lhsAddress, const SymbolTableEntry *lhsEntry, LLVMExprResult &rhs,
172 const QualType &rhsSType, const ASTNode *node, bool isDecl);
173 void generateShallowCopy(llvm::Value *oldAddress, llvm::Type *varType, llvm::Value *targetAddress, bool isVolatile) const;
174 void autoDeReferencePtr(llvm::Value *&ptr, QualType &symbolType);
175 llvm::GlobalVariable *createGlobalConst(const std::string &baseName, llvm::Constant *constant) const;
176 llvm::GlobalVariable *createGlobalStringConst(const std::string &baseName, const std::string &value) const;
177 llvm::GlobalVariable *createGlobalStringConst(const std::string &baseName, const std::string &value,
178 const CodeLoc &codeLoc) const;
179 [[nodiscard]] std::string getUnusedGlobalName(const std::string &baseName) const;
180 static void materializeConstant(LLVMExprResult &exprResult);
181 const std::vector<const Function *> &getOpFctPointers(const ASTNode *node) const;
182 llvm::Value *buildFatFctPtr(Scope *bodyScope, llvm::Type *capturesStructType, llvm::Value *lambda);
183 llvm::Function *getOrCreateFatFctPtrThunk(llvm::Function *target);
184 llvm::Type *buildCapturesContainerType(const CaptureMap &captures) const;
185 void unpackCapturesToLocalVariables(const CaptureMap &captures, llvm::Value *val, llvm::Type *structType);
186 bool bindDecayedArrayParam(llvm::Argument &arg, const std::string &paramName, const SymbolTableEntry *paramSymbol);
187 llvm::Value *materializeDecayedArrayArg(llvm::Value *argValue, const QualType &paramType);
188 void setParamAttrs(llvm::Function *function, const ParamInfoList &paramInfo) const;
189 void setFunctionReturnValAttrs(llvm::Function *function, const QualType &returnType) const;
190 void setCallArgAttrs(llvm::CallInst *callInst, const Function *spiceFunc, const QualTypeList &paramSTypes) const;
191 void setCallReturnValAttrs(llvm::CallInst *callInst, const QualType &returnType) const;
192 llvm::Attribute::AttrKind getExtAttrKindForType(const QualType &type) const;
193 bool isSymbolDSOLocal(bool isPublic) const;
194 llvm::GlobalValue::LinkageTypes getSymbolLinkageType(bool isPublic) const;
195 llvm::GlobalValue::LinkageTypes getVTableLinkageType(bool isPublic) const;
196 void attachComdatToSymbol(llvm::GlobalVariable *global, const std::string &comdatName, bool isPublic) const;
197 void addCommonFctAttrs(llvm::Function *fct, bool isAlwaysInline = false) const;
198
199 // Generate implicit
200 llvm::Value *doImplicitCast(llvm::Value *src, QualType dstSTy, QualType srcSTy);
201 llvm::Value *getUpcastedStructPtr(llvm::Value *structPtr, const QualType &dstType, const QualType &srcType) const;
202 void generateScopeCleanup(const StmtLstNode *node);
203 void generateFctDecl(const Function *fct, const std::vector<llvm::Value *> &args) const;
204 llvm::CallInst *generateFctCall(const Function *fct, const std::vector<llvm::Value *> &args) const;
205 llvm::Value *generateFctDeclAndCall(const Function *fct, const std::vector<llvm::Value *> &args) const;
206 void generateProcDeclAndCall(const Function *proc, const std::vector<llvm::Value *> &args) const;
207 void generateCtorOrDtorCall(const SymbolTableEntry *entry, const Function *ctorOrDtor,
208 const std::vector<llvm::Value *> &args);
209 void generateCtorOrDtorCall(llvm::Value *structAddr, const Function *ctorOrDtor, const std::vector<llvm::Value *> &args) const;
210 void generateDeallocCall(llvm::Value *variableAddress) const;
211 llvm::Function *generateImplicitFunction(const std::function<void(void)> &generateBody, const Function *spiceFunc);
212 llvm::Function *generateImplicitProcedure(const std::function<void(void)> &generateBody, const Function *spiceProc);
213 void generateCtorBodyPreamble(Scope *bodyScope);
214 void generateDefaultCtor(const Function *ctorFunction);
215 void generateCopyCtorBodyPreamble(const Function *copyCtorFunction);
216 void generateDefaultCopyCtor(const Function *copyCtorFunction);
217 void generateMoveCtorBodyPreamble(const Function *moveCtorFunction);
218 void generateDefaultMoveCtor(const Function *moveCtorFunction);
219 void generateDtorBodyPreamble(const Function *dtorFunction);
220 void generateDefaultDtor(const Function *dtorFunction);
221 void generateTestMain();
222
223 // Generate target dependent
224 std::string getSysCallAsmString(uint8_t numRegs) const;
225 std::string getSysCallConstraintString(uint8_t numRegs) const;
226
227 // Generate VTable
228 llvm::Constant *generateTypeInfoName(StructBase *spiceStruct) const;
229 llvm::Constant *generateTypeInfo(StructBase *spiceStruct) const;
230 llvm::Constant *generateVTable(StructBase *spiceStruct) const;
231 void generateVTableInitializer(const StructBase *spiceStruct);
232
233 // Generate code instrumentation
234 void enableFunctionInstrumentation(llvm::Function *function) const;
235
236 // Private members
237 llvm::LLVMContext &context;
238 llvm::IRBuilder<> &builder;
239 llvm::Module *module;
240 OpRuleConversionManager conversionManager;
241 const StdFunctionManager stdFunctionManager;
242 DebugInfoGenerator diGenerator = DebugInfoGenerator(this);
243 MetadataGenerator mdGenerator = MetadataGenerator(this);
244 struct CommonLLVMTypes {
245 llvm::StructType *lambdaFatPtrType = nullptr;
246 } llvmTypes;
247 std::vector<llvm::BasicBlock *> breakBlocks;
248 std::vector<llvm::BasicBlock *> continueBlocks;
249 std::stack<llvm::BasicBlock *> fallthroughBlocks;
250 llvm::BasicBlock *allocaInsertBlock = nullptr;
251 llvm::AllocaInst *allocaInsertInst = nullptr;
252 bool blockAlreadyTerminated = false;
253 bool isInCtorBody = false;
254 std::vector<DeferredLogic> deferredVTableInitializations;
255 // IR-side state: separate from semantic objects to keep the type-checker model clean
256 std::unordered_map<const SymbolTableEntry *, std::stack<llvm::Value *>> addressMap;
257 std::unordered_map<const Function *, llvm::Function *> llvmFunctions;
258 };
259
260 } // namespace spice::compiler
261