void FreeAllAttr(struct SymTab * table) { struct SymEntry * anEntry = FirstEntry(table); while (anEntry) { free(GetAttr(anEntry)); anEntry = NextEntry(table, anEntry); } }
void VerifyCounts(struct SymTab * table) { struct SymEntry * anEntry = FirstEntry(table); while (anEntry) { struct Attributes * attr = GetAttr(anEntry); if (attr->value1 != attr->value2) { fprintf(stdout,"count for %s not correct\n",GetName(anEntry)); } anEntry = NextEntry(table, anEntry); } }
void DestroySymTab(struct SymTab *aTable){ struct SymEntry* entry=FirstEntry(aTable); struct SymEntry* mid; while (entry) { mid=entry; entry=NextEntry(aTable, entry); //free(mid->attributes); free(mid->name); free(mid); } free(aTable->contents); free(aTable); }
void Finish(struct InstrSeq *Code) { struct InstrSeq *code; struct SymEntry *entry; struct Attr * attr; struct StrLabels * strlabel; code = GenInstr(NULL,".text",NULL,NULL,NULL); AppendSeq(code,GenInstr(NULL,".globl","main",NULL,NULL)); AppendSeq(code, GenInstr("main",NULL,NULL,NULL,NULL)); AppendSeq(code, doFuncStmt("main")); AppendSeq(code, GenInstr(NULL, "li", "$v0", "10", NULL)); AppendSeq(code, GenInstr(NULL,"syscall",NULL,NULL,NULL)); AppendSeq(code,Code); AppendSeq(code,GenInstr(NULL,".data",NULL,NULL,NULL)); AppendSeq(code,GenInstr(NULL,".align","4",NULL,NULL)); AppendSeq(code,GenInstr("_nl",".asciiz","\"\\n\"",NULL,NULL)); AppendSeq(code,GenInstr("_tru",".asciiz","\"true\"",NULL,NULL)); AppendSeq(code,GenInstr("_fal",".asciiz","\"false\"",NULL,NULL)); AppendSeq(code,GenInstr("_sp", ".asciiz","\" \"",NULL,NULL)); entry = FirstEntry(table); while (entry) { struct Vtype * vtype = ((struct Vtype *)entry->Attributes); AppendSeq(code, GenInstr(NULL, ".align", "4", NULL, NULL)); if(vtype->Type == TYPE_INTARR || vtype->Type == TYPE_BOOLARR){ char * buffer; buffer = Imm(4 * vtype->Size); AppendSeq(code, GenInstr( (char*)GetName(entry), ".space", buffer, NULL, NULL)); } else { AppendSeq(code,GenInstr((char *) GetName(entry),".word","0",NULL,NULL)); } entry = NextEntry(table, entry); } strlabel = slabels; while( strlabel ){ AppendSeq(code, GenInstr(NULL, ".align", "4", NULL, NULL)); AppendSeq(code, GenInstr(strlabel->SLabel, ".asciiz", strlabel->Str, NULL, NULL)); strlabel = strlabel->next; } WriteSeq(code); return; }
void DisplayTable(struct SymTab * table) { struct SymEntry * anEntry = FirstEntry(table); int i = 1; while (anEntry) { fprintf(stdout,"%3d %20s %5d %5d\n", i, GetName(anEntry), ((struct Attributes *) GetAttr(anEntry))->value1, ((struct Attributes *) GetAttr(anEntry))->value2); i++; anEntry = NextEntry(table, anEntry); } }
struct SymTab * CopyTable(struct SymTab * table, int newSize) { struct SymTab * copyTable; if (!(copyTable = CreateSymTab(newSize))) ErrorExit("Failed to alloc copy table.\n"); struct SymEntry * anEntry = FirstEntry(table); while (anEntry) { struct SymEntry * copyEntry; EnterName(copyTable,GetName(anEntry),©Entry); SetAttr(copyEntry,GetAttr(anEntry)); anEntry = NextEntry(table, anEntry); } return copyTable; }