Ejemplo n.º 1
0
        public void Accept(CIL_Function function)
        {
            var scope = new Scope {
                father = CurrentScope
            };

            CurrentScope = scope;

            // Las funciones van a recivir los parametros a partir de la
            // tercera posicion del stack (-12($sp)), en las dos primeras
            // van ra (-4($sp)) y fp (-8($sp))
            // Despues de los Args van los Locals

            scope.VarInStack["void"] = 0;
            scope.VarInStack["this"] = -12;

            int i = -16;

            foreach (var arg in function.Args)
            {
                scope.VarInStack[arg] = i;
                i -= 4;
            }

            foreach (var local in function.Locals)
            {
                scope.VarInStack[local] = i;
                i -= 4;
            }

            int n = 4 * (function.Args.Count + function.Locals.Count + 2);

            Text += function.Name + ":\n";

            Text += "\t sw $zero, 0($sp) \n" +
                    "\t sw $ra, -4($sp) \n" +
                    "\t sw $fp, -8($sp) \n" +
                    "\t subu $sp, $sp, " + n + "\n" +
                    "\t addu $fp, $sp, " + n + "\n";

            foreach (var inst in function.Instructions)
            {
                inst.Accept(this);
            }

            Text += "\t lw $ra, -4($fp) \n" +
                    "\t lw $fp, -8($fp) \n" +
                    "\t addu $sp, $sp, " + n + "\n" +
                    "\t j $ra \n" +
                    "#########################################################################\n";

            CurrentScope = CurrentScope.father;
        }
Ejemplo n.º 2
0
 public void AddFunc(CIL_Function func)
 {
     Funcs.Add(func);
 }