Add a type converter to convert serene types to LLVM dialect types
This commit is contained in:
parent
bb49b7755d
commit
b6200a869b
|
@ -0,0 +1,39 @@
|
|||
/* -*- C++ -*-
|
||||
* Serene Programming Language
|
||||
*
|
||||
* Copyright (c) 2019-2022 Sameer Rahmani <lxsameer@gnu.org>
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef SERENE_CONVENTIONS_H
|
||||
#define SERENE_CONVENTIONS_H
|
||||
|
||||
#include "serene/config.h"
|
||||
|
||||
#include <llvm/ADT/StringRef.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace serene {
|
||||
static std::string mangleInternalStringName(llvm::StringRef str) {
|
||||
return "__serene__internal__str__" + str.str();
|
||||
}
|
||||
|
||||
static std::string mangleInternalSymName(llvm::StringRef str) {
|
||||
return "__serene__symbol__" + str.str();
|
||||
}
|
||||
|
||||
} // namespace serene
|
||||
|
||||
#endif
|
|
@ -0,0 +1,79 @@
|
|||
/* -*- C++ -*-
|
||||
* Serene Programming Language
|
||||
*
|
||||
* Copyright (c) 2019-2022 Sameer Rahmani <lxsameer@gnu.org>
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef SERENE_SLIR_TYPE_CONVERTER_H
|
||||
#define SERENE_SLIR_TYPE_CONVERTER_H
|
||||
|
||||
#include "serene/config.h"
|
||||
#include "serene/context.h"
|
||||
#include "serene/export.h"
|
||||
#include "serene/slir/dialect.h"
|
||||
#include "serene/utils.h"
|
||||
|
||||
#include <mlir/Dialect/LLVMIR/LLVMTypes.h>
|
||||
#include <mlir/IR/BuiltinTypes.h>
|
||||
#include <mlir/Transforms/DialectConversion.h>
|
||||
|
||||
namespace serene::slir {
|
||||
class SymbolType;
|
||||
|
||||
class TypeConverter : public mlir::TypeConverter {
|
||||
mlir::MLIRContext &ctx;
|
||||
|
||||
using mlir::TypeConverter::convertType;
|
||||
|
||||
using MaybeType = llvm::Optional<mlir::Type>;
|
||||
using MaybeValue = llvm::Optional<mlir::Value>;
|
||||
using ConverterFn = std::function<MaybeType(mlir::Type)>;
|
||||
|
||||
std::function<MaybeValue(mlir::OpBuilder &, SymbolType, mlir::ValueRange,
|
||||
mlir::Location)>
|
||||
|
||||
materializeSymbolFn() {
|
||||
return [&](mlir::OpBuilder &builder, SymbolType type,
|
||||
mlir::ValueRange values, mlir::Location loc) -> MaybeValue {
|
||||
auto targetType = convertType(type);
|
||||
auto ret = builder.create<ConvertOp>(loc, targetType, values[0]);
|
||||
llvm::outs() << "RR: " << ret << "\n";
|
||||
return ret.getResult();
|
||||
};
|
||||
};
|
||||
|
||||
/// This method will be called via `convertType` to convert Serene types
|
||||
/// to llvm dialect types
|
||||
ConverterFn convertSereneTypes();
|
||||
|
||||
public:
|
||||
TypeConverter(mlir::MLIRContext &ctx) : ctx(ctx) {
|
||||
addConversion([](mlir::Type type) { return type; });
|
||||
addConversion(convertSereneTypes());
|
||||
addArgumentMaterialization(materializeSymbolFn());
|
||||
addTargetMaterialization(materializeSymbolFn());
|
||||
}
|
||||
};
|
||||
|
||||
/// Returns the conversion result of converting Serene String type
|
||||
/// to LLVM dialect
|
||||
mlir::Type getStringTypeinLLVM(mlir::MLIRContext &ctx);
|
||||
|
||||
/// Returns the conversoin result of converting Serene Symbol type
|
||||
/// to LLVM dialect
|
||||
mlir::Type getSymbolTypeinLLVM(mlir::MLIRContext &ctx);
|
||||
} // namespace serene::slir
|
||||
|
||||
#endif
|
|
@ -0,0 +1,70 @@
|
|||
/* -*- C++ -*-
|
||||
* Serene Programming Language
|
||||
*
|
||||
* Copyright (c) 2019-2022 Sameer Rahmani <lxsameer@gnu.org>
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "serene/slir/type_converter.h"
|
||||
|
||||
namespace ll = mlir::LLVM;
|
||||
|
||||
namespace serene {
|
||||
namespace slir {
|
||||
|
||||
mlir::Type getStringTypeinLLVM(mlir::MLIRContext &ctx) {
|
||||
auto stringStruct =
|
||||
ll::LLVMStructType::getIdentified(&ctx, "serene.core.string");
|
||||
|
||||
mlir::SmallVector<mlir::Type, 4> subtypes;
|
||||
|
||||
subtypes.push_back(
|
||||
ll::LLVMPointerType::get(mlir::IntegerType::get(&ctx, I8_SIZE)));
|
||||
subtypes.push_back(mlir::IntegerType::get(&ctx, I32_SIZE));
|
||||
// subtypes.push_back(mlir::IntegerType::get(&ctx, I32_SIZE));
|
||||
// subtypes.push_back(mlir::IntegerType::get(&ctx, I32_SIZE));
|
||||
|
||||
(void)stringStruct.setBody(subtypes, false);
|
||||
|
||||
return ll::LLVMPointerType::get(stringStruct);
|
||||
};
|
||||
|
||||
mlir::Type getSymbolTypeinLLVM(mlir::MLIRContext &ctx) {
|
||||
auto symbolStruct =
|
||||
ll::LLVMStructType::getIdentified(&ctx, "serene.core.symbol");
|
||||
|
||||
auto strType = getStringTypeinLLVM(ctx);
|
||||
llvm::SmallVector<mlir::Type, 2> strings{strType, strType};
|
||||
|
||||
// We discard the result becasue if the body was already set it means
|
||||
// that we're ok and the struct already exists
|
||||
(void)symbolStruct.setBody(strings, false);
|
||||
return ll::LLVMPointerType::get(symbolStruct);
|
||||
};
|
||||
|
||||
TypeConverter::ConverterFn TypeConverter::convertSereneTypes() {
|
||||
return [&](mlir::Type type) -> MaybeType {
|
||||
if (type.isa<StringType>()) {
|
||||
return getStringTypeinLLVM(ctx);
|
||||
}
|
||||
|
||||
if (type.isa<SymbolType>()) {
|
||||
return getSymbolTypeinLLVM(ctx);
|
||||
}
|
||||
|
||||
return llvm::None;
|
||||
};
|
||||
}
|
||||
} // namespace slir
|
||||
} // namespace serene
|
Loading…
Reference in New Issue