Debian Codegraph @

js/src/jit/loong64/MoveEmitter-loong64.cpp

raw
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 #include "jit/loong64/MoveEmitter-loong64.h"
8
9 #include "jit/MacroAssembler-inl.h"
10
11 using namespace js;
12 using namespace js::jit;
13
14 void MoveEmitterLOONG64::breakCycle(const MoveOperand& from,
15 const MoveOperand& to, MoveOp::Type type,
16 uint32_t slotId) {
17 // There is some pattern:
18 // (A -> B)
19 // (B -> A)
20 //
21 // This case handles (A -> B), which we reach first. We save B, then allow
22 // the original move to continue.
23 switch (type) {
24 case MoveOp::FLOAT32:
25 if (to.isMemory()) {
26 ScratchFloat32Scope fpscratch32(masm);
27 masm.loadFloat32(getAdjustedAddress(to), fpscratch32);
28 masm.storeFloat32(fpscratch32, cycleSlot(slotId));
29 } else {
30 masm.storeFloat32(to.floatReg(), cycleSlot(slotId));
31 }
32 break;
33 case MoveOp::DOUBLE:
34 if (to.isMemory()) {
35 ScratchDoubleScope fpscratch64(masm);
36 masm.loadDouble(getAdjustedAddress(to), fpscratch64);
37 masm.storeDouble(fpscratch64, cycleSlot(slotId));
38 } else {
39 masm.storeDouble(to.floatReg(), cycleSlot(slotId));
40 }
41 break;
42 case MoveOp::INT32:
43 if (to.isMemory()) {
44 SecondScratchRegisterScope scratch2(masm);
45 masm.load32(getAdjustedAddress(to), scratch2);
46 masm.store32(scratch2, cycleSlot(0));
47 } else {
48 masm.store32(to.reg(), cycleSlot(0));
49 }
50 break;
51 case MoveOp::GENERAL:
52 if (to.isMemory()) {
53 SecondScratchRegisterScope scratch2(masm);
54 masm.loadPtr(getAdjustedAddress(to), scratch2);
55 masm.storePtr(scratch2, cycleSlot(0));
56 } else {
57 masm.storePtr(to.reg(), cycleSlot(0));
58 }
59 break;
60 default:
61 MOZ_CRASH("Unexpected move type");
62 }
63 }
64
65 void MoveEmitterLOONG64::completeCycle(const MoveOperand& from,
66 const MoveOperand& to, MoveOp::Type type,
67 uint32_t slotId) {
68 // There is some pattern:
69 // (A -> B)
70 // (B -> A)
71 //
72 // This case handles (B -> A), which we reach last. We emit a move from the
73 // saved value of B, to A.
74 switch (type) {
75 case MoveOp::FLOAT32:
76 if (to.isMemory()) {
77 ScratchFloat32Scope fpscratch32(masm);
78 masm.loadFloat32(cycleSlot(slotId), fpscratch32);
79 masm.storeFloat32(fpscratch32, getAdjustedAddress(to));
80 } else {
81 masm.loadFloat32(cycleSlot(slotId), to.floatReg());
82 }
83 break;
84 case MoveOp::DOUBLE:
85 if (to.isMemory()) {
86 ScratchDoubleScope fpscratch64(masm);
87 masm.loadDouble(cycleSlot(slotId), fpscratch64);
88 masm.storeDouble(fpscratch64, getAdjustedAddress(to));
89 } else {
90 masm.loadDouble(cycleSlot(slotId), to.floatReg());
91 }
92 break;
93 case MoveOp::INT32:
94 MOZ_ASSERT(slotId == 0);
95 if (to.isMemory()) {
96 SecondScratchRegisterScope scratch2(masm);
97 masm.load32(cycleSlot(0), scratch2);
98 masm.store32(scratch2, getAdjustedAddress(to));
99 } else {
100 masm.load32(cycleSlot(0), to.reg());
101 }
102 break;
103 case MoveOp::GENERAL:
104 MOZ_ASSERT(slotId == 0);
105 if (to.isMemory()) {
106 SecondScratchRegisterScope scratch2(masm);
107 masm.loadPtr(cycleSlot(0), scratch2);
108 masm.storePtr(scratch2, getAdjustedAddress(to));
109 } else {
110 masm.loadPtr(cycleSlot(0), to.reg());
111 }
112 break;
113 default:
114 MOZ_CRASH("Unexpected move type");
115 }
116 }
117
118 void MoveEmitterLOONG64::emit(const MoveResolver& moves) {
119 if (moves.numCycles()) {
120 // Reserve stack for cycle resolution
121 static_assert(SpillSlotSize == 8);
122 masm.reserveStack(moves.numCycles() * SpillSlotSize);
123 pushedAtCycle_ = masm.framePushed();
124 }
125
126 for (size_t i = 0; i < moves.numMoves(); i++) {
127 emit(moves.getMove(i));
128 }
129 }
130
131 Address MoveEmitterLOONG64::cycleSlot(uint32_t slot, uint32_t subslot) const {
132 int32_t offset = masm.framePushed() - pushedAtCycle_;
133 MOZ_ASSERT(Imm16::IsInSignedRange(offset));
134 return Address(StackPointer, offset + slot * sizeof(double) + subslot);
135 }
136
137 int32_t MoveEmitterLOONG64::getAdjustedOffset(const MoveOperand& operand) {
138 MOZ_ASSERT(operand.isMemoryOrEffectiveAddress());
139 if (operand.base() != StackPointer) {
140 return operand.disp();
141 }
142
143 // Adjust offset if stack pointer has been moved.
144 return operand.disp() + masm.framePushed() - pushedAtStart_;
145 }
146
147 Address MoveEmitterLOONG64::getAdjustedAddress(const MoveOperand& operand) {
148 return Address(operand.base(), getAdjustedOffset(operand));
149 }
150
151 void MoveEmitterLOONG64::emitMove(const MoveOperand& from,
152 const MoveOperand& to) {
153 if (from.isGeneralReg()) {
154 if (to.isGeneralReg()) {
155 masm.movePtr(from.reg(), to.reg());
156 } else if (to.isMemory()) {
157 masm.storePtr(from.reg(), getAdjustedAddress(to));
158 } else {
159 MOZ_CRASH("Invalid emitMove arguments.");
160 }
161 } else if (from.isMemory()) {
162 if (to.isGeneralReg()) {
163 masm.loadPtr(getAdjustedAddress(from), to.reg());
164 } else if (to.isMemory()) {
165 SecondScratchRegisterScope scratch2(masm);
166 masm.loadPtr(getAdjustedAddress(from), scratch2);
167 masm.storePtr(scratch2, getAdjustedAddress(to));
168 } else {
169 MOZ_CRASH("Invalid emitMove arguments.");
170 }
171 } else if (from.isEffectiveAddress()) {
172 if (to.isGeneralReg()) {
173 masm.computeEffectiveAddress(getAdjustedAddress(from), to.reg());
174 } else if (to.isMemory()) {
175 SecondScratchRegisterScope scratch2(masm);
176 masm.computeEffectiveAddress(getAdjustedAddress(from), scratch2);
177 masm.storePtr(scratch2, getAdjustedAddress(to));
178 } else {
179 MOZ_CRASH("Invalid emitMove arguments.");
180 }
181 } else {
182 MOZ_CRASH("Invalid emitMove arguments.");
183 }
184 }
185
186 void MoveEmitterLOONG64::emitInt32Move(const MoveOperand& from,
187 const MoveOperand& to) {
188 if (from.isGeneralReg()) {
189 if (to.isGeneralReg()) {
190 masm.move32(from.reg(), to.reg());
191 } else if (to.isMemory()) {
192 masm.store32(from.reg(), getAdjustedAddress(to));
193 } else {
194 MOZ_CRASH("Invalid emitInt32Move arguments.");
195 }
196 } else if (from.isMemory()) {
197 if (to.isGeneralReg()) {
198 masm.load32(getAdjustedAddress(from), to.reg());
199 } else if (to.isMemory()) {
200 SecondScratchRegisterScope scratch2(masm);
201 masm.load32(getAdjustedAddress(from), scratch2);
202 masm.store32(scratch2, getAdjustedAddress(to));
203 } else {
204 MOZ_CRASH("Invalid emitInt32Move arguments.");
205 }
206 } else if (from.isEffectiveAddress()) {
207 if (to.isGeneralReg()) {
208 masm.computeEffectiveAddress(getAdjustedAddress(from), to.reg());
209 } else if (to.isMemory()) {
210 SecondScratchRegisterScope scratch2(masm);
211 masm.computeEffectiveAddress(getAdjustedAddress(from), scratch2);
212 masm.store32(scratch2, getAdjustedAddress(to));
213 } else {
214 MOZ_CRASH("Invalid emitInt32Move arguments.");
215 }
216 } else {
217 MOZ_CRASH("Invalid emitInt32Move arguments.");
218 }
219 }
220
221 void MoveEmitterLOONG64::emitFloat32Move(const MoveOperand& from,
222 const MoveOperand& to) {
223 if (from.isFloatReg()) {
224 if (to.isFloatReg()) {
225 masm.moveFloat32(from.floatReg(), to.floatReg());
226 } else if (to.isGeneralReg()) {
227 // This should only be used when passing float parameter in a1,a2,a3
228 MOZ_ASSERT(to.reg() == a1 || to.reg() == a2 || to.reg() == a3);
229 masm.moveFromFloat32(from.floatReg(), to.reg());
230 } else {
231 MOZ_ASSERT(to.isMemory());
232 masm.storeFloat32(from.floatReg(), getAdjustedAddress(to));
233 }
234 } else if (to.isFloatReg()) {
235 MOZ_ASSERT(from.isMemory());
236 masm.loadFloat32(getAdjustedAddress(from), to.floatReg());
237 } else if (to.isGeneralReg()) {
238 MOZ_ASSERT(from.isMemory());
239 // This should only be used when passing float parameter in a1,a2,a3
240 MOZ_ASSERT(to.reg() == a1 || to.reg() == a2 || to.reg() == a3);
241 masm.loadPtr(getAdjustedAddress(from), to.reg());
242 } else {
243 MOZ_ASSERT(from.isMemory());
244 MOZ_ASSERT(to.isMemory());
245 ScratchFloat32Scope fpscratch32(masm);
246 masm.loadFloat32(getAdjustedAddress(from), fpscratch32);
247 masm.storeFloat32(fpscratch32, getAdjustedAddress(to));
248 }
249 }
250
251 void MoveEmitterLOONG64::emitDoubleMove(const MoveOperand& from,
252 const MoveOperand& to) {
253 if (from.isFloatReg()) {
254 if (to.isFloatReg()) {
255 masm.moveDouble(from.floatReg(), to.floatReg());
256 } else if (to.isGeneralReg()) {
257 masm.moveFromDouble(from.floatReg(), to.reg());
258 } else {
259 MOZ_ASSERT(to.isMemory());
260 masm.storeDouble(from.floatReg(), getAdjustedAddress(to));
261 }
262 } else if (to.isFloatReg()) {
263 if (from.isMemory()) {
264 masm.loadDouble(getAdjustedAddress(from), to.floatReg());
265 } else {
266 masm.moveToDouble(from.reg(), to.floatReg());
267 }
268 } else {
269 MOZ_ASSERT(from.isMemory());
270 MOZ_ASSERT(to.isMemory());
271 ScratchDoubleScope fpscratch64(masm);
272 masm.loadDouble(getAdjustedAddress(from), fpscratch64);
273 masm.storeDouble(fpscratch64, getAdjustedAddress(to));
274 }
275 }
276
277 void MoveEmitterLOONG64::emit(const MoveOp& move) {
278 const MoveOperand& from = move.from();
279 const MoveOperand& to = move.to();
280
281 if (move.isCycleEnd() && move.isCycleBegin()) {
282 // A fun consequence of aliased registers is you can have multiple
283 // cycles at once, and one can end exactly where another begins.
284 breakCycle(from, to, move.endCycleType(), move.cycleBeginSlot());
285 completeCycle(from, to, move.type(), move.cycleEndSlot());
286 return;
287 }
288
289 if (move.isCycleEnd()) {
290 MOZ_ASSERT(inCycle_);
291 completeCycle(from, to, move.type(), move.cycleEndSlot());
292 MOZ_ASSERT(inCycle_ > 0);
293 inCycle_--;
294 return;
295 }
296
297 if (move.isCycleBegin()) {
298 breakCycle(from, to, move.endCycleType(), move.cycleBeginSlot());
299 inCycle_++;
300 }
301
302 switch (move.type()) {
303 case MoveOp::FLOAT32:
304 emitFloat32Move(from, to);
305 break;
306 case MoveOp::DOUBLE:
307 emitDoubleMove(from, to);
308 break;
309 case MoveOp::INT32:
310 emitInt32Move(from, to);
311 break;
312 case MoveOp::GENERAL:
313 emitMove(from, to);
314 break;
315 default:
316 MOZ_CRASH("Unexpected move type");
317 }
318 }
319
320 void MoveEmitterLOONG64::assertDone() { MOZ_ASSERT(inCycle_ == 0); }
321
322 void MoveEmitterLOONG64::finish() {
323 assertDone();
324
325 masm.freeStack(masm.framePushed() - pushedAtStart_);
326 }