Пример #1
0
        private static MethodDef ParseMethod(ConstructorInfo fakeInst, ConstructorInfo realInst, TypeDef tDef)
        {
            var mInfo = new MethodDef()
            {
                Name           = fakeInst.Name,
                Aliases        = new List <string>(),
                IsConstructor  = true,
                IsStatic       = fakeInst.IsStatic,
                IsInternalCall = realInst.MethodImplementationFlags == MethodImplAttributes.InternalCall,
                IsIL           = realInst.MethodImplementationFlags == MethodImplAttributes.IL,
                Parameters     = ParseParams(fakeInst.GetParameters(), null, true, true, tDef.FullName, MachineSpec.GetTypeName(tDef)),
                ParentType     = tDef,

                MetadataToken = fakeInst.MetadataToken,
            };

            if (realInst.MethodImplementationFlags == MethodImplAttributes.IL)
            {
                var bc = new SSAFormByteCode(realInst);
                bc.Initialize();
                mInfo.ByteCode = bc;

                var mBody = realInst.GetMethodBody();
                mInfo.StackSize = mBody.MaxStackSize;
                mInfo.Locals    = new TypeDef[mBody.LocalVariables.Count];

                for (int j = 0; j < mBody.LocalVariables.Count; j++)
                {
                    mInfo.Locals[j] = new TypeDef()
                    {
                        Name = MachineSpec.GetTypeName(mBody.LocalVariables[j].LocalType),
                    };
                    mInfo.LocalsSize += MachineSpec.PointerSize;
                }
            }

            var aliasAttrs = realInst.GetCustomAttributes(typeof(CoreLib.AliasAttribute)).ToArray();

            for (int i = 0; i < aliasAttrs.Length; i++)
            {
                mInfo.Aliases.Add((aliasAttrs[i] as CoreLib.AliasAttribute).Name);
            }

            return(mInfo);
        }
Пример #2
0
        public static ParameterDef[] ParseParams(ParameterInfo[] ps, ParameterInfo ret, bool isInst, bool isConstructor, string parentName, string name)
        {
            List <ParameterDef> defs = new List <ParameterDef>();

            if (isConstructor)
            {
                defs.Add(new ParameterDef()
                {
                    IsRetVal      = true,
                    Name          = name,
                    ParameterType = new TypeDef()
                    {
                        FullName = parentName,
                    }
                    //TODO handle ParameterType
                });
            }

            if (isInst)
            {
                defs.Add(new ParameterDef()
                {
                    IsIn          = true,
                    Name          = name,
                    Index         = 0,
                    ParameterType = new TypeDef()
                    {
                        FullName = parentName
                    }
                    //TODO handle ParameterType
                });
            }

            for (int i = 0; i < ps.Length; i++)
            {
                defs.Add(new ParameterDef()
                {
                    IsIn          = ps[i].IsIn,
                    IsOut         = ps[i].IsOut,
                    IsRetVal      = ps[i].IsRetval,
                    Index         = ps[i].Position + (isInst ? 1 : 0),
                    Name          = MachineSpec.GetTypeName(ps[i].ParameterType),
                    ParameterType = new TypeDef()
                    {
                        FullName           = ps[i].ParameterType.FullName,
                        IsGenericParameter = ps[i].ParameterType.IsGenericParameter,
                        IsGenericType      = ps[i].ParameterType.IsGenericType,
                    }
                    //TODO handle ParameterType
                });
            }

            if (ret != null)
            {
                defs.Add(new ParameterDef()
                {
                    IsIn          = ret.IsIn,
                    IsOut         = ret.IsOut,
                    IsRetVal      = true,
                    Index         = defs.Count,
                    Name          = MachineSpec.GetTypeName(ret.ParameterType),
                    ParameterType = new TypeDef()
                    {
                        FullName           = ret.ParameterType.FullName,
                        IsGenericParameter = ret.ParameterType.IsGenericParameter,
                        IsGenericType      = ret.ParameterType.IsGenericType,
                    }
                    //TODO handle ParameterType
                });
            }

            if (isConstructor)
            {
                defs[0].Index = defs.Count - 1;
            }

            return(defs.OrderBy(a =>
            {
                return a.Index + a.Name + (a.IsOut ? "_o" : "") + (a.IsIn ? "_i" : "") + (a.IsRetVal ? "_r" : "");
            }).ToArray());
        }
Пример #3
0
        public void Load(Assembly assem, string outputDir)
        {
            //Add all the types in this assembly.
            List <TypeDef> backends      = new List <TypeDef>();
            var            dict_realType = new Dictionary <Type, Type>();
            var            dict_mapping  = new Dictionary <string, TypeDef>();

            if (!Directory.Exists(outputDir))
            {
                Directory.CreateDirectory(outputDir);
            }

            var ts = assem.GetTypes();

            foreach (Type t in ts)
            {
                dict_realType[t] = t;
            }

            var refAssemNames = assem.GetReferencedAssemblies();

            foreach (AssemblyName a in refAssemNames)
            {
                if (a.Name == "mscorlib")
                {
                    continue;
                }
                if (a.Name == "MSIL2ASM.CoreLib")
                {
                    continue;
                }

                Assembly a0 = Assembly.Load(a);
                ts = a0.GetTypes();
                foreach (Type t in ts)
                {
                    dict_realType[t] = t;
                }
            }

            foreach (KeyValuePair <Type, Type> t in Mapping.CorlibMapping.TypeMappings)
            {
                dict_realType[t.Key] = t.Value;
            }

            foreach (Type t in Mapping.CorlibMapping.IgnoreTypes)
            {
                if (dict_realType.ContainsKey(t))
                {
                    dict_realType.Remove(t);
                }
            }

            TypeMapper.SetTypeMappings(dict_realType);

            foreach (KeyValuePair <Type, Type> t in dict_realType)
            {
                var tDef = ReflectionParser.Parse(t.Key, t.Value);
                if (!backends.Contains(tDef))
                {
                    backends.Add(tDef);
                    dict_mapping[t.Key.FullName] = tDef;
                }
            }

            ReflectionParser.Reinit(dict_mapping);

            foreach (TypeDef t in backends)
            {
                for (int i = 0; i < t.StaticMethods.Length; i++)
                {
                    if (t.StaticMethods[i].ByteCode != null)
                    {
                        t.StaticMethods[i].ByteCode.Reprocess(backends);
                    }
                }

                for (int i = 0; i < t.InstanceMethods.Length; i++)
                {
                    if (t.InstanceMethods[i].ByteCode != null)
                    {
                        t.InstanceMethods[i].ByteCode.Reprocess(backends);
                    }
                }

                NasmEmitter nasmEmitter = new NasmEmitter(backends);
                nasmEmitter.Generate(t);
                File.WriteAllText(Path.Combine(outputDir, MachineSpec.GetTypeName(t) + ".S"), nasmEmitter.GetFile());
            }
        }