Merge remote branch 'upstream/master'
[mono.git] / mcs / class / IKVM.Reflection / MethodBody.cs
1 /*
2   Copyright (C) 2009 Jeroen Frijters
3
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.
7
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:
11
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.
19
20   Jeroen Frijters
21   jeroen@frijters.net
22   
23 */
24 using System;
25 using System.Collections.Generic;
26 using IKVM.Reflection.Reader;
27 using System.IO;
28
29 namespace IKVM.Reflection
30 {
31         public sealed class MethodBody
32         {
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;
38                 private byte[] body;
39
40                 internal MethodBody(ModuleReader module, int rva, IGenericContext context)
41                 {
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;
49
50                         List<ExceptionHandlingClause> exceptionClauses = new List<ExceptionHandlingClause>();
51                         List<LocalVariableInfo> locals = new List<LocalVariableInfo>();
52                         module.SeekRVA(rva);
53                         BinaryReader br = new BinaryReader(module.stream);
54                         byte b = br.ReadByte();
55                         if ((b & 3) == CorILMethod_TinyFormat)
56                         {
57                                 initLocals = true;
58                                 body = br.ReadBytes(b >> 2);
59                         }
60                         else if ((b & 3) == CorILMethod_FatFormat)
61                         {
62                                 initLocals = (b & CorILMethod_InitLocals) != 0;
63                                 short flagsAndSize = (short)(b | (br.ReadByte() << 8));
64                                 if ((flagsAndSize >> 12) != 3)
65                                 {
66                                         throw new BadImageFormatException("Fat format method header size should be 3");
67                                 }
68                                 maxStack = br.ReadUInt16();
69                                 int codeLength = br.ReadInt32();
70                                 localVarSigTok = br.ReadInt32();
71                                 body = br.ReadBytes(codeLength);
72                                 if ((b & CorILMethod_MoreSects) != 0)
73                                 {
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)
77                                         {
78                                                 throw new NotImplementedException();
79                                         }
80                                         else if ((hdr & CorILMethod_Sect_FatFormat) != 0)
81                                         {
82                                                 int count = ComputeExceptionCount((hdr >> 8) & 0xFFFFFF, 24);
83                                                 for (int i = 0; i < count; i++)
84                                                 {
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));
92                                                 }
93                                         }
94                                         else
95                                         {
96                                                 int count = ComputeExceptionCount((hdr >> 8) & 0xFF, 12);
97                                                 for (int i = 0; i < count; i++)
98                                                 {
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));
106                                                 }
107                                         }
108                                 }
109                                 if (localVarSigTok != 0)
110                                 {
111                                         ByteReader sig = module.ResolveSignature(localVarSigTok);
112                                         Signature.ReadLocalVarSig(module, sig, context, locals);
113                                 }
114                         }
115                         else
116                         {
117                                 throw new BadImageFormatException();
118                         }
119                         this.exceptionClauses = exceptionClauses.AsReadOnly();
120                         this.locals = locals.AsReadOnly();
121                 }
122
123                 private static int ComputeExceptionCount(int size, int itemLength)
124                 {
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;
129                 }
130
131                 public IList<ExceptionHandlingClause> ExceptionHandlingClauses
132                 {
133                         get { return exceptionClauses; }
134                 }
135
136                 public bool InitLocals
137                 {
138                         get { return initLocals; }
139                 }
140
141                 public IList<LocalVariableInfo> LocalVariables
142                 {
143                         get { return locals; }
144                 }
145
146                 public byte[] GetILAsByteArray()
147                 {
148                         return body;
149                 }
150
151                 public int LocalSignatureMetadataToken
152                 {
153                         get { return localVarSigTok; }
154                 }
155
156                 public int MaxStackSize
157                 {
158                         get { return maxStack; }
159                 }
160         }
161 }