CodeBinaryComparison.cs 4.3 KB

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