static void DumpAligns(TextWriter writer, ParserTargetInfo target)
        {
            var aligns = new[]
            {
                new { Name = "gint8", Align = target.CharAlign },
                new { Name = "gint16", Align = target.ShortAlign },
                new { Name = "gint32", Align = target.IntAlign },
                new { Name = "gint64", Align = GetTypeAlign(target, target.Int64Type) },
                new { Name = "float", Align = target.FloatAlign },
                new { Name = "double", Align = target.DoubleAlign },
                new { Name = "gpointer", Align = GetTypeAlign(target, target.IntPtrType) },
            };

            // Write the alignment info for the basic types.
            foreach (var align in aligns)
            {
                writer.WriteLine("DECL_ALIGN2({0},{1})", align.Name, align.Align / 8);
            }
        }
Example #2
0
        static void Dump(ASTContext ctx, ParserTargetInfo targetInfo, Target target)
        {
            var targetFile = target.Triple;

            if (!string.IsNullOrEmpty(OutputDir))
            {
                targetFile = Path.Combine(OutputDir, targetFile);
            }

            targetFile += ".h";

            using (var writer = new StreamWriter(targetFile))
            //using (var writer = Console.Out)
            {
                writer.WriteLine("#ifndef USED_CROSS_COMPILER_OFFSETS");
                writer.WriteLine("#ifdef {0}", target.Defines[0]);
                writer.WriteLine("#ifdef {0}", GetTargetPlatformDefine(target.Platform));
                writer.WriteLine("#ifndef HAVE_BOEHM_GC");
                writer.WriteLine("#define HAS_CROSS_COMPILER_OFFSETS");
                writer.WriteLine("#if defined (USE_CROSS_COMPILE_OFFSETS) || defined (MONO_CROSS_COMPILE)");
                writer.WriteLine("#if !defined (DISABLE_METADATA_OFFSETS)");
                writer.WriteLine("#define USED_CROSS_COMPILER_OFFSETS");

                DumpAligns(writer, targetInfo);
                DumpSizes(writer, targetInfo);
                DumpMetadataOffsets(writer, ctx, target);

                writer.WriteLine("#endif //disable metadata check");

                DumpJITOffsets(writer, ctx);

                writer.WriteLine("#endif //cross compiler checks");
                writer.WriteLine("#endif //gc check");
                writer.WriteLine("#endif //os check");
                writer.WriteLine("#endif //arch check");
                writer.WriteLine("#endif //USED_CROSS_COMPILER_OFFSETS check");
            }

            Console.WriteLine("Generated offsets file: {0}", targetFile);
        }
        static void DumpSizes(TextWriter writer, ParserTargetInfo target)
        {
            var sizes = new[]
            {
                new { Name = "gint8", Size = target.CharWidth},
                new { Name = "gint16", Size = target.ShortWidth},
                new { Name = "gint32", Size = target.IntWidth},
                new { Name = "gint64", Size = GetTypeSize(target, target.Int64Type)},
                new { Name = "float", Size = target.FloatWidth},
                new { Name = "double", Size = target.DoubleWidth},
                new { Name = "gpointer", Size = GetTypeSize(target, target.IntPtrType)},
            };

            // Write the size info for the basic types.
            foreach (var size in sizes)
                writer.WriteLine("DECL_SIZE2({0},{1})", size.Name, size.Size / 8);
        }
        static void DumpAligns(TextWriter writer, ParserTargetInfo target)
        {
            var aligns = new[]
            {
                new { Name = "gint8", Align = target.CharAlign},
                new { Name = "gint16", Align = target.ShortAlign},
                new { Name = "gint32", Align = target.IntAlign},
                new { Name = "gint64", Align = GetTypeAlign(target, target.Int64Type)},
                new { Name = "float", Align = target.FloatAlign},
                new { Name = "double", Align = target.DoubleAlign},
                new { Name = "gpointer", Align = GetTypeAlign(target, target.IntPtrType)},
            };

            // Write the alignment info for the basic types.
            foreach (var align in aligns)
                writer.WriteLine("DECL_ALIGN2({0},{1})", align.Name, align.Align / 8);
        }
        static void Dump(ASTContext ctx, ParserTargetInfo targetInfo, Target target)
        {
            var targetFile = target.Triple;

            if (!string.IsNullOrEmpty (OutputDir))
                targetFile = Path.Combine (OutputDir, targetFile);

            targetFile += ".h";

            using (var writer = new StreamWriter(targetFile))
            //using (var writer = Console.Out)
            {
                writer.WriteLine("#ifndef USED_CROSS_COMPILER_OFFSETS");
                writer.WriteLine("#ifdef {0}", target.Defines[0]);
                writer.WriteLine ("#ifdef {0}", GetTargetPlatformDefine (target.Platform));
                writer.WriteLine("#ifndef HAVE_BOEHM_GC");
                writer.WriteLine("#define HAS_CROSS_COMPILER_OFFSETS");
                writer.WriteLine("#if defined (USE_CROSS_COMPILE_OFFSETS) || defined (MONO_CROSS_COMPILE)");
                writer.WriteLine("#if !defined (DISABLE_METADATA_OFFSETS)");
                writer.WriteLine("#define USED_CROSS_COMPILER_OFFSETS");

                DumpAligns(writer, targetInfo);
                DumpSizes(writer, targetInfo);
                DumpMetadataOffsets(writer, ctx, target);

                writer.WriteLine("#endif //disable metadata check");

                DumpJITOffsets(writer, ctx);

                writer.WriteLine("#endif //cross compiler checks");
                writer.WriteLine("#endif //gc check");
                writer.WriteLine("#endif //os check");
                writer.WriteLine("#endif //arch check");
                writer.WriteLine("#endif //USED_CROSS_COMPILER_OFFSETS check");
            }

            Console.WriteLine("Generated offsets file: {0}", targetFile);
        }
        internal static int CalculateOffset(this LayoutField field, LayoutField previousField, ParserTargetInfo targetInfo)
        {
            var type            = field.QualifiedType.Type.Desugar();
            var prevFieldSize   = previousField.QualifiedType.Type.Desugar().GetWidth(targetInfo) / 8;
            var unalignedOffset = previousField.Offset + prevFieldSize;
            var alignment       = type.GetAlignment(targetInfo) / 8;

            if (type is ArrayType arrayType && arrayType.Type.Desugar().IsClass())
            {
                // We have an array of structs. When we generate this field, we'll transform it into an fixed
                // array of bytes to which the elements in the embedded struct array can be bound (via their
                // accessors). At that point, the .Net subsystem will have no information in the generated
                // class on which to base its own alignment calculation. Consequently, we must generate
                // padding. Set alignment to indicate one-byte alignment which is consistent with the "fixed
                // byte fieldName[]" we'll eventually generate so that the offset we return here mimics what
                // will be the case once the struct[] -> byte[] transformation occurs.
                alignment = 1;
            }

            var alignedOffset = (unalignedOffset + (alignment - 1)) & -alignment;

            return((int)alignedOffset);
        }
Example #7
0
 private static void* __CopyValue(ParserTargetInfo.Internal native)
 {
     var ret = Marshal.AllocHGlobal(176);
     CppSharp.Parser.ParserTargetInfo.Internal.cctor_1(ret, new global::System.IntPtr(&native));
     return ret.ToPointer();
 }
Example #8
0
 protected ParserTargetInfo(ParserTargetInfo.Internal* native, bool skipVTables = false)
 {
     if (native == null)
         return;
     __Instance = new global::System.IntPtr(native);
 }
Example #9
0
 internal ParserTargetInfo(ParserTargetInfo.Internal* native)
     : this(new global::System.IntPtr(native))
 {
 }
Example #10
0
 protected ParserTargetInfo(ParserTargetInfo.Internal* native, bool isInternalImpl = false)
 {
     __Instance = new global::System.IntPtr(native);
 }
Example #11
0
 private ParserTargetInfo(ParserTargetInfo.Internal native)
     : this(__CopyValue(native))
 {
     __ownsNativeInstance = true;
     NativeToManagedMap[__Instance] = this;
 }
Example #12
0
 public static ParserTargetInfo __CreateInstance(ParserTargetInfo.Internal native)
 {
     return new ParserTargetInfo(native);
 }
Example #13
0
 private ParserTargetInfo(ParserTargetInfo.Internal native)
     : this(__CopyValue(native))
 {
     __ownsNativeInstance = true;
 }
        internal static int CalculateOffset(this LayoutField field, LayoutField previousField, ParserTargetInfo targetInfo)
        {
            var type            = field.QualifiedType.Type.Desugar();
            var prevFieldSize   = previousField.QualifiedType.Type.Desugar().GetWidth(targetInfo) / 8;
            var unalignedOffset = previousField.Offset + prevFieldSize;
            var alignment       = type.GetAlignment(targetInfo) / 8;
            var alignedOffset   = (unalignedOffset + (alignment - 1)) & -alignment;

            return((int)alignedOffset);
        }
Example #15
0
 internal ParserTargetInfo(ParserTargetInfo.Internal native)
     : this(&native)
 {
 }
        public static (uint Width, uint Alignment) GetInfo(this PrimitiveType primitive, ParserTargetInfo targetInfo, out bool signed)
        {
            signed = false;

            switch (primitive)
            {
            case PrimitiveType.Bool:
                return(targetInfo.BoolWidth, targetInfo.BoolAlign);

            case PrimitiveType.Short:
                signed = true;
                return(targetInfo.ShortWidth, targetInfo.ShortAlign);

            case PrimitiveType.Int:
                signed = true;
                return(targetInfo.IntWidth, targetInfo.IntAlign);

            case PrimitiveType.Long:
                signed = true;
                return(targetInfo.LongWidth, targetInfo.LongAlign);

            case PrimitiveType.LongLong:
                signed = true;
                return(targetInfo.LongLongWidth, targetInfo.LongLongAlign);

            case PrimitiveType.UShort:
                return(targetInfo.ShortWidth, targetInfo.ShortAlign);

            case PrimitiveType.UInt:
                return(targetInfo.IntWidth, targetInfo.IntAlign);

            case PrimitiveType.ULong:
                return(targetInfo.LongWidth, targetInfo.LongAlign);

            case PrimitiveType.ULongLong:
                return(targetInfo.LongLongWidth, targetInfo.LongLongAlign);

            case PrimitiveType.Char:
                signed = true;
                return(targetInfo.CharWidth, targetInfo.CharAlign);

            case PrimitiveType.UChar:
                return(targetInfo.CharWidth, targetInfo.CharAlign);

            case PrimitiveType.WideChar:
                signed = targetInfo.WCharType.IsSigned();
                return(targetInfo.WCharWidth, targetInfo.WCharAlign);

            case PrimitiveType.Char16:
                signed = targetInfo.Char16Type.IsSigned();
                return(targetInfo.Char16Width, targetInfo.Char16Align);

            case PrimitiveType.Char32:
                signed = targetInfo.Char32Type.IsSigned();
                return(targetInfo.Char32Width, targetInfo.Char32Align);

            case PrimitiveType.Float:
                signed = true;
                return(targetInfo.FloatWidth, targetInfo.FloatAlign);

            case PrimitiveType.Double:
                signed = true;
                return(targetInfo.DoubleWidth, targetInfo.DoubleAlign);

            case PrimitiveType.LongDouble:
                signed = true;
                return(targetInfo.LongDoubleWidth, targetInfo.LongDoubleAlign);

            case PrimitiveType.Half:
                signed = true;
                return(targetInfo.HalfWidth, targetInfo.HalfAlign);

            case PrimitiveType.Float128:
                signed = true;
                return(targetInfo.Float128Width, targetInfo.Float128Align);

            case PrimitiveType.IntPtr:
            case PrimitiveType.UIntPtr:
                signed = targetInfo.IntPtrType.IsSigned();
                return(targetInfo.PointerWidth, targetInfo.PointerAlign);

            default:
                throw new NotImplementedException();
            }
        }
Example #17
0
 private static ParserTargetInfo.Internal* __CopyValue(ParserTargetInfo.Internal native)
 {
     var ret = Marshal.AllocHGlobal(176);
     CppSharp.Parser.ParserTargetInfo.Internal.cctor_2(ret, new global::System.IntPtr(&native));
     return (ParserTargetInfo.Internal*) ret;
 }
Example #18
0
 internal ParserTargetInfo(ParserTargetInfo.Internal native)
     : this(__CopyValue(native))
 {
 }
Example #19
0
 public static ParserTargetInfo __CreateInstance(ParserTargetInfo.Internal native, bool skipVTables = false)
 {
     return new ParserTargetInfo(native, skipVTables);
 }
 static uint GetTypeAlign(ParserTargetInfo target, ParserIntType type)
 {
     switch (type)
     {
         case ParserIntType.SignedChar:
         case ParserIntType.UnsignedChar:
             return target.CharAlign;
         case ParserIntType.SignedShort:
         case ParserIntType.UnsignedShort:
             return target.ShortAlign;
         case ParserIntType.SignedInt:
         case ParserIntType.UnsignedInt:
             return target.IntAlign;
         case ParserIntType.SignedLong:
         case ParserIntType.UnsignedLong:
             return target.LongAlign;
         case ParserIntType.SignedLongLong:
         case ParserIntType.UnsignedLongLong:
             return target.LongLongAlign;
         default:
             throw new Exception("Type has no alignment");
     }
 }
Example #21
0
 private ParserTargetInfo(ParserTargetInfo.Internal native, bool skipVTables = false)
     : this(__CopyValue(native), skipVTables)
 {
     __ownsNativeInstance = true;
     NativeToManagedMap[__Instance] = this;
 }
 static uint GetTypeSize(ParserTargetInfo target, ParserIntType type)
 {
     switch (type)
     {
         case ParserIntType.SignedChar:
         case ParserIntType.UnsignedChar:
             return target.CharWidth;
         case ParserIntType.SignedShort:
         case ParserIntType.UnsignedShort:
             return target.ShortWidth;
         case ParserIntType.SignedInt:
         case ParserIntType.UnsignedInt:
             return target.IntWidth;
         case ParserIntType.SignedLong:
         case ParserIntType.UnsignedLong:
             return target.LongWidth;
         case ParserIntType.SignedLongLong:
         case ParserIntType.UnsignedLongLong:
             return target.LongLongWidth;
         default:
             throw new Exception("Type has no size");
     }
 }