| 1 | //===-- SystemZXPLINKAsmPrinter.cpp - SystemZ XPLINK asm printer ----------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements the SystemZXPLINKAsmPrinter class. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "SystemZXPLINKAsmPrinter.h" |
| 14 | #include "MCTargetDesc/SystemZMCTargetDesc.h" |
| 15 | #include "MCTargetDesc/SystemZTargetStreamer.h" |
| 16 | #include "SystemZFrameLowering.h" |
| 17 | #include "SystemZInstrInfo.h" |
| 18 | #include "SystemZMCInstLower.h" |
| 19 | #include "SystemZMachineFunctionInfo.h" |
| 20 | #include "SystemZSubtarget.h" |
| 21 | #include "llvm/ADT/StringExtras.h" |
| 22 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 23 | #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h" |
| 24 | #include "llvm/IR/Function.h" |
| 25 | #include "llvm/IR/GlobalAlias.h" |
| 26 | #include "llvm/IR/GlobalObject.h" |
| 27 | #include "llvm/IR/Module.h" |
| 28 | #include "llvm/MC/MCExpr.h" |
| 29 | #include "llvm/MC/MCInstBuilder.h" |
| 30 | #include "llvm/MC/MCSymbolGOFF.h" |
| 31 | #include "llvm/Support/Chrono.h" |
| 32 | #include "llvm/Support/ConvertEBCDIC.h" |
| 33 | #include "llvm/Support/FormatVariadic.h" |
| 34 | |
| 35 | using namespace llvm; |
| 36 | |
| 37 | SystemZXPLINKAsmPrinter::SystemZXPLINKAsmPrinter( |
| 38 | TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer) |
| 39 | : SystemZAsmPrinter(TM, std::move(Streamer)), ADATable(8) {} |
| 40 | |
| 41 | bool SystemZXPLINKAsmPrinter::doInitialization(Module &M) { |
| 42 | SM.reset(); |
| 43 | |
| 44 | // In HLASM, the only way to represent aliases is to use the |
| 45 | // extra-label-at-definition strategy. This is similar to the AIX |
| 46 | // implementation with the additional caveat that all symbol attributes must |
| 47 | // be emitted before the label is emitted. |
| 48 | // Construct an aliasing list for each GlobalObject. |
| 49 | for (const auto &Alias : M.aliases()) { |
| 50 | const GlobalObject *Aliasee = Alias.getAliaseeObject(); |
| 51 | if (!Aliasee) |
| 52 | OutContext.reportError( |
| 53 | L: {}, Msg: "Alias without a base object is not yet supported on z/OS." ); |
| 54 | |
| 55 | bool IsFunc = isa<Function>(Val: Aliasee->stripPointerCasts()); |
| 56 | if (IsFunc) { |
| 57 | if (Alias.hasWeakLinkage() || Alias.hasLinkOnceLinkage()) |
| 58 | OutContext.reportError(L: {}, |
| 59 | Msg: "Weak alias/reference not supported on z/OS" ); |
| 60 | |
| 61 | GOAliasMap[Aliasee].push_back(Elt: &Alias); |
| 62 | } else |
| 63 | OutContext.reportError(L: {}, |
| 64 | Msg: "Only aliases to functions is supported in GOFF." ); |
| 65 | } |
| 66 | return AsmPrinter::doInitialization(M); |
| 67 | } |
| 68 | |
| 69 | // The XPLINK ABI requires that a no-op encoding the call type is emitted after |
| 70 | // each call to a subroutine. This information can be used by the called |
| 71 | // function to determine its entry point, e.g. for generating a backtrace. The |
| 72 | // call type is encoded as a register number in the bcr instruction. See |
| 73 | // enumeration CallType for the possible values. |
| 74 | void SystemZXPLINKAsmPrinter::emitCallInformation(CallType CT) { |
| 75 | EmitToStreamer(S&: *OutStreamer, |
| 76 | Inst: MCInstBuilder(SystemZ::BCRAsm) |
| 77 | .addImm(Val: 0) |
| 78 | .addReg(Reg: SystemZMC::GR64Regs[static_cast<unsigned>(CT)])); |
| 79 | } |
| 80 | |
| 81 | uint32_t |
| 82 | SystemZXPLINKAsmPrinter::AssociatedDataAreaTable::insert(const MCSymbol *Sym, |
| 83 | unsigned SlotKind) { |
| 84 | auto Key = std::make_pair(x&: Sym, y&: SlotKind); |
| 85 | auto It = Displacements.find(Key); |
| 86 | |
| 87 | if (It != Displacements.end()) |
| 88 | return (*It).second; |
| 89 | |
| 90 | // Determine length of descriptor. |
| 91 | uint32_t Length; |
| 92 | switch (SlotKind) { |
| 93 | case SystemZII::MO_ADA_DIRECT_FUNC_DESC: |
| 94 | Length = 2 * PointerSize; |
| 95 | break; |
| 96 | default: |
| 97 | Length = PointerSize; |
| 98 | break; |
| 99 | } |
| 100 | |
| 101 | uint32_t Displacement = NextDisplacement; |
| 102 | Displacements[std::make_pair(x&: Sym, y&: SlotKind)] = NextDisplacement; |
| 103 | NextDisplacement += Length; |
| 104 | |
| 105 | return Displacement; |
| 106 | } |
| 107 | |
| 108 | uint32_t SystemZXPLINKAsmPrinter::AssociatedDataAreaTable::insert( |
| 109 | const MachineFunction &MF, const MachineOperand &MO) { |
| 110 | MCSymbol *Sym; |
| 111 | if (MO.getType() == MachineOperand::MO_GlobalAddress) { |
| 112 | const GlobalValue *GV = MO.getGlobal(); |
| 113 | Sym = MF.getTarget().getSymbol(GV); |
| 114 | assert(Sym && "No symbol" ); |
| 115 | } else if (MO.getType() == MachineOperand::MO_ExternalSymbol) { |
| 116 | const char *SymName = MO.getSymbolName(); |
| 117 | Sym = MF.getContext().getOrCreateSymbol(Name: SymName); |
| 118 | assert(Sym && "No symbol" ); |
| 119 | } else |
| 120 | llvm_unreachable("Unexpected operand type" ); |
| 121 | |
| 122 | unsigned ADAslotType = MO.getTargetFlags(); |
| 123 | return insert(Sym, SlotKind: ADAslotType); |
| 124 | } |
| 125 | |
| 126 | void SystemZXPLINKAsmPrinter::emitInstruction(const MachineInstr *MI) { |
| 127 | SystemZMCInstLower Lower(MF->getContext(), *this); |
| 128 | MCInst LoweredMI; |
| 129 | |
| 130 | switch (MI->getOpcode()) { |
| 131 | case SystemZ::CallBRASL_XPLINK64: |
| 132 | EmitToStreamer(S&: *OutStreamer, Inst: MCInstBuilder(SystemZ::BRASL) |
| 133 | .addReg(Reg: SystemZ::R7D) |
| 134 | .addExpr(Val: Lower.getExpr(MO: MI->getOperand(i: 0), |
| 135 | SystemZ::S_None))); |
| 136 | emitCallInformation(CT: CallType::BRASL7); |
| 137 | return; |
| 138 | |
| 139 | case SystemZ::CallBASR_XPLINK64: |
| 140 | EmitToStreamer(S&: *OutStreamer, Inst: MCInstBuilder(SystemZ::BASR) |
| 141 | .addReg(Reg: SystemZ::R7D) |
| 142 | .addReg(Reg: MI->getOperand(i: 0).getReg())); |
| 143 | emitCallInformation(CT: CallType::BASR76); |
| 144 | return; |
| 145 | |
| 146 | case SystemZ::Return_XPLINK: |
| 147 | LoweredMI = |
| 148 | MCInstBuilder(SystemZ::B).addReg(Reg: SystemZ::R7D).addImm(Val: 2).addReg(Reg: 0); |
| 149 | break; |
| 150 | |
| 151 | case SystemZ::CondReturn_XPLINK: |
| 152 | LoweredMI = MCInstBuilder(SystemZ::BC) |
| 153 | .addImm(Val: MI->getOperand(i: 0).getImm()) |
| 154 | .addImm(Val: MI->getOperand(i: 1).getImm()) |
| 155 | .addReg(Reg: SystemZ::R7D) |
| 156 | .addImm(Val: 2) |
| 157 | .addReg(Reg: 0); |
| 158 | break; |
| 159 | |
| 160 | case SystemZ::XPLINK_STACKRESTORE: { |
| 161 | if (!StackRestoreStub) |
| 162 | StackRestoreStub = OutContext.createTempSymbol(Name: "stackrestore" ); |
| 163 | EmitToStreamer(S&: *OutStreamer, Inst: MCInstBuilder(SystemZ::LARL) |
| 164 | .addReg(Reg: SystemZ::R3D) |
| 165 | .addExpr(Val: MCSymbolRefExpr::create( |
| 166 | Symbol: StackRestoreStub, Ctx&: OutContext))); |
| 167 | EmitToStreamer( |
| 168 | S&: *OutStreamer, |
| 169 | Inst: MCInstBuilder(SystemZ::BASR).addReg(Reg: SystemZ::R3D).addReg(Reg: SystemZ::R3D)); |
| 170 | emitCallInformation(CT: CallType::BASR33); |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | case SystemZ::CallBASR_STACKEXT: |
| 175 | EmitToStreamer(S&: *OutStreamer, Inst: MCInstBuilder(SystemZ::BASR) |
| 176 | .addReg(Reg: SystemZ::R3D) |
| 177 | .addReg(Reg: MI->getOperand(i: 0).getReg())); |
| 178 | emitCallInformation(CT: CallType::BASR33); |
| 179 | return; |
| 180 | |
| 181 | case SystemZ::ADA_ENTRY_VALUE: |
| 182 | case SystemZ::ADA_ENTRY: { |
| 183 | const SystemZSubtarget &Subtarget = MF->getSubtarget<SystemZSubtarget>(); |
| 184 | const SystemZInstrInfo *TII = Subtarget.getInstrInfo(); |
| 185 | uint32_t Disp = ADATable.insert(MF: *MF, MO: MI->getOperand(i: 1)); |
| 186 | Register TargetReg = MI->getOperand(i: 0).getReg(); |
| 187 | |
| 188 | Register ADAReg = MI->getOperand(i: 2).getReg(); |
| 189 | Disp += MI->getOperand(i: 3).getImm(); |
| 190 | bool LoadAddr = MI->getOpcode() == SystemZ::ADA_ENTRY; |
| 191 | |
| 192 | unsigned Op0 = LoadAddr ? SystemZ::LA : SystemZ::LG; |
| 193 | unsigned Op = TII->getOpcodeForOffset(Opcode: Op0, Offset: Disp); |
| 194 | |
| 195 | Register IndexReg = 0; |
| 196 | if (!Op) { |
| 197 | if (TargetReg != ADAReg) { |
| 198 | IndexReg = TargetReg; |
| 199 | // Use TargetReg to store displacement. |
| 200 | EmitToStreamer( |
| 201 | S&: *OutStreamer, |
| 202 | Inst: MCInstBuilder(SystemZ::LLILF).addReg(Reg: TargetReg).addImm(Val: Disp)); |
| 203 | } else |
| 204 | EmitToStreamer(S&: *OutStreamer, Inst: MCInstBuilder(SystemZ::ALGFI) |
| 205 | .addReg(Reg: TargetReg) |
| 206 | .addReg(Reg: TargetReg) |
| 207 | .addImm(Val: Disp)); |
| 208 | Disp = 0; |
| 209 | Op = Op0; |
| 210 | } |
| 211 | EmitToStreamer( |
| 212 | S&: *OutStreamer, |
| 213 | Inst: MCInstBuilder(Op).addReg(Reg: TargetReg).addReg(Reg: ADAReg).addImm(Val: Disp).addReg( |
| 214 | Reg: IndexReg)); |
| 215 | return; |
| 216 | } |
| 217 | |
| 218 | default: |
| 219 | SystemZAsmPrinter::emitInstruction(MI); |
| 220 | return; |
| 221 | } |
| 222 | EmitToStreamer(S&: *OutStreamer, Inst: LoweredMI); |
| 223 | } |
| 224 | |
| 225 | void SystemZXPLINKAsmPrinter::emitXXStructorList(const DataLayout &DL, |
| 226 | const Constant *List, |
| 227 | bool IsCtor) { |
| 228 | assert(TM.getTargetTriple().isOSBinFormatGOFF() && "Only GOFF supported" ); |
| 229 | |
| 230 | SmallVector<Structor, 8> Structors; |
| 231 | preprocessXXStructorList(DL, List, Structors); |
| 232 | if (Structors.empty()) |
| 233 | return; |
| 234 | |
| 235 | const Align Align = llvm::Align(4); |
| 236 | const TargetLoweringObjectFileGOFF &Obj = |
| 237 | static_cast<const TargetLoweringObjectFileGOFF &>(getObjFileLowering()); |
| 238 | for (Structor &S : Structors) { |
| 239 | MCSectionGOFF *Section = |
| 240 | static_cast<MCSectionGOFF *>(Obj.getStaticXtorSection(Priority: S.Priority)); |
| 241 | OutStreamer->switchSection(Section); |
| 242 | if (OutStreamer->getCurrentSection() != OutStreamer->getPreviousSection()) |
| 243 | emitAlignment(Alignment: Align); |
| 244 | |
| 245 | // The priority is provided as an input to getStaticXtorSection(), and is |
| 246 | // recalculated within that function as `Prio` going to going into the |
| 247 | // PR section. |
| 248 | // This priority retrieved via the `SortKey` below is the recalculated |
| 249 | // Priority. |
| 250 | uint32_t XtorPriority = Section->getPRAttributes().SortKey; |
| 251 | |
| 252 | const GlobalValue *GV = dyn_cast<GlobalValue>(Val: S.Func->stripPointerCasts()); |
| 253 | assert(GV && "C++ xxtor pointer was not a GlobalValue!" ); |
| 254 | MCSymbolGOFF *Symbol = static_cast<MCSymbolGOFF *>(getSymbol(GV)); |
| 255 | |
| 256 | // @@SQINIT entry: { unsigned prio; void (*ctor)(); void (*dtor)(); } |
| 257 | |
| 258 | unsigned PointerSizeInBytes = DL.getPointerSize(); |
| 259 | |
| 260 | auto &Ctx = OutStreamer->getContext(); |
| 261 | const MCExpr *ADAFuncRefExpr; |
| 262 | unsigned SlotKind = SystemZII::MO_ADA_DIRECT_FUNC_DESC; |
| 263 | |
| 264 | MCSectionGOFF *ADASection = |
| 265 | static_cast<MCSectionGOFF *>(Obj.getADASection()); |
| 266 | assert(ADASection && "ADA section must exist for GOFF targets!" ); |
| 267 | const MCSymbol *ADASym = ADASection->getBeginSymbol(); |
| 268 | assert(ADASym && "ADA symbol should already be set!" ); |
| 269 | |
| 270 | ADAFuncRefExpr = MCBinaryExpr::createAdd( |
| 271 | LHS: MCSpecifierExpr::create(Expr: MCSymbolRefExpr::create(Symbol: ADASym, Ctx&: OutContext), |
| 272 | S: SystemZ::S_QCon, Ctx&: OutContext), |
| 273 | RHS: MCConstantExpr::create(Value: ADATable.insert(Sym: Symbol, SlotKind), Ctx), Ctx); |
| 274 | |
| 275 | emitInt32(Value: XtorPriority); |
| 276 | if (IsCtor) { |
| 277 | OutStreamer->emitValue(Value: ADAFuncRefExpr, Size: PointerSizeInBytes); |
| 278 | OutStreamer->emitIntValue(Value: 0, Size: PointerSizeInBytes); |
| 279 | } else { |
| 280 | OutStreamer->emitIntValue(Value: 0, Size: PointerSizeInBytes); |
| 281 | OutStreamer->emitValue(Value: ADAFuncRefExpr, Size: PointerSizeInBytes); |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | bool SystemZXPLINKAsmPrinter::doFinalization(Module &M) { |
| 287 | // The stack restore stub follows the last function. It must be emitted |
| 288 | // before the debug information is finalized, which may end the code section |
| 289 | // (emitEndOfAsmFile is too late). |
| 290 | if (StackRestoreStub) |
| 291 | emitStackRestoreStub(); |
| 292 | return SystemZAsmPrinter::doFinalization(M); |
| 293 | } |
| 294 | |
| 295 | void SystemZXPLINKAsmPrinter::emitEndOfAsmFile(Module &M) { |
| 296 | auto *ZOS = getTargetStreamer(); |
| 297 | emitADASection(); |
| 298 | emitIDRLSection(M); |
| 299 | // On z/OS, we need to associate an external data reference with an ED |
| 300 | // symbol, for which we use the the ED of the ADA. We also need to mark the |
| 301 | // reference as being to data, otherwise we cannot bind with code generated |
| 302 | // by XL. |
| 303 | for (auto &GO : M.global_objects()) { |
| 304 | if (auto *GV = dyn_cast<GlobalVariable>(Val: &GO)) { |
| 305 | if (!GV->hasInitializer()) { |
| 306 | MCSymbol *Sym = getSymbol(GV); |
| 307 | ZOS->emitADA(Sym, Section: OutContext.getObjectFileInfo()->getADASection()); |
| 308 | OutStreamer->emitSymbolAttribute(Symbol: Sym, Attribute: MCSA_ELF_TypeObject); |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | // Emit CELQMAIN in RENT format (see z/OS Language Environment Vendor |
| 315 | // Interfaces, "Program initialization and termination for AMODE 64 |
| 316 | // applications"): |
| 317 | // +0 X'04000001' |
| 318 | // +8 AD(main entry point) |
| 319 | // +10 AD(CELQINPL) |
| 320 | // +18 A(0) / Q(environment); emitted as 8 byte R-con to main, which the |
| 321 | // binder resolves to the offset of the ADA of main (fits into 4 bytes) |
| 322 | // followed by a reference to the bootstrap routine CELQBST. CELQSTRT only has |
| 323 | // a weak reference to CELQBST, so without it autocall would not include it. |
| 324 | void SystemZXPLINKAsmPrinter::emitCELQMAIN(const Function &MainFn) { |
| 325 | MCSymbol *MainSym = getSymbol(GV: &MainFn); |
| 326 | auto ExternalOSSymbol = [&](StringRef Name, bool IsRenameable) { |
| 327 | MCSymbol *Sym = OutContext.getOrCreateSymbol(Name); |
| 328 | OutStreamer->emitSymbolAttribute(Symbol: Sym, Attribute: MCSA_OSLinkage); |
| 329 | OutStreamer->emitSymbolAttribute(Symbol: Sym, Attribute: MCSA_Global); |
| 330 | getTargetStreamer()->emitRenameable(Sym, IsRenamable: IsRenameable); |
| 331 | return Sym; |
| 332 | }; |
| 333 | MCSymbol *CELQINPL = ExternalOSSymbol("CELQINPL" , /*IsRenameable=*/false); |
| 334 | MCSymbol *CELQBST = ExternalOSSymbol("CELQBST" , /*IsRenameable=*/true); |
| 335 | |
| 336 | OutStreamer->pushSection(); |
| 337 | OutStreamer->switchSection(Section: getObjFileLowering().getTextSection()); |
| 338 | OutStreamer->emitValueToAlignment(Alignment: Align(8)); |
| 339 | MCSymbol *CELQMAIN = OutContext.getOrCreateSymbol(Name: "CELQMAIN" ); |
| 340 | // Referenced by CELQSTRT, CELQINPL and CELQBST with OS linkage. |
| 341 | OutStreamer->emitSymbolAttribute(Symbol: CELQMAIN, Attribute: MCSA_OSLinkage); |
| 342 | OutStreamer->emitSymbolAttribute(Symbol: CELQMAIN, Attribute: MCSA_Global); |
| 343 | OutStreamer->emitSymbolAttribute(Symbol: CELQMAIN, Attribute: MCSA_Hidden); |
| 344 | OutStreamer->emitLabel(Symbol: CELQMAIN); |
| 345 | OutStreamer->AddComment(T: "CELQMAIN, RENT format" ); |
| 346 | OutStreamer->emitInt32(Value: 0x04000001); |
| 347 | OutStreamer->emitInt32(Value: 0); |
| 348 | OutStreamer->AddComment(T: "Address of main" ); |
| 349 | OutStreamer->emitValue(Value: MCSymbolRefExpr::create(Symbol: MainSym, Ctx&: OutContext), Size: 8); |
| 350 | OutStreamer->AddComment(T: "Address of CELQINPL" ); |
| 351 | OutStreamer->emitValue(Value: MCSymbolRefExpr::create(Symbol: CELQINPL, Ctx&: OutContext), Size: 8); |
| 352 | OutStreamer->AddComment(T: "Q(environment) of main" ); |
| 353 | OutStreamer->emitValue( |
| 354 | Value: MCSpecifierExpr::create(Expr: MCSymbolRefExpr::create(Symbol: MainSym, Ctx&: OutContext), |
| 355 | S: SystemZ::S_RCon, Ctx&: OutContext), |
| 356 | Size: 8); |
| 357 | OutStreamer->AddComment(T: "Reference to CELQBST" ); |
| 358 | OutStreamer->emitValue(Value: MCSymbolRefExpr::create(Symbol: CELQBST, Ctx&: OutContext), Size: 8); |
| 359 | OutStreamer->popSection(); |
| 360 | } |
| 361 | |
| 362 | void SystemZXPLINKAsmPrinter::emitADASection() { |
| 363 | OutStreamer->pushSection(); |
| 364 | |
| 365 | const unsigned PointerSize = getDataLayout().getPointerSize(); |
| 366 | OutStreamer->switchSection(Section: getObjFileLowering().getADASection()); |
| 367 | |
| 368 | auto *ZOS = getTargetStreamer(); |
| 369 | unsigned EmittedBytes = 0; |
| 370 | for (auto &Entry : ADATable.getTable()) { |
| 371 | const MCSymbol *Sym; |
| 372 | unsigned SlotKind; |
| 373 | std::tie(args&: Sym, args&: SlotKind) = Entry.first; |
| 374 | unsigned Offset = Entry.second; |
| 375 | assert(Offset == EmittedBytes && "Offset not as expected" ); |
| 376 | (void)EmittedBytes; |
| 377 | #define (Str) \ |
| 378 | OutStreamer->AddComment(Twine("Offset ") \ |
| 379 | .concat(utostr(Offset)) \ |
| 380 | .concat(" " Str " ") \ |
| 381 | .concat(Sym->getName())); |
| 382 | switch (SlotKind) { |
| 383 | case SystemZII::MO_ADA_DIRECT_FUNC_DESC: |
| 384 | // Language Environment DLL logic requires function descriptors, for |
| 385 | // imported functions, that are placed in the ADA to be 8 byte aligned. |
| 386 | EMIT_COMMENT("function descriptor of" ); |
| 387 | OutStreamer->emitValue( |
| 388 | Value: MCSpecifierExpr::create(Expr: MCSymbolRefExpr::create(Symbol: Sym, Ctx&: OutContext), |
| 389 | S: SystemZ::S_RCon, Ctx&: OutContext), |
| 390 | Size: PointerSize); |
| 391 | OutStreamer->emitValue( |
| 392 | Value: MCSpecifierExpr::create(Expr: MCSymbolRefExpr::create(Symbol: Sym, Ctx&: OutContext), |
| 393 | S: SystemZ::S_VCon, Ctx&: OutContext), |
| 394 | Size: PointerSize); |
| 395 | EmittedBytes += PointerSize * 2; |
| 396 | break; |
| 397 | case SystemZII::MO_ADA_DATA_SYMBOL_ADDR: |
| 398 | EMIT_COMMENT("pointer to data symbol" ); |
| 399 | OutStreamer->emitValue( |
| 400 | Value: MCSpecifierExpr::create(Expr: MCSymbolRefExpr::create(Symbol: Sym, Ctx&: OutContext), |
| 401 | S: SystemZ::S_None, Ctx&: OutContext), |
| 402 | Size: PointerSize); |
| 403 | EmittedBytes += PointerSize; |
| 404 | break; |
| 405 | case SystemZII::MO_ADA_INDIRECT_FUNC_DESC: { |
| 406 | MCSymbol *Alias = OutContext.getOrCreateSymbol( |
| 407 | Name: Twine(Sym->getName()).concat(Suffix: "@indirect" )); |
| 408 | OutStreamer->emitSymbolAttribute(Symbol: Alias, Attribute: MCSA_IndirectSymbol); |
| 409 | OutStreamer->emitSymbolAttribute(Symbol: Alias, Attribute: MCSA_ELF_TypeFunction); |
| 410 | OutStreamer->emitSymbolAttribute(Symbol: Alias, Attribute: MCSA_Global); |
| 411 | OutStreamer->emitSymbolAttribute(Symbol: Alias, Attribute: MCSA_Extern); |
| 412 | MCSymbolGOFF *GOFFSym = |
| 413 | static_cast<llvm::MCSymbolGOFF *>(const_cast<llvm::MCSymbol *>(Sym)); |
| 414 | // A weak reference (extern_weak) stays weak through the indirect |
| 415 | // symbol, otherwise the binder fails on the unresolved reference. |
| 416 | if (GOFFSym->isWeak()) |
| 417 | OutStreamer->emitSymbolAttribute(Symbol: Alias, Attribute: MCSA_WeakReference); |
| 418 | ZOS->emitExternalName(Sym: Alias, Name: GOFFSym->getExternalName()); |
| 419 | EMIT_COMMENT("pointer to function descriptor" ); |
| 420 | OutStreamer->emitValue( |
| 421 | Value: MCSpecifierExpr::create(Expr: MCSymbolRefExpr::create(Symbol: Alias, Ctx&: OutContext), |
| 422 | S: SystemZ::S_VCon, Ctx&: OutContext), |
| 423 | Size: PointerSize); |
| 424 | EmittedBytes += PointerSize; |
| 425 | break; |
| 426 | } |
| 427 | default: |
| 428 | llvm_unreachable("Unexpected slot kind" ); |
| 429 | } |
| 430 | #undef EMIT_COMMENT |
| 431 | } |
| 432 | OutStreamer->popSection(); |
| 433 | } |
| 434 | |
| 435 | static std::string getProductID(Module &M) { |
| 436 | std::string ProductID; |
| 437 | if (auto *MD = M.getModuleFlag(Key: "zos_product_id" )) |
| 438 | ProductID = cast<MDString>(Val: MD)->getString().str(); |
| 439 | if (ProductID.empty()) |
| 440 | ProductID = "LLVM" ; |
| 441 | return ProductID; |
| 442 | } |
| 443 | |
| 444 | static uint32_t getProductVersion(Module &M) { |
| 445 | if (auto *VersionVal = mdconst::extract_or_null<ConstantInt>( |
| 446 | MD: M.getModuleFlag(Key: "zos_product_major_version" ))) |
| 447 | return VersionVal->getZExtValue(); |
| 448 | return LLVM_VERSION_MAJOR; |
| 449 | } |
| 450 | |
| 451 | static uint32_t getProductRelease(Module &M) { |
| 452 | if (auto *ReleaseVal = mdconst::extract_or_null<ConstantInt>( |
| 453 | MD: M.getModuleFlag(Key: "zos_product_minor_version" ))) |
| 454 | return ReleaseVal->getZExtValue(); |
| 455 | return LLVM_VERSION_MINOR; |
| 456 | } |
| 457 | |
| 458 | static uint32_t getProductPatch(Module &M) { |
| 459 | if (auto *PatchVal = mdconst::extract_or_null<ConstantInt>( |
| 460 | MD: M.getModuleFlag(Key: "zos_product_patchlevel" ))) |
| 461 | return PatchVal->getZExtValue(); |
| 462 | return LLVM_VERSION_PATCH; |
| 463 | } |
| 464 | |
| 465 | static time_t getTranslationTime(Module &M) { |
| 466 | std::time_t Time = 0; |
| 467 | if (auto *Val = mdconst::extract_or_null<ConstantInt>( |
| 468 | MD: M.getModuleFlag(Key: "zos_translation_time" ))) { |
| 469 | long SecondsSinceEpoch = Val->getSExtValue(); |
| 470 | Time = static_cast<time_t>(SecondsSinceEpoch); |
| 471 | } |
| 472 | return Time; |
| 473 | } |
| 474 | |
| 475 | void SystemZXPLINKAsmPrinter::emitIDRLSection(Module &M) { |
| 476 | OutStreamer->pushSection(); |
| 477 | OutStreamer->switchSection(Section: getObjFileLowering().getIDRLSection()); |
| 478 | constexpr unsigned IDRLDataLength = 30; |
| 479 | std::time_t Time = getTranslationTime(M); |
| 480 | |
| 481 | uint32_t ProductVersion = getProductVersion(M); |
| 482 | uint32_t ProductRelease = getProductRelease(M); |
| 483 | |
| 484 | std::string ProductID = getProductID(M); |
| 485 | |
| 486 | SmallString<IDRLDataLength + 1> TempStr; |
| 487 | raw_svector_ostream O(TempStr); |
| 488 | O << formatv(Fmt: "{0,-10}{1,0-2:d}{2,0-2:d}{3:%Y%m%d%H%M%S}{4,0-2}" , |
| 489 | Vals: ProductID.substr(pos: 0, n: 10).c_str(), Vals&: ProductVersion, Vals&: ProductRelease, |
| 490 | Vals: llvm::sys::toUtcTime(T: Time), Vals: "0" ); |
| 491 | SmallString<IDRLDataLength> Data; |
| 492 | ConverterEBCDIC::convertToEBCDIC(Source: TempStr, Result&: Data); |
| 493 | |
| 494 | OutStreamer->emitInt8(Value: 0); // Reserved. |
| 495 | OutStreamer->emitInt8(Value: 3); // Format. |
| 496 | OutStreamer->emitInt16(Value: IDRLDataLength); // Length. |
| 497 | OutStreamer->emitBytes(Data: Data.str()); |
| 498 | OutStreamer->popSection(); |
| 499 | } |
| 500 | |
| 501 | void SystemZXPLINKAsmPrinter::emitFunctionBodyEnd() { |
| 502 | // A stub (naked function) has no PPA1 and thus no end of function symbol. |
| 503 | if (MF->getFunction().hasFnAttribute(Kind: Attribute::Naked)) |
| 504 | return; |
| 505 | |
| 506 | // Emit symbol for the end of function if the z/OS target streamer |
| 507 | // is used. This is needed to calculate the size of the function. |
| 508 | auto *ZOS = getTargetStreamer(); |
| 509 | OutStreamer->emitLabel(Symbol: ZOS->DeferredPPA1.back().FnEnd); |
| 510 | } |
| 511 | |
| 512 | // Determine the end of the prolog and the instructions which updates the stack |
| 513 | // register, and attach symbols to those instructions. |
| 514 | static void determinePrologueStackUpdateSym(MachineFunction *MF, |
| 515 | MCSymbol *&EndOfPrologSym, |
| 516 | MCSymbol *&StackUpdateSym) { |
| 517 | EndOfPrologSym = nullptr; |
| 518 | StackUpdateSym = nullptr; |
| 519 | |
| 520 | // Scan the basic block for the FENCE instruction which marks the end |
| 521 | // of the prologue. We know |
| 522 | // the prologue is spread at most across the first 3 basic blocks. Also record |
| 523 | // the first instruction updating the stack pointer. |
| 524 | const SystemZSubtarget &STI = MF->getSubtarget<SystemZSubtarget>(); |
| 525 | auto &Regs = STI.getSpecialRegisters<SystemZXPLINK64Registers>(); |
| 526 | MachineInstr *EndOfPrologMI = nullptr; |
| 527 | MachineInstr *StackUpdateMI = nullptr; |
| 528 | unsigned BBCount = 1; |
| 529 | |
| 530 | for (auto &MBB : *MF) { |
| 531 | for (auto &I : MBB) { |
| 532 | if (I.getOpcode() == SystemZ::FENCE) |
| 533 | EndOfPrologMI = &I; |
| 534 | else if (!StackUpdateMI) { |
| 535 | unsigned Opcode = I.getOpcode(); |
| 536 | // TODO: We can instead emit a pseudo instruction in |
| 537 | // SystemZFrameLowering to represent a stack adjustment instruction, and |
| 538 | // check for that here, instead of having to check for multiple |
| 539 | // instructions. |
| 540 | if ((Opcode == SystemZ::AGHI || Opcode == SystemZ::AGFI) && |
| 541 | I.getOperand(i: 0).getReg() == Regs.getStackPointerRegister()) |
| 542 | StackUpdateMI = &I; |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | // Prologue can be a max of 3 BBs if we need to call stack extension code |
| 547 | if (EndOfPrologMI || BBCount == 3) |
| 548 | break; |
| 549 | |
| 550 | ++BBCount; |
| 551 | } |
| 552 | |
| 553 | // Leaf functions do not have a prologue. |
| 554 | if (EndOfPrologMI == nullptr) |
| 555 | return; |
| 556 | |
| 557 | #ifdef EXPENSIVE_CHECKS |
| 558 | // Check that the prolog length is valid. |
| 559 | auto *TII = STI.getInstrInfo(); |
| 560 | size_t Size = 0; |
| 561 | |
| 562 | for (auto &MBB : *MF) { |
| 563 | bool TerminateLoop = false; |
| 564 | for (auto &I : MBB) { |
| 565 | Size += TII->getInstSizeInBytes(I); |
| 566 | if (&I == EndOfPrologMI) { |
| 567 | TerminateLoop = true; |
| 568 | break; |
| 569 | } |
| 570 | } |
| 571 | if (TerminateLoop) |
| 572 | break; |
| 573 | } |
| 574 | if (Size > 128) |
| 575 | report_fatal_error( |
| 576 | Twine(MF->getName()).concat(": Prolog exceeds 128 bytes" )); |
| 577 | #endif |
| 578 | |
| 579 | // Attach a temporary symbol to mark the end of the prolog. |
| 580 | EndOfPrologSym = MF->getContext().createTempSymbol(Name: "end_of_prologue" ); |
| 581 | EndOfPrologMI->setPostInstrSymbol(MF&: *MF, Symbol: EndOfPrologSym); |
| 582 | |
| 583 | if (StackUpdateMI) { |
| 584 | StackUpdateSym = MF->getContext().createTempSymbol(Name: "stack_update" ); |
| 585 | StackUpdateMI->setPreInstrSymbol(MF&: *MF, Symbol: StackUpdateSym); |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | void SystemZXPLINKAsmPrinter::calculatePPA1() { |
| 590 | auto *ZOS = getTargetStreamer(); |
| 591 | assert(ZOS->PPA2Sym != nullptr && "PPA2 Symbol not defined" ); |
| 592 | |
| 593 | SystemZTargetzOSStreamer::PPA1Info Info; |
| 594 | |
| 595 | const TargetRegisterInfo *TRI = MF->getRegInfo().getTargetRegisterInfo(); |
| 596 | const SystemZSubtarget &Subtarget = MF->getSubtarget<SystemZSubtarget>(); |
| 597 | |
| 598 | const SystemZMachineFunctionInfo *ZFI = |
| 599 | MF->getInfo<SystemZMachineFunctionInfo>(); |
| 600 | const auto *ZFL = static_cast<const SystemZXPLINKFrameLowering *>( |
| 601 | Subtarget.getFrameLowering()); |
| 602 | const MachineFrameInfo &MFFrame = MF->getFrameInfo(); |
| 603 | |
| 604 | // Get saved GPR/FPR/VPR masks. |
| 605 | const std::vector<CalleeSavedInfo> &CSI = MFFrame.getCalleeSavedInfo(); |
| 606 | uint16_t SavedGPRMask = 0; |
| 607 | uint16_t SavedFPRMask = 0; |
| 608 | uint8_t SavedVRMask = 0; |
| 609 | int64_t OffsetFPR = 0; |
| 610 | int64_t OffsetVR = 0; |
| 611 | const int64_t TopOfStack = |
| 612 | MFFrame.getOffsetAdjustment() + MFFrame.getStackSize(); |
| 613 | |
| 614 | // Loop over the spilled registers. The CalleeSavedInfo can't be used because |
| 615 | // it does not contain all spilled registers. |
| 616 | for (unsigned I = ZFI->getSpillGPRRegs().LowGPR, |
| 617 | E = ZFI->getSpillGPRRegs().HighGPR; |
| 618 | I && E && I <= E; ++I) { |
| 619 | unsigned V = TRI->getEncodingValue(Reg: (Register)I); |
| 620 | assert(V < 16 && "GPR index out of range" ); |
| 621 | SavedGPRMask |= 1 << (15 - V); |
| 622 | } |
| 623 | |
| 624 | for (auto &CS : CSI) { |
| 625 | unsigned Reg = CS.getReg(); |
| 626 | unsigned I = TRI->getEncodingValue(Reg); |
| 627 | |
| 628 | if (SystemZ::FP64BitRegClass.contains(Reg)) { |
| 629 | assert(I < 16 && "FPR index out of range" ); |
| 630 | SavedFPRMask |= 1 << (15 - I); |
| 631 | int64_t Temp = MFFrame.getObjectOffset(ObjectIdx: CS.getFrameIdx()); |
| 632 | if (Temp < OffsetFPR) |
| 633 | OffsetFPR = Temp; |
| 634 | } else if (SystemZ::VR128BitRegClass.contains(Reg)) { |
| 635 | assert(I >= 16 && I <= 23 && "VPR index out of range" ); |
| 636 | unsigned BitNum = I - 16; |
| 637 | SavedVRMask |= 1 << (7 - BitNum); |
| 638 | int64_t Temp = MFFrame.getObjectOffset(ObjectIdx: CS.getFrameIdx()); |
| 639 | if (Temp < OffsetVR) |
| 640 | OffsetVR = Temp; |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | // Adjust the offset. |
| 645 | OffsetFPR += (OffsetFPR < 0) ? TopOfStack : 0; |
| 646 | OffsetVR += (OffsetVR < 0) ? TopOfStack : 0; |
| 647 | |
| 648 | // Get alloca register. |
| 649 | uint8_t FrameReg = TRI->getEncodingValue(Reg: TRI->getFrameRegister(MF: *MF)); |
| 650 | uint8_t AllocaReg = ZFL->hasFP(MF: *MF) ? FrameReg : 0; |
| 651 | assert(AllocaReg < 16 && "Can't have alloca register larger than 15" ); |
| 652 | |
| 653 | MCSymbol *PersonalityRoutine = nullptr; |
| 654 | MCSymbol *GCCEH = nullptr; |
| 655 | uint64_t PersonalityADADisp = 0; |
| 656 | uint64_t GCCEHADADisp = 0; |
| 657 | if (!MF->getLandingPads().empty()) { |
| 658 | const Function *Per = dyn_cast<Function>( |
| 659 | Val: MF->getFunction().getPersonalityFn()->stripPointerCasts()); |
| 660 | PersonalityRoutine = Per ? MF->getTarget().getSymbol(GV: Per) : nullptr; |
| 661 | if (PersonalityRoutine) { |
| 662 | GCCEH = MF->getContext().getOrCreateSymbol( |
| 663 | Name: Twine("GCC_except_table" ) + Twine(MF->getFunctionNumber())); |
| 664 | PersonalityADADisp = ADATable.insert( |
| 665 | Sym: PersonalityRoutine, SlotKind: SystemZII::MO_ADA_INDIRECT_FUNC_DESC); |
| 666 | GCCEHADADisp = ADATable.insert(Sym: GCCEH, SlotKind: SystemZII::MO_ADA_DATA_SYMBOL_ADDR); |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | // Get the name of the function, with suffix _. |
| 671 | std::string N(MF->getFunction().hasName() |
| 672 | ? Twine(MF->getFunction().getName()).concat(Suffix: "_" ).str() |
| 673 | : "" ); |
| 674 | |
| 675 | // Calculate the lables for the prolog size and the stack update symbol. |
| 676 | MCSymbol *EndOfPrologSym; |
| 677 | MCSymbol *StackUpdateSym; |
| 678 | determinePrologueStackUpdateSym(MF, EndOfPrologSym, StackUpdateSym); |
| 679 | |
| 680 | // Save the calculated values. |
| 681 | if (MF->getFunction().hasFnAttribute(Kind: "zos-ppa1-name" )) |
| 682 | Info.Name = |
| 683 | MF->getFunction().getFnAttribute(Kind: "zos-ppa1-name" ).getValueAsString(); |
| 684 | else if (MF->getFunction().hasName()) |
| 685 | Info.Name = MF->getFunction().getName(); |
| 686 | |
| 687 | Info.PPA1 = OutContext.createTempSymbol(Name: Twine("PPA1_" ).concat(Suffix: N), AlwaysAddSuffix: true); |
| 688 | Info.EPMarker = OutContext.createTempSymbol(Name: Twine("EPM_" ).concat(Suffix: N), AlwaysAddSuffix: true); |
| 689 | Info.FnEnd = OutContext.createTempSymbol(Name: Twine(N).concat(Suffix: "end_" )); |
| 690 | Info.Fn = CurrentFnSym; |
| 691 | Info.EndOfProlog = EndOfPrologSym; |
| 692 | Info.StackUpdate = StackUpdateSym; |
| 693 | Info.PersonalityADADisp = PersonalityADADisp; |
| 694 | Info.GCCEHADADisp = GCCEHADADisp; |
| 695 | Info.OffsetFPR = OffsetFPR; |
| 696 | Info.OffsetVR = OffsetVR; |
| 697 | Info.CallFrameSize = MFFrame.getMaxCallFrameSize(); |
| 698 | Info.SizeOfFnParams = ZFI->getSizeOfFnParams(); |
| 699 | Info.SavedGPRMask = SavedGPRMask; |
| 700 | Info.SavedFPRMask = SavedFPRMask; |
| 701 | Info.SavedVRMask = SavedVRMask; |
| 702 | Info.FrameReg = FrameReg; |
| 703 | Info.AllocaReg = AllocaReg; |
| 704 | Info.IsVarArg = MF->getFunction().isVarArg(); |
| 705 | Info.HasStackProtector = MFFrame.hasStackProtectorIndex(); |
| 706 | |
| 707 | ZOS->DeferredPPA1.push_back(Elt: Info); |
| 708 | } |
| 709 | |
| 710 | void SystemZXPLINKAsmPrinter::emitStartOfAsmFile(Module &M) { |
| 711 | emitPPA2(M); |
| 712 | // A main program needs CELQMAIN, through which the Language Environment |
| 713 | // startup (CELQSTRT) finds the main routine and its environment. It is |
| 714 | // emitted here and not at the end of the file, because the text section |
| 715 | // may already be closed then (e.g. after emitting the DWARF aranges). |
| 716 | if (const Function *MainFn = M.getFunction(Name: "main" ); |
| 717 | MainFn && !MainFn->isDeclaration()) |
| 718 | emitCELQMAIN(MainFn: *MainFn); |
| 719 | AsmPrinter::emitStartOfAsmFile(M); |
| 720 | } |
| 721 | |
| 722 | // Emit the stack restore stub. A stack restore must copy the frame header |
| 723 | // (which @@ALCAXP moved down with the stack pointer) to the restored stack |
| 724 | // pointer, and no signal may be delivered while the stack pointer is changed |
| 725 | // and the header is copied. Language Environment delays signals while a stub |
| 726 | // runs, so this is done in a stub routine: it has a stub entry marker |
| 727 | // (preceded by 8 bytes of padding, so that the entry is 16 bytes after the |
| 728 | // start, like for a normal routine), no PPA1 and no save area. Like the stack |
| 729 | // extension routine, it is called with BASR 3,3 and returns with B 2(3). It |
| 730 | // gets the new stack pointer in R1. The new stack pointer is never below the |
| 731 | // current one, and both are multiples of 32 apart (0 if no storage was |
| 732 | // allocated), so copying the header in blocks of 32 bytes from the highest |
| 733 | // block down never overwrites a block that is still to be copied. |
| 734 | void SystemZXPLINKAsmPrinter::emitStackRestoreStub() { |
| 735 | const MCSubtargetInfo &STI = TM.getMCSubtargetInfo(); |
| 736 | auto Emit = [&](const MCInst &Inst) { |
| 737 | OutStreamer->emitInstruction(Inst, STI); |
| 738 | }; |
| 739 | |
| 740 | OutStreamer->pushSection(); |
| 741 | OutStreamer->switchSection(Section: getObjFileLowering().getTextSection()); |
| 742 | OutStreamer->emitValueToAlignment(Alignment: Align(8)); |
| 743 | OutStreamer->AddComment(T: "XPLINK Stub Entry Padding" ); |
| 744 | OutStreamer->emitIntValue(Value: 0, Size: 8); |
| 745 | OutStreamer->AddComment(T: "XPLINK Stub Entry Marker" ); |
| 746 | OutStreamer->AddComment(T: "Eyecatcher 0x00C300C500C500" ); |
| 747 | OutStreamer->emitIntValueInHex(Value: 0x00C300C500C500, Size: 7); |
| 748 | OutStreamer->AddComment(T: "Mark Type C'4'" ); |
| 749 | OutStreamer->emitInt8(Value: 0xF4); |
| 750 | OutStreamer->emitLabel(Symbol: StackRestoreStub); |
| 751 | |
| 752 | SystemZXPLINK64Registers Regs; |
| 753 | const int Bias = Regs.getStackPointerBias(); |
| 754 | const int Block = 32; |
| 755 | for (int Off = Regs.getCallFrameSize() - Block; Off >= 0; Off -= Block) |
| 756 | Emit(MCInstBuilder(SystemZ::MVC) |
| 757 | .addReg(Reg: SystemZ::R1D) |
| 758 | .addImm(Val: Bias + Off) |
| 759 | .addImm(Val: Block) |
| 760 | .addReg(Reg: SystemZ::R4D) |
| 761 | .addImm(Val: Bias + Off)); |
| 762 | Emit(MCInstBuilder(SystemZ::LGR).addReg(Reg: SystemZ::R4D).addReg(Reg: SystemZ::R1D)); |
| 763 | Emit(MCInstBuilder(SystemZ::B).addReg(Reg: SystemZ::R3D).addImm(Val: 2).addReg(Reg: 0)); |
| 764 | OutStreamer->popSection(); |
| 765 | } |
| 766 | |
| 767 | void SystemZXPLINKAsmPrinter::emitPPA2(Module &M) { |
| 768 | auto *ZOS = getTargetStreamer(); |
| 769 | OutStreamer->pushSection(); |
| 770 | OutStreamer->switchSection(Section: getObjFileLowering().getTextSection()); |
| 771 | MCContext &OutContext = OutStreamer->getContext(); |
| 772 | // Make CELQSTRT symbol. |
| 773 | const char *StartSymbolName = "CELQSTRT" ; |
| 774 | MCSymbol *CELQSTRT = OutContext.getOrCreateSymbol(Name: StartSymbolName); |
| 775 | OutStreamer->emitSymbolAttribute(Symbol: CELQSTRT, Attribute: MCSA_OSLinkage); |
| 776 | OutStreamer->emitSymbolAttribute(Symbol: CELQSTRT, Attribute: MCSA_Global); |
| 777 | ZOS->emitRenameable(Sym: CELQSTRT, /*IsRenamable=*/false); |
| 778 | |
| 779 | // Create symbol and assign to streamer field for use in PPA1. |
| 780 | ZOS->PPA2Sym = OutContext.createTempSymbol(Name: "PPA2" , AlwaysAddSuffix: false); |
| 781 | MCSymbol *PPA2Sym = ZOS->PPA2Sym; |
| 782 | MCSymbol *DateVersionSym = OutContext.createTempSymbol(Name: "DVS" , AlwaysAddSuffix: false); |
| 783 | |
| 784 | std::time_t Time = getTranslationTime(M); |
| 785 | SmallString<14> CompilationTimeEBCDIC, CompilationTime; |
| 786 | CompilationTime = formatv(Fmt: "{0:%Y%m%d%H%M%S}" , Vals: llvm::sys::toUtcTime(T: Time)); |
| 787 | |
| 788 | uint32_t ProductVersion = getProductVersion(M), |
| 789 | ProductRelease = getProductRelease(M), |
| 790 | ProductPatch = getProductPatch(M); |
| 791 | |
| 792 | SmallString<6> VersionEBCDIC, Version; |
| 793 | Version = formatv(Fmt: "{0,0+2:d}{1,0+2:d}{2,0+2:d}" , Vals&: ProductVersion, |
| 794 | Vals&: ProductRelease, Vals&: ProductPatch); |
| 795 | |
| 796 | ConverterEBCDIC::convertToEBCDIC(Source: CompilationTime, Result&: CompilationTimeEBCDIC); |
| 797 | ConverterEBCDIC::convertToEBCDIC(Source: Version, Result&: VersionEBCDIC); |
| 798 | |
| 799 | enum class PPA2MemberId : uint8_t { |
| 800 | // See z/OS Language Environment Vendor Interfaces v2r5, p.23, for |
| 801 | // complete list. Only the C runtime is supported by this backend. |
| 802 | LE_C_Runtime = 3, |
| 803 | }; |
| 804 | enum class PPA2MemberSubId : uint8_t { |
| 805 | // List of languages using the LE C runtime implementation. |
| 806 | C = 0x00, |
| 807 | CXX = 0x01, |
| 808 | Swift = 0x03, |
| 809 | Go = 0x60, |
| 810 | LLVMBasedLang = 0xe7, |
| 811 | }; |
| 812 | // PPA2 Flags |
| 813 | enum class PPA2Flags : uint8_t { |
| 814 | CompileForBinaryFloatingPoint = 0x80, |
| 815 | CompiledWithXPLink = 0x01, |
| 816 | CompiledUnitASCII = 0x04, |
| 817 | HasServiceInfo = 0x20, |
| 818 | }; |
| 819 | |
| 820 | PPA2MemberSubId MemberSubId = PPA2MemberSubId::LLVMBasedLang; |
| 821 | if (auto *MD = M.getModuleFlag(Key: "zos_cu_language" )) { |
| 822 | StringRef Language = cast<MDString>(Val: MD)->getString(); |
| 823 | MemberSubId = StringSwitch<PPA2MemberSubId>(Language) |
| 824 | .Case(S: "C" , Value: PPA2MemberSubId::C) |
| 825 | .Case(S: "C++" , Value: PPA2MemberSubId::CXX) |
| 826 | .Case(S: "Swift" , Value: PPA2MemberSubId::Swift) |
| 827 | .Case(S: "Go" , Value: PPA2MemberSubId::Go) |
| 828 | .Default(Value: PPA2MemberSubId::LLVMBasedLang); |
| 829 | } |
| 830 | |
| 831 | // Emit PPA2 section. |
| 832 | OutStreamer->emitLabel(Symbol: PPA2Sym); |
| 833 | OutStreamer->emitInt8(Value: static_cast<uint8_t>(PPA2MemberId::LE_C_Runtime)); |
| 834 | OutStreamer->emitInt8(Value: static_cast<uint8_t>(MemberSubId)); |
| 835 | OutStreamer->emitInt8(Value: 0x22); // Member defined, c370_plist+c370_env |
| 836 | OutStreamer->emitInt8(Value: 0x04); // Control level 4 (XPLink) |
| 837 | OutStreamer->emitAbsoluteSymbolDiff(Hi: CELQSTRT, Lo: PPA2Sym, Size: 4); |
| 838 | OutStreamer->emitInt32(Value: 0x00000000); |
| 839 | OutStreamer->emitAbsoluteSymbolDiff(Hi: DateVersionSym, Lo: PPA2Sym, Size: 4); |
| 840 | OutStreamer->emitInt32( |
| 841 | Value: 0x00000000); // Offset to main entry point, always 0 (so says TR). |
| 842 | uint8_t Flgs = static_cast<uint8_t>(PPA2Flags::CompileForBinaryFloatingPoint); |
| 843 | Flgs |= static_cast<uint8_t>(PPA2Flags::CompiledWithXPLink); |
| 844 | |
| 845 | bool IsASCII = true; |
| 846 | if (auto *MD = M.getModuleFlag(Key: "zos_le_char_mode" )) { |
| 847 | const StringRef &CharMode = cast<MDString>(Val: MD)->getString(); |
| 848 | if (CharMode == "ebcdic" ) |
| 849 | IsASCII = false; |
| 850 | else if (CharMode != "ascii" ) |
| 851 | OutContext.reportError( |
| 852 | L: {}, Msg: "Only ascii or ebcdic are allowed for zos_le_char_mode" ); |
| 853 | } |
| 854 | if (IsASCII) |
| 855 | Flgs |= static_cast<uint8_t>( |
| 856 | PPA2Flags::CompiledUnitASCII); // Setting bit for ASCII char. mode. |
| 857 | |
| 858 | OutStreamer->emitInt8(Value: Flgs); |
| 859 | OutStreamer->emitInt8(Value: 0x00); // Reserved. |
| 860 | // No MD5 signature before timestamp. |
| 861 | // No FLOAT(AFP(VOLATILE)). |
| 862 | // Remaining 5 flag bits reserved. |
| 863 | OutStreamer->emitInt16(Value: 0x0000); // 16 Reserved flag bits. |
| 864 | |
| 865 | // Emit date and version section. |
| 866 | OutStreamer->emitLabel(Symbol: DateVersionSym); |
| 867 | OutStreamer->emitBytes(Data: CompilationTimeEBCDIC.str()); |
| 868 | OutStreamer->emitBytes(Data: VersionEBCDIC.str()); |
| 869 | |
| 870 | OutStreamer->emitInt16(Value: 0x0000); // Service level string length. |
| 871 | |
| 872 | // The binder requires that the offset to the PPA2 be emitted in a different, |
| 873 | // specially-named section. |
| 874 | OutStreamer->switchSection(Section: getObjFileLowering().getPPA2ListSection()); |
| 875 | // Emit 8 byte alignment. |
| 876 | // Emit pointer to PPA2 label. |
| 877 | OutStreamer->AddComment(T: "A(PPA2-CELQSTRT)" ); |
| 878 | OutStreamer->emitAbsoluteSymbolDiff(Hi: PPA2Sym, Lo: CELQSTRT, Size: 8); |
| 879 | OutStreamer->popSection(); |
| 880 | } |
| 881 | |
| 882 | void SystemZXPLINKAsmPrinter::emitGlobalAlias(const Module &M, |
| 883 | const GlobalAlias &GA) { |
| 884 | if (!M.getTargetTriple().isOSzOS()) |
| 885 | return AsmPrinter::emitGlobalAlias(M, GA); |
| 886 | |
| 887 | // Aliased function labels have already been emitted for z/OS |
| 888 | } |
| 889 | |
| 890 | const MCExpr *SystemZXPLINKAsmPrinter::lowerConstant(const Constant *CV, |
| 891 | const Constant *BaseCV, |
| 892 | uint64_t Offset) { |
| 893 | const GlobalAlias *GA = dyn_cast<GlobalAlias>(Val: CV); |
| 894 | const GlobalVariable *GV = dyn_cast<GlobalVariable>(Val: CV); |
| 895 | const Function *FV = dyn_cast<Function>(Val: CV); |
| 896 | |
| 897 | MCSymbol *Sym = nullptr; |
| 898 | if (GA) |
| 899 | Sym = getSymbol(GV: GA); |
| 900 | else if (FV) |
| 901 | Sym = getSymbol(GV: FV); |
| 902 | else if (GV) |
| 903 | Sym = getSymbol(GV); |
| 904 | else |
| 905 | return AsmPrinter::lowerConstant(CV); |
| 906 | |
| 907 | // Functions and function aliases need special handling. |
| 908 | if (FV || (GA && isa<Function>(Val: GA->getAliaseeObject()))) { |
| 909 | // A function pointer must point to a function descriptor (a V-con would |
| 910 | // yield the entry point instead), and it must compare equal to the address |
| 911 | // of the same function taken in code. For a function that is not internal, |
| 912 | // code loads the address of the descriptor from an ADA slot that refers to |
| 913 | // the indirect symbol; the binder resolves that reference to the function |
| 914 | // descriptor. Use the same reference here. The slot is shared with code |
| 915 | // taking the address and defines the indirect symbol. |
| 916 | const GlobalValue *FGV = GA ? static_cast<const GlobalValue *>(GA) |
| 917 | : static_cast<const GlobalValue *>(FV); |
| 918 | if (!FGV->hasLocalLinkage()) { |
| 919 | ADATable.insert(Sym, SlotKind: SystemZII::MO_ADA_INDIRECT_FUNC_DESC); |
| 920 | MCSymbol *Alias = OutContext.getOrCreateSymbol( |
| 921 | Name: Twine(Sym->getName()).concat(Suffix: "@indirect" )); |
| 922 | return MCSpecifierExpr::create(Expr: MCSymbolRefExpr::create(Symbol: Alias, Ctx&: OutContext), |
| 923 | S: SystemZ::S_VCon, Ctx&: OutContext); |
| 924 | } |
| 925 | // Internal functions: function descriptor in the ADA. |
| 926 | unsigned Disp = ADATable.insert(Sym, SlotKind: SystemZII::MO_ADA_DIRECT_FUNC_DESC); |
| 927 | return MCBinaryExpr::createAdd( |
| 928 | LHS: MCSpecifierExpr::create( |
| 929 | Expr: MCSymbolRefExpr::create( |
| 930 | Symbol: getObjFileLowering().getADASection()->getBeginSymbol(), |
| 931 | Ctx&: OutContext), |
| 932 | S: SystemZ::S_None, Ctx&: OutContext), |
| 933 | RHS: MCConstantExpr::create(Value: Disp, Ctx&: OutContext), Ctx&: OutContext); |
| 934 | } |
| 935 | auto *ZOS = getTargetStreamer(); |
| 936 | ZOS->emitADA(Sym, Section: OutContext.getObjectFileInfo()->getADASection()); |
| 937 | OutStreamer->emitSymbolAttribute(Symbol: Sym, Attribute: MCSA_ELF_TypeObject); |
| 938 | return MCSymbolRefExpr::create(Symbol: Sym, Ctx&: OutContext); |
| 939 | } |
| 940 | |
| 941 | void SystemZXPLINKAsmPrinter::emitFunctionEntryLabel() { |
| 942 | auto *ZOS = getTargetStreamer(); |
| 943 | |
| 944 | // A naked function is an XPLINK stub: it has a stub entry marker and no PPA1. |
| 945 | if (MF->getFunction().hasFnAttribute(Kind: Attribute::Naked)) { |
| 946 | // The entry marker of a normal function is 16 bytes. Pad the 8 byte stub |
| 947 | // marker, so that the entry point is at the same offset from the start of |
| 948 | // the function. |
| 949 | OutStreamer->AddComment(T: "XPLINK Stub Entry Padding" ); |
| 950 | OutStreamer->emitZeros(NumBytes: 8); |
| 951 | OutStreamer->AddComment(T: "XPLINK Stub Entry Marker" ); |
| 952 | OutStreamer->AddComment(T: "Eyecatcher 0x00C300C500C500" ); |
| 953 | OutStreamer->emitIntValueInHex(Value: 0x00C300C500C500, Size: 7); // Eyecatcher. |
| 954 | OutStreamer->AddComment(T: "Mark Type C'4'" ); |
| 955 | OutStreamer->emitInt8(Value: 0xF4); // Mark Type. |
| 956 | } else { |
| 957 | calculatePPA1(); |
| 958 | |
| 959 | // EntryPoint Marker |
| 960 | const MachineFrameInfo &MFFrame = MF->getFrameInfo(); |
| 961 | bool IsUsingAlloca = MFFrame.hasVarSizedObjects(); |
| 962 | uint32_t DSASize = MFFrame.getStackSize(); |
| 963 | bool IsLeaf = DSASize == 0 && MFFrame.getCalleeSavedInfo().empty(); |
| 964 | |
| 965 | // Set Flags. |
| 966 | uint8_t Flags = 0; |
| 967 | if (IsLeaf) |
| 968 | Flags |= 0x08; |
| 969 | if (IsUsingAlloca) |
| 970 | Flags |= 0x04; |
| 971 | |
| 972 | // Combine into top 27 bits of DSASize and bottom 5 bits of Flags. |
| 973 | uint32_t DSAAndFlags = DSASize & 0xFFFFFFE0; // (x/32) << 5 |
| 974 | DSAAndFlags |= Flags; |
| 975 | |
| 976 | // Emit entry point marker section. |
| 977 | OutStreamer->AddComment(T: "XPLINK Routine Layout Entry" ); |
| 978 | OutStreamer->emitLabel(Symbol: ZOS->DeferredPPA1.back().EPMarker); |
| 979 | OutStreamer->AddComment(T: "Eyecatcher 0x00C300C500C500" ); |
| 980 | OutStreamer->emitIntValueInHex(Value: 0x00C300C500C500, Size: 7); // Eyecatcher. |
| 981 | OutStreamer->AddComment(T: "Mark Type C'1'" ); |
| 982 | OutStreamer->emitInt8(Value: 0xF1); // Mark Type. |
| 983 | OutStreamer->AddComment(T: "Offset to PPA1" ); |
| 984 | OutStreamer->emitAbsoluteSymbolDiff(Hi: ZOS->DeferredPPA1.back().PPA1, |
| 985 | Lo: ZOS->DeferredPPA1.back().EPMarker, Size: 4); |
| 986 | if (OutStreamer->isVerboseAsm()) { |
| 987 | OutStreamer->AddComment(T: "DSA Size 0x" + Twine::utohexstr(Val: DSASize)); |
| 988 | OutStreamer->AddComment(T: "Entry Flags" ); |
| 989 | if (Flags & 0x08) |
| 990 | OutStreamer->AddComment(T: " Bit 1: 1 = Leaf function" ); |
| 991 | else |
| 992 | OutStreamer->AddComment(T: " Bit 1: 0 = Non-leaf function" ); |
| 993 | if (Flags & 0x04) |
| 994 | OutStreamer->AddComment(T: " Bit 2: 1 = Uses alloca" ); |
| 995 | else |
| 996 | OutStreamer->AddComment(T: " Bit 2: 0 = Does not use alloca" ); |
| 997 | } |
| 998 | OutStreamer->emitInt32(Value: DSAAndFlags); |
| 999 | } |
| 1000 | |
| 1001 | ZOS->emitADA(Sym: CurrentFnSym, Section: getObjFileLowering().getADASection()); |
| 1002 | |
| 1003 | AsmPrinter::emitFunctionEntryLabel(); |
| 1004 | |
| 1005 | const Function *F = &MF->getFunction(); |
| 1006 | // Emit aliasing label for function entry point label. |
| 1007 | for (const GlobalAlias *Alias : GOAliasMap[F]) { |
| 1008 | MCSymbol *Sym = getSymbol(GV: Alias); |
| 1009 | ZOS->emitADA(Sym, Section: getObjFileLowering().getADASection()); |
| 1010 | OutStreamer->emitSymbolAttribute(Symbol: Sym, Attribute: MCSA_ELF_TypeFunction); |
| 1011 | emitVisibility(Sym, Visibility: Alias->getVisibility()); |
| 1012 | emitLinkage(GV: Alias, GVSym: Sym); |
| 1013 | OutStreamer->emitLabel(Symbol: Sym); |
| 1014 | } |
| 1015 | } |
| 1016 | |