Example #1
0
        Dictionary <int, Local>[] CreateLocals(Dictionary <Local, VMInfo> vmInfos)
        {
            var dict  = new Dictionary <int, Local> [vmInfos.Count];
            var infos = new List <VMInfo>(vmInfos.Values);

            for (int i = 0; i < vmInfos.Count; i++)
            {
                dict[i] = new Dictionary <int, Local>();
                var info = infos[i].Info;
                for (int j = 0; j < info.Length; j++)
                {
                    if (info[j] == ElementType.Void)
                    {
                        continue;
                    }
                    var sig = Utilis.ElementTypeToTypeSig(blocks.Method.Module, info[j]);
                    if (sig == null)
                    {
                        return(null);
                    }
                    var local = new Local(sig);
                    blocks.Method.Body.Variables.Add(local);
                    dict[i][j] = local;
                }
            }
            return(dict);
        }
Example #2
0
 bool CheckIfIndexesAreRight(Dictionary <Local, VMInfo> vmInfos)
 {
     foreach (var vmInfo in vmInfos.Values)
     {
         var info = vmInfo.Info;
         for (int i = 0; i < info.Length; i++)
         {
             if (info[i] == ElementType.Void)
             {
                 continue;
             }
             var size = Utilis.GetElementTypeSize(info[i]);
             if (size == -1 || i + size - 1 >= info.Length)
             {
                 return(false);
             }
             for (int j = i + 1; j < size - 1; j++)
             {
                 if (info[j] != ElementType.Void)
                 {
                     return(false);
                 }
             }
         }
     }
     return(true);
 }
Example #3
0
        bool CheckBlock(Block block, Dictionary <Local, VMInfo> vmDictonary, out List <Tuple <Tuple <int, int>, Tuple <int, Local> > > loalBlocks)
        {
            loalBlocks = new List <Tuple <Tuple <int, int>, Tuple <int, Local> > >();
            var instr = block.Instructions;

            for (int i = 0; i < instr.Count; i++)
            {
                if (!Utilis.IsLoc(instr[i]))
                {
                    continue;
                }
                var local = instr[i].Instruction.GetLocal(blocks.Locals);
                if (local == null || !vmDictonary.ContainsKey(local))
                {
                    continue;
                }
                var vmInfo = vmDictonary[local];
                if (Utilis.IsStloc(instr[i]))
                {
                    if (vmInfo.Instruction != instr[i])
                    {
                        return(false);
                    }
                }
                else
                {
                    ElementType type;
                    int         ptrIndex, size;
                    if (!isValidLoad(block, i, out type, out ptrIndex, out size))
                    {
                        return(false);
                    }
                    if (vmInfo.Info[ptrIndex] != ElementType.Void && !Utilis.IsValidElementType(vmInfo.Info[ptrIndex], type))
                    {
                        return(false);
                    }
                    vmInfo.Info[ptrIndex] = Utilis.GetBestElementType(type);
                    var tup1 = new Tuple <int, int>()
                    {
                        Item1 = i, Item2 = size
                    };
                    var tup2 = new Tuple <int, Local>()
                    {
                        Item1 = ptrIndex, Item2 = local
                    };
                    loalBlocks.Add(new Tuple <Tuple <int, int>, Tuple <int, Local> >()
                    {
                        Item1 = tup1, Item2 = tup2
                    });
                }
            }
            return(true);
        }
Example #4
0
        bool CheckAlocBlock(Block block, out Dictionary <Local, VMInfo> vmDictonary)
        {
            vmDictonary = new Dictionary <Local, VMInfo>();
            var outLocals = new List <VMInfo>();
            var instr     = block.Instructions;

            for (int i = 3; i < instr.Count; i++)
            {
                if (!Utilis.IsLoc(instr[i]))
                {
                    continue;
                }
                var local = instr[i].Instruction.GetLocal(blocks.Locals);
                if (!Utilis.IsPtrElementType(local.Type, ElementType.U1))
                {
                    continue;
                }
                if (!instr[i].IsStloc())
                {
                    continue;
                }
                if (vmDictonary.ContainsKey(local))
                {
                    outLocals.Remove(vmDictonary[local]);
                    continue;
                }
                if (instr[i - 1].OpCode != OpCodes.Localloc)
                {
                    continue;
                }
                if (instr[i - 2].OpCode != OpCodes.Conv_U)
                {
                    continue;
                }
                if (!instr[i - 3].IsLdcI4())
                {
                    continue;
                }
                var size   = instr[i - 3].GetLdcI4Value();
                var vmInfo = new VMInfo()
                {
                    Size = size, Local = local, Instruction = instr[i], Info = NewEmptyElementTypeArray(size)
                };
                vmDictonary[local] = vmInfo;
                outLocals.Add(vmInfo);
            }
            return(outLocals.Count != 0);
        }