From b8f468c94a698139eadd6fa98b8bad6fd0edebd0 Mon Sep 17 00:00:00 2001 From: Meyer Simon Date: Thu, 17 Apr 2025 11:14:38 -0400 Subject: [PATCH] It seems that I had for gotten to fix one of the signatures --- src/intermediate_code.c | 70 +++++++++++++++++++++++++---------------- src/intermediate_code.h | 49 +++++++++++++++-------------- 2 files changed, 68 insertions(+), 51 deletions(-) diff --git a/src/intermediate_code.c b/src/intermediate_code.c index cd967cc..6ac5429 100644 --- a/src/intermediate_code.c +++ b/src/intermediate_code.c @@ -1,6 +1,5 @@ #include #include "intermediate_code.h" -#include "symbol_table.h" Instruction * begin; Instruction * current; @@ -11,7 +10,31 @@ char * temp = NULL; // check if start is NULL if it is assign it to the start globle variable // otherwise make it next of current and set cur to your instruction. -TNodeOrConst * tn_or_const(Op op, RegConstUnion r); +TNodeOrConst * tn_or_const(Op op, void * tnc) { + TNodeOrConst * count = calloc(1, sizeof(*count)); + count->d = op; + switch (op) { + case NODE: + count->tnc_union->node = tnc; + break; + case ADDRESS: + count->tnc_union->address = tnc; + break; + case STRING: + count->tnc_union->string = tnc; + break; + case INTEGER: + count->tnc_union->integer = *(int*)tnc; + break; + case CHARACTER: + count->tnc_union->character = *(char*)tnc; + break; + case BOOLEAN: + count->tnc_union->Boolean = *(bool*)tnc; + break; + } + return count; +} static void emit_helper(void){ Instruction * inst = calloc(1, sizeof(*inst)); @@ -26,7 +49,7 @@ static void emit_helper(void){ } } -void emit_binary_op(Op op, TableNode * result, TNodeOrConst * arg1, TableNode * arg2){ +void emit_binary_op(Op op, TableNode * result, TNodeOrConst * arg1, TNodeOrConst * arg2){ emit_helper(); current->opcode = op; current->result = result; @@ -61,35 +84,29 @@ void emit_as_file(FILE * out_file, Instruction * instr_arr){ } -void emit_label(char* label){ +void emit_label(int label){ emit_helper(); current->opcode = E_LABEL; current->label = label; return; } -void emit_jump(char* label){ +void emit_jump(int label){ emit_helper(); current->opcode = E_GOTO; current->label = label; return; } -void emit_conditional_jump(int count, ...){ - // it apears that I cant do what I want So we will have to deal with - // the limitations presented. - // arg_count is the number of args passed to the function - // from there the sequence is Op, char * , TableNode * , and an optional - // extra TableNode *. - // when this instruction is a conditional jump then ... is 1 - // when the inst is a cond with a Relational op then ... is 2 + +void emit_conditional_jump(Op condition, int label, ...){ + // when this instruction is a conditional jump then the imput looks like (Op, int, TNodeOrConst *). + // when the inst is a cond with a Relational operation then the input looks like (Op, int, TNodeOrConst *, TNodeOrConst *) emit_helper(); va_list argptr; - va_start(argptr, count); - Op condition = va_arg(argptr, Op); - char * label = va_arg(argptr, char *); + va_start(argptr, label); current->opcode = condition; current->label = label; TNodeOrConst * n1; - TableNode * n2; + TNodeOrConst * n2; switch (condition) { case E_IF_X_TRUE: case E_IF_X_FALSE: n1 = va_arg(argptr, TNodeOrConst *); @@ -97,21 +114,23 @@ void emit_conditional_jump(int count, ...){ break; case E_LESSTHEN: case E_EQUALTO: n1 = va_arg(argptr, TNodeOrConst *); - n2 = va_arg(argptr, TableNode *); + n2 = va_arg(argptr, TNodeOrConst *); current->operand1 = n1; current->operand2 = n2; break; } + va_end(argptr); return; } -void emit_function_start(char * name){ - emit_helper(); // actualy what is this? +void emit_function_start(int name){ + emit_helper(); current->opcode = E_LABEL; // I think this is right TODO: ask - current->label = name; // wait what is a function start + current->label = name; // this is probabaly a func decleration return; } + void emit_parameter(TNodeOrConst * param){ emit_helper(); current->opcode = E_PARAM; @@ -119,13 +138,10 @@ void emit_parameter(TNodeOrConst * param){ return; } -void emit_function_call(TableNode * result, int param_count, TableNode * name){ +void emit_function_call(TableNode * result, int param_count, TNodeOrConst * name){ emit_helper(); current->opcode = E_CALL; - TNodeOrConst * count = calloc(1, sizeof(*count)); - count->d = INTEGER; - count->rc_union->integer = param_count; - current->operand1 = count; + current->operand1 = tn_or_const(INTEGER, ¶m_count); current->operand2 = name; current->result = result; return; @@ -168,7 +184,7 @@ char * temp_var_gen(){ return ret; } -char * lable_gen(){ +char * label_gen(){ char * ret = calloc( 9, sizeof(*ret)); sprintf(ret, "L_%d", label_count); label_count++; diff --git a/src/intermediate_code.h b/src/intermediate_code.h index 8b8edb5..fc3cdc4 100644 --- a/src/intermediate_code.h +++ b/src/intermediate_code.h @@ -2,7 +2,7 @@ // * Add Bison actions for arithmetic expressions: // - Addition: $$ = new_temp(); emit_binary_op($$, "ADD", $1, $3); // - Subtraction, multiplication, division, modulo -#include "symbol_table.h" +#include "runner.h" #include // these are from page 364 @@ -44,42 +44,43 @@ typedef enum { } Discriminant; typedef union { - TableNode * node; - int integer; - char * string; - char character; - void * address; - bool Boolean; -} RegConstUnion; + TableNode * node; + int integer; + char * string; + char character; + void * address; + bool Boolean; +} TNConstUnion; typedef struct { Discriminant d; - RegConstUnion * rc_union; + TNConstUnion * tnc_union; } TNodeOrConst; typedef struct Instruction Instruction; typedef struct Instruction { Op opcode; - TableNode * result; + TableNode * result; TNodeOrConst * operand1; - TableNode * operand2; - char * label; + TNodeOrConst * operand2; + int label; + int index; - int index; - - Instruction * prev; - Instruction * next; + Instruction * prev; + Instruction * next; } Instruction; -extern Instruction * begin; -extern Instruction * current; +extern Instruction * begin; +extern Instruction * current; int temp_count = 0; int label_count = 0; bool code_gen = true; -void emit_binary_op(Op op, TableNode * result, TNodeOrConst * arg1, TableNode * arg2); +TNodeOrConst * tn_or_const(Op op, void * tnc); + +void emit_binary_op(Op op, TableNode * result, TNodeOrConst * arg1, TNodeOrConst * arg2); void emit_unary_op(Op op, TableNode * result, TNodeOrConst * arg); void emit_assignment(TableNode * target, TNodeOrConst * source); // TODO: Find out what these are suposed to do. Guess is create an entry in @@ -93,13 +94,13 @@ void emit_as_file(FILE * out_file, Instruction * instr_arr); // * Implement instruction array storage for backpatching -void emit_label(char* label); -void emit_jump(char* label); -void emit_conditional_jump(int count, ...); +void emit_label(int label); +void emit_jump(int label); +void emit_conditional_jump(Op condition, int label, ...); -void emit_function_start(char* name); +void emit_function_start(int name); void emit_parameter(TNodeOrConst * param); -void emit_function_call(TNodeOrConst * result, TNodeOrConst * name); +void emit_function_call(TableNode * result, int param_count, TNodeOrConst * name); void emit_return(TNodeOrConst * value); void emit_reserve(char* result, char* type_name, int size); void emit_release(char* pointer);