Commit 2534466f authored by Alastair Robertson's avatar Alastair Robertson

Add code generation tests

parent 9f227ee1
...@@ -41,6 +41,12 @@ add_flex_bison_dependency(flex_lexer bison_parser) ...@@ -41,6 +41,12 @@ add_flex_bison_dependency(flex_lexer bison_parser)
add_library(parser ${BISON_bison_parser_OUTPUTS} ${FLEX_flex_lexer_OUTPUTS}) add_library(parser ${BISON_bison_parser_OUTPUTS} ${FLEX_flex_lexer_OUTPUTS})
target_include_directories(parser PUBLIC src ${CMAKE_BINARY_DIR}) target_include_directories(parser PUBLIC src ${CMAKE_BINARY_DIR})
find_package(LLVM REQUIRED CONFIG)
include_directories(${LLVM_INCLUDE_DIRS})
add_definitions(${LLVM_DEFINITIONS})
set(DYNAMIC_LINKING OFF CACHE BOOL "Build bpftrace as a dynamically linked executable")
add_subdirectory(src/arch) add_subdirectory(src/arch)
add_subdirectory(src) add_subdirectory(src)
add_subdirectory(tests) add_subdirectory(tests)
find_package(LLVM REQUIRED CONFIG)
include_directories(${LLVM_INCLUDE_DIRS})
add_definitions(${LLVM_DEFINITIONS})
set(DYNAMIC_LINKING OFF CACHE BOOL "Build bpftrace as a dynamically linked executable")
if (NOT DYNAMIC_LINKING) if (NOT DYNAMIC_LINKING)
set(CMAKE_EXE_LINKER_FLAGS "-static") set(CMAKE_EXE_LINKER_FLAGS "-static")
endif() endif()
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "arch/arch.h" #include "arch/arch.h"
#include <llvm/ExecutionEngine/SectionMemoryManager.h> #include <llvm/ExecutionEngine/SectionMemoryManager.h>
#include <llvm/Support/raw_os_ostream.h>
#include <llvm/Support/TargetRegistry.h> #include <llvm/Support/TargetRegistry.h>
#include <llvm/IR/LegacyPassManager.h> #include <llvm/IR/LegacyPassManager.h>
#include <llvm/Transforms/IPO.h> #include <llvm/Transforms/IPO.h>
...@@ -614,7 +615,7 @@ void CodegenLLVM::createStrcmpFunction() ...@@ -614,7 +615,7 @@ void CodegenLLVM::createStrcmpFunction()
b_.CreateRet(b_.getInt1(0)); b_.CreateRet(b_.getInt1(0));
} }
int CodegenLLVM::compile(bool debug) int CodegenLLVM::compile(bool debug, std::ostream &out)
{ {
createLog2Function(); createLog2Function();
createStrcmpFunction(); createStrcmpFunction();
...@@ -657,7 +658,10 @@ int CodegenLLVM::compile(bool debug) ...@@ -657,7 +658,10 @@ int CodegenLLVM::compile(bool debug)
PM.run(*module_.get()); PM.run(*module_.get());
if (debug) if (debug)
module_->dump(); {
raw_os_ostream llvm_ostream(out);
module_->print(llvm_ostream, nullptr, false, true);
}
EngineBuilder builder(move(module_)); EngineBuilder builder(move(module_));
builder.setMCJITMemoryManager(std::make_unique<BPFtraceMemoryManager>(bpftrace_.sections_)); builder.setMCJITMemoryManager(std::make_unique<BPFtraceMemoryManager>(bpftrace_.sections_));
......
#pragma once #pragma once
#include <iostream>
#include <ostream>
#include "ast.h" #include "ast.h"
#include "bpftrace.h" #include "bpftrace.h"
#include "irbuilderbpf.h" #include "irbuilderbpf.h"
...@@ -45,7 +48,7 @@ public: ...@@ -45,7 +48,7 @@ public:
void createLog2Function(); void createLog2Function();
void createStrcmpFunction(); void createStrcmpFunction();
int compile(bool debug=false); int compile(bool debug=false, std::ostream &out=std::cerr);
private: private:
Node *root_; Node *root_;
......
if (NOT DYNAMIC_LINKING)
set(CMAKE_EXE_LINKER_FLAGS "-static")
endif()
add_compile_options("-Wno-undef") add_compile_options("-Wno-undef")
add_compile_options("-Wno-switch-default") add_compile_options("-Wno-switch-default")
add_compile_options("-Wno-switch-enum") add_compile_options("-Wno-switch-enum")
...@@ -8,13 +12,16 @@ include_directories(${CMAKE_BINARY_DIR}/src) ...@@ -8,13 +12,16 @@ include_directories(${CMAKE_BINARY_DIR}/src)
add_executable(bpftrace_test add_executable(bpftrace_test
ast.cpp ast.cpp
bpftrace.cpp bpftrace.cpp
codegen.cpp
main.cpp main.cpp
parser.cpp parser.cpp
semantic_analyser.cpp semantic_analyser.cpp
${CMAKE_SOURCE_DIR}/src/ast.cpp ${CMAKE_SOURCE_DIR}/src/ast.cpp
${CMAKE_SOURCE_DIR}/src/attached_probe.cpp ${CMAKE_SOURCE_DIR}/src/attached_probe.cpp
${CMAKE_SOURCE_DIR}/src/bpftrace.cpp ${CMAKE_SOURCE_DIR}/src/bpftrace.cpp
${CMAKE_SOURCE_DIR}/src/codegen_llvm.cpp
${CMAKE_SOURCE_DIR}/src/driver.cpp ${CMAKE_SOURCE_DIR}/src/driver.cpp
${CMAKE_SOURCE_DIR}/src/irbuilderbpf.cpp
${CMAKE_SOURCE_DIR}/src/map.cpp ${CMAKE_SOURCE_DIR}/src/map.cpp
${CMAKE_SOURCE_DIR}/src/mapkey.cpp ${CMAKE_SOURCE_DIR}/src/mapkey.cpp
${CMAKE_SOURCE_DIR}/src/printer.cpp ${CMAKE_SOURCE_DIR}/src/printer.cpp
...@@ -25,6 +32,9 @@ add_executable(bpftrace_test ...@@ -25,6 +32,9 @@ add_executable(bpftrace_test
target_link_libraries(bpftrace_test arch parser) target_link_libraries(bpftrace_test arch parser)
llvm_map_components_to_libnames(llvm_libs bpfcodegen ipo irreader mcjit)
target_link_libraries(bpftrace_test ${llvm_libs})
add_dependencies(bpftrace_test bcc-build) add_dependencies(bpftrace_test bcc-build)
ExternalProject_Get_Property(bcc source_dir binary_dir) ExternalProject_Get_Property(bcc source_dir binary_dir)
include_directories(${source_dir}/src/cc) include_directories(${source_dir}/src/cc)
......
This diff is collapsed.
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