1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
6 // ArrayStack: A stack, implemented as a growable array
11 static const int builtinSize = 8;
14 ArrayStack(Compiler* comp, int initialSize = builtinSize)
18 if (initialSize > builtinSize)
20 maxIndex = initialSize;
21 data = new (compiler, CMK_ArrayStack) T[initialSize];
25 maxIndex = builtinSize;
34 if (tosIndex == maxIndex)
39 data[tosIndex] = item;
45 // get a new chunk 2x the size of the old one
48 noway_assert(maxIndex * 2 > maxIndex);
49 data = new (compiler, CMK_ArrayStack) T[maxIndex * 2];
50 for (int i = 0; i < maxIndex; i++)
57 // reverse the top N in the stack
58 void ReverseTop(int number)
65 assert(number <= tosIndex);
67 int start = tosIndex - number;
69 while (offset < number / 2)
72 int index = start + offset;
73 int otherIndex = tosIndex - 1 - offset;
75 data[index] = data[otherIndex];
76 data[otherIndex] = temp;
86 return data[tosIndex];
92 return data[tosIndex - 1];
98 return data[tosIndex - 1];
101 // return the i'th from the top
104 assert(tosIndex > idx);
105 return data[tosIndex - 1 - idx];
108 // return a reference to the i'th from the top
111 assert(tosIndex > idx);
112 return data[tosIndex - 1 - idx];
120 // return the bottom of the stack
123 assert(tosIndex > 0);
127 // return the i'th from the bottom
130 assert(tosIndex > indx);
140 Compiler* compiler; // needed for allocation
141 int tosIndex; // first free location
144 // initial allocation
145 T builtinData[builtinSize];