GCC Code Coverage Report


Directory: ../
File: src/irgenerator/OpRuleConversionManager.h
Date: 2024-12-24 01:17:15
Exec Total Coverage
Lines: 2 2 100.0%
Functions: 1 1 100.0%
Branches: 0 0 -%

Line Branch Exec Source
1 // Copyright (c) 2021-2024 ChilliBits. All rights reserved.
2
3 #pragma once
4
5 #include <irgenerator/LLVMExprResult.h>
6 #include <symboltablebuilder/Type.h>
7
8 #include <llvm/IR/IRBuilder.h>
9 #include <llvm/IR/Value.h>
10
11 namespace spice::compiler {
12
13 // Forward declarations
14 class SourceFile;
15 class IRGenerator;
16 class StdFunctionManager;
17 class SymbolTableEntry;
18 struct CodeLoc;
19
20 // Typedefs
21 using ResolverFct = const std::function<llvm::Value *()>;
22
23 #define COMB(en1, en2) ((en1) | ((en2) << 16))
24
25 /**
26 * Helper class for the IRGenerator to resolve operator instructions for each valid operator/type combination
27 */
28 class OpRuleConversionManager {
29 public:
30 // Constructors
31 OpRuleConversionManager(SourceFile *sourceFile, IRGenerator *irGenerator);
32
33 // Public methods
34 LLVMExprResult getPlusEqualInst(const ASTNode *node, LLVMExprResult &lhs, QualType lhsSTy, LLVMExprResult &rhs, QualType rhsSTy,
35 size_t opIdx);
36 LLVMExprResult getMinusEqualInst(const ASTNode *node, LLVMExprResult &lhs, QualType lhsSTy, LLVMExprResult &rhs,
37 QualType rhsSTy, size_t opIdx);
38 LLVMExprResult getMulEqualInst(const ASTNode *node, LLVMExprResult &lhs, QualType lhsSTy, LLVMExprResult &rhs, QualType rhsSTy,
39 size_t opIdx);
40 LLVMExprResult getDivEqualInst(const ASTNode *node, LLVMExprResult &lhs, QualType lhsSTy, LLVMExprResult &rhs, QualType rhsSTy,
41 size_t opIdx);
42 LLVMExprResult getRemEqualInst(const ASTNode *node, LLVMExprResult &lhs, QualType lhsSTy, LLVMExprResult &rhs, QualType rhsSTy);
43 LLVMExprResult getSHLEqualInst(const ASTNode *node, LLVMExprResult &lhs, QualType lhsSTy, LLVMExprResult &rhs, QualType rhsSTy);
44 LLVMExprResult getSHREqualInst(const ASTNode *node, LLVMExprResult &lhs, QualType lhsSTy, LLVMExprResult &rhs, QualType rhsSTy);
45 LLVMExprResult getAndEqualInst(const ASTNode *node, LLVMExprResult &lhs, QualType lhsSTy, LLVMExprResult &rhs, QualType rhsSTy);
46 LLVMExprResult getOrEqualInst(const ASTNode *node, LLVMExprResult &lhs, QualType lhsSTy, LLVMExprResult &rhs, QualType rhsSTy);
47 LLVMExprResult getXorEqualInst(const ASTNode *node, LLVMExprResult &lhs, QualType lhsSTy, LLVMExprResult &rhs, QualType rhsSTy);
48 LLVMExprResult getBitwiseOrInst(const ASTNode *node, LLVMExprResult &lhs, QualType lhsSTy, LLVMExprResult &rhs, QualType rhsSTy,
49 size_t opIdx);
50 LLVMExprResult getBitwiseXorInst(const ASTNode *node, LLVMExprResult &lhs, QualType lhsSTy, LLVMExprResult &rhs,
51 QualType rhsSTy);
52 LLVMExprResult getBitwiseAndInst(const ASTNode *node, LLVMExprResult &lhs, QualType lhsSTy, LLVMExprResult &rhs,
53 QualType rhsSTy, size_t opIdx);
54 LLVMExprResult getEqualInst(const ASTNode *node, LLVMExprResult &lhs, QualType lhsSTy, LLVMExprResult &rhs, QualType rhsSTy,
55 size_t opIdx);
56 LLVMExprResult getNotEqualInst(const ASTNode *node, LLVMExprResult &lhs, QualType lhsSTy, LLVMExprResult &rhs, QualType rhsSTy,
57 size_t opIdx);
58 LLVMExprResult getLessInst(const ASTNode *node, LLVMExprResult &lhs, QualType lhsSTy, LLVMExprResult &rhs, QualType rhsSTy);
59 LLVMExprResult getGreaterInst(const ASTNode *node, LLVMExprResult &lhs, QualType lhsSTy, LLVMExprResult &rhs, QualType rhsSTy);
60 LLVMExprResult getLessEqualInst(const ASTNode *node, LLVMExprResult &lhs, QualType lhsSTy, LLVMExprResult &rhs,
61 QualType rhsSTy);
62 LLVMExprResult getGreaterEqualInst(const ASTNode *node, LLVMExprResult &lhs, QualType lhsSTy, LLVMExprResult &rhs,
63 QualType rhsSTy);
64 LLVMExprResult getShiftLeftInst(const ASTNode *node, LLVMExprResult &lhs, QualType lhsSTy, LLVMExprResult &rhs, QualType rhsSTy,
65 size_t opIdx);
66 LLVMExprResult getShiftRightInst(const ASTNode *node, LLVMExprResult &lhs, QualType lhsSTy, LLVMExprResult &rhs,
67 QualType rhsSTy, size_t opIdx);
68 LLVMExprResult getPlusInst(const ASTNode *node, LLVMExprResult &lhs, QualType lhsSTy, LLVMExprResult &rhs, QualType rhsSTy,
69 size_t opIdx);
70 LLVMExprResult getMinusInst(const ASTNode *node, LLVMExprResult &lhs, QualType lhsSTy, LLVMExprResult &rhs, QualType rhsSTy,
71 size_t opIdx);
72 LLVMExprResult getMulInst(const ASTNode *node, LLVMExprResult &lhs, QualType lhsSTy, LLVMExprResult &rhs, QualType rhsSTy,
73 size_t opIdx);
74 LLVMExprResult getDivInst(const ASTNode *node, LLVMExprResult &lhs, QualType lhsSTy, LLVMExprResult &rhs, QualType rhsSTy,
75 size_t opIdx);
76 LLVMExprResult getRemInst(const ASTNode *node, LLVMExprResult &lhs, QualType lhsSTy, LLVMExprResult &rhs, QualType rhsSTy);
77 LLVMExprResult getPrefixMinusInst(const ASTNode *node, LLVMExprResult &lhs, QualType lhsSTy);
78 LLVMExprResult getPrefixPlusPlusInst(const ASTNode *node, LLVMExprResult &lhs, QualType lhsSTy);
79 LLVMExprResult getPrefixMinusMinusInst(const ASTNode *node, LLVMExprResult &lhs, QualType lhsSTy);
80 LLVMExprResult getPrefixNotInst(const ASTNode *node, LLVMExprResult &lhs, QualType lhsSTy);
81 LLVMExprResult getPrefixBitwiseNotInst(const ASTNode *node, LLVMExprResult &lhs, QualType lhsSTy);
82 LLVMExprResult getPostfixPlusPlusInst(const ASTNode *node, LLVMExprResult &lhs, QualType lhsSTy, size_t opIdx);
83 LLVMExprResult getPostfixMinusMinusInst(const ASTNode *node, LLVMExprResult &lhs, QualType lhsSTy, size_t opIdx);
84 LLVMExprResult getCastInst(const ASTNode *node, QualType lhsSTy, LLVMExprResult &rhs, QualType rhsSTy);
85
86 // Util methods
87 bool callsOverloadedOpFct(const ASTNode *node, size_t opIdx) const;
88
89 private:
90 // Members
91 llvm::LLVMContext &context;
92 llvm::IRBuilder<> &builder;
93 IRGenerator *irGenerator;
94 const StdFunctionManager &stdFunctionManager;
95
96 // Private methods
97 template <size_t N>
98 LLVMExprResult callOperatorOverloadFct(const ASTNode *node, const std::array<ResolverFct, N * 2> &opV, size_t opIdx);
99 [[nodiscard]] llvm::Value *generateIToFp(const QualType &srcSTy, llvm::Value *srcV, llvm::Type *tgtT) const;
100 [[nodiscard]] llvm::Value *generateSHR(const QualType &lhsSTy, const QualType &rhsSTy, llvm::Value *lhsV,
101 llvm::Value *rhsV) const;
102 [[nodiscard]] llvm::Value *generateLT(const QualType &lhsSTy, const QualType &rhsSTy, llvm::Value *lhsV,
103 llvm::Value *rhsV) const;
104 [[nodiscard]] llvm::Value *generateGT(const QualType &lhsSTy, const QualType &rhsSTy, llvm::Value *lhsV,
105 llvm::Value *rhsV) const;
106 [[nodiscard]] llvm::Value *generateLE(const QualType &lhsSTy, const QualType &rhsSTy, llvm::Value *lhsV,
107 llvm::Value *rhsV) const;
108 [[nodiscard]] llvm::Value *generateGE(const QualType &lhsSTy, const QualType &rhsSTy, llvm::Value *lhsV,
109 llvm::Value *rhsV) const;
110 [[nodiscard]] llvm::Value *generateDiv(const QualType &lhsSTy, const QualType &rhsSTy, llvm::Value *lhsV,
111 llvm::Value *rhsV) const;
112 [[nodiscard]] llvm::Value *generateRem(const QualType &lhsSTy, const QualType &rhsSTy, llvm::Value *lhsV,
113 llvm::Value *rhsV) const;
114 11197 [[nodiscard]] static uint32_t getTypeCombination(const QualType &lhsTy, const QualType &rhsTy) {
115 11197 return COMB(lhsTy.getSuperType(), rhsTy.getSuperType());
116 }
117 };
118
119 } // namespace spice::compiler
120