Debian Codegraph @

js/src/jit/shared/LIR-shared.h

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 #ifndef jit_shared_LIR_shared_h
8 #define jit_shared_LIR_shared_h
9
10 #include "mozilla/Maybe.h"
11 #include "jit/AtomicOp.h"
12 #include "jit/shared/Assembler-shared.h"
13 #include "util/Memory.h"
14
15 // This file declares LIR instructions that are common to every platform.
16
17 namespace js {
18 namespace jit {
19
20 LIR_OPCODE_CLASS_GENERATED
21
22 template <size_t Temps, size_t ExtraUses = 0>
23 class LBinaryMath : public LInstructionHelper<1, 2 + ExtraUses, Temps> {
24 protected:
25 explicit LBinaryMath(LNode::Opcode opcode)
26 : LInstructionHelper<1, 2 + ExtraUses, Temps>(opcode) {}
27
28 public:
29 const LAllocation* lhs() { return this->getOperand(0); }
30 const LAllocation* rhs() { return this->getOperand(1); }
31 };
32
33 // An LOsiPoint captures a snapshot after a call and ensures enough space to
34 // patch in a call to the invalidation mechanism.
35 //
36 // Note: LSafepoints are 1:1 with LOsiPoints, so it holds a reference to the
37 // corresponding LSafepoint to inform it of the LOsiPoint's masm offset when it
38 // gets GC'd.
39 class LOsiPoint : public LInstructionHelper<0, 0, 0> {
40 LSafepoint* safepoint_;
41
42 public:
43 LOsiPoint(LSafepoint* safepoint, LSnapshot* snapshot)
44 : LInstructionHelper(classOpcode), safepoint_(safepoint) {
45 MOZ_ASSERT(safepoint && snapshot);
46 assignSnapshot(snapshot);
47 }
48
49 LSafepoint* associatedSafepoint() { return safepoint_; }
50
51 LIR_HEADER(OsiPoint)
52 };
53
54 class LMove {
55 LAllocation from_;
56 LAllocation to_;
57 LDefinition::Type type_;
58
59 public:
60 LMove(LAllocation from, LAllocation to, LDefinition::Type type)
61 : from_(from), to_(to), type_(type) {}
62
63 LAllocation from() const { return from_; }
64 LAllocation to() const { return to_; }
65 LDefinition::Type type() const { return type_; }
66 };
67
68 class LMoveGroup : public LInstructionHelper<0, 0, 0> {
69 js::Vector<LMove, 2, JitAllocPolicy> moves_;
70
71 #ifdef JS_CODEGEN_X86
72 // Optional general register available for use when executing moves.
73 LAllocation scratchRegister_;
74 #endif
75
76 explicit LMoveGroup(TempAllocator& alloc)
77 : LInstructionHelper(classOpcode), moves_(alloc) {}
78
79 public:
80 LIR_HEADER(MoveGroup)
81
82 static LMoveGroup* New(TempAllocator& alloc) {
83 return new (alloc) LMoveGroup(alloc);
84 }
85
86 void printOperands(GenericPrinter& out);
87
88 // Add a move which takes place simultaneously with all others in the group.
89 bool add(LAllocation from, LAllocation to, LDefinition::Type type);
90
91 // Add a move which takes place after existing moves in the group.
92 bool addAfter(LAllocation from, LAllocation to, LDefinition::Type type);
93
94 size_t numMoves() const { return moves_.length(); }
95 const LMove& getMove(size_t i) const { return moves_[i]; }
96
97 #ifdef JS_CODEGEN_X86
98 void setScratchRegister(Register reg) { scratchRegister_ = LGeneralReg(reg); }
99 LAllocation maybeScratchRegister() { return scratchRegister_; }
100 #endif
101
102 bool uses(Register reg) {
103 for (size_t i = 0; i < numMoves(); i++) {
104 LMove move = getMove(i);
105 if (move.from() == LGeneralReg(reg) || move.to() == LGeneralReg(reg)) {
106 return true;
107 }
108 }
109 return false;
110 }
111 };
112
113 // A constant Value.
114 class LValue : public LInstructionHelper<BOX_PIECES, 0, 0> {
115 Value v_;
116
117 public:
118 LIR_HEADER(Value)
119
120 explicit LValue(const Value& v) : LInstructionHelper(classOpcode), v_(v) {}
121
122 Value value() const { return v_; }
123 };
124
125 // Allocate a new arguments object for an inlined frame.
126 class LCreateInlinedArgumentsObject : public LVariadicInstruction<1, 2> {
127 public:
128 LIR_HEADER(CreateInlinedArgumentsObject)
129
130 static const size_t CallObj = 0;
131 static const size_t Callee = 1;
132 static const size_t NumNonArgumentOperands = 2;
133 static size_t ArgIndex(size_t i) {
134 return NumNonArgumentOperands + BOX_PIECES * i;
135 }
136
137 LCreateInlinedArgumentsObject(uint32_t numOperands, const LDefinition& temp1,
138 const LDefinition& temp2)
139 : LVariadicInstruction(classOpcode, numOperands) {
140 setIsCall();
141 setTemp(0, temp1);
142 setTemp(1, temp2);
143 }
144
145 const LAllocation* getCallObject() { return getOperand(CallObj); }
146 const LAllocation* getCallee() { return getOperand(Callee); }
147
148 const LDefinition* temp1() { return getTemp(0); }
149 const LDefinition* temp2() { return getTemp(1); }
150
151 MCreateInlinedArgumentsObject* mir() const {
152 return mir_->toCreateInlinedArgumentsObject();
153 }
154 };
155
156 class LGetInlinedArgument : public LVariadicInstruction<BOX_PIECES, 0> {
157 public:
158 LIR_HEADER(GetInlinedArgument)
159
160 static const size_t Index = 0;
161 static const size_t NumNonArgumentOperands = 1;
162 static size_t ArgIndex(size_t i) {
163 return NumNonArgumentOperands + BOX_PIECES * i;
164 }
165
166 explicit LGetInlinedArgument(uint32_t numOperands)
167 : LVariadicInstruction(classOpcode, numOperands) {}
168
169 const LAllocation* getIndex() { return getOperand(Index); }
170
171 MGetInlinedArgument* mir() const { return mir_->toGetInlinedArgument(); }
172 };
173
174 class LGetInlinedArgumentHole : public LVariadicInstruction<BOX_PIECES, 0> {
175 public:
176 LIR_HEADER(GetInlinedArgumentHole)
177
178 static const size_t Index = 0;
179 static const size_t NumNonArgumentOperands = 1;
180 static size_t ArgIndex(size_t i) {
181 return NumNonArgumentOperands + BOX_PIECES * i;
182 }
183
184 explicit LGetInlinedArgumentHole(uint32_t numOperands)
185 : LVariadicInstruction(classOpcode, numOperands) {}
186
187 const LAllocation* getIndex() { return getOperand(Index); }
188
189 MGetInlinedArgumentHole* mir() const {
190 return mir_->toGetInlinedArgumentHole();
191 }
192 };
193
194 class LInlineArgumentsSlice : public LVariadicInstruction<1, 1> {
195 public:
196 LIR_HEADER(InlineArgumentsSlice)
197
198 static const size_t Begin = 0;
199 static const size_t Count = 1;
200 static const size_t NumNonArgumentOperands = 2;
201 static size_t ArgIndex(size_t i) {
202 return NumNonArgumentOperands + BOX_PIECES * i;
203 }
204
205 explicit LInlineArgumentsSlice(uint32_t numOperands, const LDefinition& temp)
206 : LVariadicInstruction(classOpcode, numOperands) {
207 setTemp(0, temp);
208 }
209
210 const LAllocation* begin() { return getOperand(Begin); }
211 const LAllocation* count() { return getOperand(Count); }
212 const LDefinition* temp() { return getTemp(0); }
213
214 MInlineArgumentsSlice* mir() const { return mir_->toInlineArgumentsSlice(); }
215 };
216
217 // Common code for LIR descended from MCall.
218 template <size_t Defs, size_t Operands, size_t Temps>
219 class LJSCallInstructionHelper
220 : public LCallInstructionHelper<Defs, Operands, Temps> {
221 protected:
222 explicit LJSCallInstructionHelper(LNode::Opcode opcode)
223 : LCallInstructionHelper<Defs, Operands, Temps>(opcode) {}
224
225 public:
226 MCall* mir() const { return this->mir_->toCall(); }
227
228 bool hasSingleTarget() const { return getSingleTarget() != nullptr; }
229 WrappedFunction* getSingleTarget() const { return mir()->getSingleTarget(); }
230
231 // Does not include |this|.
232 uint32_t numActualArgs() const { return mir()->numActualArgs(); }
233
234 bool isConstructing() const { return mir()->isConstructing(); }
235 bool ignoresReturnValue() const { return mir()->ignoresReturnValue(); }
236 };
237
238 // Generates a polymorphic callsite, wherein the function being called is
239 // unknown and anticipated to vary.
240 class LCallGeneric : public LJSCallInstructionHelper<BOX_PIECES, 1, 1> {
241 public:
242 LIR_HEADER(CallGeneric)
243
244 LCallGeneric(const LAllocation& callee, const LDefinition& argc)
245 : LJSCallInstructionHelper(classOpcode) {
246 setOperand(0, callee);
247 setTemp(0, argc);
248 }
249
250 const LAllocation* getCallee() { return getOperand(0); }
251 const LDefinition* getArgc() { return getTemp(0); }
252 };
253
254 // Generates a hardcoded callsite for a known, non-native target.
255 class LCallKnown : public LJSCallInstructionHelper<BOX_PIECES, 1, 1> {
256 public:
257 LIR_HEADER(CallKnown)
258
259 LCallKnown(const LAllocation& func, const LDefinition& tmpobjreg)
260 : LJSCallInstructionHelper(classOpcode) {
261 setOperand(0, func);
262 setTemp(0, tmpobjreg);
263 }
264
265 const LAllocation* getFunction() { return getOperand(0); }
266 const LDefinition* getTempObject() { return getTemp(0); }
267 };
268
269 // Generates a hardcoded callsite for a known, native target.
270 class LCallNative : public LJSCallInstructionHelper<BOX_PIECES, 0, 4> {
271 public:
272 LIR_HEADER(CallNative)
273
274 LCallNative(const LDefinition& argContext, const LDefinition& argUintN,
275 const LDefinition& argVp, const LDefinition& tmpreg)
276 : LJSCallInstructionHelper(classOpcode) {
277 // Registers used for callWithABI().
278 setTemp(0, argContext);
279 setTemp(1, argUintN);
280 setTemp(2, argVp);
281
282 // Temporary registers.
283 setTemp(3, tmpreg);
284 }
285
286 const LDefinition* getArgContextReg() { return getTemp(0); }
287 const LDefinition* getArgUintNReg() { return getTemp(1); }
288 const LDefinition* getArgVpReg() { return getTemp(2); }
289 const LDefinition* getTempReg() { return getTemp(3); }
290 };
291
292 class LCallClassHook : public LCallInstructionHelper<BOX_PIECES, 1, 4> {
293 public:
294 LIR_HEADER(CallClassHook)
295
296 LCallClassHook(const LAllocation& callee, const LDefinition& argContext,
297 const LDefinition& argUintN, const LDefinition& argVp,
298 const LDefinition& tmpreg)
299 : LCallInstructionHelper(classOpcode) {
300 setOperand(0, callee);
301
302 // Registers used for callWithABI().
303 setTemp(0, argContext);
304 setTemp(1, argUintN);
305 setTemp(2, argVp);
306
307 // Temporary registers.
308 setTemp(3, tmpreg);
309 }
310
311 MCallClassHook* mir() const { return mir_->toCallClassHook(); }
312
313 const LAllocation* getCallee() { return this->getOperand(0); }
314
315 const LDefinition* getArgContextReg() { return getTemp(0); }
316 const LDefinition* getArgUintNReg() { return getTemp(1); }
317 const LDefinition* getArgVpReg() { return getTemp(2); }
318 const LDefinition* getTempReg() { return getTemp(3); }
319 };
320
321 // Generates a hardcoded callsite for a known, DOM-native target.
322 class LCallDOMNative : public LJSCallInstructionHelper<BOX_PIECES, 0, 4> {
323 public:
324 LIR_HEADER(CallDOMNative)
325
326 LCallDOMNative(const LDefinition& argJSContext, const LDefinition& argObj,
327 const LDefinition& argPrivate, const LDefinition& argArgs)
328 : LJSCallInstructionHelper(classOpcode) {
329 setTemp(0, argJSContext);
330 setTemp(1, argObj);
331 setTemp(2, argPrivate);
332 setTemp(3, argArgs);
333 }
334
335 const LDefinition* getArgJSContext() { return getTemp(0); }
336 const LDefinition* getArgObj() { return getTemp(1); }
337 const LDefinition* getArgPrivate() { return getTemp(2); }
338 const LDefinition* getArgArgs() { return getTemp(3); }
339 };
340
341 // Generates a polymorphic callsite, wherein the function being called is
342 // unknown and anticipated to vary.
343 class LApplyArgsGeneric
344 : public LCallInstructionHelper<BOX_PIECES, BOX_PIECES + 2, 2> {
345 public:
346 LIR_HEADER(ApplyArgsGeneric)
347
348 LApplyArgsGeneric(const LAllocation& func, const LAllocation& argc,
349 const LBoxAllocation& thisv, const LDefinition& tmpObjReg,
350 const LDefinition& tmpCopy)
351 : LCallInstructionHelper(classOpcode) {
352 setOperand(0, func);
353 setOperand(1, argc);
354 setBoxOperand(ThisIndex, thisv);
355 setTemp(0, tmpObjReg);
356 setTemp(1, tmpCopy);
357 }
358
359 MApplyArgs* mir() const { return mir_->toApplyArgs(); }
360
361 bool hasSingleTarget() const { return getSingleTarget() != nullptr; }
362 WrappedFunction* getSingleTarget() const { return mir()->getSingleTarget(); }
363
364 uint32_t numExtraFormals() const { return mir()->numExtraFormals(); }
365
366 const LAllocation* getFunction() { return getOperand(0); }
367 const LAllocation* getArgc() { return getOperand(1); }
368 static const size_t ThisIndex = 2;
369 LBoxAllocation thisValue() const { return getBoxOperand(ThisIndex); }
370
371 const LDefinition* getTempObject() { return getTemp(0); }
372 const LDefinition* getTempForArgCopy() { return getTemp(1); }
373 };
374
375 class LApplyArgsObj
376 : public LCallInstructionHelper<BOX_PIECES, BOX_PIECES + 2, 2> {
377 public:
378 LIR_HEADER(ApplyArgsObj)
379
380 LApplyArgsObj(const LAllocation& func, const LAllocation& argsObj,
381 const LBoxAllocation& thisv, const LDefinition& tmpObjReg,
382 const LDefinition& tmpCopy)
383 : LCallInstructionHelper(classOpcode) {
384 setOperand(0, func);
385 setOperand(1, argsObj);
386 setBoxOperand(ThisIndex, thisv);
387 setTemp(0, tmpObjReg);
388 setTemp(1, tmpCopy);
389 }
390
391 MApplyArgsObj* mir() const { return mir_->toApplyArgsObj(); }
392
393 bool hasSingleTarget() const { return getSingleTarget() != nullptr; }
394 WrappedFunction* getSingleTarget() const { return mir()->getSingleTarget(); }
395
396 const LAllocation* getFunction() { return getOperand(0); }
397 const LAllocation* getArgsObj() { return getOperand(1); }
398 // All registers are calltemps. argc is mapped to the same register as
399 // ArgsObj. argc becomes live as ArgsObj is dying.
400 const LAllocation* getArgc() { return getOperand(1); }
401 LBoxAllocation thisValue() const { return getBoxOperand(ThisIndex); }
402 static const size_t ThisIndex = 2;
403
404 const LDefinition* getTempObject() { return getTemp(0); }
405 const LDefinition* getTempForArgCopy() { return getTemp(1); }
406 };
407
408 class LApplyArrayGeneric
409 : public LCallInstructionHelper<BOX_PIECES, BOX_PIECES + 2, 2> {
410 public:
411 LIR_HEADER(ApplyArrayGeneric)
412
413 LApplyArrayGeneric(const LAllocation& func, const LAllocation& elements,
414 const LBoxAllocation& thisv, const LDefinition& tmpObjReg,
415 const LDefinition& tmpCopy)
416 : LCallInstructionHelper(classOpcode) {
417 setOperand(0, func);
418 setOperand(1, elements);
419 setBoxOperand(ThisIndex, thisv);
420 setTemp(0, tmpObjReg);
421 setTemp(1, tmpCopy);
422 }
423
424 MApplyArray* mir() const { return mir_->toApplyArray(); }
425
426 bool hasSingleTarget() const { return getSingleTarget() != nullptr; }
427 WrappedFunction* getSingleTarget() const { return mir()->getSingleTarget(); }
428
429 const LAllocation* getFunction() { return getOperand(0); }
430 const LAllocation* getElements() { return getOperand(1); }
431 // argc is mapped to the same register as elements: argc becomes
432 // live as elements is dying, all registers are calltemps.
433 const LAllocation* getArgc() { return getOperand(1); }
434 LBoxAllocation thisValue() const { return getBoxOperand(ThisIndex); }
435 static const size_t ThisIndex = 2;
436
437 const LDefinition* getTempObject() { return getTemp(0); }
438 const LDefinition* getTempForArgCopy() { return getTemp(1); }
439 };
440
441 class LConstructArgsGeneric
442 : public LCallInstructionHelper<BOX_PIECES, BOX_PIECES + 3, 1> {
443 public:
444 LIR_HEADER(ConstructArgsGeneric)
445
446 LConstructArgsGeneric(const LAllocation& func, const LAllocation& argc,
447 const LAllocation& newTarget,
448 const LBoxAllocation& thisv,
449 const LDefinition& tmpObjReg)
450 : LCallInstructionHelper(classOpcode) {
451 setOperand(0, func);
452 setOperand(1, argc);
453 setOperand(2, newTarget);
454 setBoxOperand(ThisIndex, thisv);
455 setTemp(0, tmpObjReg);
456 }
457
458 MConstructArgs* mir() const { return mir_->toConstructArgs(); }
459
460 bool hasSingleTarget() const { return getSingleTarget() != nullptr; }
461 WrappedFunction* getSingleTarget() const { return mir()->getSingleTarget(); }
462
463 uint32_t numExtraFormals() const { return mir()->numExtraFormals(); }
464
465 const LAllocation* getFunction() { return getOperand(0); }
466 const LAllocation* getArgc() { return getOperand(1); }
467 const LAllocation* getNewTarget() { return getOperand(2); }
468 LBoxAllocation thisValue() const { return getBoxOperand(ThisIndex); }
469
470 static const size_t ThisIndex = 3;
471
472 const LDefinition* getTempObject() { return getTemp(0); }
473
474 // tempForArgCopy is mapped to the same register as newTarget:
475 // tempForArgCopy becomes live as newTarget is dying, all registers are
476 // calltemps.
477 const LAllocation* getTempForArgCopy() { return getOperand(2); }
478 };
479
480 class LConstructArrayGeneric
481 : public LCallInstructionHelper<BOX_PIECES, BOX_PIECES + 3, 1> {
482 public:
483 LIR_HEADER(ConstructArrayGeneric)
484
485 LConstructArrayGeneric(const LAllocation& func, const LAllocation& elements,
486 const LAllocation& newTarget,
487 const LBoxAllocation& thisv,
488 const LDefinition& tmpObjReg)
489 : LCallInstructionHelper(classOpcode) {
490 setOperand(0, func);
491 setOperand(1, elements);
492 setOperand(2, newTarget);
493 setBoxOperand(ThisIndex, thisv);
494 setTemp(0, tmpObjReg);
495 }
496
497 MConstructArray* mir() const { return mir_->toConstructArray(); }
498
499 bool hasSingleTarget() const { return getSingleTarget() != nullptr; }
500 WrappedFunction* getSingleTarget() const { return mir()->getSingleTarget(); }
501
502 const LAllocation* getFunction() { return getOperand(0); }
503 const LAllocation* getElements() { return getOperand(1); }
504 const LAllocation* getNewTarget() { return getOperand(2); }
505 LBoxAllocation thisValue() const { return getBoxOperand(ThisIndex); }
506
507 static const size_t ThisIndex = 3;
508
509 const LDefinition* getTempObject() { return getTemp(0); }
510
511 // argc is mapped to the same register as elements: argc becomes
512 // live as elements is dying, all registers are calltemps.
513 const LAllocation* getArgc() { return getOperand(1); }
514
515 // tempForArgCopy is mapped to the same register as newTarget:
516 // tempForArgCopy becomes live as newTarget is dying, all registers are
517 // calltemps.
518 const LAllocation* getTempForArgCopy() { return getOperand(2); }
519 };
520
521 class LApplyArgsNative
522 : public LCallInstructionHelper<BOX_PIECES, BOX_PIECES + 1, 3> {
523 public:
524 LIR_HEADER(ApplyArgsNative)
525
526 LApplyArgsNative(const LAllocation& argc, const LBoxAllocation& thisv,
527 const LDefinition& tmpObjReg, const LDefinition& tmpCopy,
528 const LDefinition& tmpExtra)
529 : LCallInstructionHelper(classOpcode) {
530 setOperand(0, argc);
531 setBoxOperand(ThisIndex, thisv);
532 setTemp(0, tmpObjReg);
533 setTemp(1, tmpCopy);
534 setTemp(2, tmpExtra);
535 }
536
537 static constexpr bool isConstructing() { return false; }
538
539 MApplyArgs* mir() const { return mir_->toApplyArgs(); }
540
541 uint32_t numExtraFormals() const { return mir()->numExtraFormals(); }
542
543 const LAllocation* getArgc() { return getOperand(0); }
544 LBoxAllocation thisValue() const { return getBoxOperand(ThisIndex); }
545
546 static const size_t ThisIndex = 1;
547
548 const LDefinition* getTempObject() { return getTemp(0); }
549 const LDefinition* getTempForArgCopy() { return getTemp(1); }
550 const LDefinition* getTempExtra() { return getTemp(2); }
551 };
552
553 class LApplyArgsObjNative
554 : public LCallInstructionHelper<BOX_PIECES, BOX_PIECES + 1, 3> {
555 public:
556 LIR_HEADER(ApplyArgsObjNative)
557
558 LApplyArgsObjNative(const LAllocation& argsObj, const LBoxAllocation& thisv,
559 const LDefinition& tmpObjReg, const LDefinition& tmpCopy,
560 const LDefinition& tmpExtra)
561 : LCallInstructionHelper(classOpcode) {
562 setOperand(0, argsObj);
563 setBoxOperand(ThisIndex, thisv);
564 setTemp(0, tmpObjReg);
565 setTemp(1, tmpCopy);
566 setTemp(2, tmpExtra);
567 }
568
569 static constexpr bool isConstructing() { return false; }
570
571 MApplyArgsObj* mir() const { return mir_->toApplyArgsObj(); }
572
573 const LAllocation* getArgsObj() { return getOperand(0); }
574 LBoxAllocation thisValue() const { return getBoxOperand(ThisIndex); }
575
576 static const size_t ThisIndex = 1;
577
578 const LDefinition* getTempObject() { return getTemp(0); }
579 const LDefinition* getTempForArgCopy() { return getTemp(1); }
580 const LDefinition* getTempExtra() { return getTemp(2); }
581
582 // argc is mapped to the same register as argsObj: argc becomes live as
583 // argsObj is dying, all registers are calltemps.
584 const LAllocation* getArgc() { return getOperand(0); }
585 };
586
587 class LApplyArrayNative
588 : public LCallInstructionHelper<BOX_PIECES, BOX_PIECES + 1, 3> {
589 public:
590 LIR_HEADER(ApplyArrayNative)
591
592 LApplyArrayNative(const LAllocation& elements, const LBoxAllocation& thisv,
593 const LDefinition& tmpObjReg, const LDefinition& tmpCopy,
594 const LDefinition& tmpExtra)
595 : LCallInstructionHelper(classOpcode) {
596 setOperand(0, elements);
597 setBoxOperand(ThisIndex, thisv);
598 setTemp(0, tmpObjReg);
599 setTemp(1, tmpCopy);
600 setTemp(2, tmpExtra);
601 }
602
603 static constexpr bool isConstructing() { return false; }
604
605 MApplyArray* mir() const { return mir_->toApplyArray(); }
606
607 const LAllocation* getElements() { return getOperand(0); }
608 LBoxAllocation thisValue() const { return getBoxOperand(ThisIndex); }
609
610 static const size_t ThisIndex = 1;
611
612 const LDefinition* getTempObject() { return getTemp(0); }
613 const LDefinition* getTempForArgCopy() { return getTemp(1); }
614 const LDefinition* getTempExtra() { return getTemp(2); }
615
616 // argc is mapped to the same register as elements: argc becomes live as
617 // elements is dying, all registers are calltemps.
618 const LAllocation* getArgc() { return getOperand(0); }
619 };
620
621 class LConstructArgsNative : public LCallInstructionHelper<BOX_PIECES, 2, 3> {
622 public:
623 LIR_HEADER(ConstructArgsNative)
624
625 LConstructArgsNative(const LAllocation& argc, const LAllocation& newTarget,
626 const LDefinition& tmpObjReg, const LDefinition& tmpCopy,
627 const LDefinition& tmpExtra)
628 : LCallInstructionHelper(classOpcode) {
629 setOperand(0, argc);
630 setOperand(1, newTarget);
631 setTemp(0, tmpObjReg);
632 setTemp(1, tmpCopy);
633 setTemp(2, tmpExtra);
634 }
635
636 static constexpr bool isConstructing() { return true; }
637
638 MConstructArgs* mir() const { return mir_->toConstructArgs(); }
639
640 uint32_t numExtraFormals() const { return mir()->numExtraFormals(); }
641
642 const LAllocation* getArgc() { return getOperand(0); }
643 const LAllocation* getNewTarget() { return getOperand(1); }
644
645 const LDefinition* getTempObject() { return getTemp(0); }
646 const LDefinition* getTempForArgCopy() { return getTemp(1); }
647 const LDefinition* getTempExtra() { return getTemp(2); }
648 };
649
650 class LConstructArrayNative : public LCallInstructionHelper<BOX_PIECES, 2, 3> {
651 public:
652 LIR_HEADER(ConstructArrayNative)
653
654 LConstructArrayNative(const LAllocation& elements,
655 const LAllocation& newTarget,
656 const LDefinition& tmpObjReg,
657 const LDefinition& tmpCopy, const LDefinition& tmpExtra)
658 : LCallInstructionHelper(classOpcode) {
659 setOperand(0, elements);
660 setOperand(1, newTarget);
661 setTemp(0, tmpObjReg);
662 setTemp(1, tmpCopy);
663 setTemp(2, tmpExtra);
664 }
665
666 static constexpr bool isConstructing() { return true; }
667
668 MConstructArray* mir() const { return mir_->toConstructArray(); }
669
670 const LAllocation* getElements() { return getOperand(0); }
671 const LAllocation* getNewTarget() { return getOperand(1); }
672
673 const LDefinition* getTempObject() { return getTemp(0); }
674 const LDefinition* getTempForArgCopy() { return getTemp(1); }
675 const LDefinition* getTempExtra() { return getTemp(2); }
676
677 // argc is mapped to the same register as elements: argc becomes live as
678 // elements is dying, all registers are calltemps.
679 const LAllocation* getArgc() { return getOperand(0); }
680 };
681
682 // Returns from the function being compiled (not used in inlined frames). The
683 // input must be a box.
684 class LReturn : public LInstructionHelper<0, BOX_PIECES, 0> {
685 bool isGenerator_;
686
687 public:
688 LIR_HEADER(Return)
689
690 explicit LReturn(bool isGenerator)
691 : LInstructionHelper(classOpcode), isGenerator_(isGenerator) {}
692
693 bool isGenerator() { return isGenerator_; }
694 };
695
696 class LHypot : public LCallInstructionHelper<1, 4, 0> {
697 uint32_t numOperands_;
698
699 public:
700 LIR_HEADER(Hypot)
701 LHypot(const LAllocation& x, const LAllocation& y)
702 : LCallInstructionHelper(classOpcode), numOperands_(2) {
703 setOperand(0, x);
704 setOperand(1, y);
705 }
706
707 LHypot(const LAllocation& x, const LAllocation& y, const LAllocation& z)
708 : LCallInstructionHelper(classOpcode), numOperands_(3) {
709 setOperand(0, x);
710 setOperand(1, y);
711 setOperand(2, z);
712 }
713
714 LHypot(const LAllocation& x, const LAllocation& y, const LAllocation& z,
715 const LAllocation& w)
716 : LCallInstructionHelper(classOpcode), numOperands_(4) {
717 setOperand(0, x);
718 setOperand(1, y);
719 setOperand(2, z);
720 setOperand(3, w);
721 }
722
723 uint32_t numArgs() const { return numOperands_; }
724
725 const LAllocation* x() { return getOperand(0); }
726
727 const LAllocation* y() { return getOperand(1); }
728 };
729
730 // Adds two integers, returning an integer value.
731 class LAddI : public LBinaryMath<0> {
732 bool recoversInput_;
733
734 public:
735 LIR_HEADER(AddI)
736
737 LAddI() : LBinaryMath(classOpcode), recoversInput_(false) {}
738
739 const char* extraName() const {
740 return snapshot() ? "OverflowCheck" : nullptr;
741 }
742
743 bool recoversInput() const { return recoversInput_; }
744 void setRecoversInput() { recoversInput_ = true; }
745
746 MAdd* mir() const { return mir_->toAdd(); }
747 };
748
749 // Subtracts two integers, returning an integer value.
750 class LSubI : public LBinaryMath<0> {
751 bool recoversInput_;
752
753 public:
754 LIR_HEADER(SubI)
755
756 LSubI() : LBinaryMath(classOpcode), recoversInput_(false) {}
757
758 const char* extraName() const {
759 return snapshot() ? "OverflowCheck" : nullptr;
760 }
761
762 bool recoversInput() const { return recoversInput_; }
763 void setRecoversInput() { recoversInput_ = true; }
764 MSub* mir() const { return mir_->toSub(); }
765 };
766
767 inline bool LNode::recoversInput() const {
768 switch (op()) {
769 case Opcode::AddI:
770 return toAddI()->recoversInput();
771 case Opcode::SubI:
772 return toSubI()->recoversInput();
773 default:
774 return false;
775 }
776 }
777
778 // Passed the BaselineFrame address in the OsrFrameReg via the IonOsrTempData
779 // populated by PrepareOsrTempData.
780 //
781 // Forwards this object to the LOsrValues for Value materialization.
782 class LOsrEntry : public LInstructionHelper<1, 0, 1> {
783 protected:
784 Label label_;
785 uint32_t frameDepth_;
786
787 public:
788 LIR_HEADER(OsrEntry)
789
790 explicit LOsrEntry(const LDefinition& temp)
791 : LInstructionHelper(classOpcode), frameDepth_(0) {
792 setTemp(0, temp);
793 }
794
795 void setFrameDepth(uint32_t depth) { frameDepth_ = depth; }
796 uint32_t getFrameDepth() { return frameDepth_; }
797 Label* label() { return &label_; }
798 const LDefinition* temp() { return getTemp(0); }
799 };
800
801 // This is used only with LWasmCall.
802 class LWasmCallIndirectAdjunctSafepoint : public LInstructionHelper<0, 0, 0> {
803 CodeOffset offs_;
804 uint32_t framePushedAtStackMapBase_;
805
806 public:
807 LIR_HEADER(WasmCallIndirectAdjunctSafepoint);
808
809 LWasmCallIndirectAdjunctSafepoint()
810 : LInstructionHelper(classOpcode),
811 offs_(0),
812 framePushedAtStackMapBase_(0) {
813 // Ensure that the safepoint does not get live registers associated with it.
814 setIsCall();
815 }
816
817 CodeOffset safepointLocation() const {
818 MOZ_ASSERT(offs_.offset() != 0);
819 return offs_;
820 }
821 uint32_t framePushedAtStackMapBase() const {
822 MOZ_ASSERT(offs_.offset() != 0);
823 return framePushedAtStackMapBase_;
824 }
825 void recordSafepointInfo(CodeOffset offs, uint32_t framePushed) {
826 offs_ = offs;
827 framePushedAtStackMapBase_ = framePushed;
828 }
829 };
830
831 // LWasmCall may be generated into two function calls in the case of
832 // call_indirect, one for the fast path and one for the slow path. In that
833 // case, the node carries a pointer to a companion node, the "adjunct
834 // safepoint", representing the safepoint for the second of the two calls. The
835 // dual-call construction is only meaningful for wasm because wasm has no
836 // invalidation of code; this is not a pattern to be used generally.
837 class LWasmCall : public LVariadicInstruction<0, 0> {
838 bool needsBoundsCheck_;
839 mozilla::Maybe<uint32_t> tableSize_;
840 LWasmCallIndirectAdjunctSafepoint* adjunctSafepoint_;
841
842 public:
843 LIR_HEADER(WasmCall);
844
845 LWasmCall(uint32_t numOperands, bool needsBoundsCheck,
846 mozilla::Maybe<uint32_t> tableSize = mozilla::Nothing())
847 : LVariadicInstruction(classOpcode, numOperands),
848 needsBoundsCheck_(needsBoundsCheck),
849 tableSize_(tableSize),
850 adjunctSafepoint_(nullptr) {
851 this->setIsCall();
852 }
853
854 MWasmCallBase* callBase() const {
855 if (isCatchable() && !isReturnCall()) {
856 return static_cast<MWasmCallBase*>(mirCatchable());
857 }
858 if (isReturnCall()) {
859 return static_cast<MWasmReturnCall*>(mirReturnCall());
860 }
861 return static_cast<MWasmCallBase*>(mirUncatchable());
862 }
863 bool isCatchable() const { return mir_->isWasmCallCatchable(); }
864 bool isReturnCall() const { return mir_->isWasmReturnCall(); }
865 MWasmCallCatchable* mirCatchable() const {
866 return mir_->toWasmCallCatchable();
867 }
868 MWasmCallUncatchable* mirUncatchable() const {
869 return mir_->toWasmCallUncatchable();
870 }
871 MWasmReturnCall* mirReturnCall() const { return mir_->toWasmReturnCall(); }
872
873 static bool isCallPreserved(AnyRegister reg) {
874 // All MWasmCalls preserve the TLS register:
875 // - internal/indirect calls do by the internal wasm ABI
876 // - import calls do by explicitly saving/restoring at the callsite
877 // - builtin calls do because the TLS reg is non-volatile
878 // See also CodeGeneratorShared::emitWasmCall.
879 //
880 // All other registers are not preserved. This is is relied upon by
881 // MWasmCallCatchable which needs all live registers to be spilled before
882 // a call.
883 return !reg.isFloat() && reg.gpr() == InstanceReg;
884 }
885
886 bool needsBoundsCheck() const { return needsBoundsCheck_; }
887 mozilla::Maybe<uint32_t> tableSize() const { return tableSize_; }
888 LWasmCallIndirectAdjunctSafepoint* adjunctSafepoint() const {
889 MOZ_ASSERT(adjunctSafepoint_ != nullptr);
890 return adjunctSafepoint_;
891 }
892 void setAdjunctSafepoint(LWasmCallIndirectAdjunctSafepoint* asp) {
893 adjunctSafepoint_ = asp;
894 }
895 };
896
897 class LWasmRegisterResult : public LInstructionHelper<1, 0, 0> {
898 public:
899 LIR_HEADER(WasmRegisterResult);
900
901 LWasmRegisterResult() : LInstructionHelper(classOpcode) {}
902
903 MWasmRegisterResult* mir() const {
904 if (!mir_->isWasmRegisterResult()) {
905 return nullptr;
906 }
907 return mir_->toWasmRegisterResult();
908 }
909 };
910
911 class LWasmRegisterPairResult : public LInstructionHelper<2, 0, 0> {
912 public:
913 LIR_HEADER(WasmRegisterPairResult);
914
915 LWasmRegisterPairResult() : LInstructionHelper(classOpcode) {}
916
917 MDefinition* mir() const { return mirRaw(); }
918 };
919
920 inline uint32_t LStackArea::base() const {
921 return ins()->toWasmStackResultArea()->mir()->base();
922 }
923 inline void LStackArea::setBase(uint32_t base) {
924 ins()->toWasmStackResultArea()->mir()->setBase(base);
925 }
926 inline uint32_t LStackArea::size() const {
927 return ins()->toWasmStackResultArea()->mir()->byteSize();
928 }
929
930 inline bool LStackArea::ResultIterator::done() const {
931 return idx_ == alloc_.ins()->toWasmStackResultArea()->mir()->resultCount();
932 }
933 inline void LStackArea::ResultIterator::next() {
934 MOZ_ASSERT(!done());
935 idx_++;
936 }
937 inline LAllocation LStackArea::ResultIterator::alloc() const {
938 MOZ_ASSERT(!done());
939 MWasmStackResultArea* area = alloc_.ins()->toWasmStackResultArea()->mir();
940 const auto& stackResult = area->result(idx_);
941 MIRType type = stackResult.type();
942 auto width =
943 #ifndef JS_PUNBOX64
944 type == MIRType::Int64 ? LStackSlot::DoubleWord :
945 #endif
946 LStackSlot::width(LDefinition::TypeFrom(type));
947 return LStackSlot(area->base() - stackResult.offset(), width);
948 }
949 inline bool LStackArea::ResultIterator::isWasmAnyRef() const {
950 MOZ_ASSERT(!done());
951 MWasmStackResultArea* area = alloc_.ins()->toWasmStackResultArea()->mir();
952 MIRType type = area->result(idx_).type();
953 #ifndef JS_PUNBOX64
954 // LDefinition::TypeFrom isn't defined for MIRType::Int64 values on
955 // this platform, so here we have a special case.
956 if (type == MIRType::Int64) {
957 return false;
958 }
959 #endif
960 return LDefinition::TypeFrom(type) == LDefinition::WASM_ANYREF;
961 }
962
963 class LWasmStackResult : public LInstructionHelper<1, 1, 0> {
964 public:
965 LIR_HEADER(WasmStackResult);
966
967 LWasmStackResult() : LInstructionHelper(classOpcode) {}
968
969 MWasmStackResult* mir() const { return mir_->toWasmStackResult(); }
970 LStackSlot result(uint32_t base) const {
971 auto width = LStackSlot::width(LDefinition::TypeFrom(mir()->type()));
972 return LStackSlot(base - mir()->result().offset(), width);
973 }
974 };
975
976 class LWasmStackResult64 : public LInstructionHelper<INT64_PIECES, 1, 0> {
977 public:
978 LIR_HEADER(WasmStackResult64);
979
980 LWasmStackResult64() : LInstructionHelper(classOpcode) {}
981
982 MWasmStackResult* mir() const { return mir_->toWasmStackResult(); }
983 LStackSlot result(uint32_t base, LDefinition* def) {
984 uint32_t offset = base - mir()->result().offset();
985 #if defined(JS_NUNBOX32)
986 if (def == getDef(INT64LOW_INDEX)) {
987 offset -= INT64LOW_OFFSET;
988 } else {
989 MOZ_ASSERT(def == getDef(INT64HIGH_INDEX));
990 offset -= INT64HIGH_OFFSET;
991 }
992 #else
993 MOZ_ASSERT(def == getDef(0));
994 #endif
995 return LStackSlot(offset, LStackSlot::DoubleWord);
996 }
997 };
998
999 inline LStackSlot LStackArea::resultAlloc(LInstruction* lir,
1000 LDefinition* def) const {
1001 if (lir->isWasmStackResult64()) {
1002 return lir->toWasmStackResult64()->result(base(), def);
1003 }
1004 MOZ_ASSERT(def == lir->getDef(0));
1005 return lir->toWasmStackResult()->result(base());
1006 }
1007
1008 inline bool LNode::isCallPreserved(AnyRegister reg) const {
1009 return isWasmCall() && LWasmCall::isCallPreserved(reg);
1010 }
1011
1012 template <size_t NumDefs>
1013 class LIonToWasmCallBase : public LVariadicInstruction<NumDefs, 1> {
1014 using Base = LVariadicInstruction<NumDefs, 1>;
1015
1016 public:
1017 explicit LIonToWasmCallBase(LNode::Opcode classOpcode, uint32_t numOperands,
1018 const LDefinition& temp)
1019 : Base(classOpcode, numOperands) {
1020 this->setIsCall();
1021 this->setTemp(0, temp);
1022 }
1023 MIonToWasmCall* mir() const { return this->mir_->toIonToWasmCall(); }
1024 const LDefinition* temp() { return this->getTemp(0); }
1025 };
1026
1027 class LIonToWasmCall : public LIonToWasmCallBase<1> {
1028 public:
1029 LIR_HEADER(IonToWasmCall);
1030 LIonToWasmCall(uint32_t numOperands, const LDefinition& temp)
1031 : LIonToWasmCallBase<1>(classOpcode, numOperands, temp) {}
1032 };
1033
1034 class LIonToWasmCallV : public LIonToWasmCallBase<BOX_PIECES> {
1035 public:
1036 LIR_HEADER(IonToWasmCallV);
1037 LIonToWasmCallV(uint32_t numOperands, const LDefinition& temp)
1038 : LIonToWasmCallBase<BOX_PIECES>(classOpcode, numOperands, temp) {}
1039 };
1040
1041 class LIonToWasmCallI64 : public LIonToWasmCallBase<INT64_PIECES> {
1042 public:
1043 LIR_HEADER(IonToWasmCallI64);
1044 LIonToWasmCallI64(uint32_t numOperands, const LDefinition& temp)
1045 : LIonToWasmCallBase<INT64_PIECES>(classOpcode, numOperands, temp) {}
1046 };
1047
1048 // Definitions for `extraName` methods of generated LIR instructions.
1049
1050 #ifdef JS_JITSPEW
1051 const char* LBox::extraName() const { return StringFromMIRType(type_); }
1052
1053 const char* LNewArray::extraName() const {
1054 return mir()->isVMCall() ? "VMCall" : nullptr;
1055 }
1056
1057 const char* LNewObject::extraName() const {
1058 return mir()->isVMCall() ? "VMCall" : nullptr;
1059 }
1060
1061 const char* LCompare::extraName() const { return CodeName(jsop_); }
1062
1063 const char* LCompareI64::extraName() const { return CodeName(jsop_); }
1064
1065 const char* LCompareI64AndBranch::extraName() const { return CodeName(jsop_); }
1066
1067 const char* LCompareAndBranch::extraName() const { return CodeName(jsop_); }
1068
1069 const char* LMathFunctionD::extraName() const {
1070 return MMathFunction::FunctionName(mir()->function());
1071 }
1072
1073 const char* LMathFunctionF::extraName() const {
1074 return MMathFunction::FunctionName(mir()->function());
1075 }
1076
1077 const char* LStoreElementV::extraName() const {
1078 return mir()->needsHoleCheck() ? "HoleCheck" : nullptr;
1079 }
1080
1081 const char* LStoreElementT::extraName() const {
1082 return mir()->needsHoleCheck() ? "HoleCheck" : nullptr;
1083 }
1084
1085 const char* LArrayPopShift::extraName() const {
1086 return mir()->mode() == MArrayPopShift::Pop ? "Pop" : "Shift";
1087 }
1088
1089 const char* LMinMaxI::extraName() const {
1090 return mir()->isMax() ? "Max" : "Min";
1091 }
1092
1093 const char* LMinMaxD::extraName() const {
1094 return mir()->isMax() ? "Max" : "Min";
1095 }
1096
1097 const char* LMinMaxF::extraName() const {
1098 return mir()->isMax() ? "Max" : "Min";
1099 }
1100
1101 const char* LMulI::extraName() const {
1102 return (mir()->mode() == MMul::Integer)
1103 ? "Integer"
1104 : (mir()->canBeNegativeZero() ? "CanBeNegativeZero" : nullptr);
1105 }
1106
1107 const char* LDivI::extraName() const {
1108 if (mir()->isTruncated()) {
1109 if (mir()->canBeNegativeZero()) {
1110 return mir()->canBeNegativeOverflow()
1111 ? "Truncate_NegativeZero_NegativeOverflow"
1112 : "Truncate_NegativeZero";
1113 }
1114 return mir()->canBeNegativeOverflow() ? "Truncate_NegativeOverflow"
1115 : "Truncate";
1116 }
1117 if (mir()->canBeNegativeZero()) {
1118 return mir()->canBeNegativeOverflow() ? "NegativeZero_NegativeOverflow"
1119 : "NegativeZero";
1120 }
1121 return mir()->canBeNegativeOverflow() ? "NegativeOverflow" : nullptr;
1122 }
1123
1124 const char* LModI::extraName() const {
1125 return mir()->isTruncated() ? "Truncated" : nullptr;
1126 }
1127
1128 const char* LBitOpI::extraName() const {
1129 if (bitop() == JSOp::Ursh && mir_->toUrsh()->bailoutsDisabled()) {
1130 return "ursh:BailoutsDisabled";
1131 }
1132 return CodeName(bitop_);
1133 }
1134
1135 const char* LBitOpI64::extraName() const { return CodeName(bitop_); }
1136
1137 const char* LShiftI::extraName() const { return CodeName(bitop_); }
1138
1139 const char* LShiftI64::extraName() const { return CodeName(bitop_); }
1140
1141 const char* LMathD::extraName() const { return CodeName(jsop_); }
1142
1143 const char* LMathF::extraName() const { return CodeName(jsop_); }
1144 #endif
1145
1146 } // namespace jit
1147 } // namespace js
1148
1149 #endif /* jit_shared_LIR_shared_h */