diff --git a/include/compiler/codegen/Instruction.h b/include/compiler/codegen/Instruction.h index a69e707..a6db21b 100644 --- a/include/compiler/codegen/Instruction.h +++ b/include/compiler/codegen/Instruction.h @@ -31,6 +31,11 @@ #define HEADER \ fprintf(stdout, ".IFJcode23\n"); +// TODO: Fix +void Instruction_pops_where(char * where, enum Frame frame); + +void Instruction_defvar_where(char * where, enum Frame frame); + // --- UTILS --- void Instruction_label_while_start(size_t id); @@ -59,7 +64,7 @@ void Instruction_readInt(size_t id, enum Frame frame); void Instruction_readFloat(size_t id, enum Frame frame); -void Instruction_write(size_t id, enum Frame frame); +void Instruction_write(char * id, enum Frame frame); void Instruction_defvar(size_t id, enum Frame frame); diff --git a/src/compiler/codegen/Codegen.c b/src/compiler/codegen/Codegen.c index 81eac60..2f68741 100644 --- a/src/compiler/codegen/Codegen.c +++ b/src/compiler/codegen/Codegen.c @@ -59,6 +59,9 @@ void Codegen_generate(Codegen *codegen) { void __Codegen_generate(Codegen *codegen) { __Codegen_generatePreamble(); + // TODO: Shortcut, fix it later + Instruction_defvar_where("WRITE_TMP", FRAME_GLOBAL); + __Codegen_generateGlobalVariablesDeclarations(codegen); __Codegen_walkAST(codegen); } @@ -291,6 +294,8 @@ void __Codegen_evaluateFunctionDeclaration(Codegen *codegen, FunctionDeclaration // Process body __Codegen_evaluateBlock(codegen, functionDeclaration->body); + + codegen->frame = FRAME_GLOBAL; } @@ -429,8 +434,8 @@ __Codegen_resolveBuiltInFunction(Codegen *codegen, FunctionCallASTNode *function for (size_t i = 0; i < argumentList->arguments->size; ++i) { ArgumentASTNode *argument = Array_get(argumentList->arguments, i); __Codegen_evaluateStatement(codegen, (StatementASTNode *) argument->expression); - Instruction_pops(argument->label->id, codegen->frame); - Instruction_write(argument->label->id, codegen->frame); + Instruction_pops_where("WRITE_TMP", codegen->frame); + Instruction_write("WRITE_TMP", codegen->frame); } } break; case FUNCTION_INT_TO_DOUBLE: diff --git a/src/compiler/codegen/Instruction.c b/src/compiler/codegen/Instruction.c index bc65ced..c2700b8 100644 --- a/src/compiler/codegen/Instruction.c +++ b/src/compiler/codegen/Instruction.c @@ -90,8 +90,17 @@ void Instruction_pushs_bool(bool value) { fprintf(stdout, "PUSHS bool@%s\n", value ? "true" : "false"); } -void Instruction_write(size_t id, enum Frame frame){ - fprintf(stdout, "WRITE %s@$%lu\n", __Instruction_getFrame(frame), id); +void Instruction_write(char * id, enum Frame frame){ + fprintf(stdout, "WRITE %s@$%s\n", __Instruction_getFrame(frame), id); +} + +// TODO: This is very bad shortcut, should be fixed +void Instruction_pops_where(char * where, enum Frame frame) { + fprintf(stdout, "POPS %s@%s\n", __Instruction_getFrame(frame), where); +} + +void Instruction_defvar_where(char * where, enum Frame frame) { + fprintf(stdout, "DEFVAR %s@%s\n", __Instruction_getFrame(frame), where); } void Instruction_pushs_int(long value) {