SourceMethodBuilder.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. //
  2. // SourceMethodBuilder.cs
  3. //
  4. // Authors:
  5. // Martin Baulig (martin@ximian.com)
  6. // Marek Safar (marek.safar@gmail.com)
  7. //
  8. // (C) 2002 Ximian, Inc. http://www.ximian.com
  9. // Copyright (C) 2012 Xamarin Inc (http://www.xamarin.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.Collections.Generic;
  32. namespace ILRuntime.Mono.CompilerServices.SymbolWriter
  33. {
  34. public class SourceMethodBuilder
  35. {
  36. List<LocalVariableEntry> _locals;
  37. List<CodeBlockEntry> _blocks;
  38. List<ScopeVariable> _scope_vars;
  39. Stack<CodeBlockEntry> _block_stack;
  40. readonly List<LineNumberEntry> method_lines;
  41. readonly ICompileUnit _comp_unit;
  42. readonly int ns_id;
  43. readonly IMethodDef method;
  44. public SourceMethodBuilder (ICompileUnit comp_unit)
  45. {
  46. this._comp_unit = comp_unit;
  47. method_lines = new List<LineNumberEntry> ();
  48. }
  49. public SourceMethodBuilder (ICompileUnit comp_unit, int ns_id, IMethodDef method)
  50. : this (comp_unit)
  51. {
  52. this.ns_id = ns_id;
  53. this.method = method;
  54. }
  55. public void MarkSequencePoint (int offset, SourceFileEntry file, int line, int column, bool is_hidden)
  56. {
  57. MarkSequencePoint (offset, file, line, column, -1, -1, is_hidden);
  58. }
  59. public void MarkSequencePoint (int offset, SourceFileEntry file, int line, int column, int end_line, int end_column, bool is_hidden)
  60. {
  61. int file_idx = file != null ? file.Index : 0;
  62. var lne = new LineNumberEntry (file_idx, line, column, end_line, end_column, offset, is_hidden);
  63. if (method_lines.Count > 0) {
  64. var prev = method_lines[method_lines.Count - 1];
  65. //
  66. // Same offset cannot be used for multiple lines
  67. //
  68. if (prev.Offset == offset) {
  69. //
  70. // Use the new location because debugger will adjust
  71. // the breakpoint to next line with sequence point
  72. //
  73. if (LineNumberEntry.LocationComparer.Default.Compare (lne, prev) > 0)
  74. method_lines[method_lines.Count - 1] = lne;
  75. return;
  76. }
  77. }
  78. method_lines.Add (lne);
  79. }
  80. public void StartBlock (CodeBlockEntry.Type type, int start_offset)
  81. {
  82. StartBlock (type, start_offset, _blocks == null ? 1 : _blocks.Count + 1);
  83. }
  84. public void StartBlock (CodeBlockEntry.Type type, int start_offset, int scopeIndex)
  85. {
  86. if (_block_stack == null) {
  87. _block_stack = new Stack<CodeBlockEntry> ();
  88. }
  89. if (_blocks == null)
  90. _blocks = new List<CodeBlockEntry> ();
  91. int parent = CurrentBlock != null ? CurrentBlock.Index : -1;
  92. CodeBlockEntry block = new CodeBlockEntry (
  93. scopeIndex, parent, type, start_offset);
  94. _block_stack.Push (block);
  95. _blocks.Add (block);
  96. }
  97. public void EndBlock (int end_offset)
  98. {
  99. CodeBlockEntry block = (CodeBlockEntry) _block_stack.Pop ();
  100. block.Close (end_offset);
  101. }
  102. public CodeBlockEntry[] Blocks {
  103. get {
  104. if (_blocks == null)
  105. return new CodeBlockEntry [0];
  106. CodeBlockEntry[] retval = new CodeBlockEntry [_blocks.Count];
  107. _blocks.CopyTo (retval, 0);
  108. return retval;
  109. }
  110. }
  111. public CodeBlockEntry CurrentBlock {
  112. get {
  113. if ((_block_stack != null) && (_block_stack.Count > 0))
  114. return (CodeBlockEntry) _block_stack.Peek ();
  115. else
  116. return null;
  117. }
  118. }
  119. public LocalVariableEntry[] Locals {
  120. get {
  121. if (_locals == null)
  122. return new LocalVariableEntry [0];
  123. else {
  124. return _locals.ToArray ();
  125. }
  126. }
  127. }
  128. public ICompileUnit SourceFile {
  129. get {
  130. return _comp_unit;
  131. }
  132. }
  133. public void AddLocal (int index, string name)
  134. {
  135. if (_locals == null)
  136. _locals = new List<LocalVariableEntry> ();
  137. int block_idx = CurrentBlock != null ? CurrentBlock.Index : 0;
  138. _locals.Add (new LocalVariableEntry (index, name, block_idx));
  139. }
  140. public ScopeVariable[] ScopeVariables {
  141. get {
  142. if (_scope_vars == null)
  143. return new ScopeVariable [0];
  144. return _scope_vars.ToArray ();
  145. }
  146. }
  147. public void AddScopeVariable (int scope, int index)
  148. {
  149. if (_scope_vars == null)
  150. _scope_vars = new List<ScopeVariable> ();
  151. _scope_vars.Add (
  152. new ScopeVariable (scope, index));
  153. }
  154. public void DefineMethod (MonoSymbolFile file)
  155. {
  156. DefineMethod (file, method.Token);
  157. }
  158. public void DefineMethod (MonoSymbolFile file, int token)
  159. {
  160. var blocks = Blocks;
  161. if (blocks.Length > 0) {
  162. //
  163. // When index is provided by user it can be inserted in
  164. // any order but mdb format does not store its value. It
  165. // uses stored order as the index instead.
  166. //
  167. var sorted = new List<CodeBlockEntry> (blocks.Length);
  168. int max_index = 0;
  169. for (int i = 0; i < blocks.Length; ++i) {
  170. max_index = System.Math.Max (max_index, blocks [i].Index);
  171. }
  172. for (int i = 0; i < max_index; ++i) {
  173. var scope_index = i + 1;
  174. //
  175. // Common fast path
  176. //
  177. if (i < blocks.Length && blocks [i].Index == scope_index) {
  178. sorted.Add (blocks [i]);
  179. continue;
  180. }
  181. bool found = false;
  182. for (int ii = 0; ii < blocks.Length; ++ii) {
  183. if (blocks [ii].Index == scope_index) {
  184. sorted.Add (blocks [ii]);
  185. found = true;
  186. break;
  187. }
  188. }
  189. if (found)
  190. continue;
  191. //
  192. // Ideally this should never happen but with current design we can
  193. // generate scope index for unreachable code before reachable code
  194. //
  195. sorted.Add (new CodeBlockEntry (scope_index, -1, CodeBlockEntry.Type.CompilerGenerated, 0));
  196. }
  197. blocks = sorted.ToArray ();
  198. //for (int i = 0; i < blocks.Length; ++i) {
  199. // if (blocks [i].Index - 1 != i)
  200. // throw new ArgumentException ("CodeBlocks cannot be converted to mdb format");
  201. //}
  202. }
  203. var entry = new MethodEntry (
  204. file, _comp_unit.Entry, token, ScopeVariables,
  205. Locals, method_lines.ToArray (), blocks, null, MethodEntry.Flags.ColumnsInfoIncluded, ns_id);
  206. file.AddMethod (entry);
  207. }
  208. }
  209. }