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:
10 // The above copyright notice and this permission notice shall be
11 // included in all copies or substantial portions of the Software.
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.
21 // Copyright (C) Lluis Sanchez Gual, 2004
26 using System.Reflection;
27 using System.Reflection.Emit;
29 namespace Mono.CodeGeneration
31 public abstract class CodeBinaryComparison: CodeConditionExpression
33 protected CodeExpression exp1;
34 protected CodeExpression exp2;
39 public CodeBinaryComparison (CodeExpression exp1, CodeExpression exp2, string symbol)
45 t1 = exp1.GetResultType ();
46 t2 = exp2.GetResultType ();
48 if (!t1.IsPrimitive || !t2.IsPrimitive || (t1 != t2)) {
49 throw new InvalidOperationException ("Operator " + GetType().Name + " cannot be applied to operands of type '" + t1.Name + " and " + t2.Name);
53 public override void PrintCode (CodeWriter cp)
56 cp.Write (" " + symbol + " ");
60 public override Type GetResultType ()
66 public class CodeGreaterThan: CodeBinaryComparison
68 public CodeGreaterThan (CodeExpression exp1, CodeExpression exp2)
69 : base (exp1, exp2, ">")
73 public override void Generate (ILGenerator gen)
77 gen.Emit (OpCodes.Cgt);
80 public override void GenerateForBranch (ILGenerator gen, Label label, bool branchCase)
85 gen.Emit (OpCodes.Bgt, label);
87 gen.Emit (OpCodes.Ble, label);
91 public class CodeGreaterEqualThan: CodeBinaryComparison
93 public CodeGreaterEqualThan (CodeExpression exp1, CodeExpression exp2)
94 : base (exp1, exp2, ">=")
98 public override void Generate (ILGenerator gen)
102 gen.Emit (OpCodes.Clt);
103 gen.Emit (OpCodes.Ldc_I4_0);
104 gen.Emit (OpCodes.Ceq);
107 public override void GenerateForBranch (ILGenerator gen, Label label, bool branchCase)
112 gen.Emit (OpCodes.Bge, label);
114 gen.Emit (OpCodes.Blt, label);
118 public class CodeLessThan: CodeBinaryComparison
120 public CodeLessThan (CodeExpression exp1, CodeExpression exp2)
121 : base (exp1, exp2, "<")
125 public override void Generate (ILGenerator gen)
129 gen.Emit (OpCodes.Clt);
132 public override void GenerateForBranch (ILGenerator gen, Label label, bool branchCase)
137 gen.Emit (OpCodes.Blt, label);
139 gen.Emit (OpCodes.Bge, label);
143 public class CodeLessEqualThan: CodeBinaryComparison
145 public CodeLessEqualThan (CodeExpression exp1, CodeExpression exp2)
146 : base (exp1, exp2, "<=")
150 public override void Generate (ILGenerator gen)
154 gen.Emit (OpCodes.Cgt);
155 gen.Emit (OpCodes.Ldc_I4_0);
156 gen.Emit (OpCodes.Ceq);
159 public override void GenerateForBranch (ILGenerator gen, Label label, bool branchCase)
164 gen.Emit (OpCodes.Ble, label);
166 gen.Emit (OpCodes.Bgt, label);