public override void Visit(MethodDeclNode node)
        {
            MethodBeingVisited = ClassBeingVisited.Methods.Lookup(node.methodName.name);

            int Offset = 4; // Initialize to 4 because we need to reserve 4 bytes for return address

            for (int x = 0; x < MethodBeingVisited.Parameters.Count; x++)
            {
                ParameterDefinition parameter = MethodBeingVisited.Parameters.ItemAt(x);
                parameter.SizeInBytes = 4;
                Offset += parameter.SizeInBytes;
                parameter.Location = Offset;
            }

            MethodBeingVisited.SizeOfParametersInBytes = Offset - 4;

            Offset = 0;
            for (int x = 0; x < MethodBeingVisited.LocalVariables.Count; x++)
            {
                VariableDefinition variable = MethodBeingVisited.LocalVariables.ItemAt(x);
                variable.SizeInBytes = 4;
                Offset += variable.SizeInBytes;
                variable.Location = Offset;
            }

            MethodBeingVisited.SizeOfLocalVariablesInBytes = Offset;
        }
예제 #2
0
        public virtual void Visit(MethodDeclNode node)
        {
            MethodBeingVisited = ClassBeingVisited.Methods.Lookup(node.methodName.name);
            if (node.paramDeclList != null)
                for (int x = 0; x < node.paramDeclList.paramDeclList.Count; x++)
                    node.paramDeclList.ParamDeclAtIndex(x).Accept(this);

            if (node.variableDeclList != null)
                for (int x = 0; x < node.variableDeclList.variableDeclList.Count; x++)
                    node.variableDeclList.VariableDeclAtIndex(x).Accept(this);

            if (node.statementList != null)
                for (int x = 0; x < node.statementList.statementList.Count; x++)
                    node.statementList.StatementAtIndex(x).Accept(this);
        }
        public override void Visit(MethodDeclNode node)
        {
            MethodBeingVisited = ClassBeingVisited.Methods.Add(node.methodName.name);
            MethodBeingVisited.ClassDefinition = ClassBeingVisited;

            MethodBeingVisited.ReturnType = node.methodType.Type;

            if (node.paramDeclList != null)
                for (int x = 0; x < node.paramDeclList.paramDeclList.Count; x++)
                    node.paramDeclList.ParamDeclAtIndex(x).Accept(this);

            if (node.variableDeclList != null)
                for (int x = 0; x < node.variableDeclList.variableDeclList.Count; x++)
                {
                    VariableDefinition variableDefinition = MethodBeingVisited.LocalVariables.Add(node.variableDeclList.VariableDeclAtIndex(x).identifier.name);
                    variableDefinition.VariableType = node.variableDeclList.VariableDeclAtIndex(x).type.Type;
                    if (variableDefinition.VariableType.GetType() == typeof(ClassType))
                        variableDefinition.VariableType = Analysis.Environment.Classes.Lookup(variableDefinition.VariableType.Name).ClassType;
                }
        }
예제 #4
0
        public override void Visit(MethodDeclNode node)
        {
            MethodBeingVisited = ClassBeingVisited.Methods.Lookup(node.methodName.name);
            GenText(ClassBeingVisited.Name + "$" + node.methodName.name + ":");
            Gen("push", "ebp", "", "Save caller's stack frame pointer");
            Gen("mov", "ebp", "esp");
            if(MethodBeingVisited.SizeOfLocalVariablesInBytes > 0)
                Gen("sub", "esp", MethodBeingVisited.SizeOfLocalVariablesInBytes.ToString(), "Allocate new stack frame");
            if (node.paramDeclList != null)
                for (int x = 0; x < node.paramDeclList.paramDeclList.Count; x++)
                    node.paramDeclList.ParamDeclAtIndex(x).Accept(this);

            if (node.variableDeclList != null)
                for (int x = 0; x < node.variableDeclList.variableDeclList.Count; x++)
                    node.variableDeclList.VariableDeclAtIndex(x).Accept(this);

            if (node.statementList != null)
                for (int x = 0; x < node.statementList.statementList.Count; x++)
                    node.statementList.StatementAtIndex(x).Accept(this);
            if (MethodBeingVisited.SizeOfLocalVariablesInBytes > 0)
                Gen("mov", "esp", "ebp", "Deallocate local variables from stack");
            Gen("pop", "ebp", "", "Restore ebp to caller's stack frame pointer");
            Gen("ret", "", "", "Retrun from " + ClassBeingVisited.Name + "$" + node.methodName.name);
        }
예제 #5
0
 public void AddMethodDecl(MethodDeclNode methodDecl)
 {
     this.methodDeclList.Add(methodDecl);
 }
예제 #6
0
 public void AddMethodDecl(MethodDeclNode methodDecl)
 {
     this.methodDeclList.Add(methodDecl);
 }
예제 #7
0
 public override void Visit(MethodDeclNode node)
 {
     Console.WriteLine(this.indentation + "Method: " + node.methodName.name + "          ---- Method Declaration ----");
     indentation = indentation + "   ";
     Console.WriteLine(this.indentation + "returns " + node.methodType.toString());
     if (node.paramDeclList != null)
     {
         for (int x = 0; x < node.paramDeclList.paramDeclList.Count; x++)
         {
             node.paramDeclList.ParamDeclAtIndex(x).Accept(this);
         }
     }
     if (node.variableDeclList != null)
     {
         for (int x = 0; x < node.variableDeclList.variableDeclList.Count; x++)
         {
             node.variableDeclList.VariableDeclAtIndex(x).Accept(this);
         }
     }
     if (node.statementList != null)
     {
         for (int x = 0; x < node.statementList.statementList.Count; x++)
         {
             node.statementList.StatementAtIndex(x).Accept(this);
         }
     }
     indentation = indentation.Substring(0, indentation.Length - 3);
 }