Skip to content

Commit

Permalink
More Polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
TheNachoBIT committed Aug 12, 2023
1 parent b8b4c4d commit ec2788c
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 15 deletions.
69 changes: 55 additions & 14 deletions Language/AST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,11 @@ bool AST::is_inside_atom = false;

std::unordered_map<std::string, AST::Type*> all_array_ptrs;

llvm::Value* GetInst(AST::Expression* v, bool enable_phi = true)
{
llvm::Value* r = v->codegen();

if (r == nullptr) CodeGen::Error("r is nullptr");

if (dynamic_cast<AST::Number*>(v) || dynamic_cast<AST::Call*>(v) || dynamic_cast<AST::Data*>(v)) return r;
std::string GetName(AST::Expression* T);

llvm::Value* GetInstNoCatch(AST::Expression* v, bool enable_phi = true)
{
AST::CurrentIdentifier = GetName(v);
if (CodeGen::NamedPures.find(AST::CurrentIdentifier) != CodeGen::NamedPures.end())
{
if (CodeGen::NamedPures[AST::CurrentIdentifier].second != nullptr)
Expand All @@ -55,9 +52,27 @@ llvm::Value* GetInst(AST::Expression* v, bool enable_phi = true)
}
}

llvm::Value* r = v->codegen();

if (r == nullptr) { return r; }

if (dynamic_cast<AST::Number*>(v) || dynamic_cast<AST::Call*>(v) || dynamic_cast<AST::Data*>(v)) return r;

return CreateAutoLoad(v, r);
}

llvm::Value* GetInst(AST::Expression* v, bool enable_phi = true)
{
auto i = GetInstNoCatch(v, enable_phi);

if(i == nullptr)
{
CodeGen::Error("i is nullptr");
}

return i;
}

llvm::Value* GetCoreInst(AST::Expression* v, std::string name)
{
if (CodeGen::NamedPures.find(name) != CodeGen::NamedPures.end())
Expand Down Expand Up @@ -429,9 +444,11 @@ llvm::Value* AST::Alloca::codegen()

llvm::Value* Alloca = nullptr;

Alloca = CodeGen::Builder->CreateAlloca(T->codegen(), 0, VarName.c_str());
auto TC = T->codegen();

Alloca = CodeGen::Builder->CreateAlloca(TC, 0, VarName.c_str());

if(dyn_cast<llvm::PointerType>(T->codegen()) && dynamic_cast<AST::Array*>(T.get()))
if(dyn_cast<llvm::PointerType>(TC) && dynamic_cast<AST::Array*>(T.get()))
{
auto A = dynamic_cast<AST::Array*>(T.get());
all_array_ptrs[VarName] = A->childType.get();
Expand All @@ -454,12 +471,12 @@ llvm::Value* AST::Store::codegen()

if(N->getType()->isIntegerTy())
{
auto C = CodeGen::Builder->CreateIntCast(GetInst(Value.get()), GetInst(Target.get())->getType(), true);
auto C = CodeGen::Builder->CreateIntCast(N, GetInst(Target.get())->getType(), true);

return CodeGen::Builder->CreateStore(C, Target->codegen());
}

return CodeGen::Builder->CreateStore(GetInst(Value.get()), Target->codegen());
return CodeGen::Builder->CreateStore(N, Target->codegen());
}

bool IsIntegerType(llvm::Value* V)
Expand Down Expand Up @@ -640,6 +657,26 @@ std::string GetName(AST::Expression* T)
AST::Variable* V = dynamic_cast<AST::Variable*>(T);
return V->Name;
}
else if (dynamic_cast<AST::Alloca*>(T))
{
AST::Alloca* V = dynamic_cast<AST::Alloca*>(T);
return V->VarName;
}
else if (dynamic_cast<AST::Load*>(T))
{
AST::Load* V = dynamic_cast<AST::Load*>(T);
return V->Name;
}
else if (dynamic_cast<AST::Pure*>(T))
{
AST::Pure* V = dynamic_cast<AST::Pure*>(T);
return V->Name;
}
else if (dynamic_cast<AST::GetElement*>(T))
{
AST::GetElement* V = dynamic_cast<AST::GetElement*>(T);
return V->GetElementName;
}

return "";
}
Expand Down Expand Up @@ -1026,14 +1063,14 @@ ARGUMENT_LIST() generate_block_codegen(ARGUMENT_LIST() Body, llvm::BasicBlock* E
{
if(l.first == i)
{
auto c = phiRelated[l.second].second->codegen(); VERIFY(c)
auto c = GetInst(phiRelated[l.second].second); VERIFY(c)
phiRelated[l.second].first.phi->setIncomingValue(0, c);
is_linked = true;
}
}

if(!is_linked) {
instructions[i]->codegen();
auto igi = GetInstNoCatch(instructions[i]);
}
}

Expand All @@ -1060,7 +1097,7 @@ llvm::Value* AST::Loop::codegen()

Body = generate_block_codegen(std::move(Body), EntryBlock, LoopBlock);

llvm::Value* ConditionV2 = Condition->codegen();
llvm::Value* ConditionV2 = GetInst(Condition.get());

CodeGen::Builder->CreateCondBr(ConditionV2, LoopBlock, ContinueBlock);

Expand Down Expand Up @@ -1373,6 +1410,8 @@ llvm::Function* AST::Function::apply_attributes(llvm::Function* f) {
if(!attributes.prints_exceptions_at_runtime) { f->addFnAttr(llvm::Attribute::NoUnwind); }
if(attributes.must_progress) { f->addFnAttr(llvm::Attribute::MustProgress); }

f->addFnAttr(llvm::Attribute::AlwaysInline);

if(attributes.calling_convention == "GLASGOW_HASKELL") { f->setCallingConv(llvm::CallingConv::GHC); }
else if(attributes.calling_convention == "TAIL") { f->setCallingConv(llvm::CallingConv::Tail); }

Expand Down Expand Up @@ -1441,5 +1480,7 @@ llvm::Function* AST::Function::codegen()

TheFunction = apply_attributes(TheFunction);

CodeGen::TheFPM->run(*TheFunction);

return TheFunction;
}
12 changes: 11 additions & 1 deletion Language/CodeGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
std::unique_ptr<llvm::LLVMContext> CodeGen::TheContext;
std::unique_ptr<llvm::IRBuilder<>> CodeGen::Builder;
std::unique_ptr<llvm::Module> CodeGen::TheModule;
std::unique_ptr<llvm::legacy::FunctionPassManager> CodeGen::TheFPM;

bool CodeGen::is_release = false;

std::map<std::string, llvm::Value*> CodeGen::NamedValues;
std::map<std::string, std::pair<llvm::LoadInst*, llvm::Value*>> CodeGen::NamedLoads;
Expand Down Expand Up @@ -53,7 +56,14 @@ void CodeGen::Initialize()
// Create a new builder for the module.
Builder = std::make_unique<llvm::IRBuilder<>>(*TheContext);

//TheFPM = std::make_unique<llvm::legacy::FunctionPassManager>(TheModule.get());
TheFPM = std::make_unique<llvm::legacy::FunctionPassManager>(TheModule.get());

if(is_release) {
TheFPM->add(llvm::createInstructionCombiningPass());
}
//TheFPM->add(llvm::createPartialInliningPass());

TheFPM->doInitialization();
}

void CodeGen::Error(std::string str)
Expand Down
3 changes: 3 additions & 0 deletions Language/CodeGen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ struct CodeGen
static std::unique_ptr<llvm::LLVMContext> TheContext;
static std::unique_ptr<llvm::IRBuilder<>> Builder;
static std::unique_ptr<llvm::Module> TheModule;
static std::unique_ptr<llvm::legacy::FunctionPassManager> TheFPM;

static bool is_release;

static std::map<std::string, llvm::Value*> NamedValues;
static std::map<std::string, std::pair<llvm::LoadInst*, llvm::Value*>> NamedLoads;
Expand Down
16 changes: 16 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,28 @@ int main(int argc, char const *argv[])
}
else if (cmd == "emit")
{
if(argc > 2) {
std::string cmd_two = argv[2];

if(cmd_two == "release") {
CodeGen::is_release = true;
}
}

CompileToLLVMIR();
CodeGen::Print();
TodoList::print();
}
else if (cmd == "build")
{
if(argc > 2) {
std::string cmd_two = argv[2];

if(cmd_two == "release") {
CodeGen::is_release = true;
}
}

CompileToLLVMIR();
TodoList::print();
CodeGen::Build();
Expand Down

0 comments on commit ec2788c

Please sign in to comment.