2021-06-11 22:06:44 +01:00
|
|
|
/* -*- C++ -*-
|
2020-07-16 15:48:06 +01:00
|
|
|
* Serene programming language.
|
|
|
|
*
|
2021-06-11 22:06:44 +01:00
|
|
|
* Copyright (c) 2019-2021 Sameer Rahmani <lxsameer@gnu.org>
|
2020-07-16 15:48:06 +01:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
2020-07-23 21:47:13 +01:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
2020-07-16 15:48:06 +01:00
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2021-04-05 19:02:30 +01:00
|
|
|
#include "serene/serene.h"
|
2021-06-13 22:58:46 +01:00
|
|
|
|
2021-06-09 13:29:14 +01:00
|
|
|
#include "serene/context.h"
|
2021-08-17 09:52:33 +01:00
|
|
|
#include "serene/jit.h"
|
2021-05-08 19:00:24 +01:00
|
|
|
#include "serene/namespace.h"
|
2021-04-06 18:28:50 +01:00
|
|
|
#include "serene/reader/reader.h"
|
2021-04-17 00:18:13 +01:00
|
|
|
#include "serene/reader/semantics.h"
|
2021-06-13 22:40:58 +01:00
|
|
|
#include "serene/slir/generatable.h"
|
2021-06-09 13:29:14 +01:00
|
|
|
#include "serene/slir/slir.h"
|
2021-06-13 22:58:46 +01:00
|
|
|
|
2021-07-10 17:52:53 +01:00
|
|
|
#include <clang/Driver/Compilation.h>
|
|
|
|
#include <clang/Driver/Driver.h>
|
|
|
|
#include <clang/Frontend/TextDiagnosticPrinter.h>
|
|
|
|
#include <clang/Tooling/Tooling.h>
|
2021-08-15 12:06:56 +01:00
|
|
|
#include <llvm/ADT/ArrayRef.h>
|
|
|
|
#include <llvm/ADT/SmallString.h>
|
|
|
|
#include <llvm/ADT/StringRef.h>
|
2021-06-28 01:12:51 +01:00
|
|
|
#include <llvm/IR/LegacyPassManager.h>
|
2021-03-21 16:25:12 +00:00
|
|
|
#include <llvm/Support/CommandLine.h>
|
2021-06-28 01:12:51 +01:00
|
|
|
#include <llvm/Support/FileSystem.h>
|
2021-07-10 17:52:53 +01:00
|
|
|
#include <llvm/Support/FormatVariadic.h>
|
2021-06-28 01:12:51 +01:00
|
|
|
#include <llvm/Support/Host.h>
|
2021-07-27 19:13:19 +01:00
|
|
|
#include <llvm/Support/Path.h>
|
2021-06-28 01:12:51 +01:00
|
|
|
#include <llvm/Support/TargetRegistry.h>
|
|
|
|
#include <llvm/Support/TargetSelect.h>
|
|
|
|
#include <llvm/Support/raw_ostream.h>
|
|
|
|
#include <llvm/Target/TargetMachine.h>
|
|
|
|
#include <llvm/Target/TargetOptions.h>
|
2021-06-16 20:19:42 +01:00
|
|
|
#include <memory>
|
2020-07-16 15:48:06 +01:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace serene;
|
|
|
|
|
2021-03-21 16:25:12 +00:00
|
|
|
namespace cl = llvm::cl;
|
|
|
|
|
|
|
|
namespace {
|
2021-06-17 13:19:19 +01:00
|
|
|
enum Action {
|
|
|
|
None,
|
|
|
|
DumpAST,
|
|
|
|
DumpSLIR,
|
|
|
|
DumpMLIR,
|
|
|
|
DumpSemantic,
|
2021-06-28 01:12:51 +01:00
|
|
|
DumpLIR,
|
|
|
|
DumpIR,
|
2021-07-27 19:13:19 +01:00
|
|
|
CompileToObject,
|
|
|
|
Compile,
|
2021-08-07 17:41:19 +01:00
|
|
|
// TODO: Remove this option and replace it by a subcommand
|
2021-08-17 09:52:33 +01:00
|
|
|
RunJIT,
|
2021-06-17 13:19:19 +01:00
|
|
|
};
|
2021-03-21 16:25:12 +00:00
|
|
|
}
|
|
|
|
|
2021-03-22 19:53:10 +00:00
|
|
|
static cl::opt<std::string> inputFile(cl::Positional,
|
|
|
|
cl::desc("The Serene file to compile"),
|
|
|
|
cl::init("-"),
|
|
|
|
cl::value_desc("filename"));
|
|
|
|
|
2021-07-27 19:13:19 +01:00
|
|
|
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("build-dir", cl::desc("The absolute path to the build directory"),
|
|
|
|
cl::init("-"), cl::value_desc("filename"));
|
2021-06-28 01:12:51 +01:00
|
|
|
|
2021-04-17 00:18:13 +01:00
|
|
|
static cl::opt<enum Action> emitAction(
|
2021-07-27 19:13:19 +01:00
|
|
|
"emit", cl::desc("Select what to dump."), cl::init(Compile),
|
2021-06-16 20:19:42 +01:00
|
|
|
cl::values(clEnumValN(DumpSemantic, "semantic",
|
2021-04-17 00:18:13 +01:00
|
|
|
"Output the AST after one level of analysis only")),
|
2021-06-13 22:40:58 +01:00
|
|
|
cl::values(clEnumValN(DumpIR, "ir", "Output the lowered IR only")),
|
|
|
|
cl::values(clEnumValN(DumpSLIR, "slir", "Output the SLIR only")),
|
2021-06-16 20:19:42 +01:00
|
|
|
cl::values(clEnumValN(DumpMLIR, "mlir",
|
|
|
|
"Output the MLIR only (Lowered SLIR)")),
|
2021-06-17 13:19:19 +01:00
|
|
|
cl::values(clEnumValN(DumpLIR, "lir",
|
|
|
|
"Output the LIR only (Lowerd to LLVM dialect)")),
|
2021-06-28 01:12:51 +01:00
|
|
|
cl::values(clEnumValN(DumpAST, "ast", "Output the AST only")),
|
|
|
|
cl::values(clEnumValN(CompileToObject, "object",
|
2021-07-27 19:13:19 +01:00
|
|
|
"Compile to object file.")),
|
|
|
|
cl::values(clEnumValN(Compile, "target",
|
2021-08-07 17:41:19 +01:00
|
|
|
"Compile to target code. (Default)")),
|
2021-08-17 09:52:33 +01:00
|
|
|
cl::values(clEnumValN(RunJIT, "jit",
|
|
|
|
"Run the give input file with the JIT."))
|
2021-04-17 00:18:13 +01:00
|
|
|
|
|
|
|
);
|
2021-03-21 16:25:12 +00:00
|
|
|
|
2021-06-16 20:19:42 +01:00
|
|
|
exprs::Ast readInputFile() {
|
|
|
|
auto r = make_unique<reader::FileReader>(inputFile);
|
|
|
|
auto maybeAst = r->read();
|
2021-03-21 16:25:12 +00:00
|
|
|
|
2021-06-16 20:19:42 +01:00
|
|
|
if (!maybeAst) {
|
|
|
|
throw std::move(maybeAst.getError());
|
|
|
|
}
|
|
|
|
return maybeAst.getValue();
|
|
|
|
};
|
2021-04-17 00:18:13 +01:00
|
|
|
|
2021-06-16 20:19:42 +01:00
|
|
|
exprs::Ast readAndAnalyze(SereneContext &ctx) {
|
|
|
|
auto ast = readInputFile();
|
|
|
|
auto afterAst = reader::analyze(ctx, ast);
|
2021-04-24 20:18:33 +01:00
|
|
|
|
2021-06-16 20:19:42 +01:00
|
|
|
if (!afterAst) {
|
|
|
|
throw std::move(afterAst.getError());
|
|
|
|
}
|
2021-04-17 00:18:13 +01:00
|
|
|
|
2021-06-16 20:19:42 +01:00
|
|
|
return afterAst.getValue();
|
|
|
|
};
|
2021-05-08 19:00:24 +01:00
|
|
|
|
2021-06-28 01:12:51 +01:00
|
|
|
int dumpAsObject(Namespace &ns) {
|
2021-07-27 19:13:19 +01:00
|
|
|
// TODO: Move the compilation process to the Namespace class
|
2021-08-15 12:06:56 +01:00
|
|
|
auto maybeModule = ns.compileToLLVM();
|
|
|
|
// TODO: Fix this call to raise the wrapped error instead
|
|
|
|
auto module = std::move(
|
|
|
|
maybeModule.getValueOrFail("Faild to generato LLVM IR for namespace"));
|
2021-08-15 13:42:22 +01:00
|
|
|
auto &ctx = ns.getContext();
|
2021-08-15 12:06:56 +01:00
|
|
|
|
|
|
|
// 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);
|
2021-06-28 01:12:51 +01:00
|
|
|
|
|
|
|
std::string Error;
|
2021-07-27 19:13:19 +01:00
|
|
|
auto target = llvm::TargetRegistry::lookupTarget(ctx.targetTriple, Error);
|
2021-06-28 01:12:51 +01:00
|
|
|
|
|
|
|
// 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) {
|
|
|
|
llvm::errs() << Error;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto cpu = "generic";
|
|
|
|
auto features = "";
|
|
|
|
|
|
|
|
llvm::TargetOptions opt;
|
|
|
|
auto rm = llvm::Optional<llvm::Reloc::Model>();
|
|
|
|
auto targetMachinePtr =
|
2021-07-27 19:13:19 +01:00
|
|
|
target->createTargetMachine(ctx.targetTriple, cpu, features, opt, rm);
|
2021-06-28 01:12:51 +01:00
|
|
|
auto targetMachine = std::unique_ptr<llvm::TargetMachine>(targetMachinePtr);
|
|
|
|
|
2021-08-15 12:06:56 +01:00
|
|
|
module->setDataLayout(targetMachine->createDataLayout());
|
2021-06-28 01:12:51 +01:00
|
|
|
|
|
|
|
auto filename =
|
2021-07-10 17:52:53 +01:00
|
|
|
strcmp(outputFile.c_str(), "-") == 0 ? "output" : outputFile.c_str();
|
2021-06-28 01:12:51 +01:00
|
|
|
|
|
|
|
std::error_code ec;
|
2021-07-27 19:13:19 +01:00
|
|
|
llvm::SmallString<256> 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);
|
2021-06-28 01:12:51 +01:00
|
|
|
|
|
|
|
if (ec) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-08-15 12:06:56 +01:00
|
|
|
pass.run(*module);
|
2021-06-28 01:12:51 +01:00
|
|
|
dest.flush();
|
|
|
|
|
2021-07-27 19:13:19 +01:00
|
|
|
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()));
|
2021-06-28 01:12:51 +01:00
|
|
|
|
2021-07-27 19:13:19 +01:00
|
|
|
clang::driver::Driver d("clang", ctx.targetTriple, diags,
|
|
|
|
"Serene compiler");
|
|
|
|
std::vector<const char *> args = {"serenec"};
|
2021-07-10 17:52:53 +01:00
|
|
|
|
2021-07-27 19:13:19 +01:00
|
|
|
args.push_back(destObjFilePath.c_str());
|
|
|
|
args.push_back("-o");
|
|
|
|
args.push_back(destFile.c_str());
|
2021-07-10 17:52:53 +01:00
|
|
|
|
2021-07-27 19:13:19 +01:00
|
|
|
d.setCheckInputsExist(false);
|
2021-07-12 23:39:29 +01:00
|
|
|
|
2021-07-27 19:13:19 +01:00
|
|
|
std::unique_ptr<clang::driver::Compilation> compilation;
|
|
|
|
compilation.reset(d.BuildCompilation(args));
|
2021-07-10 17:52:53 +01:00
|
|
|
|
2021-07-27 19:13:19 +01:00
|
|
|
if (!compilation) {
|
|
|
|
return 1;
|
|
|
|
}
|
2021-07-10 17:52:53 +01:00
|
|
|
|
2021-07-27 19:13:19 +01:00
|
|
|
llvm::SmallVector<std::pair<int, const clang::driver::Command *>>
|
|
|
|
failCommand;
|
|
|
|
// compilation->ExecuteJobs(compilation->getJobs(), failCommand);
|
2021-07-10 17:52:53 +01:00
|
|
|
|
2021-07-27 19:13:19 +01:00
|
|
|
d.ExecuteCompilation(*compilation, failCommand);
|
|
|
|
if (failCommand.empty()) {
|
|
|
|
llvm::outs() << "Done!\n";
|
|
|
|
} else {
|
|
|
|
llvm::errs() << "Linking failed!\n";
|
|
|
|
}
|
2021-07-10 17:52:53 +01:00
|
|
|
}
|
2021-07-27 19:13:19 +01:00
|
|
|
|
2021-06-28 01:12:51 +01:00
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
2021-06-16 20:19:42 +01:00
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
// mlir::registerAsmPrinterCLOptions();
|
|
|
|
mlir::registerMLIRContextCLOptions();
|
|
|
|
mlir::registerPassManagerCLOptions();
|
2021-04-17 00:18:13 +01:00
|
|
|
|
2021-06-28 01:12:51 +01:00
|
|
|
llvm::InitializeAllTargetInfos();
|
|
|
|
llvm::InitializeAllTargets();
|
|
|
|
llvm::InitializeAllTargetMCs();
|
|
|
|
llvm::InitializeAllAsmParsers();
|
|
|
|
llvm::InitializeAllAsmPrinters();
|
|
|
|
|
2021-06-16 20:19:42 +01:00
|
|
|
cl::ParseCommandLineOptions(argc, argv, "Serene compiler \n");
|
|
|
|
auto ctx = makeSereneContext();
|
|
|
|
auto ns = makeNamespace(*ctx, "user", llvm::None);
|
|
|
|
// TODO: We might want to find a better place for this
|
|
|
|
applyPassManagerCLOptions(ctx->pm);
|
2021-07-27 19:13:19 +01:00
|
|
|
|
|
|
|
// 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 '-build-dir'?\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-06-16 20:19:42 +01:00
|
|
|
switch (emitAction) {
|
2021-04-17 00:18:13 +01:00
|
|
|
|
2021-08-17 09:52:33 +01:00
|
|
|
case Action::RunJIT: {
|
|
|
|
// TODO: Replace it by a proper jit configuration
|
|
|
|
ctx->setOperationPhase(CompilationPhase::NoOptimization);
|
2021-08-07 17:41:19 +01:00
|
|
|
break;
|
|
|
|
};
|
|
|
|
|
2021-06-16 20:19:42 +01:00
|
|
|
// Just print out the raw AST
|
|
|
|
case Action::DumpAST: {
|
|
|
|
auto ast = readInputFile();
|
|
|
|
llvm::outs() << exprs::astToString(&ast) << "\n";
|
2021-04-17 00:18:13 +01:00
|
|
|
return 0;
|
|
|
|
};
|
2021-06-16 20:19:42 +01:00
|
|
|
|
|
|
|
case Action::DumpSemantic: {
|
|
|
|
auto ast = readAndAnalyze(*ctx);
|
|
|
|
llvm::outs() << exprs::astToString(&ast) << "\n";
|
2021-03-22 19:53:10 +00:00
|
|
|
return 0;
|
2021-06-16 20:19:42 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
case Action::DumpSLIR: {
|
|
|
|
ctx->setOperationPhase(CompilationPhase::SLIR);
|
|
|
|
break;
|
2021-03-22 19:53:10 +00:00
|
|
|
}
|
2021-06-16 20:19:42 +01:00
|
|
|
|
|
|
|
case Action::DumpMLIR: {
|
|
|
|
ctx->setOperationPhase(CompilationPhase::MLIR);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-06-17 13:19:19 +01:00
|
|
|
case Action::DumpLIR: {
|
|
|
|
ctx->setOperationPhase(CompilationPhase::LIR);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-06-21 11:03:18 +01:00
|
|
|
case Action::DumpIR: {
|
|
|
|
ctx->setOperationPhase(CompilationPhase::IR);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-06-28 01:12:51 +01:00
|
|
|
case Action::CompileToObject: {
|
|
|
|
ctx->setOperationPhase(CompilationPhase::NoOptimization);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-07-27 19:13:19 +01:00
|
|
|
case Action::Compile: {
|
|
|
|
ctx->setOperationPhase(CompilationPhase::NoOptimization);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-03-22 19:53:10 +00:00
|
|
|
default: {
|
2021-06-16 20:19:42 +01:00
|
|
|
llvm::errs() << "No action specified. TODO: Print out help here\n";
|
|
|
|
return 1;
|
|
|
|
}
|
2021-03-22 19:53:10 +00:00
|
|
|
}
|
2021-06-16 20:19:42 +01:00
|
|
|
|
|
|
|
auto afterAst = readAndAnalyze(*ctx);
|
|
|
|
auto isSet = ns->setTree(afterAst);
|
|
|
|
|
|
|
|
if (isSet.succeeded()) {
|
|
|
|
ctx->insertNS(ns);
|
2021-08-15 13:42:22 +01:00
|
|
|
switch (emitAction) {
|
|
|
|
case Action::DumpSLIR:
|
|
|
|
case Action::DumpMLIR:
|
|
|
|
case Action::DumpLIR: {
|
2021-08-15 12:06:56 +01:00
|
|
|
ns->dump();
|
2021-08-15 13:42:22 +01:00
|
|
|
break;
|
|
|
|
};
|
|
|
|
case Action::DumpIR: {
|
2021-08-15 12:06:56 +01:00
|
|
|
auto maybeModule = ns->compileToLLVM();
|
|
|
|
|
|
|
|
if (!maybeModule) {
|
|
|
|
llvm::errs() << "Failed to generate the IR.\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
maybeModule.getValue()->dump();
|
2021-08-15 13:42:22 +01:00
|
|
|
break;
|
|
|
|
};
|
2021-08-07 17:41:19 +01:00
|
|
|
|
2021-08-17 09:52:33 +01:00
|
|
|
case Action::RunJIT: {
|
|
|
|
auto maybeJIT = JIT::make(*ns.get());
|
|
|
|
auto jit = std::move(maybeJIT.getValueOrFail("Couldn't creat the JIT!"));
|
|
|
|
|
|
|
|
if (jit->invoke("main")) {
|
|
|
|
llvm::errs() << "Faild to invoke the 'main' function.\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
llvm::outs() << "Done!";
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
|
2021-08-15 13:42:22 +01:00
|
|
|
case Action::Compile:
|
|
|
|
case Action::CompileToObject: {
|
2021-06-28 01:12:51 +01:00
|
|
|
return dumpAsObject(*ns);
|
2021-08-15 13:42:22 +01:00
|
|
|
};
|
|
|
|
default: {
|
|
|
|
llvm::errs() << "Action is not supported yet!\n";
|
|
|
|
};
|
2021-06-28 01:12:51 +01:00
|
|
|
}
|
2021-06-16 20:19:42 +01:00
|
|
|
} else {
|
2021-08-15 13:42:22 +01:00
|
|
|
llvm::errs() << "Can't set the tree of the namespace!\n";
|
2021-03-21 16:25:12 +00: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
|
|
|
}
|