Commit 4298752c authored by Alastair Robertson's avatar Alastair Robertson

Let the driver handle most stuff

parent acb74cd5
......@@ -13,7 +13,7 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR})
include_directories(${LLVM_INCLUDE_DIRS})
add_definitions(${LLVM_DEFINITIONS})
add_executable(bpftrace main.cpp parser.tab.cc lex.yy.cc ast.cpp printer.cpp codegen.cpp)
add_executable(bpftrace main.cpp driver.cpp parser.tab.cc lex.yy.cc ast.cpp printer.cpp codegen.cpp)
llvm_map_components_to_libnames(llvm_libs support core irreader)
target_link_libraries(bpftrace ${llvm_libs})
......@@ -13,9 +13,10 @@ using namespace llvm;
class Codegen : public Visitor {
public:
Codegen() : b_(context_),
module_("bpftrace", context_)
{ }
Codegen(Module &mod, LLVMContext &context) : context_(context),
module_(mod),
b_(context_)
{ }
void visit(Integer &integer) override;
void visit(Variable &var) override;
......@@ -27,10 +28,10 @@ public:
void visit(Probe &probe) override;
void visit(Program &program) override;
//private:
LLVMContext context_;
private:
LLVMContext &context_;
Module &module_;
IRBuilder<> b_;
Module module_;
Value *expr_ = nullptr;
};
......
#include <istream>
#include <ostream>
#include "driver.h"
#include "printer.h"
#include "codegen.h"
namespace ebpf {
namespace bpftrace {
int Driver::parse()
{
return parser_.parse();
}
int Driver::parse(const std::string &f)
{
if (!(yyin = fopen(f.c_str(), "r"))) {
std::cerr << "Could not open file" << std::endl;
return -1;
}
return parser_.parse();
}
int Driver::dump_ast(std::ostream &out)
{
ast::Printer p = ebpf::bpftrace::ast::Printer(out);
root_->accept(p);
}
} // namespace bpftrace
} // namespace ebpf
#pragma once
#include <istream>
#include "parser.tab.hh"
#include "ast.h"
#include <llvm/IR/Module.h>
#define YY_DECL ebpf::bpftrace::Parser::symbol_type yylex(ebpf::bpftrace::Driver &driver)
YY_DECL;
......@@ -12,23 +13,17 @@ extern FILE *yyin;
namespace ebpf {
namespace bpftrace {
using namespace llvm;
class Driver {
public:
Driver() : parser_(*this) { }
int parse()
{
return parser_.parse();
}
Driver() : parser_(*this),
module_("bpftrace", context_)
{ }
int parse(const std::string &f)
{
if (!(yyin = fopen(f.c_str(), "r"))) {
std::cerr << "Could not open file" << std::endl;
return -1;
}
return parser_.parse();
}
int parse();
int parse(const std::string &f);
int dump_ast(std::ostream &out);
void error(const location &l, const std::string &m)
{
......@@ -41,8 +36,11 @@ public:
}
ast::Program *root_;
private:
Parser parser_;
LLVMContext context_;
Module module_;
};
} // namespace bpftrace
......
#include <iostream>
#include "driver.h"
#include "ast.h"
#include "printer.h"
#include "codegen.h"
int main(int argc, char *argv[])
{
......@@ -16,12 +13,7 @@ int main(int argc, char *argv[])
}
if (!result) {
ebpf::bpftrace::ast::Printer p = ebpf::bpftrace::ast::Printer(std::cout);
driver.root_->accept(p);
ebpf::bpftrace::ast::Codegen c;
driver.root_->accept(c);
c.module_.dump();
driver.dump_ast(std::cout);
}
return result;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment