1 // Copyright 2021 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef V8_BASELINE_ARM64_BASELINE_COMPILER_ARM64_INL_H_
6 #define V8_BASELINE_ARM64_BASELINE_COMPILER_ARM64_INL_H_
7
8 #include "src/baseline/baseline-compiler.h"
9
10 namespace v8 {
11 namespace internal {
12 namespace baseline {
13
14 #define __ basm_.
15
Prologue()16 void BaselineCompiler::Prologue() {
17 ASM_CODE_COMMENT(&masm_);
18 // Enter the frame here, since CallBuiltin will override lr.
19 __ masm()->EnterFrame(StackFrame::BASELINE);
20 DCHECK_EQ(kJSFunctionRegister, kJavaScriptCallTargetRegister);
21 int max_frame_size =
22 bytecode_->frame_size() + max_call_args_ * kSystemPointerSize;
23 CallBuiltin<Builtin::kBaselineOutOfLinePrologue>(
24 kContextRegister, kJSFunctionRegister, kJavaScriptCallArgCountRegister,
25 max_frame_size, kJavaScriptCallNewTargetRegister, bytecode_);
26
27 __ masm()->AssertSpAligned();
28 PrologueFillFrame();
29 __ masm()->AssertSpAligned();
30 }
31
PrologueFillFrame()32 void BaselineCompiler::PrologueFillFrame() {
33 ASM_CODE_COMMENT(&masm_);
34 // Inlined register frame fill
35 interpreter::Register new_target_or_generator_register =
36 bytecode_->incoming_new_target_or_generator_register();
37 if (FLAG_debug_code) {
38 __ masm()->CompareRoot(kInterpreterAccumulatorRegister,
39 RootIndex::kUndefinedValue);
40 __ masm()->Assert(eq, AbortReason::kUnexpectedValue);
41 }
42 int register_count = bytecode_->register_count();
43 // Magic value
44 const int kLoopUnrollSize = 8;
45 const int new_target_index = new_target_or_generator_register.index();
46 const bool has_new_target = new_target_index != kMaxInt;
47 // BaselineOutOfLinePrologue already pushed one undefined.
48 register_count -= 1;
49 if (has_new_target) {
50 if (new_target_index == 0) {
51 // Oops, need to fix up that undefined that BaselineOutOfLinePrologue
52 // pushed.
53 __ masm()->Poke(kJavaScriptCallNewTargetRegister, Operand(0));
54 } else {
55 DCHECK_LE(new_target_index, register_count);
56 int index = 1;
57 for (; index + 2 <= new_target_index; index += 2) {
58 __ masm()->Push(kInterpreterAccumulatorRegister,
59 kInterpreterAccumulatorRegister);
60 }
61 if (index == new_target_index) {
62 __ masm()->Push(kJavaScriptCallNewTargetRegister,
63 kInterpreterAccumulatorRegister);
64 } else {
65 DCHECK_EQ(index, new_target_index - 1);
66 __ masm()->Push(kInterpreterAccumulatorRegister,
67 kJavaScriptCallNewTargetRegister);
68 }
69 // We pushed "index" registers, minus the one the prologue pushed, plus
70 // the two registers that included new_target.
71 register_count -= (index - 1 + 2);
72 }
73 }
74 if (register_count < 2 * kLoopUnrollSize) {
75 // If the frame is small enough, just unroll the frame fill completely.
76 for (int i = 0; i < register_count; i += 2) {
77 __ masm()->Push(kInterpreterAccumulatorRegister,
78 kInterpreterAccumulatorRegister);
79 }
80 } else {
81 BaselineAssembler::ScratchRegisterScope temps(&basm_);
82 Register scratch = temps.AcquireScratch();
83
84 // Extract the first few registers to round to the unroll size.
85 int first_registers = register_count % kLoopUnrollSize;
86 for (int i = 0; i < first_registers; i += 2) {
87 __ masm()->Push(kInterpreterAccumulatorRegister,
88 kInterpreterAccumulatorRegister);
89 }
90 __ Move(scratch, register_count / kLoopUnrollSize);
91 // We enter the loop unconditionally, so make sure we need to loop at least
92 // once.
93 DCHECK_GT(register_count / kLoopUnrollSize, 0);
94 Label loop;
95 __ Bind(&loop);
96 for (int i = 0; i < kLoopUnrollSize; i += 2) {
97 __ masm()->Push(kInterpreterAccumulatorRegister,
98 kInterpreterAccumulatorRegister);
99 }
100 __ masm()->Subs(scratch, scratch, 1);
101 __ masm()->B(gt, &loop);
102 }
103 }
104
VerifyFrameSize()105 void BaselineCompiler::VerifyFrameSize() {
106 ASM_CODE_COMMENT(&masm_);
107 __ masm()->Add(x15, sp,
108 RoundUp(InterpreterFrameConstants::kFixedFrameSizeFromFp +
109 bytecode_->frame_size(),
110 2 * kSystemPointerSize));
111 __ masm()->Cmp(x15, fp);
112 __ masm()->Assert(eq, AbortReason::kUnexpectedStackPointer);
113 }
114
115 #undef __
116
117 } // namespace baseline
118 } // namespace internal
119 } // namespace v8
120
121 #endif // V8_BASELINE_ARM64_BASELINE_COMPILER_ARM64_INL_H_
122