/src/System.Management.Automation/engine/interpreter/SubInstruction.cs

https://github.com/cobbr/InsecurePowerShell · C# · 284 lines · 241 code · 29 blank · 14 comment · 2 complexity · e16afde5fed796dbb0e5441eeded53c1 MD5 · raw file

  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * This source code is subject to terms and conditions of the Apache License, Version 2.0. A
  6. * copy of the license can be found in the License.html file at the root of this distribution. If
  7. * you cannot locate the Apache License, Version 2.0, please send an email to
  8. * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. * by the terms of the Apache License, Version 2.0.
  10. *
  11. * You must not remove this notice, or any other, from this software.
  12. *
  13. *
  14. * ***************************************************************************/
  15. using System.Diagnostics;
  16. using System.Reflection;
  17. namespace System.Management.Automation.Interpreter
  18. {
  19. internal abstract class SubInstruction : Instruction
  20. {
  21. private static Instruction s_int16,s_int32,s_int64,s_UInt16,s_UInt32,s_UInt64,s_single,s_double;
  22. public override int ConsumedStack { get { return 2; } }
  23. public override int ProducedStack { get { return 1; } }
  24. private SubInstruction()
  25. {
  26. }
  27. internal sealed class SubInt32 : SubInstruction
  28. {
  29. public override int Run(InterpretedFrame frame)
  30. {
  31. object l = frame.Data[frame.StackIndex - 2];
  32. object r = frame.Data[frame.StackIndex - 1];
  33. frame.Data[frame.StackIndex - 2] = ScriptingRuntimeHelpers.Int32ToObject(unchecked((Int32)l - (Int32)r));
  34. frame.StackIndex--;
  35. return +1;
  36. }
  37. }
  38. internal sealed class SubInt16 : SubInstruction
  39. {
  40. public override int Run(InterpretedFrame frame)
  41. {
  42. object l = frame.Data[frame.StackIndex - 2];
  43. object r = frame.Data[frame.StackIndex - 1];
  44. frame.Data[frame.StackIndex - 2] = (Int16)unchecked((Int16)l - (Int16)r);
  45. frame.StackIndex--;
  46. return +1;
  47. }
  48. }
  49. internal sealed class SubInt64 : SubInstruction
  50. {
  51. public override int Run(InterpretedFrame frame)
  52. {
  53. object l = frame.Data[frame.StackIndex - 2];
  54. object r = frame.Data[frame.StackIndex - 1];
  55. frame.Data[frame.StackIndex - 2] = (Int64)unchecked((Int64)l - (Int64)r);
  56. frame.StackIndex--;
  57. return +1;
  58. }
  59. }
  60. internal sealed class SubUInt16 : SubInstruction
  61. {
  62. public override int Run(InterpretedFrame frame)
  63. {
  64. object l = frame.Data[frame.StackIndex - 2];
  65. object r = frame.Data[frame.StackIndex - 1];
  66. frame.Data[frame.StackIndex - 2] = (UInt16)unchecked((UInt16)l - (UInt16)r);
  67. frame.StackIndex--;
  68. return +1;
  69. }
  70. }
  71. internal sealed class SubUInt32 : SubInstruction
  72. {
  73. public override int Run(InterpretedFrame frame)
  74. {
  75. object l = frame.Data[frame.StackIndex - 2];
  76. object r = frame.Data[frame.StackIndex - 1];
  77. frame.Data[frame.StackIndex - 2] = (UInt32)unchecked((UInt32)l - (UInt32)r);
  78. frame.StackIndex--;
  79. return +1;
  80. }
  81. }
  82. internal sealed class SubUInt64 : SubInstruction
  83. {
  84. public override int Run(InterpretedFrame frame)
  85. {
  86. object l = frame.Data[frame.StackIndex - 2];
  87. object r = frame.Data[frame.StackIndex - 1];
  88. frame.Data[frame.StackIndex - 2] = (UInt64)unchecked((Int16)l - (Int16)r);
  89. frame.StackIndex--;
  90. return +1;
  91. }
  92. }
  93. internal sealed class SubSingle : SubInstruction
  94. {
  95. public override int Run(InterpretedFrame frame)
  96. {
  97. object l = frame.Data[frame.StackIndex - 2];
  98. object r = frame.Data[frame.StackIndex - 1];
  99. frame.Data[frame.StackIndex - 2] = (Single)((Single)l - (Single)r);
  100. frame.StackIndex--;
  101. return +1;
  102. }
  103. }
  104. internal sealed class SubDouble : SubInstruction
  105. {
  106. public override int Run(InterpretedFrame frame)
  107. {
  108. object l = frame.Data[frame.StackIndex - 2];
  109. object r = frame.Data[frame.StackIndex - 1];
  110. frame.Data[frame.StackIndex - 2] = (Double)l - (Double)r;
  111. frame.StackIndex--;
  112. return +1;
  113. }
  114. }
  115. public static Instruction Create(Type type)
  116. {
  117. Debug.Assert(!type.GetTypeInfo().IsEnum);
  118. switch (type.GetTypeCode())
  119. {
  120. case TypeCode.Int16: return s_int16 ?? (s_int16 = new SubInt16());
  121. case TypeCode.Int32: return s_int32 ?? (s_int32 = new SubInt32());
  122. case TypeCode.Int64: return s_int64 ?? (s_int64 = new SubInt64());
  123. case TypeCode.UInt16: return s_UInt16 ?? (s_UInt16 = new SubUInt16());
  124. case TypeCode.UInt32: return s_UInt32 ?? (s_UInt32 = new SubUInt32());
  125. case TypeCode.UInt64: return s_UInt64 ?? (s_UInt64 = new SubUInt64());
  126. case TypeCode.Single: return s_single ?? (s_single = new SubSingle());
  127. case TypeCode.Double: return s_double ?? (s_double = new SubDouble());
  128. default:
  129. throw Assert.Unreachable;
  130. }
  131. }
  132. public override string ToString()
  133. {
  134. return "Sub()";
  135. }
  136. }
  137. internal abstract class SubOvfInstruction : Instruction
  138. {
  139. private static Instruction s_int16,s_int32,s_int64,s_UInt16,s_UInt32,s_UInt64,s_single,s_double;
  140. public override int ConsumedStack { get { return 2; } }
  141. public override int ProducedStack { get { return 1; } }
  142. private SubOvfInstruction()
  143. {
  144. }
  145. internal sealed class SubOvfInt32 : SubOvfInstruction
  146. {
  147. public override int Run(InterpretedFrame frame)
  148. {
  149. object l = frame.Data[frame.StackIndex - 2];
  150. object r = frame.Data[frame.StackIndex - 1];
  151. frame.Data[frame.StackIndex - 2] = ScriptingRuntimeHelpers.Int32ToObject(checked((Int32)l - (Int32)r));
  152. frame.StackIndex--;
  153. return +1;
  154. }
  155. }
  156. internal sealed class SubOvfInt16 : SubOvfInstruction
  157. {
  158. public override int Run(InterpretedFrame frame)
  159. {
  160. object l = frame.Data[frame.StackIndex - 2];
  161. object r = frame.Data[frame.StackIndex - 1];
  162. frame.Data[frame.StackIndex - 2] = (Int16)checked((Int16)l - (Int16)r);
  163. frame.StackIndex--;
  164. return +1;
  165. }
  166. }
  167. internal sealed class SubOvfInt64 : SubOvfInstruction
  168. {
  169. public override int Run(InterpretedFrame frame)
  170. {
  171. object l = frame.Data[frame.StackIndex - 2];
  172. object r = frame.Data[frame.StackIndex - 1];
  173. frame.Data[frame.StackIndex - 2] = (Int64)checked((Int64)l - (Int64)r);
  174. frame.StackIndex--;
  175. return +1;
  176. }
  177. }
  178. internal sealed class SubOvfUInt16 : SubOvfInstruction
  179. {
  180. public override int Run(InterpretedFrame frame)
  181. {
  182. object l = frame.Data[frame.StackIndex - 2];
  183. object r = frame.Data[frame.StackIndex - 1];
  184. frame.Data[frame.StackIndex - 2] = (UInt16)checked((UInt16)l - (UInt16)r);
  185. frame.StackIndex--;
  186. return +1;
  187. }
  188. }
  189. internal sealed class SubOvfUInt32 : SubOvfInstruction
  190. {
  191. public override int Run(InterpretedFrame frame)
  192. {
  193. object l = frame.Data[frame.StackIndex - 2];
  194. object r = frame.Data[frame.StackIndex - 1];
  195. frame.Data[frame.StackIndex - 2] = (UInt32)checked((UInt32)l - (UInt32)r);
  196. frame.StackIndex--;
  197. return +1;
  198. }
  199. }
  200. internal sealed class SubOvfUInt64 : SubOvfInstruction
  201. {
  202. public override int Run(InterpretedFrame frame)
  203. {
  204. object l = frame.Data[frame.StackIndex - 2];
  205. object r = frame.Data[frame.StackIndex - 1];
  206. frame.Data[frame.StackIndex - 2] = (UInt64)checked((Int16)l - (Int16)r);
  207. frame.StackIndex--;
  208. return +1;
  209. }
  210. }
  211. internal sealed class SubOvfSingle : SubOvfInstruction
  212. {
  213. public override int Run(InterpretedFrame frame)
  214. {
  215. object l = frame.Data[frame.StackIndex - 2];
  216. object r = frame.Data[frame.StackIndex - 1];
  217. frame.Data[frame.StackIndex - 2] = (Single)((Single)l - (Single)r);
  218. frame.StackIndex--;
  219. return +1;
  220. }
  221. }
  222. internal sealed class SubOvfDouble : SubOvfInstruction
  223. {
  224. public override int Run(InterpretedFrame frame)
  225. {
  226. object l = frame.Data[frame.StackIndex - 2];
  227. object r = frame.Data[frame.StackIndex - 1];
  228. frame.Data[frame.StackIndex - 2] = (Double)l - (Double)r;
  229. frame.StackIndex--;
  230. return +1;
  231. }
  232. }
  233. public static Instruction Create(Type type)
  234. {
  235. Debug.Assert(!type.GetTypeInfo().IsEnum);
  236. switch (type.GetTypeCode())
  237. {
  238. case TypeCode.Int16: return s_int16 ?? (s_int16 = new SubOvfInt16());
  239. case TypeCode.Int32: return s_int32 ?? (s_int32 = new SubOvfInt32());
  240. case TypeCode.Int64: return s_int64 ?? (s_int64 = new SubOvfInt64());
  241. case TypeCode.UInt16: return s_UInt16 ?? (s_UInt16 = new SubOvfUInt16());
  242. case TypeCode.UInt32: return s_UInt32 ?? (s_UInt32 = new SubOvfUInt32());
  243. case TypeCode.UInt64: return s_UInt64 ?? (s_UInt64 = new SubOvfUInt64());
  244. case TypeCode.Single: return s_single ?? (s_single = new SubOvfSingle());
  245. case TypeCode.Double: return s_double ?? (s_double = new SubOvfDouble());
  246. default:
  247. throw Assert.Unreachable;
  248. }
  249. }
  250. public override string ToString()
  251. {
  252. return "SubOvf()";
  253. }
  254. }
  255. }