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 
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 
154 
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
this is an AST-like representation of a function in a graph It is used for IDE-like behavior...
NodeCompiler * nodeCompiler(NodeInstance &node)
Get a node compile for a certain node.
Forward declares all the chigraph data types.
llvm::Module & llvmModule() const
Get the module being generated.
DebugFunction diFunction()
Get the debug function.
An instance of a node.
Helper to compile nodes.
Result compile()
Generates the contents of the function.
llvm::DICompileUnit * debugCompileUnit() const
The compile unit for the module.
The class that handles the loading, creation, storing, and compilation of modules It also stores a LL...
Definition: Context.hpp:55
Class for compiling GraphFunctions into llvm::Functions.
llvm::Value & postPureBreak() const
Get the value for the address to jump back to.
int nodeLineNumber(NodeInstance &node)
Get the debug line number for the node instance Unique for each node.
llvm::BasicBlock & allocBlock() const
The block for allocating variables at the beginning of the function.
NodeCompiler * getOrCreateNodeCompiler(NodeInstance &node)
Get or create a node compiler for a node.
bool compiled() const
Get if the function has been compiled (compile() has been called)
FunctionCompiler(const GraphFunction &func, llvm::Module &moduleToGenInto, llvm::DICompileUnit &debugCU, llvm::DIBuilder &debugBuilder)
Constructor.
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) ...
llvm::Value * localVariable(boost::string_view name)
Get the value for a local variable.
Context & context() const
Get function().context()
llvm::DIBuilder & diBuilder() const
The debug builder we&#39;re using for the module.
Module that holds graph functions.
Definition: GraphModule.hpp:16
llvm::Function & llFunction() const
Get the llvm function this compiler is generating.
The namespace where chigraph lives.
Result initialize(bool validate=true)
Creates the function, but don&#39;t actually generate into it.
bool initialized() const
Get if the function is initialized (initialize() has been called)
The result object, used for identifiying errors with good diagnostics.
Definition: Result.hpp:72
GraphModule & module() const
Get function().module()
DebugFunctionType createSubroutineType()
Create the subroutine type for the function.