From fd39afc9e0db6327c9b461265c89fa97a4e15579 Mon Sep 17 00:00:00 2001 From: Meyer Simon Date: Thu, 27 Mar 2025 15:28:30 -0400 Subject: [PATCH 1/5] Added the files to the repo #t51 --- src/intermediate_code.c | 0 src/intermediate_code.h | 0 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 src/intermediate_code.c create mode 100644 src/intermediate_code.h diff --git a/src/intermediate_code.c b/src/intermediate_code.c new file mode 100644 index 0000000..e69de29 diff --git a/src/intermediate_code.h b/src/intermediate_code.h new file mode 100644 index 0000000..e69de29 From 03be62dc1946e5951256fd99b14af6bc50fba783 Mon Sep 17 00:00:00 2001 From: Meyer Simon Date: Thu, 27 Mar 2025 16:08:31 -0400 Subject: [PATCH 2/5] Added structs enums and stubs #t51 --- src/intermediate_code.c | 13 +++++++++++++ src/intermediate_code.h | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/src/intermediate_code.c b/src/intermediate_code.c index e69de29..f32b966 100644 --- a/src/intermediate_code.c +++ b/src/intermediate_code.c @@ -0,0 +1,13 @@ + + void emit_binary_op(char* result, char* op, char* arg1, char* arg2){ + return; + } + void emit_unary_op(char* result, char* op, char* arg){ + return; + } + void emit_assignment(char* target, char* source){ + return; + } +void emit_as_file(FILE * out_file, Instruction * instr_arr){ + return; +} diff --git a/src/intermediate_code.h b/src/intermediate_code.h index e69de29..c2aa796 100644 --- a/src/intermediate_code.h +++ b/src/intermediate_code.h @@ -0,0 +1,37 @@ +// Track 1: Core Infrastructure & Basic Expressions +// * Create intermediate_code.h/.c defining the instruction structure: +// - Struct with fields for: opcode, result, operand1, operand2, label +// - Enum for all operation types (ADD, SUB, MUL, DIV, etc.) +// * Implement temp variable generator function that produces unique names (t1, t2, etc.) +// * Create specific code emission functions: +// - emit_binary_op(char* result, char* op, char* arg1, char* arg2) +// - emit_unary_op(char* result, char* op, char* arg) +// - emit_assignment(char* target, char* source) +// * Add Bison actions for arithmetic expressions: +// - Addition: $$ = new_temp(); emit_binary_op($$, "ADD", $1, $3); +// - Subtraction, multiplication, division, modulo +#include "symbol_table.h" + +typedef enum {ADD, SUB, MUL, DIV} Op; // TODO: add all the instructions +typedef struct { + Op opcode; + TableNode * result; + TableNode * operand1; + TableNode * operand2; + int label; +} Instruction; + +void emit_binary_op(char* result, char* op, char* arg1, char* arg2); +void emit_unary_op(char* result, char* op, char* arg); +void emit_assignment(char* target, char* source); +// TODO: Find out what these are suposed to do. Guess is create an entry in +// the list of instructions + + +// * Implement integer/boolean/character specific operation handling +// TODO: Find out what this means + +// * Create output function to write instructions to file with line formatting +void emit_as_file(FILE * out_file, Instruction * instr_arr); + +// * Implement instruction array storage for backpatching From 666e0a4159249676b4490e378e481c15f873b007 Mon Sep 17 00:00:00 2001 From: Meyer Simon Date: Thu, 27 Mar 2025 17:28:16 -0400 Subject: [PATCH 3/5] No change #t51 --- src/intermediate_code.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/intermediate_code.h b/src/intermediate_code.h index c2aa796..446f4c4 100644 --- a/src/intermediate_code.h +++ b/src/intermediate_code.h @@ -25,11 +25,11 @@ void emit_binary_op(char* result, char* op, char* arg1, char* arg2); void emit_unary_op(char* result, char* op, char* arg); void emit_assignment(char* target, char* source); // TODO: Find out what these are suposed to do. Guess is create an entry in -// the list of instructions +// the list of instructions. Guess is that its suposed to ret a struct ptr // * Implement integer/boolean/character specific operation handling -// TODO: Find out what this means +// TODO: Find out what this means. // * Create output function to write instructions to file with line formatting void emit_as_file(FILE * out_file, Instruction * instr_arr); From 24caa0e9a7217ad7272ef1828e9e4176dae1c63c Mon Sep 17 00:00:00 2001 From: Meyer Simon Date: Fri, 28 Mar 2025 10:18:18 -0400 Subject: [PATCH 4/5] Updeated the contents of st to Dev #t51 --- src/symbol_table.c | 89 ++++++++++++++++++++++++++++++++++++++-------- src/symbol_table.h | 4 +-- 2 files changed, 77 insertions(+), 16 deletions(-) diff --git a/src/symbol_table.c b/src/symbol_table.c index 205bcbe..4ce220b 100644 --- a/src/symbol_table.c +++ b/src/symbol_table.c @@ -27,23 +27,23 @@ typedef enum { // TYPE_BOOLEAN, // TYPE_ADDRESS, // Type String is an array of char enclosed in double quotes per lexer - TYPE_STRING, + TYPE_STRING = 1, // Array can be multidimensional. Information should be stored here - TYPE_ARRAY, + TYPE_ARRAY = 2, // Record is user defined types - TYPE_RECORD, + TYPE_RECORD = 3, // Declaring what type a particular function is without as - TYPE_FUNCTION_DECLARATION, + TYPE_FUNCTION_DECLARATION = 4, // Declaring what type a particular function is with as // TYPE_AS_FUNCTION_DECLARATION, // Declaring what type a function is (what the parameters and output // are) - TYPE_FUNCTION_TYPE, + TYPE_FUNCTION_TYPE = 5, // The Type being pointed to by the first 4 above that only stores the // size - TYPE_PRIMITIVE, + TYPE_PRIMITIVE = 6, //likely NULL - TYPE_ALL_ELSE + TYPE_ALL_ELSE = 7 } types; @@ -153,12 +153,12 @@ TableNode *getArrType(TableNode *definition) { // order, of what make up that type in an array. Unfortunately this second part // should probably instead be replaced by a reference to a scope in which those // elements are found. -AdInfo *CreateRecordInfo(int length, TableNode *typesarray) { +AdInfo *CreateRecordInfo(int length, SymbolTable *recordScope) { AdInfo *info = (AdInfo *)malloc(sizeof(AdInfo)); info->RecAdInfo = (record_info *)malloc(sizeof(record_info)); info->RecAdInfo->numofelements = length; // replace below with reference to a scope, not an array - info->RecAdInfo->listoftypes = typesarray; + info->RecAdInfo->recordScope = recordScope; return info; } // This gets the number of elements that make up a record. @@ -172,13 +172,13 @@ int getRecLength(TableNode *definition) { return definition->additionalinfo->RecAdInfo->numofelements; } // This gets the array. Needs to up be updated to get the scope instead -TableNode *getRecList(TableNode *definition) { +SymbolTable *getRecList(TableNode *definition) { if (strcmp(getType(definition), "record") != 0) { printf("not checking the list of types of a record -- invalid " "op\n"); return NULL; } - return definition->additionalinfo->RecAdInfo->listoftypes; + return definition->additionalinfo->RecAdInfo->recordScope; } // below function takes a bool to see if parameter should be decomposed or not @@ -379,8 +379,34 @@ TableNode* boo; TableNode* recprime; TableNode* funtypeprime; */ +TableNode* populateTypeAndInfo(TableNode* tn, TableNode* type, AdInfo* info){ + if(tn == NULL){ + printf("passed in an invalid table node to modify (NULL).\n"); + return NULL; + } + if(type == NULL){ + printf("passed in a NULL type reference to populate a table node. Invalid.\n"); + return NULL; + } + if(info == NULL){ + printf("passed in a NULL info reference to populate a table node. Invalid.\n"); + return NULL; + } + tn->theType = type; + tn->additionalinfo = info; + //returning reference to modified table node + return tn; + } int getAdInfoType(TableNode* tn){ + if(tn == NULL){ + printf("passing in NULL table entry. Invalid\n"); + return -1; + } + if(tn->theType == NULL){ + printf("Entry being passed in has a null reference for theType. Invalid.\n"); + return -1; + } if(strcmp(getType(tn),getName(integ))==0){ return TYPE_PRIMITIVE; } @@ -443,11 +469,41 @@ TableNode *CreateEntry(SymbolTable *table, TableNode *typeOf, char *id, } } -char *getType(TableNode *tn) { return tn->theType->theName; } +char *getType(TableNode *tn) { + if(tn == NULL){ + printf("passed a NULL table entry to getType\n"); + return ""; + } + if(tn->theType == NULL){ + printf("type of entry is currently NULL, undefined type \n"); + return ""; + } + return tn->theType->theName; } char *getName(TableNode *tn) { return tn->theName; } int getLine(SymbolTable *st) { return st->Line_Number; } int getColumn(SymbolTable *st) { return st->Column_Number; } +TableNode* addName(TableNode *tn, char* str){ + if(tn == NULL){ + printf("passed a Null table node to the addName function. Invalid./n"); + return tn; + } +} +SymbolTable* setLineNumber(SymbolTable *st,int line){ + if(st == NULL){ + printf("passed a Null Symbol Table to the setLineNumber function. Invalid./n"); + return st; + } + st->Line_Number = line; + } + +SymbolTable* setColumnNumber(SymbolTable *st,int column){ + if(st == NULL){ + printf("passed a Null Symbol Table to the setColumnNumber function. Invalid./n"); + return st; + } + st->Line_Number = column; + } /* //we use false for type defs and true for functions for parameter of typeOf TableNode* Define(SymbolTable* table, bool typeOf, char* id) { @@ -483,6 +539,7 @@ names\n"); return NULL; } */ +//only check table that is given TableNode *table_lookup(SymbolTable *table, char *x) { TableNode *entrie = table->entries; for (; entrie != NULL; entrie = entrie->next) { @@ -492,6 +549,7 @@ TableNode *table_lookup(SymbolTable *table, char *x) { } return NULL; } +//check current table and all parents TableNode *look_up(SymbolTable *table, char *x) { if (table == NULL) { return NULL; @@ -504,6 +562,8 @@ TableNode *look_up(SymbolTable *table, char *x) { } void print_symbol_table(SymbolTable *table, FILE *file_ptr) { + + return; if (table->Parent_Scope == NULL) { fprintf(file_ptr, "%-17s: %-6s : %-6s : %-21s: %-28s\n", "NAME", "SCOPE", "PARENT", "TYPE", "Extra annotation"); @@ -526,7 +586,7 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr) { fprintf(file_ptr, "%-17s: %06d : %06d : %-21s: %-28s\n", "", current_scope, parant_scope, "", "Empty Scope"); } - for (; entrie != NULL; entrie = entrie->next) { + for (entrie != NULL; entrie = entrie->next;) { if (parant_scope == 0) { /*have to update*/ if (strcmp(entrie->theType->theName, "function primitive") || @@ -555,7 +615,7 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr) { "----------------------\n"); } } - +//get top most symbol table SymbolTable *getAncestor(SymbolTable *table) { if (table->Parent_Scope == NULL) { // if table has no parent, return itself @@ -594,6 +654,7 @@ SymbolTable *removeEntry(SymbolTable *scope, char *search) { return scope; } +//almost certainly don't need to use the below function since type checking happens by passing types up the grammar bool typeCheck(char *firstID, char *secondID) { TableNode *entry1 = look_up(cur, firstID); diff --git a/src/symbol_table.h b/src/symbol_table.h index 84b3e5a..de37098 100644 --- a/src/symbol_table.h +++ b/src/symbol_table.h @@ -30,7 +30,7 @@ typedef struct { // similar to above we define a record to hold the number of elements // and an array of tablenodes (types) that it contains in the > int numofelements; - struct TableNode *listoftypes; + struct SymbolTable *recordScope; } record_info; typedef struct { @@ -91,7 +91,7 @@ int getPrimSize(TableNode *definition); int getNumArrDim(TableNode *definition); TableNode *getArrType(TableNode *definition); int getRecLength(TableNode *definition); -TableNode *getRecList(TableNode *definition); +SymbolTable *getRecList(TableNode *definition); int getStartLine(TableNode *definition); bool getAsKeyword(TableNode *definition); TableNode *getParameter(TableNode *definition); From 05b641a32e52d2dca43570cc8256ede128bea551 Mon Sep 17 00:00:00 2001 From: Meyer Simon Date: Tue, 1 Apr 2025 12:53:31 -0400 Subject: [PATCH 5/5] Added stubs for all emit funcs and added the 3 fields discussed in the library t#51 --- src/intermediate_code.c | 51 +++++++++++++++++++++++++++++++++++++++++ src/intermediate_code.h | 22 ++++++++++++++++++ 2 files changed, 73 insertions(+) diff --git a/src/intermediate_code.c b/src/intermediate_code.c index f32b966..2d50a34 100644 --- a/src/intermediate_code.c +++ b/src/intermediate_code.c @@ -1,4 +1,12 @@ + + +// TODO: this is here to bring your attention to the comment bellow. +// 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. + + + void emit_binary_op(char* result, char* op, char* arg1, char* arg2){ return; } @@ -11,3 +19,46 @@ void emit_as_file(FILE * out_file, Instruction * instr_arr){ return; } + + +void emit_label(char* label){ + return; +} +void emit_jump(char* label){ + return; +} +void emit_conditional_jump(char* condition, char* label){ + return; +} + + + +void emit_function_start(char* name){ + return; +} +void emit_parameter(char* param){ + return; +} +void emit_function_call(char* result, char* name){ + return; +} +void emit_return(char* value){ + return; +} +void emit_reserve(char* result, char* type_name, int size){ + return; +} +void emit_release(char* pointer){ + return; +} + + +void emit_field_access(char* result, char* record, char* field){ + return; +} +void emit_array_access(char* result, char* array, char* index, char* dimension){ + return; +} +void emit_bounds_check(char* index, char* size, char* error_label){ + return; +} diff --git a/src/intermediate_code.h b/src/intermediate_code.h index 446f4c4..1ccd389 100644 --- a/src/intermediate_code.h +++ b/src/intermediate_code.h @@ -19,8 +19,14 @@ typedef struct { TableNode * operand1; TableNode * operand2; int label; + int instruction; + Instruction * prev; + Instruction * next; } Instruction; +Instruction * start = NULL; +Instruction * current = NULL; + void emit_binary_op(char* result, char* op, char* arg1, char* arg2); void emit_unary_op(char* result, char* op, char* arg); void emit_assignment(char* target, char* source); @@ -35,3 +41,19 @@ void emit_assignment(char* target, char* source); 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(char* condition, char* label); + +void emit_function_start(char* name); +void emit_parameter(char* param); +void emit_function_call(char* result, char* name); +void emit_return(char* value); +void emit_reserve(char* result, char* type_name, int size); +void emit_release(char* pointer); + + +void emit_field_access(char* result, char* record, char* field); +void emit_array_access(char* result, char* array, char* index, char* dimension); +void emit_bounds_check(char* index, char* size, char* error_label);