diff --git a/examples/a.glx b/examples/a.glx index add9142..1960342 100644 --- a/examples/a.glx +++ b/examples/a.glx @@ -1,11 +1,10 @@ -extern int writeln( string) ; +extern int writeln( string) ; +extern string itos( int) ; -def main( ) -> int: +def main( ) -> int: - int N := 100000; - - for parallel static ( int i := 0; i < N; ++ i) -> 8: - writeln( "hello") ; + for ( int i := 0; i < 100000; ++ i) : + writeln( "val" ) ; end; return 0; diff --git a/include/backend/generator/utils/return_id.hpp b/include/backend/generator/utils/return_id.hpp new file mode 100644 index 0000000..09e6d00 --- /dev/null +++ b/include/backend/generator/utils/return_id.hpp @@ -0,0 +1,8 @@ +#ifndef GLOBAL_ID_RETURN_H +#define GLOBAL_ID_RETURN_H + +#include + +extern std::string global_id_return; + +#endif // GLOBAL_ID_RETURN_H \ No newline at end of file diff --git a/src/backend/generator/expressions/generate_assignment_expr.cpp b/src/backend/generator/expressions/generate_assignment_expr.cpp index 5ee8300..60c6d1f 100644 --- a/src/backend/generator/expressions/generate_assignment_expr.cpp +++ b/src/backend/generator/expressions/generate_assignment_expr.cpp @@ -1,13 +1,16 @@ #include "backend/generator/expressions/generate_assignment_expr.hpp" #include "backend/generator/expressions/generate_expr.hpp" +#include "backend/generator/utils/return_id.hpp" llvm::Value *generate_assignment_expr(AssignmentNode *node, llvm::LLVMContext &Context, llvm::IRBuilder<> &Builder, llvm::Module &Module) { - // Evaluate the right-hand side expression and get its LLVM value representation. - llvm::Value *right_value = generate_expr(node->value, Context, Builder, 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); + // Evaluate the right-hand side expression and get its LLVM value representation. + global_id_return = "value"; + llvm::Value *right_value = generate_expr(node->value, Context, Builder, Module); + // Create a store instruction that assigns the value of the // right-hand side to the memory location of the left-hand side. Builder.CreateStore(right_value, left_value); diff --git a/src/backend/generator/expressions/generate_call.cpp b/src/backend/generator/expressions/generate_call.cpp index 35c1bb4..95738eb 100644 --- a/src/backend/generator/expressions/generate_call.cpp +++ b/src/backend/generator/expressions/generate_call.cpp @@ -1,7 +1,7 @@ #include "backend/generator/expressions/generate_call.hpp" #include "backend/generator/expressions/generate_expr.hpp" #include "backend/generator/symbols/function_symbol_table.hpp" -#include "backend/generator/parallel/queue.hpp" +#include "backend/generator/utils/return_id.hpp" llvm::Value *generate_call(CallNode *call_node, llvm::LLVMContext &Context, llvm::IRBuilder<> &Builder, llvm::Module &Module) { if (!call_node || !call_node->caller) { diff --git a/src/backend/generator/expressions/generate_identifier.cpp b/src/backend/generator/expressions/generate_identifier.cpp index bf2e781..17ba025 100644 --- a/src/backend/generator/expressions/generate_identifier.cpp +++ b/src/backend/generator/expressions/generate_identifier.cpp @@ -1,12 +1,14 @@ #include "backend/generator/expressions/generate_identifier.hpp" #include "backend/generator/symbols/identifier_symbol_table.hpp" -#include "backend/generator/parallel/queue.hpp" +#include "backend/generator/utils/return_id.hpp" llvm::Value *generate_identifier(IdentifierNode *node) { - try { - return find_or_wait_for_identifier(node, global_id_return); - } catch (...) { - throw std::runtime_error("Error: identifier not found!"); + const SymbolInfo* id = find_identifier(node->symbol); + if (id) { + if (global_id_return == "declaration") { + return id->declaration; + } else { + return id->value; + } } -} - +} \ No newline at end of file diff --git a/src/backend/generator/generator.test.cpp b/src/backend/generator/generator.test.cpp index e63a341..857606d 100644 --- a/src/backend/generator/generator.test.cpp +++ b/src/backend/generator/generator.test.cpp @@ -19,7 +19,6 @@ #include "backend/generator/generate_ir.hpp" #include "backend/generator/symbols/symbol_stack.hpp" -#include "backend/generator/parallel/queue.hpp" extern "C" { #include "frontend/lexer/core.h" @@ -85,9 +84,6 @@ int main(int argc, char **argv) { // Create the global scope enter_scope(); - std::thread pendingThread(process_pending_identifiers_periodically); - pendingThread.detach(); - std::vector values = generate_ir(ast, TheContext, TheModule, Builder); // Print out the generated LLVM IR for debugging diff --git a/src/backend/generator/statements/generate_extern_stmt.cpp b/src/backend/generator/statements/generate_extern_stmt.cpp index 41feb44..2d17683 100644 --- a/src/backend/generator/statements/generate_extern_stmt.cpp +++ b/src/backend/generator/statements/generate_extern_stmt.cpp @@ -2,7 +2,7 @@ #include "backend/generator/types/generate_type.hpp" #include "backend/generator/symbols/function_symbol_table.hpp" #include "backend/generator/symbols/identifier_symbol_table.hpp" -#include "backend/generator/parallel/queue.hpp" +#include "backend/generator/utils/return_id.hpp" llvm::Value* generate_extern_stmt(ExternNode *node, llvm::LLVMContext &Context, llvm::IRBuilder<> &Builder, llvm::Module &Module) { if (!node || !node->type || !node->identifier) { @@ -45,10 +45,7 @@ llvm::Value* generate_extern_stmt(ExternNode *node, llvm::LLVMContext &Context, global_var->setLinkage(llvm::GlobalValue::ExternalLinkage); - { - std::unique_lock lock(symbolTableMutex); - add_identifier(node->identifier, global_var, nullptr, decl_type); - } + add_identifier(node->identifier, global_var, nullptr, decl_type); return global_var; } else { diff --git a/src/backend/generator/statements/generate_for_stmt.cpp b/src/backend/generator/statements/generate_for_stmt.cpp index df72c07..0f9194b 100644 --- a/src/backend/generator/statements/generate_for_stmt.cpp +++ b/src/backend/generator/statements/generate_for_stmt.cpp @@ -36,6 +36,7 @@ llvm::Value* generate_for_stmt(ForNode *node, llvm::LLVMContext &Context, llvm:: } if (stopVal->getType() != llvm::Type::getInt32Ty(Context)) { + stopVal = Builder.CreateLoad(llvm::Type::getInt32Ty(Context), stopVal, "stopval"); stopVal = Builder.CreateSExt(stopVal, llvm::Type::getInt32Ty(Context)); } diff --git a/src/backend/generator/statements/generate_outlined_for.cpp b/src/backend/generator/statements/generate_outlined_for.cpp index cbb0d27..d349a15 100644 --- a/src/backend/generator/statements/generate_outlined_for.cpp +++ b/src/backend/generator/statements/generate_outlined_for.cpp @@ -44,25 +44,6 @@ llvm::Function* generate_outlined_for(ForNode *node, llvm::LLVMContext &Context, Builder.CreateStore(llvm::ConstantInt::get(llvm::Type::getInt32Ty(Context), 1), ompStride); // Incremento Builder.CreateStore(llvm::ConstantInt::get(llvm::Type::getInt32Ty(Context), 0), ompIsLast); // Última iteração (inicialmente 0) - llvm::BasicBlock *preLoopBB = llvm::BasicBlock::Create(Context, "preloop", outlinedFunc); - llvm::BasicBlock *condBB = llvm::BasicBlock::Create(Context, "cond", outlinedFunc); - llvm::BasicBlock *bodyBB = llvm::BasicBlock::Create(Context, "body", outlinedFunc); - llvm::BasicBlock *updateBB = llvm::BasicBlock::Create(Context, "update", outlinedFunc); - llvm::BasicBlock *endBB = llvm::BasicBlock::Create(Context, "endloop", outlinedFunc); - - Builder.CreateBr(preLoopBB); - Builder.SetInsertPoint(preLoopBB); - - Builder.CreateBr(condBB); - - Builder.SetInsertPoint(condBB); - llvm::Value *loopVarValLoad = Builder.CreateLoad(loopVarVal->getType(), loopVar, node->variable); - llvm::Value *cond = Builder.CreateICmpSLT(loopVarValLoad, stopVal, "loopcond"); - - Builder.CreateCondBr(cond, bodyBB, endBB); - - Builder.SetInsertPoint(bodyBB); - // Determinar o tipo de agendamento com base na política llvm::Value *scheduleType = nullptr; if (strcmp(schedule_policy, "static") == 0) { @@ -146,6 +127,25 @@ llvm::Function* generate_outlined_for(ForNode *node, llvm::LLVMContext &Context, }); } + llvm::BasicBlock *preLoopBB = llvm::BasicBlock::Create(Context, "preloop", outlinedFunc); + llvm::BasicBlock *condBB = llvm::BasicBlock::Create(Context, "cond", outlinedFunc); + llvm::BasicBlock *bodyBB = llvm::BasicBlock::Create(Context, "body", outlinedFunc); + llvm::BasicBlock *updateBB = llvm::BasicBlock::Create(Context, "update", outlinedFunc); + llvm::BasicBlock *endBB = llvm::BasicBlock::Create(Context, "endloop", outlinedFunc); + + Builder.CreateBr(preLoopBB); + Builder.SetInsertPoint(preLoopBB); + + Builder.CreateBr(condBB); + + Builder.SetInsertPoint(condBB); + llvm::Value *loopVarValLoad = Builder.CreateLoad(loopVarVal->getType(), loopVar, node->variable); + llvm::Value *cond = Builder.CreateICmpSLT(loopVarValLoad, stopVal, "loopcond"); + + Builder.CreateCondBr(cond, bodyBB, endBB); + + Builder.SetInsertPoint(bodyBB); + for (size_t i = 0; i < node->body_count; i++) { generate_stmt(node->body[i], Context, Module, Builder); } diff --git a/src/backend/generator/utils/generate_stop.cpp b/src/backend/generator/utils/generate_stop.cpp index d7f85de..5cd1ebf 100644 --- a/src/backend/generator/utils/generate_stop.cpp +++ b/src/backend/generator/utils/generate_stop.cpp @@ -1,7 +1,13 @@ #include "backend/generator/expressions/generate_expr.hpp" #include "backend/generator/utils/generate_stop.hpp" +#include "backend/generator/utils/return_id.hpp" llvm::Value *generate_stop(AstNode *node, llvm::LLVMContext &Context, llvm::IRBuilder<> &Builder, llvm::Module &Module) { BinaryExprNode *bin_expr = (BinaryExprNode *)node->data; - return generate_expr(bin_expr->right, Context, Builder, Module); + + global_id_return = "declaration"; + llvm::Value *expr = generate_expr(bin_expr->right, Context, Builder, Module); + global_id_return = "value"; + + return expr; } \ No newline at end of file diff --git a/src/backend/generator/utils/return_id.cpp b/src/backend/generator/utils/return_id.cpp new file mode 100644 index 0000000..6508353 --- /dev/null +++ b/src/backend/generator/utils/return_id.cpp @@ -0,0 +1,3 @@ +#include "backend/generator/utils/return_id.hpp" + +std::string global_id_return; \ No newline at end of file diff --git a/src/frontend/parser/statements/parse_stmt.c b/src/frontend/parser/statements/parse_stmt.c index 3df1af2..9a98075 100644 --- a/src/frontend/parser/statements/parse_stmt.c +++ b/src/frontend/parser/statements/parse_stmt.c @@ -37,7 +37,13 @@ AstNode *parse_stmt(Parser *parser) { } // Attempt to parse a type. If this fails, it's likely an expression. - char *type = parse_type(parser); + char *type; + + if (next_token(parser).type != TOKEN_ASSIGN) { + type = parse_type(parser); + } else { + return parse_expr(parser); + } // If the type is "implicit", this means it's an ambiguous type. if (strcmp(type, "implicit") == 0) { @@ -64,7 +70,6 @@ AstNode *parse_stmt(Parser *parser) { return parse_variable_declaration_stmt(parser, isPtr, isConst, type); } - // If it's not a declaration, treat it as a generic expression. return parse_expr(parser); } }