| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#ifndef jit_StackSlotAllocator_h |
| 8 |
#define jit_StackSlotAllocator_h |
| 9 |
|
| 10 |
#include "jit/LIR.h" |
| 11 |
#include "jit/Registers.h" |
| 12 |
|
| 13 |
namespace js { |
| 14 |
namespace jit { |
| 15 |
|
| 16 |
class StackSlotAllocator { |
| 17 |
js::Vector<uint32_t, 4, SystemAllocPolicy> normalSlots; |
| 18 |
js::Vector<uint32_t, 4, SystemAllocPolicy> doubleSlots; |
| 19 |
js::Vector<uint32_t, 4, SystemAllocPolicy> quadSlots; |
| 20 |
uint32_t height_; |
| 21 |
|
| 22 |
[[nodiscard]] bool incrementHeight(uint32_t amount) { |
| 23 |
// See MaxBytes for why we don't need to check for overflow here. |
| 24 |
if (amount > MaxBytes || height_ + amount > MaxBytes) { |
| 25 |
return false; |
| 26 |
} |
| 27 |
height_ += amount; |
| 28 |
return true; |
| 29 |
} |
| 30 |
|
| 31 |
void freeSlot(uint32_t offset) { (void)normalSlots.append(offset); } |
| 32 |
void freeDoubleSlot(uint32_t offset) { (void)doubleSlots.append(offset); } |
| 33 |
void freeQuadSlot(uint32_t offset) { (void)quadSlots.append(offset); } |
| 34 |
|
| 35 |
[[nodiscard]] bool allocateSlot(uint32_t* slotOffset) { |
| 36 |
// Re-use a normal slot if we have one. |
| 37 |
if (!normalSlots.empty()) { |
| 38 |
*slotOffset = normalSlots.popCopy(); |
| 39 |
return true; |
| 40 |
} |
| 41 |
|
| 42 |
// Otherwise split a double slot in half if we can. |
| 43 |
if (!doubleSlots.empty()) { |
| 44 |
uint32_t doubleSlotOffset = doubleSlots.popCopy(); |
| 45 |
freeSlot(doubleSlotOffset - 4); |
| 46 |
*slotOffset = doubleSlotOffset; |
| 47 |
return true; |
| 48 |
} |
| 49 |
|
| 50 |
// Or else grow the stack by a word. |
| 51 |
if (!incrementHeight(4)) { |
| 52 |
return false; |
| 53 |
} |
| 54 |
*slotOffset = height_; |
| 55 |
return true; |
| 56 |
} |
| 57 |
|
| 58 |
[[nodiscard]] bool allocateDoubleSlot(uint32_t* slotOffset) { |
| 59 |
if (!doubleSlots.empty()) { |
| 60 |
*slotOffset = doubleSlots.popCopy(); |
| 61 |
return true; |
| 62 |
} |
| 63 |
|
| 64 |
// Grow the stack, freeing the padding word for reuse if we need to align. |
| 65 |
if (height_ % 8 != 0) { |
| 66 |
if (!incrementHeight(4)) { |
| 67 |
return false; |
| 68 |
} |
| 69 |
freeSlot(height_); |
| 70 |
} |
| 71 |
if (!incrementHeight(8)) { |
| 72 |
return false; |
| 73 |
} |
| 74 |
*slotOffset = height_; |
| 75 |
return true; |
| 76 |
} |
| 77 |
|
| 78 |
[[nodiscard]] bool allocateQuadSlot(uint32_t* slotOffset) { |
| 79 |
if (!quadSlots.empty()) { |
| 80 |
*slotOffset = quadSlots.popCopy(); |
| 81 |
return true; |
| 82 |
} |
| 83 |
|
| 84 |
// Grow the stack, freeing any padding for reuse if we need to align. This |
| 85 |
// relies on the fact that any architecture specific alignment of the stack |
| 86 |
// pointer is done a priori. |
| 87 |
if (height_ % 8 != 0) { |
| 88 |
if (!incrementHeight(4)) { |
| 89 |
return false; |
| 90 |
} |
| 91 |
freeSlot(height_); |
| 92 |
} |
| 93 |
if (height_ % 16 != 0) { |
| 94 |
if (!incrementHeight(8)) { |
| 95 |
return false; |
| 96 |
} |
| 97 |
freeDoubleSlot(height_); |
| 98 |
} |
| 99 |
if (!incrementHeight(16)) { |
| 100 |
return false; |
| 101 |
} |
| 102 |
*slotOffset = height_; |
| 103 |
return true; |
| 104 |
} |
| 105 |
|
| 106 |
public: |
| 107 |
StackSlotAllocator() : height_(0) {} |
| 108 |
|
| 109 |
// The maximum size we allow. This is a conservative value, even values this |
| 110 |
// low are very likely to hit stack overflows. |
| 111 |
static constexpr size_t MaxBytes = 2 * 1024 * 1024; |
| 112 |
// Check that as long as we keep height_ within MaxBytes and only add by up |
| 113 |
// to MaxBytes, that we cannot overflow. This is used in several places. |
| 114 |
static_assert(uint64_t(MaxBytes) + uint64_t(MaxBytes) <= UINT32_MAX); |
| 115 |
// Any offset we provide can be stored in LStackSlot. |
| 116 |
static_assert(MaxBytes <= LStackSlot::MAX_SLOT); |
| 117 |
|
| 118 |
[[nodiscard]] bool allocateStackArea(LStackArea* alloc) { |
| 119 |
uint32_t size = alloc->size(); |
| 120 |
|
| 121 |
// Check that size is within MaxBytes so that we don't need to check for |
| 122 |
// overflow below when adding to height_. |
| 123 |
if (size > MaxBytes) { |
| 124 |
return false; |
| 125 |
} |
| 126 |
|
| 127 |
MOZ_ASSERT(size % 4 == 0); |
| 128 |
switch (alloc->alignment()) { |
| 129 |
case 8: { |
| 130 |
// Grow the stack, freeing the padding word for reuse if we need to |
| 131 |
// align. |
| 132 |
if ((height_ + size) % 8 != 0) { |
| 133 |
if (!incrementHeight(4)) { |
| 134 |
return false; |
| 135 |
} |
| 136 |
freeSlot(height_); |
| 137 |
} |
| 138 |
break; |
| 139 |
} |
| 140 |
default: |
| 141 |
MOZ_CRASH("unexpected stack results area alignment"); |
| 142 |
} |
| 143 |
|
| 144 |
// See MaxBytes for why we don't need to check for overflow here. |
| 145 |
uint32_t areaSlotOffset = height_ + size; |
| 146 |
if (areaSlotOffset > MaxBytes) { |
| 147 |
return false; |
| 148 |
} |
| 149 |
MOZ_ASSERT(areaSlotOffset % alloc->alignment() == 0); |
| 150 |
|
| 151 |
alloc->setBase(areaSlotOffset); |
| 152 |
height_ = areaSlotOffset; |
| 153 |
return true; |
| 154 |
} |
| 155 |
|
| 156 |
[[nodiscard]] bool allocateSlot(LStackSlot::Width width, |
| 157 |
uint32_t* slotOffset) { |
| 158 |
switch (width) { |
| 159 |
case LStackSlot::Word: |
| 160 |
return allocateSlot(slotOffset); |
| 161 |
case LStackSlot::DoubleWord: |
| 162 |
return allocateDoubleSlot(slotOffset); |
| 163 |
case LStackSlot::QuadWord: |
| 164 |
return allocateQuadSlot(slotOffset); |
| 165 |
} |
| 166 |
MOZ_CRASH("Unknown slot width"); |
| 167 |
} |
| 168 |
|
| 169 |
// This method is used by the Simple allocator to free stack slots so that |
| 170 |
// they can be reused. The Backtracking allocator doesn't call this. |
| 171 |
void freeSlot(LStackSlot::Width width, uint32_t slotOffset) { |
| 172 |
switch (width) { |
| 173 |
case LStackSlot::Word: |
| 174 |
freeSlot(slotOffset); |
| 175 |
return; |
| 176 |
case LStackSlot::DoubleWord: |
| 177 |
freeDoubleSlot(slotOffset); |
| 178 |
return; |
| 179 |
case LStackSlot::QuadWord: |
| 180 |
freeQuadSlot(slotOffset); |
| 181 |
return; |
| 182 |
} |
| 183 |
MOZ_CRASH("Unknown slot width"); |
| 184 |
} |
| 185 |
|
| 186 |
uint32_t stackHeight() const { return height_; } |
| 187 |
}; |
| 188 |
|
| 189 |
} // namespace jit |
| 190 |
} // namespace js |
| 191 |
|
| 192 |
#endif /* jit_StackSlotAllocator_h */ |