MethodEntry.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. using System;
  2. using System.Collections.Generic;
  3. namespace Mono.CompilerServices.SymbolWriter
  4. {
  5. public class MethodEntry : IComparable
  6. {
  7. [Flags]
  8. public enum Flags
  9. {
  10. LocalNamesAmbiguous = 1
  11. }
  12. public const int Size = 12;
  13. public readonly int CompileUnitIndex;
  14. public readonly int Token;
  15. public readonly int NamespaceID;
  16. private int DataOffset;
  17. private int LocalVariableTableOffset;
  18. private int LineNumberTableOffset;
  19. private int CodeBlockTableOffset;
  20. private int ScopeVariableTableOffset;
  21. private int RealNameOffset;
  22. private MethodEntry.Flags flags;
  23. private int index;
  24. public readonly CompileUnitEntry CompileUnit;
  25. private LocalVariableEntry[] locals;
  26. private CodeBlockEntry[] code_blocks;
  27. private ScopeVariable[] scope_vars;
  28. private LineNumberTable lnt;
  29. private string real_name;
  30. public readonly MonoSymbolFile SymbolFile;
  31. public MethodEntry.Flags MethodFlags
  32. {
  33. get
  34. {
  35. return this.flags;
  36. }
  37. }
  38. public int Index
  39. {
  40. get
  41. {
  42. return this.index;
  43. }
  44. set
  45. {
  46. this.index = value;
  47. }
  48. }
  49. internal MethodEntry(MonoSymbolFile file, MyBinaryReader reader, int index)
  50. {
  51. this.SymbolFile = file;
  52. this.index = index;
  53. this.Token = reader.ReadInt32();
  54. this.DataOffset = reader.ReadInt32();
  55. this.LineNumberTableOffset = reader.ReadInt32();
  56. long old_pos = reader.BaseStream.Position;
  57. reader.BaseStream.Position = (long)this.DataOffset;
  58. this.CompileUnitIndex = reader.ReadLeb128();
  59. this.LocalVariableTableOffset = reader.ReadLeb128();
  60. this.NamespaceID = reader.ReadLeb128();
  61. this.CodeBlockTableOffset = reader.ReadLeb128();
  62. this.ScopeVariableTableOffset = reader.ReadLeb128();
  63. this.RealNameOffset = reader.ReadLeb128();
  64. this.flags = (MethodEntry.Flags)reader.ReadLeb128();
  65. reader.BaseStream.Position = old_pos;
  66. this.CompileUnit = file.GetCompileUnit(this.CompileUnitIndex);
  67. }
  68. internal MethodEntry(MonoSymbolFile file, CompileUnitEntry comp_unit, int token, ScopeVariable[] scope_vars, LocalVariableEntry[] locals, LineNumberEntry[] lines, CodeBlockEntry[] code_blocks, string real_name, MethodEntry.Flags flags, int namespace_id)
  69. {
  70. this.SymbolFile = file;
  71. this.real_name = real_name;
  72. this.locals = locals;
  73. this.code_blocks = code_blocks;
  74. this.scope_vars = scope_vars;
  75. this.flags = flags;
  76. this.index = -1;
  77. this.Token = token;
  78. this.CompileUnitIndex = comp_unit.Index;
  79. this.CompileUnit = comp_unit;
  80. this.NamespaceID = namespace_id;
  81. this.CheckLineNumberTable(lines);
  82. this.lnt = new LineNumberTable(file, lines);
  83. file.NumLineNumbers += lines.Length;
  84. int num_locals = (locals != null) ? locals.Length : 0;
  85. if (num_locals <= 32)
  86. {
  87. for (int i = 0; i < num_locals; i++)
  88. {
  89. string nm = locals[i].Name;
  90. for (int j = i + 1; j < num_locals; j++)
  91. {
  92. if (locals[j].Name == nm)
  93. {
  94. flags |= MethodEntry.Flags.LocalNamesAmbiguous;
  95. goto IL_108;
  96. }
  97. }
  98. }
  99. IL_108:;
  100. }
  101. else
  102. {
  103. Dictionary<string, LocalVariableEntry> local_names = new Dictionary<string, LocalVariableEntry>();
  104. for (int k = 0; k < locals.Length; k++)
  105. {
  106. LocalVariableEntry local = locals[k];
  107. if (local_names.ContainsKey(local.Name))
  108. {
  109. flags |= MethodEntry.Flags.LocalNamesAmbiguous;
  110. break;
  111. }
  112. local_names.Add(local.Name, local);
  113. }
  114. }
  115. }
  116. private void CheckLineNumberTable(LineNumberEntry[] line_numbers)
  117. {
  118. int last_offset = -1;
  119. int last_row = -1;
  120. if (line_numbers != null)
  121. {
  122. for (int i = 0; i < line_numbers.Length; i++)
  123. {
  124. LineNumberEntry line = line_numbers[i];
  125. if (line.Equals(LineNumberEntry.Null))
  126. {
  127. throw new MonoSymbolFileException();
  128. }
  129. if (line.Offset < last_offset)
  130. {
  131. throw new MonoSymbolFileException();
  132. }
  133. if (line.Offset > last_offset)
  134. {
  135. last_row = line.Row;
  136. last_offset = line.Offset;
  137. }
  138. else
  139. {
  140. if (line.Row > last_row)
  141. {
  142. last_row = line.Row;
  143. }
  144. }
  145. }
  146. }
  147. }
  148. //internal void Write(MyBinaryWriter bw)
  149. //{
  150. // if (this.index <= 0 || this.DataOffset == 0)
  151. // {
  152. // throw new InvalidOperationException();
  153. // }
  154. // bw.Write(this.Token);
  155. // bw.Write(this.DataOffset);
  156. // bw.Write(this.LineNumberTableOffset);
  157. //}
  158. //internal void WriteData(MonoSymbolFile file, MyBinaryWriter bw)
  159. //{
  160. // if (this.index <= 0)
  161. // {
  162. // throw new InvalidOperationException();
  163. // }
  164. // this.LocalVariableTableOffset = (int)bw.BaseStream.Position;
  165. // int num_locals = (this.locals != null) ? this.locals.Length : 0;
  166. // bw.WriteLeb128(num_locals);
  167. // for (int i = 0; i < num_locals; i++)
  168. // {
  169. // this.locals[i].Write(file, bw);
  170. // }
  171. // file.LocalCount += num_locals;
  172. // this.CodeBlockTableOffset = (int)bw.BaseStream.Position;
  173. // int num_code_blocks = (this.code_blocks != null) ? this.code_blocks.Length : 0;
  174. // bw.WriteLeb128(num_code_blocks);
  175. // for (int i = 0; i < num_code_blocks; i++)
  176. // {
  177. // this.code_blocks[i].Write(bw);
  178. // }
  179. // this.ScopeVariableTableOffset = (int)bw.BaseStream.Position;
  180. // int num_scope_vars = (this.scope_vars != null) ? this.scope_vars.Length : 0;
  181. // bw.WriteLeb128(num_scope_vars);
  182. // for (int i = 0; i < num_scope_vars; i++)
  183. // {
  184. // this.scope_vars[i].Write(bw);
  185. // }
  186. // if (this.real_name != null)
  187. // {
  188. // this.RealNameOffset = (int)bw.BaseStream.Position;
  189. // bw.Write(this.real_name);
  190. // }
  191. // this.LineNumberTableOffset = (int)bw.BaseStream.Position;
  192. // this.lnt.Write(file, bw);
  193. // this.DataOffset = (int)bw.BaseStream.Position;
  194. // bw.WriteLeb128(this.CompileUnitIndex);
  195. // bw.WriteLeb128(this.LocalVariableTableOffset);
  196. // bw.WriteLeb128(this.NamespaceID);
  197. // bw.WriteLeb128(this.CodeBlockTableOffset);
  198. // bw.WriteLeb128(this.ScopeVariableTableOffset);
  199. // bw.WriteLeb128(this.RealNameOffset);
  200. // bw.WriteLeb128((int)this.flags);
  201. //}
  202. public LineNumberTable GetLineNumberTable()
  203. {
  204. LineNumberTable result;
  205. lock (this.SymbolFile)
  206. {
  207. if (this.lnt != null)
  208. {
  209. result = this.lnt;
  210. }
  211. else
  212. {
  213. if (this.LineNumberTableOffset == 0)
  214. {
  215. result = null;
  216. }
  217. else
  218. {
  219. MyBinaryReader reader = this.SymbolFile.BinaryReader;
  220. long old_pos = reader.BaseStream.Position;
  221. reader.BaseStream.Position = (long)this.LineNumberTableOffset;
  222. this.lnt = LineNumberTable.Read(this.SymbolFile, reader);
  223. reader.BaseStream.Position = old_pos;
  224. result = this.lnt;
  225. }
  226. }
  227. }
  228. return result;
  229. }
  230. public LocalVariableEntry[] GetLocals()
  231. {
  232. LocalVariableEntry[] result;
  233. lock (this.SymbolFile)
  234. {
  235. if (this.locals != null)
  236. {
  237. result = this.locals;
  238. }
  239. else
  240. {
  241. if (this.LocalVariableTableOffset == 0)
  242. {
  243. result = null;
  244. }
  245. else
  246. {
  247. MyBinaryReader reader = this.SymbolFile.BinaryReader;
  248. long old_pos = reader.BaseStream.Position;
  249. reader.BaseStream.Position = (long)this.LocalVariableTableOffset;
  250. int num_locals = reader.ReadLeb128();
  251. this.locals = new LocalVariableEntry[num_locals];
  252. for (int i = 0; i < num_locals; i++)
  253. {
  254. this.locals[i] = new LocalVariableEntry(this.SymbolFile, reader);
  255. }
  256. reader.BaseStream.Position = old_pos;
  257. result = this.locals;
  258. }
  259. }
  260. }
  261. return result;
  262. }
  263. public CodeBlockEntry[] GetCodeBlocks()
  264. {
  265. CodeBlockEntry[] result;
  266. lock (this.SymbolFile)
  267. {
  268. if (this.code_blocks != null)
  269. {
  270. result = this.code_blocks;
  271. }
  272. else
  273. {
  274. if (this.CodeBlockTableOffset == 0)
  275. {
  276. result = null;
  277. }
  278. else
  279. {
  280. MyBinaryReader reader = this.SymbolFile.BinaryReader;
  281. long old_pos = reader.BaseStream.Position;
  282. reader.BaseStream.Position = (long)this.CodeBlockTableOffset;
  283. int num_code_blocks = reader.ReadLeb128();
  284. this.code_blocks = new CodeBlockEntry[num_code_blocks];
  285. for (int i = 0; i < num_code_blocks; i++)
  286. {
  287. this.code_blocks[i] = new CodeBlockEntry(i, reader);
  288. }
  289. reader.BaseStream.Position = old_pos;
  290. result = this.code_blocks;
  291. }
  292. }
  293. }
  294. return result;
  295. }
  296. public ScopeVariable[] GetScopeVariables()
  297. {
  298. ScopeVariable[] result;
  299. lock (this.SymbolFile)
  300. {
  301. if (this.scope_vars != null)
  302. {
  303. result = this.scope_vars;
  304. }
  305. else
  306. {
  307. if (this.ScopeVariableTableOffset == 0)
  308. {
  309. result = null;
  310. }
  311. else
  312. {
  313. MyBinaryReader reader = this.SymbolFile.BinaryReader;
  314. long old_pos = reader.BaseStream.Position;
  315. reader.BaseStream.Position = (long)this.ScopeVariableTableOffset;
  316. int num_scope_vars = reader.ReadLeb128();
  317. this.scope_vars = new ScopeVariable[num_scope_vars];
  318. for (int i = 0; i < num_scope_vars; i++)
  319. {
  320. this.scope_vars[i] = new ScopeVariable(reader);
  321. }
  322. reader.BaseStream.Position = old_pos;
  323. result = this.scope_vars;
  324. }
  325. }
  326. }
  327. return result;
  328. }
  329. public string GetRealName()
  330. {
  331. string result;
  332. lock (this.SymbolFile)
  333. {
  334. if (this.real_name != null)
  335. {
  336. result = this.real_name;
  337. }
  338. else
  339. {
  340. if (this.RealNameOffset == 0)
  341. {
  342. result = null;
  343. }
  344. else
  345. {
  346. this.real_name = this.SymbolFile.BinaryReader.ReadString(this.RealNameOffset);
  347. result = this.real_name;
  348. }
  349. }
  350. }
  351. return result;
  352. }
  353. public int CompareTo(object obj)
  354. {
  355. MethodEntry method = (MethodEntry)obj;
  356. int result;
  357. if (method.Token < this.Token)
  358. {
  359. result = 1;
  360. }
  361. else
  362. {
  363. if (method.Token > this.Token)
  364. {
  365. result = -1;
  366. }
  367. else
  368. {
  369. result = 0;
  370. }
  371. }
  372. return result;
  373. }
  374. public override string ToString()
  375. {
  376. return string.Format("[Method {0}:{1:x}:{2}:{3}]", new object[]
  377. {
  378. this.index,
  379. this.Token,
  380. this.CompileUnitIndex,
  381. this.CompileUnit
  382. });
  383. }
  384. }
  385. }