14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2022-2024 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ci#include "ecmascript/compiler/assembler/aarch64/extend_assembler.h"
174514f5e3Sopenharmony_ci
184514f5e3Sopenharmony_ci#include "ecmascript/frames.h"
194514f5e3Sopenharmony_ci
204514f5e3Sopenharmony_cinamespace panda::ecmascript::aarch64 {
214514f5e3Sopenharmony_ciRegister ExtendedAssembler::ghcJSCallDispacherArgs_[JS_CALL_DISPATCHER_ARGS_COUNT] = {
224514f5e3Sopenharmony_ci    X19, FP, X20, X21, X22, X23, X24, X25, X26
234514f5e3Sopenharmony_ci};
244514f5e3Sopenharmony_ciRegister ExtendedAssembler::cppJSCallDispacherArgs_[JS_CALL_DISPATCHER_ARGS_COUNT] = {
254514f5e3Sopenharmony_ci    X0, FP, X1, X2, X3, X4, X5, X6, X7
264514f5e3Sopenharmony_ci};
274514f5e3Sopenharmony_ci
284514f5e3Sopenharmony_civoid ExtendedAssembler::CalleeSave()
294514f5e3Sopenharmony_ci{
304514f5e3Sopenharmony_ci    Register sp(SP);
314514f5e3Sopenharmony_ci    Stp(Register(X27), Register(X28), MemoryOperand(sp, -PAIR_SLOT_SIZE, PREINDEX));
324514f5e3Sopenharmony_ci    Stp(Register(X25), Register(X26), MemoryOperand(sp, -PAIR_SLOT_SIZE, PREINDEX));
334514f5e3Sopenharmony_ci    Stp(Register(X23), Register(X24), MemoryOperand(sp, -PAIR_SLOT_SIZE, PREINDEX));
344514f5e3Sopenharmony_ci    Stp(Register(X21), Register(X22), MemoryOperand(sp, -PAIR_SLOT_SIZE, PREINDEX));
354514f5e3Sopenharmony_ci    Stp(Register(X19), Register(X20), MemoryOperand(sp, -PAIR_SLOT_SIZE, PREINDEX));
364514f5e3Sopenharmony_ci
374514f5e3Sopenharmony_ci    Stp(VectorRegister(v14), VectorRegister(v15), MemoryOperand(sp, -PAIR_SLOT_SIZE, PREINDEX));
384514f5e3Sopenharmony_ci    Stp(VectorRegister(v12), VectorRegister(v13), MemoryOperand(sp, -PAIR_SLOT_SIZE, PREINDEX));
394514f5e3Sopenharmony_ci    Stp(VectorRegister(v10), VectorRegister(v11), MemoryOperand(sp, -PAIR_SLOT_SIZE, PREINDEX));
404514f5e3Sopenharmony_ci    Stp(VectorRegister(v8), VectorRegister(v9), MemoryOperand(sp, -PAIR_SLOT_SIZE, PREINDEX));
414514f5e3Sopenharmony_ci}
424514f5e3Sopenharmony_ci
434514f5e3Sopenharmony_civoid ExtendedAssembler::CalleeRestore()
444514f5e3Sopenharmony_ci{
454514f5e3Sopenharmony_ci    Register sp(SP);
464514f5e3Sopenharmony_ci    Ldp(VectorRegister(v8), VectorRegister(v9), MemoryOperand(sp, PAIR_SLOT_SIZE, POSTINDEX));
474514f5e3Sopenharmony_ci    Ldp(VectorRegister(v10), VectorRegister(v11), MemoryOperand(sp, PAIR_SLOT_SIZE, POSTINDEX));
484514f5e3Sopenharmony_ci    Ldp(VectorRegister(v12), VectorRegister(v13), MemoryOperand(sp, PAIR_SLOT_SIZE, POSTINDEX));
494514f5e3Sopenharmony_ci    Ldp(VectorRegister(v14), VectorRegister(v15), MemoryOperand(sp, PAIR_SLOT_SIZE, POSTINDEX));
504514f5e3Sopenharmony_ci    Ldp(Register(X19), Register(X20), MemoryOperand(sp, PAIR_SLOT_SIZE, POSTINDEX));
514514f5e3Sopenharmony_ci    Ldp(Register(X21), Register(X22), MemoryOperand(sp, PAIR_SLOT_SIZE, POSTINDEX));
524514f5e3Sopenharmony_ci    Ldp(Register(X23), Register(X24), MemoryOperand(sp, PAIR_SLOT_SIZE, POSTINDEX));
534514f5e3Sopenharmony_ci    Ldp(Register(X25), Register(X26), MemoryOperand(sp, PAIR_SLOT_SIZE, POSTINDEX));
544514f5e3Sopenharmony_ci    Ldp(Register(X27), Register(X28), MemoryOperand(sp, PAIR_SLOT_SIZE, POSTINDEX));
554514f5e3Sopenharmony_ci}
564514f5e3Sopenharmony_ci
574514f5e3Sopenharmony_civoid ExtendedAssembler::CallAssemblerStub(int id, bool isTail)
584514f5e3Sopenharmony_ci{
594514f5e3Sopenharmony_ci    Label *target = module_->GetFunctionLabel(id);
604514f5e3Sopenharmony_ci    isTail ? B(target) : Bl(target);
614514f5e3Sopenharmony_ci}
624514f5e3Sopenharmony_ci
634514f5e3Sopenharmony_civoid ExtendedAssembler::BindAssemblerStub(int id)
644514f5e3Sopenharmony_ci{
654514f5e3Sopenharmony_ci    Label *target = module_->GetFunctionLabel(id);
664514f5e3Sopenharmony_ci    Bind(target);
674514f5e3Sopenharmony_ci    auto callSigns = module_->GetCSigns();
684514f5e3Sopenharmony_ci    auto cs = callSigns[id];
694514f5e3Sopenharmony_ci    isGhcCallingConv_ = cs->GetCallConv() == kungfu::CallSignature::CallConv::GHCCallConv;
704514f5e3Sopenharmony_ci}
714514f5e3Sopenharmony_ci
724514f5e3Sopenharmony_civoid ExtendedAssembler::PushFpAndLr()
734514f5e3Sopenharmony_ci{
744514f5e3Sopenharmony_ci    Register sp(SP);
754514f5e3Sopenharmony_ci    Stp(Register(X29), Register(X30), MemoryOperand(sp, -PAIR_SLOT_SIZE, PREINDEX));
764514f5e3Sopenharmony_ci}
774514f5e3Sopenharmony_ci
784514f5e3Sopenharmony_civoid ExtendedAssembler::SaveFpAndLr()
794514f5e3Sopenharmony_ci{
804514f5e3Sopenharmony_ci    Register sp(SP);
814514f5e3Sopenharmony_ci    Stp(Register(X29), Register(X30), MemoryOperand(sp, -PAIR_SLOT_SIZE, PREINDEX));
824514f5e3Sopenharmony_ci    Mov(Register(X29), Register(SP));
834514f5e3Sopenharmony_ci}
844514f5e3Sopenharmony_ci
854514f5e3Sopenharmony_civoid ExtendedAssembler::RestoreFpAndLr()
864514f5e3Sopenharmony_ci{
874514f5e3Sopenharmony_ci    Register sp(SP);
884514f5e3Sopenharmony_ci    Ldp(Register(X29), Register(X30), MemoryOperand(sp, PAIR_SLOT_SIZE, POSTINDEX));
894514f5e3Sopenharmony_ci}
904514f5e3Sopenharmony_ci
914514f5e3Sopenharmony_civoid ExtendedAssembler::PushLrAndFp()
924514f5e3Sopenharmony_ci{
934514f5e3Sopenharmony_ci    Register sp(SP);
944514f5e3Sopenharmony_ci    Stp(Register(X30), Register(X29), MemoryOperand(sp, -PAIR_SLOT_SIZE, PREINDEX));
954514f5e3Sopenharmony_ci}
964514f5e3Sopenharmony_ci
974514f5e3Sopenharmony_civoid ExtendedAssembler::SaveLrAndFp()
984514f5e3Sopenharmony_ci{
994514f5e3Sopenharmony_ci    Register sp(SP);
1004514f5e3Sopenharmony_ci    Stp(Register(X30), Register(X29), MemoryOperand(sp, -PAIR_SLOT_SIZE, PREINDEX));
1014514f5e3Sopenharmony_ci    Mov(Register(X29), Register(SP));
1024514f5e3Sopenharmony_ci}
1034514f5e3Sopenharmony_ci
1044514f5e3Sopenharmony_civoid ExtendedAssembler::RestoreLrAndFp()
1054514f5e3Sopenharmony_ci{
1064514f5e3Sopenharmony_ci    Register sp(SP);
1074514f5e3Sopenharmony_ci    Ldp(Register(X30), Register(X29), MemoryOperand(sp, PAIR_SLOT_SIZE, POSTINDEX));
1084514f5e3Sopenharmony_ci}
1094514f5e3Sopenharmony_ci
1104514f5e3Sopenharmony_civoid ExtendedAssembler::PushArgc(int32_t argc, Register op, Register fp)
1114514f5e3Sopenharmony_ci{
1124514f5e3Sopenharmony_ci    Mov(op, Immediate(JSTaggedValue(argc).GetRawData()));
1134514f5e3Sopenharmony_ci    Str(op, MemoryOperand(fp, -8, PREINDEX));  // -8: 8 bytes
1144514f5e3Sopenharmony_ci}
1154514f5e3Sopenharmony_ci
1164514f5e3Sopenharmony_civoid ExtendedAssembler::PushArgc(Register argc, Register op, Register fp)
1174514f5e3Sopenharmony_ci{
1184514f5e3Sopenharmony_ci    Orr(op, argc, LogicalImmediate::Create(JSTaggedValue::TAG_INT, RegXSize));
1194514f5e3Sopenharmony_ci    Str(op, MemoryOperand(fp, -8, PREINDEX));  // -8: 8 bytes
1204514f5e3Sopenharmony_ci}
1214514f5e3Sopenharmony_ci
1224514f5e3Sopenharmony_civoid ExtendedAssembler::Align16(Register fp)
1234514f5e3Sopenharmony_ci{
1244514f5e3Sopenharmony_ci    Label aligned;
1254514f5e3Sopenharmony_ci    Tst(fp, LogicalImmediate::Create(0xf, RegXSize));  // 0xf: 0x1111
1264514f5e3Sopenharmony_ci    B(Condition::EQ, &aligned);
1274514f5e3Sopenharmony_ci    // 8: frame slot size
1284514f5e3Sopenharmony_ci    Sub(fp, fp, Immediate(8));
1294514f5e3Sopenharmony_ci    Bind(&aligned);
1304514f5e3Sopenharmony_ci}
1314514f5e3Sopenharmony_ci}  // namespace panda::ecmascript::aarch64