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 1884 times.
✗ Branch 21 → 252 not taken.
✓ Branch 28 → 29 taken 1 time.
✗ Branch 28 → 97 not taken.
✓ Branch 35 → 36 taken 1811 times.
✗ Branch 35 → 79 not taken.
✓ Branch 39 → 40 taken 1811 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 89 → 90 not taken.
✗ Branch 89 → 120 not taken.
✗ Branch 94 → 95 not taken.
✗ Branch 94 → 124 not taken.
✓ Branch 98 → 99 taken 355 times.
✗ Branch 98 → 129 not taken.
✓ Branch 103 → 104 taken 355 times.
✗ Branch 103 → 133 not taken.
✓ Branch 113 → 114 taken 2 times.
✗ Branch 113 → 223 not taken.
✓ Branch 116 → 117 taken 8967 times.
✗ Branch 116 → 454 not taken.
✓ Branch 120 → 121 taken 8967 times.
✗ Branch 120 → 571 not taken.
✓ Branch 137 → 138 taken 2 times.
✗ Branch 137 → 230 not taken.
✓ Branch 393 → 394 taken 768 times.
✗ Branch 393 → 562 not taken.
✓ Branch 398 → 399 taken 768 times.
✗ Branch 398 → 569 not taken.
|
48956 | 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 18830 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.
|
56556 | llvm::AllocaInst *insertAlloca(const QualType &qualType, const std::string &varName = ""); |
| 123 | llvm::LoadInst *insertLoad(llvm::Type *llvmType, llvm::Value *ptr, bool isVolatile = false, | ||
| 124 |
25/58✓ Branch 12 → 13 taken 14654 times.
✗ Branch 12 → 37 not taken.
✓ Branch 18 → 19 taken 1624 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 2252 times.
✗ Branch 28 → 58 not taken.
✗ Branch 28 → 136 not taken.
✓ Branch 30 → 31 taken 2227 times.
✗ Branch 30 → 106 not taken.
✓ Branch 33 → 34 taken 1325 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 297 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 2237 times.
✗ Branch 73 → 142 not taken.
✓ Branch 76 → 77 taken 168 times.
✗ Branch 76 → 176 not taken.
✓ Branch 77 → 78 taken 2237 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 2701 times.
✗ Branch 104 → 174 not taken.
✓ Branch 130 → 131 taken 341 times.
✗ Branch 130 → 197 not taken.
✗ Branch 133 → 134 not taken.
✗ Branch 133 → 218 not taken.
✓ Branch 135 → 136 taken 8 times.
✗ Branch 135 → 181 not taken.
✓ Branch 136 → 137 taken 341 times.
✗ Branch 136 → 202 not taken.
✗ Branch 138 → 139 not taken.
✗ Branch 138 → 224 not taken.
✓ Branch 190 → 191 taken 948 times.
✗ Branch 190 → 294 not taken.
✓ Branch 191 → 192 taken 47 times.
✗ Branch 191 → 494 not taken.
✓ Branch 195 → 196 taken 995 times.
✗ Branch 195 → 298 not taken.
✗ Branch 195 → 498 not taken.
|
84943 | 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 63749 times.
✗ Branch 5 → 21 not taken.
✗ Branch 8 → 9 not taken.
✗ Branch 8 → 59 not taken.
✓ Branch 9 → 10 taken 63749 times.
✗ Branch 9 → 25 not taken.
✗ Branch 12 → 13 not taken.
✗ Branch 12 → 63 not taken.
✓ Branch 27 → 28 taken 127354 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.
|
573309 | 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 1325 times.
✗ Branch 38 → 143 not taken.
✓ Branch 43 → 44 taken 1500 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 → 438 not taken.
✓ Branch 102 → 103 taken 2 times.
✗ Branch 102 → 271 not taken.
✓ Branch 107 → 108 taken 2 times.
✗ Branch 107 → 275 not taken.
|
23412 | const std::string &varName = "") const; |
| 131 |
25/51✓ Branch 36 → 37 taken 927 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 7751 times.
✗ Branch 62 → 425 not taken.
✓ Branch 63 → 64 taken 297 times.
✗ Branch 63 → 98 not taken.
✓ Branch 67 → 68 taken 2237 times.
✗ Branch 67 → 136 not taken.
✓ Branch 73 → 74 taken 74 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 74 times.
✗ Branch 80 → 140 not taken.
✓ Branch 84 → 85 taken 424 times.
✓ Branch 84 → 88 taken 33 times.
✗ Branch 84 → 182 not taken.
✓ Branch 90 → 91 taken 1420 times.
✗ Branch 90 → 150 not taken.
✗ Branch 90 → 165 not taken.
✗ Branch 90 → 166 not taken.
✓ Branch 92 → 93 taken 74 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 9473 times.
✓ Branch 112 → 115 taken 967 times.
✗ Branch 112 → 180 not taken.
✓ Branch 116 → 117 taken 5227 times.
✓ Branch 116 → 119 taken 3875 times.
✓ Branch 126 → 127 taken 99 times.
✗ Branch 126 → 460 not taken.
✓ Branch 128 → 129 taken 8 times.
✗ Branch 128 → 175 not taken.
✓ Branch 132 → 133 taken 99 times.
✗ Branch 132 → 466 not taken.
|
76593 | 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 |
10/26✓ Branch 61 → 62 taken 58 times.
✗ Branch 61 → 144 not taken.
✗ Branch 61 → 151 not taken.
✗ Branch 61 → 181 not taken.
✓ Branch 65 → 66 taken 58 times.
✗ Branch 65 → 162 not taken.
✗ Branch 65 → 170 not taken.
✗ Branch 65 → 224 not taken.
✓ Branch 71 → 72 taken 364 times.
✗ Branch 71 → 162 not taken.
✓ Branch 75 → 76 taken 364 times.
✗ Branch 75 → 202 not taken.
✓ Branch 92 → 93 taken 1082 times.
✗ Branch 92 → 146 not taken.
✗ Branch 92 → 148 not taken.
✓ Branch 96 → 97 taken 1082 times.
✗ Branch 96 → 162 not taken.
✗ Branch 96 → 164 not taken.
✓ Branch 101 → 102 taken 11257 times.
✗ Branch 101 → 194 not taken.
✓ Branch 105 → 106 taken 11257 times.
✗ Branch 105 → 211 not taken.
✓ Branch 123 → 124 taken 21950 times.
✗ Branch 123 → 257 not taken.
✓ Branch 127 → 128 taken 21950 times.
✗ Branch 127 → 298 not taken.
|
104133 | 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 | void setParamAttrs(llvm::Function *function, const ParamInfoList ¶mInfo) const; | ||
| 187 | void setFunctionReturnValAttrs(llvm::Function *function, const QualType &returnType) const; | ||
| 188 | void setCallArgAttrs(llvm::CallInst *callInst, const Function *spiceFunc, const QualTypeList ¶mSTypes) const; | ||
| 189 | void setCallReturnValAttrs(llvm::CallInst *callInst, const QualType &returnType) const; | ||
| 190 | llvm::Attribute::AttrKind getExtAttrKindForType(const QualType &type) const; | ||
| 191 | bool isSymbolDSOLocal(bool isPublic) const; | ||
| 192 | llvm::GlobalValue::LinkageTypes getSymbolLinkageType(bool isPublic) const; | ||
| 193 | llvm::GlobalValue::LinkageTypes getVTableLinkageType(bool isPublic) const; | ||
| 194 | void attachComdatToSymbol(llvm::GlobalVariable *global, const std::string &comdatName, bool isPublic) const; | ||
| 195 | |||
| 196 | // Generate implicit | ||
| 197 | llvm::Value *doImplicitCast(llvm::Value *src, QualType dstSTy, QualType srcSTy); | ||
| 198 | llvm::Value *getUpcastedStructPtr(llvm::Value *structPtr, const QualType &dstType, const QualType &srcType) const; | ||
| 199 | void generateScopeCleanup(const StmtLstNode *node); | ||
| 200 | void generateFctDecl(const Function *fct, const std::vector<llvm::Value *> &args) const; | ||
| 201 | llvm::CallInst *generateFctCall(const Function *fct, const std::vector<llvm::Value *> &args) const; | ||
| 202 | llvm::Value *generateFctDeclAndCall(const Function *fct, const std::vector<llvm::Value *> &args) const; | ||
| 203 | void generateProcDeclAndCall(const Function *proc, const std::vector<llvm::Value *> &args) const; | ||
| 204 | void generateCtorOrDtorCall(const SymbolTableEntry *entry, const Function *ctorOrDtor, | ||
| 205 | const std::vector<llvm::Value *> &args); | ||
| 206 | void generateCtorOrDtorCall(llvm::Value *structAddr, const Function *ctorOrDtor, const std::vector<llvm::Value *> &args) const; | ||
| 207 | void generateDeallocCall(llvm::Value *variableAddress) const; | ||
| 208 | llvm::Function *generateImplicitFunction(const std::function<void(void)> &generateBody, const Function *spiceFunc); | ||
| 209 | llvm::Function *generateImplicitProcedure(const std::function<void(void)> &generateBody, const Function *spiceProc); | ||
| 210 | void generateCtorBodyPreamble(Scope *bodyScope); | ||
| 211 | void generateDefaultCtor(const Function *ctorFunction); | ||
| 212 | void generateCopyCtorBodyPreamble(const Function *copyCtorFunction); | ||
| 213 | void generateDefaultCopyCtor(const Function *copyCtorFunction); | ||
| 214 | void generateMoveCtorBodyPreamble(const Function *moveCtorFunction); | ||
| 215 | void generateDefaultMoveCtor(const Function *moveCtorFunction); | ||
| 216 | void generateDtorBodyPreamble(const Function *dtorFunction); | ||
| 217 | void generateDefaultDtor(const Function *dtorFunction); | ||
| 218 | void generateTestMain(); | ||
| 219 | |||
| 220 | // Generate target dependent | ||
| 221 | std::string getSysCallAsmString(uint8_t numRegs) const; | ||
| 222 | std::string getSysCallConstraintString(uint8_t numRegs) const; | ||
| 223 | |||
| 224 | // Generate VTable | ||
| 225 | llvm::Constant *generateTypeInfoName(StructBase *spiceStruct) const; | ||
| 226 | llvm::Constant *generateTypeInfo(StructBase *spiceStruct) const; | ||
| 227 | llvm::Constant *generateVTable(StructBase *spiceStruct) const; | ||
| 228 | void generateVTableInitializer(const StructBase *spiceStruct); | ||
| 229 | |||
| 230 | // Generate code instrumentation | ||
| 231 | void enableFunctionInstrumentation(llvm::Function *function) const; | ||
| 232 | |||
| 233 | // Private members | ||
| 234 | llvm::LLVMContext &context; | ||
| 235 | llvm::IRBuilder<> &builder; | ||
| 236 | llvm::Module *module; | ||
| 237 | OpRuleConversionManager conversionManager; | ||
| 238 | const StdFunctionManager stdFunctionManager; | ||
| 239 | DebugInfoGenerator diGenerator = DebugInfoGenerator(this); | ||
| 240 | MetadataGenerator mdGenerator = MetadataGenerator(this); | ||
| 241 | struct CommonLLVMTypes { | ||
| 242 | llvm::StructType *lambdaFatPtrType = nullptr; | ||
| 243 | } llvmTypes; | ||
| 244 | std::vector<llvm::BasicBlock *> breakBlocks; | ||
| 245 | std::vector<llvm::BasicBlock *> continueBlocks; | ||
| 246 | std::stack<llvm::BasicBlock *> fallthroughBlocks; | ||
| 247 | llvm::BasicBlock *allocaInsertBlock = nullptr; | ||
| 248 | llvm::AllocaInst *allocaInsertInst = nullptr; | ||
| 249 | bool blockAlreadyTerminated = false; | ||
| 250 | bool isInCtorBody = false; | ||
| 251 | std::vector<DeferredLogic> deferredVTableInitializations; | ||
| 252 | // IR-side state: separate from semantic objects to keep the type-checker model clean | ||
| 253 | std::unordered_map<const SymbolTableEntry *, std::stack<llvm::Value *>> addressMap; | ||
| 254 | std::unordered_map<const Function *, llvm::Function *> llvmFunctions; | ||
| 255 | }; | ||
| 256 | |||
| 257 | } // namespace spice::compiler | ||
| 258 |