Merge pull request #4155 from BrzVlad/fix-tls-lmf-addr
[mono-project.git] / mcs / class / dlr / Runtime / Microsoft.Dynamic / Interpreter / Instructions / ArrayOperations.cs
blobd8e0bc6c8319b7b0737907ea67763bbd9f7573e7
1 /* ****************************************************************************
3 * Copyright (c) Microsoft Corporation.
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.
11 * You must not remove this notice, or any other, from this software.
14 * ***************************************************************************/
16 using System;
18 namespace Microsoft.Scripting.Interpreter {
19 public sealed class NewArrayInitInstruction<TElement> : Instruction {
20 private readonly int _elementCount;
22 internal NewArrayInitInstruction(int elementCount) {
23 _elementCount = elementCount;
26 public override int ConsumedStack { get { return _elementCount; } }
27 public override int ProducedStack { get { return 1; } }
29 public override int Run(InterpretedFrame frame) {
30 TElement[] array = new TElement[_elementCount];
31 for (int i = _elementCount - 1; i >= 0; i--) {
32 array[i] = (TElement)frame.Pop();
34 frame.Push(array);
35 return +1;
39 public sealed class NewArrayInstruction<TElement> : Instruction {
40 internal NewArrayInstruction() { }
42 public override int ConsumedStack { get { return 1; } }
43 public override int ProducedStack { get { return 1; } }
45 public override int Run(InterpretedFrame frame) {
46 int length = (int)frame.Pop();
47 frame.Push(new TElement[length]);
48 return +1;
52 public sealed class NewArrayBoundsInstruction : Instruction {
53 private readonly Type _elementType;
54 private readonly int _rank;
56 internal NewArrayBoundsInstruction(Type elementType, int rank) {
57 _elementType = elementType;
58 _rank = rank;
61 public override int ConsumedStack { get { return _rank; } }
62 public override int ProducedStack { get { return 1; } }
64 public override int Run(InterpretedFrame frame) {
65 var lengths = new int[_rank];
66 for (int i = _rank - 1; i >= 0; i--) {
67 lengths[i] = (int)frame.Pop();
69 var array = Array.CreateInstance(_elementType, lengths);
70 frame.Push(array);
71 return +1;
75 public sealed class GetArrayItemInstruction<TElement> : Instruction {
76 internal GetArrayItemInstruction() { }
78 public override int ConsumedStack { get { return 2; } }
79 public override int ProducedStack { get { return 1; } }
81 public override int Run(InterpretedFrame frame) {
82 int index = (int)frame.Pop();
83 TElement[] array = (TElement[])frame.Pop();
84 frame.Push(array[index]);
85 return +1;
88 public override string InstructionName {
89 get { return "GetArrayItem"; }
93 public sealed class GetArrayLengthInstruction : Instruction {
94 private static Instruction instance;
96 private GetArrayLengthInstruction() { }
98 public override int ConsumedStack { get { return 1; } }
99 public override int ProducedStack { get { return 1; } }
101 public override int Run(InterpretedFrame frame) {
102 var array = (Array)frame.Pop();
103 frame.Push(array.Length);
104 return +1;
107 public static Instruction Create() {
108 return instance ?? (instance = new GetArrayLengthInstruction());
111 public override string InstructionName {
112 get { return "GetArrayLength"; }
116 public sealed class SetArrayItemInstruction<TElement> : Instruction {
117 internal SetArrayItemInstruction() { }
119 public override int ConsumedStack { get { return 3; } }
120 public override int ProducedStack { get { return 0; } }
122 public override int Run(InterpretedFrame frame) {
123 TElement value = (TElement)frame.Pop();
124 int index = (int)frame.Pop();
125 TElement[] array = (TElement[])frame.Pop();
126 array[index] = value;
127 return +1;
130 public override string InstructionName {
131 get { return "SetArrayItem"; }