From: Zoltan Varga Date: Wed, 12 Jun 2013 16:33:33 +0000 (+0200) Subject: Revert "Add beginnings of a managed IL disassembler which uses Mono.Cecil. Not yet... X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=commitdiff_plain;h=650d09e31906a41168acf8cc2766867689f233bd;p=mono.git Revert "Add beginnings of a managed IL disassembler which uses Mono.Cecil. Not yet part of the build." This reverts commit 535e76a05898eb0bb62932f12c145ad02c215f64. Revert this as we will be using something else. --- diff --git a/mcs/tools/ildasm/DataConverter.cs b/mcs/tools/ildasm/DataConverter.cs deleted file mode 100644 index d0bf0476f60..00000000000 --- a/mcs/tools/ildasm/DataConverter.cs +++ /dev/null @@ -1,1836 +0,0 @@ -// -// Authors: -// Miguel de Icaza (miguel@novell.com) -// -// See the following url for documentation: -// http://www.mono-project.com/Mono_DataConvert -// -// Compilation Options: -// MONO_DATACONVERTER_PUBLIC: -// Makes the class public instead of the default internal. -// -// MONO_DATACONVERTER_STATIC_METHODS: -// Exposes the public static methods. -// -// TODO: -// Support for "DoubleWordsAreSwapped" for ARM devices -// -// Copyright (C) 2006 Novell, Inc (http://www.novell.com) -// -// Permission is hereby granted, free of charge, to any person obtaining -// a copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to -// permit persons to whom the Software is furnished to do so, subject to -// the following conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -// -using System; -using System.Collections; -using System.Text; - -#pragma warning disable 3021 - -namespace Mono { - -#if MONO_DATACONVERTER_PUBLIC - unsafe public abstract class DataConverter { -#else - unsafe internal abstract class DataConverter { - -// Disables the warning: CLS compliance checking will not be performed on -// `XXXX' because it is not visible from outside this assembly -#pragma warning disable 3019 -#endif - static DataConverter SwapConv = new SwapConverter (); - static DataConverter CopyConv = new CopyConverter (); - - public static readonly bool IsLittleEndian = BitConverter.IsLittleEndian; - - public abstract double GetDouble (byte [] data, int index); - public abstract float GetFloat (byte [] data, int index); - public abstract long GetInt64 (byte [] data, int index); - public abstract int GetInt32 (byte [] data, int index); - - public abstract short GetInt16 (byte [] data, int index); - - [CLSCompliant (false)] - public abstract uint GetUInt32 (byte [] data, int index); - [CLSCompliant (false)] - public abstract ushort GetUInt16 (byte [] data, int index); - [CLSCompliant (false)] - public abstract ulong GetUInt64 (byte [] data, int index); - - public abstract void PutBytes (byte [] dest, int destIdx, double value); - public abstract void PutBytes (byte [] dest, int destIdx, float value); - public abstract void PutBytes (byte [] dest, int destIdx, int value); - public abstract void PutBytes (byte [] dest, int destIdx, long value); - public abstract void PutBytes (byte [] dest, int destIdx, short value); - - [CLSCompliant (false)] - public abstract void PutBytes (byte [] dest, int destIdx, ushort value); - [CLSCompliant (false)] - public abstract void PutBytes (byte [] dest, int destIdx, uint value); - [CLSCompliant (false)] - public abstract void PutBytes (byte [] dest, int destIdx, ulong value); - - public byte[] GetBytes (double value) - { - byte [] ret = new byte [8]; - PutBytes (ret, 0, value); - return ret; - } - - public byte[] GetBytes (float value) - { - byte [] ret = new byte [4]; - PutBytes (ret, 0, value); - return ret; - } - - public byte[] GetBytes (int value) - { - byte [] ret = new byte [4]; - PutBytes (ret, 0, value); - return ret; - } - - public byte[] GetBytes (long value) - { - byte [] ret = new byte [8]; - PutBytes (ret, 0, value); - return ret; - } - - public byte[] GetBytes (short value) - { - byte [] ret = new byte [2]; - PutBytes (ret, 0, value); - return ret; - } - - [CLSCompliant (false)] - public byte[] GetBytes (ushort value) - { - byte [] ret = new byte [2]; - PutBytes (ret, 0, value); - return ret; - } - - [CLSCompliant (false)] - public byte[] GetBytes (uint value) - { - byte [] ret = new byte [4]; - PutBytes (ret, 0, value); - return ret; - } - - [CLSCompliant (false)] - public byte[] GetBytes (ulong value) - { - byte [] ret = new byte [8]; - PutBytes (ret, 0, value); - return ret; - } - - static public DataConverter LittleEndian { - get { - return BitConverter.IsLittleEndian ? CopyConv : SwapConv; - } - } - - static public DataConverter BigEndian { - get { - return BitConverter.IsLittleEndian ? SwapConv : CopyConv; - } - } - - static public DataConverter Native { - get { - return CopyConv; - } - } - - static int Align (int current, int align) - { - return ((current + align - 1) / align) * align; - } - - class PackContext { - // Buffer - public byte [] buffer; - int next; - - public string description; - public int i; // position in the description - public DataConverter conv; - public int repeat; - - // - // if align == -1, auto align to the size of the byte array - // if align == 0, do not do alignment - // Any other values aligns to that particular size - // - public int align; - - public void Add (byte [] group) - { - //Console.WriteLine ("Adding {0} bytes to {1} (next={2}", group.Length, - // buffer == null ? "null" : buffer.Length.ToString (), next); - - if (buffer == null){ - buffer = group; - next = group.Length; - return; - } - if (align != 0){ - if (align == -1) - next = Align (next, group.Length); - else - next = Align (next, align); - align = 0; - } - - if (next + group.Length > buffer.Length){ - byte [] nb = new byte [System.Math.Max (next, 16) * 2 + group.Length]; - Array.Copy (buffer, nb, buffer.Length); - Array.Copy (group, 0, nb, next, group.Length); - next = next + group.Length; - buffer = nb; - } else { - Array.Copy (group, 0, buffer, next, group.Length); - next += group.Length; - } - } - - public byte [] Get () - { - if (buffer == null) - return new byte [0]; - - if (buffer.Length != next){ - byte [] b = new byte [next]; - Array.Copy (buffer, b, next); - return b; - } - return buffer; - } - } - - // - // Format includes: - // Control: - // ^ Switch to big endian encoding - // _ Switch to little endian encoding - // % Switch to host (native) encoding - // ! aligns the next data type to its natural boundary (for strings this is 4). - // - // Types: - // s Int16 - // S UInt16 - // i Int32 - // I UInt32 - // l Int64 - // L UInt64 - // f float - // d double - // b byte - // c 1-byte signed character - // C 1-byte unsigned character - // z8 string encoded as UTF8 with 1-byte null terminator - // z6 string encoded as UTF16 with 2-byte null terminator - // z7 string encoded as UTF7 with 1-byte null terminator - // zb string encoded as BigEndianUnicode with 2-byte null terminator - // z3 string encoded as UTF32 with 4-byte null terminator - // z4 string encoded as UTF32 big endian with 4-byte null terminator - // $8 string encoded as UTF8 - // $6 string encoded as UTF16 - // $7 string encoded as UTF7 - // $b string encoded as BigEndianUnicode - // $3 string encoded as UTF32 - // $4 string encoded as UTF-32 big endian encoding - // x null byte - // - // Repeats, these are prefixes: - // N a number between 1 and 9, indicates a repeat count (process N items - // with the following datatype - // [N] For numbers larger than 9, use brackets, for example [20] - // * Repeat the next data type until the arguments are exhausted - // - static public byte [] Pack (string description, params object [] args) - { - int argn = 0; - PackContext b = new PackContext (); - b.conv = CopyConv; - b.description = description; - - for (b.i = 0; b.i < description.Length; ){ - object oarg; - - if (argn < args.Length) - oarg = args [argn]; - else { - if (b.repeat != 0) - break; - - oarg = null; - } - - int save = b.i; - - if (PackOne (b, oarg)){ - argn++; - if (b.repeat > 0){ - if (--b.repeat > 0) - b.i = save; - else - b.i++; - } else - b.i++; - } else - b.i++; - } - return b.Get (); - } - - static public byte [] PackEnumerable (string description, IEnumerable args) - { - PackContext b = new PackContext (); - b.conv = CopyConv; - b.description = description; - - IEnumerator enumerator = args.GetEnumerator (); - bool ok = enumerator.MoveNext (); - - for (b.i = 0; b.i < description.Length; ){ - object oarg; - - if (ok) - oarg = enumerator.Current; - else { - if (b.repeat != 0) - break; - oarg = null; - } - - int save = b.i; - - if (PackOne (b, oarg)){ - ok = enumerator.MoveNext (); - if (b.repeat > 0){ - if (--b.repeat > 0) - b.i = save; - else - b.i++; - } else - b.i++; - } else - b.i++; - } - return b.Get (); - } - - // - // Packs one datum `oarg' into the buffer `b', using the string format - // in `description' at position `i' - // - // Returns: true if we must pick the next object from the list - // - static bool PackOne (PackContext b, object oarg) - { - int n; - - switch (b.description [b.i]){ - case '^': - b.conv = BigEndian; - return false; - case '_': - b.conv = LittleEndian; - return false; - case '%': - b.conv = Native; - return false; - - case '!': - b.align = -1; - return false; - - case 'x': - b.Add (new byte [] { 0 }); - return false; - - // Type Conversions - case 'i': - b.Add (b.conv.GetBytes (Convert.ToInt32 (oarg))); - break; - - case 'I': - b.Add (b.conv.GetBytes (Convert.ToUInt32 (oarg))); - break; - - case 's': - b.Add (b.conv.GetBytes (Convert.ToInt16 (oarg))); - break; - - case 'S': - b.Add (b.conv.GetBytes (Convert.ToUInt16 (oarg))); - break; - - case 'l': - b.Add (b.conv.GetBytes (Convert.ToInt64 (oarg))); - break; - - case 'L': - b.Add (b.conv.GetBytes (Convert.ToUInt64 (oarg))); - break; - - case 'f': - b.Add (b.conv.GetBytes (Convert.ToSingle (oarg))); - break; - - case 'd': - b.Add (b.conv.GetBytes (Convert.ToDouble (oarg))); - break; - - case 'b': - b.Add (new byte [] { Convert.ToByte (oarg) }); - break; - - case 'c': - b.Add (new byte [] { (byte) (Convert.ToSByte (oarg)) }); - break; - - case 'C': - b.Add (new byte [] { Convert.ToByte (oarg) }); - break; - - // Repeat acount; - case '1': case '2': case '3': case '4': case '5': - case '6': case '7': case '8': case '9': - b.repeat = ((short) b.description [b.i]) - ((short) '0'); - return false; - - case '*': - b.repeat = Int32.MaxValue; - return false; - - case '[': - int count = -1, j; - - for (j = b.i+1; j < b.description.Length; j++){ - if (b.description [j] == ']') - break; - n = ((short) b.description [j]) - ((short) '0'); - if (n >= 0 && n <= 9){ - if (count == -1) - count = n; - else - count = count * 10 + n; - } - } - if (count == -1) - throw new ArgumentException ("invalid size specification"); - b.i = j; - b.repeat = count; - return false; - - case '$': case 'z': - bool add_null = b.description [b.i] == 'z'; - b.i++; - if (b.i >= b.description.Length) - throw new ArgumentException ("$ description needs a type specified", "description"); - char d = b.description [b.i]; - Encoding e; - - switch (d){ - case '8': - e = Encoding.UTF8; - n = 1; - break; - case '6': - e = Encoding.Unicode; - n = 2; - break; - case '7': - e = Encoding.UTF7; - n = 1; - break; - case 'b': - e = Encoding.BigEndianUnicode; - n = 2; - break; - case '3': - e = Encoding.GetEncoding (12000); - n = 4; - break; - case '4': - e = Encoding.GetEncoding (12001); - n = 4; - break; - - default: - throw new ArgumentException ("Invalid format for $ specifier", "description"); - } - if (b.align == -1) - b.align = 4; - b.Add (e.GetBytes (Convert.ToString (oarg))); - if (add_null) - b.Add (new byte [n]); - break; - default: - throw new ArgumentException (String.Format ("invalid format specified `{0}'", - b.description [b.i])); - } - return true; - } - - static bool Prepare (byte [] buffer, ref int idx, int size, ref bool align) - { - if (align){ - idx = Align (idx, size); - align = false; - } - if (idx + size > buffer.Length){ - idx = buffer.Length; - return false; - } - return true; - } - - static public IList Unpack (string description, byte [] buffer, int startIndex) - { - DataConverter conv = CopyConv; - ArrayList result = new ArrayList (); - int idx = startIndex; - bool align = false; - int repeat = 0, n; - - for (int i = 0; i < description.Length && idx < buffer.Length; ){ - int save = i; - - switch (description [i]){ - case '^': - conv = BigEndian; - break; - case '_': - conv = LittleEndian; - break; - case '%': - conv = Native; - break; - case 'x': - idx++; - break; - - case '!': - align = true; - break; - - // Type Conversions - case 'i': - if (Prepare (buffer, ref idx, 4, ref align)){ - result.Add (conv.GetInt32 (buffer, idx)); - idx += 4; - } - break; - - case 'I': - if (Prepare (buffer, ref idx, 4, ref align)){ - result.Add (conv.GetUInt32 (buffer, idx)); - idx += 4; - } - break; - - case 's': - if (Prepare (buffer, ref idx, 2, ref align)){ - result.Add (conv.GetInt16 (buffer, idx)); - idx += 2; - } - break; - - case 'S': - if (Prepare (buffer, ref idx, 2, ref align)){ - result.Add (conv.GetUInt16 (buffer, idx)); - idx += 2; - } - break; - - case 'l': - if (Prepare (buffer, ref idx, 8, ref align)){ - result.Add (conv.GetInt64 (buffer, idx)); - idx += 8; - } - break; - - case 'L': - if (Prepare (buffer, ref idx, 8, ref align)){ - result.Add (conv.GetUInt64 (buffer, idx)); - idx += 8; - } - break; - - case 'f': - if (Prepare (buffer, ref idx, 4, ref align)){ - result.Add (conv.GetDouble (buffer, idx)); - idx += 4; - } - break; - - case 'd': - if (Prepare (buffer, ref idx, 8, ref align)){ - result.Add (conv.GetDouble (buffer, idx)); - idx += 8; - } - break; - - case 'b': - if (Prepare (buffer, ref idx, 1, ref align)){ - result.Add (buffer [idx]); - idx++; - } - break; - - case 'c': case 'C': - if (Prepare (buffer, ref idx, 1, ref align)){ - char c; - - if (description [i] == 'c') - c = ((char) ((sbyte)buffer [idx])); - else - c = ((char) ((byte)buffer [idx])); - - result.Add (c); - idx++; - } - break; - - // Repeat acount; - case '1': case '2': case '3': case '4': case '5': - case '6': case '7': case '8': case '9': - repeat = ((short) description [i]) - ((short) '0'); - save = i + 1; - break; - - case '*': - repeat = Int32.MaxValue; - break; - - case '[': - int count = -1, j; - - for (j = i+1; j < description.Length; j++){ - if (description [j] == ']') - break; - n = ((short) description [j]) - ((short) '0'); - if (n >= 0 && n <= 9){ - if (count == -1) - count = n; - else - count = count * 10 + n; - } - } - if (count == -1) - throw new ArgumentException ("invalid size specification"); - i = j; - repeat = count; - break; - - case '$': case 'z': - // bool with_null = description [i] == 'z'; - i++; - if (i >= description.Length) - throw new ArgumentException ("$ description needs a type specified", "description"); - char d = description [i]; - Encoding e; - if (align){ - idx = Align (idx, 4); - align = false; - } - if (idx >= buffer.Length) - break; - - switch (d){ - case '8': - e = Encoding.UTF8; - n = 1; - break; - case '6': - e = Encoding.Unicode; - n = 2; - break; - case '7': - e = Encoding.UTF7; - n = 1; - break; - case 'b': - e = Encoding.BigEndianUnicode; - n = 2; - break; - case '3': - e = Encoding.GetEncoding (12000); - n = 4; - break; - case '4': - e = Encoding.GetEncoding (12001); - n = 4; - break; - - default: - throw new ArgumentException ("Invalid format for $ specifier", "description"); - } - int k = idx; - switch (n){ - case 1: - for (; k < buffer.Length && buffer [k] != 0; k++) - ; - result.Add (e.GetChars (buffer, idx, k-idx)); - if (k == buffer.Length) - idx = k; - else - idx = k+1; - break; - - case 2: - for (; k < buffer.Length; k++){ - if (k+1 == buffer.Length){ - k++; - break; - } - if (buffer [k] == 0 && buffer [k+1] == 0) - break; - } - result.Add (e.GetChars (buffer, idx, k-idx)); - if (k == buffer.Length) - idx = k; - else - idx = k+2; - break; - - case 4: - for (; k < buffer.Length; k++){ - if (k+3 >= buffer.Length){ - k = buffer.Length; - break; - } - if (buffer[k]==0 && buffer[k+1] == 0 && buffer[k+2] == 0 && buffer[k+3]== 0) - break; - } - result.Add (e.GetChars (buffer, idx, k-idx)); - if (k == buffer.Length) - idx = k; - else - idx = k+4; - break; - } - break; - default: - throw new ArgumentException (String.Format ("invalid format specified `{0}'", - description [i])); - } - - if (repeat > 0){ - if (--repeat > 0) - i = save; - } else - i++; - } - return result; - } - - internal void Check (byte [] dest, int destIdx, int size) - { - if (dest == null) - throw new ArgumentNullException ("dest"); - if (destIdx < 0 || destIdx > dest.Length - size) - throw new ArgumentException ("destIdx"); - } - - class CopyConverter : DataConverter { - public override double GetDouble (byte [] data, int index) - { - if (data == null) - throw new ArgumentNullException ("data"); - if (data.Length - index < 8) - throw new ArgumentException ("index"); - if (index < 0) - throw new ArgumentException ("index"); - double ret; - byte *b = (byte *)&ret; - - for (int i = 0; i < 8; i++) - b [i] = data [index+i]; - - return ret; - } - - public override ulong GetUInt64 (byte [] data, int index) - { - if (data == null) - throw new ArgumentNullException ("data"); - if (data.Length - index < 8) - throw new ArgumentException ("index"); - if (index < 0) - throw new ArgumentException ("index"); - - ulong ret; - byte *b = (byte *)&ret; - - for (int i = 0; i < 8; i++) - b [i] = data [index+i]; - - return ret; - } - - public override long GetInt64 (byte [] data, int index) - { - if (data == null) - throw new ArgumentNullException ("data"); - if (data.Length - index < 8) - throw new ArgumentException ("index"); - if (index < 0) - throw new ArgumentException ("index"); - - long ret; - byte *b = (byte *)&ret; - - for (int i = 0; i < 8; i++) - b [i] = data [index+i]; - - return ret; - } - - public override float GetFloat (byte [] data, int index) - { - if (data == null) - throw new ArgumentNullException ("data"); - if (data.Length - index < 4) - throw new ArgumentException ("index"); - if (index < 0) - throw new ArgumentException ("index"); - - float ret; - byte *b = (byte *)&ret; - - for (int i = 0; i < 4; i++) - b [i] = data [index+i]; - - return ret; - } - - public override int GetInt32 (byte [] data, int index) - { - if (data == null) - throw new ArgumentNullException ("data"); - if (data.Length - index < 4) - throw new ArgumentException ("index"); - if (index < 0) - throw new ArgumentException ("index"); - - int ret; - byte *b = (byte *)&ret; - - for (int i = 0; i < 4; i++) - b [i] = data [index+i]; - - return ret; - } - - public override uint GetUInt32 (byte [] data, int index) - { - if (data == null) - throw new ArgumentNullException ("data"); - if (data.Length - index < 4) - throw new ArgumentException ("index"); - if (index < 0) - throw new ArgumentException ("index"); - - uint ret; - byte *b = (byte *)&ret; - - for (int i = 0; i < 4; i++) - b [i] = data [index+i]; - - return ret; - } - - public override short GetInt16 (byte [] data, int index) - { - if (data == null) - throw new ArgumentNullException ("data"); - if (data.Length - index < 2) - throw new ArgumentException ("index"); - if (index < 0) - throw new ArgumentException ("index"); - - short ret; - byte *b = (byte *)&ret; - - for (int i = 0; i < 2; i++) - b [i] = data [index+i]; - - return ret; - } - - public override ushort GetUInt16 (byte [] data, int index) - { - if (data == null) - throw new ArgumentNullException ("data"); - if (data.Length - index < 2) - throw new ArgumentException ("index"); - if (index < 0) - throw new ArgumentException ("index"); - - ushort ret; - byte *b = (byte *)&ret; - - for (int i = 0; i < 2; i++) - b [i] = data [index+i]; - - return ret; - } - - public override void PutBytes (byte [] dest, int destIdx, double value) - { - Check (dest, destIdx, 8); - fixed (byte *target = &dest [destIdx]){ - long *source = (long *) &value; - - *((long *)target) = *source; - } - } - - public override void PutBytes (byte [] dest, int destIdx, float value) - { - Check (dest, destIdx, 4); - fixed (byte *target = &dest [destIdx]){ - uint *source = (uint *) &value; - - *((uint *)target) = *source; - } - } - - public override void PutBytes (byte [] dest, int destIdx, int value) - { - Check (dest, destIdx, 4); - fixed (byte *target = &dest [destIdx]){ - uint *source = (uint *) &value; - - *((uint *)target) = *source; - } - } - - public override void PutBytes (byte [] dest, int destIdx, uint value) - { - Check (dest, destIdx, 4); - fixed (byte *target = &dest [destIdx]){ - uint *source = (uint *) &value; - - *((uint *)target) = *source; - } - } - - public override void PutBytes (byte [] dest, int destIdx, long value) - { - Check (dest, destIdx, 8); - fixed (byte *target = &dest [destIdx]){ - long *source = (long *) &value; - - *((long*)target) = *source; - } - } - - public override void PutBytes (byte [] dest, int destIdx, ulong value) - { - Check (dest, destIdx, 8); - fixed (byte *target = &dest [destIdx]){ - ulong *source = (ulong *) &value; - - *((ulong *) target) = *source; - } - } - - public override void PutBytes (byte [] dest, int destIdx, short value) - { - Check (dest, destIdx, 2); - fixed (byte *target = &dest [destIdx]){ - ushort *source = (ushort *) &value; - - *((ushort *)target) = *source; - } - } - - public override void PutBytes (byte [] dest, int destIdx, ushort value) - { - Check (dest, destIdx, 2); - fixed (byte *target = &dest [destIdx]){ - ushort *source = (ushort *) &value; - - *((ushort *)target) = *source; - } - } - } - - class SwapConverter : DataConverter { - public override double GetDouble (byte [] data, int index) - { - if (data == null) - throw new ArgumentNullException ("data"); - if (data.Length - index < 8) - throw new ArgumentException ("index"); - if (index < 0) - throw new ArgumentException ("index"); - - double ret; - byte *b = (byte *)&ret; - - for (int i = 0; i < 8; i++) - b [7-i] = data [index+i]; - - return ret; - } - - public override ulong GetUInt64 (byte [] data, int index) - { - if (data == null) - throw new ArgumentNullException ("data"); - if (data.Length - index < 8) - throw new ArgumentException ("index"); - if (index < 0) - throw new ArgumentException ("index"); - - ulong ret; - byte *b = (byte *)&ret; - - for (int i = 0; i < 8; i++) - b [7-i] = data [index+i]; - - return ret; - } - - public override long GetInt64 (byte [] data, int index) - { - if (data == null) - throw new ArgumentNullException ("data"); - if (data.Length - index < 8) - throw new ArgumentException ("index"); - if (index < 0) - throw new ArgumentException ("index"); - - long ret; - byte *b = (byte *)&ret; - - for (int i = 0; i < 8; i++) - b [7-i] = data [index+i]; - - return ret; - } - - public override float GetFloat (byte [] data, int index) - { - if (data == null) - throw new ArgumentNullException ("data"); - if (data.Length - index < 4) - throw new ArgumentException ("index"); - if (index < 0) - throw new ArgumentException ("index"); - - float ret; - byte *b = (byte *)&ret; - - for (int i = 0; i < 4; i++) - b [3-i] = data [index+i]; - - return ret; - } - - public override int GetInt32 (byte [] data, int index) - { - if (data == null) - throw new ArgumentNullException ("data"); - if (data.Length - index < 4) - throw new ArgumentException ("index"); - if (index < 0) - throw new ArgumentException ("index"); - - int ret; - byte *b = (byte *)&ret; - - for (int i = 0; i < 4; i++) - b [3-i] = data [index+i]; - - return ret; - } - - public override uint GetUInt32 (byte [] data, int index) - { - if (data == null) - throw new ArgumentNullException ("data"); - if (data.Length - index < 4) - throw new ArgumentException ("index"); - if (index < 0) - throw new ArgumentException ("index"); - - uint ret; - byte *b = (byte *)&ret; - - for (int i = 0; i < 4; i++) - b [3-i] = data [index+i]; - - return ret; - } - - public override short GetInt16 (byte [] data, int index) - { - if (data == null) - throw new ArgumentNullException ("data"); - if (data.Length - index < 2) - throw new ArgumentException ("index"); - if (index < 0) - throw new ArgumentException ("index"); - - short ret; - byte *b = (byte *)&ret; - - for (int i = 0; i < 2; i++) - b [1-i] = data [index+i]; - - return ret; - } - - public override ushort GetUInt16 (byte [] data, int index) - { - if (data == null) - throw new ArgumentNullException ("data"); - if (data.Length - index < 2) - throw new ArgumentException ("index"); - if (index < 0) - throw new ArgumentException ("index"); - - ushort ret; - byte *b = (byte *)&ret; - - for (int i = 0; i < 2; i++) - b [1-i] = data [index+i]; - - return ret; - } - - public override void PutBytes (byte [] dest, int destIdx, double value) - { - Check (dest, destIdx, 8); - - fixed (byte *target = &dest [destIdx]){ - byte *source = (byte *) &value; - - for (int i = 0; i < 8; i++) - target [i] = source [7-i]; - } - } - - public override void PutBytes (byte [] dest, int destIdx, float value) - { - Check (dest, destIdx, 4); - - fixed (byte *target = &dest [destIdx]){ - byte *source = (byte *) &value; - - for (int i = 0; i < 4; i++) - target [i] = source [3-i]; - } - } - - public override void PutBytes (byte [] dest, int destIdx, int value) - { - Check (dest, destIdx, 4); - - fixed (byte *target = &dest [destIdx]){ - byte *source = (byte *) &value; - - for (int i = 0; i < 4; i++) - target [i] = source [3-i]; - } - } - - public override void PutBytes (byte [] dest, int destIdx, uint value) - { - Check (dest, destIdx, 4); - - fixed (byte *target = &dest [destIdx]){ - byte *source = (byte *) &value; - - for (int i = 0; i < 4; i++) - target [i] = source [3-i]; - } - } - - public override void PutBytes (byte [] dest, int destIdx, long value) - { - Check (dest, destIdx, 8); - - fixed (byte *target = &dest [destIdx]){ - byte *source = (byte *) &value; - - for (int i = 0; i < 8; i++) - target [i] = source [7-i]; - } - } - - public override void PutBytes (byte [] dest, int destIdx, ulong value) - { - Check (dest, destIdx, 8); - - fixed (byte *target = &dest [destIdx]){ - byte *source = (byte *) &value; - - for (int i = 0; i < 4; i++) - target [i] = source [7-i]; - } - } - - public override void PutBytes (byte [] dest, int destIdx, short value) - { - Check (dest, destIdx, 2); - - fixed (byte *target = &dest [destIdx]){ - byte *source = (byte *) &value; - - for (int i = 0; i < 2; i++) - target [i] = source [1-i]; - } - } - - public override void PutBytes (byte [] dest, int destIdx, ushort value) - { - Check (dest, destIdx, 2); - - fixed (byte *target = &dest [destIdx]){ - byte *source = (byte *) &value; - - for (int i = 0; i < 2; i++) - target [i] = source [1-i]; - } - } - } - -#if MONO_DATACONVERTER_STATIC_METHODS - static unsafe void PutBytesLE (byte *dest, byte *src, int count) - { - int i = 0; - - if (BitConverter.IsLittleEndian){ - for (; i < count; i++) - *dest++ = *src++; - } else { - dest += count; - for (; i < count; i++) - *(--dest) = *src++; - } - } - - static unsafe void PutBytesBE (byte *dest, byte *src, int count) - { - int i = 0; - - if (BitConverter.IsLittleEndian){ - dest += count; - for (; i < count; i++) - *(--dest) = *src++; - } else { - for (; i < count; i++) - *dest++ = *src++; - } - } - - static unsafe void PutBytesNative (byte *dest, byte *src, int count) - { - int i = 0; - - for (; i < count; i++) - dest [i-count] = *src++; - } - - static public unsafe double DoubleFromLE (byte[] data, int index) - { - if (data == null) - throw new ArgumentNullException ("data"); - if (data.Length - index < 8) - throw new ArgumentException ("index"); - if (index < 0) - throw new ArgumentException ("index"); - - double ret; - fixed (byte *src = &data[index]){ - PutBytesLE ((byte *) &ret, src, 8); - } - return ret; - } - - static public unsafe float FloatFromLE (byte [] data, int index) - { - if (data == null) - throw new ArgumentNullException ("data"); - if (data.Length - index < 4) - throw new ArgumentException ("index"); - if (index < 0) - throw new ArgumentException ("index"); - - float ret; - fixed (byte *src = &data[index]){ - PutBytesLE ((byte *) &ret, src, 4); - } - return ret; - } - - static public unsafe long Int64FromLE (byte [] data, int index) - { - if (data == null) - throw new ArgumentNullException ("data"); - if (data.Length - index < 8) - throw new ArgumentException ("index"); - if (index < 0) - throw new ArgumentException ("index"); - - long ret; - fixed (byte *src = &data[index]){ - PutBytesLE ((byte *) &ret, src, 8); - } - return ret; - } - - static public unsafe ulong UInt64FromLE (byte [] data, int index) - { - if (data == null) - throw new ArgumentNullException ("data"); - if (data.Length - index < 8) - throw new ArgumentException ("index"); - if (index < 0) - throw new ArgumentException ("index"); - - ulong ret; - fixed (byte *src = &data[index]){ - PutBytesLE ((byte *) &ret, src, 8); - } - return ret; - } - - static public unsafe int Int32FromLE (byte [] data, int index) - { - if (data == null) - throw new ArgumentNullException ("data"); - if (data.Length - index < 4) - throw new ArgumentException ("index"); - if (index < 0) - throw new ArgumentException ("index"); - - int ret; - fixed (byte *src = &data[index]){ - PutBytesLE ((byte *) &ret, src, 4); - } - return ret; - } - - static public unsafe uint UInt32FromLE (byte [] data, int index) - { - if (data == null) - throw new ArgumentNullException ("data"); - if (data.Length - index < 4) - throw new ArgumentException ("index"); - if (index < 0) - throw new ArgumentException ("index"); - - uint ret; - fixed (byte *src = &data[index]){ - PutBytesLE ((byte *) &ret, src, 4); - } - return ret; - } - - static public unsafe short Int16FromLE (byte [] data, int index) - { - if (data == null) - throw new ArgumentNullException ("data"); - if (data.Length - index < 2) - throw new ArgumentException ("index"); - if (index < 0) - throw new ArgumentException ("index"); - - short ret; - fixed (byte *src = &data[index]){ - PutBytesLE ((byte *) &ret, src, 2); - } - return ret; - } - - static public unsafe ushort UInt16FromLE (byte [] data, int index) - { - if (data == null) - throw new ArgumentNullException ("data"); - if (data.Length - index < 2) - throw new ArgumentException ("index"); - if (index < 0) - throw new ArgumentException ("index"); - - ushort ret; - fixed (byte *src = &data[index]){ - PutBytesLE ((byte *) &ret, src, 2); - } - return ret; - } - - static public unsafe double DoubleFromBE (byte[] data, int index) - { - if (data == null) - throw new ArgumentNullException ("data"); - if (data.Length - index < 8) - throw new ArgumentException ("index"); - if (index < 0) - throw new ArgumentException ("index"); - - double ret; - fixed (byte *src = &data[index]){ - PutBytesBE ((byte *) &ret, src, 8); - } - return ret; - } - - static public unsafe float FloatFromBE (byte [] data, int index) - { - if (data == null) - throw new ArgumentNullException ("data"); - if (data.Length - index < 4) - throw new ArgumentException ("index"); - if (index < 0) - throw new ArgumentException ("index"); - - float ret; - fixed (byte *src = &data[index]){ - PutBytesBE ((byte *) &ret, src, 4); - } - return ret; - } - - static public unsafe long Int64FromBE (byte [] data, int index) - { - if (data == null) - throw new ArgumentNullException ("data"); - if (data.Length - index < 8) - throw new ArgumentException ("index"); - if (index < 0) - throw new ArgumentException ("index"); - - long ret; - fixed (byte *src = &data[index]){ - PutBytesBE ((byte *) &ret, src, 8); - } - return ret; - } - - static public unsafe ulong UInt64FromBE (byte [] data, int index) - { - if (data == null) - throw new ArgumentNullException ("data"); - if (data.Length - index < 8) - throw new ArgumentException ("index"); - if (index < 0) - throw new ArgumentException ("index"); - - ulong ret; - fixed (byte *src = &data[index]){ - PutBytesBE ((byte *) &ret, src, 8); - } - return ret; - } - - static public unsafe int Int32FromBE (byte [] data, int index) - { - if (data == null) - throw new ArgumentNullException ("data"); - if (data.Length - index < 4) - throw new ArgumentException ("index"); - if (index < 0) - throw new ArgumentException ("index"); - - int ret; - fixed (byte *src = &data[index]){ - PutBytesBE ((byte *) &ret, src, 4); - } - return ret; - } - - static public unsafe uint UInt32FromBE (byte [] data, int index) - { - if (data == null) - throw new ArgumentNullException ("data"); - if (data.Length - index < 4) - throw new ArgumentException ("index"); - if (index < 0) - throw new ArgumentException ("index"); - - uint ret; - fixed (byte *src = &data[index]){ - PutBytesBE ((byte *) &ret, src, 4); - } - return ret; - } - - static public unsafe short Int16FromBE (byte [] data, int index) - { - if (data == null) - throw new ArgumentNullException ("data"); - if (data.Length - index < 2) - throw new ArgumentException ("index"); - if (index < 0) - throw new ArgumentException ("index"); - - short ret; - fixed (byte *src = &data[index]){ - PutBytesBE ((byte *) &ret, src, 2); - } - return ret; - } - - static public unsafe ushort UInt16FromBE (byte [] data, int index) - { - if (data == null) - throw new ArgumentNullException ("data"); - if (data.Length - index < 2) - throw new ArgumentException ("index"); - if (index < 0) - throw new ArgumentException ("index"); - - ushort ret; - fixed (byte *src = &data[index]){ - PutBytesBE ((byte *) &ret, src, 2); - } - return ret; - } - - static public unsafe double DoubleFromNative (byte[] data, int index) - { - if (data == null) - throw new ArgumentNullException ("data"); - if (data.Length - index < 8) - throw new ArgumentException ("index"); - if (index < 0) - throw new ArgumentException ("index"); - - double ret; - fixed (byte *src = &data[index]){ - PutBytesNative ((byte *) &ret, src, 8); - } - return ret; - } - - static public unsafe float FloatFromNative (byte [] data, int index) - { - if (data == null) - throw new ArgumentNullException ("data"); - if (data.Length - index < 4) - throw new ArgumentException ("index"); - if (index < 0) - throw new ArgumentException ("index"); - - float ret; - fixed (byte *src = &data[index]){ - PutBytesNative ((byte *) &ret, src, 4); - } - return ret; - } - - static public unsafe long Int64FromNative (byte [] data, int index) - { - if (data == null) - throw new ArgumentNullException ("data"); - if (data.Length - index < 8) - throw new ArgumentException ("index"); - if (index < 0) - throw new ArgumentException ("index"); - - long ret; - fixed (byte *src = &data[index]){ - PutBytesNative ((byte *) &ret, src, 8); - } - return ret; - } - - static public unsafe ulong UInt64FromNative (byte [] data, int index) - { - if (data == null) - throw new ArgumentNullException ("data"); - if (data.Length - index < 8) - throw new ArgumentException ("index"); - if (index < 0) - throw new ArgumentException ("index"); - - ulong ret; - fixed (byte *src = &data[index]){ - PutBytesNative ((byte *) &ret, src, 8); - } - return ret; - } - - static public unsafe int Int32FromNative (byte [] data, int index) - { - if (data == null) - throw new ArgumentNullException ("data"); - if (data.Length - index < 4) - throw new ArgumentException ("index"); - if (index < 0) - throw new ArgumentException ("index"); - - int ret; - fixed (byte *src = &data[index]){ - PutBytesNative ((byte *) &ret, src, 4); - } - return ret; - } - - static public unsafe uint UInt32FromNative (byte [] data, int index) - { - if (data == null) - throw new ArgumentNullException ("data"); - if (data.Length - index < 4) - throw new ArgumentException ("index"); - if (index < 0) - throw new ArgumentException ("index"); - - uint ret; - fixed (byte *src = &data[index]){ - PutBytesNative ((byte *) &ret, src, 4); - } - return ret; - } - - static public unsafe short Int16FromNative (byte [] data, int index) - { - if (data == null) - throw new ArgumentNullException ("data"); - if (data.Length - index < 2) - throw new ArgumentException ("index"); - if (index < 0) - throw new ArgumentException ("index"); - - short ret; - fixed (byte *src = &data[index]){ - PutBytesNative ((byte *) &ret, src, 2); - } - return ret; - } - - static public unsafe ushort UInt16FromNative (byte [] data, int index) - { - if (data == null) - throw new ArgumentNullException ("data"); - if (data.Length - index < 2) - throw new ArgumentException ("index"); - if (index < 0) - throw new ArgumentException ("index"); - - ushort ret; - fixed (byte *src = &data[index]){ - PutBytesNative ((byte *) &ret, src, 2); - } - return ret; - } - - unsafe static byte[] GetBytesPtr (byte *ptr, int count) - { - byte [] ret = new byte [count]; - - for (int i = 0; i < count; i++) { - ret [i] = ptr [i]; - } - - return ret; - } - - unsafe static byte[] GetBytesSwap (bool swap, byte *ptr, int count) - { - byte [] ret = new byte [count]; - - if (swap){ - int t = count-1; - for (int i = 0; i < count; i++) { - ret [t-i] = ptr [i]; - } - } else { - for (int i = 0; i < count; i++) { - ret [i] = ptr [i]; - } - } - return ret; - } - - unsafe public static byte[] GetBytesNative (bool value) - { - return GetBytesPtr ((byte *) &value, 1); - } - - unsafe public static byte[] GetBytesNative (char value) - { - return GetBytesPtr ((byte *) &value, 2); - } - - unsafe public static byte[] GetBytesNative (short value) - { - return GetBytesPtr ((byte *) &value, 2); - } - - unsafe public static byte[] GetBytesNative (int value) - { - return GetBytesPtr ((byte *) &value, 4); - } - - unsafe public static byte[] GetBytesNative (long value) - { - return GetBytesPtr ((byte *) &value, 8); - } - - [CLSCompliant (false)] - unsafe public static byte[] GetBytesNative (ushort value) - { - return GetBytesPtr ((byte *) &value, 2); - } - - [CLSCompliant (false)] - unsafe public static byte[] GetBytesNative (uint value) - { - return GetBytesPtr ((byte *) &value, 4); - } - - [CLSCompliant (false)] - unsafe public static byte[] GetBytesNative (ulong value) - { - return GetBytesPtr ((byte *) &value, 8); - } - - unsafe public static byte[] GetBytesNative (float value) - { - return GetBytesPtr ((byte *) &value, 4); - } - - unsafe public static byte[] GetBytesNative (double value) - { - return GetBytesPtr ((byte *) &value, 8); - } - - unsafe public static byte[] GetBytesLE (bool value) - { - return GetBytesSwap (!BitConverter.IsLittleEndian, (byte *) &value, 1); - } - - unsafe public static byte[] GetBytesLE (char value) - { - return GetBytesSwap (!BitConverter.IsLittleEndian, (byte *) &value, 2); - } - - unsafe public static byte[] GetBytesLE (short value) - { - return GetBytesSwap (!BitConverter.IsLittleEndian, (byte *) &value, 2); - } - - unsafe public static byte[] GetBytesLE (int value) - { - return GetBytesSwap (!BitConverter.IsLittleEndian, (byte *) &value, 4); - } - - unsafe public static byte[] GetBytesLE (long value) - { - return GetBytesSwap (!BitConverter.IsLittleEndian, (byte *) &value, 8); - } - - [CLSCompliant (false)] - unsafe public static byte[] GetBytesLE (ushort value) - { - return GetBytesSwap (!BitConverter.IsLittleEndian, (byte *) &value, 2); - } - - [CLSCompliant (false)] - unsafe public static byte[] GetBytesLE (uint value) - { - return GetBytesSwap (!BitConverter.IsLittleEndian, (byte *) &value, 4); - } - - [CLSCompliant (false)] - unsafe public static byte[] GetBytesLE (ulong value) - { - return GetBytesSwap (!BitConverter.IsLittleEndian, (byte *) &value, 8); - } - - unsafe public static byte[] GetBytesLE (float value) - { - return GetBytesSwap (!BitConverter.IsLittleEndian, (byte *) &value, 4); - } - - unsafe public static byte[] GetBytesLE (double value) - { - return GetBytesSwap (!BitConverter.IsLittleEndian, (byte *) &value, 8); - } - - unsafe public static byte[] GetBytesBE (bool value) - { - return GetBytesSwap (BitConverter.IsLittleEndian, (byte *) &value, 1); - } - - unsafe public static byte[] GetBytesBE (char value) - { - return GetBytesSwap (BitConverter.IsLittleEndian, (byte *) &value, 2); - } - - unsafe public static byte[] GetBytesBE (short value) - { - return GetBytesSwap (BitConverter.IsLittleEndian, (byte *) &value, 2); - } - - unsafe public static byte[] GetBytesBE (int value) - { - return GetBytesSwap (BitConverter.IsLittleEndian, (byte *) &value, 4); - } - - unsafe public static byte[] GetBytesBE (long value) - { - return GetBytesSwap (BitConverter.IsLittleEndian, (byte *) &value, 8); - } - - [CLSCompliant (false)] - unsafe public static byte[] GetBytesBE (ushort value) - { - return GetBytesSwap (BitConverter.IsLittleEndian, (byte *) &value, 2); - } - - [CLSCompliant (false)] - unsafe public static byte[] GetBytesBE (uint value) - { - return GetBytesSwap (BitConverter.IsLittleEndian, (byte *) &value, 4); - } - - [CLSCompliant (false)] - unsafe public static byte[] GetBytesBE (ulong value) - { - return GetBytesSwap (BitConverter.IsLittleEndian, (byte *) &value, 8); - } - - unsafe public static byte[] GetBytesBE (float value) - { - return GetBytesSwap (BitConverter.IsLittleEndian, (byte *) &value, 4); - } - - unsafe public static byte[] GetBytesBE (double value) - { - return GetBytesSwap (BitConverter.IsLittleEndian, (byte *) &value, 8); - } -#endif - - } -} diff --git a/mcs/tools/ildasm/Makefile b/mcs/tools/ildasm/Makefile deleted file mode 100644 index 15b8dfb2ba2..00000000000 --- a/mcs/tools/ildasm/Makefile +++ /dev/null @@ -1,8 +0,0 @@ -thisdir = tools/ildasm -SUBDIRS = -include ../../build/rules.make - -LOCAL_MCS_FLAGS = -r:Mono.Cecil.dll -D:MONO_DATACONVERTER_STATIC_METHODS -unsafe -PROGRAM = $(topdir)/class/lib/$(PROFILE)/ildasm.exe - -include ../../build/executable.make diff --git a/mcs/tools/ildasm/ildasm.cs b/mcs/tools/ildasm/ildasm.cs deleted file mode 100644 index b19eea4f41b..00000000000 --- a/mcs/tools/ildasm/ildasm.cs +++ /dev/null @@ -1,1029 +0,0 @@ -using System; -using System.IO; -using System.Text; -using System.Collections.Generic; -using System.Linq; -using Mono.Cecil; -using Mono.Cecil.Cil; -using Mono; - -public partial class ILDAsm -{ - public static int Main (String[] args) { - if (args.Length != 1) { - Console.Error.WriteLine ("Usage: ildasm "); - return 1; - } - - var inst = new ILDAsm (); - return inst.Run (args); - } - - public AssemblyDefinition ad; - public ModuleDefinition main; - public TextWriter os; - public int indent; - public List fields_with_rva = new List (); - - public void WriteLine () { - os.WriteLine (); - } - - public void WriteLine (String s) { - for (int i = 0; i < indent; ++i) - os.Write (" "); - os.WriteLine (s); - } - - public int Run (String[] args) { - ad = AssemblyDefinition.ReadAssembly (args [0]); - - main = ad.MainModule; - - os = Console.Out; - - // Emit assembly references - EmitAssemblyReferences (); - EmitAssembly (); - EmitModule (); - foreach (var typedef in main.Types) { - // FIXME: - if (typedef.Name == "") - EmitGlobals (typedef); - else - EmitType (typedef); - } - EmitData (); - - return 0; - } - - string EscapeName (string s) { - bool escape = false; - - if (s.Contains ("/")) { - string[] parts = s.Split ('/'); - var sb = new StringBuilder (); - for (int i = 0; i < parts.Length; ++i) { - if (i > 0) - sb.Append ("/"); - sb.Append (EscapeName (parts [i])); - } - return sb.ToString (); - } - - foreach (char c in s) { - if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || (c == '_') || (c == '.') || (c == '`'))) - escape = true; - } - if (!escape && keyword_table.ContainsKey (s)) - escape = true; - if (escape) - return "'" + s + "'"; - else - return s; - } - - string EscapeString (string s) { - return s.Replace ("\\", "\\\\").Replace ("\"", "\\\""); - } - - void EmitScope (IMetadataScope s, StringBuilder sb) { - if (s is AssemblyNameReference) { - AssemblyNameReference aname = (s as AssemblyNameReference); - sb.Append ("[" + EscapeName (aname.Name) + "]"); - } else if (s is ModuleDefinition) { - if (s != main) - throw new NotImplementedException (); - } else { - throw new NotImplementedException (s.GetType ().ToString ()); - } - } - - string StringifyTypeRef (TypeReference t) { - switch (t.MetadataType) { - case MetadataType.Void: - return "void"; - case MetadataType.Boolean: - return "bool"; - case MetadataType.Char: - return "char"; - case MetadataType.SByte: - return "int8"; - case MetadataType.Byte: - return "unsigned int8"; - case MetadataType.Int16: - return "int16"; - case MetadataType.UInt16: - return "unsigned int16"; - case MetadataType.Int32: - return "int32"; - case MetadataType.UInt32: - return "unsigned int32"; - case MetadataType.Int64: - return "int64"; - case MetadataType.UInt64: - return "unsigned int64"; - case MetadataType.Single: - return "float32"; - case MetadataType.Double: - return "float64"; - case MetadataType.String: - return "string"; - case MetadataType.IntPtr: - return "native int"; - case MetadataType.UIntPtr: - //return "unsigned native int"; - return "[mscorlib]System.UIntPtr"; - case MetadataType.TypedByReference: - return "typedref"; - case MetadataType.Class: - case MetadataType.Object: - case MetadataType.ValueType: { - var sb = new StringBuilder (); - IMetadataScope s = t.Scope; - if (t.MetadataType == MetadataType.ValueType) - sb.Append ("valuetype "); - else - sb.Append ("class "); - EmitScope (s, sb); - sb.Append (EscapeName (t.FullName)); - return sb.ToString (); - } - case MetadataType.Array: { - ArrayType at = (t as ArrayType); - - if (at.IsVector) - return StringifyTypeRef (at.ElementType) + "[]"; - - var suffix = new StringBuilder (); - suffix.Append ("["); - for (int i = 0; i < at.Dimensions.Count; i++) { - if (i > 0) - suffix.Append (","); - - suffix.Append (at.Dimensions [i].ToString ()); - } - suffix.Append ("]"); - - return StringifyTypeRef (at.ElementType) + suffix; - } - case MetadataType.Pointer: - return StringifyTypeRef ((t as TypeSpecification).ElementType) + "*"; - case MetadataType.ByReference: - return StringifyTypeRef ((t as TypeSpecification).ElementType) + "&"; - case MetadataType.Pinned: - return StringifyTypeRef ((t as TypeSpecification).ElementType) + " pinned"; - case MetadataType.GenericInstance: { - var sb = new StringBuilder (); - var inst = (t as GenericInstanceType); - sb.Append (StringifyTypeRef (inst.ElementType)); - sb.Append ("<"); - int aindex = 0; - foreach (TypeReference arg in inst.GenericArguments) { - if (aindex > 0) - sb.Append (", "); - sb.Append (StringifyTypeRef (arg)); - aindex ++; - } - sb.Append (">"); - return sb.ToString (); - } - case MetadataType.Var: - return "!" + (t as GenericParameter).Position; - case MetadataType.MVar: - return "!!" + (t as GenericParameter).Position; - case MetadataType.Sentinel: - return "..., " + StringifyTypeRef ((t as SentinelType).ElementType); - case MetadataType.RequiredModifier: { - var mod = (t as RequiredModifierType); - if (mod.ModifierType.MetadataType != MetadataType.Class) - throw new NotImplementedException (); - var sb = new StringBuilder (); - sb.Append (StringifyTypeRef (mod.ElementType)); - sb.Append (" modreq ("); - EmitScope (mod.ModifierType.Scope, sb); - sb.Append (EscapeName (mod.ModifierType.FullName)); - sb.Append (")"); - return sb.ToString (); - } - default: - throw new NotImplementedException ("" + t.MetadataType + " " + t.ToString ()); - } - } - - // Same as StringifyTypeRef, but emit primitive types as [mscorlib]... - string StringifyTypeRefNoPrim (TypeReference t) { - switch (t.MetadataType) { - case MetadataType.Void: - case MetadataType.Boolean: - case MetadataType.Char: - case MetadataType.SByte: - case MetadataType.Byte: - case MetadataType.Int16: - case MetadataType.UInt16: - case MetadataType.Int32: - case MetadataType.UInt32: - case MetadataType.Int64: - case MetadataType.UInt64: - case MetadataType.Single: - case MetadataType.Double: - case MetadataType.String: - case MetadataType.IntPtr: - case MetadataType.UIntPtr: - case MetadataType.TypedByReference: - return "[mscorlib]" + t.FullName; - default: - return StringifyTypeRef (t); - } - } - - string StringifyMethodRef (MethodReference method) { - var sb = new StringBuilder (); - if (method.CallingConvention == MethodCallingConvention.VarArg) - sb.Append ("vararg "); - if (method.HasThis) - sb.Append ("instance "); - sb.Append (StringifyTypeRef (method.ReturnType)); - sb.Append (' '); - sb.Append (StringifyTypeRefNoPrim (method.DeclaringType)); - sb.Append ("::"); - sb.Append (EscapeName (method.Name)); - if (method is GenericInstanceMethod) { - sb.Append ("<"); - int idx = 0; - foreach (var gpar in (method as GenericInstanceMethod).GenericArguments) { - if (idx > 0) - sb.Append (", "); - sb.Append (StringifyTypeRef (gpar)); - idx ++; - } - sb.Append (">"); - } - sb.Append ('('); - int par_index = 0; - foreach (ParameterReference par in method.Parameters) { - if (par_index > 0) - sb.Append (", "); - sb.Append (StringifyTypeRef (par.ParameterType)); - par_index ++; - } - sb.Append (")"); - return sb.ToString (); - } - - string StringifyFieldRef (FieldReference field) { - var sb = new StringBuilder (); - sb.Append (StringifyTypeRef (field.FieldType)); - sb.Append (' '); - sb.Append (StringifyTypeRefNoPrim (field.DeclaringType)); - sb.Append ("::"); - sb.Append (EscapeName (field.Name)); - return sb.ToString (); - } - - void WriteBlob (byte[] blob) { - int idx = 0; - while (idx < blob.Length) { - int len = idx + 16 < blob.Length ? 16 : blob.Length - idx; - var sb = new StringBuilder (); - var sb2 = new StringBuilder (); - for (int i = idx; i < idx + len; ++i) { - sb.Append (String.Format ("{0:X2} ", blob [i])); - if (Char.IsLetterOrDigit ((char)blob [i])) - sb2.Append ((char)blob [i]); - else - sb2.Append ('.'); - } - for (int i = 0; i < 16 - len; ++i) - sb.Append (" "); - if (len < 16 || idx + 16 == blob.Length) - sb.Append (')'); - else - sb.Append (' '); - WriteLine (String.Format ("{0} // {1}", sb.ToString (), sb2.ToString ())); - idx += 16; - } - } - - string Map (Dictionary map, uint val) { - string s; - - if (map.TryGetValue (val, out s)) - return s; - else - throw new NotImplementedException ("Value '" + val + "' not supported."); - } - - string MapFlags (Dictionary map, uint val) { - var sb = new StringBuilder (); - foreach (var flag in map.Keys) - if ((val & flag) != 0) - sb.Append (map [flag]); - return sb.ToString (); - } - - void EmitAssemblyReferences () { - foreach (var aname in main.AssemblyReferences) { - os.WriteLine (".assembly extern " + EscapeName (aname.Name)); - os.WriteLine ("{"); - indent ++; - Version v = aname.Version; - WriteLine (String.Format (".ver {0}:{1}:{2}:{3}", v.Major, v.Minor, v.Build, v.Revision)); - byte [] token = aname.PublicKeyToken; - if (token.Length > 0) { - StringBuilder sb = new StringBuilder (); - StringBuilder sb2 = new StringBuilder (); - for (int i = 0; i < token.Length; ++i) { - if (i > 0) - sb.Append (" "); - sb.Append (String.Format ("{0:X2}", token [i])); - if (Char.IsLetterOrDigit ((char)token [i])) - sb2.Append ((char)token [i]); - else - sb2.Append ('.'); - } - WriteLine (String.Format (".publickeytoken = ({0}) // {1}", sb, sb2)); - } - indent --; - WriteLine ("}"); - } - } - - void EmitCattrs (ICustomAttributeProvider prov) { - foreach (var cattr in prov.CustomAttributes) { - WriteLine (String.Format (".custom {0} = (", StringifyMethodRef (cattr.Constructor))); - indent += 3; - byte[] blob = cattr.GetBlob (); - WriteBlob (blob); - indent -= 3; - } - } - - void EmitSecDeclarations (ISecurityDeclarationProvider prov) { - foreach (var sec in prov.SecurityDeclarations) { - string act_str = null; - if (!sec_action_to_string.TryGetValue (sec.Action, out act_str)) - throw new NotImplementedException (sec.Action.ToString ()); - WriteLine (".permissionset " + act_str + " = ("); - WriteBlob (sec.GetBlob ()); - } - } - - void EmitAssembly () { - AssemblyNameDefinition aname = ad.Name; - - WriteLine (".assembly " + EscapeName (aname.Name)); - WriteLine ("{"); - indent ++; - EmitCattrs (ad); - EmitSecDeclarations (ad); - WriteLine (String.Format (".hash algorithm 0x{0:X8}", (int)aname.HashAlgorithm)); - Version v = aname.Version; - WriteLine (String.Format (".ver {0}:{1}:{2}:{3}", v.Major, v.Minor, v.Build, v.Revision)); - byte[] token = aname.PublicKey; - if (token != null && token.Length > 0) { - StringBuilder sb = new StringBuilder (); - StringBuilder sb2 = new StringBuilder (); - for (int i = 0; i < token.Length; ++i) { - if (i > 0) - sb.Append (" "); - sb.Append (String.Format ("{0:X2}", token [i])); - if (Char.IsLetterOrDigit ((char)token [i])) - sb2.Append ((char)token [i]); - else - sb2.Append ('.'); - } - WriteLine (String.Format (".publickey = ({0}) // {1}", sb, sb2)); - } - indent --; - WriteLine ("}"); - } - - void EmitModule () { - WriteLine (".module " + EscapeName (main.Name) + " // GUID = " + "{" + main.Mvid.ToString ().ToUpper () + "}"); - EmitCattrs (main); - } - - string StringifyTypeAttrs (TypeAttributes attrs) { - var sb = new StringBuilder (); - - sb.Append (Map (type_sem_map, (uint)(attrs & TypeAttributes.ClassSemanticMask))); - sb.Append (Map (type_access_map, (uint)(attrs & TypeAttributes.VisibilityMask))); - sb.Append (Map (type_layout_map, (uint)(attrs & TypeAttributes.LayoutMask))); - sb.Append (Map (type_string_format_map, (uint)(attrs & TypeAttributes.StringFormatMask))); - sb.Append (MapFlags (type_flag_map, (uint)(attrs))); - - return sb.ToString (); - } - - void EmitGenParams (IGenericParameterProvider prov, StringBuilder sb) { - sb.Append ("<"); - int idx = 0; - foreach (var gpar in prov.GenericParameters) { - if (idx > 0) - sb.Append (", "); - if (gpar.HasDefaultConstructorConstraint) - sb.Append (".ctor "); - if (gpar.HasNotNullableValueTypeConstraint) - sb.Append ("valuetype "); - if (gpar.HasReferenceTypeConstraint) - sb.Append ("class "); - if (gpar.HasConstraints) { - int idx2 = 0; - sb.Append ("("); - foreach (var c in gpar.Constraints) { - if (idx2 > 0) - sb.Append (", "); - sb.Append (StringifyTypeRef (c)); - idx2 ++; - } - sb.Append (")"); - } - sb.Append (EscapeName (gpar.Name)); - idx ++; - } - sb.Append (">"); - } - - void EmitGenParamCattrs (IGenericParameterProvider prov) { - foreach (var gpar in prov.GenericParameters) { - if (gpar.HasCustomAttributes) { - WriteLine (".param type " + gpar.Name); - EmitCattrs (gpar); - } - } - } - - void EmitGlobals (TypeDefinition td) { - foreach (var md in td.Methods) - EmitMethod (md); - } - - void EmitType (TypeDefinition td) { - WriteLine (""); - - if (td.MetadataType != MetadataType.Class && td.MetadataType != MetadataType.ValueType) - throw new NotImplementedException (td.MetadataType.ToString ()); - - // FIXME: Group types by namespaces - if (!td.IsNested && td.Namespace != null && td.Namespace != String.Empty) { - WriteLine (".namespace " + EscapeName (td.Namespace)); - WriteLine ("{"); - indent ++; - } - - var sb = new StringBuilder (); - sb.Append (".class "); - sb.Append (StringifyTypeAttrs (td.Attributes)); - sb.Append (EscapeName (td.Name)); - if (td.HasGenericParameters) - EmitGenParams (td, sb); - WriteLine (sb.ToString ()); - indent ++; - if (td.BaseType != null) - WriteLine ("extends " + StringifyTypeRef (td.BaseType)); - if (td.HasInterfaces) { - int idx = 0; - sb = new StringBuilder (); - foreach (TypeReference t in td.Interfaces) { - if (idx > 0) - sb.Append (", "); - sb.Append (StringifyTypeRef (t)); - idx ++; - } - WriteLine (String.Format ("implements {0}", sb.ToString ())); - } - indent --; - WriteLine ("{"); - indent ++; - if (td.PackingSize != -1) - WriteLine (".pack " + td.PackingSize); - if (td.ClassSize != -1) - WriteLine (".size " + td.ClassSize); - EmitCattrs (td); - EmitGenParamCattrs (td); - EmitSecDeclarations (td); - foreach (var fd in td.Fields) - EmitField (fd); - foreach (var md in td.Methods) - EmitMethod (md); - foreach (var p in td.Properties) - EmitProperty (p); - foreach (var e in td.Events) - EmitEvent (e); - foreach (var t in td.NestedTypes) - EmitType (t); - indent --; - WriteLine ("}"); - - if (!td.IsNested && td.Namespace != null && td.Namespace != String.Empty) { - WriteLine ("}"); - indent --; - } - } - - string StringifyFieldAttributes (FieldAttributes attrs) { - var sb = new StringBuilder (); - sb.Append (Map (field_access_map, (uint)(attrs & FieldAttributes.FieldAccessMask))); - sb.Append (MapFlags (field_flag_map, (uint)(attrs))); - return sb.ToString (); - } - - void EmitField (FieldDefinition fd) { - var sb = new StringBuilder (); - sb.Append (".field "); - if (fd.Offset != -1) - sb.Append ("[" + fd.Offset + "] "); - sb.Append (StringifyFieldAttributes (fd.Attributes)); - if (fd.HasMarshalInfo) { - sb.Append (" "); - sb.Append (StringifyMarshalInfo (fd.MarshalInfo)); - } - sb.Append (" "); - sb.Append (StringifyTypeRef (fd.FieldType)); - sb.Append (" "); - sb.Append (EscapeName (fd.Name)); - if (fd.HasConstant) - EmitConstant (fd.Constant, sb); - if (fd.RVA != 0) { - sb.Append (String.Format (" at D_{0:x8}", fd.RVA)); - fields_with_rva.Add (fd); - } - WriteLine (sb.ToString ()); - EmitCattrs (fd); - } - - string StringifyMethodAttributes (MethodAttributes attrs) { - var sb = new StringBuilder (); - sb.Append (Map (method_access_map, (uint)(attrs & MethodAttributes.MemberAccessMask))); - sb.Append (MapFlags (method_flag_map, (uint)(attrs))); - return sb.ToString (); - } - - string StringifyMethodImplAttributes (MethodImplAttributes attrs) { - var sb = new StringBuilder (); - sb.Append (Map (method_impl_map, (uint)(attrs & MethodImplAttributes.CodeTypeMask))); - sb.Append (Map (method_managed_map, (uint)(attrs & MethodImplAttributes.ManagedMask))); - sb.Append (MapFlags (method_impl_flag_map, (uint)(attrs))); - - return sb.ToString (); - } - - string StringifyTypeNameReflection (TypeReference t) { - if (t.MetadataType != MetadataType.Class) - throw new NotImplementedException (); - IMetadataScope s = t.Scope; - if (!(s is ModuleDefinition)) - throw new NotImplementedException (); - return t.FullName.Replace ("/", "+"); - } - - string StringifyMarshalInfo (MarshalInfo mi) { - var sb = new StringBuilder (); - - sb.Append ("marshal ("); - - string s = null; - switch (mi.NativeType) { - case NativeType.Array: { - var ami = (mi as ArrayMarshalInfo); - if (native_type_to_str.TryGetValue (ami.ElementType, out s)) { - sb.Append (s); - } - sb.Append ("["); - //Console.WriteLine ("// XXX: " + ami.Size + " " + ami.SizeParameterIndex + " " + ami.SizeParameterMultiplier); - - /* - * Comments in metadata.c: - * So if (param_num == 0) && (num_elem > 0), then - * elem_mult == 0 -> the array size is num_elem - * elem_mult == 1 -> the array size is @param_num + num_elem - */ - if (ami.Size != -1 && ami.Size != 0) - sb.Append (ami.Size.ToString ()); - if (ami.SizeParameterMultiplier != 0 && ami.SizeParameterIndex != -1) - sb.Append ("+" + ami.SizeParameterIndex.ToString ()); - sb.Append ("]"); - break; - } - case NativeType.FixedArray: { - var ami = (mi as FixedArrayMarshalInfo); - /* - if (native_type_to_str.TryGetValue (ami.ElementType, out s)) { - sb.Append (s); - } - */ - sb.Append ("fixed array [" + ami.Size + "]"); - break; - } - case NativeType.FixedSysString: { - var ami = (mi as FixedSysStringMarshalInfo); - sb.Append ("fixed sysstring [" + ami.Size + "]"); - break; - } - case NativeType.SafeArray: { - var sami = (mi as SafeArrayMarshalInfo); - sb.Append ("safearray "); - switch (sami.ElementType) { - case VariantType.Variant: - sb.Append ("variant"); - break; - default: - throw new NotImplementedException (); - } - break; - } - case NativeType.CustomMarshaler: { - var cmi = (mi as CustomMarshalInfo); - - if (cmi.Guid != Guid.Empty || cmi.UnmanagedType != String.Empty) - throw new NotImplementedException (); - sb.Append ("custom (\"" + StringifyTypeNameReflection (cmi.ManagedType) + "\", \"" + EscapeString (cmi.Cookie) + "\")"); - break; - } - default: - if (native_type_to_str.TryGetValue (mi.NativeType, out s)) - sb.Append (s); - else - throw new NotImplementedException (mi.NativeType.ToString ()); - break; - } - sb.Append (")"); - return sb.ToString (); - } - - string StringifySignature (MethodDefinition md) { - var sb = new StringBuilder (); - int pindex = 0; - foreach (var par in md.Parameters) { - if (pindex > 0) - sb.Append (", "); - if (par.IsIn) - sb.Append ("[in] "); - if (par.IsOut) - sb.Append ("[out] "); - if (par.IsOptional) - sb.Append ("[opt] "); - sb.Append (StringifyTypeRef (par.ParameterType)); - if (par.HasMarshalInfo) { - sb.Append (" "); - sb.Append (StringifyMarshalInfo (par.MarshalInfo)); - } - sb.Append (" "); - sb.Append (EscapeName (par.Name)); - pindex ++; - } - - return sb.ToString (); - } - - string StringifyPInvokeAttrs (PInvokeAttributes attrs) { - var sb = new StringBuilder (); - sb.Append (Map (pinvoke_char_set_map, (uint)(attrs & PInvokeAttributes.CharSetMask))); - sb.Append (Map (pinvoke_cconv_map, (uint)(attrs & PInvokeAttributes.CallConvMask))); - sb.Append (MapFlags (pinvoke_flags_map, (uint)(attrs))); - return sb.ToString (); - } - - string StringifyCallingConvention (MethodCallingConvention cconv) { - switch (cconv) { - case MethodCallingConvention.Default: - return "default"; - case MethodCallingConvention.VarArg: - return "vararg"; - default: - throw new NotImplementedException (cconv.ToString ()); - } - } - - void EmitMethod (MethodDefinition md) { - int idx; - - WriteLine (); - var pinvoke_sb = new StringBuilder (); - if ((uint)(md.Attributes & MethodAttributes.PInvokeImpl) != 0) { - var pinvoke = md.PInvokeInfo; - pinvoke_sb.Append ("pinvokeimpl (\"" + pinvoke.Module.Name + "\" as \"" + pinvoke.EntryPoint + "\" " + StringifyPInvokeAttrs (pinvoke.Attributes) + " )"); - } - WriteLine (String.Format (".method {0}{1}", StringifyMethodAttributes (md.Attributes), pinvoke_sb)); - - var sb = new StringBuilder (); - sb.Append (" "); - if (!md.IsStatic) - sb.Append ("instance "); - // CallingConvention seems to be 32 - sb.Append (StringifyCallingConvention ((MethodCallingConvention)((uint)md.CallingConvention & 0xf))); - sb.Append (" "); - sb.Append (StringifyTypeRef (md.ReturnType)); - sb.Append (" "); - if (md.MethodReturnType.HasMarshalInfo) { - sb.Append (StringifyMarshalInfo (md.MethodReturnType.MarshalInfo)); - sb.Append (" "); - } - sb.Append (EscapeName (md.Name)); - if (md.HasGenericParameters) - EmitGenParams (md, sb); - WriteLine (String.Format ("{0} ({1}) {2}", sb, StringifySignature (md), StringifyMethodImplAttributes (md.ImplAttributes))); - WriteLine ("{"); - indent ++; - foreach (var ov in md.Overrides) - WriteLine (".override method " + StringifyMethodRef (ov)); - EmitCattrs (md); - EmitGenParamCattrs (md); - EmitSecDeclarations (md); - idx = 0; - foreach (var par in md.Parameters) { - if (par.HasCustomAttributes) { - WriteLine (".param [" + (idx + 1) + "]"); - EmitCattrs (par); - } - if (par.HasConstant) { - sb = new StringBuilder (); - EmitConstant (par.Constant, sb); - WriteLine (".param [" + (idx + 1) + "]" + sb); - } - idx ++; - } - // FIXME: Print RVA, code size - if (md == main.EntryPoint) - WriteLine (".entrypoint"); - if (md.HasBody) { - MethodBody body = md.Body; - WriteLine (".maxstack " + body.MaxStackSize); - if (body.InitLocals) - WriteLine (".locals init ("); - else - WriteLine (".locals ("); - indent ++; - int vindex = 0; - foreach (var v in body.Variables) { - WriteLine (StringifyTypeRef (v.VariableType) + " " + (v.Name == "" ? "V_" + v.Index : v.Name) + (vindex + 1 < body.Variables.Count ? ", " : "")); - vindex ++; - } - indent --; - WriteLine (")"); - - List handlers = body.ExceptionHandlers.ToList (); - List reverse_handlers = body.ExceptionHandlers.Reverse ().ToList (); - - var trys = new Dictionary (); - if (handlers.Count > 0) - trys [handlers [0]] = true; - for (int i = 1; i < handlers.Count; ++i) { - trys [handlers [i]] = true; - for (int j = 0; j < i; ++j) { - if (handlers [i].TryStart == handlers [j].TryStart && handlers [i].TryEnd == handlers [j].TryEnd) { - trys [handlers [i]] = false; - break; - } - } - } - - idx = 0; - var handler_to_idx = new Dictionary (); - foreach (ExceptionHandler eh in body.ExceptionHandlers) { - handler_to_idx [eh] = idx; - idx ++; - } - - foreach (var ins in body.Instructions) { - foreach (var eh in handlers) { - if (eh.TryEnd == ins && trys [eh]) { - indent --; - WriteLine ("} // end try " + handler_to_idx [eh]); - } - - if (eh.HandlerEnd == ins) { - indent --; - WriteLine ("} // end handler " + handler_to_idx [eh]); - } - } - - foreach (var eh in reverse_handlers) { - if (eh.TryStart == ins && trys [eh]) { - WriteLine (".try { // " + handler_to_idx [eh]); - indent ++; - } - if (eh.HandlerStart == ins) { - string type_str = null; - switch (eh.HandlerType) { - case ExceptionHandlerType.Catch: - type_str = "catch"; - break; - case ExceptionHandlerType.Finally: - type_str = "finally"; - break; - default: - throw new NotImplementedException (eh.HandlerType.ToString ()); - } - if (eh.CatchType == null) - WriteLine (type_str + " { // " + handler_to_idx [eh]); - else - WriteLine (type_str + " " + StringifyTypeRef (eh.CatchType) + " { // " + handler_to_idx [eh]); - indent ++; - } - } - - WriteLine (StringifyIns (ins)); - } - } - indent --; - WriteLine ("}"); - } - - // Based on Instruction:ToString () - public string StringifyIns (Instruction ins) { - var sb = new StringBuilder (); - - AppendLabel (sb, ins); - sb.Append (':'); - sb.Append (" "); - sb.Append (ins.OpCode.Name); - - if (ins.Operand == null) - return sb.ToString (); - - sb.Append (' '); - - object operand = ins.Operand; - switch (ins.OpCode.OperandType) { - case OperandType.ShortInlineBrTarget: - case OperandType.InlineBrTarget: - AppendLabel (sb, (Instruction) operand); - break; - case OperandType.InlineSwitch: - var labels = (Instruction []) operand; - sb.Append ("("); - for (int i = 0; i < labels.Length; i++) { - if (i > 0) - sb.Append (','); - - AppendLabel (sb, labels [i]); - } - sb.Append (")"); - break; - case OperandType.InlineString: - sb.Append ('\"'); - sb.Append (EscapeString ((string)operand)); - sb.Append ('\"'); - break; - default: - if (operand is MethodReference) { - if (ins.OpCode == OpCodes.Ldtoken) - sb.Append ("method "); - sb.Append (StringifyMethodRef ((MethodReference)operand)); - } else if (operand is TypeReference) - sb.Append (StringifyTypeRef ((TypeReference)operand)); - else if (operand is VariableDefinition) - sb.Append (operand.ToString ()); - else if (operand is FieldReference) { - if (ins.OpCode == OpCodes.Ldtoken) - sb.Append ("field "); - sb.Append (StringifyFieldRef ((FieldReference)operand)); - } else if (operand is ParameterDefinition) { - var pd = (operand as ParameterDefinition); - sb.Append (pd.Index + (pd.Method.HasThis ? 1 : 0)); - } - else { - EmitConstantOperand (operand, sb); - } - break; - } - - return sb.ToString (); - } - - static void AppendLabel (StringBuilder builder, Instruction instruction) { - builder.Append ("IL_"); - builder.Append (instruction.Offset.ToString ("x4")); - } - - void EmitProperty (PropertyDefinition p) { - // FIXME: attributes - - var sb = new StringBuilder (); - sb.Append (".property "); - if (p.HasThis) - sb.Append ("instance "); - sb.Append (StringifyTypeRef (p.PropertyType)); - sb.Append (" "); - sb.Append (EscapeName (p.Name)); - sb.Append ("("); - int idx = 0; - foreach (var par in p.Parameters) { - if (idx > 0) - sb.Append (", "); - sb.Append (StringifyTypeRef (par.ParameterType)); - idx ++; - } - sb.Append (")"); - WriteLine (sb.ToString ()); - WriteLine ("{"); - indent ++; - EmitCattrs (p); - if (p.SetMethod != null) - WriteLine (".set " + StringifyMethodRef (p.SetMethod)); - if (p.GetMethod != null) - WriteLine (".get " + StringifyMethodRef (p.GetMethod)); - if (p.HasOtherMethods) - throw new NotImplementedException (); - indent --; - WriteLine ("}"); - } - - void EmitEvent (EventDefinition e) { - WriteLine (".event " + StringifyTypeRef (e.EventType) + " " + EscapeName (e.Name)); - WriteLine ("{"); - indent ++; - if (e.AddMethod != null) - WriteLine (".addon " + StringifyMethodRef (e.AddMethod)); - if (e.RemoveMethod != null) - WriteLine (".removeon " + StringifyMethodRef (e.RemoveMethod)); - foreach (var m in e.OtherMethods) - WriteLine (".other " + StringifyMethodRef (m)); - indent --; - WriteLine ("}"); - } - - void EmitData () { - foreach (var fd in fields_with_rva) { - WriteLine (String.Format (".data D_{0:x8} = bytearray (", fd.RVA)); - WriteBlob (fd.InitialValue); - } - } - - void EmitConstantOperand (object operand, StringBuilder sb) { - if (operand is double) { - double d = (double)operand; - // FIXME: - //if (Double.IsNaN (d) || Double.IsInfinity (d)) { - { - byte[] b = DataConverter.GetBytesLE (d); - sb.Append ("("); - int index = 0; - for (int i = 0; i < b.Length; ++i) { - if (index > 0) - sb.Append (" "); - sb.Append (String.Format ("{0:x2}", b [i])); - index ++; - } - sb.Append (")"); - } - } else if (operand is float) { - float d = (float)operand; - // FIXME: - //if (Single.IsNaN (d) || Single.IsInfinity (d)) { - { - byte[] b = DataConverter.GetBytesLE (d); - sb.Append ("("); - int index = 0; - for (int i = 0; i < b.Length; ++i) { - if (index > 0) - sb.Append (" "); - sb.Append (String.Format ("{0:x2}", b [i])); - index ++; - } - sb.Append (")"); - } - } else if (operand.GetType ().Assembly == typeof (int).Assembly) - sb.Append (operand.ToString ()); - else - throw new NotImplementedException (operand.GetType ().ToString ()); - } - - void EmitConstant (object o, StringBuilder sb) { - if (o is byte) - sb.Append (String.Format (" = int8(0x{0:x2})", (byte)o)); - else if (o is sbyte) - sb.Append (String.Format (" = int8(0x{0:x2})", (sbyte)o)); - else if (o is short) - sb.Append (String.Format (" = int16(0x{0:x4})", (short)o)); - else if (o is ushort) - sb.Append (String.Format (" = int16(0x{0:x4})", (ushort)o)); - else if (o is int) - sb.Append (String.Format (" = int32(0x{0:x8})", (int)o)); - else if (o is uint) - sb.Append (String.Format (" = int32(0x{0:x8})", (uint)o)); - else if (o is long) - sb.Append (String.Format (" = int64(0x{0:x8})", (long)o)); - else if (o is ulong) - sb.Append (String.Format (" = int64(0x{0:x8})", (ulong)o)); - else if (o is string) - sb.Append (String.Format (" = \"{0}\"", EscapeString ((string)o))); - else if (o is bool) - sb.Append (String.Format (" = bool({0})", (bool)o ? "true" : " false")); - else if (o is char) - sb.Append (String.Format (" = char(0x{0:x4})", (int)(char)o)); - else if (o is double) - // FIXME: - sb.Append (String.Format (" = float64({0:f})", (double)o)); - else if (o is float) - // FIXME: - sb.Append (String.Format (" = float32({0:f})", (float)o)); - else if (o == null) - sb.Append ("= nullref"); - else - throw new NotImplementedException ("" + o.GetType ().ToString () + " " + o.ToString ()); - } -} diff --git a/mcs/tools/ildasm/ildasm.exe.sources b/mcs/tools/ildasm/ildasm.exe.sources deleted file mode 100644 index 937aaeab9fd..00000000000 --- a/mcs/tools/ildasm/ildasm.exe.sources +++ /dev/null @@ -1,5 +0,0 @@ -ildasm.cs -tables.cs -DataConverter.cs - - diff --git a/mcs/tools/ildasm/tables.cs b/mcs/tools/ildasm/tables.cs deleted file mode 100644 index dd8d8c6839c..00000000000 --- a/mcs/tools/ildasm/tables.cs +++ /dev/null @@ -1,606 +0,0 @@ -using System; -using System.IO; -using System.Text; -using System.Collections.Generic; -using System.Linq; -using Mono.Cecil; -using Mono.Cecil.Cil; -using Mono; - -/* - * This partial class contains the tables used by the disassembler. - */ -public partial class ILDAsm { - - Dictionary type_access_map = new Dictionary () { - { (uint)TypeAttributes.NotPublic, "private " }, - { (uint)TypeAttributes.Public, "public " }, - { (uint)TypeAttributes.NestedPublic, "nested public " }, - { (uint)TypeAttributes.NestedPrivate, "nested private " }, - { (uint)TypeAttributes.NestedFamily, "nested family " }, - { (uint)TypeAttributes.NestedAssembly, "nested assembly " }, - { (uint)TypeAttributes.NestedFamANDAssem, "nested famandassem " }, - { (uint)TypeAttributes.NestedFamORAssem, "nested famorassem " }, - }; - Dictionary type_layout_map = new Dictionary () { - { (uint)TypeAttributes.AutoLayout, "auto " }, - { (uint)TypeAttributes.SequentialLayout, "sequential " }, - { (uint)TypeAttributes.ExplicitLayout, "explicit " } - }; - Dictionary type_string_format_map = new Dictionary () { - { (uint)TypeAttributes.AnsiClass, "ansi " }, - { (uint)TypeAttributes.UnicodeClass, "unicode " }, - //{ (uint)TypeAttributes.AutoClass, "unicode " }, - }; - Dictionary type_flag_map = new Dictionary () { - { (uint)TypeAttributes.Abstract, "abstract " }, - { (uint)TypeAttributes.Sealed, "sealed " }, - { (uint)TypeAttributes.SpecialName, "specialname " }, - { (uint)TypeAttributes.Import, "import " }, - { (uint)TypeAttributes.Serializable, "serializable " }, - { (uint)TypeAttributes.BeforeFieldInit, "beforefieldinit " }, - { (uint)TypeAttributes.RTSpecialName, "rtspecialname " }, - //{ (uint)TypeAttributes.HasSecurity, "rtspecialname " }, - //{ (uint)TypeAttributes.Forwarder, "rtspecialname " }, - }; - - Dictionary type_sem_map = new Dictionary () { - { (uint)TypeAttributes.Class, "" }, - { (uint)TypeAttributes.Interface, "interface " }, - }; - - - Dictionary field_access_map = new Dictionary () { - { (uint)FieldAttributes.Private, "private " }, - { (uint)FieldAttributes.FamANDAssem, "famandassem " }, - { (uint)FieldAttributes.Assembly, "assembly " }, - { (uint)FieldAttributes.Family, "family " }, - { (uint)FieldAttributes.FamORAssem, "famorassem " }, - { (uint)FieldAttributes.Public, "public " }, - }; - - Dictionary field_flag_map = new Dictionary () { - { (uint)FieldAttributes.Static, "static " }, - { (uint)FieldAttributes.Literal, "literal " }, - { (uint)FieldAttributes.InitOnly, "initonly " }, - { (uint)FieldAttributes.SpecialName, "specialname " }, - { (uint)FieldAttributes.RTSpecialName, "rtspecialname " }, - { (uint)FieldAttributes.NotSerialized, "notserialized " }, - }; - - - Dictionary method_access_map = new Dictionary () { - { (uint)MethodAttributes.CompilerControlled, "privatescope " }, - { (uint)MethodAttributes.Private, "private " }, - { (uint)MethodAttributes.Public, "public " }, - { (uint)MethodAttributes.Family, "family " }, - { (uint)MethodAttributes.FamORAssem, "famorassem " }, - { (uint)MethodAttributes.Assembly, "assembly " } - }; - - Dictionary method_flag_map = new Dictionary () { - { (uint)MethodAttributes.Static, "static " }, - { (uint)MethodAttributes.Final, "final " }, - { (uint)MethodAttributes.Virtual, "virtual " }, - { (uint)MethodAttributes.HideBySig, "hidebysig " }, - { (uint)MethodAttributes.NewSlot, "newslot " }, - { (uint)MethodAttributes.CheckAccessOnOverride, "strict " }, - { (uint)MethodAttributes.Abstract, "abstract " }, - { (uint)MethodAttributes.SpecialName, "specialname " }, - { (uint)MethodAttributes.RTSpecialName, "rtspecialname " }, - }; - - - Dictionary method_impl_map = new Dictionary () { - { (uint)MethodImplAttributes.IL, "cil " }, - { (uint)MethodImplAttributes.Runtime, "runtime " } - }; - Dictionary method_managed_map = new Dictionary () { - { (uint)MethodImplAttributes.Managed, "managed " } - }; - Dictionary method_impl_flag_map = new Dictionary () { - { (uint)MethodImplAttributes.PreserveSig, "preservesig " }, - { (uint)MethodImplAttributes.InternalCall, "internalcall " }, - { (uint)MethodImplAttributes.Synchronized, "synchronized " }, - { (uint)MethodImplAttributes.NoInlining, "noinlining " }, - }; - - - Dictionary native_type_to_str = new Dictionary () { - { NativeType.Boolean, "bool" }, - { NativeType.I1, "int8" }, - { NativeType.U1, "unsigned int8" }, - { NativeType.I2, "int16" }, - { NativeType.U2, "unsigned int16" }, - { NativeType.I4, "int32" }, - { NativeType.U4, "unsigned int32" }, - { NativeType.I8, "int64" }, - { NativeType.U8, "unsigned int64" }, - //{ NativeType.R4, "int32" }, - //{ NativeType.R8, "unsigned int32" }, - { NativeType.LPStr, "lpstr" }, - { NativeType.Int, "int" }, - //{ NativeType.Int, "int" }, - { NativeType.Func, "method" }, - //{ NativeType.Array, "int" }, - //{ NativeType.Currency, "method" }, - { NativeType.BStr, "bstr " }, - { NativeType.LPWStr, "lpwstr" }, - //{ NativeType.LPTStr, "lpwstr" }, - //{ NativeType.FixedSysString, "lpwstr" }, - { NativeType.IUnknown, "iunknown" }, - { NativeType.IDispatch, "idispatch" }, - { NativeType.Struct, "struct" }, - { NativeType.IntF, "interface" }, - //{ NativeType.SafeArray, "interface" }, - //{ NativeType.FixedArray, "interface" }, - //{ NativeType.ByValStr, "interface" }, - //{ NativeType.ANSIBStr, "interface" }, - //{ NativeType.TBStr, "interface" }, - { NativeType.VariantBool, "variant bool" }, - { NativeType.ASAny, "as any" }, - { NativeType.LPStruct, "lpstruct" }, - //{ NativeType.CustomMarshaler, "lpstruct" }, - //{ NativeType.Error, "lpstruct" }, - }; - - - Dictionary pinvoke_char_set_map = new Dictionary () { - { (uint)PInvokeAttributes.CharSetNotSpec, "" }, - { (uint)PInvokeAttributes.CharSetAnsi, "ansi " }, - { (uint)PInvokeAttributes.CharSetUnicode, "unicode " }, - { (uint)PInvokeAttributes.CharSetAuto, "autochar " }, - }; - - Dictionary pinvoke_cconv_map = new Dictionary () { - { (uint)PInvokeAttributes.CallConvWinapi, "winapi " }, - { (uint)PInvokeAttributes.CallConvCdecl, "cdecl " }, - { (uint)PInvokeAttributes.CallConvStdCall, "stdcall " }, - { (uint)PInvokeAttributes.CallConvThiscall, "thiscall " }, - { (uint)PInvokeAttributes.CallConvFastcall, "fastcall " }, - }; - - Dictionary pinvoke_flags_map = new Dictionary () { - { (uint)PInvokeAttributes.SupportsLastError, "lasterr " }, - }; - - - Dictionary sec_action_to_string = new Dictionary () { - //{ SecurityAction.Request, "reqmin" }, - { SecurityAction.Demand, "demand" }, - { SecurityAction.Assert, "assert" }, - { SecurityAction.Deny, "deny" }, - { SecurityAction.PermitOnly, "permitonly" }, - { SecurityAction.LinkDemand, "linkcheck" }, - { SecurityAction.InheritDemand, "inheritcheck" }, - { SecurityAction.RequestMinimum, "reqmin" }, - { SecurityAction.RequestOptional, "reqopt" }, - { SecurityAction.RequestRefuse, "reqrefuse" }, - //{ SecurityAction.PreJitGrant, "reqmin" }, - //{ SecurityAction.PreJitDeny, "reqmin" }, - //{ SecurityAction.NonCasDemand, "reqmin" }, - //{ SecurityAction.NonCasLinkDemand, "reqmin" }, - //{ SecurityAction.NonCasInheritance, "reqmin" }, - }; - - - Dictionary keyword_table = new Dictionary { - {"9", true}, - {"abstract", true}, - {"add", true}, - {"add.ovf", true}, - {"add.ovf.un", true}, - {"algorithm", true}, - {"alignment", true}, - {"and", true}, - {"ansi", true}, - {"any", true}, - {"arglist", true}, - {"array", true}, - {"as", true}, - {"assembly", true}, - {"assert", true}, - {"at", true}, - {"autochar", true}, - {"auto", true}, - {"beforefieldinit", true}, - {"bestfit", true}, - {"beq", true}, - {"beq.s", true}, - {"bge", true}, - {"bge.s", true}, - {"bge.un", true}, - {"bge.un.s", true}, - {"bgt", true}, - {"bgt.s", true}, - {"bgt.un", true}, - {"bgt.un.s", true}, - {"ble", true}, - {"ble.s", true}, - {"ble.un", true}, - {"ble.un.s", true}, - {"blob", true}, - {"blob_object", true}, - {"blt", true}, - {"blt.s", true}, - {"blt.un", true}, - {"blt.un.s", true}, - {"bne.un", true}, - {"bne.un.s", true}, - {"bool", true}, - {"box", true}, - {"break", true}, - {"brfalse", true}, - {"brfalse.s", true}, - {"br", true}, - {"brinst", true}, - {"brinst.s", true}, - {"brnull", true}, - {"brnull.s", true}, - {"br.s", true}, - {"brtrue", true}, - {"brtrue.s", true}, - {"brzero", true}, - {"brzero.s", true}, - {"bstr", true}, - {"bytearray", true}, - {"byvalstr", true}, - {"call", true}, - {"callconv", true}, - {"calli", true}, - {"callmostderived", true}, - {"callvirt", true}, - {"carray", true}, - {"castclass", true}, - {"catch", true}, - {"cdecl", true}, - {"ceq", true}, - {"cf", true}, - {"cgt", true}, - {"cgt.un", true}, - {"char", true}, - {"charmaperror", true}, - {"cil", true}, - {"ckfinite", true}, - {"class", true}, - {"clsid", true}, - {"clt", true}, - {"clt.un", true}, - {"Compilercontrolled", true}, - {"const", true}, - {"conv.i1", true}, - {"conv.i2", true}, - {"conv.i4", true}, - {"conv.i8", true}, - {"conv.i", true}, - {"conv.ovf.i1", true}, - {"conv.ovf.i1.un", true}, - {"conv.ovf.i2", true}, - {"conv.ovf.i2.un", true}, - {"conv.ovf.i4", true}, - {"conv.ovf.i4.un", true}, - {"conv.ovf.i8", true}, - {"conv.ovf.i8.un", true}, - {"conv.ovf.i", true}, - {"conv.ovf.i.un", true}, - {"conv.ovf.u1", true}, - {"conv.ovf.u1.un", true}, - {"conv.ovf.u2", true}, - {"conv.ovf.u2.un", true}, - {"conv.ovf.u4", true}, - {"conv.ovf.u4.un", true}, - {"conv.ovf.u8", true}, - {"conv.ovf.u8.un", true}, - {"conv.ovf.u", true}, - {"conv.ovf.u.un", true}, - {"conv.r4", true}, - {"conv.r8", true}, - {"conv.r.un", true}, - {"conv.u1", true}, - {"conv.u2", true}, - {"conv.u4", true}, - {"conv.u8", true}, - {"conv.u", true}, - {"cpblk", true}, - {"cpobj", true}, - {"currency", true}, - {"custom", true}, - {"date", true}, - {"decimal", true}, - {"default", true}, - {"demand", true}, - {"deny", true}, - {"div", true}, - {"div.un", true}, - {"dup", true}, - {"endfault", true}, - {"endfilter", true}, - {"endfinally", true}, - {"endmac", true}, - {"enum", true}, - {"error", true}, - {"explicit", true}, - {"extends", true}, - {"extern", true}, - {"false", true}, - {"famandassem", true}, - {"family", true}, - {"famorassem", true}, - {"fastcall", true}, - {"fault", true}, - {"field", true}, - {"filetime", true}, - {"filter", true}, - {"final", true}, - {"finally", true}, - {"fixed", true}, - {"flags", true}, - {"float32", true}, - {"float64", true}, - {"float", true}, - {"forwardref", true}, - {"fromunmanaged", true}, - {"handler", true}, - {"hidebysig", true}, - {"hresult", true}, - {"idispatch", true}, - {"il", true}, - {"illegal", true}, - {"implements", true}, - {"implicitcom", true}, - {"implicitres", true}, - {"import", true}, - {"in", true}, - {"inheritcheck", true}, - {"initblk", true}, - {"init", true}, - {"initobj", true}, - {"initonly", true}, - {"instance", true}, - {"int16", true}, - {"int32", true}, - {"int64", true}, - {"int8", true}, - {"interface", true}, - {"internalcall", true}, - {"int", true}, - {"isinst", true}, - {"iunknown", true}, - {"jmp", true}, - {"lasterr", true}, - {"lcid", true}, - {"ldarg.0", true}, - {"ldarg.1", true}, - {"ldarg.2", true}, - {"ldarg.3", true}, - {"ldarga", true}, - {"ldarga.s", true}, - {"ldarg", true}, - {"ldarg.s", true}, - {"ldc.i4.0", true}, - {"ldc.i4.1", true}, - {"ldc.i4.2", true}, - {"ldc.i4.3", true}, - {"ldc.i4.4", true}, - {"ldc.i4.5", true}, - {"ldc.i4.6", true}, - {"ldc.i4.7", true}, - {"ldc.i4.8", true}, - {"ldc.i4", true}, - {"ldc.i4.m1", true}, - {"ldc.i4.M1", true}, - {"ldc.i4.s", true}, - {"ldc.i8", true}, - {"ldc.r4", true}, - {"ldc.r8", true}, - {"ldelem", true}, - {"ldelema", true}, - {"ldelem.i1", true}, - {"ldelem.i2", true}, - {"ldelem.i4", true}, - {"ldelem.i8", true}, - {"ldelem.i", true}, - {"ldelem.r4", true}, - {"ldelem.r8", true}, - {"ldelem.ref", true}, - {"ldelem.u1", true}, - {"ldelem.u2", true}, - {"ldelem.u4", true}, - {"ldelem.u8", true}, - {"ldflda", true}, - {"ldfld", true}, - {"ldftn", true}, - {"ldind.i1", true}, - {"ldind.i2", true}, - {"ldind.i4", true}, - {"ldind.i8", true}, - {"ldind.i", true}, - {"ldind.r4", true}, - {"ldind.r8", true}, - {"ldind.ref", true}, - {"ldind.u1", true}, - {"ldind.u2", true}, - {"ldind.u4", true}, - {"ldind.u8", true}, - {"ldlen", true}, - {"ldloc.0", true}, - {"ldloc.1", true}, - {"ldloc.2", true}, - {"ldloc.3", true}, - {"ldloca", true}, - {"ldloca.s", true}, - {"ldloc", true}, - {"ldloc.s", true}, - {"ldnull", true}, - {"ldobj", true}, - {"ldsflda", true}, - {"ldsfld", true}, - {"ldstr", true}, - {"ldtoken", true}, - {"ldvirtftn", true}, - {"leave", true}, - {"leave.s", true}, - {"legacy", true}, - {"linkcheck", true}, - {"literal", true}, - {"localloc", true}, - {"lpstr", true}, - {"lpstruct", true}, - {"lptstr", true}, - {"lpvoid", true}, - {"lpwstr", true}, - {"managed", true}, - {"marshal", true}, - {"method", true}, - {"mkrefany", true}, - {"modopt", true}, - {"modreq", true}, - {"mul", true}, - {"mul.ovf", true}, - {"mul.ovf.un", true}, - {"native", true}, - {"neg", true}, - {"nested", true}, - {"newarr", true}, - {"newobj", true}, - {"newslot", true}, - {"noappdomain", true}, - {"noinlining", true}, - {"nomachine", true}, - {"nomangle", true}, - {"nometadata", true}, - {"noncasdemand", true}, - {"noncasinheritance", true}, - {"noncaslinkdemand", true}, - {"nop", true}, - {"noprocess", true}, - {"not", true}, - {"not_in_gc_heap", true}, - {"notremotable", true}, - {"notserialized", true}, - {"null", true}, - {"nullref", true}, - {"object", true}, - {"objectref", true}, - {"off", true}, - {"on", true}, - {"opt", true}, - {"optil", true}, - {"or", true}, - {"out", true}, - {"permitonly", true}, - {"pinned", true}, - {"pinvokeimpl", true}, - {"pop", true}, - {"prefix1", true}, - {"prefix2", true}, - {"prefix3", true}, - {"prefix4", true}, - {"prefix5", true}, - {"prefix6", true}, - {"prefix7", true}, - {"prefixref", true}, - {"prejitdeny", true}, - {"prejitgrant", true}, - {"preservesig", true}, - {"private", true}, - {"privatescope", true}, - {"property", true}, - {"protected", true}, - {"public", true}, - {"readonly", true}, - {"record", true}, - {"refany", true}, - {"refanytype", true}, - {"refanyval", true}, - {"rem", true}, - {"rem.un", true}, - {"reqmin", true}, - {"reqopt", true}, - {"reqrefuse", true}, - {"reqsecobj", true}, - {"request", true}, - {"ret", true}, - {"rethrow", true}, - {"retval", true}, - {"rtspecialname", true}, - {"runtime", true}, - {"safearray", true}, - {"sealed", true}, - {"sequential", true}, - {"serializable", true}, - {"shl", true}, - {"shr", true}, - {"shr.un", true}, - {"sizeof", true}, - {"special", true}, - {"specialname", true}, - {"starg", true}, - {"starg.s", true}, - {"static", true}, - {"stdcall", true}, - {"stelem", true}, - {"stelem.i1", true}, - {"stelem.i2", true}, - {"stelem.i4", true}, - {"stelem.i8", true}, - {"stelem.i", true}, - {"stelem.r4", true}, - {"stelem.r8", true}, - {"stelem.ref", true}, - {"stfld", true}, - {"stind.i1", true}, - {"stind.i2", true}, - {"stind.i4", true}, - {"stind.i8", true}, - {"stind.i", true}, - {"stind.r4", true}, - {"stind.r8", true}, - {"stloc", true}, - {"stobj", true}, - {"storage", true}, - {"stored_object", true}, - {"streamed_object", true}, - {"stream", true}, - {"strict", true}, - {"string", true}, - {"struct", true}, - {"stsfld", true}, - {"sub", true}, - {"sub.ovf", true}, - {"sub.ovf.un", true}, - {"switch", true}, - {"synchronized", true}, - {"syschar", true}, - {"sysstring", true}, - {"tbstr", true}, - {"thiscall", true}, - {"tls", true}, - {"to", true}, - {"true", true}, - {"type", true}, - {"typedref", true}, - {"uint", true}, - {"uint8", true}, - {"uint16", true}, - {"uint32", true}, - {"uint64", true}, - {"unbox", true}, - {"unicode", true}, - {"unmanagedexp", true}, - {"unmanaged", true}, - {"unsigned", true}, - {"userdefined", true}, - {"value", true}, - {"valuetype", true}, - {"vararg", true}, - {"variant", true}, - {"vector", true}, - {"virtual", true}, - {"void", true}, - {"wchar", true}, - {"winapi", true}, - {"with", true}, - {"xor", true}, - }; -} \ No newline at end of file