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