"Fossies" - the Fresh Open Source Software Archive

Member "PowerShell-7.2.6/src/System.Management.Automation/engine/interpreter/AddInstruction.cs" (11 Aug 2022, 10536 Bytes) of package /linux/misc/PowerShell-7.2.6.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C# source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "AddInstruction.cs" see the Fossies "Dox" file reference documentation.

    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 
   16 using System.Diagnostics;
   17 
   18 namespace System.Management.Automation.Interpreter
   19 {
   20     internal abstract class AddInstruction : Instruction
   21     {
   22         private static Instruction s_int16, s_int32, s_int64, s_UInt16, s_UInt32, s_UInt64, s_single, s_double;
   23 
   24         public override int ConsumedStack { get { return 2; } }
   25 
   26         public override int ProducedStack { get { return 1; } }
   27 
   28         private AddInstruction()
   29         {
   30         }
   31 
   32         internal sealed class AddInt32 : AddInstruction
   33         {
   34             public override int Run(InterpretedFrame frame)
   35             {
   36                 object l = frame.Data[frame.StackIndex - 2];
   37                 object r = frame.Data[frame.StackIndex - 1];
   38                 frame.Data[frame.StackIndex - 2] = ScriptingRuntimeHelpers.Int32ToObject(unchecked((Int32)l + (Int32)r));
   39                 frame.StackIndex--;
   40                 return +1;
   41             }
   42         }
   43 
   44         internal sealed class AddInt16 : AddInstruction
   45         {
   46             public override int Run(InterpretedFrame frame)
   47             {
   48                 object l = frame.Data[frame.StackIndex - 2];
   49                 object r = frame.Data[frame.StackIndex - 1];
   50                 frame.Data[frame.StackIndex - 2] = (Int16)unchecked((Int16)l + (Int16)r);
   51                 frame.StackIndex--;
   52                 return +1;
   53             }
   54         }
   55 
   56         internal sealed class AddInt64 : AddInstruction
   57         {
   58             public override int Run(InterpretedFrame frame)
   59             {
   60                 object l = frame.Data[frame.StackIndex - 2];
   61                 object r = frame.Data[frame.StackIndex - 1];
   62                 frame.Data[frame.StackIndex - 2] = (Int64)unchecked((Int64)l + (Int64)r);
   63                 frame.StackIndex--;
   64                 return +1;
   65             }
   66         }
   67 
   68         internal sealed class AddUInt16 : AddInstruction
   69         {
   70             public override int Run(InterpretedFrame frame)
   71             {
   72                 object l = frame.Data[frame.StackIndex - 2];
   73                 object r = frame.Data[frame.StackIndex - 1];
   74                 frame.Data[frame.StackIndex - 2] = (UInt16)unchecked((UInt16)l + (UInt16)r);
   75                 frame.StackIndex--;
   76                 return +1;
   77             }
   78         }
   79 
   80         internal sealed class AddUInt32 : AddInstruction
   81         {
   82             public override int Run(InterpretedFrame frame)
   83             {
   84                 object l = frame.Data[frame.StackIndex - 2];
   85                 object r = frame.Data[frame.StackIndex - 1];
   86                 frame.Data[frame.StackIndex - 2] = (UInt32)unchecked((UInt32)l + (UInt32)r);
   87                 frame.StackIndex--;
   88                 return +1;
   89             }
   90         }
   91 
   92         internal sealed class AddUInt64 : AddInstruction
   93         {
   94             public override int Run(InterpretedFrame frame)
   95             {
   96                 object l = frame.Data[frame.StackIndex - 2];
   97                 object r = frame.Data[frame.StackIndex - 1];
   98                 frame.Data[frame.StackIndex - 2] = (UInt64)unchecked((Int16)l + (Int16)r);
   99                 frame.StackIndex--;
  100                 return +1;
  101             }
  102         }
  103 
  104         internal sealed class AddSingle : AddInstruction
  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] = (Single)((Single)l + (Single)r);
  111                 frame.StackIndex--;
  112                 return +1;
  113             }
  114         }
  115 
  116         internal sealed class AddDouble : AddInstruction
  117         {
  118             public override int Run(InterpretedFrame frame)
  119             {
  120                 object l = frame.Data[frame.StackIndex - 2];
  121                 object r = frame.Data[frame.StackIndex - 1];
  122                 frame.Data[frame.StackIndex - 2] = (double)l + (double)r;
  123                 frame.StackIndex--;
  124                 return +1;
  125             }
  126         }
  127 
  128         public static Instruction Create(Type type)
  129         {
  130             Debug.Assert(!type.IsEnum);
  131             switch (type.GetTypeCode())
  132             {
  133                 case TypeCode.Int16: return s_int16 ??= new AddInt16();
  134                 case TypeCode.Int32: return s_int32 ??= new AddInt32();
  135                 case TypeCode.Int64: return s_int64 ??= new AddInt64();
  136                 case TypeCode.UInt16: return s_UInt16 ??= new AddUInt16();
  137                 case TypeCode.UInt32: return s_UInt32 ??= new AddUInt32();
  138                 case TypeCode.UInt64: return s_UInt64 ??= new AddUInt64();
  139                 case TypeCode.Single: return s_single ??= new AddSingle();
  140                 case TypeCode.Double: return s_double ??= new AddDouble();
  141 
  142                 default:
  143                     throw Assert.Unreachable;
  144             }
  145         }
  146 
  147         public override string ToString()
  148         {
  149             return "Add()";
  150         }
  151     }
  152 
  153     internal abstract class AddOvfInstruction : Instruction
  154     {
  155         private static Instruction s_int16, s_int32, s_int64, s_UInt16, s_UInt32, s_UInt64, s_single, s_double;
  156 
  157         public override int ConsumedStack { get { return 2; } }
  158 
  159         public override int ProducedStack { get { return 1; } }
  160 
  161         private AddOvfInstruction()
  162         {
  163         }
  164 
  165         internal sealed class AddOvfInt32 : AddOvfInstruction
  166         {
  167             public override int Run(InterpretedFrame frame)
  168             {
  169                 object l = frame.Data[frame.StackIndex - 2];
  170                 object r = frame.Data[frame.StackIndex - 1];
  171                 frame.Data[frame.StackIndex - 2] = ScriptingRuntimeHelpers.Int32ToObject(checked((Int32)l + (Int32)r));
  172                 frame.StackIndex--;
  173                 return +1;
  174             }
  175         }
  176 
  177         internal sealed class AddOvfInt16 : AddOvfInstruction
  178         {
  179             public override int Run(InterpretedFrame frame)
  180             {
  181                 object l = frame.Data[frame.StackIndex - 2];
  182                 object r = frame.Data[frame.StackIndex - 1];
  183                 frame.Data[frame.StackIndex - 2] = (Int16)checked((Int16)l + (Int16)r);
  184                 frame.StackIndex--;
  185                 return +1;
  186             }
  187         }
  188 
  189         internal sealed class AddOvfInt64 : AddOvfInstruction
  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] = (Int64)checked((Int64)l + (Int64)r);
  196                 frame.StackIndex--;
  197                 return +1;
  198             }
  199         }
  200 
  201         internal sealed class AddOvfUInt16 : AddOvfInstruction
  202         {
  203             public override int Run(InterpretedFrame frame)
  204             {
  205                 object l = frame.Data[frame.StackIndex - 2];
  206                 object r = frame.Data[frame.StackIndex - 1];
  207                 frame.Data[frame.StackIndex - 2] = (UInt16)checked((UInt16)l + (UInt16)r);
  208                 frame.StackIndex--;
  209                 return +1;
  210             }
  211         }
  212 
  213         internal sealed class AddOvfUInt32 : AddOvfInstruction
  214         {
  215             public override int Run(InterpretedFrame frame)
  216             {
  217                 object l = frame.Data[frame.StackIndex - 2];
  218                 object r = frame.Data[frame.StackIndex - 1];
  219                 frame.Data[frame.StackIndex - 2] = (UInt32)checked((UInt32)l + (UInt32)r);
  220                 frame.StackIndex--;
  221                 return +1;
  222             }
  223         }
  224 
  225         internal sealed class AddOvfUInt64 : AddOvfInstruction
  226         {
  227             public override int Run(InterpretedFrame frame)
  228             {
  229                 object l = frame.Data[frame.StackIndex - 2];
  230                 object r = frame.Data[frame.StackIndex - 1];
  231                 frame.Data[frame.StackIndex - 2] = (UInt64)checked((Int16)l + (Int16)r);
  232                 frame.StackIndex--;
  233                 return +1;
  234             }
  235         }
  236 
  237         internal sealed class AddOvfSingle : AddOvfInstruction
  238         {
  239             public override int Run(InterpretedFrame frame)
  240             {
  241                 object l = frame.Data[frame.StackIndex - 2];
  242                 object r = frame.Data[frame.StackIndex - 1];
  243                 frame.Data[frame.StackIndex - 2] = (Single)((Single)l + (Single)r);
  244                 frame.StackIndex--;
  245                 return +1;
  246             }
  247         }
  248 
  249         internal sealed class AddOvfDouble : AddOvfInstruction
  250         {
  251             public override int Run(InterpretedFrame frame)
  252             {
  253                 object l = frame.Data[frame.StackIndex - 2];
  254                 object r = frame.Data[frame.StackIndex - 1];
  255                 frame.Data[frame.StackIndex - 2] = (double)l + (double)r;
  256                 frame.StackIndex--;
  257                 return +1;
  258             }
  259         }
  260 
  261         public static Instruction Create(Type type)
  262         {
  263             Debug.Assert(!type.IsEnum);
  264             switch (type.GetTypeCode())
  265             {
  266                 case TypeCode.Int16: return s_int16 ??= new AddOvfInt16();
  267                 case TypeCode.Int32: return s_int32 ??= new AddOvfInt32();
  268                 case TypeCode.Int64: return s_int64 ??= new AddOvfInt64();
  269                 case TypeCode.UInt16: return s_UInt16 ??= new AddOvfUInt16();
  270                 case TypeCode.UInt32: return s_UInt32 ??= new AddOvfUInt32();
  271                 case TypeCode.UInt64: return s_UInt64 ??= new AddOvfUInt64();
  272                 case TypeCode.Single: return s_single ??= new AddOvfSingle();
  273                 case TypeCode.Double: return s_double ??= new AddOvfDouble();
  274 
  275                 default:
  276                     throw Assert.Unreachable;
  277             }
  278         }
  279 
  280         public override string ToString()
  281         {
  282             return "AddOvf()";
  283         }
  284     }
  285 }