예제 #1
0
 private string GetExpressionString(Expression expression)
 {
     if( expression is DecValue)
     {
         return expression.ToString();
     }
     throw new NotImplementedException();
 }
 public IdDeclarationStatement(string id, Expression initializationExpression, IRType type)
 {
     this.Id = id;
     this.InitializationExpression = initializationExpression;
     this.Type = type;
 }
 public IdDeclarationStatement(string id, Expression initializationExpression)
     : this(id)
 {
     this.InitializationExpression = initializationExpression;
 }
예제 #4
0
 public SubExpression(Expression left, Expression right)
 {
     this.Left = left;
     this.Right = right;
 }
예제 #5
0
        private string WriteExpr(Expression expr, RegisterFile registerFile, StringBuilder programBuilder, string registerToUse = "")
        {
            #region INT_EXPRESSIONS
            if (expr == null)
            {
                return "";

            }
            else if (expr is DecValue)
            {
                var register = expr.ToString();
                for (int x = 0; x < registerFile.stack.Count(); x++)
                {
                    if (registerFile.stack.ElementAt(x).Key == savedValue)
                    {
                        indexFound = true;
                        //inStack = true;
                        inCycle = true;
                        register = registerFile.RegisterPosition(savedValue);//registerFile.stack.ElementAt(x).Value.positioninStack;

                        registerFile.stackx86.positioninStack = register;
                        registerFile.stackx86.idValue = expr.ToString();
                        registerFile.stack[registerFile.stack.ElementAt(x).Key] = registerFile.stackx86;

                    }
                    else
                        for (int i = 0; i < registerFile.savedTemporals.Count(); i++)
                        {
                            if (registerFile.savedTemporals.ElementAt(i).Key == savedValue)
                            {
                                indexFound = false;
                                register = registerFile.RegisterPosition(savedValue);//registerFile.stack.ElementAt(i).Value.positioninStack;
                                break;
                            }
                        }
                }

                if (indexFound == true && inCycle == true)//inStack == true)
                {
                    programBuilder.AppendFormat("\t mov DWORD PTR [ebp-{0}], {1}", register, expr.ToString());
                    programBuilder.AppendFormat("\t# Mueve {0} a la posición en la pila {1}", expr.ToString(), register);
                    programBuilder.AppendLine();
                    register = string.Format("DWORD PTR [ebp-{0}]", register);
                    indexFound = false;
                }
                else
                    if (indexFound == false || inStack == true || inCycle == true)
                    {
                        var reg = registerFile.FirstAvailableRegister();
                        programBuilder.AppendFormat("\t mov {0} , {1}", reg, expr.ToString());
                        programBuilder.AppendFormat("\t# Mueve {0} a {1}", expr.ToString(), reg);
                        programBuilder.AppendLine();
                        register = reg;

                    }

                return register;
            }
            else if (expr is IdValue)
            {
                var register = expr.ToString();
                var firstAvailableReg = registerFile.FirstAvailableRegister();

                for (int x = 0; x < registerFile.stack.Count(); x++)
                {
                    if (registerFile.stack.ElementAt(x).Key == register)
                    {
                        register = registerFile.stack.ElementAt(x).Value.positioninStack;
                    }
                }
                programBuilder.AppendFormat("\t mov {0}, DWORD PTR [ebp-{1}]", firstAvailableReg, register);
                programBuilder.AppendFormat("\t# Mueve {0} a la pos ebp - {1}", expr.ToString(), register);
                programBuilder.AppendLine();
                register = firstAvailableReg;
                registerFile.FreeRegister(firstAvailableReg);

                return register;
            }
            else if (expr is AddExpression)
            {
                if (string.IsNullOrEmpty(registerToUse))
                {
                    registerToUse = registerFile.FirstAvailableRegister();
                }
                var add = expr as AddExpression;
                var leftRegister = WriteExpr(add.Right, registerFile, programBuilder);
                var register = registerToUse;
                programBuilder.AppendFormat("\t add {0}, {1}", register, leftRegister);
                programBuilder.AppendFormat("\t\t# {0} + {1}", register, leftRegister);
                programBuilder.AppendLine();
                registerFile.FreeRegister(leftRegister);

                var rightRegister = WriteExpr(add.Left, registerFile, programBuilder, register);

                if (register != rightRegister)
                {
                    programBuilder.AppendFormat("\t add {0}, {1}", register, rightRegister);
                    programBuilder.AppendFormat("\t\t# {0} + {1}", register, rightRegister);
                    programBuilder.AppendLine();
                    registerFile.FreeRegister(rightRegister);

                }
                return register;
            }
            else if (expr is SubExpression)
            {
                if (string.IsNullOrEmpty(registerToUse))
                {
                    registerToUse = registerFile.FirstAvailableRegister();
                }
                var add = expr as SubExpression;
                var leftRegister = WriteExpr(add.Right, registerFile, programBuilder);
                var register = registerToUse;
                programBuilder.AppendFormat("\t sub {0}, {1}", register, leftRegister);
                programBuilder.AppendFormat("\t\t# {0} - {1}", register, leftRegister);
                programBuilder.AppendLine();
                registerFile.FreeRegister(leftRegister);

                var rightRegister = WriteExpr(add.Left, registerFile, programBuilder, register);

                if (register != rightRegister)
                {
                    programBuilder.AppendFormat("\t sub {0}, {1}", register, rightRegister);
                    programBuilder.AppendFormat("\t\t# {0} - {1}", register, rightRegister);
                    programBuilder.AppendLine();
                    registerFile.FreeRegister(rightRegister);

                }
                return register;

            }
            else if (expr is MulExpression)
            {
                if (string.IsNullOrEmpty(registerToUse))
                {
                    registerToUse = registerFile.FirstAvailableRegister();
                }
                var add = expr as MulExpression;
                var leftRegister = WriteExpr(add.Right, registerFile, programBuilder);
                var register = registerToUse;
                programBuilder.AppendFormat("\t mul {0}, {1}", register, leftRegister);
                programBuilder.AppendFormat("\t\t# {0} * {1}", register, leftRegister);
                programBuilder.AppendLine();
                registerFile.FreeRegister(leftRegister);

                var rightRegister = WriteExpr(add.Left, registerFile, programBuilder, register);

                if (register != rightRegister)
                {
                    programBuilder.AppendFormat("\t mul {0}, {1}", register, rightRegister);
                    programBuilder.AppendFormat("\t\t# {0} * {1}", register, rightRegister);
                    programBuilder.AppendLine();
                    registerFile.FreeRegister(rightRegister);

                }
                return register;

            }
            else if (expr is DivisionExpression)
            {
                if (string.IsNullOrEmpty(registerToUse))
                {
                    registerToUse = registerFile.FirstAvailableRegister();
                }
                var add = expr as DivisionExpression;
                var leftRegister = WriteExpr(add.Right, registerFile, programBuilder);
                var register = registerToUse;
                programBuilder.AppendFormat("\t div {0}, {1}", register, leftRegister);
                programBuilder.AppendFormat("\t\t# {0} / {1}", register, leftRegister);
                programBuilder.AppendLine();
                registerFile.FreeRegister(leftRegister);

                var rightRegister = WriteExpr(add.Left, registerFile, programBuilder, register);

                if (register != rightRegister)
                {
                    programBuilder.AppendFormat("\t div {0}, {1}", register, rightRegister);
                    programBuilder.AppendFormat("\t\t# {0} / {1}", register, rightRegister);
                    programBuilder.AppendLine();
                    registerFile.FreeRegister(rightRegister);

                }
                return register;
            }
            else if (expr is ModExpression)
            {
                if (string.IsNullOrEmpty(registerToUse))
                {
                    registerToUse = registerFile.FirstAvailableRegister();
                }
                var add = expr as ModExpression;
                var leftRegister = WriteExpr(add.Right, registerFile, programBuilder);
                var register = registerToUse;
                programBuilder.AppendFormat("\t mov {0}, {1}", register, leftRegister);
                programBuilder.AppendLine();
                programBuilder.AppendFormat("\t sar {0}, 31", leftRegister);
                programBuilder.AppendLine();
               // register = leftRegister;
                registerFile.FreeRegister(leftRegister);

                var rightRegister = WriteExpr(add.Left, registerFile, programBuilder, register);

                if (register != rightRegister)
                {
                    programBuilder.AppendFormat("\t idiv {0}", rightRegister);
                    programBuilder.AppendFormat("\t\t# {0} % {1}", register, rightRegister);
                    programBuilder.AppendLine();
                    registerFile.FreeRegister(rightRegister);

                }
                return register;
            }
            else if (expr is ModExpression)
            {
                if (string.IsNullOrEmpty(registerToUse))
                {
                    registerToUse = registerFile.FirstAvailableRegister();
                }
                var add = expr as ModExpression;
                var leftRegister = WriteExpr(add.Right, registerFile, programBuilder);
                var register = registerToUse;
                programBuilder.AppendFormat("\t div {0}, {1}", register, leftRegister);
                programBuilder.AppendFormat("\t\t# {0} % {1}", register, leftRegister);
                programBuilder.AppendLine();
                registerFile.FreeRegister(leftRegister);

                var rightRegister = WriteExpr(add.Left, registerFile, programBuilder, register);

                if (register != rightRegister)
                {
                    programBuilder.AppendFormat("\t move {0}, {1}", register, rightRegister);
                    programBuilder.AppendFormat("\t\t# {0} / {1}", register, rightRegister);
                    programBuilder.AppendLine();
                    programBuilder.AppendFormat("\t mfhi {0}", rightRegister);
                    programBuilder.AppendFormat("\t\t# Pasa el valor {0} para guardar el MOD", rightRegister);
                    programBuilder.AppendLine();
                    registerFile.FreeRegister(rightRegister);

                }
                return register;
            }
            #endregion

            #region BITWISE_OP_EXPRESSIONS
            else if (expr is BitwiseAndExpression)
            {
                if (string.IsNullOrEmpty(registerToUse))
                {
                    registerToUse = registerFile.FirstAvailableRegister();
                }
                var add = expr as BitwiseAndExpression;
                var leftRegister = WriteExpr(add.Right, registerFile, programBuilder);
                var register = registerToUse;
                programBuilder.AppendFormat("\t and {0}, {1}", register, leftRegister);
                programBuilder.AppendFormat("\t\t# {0} & {1}", register, leftRegister);
                programBuilder.AppendLine();
                registerFile.FreeRegister(leftRegister);

                var rightRegister = WriteExpr(add.Left, registerFile, programBuilder, register);

                if (register != rightRegister)
                {
                    programBuilder.AppendFormat("\t and {0}, {1}", register, rightRegister);
                    programBuilder.AppendFormat("\t\t# {0} & {1}", register, rightRegister);
                    programBuilder.AppendLine();
                    registerFile.FreeRegister(rightRegister);

                }
                return register;
            }
            else if (expr is BitwiseOrExpression)
            {
                if (string.IsNullOrEmpty(registerToUse))
                {
                    registerToUse = registerFile.FirstAvailableRegister();
                }
                var add = expr as BitwiseOrExpression;
                var leftRegister = WriteExpr(add.Right, registerFile, programBuilder);
                var register = registerToUse;
                programBuilder.AppendFormat("\t or {0}, {1}", register, leftRegister);
                programBuilder.AppendFormat("\t\t# {0} | {1}", register, leftRegister);
                programBuilder.AppendLine();
                registerFile.FreeRegister(leftRegister);

                var rightRegister = WriteExpr(add.Left, registerFile, programBuilder, register);

                if (register != rightRegister)
                {
                    programBuilder.AppendFormat("\t or {0}, {1}", register, rightRegister);
                    programBuilder.AppendFormat("\t\t# {0} | {1}", register, rightRegister);
                    programBuilder.AppendLine();
                    registerFile.FreeRegister(rightRegister);

                }
                return register;
            }
            else if (expr is BitwiseXorExpression)
            {
                if (string.IsNullOrEmpty(registerToUse))
                {
                    registerToUse = registerFile.FirstAvailableRegister();
                }
                var add = expr as BitwiseXorExpression;
                var leftRegister = WriteExpr(add.Right, registerFile, programBuilder);
                var register = registerToUse;
                programBuilder.AppendFormat("\t xor {0}, {1}", register, leftRegister);
                programBuilder.AppendFormat("\t\t# {0} ^ {1}", register, leftRegister);
                programBuilder.AppendLine();
                registerFile.FreeRegister(leftRegister);

                var rightRegister = WriteExpr(add.Left, registerFile, programBuilder, register);

                if (register != rightRegister)
                {
                    programBuilder.AppendFormat("\t xor {0}, {1}", register, rightRegister);
                    programBuilder.AppendFormat("\t\t# {0} ^ {1}", register, rightRegister);
                    programBuilder.AppendLine();
                    registerFile.FreeRegister(rightRegister);

                }
                return register;
            }
            else if (expr is LessThanExpression)
            {
                if (string.IsNullOrEmpty(registerToUse))
                {
                    registerToUse = registerFile.FirstAvailableRegister();
                }
                var add = expr as LessThanExpression;
                var leftRegister = WriteExpr(add.Right, registerFile, programBuilder);
                var register = registerToUse;
                programBuilder.AppendFormat("\t mov {0}, {1}", register, leftRegister);
                programBuilder.AppendFormat("\t# Mueve {0} a {1} para hacer el cmp", register, leftRegister);
                programBuilder.AppendLine();
                registerFile.FreeRegister(leftRegister);

                var rightRegister = WriteExpr(add.Left, registerFile, programBuilder, register);

                if (register != rightRegister)
                {
                    programBuilder.AppendFormat("\t cmp {0}, {1}", register, rightRegister);
                    programBuilder.AppendFormat("\t\t# {0} < {1}", register, rightRegister);
                    programBuilder.AppendLine();
                    registerFile.FreeRegister(rightRegister);

                }
                labelCount += 1;
                programBuilder.AppendFormat("\t jge .L{0}", labelCount);
                programBuilder.AppendFormat("\t\t# Salta al label {0} si es >=.", labelCount);
                programBuilder.AppendLine();
                programBuilder.AppendFormat(".L{0}", labelCount);
                programBuilder.AppendLine();

                return register;

            }
            else if (expr is LessOrEqualToExpression)
            {
                if (string.IsNullOrEmpty(registerToUse))
                {
                    registerToUse = registerFile.FirstAvailableRegister();
                }
                var add = expr as LessOrEqualToExpression;
                var leftRegister = WriteExpr(add.Right, registerFile, programBuilder);
                var register = registerToUse;
                programBuilder.AppendFormat("\t mov {0}, {1}", register, leftRegister);
                programBuilder.AppendFormat("\t# Mueve {0} a {1} para hacer el cmp", register, leftRegister);
                programBuilder.AppendLine();
                registerFile.FreeRegister(leftRegister);

                var rightRegister = WriteExpr(add.Left, registerFile, programBuilder, register);

                if (register != rightRegister)
                {
                    programBuilder.AppendFormat("\t cmp {0}, {1}", register, rightRegister);
                    programBuilder.AppendFormat("\t\t# {0} < {1}", register, rightRegister);
                    programBuilder.AppendLine();
                    registerFile.FreeRegister(rightRegister);

                }
                labelCount += 1;
                programBuilder.AppendFormat("\t jg .L{0}", labelCount);
                programBuilder.AppendFormat("\t\t# Salta al label {0} si es >=.", labelCount);
                programBuilder.AppendLine();
                programBuilder.AppendFormat(".L{0}", labelCount);
                programBuilder.AppendLine();

                return register;
            }
            else if (expr is GreaterThanExpression)
            {
                if (string.IsNullOrEmpty(registerToUse))
                {
                    registerToUse = registerFile.FirstAvailableRegister();
                }
                var add = expr as GreaterThanExpression;
                var leftRegister = WriteExpr(add.Right, registerFile, programBuilder);
                var register = registerToUse;
                programBuilder.AppendFormat("\t mov {0}, {1}", register, leftRegister);
                programBuilder.AppendFormat("\t# Mueve {0} a {1} para hacer el cmp", register, leftRegister);
                programBuilder.AppendLine();
                registerFile.FreeRegister(leftRegister);

                var rightRegister = WriteExpr(add.Left, registerFile, programBuilder, register);

                if (register != rightRegister)
                {
                    programBuilder.AppendFormat("\t cmp {0}, {1}", register, rightRegister);
                    programBuilder.AppendFormat("\t\t# {0} < {1}", register, rightRegister);
                    programBuilder.AppendLine();
                    registerFile.FreeRegister(rightRegister);

                }
                labelCount += 1;
                programBuilder.AppendFormat("\t jle .L{0}", labelCount);
                programBuilder.AppendFormat("\t\t# Salta al label {0} si es <.", labelCount);
                programBuilder.AppendLine();
                programBuilder.AppendFormat(".L{0}", labelCount);
                programBuilder.AppendLine();

                return register;
            }
            else if (expr is GreaterOrEqualExpression)
            {
                if (string.IsNullOrEmpty(registerToUse))
                {
                    registerToUse = registerFile.FirstAvailableRegister();
                }
                var add = expr as GreaterOrEqualExpression;
                var leftRegister = WriteExpr(add.Right, registerFile, programBuilder);
                var register = registerToUse;
                programBuilder.AppendFormat("\t mov {0}, {1}", register, leftRegister);
                programBuilder.AppendFormat("\t# Mueve {0} a {1} para hacer el cmp", register, leftRegister);
                programBuilder.AppendLine();
                registerFile.FreeRegister(leftRegister);

                var rightRegister = WriteExpr(add.Left, registerFile, programBuilder, register);

                if (register != rightRegister)
                {
                    programBuilder.AppendFormat("\t cmp {0}, {1}", register, rightRegister);
                    programBuilder.AppendFormat("\t\t# {0} < {1}", register, rightRegister);
                    programBuilder.AppendLine();
                    registerFile.FreeRegister(rightRegister);

                }
                labelCount += 1;
                programBuilder.AppendFormat("\t jl .L{0}", labelCount);
                programBuilder.AppendFormat("\t\t# Salta al label {0} si es <.", labelCount);
                programBuilder.AppendLine();
                programBuilder.AppendFormat(".L{0}", labelCount);
                programBuilder.AppendLine();

                return register;

            }
            else if (expr is EqualsExpression)
            {
                if (string.IsNullOrEmpty(registerToUse))
                {
                    registerToUse = registerFile.FirstAvailableRegister();
                }
                var add = expr as EqualsExpression;
                var leftRegister = WriteExpr(add.Right, registerFile, programBuilder);
                var register = registerToUse;
                programBuilder.AppendFormat("\t mov {0}, {1}", register, leftRegister);
                programBuilder.AppendFormat("\t# Mueve {0} a {1} para hacer el cmp", register, leftRegister);
                programBuilder.AppendLine();
                registerFile.FreeRegister(leftRegister);

                var rightRegister = WriteExpr(add.Left, registerFile, programBuilder, register);

                if (register != rightRegister)
                {
                    programBuilder.AppendFormat("\t cmp {0}, {1}", register, rightRegister);
                    programBuilder.AppendFormat("\t\t# {0} < {1}", register, rightRegister);
                    programBuilder.AppendLine();
                    registerFile.FreeRegister(rightRegister);

                }
                labelCount += 1;
                programBuilder.AppendFormat("\t jne .L{0}", labelCount);
                programBuilder.AppendFormat("\t\t# Salta al label {0} si es <.", labelCount);
                programBuilder.AppendLine();

                return register;

            }
            #endregion

            #region LOGICAL_EXPR
            else if (expr is LogicalOrExpression)
            {
                if (string.IsNullOrEmpty(registerToUse))
                {
                    registerToUse = registerFile.FirstAvailableRegister();
                }
                var add = expr as BitwiseAndExpression;
                var leftRegister = WriteExpr(add.Right, registerFile, programBuilder);
                var register = registerToUse;
                programBuilder.AppendFormat("\t cmp {0}, 0", leftRegister);
                programBuilder.AppendFormat("\t\t# Compara {0} con 0", leftRegister);
                programBuilder.AppendLine();
                programBuilder.AppendFormat("\t jne .L{0}", labelCount);
                programBuilder.AppendLine();
                registerFile.FreeRegister(leftRegister);

                var rightRegister = WriteExpr(add.Left, registerFile, programBuilder, register);

                if (register != rightRegister)
                {
                    programBuilder.AppendFormat("\t cmp {0}, 0", rightRegister);
                    programBuilder.AppendFormat("\t\t# Compara {0} con 0", rightRegister);
                    programBuilder.AppendLine();
                    programBuilder.AppendFormat("\t jne .L{0}", labelCount);
                    programBuilder.AppendLine();
                    registerFile.FreeRegister(rightRegister);

                }
                return register;

            }
            else if (expr is LogicAndExpression)
            {
                {
                    registerToUse = registerFile.FirstAvailableRegister();
                }
                var add = expr as BitwiseAndExpression;
                var leftRegister = WriteExpr(add.Right, registerFile, programBuilder);
                var register = registerToUse;
                programBuilder.AppendFormat("\t cmp {0}, 0", leftRegister);
                programBuilder.AppendFormat("\t\t# Compara {0} con 0", leftRegister);
                programBuilder.AppendLine();
                programBuilder.AppendFormat("\t je .L{0}", labelCount);
                programBuilder.AppendLine();
                registerFile.FreeRegister(leftRegister);

                var rightRegister = WriteExpr(add.Left, registerFile, programBuilder, register);

                if (register != rightRegister)
                {
                    programBuilder.AppendFormat("\t cmp {0}, 0", rightRegister);
                    programBuilder.AppendFormat("\t\t# Compara {0} con 0", rightRegister);
                    programBuilder.AppendLine();
                    programBuilder.AppendFormat("\t je .L{0}", labelCount);
                    programBuilder.AppendLine();
                    registerFile.FreeRegister(rightRegister);

                }
                return register;

            #endregion

            #region ASSIGN_EXPRESSIONS
            }
            else if (expr is AdditionAssignmentExpression)
            {
                var add = expr as AdditionAssignmentExpression;
                var leftRegister = WriteExpr(add.Right, registerFile, programBuilder);
                programBuilder.AppendFormat("\t add {0}, {1}", leftRegister, add.Left);
                programBuilder.AppendFormat("\t# {0}", expr.ToString());
                programBuilder.AppendLine();

            }
            else if (expr is SubtractionAsigExpression)
            {
                var sub = expr as SubtractionAsigExpression;
                var leftRegister = WriteExpr(sub.Right, registerFile, programBuilder);
                programBuilder.AppendFormat("\t sub {0}, {1}", leftRegister, sub.Left);
                programBuilder.AppendFormat("\t# {0}", expr.ToString());
                programBuilder.AppendLine();

            }
            else if (expr is MultiplicationAsigExpression)
            {
                var sub = expr as MultiplicationAsigExpression;
                var leftRegister = WriteExpr(sub.Right, registerFile, programBuilder);
                programBuilder.AppendFormat("\t mul {0}, {1}", leftRegister, sub.Left);
                programBuilder.AppendFormat("\t# {0}", expr.ToString());
                programBuilder.AppendLine();

            }
            else if (expr is DivisionAsigExpression)
            {
                var sub = expr as DivisionAsigExpression;
                var leftRegister = WriteExpr(sub.Right, registerFile, programBuilder);
                programBuilder.AppendFormat("\t div {0}, {1}", leftRegister, sub.Left);
                programBuilder.AppendFormat("\t# {0}", expr.ToString());
                programBuilder.AppendLine();
            }
            else if (expr is AssignExpression)
            {
                var assign = expr as AssignExpression;

                foreach (var id in registerFile.stack)
                {
                    if (id.Key == assign.Left.ToString())
                    {
                        savedValue = id.Key;
                        inStack = true;
                    }
                }

                if (inStack == false)
                {
                    var pos = registerFile.stack.Last().Value.positioninStack;
                    registerFile.stackx86.idValue = assign.Right.ToString();
                    savedValue = assign.Left.ToString();
                    registerFile.stackx86.positioninStack = Convert.ToString(Convert.ToInt32(pos) + 4);
                    registerFile.stack.Add(assign.Left.ToString(), registerFile.stackx86);
                    inStack = true;
                }

                var assignRegister = WriteExpr(assign.Right, registerFile, programBuilder);

                if (assign.Left is PointerArrayAccessExpr)
                {
                    isArray = true;
                    registerFile.FreeRegister(assignRegister);
                    WriteExpr(assign.Left, registerFile, programBuilder);
                    return assignRegister;
                }
                else
                {
                    if (inStack == false)
                    {
                        programBuilder.AppendFormat("\t mov {0}, {1}", assignRegister, assign.Left.ToString());
                        programBuilder.AppendLine();
                    }

                    inStack = false;
                    return assignRegister;
                }
            }
                #endregion

            #region POST_PRE_INCREMENT_DECREMENT
            else if (expr is PostIncrementExpression)
            {
                var postIncrement = expr as PostIncrementExpression;
                var register = registerFile.RegisterPosition(postIncrement.Expression.ToString());
                programBuilder.AppendFormat("\t add DWORD PTR [epb-{0}], 1", register);
                programBuilder.AppendFormat("\t# {0}", expr.ToString());
                programBuilder.AppendLine();

                return register;
            }
            else if (expr is PreIncrementExpression)
            {

            }
            else if (expr is PostDecrementExpression)
            {

            }
            else if (expr is PreDecrementExpression)
            {
            }
            #endregion

            #region FUNTIONCALL_EXPRESSION
            else if (expr is FunctionCallExpression)
            {
                WriteFunctionCallExp(expr as FunctionCallExpression, programBuilder);
            }
            #endregion

            #region ARRAY_EXPRESSION
            else if (expr is PointerArrayAccessExpr)
            {

            }
            #endregion

            return registerFile.FirstAvailableRegister();
        }
예제 #6
0
        private string WriteExpr(Expression expr, RegisterFile registerFile, StringBuilder programBuilder, string registerToUse = "")
        {
            #region INT_EXPRESSIONS
            if (expr == null)
            {
                return "";

            }else if (expr is DecValue)
            {
                var register = expr.ToString();

                if (inStack == true)
                {
                    for (int x = 0; x < registerFile.savedValuesList.Count; x++)
                    {
                        if (registerFile.savedValuesList.ElementAt(x).Key == savedValue)
                        {
                            registerFile.stack1.id = registerFile.savedValuesList.ElementAt(x).Value.id;
                            registerFile.stack1.value = expr.ToString();
                            registerFile.savedValuesList[registerFile.savedValuesList.ElementAt(x).Key] = registerFile.stack1;
                        }

                    }
                   register = savedValue;
                }
                else if (param == true)
                {
                    for (int x = 0; x < registerFile.savedArguments.Count; x++)
                    {
                        if (registerFile.savedArguments.ElementAt(x).Value == expr.ToString())
                        {
                            register = registerFile.savedArguments.ElementAt(x).Key;
                        }
                    }

                }else
                {
                    register = registerFile.FirstAvailableRegister();

                }

                programBuilder.AppendFormat("\taddi {0}, $zero, {1}", register, expr.ToString());
              programBuilder.AppendFormat("\t# Agrega {0} al registro {1}", expr.ToString(), register);
              programBuilder.AppendLine();
              return register;

            }
            else if(expr is IdValue)
            {
                var register =  registerFile.FirstAvailableSavedValue();
                if (inStack == true && param == false)
                {
                    if (!string.IsNullOrEmpty(registerFile.FirstAvailableSavedValue()))
                    {
                        for (int x = 0; x < registerFile.savedValuesList.Count; x++)
                        {
                            if (registerFile.savedValuesList.ElementAt(x).Key == savedValue)
                            {
                                registerFile.stack1.id = registerFile.savedValuesList.ElementAt(x).Value.id;
                                registerFile.stack1.value = expr.ToString();
                                registerFile.savedValuesList[registerFile.savedValuesList.ElementAt(x).Key] = registerFile.stack1;
                                register = registerFile.savedValuesList.ElementAt(x).Key;
                            }
                        }
                    }
                }
                else if (inStack == false && param == false)
                {
                    for (int x = 0; x < registerFile.savedTemporals.Count; x++)
                    {
                        if (registerFile.savedTemporals.ElementAt(x).Value == expr.ToString())
                        {
                            register = registerFile.savedTemporals.ElementAt(x).Key;
                        }
                    }
                }else if(param == true)
                {
                    for (int x = 0; x < registerFile.savedArguments.Count; x++)
                    {
                        if (registerFile.savedArguments.ElementAt(x).Value == expr.ToString())
                        {
                            register = registerFile.savedArguments.ElementAt(x).Key;
                        }
                    }
                }

                return register;
            }
            else if (expr is AddExpression)
            {
                if (string.IsNullOrEmpty(registerToUse))
                {
                    registerToUse = registerFile.FirstAvailableRegister();
                    programBuilder.AppendFormat("\tadd {0}, $zero, $zero", registerToUse);
                    programBuilder.AppendFormat("\t# Inicializar la variable {0} con 0",registerToUse);
                    programBuilder.AppendLine();
                }
                var add = expr as AddExpression;
                var leftRegister = WriteExpr(add.Right,registerFile, programBuilder);
                var register = registerToUse;
                programBuilder.AppendFormat("\tadd {0}, {1}, {2}", register, register, leftRegister);
                programBuilder.AppendFormat("\t# {0} + {1}", register, leftRegister);
                programBuilder.AppendLine();
                registerFile.FreeRegister(leftRegister);
                registerFile.FreeArgument(leftRegister);
                registerFile.FreeSavedValue(leftRegister);

                var rightRegister = WriteExpr(add.Left, registerFile, programBuilder, register);
                if (register != rightRegister)
                {
                    programBuilder.AppendFormat("\tadd {0}, {1}, {2}", register, register, rightRegister);
                    programBuilder.AppendFormat("\t# {0} + {1}", register, rightRegister);
                    programBuilder.AppendLine();
                    registerFile.FreeRegister(rightRegister);
                    registerFile.FreeArgument(rightRegister);
                    registerFile.FreeSavedValue(rightRegister);
                }
                return register;

            }else if (expr is SubExpression)
            {
                if (string.IsNullOrEmpty(registerToUse))
                {
                    registerToUse = registerFile.FirstAvailableRegister();
                    programBuilder.AppendFormat("\tadd {0}, $zero, $zero", registerToUse);
                    programBuilder.AppendFormat("\t# Inicializar la variable {0} con 0", registerToUse);
                    programBuilder.AppendLine();
                }
                var add = expr as SubExpression;
                var leftRegister = WriteExpr(add.Right, registerFile, programBuilder);
                var register = registerToUse;
                programBuilder.AppendFormat("\tsub {0}, {1}, {2}", register, register, leftRegister);
                programBuilder.AppendLine();
                registerFile.FreeRegister(leftRegister);
                registerFile.FreeArgument(leftRegister);
                registerFile.FreeSavedValue(leftRegister);

                var rightRegister = WriteExpr(add.Left, registerFile, programBuilder, register);
                if (register != rightRegister)
                {
                    programBuilder.AppendFormat("\tsub {0}, {1}, {2}", register, register, rightRegister);
                    programBuilder.AppendLine();
                    registerFile.FreeRegister(rightRegister);
                    registerFile.FreeArgument(rightRegister);
                    registerFile.FreeSavedValue(rightRegister);
                }
                return register;

            }else if (expr is MulExpression)
            {
                if (string.IsNullOrEmpty(registerToUse))
                {
                    registerToUse = registerFile.FirstAvailableRegister();
                    programBuilder.AppendFormat("\tadd {0}, $zero, $zero", registerToUse);
                    programBuilder.AppendFormat("\t# Inicializar la variable {0} con 0", registerToUse);
                    programBuilder.AppendLine();
                }
                var add = expr as MulExpression;
                var leftRegister = WriteExpr(add.Right, registerFile, programBuilder);
                var register = registerToUse;
                programBuilder.AppendFormat("\tmult {0}, {1}, {2}", register, register, leftRegister);
                programBuilder.AppendLine();
                registerFile.FreeRegister(leftRegister);

                var rightRegister = WriteExpr(add.Left, registerFile, programBuilder, register);
                if (register != rightRegister)
                {
                    programBuilder.AppendFormat("\tmult {0}, {1}, {2}", register, register, rightRegister);
                    programBuilder.AppendLine();
                    registerFile.FreeRegister(rightRegister);
                }
                return register;

            }else if (expr is DivisionExpression)
            {
                if (string.IsNullOrEmpty(registerToUse))
                {
                    registerToUse = registerFile.FirstAvailableRegister();
                    programBuilder.AppendFormat("\tadd {0}, $zero, $zero", registerToUse);
                    programBuilder.AppendFormat("\t# Inicializar la variable {0} con 0", registerToUse);
                    programBuilder.AppendLine();
                }
                var add = expr as DivisionExpression;
                var leftRegister = WriteExpr(add.Right, registerFile, programBuilder);
                var register = registerToUse;
                programBuilder.AppendFormat("\tdiv {0}, {1}, {2}", register, register, leftRegister);
                programBuilder.AppendLine();
                registerFile.FreeRegister(leftRegister);

                var rightRegister = WriteExpr(add.Left, registerFile, programBuilder, register);
                if (register != rightRegister)
                {
                    programBuilder.AppendFormat("\tdiv {0}, {1}, {2}", register, register, rightRegister);
                    programBuilder.AppendLine();
                    registerFile.FreeRegister(rightRegister);
                }
                return register;

            }
            else if (expr is ModExpression)
            {
                if (string.IsNullOrEmpty(registerToUse))
                {
                    registerToUse = registerFile.FirstAvailableRegister();
                    programBuilder.AppendFormat("\tadd {0}, $zero, $zero", registerToUse);
                    programBuilder.AppendFormat("\t# Inicializar la variable {0} con 0", registerToUse);
                    programBuilder.AppendLine();
                }
                var add = expr as ModExpression;
                var leftRegister = WriteExpr(add.Right, registerFile, programBuilder);
                var register = registerToUse;
                programBuilder.AppendFormat("\tdiv {1}, {2}", register, register, leftRegister);
                programBuilder.AppendLine();
                register = leftRegister;
                registerFile.FreeRegister(leftRegister);

                var rightRegister = WriteExpr(add.Left, registerFile, programBuilder, register);
                if (register != rightRegister)
                {
                    programBuilder.AppendFormat("\tdiv {0}, {1}",  rightRegister, leftRegister);
                    programBuilder.AppendLine();
                    programBuilder.AppendFormat("\tmove {0}, {1}", rightRegister, register);
                    programBuilder.AppendLine();
                    programBuilder.AppendFormat("\tmfhi {0}", register);
                    programBuilder.AppendFormat("\t# Mueve el HI de {0}", register);
                    programBuilder.AppendLine();
                    registerFile.FreeRegister(rightRegister);
                }
                return register;

            }
            #endregion

            #region BITWISE_OP_EXPRESSIONS
            else if (expr is BitwiseAndExpression)
            {
                if (string.IsNullOrEmpty(registerToUse))
                {
                    registerToUse = registerFile.FirstAvailableRegister();
                    programBuilder.AppendFormat("\tadd {0}, $zero, $zero", registerToUse);
                    programBuilder.AppendFormat("\t# Inicializar la variable {0} con 0", registerToUse);
                    programBuilder.AppendLine();
                }
                var add = expr as BitwiseAndExpression;
                var leftRegister = WriteExpr(add.Right, registerFile, programBuilder);
                var register = registerToUse;
                programBuilder.AppendFormat("\tand {0}, {1}, {2}", register, register, leftRegister);
                programBuilder.AppendLine();
                registerFile.FreeRegister(leftRegister);

                var rightRegister = WriteExpr(add.Left, registerFile, programBuilder, register);
                if (register != rightRegister)
                {
                    programBuilder.AppendFormat("\tand {0}, {1}, {2}", register, register, rightRegister);
                    programBuilder.AppendLine();
                    registerFile.FreeRegister(rightRegister);
                }
                return register;

            }
            else if (expr is BitwiseOrExpression)
            {
                if (string.IsNullOrEmpty(registerToUse))
                {
                    registerToUse = registerFile.FirstAvailableRegister();
                    programBuilder.AppendFormat("\tadd {0}, $zero, $zero", registerToUse);
                    programBuilder.AppendFormat("\t# Inicializar la variable {0} con 0", registerToUse);
                    programBuilder.AppendLine();
                }
                var add = expr as BitwiseOrExpression;
                var leftRegister = WriteExpr(add.Right, registerFile, programBuilder);
                var register = registerToUse;
                programBuilder.AppendFormat("\tor {0}, {1}, {2}", register, register, leftRegister);
                programBuilder.AppendLine();
                registerFile.FreeRegister(leftRegister);

                var rightRegister = WriteExpr(add.Left, registerFile, programBuilder, register);
                if (register != rightRegister)
                {
                    programBuilder.AppendFormat("\tor {0}, {1}, {2}", register, register, rightRegister);
                    programBuilder.AppendLine();
                    registerFile.FreeRegister(rightRegister);
                }
                return register;

            }
            else if (expr is BitwiseXorExpression)
            {
                if (string.IsNullOrEmpty(registerToUse))
                {
                    registerToUse = registerFile.FirstAvailableRegister();
                    programBuilder.AppendFormat("\tadd {0}, $zero, $zero", registerToUse);
                    programBuilder.AppendFormat("\t# Inicializar la variable {0} con 0", registerToUse);
                    programBuilder.AppendLine();
                }
                var add = expr as BitwiseXorExpression;
                var leftRegister = WriteExpr(add.Right, registerFile, programBuilder);
                var register = registerToUse;
                programBuilder.AppendFormat("\txor {0}, {1}, {2}", register, register, leftRegister);
                programBuilder.AppendLine();
                registerFile.FreeRegister(leftRegister);

                var rightRegister = WriteExpr(add.Left, registerFile, programBuilder, register);
                if (register != rightRegister)
                {
                    programBuilder.AppendFormat("\txor {0}, {1}, {2}", register, register, rightRegister);
                    programBuilder.AppendLine();
                    registerFile.FreeRegister(rightRegister);
                }
                return register;

            }
            else if (expr is LessThanExpression)
            {
                if (string.IsNullOrEmpty(registerToUse))
                {
                    registerToUse = registerFile.FirstAvailableRegister();
                    programBuilder.AppendFormat("\tadd {0}, $zero, $zero", registerToUse);
                    programBuilder.AppendFormat("\t# Inicializar la variable {0} con 0", registerToUse);
                    programBuilder.AppendLine();
                }
                var slt = expr as LessThanExpression;
                var leftRegister = WriteExpr(slt.Right, registerFile, programBuilder);
                var register = registerToUse;
                programBuilder.AppendFormat("\tslt {0}, {1}, {2}", register, register, leftRegister);
                programBuilder.AppendFormat("\t# Evalua {0} < {1} ", slt.Left.ToString(),slt.Right.ToString());
                programBuilder.AppendLine();
                registerFile.FreeRegister(leftRegister);

                var rightRegister = WriteExpr(slt.Left, registerFile, programBuilder, register);
                if (register != rightRegister)
                {
                    programBuilder.AppendFormat("\tslt {0}, {1}, {2}", register, register, rightRegister);
                    programBuilder.AppendLine();
                    registerFile.FreeRegister(rightRegister);
                }
                return register;
            }
            else if (expr is LessOrEqualToExpression)
            {
              /*  if (string.IsNullOrEmpty(registerToUse))
                {
                    registerToUse = registerFile.FirstAvailableRegister();
                    programBuilder.AppendFormat("add {0}, $zero, $zero", registerToUse);
                    programBuilder.AppendFormat("\t# Inicializar la variable {0} con 0", registerToUse);
                    programBuilder.AppendLine();
                }
                var slt = expr as LessOrEqualToExpression;
                var leftRegister = WriteExpr(slt.Right, registerFile, programBuilder);
                var register = registerToUse;
                programBuilder.AppendFormat("ble {0}, {1}, {2}", register, register, leftRegister);
                programBuilder.AppendFormat("\t# Evalua {0} < {1} ", slt.Left.ToString(), slt.Right.ToString());
                programBuilder.AppendLine();
                registerFile.FreeRegister(leftRegister);

                var rightRegister = WriteExpr(slt.Left, registerFile, programBuilder, register);
                if (register != rightRegister)
                {
                    programBuilder.AppendFormat("ble {0}, {1}, {2}", register, register, rightRegister);
                    programBuilder.AppendLine();
                    registerFile.FreeRegister(rightRegister);
                }
                return register;*/
                var slt = expr as LessOrEqualToExpression;
                var leftRegister = WriteExpr(slt.Right, registerFile, programBuilder);
                // var register = registerToUse;
                var rightRegister = WriteExpr(slt.Left, registerFile, programBuilder);
                var register = rightRegister;
                programBuilder.AppendFormat("\tble {0}, {1}, _BLE", rightRegister, leftRegister);
                programBuilder.AppendFormat("\t# Evalua {0} > {1} ", slt.Left.ToString(), slt.Right.ToString());
                programBuilder.AppendLine();
                programBuilder.AppendLine("_BLE: ");

                registerFile.FreeRegister(leftRegister);
                registerFile.FreeArgument(leftRegister);
                registerFile.FreeSavedValue(leftRegister);
                registerFile.FreeRegister(rightRegister);
                registerFile.FreeArgument(rightRegister);
                registerFile.FreeSavedValue(rightRegister);

                return register;
            }
            else if (expr is GreaterThanExpression)
            {
               /* if (string.IsNullOrEmpty(registerToUse))
                {
                    registerToUse = registerFile.FirstAvailableRegister();
                    programBuilder.AppendFormat("add {0}, $zero, $zero", registerToUse);
                    programBuilder.AppendFormat("\t# Inicializar la variable {0} con 0", registerToUse);
                    programBuilder.AppendLine();
                }*/
                var slt = expr as GreaterThanExpression;
                var leftRegister = WriteExpr(slt.Right, registerFile, programBuilder);
               // var register = registerToUse;
                var rightRegister = WriteExpr(slt.Left, registerFile, programBuilder);
                programBuilder.AppendFormat("\tbgt {0}, {1}, _BGT", leftRegister, rightRegister);
                programBuilder.AppendFormat("\t# Evalua {0} > {1} ", slt.Left.ToString(), slt.Right.ToString());
                programBuilder.AppendLine();
                programBuilder.AppendLine("_BGT: ");

                registerFile.FreeRegister(leftRegister);
                registerFile.FreeArgument(leftRegister);
                registerFile.FreeSavedValue(leftRegister);
                registerFile.FreeRegister(rightRegister);
                registerFile.FreeArgument(rightRegister);
                registerFile.FreeSavedValue(rightRegister);

              /*  var rightRegister = WriteExpr(slt.Left, registerFile, programBuilder, register);
                if (register != rightRegister)
                {
                    programBuilder.AppendFormat("bgt {0}, {1}, {2}", register, register, rightRegister);
                    programBuilder.AppendLine();
                    registerFile.FreeRegister(rightRegister);
                    registerFile.FreeArgument(rightRegister);
                    registerFile.FreeSavedValue(rightRegister);
                }*/
                return leftRegister;
            }
            else if (expr is GreaterOrEqualExpression)
            {
                /*if (string.IsNullOrEmpty(registerToUse))
                {
                    registerToUse = registerFile.FirstAvailableRegister();
                    programBuilder.AppendFormat("add {0}, $zero, $zero", registerToUse);
                    programBuilder.AppendFormat("\t# Inicializar la variable {0} con 0", registerToUse);
                    programBuilder.AppendLine();
                }
                var slt = expr as GreaterThanExpression;
                var leftRegister = WriteExpr(slt.Right, registerFile, programBuilder);
                var register = registerToUse;
                programBuilder.AppendFormat("bge {0}, {1}, {2}", register, register, leftRegister);
                programBuilder.AppendFormat("\t# Evalua {0} > {1} ", slt.Left.ToString(), slt.Right.ToString());
                programBuilder.AppendLine();
                registerFile.FreeRegister(leftRegister);

                var rightRegister = WriteExpr(slt.Left, registerFile, programBuilder, register);
                if (register != rightRegister)
                {
                    programBuilder.AppendFormat("bge {0}, {1}, {2}", register, register, rightRegister);
                    programBuilder.AppendLine();
                    registerFile.FreeRegister(rightRegister);
                }
                return register;*/
                var slt = expr as GreaterOrEqualExpression;
                var leftRegister = WriteExpr(slt.Right, registerFile, programBuilder);
                // var register = registerToUse;
                var rightRegister = WriteExpr(slt.Left, registerFile, programBuilder);
                var register = rightRegister;
                programBuilder.AppendFormat("\tbge {0}, {1}, _BGE", rightRegister, leftRegister);
                programBuilder.AppendFormat("\t# Evalua {0} > {1} ", slt.Left.ToString(), slt.Right.ToString());
                programBuilder.AppendLine();
                programBuilder.AppendLine("_BGE: ");

                registerFile.FreeRegister(leftRegister);
                registerFile.FreeArgument(leftRegister);
                registerFile.FreeSavedValue(leftRegister);
                registerFile.FreeRegister(rightRegister);
                registerFile.FreeArgument(rightRegister);
                registerFile.FreeSavedValue(rightRegister);

                return register;
            }
            else if (expr is EqualsExpression)
            {
              /*  if (string.IsNullOrEmpty(registerToUse))
                {
                    registerToUse = registerFile.FirstAvailableRegister();
                    programBuilder.AppendFormat("\tadd {0}, $zero, $zero", registerToUse);
                    programBuilder.AppendFormat("\t# Inicializar la variable {0} con 0", registerToUse);
                    programBuilder.AppendLine();
                }
                var slt = expr as EqualsExpression;
                var leftRegister = WriteExpr(slt.Right, registerFile, programBuilder);
                var register = registerToUse;
                programBuilder.AppendFormat("\tbeq {0}, {1}, {2}", register, register, leftRegister);
                programBuilder.AppendFormat("\t# Evalua {0} < {1} ", slt.Left.ToString(), slt.Right.ToString());
                programBuilder.AppendLine();
                registerFile.FreeRegister(leftRegister);

                var rightRegister = WriteExpr(slt.Left, registerFile, programBuilder, register);
                if (register != rightRegister)
                {
                    programBuilder.AppendFormat("\tbeq {0}, {1}, {2}", register, register, rightRegister);
                    programBuilder.AppendLine();
                    registerFile.FreeRegister(rightRegister);
                }
                return register;*/
                var slt = expr as EqualsExpression;
                var leftRegister = WriteExpr(slt.Right, registerFile, programBuilder);
                // var register = registerToUse;
                var rightRegister = WriteExpr(slt.Left, registerFile, programBuilder);
                var register = rightRegister;
                programBuilder.AppendFormat("\tbeq {0}, {1}, _BEQ", rightRegister, leftRegister);
                programBuilder.AppendFormat("\t# Evalua {0} == {1} ", slt.Left.ToString(), slt.Right.ToString());
                programBuilder.AppendLine();
                programBuilder.AppendLine("_BEQ: ");

                registerFile.FreeRegister(leftRegister);
                registerFile.FreeArgument(leftRegister);
                registerFile.FreeSavedValue(leftRegister);
                registerFile.FreeRegister(rightRegister);
                registerFile.FreeArgument(rightRegister);
                registerFile.FreeSavedValue(rightRegister);

                return register;

            }
            #endregion

            #region LOGICAL_EXPR
            else if (expr is LogicalOrExpression)
            {
                if (string.IsNullOrEmpty(registerToUse))
                {
                    registerToUse = registerFile.FirstAvailableRegister();
                    programBuilder.AppendFormat("\tadd {0}, $zero, $zero", registerToUse);
                    programBuilder.AppendFormat("\t# Inicializar la variable {0} con 0", registerToUse);
                    programBuilder.AppendLine();
                }
                var slt = expr as LogicalOrExpression;
                var leftRegister = WriteExpr(slt.Right, registerFile, programBuilder);
                var register = registerToUse;
                programBuilder.AppendFormat("\tor {0}, {1}, {2}", register, register, leftRegister);
                programBuilder.AppendFormat("\t# Evalua {0} < {1} ", slt.Left.ToString(), slt.Right.ToString());
                programBuilder.AppendLine();
                registerFile.FreeRegister(leftRegister);

                var rightRegister = WriteExpr(slt.Left, registerFile, programBuilder, register);
                if (register != rightRegister)
                {
                    programBuilder.AppendFormat("\tor {0}, {1}, {2}", register, register, rightRegister);
                    programBuilder.AppendLine();
                    registerFile.FreeRegister(rightRegister);
                }
                return register;

            }
            else if (expr is LogicAndExpression)
            {
                if (string.IsNullOrEmpty(registerToUse))
                {
                    registerToUse = registerFile.FirstAvailableRegister();
                    programBuilder.AppendFormat("\tadd {0}, $zero, $zero", registerToUse);
                    programBuilder.AppendFormat("\t# Inicializar la variable {0} con 0", registerToUse);
                    programBuilder.AppendLine();
                }
                var slt = expr as LogicAndExpression;
                var leftRegister = WriteExpr(slt.Right, registerFile, programBuilder);
                var register = registerToUse;
                programBuilder.AppendFormat("\tand {0}, {1}, {2}", register, register, leftRegister);
                programBuilder.AppendFormat("\t# Evalua {0} < {1} ", slt.Left.ToString(), slt.Right.ToString());
                programBuilder.AppendLine();
                registerFile.FreeRegister(leftRegister);

                var rightRegister = WriteExpr(slt.Left, registerFile, programBuilder, register);
                if (register != rightRegister)
                {
                    programBuilder.AppendFormat("\tand {0}, {1}, {2}", register, register, rightRegister);
                    programBuilder.AppendLine();
                    registerFile.FreeRegister(rightRegister);
                }
                return register;
            #endregion

            #region ASSIGN_EXPRESSIONS
            }
            else if (expr is AdditionAssignmentExpression)
            {
                var add = expr as AdditionAssignmentExpression;
                var leftRegister = WriteExpr(add.Right, registerFile, programBuilder);
                programBuilder.AppendFormat("\taddi $t0, {0}, {1}", leftRegister, add.Left);
                programBuilder.AppendFormat("\t# Inicializar la variable {0} con {1}", leftRegister, add.Left);
                programBuilder.AppendLine();

            }
            else if (expr is SubtractionAsigExpression)
            {
                var add = expr as SubtractionAsigExpression;
                var leftRegister = WriteExpr(add.Right, registerFile, programBuilder);
                programBuilder.AppendFormat("\tsub $t0, $zero, {0}", leftRegister);
                programBuilder.AppendFormat("\t# Inicializar la variable {0} con 0", leftRegister);
                programBuilder.AppendLine();

            }
            else if (expr is MultiplicationAsigExpression)
            {
                var add = expr as MultiplicationAsigExpression;
                var leftRegister = WriteExpr(add.Right, registerFile, programBuilder);
                programBuilder.AppendFormat("\tmul $t0, $zero, {0}", leftRegister);
                programBuilder.AppendFormat("\t# Inicializar la variable {0} con 0", leftRegister);
                programBuilder.AppendLine();

            }
            else if (expr is DivisionAsigExpression)
            {
                var add = expr as DivisionAsigExpression;
                var leftRegister = WriteExpr(add.Right, registerFile, programBuilder);
                programBuilder.AppendFormat("\tmul $t0, $zero, {0}", leftRegister);
                programBuilder.AppendFormat("\t# Inicializar la variable {0} con 0", leftRegister);
                programBuilder.AppendLine();

            }
            else if (expr is AssignExpression)
            {
                var assign = expr as AssignExpression;

                foreach (var id in registerFile.savedValuesList)
                {
                    if (id.Value.id == assign.Left.ToString())
                    {
                        inStack = true;
                        savedValue = id.Key;
                    }
                }

                var assignRegister = WriteExpr(assign.Right, registerFile, programBuilder);

                if (assign.Left is PointerArrayAccessExpr)
                {
                    isArray = true;
                    registerFile.FreeRegister(assignRegister);
                    WriteExpr(assign.Left, registerFile, programBuilder);
                    return assignRegister;
                }
                else
                {
                    if (inStack == false)
                    {
                        programBuilder.AppendFormat("\tadd $t0, $zero, {0}", assignRegister);
                    }
                    programBuilder.AppendLine();
                    inStack = false;
                    return assignRegister;
                }

            }
                #endregion

            #region POST_PRE_INCREMENT_DECREMENT
            else if (expr is PostIncrementExpression)
            {
                var postIncrement = expr as PostIncrementExpression;
                var register = registerFile.FirstUsedRegister();
                programBuilder.AppendFormat("\taddi {0}, {1}, 1", register, register);
                programBuilder.AppendFormat("\t# {0}", expr.ToString());
                programBuilder.AppendLine();
                return register;
            }
            else if (expr is PreIncrementExpression)
            {
                var postIncrement = expr as PreIncrementExpression;
                var register = registerFile.FirstUsedRegister();
                programBuilder.AppendFormat("\taddi {0}, {1}, 1", register, register);
                programBuilder.AppendFormat("\t# {0}", expr.ToString());
                programBuilder.AppendLine();
                return register;
            }
            else if (expr is PostDecrementExpression)
            {
                var postIncrement = expr as PostDecrementExpression;
                var register = registerFile.FirstUsedRegister();
                programBuilder.AppendFormat("\tsubi {0}, {1}, 1", register, register);
                programBuilder.AppendFormat("\t# {0}", expr.ToString());
                programBuilder.AppendLine();
                return register;
            }
            else if (expr is PreDecrementExpression)
            {
                var postIncrement = expr as PreDecrementExpression;
                var register = registerFile.FirstUsedRegister();
                programBuilder.AppendFormat("\tsubi {0}, {1}, 1", register, register);
                programBuilder.AppendFormat("\t# {0}", expr.ToString());
                programBuilder.AppendLine();
                return register;
            }
            #endregion

            #region FUNTIONCALL_EXPRESSION
            else if (expr is FunctionCallExpression)
            {
                WriteFunctionCallExp(expr as FunctionCallExpression, programBuilder);
            }
            #endregion

            #region ARRAY_EXPRESSION
            else if (expr is PointerArrayAccessExpr)
            {
                var array = expr as PointerArrayAccessExpr;

                programBuilder.AppendFormat("{0}:	.word {1}", array.Pointer.ToString(), "1000");
                programBuilder.AppendLine();
                var listAddress = registerFile.FirstAvailableRegister();
                programBuilder.AppendFormat("\tla {0}, {1}", listAddress, array.Pointer.ToString());
                programBuilder.AppendFormat("\t\t# Pone la dirección de la lista en {0}", listAddress);
                programBuilder.AppendLine();
                //registerFile.FreeRegister(listAddress);
                var index = registerFile.FirstAvailableRegister();
                string arrayValue = array.Value.ToString();

                foreach (var id in registerFile.savedValuesList)
                {
                    if (id.Value.id == arrayValue.ToString())
                    {
                        arrayValue = id.Value.value;
                    }
                }

                programBuilder.AppendFormat("\tli {0}, {1}", index, arrayValue);
                programBuilder.AppendFormat("\t\t# Agrega el indice a {0}", index);
                programBuilder.AppendLine();
                var tempReg = registerFile.FirstAvailableRegister();
                programBuilder.AppendFormat("\tmul {0}, {1}, {2}", tempReg, index, arrayValue);
                programBuilder.AppendFormat("\t\t# {0} es el offset", tempReg);
                programBuilder.AppendLine();
                registerFile.FreeRegister(tempReg);
                tempReg = registerFile.FirstAvailableRegister();
                programBuilder.AppendFormat("\tadd {0}, {1}, {2}", tempReg, tempReg, listAddress);
                programBuilder.AppendFormat("\t# {0} es la dirección de {1}[{0}]", tempReg, array.Pointer, arrayValue);
                programBuilder.AppendLine();
                /* programBuilder.AppendFormat("add {0}, {1}, {2}", index, index, index);
               programBuilder.Append("\t# Dobla el índice");
               programBuilder.AppendLine();
               programBuilder.AppendFormat("add {0}, {1}, {2}", index, index, index);
               programBuilder.Append("\t# Dobla el índice nuevamente (por 4x)");
               programBuilder.AppendLine();
               var indexAddress = registerFile.FirstAvailableRegister();
               programBuilder.AppendFormat("add {0}, {1}, {2}", indexAddress, index, listAddress);
               programBuilder.Append("\t# Combina los dos elementos de la dirección.");
               programBuilder.AppendLine();*/
                registerFile.FreeRegister(index);
                var valueAddress = registerFile.FirstAvailableRegister();
                //programBuilder.AppendFormat("li {0}, {1}", valueAddress, array.Value);
                programBuilder.AppendFormat("\t\t\t# {0} es el valor {1} que se guardará en {2}[{3}]", valueAddress, array.Value, array.Pointer, array.Value);
                programBuilder.AppendLine();
                programBuilder.AppendFormat("\tsw {0}, 0({1})", valueAddress, tempReg);
                programBuilder.Append("\t\t# Toma el valor indexado en el arreglo.");
                programBuilder.AppendLine();
                //registerFile.FreeRegister(indexAddress);
                registerFile.FreeRegister(tempReg);
                registerFile.FreeRegister(valueAddress);
                return listAddress;
            }
            #endregion

            return registerFile.FirstAvailableRegister();
        }
예제 #7
0
 public MulExpression(Expression left, Expression right)
 {
     base.Left = left;
     base.Right = right;
 }