Exemplo n.º 1
0
 public DebugPointerType(IPointerType llvmPtrType, BitcodeModule module, DIType elementType, string name = null, uint alignment = 0)
     : base(llvmPtrType)
 {
     module.ValidateNotNull(nameof(module));
     DIType = module.DIBuilder
              .CreatePointerType(elementType
                                 , name
                                 , module.Layout.BitSizeOf(llvmPtrType)
                                 , alignment
                                 );
 }
Exemplo n.º 2
0
 /// <summary>Initializes a new instance of the <see cref="DebugFunctionType"/> class.</summary>
 /// <param name="llvmType">Native LLVM function signature</param>
 /// <param name="module"><see cref="BitcodeModule"/> to use when construction debug information</param>
 /// <param name="debugFlags"><see cref="DebugInfoFlags"/> for this signature</param>
 /// <param name="retType">Return type for the function</param>
 /// <param name="argTypes">Potentially empty set of argument types for the signature</param>
 public DebugFunctionType(IFunctionType llvmType
                          , BitcodeModule module
                          , DebugInfoFlags debugFlags
                          , DebugType <ITypeRef, DIType> retType
                          , params DebugType <ITypeRef, DIType>[] argTypes
                          )
     : base(llvmType.ValidateNotNull(nameof(llvmType)))
 {
     DIType = module.ValidateNotNull(nameof(module))
              .DIBuilder
              .CreateSubroutineType(debugFlags
                                    , retType.ValidateNotNull(nameof(retType)).DIType
                                    , argTypes.Select(t => t.DIType)
                                    );
 }
Exemplo n.º 3
0
 /// <summary>Initializes a new instance of the <see cref="DebugStructType"/> class.</summary>
 /// <param name="module">Module to contain the debug meta data</param>
 /// <param name="nativeName">Name of the type in LLVM IR</param>
 /// <param name="scope">Debug scope for the structure</param>
 /// <param name="name">Source/debug name of the struct</param>
 /// <param name="file">File containing the definition of this type</param>
 /// <param name="line">line number this type is defined at</param>
 /// <remarks>
 /// This constructor creates a replaceable type that is replaced later with a full
 /// definition of the type
 /// </remarks>
 public DebugStructType(BitcodeModule module
                        , string nativeName
                        , DIScope scope
                        , string name
                        , DIFile file = null
                        , uint line   = 0
                        )
     : this(module.ValidateNotNull(nameof(module)).Context.CreateStructType(nativeName)
            , module
            , scope
            , name
            , file
            , line
            )
 {
 }
Exemplo n.º 4
0
        /// <summary>Initializes a new instance of the <see cref="DebugStructType"/> class.</summary>
        /// <param name="module">Module to contain the debug meta data</param>
        /// <param name="nativeName">Name of the type in LLVM IR</param>
        /// <param name="scope">Debug scope for the structure</param>
        /// <param name="name">Source/debug name of the struct</param>
        /// <param name="file">File containing the definition of this type</param>
        /// <param name="line">line number this type is defined at</param>
        /// <param name="debugFlags">debug flags for this type</param>
        /// <param name="debugElements">Description of all the members of this structure</param>
        /// <param name="derivedFrom">Base type, if any for this type</param>
        /// <param name="packed">Indicates if this type is packed or not</param>
        /// <param name="bitSize">Total bit size for this type or <see langword="null"/> to use default for target</param>
        /// <param name="bitAlignment">Alignment of the type in bits, 0 indicates default for target</param>
        public DebugStructType(BitcodeModule module
                               , string nativeName
                               , DIScope scope
                               , string name
                               , DIFile file
                               , uint line
                               , DebugInfoFlags debugFlags
                               , IEnumerable <DebugMemberInfo> debugElements
                               , DIType derivedFrom = null
                               , bool packed        = false
                               , uint?bitSize       = null
                               , uint bitAlignment  = 0
                               )
        {
            module.ValidateNotNull(nameof(module));
            DebugMembers = new ReadOnlyCollection <DebugMemberInfo>(debugElements as IList <DebugMemberInfo> ?? debugElements.ToList( ));

            NativeType = module.Context.CreateStructType(nativeName, packed, debugElements.Select(e => e.DebugType).ToArray( ));

            // create a temp opaque type to act as scope for members
            // this is RAUW with the full struct once it is defined
            DIType = module.DIBuilder.CreateReplaceableCompositeType(Tag.StructureType
                                                                     , name
                                                                     , scope
                                                                     , file
                                                                     , line
                                                                     );

            var memberTypes = from memberInfo in DebugMembers
                              select CreateMemberType(module, memberInfo);

            var concreteType = module.DIBuilder.CreateStructType(scope: scope
                                                                 , name: name
                                                                 , file: file
                                                                 , line: line
                                                                 , bitSize: bitSize ?? module.Layout.BitSizeOf(NativeType)
                                                                 , bitAlign: bitAlignment
                                                                 , debugFlags: debugFlags
                                                                 , derivedFrom: derivedFrom
                                                                 , elements: memberTypes
                                                                 );

            // assignment performs RAUW
            DIType = concreteType;
        }
Exemplo n.º 5
0
 /// <summary>Initializes a new instance of the <see cref="DebugStructType"/> class.</summary>
 /// <param name="llvmType">LLVM native type to build debug information for</param>
 /// <param name="module">Module to contain the debug meta data</param>
 /// <param name="scope">Debug scope for the structure</param>
 /// <param name="name">Source/debug name of the struct</param>
 /// <param name="file">File containing the definition of this type</param>
 /// <param name="line">line number this type is defined at</param>
 /// <remarks>
 /// This constructor creates a replaceable type that is replaced later with a full
 /// definition of the type
 /// </remarks>
 public DebugStructType(IStructType llvmType
                        , BitcodeModule module
                        , DIScope scope
                        , string name
                        , DIFile file
                        , uint line
                        )
     : base(llvmType)
 {
     DIType = module.ValidateNotNull(nameof(module))
              .DIBuilder
              .CreateReplaceableCompositeType(Tag.StructureType
                                              , name
                                              , scope
                                              , file
                                              , line
                                              );
 }
Exemplo n.º 6
0
 /// <summary>Initializes a new instance of the <see cref="DebugUnionType"/> class.</summary>
 /// <param name="module">Module to contain the debug metadata for this type</param>
 /// <param name="nativeName">Native LLVM type name</param>
 /// <param name="scope">Scope containing this type</param>
 /// <param name="name">Debug/source name of the type</param>
 /// <param name="file">Source file containing this type</param>
 /// <param name="line">Line number for this type</param>
 public DebugUnionType(BitcodeModule module
                       , string nativeName
                       , DIScope?scope
                       , string name
                       , DIFile?file
                       , uint line = 0
                       )
     : base(module.ValidateNotNull(nameof(module)).Context.CreateStructType(nativeName),
            module.DIBuilder
            .CreateReplaceableCompositeType(Tag.UnionType
                                            , name
                                            , scope
                                            , file
                                            , line
                                            )
            )
 {
 }
Exemplo n.º 7
0
        /// <inheritdoc/>
        public ulong AddLazyCompiledModule(BitcodeModule bitcodeModule, LLVMOrcSymbolResolverFn resolver)
        {
            bitcodeModule.ValidateNotNull(nameof(bitcodeModule));
            resolver.ValidateNotNull(nameof(resolver));

            LLVMModuleRef moduleHandle    = bitcodeModule.Detach( );
            var           wrappedResolver = new WrappedNativeCallback <LLVMOrcSymbolResolverFn>(resolver);
            var           err             = LLVMOrcAddLazilyCompiledIR(JitStackHandle, out ulong retHandle, moduleHandle, wrappedResolver, IntPtr.Zero);

            moduleHandle.SetHandleAsInvalid( );
            if (!err.IsInvalid)
            {
                throw new LlvmException(err.ToString( ));
            }

            // keep resolver delegate alive as native code needs to call it after this function exits
            SymbolResolvers.Add(retHandle, wrappedResolver);
            return(retHandle);
        }
Exemplo n.º 8
0
        /// <summary>Initializes a new instance of the <see cref="DebugStructType"/> class.</summary>
        /// <param name="module">Module to contain the debug meta data</param>
        /// <param name="nativeName">Name of the type in LLVM IR (use <see cref="string.Empty"/> for anonymous types)</param>
        /// <param name="scope">Debug scope for the structure</param>
        /// <param name="sourceName">Source/debug name of the struct (use <see cref="string.Empty"/> for anonymous types)</param>
        /// <param name="file">File containing the definition of this type</param>
        /// <param name="line">line number this type is defined at</param>
        /// <param name="debugFlags">debug flags for this type</param>
        /// <param name="members">Description of all the members of this structure</param>
        /// <param name="derivedFrom">Base type, if any for this type</param>
        /// <param name="packed">Indicates if this type is packed or not</param>
        /// <param name="bitSize">Total bit size for this type or <see langword="null"/> to use default for target</param>
        /// <param name="bitAlignment">Alignment of the type in bits, 0 indicates default for target</param>
        public DebugStructType(BitcodeModule module
                               , string nativeName
                               , DIScope?scope
                               , string sourceName
                               , DIFile?file
                               , uint line
                               , DebugInfoFlags debugFlags
                               , IEnumerable <DebugMemberInfo> members
                               , DIType?derivedFrom = null
                               , bool packed        = false
                               , uint?bitSize       = null
                               , uint bitAlignment  = 0
                               )
            : base(module.ValidateNotNull(nameof(module))
                   .Context.CreateStructType(nativeName, packed, members.Select(e => e.DebugType).ToArray( ))
                   , module.DIBuilder.CreateReplaceableCompositeType(Tag.StructureType
                                                                     , sourceName
                                                                     , scope
                                                                     , file
                                                                     , line
                                                                     )
                   )
        {
            DebugMembers = new ReadOnlyCollection <DebugMemberInfo>(members as IList <DebugMemberInfo> ?? members.ToList( ));

            var memberTypes = from memberInfo in DebugMembers
                              select CreateMemberType(module, memberInfo);

            var concreteType = module.DIBuilder.CreateStructType(scope: scope
                                                                 , name: sourceName
                                                                 , file: file
                                                                 , line: line
                                                                 , bitSize: bitSize ?? module.Layout.BitSizeOf(NativeType)
                                                                 , bitAlign: bitAlignment
                                                                 , debugFlags: debugFlags
                                                                 , derivedFrom: derivedFrom
                                                                 , elements: memberTypes
                                                                 );

            // assignment performs RAUW
            DIType = concreteType;
        }
Exemplo n.º 9
0
        /// <summary>Initializes a new instance of the <see cref="DebugUnionType"/> class.</summary>
        /// <param name="module">Module to contain the debug metadata for this type</param>
        /// <param name="nativeName">Native LLVM type name</param>
        /// <param name="scope">Scope containing this type</param>
        /// <param name="name">Debug/source name of the type</param>
        /// <param name="file">Source file containing this type</param>
        /// <param name="line">Line number for this type</param>
        public DebugUnionType(BitcodeModule module
                              , string nativeName
                              , DIScope scope
                              , string name
                              , DIFile file
                              , uint line = 0
                              )
        {
            module.ValidateNotNull(nameof(module));
            file.ValidateNotNull(nameof(file));

            NativeType = module.Context.CreateStructType(nativeName);
            DIType     = module.DIBuilder
                         .CreateReplaceableCompositeType(Tag.UnionType
                                                         , name
                                                         , scope
                                                         , file
                                                         , line
                                                         );
        }
Exemplo n.º 10
0
 /// <summary>Initializes a new instance of the <see cref="FunctionPassManager"/> class.</summary>
 /// <param name="module">Module that owns the functions this manager works on</param>
 public FunctionPassManager(BitcodeModule module)
     : base(LLVMCreateFunctionPassManagerForModule(module.ValidateNotNull(nameof(module)).ModuleHandle))
 {
 }
Exemplo n.º 11
0
 /// <summary>Runs the passes added to this manager for the target module</summary>
 /// <param name="target">Module to run the passes on</param>
 /// <returns><see langword="true"/> if one of the passes modified the module</returns>
 public bool Run(BitcodeModule target)
 {
     target.ValidateNotNull(nameof(target));
     return(LLVMRunPassManager(Handle, target.ModuleHandle));
 }
Exemplo n.º 12
0
        /// <summary>Sets the body of the union type</summary>
        /// <param name="module">Module to contain the debug metadata</param>
        /// <param name="scope">Scope containing this type</param>
        /// <param name="file">File for the type</param>
        /// <param name="line">line number for the type</param>
        /// <param name="debugFlags">Flags for the type</param>
        /// <param name="debugElements">Descriptors for each element in the type</param>
        public void SetBody(BitcodeModule module
                            , DIScope scope
                            , DIFile file
                            , uint line
                            , DebugInfoFlags debugFlags
                            , IEnumerable <DebugMemberInfo> debugElements
                            )
        {
            module.ValidateNotNull(nameof(module));
            scope.ValidateNotNull(nameof(scope));
            debugElements.ValidateNotNull(nameof(debugElements));

            if (module.Layout == null)
            {
                throw new ArgumentException(Resources.Module_needs_Layout_to_build_basic_types, nameof(module));
            }

            // Native body is a single element of a type with the largest size
            ulong maxSize = 0UL;

            ITypeRef[] nativeMembers = { null };
            foreach (var elem in debugElements)
            {
                /* ReSharper disable ConditionIsAlwaysTrueOrFalse */
                /* ReSharper disable HeuristicUnreachableCode */
                ulong?bitSize = elem.ExplicitLayout?.BitSize ?? module.Layout?.BitSizeOf(elem.DebugType);
                if (!bitSize.HasValue)
                {
                    throw new ArgumentException(Resources.Cannot_determine_layout_for_element__The_element_must_have_an_explicit_layout_or_the_module_has_a_layout_to_use, nameof(debugElements));
                }

                /* ReSharper enable HeuristicUnreachableCode */
                /* ReSharper enable ConditionIsAlwaysTrueOrFalse */

                if (maxSize >= bitSize.Value)
                {
                    continue;
                }

                maxSize          = bitSize.Value;
                nativeMembers[0] = elem.DebugType;
            }

            var nativeType = ( IStructType )NativeType;

            nativeType.SetBody(false, nativeMembers);

            // Debug info contains details of each member of the union
            DebugMembers = new ReadOnlyCollection <DebugMemberInfo>(debugElements as IList <DebugMemberInfo> ?? debugElements.ToList( ));
            var memberTypes = from memberInfo in DebugMembers
                              select module.DIBuilder.CreateMemberType(scope : DIType
                                                                       , name : memberInfo.Name
                                                                       , file : memberInfo.File
                                                                       , line : memberInfo.Line
                                                                       , bitSize : (memberInfo.ExplicitLayout?.BitSize ?? module.Layout?.BitSizeOf(memberInfo.DebugType)).Value
                                                                       , bitAlign : memberInfo.ExplicitLayout?.BitAlignment ?? 0
                                                                       , bitOffset : 0
                                                                       , debugFlags : memberInfo.DebugInfoFlags
                                                                       , type : memberInfo.DebugType.DIType
                                                                       );

            var concreteType = module.DIBuilder.CreateUnionType(scope: scope
                                                                , name: DIType.Name
                                                                , file: file
                                                                , line: line
                                                                , bitSize: 0  // TODO: find largest sized member
                                                                , bitAlign: 0 // TODO: Find most restrictive alignment
                                                                , debugFlags: debugFlags
                                                                , elements: memberTypes
                                                                );

            DIType = concreteType;
        }
Exemplo n.º 13
0
        /// <summary>Sets the body of the union type</summary>
        /// <param name="module">Module to contain the debug metadata</param>
        /// <param name="scope">Scope containing this type</param>
        /// <param name="file">File for the type</param>
        /// <param name="line">line number for the type</param>
        /// <param name="debugFlags">Flags for the type</param>
        /// <param name="debugElements">Dexcriptors for each element in the type</param>
        public void SetBody(BitcodeModule module
                            , DIScope scope
                            , DIFile file
                            , uint line
                            , DebugInfoFlags debugFlags
                            , IEnumerable <DebugMemberInfo> debugElements
                            )
        {
            module.ValidateNotNull(nameof(module));
            scope.ValidateNotNull(nameof(scope));
            debugElements.ValidateNotNull(nameof(debugElements));

            if (module.Layout == null)
            {
                throw new ArgumentException("Module needs Layout to build basic types", nameof(module));
            }

            // Native body is a single element of a type with the largest size
            ulong maxSize = 0UL;

            ITypeRef[] nativeMembers = { null };
            foreach (var elem in debugElements)
            {
                var bitSize = elem.ExplicitLayout?.BitSize ?? module.Layout?.BitSizeOf(elem.DebugType);
                if (!bitSize.HasValue)
                {
                    throw new ArgumentException("Cannot determine layout for element; The element must have an explicit layout or the module has a layout to use", nameof(debugElements));
                }

                if (maxSize < bitSize.Value)
                {
                    maxSize          = bitSize.Value;
                    nativeMembers[0] = elem.DebugType;
                }
            }

            var nativeType = ( IStructType )NativeType;

            nativeType.SetBody(false, nativeMembers);

            // Debug info contains details of each member of the union
            DebugMembers = new ReadOnlyCollection <DebugMemberInfo>(debugElements as IList <DebugMemberInfo> ?? debugElements.ToList( ));
            var memberTypes = from memberInfo in DebugMembers
                              select module.DIBuilder.CreateMemberType(scope : DIType
                                                                       , name : memberInfo.Name
                                                                       , file : memberInfo.File
                                                                       , line : memberInfo.Line
                                                                       , bitSize : (memberInfo.ExplicitLayout?.BitSize ?? module.Layout?.BitSizeOf(memberInfo.DebugType)).Value
                                                                       , bitAlign : memberInfo.ExplicitLayout?.BitAlignment ?? 0
                                                                       , bitOffset : 0
                                                                       , debugFlags : memberInfo.DebugInfoFlags
                                                                       , type : memberInfo.DebugType.DIType
                                                                       );

            var concreteType = module.DIBuilder.CreateUnionType(scope: scope
                                                                , name: DIType.Name
                                                                , file: file
                                                                , line: line
                                                                , bitSize: 0  // TODO: find largest sized member
                                                                , bitAlign: 0 // TODO: Find most restrictive alignment
                                                                , debugFlags: debugFlags
                                                                , elements: memberTypes
                                                                );

            DIType = concreteType;
        }
Exemplo n.º 14
0
        /// <summary>Sets the body of the union type</summary>
        /// <param name="module">Module to contain the debug metadata</param>
        /// <param name="scope">Scope containing this type</param>
        /// <param name="file">File for the type</param>
        /// <param name="line">line number for the type</param>
        /// <param name="debugFlags">Flags for the type</param>
        /// <param name="debugElements">Descriptors for each element in the type</param>
        public void SetBody(BitcodeModule module
                            , DIScope?scope
                            , DIFile?file
                            , uint line
                            , DebugInfoFlags debugFlags
                            , IEnumerable <DebugMemberInfo> debugElements
                            )
        {
            module.ValidateNotNull(nameof(module));
            debugElements.ValidateNotNull(nameof(debugElements));

            if (module.Layout == null)
            {
                throw new ArgumentException(Resources.Module_needs_Layout_to_build_basic_types, nameof(module));
            }

            // Native body is a single element of a type with the largest size
            ulong maxSize = 0UL;

            ITypeRef[] nativeMembers = { null ! };
            foreach (var elem in debugElements)
            {
                ulong?bitSize = elem.ExplicitLayout?.BitSize ?? module.Layout?.BitSizeOf(elem.DebugType);
                if (!bitSize.HasValue)
                {
                    throw new ArgumentException(Resources.Cannot_determine_layout_for_element__The_element_must_have_an_explicit_layout_or_the_module_has_a_layout_to_use, nameof(debugElements));
                }

                if (maxSize >= bitSize.Value)
                {
                    continue;
                }

                maxSize          = bitSize.Value;
                nativeMembers[0] = elem.DebugType;
            }

            var nativeType = ( IStructType )NativeType;

            nativeType.SetBody(false, nativeMembers);

            // Debug info contains details of each member of the union
            DebugMembers = new ReadOnlyCollection <DebugMemberInfo>(debugElements as IList <DebugMemberInfo> ?? debugElements.ToList( ));
            var memberTypes = (from memberInfo in DebugMembers
                               select CreateMemberType(module, memberInfo)
                               ).ToList <DIDerivedType>();

            var(unionBitSize, unionAlign)
                = memberTypes.Aggregate((MaxSize: 0ul, MaxAlign: 0ul)
                                        , (a, d) => (Math.Max(a.MaxSize, d.BitSize), Math.Max(a.MaxAlign, d.BitAlignment))
                                        );
            var concreteType = module.DIBuilder.CreateUnionType(scope: scope
                                                                , name: DIType !.Name // not null via construction
                                                                , file: file
                                                                , line: line
                                                                , bitSize: checked ((uint)unionBitSize)
                                                                , bitAlign: checked ((uint)unionAlign)
                                                                , debugFlags: debugFlags
                                                                , elements: memberTypes
                                                                );

            DIType = concreteType;
        }