Esempio n. 1
0
    public void Stack_And_Queue()
    {
        var sq = new StackQueue <int>();

        for (var i = 0; i < 5; i++)
        {
            if (i % 2 == 0)
            {
                sq.Push(i);
            }
            else
            {
                sq.Enqueue(i);
            }
        }

        // 4 (push)
        // 3 (enqueue)
        // 2 (push)
        // 1 (enqueue)
        // 0 (push)
        Assert.AreEqual(4, sq.Pop());
        Assert.AreEqual(0, sq.Dequeue());
        Assert.AreEqual(3, sq.Pop());
        Assert.AreEqual(1, sq.Dequeue());
        Assert.AreEqual(2, sq.Pop());
    }
Esempio n. 2
0
            public IEnumerator <Mono.Cecil.MethodDefinition> GetEnumerator()
            {
                StackQueue <Mono.Cecil.TypeDefinition> type_definitions         = new StackQueue <Mono.Cecil.TypeDefinition>();
                StackQueue <Mono.Cecil.TypeDefinition> type_definitions_closure = new StackQueue <Mono.Cecil.TypeDefinition>();

                foreach (Mono.Cecil.TypeDefinition td in _module.Types)
                {
                    type_definitions.Push(td);
                }
                while (type_definitions.Count > 0)
                {
                    Mono.Cecil.TypeDefinition td = type_definitions.Pop();
                    type_definitions_closure.Push(td);
                    foreach (Mono.Cecil.TypeDefinition ntd in td.NestedTypes)
                    {
                        type_definitions.Push(ntd);
                    }
                }
                foreach (Mono.Cecil.TypeDefinition type in type_definitions_closure)
                {
                    foreach (Mono.Cecil.MethodDefinition method in type.Methods)
                    {
                        yield return(method);
                    }
                }
            }
Esempio n. 3
0
        public void Add(ModuleDefinition module)
        {
            StackQueue <TypeDefinition> type_definitions         = new StackQueue <TypeDefinition>();
            StackQueue <TypeDefinition> type_definitions_closure = new StackQueue <TypeDefinition>();

            foreach (TypeDefinition td in module.Types)
            {
                type_definitions.Push(td);
            }
            while (type_definitions.Count > 0)
            {
                TypeDefinition ty = type_definitions.Pop();
                type_definitions_closure.Push(ty);
                foreach (TypeDefinition ntd in ty.NestedTypes)
                {
                    type_definitions.Push(ntd);
                }
            }
            foreach (TypeDefinition td in type_definitions_closure)
            {
                foreach (MethodDefinition definition in td.Methods)
                {
                    Add(definition);
                }
            }
        }
Esempio n. 4
0
        private void ExtractBasicBlocks()
        {
            while (_methods_to_do.Count > 0)
            {
                int change_set_id = this.Cfg.StartChangeSet();

                Tuple <MethodReference, List <TypeReference> > definition = _methods_to_do.Pop();

                if (Campy.Utils.Options.IsOn("jit_trace"))
                {
                    System.Console.WriteLine("ExtractBasicBlocks for " + definition.Item1.FullName);
                }

                ExtractBasicBlocksOfMethod(definition);

                var blocks = this.Cfg.PopChangeSet(change_set_id);

                blocks.ComputeBasicMethodProperties();

                blocks.ThreadInstructions();

                // Perform type propagation on blocks, and get call targets
                // for new methods to analyze.
                blocks.InstantiateGenerics();
            }
        }
Esempio n. 5
0
        public static System.Collections.Generic.IEnumerable <T> Sort <T, E>
            (IGraph <T, E> graph, IEnumerable <T> source)
            where E : IEdge <T>
        {
            Dictionary <T, bool> Visited = new Dictionary <T, bool>();
            StackQueue <T>       Stack   = new StackQueue <T>();

            foreach (T v in graph.Vertices)
            {
                Visited[v] = false;
            }

            foreach (T v in source)
            {
                Stack.Push(v);
            }

            while (Stack.Count != 0)
            {
                T u = Stack.Pop();
                Visited[u] = true;
                yield return(u);

                foreach (T v in graph.ReversePredecessors(u))
                {
                    if (!Visited[v] && !Stack.Contains(v))
                    {
                        Stack.Push(v);
                    }
                }
            }
        }
        public System.Collections.Generic.IEnumerator <T> GetEnumerator()
        {
            foreach (T v in graph.Vertices)
            {
                Visited[v] = false;
            }

            foreach (T v in Source)
            {
                Stack.Push(v);
            }

            while (Stack.Count != 0)
            {
                T u = Stack.Pop();
                if (Visited[u])
                {
                    yield return(u);
                }
                else
                {
                    Visited[u] = true;
                    Stack.Push(u);
                    foreach (T v in graph.ReverseSuccessors(u))
                    {
                        if (!Visited[v] && !Stack.Contains(v))
                        {
                            Stack.Push(v);
                        }
                    }
                }
            }
        }
Esempio n. 7
0
        public static Mono.Cecil.MethodDefinition ConvertToMonoCecilMethodDefinition(System.Reflection.MethodBase mi)
        {
            // Get assembly name which encloses code for kernel.
            String kernel_assembly_file_name = mi.DeclaringType.Assembly.Location;

            // Get directory containing the assembly.
            String full_path = Path.GetFullPath(kernel_assembly_file_name);

            full_path = Path.GetDirectoryName(full_path);

            String kernel_full_name = null;

            // Get full name of kernel, including normalization because they cannot be compared directly with Mono.Cecil names.
            if (mi as System.Reflection.MethodInfo != null)
            {
                System.Reflection.MethodInfo mik = mi as System.Reflection.MethodInfo;
                kernel_full_name = string.Format("{0} {1}.{2}({3})", mik.ReturnType.FullName, Campy.Utils.Utility.RemoveGenericParameters(mi.ReflectedType), mi.Name, string.Join(",", mi.GetParameters().Select(o => string.Format("{0}", o.ParameterType)).ToArray()));
            }
            else
            {
                kernel_full_name = string.Format("{0}.{1}({2})", Campy.Utils.Utility.RemoveGenericParameters(mi.ReflectedType), mi.Name, string.Join(",", mi.GetParameters().Select(o => string.Format("{0}", o.ParameterType)).ToArray()));
            }

            kernel_full_name = Campy.Utils.Utility.NormalizeSystemReflectionName(kernel_full_name);

            // Decompile entire module.
            Mono.Cecil.ModuleDefinition md = Mono.Cecil.ModuleDefinition.ReadModule(kernel_assembly_file_name);

            // Examine all types, and all methods of types in order to find the lambda in Mono.Cecil.
            List <Type> types = new List <Type>();
            StackQueue <Mono.Cecil.TypeDefinition> type_definitions         = new StackQueue <Mono.Cecil.TypeDefinition>();
            StackQueue <Mono.Cecil.TypeDefinition> type_definitions_closure = new StackQueue <Mono.Cecil.TypeDefinition>();

            foreach (Mono.Cecil.TypeDefinition td in md.Types)
            {
                type_definitions.Push(td);
            }
            while (type_definitions.Count > 0)
            {
                Mono.Cecil.TypeDefinition ty = type_definitions.Pop();
                type_definitions_closure.Push(ty);
                foreach (Mono.Cecil.TypeDefinition ntd in ty.NestedTypes)
                {
                    type_definitions.Push(ntd);
                }
            }
            foreach (Mono.Cecil.TypeDefinition td in type_definitions_closure)
            {
                foreach (Mono.Cecil.MethodDefinition md2 in td.Methods)
                {
                    String md2_name = Campy.Utils.Utility.NormalizeMonoCecilName(md2.FullName);
                    if (md2_name.Contains(kernel_full_name))
                    {
                        return(md2);
                    }
                }
            }
            return(null);
        }
Esempio n. 8
0
 private void ExtractBasicBlocks()
 {
     while (_methods_to_do.Count > 0)
     {
         int             change_set_id = this.Cfg.StartChangeSet();
         MethodReference reference     = _methods_to_do.Pop();
         if (Campy.Utils.Options.IsOn("overview_import_computation_trace"))
         {
             System.Console.WriteLine("Importing " + reference.FullName);
         }
         ExtractBasicBlocksOfMethod(reference);
         var blocks = this.Cfg.PopChangeSet(change_set_id);
         blocks.ComputeBasicMethodProperties();
         blocks.PropagateCallClosure();
         blocks.AddCctors();
     }
 }
Esempio n. 9
0
    public void Stack()
    {
        var sq = new StackQueue <int>();

        for (var i = 0; i < 3; i++)
        {
            sq.Push(i);
        }

        var expected = 2;

        while (sq.Count > 0)
        {
            var next = sq.Pop();
            Assert.AreEqual(expected, next);
            expected--;
        }
    }
Esempio n. 10
0
        public static Mono.Cecil.TypeDefinition ConvertToMonoCecilTypeDefinition(Type ty)
        {
            // Get assembly name which encloses code for kernel.
            String kernel_assembly_file_name = ty.Assembly.Location;

            // Get directory containing the assembly.
            String full_path = Path.GetFullPath(kernel_assembly_file_name);

            full_path = Path.GetDirectoryName(full_path);

            // Decompile entire module.
            Mono.Cecil.ModuleDefinition md = Mono.Cecil.ModuleDefinition.ReadModule(kernel_assembly_file_name);

            // Examine all types, and all methods of types in order to find the lambda in Mono.Cecil.
            List <Type> types = new List <Type>();
            StackQueue <Mono.Cecil.TypeDefinition> type_definitions         = new StackQueue <Mono.Cecil.TypeDefinition>();
            StackQueue <Mono.Cecil.TypeDefinition> type_definitions_closure = new StackQueue <Mono.Cecil.TypeDefinition>();

            foreach (Mono.Cecil.TypeDefinition td in md.Types)
            {
                type_definitions.Push(td);
            }
            while (type_definitions.Count > 0)
            {
                Mono.Cecil.TypeDefinition td = type_definitions.Pop();
                if (Campy.Utils.Utility.IsSimilarType(ty, td))
                {
                    return(td);
                }
                type_definitions_closure.Push(td);
                foreach (Mono.Cecil.TypeDefinition ntd in td.NestedTypes)
                {
                    type_definitions.Push(ntd);
                }
            }
            foreach (Mono.Cecil.TypeDefinition td in type_definitions_closure)
            {
                if (Campy.Utils.Utility.IsSimilarType(ty, td))
                {
                    return(td);
                }
            }
            return(null);
        }
Esempio n. 11
0
        public static Type ConvertToSystemReflectionType(Mono.Cecil.TypeDefinition td)
        {
            // Find equivalent to type definition in Mono to System Reflection type.

            // get module.
            String assembly_location = td.Module.FullyQualifiedName;

            System.Reflection.Assembly assembly = System.Reflection.Assembly.LoadFile(assembly_location);

            List <Type>       types                    = new List <Type>();
            StackQueue <Type> type_definitions         = new StackQueue <Type>();
            StackQueue <Type> type_definitions_closure = new StackQueue <Type>();

            foreach (Type t in assembly.GetTypes())
            {
                type_definitions.Push(t);
            }
            while (type_definitions.Count > 0)
            {
                Type t = type_definitions.Pop();
                if (Campy.Utils.Utility.IsSimilarType(t, td))
                {
                    return(t);
                }
                type_definitions_closure.Push(t);
                foreach (Type ntd in t.GetNestedTypes())
                {
                    type_definitions.Push(ntd);
                }
            }
            foreach (Type t in type_definitions_closure)
            {
                if (Campy.Utils.Utility.IsSimilarType(t, td))
                {
                    return(t);
                }
            }
            return(null);
        }
Esempio n. 12
0
        public void VisitNodes(Func <T, bool> func)
        {
            foreach (T v in graph.Vertices)
            {
                Visited[v] = false;
            }

            // Initialize all workers with
            // empty stack.
            Stack = new StackQueue <Tuple <T, StackQueue <T> > > [NumberOfWorkers];
            for (int i = 0; i < NumberOfWorkers; ++i)
            {
                Stack[i] = new StackQueue <Tuple <T, StackQueue <T> > >(
                    new Tuple <T, StackQueue <T> >(default(T), new StackQueue <T>()));
            }

            // Initialize first worker with stack containing all sources.
            foreach (T v in Source)
            {
                Stack[0].PeekTop().Item2.Push(v);
            }

            // Spawn workers.
            Parallel.For(0, NumberOfWorkers, (int index) =>
            {
                bool terminate = false;
                while (!terminate)
                {
                    T u = default(T);

                    GetWork(index);

                    while (Stack[index].Count >= 1 && Stack[index].PeekTop().Item2.Count > 0)
                    {
                        // There is stuff in the to do list. Pop it and perform dfs
                        // expansion of the vertex.
                        // Safe: No other threads will grab nodes within the cutoff,
                        // and no other threads can change this stack size.
                        StackQueue <T> todo = Stack[index].PeekTop().Item2;
                        u          = todo.Pop();
                        Visited[u] = true;

                        // visit.
                        // yield return u;
                        //System.Console.WriteLine("visit " + u);
                        bool term = func(u);
                        if (term)
                        {
                            Terminate = true;
                            break;
                        }

                        // Push successors.
                        StackQueue <T> items = new StackQueue <T>();
                        foreach (T v in graph.ReverseSuccessors(u))
                        {
                            if (!Visited[v] && !SpecialContains(index, v))
                            {
                                items.Push(v);
                            }
                        }
                        if (items.Count != 0)
                        {
                            // Add new backtrack and to do list.
                            Stack[index].Push(
                                new Tuple <T, StackQueue <T> >(u, items));
                        }

                        // Synchronize threads on stack.
                        GetWork(index);
                    }

                    // Check for termination.
                    terminate = TerminateTest();
                }
            });
        }