2 Copyright (C) 2009 Jeroen Frijters
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
12 1. The origin of this software must not be misrepresented; you must not
13 claim that you wrote the original software. If you use this software
14 in a product, an acknowledgment in the product documentation would be
15 appreciated but is not required.
16 2. Altered source versions must be plainly marked as such, and must not be
17 misrepresented as being the original software.
18 3. This notice may not be removed or altered from any source distribution.
25 using System.Collections.Generic;
26 using IKVM.Reflection.Reader;
29 namespace IKVM.Reflection
31 public sealed class MethodBody
33 private readonly IList<ExceptionHandlingClause> exceptionClauses;
34 private readonly IList<LocalVariableInfo> locals;
35 private readonly bool initLocals;
36 private readonly int maxStack;
37 private readonly int localVarSigTok;
40 internal MethodBody(ModuleReader module, int rva, IGenericContext context)
42 const byte CorILMethod_TinyFormat = 0x02;
43 const byte CorILMethod_FatFormat = 0x03;
44 const byte CorILMethod_MoreSects = 0x08;
45 const byte CorILMethod_InitLocals = 0x10;
46 const byte CorILMethod_Sect_EHTable = 0x01;
47 const byte CorILMethod_Sect_FatFormat = 0x40;
48 const byte CorILMethod_Sect_MoreSects = 0x80;
50 List<ExceptionHandlingClause> exceptionClauses = new List<ExceptionHandlingClause>();
51 List<LocalVariableInfo> locals = new List<LocalVariableInfo>();
53 BinaryReader br = new BinaryReader(module.stream);
54 byte b = br.ReadByte();
55 if ((b & 3) == CorILMethod_TinyFormat)
58 body = br.ReadBytes(b >> 2);
60 else if ((b & 3) == CorILMethod_FatFormat)
62 initLocals = (b & CorILMethod_InitLocals) != 0;
63 short flagsAndSize = (short)(b | (br.ReadByte() << 8));
64 if ((flagsAndSize >> 12) != 3)
66 throw new BadImageFormatException("Fat format method header size should be 3");
68 maxStack = br.ReadUInt16();
69 int codeLength = br.ReadInt32();
70 localVarSigTok = br.ReadInt32();
71 body = br.ReadBytes(codeLength);
72 if ((b & CorILMethod_MoreSects) != 0)
74 module.stream.Position = (module.stream.Position + 3) & ~3;
75 int hdr = br.ReadInt32();
76 if ((hdr & CorILMethod_Sect_MoreSects) != 0 || (hdr & CorILMethod_Sect_EHTable) == 0)
78 throw new NotImplementedException();
80 else if ((hdr & CorILMethod_Sect_FatFormat) != 0)
82 int count = ComputeExceptionCount((hdr >> 8) & 0xFFFFFF, 24);
83 for (int i = 0; i < count; i++)
85 int flags = br.ReadInt32();
86 int tryOffset = br.ReadInt32();
87 int tryLength = br.ReadInt32();
88 int handlerOffset = br.ReadInt32();
89 int handlerLength = br.ReadInt32();
90 int classTokenOrFilterOffset = br.ReadInt32();
91 exceptionClauses.Add(new ExceptionHandlingClause(module, flags, tryOffset, tryLength, handlerOffset, handlerLength, classTokenOrFilterOffset, context));
96 int count = ComputeExceptionCount((hdr >> 8) & 0xFF, 12);
97 for (int i = 0; i < count; i++)
99 int flags = br.ReadUInt16();
100 int tryOffset = br.ReadUInt16();
101 int tryLength = br.ReadByte();
102 int handlerOffset = br.ReadUInt16();
103 int handlerLength = br.ReadByte();
104 int classTokenOrFilterOffset = br.ReadInt32();
105 exceptionClauses.Add(new ExceptionHandlingClause(module, flags, tryOffset, tryLength, handlerOffset, handlerLength, classTokenOrFilterOffset, context));
109 if (localVarSigTok != 0)
111 ByteReader sig = module.ResolveSignature(localVarSigTok);
112 Signature.ReadLocalVarSig(module, sig, context, locals);
117 throw new BadImageFormatException();
119 this.exceptionClauses = exceptionClauses.AsReadOnly();
120 this.locals = locals.AsReadOnly();
123 private static int ComputeExceptionCount(int size, int itemLength)
125 // LAMESPEC according to the spec, the count should be calculated as "(size - 4) / itemLength",
126 // FXBUG but to workaround a VB compiler bug that specifies the size incorrectly,
127 // we do a truncating division instead.
128 return size / itemLength;
131 public IList<ExceptionHandlingClause> ExceptionHandlingClauses
133 get { return exceptionClauses; }
136 public bool InitLocals
138 get { return initLocals; }
141 public IList<LocalVariableInfo> LocalVariables
143 get { return locals; }
146 public byte[] GetILAsByteArray()
151 public int LocalSignatureMetadataToken
153 get { return localVarSigTok; }
156 public int MaxStackSize
158 get { return maxStack; }