ILGenerator.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. //
  2. // System.Reflection.Emit/ILGenerator.cs
  3. //
  4. // Author:
  5. // Paolo Molaro ([email protected])
  6. //
  7. // (C) 2001 Ximian, Inc. http://www.ximian.com
  8. //
  9. using System;
  10. using System.Collections;
  11. using System.Diagnostics.SymbolStore;
  12. using System.Runtime.InteropServices;
  13. namespace System.Reflection.Emit {
  14. internal struct ILExceptionBlock {
  15. public const int CATCH = 0;
  16. public const int FILTER = 1;
  17. public const int FINALLY = 2;
  18. public const int FAULT = 4;
  19. internal Type extype;
  20. internal int type;
  21. internal int start;
  22. internal int len;
  23. internal int filter_offset;
  24. internal void Debug () {
  25. #if NO
  26. System.Console.Write ("\ttype="+type.ToString()+" start="+start.ToString()+" len="+len.ToString());
  27. if (extype != null)
  28. System.Console.WriteLine (" extype="+extype.ToString());
  29. else
  30. System.Console.WriteLine ("");
  31. #endif
  32. }
  33. }
  34. internal struct ILExceptionInfo {
  35. ILExceptionBlock[] handlers;
  36. internal int start;
  37. int len;
  38. internal Label end;
  39. internal int NumHandlers () {
  40. return handlers.Length;
  41. }
  42. internal void AddCatch (Type extype, int offset) {
  43. int i;
  44. End (offset);
  45. add_block (offset);
  46. i = handlers.Length - 1;
  47. handlers [i].type = ILExceptionBlock.CATCH;
  48. handlers [i].start = offset;
  49. handlers [i].extype = extype;
  50. }
  51. internal void AddFinally (int offset) {
  52. int i;
  53. End (offset);
  54. add_block (offset);
  55. i = handlers.Length - 1;
  56. handlers [i].type = ILExceptionBlock.FINALLY;
  57. handlers [i].start = offset;
  58. handlers [i].extype = null;
  59. }
  60. internal void End (int offset) {
  61. if (handlers == null)
  62. return;
  63. int i = handlers.Length - 1;
  64. if (i >= 0)
  65. handlers [i].len = offset - handlers [i].start;
  66. }
  67. internal int LastClauseType () {
  68. if (handlers != null)
  69. return handlers [handlers.Length-1].type;
  70. else
  71. return ILExceptionBlock.CATCH;
  72. }
  73. internal void Debug (int b) {
  74. #if NO
  75. System.Console.WriteLine ("Handler {0} at {1}, len: {2}", b, start, len);
  76. for (int i = 0; i < handlers.Length; ++i)
  77. handlers [i].Debug ();
  78. #endif
  79. }
  80. void add_block (int offset) {
  81. if (handlers != null) {
  82. int i = handlers.Length;
  83. ILExceptionBlock[] new_b = new ILExceptionBlock [i + 1];
  84. System.Array.Copy (handlers, new_b, i);
  85. handlers = new_b;
  86. handlers [i].len = offset - handlers [i].start;
  87. } else {
  88. handlers = new ILExceptionBlock [1];
  89. len = offset - start;
  90. }
  91. }
  92. }
  93. internal struct ILTokenInfo {
  94. public MemberInfo member;
  95. public int code_pos;
  96. }
  97. public class ILGenerator: Object {
  98. private struct LabelFixup {
  99. public int size;
  100. public int pos; // the location of the fixup
  101. public int label_base; // the base address for this fixup
  102. public int label_idx;
  103. };
  104. static Type void_type = typeof (void);
  105. private byte[] code;
  106. private MethodBase mbuilder; /* a MethodBuilder or ConstructorBuilder */
  107. private int code_len;
  108. private int max_stack;
  109. private int cur_stack;
  110. private LocalBuilder[] locals;
  111. private ILExceptionInfo[] ex_handlers;
  112. private int num_token_fixups;
  113. private ILTokenInfo[] token_fixups;
  114. private int[] label_to_addr;
  115. private int num_labels;
  116. private LabelFixup[] fixups;
  117. private int num_fixups;
  118. private ModuleBuilder module;
  119. private AssemblyBuilder abuilder;
  120. private ISymbolWriter sym_writer;
  121. private Stack scopes;
  122. private int cur_block;
  123. private Stack open_blocks;
  124. internal ILGenerator (MethodBase mb, int size) {
  125. if (size < 0)
  126. size = 128;
  127. code_len = 0;
  128. code = new byte [size];
  129. mbuilder = mb;
  130. cur_stack = max_stack = 0;
  131. num_fixups = num_labels = 0;
  132. label_to_addr = new int [8];
  133. fixups = new LabelFixup [8];
  134. token_fixups = new ILTokenInfo [8];
  135. num_token_fixups = 0;
  136. if (mb is MethodBuilder) {
  137. module = (ModuleBuilder)((MethodBuilder)mb).TypeBuilder.Module;
  138. } else if (mb is ConstructorBuilder) {
  139. module = (ModuleBuilder)((ConstructorBuilder)mb).TypeBuilder.Module;
  140. }
  141. abuilder = (AssemblyBuilder)module.Assembly;
  142. sym_writer = module.GetSymWriter ();
  143. open_blocks = new Stack ();
  144. }
  145. private void add_token_fixup (MemberInfo mi) {
  146. if (num_token_fixups == token_fixups.Length) {
  147. ILTokenInfo[] ntf = new ILTokenInfo [num_token_fixups * 2];
  148. token_fixups.CopyTo (ntf, 0);
  149. token_fixups = ntf;
  150. }
  151. token_fixups [num_token_fixups].member = mi;
  152. token_fixups [num_token_fixups++].code_pos = code_len;
  153. }
  154. private void make_room (int nbytes) {
  155. if (code_len + nbytes < code.Length)
  156. return;
  157. byte[] new_code = new byte [(code_len + nbytes) * 2 + 128];
  158. System.Array.Copy (code, 0, new_code, 0, code.Length);
  159. code = new_code;
  160. }
  161. private void emit_int (int val) {
  162. code [code_len++] = (byte) (val & 0xFF);
  163. code [code_len++] = (byte) ((val >> 8) & 0xFF);
  164. code [code_len++] = (byte) ((val >> 16) & 0xFF);
  165. code [code_len++] = (byte) ((val >> 24) & 0xFF);
  166. }
  167. /* change to pass by ref to avoid copy */
  168. private void ll_emit (OpCode opcode) {
  169. /*
  170. * there is already enough room allocated in code.
  171. */
  172. // access op1 and op2 directly since the Value property is useless
  173. if (opcode.Size == 2)
  174. code [code_len++] = opcode.op1;
  175. code [code_len++] = opcode.op2;
  176. /*
  177. * We should probably keep track of stack needs here.
  178. * Or we may want to run the verifier on the code before saving it
  179. * (this may be needed anyway when the ILGenerator is not used...).
  180. */
  181. switch (opcode.StackBehaviourPush) {
  182. case StackBehaviour.Push1:
  183. case StackBehaviour.Pushi:
  184. case StackBehaviour.Pushi8:
  185. case StackBehaviour.Pushr4:
  186. case StackBehaviour.Pushr8:
  187. case StackBehaviour.Pushref:
  188. case StackBehaviour.Varpush: /* again we are conservative and assume it pushes 1 */
  189. cur_stack ++;
  190. break;
  191. case StackBehaviour.Push1_push1:
  192. cur_stack += 2;
  193. break;
  194. }
  195. if (max_stack < cur_stack)
  196. max_stack = cur_stack;
  197. /*
  198. * Note that we adjust for the pop behaviour _after_ setting max_stack.
  199. */
  200. switch (opcode.StackBehaviourPop) {
  201. case StackBehaviour.Varpop:
  202. break; /* we are conservative and assume it doesn't decrease the stack needs */
  203. case StackBehaviour.Pop1:
  204. case StackBehaviour.Popi:
  205. case StackBehaviour.Popref:
  206. cur_stack --;
  207. break;
  208. case StackBehaviour.Pop1_pop1:
  209. case StackBehaviour.Popi_pop1:
  210. case StackBehaviour.Popi_popi:
  211. case StackBehaviour.Popi_popi8:
  212. case StackBehaviour.Popi_popr4:
  213. case StackBehaviour.Popi_popr8:
  214. case StackBehaviour.Popref_pop1:
  215. case StackBehaviour.Popref_popi:
  216. cur_stack -= 2;
  217. break;
  218. case StackBehaviour.Popi_popi_popi:
  219. case StackBehaviour.Popref_popi_popi:
  220. case StackBehaviour.Popref_popi_popi8:
  221. case StackBehaviour.Popref_popi_popr4:
  222. case StackBehaviour.Popref_popi_popr8:
  223. case StackBehaviour.Popref_popi_popref:
  224. cur_stack -= 3;
  225. break;
  226. }
  227. }
  228. private static int target_len (OpCode opcode) {
  229. if (opcode.operandType == OperandType.InlineBrTarget)
  230. return 4;
  231. return 1;
  232. }
  233. private void InternalEndClause () {
  234. switch (ex_handlers [cur_block].LastClauseType ()) {
  235. case ILExceptionBlock.CATCH:
  236. // how could we optimize code size here?
  237. Emit (OpCodes.Leave, ex_handlers [cur_block].end);
  238. break;
  239. case ILExceptionBlock.FAULT:
  240. case ILExceptionBlock.FINALLY:
  241. Emit (OpCodes.Endfinally);
  242. break;
  243. case ILExceptionBlock.FILTER:
  244. Emit (OpCodes.Endfilter);
  245. break;
  246. }
  247. }
  248. public virtual void BeginCatchBlock (Type exceptionType) {
  249. if (open_blocks.Count <= 0)
  250. throw new NotSupportedException ("Not in an exception block");
  251. InternalEndClause ();
  252. ex_handlers [cur_block].AddCatch (exceptionType, code_len);
  253. cur_stack = 1; // the exception object is on the stack by default
  254. if (max_stack < cur_stack)
  255. max_stack = cur_stack;
  256. //System.Console.WriteLine ("Begin catch Block: {0} {1}",exceptionType.ToString(), max_stack);
  257. //throw new NotImplementedException ();
  258. }
  259. public virtual void BeginExceptFilterBlock () {
  260. throw new NotImplementedException ();
  261. }
  262. public virtual Label BeginExceptionBlock () {
  263. //System.Console.WriteLine ("Begin Block");
  264. if (ex_handlers != null) {
  265. cur_block = ex_handlers.Length;
  266. ILExceptionInfo[] new_ex = new ILExceptionInfo [cur_block + 1];
  267. System.Array.Copy (ex_handlers, new_ex, cur_block);
  268. ex_handlers = new_ex;
  269. } else {
  270. ex_handlers = new ILExceptionInfo [1];
  271. cur_block = 0;
  272. }
  273. open_blocks.Push (cur_block);
  274. ex_handlers [cur_block].start = code_len;
  275. return ex_handlers [cur_block].end = DefineLabel ();
  276. }
  277. public virtual void BeginFaultBlock() {
  278. if (open_blocks.Count <= 0)
  279. throw new NotSupportedException ("Not in an exception block");
  280. //System.Console.WriteLine ("Begin fault Block");
  281. //throw new NotImplementedException ();
  282. }
  283. public virtual void BeginFinallyBlock() {
  284. if (open_blocks.Count <= 0)
  285. throw new NotSupportedException ("Not in an exception block");
  286. InternalEndClause ();
  287. //System.Console.WriteLine ("Begin finally Block");
  288. ex_handlers [cur_block].AddFinally (code_len);
  289. }
  290. public virtual void BeginScope () {
  291. if (sym_writer != null) {
  292. if (scopes == null)
  293. scopes = new Stack ();
  294. scopes.Push (sym_writer.OpenScope (code_len));
  295. }
  296. }
  297. public LocalBuilder DeclareLocal (Type localType) {
  298. LocalBuilder res = new LocalBuilder (module, localType, this);
  299. if (locals != null) {
  300. LocalBuilder[] new_l = new LocalBuilder [locals.Length + 1];
  301. System.Array.Copy (locals, new_l, locals.Length);
  302. new_l [locals.Length] = res;
  303. locals = new_l;
  304. } else {
  305. locals = new LocalBuilder [1];
  306. locals [0] = res;
  307. }
  308. res.position = (uint)(locals.Length - 1);
  309. return res;
  310. }
  311. public virtual Label DefineLabel () {
  312. if (num_labels >= label_to_addr.Length) {
  313. int[] new_l = new int [label_to_addr.Length * 2];
  314. System.Array.Copy (label_to_addr, new_l, label_to_addr.Length);
  315. label_to_addr = new_l;
  316. }
  317. label_to_addr [num_labels] = -1;
  318. return new Label (num_labels++);
  319. }
  320. public virtual void Emit (OpCode opcode) {
  321. make_room (2);
  322. ll_emit (opcode);
  323. }
  324. public virtual void Emit (OpCode opcode, Byte val) {
  325. make_room (3);
  326. ll_emit (opcode);
  327. code [code_len++] = val;
  328. }
  329. public virtual void Emit (OpCode opcode, ConstructorInfo constructor) {
  330. int token = abuilder.GetToken (constructor);
  331. make_room (6);
  332. ll_emit (opcode);
  333. if (constructor is ConstructorBuilder)
  334. add_token_fixup (constructor);
  335. emit_int (token);
  336. ParameterInfo[] mparams = constructor.GetParameters();
  337. if (mparams != null)
  338. cur_stack -= mparams.Length;
  339. }
  340. public virtual void Emit (OpCode opcode, double val) {
  341. byte[] s = System.BitConverter.GetBytes (val);
  342. make_room (10);
  343. ll_emit (opcode);
  344. if (BitConverter.IsLittleEndian){
  345. System.Array.Copy (s, 0, code, code_len, 8);
  346. code_len += 8;
  347. } else {
  348. code [code_len++] = s [7];
  349. code [code_len++] = s [6];
  350. code [code_len++] = s [5];
  351. code [code_len++] = s [4];
  352. code [code_len++] = s [3];
  353. code [code_len++] = s [2];
  354. code [code_len++] = s [1];
  355. code [code_len++] = s [0];
  356. }
  357. }
  358. public virtual void Emit (OpCode opcode, FieldInfo field) {
  359. int token = abuilder.GetToken (field);
  360. make_room (6);
  361. ll_emit (opcode);
  362. if (field is FieldBuilder)
  363. add_token_fixup (field);
  364. emit_int (token);
  365. }
  366. public virtual void Emit (OpCode opcode, Int16 val) {
  367. make_room (4);
  368. ll_emit (opcode);
  369. code [code_len++] = (byte) (val & 0xFF);
  370. code [code_len++] = (byte) ((val >> 8) & 0xFF);
  371. }
  372. public virtual void Emit (OpCode opcode, int val) {
  373. make_room (6);
  374. ll_emit (opcode);
  375. emit_int (val);
  376. }
  377. public virtual void Emit (OpCode opcode, long val) {
  378. make_room (10);
  379. ll_emit (opcode);
  380. code [code_len++] = (byte) (val & 0xFF);
  381. code [code_len++] = (byte) ((val >> 8) & 0xFF);
  382. code [code_len++] = (byte) ((val >> 16) & 0xFF);
  383. code [code_len++] = (byte) ((val >> 24) & 0xFF);
  384. code [code_len++] = (byte) ((val >> 32) & 0xFF);
  385. code [code_len++] = (byte) ((val >> 40) & 0xFF);
  386. code [code_len++] = (byte) ((val >> 48) & 0xFF);
  387. code [code_len++] = (byte) ((val >> 56) & 0xFF);
  388. }
  389. public virtual void Emit (OpCode opcode, Label label) {
  390. int tlen = target_len (opcode);
  391. make_room (6);
  392. ll_emit (opcode);
  393. if (num_fixups >= fixups.Length) {
  394. LabelFixup[] newf = new LabelFixup [fixups.Length + 16];
  395. System.Array.Copy (fixups, newf, fixups.Length);
  396. fixups = newf;
  397. }
  398. fixups [num_fixups].size = tlen;
  399. fixups [num_fixups].pos = code_len;
  400. fixups [num_fixups].label_base = code_len;
  401. fixups [num_fixups].label_idx = label.label;
  402. num_fixups++;
  403. code_len += tlen;
  404. }
  405. public virtual void Emit (OpCode opcode, Label[] labels) {
  406. /* opcode needs to be switch. */
  407. int count = labels.Length;
  408. make_room (6 + count * 4);
  409. ll_emit (opcode);
  410. int switch_base = code_len + count*4;
  411. emit_int (count);
  412. if (num_fixups + count >= fixups.Length) {
  413. LabelFixup[] newf = new LabelFixup [fixups.Length + count + 16];
  414. System.Array.Copy (fixups, newf, fixups.Length);
  415. fixups = newf;
  416. }
  417. for (int i = 0; i < count; ++i) {
  418. fixups [num_fixups].size = 4;
  419. fixups [num_fixups].pos = code_len;
  420. fixups [num_fixups].label_base = switch_base;
  421. fixups [num_fixups].label_idx = labels [i].label;
  422. num_fixups++;
  423. code_len += 4;
  424. }
  425. }
  426. public virtual void Emit (OpCode opcode, LocalBuilder lbuilder) {
  427. uint pos = lbuilder.position;
  428. bool load_addr = false;
  429. bool is_store = false;
  430. make_room (6);
  431. if (lbuilder.ilgen != this)
  432. throw new Exception ("Trying to emit a local from a different ILGenerator.");
  433. /* inline the code from ll_emit () to optimize il code size */
  434. if (opcode.StackBehaviourPop == StackBehaviour.Pop1) {
  435. cur_stack --;
  436. is_store = true;
  437. } else {
  438. cur_stack++;
  439. if (cur_stack > max_stack)
  440. max_stack = cur_stack;
  441. load_addr = opcode.StackBehaviourPush == StackBehaviour.Pushi;
  442. }
  443. if (load_addr) {
  444. if (pos < 256) {
  445. code [code_len++] = (byte)0x12;
  446. code [code_len++] = (byte)pos;
  447. } else {
  448. code [code_len++] = (byte)0xfe;
  449. code [code_len++] = (byte)0x0d;
  450. code [code_len++] = (byte)(pos & 0xff);
  451. code [code_len++] = (byte)((pos >> 8) & 0xff);
  452. }
  453. } else {
  454. if (is_store) {
  455. if (pos < 4) {
  456. code [code_len++] = (byte)(0x0a + pos);
  457. } else if (pos < 256) {
  458. code [code_len++] = (byte)0x13;
  459. code [code_len++] = (byte)pos;
  460. } else {
  461. code [code_len++] = (byte)0xfe;
  462. code [code_len++] = (byte)0x0e;
  463. code [code_len++] = (byte)(pos & 0xff);
  464. code [code_len++] = (byte)((pos >> 8) & 0xff);
  465. }
  466. } else {
  467. if (pos < 4) {
  468. code [code_len++] = (byte)(0x06 + pos);
  469. } else if (pos < 256) {
  470. code [code_len++] = (byte)0x11;
  471. code [code_len++] = (byte)pos;
  472. } else {
  473. code [code_len++] = (byte)0xfe;
  474. code [code_len++] = (byte)0x0c;
  475. code [code_len++] = (byte)(pos & 0xff);
  476. code [code_len++] = (byte)((pos >> 8) & 0xff);
  477. }
  478. }
  479. }
  480. }
  481. public virtual void Emit (OpCode opcode, MethodInfo method) {
  482. if (method == null)
  483. throw new ArgumentNullException ("method");
  484. int token = abuilder.GetToken (method);
  485. make_room (6);
  486. ll_emit (opcode);
  487. if (method is MethodBuilder)
  488. add_token_fixup (method);
  489. emit_int (token);
  490. if (method.ReturnType != void_type)
  491. cur_stack ++;
  492. ParameterInfo[] mparams = method.GetParameters();
  493. if (mparams != null)
  494. cur_stack -= mparams.Length;
  495. }
  496. [CLSCompliant(false)]
  497. public void Emit (OpCode opcode, sbyte val) {
  498. make_room (3);
  499. ll_emit (opcode);
  500. code [code_len++] = (byte)val;
  501. }
  502. public virtual void Emit (OpCode opcode, SignatureHelper shelper) {
  503. int token = abuilder.GetToken (shelper);
  504. make_room (6);
  505. ll_emit (opcode);
  506. emit_int (token);
  507. }
  508. public virtual void Emit (OpCode opcode, float val) {
  509. byte[] s = System.BitConverter.GetBytes (val);
  510. make_room (6);
  511. ll_emit (opcode);
  512. if (BitConverter.IsLittleEndian){
  513. System.Array.Copy (s, 0, code, code_len, 4);
  514. code_len += 4;
  515. } else {
  516. code [code_len++] = s [3];
  517. code [code_len++] = s [2];
  518. code [code_len++] = s [1];
  519. code [code_len++] = s [0];
  520. }
  521. }
  522. public virtual void Emit (OpCode opcode, string val) {
  523. int token = abuilder.GetToken (val);
  524. make_room (6);
  525. ll_emit (opcode);
  526. emit_int (token);
  527. }
  528. public virtual void Emit (OpCode opcode, Type type) {
  529. make_room (6);
  530. ll_emit (opcode);
  531. if (type is TypeBuilder)
  532. add_token_fixup (type);
  533. emit_int (abuilder.GetToken (type));
  534. }
  535. public void EmitCall (OpCode opcode, MethodInfo methodinfo, Type[] optionalParamTypes) {
  536. throw new NotImplementedException ();
  537. }
  538. public void EmitCalli (OpCode opcode, CallingConvention unmanagedCallConv, Type returnType, Type[] paramTypes) {
  539. SignatureHelper helper
  540. = SignatureHelper.GetMethodSigHelper (module, 0, unmanagedCallConv, returnType, paramTypes);
  541. Emit (opcode, helper);
  542. }
  543. public void EmitCalli (OpCode opcode, CallingConventions callConv, Type returnType, Type[] paramTypes, Type[] optionalParamTypes) {
  544. if (optionalParamTypes != null)
  545. throw new NotImplementedException ();
  546. SignatureHelper helper
  547. = SignatureHelper.GetMethodSigHelper (module, callConv, 0, returnType, paramTypes);
  548. Emit (opcode, helper);
  549. }
  550. public virtual void EmitWriteLine (FieldInfo field) {
  551. throw new NotImplementedException ();
  552. }
  553. public virtual void EmitWriteLine (LocalBuilder lbuilder) {
  554. throw new NotImplementedException ();
  555. }
  556. public virtual void EmitWriteLine (string val) {
  557. Emit (OpCodes.Ldstr, val);
  558. Emit (OpCodes.Call,
  559. typeof (Console).GetMethod ("WriteLine",
  560. new Type[1] { typeof(string)}));
  561. }
  562. public virtual void EndExceptionBlock () {
  563. if (open_blocks.Count <= 0)
  564. throw new NotSupportedException ("Not in an exception block");
  565. InternalEndClause ();
  566. MarkLabel (ex_handlers [cur_block].end);
  567. ex_handlers [cur_block].End (code_len);
  568. ex_handlers [cur_block].Debug (cur_block);
  569. //System.Console.WriteLine ("End Block {0} (handlers: {1})", cur_block, ex_handlers [cur_block].NumHandlers ());
  570. open_blocks.Pop ();
  571. if (open_blocks.Count > 0)
  572. cur_block = (int)open_blocks.Peek ();
  573. //Console.WriteLine ("curblock restored to {0}", cur_block);
  574. //throw new NotImplementedException ();
  575. }
  576. public virtual void EndScope () {
  577. if (sym_writer != null) {
  578. sym_writer.CloseScope (code_len);
  579. if (scopes == null)
  580. throw new InvalidOperationException ();
  581. scopes.Pop ();
  582. }
  583. }
  584. public virtual void MarkLabel (Label loc) {
  585. if (loc.label < 0 || loc.label >= num_labels)
  586. throw new System.ArgumentException ("The label is not valid");
  587. if (label_to_addr [loc.label] >= 0)
  588. throw new System.ArgumentException ("The label was already defined");
  589. label_to_addr [loc.label] = code_len;
  590. }
  591. public virtual void MarkSequencePoint (ISymbolDocumentWriter document, int startLine,
  592. int startColumn, int endLine, int endColumn) {
  593. if (sym_writer == null)
  594. return;
  595. int[] offsets = { code_len };
  596. int[] startLines = { startLine };
  597. int[] startColumns = { startColumn };
  598. int[] endLines = { endLine };
  599. int[] endColumns = { endColumn };
  600. sym_writer.DefineSequencePoints (document, offsets, startLines, startColumns,
  601. endLines, endColumns);
  602. }
  603. public virtual void ThrowException (Type exceptionType) {
  604. throw new NotImplementedException ();
  605. }
  606. public void UsingNamespace (String usingNamespace) {
  607. throw new NotImplementedException ();
  608. }
  609. internal void label_fixup () {
  610. int i;
  611. for (i = 0; i < num_fixups; ++i) {
  612. int diff = label_to_addr [fixups [i].label_idx] - fixups [i].label_base;
  613. if (fixups [i].size == 1) {
  614. code [fixups [i].pos] = (byte)((sbyte) diff - 1);
  615. } else {
  616. int old_cl = code_len;
  617. code_len = fixups [i].pos;
  618. emit_int (diff - 4);
  619. code_len = old_cl;
  620. }
  621. }
  622. }
  623. }
  624. }