Proteus
Programmable JIT compilation and optimization for C/C++ using LLVM
Loading...
Searching...
No Matches
Func.hpp
Go to the documentation of this file.
1#ifndef PROTEUS_FRONTEND_FUNC_HPP
2#define PROTEUS_FRONTEND_FUNC_HPP
3
4#include <deque>
5
6#include <llvm/IR/IRBuilder.h>
7#include <llvm/IR/Module.h>
8
9#include "proteus/Error.h"
12
13namespace proteus {
14
15struct Var;
16
17using namespace llvm;
18
19class Func {
20private:
21 FunctionCallee FC;
22 IRBuilder<> IRB;
23 IRBuilderBase::InsertPoint IP;
24 std::deque<Var> Arguments;
25 std::deque<Var> Variables;
26 std::string Name;
27
28 enum class ScopeKind { FUNCTION, IF, FOR };
29 struct Scope {
30 std::string File;
31 int Line;
32 ScopeKind Kind;
33 IRBuilderBase::InsertPoint ContIP;
34
35 explicit Scope(const char *File, int Line, ScopeKind Kind,
36 IRBuilderBase::InsertPoint ContIP)
37 : File(File), Line(Line), Kind(Kind), ContIP(ContIP) {}
38 };
39 std::vector<Scope> Scopes;
40
41 std::string toString(ScopeKind Kind) {
42 switch (Kind) {
43 case ScopeKind::FUNCTION:
44 return "FUNCTION";
45 case ScopeKind::IF:
46 return "IF";
47 case ScopeKind::FOR:
48 return "FOR";
49 default:
50 PROTEUS_FATAL_ERROR("Unsupported Kind " +
51 std::to_string(static_cast<int>(Kind)));
52 }
53 }
54
55public:
56 Func(FunctionCallee FC);
57
58 Function *getFunction();
59
60 AllocaInst *emitAlloca(Type *Ty, StringRef Name);
61
62 IRBuilderBase &getIRB();
63
64 Var &declVarInternal(StringRef Name, Type *Ty,
65 Type *PointerElemType = nullptr);
66
67 template <typename T> Var &declVar(StringRef Name) {
68 Function *F = getFunction();
69 auto *Alloca = emitAlloca(TypeMap<T>::get(F->getContext()), Name);
70
71 return Variables.emplace_back(
72 Alloca, *this, TypeMap<T>::getPointerElemType(F->getContext()));
73 }
74
75 template <typename... Ts> void declArgs() {
76 Function *F = getFunction();
77 auto &EntryBB = F->getEntryBlock();
78 IP = IRBuilderBase::InsertPoint(&EntryBB, EntryBB.end());
79 IRB.restoreIP(IP);
80
81 (
82 [&]() {
83 auto *Alloca = emitAlloca(TypeMap<Ts>::get(F->getContext()),
84 "arg." + std::to_string(Arguments.size()));
85
86 auto *Arg = F->getArg(Arguments.size());
87 IRB.CreateStore(Arg, Alloca);
88
89 Arguments.emplace_back(
90 Alloca, *this, TypeMap<Ts>::getPointerElemType(F->getContext()));
91 }(),
92 ...);
93 IRB.ClearInsertionPoint();
94 }
95
96 Var &getArg(unsigned int ArgNo);
97
98 void beginFunction(const char *File = __builtin_FILE(),
99 int Line = __builtin_LINE());
100 void endFunction();
101
102 void beginIf(Var &CondVar, const char *File = __builtin_FILE(),
103 int Line = __builtin_LINE());
104 void endIf();
105
106 void beginFor(Var &IterVar, Var &InitVar, Var &UpperBound, Var &IncVar,
107 const char *File = __builtin_FILE(),
108 int Line = __builtin_LINE());
109 void endFor();
110
111 template <typename RetT, typename... ArgT> void call(StringRef Name);
112
113 Var &callBuiltin(function_ref<Var &(Func &)> Lower) { return Lower(*this); }
114
115 void ret(std::optional<std::reference_wrapper<Var>> OptRet = std::nullopt);
116
117 StringRef getName() { return Name; }
118};
119
120} // namespace proteus
121
122#endif // PROTEUS_FRONTEND_FUNC_HPP
#define PROTEUS_FATAL_ERROR(x)
Definition Error.h:4
Definition Func.hpp:19
void endFor()
Definition Func.cpp:204
Function * getFunction()
Definition Func.cpp:58
void declArgs()
Definition Func.hpp:75
StringRef getName()
Definition Func.hpp:117
void beginIf(Var &CondVar, const char *File=__builtin_FILE(), int Line=__builtin_LINE())
Definition Func.cpp:96
void endFunction()
Definition Func.cpp:45
Var & declVarInternal(StringRef Name, Type *Ty, Type *PointerElemType=nullptr)
Definition Func.cpp:17
Var & getArg(unsigned int ArgNo)
Definition Func.cpp:65
Var & declVar(StringRef Name)
Definition Func.hpp:67
void ret(std::optional< std::reference_wrapper< Var > > OptRet=std::nullopt)
Definition Func.cpp:79
AllocaInst * emitAlloca(Type *Ty, StringRef Name)
Definition Func.cpp:67
void endIf()
Definition Func.cpp:130
void call(StringRef Name)
Definition JitFrontend.hpp:222
Var & callBuiltin(function_ref< Var &(Func &)> Lower)
Definition Func.hpp:113
void beginFor(Var &IterVar, Var &InitVar, Var &UpperBound, Var &IncVar, const char *File=__builtin_FILE(), int Line=__builtin_LINE())
Definition Func.cpp:146
void beginFunction(const char *File=__builtin_FILE(), int Line=__builtin_LINE())
Definition Func.cpp:22
IRBuilderBase & getIRB()
Definition Func.cpp:11
Definition Dispatcher.cpp:14
Definition TypeMap.hpp:13
Definition Var.hpp:13