2021-08-15 12:06:56 +01:00
|
|
|
/*
|
2020-07-25 15:49:19 +01:00
|
|
|
* Serene programming language.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2020 Sameer Rahmani <lxsameer@gnu.org>
|
|
|
|
*
|
|
|
|
* 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:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* 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-11 00:02:56 +01:00
|
|
|
#include "serene/namespace.h"
|
2021-06-13 22:58:46 +01:00
|
|
|
|
2021-06-13 22:40:58 +01:00
|
|
|
#include "serene/context.h"
|
2021-08-15 12:06:56 +01:00
|
|
|
#include "serene/errors/constants.h"
|
2021-04-11 00:02:56 +01:00
|
|
|
#include "serene/exprs/expression.h"
|
2020-07-25 15:49:19 +01:00
|
|
|
#include "serene/llvm/IR/Value.h"
|
2021-09-25 16:18:33 +01:00
|
|
|
#include "serene/reader/semantics.h"
|
2021-08-15 12:06:56 +01:00
|
|
|
#include "serene/slir/slir.h"
|
2021-06-13 22:58:46 +01:00
|
|
|
|
2021-07-10 17:52:53 +01:00
|
|
|
#include <llvm/ADT/StringRef.h>
|
|
|
|
#include <llvm/Support/FormatVariadic.h>
|
|
|
|
#include <llvm/Support/raw_ostream.h>
|
2021-08-15 12:06:56 +01:00
|
|
|
#include <mlir/IR/Builders.h>
|
|
|
|
#include <mlir/IR/BuiltinOps.h>
|
2021-09-25 16:18:33 +01:00
|
|
|
#include <mlir/IR/Verifier.h>
|
|
|
|
#include <mlir/Support/LogicalResult.h>
|
2021-10-24 23:34:28 +01:00
|
|
|
|
|
|
|
#include <memory>
|
2021-06-13 22:40:58 +01:00
|
|
|
#include <stdexcept>
|
2020-07-25 15:49:19 +01:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace serene {
|
|
|
|
|
2021-06-13 22:40:58 +01:00
|
|
|
Namespace::Namespace(SereneContext &ctx, llvm::StringRef ns_name,
|
2021-04-27 23:43:18 +01:00
|
|
|
llvm::Optional<llvm::StringRef> filename)
|
2021-10-16 16:15:56 +01:00
|
|
|
: ctx(ctx), name(ns_name) {
|
2021-05-08 19:00:24 +01:00
|
|
|
if (filename.hasValue()) {
|
|
|
|
this->filename.emplace(filename.getValue().str());
|
|
|
|
}
|
|
|
|
};
|
2021-04-04 23:08:57 +01:00
|
|
|
|
2021-10-17 12:14:04 +01:00
|
|
|
void Namespace::enqueueError(llvm::StringRef e) const {
|
2021-09-25 16:18:33 +01:00
|
|
|
ctx.diagEngine->enqueueError(e);
|
|
|
|
}
|
|
|
|
|
2021-04-28 21:03:22 +01:00
|
|
|
exprs::Ast &Namespace::getTree() { return this->tree; }
|
2020-08-03 12:39:20 +01:00
|
|
|
|
2021-10-24 23:34:28 +01:00
|
|
|
errors::OptionalErrors Namespace::expandTree(exprs::Ast &ast) {
|
2021-09-25 16:18:33 +01:00
|
|
|
|
|
|
|
if (ctx.getTargetPhase() == CompilationPhase::Parse) {
|
|
|
|
// we just want the raw AST
|
|
|
|
this->tree.insert(this->tree.end(), ast.begin(), ast.end());
|
2021-10-24 23:34:28 +01:00
|
|
|
return llvm::None;
|
2021-09-25 16:18:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run the semantic analyer on the ast and then if everything
|
|
|
|
// is ok expand the currnt tree by the semantically correct ast.
|
|
|
|
auto maybeAst = reader::analyze(ctx, ast);
|
|
|
|
|
|
|
|
if (!maybeAst) {
|
2021-10-24 23:34:28 +01:00
|
|
|
return maybeAst.getError();
|
2020-08-01 21:32:21 +01:00
|
|
|
}
|
2021-09-19 10:27:13 +01:00
|
|
|
|
2021-09-25 16:18:33 +01:00
|
|
|
auto semanticAst = std::move(maybeAst.getValue());
|
|
|
|
this->tree.insert(this->tree.end(), semanticAst.begin(), semanticAst.end());
|
2021-10-24 23:34:28 +01:00
|
|
|
return llvm::None;
|
2021-03-28 10:53:32 +01:00
|
|
|
}
|
|
|
|
|
2021-06-01 20:47:46 +01:00
|
|
|
uint Namespace::nextFnCounter() { return fn_counter++; };
|
2021-06-13 22:40:58 +01:00
|
|
|
|
|
|
|
SereneContext &Namespace::getContext() { return this->ctx; };
|
2021-06-21 11:03:18 +01:00
|
|
|
|
2021-08-15 12:06:56 +01:00
|
|
|
MaybeModuleOp Namespace::generate() {
|
|
|
|
mlir::OpBuilder builder(&ctx.mlirContext);
|
|
|
|
// TODO: Fix the unknown location by pointing to the `ns` form
|
2021-09-25 16:18:33 +01:00
|
|
|
auto module = mlir::ModuleOp::create(builder.getUnknownLoc(),
|
|
|
|
llvm::Optional<llvm::StringRef>(name));
|
2021-06-13 22:40:58 +01:00
|
|
|
|
2021-09-17 13:49:55 +01:00
|
|
|
// Walk the AST and call the `generateIR` function of each node.
|
|
|
|
// Since nodes will have access to the a reference of the
|
|
|
|
// namespace they can use the builder and keep adding more
|
|
|
|
// operations to the module via the builder
|
2021-06-13 22:40:58 +01:00
|
|
|
for (auto &x : getTree()) {
|
2021-08-15 12:06:56 +01:00
|
|
|
x->generateIR(*this, module);
|
2021-06-13 22:40:58 +01:00
|
|
|
}
|
|
|
|
|
2021-09-25 16:18:33 +01:00
|
|
|
if (mlir::failed(mlir::verify(module))) {
|
|
|
|
module.emitError("Can't verify the module");
|
|
|
|
module.erase();
|
|
|
|
return llvm::None;
|
|
|
|
}
|
|
|
|
|
2021-08-15 12:06:56 +01:00
|
|
|
if (mlir::failed(runPasses(module))) {
|
2021-09-17 13:49:55 +01:00
|
|
|
// TODO: Report a proper error
|
2021-06-16 20:19:42 +01:00
|
|
|
module.emitError("Failure in passes!");
|
2021-10-06 21:07:56 +01:00
|
|
|
module.erase();
|
2021-09-19 10:27:13 +01:00
|
|
|
return llvm::None;
|
2021-06-16 20:19:42 +01:00
|
|
|
}
|
|
|
|
|
2021-09-19 10:27:13 +01:00
|
|
|
return MaybeModuleOp(module);
|
2021-06-13 22:40:58 +01:00
|
|
|
}
|
|
|
|
|
2021-08-15 12:06:56 +01:00
|
|
|
mlir::LogicalResult Namespace::runPasses(mlir::ModuleOp &m) {
|
|
|
|
return ctx.pm.run(m);
|
|
|
|
};
|
2021-06-13 22:40:58 +01:00
|
|
|
|
2021-06-16 20:19:42 +01:00
|
|
|
void Namespace::dump() {
|
2021-06-21 11:03:18 +01:00
|
|
|
llvm::outs() << "\nMLIR: \n";
|
2021-08-15 12:06:56 +01:00
|
|
|
auto maybeModuleOp = generate();
|
|
|
|
|
|
|
|
if (!maybeModuleOp) {
|
2021-09-17 13:49:55 +01:00
|
|
|
llvm::errs() << "Failed to generate the IR.\n";
|
2021-08-15 12:06:56 +01:00
|
|
|
return;
|
2021-06-21 11:03:18 +01:00
|
|
|
}
|
2021-08-15 12:06:56 +01:00
|
|
|
|
2021-09-25 16:18:33 +01:00
|
|
|
mlir::OpPrintingFlags flags;
|
|
|
|
flags.enableDebugInfo();
|
|
|
|
|
|
|
|
maybeModuleOp.getValue()->print(llvm::outs(), flags);
|
2021-06-13 22:40:58 +01:00
|
|
|
};
|
|
|
|
|
2021-08-15 12:06:56 +01:00
|
|
|
MaybeModule Namespace::compileToLLVM() {
|
2021-08-15 13:42:22 +01:00
|
|
|
auto maybeModule = generate();
|
2021-08-15 12:06:56 +01:00
|
|
|
|
2021-08-15 13:42:22 +01:00
|
|
|
if (!maybeModule) {
|
2021-08-15 12:06:56 +01:00
|
|
|
NAMESPACE_LOG("IR generation failed for '" << name << "'");
|
2021-09-19 10:27:13 +01:00
|
|
|
return llvm::None;
|
2021-08-15 12:06:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx.getTargetPhase() >= CompilationPhase::IR) {
|
2021-08-15 13:42:22 +01:00
|
|
|
mlir::ModuleOp module = maybeModule.getValue().get();
|
2021-11-01 15:09:11 +00:00
|
|
|
return ::serene::slir::compileToLLVMIR(ctx, module);
|
2021-08-15 12:06:56 +01:00
|
|
|
}
|
|
|
|
|
2021-09-19 10:27:13 +01:00
|
|
|
return llvm::None;
|
2021-08-15 12:06:56 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
Namespace::~Namespace(){};
|
|
|
|
|
2021-10-24 23:34:28 +01:00
|
|
|
NSPtr makeNamespace(SereneContext &ctx, llvm::StringRef name,
|
|
|
|
llvm::Optional<llvm::StringRef> filename, bool setCurrent) {
|
2021-08-22 11:09:30 +01:00
|
|
|
auto nsPtr = std::make_shared<Namespace>(ctx, name, filename);
|
2021-10-24 23:34:28 +01:00
|
|
|
|
2021-08-22 11:09:30 +01:00
|
|
|
ctx.insertNS(nsPtr);
|
2021-10-24 23:34:28 +01:00
|
|
|
|
2021-08-22 11:09:30 +01:00
|
|
|
if (setCurrent) {
|
|
|
|
if (!ctx.setCurrentNS(nsPtr->name)) {
|
|
|
|
throw std::runtime_error("Couldn't set the current NS");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nsPtr;
|
|
|
|
};
|
2020-07-25 15:49:19 +01:00
|
|
|
} // namespace serene
|