mirror of
https://github.com/PashaBibko/LX.git
synced 2026-04-04 01:49:05 +00:00
Improved compile times
Added A LOT of comments
This commit is contained in:
@@ -1,63 +1,59 @@
|
||||
#include <Parser.h>
|
||||
|
||||
#include <Util.h>
|
||||
#include <AST.h>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
namespace LX
|
||||
{
|
||||
// Tells the generator if the current node is allowed to be within a top-level context //
|
||||
// TODO: Make this function do something other than return true
|
||||
static constexpr bool IsValidTopLevelNode(AST::Node::NodeType type)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
static void GenerateFunctionIR(FunctionDefinition& funcAST, llvm::LLVMContext& context, llvm::Module& module, llvm::IRBuilder<>& builder)
|
||||
// Generates the LLVM IR for the given function //
|
||||
static void GenerateFunctionIR(FunctionDefinition& funcAST, InfoLLVM& LLVM)
|
||||
{
|
||||
// Creates the functions signature and return type //
|
||||
|
||||
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::BasicBlock* entry = llvm::BasicBlock::Create(context, "entry", func);
|
||||
builder.SetInsertPoint(entry);
|
||||
|
||||
// Generates the IR within the function //
|
||||
llvm::FunctionType* retType = llvm::FunctionType::get(llvm::Type::getInt32Ty(LLVM.context), false); // <- Defaults to int currently
|
||||
llvm::Function* func = llvm::Function::Create(retType, llvm::Function::ExternalLinkage, "main", LLVM.module); // Defaults to main currently
|
||||
llvm::BasicBlock* entry = llvm::BasicBlock::Create(LLVM.context, "entry", func);
|
||||
LLVM.builder.SetInsertPoint(entry);
|
||||
|
||||
// Generates the IR within the function by looping over the nodes //
|
||||
for (auto& node : funcAST.body)
|
||||
{
|
||||
ThrowIf<int>(IsValidTopLevelNode(node->m_Type) == false); // <- TODO: replace with actual error type
|
||||
node->GenIR(context, module, builder);
|
||||
node->GenIR(LLVM);
|
||||
}
|
||||
|
||||
// Adds a terminator if there is none //
|
||||
|
||||
if (entry->getTerminator() == nullptr)
|
||||
{
|
||||
builder.CreateRet(llvm::ConstantInt::get(llvm::Type::getInt32Ty(context), 0, true));
|
||||
LLVM.builder.CreateRet(llvm::ConstantInt::get(llvm::Type::getInt32Ty(LLVM.context), 0, true));
|
||||
}
|
||||
|
||||
// Verifies the function works //
|
||||
|
||||
ThrowIf<int>(llvm::verifyFunction(*func), &llvm::errs()); // <- Make error type
|
||||
ThrowIf<int>(llvm::verifyFunction(*func), &llvm::errs()); // <- TODO: Make error type
|
||||
}
|
||||
|
||||
// Turns an abstract binary tree into LLVM intermediate representation //
|
||||
void GenerateIR(FileAST& ast)
|
||||
{
|
||||
// Creates the LLVM variables needed for generating IR that are shared between functions //
|
||||
InfoLLVM LLVM("add_itns");
|
||||
|
||||
llvm::LLVMContext context;
|
||||
llvm::IRBuilder<> builder(context);
|
||||
llvm::Module module("add_ints", context);
|
||||
|
||||
// Loops over AST to generate IR //
|
||||
|
||||
// Loops over the functions to generate their LLVM IR //
|
||||
for (auto& func : ast.functions)
|
||||
{
|
||||
GenerateFunctionIR(func, context, module, builder);
|
||||
GenerateFunctionIR(func, LLVM);
|
||||
}
|
||||
|
||||
// Outputs the IR to the console //
|
||||
|
||||
module.print(llvm::outs(), nullptr);
|
||||
|
||||
std::cout << "Finished generating IR" << std::endl;
|
||||
LLVM.module.print(llvm::outs(), nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user