| chigraph master Systems programming language written for beginners in LLVM

FunctionCompiler.hpp
Go to the documentation of this file.
1
4 #pragma once
5
6 #ifndef CHI_FUNCTION_COMPILER_HPP
7 #define CHI_FUNCTION_COMPILER_HPP
8
9 #include "chi/Fwd.hpp"
10 #include "chi/LLVMVersion.hpp"
11 #include "chi/NodeCompiler.hpp"
12
13 #include <llvm/IR/DebugInfo.h>
14
15 #include <memory>
16 #include <unordered_map>
17
18 #include <boost/bimap.hpp>
19 #include <boost/utility/string_view.hpp>
20
21 namespace chi {
22
24 struct FunctionCompiler {
30 FunctionCompiler(const GraphFunction& func, llvm::Module& moduleToGenInto,
31 llvm::DICompileUnit& debugCU, llvm::DIBuilder& debugBuilder);
32
38 Result initialize(bool validate = true);
39
45 Result compile();
46
47 using DebugFunctionType =
48 #if LLVM_VERSION_LESS_EQUAL(3, 6)
49 llvm::DICompositeType
50 #else
51 llvm::DISubroutineType*
52 #endif
53 ;
54
57 DebugFunctionType createSubroutineType();
58
59 using DebugFunction = llvm::DISubprogram
60 #if LLVM_VERSION_AT_LEAST(3, 7)
61 *
62 #endif
63 ;
64 using DebugFile = llvm::DIFile
65 #if LLVM_VERSION_AT_LEAST(3, 7)
66 *
67 #endif
68 ;
69
73 DebugFunction diFunction() {
74 assert(initialized() &&
75 "Please initialize the function compiler before getting the debug function");
76 return mDebugFunc;
77 }
78
81 llvm::Module& llvmModule() const { return *mModule; }
82
85 llvm::DIBuilder& diBuilder() const { return *mDIBuilder; }
86
89 llvm::DICompileUnit* debugCompileUnit() const { return mDebugCU; }
90
94 llvm::BasicBlock& allocBlock() const {
95 assert(initialized() &&
96 "Please initialize the function compiler before getting the alloc block");
97 return *mAllocBlock;
98 }
99
103 llvm::Value* localVariable(boost::string_view name);
104
108 llvm::Function& llFunction() const {
109 assert(initialized() &&
110 "Please initialize the function compiler before trying to get the LLVM function");
111 return *mLLFunction;
112 }
113
117 llvm::Value& postPureBreak() const {
118 assert(initialized() &&
119 "Please initialize the function compiler before trying to get the post-pure break");
120 return *mPostPureBreak;
121 }
122
125 const GraphFunction& function() const { return *mFunction; }
126
129 GraphModule& module() const;
130
133 Context& context() const;
134
140 int nodeLineNumber(NodeInstance& node);
141
144 bool initialized() const { return mInitialized; }
145
148 bool compiled() const { return mCompiled; }
149
153 NodeCompiler* nodeCompiler(NodeInstance& node);
154
158 NodeCompiler* getOrCreateNodeCompiler(NodeInstance& node);
159
160 private:
161 std::unordered_map<std::string, llvm::Value*> mLocalVariables;
162
163 llvm::Module* mModule = nullptr;
164 llvm::DIBuilder* mDIBuilder = nullptr;
165 llvm::DICompileUnit* mDebugCU = nullptr;
166 DebugFile mDIFile{};
167 DebugFunction mDebugFunc{};
168
169 const GraphFunction* mFunction = nullptr;
170
171 llvm::Function* mLLFunction = nullptr;
172 llvm::BasicBlock* mAllocBlock = nullptr;
173
174 std::unordered_map<NodeInstance*, NodeCompiler> mNodeCompilers;
175
176 boost::bimap<unsigned, NodeInstance*> mNodeLocations;
177
178 bool mInitialized = false;
179 bool mCompiled = false;
180
181 llvm::Value* mPostPureBreak = nullptr;
182 };
183
190 Result compileFunction(const GraphFunction& func, llvm::Module* mod, llvm::DICompileUnit* debugCU,
191 llvm::DIBuilder& debugBuilder);
192 } // namespace chi
193
194 #endif // CHI_FUNCTION_COMPILER_HPP
LLVMVersion.hpp
chi::GraphFunction
this is an AST-like representation of a function in a graph It is used for IDE-like behavior...
Definition: GraphFunction.hpp:23
chi::FunctionCompiler::nodeCompiler
NodeCompiler * nodeCompiler(NodeInstance &node)
Get a node compile for a certain node.
Definition: FunctionCompiler.cpp:353
Fwd.hpp
Forward declares all the chigraph data types.
chi::FunctionCompiler::llvmModule
llvm::Module & llvmModule() const
Get the module being generated.
Definition: FunctionCompiler.hpp:81
chi::FunctionCompiler::diFunction
DebugFunction diFunction()
Get the debug function.
Definition: FunctionCompiler.hpp:73
chi::NodeInstance
An instance of a node.
Definition: NodeInstance.hpp:19
chi::NodeCompiler
Helper to compile nodes.
Definition: NodeCompiler.hpp:24
chi::FunctionCompiler::compile
Result compile()
Generates the contents of the function.
Definition: FunctionCompiler.cpp:198
chi::FunctionCompiler::debugCompileUnit
llvm::DICompileUnit * debugCompileUnit() const
The compile unit for the module.
Definition: FunctionCompiler.hpp:89
chi::Context
The class that handles the loading, creation, storing, and compilation of modules It also stores a LL...
Definition: Context.hpp:55
chi::FunctionCompiler
Class for compiling GraphFunctions into llvm::Functions.
Definition: FunctionCompiler.hpp:24
chi::FunctionCompiler::postPureBreak
llvm::Value & postPureBreak() const
Get the value for the address to jump back to.
Definition: FunctionCompiler.hpp:117
chi::FunctionCompiler::nodeLineNumber
int nodeLineNumber(NodeInstance &node)
Get the debug line number for the node instance Unique for each node.
Definition: FunctionCompiler.cpp:342
chi::FunctionCompiler::allocBlock
llvm::BasicBlock & allocBlock() const
The block for allocating variables at the beginning of the function.
Definition: FunctionCompiler.hpp:94
chi::FunctionCompiler::getOrCreateNodeCompiler
NodeCompiler * getOrCreateNodeCompiler(NodeInstance &node)
Get or create a node compiler for a node.
Definition: FunctionCompiler.cpp:362
chi::FunctionCompiler::compiled
bool compiled() const
Get if the function has been compiled (compile() has been called)
Definition: FunctionCompiler.hpp:148
chi::FunctionCompiler::FunctionCompiler
FunctionCompiler(const GraphFunction &func, llvm::Module &moduleToGenInto, llvm::DICompileUnit &debugCU, llvm::DIBuilder &debugBuilder)
Constructor.
Definition: FunctionCompiler.cpp:31
chi::compileFunction
Result compileFunction(const GraphFunction &func, llvm::Module *mod, llvm::DICompileUnit *debugCU, llvm::DIBuilder &debugBuilder)
Compile the graph to an llvm::Function (usually called from JsonModule::generateModule) ...
Definition: FunctionCompiler.cpp:371
chi::FunctionCompiler::localVariable
llvm::Value * localVariable(boost::string_view name)
Get the value for a local variable.
Definition: FunctionCompiler.cpp:330
chi::FunctionCompiler::context
Context & context() const
Get function().context()
Definition: FunctionCompiler.cpp:340
chi::FunctionCompiler::diBuilder
llvm::DIBuilder & diBuilder() const
The debug builder we&#39;re using for the module.
Definition: FunctionCompiler.hpp:85
chi::GraphModule
Module that holds graph functions.
Definition: GraphModule.hpp:16
chi::FunctionCompiler::llFunction
llvm::Function & llFunction() const
Get the llvm function this compiler is generating.
Definition: FunctionCompiler.hpp:108
chi
The namespace where chigraph lives.
Definition: BitcodeParser.hpp:12
chi::FunctionCompiler::initialize
Result initialize(bool validate=true)
Creates the function, but don&#39;t actually generate into it.
Definition: FunctionCompiler.cpp:35
chi::FunctionCompiler::initialized
bool initialized() const
Get if the function is initialized (initialize() has been called)
Definition: FunctionCompiler.hpp:144
chi::Result
The result object, used for identifiying errors with good diagnostics.
Definition: Result.hpp:72
NodeCompiler.hpp
chi::FunctionCompiler::module
GraphModule & module() const
Get function().module()
Definition: FunctionCompiler.cpp:339
chi::FunctionCompiler::createSubroutineType
DebugFunctionType createSubroutineType()
Create the subroutine type for the function.
Definition: FunctionCompiler.cpp:275