| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #ifndef LYTHON_LLVM_GEN_HEADER | ||
| 2 | #define LYTHON_LLVM_GEN_HEADER | ||
| 3 | |||
| 4 | #include "ast/magic.h" | ||
| 5 | #include "ast/ops.h" | ||
| 6 | #include "ast/visitor.h" | ||
| 7 | #include "sema/bindings.h" | ||
| 8 | #include "sema/builtin.h" | ||
| 9 | #include "sema/errors.h" | ||
| 10 | #include "utilities/strings.h" | ||
| 11 | |||
| 12 | #if WITH_LLVM_CODEGEN | ||
| 13 | // LLVM | ||
| 14 | # include "llvm/IR/IRBuilder.h" | ||
| 15 | # include "llvm/IR/LLVMContext.h" | ||
| 16 | # include "llvm/IR/Module.h" | ||
| 17 | |||
| 18 | namespace lython { | ||
| 19 | |||
| 20 | struct LLVMGenVisitorTrait { | ||
| 21 | using StmtRet = void; | ||
| 22 | using ExprRet = llvm::Value*; | ||
| 23 | using ModRet = void; | ||
| 24 | using PatRet = void; | ||
| 25 | using IsConst = std::false_type; | ||
| 26 | using Trace = std::true_type; | ||
| 27 | |||
| 28 | enum { | ||
| 29 | MaxRecursionDepth = LY_MAX_VISITOR_RECURSION_DEPTH | ||
| 30 | }; | ||
| 31 | }; | ||
| 32 | |||
| 33 | |||
| 34 | template<typename T> | ||
| 35 | struct ArrayScope { | ||
| 36 | ArrayScope(Array<T>& array): array(array), oldsize(array.size()) { | ||
| 37 | 31 | } | |
| 38 | |||
| 39 | ~ArrayScope() { | ||
| 40 |
2/2✓ Branch 1 taken 31 times.
✓ Branch 2 taken 31 times.
|
62 | for (std::size_t i = oldsize; i < array.size(); i++) { |
| 41 | 31 | array[oldsize] = nullptr; | |
| 42 | } | ||
| 43 | 31 | array.resize(oldsize); | |
| 44 | 31 | } | |
| 45 | |||
| 46 | Array<T>& array; | ||
| 47 | std::size_t oldsize; | ||
| 48 | }; | ||
| 49 | |||
| 50 | |||
| 51 | /* | ||
| 52 | */ | ||
| 53 | struct LLVMGen: BaseVisitor<LLVMGen, false, LLVMGenVisitorTrait> { | ||
| 54 | using Super = BaseVisitor<LLVMGen, false, LLVMGenVisitorTrait>; | ||
| 55 | |||
| 56 | using StmtRet = Super::StmtRet; | ||
| 57 | using ExprRet = Super::ExprRet; | ||
| 58 | using ModRet = Super::ModRet; | ||
| 59 | using PatRet = Super::PatRet; | ||
| 60 | |||
| 61 | Unique<llvm::LLVMContext> context; | ||
| 62 | Unique<llvm::Module> llmodule; | ||
| 63 | Unique<llvm::IRBuilder<>> builder; | ||
| 64 | |||
| 65 | Array<llvm::Value*> named_values; | ||
| 66 | Dict<Identifier, std::size_t> index_to_index; | ||
| 67 | |||
| 68 | llvm::Type* builtin_type(StringRef name); | ||
| 69 | llvm::Type* retrieve_type(ExprNode* type, int depth); | ||
| 70 | |||
| 71 | llvm::BasicBlock* start_block = nullptr; | ||
| 72 | llvm::BasicBlock* end_block = nullptr; | ||
| 73 | |||
| 74 | # if WITH_LLVM_DEBUG_SYMBOL | ||
| 75 | Unique<llvm::DIBuilder> dbuilder; | ||
| 76 | DICompileUnit* debug_compile_unit; | ||
| 77 | Arrayr<DIScope*> scopes; | ||
| 78 | |||
| 79 | void emit_location(ExprNode* node); | ||
| 80 | # endif | ||
| 81 | |||
| 82 | ExprRet | ||
| 83 | binary_operator(llvm::IRBuilder<>* builder, ExprNode* left, ExprNode* right, int op, int depth); | ||
| 84 | |||
| 85 | LLVMGen(); | ||
| 86 | ~LLVMGen(); | ||
| 87 | |||
| 88 | void dump() const; | ||
| 89 | |||
| 90 | # define TYPE_GEN(rtype) using rtype##_t = Super::rtype##_t; | ||
| 91 | |||
| 92 | # define X(name, _) | ||
| 93 | # define SSECTION(name) | ||
| 94 | # define EXPR(name, fun) TYPE_GEN(name) | ||
| 95 | # define STMT(name, fun) TYPE_GEN(name) | ||
| 96 | # define MOD(name, fun) TYPE_GEN(name) | ||
| 97 | # define MATCH(name, fun) TYPE_GEN(name) | ||
| 98 | |||
| 99 | NODEKIND_ENUM(X, SSECTION, EXPR, STMT, MOD, MATCH) | ||
| 100 | |||
| 101 | # undef X | ||
| 102 | # undef SSECTION | ||
| 103 | # undef EXPR | ||
| 104 | # undef STMT | ||
| 105 | # undef MOD | ||
| 106 | # undef MATCH | ||
| 107 | # undef TYPE_GEN | ||
| 108 | |||
| 109 | # define FUNCTION_GEN(name, fun, ret) virtual ret fun(name##_t* n, int depth); | ||
| 110 | |||
| 111 | # define X(name, _) | ||
| 112 | # define SSECTION(name) | ||
| 113 | # define MOD(name, fun) FUNCTION_GEN(name, fun, ModRet) | ||
| 114 | # define EXPR(name, fun) FUNCTION_GEN(name, fun, ExprRet) | ||
| 115 | # define STMT(name, fun) FUNCTION_GEN(name, fun, StmtRet) | ||
| 116 | # define MATCH(name, fun) FUNCTION_GEN(name, fun, PatRet) | ||
| 117 | |||
| 118 | NODEKIND_ENUM(X, SSECTION, EXPR, STMT, MOD, MATCH) | ||
| 119 | |||
| 120 | # undef X | ||
| 121 | # undef SSECTION | ||
| 122 | # undef EXPR | ||
| 123 | # undef STMT | ||
| 124 | # undef MOD | ||
| 125 | # undef MATCH | ||
| 126 | |||
| 127 | # undef FUNCTION_GEN | ||
| 128 | }; | ||
| 129 | |||
| 130 | } // namespace lython | ||
| 131 | |||
| 132 | #endif | ||
| 133 | #endif | ||
| 134 |