#include "ParseResult.h" #include #include static struct { char* mnem; char* mType; } type[] = { { "sub", "r" }, { "nor", "r" }, { "add", "r" }, { "syscall", "r" }, { "mul", "r" }, { "mult", "s" }, { "lw", "i" }, { "lui", "i" }, { "addi", "i" }, { "slti", "i" }, { "la", "i" }, { "beq", "i" }, { "bne", "i" }, { "sw", "i" }, { "nop", "r" }, { "sll", "r" }, { "sra", "r" }, { "srav", "r" }, { "slt", "r" }, { "blt", "r" }, { "j", "ju" }, { "addu", "r" }, { "move", "r" }, { "addiu", "i" }, { "li", "i" }, { "blez", "i" }, { "bgtz", "i" }, { NULL, 0 } }; /*** Add include directives for here as needed. ***/ char* clearType(char* inp); /** Frees the dynamic content of a ParseResult object. * * Pre: pPR points to a proper ParseResult object. * Post: All of the dynamically-allocated arrays in *pPR have been * deallocated; pointers are NULL, static fields are reset to * default values. * * Comments: * - The function has no information about whether *pPR has been * allocated dynamically, so it cannot risk attempting to * deallocate *pPR. * - The function is intended to provide the user with a simple * way to free memory; the user may or may not reuse *pPR. So, * the function does set the pointers in *pPR to NULL. */ void clearResult(ParseResult* const pPR) { if(pPR){ pPR->Imm = 0; pPR->rd = 0; pPR->rs = 0; pPR->rt = 0; if(!strcmp(clearType(pPR->Mnemonic), "ju")){ free(pPR->Opcode); free(pPR->Target); } if(!strcmp(clearType(pPR->Mnemonic), "i")){ free(pPR->RS); free(pPR->RT); free(pPR->rsName); free(pPR->rtName); free(pPR->Opcode); free(pPR->IMM); } if(!strcmp(clearType(pPR->Mnemonic), "r")){ free(pPR->rdName); free(pPR->rsName); free(pPR->rtName); free(pPR->Opcode); free(pPR->Funct); free(pPR->RD); free(pPR->RS); free(pPR->RT); free(pPR->shamt); } if(!strcmp(clearType(pPR->Mnemonic), "s")){ free(pPR->RS); free(pPR->RT); free(pPR->RD); free(pPR->rsName); free(pPR->rtName); free(pPR->Opcode); free(pPR->Funct); } } free(pPR->Mnemonic); free(pPR->ASMInstruction); } /** Prints the contents of a ParseResult object. * * Pre: Log is open on an output file. * pPR points to a proper ParseResult object. */ void printResult(FILE* Log, const ParseResult* const pPR) { fprintf(Log, "%s\n", pPR->ASMInstruction); fprintf(Log, " %s %s\n", pPR->Opcode, pPR->Mnemonic); fprintf(Log, " %2"PRIu8" %s", pPR->rd, pPR->rdName); if ( pPR->RD != NULL ) { fprintf(Log, " %s", pPR->RD); } fprintf(Log, "\n"); fprintf(Log, " %2"PRIu8" %s", pPR->rs, pPR->rsName); if ( pPR->RS != NULL ) { fprintf(Log, " %s", pPR->RS); } fprintf(Log, "\n"); fprintf(Log, " %2"PRIu8" %s", pPR->rt, pPR->rtName); if ( pPR->RT != NULL ) { fprintf(Log, " %s", pPR->RT); } fprintf(Log, "\n"); fprintf(Log, " %s\n", pPR->Funct); fprintf(Log, " %"PRId16"\n", pPR->Imm); if ( pPR->IMM != NULL ) { fprintf(Log, " %s", pPR->IMM); } fprintf(Log, "\n"); fprintf(Log, "\n"); } char* clearType(char* inp){ int i = -1; while(type[++i].mnem) if(!strcmp(inp, type[i].mnem)) return type[i].mType; return NULL; }