Commit 290333bc authored by Alastair Robertson's avatar Alastair Robertson

Remove attempt at generating C code

parent 28722b71
......@@ -30,7 +30,7 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR})
include_directories(${LLVM_INCLUDE_DIRS})
add_definitions(${LLVM_DEFINITIONS})
add_executable(bpftrace main.cpp driver.cpp parser.tab.cc lex.yy.cc ast.cpp printer.cpp codegen_llvm.cpp codegen_bcc.cpp map.cpp semantic_analyser.cpp bpftrace.cpp)
add_executable(bpftrace main.cpp driver.cpp parser.tab.cc lex.yy.cc ast.cpp printer.cpp codegen_llvm.cpp map.cpp semantic_analyser.cpp bpftrace.cpp)
llvm_map_components_to_libnames(llvm_libs irreader bpfcodegen mcjit)
target_link_libraries(bpftrace ${llvm_libs})
#include "codegen_bcc.h"
#include "ast.h"
#include "parser.tab.hh"
namespace ebpf {
namespace bpftrace {
namespace ast {
void CodegenBCC::visit(Integer &integer)
{
code << integer.n;
}
void CodegenBCC::visit(Builtin &builtin)
{
code << builtin.ident;
}
void CodegenBCC::visit(Call &call)
{
code << call.func << "(";
if (call.vargs) {
for (Expression *expr : *call.vargs) {
expr->accept(*this);
code << ", ";
}
}
code << ")";
}
void CodegenBCC::visit(Map &map)
{
code << map.ident;
if (map.vargs) {
code << "[";
for (Expression *expr : *map.vargs) {
expr->accept(*this);
code << ", ";
}
code << "]";
}
}
void CodegenBCC::visit(Binop &binop)
{
code << "(";
binop.left->accept(*this);
switch (binop.op) {
case ebpf::bpftrace::Parser::token::EQ: code << "=="; break;
case ebpf::bpftrace::Parser::token::NE: code << "!="; break;
case ebpf::bpftrace::Parser::token::LE: code << "<="; break;
case ebpf::bpftrace::Parser::token::GE: code << ">="; break;
case ebpf::bpftrace::Parser::token::LT: code << "<"; break;
case ebpf::bpftrace::Parser::token::GT: code << ">"; break;
case ebpf::bpftrace::Parser::token::LAND: code << "&&"; break;
case ebpf::bpftrace::Parser::token::LOR: code << "||"; break;
case ebpf::bpftrace::Parser::token::PLUS: code << "+"; break;
case ebpf::bpftrace::Parser::token::MINUS: code << "-"; break;
case ebpf::bpftrace::Parser::token::MUL: code << "*"; break;
case ebpf::bpftrace::Parser::token::DIV: code << "/"; break;
case ebpf::bpftrace::Parser::token::MOD: code << "%"; break;
case ebpf::bpftrace::Parser::token::BAND: code << "&"; break;
case ebpf::bpftrace::Parser::token::BOR: code << "|"; break;
case ebpf::bpftrace::Parser::token::BXOR: code << "^"; break;
default: abort();
}
binop.right->accept(*this);
code << ")";
}
void CodegenBCC::visit(Unop &unop)
{
code << "(";
switch (unop.op) {
case ebpf::bpftrace::Parser::token::LNOT: code << "!"; break;
case ebpf::bpftrace::Parser::token::BNOT: code << "~"; break;
default: abort();
}
unop.expr->accept(*this);
code << ")";
}
void CodegenBCC::visit(ExprStatement &expr)
{
expr.expr->accept(*this);
}
void CodegenBCC::visit(AssignMapStatement &assignment)
{
assignment.map->accept(*this);
code << "=";
assignment.expr->accept(*this);
}
void CodegenBCC::visit(AssignMapCallStatement &assignment)
{
assignment.map->accept(*this);
code << "=";
assignment.call->accept(*this);
}
void CodegenBCC::visit(Predicate &pred)
{
code << "if (!(";
pred.expr->accept(*this);
code << ")) return 0;" << std::endl;
}
void CodegenBCC::visit(Probe &probe)
{
code << "int " << probe.type << "__" << probe.attach_point << "() {" << std::endl;
if (probe.pred) {
probe.pred->accept(*this);
}
for (Statement *stmt : *probe.stmts) {
stmt->accept(*this);
code << ";" << std::endl;
}
code << "return 0;\n}" << std::endl;
}
void CodegenBCC::visit(Program &program)
{
for (Probe *probe : *program.probes) {
probe->accept(*this);
}
}
int CodegenBCC::compile()
{
root_->accept(*this);
return 0;
}
} // namespace ast
} // namespace bpftrace
} // namespace ebpf
#pragma once
#include <sstream>
#include "ast.h"
#include "bpftrace.h"
#include "map.h"
namespace ebpf {
namespace bpftrace {
namespace ast {
class CodegenBCC : public Visitor {
public:
explicit CodegenBCC(Node *root, BPFtrace &bpftrace)
: root_(root),
bpftrace_(bpftrace) { }
void visit(Integer &integer) override;
void visit(Builtin &builtin) override;
void visit(Call &call) override;
void visit(Map &map) override;
void visit(Binop &binop) override;
void visit(Unop &unop) override;
void visit(ExprStatement &expr) override;
void visit(AssignMapStatement &assignment) override;
void visit(AssignMapCallStatement &assignment) override;
void visit(Predicate &pred) override;
void visit(Probe &probe) override;
void visit(Program &program) override;
int compile();
std::ostringstream code;
private:
Node *root_;
BPFtrace &bpftrace_;
};
} // namespace ast
} // namespace bpftrace
} // namespace ebpf
#include <iostream>
#include "codegen_bcc.h"
#include "codegen_llvm.h"
#include "driver.h"
#include "printer.h"
......@@ -38,12 +37,7 @@ int Driver::analyse()
int Driver::compile()
{
ast::CodegenLLVM llvm(root_, bpftrace_);
int result_llvm = llvm.compile();
ast::CodegenBCC bcc(root_, bpftrace_);
int result_bcc = bcc.compile();
std::cout << bcc.code.str();
return result_llvm && result_bcc;
return llvm.compile();
}
} // namespace bpftrace
......
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