finalize ep 12

This commit is contained in:
Sameer Rahmani 2021-11-03 14:06:45 +00:00
parent 5622b4de0c
commit 69e8782c67
4 changed files with 192 additions and 148 deletions

View File

@ -169,10 +169,12 @@ if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
# LLVM setup =========================================
find_package(LLVM REQUIRED CONFIG)
find_package(MLIR REQUIRED CONFIG)
find_package(LLD REQUIRED CONFIG)
message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
message(STATUS "Using MLIRConfig.cmake in: ${MLIR_DIR}")
message(STATUS "Using LLDConfig.cmake in: ${LLD_DIR}")
set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/bin)
set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/lib)
@ -180,15 +182,15 @@ if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
list(APPEND CMAKE_MODULE_PATH "${MLIR_CMAKE_DIR}")
list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_DIR}")
list(APPEND CMAKE_MODULE_PATH "${LLD_CMAKE_DIR}")
include(TableGen)
include(AddLLVM)
include(AddMLIR)
include(HandleLLVMOptions)
include_directories(SYSTEM ${LLVM_INCLUDE_DIRS})
separate_arguments(LLVM_DEFINITIONS_LIST NATIVE_COMMAND ${LLVM_DEFINITIONS})
add_definitions(${LLVM_DEFINITIONS_LIST})

View File

@ -517,6 +517,7 @@ This framework allows for transforming a set of illegal operations to a set of l
** Next Step
*** Compile to object files
*** Link object files to create an executable
** End of wiring for static compilers
** What is an object file?
*** Symbols
- A pair of a name and a value
@ -558,3 +559,5 @@ and tries to =resolve= *undefined* symbols
+ write the result to a file as an executable
** AOT vs JIT
** Let's look at some code
** Resources:
- [[https://lwn.net/Articles/276782/][20 part linker essay]]

View File

@ -43,7 +43,8 @@ target_link_libraries(serenec
LLVMTarget
LLVMOption
lldDriver
lldELF
clangDriver
clangBasic
clangdSupport

View File

@ -25,6 +25,8 @@
#include "serene/slir/generatable.h"
#include "serene/slir/slir.h"
#include <lld/Common/Driver.h>
#include <clang/Driver/Compilation.h>
#include <clang/Driver/Driver.h>
#include <clang/Frontend/TextDiagnosticPrinter.h>
@ -176,47 +178,82 @@ int dumpAsObject(Namespace &ns) {
dest.flush();
if (emitAction == Action::Compile) {
llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions> opts =
new clang::DiagnosticOptions;
clang::DiagnosticsEngine diags(
new clang::DiagnosticIDs, opts,
new clang::TextDiagnosticPrinter(llvm::errs(), opts.get()));
clang::driver::Driver d("clang", ctx.targetTriple, diags,
"Serene compiler");
std::vector<const char *> args = {"serenec"};
args.push_back("--eh-frame-hdr");
args.push_back("-m");
args.push_back("elf_x86_64");
args.push_back("-dynamic-linker");
args.push_back("/lib64/ld-linux-x86-64.so.2");
args.push_back(
"/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.0/../../../../lib64/crt1.o");
args.push_back(
"/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.0/../../../../lib64/crti.o");
args.push_back("/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.0/crtbegin.o");
args.push_back("-L");
args.push_back("/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.0/");
args.push_back("-L");
args.push_back("/usr/lib64/");
args.push_back(destObjFilePath.c_str());
args.push_back("-o");
args.push_back(destFile.c_str());
args.push_back("-lgcc");
args.push_back("--as-needed");
args.push_back("-lgcc_s");
args.push_back("--no-as-needed");
args.push_back("-lc");
args.push_back("-lgcc");
args.push_back("--as-needed");
args.push_back("-lgcc_s");
args.push_back("--no-as-needed");
args.push_back("/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.0/crtend.o");
args.push_back(
"/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.0/../../../../lib64/crtn.o");
d.setCheckInputsExist(true);
lld::elf::link(args, false, llvm::outs(), llvm::errs());
std::unique_ptr<clang::driver::Compilation> compilation;
compilation.reset(d.BuildCompilation(args));
// llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions> opts =
// new clang::DiagnosticOptions;
// clang::DiagnosticsEngine diags(
// new clang::DiagnosticIDs, opts,
// new clang::TextDiagnosticPrinter(llvm::errs(), opts.get()));
if (!compilation) {
llvm::errs() << "can't create the compilation!\n";
return 1;
}
// clang::driver::Driver d("clang", ctx.targetTriple, diags,
// "Serene compiler");
// std::vector<const char *> args = {"serenec"};
llvm::SmallVector<std::pair<int, const clang::driver::Command *>>
failCommand;
// args.push_back(destObjFilePath.c_str());
// args.push_back("-o");
// args.push_back(destFile.c_str());
d.ExecuteCompilation(*compilation, failCommand);
// d.setCheckInputsExist(true);
if (failCommand.empty()) {
llvm::outs() << "Done!\n";
} else {
llvm::errs() << "Linking failed!\n";
failCommand.front().second->Print(llvm::errs(), "\n", false);
}
}
// std::unique_ptr<clang::driver::Compilation> compilation;
// compilation.reset(d.BuildCompilation(args));
// if (!compilation) {
// llvm::errs() << "can't create the compilation!\n";
// return 1;
// }
// llvm::SmallVector<std::pair<int, const clang::driver::Command *>>
// failCommand;
// d.ExecuteCompilation(*compilation, failCommand);
// if (failCommand.empty()) {
// llvm::outs() << "Done!\n";
// } else {
// llvm::errs() << "Linking failed!\n";
// failCommand.front().second->Print(llvm::errs(), "\n", false);
// }
// }
return 0;
};
};
int main(int argc, char *argv[]) {
int main(int argc, char *argv[]) {
initCompiler();
registerSereneCLOptions();
@ -230,7 +267,8 @@ int main(int argc, char *argv[]) {
// TODO: handle the outputDir by not forcing it. it should be
// default to the current working dir
if (outputDir == "-") {
llvm::errs() << "Error: The build directory is not set. Did you forget to "
llvm::errs()
<< "Error: The build directory is not set. Did you forget to "
"use '-b'?\n";
return 1;
}
@ -354,4 +392,4 @@ int main(int argc, char *argv[]) {
}
return 0;
}
}