1 /* 2 * Copyright (c) 2023 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 #ifndef MAPLEBE_INCLUDE_CG_AARCH64_AARCH64_OBJ_EMIT_H 17 #define MAPLEBE_INCLUDE_CG_AARCH64_AARCH64_OBJ_EMIT_H 18 19 #include "obj_emit.h" 20 #include "aarch64_insn.h" 21 #include "aarch64_cg.h" 22 23 namespace maplebe { 24 enum AArch64FixupKind { 25 kAArch64PCRelAdrImm21 = kFirstTargetFixupKind, 26 kAArch64PCRelAdrpImm21, 27 kAArch64LoadPCRelImm19, 28 kAArch64CondBranchPCRelImm19, 29 kAArch64UnCondBranchPCRelImm26, 30 kAArch64CompareBranchPCRelImm19, 31 kAArch64TestBranchPCRelImm14, 32 kAArch64CallPCRelImm26, 33 kAArch64AddPCRelLo12, 34 kAArch64LdrPCRelLo12, 35 }; 36 37 class AArch64ObjFuncEmitInfo : public ObjFuncEmitInfo { 38 public: AArch64ObjFuncEmitInfo(CGFunc & func,MemPool & memPool)39 AArch64ObjFuncEmitInfo(CGFunc &func, MemPool &memPool) : ObjFuncEmitInfo(func, memPool) {} 40 ~AArch64ObjFuncEmitInfo() = default; 41 void HandleLocalBranchFixup(const std::vector<uint32> &label2Offset, 42 const std::vector<uint32> &symbol2Offset) override; 43 }; 44 45 class AArch64ObjEmitter : public ObjEmitter { 46 public: AArch64ObjEmitter(CG & cg,const std::string & objFileName)47 AArch64ObjEmitter(CG &cg, const std::string &objFileName) : ObjEmitter(cg, objFileName) {} 48 ~AArch64ObjEmitter() = default; 49 EncodeInstruction(const Insn & insn,const std::vector<uint32> & label2Offset,ObjFuncEmitInfo & objFuncEmitInfo)50 void EncodeInstruction(const Insn &insn, const std::vector<uint32> &label2Offset, 51 ObjFuncEmitInfo &objFuncEmitInfo) override 52 { 53 uint32 binInsn = GetBinaryCodeForInsn(insn, label2Offset, objFuncEmitInfo); 54 objFuncEmitInfo.AppendTextData(binInsn, k4ByteSize); 55 if (insn.GetMachineOpcode() == MOP_xbl || insn.GetMachineOpcode() == MOP_xblr) { 56 if (insn.GetStackMap() == nullptr) { 57 return; 58 } 59 objFuncEmitInfo.RecordOffset2StackMapInfo(objFuncEmitInfo.GetTextDataSize(), 60 insn.GetStackMap()->GetReferenceMap().SerializeInfo(), 61 insn.GetStackMap()->GetDeoptInfo().SerializeInfo()); 62 } 63 } 64 GetInsnSize(const Insn & insn)65 uint32 GetInsnSize(const Insn &insn) const override 66 { 67 (void)insn; 68 return k4ByteSize; 69 } 70 CreateFuncEmitInfo(CGFunc & cgFunc)71 FuncEmitInfo &CreateFuncEmitInfo(CGFunc &cgFunc) 72 { 73 MemPool *memPool = cgFunc.GetCG()->GetMIRModule()->GetMemPool(); 74 AArch64ObjFuncEmitInfo *content = memPool->New<AArch64ObjFuncEmitInfo>(cgFunc, *memPool); 75 contents.insert(contents.begin() + cgFunc.GetFunction().GetPuidxOrigin(), content); 76 return *content; 77 } 78 79 void HandleTextSectionGlobalFixup() override; 80 void AppendTextSectionData() override; 81 void AppendGlobalLabel() override; 82 void AppendSymsToSymTabSec() override; 83 void InitSections() override; 84 void LayoutSections() override; 85 void UpdateMachineAndFlags(FileHeader &header) override; 86 void EmitDataToDynamic(); 87 void EmitDataToHash(); 88 void EmitIntrinsicInsn(const Insn &insn, ObjFuncEmitInfo &objFuncEmitInfo) override; 89 void EmitSpinIntrinsicInsn(const Insn &insn, ObjFuncEmitInfo &objFuncEmitInfo) override; 90 91 uint32 GetBinaryCodeForInsn(const Insn &insn, const std::vector<uint32> &label2Offset, 92 ObjFuncEmitInfo &objFuncEmitInfo) const; 93 uint32 GetOpndMachineValue(const Operand &opnd) const; 94 uint32 GetAdrLabelOpndValue(const Insn &insn, const Operand &opnd, ObjFuncEmitInfo &objFuncEmitInfo) const; 95 uint32 GetLoadLiteralOpndValue(const Operand &opnd, ObjFuncEmitInfo &objFuncEmitInfo) const; 96 uint32 GetCondBranchOpndValue(const Operand &opnd, ObjFuncEmitInfo &objFuncEmitInfo) const; 97 uint32 GetUnCondBranchOpndValue(const Operand &opnd, const std::vector<uint32> &label2Offset, 98 ObjFuncEmitInfo &objFuncEmitInfo) const; 99 uint32 GetCallFuncOpndValue(const Operand &opnd, ObjFuncEmitInfo &objFuncEmitInfo) const; 100 uint32 GetTestBranchOpndValue(const Operand &opnd, ObjFuncEmitInfo &objFuncEmitInfo) const; 101 uint32 GetCompareBranchOpndValue(const Operand &opnd, ObjFuncEmitInfo &objFuncEmitInfo) const; 102 uint32 GetLo12LitrealOpndValue(MOperator mOp, const Operand &opnd, ObjFuncEmitInfo &objFuncEmitInfo) const; 103 void InsertNopInsn(ObjFuncEmitInfo &objFuncEmitInfo) const override; 104 105 private: 106 uint32 GenAddSubExtendRegInsn(const Insn &insn) const; 107 uint32 GenAddSubImmInsn(const Insn &insn) const; 108 uint32 GenAddSubShiftImmInsn(const Insn &insn) const; 109 uint32 GenAddSubRegInsn(const Insn &insn) const; 110 uint32 GenAddSubShiftRegInsn(const Insn &insn) const; 111 uint32 GenBitfieldInsn(const Insn &insn) const; 112 uint32 GenExtractInsn(const Insn &insn) const; 113 uint32 GenBranchImmInsn(const Insn &insn, const std::vector<uint32> &label2Offset, 114 ObjFuncEmitInfo &objFuncEmitInfo) const; 115 uint32 GenBranchRegInsn(const Insn &insn) const; 116 uint32 GenCompareBranchInsn(const Insn &insn, ObjFuncEmitInfo &objFuncEmitInfo) const; 117 uint32 GenCondCompareImmInsn(const Insn &insn) const; 118 uint32 GenCondCompareRegInsn(const Insn &insn) const; 119 uint32 GenConditionalSelectInsn(const Insn &insn) const; 120 uint32 GenDataProcess1SrcInsn(const Insn &insn) const; 121 uint32 GenDataProcess2SrcInsn(const Insn &insn) const; 122 uint32 GenDataProcess3SrcInsn(const Insn &insn) const; 123 uint32 GenFloatIntConversionsInsn(const Insn &insn) const; 124 uint32 GenFloatCompareInsn(const Insn &insn) const; 125 uint32 GenFloatDataProcessing1Insn(const Insn &insn) const; 126 uint32 GenFloatDataProcessing2Insn(const Insn &insn) const; 127 uint32 GenFloatImmInsn(const Insn &insn) const; 128 uint32 GenFloatCondSelectInsn(const Insn &insn) const; 129 uint32 GenLoadStoreModeLiteral(const Insn &insn, ObjFuncEmitInfo &objFuncEmitInfo) const; 130 uint32 GenLoadStoreModeBOi(const Insn &insn) const; 131 uint32 GenLoadStoreModeBOrX(const Insn &insn) const; 132 uint32 GenLoadStoreRegInsn(const Insn &insn, ObjFuncEmitInfo &objFuncEmitInfo) const; 133 uint32 GenLoadStoreARInsn(const Insn &insn) const; 134 uint32 GenLoadExclusiveInsn(const Insn &insn) const; 135 uint32 GenLoadExclusivePairInsn(const Insn &insn) const; 136 uint32 GenStoreExclusiveInsn(const Insn &insn) const; 137 uint32 GenStoreExclusivePairInsn(const Insn &insn) const; 138 uint32 GenLoadPairInsn(const Insn &insn) const; 139 uint32 GenStorePairInsn(const Insn &insn) const; 140 uint32 GenLoadStoreFloatInsn(const Insn &insn, ObjFuncEmitInfo &objFuncEmitInfo) const; 141 uint32 GenLoadPairFloatInsn(const Insn &insn) const; 142 uint32 GenStorePairFloatInsn(const Insn &insn) const; 143 uint32 GenLoadLiteralRegInsn(const Insn &insn, ObjFuncEmitInfo &objFuncEmitInfo) const; 144 uint32 GenLogicalRegInsn(const Insn &insn) const; 145 uint32 GenLogicalImmInsn(const Insn &insn) const; 146 uint32 GenMoveWideInsn(const Insn &insn) const; 147 uint32 GenPCRelAddrInsn(const Insn &insn, ObjFuncEmitInfo &objFuncEmitInfo) const; 148 uint32 GenAddPCRelAddrInsn(const Insn &insn, ObjFuncEmitInfo &objFuncEmitInfo) const; 149 uint32 GenSystemInsn(const Insn &insn) const; 150 uint32 GenTestBranchInsn(const Insn &insn, ObjFuncEmitInfo &objFuncEmitInfo) const; 151 uint32 GenCondBranchInsn(const Insn &insn, ObjFuncEmitInfo &objFuncEmitInfo) const; 152 uint32 GenMovReg(const Insn &insn) const; 153 uint32 GenMovImm(const Insn &insn) const; 154 uint32 EncodeLogicaImm(uint64 imm, uint32 size) const; 155 void HandleCallFixup(ObjFuncEmitInfo &objFuncEmitInfo, const Fixup &fixup); 156 void HandleAdrFixup(ObjFuncEmitInfo &objFuncEmitInfo, const Fixup &fixup); 157 158 /* emit intrinsic insn */ 159 void EmitMCCStackMapCall(const Insn &insn, std::vector<uint32> &label2Offset, ObjFuncEmitInfo &objFuncEmitInfo); 160 void EmitEnv(const Insn &insn, std::vector<uint32> &label2Offset, ObjFuncEmitInfo &objFuncEmitInfo); 161 void EmitClinit(const Insn &insn, std::vector<uint32> &label2Offset, ObjFuncEmitInfo &objFuncEmitInfo); 162 void EmitCounter(const Insn &insn, std::vector<uint32> &label2Offset, ObjFuncEmitInfo &objFuncEmitInfo); 163 void EmitLazyLoad(const Insn &insn, std::vector<uint32> &label2Offset, ObjFuncEmitInfo &objFuncEmitInfo); 164 void EmitLazyLoadStatic(const Insn &insn, std::vector<uint32> &label2Offset, ObjFuncEmitInfo &objFuncEmitInfo); 165 void EmitAdrpLdr(const Insn &insn, std::vector<uint32> &label2Offset, ObjFuncEmitInfo &objFuncEmitInfo); 166 void EmitArrayClassCacheLoad(const Insn &insn, std::vector<uint32> &label2Offset, ObjFuncEmitInfo &objFuncEmitInfo); 167 void EmitClinitTail(const Insn &insn, std::vector<uint32> &label2Offset, ObjFuncEmitInfo &objFuncEmitInfo); 168 void EmitGetAndAddInt(const Insn &insn, std::vector<uint32> &label2Offset, ObjFuncEmitInfo &objFuncEmitInfo); 169 void EmitGetAndSetInt(const Insn &insn, std::vector<uint32> &label2Offset, ObjFuncEmitInfo &objFuncEmitInfo); 170 void EmitCompareAndSwapInt(const Insn &insn, std::vector<uint32> &label2Offset, ObjFuncEmitInfo &objFuncEmitInfo); 171 void EmitStringIndexOf(const Insn &insn, std::vector<uint32> &label2Offset, ObjFuncEmitInfo &objFuncEmitInfo); 172 void EmitStringIndexOf2(const Insn &insn, std::vector<uint32> &label2Offset, ObjFuncEmitInfo &objFuncEmitInfo); 173 void EmitStringIndexOf3(const Insn &insn, std::vector<uint32> &label2Offset, ObjFuncEmitInfo &objFuncEmitInfo); 174 void EmitCheckCastNoArray(const Insn &insn, std::vector<uint32> &label2Offset, ObjFuncEmitInfo &objFuncEmitInfo); 175 void EmitCheckCastIsAssignable(const Insn &insn, std::vector<uint32> &label2Offset, 176 ObjFuncEmitInfo &objFuncEmitInfo); 177 void EmitCheckCastNoSubIsAssignable(const Insn &insn, std::vector<uint32> &label2Offset, 178 ObjFuncEmitInfo &objFuncEmitInfo); 179 void EmitInstanceOfIsAssignable(const Insn &insn, std::vector<uint32> &label2Offset, 180 ObjFuncEmitInfo &objFuncEmitInfo); 181 void EmitInstanceOfNoSubIsAssignable(const Insn &insn, std::vector<uint32> &label2Offset, 182 ObjFuncEmitInfo &objFuncEmitInfo); 183 void EmitMovMovkri16(const Insn &insn, std::vector<uint32> &label2Offset, ObjFuncEmitInfo &objFuncEmitInfo); 184 void EmitMovMovk64ri16(const Insn &insn, std::vector<uint32> &label2Offset, ObjFuncEmitInfo &objFuncEmitInfo); 185 EmitInsn(MOperator mOp,Operand & opnd1,std::vector<uint32> & label2Offset,ObjFuncEmitInfo & objFuncEmitInfo)186 void EmitInsn(MOperator mOp, Operand &opnd1, std::vector<uint32> &label2Offset, ObjFuncEmitInfo &objFuncEmitInfo) 187 { 188 Insn &insn = objFuncEmitInfo.GetCGFunc().GetInsnBuilder()->BuildInsn(mOp, opnd1); 189 EncodeInstruction(insn, label2Offset, objFuncEmitInfo); 190 } 191 EmitInsn(MOperator mOp,Operand & opnd1,Operand & opnd2,std::vector<uint32> & label2Offset,ObjFuncEmitInfo & objFuncEmitInfo)192 void EmitInsn(MOperator mOp, Operand &opnd1, Operand &opnd2, std::vector<uint32> &label2Offset, 193 ObjFuncEmitInfo &objFuncEmitInfo) 194 { 195 Insn &insn = objFuncEmitInfo.GetCGFunc().GetInsnBuilder()->BuildInsn(mOp, opnd1, opnd2); 196 EncodeInstruction(insn, label2Offset, objFuncEmitInfo); 197 } 198 EmitInsn(MOperator mOp,Operand & opnd1,Operand & opnd2,Operand & opnd3,std::vector<uint32> & label2Offset,ObjFuncEmitInfo & objFuncEmitInfo)199 void EmitInsn(MOperator mOp, Operand &opnd1, Operand &opnd2, Operand &opnd3, std::vector<uint32> &label2Offset, 200 ObjFuncEmitInfo &objFuncEmitInfo) 201 { 202 Insn &insn = objFuncEmitInfo.GetCGFunc().GetInsnBuilder()->BuildInsn(mOp, opnd1, opnd2, opnd3); 203 EncodeInstruction(insn, label2Offset, objFuncEmitInfo); 204 } 205 }; 206 } /* namespace maplebe */ 207 208 #endif /* MAPLEBE_INCLUDE_CG_AARCH64_AARCH64_OBJ_EMIT_H */ 209