CodeIncrement.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //
  2. // Permission is hereby granted, free of charge, to any person obtaining
  3. // a copy of this software and associated documentation files (the
  4. // "Software"), to deal in the Software without restriction, including
  5. // without limitation the rights to use, copy, modify, merge, publish,
  6. // distribute, sublicense, and/or sell copies of the Software, and to
  7. // permit persons to whom the Software is furnished to do so, subject to
  8. // the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be
  11. // included in all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  17. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  18. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  19. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. //
  21. // Copyright (C) Lluis Sanchez Gual, 2004
  22. //
  23. #if !FULL_AOT_RUNTIME
  24. using System;
  25. using System.Reflection;
  26. using System.Reflection.Emit;
  27. namespace Mono.CodeGeneration
  28. {
  29. public class CodeIncrement: CodeValueReference
  30. {
  31. CodeValueReference exp;
  32. public CodeIncrement (CodeValueReference exp)
  33. {
  34. this.exp = exp;
  35. }
  36. public override void Generate (ILGenerator gen)
  37. {
  38. exp.GenerateSet (gen, new CodeAddOne (exp));
  39. exp.Generate (gen);
  40. }
  41. public override void GenerateAsStatement (ILGenerator gen)
  42. {
  43. exp.GenerateSet (gen, new CodeAddOne (exp));
  44. }
  45. public override void GenerateSet (ILGenerator gen, CodeExpression value)
  46. {
  47. exp.GenerateSet (gen, value);
  48. }
  49. public override void PrintCode (CodeWriter cp)
  50. {
  51. exp.PrintCode (cp);
  52. cp.Write ("++");
  53. }
  54. public override Type GetResultType ()
  55. {
  56. return exp.GetResultType();
  57. }
  58. }
  59. public class CodeAddOne: CodeExpression
  60. {
  61. CodeValueReference exp;
  62. MethodInfo incMet;
  63. public CodeAddOne (CodeValueReference exp)
  64. {
  65. this.exp = exp;
  66. if (!exp.IsNumber) {
  67. incMet = exp.GetResultType ().GetMethod ("op_Increment");
  68. if (incMet == null)
  69. throw new InvalidOperationException ("Operator '++' cannot be applied to operand of type '" + exp.GetResultType().FullName + "'");
  70. }
  71. }
  72. public override void Generate (ILGenerator gen)
  73. {
  74. if (incMet != null) {
  75. CodeGenerationHelper.GenerateMethodCall (gen, null, incMet, exp);
  76. return;
  77. }
  78. exp.Generate (gen);
  79. Type t = exp.GetResultType ();
  80. switch (Type.GetTypeCode (t))
  81. {
  82. case TypeCode.Byte:
  83. gen.Emit (OpCodes.Ldc_I4_1);
  84. gen.Emit (OpCodes.Add);
  85. gen.Emit (OpCodes.Conv_U1);
  86. break;
  87. case TypeCode.Double:
  88. gen.Emit (OpCodes.Ldc_R8, 1);
  89. gen.Emit (OpCodes.Add);
  90. break;
  91. case TypeCode.Int16:
  92. gen.Emit (OpCodes.Ldc_I4_1);
  93. gen.Emit (OpCodes.Add);
  94. gen.Emit (OpCodes.Conv_I2);
  95. break;
  96. case TypeCode.UInt32:
  97. case TypeCode.Int32:
  98. gen.Emit (OpCodes.Ldc_I4_1);
  99. gen.Emit (OpCodes.Add);
  100. break;
  101. case TypeCode.UInt64:
  102. case TypeCode.Int64:
  103. gen.Emit (OpCodes.Ldc_I4_1);
  104. gen.Emit (OpCodes.Add);
  105. gen.Emit (OpCodes.Conv_U8);
  106. break;
  107. case TypeCode.SByte:
  108. gen.Emit (OpCodes.Ldc_I4_1);
  109. gen.Emit (OpCodes.Add);
  110. gen.Emit (OpCodes.Conv_I1);
  111. break;
  112. case TypeCode.Single:
  113. gen.Emit (OpCodes.Ldc_R4, 1);
  114. gen.Emit (OpCodes.Add);
  115. break;
  116. case TypeCode.UInt16:
  117. gen.Emit (OpCodes.Ldc_I4_1);
  118. gen.Emit (OpCodes.Add);
  119. gen.Emit (OpCodes.Conv_U2);
  120. break;
  121. }
  122. }
  123. public override void PrintCode (CodeWriter cp)
  124. {
  125. exp.PrintCode (cp);
  126. cp.Write (" + 1");
  127. }
  128. public override Type GetResultType ()
  129. {
  130. return exp.GetResultType();
  131. }
  132. }
  133. }
  134. #endif