<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using Mono.Cecil;
using SharpLang.CompilerServices.Cecil;
using SharpLLVM;

namespace SharpLang.CompilerServices
{
    /// <summary>
    /// Provides access to C# LLVM code generator.
    /// </summary>
    public sealed partial class Compiler
    {
        private ModuleRef module;
        private ModuleRef runtimeModule;
        private ContextRef context;

        // RuntimeInline
        private ValueRef allocObjectFunction;
        private ValueRef resolveInterfaceCallFunction;
        private ValueRef isInstInterfaceFunction;
        private ValueRef throwExceptionFunction;
        private ValueRef sharpPersonalityFunction;
        private ValueRef pinvokeLoadLibraryFunction;
        private ValueRef pinvokeGetProcAddressFunction;

        // Builder for normal instructions
        private BuilderRef builder;
        private Type intPtr;
        private Type int8;
        private Type int16;
        private Type int32;
        private Type int64;
        private Type uint8;
        private Type uint16;
        private Type uint32;
        private Type uint64;
        private Type @bool;
        private Type @float;
        private Type @double;
        private Type @char;
        private Type @object;
        private TypeRef intPtrType; // Native integer, pointer representation
        private int intPtrSize;
        private TypeRef nativeIntType; // Native integer, integer representation
        private TypeRef int32Type;
        private TypeRef int64Type;
        private TypeRef imtEntryType;
        private TypeRef caughtResultType;

        // Builder that is used for PHI nodes
        private BuilderRef builder2;

        private DIBuilderRef debugBuilder;
        private TargetDataRef targetData;

        private AssemblyDefinition assembly;
        private AssemblyDefinition corlib;

        private Dictionary<TypeReference, Type> types = new Dictionary<TypeReference, Type>(MemberEqualityComparer.Default);
        private Dictionary<MethodReference, Function> functions = new Dictionary<MethodReference, Function>(MemberEqualityComparer.Default);
        private Queue<Type> classesToGenerate = new Queue<Type>();

        private Queue<KeyValuePair<MethodReference, Function>> methodsToCompile = new Queue<KeyValuePair<MethodReference, Function>>();

        // List of all referenced assemblies (including indirect ones)
        private HashSet<AssemblyDefinition> referencedAssemblies = new HashSet<AssemblyDefinition>();
        private HashSet<TypeReference> referencedTypes = new HashSet<TypeReference>(MemberEqualityComparer.Default);

        public bool TestMode { get; set; }

        public void PrepareAssembly(AssemblyDefinition assembly)
        {
            this.assembly = assembly;

            // Load all dependent assemblies (except ourself)
            CollectAssemblyReferences(referencedAssemblies, assembly);
            referencedAssemblies.Remove(assembly);

            foreach (var referencedAssembly in referencedAssemblies)
            {
                foreach (var type in referencedAssembly.MainModule.Types)
                {
                    RegisterExternalType(type);
                }
                foreach (var type in referencedAssembly.MainModule.GetTypeSpecifications())
                {
                    RegisterExternalType(type);
                }
                foreach (var type in referencedAssembly.MainModule.GetTypeReferences())
                {
                    RegisterExternalType(type);
                }
            }

            corlib = assembly.MainModule.Import(typeof (void)).Resolve().Module.Assembly;

            context = LLVM.GetGlobalContext();
            module = LLVM.ModuleCreateWithName(assembly.Name.Name);

            MemoryBufferRef memoryBuffer;
            string message;
            if (LLVM.CreateMemoryBufferWithContentsOfFile(@"SharpLang.Runtime.bc", out memoryBuffer, out message))
                throw new InvalidOperationException(message);

            if (LLVM.GetBitcodeModuleInContext(context, memoryBuffer, out runtimeModule, out message))
                throw new InvalidOperationException(message);

            LLVM.DisposeMemoryBuffer(memoryBuffer);
<commit_msg>Compiler: Use existing Cecil API (fix compilation error)<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using Mono.Cecil;
using SharpLang.CompilerServices.Cecil;
using SharpLLVM;

namespace SharpLang.CompilerServices
{
    /// <summary>
    /// Provides access to C# LLVM code generator.
    /// </summary>
    public sealed partial class Compiler
    {
        private ModuleRef module;
        private ModuleRef runtimeModule;
        private ContextRef context;

        // RuntimeInline
        private ValueRef allocObjectFunction;
        private ValueRef resolveInterfaceCallFunction;
        private ValueRef isInstInterfaceFunction;
        private ValueRef throwExceptionFunction;
        private ValueRef sharpPersonalityFunction;
        private ValueRef pinvokeLoadLibraryFunction;
        private ValueRef pinvokeGetProcAddressFunction;

        // Builder for normal instructions
        private BuilderRef builder;
        private Type intPtr;
        private Type int8;
        private Type int16;
        private Type int32;
        private Type int64;
        private Type uint8;
        private Type uint16;
        private Type uint32;
        private Type uint64;
        private Type @bool;
        private Type @float;
        private Type @double;
        private Type @char;
        private Type @object;
        private TypeRef intPtrType; // Native integer, pointer representation
        private int intPtrSize;
        private TypeRef nativeIntType; // Native integer, integer representation
        private TypeRef int32Type;
        private TypeRef int64Type;
        private TypeRef imtEntryType;
        private TypeRef caughtResultType;

        // Builder that is used for PHI nodes
        private BuilderRef builder2;

        private DIBuilderRef debugBuilder;
        private TargetDataRef targetData;

        private AssemblyDefinition assembly;
        private AssemblyDefinition corlib;

        private Dictionary<TypeReference, Type> types = new Dictionary<TypeReference, Type>(MemberEqualityComparer.Default);
        private Dictionary<MethodReference, Function> functions = new Dictionary<MethodReference, Function>(MemberEqualityComparer.Default);
        private Queue<Type> classesToGenerate = new Queue<Type>();

        private Queue<KeyValuePair<MethodReference, Function>> methodsToCompile = new Queue<KeyValuePair<MethodReference, Function>>();

        // List of all referenced assemblies (including indirect ones)
        private HashSet<AssemblyDefinition> referencedAssemblies = new HashSet<AssemblyDefinition>();
        private HashSet<TypeReference> referencedTypes = new HashSet<TypeReference>(MemberEqualityComparer.Default);

        public bool TestMode { get; set; }

        public void PrepareAssembly(AssemblyDefinition assembly)
        {
            this.assembly = assembly;

            // Load all dependent assemblies (except ourself)
            CollectAssemblyReferences(referencedAssemblies, assembly);
            referencedAssemblies.Remove(assembly);

            foreach (var referencedAssembly in referencedAssemblies)
            {
                foreach (var type in referencedAssembly.MainModule.Types)
                {
                    RegisterExternalType(type);
                }
                for (uint i = 1;; ++i)
                {
                    var type = (TypeReference)referencedAssembly.MainModule.LookupToken(new MetadataToken(TokenType.TypeSpec, i));
                    if (type == null)
                        break;

                    RegisterExternalType(type);
                }
                foreach (var type in referencedAssembly.MainModule.GetTypeReferences())
                {
                    RegisterExternalType(type);
                }
            }

            corlib = assembly.MainModule.Import(typeof (void)).Resolve().Module.Assembly;

            context = LLVM.GetGlobalContext();
            module = LLVM.ModuleCreateWithName(assembly.Name.Name);

            MemoryBufferRef memoryBuffer;
            string message;
            if (LLVM.CreateMemoryBufferWithContentsOfFile(@"SharpLang.Runtime.bc", out memoryBuffer, out message))
                throw new InvalidOperationException(message);

            if (LLVM.GetBitcodeModuleInContext(context, memoryBuffer, out runtimeModule, out message))
                throw new InvalidOperationException(message);

            LLVM.DisposeMemoryBuffer(memoryBuffer);

{'fraction_non_alphanumeric': 0.06328826269742074, 'fraction_numerical': 0.004431314623338257, 'mean_word_length': 2.4818037974683542, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '650918', 'n_tokens_mistral': 2054, 'n_tokens_neox': 1939, 'n_words': 570}