1/*
2* Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include "ecmascript/compiler/codegen/llvm/llvm_ir_builder.h"
17
18
19namespace panda::ecmascript::kungfu {
20
21class Aarch64TargetBuilder final : public LLVMTargetBuilder {
22public:
23    ~Aarch64TargetBuilder() override = default;
24
25    LLVMValueRef GetASMBarrierCall(LLVMModule* llvmModule) override
26    {
27        std::string asmCall = "blr $0"; // call to the first input register.
28        std::string constraints = "r,{x0},{x1},{x2},{x3},"
29            // input registerds, first is the runtime check barrier stub.
30            // others are same with the sign of runtime check barrier stub.
31            "~{x15},~{nzcv},~{fpsr},~{x30}"
32            // x15 will be used as scratch register, so mark it as clobbered, all the flag registers are also clobbered.
33            // lr will be early clobbered at call.
34            "~{v0},~{v1},~{v2},~{v3},~{v4},~{v5},~{v6},~{v7},~{v8},~{v9},~{v10},~{v11},~{v12},~{v13},~{v14},~{v15},"
35            "~{v16},~{v17},~{v18},~{v19},~{v20},~{v21},~{v22},~{v23},~{v24},~{v25},~{v26},~{v27},~{v28},~{v29},~{v30},"
36            "~{v31},"
37            "~{q0},~{q1},~{q2},~{q3},~{q4},~{q5},~{q6},~{q7},~{q8},~{q9},~{q10},~{q11},~{q12},~{q13},~{q14},~{q15},"
38            "~{q16},~{q17},~{q18},~{q19},~{q20},~{q21},~{q22},~{q23},~{q24},~{q25},~{q26},~{q27},~{q28},~{q29},~{q30},"
39            "~{q31}";
40        const CallSignature* cs = RuntimeStubCSigns::Get(RuntimeStubCSigns::ID_ASMFastWriteBarrier);
41        std::vector<LLVMTypeRef> paramTys;
42        paramTys.push_back(llvmModule->GetRawPtrT()); // add the runtime check barrier stub as the first arg.
43        const size_t count = cs->GetParametersCount();
44        const VariableType* originParamType = cs->GetParametersType();
45        for (size_t i = 0; i < count; i++) {
46            paramTys.push_back(llvmModule->ConvertLLVMTypeFromVariableType(originParamType[i]));
47        }
48        LLVMTypeRef functype = LLVMFunctionType(llvmModule->GetVoidT(), paramTys.data(), paramTys.size(), false);
49#if defined(PANDA_TARGET_MACOS)
50        return LLVMGetInlineAsm(functype, asmCall.data(), asmCall.size(), constraints.data(),
51                                constraints.size(), true, true, LLVMInlineAsmDialectATT);
52#else
53        return LLVMGetInlineAsm(functype, asmCall.data(), asmCall.size(), constraints.data(),
54                                constraints.size(), true, true, LLVMInlineAsmDialectATT, false);
55#endif
56    }
57};
58
59class Aarch64TargetBuilderRegistry
60{
61public:
62    Aarch64TargetBuilderRegistry()
63    {
64        LLVMIRBuilder::RegisterTargetBuilder(TARGET_AARCH64, []()-> LLVMTargetBuilder* {
65            return new Aarch64TargetBuilder();
66        });
67    }
68};
69
70Aarch64TargetBuilderRegistry g_aarch64Registry;
71}
72