Skip to content

Commit

Permalink
fix: Fixed type comparisons across modules, fixed other asserts, cont…
Browse files Browse the repository at this point in the history
…exts reused across modules
  • Loading branch information
alinalihassan committed Aug 25, 2022
1 parent 1ff4f36 commit 953f6bc
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 37 deletions.
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
- [ ] Fix class import behavior not working properly
- [ ] Fix imported modules/files skip already imported modules (such as base.les)
- [ ] Fix unused extern functions get optimized (even without optimized) and we have to make dummy functions
- [ ] Fix type comparison across modules (string class test doesn't work because strlen is defined in base.les
- [x] Fix type comparison across modules (string class test doesn't work because strlen is defined in base.les
and class is defined in its own file, thus cast doesn't see that i64's pointer is i64 from another module)

## Refactoring
Expand Down
62 changes: 28 additions & 34 deletions src/liblesma/Backend/Codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

using namespace lesma;

Codegen::Codegen(std::unique_ptr<Parser> parser, std::shared_ptr<SourceMgr> srcMgr, const std::string &filename, std::vector<std::string> imports, bool jit, bool main, std::string alias, const std::shared_ptr<LLVMContext>& context) {
Codegen::Codegen(std::unique_ptr<Parser> parser, std::shared_ptr<SourceMgr> srcMgr, const std::string &filename, std::vector<std::string> imports, bool jit, bool main, std::string alias, ThreadSafeContext* context) {
InitializeNativeTarget();
InitializeNativeTargetAsmPrinter();
InitializeNativeTargetAsmParser();

ImportedModules = std::move(imports);
TheJIT = ExitOnErr(LesmaJIT::Create());
TheContext = context == nullptr ? std::make_shared<LLVMContext>() : context;
TheModule = std::make_unique<Module>("Lesma JIT", *TheContext);
TheContext = context == nullptr ? new ThreadSafeContext(std::make_unique<LLVMContext>()) : context;
TheModule = std::make_unique<Module>("Lesma JIT", *TheContext->getContext());
TheModule->setDataLayout(TheJIT->getDataLayout());
TheModule->setSourceFileName(filename);
Builder = std::make_unique<IRBuilder<>>(*TheContext);
Builder = std::make_unique<IRBuilder<>>(*TheContext->getContext());
Parser_ = std::move(parser);
SourceManager = std::move(srcMgr);
bufferId = SourceManager->getNumBuffers();
Expand All @@ -39,7 +39,7 @@ llvm::Function *Codegen::InitializeTopLevel() {
FunctionType *FT = FunctionType::get(Builder->getInt64Ty(), paramTypes, false);
Function *F = Function::Create(FT, isMain ? Function::ExternalLinkage : Function::InternalLinkage, "main", *TheModule);

auto entry = BasicBlock::Create(*TheContext, "entry", F);
auto entry = BasicBlock::Create(*TheContext->getContext(), "entry", F);
Builder->SetInsertPoint(entry);

return F;
Expand All @@ -49,7 +49,7 @@ void Codegen::defineFunction(Function *F, FuncDecl *node, SymbolTableEntry *clsS
Scope = Scope->createChildBlock(node->getName());
deferStack.push({});

BasicBlock *entry = BasicBlock::Create(*TheContext, "entry", F);
BasicBlock *entry = BasicBlock::Create(*TheContext->getContext(), "entry", F);
Builder->SetInsertPoint(entry);

for (auto &param: F->args()) {
Expand Down Expand Up @@ -185,7 +185,7 @@ void Codegen::CompileModule(llvm::SMRange span, const std::string &filepath, boo
if (sym.second->getType()->isOneOf({TY_ENUM, TY_CLASS}) && sym.second->isExported() && (importAll || !imp_alias.empty())) {
// TODO: We have to reconstruct classes and enums, fix me
auto struct_ty = cast<llvm::StructType>(sym.second->getLLVMType());
llvm::StructType *structType = llvm::StructType::create(*TheContext, struct_ty->elements(), sym.first);
llvm::StructType *structType = llvm::StructType::create(*TheContext->getContext(), struct_ty->elements(), sym.first);

auto *structSymbol = new SymbolTableEntry(imp_alias.empty() ? sym.first : imp_alias, sym.second->getType());
structSymbol->setLLVMType(structType);
Expand Down Expand Up @@ -345,20 +345,14 @@ void Codegen::LinkObjectFile(const std::string &obj_filename) {
}

int Codegen::JIT() {
return 0;
// auto J = ExitOnErr(llvm::orc::LLJITBuilder().create());
// ExitOnErr(J->addIRModule(ThreadSafeModule(std::move(TheModule), std::move(context)));

// Look up the JIT'd function, cast it to a function pointer, then call it.
// auto Add1Addr = ExitOnErr(J->lookup("main"));
// int (*Add1)(int) = Add1Addr.toPtr<int(int)>();

// auto jit_error = TheJIT->addModule(ThreadSafeModule(std::move(TheModule), std::move(context)));
// if (jit_error)
// throw CodegenError({}, "JIT Error:\n{}");
// using MainFnTy = int();
// auto jit_main = jitTargetAddressToFunction<MainFnTy *>(TheJIT->lookup(TopLevelFunc->getName())->getAddress());
// return jit_main();
auto jit_error = TheJIT->addModule(ThreadSafeModule(std::move(TheModule), *TheContext));
if (jit_error)
throw CodegenError({}, "JIT Error:\n{}");
using MainFnTy = int();
auto jit_main = jitTargetAddressToFunction<MainFnTy *>(TheJIT->lookup(TopLevelFunc->getName())->getAddress());
auto ret = jit_main();

return ret;
}

void Codegen::Run() {
Expand Down Expand Up @@ -511,19 +505,19 @@ void Codegen::visit(VarDecl *node) {

void Codegen::visit(If *node) {
auto parentFct = Builder->GetInsertBlock()->getParent();
auto bStart = llvm::BasicBlock::Create(*TheContext, "if.start");
auto bEnd = llvm::BasicBlock::Create(*TheContext, "if.end");
auto bStart = llvm::BasicBlock::Create(*TheContext->getContext(), "if.start");
auto bEnd = llvm::BasicBlock::Create(*TheContext->getContext(), "if.end");

Builder->CreateBr(bStart);
parentFct->getBasicBlockList().push_back(bStart);
Builder->SetInsertPoint(bStart);

for (unsigned long i = 0; i < node->getConds().size(); i++) {
auto bIfTrue = llvm::BasicBlock::Create(*TheContext, "if.true");
auto bIfTrue = llvm::BasicBlock::Create(*TheContext->getContext(), "if.true");
parentFct->getBasicBlockList().push_back(bIfTrue);
auto bIfFalse = bEnd;
if (i + 1 < node->getConds().size()) {
bIfFalse = llvm::BasicBlock::Create(*TheContext, "if.false");
bIfFalse = llvm::BasicBlock::Create(*TheContext->getContext(), "if.false");
parentFct->getBasicBlockList().push_back(bIfFalse);
}

Expand Down Expand Up @@ -561,9 +555,9 @@ void Codegen::visit(While *node) {
llvm::Function *parentFct = Builder->GetInsertBlock()->getParent();

// Create blocks
llvm::BasicBlock *bCond = llvm::BasicBlock::Create(*TheContext, "while.cond");
llvm::BasicBlock *bLoop = llvm::BasicBlock::Create(*TheContext, "while");
llvm::BasicBlock *bEnd = llvm::BasicBlock::Create(*TheContext, "while.end");
llvm::BasicBlock *bCond = llvm::BasicBlock::Create(*TheContext->getContext(), "while.cond");
llvm::BasicBlock *bLoop = llvm::BasicBlock::Create(*TheContext->getContext(), "while");
llvm::BasicBlock *bEnd = llvm::BasicBlock::Create(*TheContext->getContext(), "while.end");

breakBlocks.push(bEnd);
continueBlocks.push(bCond);
Expand Down Expand Up @@ -805,7 +799,7 @@ void Codegen::visit(Class *node) {
elementTypes.push_back(typ);
}

llvm::StructType *structType = llvm::StructType::create(*TheContext, elementTypes, node->getIdentifier());
llvm::StructType *structType = llvm::StructType::create(*TheContext->getContext(), elementTypes, node->getIdentifier());

std::vector<std::tuple<std::string, SymbolType *>> fields;

Expand Down Expand Up @@ -835,7 +829,7 @@ void Codegen::visit(Class *node) {

void Codegen::visit(Enum *node) {
std::vector<llvm::Type *> elementTypes = {Builder->getInt8Ty()};
llvm::StructType *structType = llvm::StructType::create(*TheContext, elementTypes, node->getIdentifier());
llvm::StructType *structType = llvm::StructType::create(*TheContext->getContext(), elementTypes, node->getIdentifier());
std::vector<std::tuple<std::string, SymbolType *>> fields;

for (const auto &field: node->getValues())
Expand Down Expand Up @@ -930,7 +924,7 @@ llvm::Value *Codegen::visit(BinaryOp *node) {
auto sym = Scope->lookup(left->getType()->getPointerElementType()->getStructName().str());
if (sym != nullptr && sym->getType()->is(TY_ENUM)) {
if (left->getType()->getPointerElementType() != right->getType()->getPointerElementType())
return ConstantInt::getBool(*TheContext, false);
return ConstantInt::getBool(*TheContext->getContext(), false);
else {
auto left_gep = Builder->CreateStructGEP(sym->getLLVMType(), left, 0);
auto left_load = Builder->CreateLoad(Builder->getInt8Ty(), left_gep);
Expand Down Expand Up @@ -965,7 +959,7 @@ llvm::Value *Codegen::visit(BinaryOp *node) {
right = Cast(node->getSpan(), right, finalType);

if (finalType == nullptr && (left->getType()->isPointerTy() && left->getType()->getPointerElementType()->isStructTy()) && (right->getType()->isPointerTy() && right->getType()->getPointerElementType()->isStructTy()))
return ConstantInt::getBool(*TheContext, true);
return ConstantInt::getBool(*TheContext->getContext(), true);
else if (left->getType()->isPointerTy() && right->getType()->isPointerTy())
return Builder->CreateICmpNE(left, right);
else if (finalType == nullptr)
Expand Down Expand Up @@ -1186,7 +1180,7 @@ llvm::Value *Codegen::visit(UnaryOp *node) {

llvm::Value *Codegen::visit(Literal *node) {
if (node->getType() == TokenType::DOUBLE)
return ConstantFP::get(*TheContext, APFloat(std::stod(node->getValue())));
return ConstantFP::get(*TheContext->getContext(), APFloat(std::stod(node->getValue())));
else if (node->getType() == TokenType::INTEGER)
return ConstantInt::getSigned(Builder->getInt64Ty(), std::stoi(node->getValue()));
else if (node->getType() == TokenType::BOOL)
Expand All @@ -1212,7 +1206,7 @@ llvm::Value *Codegen::visit(Literal *node) {
}

llvm::Value *Codegen::visit(Else * /*node*/) {
return llvm::ConstantInt::getTrue(*TheContext);
return llvm::ConstantInt::getTrue(*TheContext->getContext());
}

std::string Codegen::getTypeMangledName(llvm::SMRange span, llvm::Type *type) {
Expand Down
4 changes: 2 additions & 2 deletions src/liblesma/Backend/Codegen.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace lesma {
};

class Codegen final : public ExprVisitor<llvm::Value *, llvm::Type *>, public StmtVisitor<void> {
std::shared_ptr<LLVMContext> TheContext;
ThreadSafeContext *TheContext;
std::unique_ptr<Module> TheModule;
std::unique_ptr<IRBuilder<>> Builder;
ExitOnError ExitOnErr;
Expand Down Expand Up @@ -64,7 +64,7 @@ namespace lesma {
bool isMain = true;

public:
Codegen(std::unique_ptr<Parser> parser, std::shared_ptr<SourceMgr> srcMgr, const std::string &filename, std::vector<std::string> imports, bool jit, bool main, std::string alias = "", const std::shared_ptr<LLVMContext>& context = nullptr);
Codegen(std::unique_ptr<Parser> parser, std::shared_ptr<SourceMgr> srcMgr, const std::string &filename, std::vector<std::string> imports, bool jit, bool main, std::string alias = "", ThreadSafeContext* context = nullptr);

void Dump();
void Run();
Expand Down

0 comments on commit 953f6bc

Please sign in to comment.