|
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
|
2 * vim: set ts=8 sts=4 et sw=4 tw=99: |
|
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/Registers.h" |
|
11 |
|
12 namespace js { |
|
13 namespace jit { |
|
14 |
|
15 class StackSlotAllocator |
|
16 { |
|
17 js::Vector<uint32_t, 4, SystemAllocPolicy> normalSlots; |
|
18 js::Vector<uint32_t, 4, SystemAllocPolicy> doubleSlots; |
|
19 uint32_t height_; |
|
20 |
|
21 void freeSlot(uint32_t index) { |
|
22 normalSlots.append(index); |
|
23 } |
|
24 void freeDoubleSlot(uint32_t index) { |
|
25 doubleSlots.append(index); |
|
26 } |
|
27 |
|
28 uint32_t allocateDoubleSlot() { |
|
29 if (!doubleSlots.empty()) |
|
30 return doubleSlots.popCopy(); |
|
31 if (height_ % 8 != 0) |
|
32 normalSlots.append(height_ += 4); |
|
33 return height_ += 8; |
|
34 } |
|
35 uint32_t allocateSlot() { |
|
36 if (!normalSlots.empty()) |
|
37 return normalSlots.popCopy(); |
|
38 if (!doubleSlots.empty()) { |
|
39 uint32_t index = doubleSlots.popCopy(); |
|
40 normalSlots.append(index - 4); |
|
41 return index; |
|
42 } |
|
43 return height_ += 4; |
|
44 } |
|
45 |
|
46 public: |
|
47 StackSlotAllocator() : height_(0) |
|
48 { } |
|
49 |
|
50 void freeSlot(LDefinition::Type type, uint32_t index) { |
|
51 switch (type) { |
|
52 #if JS_BITS_PER_WORD == 32 |
|
53 case LDefinition::GENERAL: |
|
54 case LDefinition::OBJECT: |
|
55 case LDefinition::SLOTS: |
|
56 #endif |
|
57 case LDefinition::INT32: |
|
58 case LDefinition::FLOAT32: return freeSlot(index); |
|
59 #if JS_BITS_PER_WORD == 64 |
|
60 case LDefinition::GENERAL: |
|
61 case LDefinition::OBJECT: |
|
62 case LDefinition::SLOTS: |
|
63 #endif |
|
64 #ifdef JS_PUNBOX64 |
|
65 case LDefinition::BOX: |
|
66 #endif |
|
67 #ifdef JS_NUNBOX32 |
|
68 case LDefinition::TYPE: |
|
69 case LDefinition::PAYLOAD: |
|
70 #endif |
|
71 case LDefinition::DOUBLE: return freeDoubleSlot(index); |
|
72 default: MOZ_ASSUME_UNREACHABLE("Unknown slot type"); |
|
73 } |
|
74 } |
|
75 |
|
76 uint32_t allocateSlot(LDefinition::Type type) { |
|
77 switch (type) { |
|
78 #if JS_BITS_PER_WORD == 32 |
|
79 case LDefinition::GENERAL: |
|
80 case LDefinition::OBJECT: |
|
81 case LDefinition::SLOTS: |
|
82 #endif |
|
83 case LDefinition::INT32: |
|
84 case LDefinition::FLOAT32: return allocateSlot(); |
|
85 #if JS_BITS_PER_WORD == 64 |
|
86 case LDefinition::GENERAL: |
|
87 case LDefinition::OBJECT: |
|
88 case LDefinition::SLOTS: |
|
89 #endif |
|
90 #ifdef JS_PUNBOX64 |
|
91 case LDefinition::BOX: |
|
92 #endif |
|
93 #ifdef JS_NUNBOX32 |
|
94 case LDefinition::TYPE: |
|
95 case LDefinition::PAYLOAD: |
|
96 #endif |
|
97 case LDefinition::DOUBLE: return allocateDoubleSlot(); |
|
98 default: MOZ_ASSUME_UNREACHABLE("Unknown slot type"); |
|
99 } |
|
100 } |
|
101 |
|
102 uint32_t stackHeight() const { |
|
103 return height_; |
|
104 } |
|
105 }; |
|
106 |
|
107 } // namespace jit |
|
108 } // namespace js |
|
109 |
|
110 #endif /* jit_StackSlotAllocator_h */ |