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 X64TargetBuilder final : public LLVMTargetBuilder {
22public:
23    ~X64TargetBuilder() override = default;
24
25    LLVMValueRef GetASMBarrierCall(LLVMModule* llvmModule) override
26    {
27        std::string asmCall = "call *${0:c}"; // call to the first input register.
28        std::string constraints = "r,{rdi},{rsi},{rdx},{rcx},"
29        // input registerds, first is the runtime check barrier stub.
30        // others are same with the sign of runtime check barrier stub.
31        "~{r11},~{dirflag},~{fpsr},~{flags},"
32        // r11 will be used as scratch register, so mark it as clobbered, all the flag registers are also clobbered.
33        "~{xmm0},~{xmm1},~{xmm2},~{xmm3},~{xmm4},~{xmm5},~{xmm6},~{xmm7},"
34        "~{xmm8},~{xmm9},~{xmm10},~{xmm11},~{xmm12},~{xmm13},~{xmm14},~{xmm15}";
35        // can't promise the vector registers are preserved, so mark them clobbered.
36        // NOTE: if AVX512 or more vector registers are enabled, need add them to clobber list.
37        const CallSignature* cs = RuntimeStubCSigns::Get(RuntimeStubCSigns::ID_ASMFastWriteBarrier);
38        std::vector<LLVMTypeRef> paramTys;
39        paramTys.push_back(llvmModule->GetRawPtrT()); // add the runtime check barrier stub as the first arg.
40        const size_t count = cs->GetParametersCount();
41        const VariableType* originParamType = cs->GetParametersType();
42        for (size_t i = 0; i < count; i++) {
43            paramTys.push_back(llvmModule->ConvertLLVMTypeFromVariableType(originParamType[i]));
44        }
45        LLVMTypeRef functype = LLVMFunctionType(llvmModule->GetVoidT(), paramTys.data(), paramTys.size(), false);
46#if defined(PANDA_TARGET_MACOS)
47        return LLVMGetInlineAsm(functype, asmCall.data(), asmCall.size(), constraints.data(),
48                                constraints.size(), true, true, LLVMInlineAsmDialectATT);
49#else
50        return LLVMGetInlineAsm(functype, asmCall.data(), asmCall.size(), constraints.data(),
51                                constraints.size(), true, true, LLVMInlineAsmDialectATT, false);
52#endif
53    }
54};
55
56class X64TargetBuilderRegistry
57{
58public:
59    X64TargetBuilderRegistry()
60    {
61        LLVMIRBuilder::RegisterTargetBuilder(TARGET_X64, []()-> LLVMTargetBuilder* {
62            return new X64TargetBuilder();
63        });
64    }
65};
66
67X64TargetBuilderRegistry g_x64Registry;
68}
69