public override string GenerateCode() { var s = "# Method Call\t\t\t" + MethodName + "\n"; //foreach son generate his code and //give me the reg where its values //its located foreach (var item in Childs) { //if (item is IdentifierNode && item.ToString() == "value") // continue; s += item.GenerateCode(); var reg1 = MIPS.LastReg(); // s += ("\tsubu $sp, $sp, 4\n"); s += MIPS.Emit(MIPS.Opcodes.sw, reg1, "($sp)"); //push all your sons } // in this case we dont need a regg s += MIPS.Emit(MIPS.Opcodes.jal, MethodName); var reg2 = MIPS.GetReg();// s += MIPS.Emit(MIPS.Opcodes.move, reg2, "$v0\t\t#Method return value"); return(s); }
public override string GenerateCode() { var s = ""; s += MIPS.Emit(MIPS.Opcodes.jal, Type, "\t\t\t# jump to ctor"); s += MIPS.Emit(MIPS.Opcodes.move, MIPS.GetReg(), "$v0\t\t#Ctor return value"); return(s); }
public override string GenerateCode() { var s = ""; var register = MIPS.GetReg(); s += MIPS.Emit(MIPS.Opcodes.li, register, V.ToString()); return(s); }
public override string GenerateCode() { var s = ""; var i = MIPS.GetReg(); s = MIPS.Emit(MIPS.Opcodes.la, i, "str" + I, "\t\t# " + V); return(s); }
public override string GenerateCode() { /**/ var s = ""; var register = ""; var v = TryGetValue(); if (v != null) { register = MIPS.GetReg(); s += MIPS.Emit(MIPS.Opcodes.li, register, v.ToString(), "\t\t\t# calculated in compilation time :)"); return(s); } var l = LE.GenerateCode(); //var reg2 = (MIPS.regCount - 1).ToReg(); var reg2 = MIPS.LastReg();// var r = RE.GenerateCode(); //var reg1 = (MIPS.regCount - 1).ToReg(); var reg1 = MIPS.LastReg();// register = MIPS.GetReg(); switch (OP) { case "+": s += MIPS.Emit(MIPS.Opcodes.add, register, reg2, reg1); break; case "-": s += MIPS.Emit(MIPS.Opcodes.sub, register, reg2, reg1); break; case "*": s += MIPS.Emit(MIPS.Opcodes.mul, register, reg2, reg1); break; case "/": s += MIPS.Emit(MIPS.Opcodes.div, register, reg2, reg1); break; default: break; } if (l.Contains("Method")) { return(l + r + s); } else { return(r + l + s); } }
public override string GenerateCode() { string result = ""; result += s.Childs[0].GenerateCode(); string reg = MIPS.LastReg(); result += MIPS.Emit(MIPS.Opcodes.neg, reg, reg); return(result); }
public override string GenerateCode() { //this.Type = SymbolTable.Symbols.Peek()[Name].Name; var s = ""; var reg1 = ""; reg1 = MIPS.GetReg();// here we asume that an int has been loaded to the last position s += MIPS.Emit(MIPS.Opcodes.lw, reg1, Name); return(s); }
public override string GenerateCode() { string result = ""; result += lhs.GenerateCode(); string lhsReg = MIPS.LastReg(); result += rhs.GenerateCode(); string rhsReg = MIPS.LastReg(); result += MIPS.Emit(MIPS.Opcodes.sle, rhsReg, lhsReg, rhsReg); return(result); }
public override string GenerateCode() { var s = "# coolc-output" + ":\n";//todo add the calss name :( s += ".data\n"; s += "self:\t.word\t0\t#self\n"; s += GiveMeTheData(); s += ".text\n"; s += base.GenerateCode(); s += MIPS.BasicFunctions(); s += MIPS.HelpFunctions(); return(s); }
public override string GenerateCode() { var s = ""; var reg1 = ""; //var exp = Childs[0];//why it only has one son //var es = exp.GenerateCode(); //reg1 = MIPS.LastReg().ToReg();// here we asume that an int has been loaded to the last position reg1 = MIPS.GetReg();// here we asume that an int has been loaded to the last position //s += "\t# Assign\n"; //s += MIPS.Emit(MIPS.Opcodes.usw, reg1, ID + "\t\t# " + ID); s += MIPS.Emit(MIPS.Opcodes.li, reg1, V.GetHashCode().ToString()); return(s); }
public override string GenerateCode() { int r = TryGetValue(); string result = ""; foreach (var child in Childs) { result += child.GenerateCode(); } string lastReg = MIPS.LastReg(); result += MIPS.Emit(MIPS.Opcodes.not, lastReg, lastReg); return(result); }
public override string GenerateCode() { MIPS.whileCount++; string code = ""; string whileName = "while" + MIPS.whileCount.ToString(); string whileEnd = "whileEnd" + MIPS.whileCount.ToString(); code += whileName + ":\n"; code += Cond.GenerateCode(); string condEval = MIPS.LastReg(); code += MIPS.Emit(MIPS.Opcodes.bne, condEval, "1", whileEnd); code += Body.GenerateCode(); code += MIPS.Emit(MIPS.Opcodes.j, whileName); code += whileEnd + ":\n"; return(code); }
public override string GenerateCode() { SymbolTable.Symbols.Push(Symbols); var s = "";// = ClassName + "." + Name + ":\n";//todo add the calss name :( if (ClassName == "" || Name.ToLower() == "main") { s += Name + ":\n"; } else { s += ClassName + "." + Name + ":\n"; } var reg1 = MIPS.GetReg();// s += "\t" + "lw " + "$fp" + ", ($sp)" + "\n" + "\t" + "addu $sp, $sp, 4" + "\n"; foreach (var item in Params) { s += MIPS.Emit(MIPS.Opcodes.lw, reg1, "($sp)");//pop all your sons //correjir la pila s += "\taddu $sp, $sp, 4\n"; s += MIPS.Emit(MIPS.Opcodes.sw, reg1, /* Name + "." +*/ item.Key); } s += "\t" + "subu $sp, $sp, 4 " + "\n" + "\t" + "sw " + "$fp" + ", ($sp) " + "\n"; s += base.GenerateCode(); s = include(s, "value", Name); if (ClassName == "" && Name.ToLower() == "main") { s += "\tli $v0, 10\t\t\t# 10 is the exit syscall.\n\tsyscall\t\t\t\t# do the syscall.\n"; } else { s += MIPS.Emit(MIPS.Opcodes.move, "$v0", MIPS.LastReg()); try { var c = SymbolTable.Classes[ClassName]; var reg0 = MIPS.GetReg(); //before pop must save last callreturn s += "\t" + "lw " + "$fp" + ", ($sp)" + "\n" + "\t" + "addu $sp, $sp, 4" + "\n"; s += MIPS.Emit(MIPS.Opcodes.lw, reg0, "($sp)");//pop all your sons s += "\taddu $sp, $sp, 4\n"; foreach (var item in c.Attributes) { s += MIPS.Emit(MIPS.Opcodes.lw, reg1, item.Key + "_" + Name); //pop all your sons s += MIPS.Emit(MIPS.Opcodes.sw, reg1, "(" + reg0 + ")"); //pop all your sons } s += "\t" + "subu $sp, $sp, 4 " + "\n" + "\t" + "sw " + "$fp" + ", ($sp) " + "\n"; } catch (System.Exception) {} s += MIPS.Emit(MIPS.Opcodes.jr); } SymbolTable.Symbols.Pop(); return(s); }
public override string GenerateCode() { try { //if (SymbolTable.Classes[((IdentifierNode)Childs[0]).Name]!=null) // CallerType = SymbolTable.Classes[((IdentifierNode)Childs[0]).Name]; CallerType = SymbolTable.Symbols.Peek()[((IdentifierNode)Childs[0]).Name]; } catch (Exception) { Type = Childs[0].Type; CallerType = SymbolTable.Classes[Type]; } var s = "# Atsim cal\t\t\t" + CallerType.Name + "." + MethodID.GetText() + "\n"; int offset = 0; foreach (var item in Childs) { s += item.GenerateCode(); var reg1 = MIPS.LastReg(); // s += MIPS.Emit(MIPS.Opcodes.sw, reg1, offset + "($sp)"); //push all your sons offset += 4; //s += MIPS.Emit(MIPS.Opcodes.push, reg1);//push all your sons } /*Parche*/ try{ var t = MIPS.LastReg(); s += MIPS.Emit(MIPS.Opcodes.lw, t, ((IdentifierNode)Childs[0]).Name);//load the caller offset = 8; var n = MIPS.GetReg(); foreach (var item in CallerType.Attributes)// iterate the attributes { s += MIPS.Emit(MIPS.Opcodes.lw, n, offset + "(" + t + ")"); s += MIPS.Emit(MIPS.Opcodes.sw, n, item.Key + "_" + item.ToString()); offset += 4; } }catch (Exception) {} /* end of Parche*/ //s += MIPS.Emit(MIPS.Opcodes.sw, MIPS.LastReg().ToReg(), ); var m = CallerType.GetMethod(MethodID.GetText()); var callerTypeName = m.ClassName; if (callerTypeName != CallerType.Name) { //cast s += MIPS.Emit(MIPS.Opcodes.lw, MIPS.LastReg(), "($sp)"); s += MIPS.Emit(MIPS.Opcodes.move, "$s2", MIPS.LastReg()); var j = MIPS.LastReg(); var y = MIPS.GetReg(); s += MIPS.Emit(MIPS.Opcodes.lw, y, SymbolTable.Classes[CallerType.Name].Size - 4 + "(" + j + ")"); s += MIPS.Emit(MIPS.Opcodes.sw, y, "($sp)"); //s += MIPS.Emit(MIPS.Opcodes.sw, MIPS.LastReg(), "0($sp)"); } //s += MIPS.Emit(MIPS.Opcodes.move, "$s0", "$ra"); if (callerTypeName != "") { s += MIPS.Emit(MIPS.Opcodes.jal, callerTypeName + "." + MethodID);// this blows $ra } else { s += MIPS.Emit(MIPS.Opcodes.jal, " " + MethodID);// this blows $ra } //s += MIPS.Emit(MIPS.Opcodes.move, "$ra", "$s0"); var reg2 = MIPS.GetReg(); s += MIPS.Emit(MIPS.Opcodes.move, reg2, "$v0\t\t#Method return value"); if (callerTypeName != CallerType.Name) { s += MIPS.Emit(MIPS.Opcodes.sw, "$s2", "($sp)"); } //s += MIPS.Emit(MIPS.Opcodes.subu, "$sp","$sp", "4"); //SymbolTable.Symbols.Pop(); return(s);// base.GenerateCode(); }
public override string GenerateCode() { var s = ""; var t = Attributes.Count; string r = ""; var reg1 = ""; if (Name.ToLower() != "main") { s += Name + ":\t\t\t\t#Ctor\n"; //t = (t * 4) + 8; reg1 = MIPS.GetReg(); s += MIPS.Emit(MIPS.Opcodes.li, reg1, Size.ToString()); //correjir la pila para push s += "\tsubu $sp, $sp, 4\n"; s += MIPS.Emit(MIPS.Opcodes.sw, reg1, "($sp)"); //intento de savarlo antes de perderlo:: s += MIPS.Emit(MIPS.Opcodes.move, "$s0", "$ra"); s += MIPS.Emit(MIPS.Opcodes.jal, "makeroom");// this blows $ra s += MIPS.Emit(MIPS.Opcodes.move, "$ra", "$s0"); s += MIPS.Emit(MIPS.Opcodes.move, reg1, "$v0"); var reg2 = MIPS.GetReg(); //put class name s += MIPS.Emit(MIPS.Opcodes.la, reg2, Name + "_str"); s += MIPS.Emit(MIPS.Opcodes.sw, reg2, "(" + reg1 + ")"); //put class size s += MIPS.Emit(MIPS.Opcodes.li, reg2, Size.ToString()); s += MIPS.Emit(MIPS.Opcodes.sw, reg2, "(" + reg1 + ")"); int offset = 4; foreach (var item in Attributes) { offset += 4; if (item.Value.GetType().Name.ToLower() == "int" || item.Value.GetType().Name.ToLower() == "bool" || item.Value.GetType().Name.ToLower() == "string") { //s += "# this value goes here? " + item.Value.Name + " " + 4 + " " + "\n"; s += item.Value.GenerateCode(); var i = MIPS.LastReg(); s += MIPS.Emit(MIPS.Opcodes.sw, i, offset + "(" + reg1 + ")"); } else { s += "#allocating space for " + item.Value.Name + " " + item.Value.GetType().Size + " " + "\n"; s += item.Value.GenerateCode(); // not sure about this :S var i = MIPS.LastReg(); s += MIPS.Emit(MIPS.Opcodes.sw, i, offset + "(" + reg1 + ")"); } } if (Parent != null && (Parent.Name != "Object" && Parent.Name != "Bool" && Parent.Name != "IO" && Parent.Name != "Int" && Parent.Name != "String")) { s += MIPS.Emit(MIPS.Opcodes.move, "$s1", "$ra"); // todo save this int the stack s += MIPS.Emit(MIPS.Opcodes.jal, Parent.Name); // this blows $ra s += MIPS.Emit(MIPS.Opcodes.move, "$ra", "$s1"); var i = MIPS.GetReg(); s += MIPS.Emit(MIPS.Opcodes.move, i, "$v0"); //copy your parent addres form v0 to a new reg s += MIPS.Emit(MIPS.Opcodes.sw, i, offset + 4 + "(" + reg1 + ")"); // then coy it to the last position of you } s += MIPS.Emit(MIPS.Opcodes.move, "$v0", reg1); s += MIPS.Emit(MIPS.Opcodes.jr); //if (Parent != null && Parent.Name != "Object") // s += Parent.GenerateCode(); } else { //main class attribute should be on the heap? } foreach (var item in this.Attributes) { s += item.Value.GenerateCode(); } foreach (var item in Methods) { if (item.Key.ToString() == "main") { s += item.Value.GenerateCode(); } else { r += item.Value.GenerateCode(); } } s += r; return(s);// +base.GenerateCode(); }