🤯🤯🤯🤯🤯🤯🤯🤯🤯

This commit is contained in:
Scarlett
2025-05-04 23:54:42 -04:00
parent 8b0d191409
commit 0fc796aa25
32 changed files with 867 additions and 1021 deletions

View File

@ -3,87 +3,86 @@
#include "codegen.h"
int generate(){
int generate() {
offset = 0;
currentsp = 0;
Instruction *i = begin;
// temporary
fprintf(cg_flag, ".globl entry\n");
while (i != NULL) {
switch(getOp(i)) {
case E_LABEL:
generateLabel(i);
break;
case E_ADD:
generateAdd(i);
break;
case E_SUB:
generateSub(i);
break;
case E_MUL:
generateMult(i);
break;
case E_DIV:
generateDiv(i);
break;
case E_MOD:
generateMod(i);
break;
case E_OR:
generateOr(i);
break;
case E_AND:
generateAnd(i);
break;
case E_NEG:
generateNeg(i);
break;
case E_NOT:
generateNot(i);
break;
case E_ASSIGN:
generateAssign(i);
break;
case E_GOTO:
generateGoto(i);
break;
case E_IF_X_TRUE:
generateIfTrue(i);
break;
case E_IF_X_FALSE:
generateIfFalse(i);
break;
case E_LESS_THAN:
generateLessThan(i);
break;
case E_EQUAL_TO:
generateEqualTo(i);
break;
case E_CALL:
generateCall(i);
break;
case E_PARAM:
generateParam(i);
break;
case E_RETURN:
generateReturn(i);
break;
case E_INDEX_COPY_RIGHT:
generateCopyRight(i);
break;
case E_INDEX_COPY_LEFT:
generateCopyLeft(i);
break;
case E_ADDRESS_OF:
generateAddressOf(i);
break;
case E_FUNC_START:
generateFunctionStart(i);
break;
default:
;
switch (getOp(i)) {
case E_LABEL:
generateLabel(i);
break;
case E_ADD:
generateAdd(i);
break;
case E_SUB:
generateSub(i);
break;
case E_MUL:
generateMult(i);
break;
case E_DIV:
generateDiv(i);
break;
case E_MOD:
generateMod(i);
break;
case E_OR:
generateOr(i);
break;
case E_AND:
generateAnd(i);
break;
case E_NEG:
generateNeg(i);
break;
case E_NOT:
generateNot(i);
break;
case E_ASSIGN:
generateAssign(i);
break;
case E_GOTO:
generateGoto(i);
break;
case E_IF_X_TRUE:
generateIfTrue(i);
break;
case E_IF_X_FALSE:
generateIfFalse(i);
break;
case E_LESS_THAN:
generateLessThan(i);
break;
case E_EQUAL_TO:
generateEqualTo(i);
break;
case E_CALL:
generateCall(i);
break;
case E_PARAM:
generateParam(i);
break;
case E_RETURN:
generateReturn(i);
break;
case E_INDEX_COPY_RIGHT:
generateCopyRight(i);
break;
case E_INDEX_COPY_LEFT:
generateCopyLeft(i);
break;
case E_ADDRESS_OF:
generateAddressOf(i);
break;
case E_FUNC_START:
generateFunctionStart(i);
break;
default:;
}
i = i->next;
}
@ -133,14 +132,13 @@ CGNode *addCG(TableNode *tn, int sp) {
return cg;
}
int generateLabel(Instruction *inst) {
if (inst == NULL) {
return -1;
}
fprintf(cg_flag, ".L%d:\n", getLabel(inst));
return 0;
}
int generateAdd(Instruction *inst) {
@ -162,7 +160,6 @@ int generateAdd(Instruction *inst) {
cg = addCG(getResult(inst), offset);
}
CGNode *op1CG = findCG(getTN(op1));
CGNode *op2CG = findCG(getTN(op2));
if (op1CG == NULL) {
@ -181,7 +178,7 @@ int generateAdd(Instruction *inst) {
}
int generateSub(Instruction *instruction) {
/*
/*
Both immediate:
One immediate:
Neither immediate:
@ -217,8 +214,8 @@ int generateSub(Instruction *instruction) {
return 0;
}
int generateMult(Instruction *inst){
/*
int generateMult(Instruction *inst) {
/*
Both immediate:
One immediate:
Neither immediate:
@ -254,7 +251,7 @@ int generateMult(Instruction *inst){
}
int generateDiv(Instruction *inst) {
/*
/*
Both immediate:
One immediate:
Neither immediate:
@ -284,10 +281,10 @@ int generateDiv(Instruction *inst) {
return -1;
}
fprintf(cg_flag, "\tmovl\t%d(%%rbp), %%eax\t#division start\n", getAddress(op1CG)); //moves dividend into eax
fprintf(cg_flag, "\tcltd\n"); //sign extends the dividend in eax
fprintf(cg_flag, "\tidivl\t%d(%%rbp)\n", getAddress(op2CG));//divides edx by value accessed from stack
fprintf(cg_flag, "\tmovl\t%%eax, %d(%%rbp)\t#division end\n", getAddress(cg)); //stores result
fprintf(cg_flag, "\tmovl\t%d(%%rbp), %%eax\t#division start\n", getAddress(op1CG)); //moves dividend into eax
fprintf(cg_flag, "\tcltd\n"); //sign extends the dividend in eax
fprintf(cg_flag, "\tidivl\t%d(%%rbp)\n", getAddress(op2CG)); //divides edx by value accessed from stack
fprintf(cg_flag, "\tmovl\t%%eax, %d(%%rbp)\t#division end\n", getAddress(cg)); //stores result
return 0;
}
@ -321,15 +318,15 @@ int generateMod(Instruction *inst) {
return -1;
}
fprintf(cg_flag, "\tmovl\t%d(%%rbp), %%eax\t#mod start\n", getAddress(op1CG)); //moves dividend into eax
fprintf(cg_flag, "\tcltd\n"); //sign extends the dividend in eax
fprintf(cg_flag, "\tidivl\t%d(%%rbp)\n", getAddress(op2CG));//divides edx by value accessed from stack
fprintf(cg_flag, "\tmovl\t%%edx, %d(%%rbp)\t#mod end\n", getAddress(cg)); //stores result from edx (remainder)
fprintf(cg_flag, "\tmovl\t%d(%%rbp), %%eax\t#mod start\n", getAddress(op1CG)); //moves dividend into eax
fprintf(cg_flag, "\tcltd\n"); //sign extends the dividend in eax
fprintf(cg_flag, "\tidivl\t%d(%%rbp)\n", getAddress(op2CG)); //divides edx by value accessed from stack
fprintf(cg_flag, "\tmovl\t%%edx, %d(%%rbp)\t#mod end\n", getAddress(cg)); //stores result from edx (remainder)
return 0;
}
int generateOr(Instruction *inst) {
/*
/*
Both immediate:
One immediate:
Neither immediate:
@ -376,7 +373,7 @@ int generateOr(Instruction *inst) {
fprintf(cg_flag, ".L%dor4:\n", label);
fprintf(cg_flag, "\tmovb\t%%al, %d(%%rbp)\n", getAddress(cg));
fprintf(cg_flag, "\tandb\t$1, %d(%%rbp)\t#or end\n", getAddress(cg)); //stores result
fprintf(cg_flag, "\tandb\t$1, %d(%%rbp)\t#or end\n", getAddress(cg)); //stores result
return 0;
}
@ -410,7 +407,7 @@ int generateAnd(Instruction *inst) {
printdebug("generateAnd failed, %s is not initialized/in CG", getName(getTN(op2)));
return -1;
}
int label = label_gen();
int label = label_gen();
fprintf(cg_flag, "\tcmpl\t$0, %d(%%rbp)\t#start and\n", getAddress(op1CG));
fprintf(cg_flag, "\tje\t.L%dor2\n", label);
@ -426,7 +423,7 @@ int generateAnd(Instruction *inst) {
fprintf(cg_flag, ".L%dor3:\n", label);
fprintf(cg_flag, "\tmovb\t%%al, %d(%%rbp)\n", getAddress(cg));
fprintf(cg_flag, "\tandb\t$1, %d(%%rbp)\t#and end\n", getAddress(cg)); //stores result
fprintf(cg_flag, "\tandb\t$1, %d(%%rbp)\t#and end\n", getAddress(cg)); //stores result
return 0;
}
int generateNeg(Instruction *inst) {
@ -486,7 +483,6 @@ int generateAssign(Instruction *inst) {
TNodeOrConst *op1 = getOperand1(inst);
CGNode *cg = findCG(getResult(inst));
if (op1 == NULL) {
printdebug("generateAssign failed, NULL operand");
return -1;
@ -514,7 +510,7 @@ int generateAssign(Instruction *inst) {
return 0;
}
int generateGoto(Instruction *inst){
int generateGoto(Instruction *inst) {
return -1;
}
@ -522,17 +518,17 @@ int generateCondGoto(Instruction *inst) {
return -1;
}
int generateIfTrue(Instruction *inst){
int generateIfTrue(Instruction *inst) {
return -1;
// might just be a goto for where to go if something is true, or returning if something is true, or checking if true and writing goto if thats the case
}
int generateIfFalse(Instruction *inst){
int generateIfFalse(Instruction *inst) {
return -1;
}
int generateLessThan(Instruction *inst){
/*
int generateLessThan(Instruction *inst) {
/*
Both immediate:
One immediate:
Neither immediate:
@ -569,8 +565,8 @@ int generateLessThan(Instruction *inst){
return 0;
}
int generateEqualTo(Instruction *inst){
/*
int generateEqualTo(Instruction *inst) {
/*
Both immediate:
One immediate:
Neither immediate:
@ -606,8 +602,7 @@ int generateEqualTo(Instruction *inst){
fprintf(cg_flag, "\tmovb\t%%al, %d(%%rbp)\t#equal to end\n", getAddress(cg));
return 0;
}
int generateCall(Instruction *inst){
int generateCall(Instruction *inst) {
TNodeOrConst *op1 = getOperand1(inst);
TNodeOrConst *op2 = getOperand2(inst);
if (op1 == NULL) {
@ -630,7 +625,6 @@ int generateCall(Instruction *inst){
// return -1;
//}
fprintf(cg_flag, "\tcall %s\n", getName(getTN(op1)));
//now for the return
@ -643,22 +637,20 @@ int generateCall(Instruction *inst){
fprintf(cg_flag, "\tmovl\t%%eax, %d(%%rbp)\t#store return from call\n", getAddress(cg));
return 0;
}
int generateReturn(Instruction *inst){
int generateReturn(Instruction *inst) {
TNodeOrConst *op1 = getOperand1(inst);
CGNode *cg;
if (op1 == NULL) {
printdebug("generateReturn failed, NULL operand");
return -1;
}
cg = findCG(getTN(op1));
if (cg == NULL) {
printdebug("generateReturn failed, trying to return %s not in CGList", getName(getTN(op1)));
return -1;
return -1;
}
fprintf(cg_flag, "\tmovl\t%d(%%rbp), %%eax\t#return %s\n", getAddress(cg), getName(getTN(op1)));
@ -666,38 +658,38 @@ int generateReturn(Instruction *inst){
fprintf(cg_flag, "\tret\n");
return 0;
}
int generateCopyRight(Instruction *inst){
int generateCopyRight(Instruction *inst) {
return -1;
}
int generateCopyLeft(Instruction *inst){
int generateCopyLeft(Instruction *inst) {
return -1;
}
int generateAddressOf(Instruction *inst){
int generateAddressOf(Instruction *inst) {
return -1;
}
int generateParam(Instruction *inst){
int generateParam(Instruction *inst) {
TNodeOrConst *op1 = getOperand1(inst);
if (op1 == NULL) {
printdebug("generateParam failed, NULL operand");
return -1;
}
CGNode *op1CG = findCG(getTN(op1));
if (op1CG == NULL) {
printdebug("generateParam failed, %s is not in CGlist", getName(getTN(op1)));
return -1;
}
fprintf(cg_flag, "\tmovl\t%d(%%rbp), %%edi\t#adding param start\n", getAddress(op1CG));
fprintf(cg_flag, "\tmovl\t%d(%%rbp), %%edi\t#adding param start\n", getAddress(op1CG));
return 0;
}
int generateFunctionStart(Instruction *inst) {
currentsp = offset;
TableNode *funDecTN = getResult(inst);
if (funDecTN == NULL) {
printdebug("generateFunctionStart failed, NULL tablenode");
return -1;
@ -707,27 +699,27 @@ int generateFunctionStart(Instruction *inst) {
fprintf(cg_flag, "%s:\n", getName(funDecTN));
fprintf(cg_flag, "\tpushq\t%%rbp\n");
fprintf(cg_flag, "\tmovq\t%%rsp, %%rbp\n");
fprintf(cg_flag, "\tsubq\t$%d, %%rsp\n", 128); // [CHANGE ME] 128 is a placeholder for the stack size
fprintf(cg_flag, "\tsubq\t$%d, %%rsp\n", 128); // [CHANGE ME] 128 is a placeholder for the stack size
//now we need to add the CGs of nodes to the CG list by doing assign from the
// have function declararation node
//declaration ->getType: if record, go through each element and load param from stack and store to correct tn cg
// if not, go get one element of type of param
//declaration ->getType->getDefinitionScope?: go through first n entries to get table nodes for params
TableNode *paramTN = getParameter(getTypeEntry(funDecTN));
SymbolTable *st = getFunScope(funDecTN);
int paramOffset = 16;
TableNode *tnToAdd = getFirstEntry(st);
if (getAdInfoType(paramTN) != TYPE_RECORD_TYPE) {
CGNode *paramCG = addCG(tnToAdd, offset);
fprintf(cg_flag, "\tmovl\t%d(%%rbp), %%eax\t#FunctionStart1Param start\n", paramOffset);
fprintf(cg_flag, "\tmovl\t%d(%%rbp), %%eax\t#FunctionStart1Param start\n", paramOffset);
fprintf(cg_flag, "\tmovl\t%%eax, %d(%%rbp)\t#FunctionStart1param end\n", getAddress(paramCG));
} else {
int numParams = getRecLength(paramTN);
for (int i = 0; i < numParams; i++) {
CGNode *paramCG = addCG(tnToAdd, offset);
fprintf(cg_flag, "\tmovl\t%d(%%rbp), %%eax\t#FunctionStart1Param start\n", paramOffset);
CGNode *paramCG = addCG(tnToAdd, offset);
fprintf(cg_flag, "\tmovl\t%d(%%rbp), %%eax\t#FunctionStart1Param start\n", paramOffset);
fprintf(cg_flag, "\tmovl\t%%eax, %d(%%rbp)\t#FunctionStart1param end\n", getAddress(paramCG));
paramOffset = getPrimSize(getTypeEntry(tnToAdd));
tnToAdd = getNextEntry(tnToAdd);

View File

@ -8,7 +8,7 @@
#include "../src/symbol_table.h"
extern FILE *asc_flag;
extern bool tc_flag;
extern void insert_code_line(char * error_message, int line_number);
extern void insert_code_line(char *error_message, int line_number);
extern bool contains_errors;
typedef enum {
@ -32,6 +32,6 @@ int offset;
int currentsp;
CGNode *cgList;
Stack* stack;
Stack* TrueList;
Stack* FalseList;
Stack *stack;
Stack *TrueList;
Stack *FalseList;

View File

@ -138,7 +138,7 @@ include_list:
include_statement:
INCLUDE C_STRING
INCLUDE C_STRING
;

File diff suppressed because it is too large Load Diff

View File

@ -16,20 +16,20 @@ typedef struct Stack Stack;
typedef struct __Node __Node;
typedef struct __Node {
void * v;
__Node * next;
void *v;
__Node *next;
} __Node;
typedef struct Stack {
__Node * n;
int w;
int size;
__Node *n;
int w;
int size;
} Stack;
Stack * S_Init();
Stack *S_Init();
void S_Free(Stack *s);
void S_Push(Stack * s, void *v, int i);
void * S_Pop(Stack *s);
void * S_Peek(Stack *s);
void S_Push(Stack *s, void *v, int i);
void *S_Pop(Stack *s);
void *S_Peek(Stack *s);
bool S_IsEmpty(Stack *s);
int S_Size(Stack *s);
void S_Merge(Stack *list);
@ -39,137 +39,130 @@ typedef union TNConstUnion TNConstUnion;
typedef struct Instruction Instruction;
typedef struct TNodeOrConst TNodeOrConst;
// these are from page 364
typedef enum { // these are from page 364
E_LABEL = 10000, // this is not in the book
E_FUNC_START,
E_ADD, // 1 from the list
E_SUB, // 1
E_MUL, // 1
E_DIV, // 1
E_MOD, // 1 TODO: Please change to REM
E_OR, // 1
E_AND, // 1
E_NEG, // 2
E_NOT, // 2
E_ASSIGN, // 3
E_GOTO, // 4
E_COND_GOTO, // 5 I don't thik I need this because we could just follow the < or the = and just assume that it's a cond got
E_IF_X_TRUE, // 5
E_IF_X_FALSE, // 5
E_LESS_THAN, // 6 rule 1 + 5
E_EQUAL_TO, // 6 rule 1 + 5
E_CALL, // 7
E_PARAM, // 7
E_RETURN, // 7
E_INDEX_COPY_RIGHT, // 8 this is x = y[i]
E_INDEX_COPY_LEFT, // 8 x[i] = y
E_ADDRESS_OF, // 9 x = &y
E_DEREF_RIGHT, // 9 x = *y
E_DEREF_LEFT // 9 x* = y
typedef enum { // these are from page 364
E_LABEL = 10000, // this is not in the book
E_FUNC_START,
E_ADD, // 1 from the list
E_SUB, // 1
E_MUL, // 1
E_DIV, // 1
E_MOD, // 1 TODO: Please change to REM
E_OR, // 1
E_AND, // 1
E_NEG, // 2
E_NOT, // 2
E_ASSIGN, // 3
E_GOTO, // 4
E_COND_GOTO, // 5 I don't thik I need this because we could just follow the < or the = and just assume that it's a cond got
E_IF_X_TRUE, // 5
E_IF_X_FALSE, // 5
E_LESS_THAN, // 6 rule 1 + 5
E_EQUAL_TO, // 6 rule 1 + 5
E_CALL, // 7
E_PARAM, // 7
E_RETURN, // 7
E_INDEX_COPY_RIGHT, // 8 this is x = y[i]
E_INDEX_COPY_LEFT, // 8 x[i] = y
E_ADDRESS_OF, // 9 x = &y
E_DEREF_RIGHT, // 9 x = *y
E_DEREF_LEFT // 9 x* = y
} Op;
typedef enum {
NODE = 11000, // TableNode
INTEGER, // int
STRING, // char *
CHARACTER, // char
ADDRESS, // void *
BOOLEAN // bool
NODE = 11000, // TableNode
INTEGER, // int
STRING, // char *
CHARACTER, // char
ADDRESS, // void *
BOOLEAN // bool
} Discriminant;
typedef union TNConstUnion {
TableNode * node;
int integer;
char * string;
char character;
void * address;
bool Boolean;
TableNode *node;
int integer;
char *string;
char character;
void *address;
bool Boolean;
} TNConstUnion;
typedef struct TNodeOrConst {
Discriminant d;
TNConstUnion * tnc_union;
Discriminant d;
TNConstUnion *tnc_union;
} TNodeOrConst;
typedef struct Instruction {
Op opcode;
TableNode * result;
TNodeOrConst * operand1;
TNodeOrConst * operand2;
int label;
int index;
Op opcode;
TableNode *result;
TNodeOrConst *operand1;
TNodeOrConst *operand2;
int label;
int index;
Instruction * prev;
Instruction * next;
Instruction *prev;
Instruction *next;
} Instruction;
// NOTE We are not using this We are using the Stack api
typedef struct TFList {
Instruction * i;
TFList * next;
Instruction *i;
TFList *next;
} TFList;
// TFList * make_list(Instruction * i);
// - makelist(i) function to create instruction lists
// - makelist(i) function to create instruction lists
// void merge(TFList * l1, TFList * l2);
// - merge(p1,p2) function to concatenate lists
// - merge(p1,p2) function to concatenate lists
// void backpatch(TFList * l, int label);
// - backpatch(p,i) function to fill in jump targets
// - backpatch(p,i) function to fill in jump targets
// void bp_temp(int n);
extern Instruction * begin;
extern Instruction * current;
extern Instruction *begin;
extern Instruction *current;
extern int label_count;
extern bool code_gen;
extern FILE * ir_flag;
extern FILE *ir_flag;
TNodeOrConst * tn_or_const(Discriminant , void * );
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);
void emit_as_file(FILE * out_file, Instruction * instr_arr);
TNodeOrConst *tn_or_const(Discriminant, void *);
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);
void emit_as_file(FILE *out_file, Instruction *instr_arr);
void emit_label(int label);
void emit_jump(int label);
void emit_conditional_jump(Op condition, int label, ...);
void emit_function_start(TableNode* name);
void emit_parameter(TNodeOrConst * param);
void emit_function_call(TableNode * result, int param_count, TNodeOrConst * name);
void emit_return(TNodeOrConst * value);
void emit_reserve(TableNode * result, TNodeOrConst * size);
void emit_release(TableNode * pointer);
void emit_field_access(char* result, char* record, char* field);
void emit_array_access(Op op, TableNode * result, TNodeOrConst * array, TNodeOrConst * index);
void emit_bounds_check(TNodeOrConst * index, TNodeOrConst * arr);
void emit_function_start(TableNode *name);
void emit_parameter(TNodeOrConst *param);
void emit_function_call(TableNode *result, int param_count, TNodeOrConst *name);
void emit_return(TNodeOrConst *value);
void emit_reserve(TableNode *result, TNodeOrConst *size);
void emit_release(TableNode *pointer);
void emit_field_access(char *result, char *record, char *field);
void emit_array_access(Op op, TableNode *result, TNodeOrConst *array, TNodeOrConst *index);
void emit_bounds_check(TNodeOrConst *index, TNodeOrConst *arr);
void emit_goto(int i);
void emit_detach();
void emit_push_all(Stack * s);
void emit_push_all(Stack *s);
int getLabel(Instruction *i);
TableNode *getTN(TNodeOrConst *tnc);
int getConst(TNodeOrConst *tnc);
int getLabel(Instruction * i);
TableNode* getTN(TNodeOrConst* tnc);
int getConst(TNodeOrConst* tnc);
TNodeOrConst * getOperand1(Instruction * i);
TNodeOrConst * getOperand2(Instruction * i);
TableNode * getResult(Instruction * i);
Op getOp(Instruction * i);
int getLabel(Instruction * i);
int get_index(Instruction * i);
void set_label(Instruction * i, int label);
bool isConst(TNodeOrConst * tnc);
TNodeOrConst *getOperand1(Instruction *i);
TNodeOrConst *getOperand2(Instruction *i);
TableNode *getResult(Instruction *i);
Op getOp(Instruction *i);
int getLabel(Instruction *i);
int get_index(Instruction *i);
void set_label(Instruction *i, int label);
bool isConst(TNodeOrConst *tnc);
int label_gen();
void backpatch(Stack *s, int l);
void emit_backpatch(Stack *s, int l);
extern int offset;
extern int currentsp;
extern CGNode* cgList;
extern CGNode *cgList;

View File

@ -25,10 +25,11 @@ int line_number = 1;
int column_number = 1;
int yycolumn = 1;
#define YY_USER_ACTION { \
#define YY_USER_ACTION \
{ \
yylloc.first_line = yylineno; \
yylloc.last_line = yylineno; \
yylloc.first_column = yycolumn; \
yylloc.last_column = yycolumn + yyleng - 1; \
yycolumn += yyleng; \
}
}

View File

@ -103,7 +103,6 @@ int run(FILE *alpha) {
int token;
top = cur = init(CreateScope(NULL, 1, 1));
// If file is not found
if (alpha == NULL) {
fprintf(stderr, "INPUT FILE NOT FOUND\n");

View File

@ -59,8 +59,8 @@ bool DEBUG = false;
int no_flag = 0;
int arg;
bool contains_errors = false;
char * cg_name;
char * ir_name;
char *cg_name;
char *ir_name;
TableNode *funprime;
TableNode *arrayprim;
@ -73,9 +73,9 @@ TableNode *recprime;
TableNode *funtypeprime;
TableNode *undefined;
extern Instruction *begin;
extern Stack* stack;
extern Stack* TrueList;
extern Stack* FalseList;
extern Stack *stack;
extern Stack *TrueList;
extern Stack *FalseList;
int main(int argc, char *argv[]);
int check_flag(char *arg, char *alpha);
@ -112,6 +112,6 @@ typedef struct CodeLine {
CodeLine *code_head;
char *file_read_line(FILE *fp);
void insert_code_line(char * error_message, int line_number);
void insert_code_line(char *error_message, int line_number);
void append_code_line(CodeLine *code_line);
void print_code_lines();

View File

@ -116,15 +116,15 @@ int getPrimSize(TableNode *definition) {
"Invalid.");
return -1;
}
if(getAdInfoType(definition) == TYPE_ARRAY_TYPE){
if (getAdInfoType(definition) == TYPE_ARRAY_TYPE) {
//special case to return size for reference to an array
return 8;
}
if(getAdInfoType(definition) == TYPE_FUNCTION_TYPE){
if (getAdInfoType(definition) == TYPE_FUNCTION_TYPE) {
//special case to return size for reference to a function
return 8;
}
if(getAdInfoType(definition) == TYPE_RECORD_TYPE){
if (getAdInfoType(definition) == TYPE_RECORD_TYPE) {
//special case to return size for reference to a record
return getRecTotal(definition);
}
@ -469,7 +469,7 @@ int getRecSize(SymbolTable *tn) {
// multiple inputs Below function also has the line number where the function is
// first defined
AdInfo *CreateFunctionDeclarationInfo(int line, bool asorregular, SymbolTable *scope) {
AdInfo *info = (AdInfo *)calloc(1,sizeof(AdInfo));
AdInfo *info = (AdInfo *)calloc(1, sizeof(AdInfo));
info->FunDecAdInfo = (function_declaration_info *)malloc(
sizeof(function_declaration_info));
info->FunDecAdInfo->startlinenumber = line;
@ -561,12 +561,12 @@ SymbolTable *getFunScope(TableNode *definition) {
"node has NULL additionalinfo. Invalid.");
return NULL;
}
if(definition->additionalinfo->FunDecAdInfo == NULL) {
if (definition->additionalinfo->FunDecAdInfo == NULL) {
printdebug(
"node has NULL additionalinfo. Invalid.");
return NULL;
}
if(definition->additionalinfo->FunDecAdInfo->scope == NULL) {
if (definition->additionalinfo->FunDecAdInfo->scope == NULL) {
printdebug(
"node has no scope initialized.");
return NULL;
@ -587,17 +587,17 @@ TableNode *setFunScope(TableNode *tn, SymbolTable *scope) {
"invalid");
return undefined;
}
if(tn->additionalinfo == NULL) {
if (tn->additionalinfo == NULL) {
printdebug(
"node has NULL additionalinfo. Invalid.");
return undefined;
}
if(tn->additionalinfo->FunDecAdInfo == NULL) {
if (tn->additionalinfo->FunDecAdInfo == NULL) {
printdebug(
"node has NULL additionalinfo. Invalid.");
return undefined;
}
if(scope == NULL) {
if (scope == NULL) {
printdebug(
"passed in an empty scope.");
return undefined;
@ -705,7 +705,7 @@ TableNode *getReturn(TableNode *definition) {
"node has NULL additionalinfo. Invalid.");
return undefined;
}
printdebug("function:%s with return type %s\n",getName(definition),getName(definition->additionalinfo->FunTypeAdInfo->returntype));
printdebug("function:%s with return type %s\n", getName(definition), getName(definition->additionalinfo->FunTypeAdInfo->returntype));
return definition->additionalinfo->FunTypeAdInfo->returntype;
}
@ -862,10 +862,10 @@ SymbolTable *init(SymbolTable *start) {
chara->additionalinfo = CreatePrimitiveInfo(SIZE_CHAR);
stri->additionalinfo = CreateArrayInfo(1, chara);
boo->additionalinfo = CreatePrimitiveInfo(SIZE_BOOL);
reserve->additionalinfo = CreateFunctionDeclarationInfo(0, false,NULL);
reserve->additionalinfo = CreateFunctionDeclarationInfo(0, false, NULL);
reservetype->additionalinfo = CreateFunctionTypeInfo(integ, addr);
releasetype->additionalinfo = CreateFunctionTypeInfo(addr, integ);
release->additionalinfo = CreateFunctionDeclarationInfo(0, false,NULL);
release->additionalinfo = CreateFunctionDeclarationInfo(0, false, NULL);
integ->tag = TYPE_PRIMITIVE_TYPE; // explicitly set the type for integ
addr->tag = TYPE_PRIMITIVE_TYPE; // explicitly set the type for addr
@ -1031,7 +1031,7 @@ TableNode *CreateEntry(SymbolTable *table, int tag, TableNode *typeOf, char *id,
return NULL;
}
*/
if((id != NULL) && table_lookup(cur,id)!=undefined){
if ((id != NULL) && table_lookup(cur, id) != undefined) {
printdebug("This name is already defined in the current scope");
//throw_error(ERROR_TYPE, "Already defined.");
return undefined;
@ -1062,7 +1062,7 @@ TableNode *CreateEntry(SymbolTable *table, int tag, TableNode *typeOf, char *id,
printdebug("[CreateEntry] Adding %s to the symbol table", id);
return newEntry;
} else {
TableNode*oldEntry = table->entries;
TableNode *oldEntry = table->entries;
while (oldEntry->next != NULL) {
oldEntry = oldEntry->next;
}
@ -1309,7 +1309,7 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr) {
}
for (; entry != NULL; entry = getNextEntry(entry)) {
if((getName(entry)[0] == '$' || getName(entry)[0] == '&') && ir_flag == NULL){
if ((getName(entry)[0] == '$' || getName(entry)[0] == '&') && ir_flag == NULL) {
continue;
}
if (getAdInfoType(entry) == TYPE_ARRAY_TYPE) {
@ -1391,11 +1391,11 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr) {
if (getAdInfoType(entry) == TYPE_FUNCTION_DECLARATION) {
char *functiontype = (char *)malloc(100);
sprintf(functiontype, " %s", getName(getTypeEntry(entry)));
char* functionScope = (char *)malloc(100);
if(getLine(getFunScope(entry)) < 1){
char *functionScope = (char *)malloc(100);
if (getLine(getFunScope(entry)) < 1) {
sprintf(functionScope, " Function not defined before runtime");
}else{
sprintf(functionScope, " Function Definition that starts at line %d",getLine(getFunScope(entry)));
} else {
sprintf(functionScope, " Function Definition that starts at line %d", getLine(getFunScope(entry)));
}
if (parentScopeNum == 0) {
st_fprint(file_ptr, getName(entry), currentScopeNum, -100, functiontype, functionScope);
@ -1545,7 +1545,13 @@ SymbolTable *getParent(SymbolTable *st) {
ListOfTable *getChildren(SymbolTable *st) { return st->Children_Scope; }
SymbolTable *getFirstChild(ListOfTable *lt) { return lt->table; }
ListOfTable *getRestOfChildren(ListOfTable *lt) { return lt->next; }
TableNode *getFirstEntry(SymbolTable *st) { return st->entries; }
TableNode *getFirstEntry(SymbolTable *st) {
if (st == NULL || st->entries == NULL) {
printdebug("passed a NULL symbol table to getFirstEntry");
return undefined;
}
return st->entries;
}
// Segfaults when passed an invalid table node!
TableNode *getNextEntry(TableNode *tn) {

View File

@ -48,7 +48,7 @@ typedef struct {
typedef struct {
int startlinenumber;
bool regularoras;
SymbolTable* scope;
SymbolTable *scope;
} function_declaration_info;
typedef struct {
@ -122,7 +122,7 @@ int getRecLength(TableNode *definition);
SymbolTable *getRecList(TableNode *definition);
TableNode *setRecSize(TableNode *tn, int n);
int getRecSize(SymbolTable *tn);
AdInfo *CreateFunctionDeclarationInfo(int line, bool asorregular,SymbolTable *scope);
AdInfo *CreateFunctionDeclarationInfo(int line, bool asorregular, SymbolTable *scope);
TableNode *setFunScope(TableNode *tn, SymbolTable *scope);
SymbolTable *getFunScope(TableNode *definition);
int getStartLine(TableNode *definition);