CodeBinaryComparison.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. using System;
  24. using System.Reflection;
  25. using System.Reflection.Emit;
  26. namespace Mono.CodeGeneration
  27. {
  28. public abstract class CodeBinaryComparison: CodeConditionExpression
  29. {
  30. protected CodeExpression exp1;
  31. protected CodeExpression exp2;
  32. protected Type t1;
  33. protected Type t2;
  34. string symbol;
  35. public CodeBinaryComparison (CodeExpression exp1, CodeExpression exp2, string symbol)
  36. {
  37. this.symbol = symbol;
  38. this.exp1 = exp1;
  39. this.exp2 = exp2;
  40. t1 = exp1.GetResultType ();
  41. t2 = exp2.GetResultType ();
  42. if (!t1.IsPrimitive || !t2.IsPrimitive || (t1 != t2)) {
  43. throw new InvalidOperationException ("Operator " + GetType().Name + " cannot be applied to operands of type '" + t1.Name + " and " + t2.Name);
  44. }
  45. }
  46. public override void PrintCode (CodeWriter cp)
  47. {
  48. exp1.PrintCode (cp);
  49. cp.Write (" " + symbol + " ");
  50. exp2.PrintCode (cp);
  51. }
  52. public override Type GetResultType ()
  53. {
  54. return typeof(bool);
  55. }
  56. }
  57. public class CodeGreaterThan: CodeBinaryComparison
  58. {
  59. public CodeGreaterThan (CodeExpression exp1, CodeExpression exp2)
  60. : base (exp1, exp2, ">")
  61. {
  62. }
  63. public override void Generate (ILGenerator gen)
  64. {
  65. exp1.Generate (gen);
  66. exp2.Generate (gen);
  67. gen.Emit (OpCodes.Cgt);
  68. }
  69. public override void GenerateForBranch (ILGenerator gen, Label label, bool branchCase)
  70. {
  71. exp1.Generate (gen);
  72. exp2.Generate (gen);
  73. if (branchCase)
  74. gen.Emit (OpCodes.Bgt, label);
  75. else
  76. gen.Emit (OpCodes.Ble, label);
  77. }
  78. }
  79. public class CodeGreaterEqualThan: CodeBinaryComparison
  80. {
  81. public CodeGreaterEqualThan (CodeExpression exp1, CodeExpression exp2)
  82. : base (exp1, exp2, ">=")
  83. {
  84. }
  85. public override void Generate (ILGenerator gen)
  86. {
  87. exp1.Generate (gen);
  88. exp2.Generate (gen);
  89. gen.Emit (OpCodes.Clt);
  90. gen.Emit (OpCodes.Ldc_I4_0);
  91. gen.Emit (OpCodes.Ceq);
  92. }
  93. public override void GenerateForBranch (ILGenerator gen, Label label, bool branchCase)
  94. {
  95. exp1.Generate (gen);
  96. exp2.Generate (gen);
  97. if (branchCase)
  98. gen.Emit (OpCodes.Bge, label);
  99. else
  100. gen.Emit (OpCodes.Blt, label);
  101. }
  102. }
  103. public class CodeLessThan: CodeBinaryComparison
  104. {
  105. public CodeLessThan (CodeExpression exp1, CodeExpression exp2)
  106. : base (exp1, exp2, "<")
  107. {
  108. }
  109. public override void Generate (ILGenerator gen)
  110. {
  111. exp1.Generate (gen);
  112. exp2.Generate (gen);
  113. gen.Emit (OpCodes.Clt);
  114. }
  115. public override void GenerateForBranch (ILGenerator gen, Label label, bool branchCase)
  116. {
  117. exp1.Generate (gen);
  118. exp2.Generate (gen);
  119. if (branchCase)
  120. gen.Emit (OpCodes.Blt, label);
  121. else
  122. gen.Emit (OpCodes.Bge, label);
  123. }
  124. }
  125. public class CodeLessEqualThan: CodeBinaryComparison
  126. {
  127. public CodeLessEqualThan (CodeExpression exp1, CodeExpression exp2)
  128. : base (exp1, exp2, "<=")
  129. {
  130. }
  131. public override void Generate (ILGenerator gen)
  132. {
  133. exp1.Generate (gen);
  134. exp2.Generate (gen);
  135. gen.Emit (OpCodes.Cgt);
  136. gen.Emit (OpCodes.Ldc_I4_0);
  137. gen.Emit (OpCodes.Ceq);
  138. }
  139. public override void GenerateForBranch (ILGenerator gen, Label label, bool branchCase)
  140. {
  141. exp1.Generate (gen);
  142. exp2.Generate (gen);
  143. if (branchCase)
  144. gen.Emit (OpCodes.Ble, label);
  145. else
  146. gen.Emit (OpCodes.Bgt, label);
  147. }
  148. }
  149. }