|
| 1 | +// Part of the Carbon Language project, under the Apache License v2.0 with LLVM |
| 2 | +// Exceptions. See /LICENSE for license information. |
| 3 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 4 | + |
| 5 | +#ifndef CARBON_TOOLCHAIN_LOWER_CONTEXT_H_ |
| 6 | +#define CARBON_TOOLCHAIN_LOWER_CONTEXT_H_ |
| 7 | + |
| 8 | +#include <memory> |
| 9 | +#include <optional> |
| 10 | +#include <utility> |
| 11 | + |
| 12 | +#include "llvm/IR/Constants.h" |
| 13 | +#include "llvm/IR/DIBuilder.h" |
| 14 | +#include "llvm/IR/LLVMContext.h" |
| 15 | +#include "llvm/IR/Module.h" |
| 16 | +#include "toolchain/parse/tree_and_subtrees.h" |
| 17 | +#include "toolchain/sem_ir/absolute_node_id.h" |
| 18 | +#include "toolchain/sem_ir/ids.h" |
| 19 | +#include "toolchain/sem_ir/inst_namer.h" |
| 20 | + |
| 21 | +namespace Carbon::Lower { |
| 22 | + |
| 23 | +class FileContext; |
| 24 | + |
| 25 | +// Context for lowering to an LLVM module. |
| 26 | +class Context { |
| 27 | + public: |
| 28 | + // Location information for use with DebugInfo. The line_number and |
| 29 | + // column_number are >= 0, with 0 as unknown, so that they can be passed |
| 30 | + // directly to DebugInfo. |
| 31 | + struct LocForDI { |
| 32 | + llvm::StringRef filename; |
| 33 | + int32_t line_number; |
| 34 | + int32_t column_number; |
| 35 | + }; |
| 36 | + |
| 37 | + explicit Context(llvm::LLVMContext& llvm_context, |
| 38 | + llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs, |
| 39 | + std::optional<llvm::ArrayRef<Parse::GetTreeAndSubtreesFn>> |
| 40 | + tree_and_subtrees_getters_for_debug_info, |
| 41 | + llvm::StringRef module_name, llvm::raw_ostream* vlog_stream); |
| 42 | + |
| 43 | + // Gets or creates the `FileContext` for a given SemIR file. If an |
| 44 | + // `inst_namer` is specified the first time this is called for a file, it will |
| 45 | + // be used for that file. Otherwise, no instruction namer will be used. |
| 46 | + // TODO: Consider building an InstNamer if we're not given one. |
| 47 | + auto GetFileContext(const SemIR::File* file, |
| 48 | + const SemIR::InstNamer* inst_namer = nullptr) |
| 49 | + -> FileContext&; |
| 50 | + |
| 51 | + // Finishes lowering and takes ownership of the LLVM module. The context |
| 52 | + // cannot be used further after calling this. |
| 53 | + auto Finalize() && -> std::unique_ptr<llvm::Module>; |
| 54 | + |
| 55 | + // Returns location information for use with DebugInfo. |
| 56 | + auto GetLocForDI(SemIR::AbsoluteNodeId abs_node_id) -> LocForDI; |
| 57 | + |
| 58 | + // Returns a lowered value to use for a value of type `type`. |
| 59 | + auto GetTypeAsValue() -> llvm::Constant* { |
| 60 | + return llvm::ConstantStruct::get(GetTypeType()); |
| 61 | + } |
| 62 | + |
| 63 | + // Returns a lowered value to use for a value of int literal type. |
| 64 | + auto GetIntLiteralAsValue() -> llvm::Constant* { |
| 65 | + // TODO: Consider adding a named struct type for integer literals. |
| 66 | + return llvm::ConstantStruct::get(llvm::StructType::get(llvm_context())); |
| 67 | + } |
| 68 | + |
| 69 | + // Returns the empty LLVM struct type used to represent the type `type`. |
| 70 | + auto GetTypeType() -> llvm::StructType* { |
| 71 | + if (!type_type_) { |
| 72 | + // `type` is lowered to an empty LLVM StructType. |
| 73 | + type_type_ = llvm::StructType::create(*llvm_context_, {}, "type"); |
| 74 | + } |
| 75 | + return type_type_; |
| 76 | + } |
| 77 | + |
| 78 | + auto llvm_context() -> llvm::LLVMContext& { return *llvm_context_; } |
| 79 | + auto llvm_module() -> llvm::Module& { return *llvm_module_; } |
| 80 | + auto file_system() -> llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>& { |
| 81 | + return file_system_; |
| 82 | + } |
| 83 | + auto di_builder() -> llvm::DIBuilder& { return di_builder_; } |
| 84 | + auto di_compile_unit() -> llvm::DICompileUnit* { return di_compile_unit_; } |
| 85 | + |
| 86 | + auto printf_int_format_string() -> llvm::Value* { |
| 87 | + return printf_int_format_string_; |
| 88 | + } |
| 89 | + auto SetPrintfIntFormatString(llvm::Value* printf_int_format_string) { |
| 90 | + CARBON_CHECK(!printf_int_format_string_, |
| 91 | + "PrintInt formatting string already generated"); |
| 92 | + printf_int_format_string_ = printf_int_format_string; |
| 93 | + } |
| 94 | + |
| 95 | + private: |
| 96 | + // Create the DICompileUnit metadata for this compilation. |
| 97 | + auto BuildDICompileUnit(llvm::StringRef module_name, |
| 98 | + llvm::Module& llvm_module, |
| 99 | + llvm::DIBuilder& di_builder) -> llvm::DICompileUnit*; |
| 100 | + |
| 101 | + // State for building the LLVM IR. |
| 102 | + llvm::LLVMContext* llvm_context_; |
| 103 | + std::unique_ptr<llvm::Module> llvm_module_; |
| 104 | + |
| 105 | + // The filesystem for source code. |
| 106 | + llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> file_system_; |
| 107 | + |
| 108 | + // State for building the LLVM IR debug info metadata. |
| 109 | + llvm::DIBuilder di_builder_; |
| 110 | + |
| 111 | + // The DICompileUnit, if any - null implies debug info is not being emitted. |
| 112 | + llvm::DICompileUnit* di_compile_unit_; |
| 113 | + |
| 114 | + // The trees are only provided when debug info should be emitted. |
| 115 | + std::optional<llvm::ArrayRef<Parse::GetTreeAndSubtreesFn>> |
| 116 | + tree_and_subtrees_getters_for_debug_info_; |
| 117 | + |
| 118 | + // The optional vlog stream. |
| 119 | + llvm::raw_ostream* vlog_stream_; |
| 120 | + |
| 121 | + // The `FileContext`s for each IR that is involved in this lowering action. |
| 122 | + Map<SemIR::CheckIRId, std::unique_ptr<FileContext>> file_contexts_; |
| 123 | + |
| 124 | + // Lowered version of the builtin type `type`. |
| 125 | + llvm::StructType* type_type_ = nullptr; |
| 126 | + |
| 127 | + // Global format string for `printf.int.format` used by the PrintInt builtin. |
| 128 | + llvm::Value* printf_int_format_string_ = nullptr; |
| 129 | +}; |
| 130 | + |
| 131 | +} // namespace Carbon::Lower |
| 132 | + |
| 133 | +#endif // CARBON_TOOLCHAIN_LOWER_CONTEXT_H_ |
0 commit comments