Skip to content

Commit

Permalink
Now LLVM initializes correctly (#46 from wesuRage/main)
Browse files Browse the repository at this point in the history
Now LLVM initializes correctly
  • Loading branch information
wesuRage authored Dec 9, 2024
2 parents 25fc44e + c6f1b8f commit 10d39e3
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 28 deletions.
5 changes: 1 addition & 4 deletions examples/a.glx
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
@teste;

array[0] := 3..=7;
array[return_index().array] := 5;
int oi := 3;
15 changes: 8 additions & 7 deletions src/backend/generator/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set(CMAKE_BUILD_TYPE Debug)
include_directories(${PROJECT_SOURCE_DIR}/include)

# Gather generator sources
file(GLOB_RECURSE GENERATOR_SOURCES "*.cpp")
file(GLOB_RECURSE GENERATOR_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp")
file(GLOB FRONTEND_SOURCES "${PROJECT_SOURCE_DIR}/src/frontend/**/*.c")
list(FILTER GENERATOR_SOURCES EXCLUDE REGEX ".*generator\\.test\\.cpp$")

Expand All @@ -17,7 +17,7 @@ execute_process(
)

# Remove leading/trailing spaces and newlines from LLVM_FLAGS
string(REPLACE "\n" "" LLVM_FLAGS "${LLVM_FLAGS}")
string(REPLACE "\n" " " LLVM_FLAGS "${LLVM_FLAGS}")
string(REPLACE " " ";" LLVM_FLAGS "${LLVM_FLAGS}")

# Include LLVM directories
Expand All @@ -27,13 +27,14 @@ add_definitions(${LLVM_DEFINITIONS})

# Create generator library
add_library(generator ${GENERATOR_SOURCES} ${FRONTEND_SOURCES})
target_include_directories(generator PUBLIC ${PROJECT_SOURCE_DIR}/include)
target_link_libraries(generator PUBLIC lexer parser node_definitions ${LLVM_LIBRARIES})

# Create a test executable if test file exists
# Create a test executable if the test file exists
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/generator.test.cpp")
add_executable(generator_test generator.test.cpp)
add_executable(generator_test ${CMAKE_CURRENT_SOURCE_DIR}/generator.test.cpp)
target_include_directories(generator_test PRIVATE ${PROJECT_SOURCE_DIR}/include ${LLVM_INCLUDE_DIRS})
target_compile_options(generator_test PRIVATE ${LLVM_FLAGS})
target_link_libraries(generator_test PRIVATE LLVMCore LLVMExecutionEngine LLVMBitWriter LLVMSupport)
target_link_libraries(generator_test PRIVATE generator lexer parser node_definitions ${LLVM_FLAGS})
target_include_directories(generator_test PRIVATE ${PROJECT_SOURCE_DIR}/include)
target_link_libraries(generator_test PRIVATE generator lexer parser node_definitions)
target_link_libraries(generator_test PRIVATE LLVMCore LLVMExecutionEngine LLVMBitWriter LLVMSupport ${LLVM_LIBRARIES})
endif()
60 changes: 44 additions & 16 deletions src/backend/generator/generator.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@
#include <llvm/IR/LLVMContext.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/IRBuilder.h>
#include <llvm/Support/raw_ostream.h>
#include <llvm/IR/Verifier.h>
#include <llvm/Support/TargetSelect.h>
#include <llvm/MC/TargetRegistry.h>
#include <llvm/Target/TargetOptions.h>
#include <llvm/CodeGen/CommandFlags.h>
#include <llvm/IR/LegacyPassManager.h>
#include <llvm/Support/FileSystem.h>
#include <llvm/Support/raw_ostream.h>
#include <llvm/Support/Error.h>
#include <llvm/Target/TargetMachine.h>
#include <llvm/TargetParser/Host.h>

#include "backend/generator/generate_ir.hpp"

Expand Down Expand Up @@ -56,13 +63,11 @@ int main(int argc, char **argv) {
printf("-----------------\n");
print_ast(ast);

llvm::InitializeNativeTarget();
llvm::InitializeNativeTargetAsmPrinter();
llvm::LLVMContext Context;
llvm::Module Module("galaxy_module", Context);
llvm::IRBuilder<> Builder(Context);
llvm::LLVMContext TheContext;
llvm::Module TheModule("GalaxyJIT", TheContext);
llvm::IRBuilder<> Builder(TheContext);

std::vector<llvm::Value*> values = generate_ir(ast, Context, Module, Builder);
std::vector<llvm::Value*> values = generate_ir(ast, TheContext, TheModule, Builder);

for (size_t i = 0; i < values.size(); ++i) {
if (values[i]) {
Expand All @@ -73,24 +78,47 @@ int main(int argc, char **argv) {
}
}

std::string error;
if (llvm::verifyModule(Module, &llvm::errs())) {
std::cerr << "O módulo LLVM contém erros!\n";
auto TargetTriple = llvm::sys::getDefaultTargetTriple();
TheModule.setTargetTriple(TargetTriple);

std::string Error;
auto Target = llvm::TargetRegistry::lookupTarget(TargetTriple, Error);

if (!Target) {
llvm::errs() << Error;
return 1;
}

std::string filename = "generated_ir.ll";
auto CPU = "generic";
auto Features = "";

llvm::TargetOptions opt;
auto TheTargetMachine = Target->createTargetMachine(
TargetTriple, CPU, Features, opt, llvm::Reloc::PIC_);

TheModule.setDataLayout(TheTargetMachine->createDataLayout());

auto Filename = "output.o";
std::error_code EC;
llvm::raw_fd_ostream dest(filename, EC);
llvm::raw_fd_ostream dest(Filename, EC, llvm::sys::fs::OF_None);

if (EC) {
std::cerr << "Erro ao abrir o arquivo para escrita: " << EC.message() << std::endl;
llvm::errs() << "Could not open file: " << EC.message();
return 1;
}

llvm::legacy::PassManager pass;
auto FileType = llvm::CodeGenFileType::ObjectFile;

if (TheTargetMachine->addPassesToEmitFile(pass, dest, nullptr, FileType)) {
llvm::errs() << "TheTargetMachine can't emit a file of this type";
return 1;
}

Module.print(dest, nullptr);
pass.run(TheModule);
dest.flush();

std::cout << "IR escrito no arquivo " << filename << "\n";
llvm::outs() << "Wrote " << Filename << "\n";

free_ast_node(ast);
freeTokens(tokens, count);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ llvm::Value* generate_variable_declaration_stmt(VariableNode *node, llvm::LLVMCo
case TYPE_FLOAT: var_type = llvm::Type::getFloatTy(Context); break;
case TYPE_DOUBLE: var_type = llvm::Type::getDoubleTy(Context); break;
case TYPE_BOOL: var_type = llvm::Type::getInt1Ty(Context); break;
default: throw std::runtime_error("Tipo de variável não suportado");
default: var_type = llvm::Type::getInt32Ty(Context); break;
}

// Criar espaço para a variável no IR
Expand Down

0 comments on commit 10d39e3

Please sign in to comment.