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 Aarch64TargetBuilder final : public LLVMTargetBuilder { 22 public: 23 ~Aarch64TargetBuilder() 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 = "bl " + RuntimeStubCSigns::GetRTName(RuntimeStubCSigns::ID_ASMFastWriteBarrier); 31 inputRegs = "{x0},{x1},{x2},{x3},"; 32 // input registers are same with the sign of runtime check barrier stub. 33 } else { 34 asmCall = "blr $0"; // call to the first input register. 35 inputRegs = "r,{x0},{x1},{x2},{x3},"; 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 + "~{x15},~{nzcv},~{fpsr},~{x30}" 40 // x15 will be used as scratch register, so mark it as clobbered, all the flag registers are also clobbered. 41 // lr will be early clobbered at call. 42 "~{q0},~{q1},~{q2},~{q3},~{q4},~{q5},~{q6},~{q7}," 43 // d8 ~ d15 are callee saved in C calling conv, do not mark q8 ~ q15 clobbered, but we can't use q8 ~ q15 44 // cross such call site 45 "~{q16},~{q17},~{q18},~{q19},~{q20},~{q21},~{q22},~{q23},~{q24},~{q25},~{q26},~{q27},~{q28},~{q29},~{q30}," 46 "~{q31}"; 47 const CallSignature* cs = RuntimeStubCSigns::Get(RuntimeStubCSigns::ID_ASMFastWriteBarrier); 48 std::vector<LLVMTypeRef> paramTys; 49 if (!isDirectCall) { 50 paramTys.push_back(llvmModule->GetRawPtrT()); // add the runtime check barrier stub as the first arg. 51 } 52 const size_t count = cs->GetParametersCount(); 53 const VariableType* originParamType = cs->GetParametersType(); 54 for (size_t i = 0; i < count; i++) { 55 paramTys.push_back(llvmModule->ConvertLLVMTypeFromVariableType(originParamType[i])); 56 } 57 LLVMTypeRef functype = LLVMFunctionType(llvmModule->GetVoidT(), paramTys.data(), paramTys.size(), false); 58 #if defined(PANDA_TARGET_MACOS) 59 return LLVMGetInlineAsm(functype, asmCall.data(), asmCall.size(), constraints.data(), 60 constraints.size(), true, true, LLVMInlineAsmDialectATT); 61 #else 62 return LLVMGetInlineAsm(functype, asmCall.data(), asmCall.size(), constraints.data(), 63 constraints.size(), true, true, LLVMInlineAsmDialectATT, false); 64 #endif 65 } 66 }; 67 68 class Aarch64TargetBuilderRegistry { 69 public: Aarch64TargetBuilderRegistry()70 Aarch64TargetBuilderRegistry() 71 { 72 LLVMIRBuilder::RegisterTargetBuilder(TARGET_AARCH64, []()-> LLVMTargetBuilder* { 73 return new Aarch64TargetBuilder(); 74 }); 75 } 76 }; 77 78 Aarch64TargetBuilderRegistry g_aarch64Registry; 79 } 80