Exemplo n.º 1
0
 public CILClass GetClass(string name)
 {
     foreach(CILClass theClass in classes)
         if(theClass.Name == name)
             return theClass;
     CILClass newClass = new CILClass(name);
     classes.Add(newClass);
     return newClass;
 }
Exemplo n.º 2
0
        public CILMethod(CILClass parentClass, string name, string sig)
        {
            this.id = methodIdGenerator++;
            this.parentClass = parentClass;
            this.name = name;
            this.sig = sig;

            if(sig == "()")
                parameterCount = 0;
            else
            {
                parameterCount = 1;
                for(int i = 0; i < sig.Length; i++)
                    if(sig[i] == ',')
                        parameterCount++;
            }
        }
Exemplo n.º 3
0
 public CIL_stsfld(string label, CILClass classType, CILVariable field)
     : base(label)
 {
     this.classType = classType;
     this.field = field;
 }
Exemplo n.º 4
0
 public CIL_newobj(string label, CILClass classType, string ctorSig)
     : base(label)
 {
     this.classType = classType;
     this.ctorSig = ctorSig;
 }
Exemplo n.º 5
0
        CILMethod ParseMethod(CILClass parentclass, string str)
        {
            // TODO: Need cleanup and reparse using Scanner
            List<string> argNames = new List<string>();
            // count how much deep we are in a try block so that we
            // dont confuse } of the try-s as } of the method
            int try_counter = 0;
            int catch_counter;

            bool isStatic = false;

            CILScanner scanner = new CILScanner(fin, str);
            string st;
            string methodName = null;
            while(methodName == null)
            {
                st = scanner.NextToken();
                if(st == "static")
                    isStatic = true;
                if((st == "int32") || (st == "int64") || (st == "void") || (st == "float64") || (st == "bool"))
                    methodName = scanner.NextToken();
                else if(st == "class")
                {
                    st = scanner.NextToken();
                    methodName = scanner.NextToken();
                }
            }
            // skips "("
            st = scanner.NextToken();
            string methodSig = "(";
            int paramCount = 0;
            while(true)
            {
                st = scanner.NextToken();
                if(st == ")")
                    break;
                if(st != ",")
                {
                    if(st == "class")
                    {
                        paramCount++;
                        if(paramCount > 1)
                            methodSig += ",";
                        methodSig += scanner.NextToken();
                    }
                    else
                    {
                        paramCount++;
                        if(paramCount > 1)
                            methodSig += ",";
                        methodSig += st;
                    }
                    argNames.Add(scanner.NextToken());
                }
            }
            methodSig += ")";

            CILMethod themethod = parentclass.GetMethod(methodName, methodSig);
            themethod.SetArgumentNames(argNames);
            themethod.IsStatic = isStatic;
            currentMethod = themethod;

            // parse the method body
            bool done = false;
            str = GetNextLine(); // skips "{"
            while(done == false)
            {
                str = GetNextLine();
                string[] ss = str.Split(' ');
                switch(ss[0])
                {
                    case ".maxstack":
                        // TODO
                        // can optimize stack allocation
                        break;
                    case ".try":
                        // ignore exception handling
                        // increase the try counter so that we know next }
                        // is not end of the method
                        try_counter++;
                        GetNextLine(); // skip the { of the try block
                        break;
                    case "finally":
                        try_counter++;
                        GetNextLine(); // skip the { of the finally block
                        break;
                    case "catch":
                        // ignore the whole catch block, find } to stop
                        // first ignore the {, then count to find the end
                        GetNextLine();
                        catch_counter = 1;
                        while(true)
                        {
                            string sc = GetNextLine();
                            sc = sc.TrimStart();
                            if(sc.Length > 1)
                            {
                                if(sc[0] == '{')
                                    catch_counter++;
                                else if(sc[0] == '}')
                                    catch_counter--;
                            }
                            if(catch_counter == 0)
                                break;
                        }
                        break;
                    case ".locals":
                        bool endlocals = false;
                        while(endlocals == false)
                        {
                            if(str.IndexOf("(") >= 0)
                                str = str.Substring(str.IndexOf("(") + 1);
                            string s2;
                            if(str.IndexOf(",") >= 0)
                                s2 = str.Substring(0, str.IndexOf(","));
                            else if(str.IndexOf(")") >= 0)
                            {
                                s2 = str.Substring(0, str.IndexOf(")"));
                                endlocals = true;
                            }
                            else
                            {
                                Console.WriteLine("Unknown local variable declaration {0}", str);
                                throw new Exception("Unknown local variable declaration");
                            }
                            themethod.AddLocalVariable(ParseLocalVariable(s2));
                            if(endlocals == false)
                                str = GetNextLine();
                        }
                        break;
                    case "}":
                        if(try_counter == 0)
                            done = true;
                        else
                            try_counter--;
                        break;
                    case ".entrypoint":
                        program.EntryPoint = themethod;
                        break;
                    default:
                        string label = str.Substring(0, str.IndexOf(":"));
                        CILInstruction inst = ParseInstruction(label, str.Substring(10, str.Length - 10));
                        if(inst != null)
                            themethod.AddInstruction(inst);
                        break;
                }
            }
            return themethod;
        }
Exemplo n.º 6
0
 public CILVar_object(string name, CILClass classType)
     : base(name)
 {
     this.classType = classType;
 }