Repository navigation
Expand file tree
/
Copy pathCCallHelpers.h
More file actions
722 lines (614 loc) · 36.7 KB
/
Copy pathCCallHelpers.h
File metadata and controls
722 lines (614 loc) · 36.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
/*
* Copyright (C) 2011-2020 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <wtf/Platform.h>
#if ENABLE(JIT)
#include <JavaScriptCore/AssemblyHelpers.h>
#include <JavaScriptCore/DisallowMacroScratchRegisterUsage.h>
#include <JavaScriptCore/FPRInfo.h>
#include <JavaScriptCore/GPRInfo.h>
#include <JavaScriptCore/OperationResult.h>
#include <JavaScriptCore/StackAlignment.h>
#include <wtf/FunctionTraits.h>
#include <wtf/ScopedLambda.h>
#include <wtf/TZoneMalloc.h>
namespace JSC {
#define POKE_ARGUMENT_OFFSET 0
class CallFrame;
class Structure;
namespace DFG {
class RegisteredStructure;
};
class CCallHelpers : public AssemblyHelpers {
WTF_MAKE_TZONE_ALLOCATED(CCallHelpers);
public:
CCallHelpers(CodeBlock* codeBlock = nullptr)
: AssemblyHelpers(codeBlock)
{
}
// Base class for constant materializers.
// It offers DerivedClass::materialize and poke functions.
class ConstantMaterializer { };
// The most general helper for setting arguments that fit in a GPR, if you can compute each
// argument without using any argument registers. You usually want one of the setupArguments*()
// methods below instead of this. This thing is most useful if you have *a lot* of arguments.
template<typename Functor>
void setupArgument(unsigned argumentIndex, NOESCAPE const Functor& functor)
{
unsigned numberOfRegs = GPRInfo::numberOfArgumentRegisters; // Disguise the constant from clang's tautological compare warning.
if (argumentIndex < numberOfRegs) {
functor(GPRInfo::toArgumentRegister(argumentIndex));
return;
}
functor(GPRInfo::nonArgGPR0);
poke(GPRInfo::nonArgGPR0, POKE_ARGUMENT_OFFSET + argumentIndex - GPRInfo::numberOfArgumentRegisters);
}
enum class ShuffleStatus : uint8_t {
ToMove,
BeingMoved,
Moved
};
template<typename RegType, size_t N, typename RegPair = std::pair<RegType, RegType>>
void emitShuffleMove(Vector<RegPair, N>& moves, Vector<ShuffleStatus, N>& status, unsigned index, RegType scratch)
{
status[index] = ShuffleStatus::BeingMoved;
for (unsigned i = 0; i < moves.size(); i ++) {
if (moves[i].first == moves[index].second) {
ASSERT(i != index);
switch (status[i]) {
case ShuffleStatus::ToMove:
emitShuffleMove(moves, status, i, scratch);
break;
case ShuffleStatus::BeingMoved: {
if constexpr (std::is_same_v<RegType, FPRegisterID>)
moveDouble(moves[i].first, scratch);
else
move(moves[i].first, scratch);
moves[i].first = scratch;
break;
}
case ShuffleStatus::Moved:
break;
}
}
}
if constexpr (std::is_same_v<RegType, FPRegisterID>)
moveDouble(moves[index].first, moves[index].second);
else
move(moves[index].first, moves[index].second);
status[index] = ShuffleStatus::Moved;
}
template<typename RegType, unsigned NumberOfRegisters>
ALWAYS_INLINE void shuffleRegisters(std::array<RegType, NumberOfRegisters> sources, std::array<RegType, NumberOfRegisters> destinations)
{
if (ASSERT_ENABLED) {
RegisterSet set;
for (RegType dest : destinations)
set.add(dest, IgnoreVectors);
ASSERT_WITH_MESSAGE(set.numberOfSetRegisters() == NumberOfRegisters, "Destinations should not be aliased.");
}
using RegPair = std::pair<RegType, RegType>;
Vector<RegPair, NumberOfRegisters> pairs;
// if constexpr avoids warnings when NumberOfRegisters is 0.
if constexpr (NumberOfRegisters > 0) {
for (unsigned i = 0; i < NumberOfRegisters; ++i) {
if (sources[i] != destinations[i])
pairs.append(std::make_pair(sources[i], destinations[i]));
}
} else {
// Silence some older compilers (GCC up to 9.X) about unused but set parameters.
UNUSED_PARAM(sources);
UNUSED_PARAM(destinations);
}
Vector<ShuffleStatus, NumberOfRegisters> status;
status.fill(ShuffleStatus::ToMove, pairs.size());
RELEASE_ASSERT(m_allowScratchRegister); // We absolutely need one for the recursive shuffle algorithm.
auto scratch = scratchRegister();
{
DisallowMacroScratchRegisterUsage disallowScratch(*this);
for (size_t i = 0; i < pairs.size(); i ++) {
if (status[i] == ShuffleStatus::ToMove) {
if constexpr (std::is_same_v<RegType, RegisterID>)
emitShuffleMove(pairs, status, i, scratch);
else
emitShuffleMove(pairs, status, i, fpTempRegister);
}
}
}
}
private:
template<typename RegType>
using InfoTypeForReg = decltype(toInfoFromReg(RegType(-1)));
// extraPoke is used to track 64-bit argument types passed on the stack.
template<unsigned numGPRArgs, unsigned numGPRSources, unsigned numFPRArgs, unsigned numFPRSources, unsigned numCrossSources, unsigned nonArgGPRs, unsigned extraPoke>
struct ArgCollection {
ArgCollection()
{
gprSources.fill(InvalidGPRReg);
gprDestinations.fill(InvalidGPRReg);
fprSources.fill(InvalidFPRReg);
fprDestinations.fill(InvalidFPRReg);
crossSources.fill(InvalidFPRReg);
crossDestinations.fill(InvalidGPRReg);
}
template<unsigned a, unsigned b, unsigned c, unsigned d, unsigned e, unsigned f, unsigned g>
ArgCollection(ArgCollection<a, b, c, d, e, f, g>& other)
{
gprSources = other.gprSources;
gprDestinations = other.gprDestinations;
fprSources = other.fprSources;
fprDestinations = other.fprDestinations;
crossSources = other.crossSources;
crossDestinations = other.crossDestinations;
}
ArgCollection<numGPRArgs + 1, numGPRSources + 1, numFPRArgs, numFPRSources, numCrossSources, nonArgGPRs, extraPoke> pushRegArg(GPRReg argument, GPRReg destination)
{
ArgCollection<numGPRArgs + 1, numGPRSources + 1, numFPRArgs, numFPRSources, numCrossSources, nonArgGPRs, extraPoke> result(*this);
result.gprSources[numGPRSources] = argument;
result.gprDestinations[numGPRSources] = destination;
return result;
}
ArgCollection<numGPRArgs, numGPRSources, numFPRArgs + 1, numFPRSources + 1, numCrossSources, nonArgGPRs, extraPoke> pushRegArg(FPRReg argument, FPRReg destination)
{
ArgCollection<numGPRArgs, numGPRSources, numFPRArgs + 1, numFPRSources + 1, numCrossSources, nonArgGPRs, extraPoke> result(*this);
result.fprSources[numFPRSources] = argument;
result.fprDestinations[numFPRSources] = destination;
return result;
}
ArgCollection<numGPRArgs, numGPRSources, numFPRArgs + 1, numFPRSources, numCrossSources + 1, nonArgGPRs, extraPoke> pushRegArg(FPRReg argument, GPRReg destination)
{
ArgCollection<numGPRArgs, numGPRSources, numFPRArgs + 1, numFPRSources, numCrossSources + 1, nonArgGPRs, extraPoke> result(*this);
result.crossSources[numCrossSources] = argument;
result.crossDestinations[numCrossSources] = destination;
return result;
}
ArgCollection<numGPRArgs, numGPRSources + 1, numFPRArgs, numFPRSources, numCrossSources, nonArgGPRs + 1, extraPoke> pushNonArg(GPRReg argument, GPRReg destination)
{
ArgCollection<numGPRArgs, numGPRSources + 1, numFPRArgs, numFPRSources, numCrossSources, nonArgGPRs + 1, extraPoke> result(*this);
result.gprSources[numGPRSources] = argument;
result.gprDestinations[numGPRSources] = destination;
return result;
}
ArgCollection<numGPRArgs + 1, numGPRSources, numFPRArgs, numFPRSources, numCrossSources, nonArgGPRs, extraPoke> addGPRArg()
{
return ArgCollection<numGPRArgs + 1, numGPRSources, numFPRArgs, numFPRSources, numCrossSources, nonArgGPRs, extraPoke>(*this);
}
ArgCollection<numGPRArgs + 1, numGPRSources, numFPRArgs, numFPRSources, numCrossSources, nonArgGPRs, extraPoke> addStackArg(GPRReg)
{
return ArgCollection<numGPRArgs + 1, numGPRSources, numFPRArgs, numFPRSources, numCrossSources, nonArgGPRs, extraPoke>(*this);
}
ArgCollection<numGPRArgs, numGPRSources, numFPRArgs + 1, numFPRSources, numCrossSources, nonArgGPRs, extraPoke> addStackArg(FPRReg)
{
return ArgCollection<numGPRArgs, numGPRSources, numFPRArgs + 1, numFPRSources, numCrossSources, nonArgGPRs, extraPoke>(*this);
}
ArgCollection<numGPRArgs, numGPRSources, numFPRArgs, numFPRSources, numCrossSources, nonArgGPRs, extraPoke + 1> addPoke()
{
return ArgCollection<numGPRArgs, numGPRSources, numFPRArgs, numFPRSources, numCrossSources, nonArgGPRs, extraPoke + 1>(*this);
}
unsigned argCount(GPRReg) { return numGPRArgs; }
unsigned argCount(FPRReg) { return numFPRArgs; }
// store GPR -> GPR assignments
std::array<GPRReg, GPRInfo::numberOfRegisters> gprSources;
std::array<GPRReg, GPRInfo::numberOfRegisters> gprDestinations;
// store FPR -> FPR assignments
std::array<FPRReg, FPRInfo::numberOfRegisters> fprSources;
std::array<FPRReg, FPRInfo::numberOfRegisters> fprDestinations;
// store FPR -> GPR assignments
std::array<FPRReg, GPRInfo::numberOfRegisters> crossSources;
std::array<GPRReg, GPRInfo::numberOfRegisters> crossDestinations;
};
template<unsigned TargetSize, typename RegType>
std::array<RegType, TargetSize> clampArrayToSize(std::array<RegType, InfoTypeForReg<RegType>::numberOfRegisters> sourceArray)
{
static_assert(TargetSize <= sourceArray.size(), "TargetSize is bigger than source.size()");
RELEASE_ASSERT(TargetSize <= InfoTypeForReg<RegType>::numberOfRegisters);
std::array<RegType, TargetSize> result { };
// if constexpr avoids warnings when TargetSize is 0.
if constexpr (TargetSize > 0) {
for (unsigned i = 0; i < TargetSize; i++) {
ASSERT(sourceArray[i] != static_cast<int32_t>(InfoTypeForReg<RegType>::InvalidIndex));
result[i] = sourceArray[i];
}
}
return result;
}
ALWAYS_INLINE unsigned calculatePokeOffset(unsigned currentGPRArgument, unsigned currentFPRArgument, unsigned numCrossSources, unsigned nonArgGPRs, unsigned extraPoke)
{
// Clang claims that it cannot find the symbol for FPRReg/GPRReg::numberOfArgumentRegisters when they are passed directly to std::max... seems like a bug
unsigned numberOfFPArgumentRegisters = FPRInfo::numberOfArgumentRegisters;
unsigned numberOfGPArgumentRegisters = GPRInfo::numberOfArgumentRegisters;
UNUSED_PARAM(nonArgGPRs);
currentFPRArgument -= numCrossSources;
IGNORE_WARNINGS_BEGIN("type-limits")
ASSERT(currentGPRArgument >= GPRInfo::numberOfArgumentRegisters || currentFPRArgument >= FPRInfo::numberOfArgumentRegisters);
IGNORE_WARNINGS_END
unsigned pokeOffset = POKE_ARGUMENT_OFFSET + extraPoke;
pokeOffset += std::max(currentGPRArgument, numberOfGPArgumentRegisters) - numberOfGPArgumentRegisters;
pokeOffset += std::max(currentFPRArgument, numberOfFPArgumentRegisters) - numberOfFPArgumentRegisters;
return pokeOffset;
}
template<typename ArgType>
ALWAYS_INLINE void pokeForArgument(ArgType arg, unsigned currentGPRArgument, unsigned currentFPRArgument, unsigned numCrossSources, unsigned nonArgGPRs, unsigned extraPoke)
{
unsigned pokeOffset = calculatePokeOffset(currentGPRArgument, currentFPRArgument, numCrossSources, nonArgGPRs, extraPoke);
if constexpr (std::derived_from<ArgType, ConstantMaterializer>)
arg.store(*this, addressForPoke(pokeOffset));
else
poke(arg, pokeOffset);
}
ALWAYS_INLINE bool stackAligned(unsigned currentGPRArgument, unsigned currentFPRArgument, unsigned numCrossSources, unsigned nonArgGPRs, unsigned extraPoke)
{
unsigned pokeOffset = calculatePokeOffset(currentGPRArgument, currentFPRArgument, numCrossSources, nonArgGPRs, extraPoke);
return !(pokeOffset & 1);
}
// In the auto-calling convention code below the order of operations is:
// 1) spill arguments to stack slots
// 2) shuffle incomming argument values in registers to argument registers
// 3) fill immediate values to argument registers
// To do this, we recurse forwards through our args collecting argument values in registers and spilling stack slots.
// when we run out of args we then run our shuffling code to relocate registers. Finally, as we unwind from our
// recursion we can fill immediates.
#define CURRENT_ARGUMENT_TYPE typename FunctionTraits<OperationType>::template ArgumentType<numGPRArgs + numFPRArgs>
#define RESULT_TYPE typename FunctionTraits<OperationType>::ResultType
// Avoid MSVC optimization time explosion associated with __forceinline in recursive templates.
template<typename OperationType, unsigned numGPRArgs, unsigned numGPRSources, unsigned numFPRArgs, unsigned numFPRSources, unsigned numCrossSources, unsigned nonArgGPRs, unsigned extraPoke, typename RegType, typename... Args>
ALWAYS_INLINE void marshallArgumentRegister(ArgCollection<numGPRArgs, numGPRSources, numFPRArgs, numFPRSources, numCrossSources, nonArgGPRs, extraPoke> argSourceRegs, RegType arg, Args... args)
{
using InfoType = InfoTypeForReg<RegType>;
unsigned numArgRegisters = InfoType::numberOfArgumentRegisters;
unsigned currentArgCount = argSourceRegs.argCount(arg);
if (currentArgCount < numArgRegisters) {
auto updatedArgSourceRegs = argSourceRegs.pushRegArg(arg, InfoType::toArgumentRegister(currentArgCount));
setupArgumentsImpl<OperationType>(updatedArgSourceRegs, args...);
return;
}
pokeForArgument(arg, numGPRArgs, numFPRArgs, numCrossSources, nonArgGPRs, extraPoke);
setupArgumentsImpl<OperationType>(argSourceRegs.addStackArg(arg), args...);
}
template<typename OperationType, unsigned numGPRArgs, unsigned numGPRSources, unsigned numFPRArgs, unsigned numFPRSources, unsigned numCrossSources, unsigned nonArgGPRs, unsigned extraPoke, typename... Args>
ALWAYS_INLINE void setupArgumentsImpl(ArgCollection<numGPRArgs, numGPRSources, numFPRArgs, numFPRSources, numCrossSources, nonArgGPRs, extraPoke> argSourceRegs, FPRReg arg, Args... args)
{
static_assert(std::is_same_v<CURRENT_ARGUMENT_TYPE, double>, "We should only be passing FPRRegs to a double. We use moveDouble / loadDouble / storeDouble exclusively");
marshallArgumentRegister<OperationType>(argSourceRegs, arg, args...);
}
template<typename OperationType, unsigned numGPRArgs, unsigned numGPRSources, unsigned numFPRArgs, unsigned numFPRSources, unsigned numCrossSources, unsigned nonArgGPRs, unsigned extraPoke, typename... Args>
ALWAYS_INLINE void setupArgumentsImpl(ArgCollection<numGPRArgs, numGPRSources, numFPRArgs, numFPRSources, numCrossSources, nonArgGPRs, extraPoke> argSourceRegs, GPRReg arg, Args... args)
{
marshallArgumentRegister<OperationType>(argSourceRegs, arg, args...);
}
template<typename OperationType, unsigned numGPRArgs, unsigned numGPRSources, unsigned numFPRArgs, unsigned numFPRSources, unsigned numCrossSources, unsigned nonArgGPRs, unsigned extraPoke, typename Arg, typename... Args>
requires DerivedFromOrConvertibleTo<Arg, TrustedImm> // DerivedFromOrConvertibleTo instead of derived_from since DFGSpeculativeJIT has its own implementation of TrustedImmPtr
ALWAYS_INLINE void setupArgumentsImpl(ArgCollection<numGPRArgs, numGPRSources, numFPRArgs, numFPRSources, numCrossSources, nonArgGPRs, extraPoke> argSourceRegs, Arg arg, Args... args)
{
// Right now this only supports non-floating point immediate arguments since we never call operations with non-register values.
// If we ever needed to support immediate floating point arguments we would need to duplicate this logic for both types, which sounds
// gross so it's probably better to do that marshalling before the call operation...
static_assert(!std::is_floating_point_v<CURRENT_ARGUMENT_TYPE>, "We don't support immediate floats/doubles in setupArguments");
auto numArgRegisters = GPRInfo::numberOfArgumentRegisters;
auto currentArgCount = numGPRArgs;
if (currentArgCount < numArgRegisters) {
setupArgumentsImpl<OperationType>(argSourceRegs.addGPRArg(), args...);
move(arg, GPRInfo::toArgumentRegister(currentArgCount));
return;
}
pokeForArgument(arg, numGPRArgs, numFPRArgs, numCrossSources, nonArgGPRs, extraPoke);
setupArgumentsImpl<OperationType>(argSourceRegs.addGPRArg(), args...);
}
template<typename OperationType, unsigned numGPRArgs, unsigned numGPRSources, unsigned numFPRArgs, unsigned numFPRSources, unsigned numCrossSources, unsigned nonArgGPRs, unsigned extraPoke, typename Arg, typename... Args>
requires (std::same_as<CURRENT_ARGUMENT_TYPE, Arg> && std::integral<CURRENT_ARGUMENT_TYPE> && sizeof(CURRENT_ARGUMENT_TYPE) <= 4)
ALWAYS_INLINE void setupArgumentsImpl(ArgCollection<numGPRArgs, numGPRSources, numFPRArgs, numFPRSources, numCrossSources, nonArgGPRs, extraPoke> argSourceRegs, Arg arg, Args... args)
{
setupArgumentsImpl<OperationType>(argSourceRegs, TrustedImm32(arg), args...);
}
template<typename OperationType, unsigned numGPRArgs, unsigned numGPRSources, unsigned numFPRArgs, unsigned numFPRSources, unsigned numCrossSources, unsigned nonArgGPRs, unsigned extraPoke, typename Arg, typename... Args>
requires (std::same_as<CURRENT_ARGUMENT_TYPE, Arg> && std::integral<CURRENT_ARGUMENT_TYPE> && sizeof(CURRENT_ARGUMENT_TYPE) == 8)
ALWAYS_INLINE void setupArgumentsImpl(ArgCollection<numGPRArgs, numGPRSources, numFPRArgs, numFPRSources, numCrossSources, nonArgGPRs, extraPoke> argSourceRegs, Arg arg, Args... args)
{
setupArgumentsImpl<OperationType>(argSourceRegs, TrustedImm64(arg), args...);
}
template<typename OperationType, unsigned numGPRArgs, unsigned numGPRSources, unsigned numFPRArgs, unsigned numFPRSources, unsigned numCrossSources, unsigned nonArgGPRs, unsigned extraPoke, typename Arg, typename... Args>
requires (std::is_pointer_v<CURRENT_ARGUMENT_TYPE> && std::same_as<Arg, std::nullptr_t>)
ALWAYS_INLINE void setupArgumentsImpl(ArgCollection<numGPRArgs, numGPRSources, numFPRArgs, numFPRSources, numCrossSources, nonArgGPRs, extraPoke> argSourceRegs, Arg arg, Args... args)
{
setupArgumentsImpl<OperationType>(argSourceRegs, TrustedImmPtr(arg), args...);
}
// Special case DFG::RegisteredStructure because it's really annoying to deal with otherwise...
template<typename OperationType, unsigned numGPRArgs, unsigned numGPRSources, unsigned numFPRArgs, unsigned numFPRSources, unsigned numCrossSources, unsigned nonArgGPRs, unsigned extraPoke, typename Arg, typename... Args>
requires (std::same_as<CURRENT_ARGUMENT_TYPE, Structure*> && std::same_as<Arg, DFG::RegisteredStructure>)
ALWAYS_INLINE void setupArgumentsImpl(ArgCollection<numGPRArgs, numGPRSources, numFPRArgs, numFPRSources, numCrossSources, nonArgGPRs, extraPoke> argSourceRegs, Arg arg, Args... args)
{
setupArgumentsImpl<OperationType>(argSourceRegs, TrustedImmPtr(arg.get()), args...);
}
template<typename OperationType, unsigned numGPRArgs, unsigned numGPRSources, unsigned numFPRArgs, unsigned numFPRSources, unsigned numCrossSources, unsigned nonArgGPRs, unsigned extraPoke, typename Arg, typename... Args>
requires std::derived_from<Arg, ConstantMaterializer>
ALWAYS_INLINE void setupArgumentsImpl(ArgCollection<numGPRArgs, numGPRSources, numFPRArgs, numFPRSources, numCrossSources, nonArgGPRs, extraPoke> argSourceRegs, Arg arg, Args... args)
{
static_assert(!std::is_floating_point_v<CURRENT_ARGUMENT_TYPE>, "We don't support immediate floats/doubles in setupArguments");
auto numArgRegisters = GPRInfo::numberOfArgumentRegisters;
auto currentArgCount = numGPRArgs;
if (currentArgCount < numArgRegisters) {
setupArgumentsImpl<OperationType>(argSourceRegs.addGPRArg(), args...);
arg.materialize(*this, GPRInfo::toArgumentRegister(currentArgCount));
return;
}
pokeForArgument(arg, numGPRArgs, numFPRArgs, numCrossSources, nonArgGPRs, extraPoke);
setupArgumentsImpl<OperationType>(argSourceRegs.addGPRArg(), args...);
}
template<typename OperationType, unsigned gprIndex>
constexpr void finalizeGPRArguments(std::index_sequence<>)
{
}
template<typename OperationType, unsigned gprIndex, size_t arityIndex, size_t... remainingArityIndices>
constexpr void finalizeGPRArguments(std::index_sequence<arityIndex, remainingArityIndices...>)
{
using NextIndexSequenceType = std::index_sequence<remainingArityIndices...>;
using ArgumentType = typename FunctionTraits<OperationType>::template ArgumentType<arityIndex>;
// Every non-double-typed argument should be passed in through GPRRegs, and inversely only double-typed
// arguments should be passed through FPRRegs. This is asserted in the invocation of the lastly-called
// setupArgumentsImpl(ArgCollection<>) overload, by matching the number of handled GPR and FPR arguments
// with the corresponding count of properly-typed arguments for this operation.
if constexpr (!std::is_same_v<ArgumentType, double>) {
// RV64 calling convention requires all 32-bit values to be sign-extended into the whole register.
// JSC JIT is tailored for other ISAs that pass these values in 32-bit-wide registers, which RISC-V
// doesn't support, so any 32-bit value passed in argument registers has to be manually sign-extended.
if (isRISCV64() && gprIndex < GPRInfo::numberOfArgumentRegisters
&& std::is_integral_v<ArgumentType> && sizeof(ArgumentType) == 4) {
GPRReg argReg = GPRInfo::toArgumentRegister(gprIndex);
signExtend32ToPtr(argReg, argReg);
}
finalizeGPRArguments<OperationType, gprIndex + 1>(NextIndexSequenceType());
} else
finalizeGPRArguments<OperationType, gprIndex>(NextIndexSequenceType());
}
template<typename ArgType> using GPRArgCountValue = std::conditional_t<std::is_same_v<ArgType, double>,
std::integral_constant<unsigned, 0>, std::integral_constant<unsigned, 1>>;
template<typename ArgType> using FPRArgCountValue = std::conditional_t<std::is_same_v<ArgType, double>,
std::integral_constant<unsigned, 1>, std::integral_constant<unsigned, 0>>;
template<typename OperationTraitsType, size_t... argIndices>
static constexpr unsigned gprArgsCount(std::index_sequence<argIndices...>)
{
return (0 + ... + (GPRArgCountValue<typename OperationTraitsType::template ArgumentType<argIndices>>::value));
}
template<typename OperationTraitsType, size_t... argIndices>
static constexpr unsigned fprArgsCount(std::index_sequence<argIndices...>)
{
return (0 + ... + (FPRArgCountValue<typename OperationTraitsType::template ArgumentType<argIndices>>::value));
}
// Base case; set up the argument registers.
template<typename OperationType, unsigned numGPRArgs, unsigned numGPRSources, unsigned numFPRArgs, unsigned numFPRSources, unsigned numCrossSources, unsigned nonArgGPRs, unsigned extraPoke>
ALWAYS_INLINE void setupArgumentsImpl(ArgCollection<numGPRArgs, numGPRSources, numFPRArgs, numFPRSources, numCrossSources, nonArgGPRs, extraPoke> argSourceRegs)
{
using TraitsType = FunctionTraits<OperationType>;
static_assert(TraitsType::arity == numGPRArgs + numFPRArgs, "One last sanity check");
static_assert(TraitsType::cCallArity() == numGPRArgs + numFPRArgs + extraPoke, "Check the CCall arity");
static_assert(gprArgsCount<TraitsType>(std::make_index_sequence<TraitsType::arity>()) == numGPRArgs);
static_assert(fprArgsCount<TraitsType>(std::make_index_sequence<TraitsType::arity>()) == numFPRArgs);
shuffleRegisters<GPRReg, numGPRSources>(clampArrayToSize<numGPRSources, GPRReg>(argSourceRegs.gprSources), clampArrayToSize<numGPRSources, GPRReg>(argSourceRegs.gprDestinations));
static_assert(!numCrossSources, "shouldn't be used on this architecture.");
shuffleRegisters<FPRReg, numFPRSources>(clampArrayToSize<numFPRSources, FPRReg>(argSourceRegs.fprSources), clampArrayToSize<numFPRSources, FPRReg>(argSourceRegs.fprDestinations));
}
template<typename OperationType, unsigned numGPRArgs, unsigned numGPRSources, unsigned numFPRArgs, unsigned numFPRSources, unsigned numCrossSources, unsigned nonArgGPRs, unsigned extraPoke, typename... Args>
ALWAYS_INLINE void setupArgumentsEntryImpl(ArgCollection<numGPRArgs, numGPRSources, numFPRArgs, numFPRSources, numCrossSources, nonArgGPRs, extraPoke> argSourceRegs, Args... args)
{
if constexpr (!FunctionTraits<OperationType>::arity) {
static_assert(!sizeof...(Args), "Basic sanity check");
setupArgumentsImpl<OperationType>(argSourceRegs);
} else if constexpr (std::same_as<typename FunctionTraits<OperationType>::template ArgumentType<0>, CallFrame*>) {
static_assert(FunctionTraits<OperationType>::cCallArity() == sizeof...(Args) + 1, "Basic sanity check; Did you explicitly pass callFrameRegister for the first argument?");
setupArgumentsImpl<OperationType>(argSourceRegs, GPRInfo::callFrameRegister, args...);
} else {
static_assert(FunctionTraits<OperationType>::cCallArity() == sizeof...(Args), "Basic sanity check");
setupArgumentsImpl<OperationType>(argSourceRegs, args...);
}
finalizeGPRArguments<OperationType, 0>(std::make_index_sequence<FunctionTraits<OperationType>::arity>());
}
#undef CURRENT_ARGUMENT_TYPE
#undef RESULT_TYPE
public:
template<typename OperationType, typename... Args>
ALWAYS_INLINE void setupArguments(Args... args)
{
setupArgumentsEntryImpl<OperationType>(ArgCollection<0, 0, 0, 0, 0, 0, 0>(), args...);
}
template<typename OperationType, typename... Args>
ALWAYS_INLINE void setupArgumentsForIndirectCall(GPRReg functionGPR, Args... args)
{
setupArgumentsEntryImpl<OperationType>(ArgCollection<0, 0, 0, 0, 0, 0, 0>().pushNonArg(functionGPR, GPRInfo::nonArgGPR0), args...);
}
template<typename OperationType, typename... Args>
ALWAYS_INLINE void setupArgumentsForIndirectCall(Address address, Args... args)
{
setupArgumentsEntryImpl<OperationType>(ArgCollection<0, 0, 0, 0, 0, 0, 0>().pushNonArg(address.base, GPRInfo::nonArgGPR0), args...);
}
void setupResults(GPRReg destA, GPRReg destB = InvalidGPRReg)
{
GPRReg srcA = GPRInfo::returnValueGPR;
GPRReg srcB = GPRInfo::returnValueGPR2;
if (destA == InvalidGPRReg)
move(srcB, destB);
else if (destB == InvalidGPRReg)
move(srcA, destA);
else if (srcB != destA) {
// Handle the easy cases - two simple moves.
move(srcA, destA);
move(srcB, destB);
} else if (srcA != destB) {
// Handle the non-swap case - just put srcB in place first.
move(srcB, destB);
move(srcA, destA);
} else
swap(destA, destB);
}
void setupResults(FPRReg destA)
{
if (destA != InvalidFPRReg)
moveDouble(FPRInfo::returnValueFPR, destA);
}
void jumpToExceptionHandler(VM& vm)
{
// genericUnwind() leaves the handler CallFrame* in vm->callFrameForCatch,
// and the address of the handler in vm->targetMachinePCForThrow.
loadPtr(&vm.targetMachinePCForThrow, GPRInfo::regT1);
farJump(GPRInfo::regT1, ExceptionHandlerPtrTag);
}
template<typename T>
requires (isExceptionOperationResult<T>)
static constexpr GPRReg operationExceptionRegister()
{
static_assert(assertNotOperationSignature<T>);
if constexpr (std::is_floating_point_v<typename T::ResultType> || std::is_same_v<typename T::ResultType, void>)
return GPRInfo::returnValueGPR;
else
return GPRInfo::returnValueGPR2;
}
template<typename T>
requires (!isExceptionOperationResult<T>)
static constexpr GPRReg operationExceptionRegister()
{
static_assert(assertNotOperationSignature<T>);
return InvalidGPRReg;
}
void prepareForTailCallSlow(RegisterSet preserved = { })
{
GPRReg temp1 = selectScratchGPR(preserved);
preserved.add(temp1, IgnoreVectors);
GPRReg temp2 = selectScratchGPR(preserved);
preserved.add(temp2, IgnoreVectors);
#if CPU(ARM64E)
GPRReg temp3 = selectScratchGPR(preserved);
preserved.add(temp3, IgnoreVectors);
#endif
ASSERT(!preserved.numberOfSetFPRs());
GPRReg newFramePointer = temp1;
GPRReg newFrameSizeGPR = temp2;
{
// The old frame size is its number of arguments (or number of
// parameters in case of arity fixup), plus the frame header size,
// aligned
GPRReg oldFrameSizeGPR = temp2;
{
GPRReg argCountGPR = oldFrameSizeGPR;
load32(Address(framePointerRegister, CallFrameSlot::argumentCountIncludingThis * static_cast<int>(sizeof(Register)) + LowWordOffset), argCountGPR);
{
GPRReg numParametersGPR = temp1;
{
GPRReg codeBlockGPR = numParametersGPR;
loadPtr(Address(framePointerRegister, CallFrameSlot::codeBlock * static_cast<int>(sizeof(Register))), codeBlockGPR);
load32(Address(codeBlockGPR, CodeBlock::offsetOfNumParameters()), numParametersGPR);
}
ASSERT(numParametersGPR != argCountGPR);
// argCountGPR = max(numParametersGPR, argCountGPR)
moveConditionally32(Above, numParametersGPR, argCountGPR, numParametersGPR, argCountGPR, argCountGPR);
}
add32(TrustedImm32(stackAlignmentRegisters() + CallFrame::headerSizeInRegisters - 1), argCountGPR, oldFrameSizeGPR);
and32(TrustedImm32(-stackAlignmentRegisters()), oldFrameSizeGPR);
// We assume < 2^28 arguments
mul32(TrustedImm32(sizeof(Register)), oldFrameSizeGPR, oldFrameSizeGPR);
}
// The new frame pointer is at framePointer + oldFrameSize - newFrameSize
ASSERT(newFramePointer != oldFrameSizeGPR);
addPtr(framePointerRegister, oldFrameSizeGPR, newFramePointer);
// The new frame size is just the number of arguments plus the
// frame header size, aligned
ASSERT(newFrameSizeGPR != newFramePointer);
load32(Address(stackPointerRegister, CallFrameSlot::argumentCountIncludingThis * static_cast<int>(sizeof(Register)) + LowWordOffset - sizeof(CallerFrameAndPC)),
newFrameSizeGPR);
add32(TrustedImm32(stackAlignmentRegisters() + CallFrame::headerSizeInRegisters - 1), newFrameSizeGPR);
and32(TrustedImm32(-stackAlignmentRegisters()), newFrameSizeGPR);
// We assume < 2^28 arguments
mul32(TrustedImm32(sizeof(Register)), newFrameSizeGPR, newFrameSizeGPR);
}
// We don't need the current frame beyond this point. Masquerade as our
// caller.
#if CPU(ARM64) || CPU(RISCV64)
loadPtr(Address(framePointerRegister, CallFrame::returnPCOffset()), linkRegister);
subPtr(TrustedImm32(2 * sizeof(void*)), newFrameSizeGPR);
#if CPU(ARM64E)
GPRReg tempGPR = temp3;
ASSERT(tempGPR != newFramePointer && tempGPR != newFrameSizeGPR);
addPtr(TrustedImm32(sizeof(CallerFrameAndPC)), MacroAssembler::framePointerRegister, tempGPR);
untagPtr(tempGPR, linkRegister);
validateUntaggedPtr(linkRegister, tempGPR);
#endif
#elif CPU(X86_64)
push(Address(framePointerRegister, sizeof(void*)));
subPtr(TrustedImm32(sizeof(void*)), newFrameSizeGPR);
#else
UNREACHABLE_FOR_PLATFORM();
#endif
subPtr(newFrameSizeGPR, newFramePointer);
loadPtr(Address(framePointerRegister), framePointerRegister);
// We need to move the newFrameSizeGPR slots above the stack pointer by
// newFramePointer registers. We use pointer-sized chunks.
MacroAssembler::Label copyLoop(label());
subPtr(TrustedImm32(sizeof(void*)), newFrameSizeGPR);
transferPtr(BaseIndex(stackPointerRegister, newFrameSizeGPR, TimesOne), BaseIndex(newFramePointer, newFrameSizeGPR, TimesOne));
branchTest32(MacroAssembler::NonZero, newFrameSizeGPR).linkTo(copyLoop, this);
// Ready for a jump!
move(newFramePointer, stackPointerRegister);
}
static Address addressOfCalleeCalleeFromCallerPerspective(int offset)
{
CCallHelpers::Address calleeFrame = CCallHelpers::Address(MacroAssembler::stackPointerRegister, 0);
return calleeFrame.withOffset(CallFrameSlot::callee * static_cast<int>(sizeof(Register)))
// calleeFrame is from the caller's perspective
.withOffset(-safeCast<int>(sizeof(CallerFrameAndPC)))
.withOffset(LowWordOffset)
.withOffset(offset);
}
// This function is used to store the wasm callee in case this function hasn't tiered up yet.
// The IPInt is going to expect this so that the common entrypoint can read bytecode/metadata.
void storeWasmCalleeToCalleeCallFrame(RegisterID value, int offset = 0)
{
JIT_COMMENT(*this, "< Store Callee's wasm callee");
auto addr = CCallHelpers::addressOfCalleeCalleeFromCallerPerspective(offset);
storePtr(value, addr);
}
void storeWasmCalleeToCalleeCallFrame(CalleeBits boxedCallee)
{
JIT_COMMENT(*this, "> ", RawPointer(boxedCallee.asNativeCallee()));
storeWasmCalleeToCalleeCallFrame(TrustedImmPtr(boxedCallee.rawPtr()));
}
void storeWasmCalleeToCalleeCallFrame(TrustedImmPtr imm, int offset = 0)
{
JIT_COMMENT(*this, "Store Callee's wasm callee");
auto addr = CCallHelpers::addressOfCalleeCalleeFromCallerPerspective(offset);
store64(imm, addr);
}
// These operations clobber all volatile registers. They assume that there is room on the top of
// stack to marshall call arguments.
void logShadowChickenProloguePacket(GPRReg shadowPacket, GPRReg scratch1, GPRReg scope);
private:
template <typename CodeBlockType>
void logShadowChickenTailPacketImpl(GPRReg shadowPacket, GPRReg thisGPR, GPRReg scope, CodeBlockType, CallSiteIndex);
public:
void logShadowChickenTailPacket(GPRReg shadowPacket, GPRReg thisGPR, GPRReg scope, GPRReg codeBlock, CallSiteIndex);
// Leaves behind a pointer to the Packet we should write to in shadowPacket.
void ensureShadowChickenPacket(VM&, GPRReg shadowPacket, GPRReg scratch1NonArgGPR, GPRReg scratch2);
void emitCTIThunkPrologue(bool returnAddressAlreadyTagged = false);
void emitCTIThunkEpilogue();
};
} // namespace JSC
#endif // ENABLE(JIT)