do not check order sequence if option /order was not used
[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                                 maxStack = 8;
60                         }
61                         else if ((b & 3) == CorILMethod_FatFormat)
62                         {
63                                 initLocals = (b & CorILMethod_InitLocals) != 0;
64                                 short flagsAndSize = (short)(b | (br.ReadByte() << 8));
65                                 if ((flagsAndSize >> 12) != 3)
66                                 {
67                                         throw new BadImageFormatException("Fat format method header size should be 3");
68                                 }
69                                 maxStack = br.ReadUInt16();
70                                 int codeLength = br.ReadInt32();
71                                 localVarSigTok = br.ReadInt32();
72                                 body = br.ReadBytes(codeLength);
73                                 if ((b & CorILMethod_MoreSects) != 0)
74                                 {
75                                         module.stream.Position = (module.stream.Position + 3) & ~3;
76                                         int hdr = br.ReadInt32();
77                                         if ((hdr & CorILMethod_Sect_MoreSects) != 0 || (hdr & CorILMethod_Sect_EHTable) == 0)
78                                         {
79                                                 throw new NotImplementedException();
80                                         }
81                                         else if ((hdr & CorILMethod_Sect_FatFormat) != 0)
82                                         {
83                                                 int count = ComputeExceptionCount((hdr >> 8) & 0xFFFFFF, 24);
84                                                 for (int i = 0; i < count; i++)
85                                                 {
86                                                         int flags = br.ReadInt32();
87                                                         int tryOffset = br.ReadInt32();
88                                                         int tryLength = br.ReadInt32();
89                                                         int handlerOffset = br.ReadInt32();
90                                                         int handlerLength = br.ReadInt32();
91                                                         int classTokenOrFilterOffset = br.ReadInt32();
92                                                         exceptionClauses.Add(new ExceptionHandlingClause(module, flags, tryOffset, tryLength, handlerOffset, handlerLength, classTokenOrFilterOffset, context));
93                                                 }
94                                         }
95                                         else
96                                         {
97                                                 int count = ComputeExceptionCount((hdr >> 8) & 0xFF, 12);
98                                                 for (int i = 0; i < count; i++)
99                                                 {
100                                                         int flags = br.ReadUInt16();
101                                                         int tryOffset = br.ReadUInt16();
102                                                         int tryLength = br.ReadByte();
103                                                         int handlerOffset = br.ReadUInt16();
104                                                         int handlerLength = br.ReadByte();
105                                                         int classTokenOrFilterOffset = br.ReadInt32();
106                                                         exceptionClauses.Add(new ExceptionHandlingClause(module, flags, tryOffset, tryLength, handlerOffset, handlerLength, classTokenOrFilterOffset, context));
107                                                 }
108                                         }
109                                 }
110                                 if (localVarSigTok != 0)
111                                 {
112                                         ByteReader sig = module.GetStandAloneSig((localVarSigTok & 0xFFFFFF) - 1);
113                                         Signature.ReadLocalVarSig(module, sig, context, locals);
114                                 }
115                         }
116                         else
117                         {
118                                 throw new BadImageFormatException();
119                         }
120                         this.exceptionClauses = exceptionClauses.AsReadOnly();
121                         this.locals = locals.AsReadOnly();
122                 }
123
124                 private static int ComputeExceptionCount(int size, int itemLength)
125                 {
126                         // LAMESPEC according to the spec, the count should be calculated as "(size - 4) / itemLength",
127                         // FXBUG but to workaround a VB compiler bug that specifies the size incorrectly,
128                         // we do a truncating division instead.
129                         return size / itemLength;
130                 }
131
132                 public IList<ExceptionHandlingClause> ExceptionHandlingClauses
133                 {
134                         get { return exceptionClauses; }
135                 }
136
137                 public bool InitLocals
138                 {
139                         get { return initLocals; }
140                 }
141
142                 public IList<LocalVariableInfo> LocalVariables
143                 {
144                         get { return locals; }
145                 }
146
147                 public byte[] GetILAsByteArray()
148                 {
149                         return body;
150                 }
151
152                 public int LocalSignatureMetadataToken
153                 {
154                         get { return localVarSigTok; }
155                 }
156
157                 public int MaxStackSize
158                 {
159                         get { return maxStack; }
160                 }
161         }
162 }