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; }
struct InstrSeq * doArrayAssign( char * name, struct ExprRes * index, struct ExprRes * val){ struct SymEntry *e; if( global ){ e = FindName(locTable, name); if( !e){ e = FindName(table, name); } } else{ e = FindName(table, name); } struct Vtype * vtype = ((struct Vtype *)e->Attributes); struct InstrSeq * code; int reg = AvailTmpReg(); char * buffer; if (!e){ WriteIndicator(GetCurrentColumn()); WriteMessage("Undeclared variable"); } if(index->Type != TYPE_INT){ TypeError(); } else if( val->Type != -1 && ((vtype->Type == TYPE_BOOLARR && val->Type != TYPE_BOOL) || (vtype->Type == TYPE_INTARR && val->Type != TYPE_INT ))){ TypeError(); } buffer = RegOff(0, TmpRegName(reg)); code = val->Instrs; AppendSeq(code, index->Instrs); // change for locality later if (! vtype->Local){ AppendSeq( code, GenInstr( NULL, "la", TmpRegName(reg), name, NULL)); } else { AppendSeq(code, GenInstr(NULL, "addi", TmpRegName(reg), "$sp", Imm(vtype->StackIndex))); } AppendSeq( code, GenInstr( NULL, "mul", TmpRegName(index->Reg), TmpRegName(index->Reg), "4")); AppendSeq( code, GenInstr(NULL, "add", TmpRegName(reg), TmpRegName(reg), TmpRegName(index->Reg))); AppendSeq( code, GenInstr(NULL, "sw", TmpRegName(val->Reg), buffer, NULL)); ReleaseTmpReg(val->Reg); ReleaseTmpReg(index->Reg); ReleaseTmpReg(reg); free(val); free(index); return code; }
struct ExprRes * doArrayRval(char * name, struct ExprRes * index){ int reg = AvailTmpReg(); struct SymEntry * e = FindName(table, name); if( !e ){ WriteIndicator(GetCurrentColumn()); WriteMessage("Undeclared variable"); exit(1); } if(index->Type != TYPE_INT){ TypeError(); } char * buffer = RegOff(0, TmpRegName(reg)); struct Vtype * vtype = ((struct Vtype *)e->Attributes); //change later for variable locality if( vtype->Local) { AppendSeq(index->Instrs, GenInstr(NULL, "addi", TmpRegName(reg), "$sp", Imm(vtype->StackIndex))); } else{ AppendSeq(index->Instrs, GenInstr( NULL, "la", TmpRegName(reg), name, NULL)); } AppendSeq(index->Instrs, GenInstr(NULL, "mul", TmpRegName(index->Reg), TmpRegName(index->Reg), "4")); AppendSeq(index->Instrs, GenInstr(NULL, "add", TmpRegName(reg), TmpRegName(reg), TmpRegName(index->Reg))); AppendSeq(index->Instrs, GenInstr(NULL , "lw", TmpRegName(index->Reg), buffer, NULL)); if( vtype->Type == TYPE_INTARR){ index->Type = TYPE_INT; } else { index->Type = TYPE_BOOL; } ReleaseTmpReg(reg); return index; }
#include "x86.hpp" std::vector<x86_insn_mnemonic> x86_format_table = { {"mov", { {0x88, DFDX_OPC_MOV, true,ModRM(), 0}, {0x89, DFDX_OPC_MOV, false, ModRM(), 0}, {0x8a, DFDX_OPC_MOV, true, ModRM(), 1}, {0x8b, DFDX_OPC_MOV, false, ModRM(), 1}, }, }, {"add", { {0x04, DFDX_OPC_ADD, false, Imm8(), 1}, {0x05, DFDX_OPC_ADD, false, Imm(), 1}, {0x80, DFDX_OPC_ADD, true, ModRM_Imm(), 0}, {0x81, DFDX_OPC_ADD, false, ModRM_Imm(), 0}, {0x83, DFDX_OPC_ADD, false, ModRM_Op_Imm8(0), 0}, {0x00, DFDX_OPC_ADD, true, ModRM(), 0}, {0x01, DFDX_OPC_ADD, false, ModRM(), 0}, {0x02, DFDX_OPC_ADD, true, ModRM(), 1}, {0x03, DFDX_OPC_ADD, false, ModRM(), 1}, }, }, {"sub", { {0x2c, DFDX_OPC_SUB, false, Imm8(), 1}, {0x2d, DFDX_OPC_SUB, false, Imm(), 1}, {0x80, DFDX_OPC_SUB, true, ModRM_Op_Imm8(5), 0}, {0x81, DFDX_OPC_SUB, false, ModRM_Op_Imm(5), 0}, {0x83, DFDX_OPC_SUB, false, ModRM_Op_Imm8(5), 0}, {0x28, DFDX_OPC_SUB, true, ModRM(), 0}, {0x29, DFDX_OPC_SUB, false, ModRM(), 0}, {0x2a, DFDX_OPC_SUB, true, ModRM(), 1}, {0x2b, DFDX_OPC_SUB, false, ModRM(), 1},
Error BaseAssembler::emit(uint32_t code, const Operand& o0, const Operand& o1, const Operand& o2, int o3_) { return _emit(code, o0, o1, o2, Imm(o3_)); }
Error BaseAssembler::emit(uint32_t code, const Operand& o0, const Operand& o1, int o2_) { return _emit(code, o0, o1, Imm(o2_), no); }
Error BaseAssembler::emit(uint32_t code, int o0_) { return _emit(code, Imm(o0_), no, no, no); }