Skip to content

Commit

Permalink
fix(for): now it constructs the iterator correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
wesuRage committed Jan 12, 2025
1 parent e134d98 commit 31d2331
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
llvm::Value *generate_assignment_expr(AssignmentNode *node, llvm::LLVMContext &Context, llvm::IRBuilder<llvm::NoFolder> &Builder, llvm::Module &Module) {
// Evaluate the left-hand side expression to get the memory location (pointer).
global_id_return = "declaration";
llvm::Value *left_value = generate_expr(node->left, Context, Builder, Module);
llvm::Value *left_value = generate_expr(node->left, Context, Builder, Module);

// Evaluate the right-hand side expression and get its LLVM value representation.
global_id_return = "value";
Expand Down
92 changes: 49 additions & 43 deletions src/backend/generator/expressions/generate_binary_expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "backend/generator/symbols/identifier_symbol_table.hpp"
#include "backend/generator/symbols/string_symbol_table.hpp"
#include "backend/generator/symbols/function_symbol_table.hpp"
#include <iostream>

llvm::Value *generate_binary_expr(BinaryExprNode *node, llvm::LLVMContext &Context, llvm::IRBuilder<llvm::NoFolder> &Builder, llvm::Module &Module) {
llvm::Value *L = generate_expr(node->left, Context, Builder, Module);
Expand All @@ -19,55 +20,60 @@ llvm::Value *generate_binary_expr(BinaryExprNode *node, llvm::LLVMContext &Conte
bool isRPointer = RType->isPointerTy();

if (isLPointer) {
llvm::errs() << L->getValueName()->getValue()->getName().str() << "\n";
try {
llvm::Value *string = find_string(L->getValueName()->getValue()->getName().str());
if (string) {
if (isRInteger && strcmp(node->op, "*") == 0) {
auto it = function_symbol_table.find("strrep");
if (it == function_symbol_table.end()) {
throw std::runtime_error("Function not found in symbol table: strrep");
}
llvm::Value *string = find_string(L->getValueName()->getValue()->getName().str());
if (string) {
if (isRInteger && strcmp(node->op, "*") == 0) {
auto it = function_symbol_table.find("strrep");
if (it == function_symbol_table.end()) {
throw std::runtime_error("Function not found in symbol table: strrep");
}

llvm::Function *function = it->second;
llvm::FunctionType *func_type = function->getFunctionType();

// Prepare arguments for the strrep function
std::vector<llvm::Value *> args;
// The first argument is the string (L)
args.push_back(string);
// The second argument is the repetition count (R)
args.push_back(R);

// Ensure the types of the arguments are correct
for (size_t i = 0; i < args.size(); ++i) {
llvm::Value *arg = args[i];
llvm::Type *expected_type = func_type->getParamType(i);
if (arg->getType() != expected_type) {
if (arg->getType()->isIntegerTy() && expected_type->isIntegerTy()) {
arg = Builder.CreateIntCast(arg, expected_type, true);
} else if (arg->getType()->isFloatingPointTy() && expected_type->isFloatingPointTy()) {
arg = Builder.CreateFPCast(arg, expected_type);
} else if (arg->getType()->isPointerTy() && expected_type->isIntegerTy()) {
arg = Builder.CreatePointerCast(arg, expected_type);
} else if (arg->getType()->isPointerTy() && expected_type->isFloatingPointTy()) {
arg = Builder.CreatePointerCast(arg, expected_type);
} else {
throw std::runtime_error("Argument type mismatch.");
}
llvm::Function *function = it->second;
llvm::FunctionType *func_type = function->getFunctionType();

// Prepare arguments for the strrep function
std::vector<llvm::Value *> args;
// The first argument is the string (L)
args.push_back(string);
// The second argument is the repetition count (R)
args.push_back(R);

// Ensure the types of the arguments are correct
for (size_t i = 0; i < args.size(); ++i) {
llvm::Value *arg = args[i];
llvm::Type *expected_type = func_type->getParamType(i);
if (arg->getType() != expected_type) {
if (arg->getType()->isIntegerTy() && expected_type->isIntegerTy()) {
arg = Builder.CreateIntCast(arg, expected_type, true);
} else if (arg->getType()->isFloatingPointTy() && expected_type->isFloatingPointTy()) {
arg = Builder.CreateFPCast(arg, expected_type);
} else if (arg->getType()->isPointerTy() && expected_type->isIntegerTy()) {
arg = Builder.CreatePointerCast(arg, expected_type);
} else if (arg->getType()->isPointerTy() && expected_type->isFloatingPointTy()) {
arg = Builder.CreatePointerCast(arg, expected_type);
} else {
throw std::runtime_error("Argument type mismatch.");
}
args[i] = arg;
}

// Call the strrep function
llvm::Value *resultBuffer = Builder.CreateCall(function, args);
return resultBuffer;
args[i] = arg;
}

// Call the strrep function
llvm::Value *resultBuffer = Builder.CreateCall(function, args);
return resultBuffer;
}
} catch (...) {
const SymbolInfo* symbolInfo = find_identifier(static_cast<IdentifierNode*>(node->left->data)->symbol);
} else {

llvm::errs() << "Value: " << L->getValueName()->getValue()->getName().str() << "\n";
const SymbolInfo* symbolInfo;

symbolInfo = find_identifier(static_cast<IdentifierNode*>(node->left->data)->symbol);
if (!symbolInfo) {
throw std::runtime_error("Unsupported Left type for binary operation");
symbolInfo = find_identifier(L->getValueName()->getValue()->getName().str());

if (!symbolInfo) {
throw std::runtime_error("Unsupported Left type for binary operation");
}
}
llvm::Type* pointeeType = symbolInfo->type;

Expand Down
14 changes: 8 additions & 6 deletions src/backend/generator/statements/generate_for_stmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "backend/generator/symbols/identifier_symbol_table.hpp"
#include "backend/generator/symbols/iterator_stack.hpp"
#include "backend/generator/types/generate_type.hpp"
#include <iostream>

llvm::Value* generate_for_stmt(ForNode *node, llvm::LLVMContext &Context, llvm::IRBuilder<llvm::NoFolder> &Builder, llvm::Module &Module) {
llvm::Function *currentFunction = Builder.GetInsertBlock()->getParent();
Expand All @@ -18,21 +19,25 @@ llvm::Value* generate_for_stmt(ForNode *node, llvm::LLVMContext &Context, llvm::
iteratorVar.isConst = false;
iteratorVar.value = node->start;

llvm::BasicBlock *preLoopBB = llvm::BasicBlock::Create(Context, "preloop", currentFunction);
Builder.CreateBr(preLoopBB);

Builder.SetInsertPoint(preLoopBB);

llvm::Value *decl;
llvm::Value *iterator;

if (node->iterator) {
iterator = generate_expr(node->iterator, Context, Builder, Module);
decl = generate_variable_declaration_stmt(&iteratorVar, Context, Builder, Module);
iterator = generate_expr(node->iterator, Context, Builder, Module);
}

llvm::Value *startVal = generate_expr(node->start, Context, Builder, Module);

push_iterator(decl, iterator, iterator->getType());
push_iterator(decl, startVal, startVal->getType());

llvm::AllocaInst *loopVar = Builder.CreateAlloca(startVal->getType(), nullptr, node->variable);
Builder.CreateStore(startVal, loopVar); // Inicializar a variável do loop
Builder.CreateStore(startVal, loopVar);

{
std::lock_guard<std::mutex> lock(symbol_stack_mutex);
Expand Down Expand Up @@ -142,14 +147,11 @@ llvm::Value* generate_for_stmt(ForNode *node, llvm::LLVMContext &Context, llvm::

Builder.CreateCall(kmpcForkCall, { ompIdent1, llvm::ConstantInt::get(llvm::Type::getInt32Ty(Context), 0), ompOutlined });
} else {
llvm::BasicBlock *preLoopBB = llvm::BasicBlock::Create(Context, "preloop", currentFunction);
llvm::BasicBlock *condBB = llvm::BasicBlock::Create(Context, "cond", currentFunction);
llvm::BasicBlock *bodyBB = llvm::BasicBlock::Create(Context, "body", currentFunction);
llvm::BasicBlock *updateBB = llvm::BasicBlock::Create(Context, "update", currentFunction);
llvm::BasicBlock *endBB = llvm::BasicBlock::Create(Context, "endloop", currentFunction);

Builder.CreateBr(preLoopBB);
Builder.SetInsertPoint(preLoopBB);

Builder.CreateBr(condBB);

Expand Down
2 changes: 2 additions & 0 deletions src/backend/generator/statements/generate_if_stmt.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include "backend/generator/statements/generate_if_stmt.hpp"
#include "backend/generator/statements/generate_stmt.hpp"
#include "backend/generator/expressions/generate_expr.hpp"
#include "backend/generator/parallel/queue.hpp"

llvm::Value* generate_if_stmt(IfNode *node, llvm::LLVMContext &Context, llvm::IRBuilder<llvm::NoFolder> &Builder, llvm::Module &Module) {

global_id_return = "declaration";
llvm::Value *condition = generate_expr(node->condition, Context, Builder, Module);
if (!condition) {
throw std::runtime_error("Failed to generate condition for if statement.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,23 @@ llvm::Value* generate_variable_declaration_stmt(VariableNode *node, llvm::LLVMCo
// Create an AllocaInst to allocate space for the variable on the stack
llvm::AllocaInst *alloca = Builder.CreateAlloca(var_type, nullptr, node->name);


llvm::Value *init_value;

if (node->value != nullptr) {
// Generate IR for the expression
llvm::Value *init_value = generate_expr(node->value, Context, Builder, Module);

// Store the initialized value into the allocated space (AllocaInst)
Builder.CreateStore(init_value, alloca);
init_value = generate_expr(node->value, Context, Builder, Module);

// Stores the allocated variable in the identifier symbol table
{
std::lock_guard<std::mutex> lock(symbol_stack_mutex);
add_identifier(node->name, alloca, init_value, var_type);
}

// Store the initialized value into the allocated space (AllocaInst)
init_value = Builder.CreateStore(init_value, alloca);

}

return alloca;
return init_value;
}

0 comments on commit 31d2331

Please sign in to comment.