/
codegen_llvm.h
90 lines (79 loc) · 2.64 KB
/
codegen_llvm.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#pragma once
#include <iostream>
#include <ostream>
#include "ast.h"
#include "bpftrace.h"
#include "irbuilderbpf.h"
#include "map.h"
#include <llvm/ExecutionEngine/MCJIT.h>
#include <llvm/IR/IRBuilder.h>
#include <llvm/IR/Module.h>
namespace bpftrace {
namespace ast {
using namespace llvm;
class CodegenLLVM : public Visitor {
public:
explicit CodegenLLVM(Node *root, BPFtrace &bpftrace) :
root_(root),
module_(std::make_unique<Module>("bpftrace", context_)),
b_(context_, *module_.get(), bpftrace),
layout_(module_.get()),
bpftrace_(bpftrace)
{ }
void visit(Integer &integer) override;
void visit(PositionalParameter ¶m) override;
void visit(String &string) override;
void visit(Identifier &identifier) override;
void visit(Builtin &builtin) override;
void visit(StackMode &) override { };
void visit(Call &call) override;
void visit(Map &map) override;
void visit(Variable &var) override;
void visit(Binop &binop) override;
void visit(Unop &unop) override;
void visit(Ternary &ternary) override;
void visit(FieldAccess &acc) override;
void visit(ArrayAccess &arr) override;
void visit(Cast &cast) override;
void visit(ExprStatement &expr) override;
void visit(AssignMapStatement &assignment) override;
void visit(AssignVarStatement &assignment) override;
void visit(If &if_block) override;
void visit(Unroll &unroll) override;
void visit(Predicate &pred) override;
void visit(AttachPoint &ap) override;
void visit(Probe &probe) override;
void visit(Program &program) override;
AllocaInst *getMapKey(Map &map);
AllocaInst *getHistMapKey(Map &map, Value *log2);
int getNextIndexForProbe(const std::string &probe_name);
std::string getSectionNameForProbe(const std::string &probe_name, int index);
Value *createLogicalAnd(Binop &binop);
Value *createLogicalOr(Binop &binop);
void createLog2Function();
void createLinearFunction();
std::unique_ptr<BpfOrc> compile(DebugLevel debug=DebugLevel::kNone, std::ostream &out=std::cerr);
private:
Node *root_;
LLVMContext context_;
std::unique_ptr<Module> module_;
std::unique_ptr<ExecutionEngine> ee_;
IRBuilderBPF b_;
DataLayout layout_;
Value *expr_ = nullptr;
std::function<void()> expr_deleter_; // intentionally empty
Value *ctx_;
AttachPoint *current_attach_point_ = nullptr;
BPFtrace &bpftrace_;
std::string probefull_;
std::string tracepoint_struct_;
std::map<std::string, int> next_probe_index_;
std::map<std::string, AllocaInst *> variables_;
int printf_id_ = 0;
int time_id_ = 0;
int cat_id_ = 0;
uint64_t join_id_ = 0;
int system_id_ = 0;
};
} // namespace ast
} // namespace bpftrace