LLVM-Works

This commit is contained in:
Pasha Bibko
2025-04-20 13:49:43 +01:00
parent 14b2b36748
commit 680a3d1323
3 changed files with 63 additions and 47 deletions

View File

@@ -1,63 +1,62 @@
#include <Parser.h> #include <Parser.h>
#include <Util.h>
#include <iostream> #include <iostream>
namespace LX namespace LX
{ {
void GenerateIR(FileAST& ast) static constexpr bool IsValidTopLevelNode(AST::Node::NodeType type)
{ {
// Generates stuff // return true;
}
llvm::LLVMContext context;
llvm::IRBuilder<> builder(context);
static void GenerateFunctionIR(FunctionDefinition& funcAST, llvm::LLVMContext& context, llvm::Module& module, llvm::IRBuilder<>& builder)
{ {
std::unique_ptr<llvm::Module> module = std::make_unique<llvm::Module>("add_ints", context); // Creates the functions signature and return type //
// Defines main function // llvm::FunctionType* retType = llvm::FunctionType::get(llvm::Type::getInt32Ty(context), false); // <- Defaults to int currently
llvm::Function* func = llvm::Function::Create(retType, llvm::Function::ExternalLinkage, "main", module); // Defaults to main currently
llvm::FunctionType* funcType = llvm::FunctionType::get(llvm::Type::getInt32Ty(context), false); llvm::BasicBlock* entry = llvm::BasicBlock::Create(context, "entry", func);
llvm::Function* mainFunc = llvm::Function::Create(funcType, llvm::Function::ExternalLinkage, "main", module.get());
llvm::BasicBlock* entry = llvm::BasicBlock::Create(context, "entry", mainFunc);
builder.SetInsertPoint(entry); builder.SetInsertPoint(entry);
// Loops over AST to generate IR // // Generates the IR within the function //
for (auto& node : ast.functions[0].body) for (auto& node : funcAST.body)
{ {
switch (node->m_Type) ThrowIf<int>(IsValidTopLevelNode(node->m_Type) == false); // <- TODO: replace with actual error type
{ node->GenIR(context, module, builder);
case AST::Node::RETURN_STATEMENT:
{
node->GenIR(context, *module, builder);
break;
} }
default: // Adds a terminator if there is none //
{
break;
}
}
}
if (entry->getTerminator() == nullptr) if (entry->getTerminator() == nullptr)
{ {
builder.CreateRet(llvm::ConstantInt::get(llvm::Type::getInt32Ty(context), 0, true)); builder.CreateRet(llvm::ConstantInt::get(llvm::Type::getInt32Ty(context), 0, true));
} }
// Verification of the IR // // Verifies the function works //
if (llvm::verifyFunction(*mainFunc, &llvm::errs()) || llvm::verifyModule(*module, &llvm::errs())) ThrowIf<int>(llvm::verifyFunction(*func), &llvm::errs()); // <- Make error type
}
void GenerateIR(FileAST& ast)
{ {
std::cerr << "Error: IR generation failed" << std::endl; // Creates the LLVM variables needed for generating IR that are shared between functions //
return;
llvm::LLVMContext context;
llvm::IRBuilder<> builder(context);
llvm::Module module("add_ints", context);
// Loops over AST to generate IR //
for (auto& func : ast.functions)
{
GenerateFunctionIR(func, context, module, builder);
} }
// Outputs the IR to the console // // Outputs the IR to the console //
module->print(llvm::outs(), nullptr); module.print(llvm::outs(), nullptr);
} // <- Crashes here
std::cout << "Finished generating IR" << std::endl; std::cout << "Finished generating IR" << std::endl;
} }

View File

@@ -0,0 +1,17 @@
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Started lexing file
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Token::FUNCTION
Token::IDENTIFIER: main
Unknown: 3
Token::NUMBER_LITERAL: 3
Token::ADD
Token::NUMBER_LITERAL: 56
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Started parsing tokens
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
AST length: 1

View File

@@ -1,2 +1,2 @@
func main func main
return 3 + 4 return 3 + 56