Esempio n. 1
0
            public MethodDefinitionHandle EmitMethod()
            {
                s_emitCount++;
                string basicName = "Assemblies_" + _name;

                if (_name.Length > 200)
                {
                    basicName = basicName.Substring(0, 200); // Cap length of name at 200, which is reasonably small.
                }
                string methodName = basicName + "_" + s_emitCount.ToString(CultureInfo.InvariantCulture);

                return(_emitter.AddGlobalMethod(methodName, _il, 8));
            }
Esempio n. 2
0
        // ...
        /// <summary>
        /// Emit a global method "MibcConfig" that will contain key-value settings in the following format:
        ///   ldstr "key1"
        ///   ldstr "value1"
        ///   pop
        ///   pop
        ///   ldstr "key2"
        ///   ldstr "value2"
        ///   pop
        ///   pop
        ///   ...
        /// </summary>
        public static void GenerateConfigData(MibcConfig config, TypeSystemMetadataEmitter emitter)
        {
            var buffer = new BlobBuilder();
            var il     = new InstructionEncoder(buffer);

            foreach (FieldInfo mibcCfgField in typeof(MibcConfig).GetFields())
            {
                Debug.Assert(!mibcCfgField.IsStatic && mibcCfgField.FieldType == typeof(string));
                il.LoadString(emitter.GetUserStringHandle(mibcCfgField.Name));
                il.LoadString(emitter.GetUserStringHandle((string)mibcCfgField.GetValue(config) ?? ""));
                il.OpCode(ILOpCode.Pop);
                il.OpCode(ILOpCode.Pop);
            }
            il.OpCode(ILOpCode.Ret);
            emitter.AddGlobalMethod(nameof(MibcConfig), il, 8);
        }
Esempio n. 3
0
        static int GenerateMibcFile(TraceTypeSystemContext tsc, FileInfo outputFileName, ICollection <ProcessedMethodData> methodsToAttemptToPlaceIntoProfileData, bool validate)
        {
            TypeSystemMetadataEmitter emitter = new TypeSystemMetadataEmitter(new AssemblyName(outputFileName.Name), tsc);

            SortedDictionary <string, MIbcGroup> groups     = new SortedDictionary <string, MIbcGroup>();
            StringBuilder    mibcGroupNameBuilder           = new StringBuilder();
            HashSet <string> assembliesAssociatedWithMethod = new HashSet <string>();

            foreach (var entry in methodsToAttemptToPlaceIntoProfileData)
            {
                MethodDesc method = entry.Method;
                assembliesAssociatedWithMethod.Clear();
                AddAssembliesAssociatedWithMethod(method, assembliesAssociatedWithMethod, out string definingAssembly);

                string[] assemblyNames = new string[assembliesAssociatedWithMethod.Count];
                int      i             = 1;
                assemblyNames[0] = definingAssembly;

                foreach (string s in assembliesAssociatedWithMethod)
                {
                    if (s.Equals(definingAssembly))
                    {
                        continue;
                    }
                    assemblyNames[i++] = s;
                }

                // Always keep the defining assembly as the first name
                Array.Sort(assemblyNames, 1, assemblyNames.Length - 1);
                mibcGroupNameBuilder.Clear();
                foreach (string s in assemblyNames)
                {
                    mibcGroupNameBuilder.Append(s);
                    mibcGroupNameBuilder.Append(';');
                }

                string mibcGroupName = mibcGroupNameBuilder.ToString();
                if (!groups.TryGetValue(mibcGroupName, out MIbcGroup mibcGroup))
                {
                    mibcGroup = new MIbcGroup(mibcGroupName, emitter);
                    groups.Add(mibcGroupName, mibcGroup);
                }
                mibcGroup.AddProcessedMethodData(entry);
            }

            var buffer = new BlobBuilder();
            var il     = new InstructionEncoder(buffer);

            foreach (var entry in groups)
            {
                il.LoadString(emitter.GetUserStringHandle(entry.Key));
                il.OpCode(ILOpCode.Ldtoken);
                il.Token(entry.Value.EmitMethod());
                il.OpCode(ILOpCode.Pop);
            }

            emitter.AddGlobalMethod("AssemblyDictionary", il, 8);
            MemoryStream peFile = new MemoryStream();

            emitter.SerializeToStream(peFile);
            peFile.Position = 0;

            if (outputFileName.Exists)
            {
                outputFileName.Delete();
            }

            using (ZipArchive file = ZipFile.Open(outputFileName.FullName, ZipArchiveMode.Create))
            {
                var entry = file.CreateEntry(outputFileName.Name + ".dll", CompressionLevel.Optimal);
                using (Stream archiveStream = entry.Open())
                {
                    peFile.CopyTo(archiveStream);
                }
            }

            Console.WriteLine($"Generated {outputFileName.FullName}");
            if (validate)
            {
                return(ValidateMIbcData(tsc, outputFileName, peFile.ToArray(), methodsToAttemptToPlaceIntoProfileData));
            }
            else
            {
                return(0);
            }
        }