/
builtins-riscv.cc
3867 lines (3352 loc) Β· 144 KB
/
builtins-riscv.cc
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
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/api/api-arguments.h"
#include "src/codegen/code-factory.h"
#include "src/codegen/interface-descriptors-inl.h"
#include "src/debug/debug.h"
#include "src/deoptimizer/deoptimizer.h"
#include "src/execution/frame-constants.h"
#include "src/execution/frames.h"
#include "src/logging/counters.h"
// For interpreter_entry_return_pc_offset. TODO(jkummerow): Drop.
#include "src/codegen/macro-assembler-inl.h"
#include "src/codegen/register-configuration.h"
#include "src/heap/heap-inl.h"
#include "src/objects/cell.h"
#include "src/objects/foreign.h"
#include "src/objects/heap-number.h"
#include "src/objects/js-generator.h"
#include "src/objects/objects-inl.h"
#include "src/objects/smi.h"
#include "src/runtime/runtime.h"
#if V8_ENABLE_WEBASSEMBLY
#include "src/wasm/baseline/liftoff-assembler-defs.h"
#include "src/wasm/wasm-linkage.h"
#include "src/wasm/wasm-objects.h"
#endif // V8_ENABLE_WEBASSEMBLY
namespace v8 {
namespace internal {
#define __ ACCESS_MASM(masm)
void Builtins::Generate_Adaptor(MacroAssembler* masm, Address address) {
ASM_CODE_COMMENT(masm);
__ li(kJavaScriptCallExtraArg1Register, ExternalReference::Create(address));
__ Jump(BUILTIN_CODE(masm->isolate(), AdaptorWithBuiltinExitFrame),
RelocInfo::CODE_TARGET);
}
namespace {
enum class ArgumentsElementType {
kRaw, // Push arguments as they are.
kHandle // Dereference arguments before pushing.
};
void Generate_PushArguments(MacroAssembler* masm, Register array, Register argc,
Register scratch, Register scratch2,
ArgumentsElementType element_type) {
DCHECK(!AreAliased(array, argc, scratch));
Label loop, entry;
__ SubWord(scratch, argc, Operand(kJSArgcReceiverSlots));
__ Branch(&entry);
__ bind(&loop);
__ CalcScaledAddress(scratch2, array, scratch, kSystemPointerSizeLog2);
__ LoadWord(scratch2, MemOperand(scratch2));
if (element_type == ArgumentsElementType::kHandle) {
__ LoadWord(scratch2, MemOperand(scratch2));
}
__ push(scratch2);
__ bind(&entry);
__ AddWord(scratch, scratch, Operand(-1));
__ Branch(&loop, greater_equal, scratch, Operand(zero_reg));
}
void Generate_JSBuiltinsConstructStubHelper(MacroAssembler* masm) {
// ----------- S t a t e -------------
// -- a0 : number of arguments
// -- a1 : constructor function
// -- a3 : new target
// -- cp : context
// -- ra : return address
// -- sp[...]: constructor arguments
// -----------------------------------
// Enter a construct frame.
{
FrameScope scope(masm, StackFrame::CONSTRUCT);
// Preserve the incoming parameters on the stack.
__ SmiTag(a0);
__ Push(cp, a0);
__ SmiUntag(a0);
// Set up pointer to first argument (skip receiver).
__ AddWord(
t2, fp,
Operand(StandardFrameConstants::kCallerSPOffset + kSystemPointerSize));
// t2: Pointer to start of arguments.
// a0: Number of arguments.
{
UseScratchRegisterScope temps(masm);
temps.Include(t0);
Generate_PushArguments(masm, t2, a0, temps.Acquire(), temps.Acquire(),
ArgumentsElementType::kRaw);
}
// The receiver for the builtin/api call.
__ PushRoot(RootIndex::kTheHoleValue);
// Call the function.
// a0: number of arguments (untagged)
// a1: constructor function
// a3: new target
__ InvokeFunctionWithNewTarget(a1, a3, a0, InvokeType::kCall);
// Restore context from the frame.
__ LoadWord(cp, MemOperand(fp, ConstructFrameConstants::kContextOffset));
// Restore smi-tagged arguments count from the frame.
__ LoadWord(kScratchReg,
MemOperand(fp, ConstructFrameConstants::kLengthOffset));
// Leave construct frame.
}
// Remove caller arguments from the stack and return.
__ DropArguments(kScratchReg, MacroAssembler::kCountIsSmi,
MacroAssembler::kCountIncludesReceiver, kScratchReg);
__ Ret();
}
} // namespace
// The construct stub for ES5 constructor functions and ES6 class constructors.
void Builtins::Generate_JSConstructStubGeneric(MacroAssembler* masm) {
// ----------- S t a t e -------------
// -- a0: number of arguments (untagged)
// -- a1: constructor function
// -- a3: new target
// -- cp: context
// -- ra: return address
// -- sp[...]: constructor arguments
// -----------------------------------
UseScratchRegisterScope temps(masm);
temps.Include(t0, t1);
// Enter a construct frame.
FrameScope scope(masm, StackFrame::MANUAL);
Label post_instantiation_deopt_entry, not_create_implicit_receiver;
__ EnterFrame(StackFrame::CONSTRUCT);
// Preserve the incoming parameters on the stack.
__ SmiTag(a0);
__ Push(cp, a0, a1);
__ PushRoot(RootIndex::kUndefinedValue);
__ Push(a3);
// ----------- S t a t e -------------
// -- sp[0*kSystemPointerSize]: new target
// -- sp[1*kSystemPointerSize]: padding
// -- a1 and sp[2*kSystemPointerSize]: constructor function
// -- sp[3*kSystemPointerSize]: number of arguments (tagged)
// -- sp[4*kSystemPointerSize]: context
// -----------------------------------
{
UseScratchRegisterScope temps(masm);
Register func_info = temps.Acquire();
__ LoadTaggedField(
func_info, FieldMemOperand(a1, JSFunction::kSharedFunctionInfoOffset));
__ Load32U(func_info,
FieldMemOperand(func_info, SharedFunctionInfo::kFlagsOffset));
__ DecodeField<SharedFunctionInfo::FunctionKindBits>(func_info);
__ JumpIfIsInRange(
func_info,
static_cast<uint32_t>(FunctionKind::kDefaultDerivedConstructor),
static_cast<uint32_t>(FunctionKind::kDerivedConstructor),
¬_create_implicit_receiver);
// If not derived class constructor: Allocate the new receiver object.
__ Call(BUILTIN_CODE(masm->isolate(), FastNewObject),
RelocInfo::CODE_TARGET);
__ BranchShort(&post_instantiation_deopt_entry);
// Else: use TheHoleValue as receiver for constructor call
__ bind(¬_create_implicit_receiver);
__ LoadRoot(a0, RootIndex::kTheHoleValue);
}
// ----------- S t a t e -------------
// -- a0: receiver
// -- Slot 4 / sp[0*kSystemPointerSize]: new target
// -- Slot 3 / sp[1*kSystemPointerSize]: padding
// -- Slot 2 / sp[2*kSystemPointerSize]: constructor function
// -- Slot 1 / sp[3*kSystemPointerSize]: number of arguments (tagged)
// -- Slot 0 / sp[4*kSystemPointerSize]: context
// -----------------------------------
// Deoptimizer enters here.
masm->isolate()->heap()->SetConstructStubCreateDeoptPCOffset(
masm->pc_offset());
__ bind(&post_instantiation_deopt_entry);
// Restore new target.
__ Pop(a3);
// Push the allocated receiver to the stack.
__ Push(a0);
// We need two copies because we may have to return the original one
// and the calling conventions dictate that the called function pops the
// receiver. The second copy is pushed after the arguments, we saved in a6
// since a0 will store the return value of callRuntime.
__ Move(a6, a0);
// Set up pointer to first argument (skip receiver)..
__ AddWord(
t2, fp,
Operand(StandardFrameConstants::kCallerSPOffset + kSystemPointerSize));
// ----------- S t a t e -------------
// -- a3: new target
// -- sp[0*kSystemPointerSize]: implicit receiver
// -- sp[1*kSystemPointerSize]: implicit receiver
// -- sp[2*kSystemPointerSize]: padding
// -- sp[3*kSystemPointerSize]: constructor function
// -- sp[4*kSystemPointerSize]: number of arguments (tagged)
// -- sp[5*kSystemPointerSize]: context
// -----------------------------------
// Restore constructor function and argument count.
__ LoadWord(a1, MemOperand(fp, ConstructFrameConstants::kConstructorOffset));
__ LoadWord(a0, MemOperand(fp, ConstructFrameConstants::kLengthOffset));
__ SmiUntag(a0);
Label stack_overflow;
{
UseScratchRegisterScope temps(masm);
__ StackOverflowCheck(a0, temps.Acquire(), temps.Acquire(),
&stack_overflow);
}
// TODO(victorgomes): When the arguments adaptor is completely removed, we
// should get the formal parameter count and copy the arguments in its
// correct position (including any undefined), instead of delaying this to
// InvokeFunction.
// Copy arguments and receiver to the expression stack.
// t2: Pointer to start of argument.
// a0: Number of arguments.
{
UseScratchRegisterScope temps(masm);
Generate_PushArguments(masm, t2, a0, temps.Acquire(), temps.Acquire(),
ArgumentsElementType::kRaw);
}
// We need two copies because we may have to return the original one
// and the calling conventions dictate that the called function pops the
// receiver. The second copy is pushed after the arguments,
__ Push(a6);
// Call the function.
__ InvokeFunctionWithNewTarget(a1, a3, a0, InvokeType::kCall);
// ----------- S t a t e -------------
// -- a0: constructor result
// -- sp[0*kSystemPointerSize]: implicit receiver
// -- sp[1*kSystemPointerSize]: padding
// -- sp[2*kSystemPointerSize]: constructor function
// -- sp[3*kSystemPointerSize]: number of arguments
// -- sp[4*kSystemPointerSize]: context
// -----------------------------------
// Store offset of return address for deoptimizer.
masm->isolate()->heap()->SetConstructStubInvokeDeoptPCOffset(
masm->pc_offset());
// Restore the context from the frame.
__ LoadWord(cp, MemOperand(fp, ConstructFrameConstants::kContextOffset));
// If the result is an object (in the ECMA sense), we should get rid
// of the receiver and use the result; see ECMA-262 section 13.2.2-7
// on page 74.
Label use_receiver, do_throw, leave_and_return, check_receiver;
// If the result is undefined, we jump out to using the implicit receiver.
__ JumpIfNotRoot(a0, RootIndex::kUndefinedValue, &check_receiver);
// Otherwise we do a smi check and fall through to check if the return value
// is a valid receiver.
// Throw away the result of the constructor invocation and use the
// on-stack receiver as the result.
__ bind(&use_receiver);
__ LoadWord(a0, MemOperand(sp, 0 * kSystemPointerSize));
__ JumpIfRoot(a0, RootIndex::kTheHoleValue, &do_throw);
__ bind(&leave_and_return);
// Restore smi-tagged arguments count from the frame.
__ LoadWord(a1, MemOperand(fp, ConstructFrameConstants::kLengthOffset));
// Leave construct frame.
__ LeaveFrame(StackFrame::CONSTRUCT);
// Remove caller arguments from the stack and return.
__ DropArguments(a1, MacroAssembler::kCountIsSmi,
MacroAssembler::kCountIncludesReceiver, a4);
__ Ret();
__ bind(&check_receiver);
__ JumpIfSmi(a0, &use_receiver);
// If the type of the result (stored in its map) is less than
// FIRST_JS_RECEIVER_TYPE, it is not an object in the ECMA sense.
{
UseScratchRegisterScope temps(masm);
Register map = temps.Acquire(), type = temps.Acquire();
__ GetObjectType(a0, map, type);
static_assert(LAST_JS_RECEIVER_TYPE == LAST_TYPE);
__ Branch(&leave_and_return, greater_equal, type,
Operand(FIRST_JS_RECEIVER_TYPE));
__ Branch(&use_receiver);
}
__ bind(&do_throw);
// Restore the context from the frame.
__ LoadWord(cp, MemOperand(fp, ConstructFrameConstants::kContextOffset));
__ CallRuntime(Runtime::kThrowConstructorReturnedNonObject);
__ break_(0xCC);
__ bind(&stack_overflow);
// Restore the context from the frame.
__ LoadWord(cp, MemOperand(fp, ConstructFrameConstants::kContextOffset));
__ CallRuntime(Runtime::kThrowStackOverflow);
__ break_(0xCC);
}
void Builtins::Generate_JSBuiltinsConstructStub(MacroAssembler* masm) {
Generate_JSBuiltinsConstructStubHelper(masm);
}
static void AssertCodeIsBaseline(MacroAssembler* masm, Register code,
Register scratch) {
DCHECK(!AreAliased(code, scratch));
// Verify that the code kind is baseline code via the CodeKind.
__ LoadWord(scratch, FieldMemOperand(code, Code::kFlagsOffset));
__ DecodeField<Code::KindField>(scratch);
__ Assert(eq, AbortReason::kExpectedBaselineData, scratch,
Operand(static_cast<int64_t>(CodeKind::BASELINE)));
}
// TODO(v8:11429): Add a path for "not_compiled" and unify the two uses under
// the more general dispatch.
static void GetSharedFunctionInfoBytecodeOrBaseline(MacroAssembler* masm,
Register sfi_data,
Register scratch1,
Label* is_baseline) {
ASM_CODE_COMMENT(masm);
Label done;
__ GetObjectType(sfi_data, scratch1, scratch1);
#ifndef V8_JITLESS
if (v8_flags.debug_code) {
Label not_baseline;
__ Branch(¬_baseline, ne, scratch1, Operand(CODE_TYPE));
AssertCodeIsBaseline(masm, sfi_data, scratch1);
__ Branch(is_baseline);
__ bind(¬_baseline);
} else {
__ Branch(is_baseline, eq, scratch1, Operand(CODE_TYPE));
}
#endif // !V8_JITLESS
__ Branch(&done, ne, scratch1, Operand(INTERPRETER_DATA_TYPE),
Label::Distance::kNear);
__ LoadTaggedField(
sfi_data,
FieldMemOperand(sfi_data, InterpreterData::kBytecodeArrayOffset));
__ bind(&done);
}
// static
void Builtins::Generate_ResumeGeneratorTrampoline(MacroAssembler* masm) {
// ----------- S t a t e -------------
// -- a0 : the value to pass to the generator
// -- a1 : the JSGeneratorObject to resume
// -- ra : return address
// -----------------------------------
// Store input value into generator object.
__ StoreTaggedField(
a0, FieldMemOperand(a1, JSGeneratorObject::kInputOrDebugPosOffset));
__ RecordWriteField(a1, JSGeneratorObject::kInputOrDebugPosOffset, a0,
kRAHasNotBeenSaved, SaveFPRegsMode::kIgnore);
// Check that a1 is still valid, RecordWrite might have clobbered it.
__ AssertGeneratorObject(a1);
// Load suspended function and context.
__ LoadTaggedField(a4,
FieldMemOperand(a1, JSGeneratorObject::kFunctionOffset));
__ LoadTaggedField(cp, FieldMemOperand(a4, JSFunction::kContextOffset));
// Flood function if we are stepping.
Label prepare_step_in_if_stepping, prepare_step_in_suspended_generator;
Label stepping_prepared;
ExternalReference debug_hook =
ExternalReference::debug_hook_on_function_call_address(masm->isolate());
__ li(a5, debug_hook);
__ Lb(a5, MemOperand(a5));
__ Branch(&prepare_step_in_if_stepping, ne, a5, Operand(zero_reg));
// Flood function if we need to continue stepping in the suspended generator.
ExternalReference debug_suspended_generator =
ExternalReference::debug_suspended_generator_address(masm->isolate());
__ li(a5, debug_suspended_generator);
__ LoadWord(a5, MemOperand(a5));
__ Branch(&prepare_step_in_suspended_generator, eq, a1, Operand(a5));
__ bind(&stepping_prepared);
// Check the stack for overflow. We are not trying to catch interruptions
// (i.e. debug break and preemption) here, so check the "real stack limit".
Label stack_overflow;
__ LoadStackLimit(kScratchReg,
MacroAssembler::StackLimitKind::kRealStackLimit);
__ Branch(&stack_overflow, Uless, sp, Operand(kScratchReg));
// ----------- S t a t e -------------
// -- a1 : the JSGeneratorObject to resume
// -- a4 : generator function
// -- cp : generator context
// -- ra : return address
// -----------------------------------
// Push holes for arguments to generator function. Since the parser forced
// context allocation for any variables in generators, the actual argument
// values have already been copied into the context and these dummy values
// will never be used.
__ LoadTaggedField(
a3, FieldMemOperand(a4, JSFunction::kSharedFunctionInfoOffset));
__ Lhu(a3,
FieldMemOperand(a3, SharedFunctionInfo::kFormalParameterCountOffset));
__ SubWord(a3, a3, Operand(kJSArgcReceiverSlots));
__ LoadTaggedField(
t1,
FieldMemOperand(a1, JSGeneratorObject::kParametersAndRegistersOffset));
{
Label done_loop, loop;
__ bind(&loop);
__ SubWord(a3, a3, Operand(1));
__ Branch(&done_loop, lt, a3, Operand(zero_reg), Label::Distance::kNear);
__ CalcScaledAddress(kScratchReg, t1, a3, kTaggedSizeLog2);
__ LoadTaggedField(kScratchReg,
FieldMemOperand(kScratchReg, FixedArray::kHeaderSize));
__ Push(kScratchReg);
__ Branch(&loop);
__ bind(&done_loop);
// Push receiver.
__ LoadTaggedField(kScratchReg,
FieldMemOperand(a1, JSGeneratorObject::kReceiverOffset));
__ Push(kScratchReg);
}
// Underlying function needs to have bytecode available.
if (v8_flags.debug_code) {
Label is_baseline;
__ LoadTaggedField(
a3, FieldMemOperand(a4, JSFunction::kSharedFunctionInfoOffset));
__ LoadTaggedField(
a3, FieldMemOperand(a3, SharedFunctionInfo::kFunctionDataOffset));
GetSharedFunctionInfoBytecodeOrBaseline(masm, a3, a0, &is_baseline);
__ GetObjectType(a3, a3, a3);
__ Assert(eq, AbortReason::kMissingBytecodeArray, a3,
Operand(BYTECODE_ARRAY_TYPE));
__ bind(&is_baseline);
}
// Resume (Ignition/TurboFan) generator object.
{
__ LoadTaggedField(
a0, FieldMemOperand(a4, JSFunction::kSharedFunctionInfoOffset));
__ Lhu(a0, FieldMemOperand(
a0, SharedFunctionInfo::kFormalParameterCountOffset));
// We abuse new.target both to indicate that this is a resume call and to
// pass in the generator object. In ordinary calls, new.target is always
// undefined because generator functions are non-constructable.
__ Move(a3, a1);
__ Move(a1, a4);
static_assert(kJavaScriptCallCodeStartRegister == a2, "ABI mismatch");
__ LoadTaggedField(a2, FieldMemOperand(a1, JSFunction::kCodeOffset));
__ JumpCodeObject(a2);
}
__ bind(&prepare_step_in_if_stepping);
{
FrameScope scope(masm, StackFrame::INTERNAL);
__ Push(a1, a4);
// Push hole as receiver since we do not use it for stepping.
__ PushRoot(RootIndex::kTheHoleValue);
__ CallRuntime(Runtime::kDebugOnFunctionCall);
__ Pop(a1);
}
__ LoadTaggedField(a4,
FieldMemOperand(a1, JSGeneratorObject::kFunctionOffset));
__ Branch(&stepping_prepared);
__ bind(&prepare_step_in_suspended_generator);
{
FrameScope scope(masm, StackFrame::INTERNAL);
__ Push(a1);
__ CallRuntime(Runtime::kDebugPrepareStepInSuspendedGenerator);
__ Pop(a1);
}
__ LoadTaggedField(a4,
FieldMemOperand(a1, JSGeneratorObject::kFunctionOffset));
__ Branch(&stepping_prepared);
__ bind(&stack_overflow);
{
FrameScope scope(masm, StackFrame::INTERNAL);
__ CallRuntime(Runtime::kThrowStackOverflow);
__ break_(0xCC); // This should be unreachable.
}
}
void Builtins::Generate_ConstructedNonConstructable(MacroAssembler* masm) {
FrameScope scope(masm, StackFrame::INTERNAL);
__ Push(a1);
__ CallRuntime(Runtime::kThrowConstructedNonConstructable);
}
// Clobbers scratch1 and scratch2; preserves all other registers.
static void Generate_CheckStackOverflow(MacroAssembler* masm, Register argc,
Register scratch1, Register scratch2) {
// Check the stack for overflow. We are not trying to catch
// interruptions (e.g. debug break and preemption) here, so the "real stack
// limit" is checked.
Label okay;
__ LoadStackLimit(scratch1, MacroAssembler::StackLimitKind::kRealStackLimit);
// Make a2 the space we have left. The stack might already be overflowed
// here which will cause r2 to become negative.
__ SubWord(scratch1, sp, scratch1);
// Check if the arguments will overflow the stack.
__ SllWord(scratch2, argc, kSystemPointerSizeLog2);
__ Branch(&okay, gt, scratch1, Operand(scratch2),
Label::Distance::kNear); // Signed comparison.
// Out of stack space.
__ CallRuntime(Runtime::kThrowStackOverflow);
__ bind(&okay);
}
namespace {
// Called with the native C calling convention. The corresponding function
// signature is either:
//
// using JSEntryFunction = GeneratedCode<Address(
// Address root_register_value, Address new_target, Address target,
// Address receiver, intptr_t argc, Address** args)>;
// or
// using JSEntryFunction = GeneratedCode<Address(
// Address root_register_value, MicrotaskQueue* microtask_queue)>;
void Generate_JSEntryVariant(MacroAssembler* masm, StackFrame::Type type,
Builtin entry_trampoline) {
Label invoke, handler_entry, exit;
{
NoRootArrayScope no_root_array(masm);
// TODO(plind): unify the ABI description here.
// Registers:
// either
// a0: root register value
// a1: entry address
// a2: function
// a3: receiver
// a4: argc
// a5: argv
// or
// a0: root register value
// a1: microtask_queue
// Save callee saved registers on the stack.
__ MultiPush(kCalleeSaved | ra);
// Save callee-saved FPU registers.
__ MultiPushFPU(kCalleeSavedFPU);
// Set up the reserved register for 0.0.
__ LoadFPRImmediate(kDoubleRegZero, 0.0);
__ LoadFPRImmediate(kSingleRegZero, 0.0f);
// Initialize the root register.
// C calling convention. The first argument is passed in a0.
__ Move(kRootRegister, a0);
#ifdef V8_COMPRESS_POINTERS
// Initialize the pointer cage base register.
__ LoadRootRelative(kPtrComprCageBaseRegister,
IsolateData::cage_base_offset());
#endif
}
// a1: entry address
// a2: function
// a3: receiver
// a4: argc
// a5: argv
// We build an EntryFrame.
__ li(s1, Operand(-1)); // Push a bad frame pointer to fail if it is used.
__ li(s2, Operand(StackFrame::TypeToMarker(type)));
__ li(s3, Operand(StackFrame::TypeToMarker(type)));
ExternalReference c_entry_fp = ExternalReference::Create(
IsolateAddressId::kCEntryFPAddress, masm->isolate());
__ li(s5, c_entry_fp);
__ LoadWord(s4, MemOperand(s5));
__ Push(s1, s2, s3, s4);
// Clear c_entry_fp, now we've pushed its previous value to the stack.
// If the c_entry_fp is not already zero and we don't clear it, the
// StackFrameIteratorForProfiler will assume we are executing C++ and miss the
// JS frames on top.
__ StoreWord(zero_reg, MemOperand(s5));
// Set up frame pointer for the frame to be pushed.
__ AddWord(fp, sp, -EntryFrameConstants::kNextExitFrameFPOffset);
// Registers:
// either
// a1: entry address
// a2: function
// a3: receiver
// a4: argc
// a5: argv
// or
// a1: microtask_queue
//
// Stack:
// caller fp |
// function slot | entry frame
// context slot |
// bad fp (0xFF...F) |
// callee saved registers + ra
// [ O32: 4 args slots]
// args
// If this is the outermost JS call, set js_entry_sp value.
Label non_outermost_js;
ExternalReference js_entry_sp = ExternalReference::Create(
IsolateAddressId::kJSEntrySPAddress, masm->isolate());
__ li(s1, js_entry_sp);
__ LoadWord(s2, MemOperand(s1));
__ Branch(&non_outermost_js, ne, s2, Operand(zero_reg),
Label::Distance::kNear);
__ StoreWord(fp, MemOperand(s1));
__ li(s3, Operand(StackFrame::OUTERMOST_JSENTRY_FRAME));
Label cont;
__ Branch(&cont);
__ bind(&non_outermost_js);
__ li(s3, Operand(StackFrame::INNER_JSENTRY_FRAME));
__ bind(&cont);
__ push(s3);
// Jump to a faked try block that does the invoke, with a faked catch
// block that sets the pending exception.
__ BranchShort(&invoke);
__ bind(&handler_entry);
// Store the current pc as the handler offset. It's used later to create the
// handler table.
masm->isolate()->builtins()->SetJSEntryHandlerOffset(handler_entry.pos());
// Caught exception: Store result (exception) in the pending exception
// field in the JSEnv and return a failure sentinel. Coming in here the
// fp will be invalid because the PushStackHandler below sets it to 0 to
// signal the existence of the JSEntry frame.
__ li(s1, ExternalReference::Create(
IsolateAddressId::kPendingExceptionAddress, masm->isolate()));
__ StoreWord(a0,
MemOperand(s1)); // We come back from 'invoke'. result is in a0.
__ LoadRoot(a0, RootIndex::kException);
__ BranchShort(&exit);
// Invoke: Link this frame into the handler chain.
__ bind(&invoke);
__ PushStackHandler();
// If an exception not caught by another handler occurs, this handler
// returns control to the code after the bal(&invoke) above, which
// restores all kCalleeSaved registers (including cp and fp) to their
// saved values before returning a failure to C.
//
// Registers:
// either
// a0: root register value
// a1: entry address
// a2: function
// a3: receiver
// a4: argc
// a5: argv
// or
// a0: root register value
// a1: microtask_queue
//
// Stack:
// handler frame
// entry frame
// callee saved registers + ra
// [ O32: 4 args slots]
// args
//
// Invoke the function by calling through JS entry trampoline builtin and
// pop the faked function when we return.
Handle<Code> trampoline_code =
masm->isolate()->builtins()->code_handle(entry_trampoline);
__ Call(trampoline_code, RelocInfo::CODE_TARGET);
// Unlink this frame from the handler chain.
__ PopStackHandler();
__ bind(&exit); // a0 holds result
// Check if the current stack frame is marked as the outermost JS frame.
Label non_outermost_js_2;
__ pop(a5);
__ Branch(&non_outermost_js_2, ne, a5,
Operand(StackFrame::OUTERMOST_JSENTRY_FRAME),
Label::Distance::kNear);
__ li(a5, js_entry_sp);
__ StoreWord(zero_reg, MemOperand(a5));
__ bind(&non_outermost_js_2);
// Restore the top frame descriptors from the stack.
__ pop(a5);
__ li(a4, ExternalReference::Create(IsolateAddressId::kCEntryFPAddress,
masm->isolate()));
__ StoreWord(a5, MemOperand(a4));
// Reset the stack to the callee saved registers.
__ AddWord(sp, sp, -EntryFrameConstants::kNextExitFrameFPOffset);
// Restore callee-saved fpu registers.
__ MultiPopFPU(kCalleeSavedFPU);
// Restore callee saved registers from the stack.
__ MultiPop(kCalleeSaved | ra);
// Return.
__ Jump(ra);
}
} // namespace
void Builtins::Generate_JSEntry(MacroAssembler* masm) {
Generate_JSEntryVariant(masm, StackFrame::ENTRY, Builtin::kJSEntryTrampoline);
}
void Builtins::Generate_JSConstructEntry(MacroAssembler* masm) {
Generate_JSEntryVariant(masm, StackFrame::CONSTRUCT_ENTRY,
Builtin::kJSConstructEntryTrampoline);
}
void Builtins::Generate_JSRunMicrotasksEntry(MacroAssembler* masm) {
Generate_JSEntryVariant(masm, StackFrame::ENTRY,
Builtin::kRunMicrotasksTrampoline);
}
static void Generate_JSEntryTrampolineHelper(MacroAssembler* masm,
bool is_construct) {
// ----------- S t a t e -------------
// -- a1: new.target
// -- a2: function
// -- a3: receiver_pointer
// -- a4: argc
// -- a5: argv
// -----------------------------------
// Enter an internal frame.
{
FrameScope scope(masm, StackFrame::INTERNAL);
// Setup the context (we need to use the caller context from the isolate).
ExternalReference context_address = ExternalReference::Create(
IsolateAddressId::kContextAddress, masm->isolate());
__ li(cp, context_address);
__ LoadWord(cp, MemOperand(cp));
// Push the function onto the stack.
__ Push(a2);
// Check if we have enough stack space to push all arguments.
__ mv(a6, a4);
Generate_CheckStackOverflow(masm, a6, a0, s2);
// Copy arguments to the stack.
// a4: argc
// a5: argv, i.e. points to first arg
{
UseScratchRegisterScope temps(masm);
Generate_PushArguments(masm, a5, a4, temps.Acquire(), temps.Acquire(),
ArgumentsElementType::kHandle);
}
// Push the receive.
__ Push(a3);
// a0: argc
// a1: function
// a3: new.target
__ Move(a3, a1);
__ Move(a1, a2);
__ Move(a0, a4);
// Initialize all JavaScript callee-saved registers, since they will be seen
// by the garbage collector as part of handlers.
__ LoadRoot(a4, RootIndex::kUndefinedValue);
__ Move(a5, a4);
__ Move(s1, a4);
__ Move(s2, a4);
__ Move(s3, a4);
__ Move(s4, a4);
__ Move(s5, a4);
#ifndef V8_COMPRESS_POINTERS
__ Move(s11, a4);
#endif
// s6 holds the root address. Do not clobber.
// s7 is cp. Do not init.
// Invoke the code.
Handle<Code> builtin = is_construct
? BUILTIN_CODE(masm->isolate(), Construct)
: masm->isolate()->builtins()->Call();
__ Call(builtin, RelocInfo::CODE_TARGET);
// Leave internal frame.
}
__ Jump(ra);
}
void Builtins::Generate_JSEntryTrampoline(MacroAssembler* masm) {
Generate_JSEntryTrampolineHelper(masm, false);
}
void Builtins::Generate_JSConstructEntryTrampoline(MacroAssembler* masm) {
Generate_JSEntryTrampolineHelper(masm, true);
}
void Builtins::Generate_RunMicrotasksTrampoline(MacroAssembler* masm) {
// a1: microtask_queue
__ Move(RunMicrotasksDescriptor::MicrotaskQueueRegister(), a1);
__ Jump(BUILTIN_CODE(masm->isolate(), RunMicrotasks), RelocInfo::CODE_TARGET);
}
static void LeaveInterpreterFrame(MacroAssembler* masm, Register scratch1,
Register scratch2) {
ASM_CODE_COMMENT(masm);
Register params_size = scratch1;
// Get the size of the formal parameters + receiver (in bytes).
__ LoadWord(params_size,
MemOperand(fp, InterpreterFrameConstants::kBytecodeArrayFromFp));
__ Lw(params_size,
FieldMemOperand(params_size, BytecodeArray::kParameterSizeOffset));
Register actual_params_size = scratch2;
Label L1;
// Compute the size of the actual parameters + receiver (in bytes).
__ LoadWord(actual_params_size,
MemOperand(fp, StandardFrameConstants::kArgCOffset));
__ SllWord(actual_params_size, actual_params_size, kSystemPointerSizeLog2);
// If actual is bigger than formal, then we should use it to free up the stack
// arguments.
__ Branch(&L1, le, actual_params_size, Operand(params_size),
Label::Distance::kNear);
__ Move(params_size, actual_params_size);
__ bind(&L1);
// Leave the frame (also dropping the register file).
__ LeaveFrame(StackFrame::INTERPRETED);
// Drop receiver + arguments.
__ DropArguments(params_size, MacroAssembler::kCountIsBytes,
MacroAssembler::kCountIncludesReceiver);
}
// Advance the current bytecode offset. This simulates what all bytecode
// handlers do upon completion of the underlying operation. Will bail out to a
// label if the bytecode (without prefix) is a return bytecode. Will not advance
// the bytecode offset if the current bytecode is a JumpLoop, instead just
// re-executing the JumpLoop to jump to the correct bytecode.
static void AdvanceBytecodeOffsetOrReturn(MacroAssembler* masm,
Register bytecode_array,
Register bytecode_offset,
Register bytecode, Register scratch1,
Register scratch2, Register scratch3,
Label* if_return) {
ASM_CODE_COMMENT(masm);
Register bytecode_size_table = scratch1;
// The bytecode offset value will be increased by one in wide and extra wide
// cases. In the case of having a wide or extra wide JumpLoop bytecode, we
// will restore the original bytecode. In order to simplify the code, we have
// a backup of it.
Register original_bytecode_offset = scratch3;
DCHECK(!AreAliased(bytecode_array, bytecode_offset, bytecode,
bytecode_size_table, original_bytecode_offset));
__ Move(original_bytecode_offset, bytecode_offset);
__ li(bytecode_size_table, ExternalReference::bytecode_size_table_address());
// Check if the bytecode is a Wide or ExtraWide prefix bytecode.
Label process_bytecode, extra_wide;
static_assert(0 == static_cast<int>(interpreter::Bytecode::kWide));
static_assert(1 == static_cast<int>(interpreter::Bytecode::kExtraWide));
static_assert(2 == static_cast<int>(interpreter::Bytecode::kDebugBreakWide));
static_assert(3 ==
static_cast<int>(interpreter::Bytecode::kDebugBreakExtraWide));
__ Branch(&process_bytecode, Ugreater, bytecode, Operand(3),
Label::Distance::kNear);
__ And(scratch2, bytecode, Operand(1));
__ Branch(&extra_wide, ne, scratch2, Operand(zero_reg),
Label::Distance::kNear);
// Load the next bytecode and update table to the wide scaled table.
__ AddWord(bytecode_offset, bytecode_offset, Operand(1));
__ AddWord(scratch2, bytecode_array, bytecode_offset);
__ Lbu(bytecode, MemOperand(scratch2));
__ AddWord(bytecode_size_table, bytecode_size_table,
Operand(kByteSize * interpreter::Bytecodes::kBytecodeCount));
__ BranchShort(&process_bytecode);
__ bind(&extra_wide);
// Load the next bytecode and update table to the extra wide scaled table.
__ AddWord(bytecode_offset, bytecode_offset, Operand(1));
__ AddWord(scratch2, bytecode_array, bytecode_offset);
__ Lbu(bytecode, MemOperand(scratch2));
__ AddWord(bytecode_size_table, bytecode_size_table,
Operand(2 * kByteSize * interpreter::Bytecodes::kBytecodeCount));
__ bind(&process_bytecode);
// Bailout to the return label if this is a return bytecode.
#define JUMP_IF_EQUAL(NAME) \
__ Branch(if_return, eq, bytecode, \
Operand(static_cast<int64_t>(interpreter::Bytecode::k##NAME)));
RETURN_BYTECODE_LIST(JUMP_IF_EQUAL)
#undef JUMP_IF_EQUAL
// If this is a JumpLoop, re-execute it to perform the jump to the beginning
// of the loop.
Label end, not_jump_loop;
__ Branch(¬_jump_loop, ne, bytecode,
Operand(static_cast<int64_t>(interpreter::Bytecode::kJumpLoop)),
Label::Distance::kNear);
// We need to restore the original bytecode_offset since we might have
// increased it to skip the wide / extra-wide prefix bytecode.
__ Move(bytecode_offset, original_bytecode_offset);
__ BranchShort(&end);
__ bind(¬_jump_loop);
// Otherwise, load the size of the current bytecode and advance the offset.
__ AddWord(scratch2, bytecode_size_table, bytecode);
__ Lb(scratch2, MemOperand(scratch2));
__ AddWord(bytecode_offset, bytecode_offset, scratch2);
__ bind(&end);
}
namespace {
void ResetBytecodeAge(MacroAssembler* masm, Register bytecode_array) {
__ Sh(zero_reg,
FieldMemOperand(bytecode_array, BytecodeArray::kBytecodeAgeOffset));
}
void ResetFeedbackVectorOsrUrgency(MacroAssembler* masm,
Register feedback_vector, Register scratch) {
DCHECK(!AreAliased(feedback_vector, scratch));
__ Lbu(scratch,
FieldMemOperand(feedback_vector, FeedbackVector::kOsrStateOffset));
__ And(scratch, scratch,
Operand(FeedbackVector::MaybeHasOptimizedOsrCodeBit::kMask));
__ Sb(scratch,
FieldMemOperand(feedback_vector, FeedbackVector::kOsrStateOffset));
}
} // namespace
// static
void Builtins::Generate_BaselineOutOfLinePrologueDeopt(MacroAssembler* masm) {
// We're here because we got deopted during BaselineOutOfLinePrologue's stack
// check. Undo all its frame creation and call into the interpreter instead.
// Drop bytecode offset (was the feedback vector but got replaced during
// deopt) and bytecode array.
__ AddWord(sp, sp, Operand(2 * kPointerSize));
// Context, closure, argc.
__ Pop(kContextRegister, kJavaScriptCallTargetRegister,
kJavaScriptCallArgCountRegister);
// Drop frame pointer
__ LeaveFrame(StackFrame::BASELINE);
// Enter the interpreter.
__ TailCallBuiltin(Builtin::kInterpreterEntryTrampoline);
}
void Builtins::Generate_BaselineOutOfLinePrologue(MacroAssembler* masm) {
UseScratchRegisterScope temps(masm);
temps.Include({kScratchReg, kScratchReg2});
auto descriptor =
Builtins::CallInterfaceDescriptorFor(Builtin::kBaselineOutOfLinePrologue);
Register closure = descriptor.GetRegisterParameter(
BaselineOutOfLinePrologueDescriptor::kClosure);
// Load the feedback vector from the closure.
Register feedback_vector = temps.Acquire();
__ LoadWord(feedback_vector,
FieldMemOperand(closure, JSFunction::kFeedbackCellOffset));
__ LoadWord(feedback_vector,
FieldMemOperand(feedback_vector, Cell::kValueOffset));
{
UseScratchRegisterScope temp(masm);