示例#1
0
文件: VTable.cs 项目: jncronin/tysila
        /* Vtable:
         *
         *  TIPtr (just to a glorified TypeSpec)
         *  IFacePtr (to list of implemented interfaces)
         *  Extends (to base classes for quick castclassex)
         *  TypeSize (to allow MemberwiseClone to work without full Reflection)
         *  Any target-specific data
         *  Method 0
         *  ...
         *
         *  IFaceList TODO
         *
         *
         */

        public static void OutputVTable(TypeSpec ts,
                                        target.Target t, binary_library.IBinaryFile of,
                                        TysilaState s,
                                        MetadataStream base_m = null,
                                        ISection os           = null,
                                        ISection data_sect    = null)
        {
            // Don't compile if not for this architecture
            if (!t.IsTypeValid(ts))
            {
                return;
            }

            /* New signature table */
            s.sigt = new SignatureTable(ts.MangleType());

            // If its a delegate type we also need to output its methods
            if (ts.IsDelegate && !ts.IsGenericTemplate)
            {
                s.r.DelegateRequestor.Request(ts);
            }

            if (os == null)
            {
                os = of.GetRDataSection();
            }
            var d        = os.Data;
            var ptr_size = t.GetCTSize(ir.Opcode.ct_object);

            os.Align(ptr_size);

            ulong offset = (ulong)os.Data.Count;

            /* Symbol */
            var sym = of.CreateSymbol();

            sym.Name       = ts.MangleType();
            sym.ObjectType = binary_library.SymbolObjectType.Object;
            sym.Offset     = offset;
            sym.Type       = binary_library.SymbolType.Global;
            os.AddSymbol(sym);

            if (base_m != null && ts.m != base_m)
            {
                sym.Type = SymbolType.Weak;
            }

            /* TIPtr */
            var tiptr_offset = s.sigt.GetSignatureAddress(ts.Signature, t, s);

            var ti_reloc = of.CreateRelocation();

            ti_reloc.Addend          = tiptr_offset;
            ti_reloc.DefinedIn       = os;
            ti_reloc.Offset          = offset;
            ti_reloc.Type            = t.GetDataToDataReloc();
            ti_reloc.References      = of.CreateSymbol();
            ti_reloc.References.Name = s.sigt.GetStringTableName();
            of.AddRelocation(ti_reloc);

            for (int i = 0; i < ptr_size; i++, offset++)
            {
                d.Add(0);
            }

            /* IFacePtr */
            IRelocation if_reloc = null;

            if (!ts.IsGenericTemplate && !ts.IsInterface && ts.stype != TypeSpec.SpecialType.Ptr && ts.stype != TypeSpec.SpecialType.MPtr)
            {
                if_reloc            = of.CreateRelocation();
                if_reloc.DefinedIn  = os;
                if_reloc.Offset     = offset;
                if_reloc.Type       = t.GetDataToDataReloc();
                if_reloc.References = sym;
                of.AddRelocation(if_reloc);
            }

            for (int i = 0; i < ptr_size; i++, offset++)
            {
                d.Add(0);
            }

            /* Extends */
            var ts_extends = ts.GetExtends();

            if (ts_extends != null)
            {
                var ext_reloc = of.CreateRelocation();
                ext_reloc.Addend    = 0;
                ext_reloc.DefinedIn = os;
                ext_reloc.Offset    = offset;
                ext_reloc.Type      = t.GetDataToDataReloc();

                var ext_sym = of.CreateSymbol();
                ext_sym.Name = ts_extends.MangleType();

                ext_reloc.References = ext_sym;
                of.AddRelocation(ext_reloc);

                s.r.VTableRequestor.Request(ts_extends);
            }
            for (int i = 0; i < ptr_size; i++, offset++)
            {
                d.Add(0);
            }

            if (ts.IsInterface || ts.IsGenericTemplate || ts.stype == TypeSpec.SpecialType.MPtr || ts.stype == TypeSpec.SpecialType.Ptr)
            {
                /* Type size is zero for somethinh we cannot
                 * instantiate */
                for (int i = 0; i < ptr_size; i++, offset++)
                {
                    d.Add(0);
                }

                /* Flags */
                AddFlags(d, ts, ref offset, t);

                /* BaseType */
                AddBaseType(d, ts, of, os, ref offset, t, s);

                // Target-specific information
                t.AddExtraVTableFields(ts, d, ref offset);
            }
            else
            {
                /* Type size */
                var tsize = t.IntPtrArray(BitConverter.GetBytes(GetTypeSize(ts, t)));
                foreach (var b in tsize)
                {
                    d.Add(b);
                    offset++;
                }

                /* Flags */
                AddFlags(d, ts, ref offset, t);

                /* BaseType */
                AddBaseType(d, ts, of, os, ref offset, t, s);

                // Target specific information
                t.AddExtraVTableFields(ts, d, ref offset);

                /* Virtual methods */
                OutputVirtualMethods(ts, of, os,
                                     d, ref offset, t, s);

                /* Interface implementations */

                // build list of implemented interfaces
                var ii = ts.ImplementedInterfaces;

                // first, add all interface implementations
                List <ulong> ii_offsets = new List <ulong>();

                for (int i = 0; i < ii.Count; i++)
                {
                    ii_offsets.Add(offset - sym.Offset);
                    OutputInterface(ts, ii[i],
                                    of, os, d, ref offset, t, s);
                    s.r.VTableRequestor.Request(ii[i]);
                }

                // point iface ptr here
                if_reloc.Addend = (long)offset - (long)sym.Offset;
                for (int i = 0; i < ii.Count; i++)
                {
                    // list is pointer to interface declaration, then implementation
                    var id_ptr_sym = of.CreateSymbol();
                    id_ptr_sym.Name = ii[i].MangleType();

                    var id_ptr_reloc = of.CreateRelocation();
                    id_ptr_reloc.Addend     = 0;
                    id_ptr_reloc.DefinedIn  = os;
                    id_ptr_reloc.Offset     = offset;
                    id_ptr_reloc.References = id_ptr_sym;
                    id_ptr_reloc.Type       = t.GetDataToDataReloc();
                    of.AddRelocation(id_ptr_reloc);

                    for (int j = 0; j < ptr_size; j++, offset++)
                    {
                        d.Add(0);
                    }

                    // implementation
                    var ii_ptr_reloc = of.CreateRelocation();
                    ii_ptr_reloc.Addend     = (long)ii_offsets[i];
                    ii_ptr_reloc.DefinedIn  = os;
                    ii_ptr_reloc.Offset     = offset;
                    ii_ptr_reloc.References = sym;
                    ii_ptr_reloc.Type       = t.GetDataToDataReloc();
                    of.AddRelocation(ii_ptr_reloc);

                    for (int j = 0; j < ptr_size; j++, offset++)
                    {
                        d.Add(0);
                    }
                }

                // null terminate the list
                for (int j = 0; j < ptr_size; j++, offset++)
                {
                    d.Add(0);
                }
            }

            sym.Size = (long)(offset - sym.Offset);

            /* Output signature table if any */
            if (data_sect == null)
            {
                data_sect = of.GetDataSection();
            }
            s.sigt.WriteToOutput(of, base_m, t, data_sect);
        }
示例#2
0
文件: Field.cs 项目: jncronin/tysila
        public static void OutputStaticFields(metadata.TypeSpec ts,
                                              target.Target t, binary_library.IBinaryFile of,
                                              MetadataStream base_m         = null,
                                              binary_library.ISection os    = null,
                                              binary_library.ISection tlsos = null)
        {
            // Don't compile if not for this architecture
            if (!t.IsTypeValid(ts))
            {
                return;
            }

            int align = 1;

            if (ts.SimpleType == 0)
            {
                align = GetTypeAlignment(ts, t, true);
            }

            if (os == null)
            {
                os = of.GetDataSection();
            }
            os.Align(t.GetPointerSize());
            os.Align(align);

            ulong offset    = (ulong)os.Data.Count;
            ulong tl_offset = 0;

            if (tlsos != null)
            {
                tl_offset = (ulong)tlsos.Data.Count;
            }

            int cur_offset   = 0;
            int cur_tloffset = 0;

            /* is_initialized */
            for (int i = 0; i < t.psize; i++)
            {
                os.Data.Add(0);
            }
            cur_offset += t.psize;

            /* Iterate through methods looking for requested
             *  one */
            var first_fdef = ts.m.GetIntEntry(MetadataStream.tid_TypeDef,
                                              ts.tdrow, 4);
            var last_fdef = ts.m.GetLastFieldDef(ts.tdrow);

            for (uint fdef_row = first_fdef; fdef_row < last_fdef; fdef_row++)
            {
                // Ensure field is static or not as required
                var flags = ts.m.GetIntEntry(MetadataStream.tid_Field,
                                             (int)fdef_row, 0);
                if ((flags & 0x10) == 0x10)
                {
                    // Increment by type size
                    var fsig = (int)ts.m.GetIntEntry(MetadataStream.tid_Field,
                                                     (int)fdef_row, 2);

                    var ft      = ts.m.GetFieldType(ref fsig, ts.gtparams, null);
                    var ft_size = t.GetSize(ft);

                    ft_size = util.util.align(ft_size, align);

                    bool     is_tls = ts.m.thread_local_fields[(int)fdef_row];
                    ISection cur_os = os;
                    if (is_tls)
                    {
                        if (tlsos == null)
                        {
                            throw new NotSupportedException("No thread-local section provided");
                        }
                        cur_os = tlsos;
                    }

                    /* See if there is any data defined as an rva */
                    var rva = ts.m.fieldrvas[(int)fdef_row];
                    if (rva != 0)
                    {
                        var rrva = (int)ts.m.ResolveRVA(rva);
                        for (int i = 0; i < ft_size; i++)
                        {
                            cur_os.Data.Add(ts.m.file.ReadByte(rrva++));
                        }
                    }
                    else
                    {
                        for (int i = 0; i < ft_size; i++)
                        {
                            cur_os.Data.Add(0);
                        }
                    }

                    /* Output any additional defined symbols */
                    foreach (var alias in ts.m.GetFieldAliases((int)fdef_row))
                    {
                        var asym = of.CreateSymbol();
                        asym.Name       = alias;
                        asym.Offset     = offset + (ulong)cur_offset;
                        asym.Type       = binary_library.SymbolType.Global;
                        asym.ObjectType = binary_library.SymbolObjectType.Object;
                        cur_os.AddSymbol(asym);

                        asym.Size = ft_size;

                        if (base_m != null && ts.m != base_m)
                        {
                            asym.Type = binary_library.SymbolType.Weak;
                        }
                    }

                    if (is_tls)
                    {
                        cur_tloffset += ft_size;
                    }
                    else
                    {
                        cur_offset += ft_size;
                    }
                }
            }

            if (cur_offset > 0)
            {
                /* Add symbol */

                var sym = of.CreateSymbol();
                sym.Name       = ts.m.MangleType(ts) + "S";
                sym.Offset     = offset;
                sym.Type       = binary_library.SymbolType.Global;
                sym.ObjectType = binary_library.SymbolObjectType.Object;
                os.AddSymbol(sym);

                sym.Size = os.Data.Count - (int)offset;

                if (base_m != null && ts.m != base_m)
                {
                    sym.Type = binary_library.SymbolType.Weak;
                }
            }
            if (cur_tloffset > 0)
            {
                /* Add thread local symbol */
                var sym = of.CreateSymbol();
                sym.Name       = ts.m.MangleType(ts) + "ST";
                sym.Offset     = tl_offset;
                sym.Type       = binary_library.SymbolType.Global;
                sym.ObjectType = binary_library.SymbolObjectType.Object;
                tlsos.AddSymbol(sym);

                sym.Size = tlsos.Data.Count - (int)tl_offset;

                if (base_m != null && ts.m != base_m)
                {
                    sym.Type = binary_library.SymbolType.Weak;
                }
            }
        }