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-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
|
|
|
|
2020-07-23 21:47:13 +01:00
|
|
|
#include <iostream>
|
2021-03-21 16:25:12 +00:00
|
|
|
#include <llvm/Support/CommandLine.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,
|
|
|
|
DumpIR,
|
|
|
|
DumpSLIR,
|
|
|
|
DumpMLIR,
|
|
|
|
DumpSemantic,
|
|
|
|
DumpLIR
|
|
|
|
};
|
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-04-17 00:18:13 +01:00
|
|
|
static cl::opt<enum Action> emitAction(
|
|
|
|
"emit", cl::desc("Select what to dump."),
|
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-04-17 00:18:13 +01:00
|
|
|
cl::values(clEnumValN(DumpAST, "ast", "Output the AST only"))
|
|
|
|
|
|
|
|
);
|
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-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-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);
|
|
|
|
switch (emitAction) {
|
2021-04-17 00:18:13 +01:00
|
|
|
|
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-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-06-21 11:03:18 +01:00
|
|
|
if (mlir::failed(serene::slir::generate<Namespace>(*ns))) {
|
|
|
|
llvm::errs() << "IR generation faild\n";
|
|
|
|
return 1;
|
|
|
|
}
|
2021-06-16 20:19:42 +01:00
|
|
|
serene::slir::dump<Namespace>(*ns);
|
|
|
|
} else {
|
|
|
|
llvm::outs() << "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
|
|
|
}
|