CodeArithmeticOperation.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 CodeArithmeticOperation: CodeExpression
  29. {
  30. protected CodeExpression exp1;
  31. protected CodeExpression exp2;
  32. protected Type t1;
  33. protected Type t2;
  34. protected string symbol;
  35. protected CodeArithmeticOperation ()
  36. {
  37. }
  38. public CodeArithmeticOperation (CodeExpression exp1, CodeExpression exp2, string symbol)
  39. {
  40. this.symbol = symbol;
  41. this.exp1 = exp1;
  42. this.exp2 = exp2;
  43. t1 = exp1.GetResultType ();
  44. t2 = exp2.GetResultType ();
  45. if (!t1.IsPrimitive || !t2.IsPrimitive || (t1 != t2)) {
  46. throw new InvalidOperationException ("Operator " + GetType().Name + " cannot be applied to operands of type '" + t1.Name + " and " + t2.Name);
  47. }
  48. }
  49. public override void PrintCode (CodeWriter cp)
  50. {
  51. exp1.PrintCode (cp);
  52. cp.Write (" " + symbol + " ");
  53. exp2.PrintCode (cp);
  54. }
  55. public override Type GetResultType ()
  56. {
  57. return exp1.GetResultType();
  58. }
  59. }
  60. public class CodeAdd: CodeArithmeticOperation
  61. {
  62. public CodeAdd (CodeExpression exp1, CodeExpression exp2)
  63. {
  64. this.symbol = "+";
  65. this.exp1 = exp1;
  66. this.exp2 = exp2;
  67. t1 = exp1.GetResultType ();
  68. t2 = exp2.GetResultType ();
  69. if ((!t1.IsPrimitive || !t2.IsPrimitive || (t1 != t2)) && (t1 != typeof(string) || t2 != typeof(string))) {
  70. throw new InvalidOperationException ("Operator " + GetType().Name + " cannot be applied to operands of type '" + t1.Name + " and " + t2.Name);
  71. }
  72. }
  73. public override void Generate (ILGenerator gen)
  74. {
  75. if (exp1.GetResultType () == typeof(string)) {
  76. MethodInfo m = typeof(string).GetMethod ("Concat", new Type[] { typeof(string), typeof(string) });
  77. CodeGenerationHelper.GenerateMethodCall (gen, null, m, exp1, exp2);
  78. }
  79. else {
  80. exp1.Generate (gen);
  81. exp2.Generate (gen);
  82. gen.Emit (OpCodes.Add);
  83. }
  84. }
  85. }
  86. public class CodeMul: CodeArithmeticOperation
  87. {
  88. public CodeMul (CodeExpression exp1, CodeExpression exp2)
  89. : base (exp1, exp2, "*")
  90. {
  91. }
  92. public override void Generate (ILGenerator gen)
  93. {
  94. exp1.Generate (gen);
  95. exp2.Generate (gen);
  96. gen.Emit (OpCodes.Mul);
  97. }
  98. }
  99. public class CodeDiv: CodeArithmeticOperation
  100. {
  101. public CodeDiv (CodeExpression exp1, CodeExpression exp2)
  102. : base (exp1, exp2, "*")
  103. {
  104. }
  105. public override void Generate (ILGenerator gen)
  106. {
  107. exp1.Generate (gen);
  108. exp2.Generate (gen);
  109. gen.Emit (OpCodes.Div);
  110. }
  111. }
  112. public class CodeSub: CodeArithmeticOperation
  113. {
  114. public CodeSub (CodeExpression exp1, CodeExpression exp2)
  115. : base (exp1, exp2, "-")
  116. {
  117. }
  118. public override void Generate (ILGenerator gen)
  119. {
  120. exp1.Generate (gen);
  121. exp2.Generate (gen);
  122. gen.Emit (OpCodes.Sub);
  123. }
  124. }
  125. }