Beispiel #1
0
        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
            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;
            }
            // 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);
        }
Beispiel #2
0
        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);
        }
Beispiel #3
0
        public override string GenerateCode()
        {
            var s        = "";
            var register = MIPS.GetReg();

            s += MIPS.Emit(MIPS.Opcodes.li, register, V.ToString());

            return(s);
        }
Beispiel #4
0
        public override string GenerateCode()
        {
            var s = "";

            var i = MIPS.GetReg();

            s = MIPS.Emit(MIPS.Opcodes.la, i, "str" + I, "\t\t# " + V);

            return(s);
        }
Beispiel #5
0
        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);
        }
Beispiel #6
0
        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";
            }

            int offset = 4;
            var reg1   = MIPS.GetReg();//

            foreach (var item in Params)
            {
                s      += MIPS.Emit(MIPS.Opcodes.lw, reg1, offset + "($sp)");//pop all your sons
                s      += MIPS.Emit(MIPS.Opcodes.usw, reg1, /* Name + "." +*/ item.Key);
                offset += 4;
            }
            s += base.GenerateCode();
            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
                {
                    offset = 8;
                    var c = SymbolTable.Classes[ClassName];

                    var reg0 = MIPS.GetReg();
                    s += MIPS.Emit(MIPS.Opcodes.lw, reg0, 0 + "($sp)");//pop all your sons

                    foreach (var item in c.Attributes)
                    {
                        s      += MIPS.Emit(MIPS.Opcodes.lw, reg1, item.Key);                  //pop all your sons
                        s      += MIPS.Emit(MIPS.Opcodes.sw, reg1, offset + "(" + reg0 + ")"); //pop all your sons
                        offset += 4;
                    }
                }catch (System.Exception) {}
                s += MIPS.Emit(MIPS.Opcodes.jr);
            }
            SymbolTable.Symbols.Pop();
            return(s);
        }
Beispiel #7
0
        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);
        }
Beispiel #8
0
        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.seq, rhsReg, lhsReg, rhsReg);

            return(result);
        }
Beispiel #9
0
        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;
            }
            return(r + l + s);
        }
Beispiel #10
0
        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);
        }
Beispiel #11
0
        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);
        }
Beispiel #12
0
        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);
        }
Beispiel #13
0
        public override string GenerateCode()
        {
            try
            {
                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.usw, n, item.Key);
                    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(), "0($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, "0($sp)");



                //s += MIPS.Emit(MIPS.Opcodes.sw, MIPS.LastReg(), "0($sp)");
            }
            //s += MIPS.Emit(MIPS.Opcodes.move, "$s0", "$ra");
            s += MIPS.Emit(MIPS.Opcodes.jal, callerTypeName + "." + 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", "0($sp)");
            }
            //s += MIPS.Emit(MIPS.Opcodes.subu, "$sp","$sp", "4");

            //SymbolTable.Symbols.Pop();

            return(s);// base.GenerateCode();
        }
Beispiel #14
0
        /// <summary>
        /// Build the ctor method with className: label
        /// </summary>
        /// <returns></returns>
        public override string GenerateCode()
        {
            var s = "";
            var t = Attributes.Count;

            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());
                s += MIPS.Emit(MIPS.Opcodes.sw, reg1, "($sp)");

                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, "0(" + reg1 + ")");

                //put class size
                s += MIPS.Emit(MIPS.Opcodes.li, reg2, Size.ToString());
                s += MIPS.Emit(MIPS.Opcodes.sw, reg2, "4(" + 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")
                {
                    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 Methods)
            {
                s += item.Value.GenerateCode();
            }

            return(s);// +base.GenerateCode();
        }