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-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 <memory>
|
|
|
|
#include <mlir/IR/Builders.h>
|
|
|
|
#include <mlir/IR/BuiltinOps.h>
|
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-06-15 01:21:48 +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-04-28 21:03:22 +01:00
|
|
|
exprs::Ast &Namespace::getTree() { return this->tree; }
|
2020-08-03 12:39:20 +01:00
|
|
|
|
2021-04-25 23:07:08 +01:00
|
|
|
mlir::LogicalResult Namespace::setTree(exprs::Ast &t) {
|
2021-03-28 10:53:32 +01:00
|
|
|
if (initialized) {
|
|
|
|
return mlir::failure();
|
2020-08-01 21:32:21 +01:00
|
|
|
}
|
2021-06-13 22:58:46 +01:00
|
|
|
this->tree = std::move(t);
|
2021-03-28 10:53:32 +01:00
|
|
|
this->initialized = true;
|
|
|
|
return mlir::success();
|
|
|
|
}
|
|
|
|
|
2021-04-27 23:43:18 +01:00
|
|
|
std::shared_ptr<Namespace>
|
|
|
|
makeNamespace(SereneContext &ctx, llvm::StringRef name,
|
2021-05-08 19:00:24 +01:00
|
|
|
llvm::Optional<llvm::StringRef> filename, bool setCurrent) {
|
2021-06-13 22:40:58 +01:00
|
|
|
auto nsPtr = std::make_shared<Namespace>(ctx, name, filename);
|
2021-04-27 23:43:18 +01:00
|
|
|
ctx.insertNS(nsPtr);
|
2021-05-08 19:00:24 +01:00
|
|
|
if (setCurrent) {
|
2021-06-13 22:40:58 +01:00
|
|
|
if (!ctx.setCurrentNS(nsPtr->name)) {
|
|
|
|
throw std::runtime_error("Couldn't set the current NS");
|
|
|
|
}
|
2021-05-08 19:00:24 +01:00
|
|
|
}
|
2021-04-27 23:43:18 +01:00
|
|
|
return nsPtr;
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
// TODO: We need to call `erase` method of module somewhere to clean it up
|
|
|
|
// maybe use a unique ptr?
|
|
|
|
auto module = mlir::ModuleOp::create(builder.getUnknownLoc(), name);
|
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-08-15 12:06:56 +01:00
|
|
|
if (mlir::failed(runPasses(module))) {
|
2021-06-16 20:19:42 +01:00
|
|
|
// TODO: throw a proper errer
|
|
|
|
module.emitError("Failure in passes!");
|
2021-08-15 12:06:56 +01:00
|
|
|
return MaybeModuleOp::error(true);
|
2021-06-16 20:19:42 +01:00
|
|
|
}
|
|
|
|
|
2021-08-15 12:06:56 +01:00
|
|
|
return MaybeModuleOp::success(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) {
|
|
|
|
llvm::outs() << "Failed to generate the IR.\n";
|
|
|
|
return;
|
2021-06-21 11:03:18 +01:00
|
|
|
}
|
2021-08-15 12:06:56 +01:00
|
|
|
|
|
|
|
maybeModuleOp.getValue().dump();
|
2021-06-13 22:40:58 +01:00
|
|
|
};
|
|
|
|
|
2021-08-15 12:06:56 +01:00
|
|
|
MaybeModule Namespace::compileToLLVM() {
|
|
|
|
auto m = generate();
|
|
|
|
|
|
|
|
if (!m) {
|
|
|
|
NAMESPACE_LOG("IR generation failed for '" << name << "'");
|
|
|
|
return MaybeModule::error(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx.getTargetPhase() >= CompilationPhase::IR) {
|
|
|
|
return MaybeModule::success(
|
|
|
|
::serene::slir::compileToLLVMIR(ctx, m.getValue()));
|
|
|
|
}
|
|
|
|
|
|
|
|
return MaybeModule::error(true);
|
|
|
|
};
|
|
|
|
|
|
|
|
Namespace::~Namespace(){};
|
|
|
|
|
2020-07-25 15:49:19 +01:00
|
|
|
} // namespace serene
|