It seems that I had for gotten to fix one of the signatures

This commit is contained in:
Meyer Simon
2025-04-17 11:14:38 -04:00
parent f8010f463b
commit b8f468c94a
2 changed files with 68 additions and 51 deletions

View File

@ -1,6 +1,5 @@
#include <stdio.h> #include <stdio.h>
#include "intermediate_code.h" #include "intermediate_code.h"
#include "symbol_table.h"
Instruction * begin; Instruction * begin;
Instruction * current; Instruction * current;
@ -11,7 +10,31 @@ char * temp = NULL;
// check if start is NULL if it is assign it to the start globle variable // 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. // 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){ static void emit_helper(void){
Instruction * inst = calloc(1, sizeof(*inst)); 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(); emit_helper();
current->opcode = op; current->opcode = op;
current->result = result; 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(); emit_helper();
current->opcode = E_LABEL; current->opcode = E_LABEL;
current->label = label; current->label = label;
return; return;
} }
void emit_jump(char* label){ void emit_jump(int label){
emit_helper(); emit_helper();
current->opcode = E_GOTO; current->opcode = E_GOTO;
current->label = label; current->label = label;
return; return;
} }
void emit_conditional_jump(int count, ...){
// it apears that I cant do what I want So we will have to deal with void emit_conditional_jump(Op condition, int label, ...){
// the limitations presented. // when this instruction is a conditional jump then the imput looks like (Op, int, TNodeOrConst *).
// arg_count is the number of args passed to the function // when the inst is a cond with a Relational operation then the input looks like (Op, int, TNodeOrConst *, TNodeOrConst *)
// 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
emit_helper(); emit_helper();
va_list argptr; va_list argptr;
va_start(argptr, count); va_start(argptr, label);
Op condition = va_arg(argptr, Op);
char * label = va_arg(argptr, char *);
current->opcode = condition; current->opcode = condition;
current->label = label; current->label = label;
TNodeOrConst * n1; TNodeOrConst * n1;
TableNode * n2; TNodeOrConst * n2;
switch (condition) { switch (condition) {
case E_IF_X_TRUE: case E_IF_X_FALSE: case E_IF_X_TRUE: case E_IF_X_FALSE:
n1 = va_arg(argptr, TNodeOrConst *); n1 = va_arg(argptr, TNodeOrConst *);
@ -97,21 +114,23 @@ void emit_conditional_jump(int count, ...){
break; break;
case E_LESSTHEN: case E_EQUALTO: case E_LESSTHEN: case E_EQUALTO:
n1 = va_arg(argptr, TNodeOrConst *); n1 = va_arg(argptr, TNodeOrConst *);
n2 = va_arg(argptr, TableNode *); n2 = va_arg(argptr, TNodeOrConst *);
current->operand1 = n1; current->operand1 = n1;
current->operand2 = n2; current->operand2 = n2;
break; break;
} }
va_end(argptr);
return; return;
} }
void emit_function_start(char * name){ void emit_function_start(int name){
emit_helper(); // actualy what is this? emit_helper();
current->opcode = E_LABEL; // I think this is right TODO: ask 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 // this is probabaly a func decleration
return; return;
} }
void emit_parameter(TNodeOrConst * param){ void emit_parameter(TNodeOrConst * param){
emit_helper(); emit_helper();
current->opcode = E_PARAM; current->opcode = E_PARAM;
@ -119,13 +138,10 @@ void emit_parameter(TNodeOrConst * param){
return; 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(); emit_helper();
current->opcode = E_CALL; current->opcode = E_CALL;
TNodeOrConst * count = calloc(1, sizeof(*count)); current->operand1 = tn_or_const(INTEGER, &param_count);
count->d = INTEGER;
count->rc_union->integer = param_count;
current->operand1 = count;
current->operand2 = name; current->operand2 = name;
current->result = result; current->result = result;
return; return;
@ -168,7 +184,7 @@ char * temp_var_gen(){
return ret; return ret;
} }
char * lable_gen(){ char * label_gen(){
char * ret = calloc( 9, sizeof(*ret)); char * ret = calloc( 9, sizeof(*ret));
sprintf(ret, "L_%d", label_count); sprintf(ret, "L_%d", label_count);
label_count++; label_count++;

View File

@ -2,7 +2,7 @@
// * Add Bison actions for arithmetic expressions: // * Add Bison actions for arithmetic expressions:
// - Addition: $$ = new_temp(); emit_binary_op($$, "ADD", $1, $3); // - Addition: $$ = new_temp(); emit_binary_op($$, "ADD", $1, $3);
// - Subtraction, multiplication, division, modulo // - Subtraction, multiplication, division, modulo
#include "symbol_table.h" #include "runner.h"
#include <stdarg.h> #include <stdarg.h>
// these are from page 364 // these are from page 364
@ -50,11 +50,11 @@ typedef union {
char character; char character;
void * address; void * address;
bool Boolean; bool Boolean;
} RegConstUnion; } TNConstUnion;
typedef struct { typedef struct {
Discriminant d; Discriminant d;
RegConstUnion * rc_union; TNConstUnion * tnc_union;
} TNodeOrConst; } TNodeOrConst;
typedef struct Instruction Instruction; typedef struct Instruction Instruction;
@ -62,9 +62,8 @@ typedef struct Instruction {
Op opcode; Op opcode;
TableNode * result; TableNode * result;
TNodeOrConst * operand1; TNodeOrConst * operand1;
TableNode * operand2; TNodeOrConst * operand2;
char * label; int label;
int index; int index;
Instruction * prev; Instruction * prev;
@ -79,7 +78,9 @@ 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_unary_op(Op op, TableNode * result, TNodeOrConst * arg);
void emit_assignment(TableNode * target, TNodeOrConst * source); void emit_assignment(TableNode * target, TNodeOrConst * source);
// TODO: Find out what these are suposed to do. Guess is create an entry in // 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 // * Implement instruction array storage for backpatching
void emit_label(char* label); void emit_label(int label);
void emit_jump(char* label); void emit_jump(int label);
void emit_conditional_jump(int count, ...); 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_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_return(TNodeOrConst * value);
void emit_reserve(char* result, char* type_name, int size); void emit_reserve(char* result, char* type_name, int size);
void emit_release(char* pointer); void emit_release(char* pointer);