CodeArithmeticOperation.cs 3.9 KB

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