2021-06-11 22:06:44 +01:00
|
|
|
/* -*- C++ -*-
|
2021-10-12 20:51:03 +01:00
|
|
|
* Serene Programming Language
|
2020-07-16 15:48:06 +01:00
|
|
|
*
|
2022-01-27 11:44:44 +00:00
|
|
|
* Copyright (c) 2019-2022 Sameer Rahmani <lxsameer@gnu.org>
|
2020-07-16 15:48:06 +01:00
|
|
|
*
|
2021-10-12 20:51:03 +01:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, version 2.
|
2020-07-16 15:48:06 +01:00
|
|
|
*
|
2021-10-12 20:51:03 +01:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2020-07-16 15:48:06 +01:00
|
|
|
*
|
2021-10-12 20:51:03 +01:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2020-07-16 15:48:06 +01:00
|
|
|
*/
|
|
|
|
|
2022-06-11 18:11:00 +01:00
|
|
|
// #include "serene/jit/halley.h"
|
|
|
|
// #include "serene/namespace.h"
|
|
|
|
// #include "serene/reader/location.h"
|
|
|
|
// #include "serene/reader/reader.h"
|
|
|
|
// #include "serene/semantics.h"
|
|
|
|
// #include "serene/serene.h"
|
|
|
|
// #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>
|
|
|
|
// #include <clang/Tooling/Tooling.h>
|
|
|
|
// #include <llvm/ADT/ArrayRef.h>
|
|
|
|
// #include <llvm/ADT/SmallString.h>
|
|
|
|
// #include <llvm/ADT/StringRef.h>
|
|
|
|
// #include <llvm/IR/LegacyPassManager.h>
|
|
|
|
// //#include <llvm/MC/TargetRegistry.h>
|
|
|
|
// #include <llvm/Support/CommandLine.h>
|
|
|
|
// #include <llvm/Support/FileSystem.h>
|
|
|
|
// #include <llvm/Support/FormatVariadic.h>
|
|
|
|
// #include <llvm/Support/Host.h>
|
|
|
|
// #include <llvm/Support/Path.h>
|
|
|
|
// #include <llvm/Support/raw_ostream.h>
|
|
|
|
// #include <llvm/Target/TargetMachine.h>
|
|
|
|
// #include <llvm/Target/TargetOptions.h>
|
|
|
|
|
|
|
|
// #include <memory>
|
|
|
|
|
|
|
|
// using namespace std;
|
|
|
|
// using namespace serene;
|
|
|
|
|
|
|
|
// namespace cl = llvm::cl;
|
|
|
|
|
|
|
|
// namespace {
|
|
|
|
// enum Action {
|
|
|
|
// None,
|
|
|
|
// DumpAST,
|
|
|
|
// DumpSLIR,
|
|
|
|
// DumpMLIR,
|
|
|
|
// DumpSemantic,
|
|
|
|
// DumpLIR,
|
|
|
|
// DumpIR,
|
|
|
|
// CompileToObject,
|
|
|
|
// Compile,
|
|
|
|
// // TODO: Remove this option and replace it by a subcommand
|
|
|
|
// RunJIT,
|
|
|
|
// };
|
|
|
|
// } // namespace
|
|
|
|
|
|
|
|
// static std::string banner =
|
|
|
|
// llvm::formatv("\n\nSerene Compiler Version {0}"
|
|
|
|
// "\nCopyright (C) 2019-2022 "
|
|
|
|
// "Sameer Rahmani <lxsameer@gnu.org>\n"
|
|
|
|
// "Serene comes with ABSOLUTELY NO WARRANTY;\n"
|
|
|
|
// "This is free software, and you are welcome\n"
|
|
|
|
// "to redistribute it under certain conditions; \n"
|
|
|
|
// "for details take a look at the LICENSE file.\n",
|
|
|
|
// SERENE_VERSION);
|
|
|
|
|
|
|
|
// static cl::opt<std::string> inputNS(cl::Positional, cl::desc("<namespace>"),
|
|
|
|
// cl::Required);
|
|
|
|
|
|
|
|
// static cl::opt<std::string> outputFile(
|
|
|
|
// "o", cl::desc("The relative path to the output file from the build dir"),
|
|
|
|
// cl::init("-"), cl::value_desc("filename"));
|
|
|
|
|
|
|
|
// static cl::opt<std::string>
|
|
|
|
// outputDir("b", cl::desc("The absolute path to the build directory"),
|
|
|
|
// cl::value_desc("dir"), cl::Required);
|
|
|
|
|
|
|
|
// static cl::opt<enum Action> emitAction(
|
|
|
|
// "emit", cl::desc("Select what to dump."), cl::init(Compile),
|
|
|
|
// cl::values(clEnumValN(DumpSemantic, "semantic",
|
|
|
|
// "Output the AST after one level of analysis
|
|
|
|
// only")),
|
|
|
|
// cl::values(clEnumValN(DumpIR, "ir", "Output the lowered IR only")),
|
|
|
|
// cl::values(clEnumValN(DumpSLIR, "slir", "Output the SLIR only")),
|
|
|
|
// cl::values(clEnumValN(DumpMLIR, "mlir",
|
|
|
|
// "Output the MLIR only (Lowered SLIR)")),
|
|
|
|
// cl::values(clEnumValN(DumpLIR, "lir",
|
|
|
|
// "Output the LIR only (Lowerd to LLVM dialect)")),
|
|
|
|
// cl::values(clEnumValN(DumpAST, "ast", "Output the AST only")),
|
|
|
|
// cl::values(clEnumValN(CompileToObject, "object",
|
|
|
|
// "Compile to object file.")),
|
|
|
|
// cl::values(clEnumValN(Compile, "target",
|
|
|
|
// "Compile to target code. (Default)")),
|
|
|
|
// cl::values(clEnumValN(RunJIT, "jit",
|
|
|
|
// "Run the give input file with the JIT."))
|
|
|
|
|
|
|
|
// );
|
2021-03-21 16:25:12 +00:00
|
|
|
|
2021-11-10 19:40:51 +00:00
|
|
|
// int dumpAsObject(Namespace &ns) {
|
|
|
|
// // TODO: Move the compilation process to the Namespace class
|
|
|
|
// auto maybeModule = ns.compileToLLVM();
|
|
|
|
// // TODO: Fix this call to raise the wrapped error instead
|
|
|
|
// if (!maybeModule) {
|
|
|
|
// // TODO: Rais and error: "Faild to generato LLVM IR for namespace"
|
|
|
|
// return -1;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// auto module = std::move(maybeModule.getValue());
|
|
|
|
// auto &ctx = ns.getContext();
|
|
|
|
|
|
|
|
// // TODO: We need to set the triple data layout and everything to that sort
|
|
|
|
// in
|
|
|
|
// // one place. We want them for the JIT as well and also we're kinda
|
|
|
|
// // duplicating what we're doing in `Namespace#compileToLLVM`.
|
|
|
|
// module->setTargetTriple(ctx.targetTriple);
|
|
|
|
|
|
|
|
// std::string Error;
|
|
|
|
// const auto *target =
|
|
|
|
// llvm::TargetRegistry::lookupTarget(ctx.targetTriple, Error);
|
|
|
|
|
|
|
|
// // Print an error and exit if we couldn't find the requested target.
|
|
|
|
// // This generally occurs if we've forgotten to initialise the
|
|
|
|
// // TargetRegistry or we have a bogus target triple.
|
|
|
|
// if (target == nullptr) {
|
|
|
|
// llvm::errs() << Error;
|
|
|
|
// return 1;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// const auto *cpu = "generic";
|
|
|
|
// const auto *features = "";
|
|
|
|
|
|
|
|
// llvm::TargetOptions opt;
|
|
|
|
// auto rm = llvm::Optional<llvm::Reloc::Model>();
|
|
|
|
// auto *targetMachinePtr =
|
|
|
|
// target->createTargetMachine(ctx.targetTriple, cpu, features, opt, rm);
|
|
|
|
// auto targetMachine =
|
|
|
|
// std::unique_ptr<llvm::TargetMachine>(targetMachinePtr);
|
|
|
|
|
|
|
|
// module->setDataLayout(targetMachine->createDataLayout());
|
|
|
|
|
|
|
|
// const auto *filename =
|
|
|
|
// strcmp(outputFile.c_str(), "-") == 0 ? "output" : outputFile.c_str();
|
|
|
|
|
|
|
|
// std::error_code ec;
|
|
|
|
// const auto pathSize(256);
|
|
|
|
|
|
|
|
// llvm::SmallString<pathSize> destFile(outputDir);
|
|
|
|
// llvm::sys::path::append(destFile, filename);
|
|
|
|
// auto destObjFilePath = llvm::formatv("{0}.o", destFile).str();
|
|
|
|
// llvm::raw_fd_ostream dest(destObjFilePath, ec, llvm::sys::fs::OF_None);
|
|
|
|
|
|
|
|
// if (ec) {
|
|
|
|
// llvm::errs() << "Could not open file: " << destObjFilePath;
|
|
|
|
// llvm::errs() << "Could not open file: " << ec.message();
|
|
|
|
// return 1;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// llvm::legacy::PassManager pass;
|
|
|
|
// auto fileType = llvm::CGFT_ObjectFile;
|
|
|
|
|
|
|
|
// if (targetMachine->addPassesToEmitFile(pass, dest, nullptr, fileType)) {
|
|
|
|
// llvm::errs() << "TheTargetMachine can't emit a file of this type";
|
|
|
|
// return 1;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// pass.run(*module);
|
|
|
|
// dest.flush();
|
|
|
|
|
|
|
|
// if (emitAction == Action::Compile) {
|
|
|
|
// 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");
|
|
|
|
|
|
|
|
// lld::elf::link(args, false, llvm::outs(), llvm::errs());
|
|
|
|
|
|
|
|
// // 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(destObjFilePath.c_str());
|
|
|
|
// // args.push_back("-o");
|
|
|
|
// // args.push_back(destFile.c_str());
|
|
|
|
|
|
|
|
// // d.setCheckInputsExist(true);
|
|
|
|
|
|
|
|
// // 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;
|
|
|
|
// };
|
2021-06-28 01:12:51 +01:00
|
|
|
|
2022-06-11 18:11:00 +01:00
|
|
|
int main() {
|
|
|
|
// initCompiler();
|
|
|
|
// registerSereneCLOptions();
|
|
|
|
|
|
|
|
// cl::ParseCommandLineOptions(argc, argv, banner);
|
|
|
|
|
|
|
|
// auto ctx = makeSereneContext();
|
|
|
|
|
|
|
|
// applySereneCLOptions(*ctx);
|
|
|
|
|
|
|
|
// // 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
|
|
|
|
// "
|
|
|
|
// "use '-b'?\n";
|
|
|
|
// return 1;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// switch (emitAction) {
|
2021-09-25 16:18:33 +01:00
|
|
|
|
2021-11-10 19:40:51 +00:00
|
|
|
// case Action::RunJIT: {
|
2022-06-11 18:11:00 +01:00
|
|
|
// // TODO: Replace it by a proper jit configuration
|
|
|
|
// ctx->setOperationPhase(CompilationPhase::NoOptimization);
|
|
|
|
// break;
|
|
|
|
// };
|
|
|
|
|
|
|
|
// // Just print out the raw AST
|
|
|
|
// case Action::DumpAST: {
|
|
|
|
// ctx->setOperationPhase(CompilationPhase::Parse);
|
|
|
|
// break;
|
|
|
|
// };
|
|
|
|
|
|
|
|
// case Action::DumpSemantic: {
|
|
|
|
// ctx->setOperationPhase(CompilationPhase::Analysis);
|
|
|
|
// break;
|
|
|
|
// };
|
|
|
|
|
|
|
|
// case Action::DumpSLIR: {
|
|
|
|
// ctx->setOperationPhase(CompilationPhase::SLIR);
|
|
|
|
// break;
|
|
|
|
// }
|
2021-11-10 19:40:51 +00:00
|
|
|
|
2022-06-11 18:11:00 +01:00
|
|
|
// case Action::DumpMLIR: {
|
|
|
|
// ctx->setOperationPhase(CompilationPhase::MLIR);
|
|
|
|
// break;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// case Action::DumpLIR: {
|
|
|
|
// ctx->setOperationPhase(CompilationPhase::LIR);
|
|
|
|
// break;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// case Action::DumpIR: {
|
|
|
|
// ctx->setOperationPhase(CompilationPhase::IR);
|
|
|
|
// break;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// case Action::CompileToObject: {
|
|
|
|
// ctx->setOperationPhase(CompilationPhase::NoOptimization);
|
|
|
|
// break;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// case Action::Compile: {
|
|
|
|
// ctx->setOperationPhase(CompilationPhase::NoOptimization);
|
|
|
|
// break;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// default: {
|
|
|
|
// llvm::errs() << "No action specified. TODO: Print out help here\n";
|
|
|
|
// return 1;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
// auto runLoc = reader::LocationRange::UnknownLocation(inputNS);
|
|
|
|
// auto maybeNS = ctx->importNamespace(inputNS, runLoc);
|
|
|
|
|
|
|
|
// if (!maybeNS) {
|
|
|
|
// auto err = maybeNS.takeError();
|
|
|
|
// throwErrors(*ctx, err);
|
|
|
|
// return (int)std::errc::no_such_file_or_directory;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// auto ns = *maybeNS;
|
|
|
|
|
|
|
|
// switch (emitAction) {
|
|
|
|
// case Action::DumpAST:
|
|
|
|
// case Action::DumpSemantic: {
|
|
|
|
// auto ast = ns->getTree();
|
|
|
|
// llvm::outs() << exprs::astToString(&ast) << "\n";
|
|
|
|
// return 0;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// case Action::DumpSLIR:
|
|
|
|
// case Action::DumpMLIR:
|
|
|
|
// case Action::DumpLIR: {
|
|
|
|
// ns->dump();
|
|
|
|
// break;
|
|
|
|
// };
|
|
|
|
// case Action::DumpIR: {
|
|
|
|
// auto maybeModule = ns->compileToLLVM();
|
|
|
|
|
|
|
|
// if (!maybeModule) {
|
|
|
|
// llvm::errs() << "Failed to generate the IR.\n";
|
2021-11-10 19:40:51 +00:00
|
|
|
// return 1;
|
|
|
|
// }
|
2022-06-11 18:11:00 +01:00
|
|
|
|
|
|
|
// auto tsm = std::move(*maybeModule);
|
|
|
|
// tsm.withModuleDo([](auto &m) { m.dump(); });
|
|
|
|
|
2021-11-10 19:40:51 +00:00
|
|
|
// break;
|
|
|
|
// };
|
|
|
|
|
2022-06-11 18:11:00 +01:00
|
|
|
// // case Action::RunJIT: {
|
|
|
|
// // auto maybeJIT = JIT::make(*ns);
|
|
|
|
// // if (!maybeJIT) {
|
|
|
|
// // // TODO: panic in here: "Couldn't creat the JIT!"
|
|
|
|
// // return -1;
|
|
|
|
// // }
|
|
|
|
// // auto jit = std::move(maybeJIT.getValue());
|
|
|
|
|
|
|
|
// // if (jit->invoke("main")) {
|
|
|
|
// // llvm::errs() << "Faild to invoke the 'main' function.\n";
|
|
|
|
// // return 1;
|
|
|
|
// // }
|
|
|
|
// // llvm::outs() << "Done!";
|
|
|
|
// // break;
|
|
|
|
// // };
|
|
|
|
|
|
|
|
// // case Action::Compile:
|
|
|
|
// // case Action::CompileToObject: {
|
|
|
|
// // return dumpAsObject(*ns);
|
|
|
|
// // };
|
|
|
|
// default: {
|
|
|
|
// llvm::errs() << "Action is not supported yet!\n";
|
2021-11-10 19:40:51 +00:00
|
|
|
// };
|
2022-06-11 18:11:00 +01:00
|
|
|
// }
|
2020-07-16 15:48:06 +01:00
|
|
|
|
2021-06-16 20:19:42 +01:00
|
|
|
return 0;
|
2020-07-10 18:06:26 +01:00
|
|
|
}
|