| 1 | //===- SystemZHLASMAsmStreamer.cpp - HLASM Assembly Text Output -----------===// |
| 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 | #include "SystemZHLASMAsmStreamer.h" |
| 10 | #include "llvm/ADT/StringExtras.h" |
| 11 | #include "llvm/BinaryFormat/GOFF.h" |
| 12 | #include "llvm/MC/MCExpr.h" |
| 13 | #include "llvm/MC/MCSectionGOFF.h" |
| 14 | #include "llvm/MC/MCSymbolGOFF.h" |
| 15 | #include "llvm/Support/Casting.h" |
| 16 | #include "llvm/Support/Format.h" |
| 17 | #include "llvm/Support/Signals.h" |
| 18 | #include <tuple> |
| 19 | |
| 20 | using namespace llvm; |
| 21 | |
| 22 | void SystemZHLASMAsmStreamer::visitUsedSymbol(const MCSymbol &Sym) { |
| 23 | Assembler->registerSymbol(Symbol: Sym); |
| 24 | } |
| 25 | |
| 26 | void SystemZHLASMAsmStreamer::EmitEOL() { |
| 27 | // Comments are emitted on a new line before the instruction. |
| 28 | if (IsVerboseAsm) |
| 29 | EmitComment(); |
| 30 | |
| 31 | // The continuation character is repeated at the start of the next line. |
| 32 | // If the last character of a line that is continued is a letter or a digit or |
| 33 | // one of _$@#, we reuse it as the continuation character by making the next |
| 34 | // substring start 1 character sooner, thus "duplicating" that character. |
| 35 | // Otherwise, we use an X as the continuation character, and the next line |
| 36 | // starts with an X followed by the remaining characters. Returns true if |
| 37 | // an X was used. |
| 38 | auto EmitContinued = [this](StringRef Chunk) { |
| 39 | char C = Chunk.back(); |
| 40 | bool IsValid = isAlnum(C) || C == '_' || C == '$' || C == '@' || C == '#'; |
| 41 | FOS << Chunk.drop_back() << (IsValid ? C : 'X'); |
| 42 | }; |
| 43 | |
| 44 | StringRef Remaining(Str); |
| 45 | while (!Remaining.empty()) { |
| 46 | StringRef S; |
| 47 | std::tie(args&: S, args&: Remaining) = Remaining.split(Separator: '\n'); |
| 48 | if (LLVM_LIKELY(S.size() < ContIndicatorColumn)) { |
| 49 | FOS << S << '\n'; |
| 50 | continue; |
| 51 | } |
| 52 | |
| 53 | EmitContinued(S.take_front(N: ContIndicatorColumn)); |
| 54 | FOS << '\n'; |
| 55 | |
| 56 | size_t Emitted = ContIndicatorColumn - 1; |
| 57 | while (Emitted < S.size()) { |
| 58 | StringRef Chunk = S.substr(Start: Emitted, N: ContLen); |
| 59 | FOS.PadToColumn(NewCol: ContStartColumn); |
| 60 | if (Chunk.size() < ContLen) |
| 61 | FOS << Chunk; |
| 62 | else |
| 63 | EmitContinued(Chunk); |
| 64 | FOS << '\n'; |
| 65 | Emitted += ContLen - 1; |
| 66 | } |
| 67 | } |
| 68 | Str.clear(); |
| 69 | } |
| 70 | |
| 71 | static void emitXATTR(raw_ostream &OS, StringRef Name, MCSectionGOFF *ADA, |
| 72 | bool IsIndirectReference, GOFF::ESDLinkageType Linkage, |
| 73 | GOFF::ESDExecutable Executable, |
| 74 | GOFF::ESDBindingScope BindingScope) { |
| 75 | llvm::ListSeparator Sep("," ); |
| 76 | OS << Name << " XATTR " ; |
| 77 | OS << Sep << "LINKAGE(" << (Linkage == GOFF::ESD_LT_OS ? "OS" : "XPLINK" ) |
| 78 | << ")" ; |
| 79 | |
| 80 | const bool NotUnspecified = (Executable != GOFF::ESD_EXE_Unspecified); |
| 81 | if (NotUnspecified || IsIndirectReference) { |
| 82 | OS << Sep << "REFERENCE(" ; |
| 83 | llvm::ListSeparator SepRef("," ); |
| 84 | |
| 85 | if (NotUnspecified) |
| 86 | OS << SepRef << (Executable == GOFF::ESD_EXE_CODE ? "CODE" : "DATA" ); |
| 87 | |
| 88 | if (IsIndirectReference) |
| 89 | OS << SepRef << "INDIRECT" ; |
| 90 | |
| 91 | OS << ")" ; |
| 92 | } |
| 93 | // Emit PSECT only for code symbols. |
| 94 | if (ADA && Executable != GOFF::ESD_EXE_DATA) |
| 95 | OS << Sep << "PSECT(" << ADA->getName() << ")" ; |
| 96 | if (BindingScope != GOFF::ESD_BSC_Unspecified) { |
| 97 | OS << Sep << "SCOPE(" ; |
| 98 | switch (BindingScope) { |
| 99 | case GOFF::ESD_BSC_Section: |
| 100 | OS << "SECTION" ; |
| 101 | break; |
| 102 | case GOFF::ESD_BSC_Module: |
| 103 | OS << "MODULE" ; |
| 104 | break; |
| 105 | case GOFF::ESD_BSC_Library: |
| 106 | OS << "LIBRARY" ; |
| 107 | break; |
| 108 | case GOFF::ESD_BSC_ImportExport: |
| 109 | OS << "EXPORT" ; |
| 110 | break; |
| 111 | default: |
| 112 | break; |
| 113 | } |
| 114 | OS << ')'; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | static void emitCATTR(raw_ostream &OS, StringRef Name, GOFF::ESDRmode Rmode, |
| 119 | GOFF::ESDAlignment Alignment, |
| 120 | GOFF::ESDLoadingBehavior LoadBehavior, |
| 121 | GOFF::ESDTaskingBehavior TaskingBehavior, |
| 122 | GOFF::ESDExecutable Executable, bool IsReadOnly, |
| 123 | uint32_t SortKey, uint8_t FillByteValue, |
| 124 | StringRef PartName) { |
| 125 | OS << Name << " CATTR " ; |
| 126 | OS << "ALIGN(" << static_cast<unsigned>(Alignment) << ")," |
| 127 | << "FILL(" << static_cast<unsigned>(FillByteValue) << ")" ; |
| 128 | switch (LoadBehavior) { |
| 129 | case GOFF::ESD_LB_Deferred: |
| 130 | OS << ",DEFLOAD" ; |
| 131 | break; |
| 132 | case GOFF::ESD_LB_NoLoad: |
| 133 | OS << ",NOLOAD" ; |
| 134 | break; |
| 135 | default: |
| 136 | break; |
| 137 | } |
| 138 | switch (TaskingBehavior) { |
| 139 | case GOFF::ESD_TA_NonReus: |
| 140 | OS << ",NOTREUS" ; |
| 141 | break; |
| 142 | case GOFF::ESD_TA_Reus: |
| 143 | OS << ",REUS" ; |
| 144 | break; |
| 145 | case GOFF::ESD_TA_Rent: |
| 146 | OS << ",RENT" ; |
| 147 | break; |
| 148 | default: |
| 149 | break; |
| 150 | } |
| 151 | switch (Executable) { |
| 152 | case GOFF::ESD_EXE_CODE: |
| 153 | OS << ",EXECUTABLE" ; |
| 154 | break; |
| 155 | case GOFF::ESD_EXE_DATA: |
| 156 | OS << ",NOTEXECUTABLE" ; |
| 157 | break; |
| 158 | default: |
| 159 | break; |
| 160 | } |
| 161 | if (IsReadOnly) |
| 162 | OS << ",READONLY" ; |
| 163 | if (Rmode != GOFF::ESD_RMODE_None) { |
| 164 | OS << ','; |
| 165 | OS << "RMODE(" ; |
| 166 | switch (Rmode) { |
| 167 | case GOFF::ESD_RMODE_None: |
| 168 | llvm_unreachable("" ); |
| 169 | case GOFF::ESD_RMODE_24: |
| 170 | OS << "24" ; |
| 171 | break; |
| 172 | case GOFF::ESD_RMODE_31: |
| 173 | OS << "31" ; |
| 174 | break; |
| 175 | case GOFF::ESD_RMODE_64: |
| 176 | OS << "64" ; |
| 177 | break; |
| 178 | } |
| 179 | OS << ')'; |
| 180 | } |
| 181 | if (SortKey) |
| 182 | OS << ",PRIORITY(" << SortKey << ")" ; |
| 183 | if (!PartName.empty()) |
| 184 | OS << ",PART(" << PartName << ")" ; |
| 185 | OS << '\n'; |
| 186 | } |
| 187 | |
| 188 | static void emitALIAS(raw_ostream &OS, StringRef Name, StringRef ExternalName) { |
| 189 | OS << Name << " ALIAS C'" ; |
| 190 | for (char C : ExternalName) { |
| 191 | if (C == '\'' || C == '&') |
| 192 | OS << C; |
| 193 | OS << C; |
| 194 | } |
| 195 | OS << "'\n" ; |
| 196 | } |
| 197 | |
| 198 | void SystemZHLASMAsmStreamer::changeSection(MCSection *Section, |
| 199 | uint32_t Subsection) { |
| 200 | auto &Sec = *static_cast<MCSectionGOFF *>(Section); |
| 201 | auto EmitExternalName = [&Sec, this]() { |
| 202 | if (Sec.hasExternalName()) |
| 203 | emitALIAS(OS, Name: Sec.getName(), ExternalName: Sec.getExternalName()); |
| 204 | }; |
| 205 | switch (Sec.getSymbolType()) { |
| 206 | case GOFF::ESD_ST_SectionDefinition: { |
| 207 | OS << Sec.getName() << " CSECT\n" ; |
| 208 | Sec.setEmitted(); |
| 209 | EmitExternalName(); |
| 210 | break; |
| 211 | } |
| 212 | case GOFF::ESD_ST_ElementDefinition: { |
| 213 | changeSection(Section: Sec.getParent(), Subsection); |
| 214 | if (!Sec.isEmitted()) { |
| 215 | GOFF::EDAttr ED = Sec.getEDAttributes(); |
| 216 | GOFF::ESDTaskingBehavior TaskingBehavior = |
| 217 | static_cast<MCSectionGOFF *>(Sec.getParent()) |
| 218 | ->getSDAttributes() |
| 219 | .TaskingBehavior; |
| 220 | emitCATTR(OS, Name: Sec.getName(), Rmode: ED.Rmode, Alignment: Sec.getEDAlignment(), |
| 221 | LoadBehavior: ED.LoadBehavior, TaskingBehavior, Executable: GOFF::ESD_EXE_Unspecified, |
| 222 | IsReadOnly: ED.IsReadOnly, SortKey: 0, FillByteValue: ED.FillByteValue, PartName: StringRef()); |
| 223 | if (auto *BeginSym = static_cast<MCSymbolGOFF *>(Sec.getBeginSymbol())) { |
| 224 | if (BeginSym->getADA()) { |
| 225 | emitXATTR(OS, Name: BeginSym->getName(), ADA: BeginSym->getADA(), |
| 226 | /*IsIndirectReference=*/false, Linkage: GOFF::ESD_LT_XPLink, |
| 227 | Executable: GOFF::ESD_EXE_Unspecified, BindingScope: GOFF::ESD_BSC_Section); |
| 228 | OS << '\n'; |
| 229 | } |
| 230 | } |
| 231 | Sec.setEmitted(); |
| 232 | EmitExternalName(); |
| 233 | } else |
| 234 | OS << Sec.getName() << " CATTR\n" ; |
| 235 | break; |
| 236 | } |
| 237 | case GOFF::ESD_ST_PartReference: { |
| 238 | MCSectionGOFF *ED = Sec.getParent(); |
| 239 | changeSection(Section: ED->getParent(), Subsection); |
| 240 | if (!Sec.isEmitted()) { |
| 241 | GOFF::ESDTaskingBehavior TaskingBehavior = |
| 242 | static_cast<MCSectionGOFF *>(ED->getParent()) |
| 243 | ->getSDAttributes() |
| 244 | .TaskingBehavior; |
| 245 | GOFF::EDAttr EDAttr = ED->getEDAttributes(); |
| 246 | GOFF::PRAttr PRAttr = Sec.getPRAttributes(); |
| 247 | emitCATTR(OS, Name: ED->getName(), Rmode: EDAttr.Rmode, Alignment: ED->getEDAlignment(), |
| 248 | LoadBehavior: EDAttr.LoadBehavior, TaskingBehavior, Executable: PRAttr.Executable, |
| 249 | IsReadOnly: EDAttr.IsReadOnly, SortKey: PRAttr.SortKey, FillByteValue: EDAttr.FillByteValue, |
| 250 | PartName: Sec.getName()); |
| 251 | MCSectionGOFF *ADA = |
| 252 | Sec.getBeginSymbol() != nullptr |
| 253 | ? static_cast<MCSymbolGOFF *>(Sec.getBeginSymbol())->getADA() |
| 254 | : nullptr; |
| 255 | emitXATTR(OS, Name: Sec.getName(), ADA, /*IsIndirectReference=*/false, |
| 256 | Linkage: PRAttr.Linkage, Executable: PRAttr.Executable, BindingScope: PRAttr.BindingScope); |
| 257 | OS << '\n'; |
| 258 | ED->setEmitted(); |
| 259 | Sec.setEmitted(); |
| 260 | EmitExternalName(); |
| 261 | } else |
| 262 | OS << ED->getName() << " CATTR PART(" << Sec.getName() << ")\n" ; |
| 263 | break; |
| 264 | } |
| 265 | default: |
| 266 | llvm_unreachable("Wrong section type" ); |
| 267 | } |
| 268 | MCStreamer::changeSection(Section, Subsection); |
| 269 | EmitEOL(); |
| 270 | } |
| 271 | |
| 272 | void SystemZHLASMAsmStreamer::emitAlignmentDS(uint64_t ByteAlignment, |
| 273 | std::optional<int64_t> Value, |
| 274 | unsigned ValueSize, |
| 275 | unsigned MaxBytesToEmit) { |
| 276 | if (!isPowerOf2_64(Value: ByteAlignment)) |
| 277 | report_fatal_error(reason: "Only power-of-two alignments are supported " ); |
| 278 | |
| 279 | OS << " DS 0" ; |
| 280 | switch (ValueSize) { |
| 281 | default: |
| 282 | llvm_unreachable("Invalid size for machine code value!" ); |
| 283 | case 1: |
| 284 | OS << "B" ; |
| 285 | break; |
| 286 | case 2: |
| 287 | OS << "H" ; |
| 288 | break; |
| 289 | case 4: |
| 290 | OS << "F" ; |
| 291 | break; |
| 292 | case 8: |
| 293 | OS << "D" ; |
| 294 | break; |
| 295 | case 16: |
| 296 | OS << "Q" ; |
| 297 | break; |
| 298 | } |
| 299 | |
| 300 | EmitEOL(); |
| 301 | } |
| 302 | |
| 303 | void SystemZHLASMAsmStreamer::(const Twine &T, bool TabPrefix) { |
| 304 | OS << MAI->getCommentString() << T; |
| 305 | EmitEOL(); |
| 306 | } |
| 307 | |
| 308 | void SystemZHLASMAsmStreamer::(const Twine &T, bool EOL) { |
| 309 | if (!IsVerboseAsm) |
| 310 | return; |
| 311 | |
| 312 | T.toVector(Out&: CommentToEmit); |
| 313 | |
| 314 | if (EOL) |
| 315 | CommentToEmit.push_back(Elt: '\n'); // Place comment in a new line. |
| 316 | } |
| 317 | |
| 318 | void SystemZHLASMAsmStreamer::() { |
| 319 | if (CommentToEmit.empty() && CommentStream.GetNumBytesInBuffer() == 0) |
| 320 | return; |
| 321 | |
| 322 | StringRef = CommentToEmit; |
| 323 | |
| 324 | assert(Comments.back() == '\n' && "Comment array not newline terminated" ); |
| 325 | do { |
| 326 | // Emit a line of comments, but not exceeding 80 characters. |
| 327 | size_t Position = std::min(a: InstLimit - 2, b: Comments.find(C: '\n')); |
| 328 | FOS << MAI->getCommentString() << ' ' << Comments.substr(Start: 0, N: Position) |
| 329 | << '\n'; |
| 330 | |
| 331 | if (Comments[Position] == '\n') |
| 332 | Position++; |
| 333 | Comments = Comments.substr(Start: Position); |
| 334 | } while (!Comments.empty()); |
| 335 | |
| 336 | CommentToEmit.clear(); |
| 337 | } |
| 338 | |
| 339 | void SystemZHLASMAsmStreamer::emitValueToAlignment(Align Alignment, |
| 340 | int64_t Fill, |
| 341 | uint8_t FillLen, |
| 342 | unsigned MaxBytesToEmit) { |
| 343 | emitAlignmentDS(ByteAlignment: Alignment.value(), Value: Fill, ValueSize: FillLen, MaxBytesToEmit); |
| 344 | } |
| 345 | |
| 346 | void SystemZHLASMAsmStreamer::emitCodeAlignment(Align Alignment, |
| 347 | const MCSubtargetInfo &STI, |
| 348 | unsigned MaxBytesToEmit) { |
| 349 | // Emit with a text fill value. |
| 350 | if (MAI->getTextAlignFillValue()) |
| 351 | emitAlignmentDS(ByteAlignment: Alignment.value(), Value: MAI->getTextAlignFillValue(), ValueSize: 1, |
| 352 | MaxBytesToEmit); |
| 353 | else |
| 354 | emitAlignmentDS(ByteAlignment: Alignment.value(), Value: std::nullopt, ValueSize: 1, MaxBytesToEmit); |
| 355 | } |
| 356 | |
| 357 | void SystemZHLASMAsmStreamer::emitBytes(StringRef Data) { |
| 358 | assert(getCurrentSectionOnly() && |
| 359 | "Cannot emit contents before setting section!" ); |
| 360 | for (size_t I = 0, E = Data.size(); I < E; I += MaxXLLen) { |
| 361 | StringRef Chunk = Data.substr(Start: I, N: MaxXLLen); |
| 362 | OS << " DC XL" << Chunk.size() << '\'' << toHex(Input: Chunk) << '\''; |
| 363 | EmitEOL(); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | void SystemZHLASMAsmStreamer::emitFill(const MCExpr &NumBytes, |
| 368 | uint64_t FillValue, SMLoc Loc) { |
| 369 | int64_t Count; |
| 370 | if (!NumBytes.evaluateAsAbsolute(Res&: Count)) { |
| 371 | getContext().reportError(L: Loc, Msg: "fill length must be absolute in HLASM" ); |
| 372 | return; |
| 373 | } |
| 374 | if (Count < 0) { |
| 375 | getContext().reportError(L: Loc, Msg: "invalid number of bytes" ); |
| 376 | return; |
| 377 | } |
| 378 | if (Count > 0x7FFFFFFF) { |
| 379 | getContext().reportError(L: Loc, Msg: "fill length too large for HLASM" ); |
| 380 | return; |
| 381 | } |
| 382 | if (Count == 0) |
| 383 | return; |
| 384 | |
| 385 | OS << " DC " << Count << "XL1'" |
| 386 | << format_hex_no_prefix(N: FillValue & 0xff, Width: 2, Upper: true) << '\''; |
| 387 | EmitEOL(); |
| 388 | } |
| 389 | |
| 390 | void SystemZHLASMAsmStreamer::(const MCInst &Inst, |
| 391 | const MCSubtargetInfo &STI) { |
| 392 | raw_ostream &OS = getCommentOS(); |
| 393 | SmallString<256> Code; |
| 394 | SmallVector<MCFixup, 4> Fixups; |
| 395 | |
| 396 | // If we have no code emitter, don't emit code. |
| 397 | if (!getAssembler().getEmitterPtr()) |
| 398 | return; |
| 399 | |
| 400 | getAssembler().getEmitter().encodeInstruction(Inst, CB&: Code, Fixups, STI); |
| 401 | |
| 402 | // If we are showing fixups, create symbolic markers in the encoded |
| 403 | // representation. We do this by making a per-bit map to the fixup item index, |
| 404 | // then trying to display it as nicely as possible. |
| 405 | SmallVector<uint8_t, 64> FixupMap; |
| 406 | FixupMap.resize(N: Code.size() * 8); |
| 407 | for (unsigned I = 0, E = Code.size() * 8; I != E; ++I) |
| 408 | FixupMap[I] = 0; |
| 409 | |
| 410 | for (unsigned I = 0, E = Fixups.size(); I != E; ++I) { |
| 411 | MCFixup &F = Fixups[I]; |
| 412 | MCFixupKindInfo Info = |
| 413 | getAssembler().getBackend().getFixupKindInfo(Kind: F.getKind()); |
| 414 | for (unsigned J = 0; J != Info.TargetSize; ++J) { |
| 415 | unsigned Index = F.getOffset() * 8 + Info.TargetOffset + J; |
| 416 | assert(Index < Code.size() * 8 && "Invalid offset in fixup!" ); |
| 417 | FixupMap[Index] = 1 + I; |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | OS << "encoding: [" ; |
| 422 | for (unsigned I = 0, E = Code.size(); I != E; ++I) { |
| 423 | if (I) |
| 424 | OS << ','; |
| 425 | |
| 426 | // See if all bits are the same map entry. |
| 427 | uint8_t MapEntry = FixupMap[I * 8 + 0]; |
| 428 | for (unsigned J = 1; J != 8; ++J) { |
| 429 | if (FixupMap[I * 8 + J] == MapEntry) |
| 430 | continue; |
| 431 | |
| 432 | MapEntry = uint8_t(~0U); |
| 433 | break; |
| 434 | } |
| 435 | |
| 436 | if (MapEntry != uint8_t(~0U)) { |
| 437 | if (MapEntry == 0) { |
| 438 | OS << format(Fmt: "0x%02x" , Vals: uint8_t(Code[I])); |
| 439 | } else { |
| 440 | if (Code[I]) { |
| 441 | // FIXME: Some of the 8 bits require fix up. |
| 442 | OS << format(Fmt: "0x%02x" , Vals: uint8_t(Code[I])) << '\'' |
| 443 | << char('A' + MapEntry - 1) << '\''; |
| 444 | } else |
| 445 | OS << char('A' + MapEntry - 1); |
| 446 | } |
| 447 | } else { |
| 448 | // Otherwise, write out in binary. |
| 449 | OS << "0b" ; |
| 450 | for (unsigned J = 8; J--;) { |
| 451 | unsigned Bit = (Code[I] >> J) & 1; |
| 452 | unsigned FixupBit = I * 8 + (7 - J); |
| 453 | if (uint8_t MapEntry = FixupMap[FixupBit]) { |
| 454 | assert(Bit == 0 && "Encoder wrote into fixed up bit!" ); |
| 455 | OS << char('A' + MapEntry - 1); |
| 456 | } else |
| 457 | OS << Bit; |
| 458 | } |
| 459 | } |
| 460 | } |
| 461 | OS << "]\n" ; |
| 462 | |
| 463 | for (unsigned I = 0, E = Fixups.size(); I != E; ++I) { |
| 464 | MCFixup &F = Fixups[I]; |
| 465 | OS << " fixup " << char('A' + I) << " - " |
| 466 | << "offset: " << F.getOffset() << ", value: " ; |
| 467 | MAI->printExpr(OS, *F.getValue()); |
| 468 | auto Kind = F.getKind(); |
| 469 | if (mc::isRelocation(FixupKind: Kind)) |
| 470 | OS << ", relocation type: " << Kind; |
| 471 | else { |
| 472 | OS << ", kind: " ; |
| 473 | auto Info = getAssembler().getBackend().getFixupKindInfo(Kind); |
| 474 | if (F.isPCRel() && StringRef(Info.Name).starts_with(Prefix: "FK_Data_" )) |
| 475 | OS << "FK_PCRel_" << (Info.TargetSize / 8); |
| 476 | else |
| 477 | OS << Info.Name; |
| 478 | } |
| 479 | OS << "\n" ; |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | void SystemZHLASMAsmStreamer::emitInstruction(const MCInst &Inst, |
| 484 | const MCSubtargetInfo &STI) { |
| 485 | // Show the encoding in a comment if we have a code emitter. |
| 486 | addEncodingComment(Inst, STI); |
| 487 | EmitEOL(); |
| 488 | |
| 489 | InstPrinter->printInst(MI: &Inst, Address: 0, Annot: "" , STI, OS); |
| 490 | EmitEOL(); |
| 491 | } |
| 492 | |
| 493 | void SystemZHLASMAsmStreamer::emitLabel(MCSymbol *Symbol, SMLoc Loc) { |
| 494 | MCSymbolGOFF *Sym = static_cast<MCSymbolGOFF *>(Symbol); |
| 495 | |
| 496 | MCStreamer::emitLabel(Symbol: Sym, Loc); |
| 497 | |
| 498 | // Emit label and ENTRY statement only if not implied by CSECT. Do not emit a |
| 499 | // label if the symbol is on a PR section. |
| 500 | bool EmitLabelAndEntry = |
| 501 | !static_cast<MCSectionGOFF *>(getCurrentSectionOnly())->isPR(); |
| 502 | if (!Sym->isTemporary() && Sym->isInEDSection()) { |
| 503 | EmitLabelAndEntry = |
| 504 | Sym->getName() != |
| 505 | static_cast<MCSectionGOFF &>(Sym->getSection()).getParent()->getName(); |
| 506 | if (EmitLabelAndEntry) { |
| 507 | OS << " ENTRY " << Sym->getName(); |
| 508 | EmitEOL(); |
| 509 | } |
| 510 | |
| 511 | emitXATTR(OS, Name: Sym->getName(), ADA: Sym->getADA(), IsIndirectReference: Sym->isIndirect(), |
| 512 | Linkage: Sym->getLinkage(), Executable: Sym->getCodeData(), BindingScope: Sym->getBindingScope()); |
| 513 | EmitEOL(); |
| 514 | if (Sym->hasExternalName()) |
| 515 | emitALIAS(OS, Name: Sym->getName(), ExternalName: Sym->getExternalName()); |
| 516 | } |
| 517 | |
| 518 | if (EmitLabelAndEntry) { |
| 519 | OS << Sym->getName() << " DS 0H" ; |
| 520 | EmitEOL(); |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | // Print the HLASM address constant type for a value of the given size: |
| 525 | // A(...) is a fullword, AD(...) a doubleword, AL<n>(...) any other length. |
| 526 | static void printAddrConstPrefix(raw_ostream &OS, unsigned Size) { |
| 527 | if (Size == 4) |
| 528 | OS << "A(" ; |
| 529 | else if (Size == 8) |
| 530 | OS << "AD(" ; |
| 531 | else |
| 532 | OS << "AL" << Size << '('; |
| 533 | } |
| 534 | |
| 535 | bool SystemZHLASMAsmStreamer::emitSymbolAttribute(MCSymbol *Sym, |
| 536 | MCSymbolAttr Attribute) { |
| 537 | return static_cast<MCSymbolGOFF *>(Sym)->setSymbolAttribute(Attribute); |
| 538 | } |
| 539 | |
| 540 | void SystemZHLASMAsmStreamer::emitCommonSymbol(MCSymbol *S, uint64_t Size, |
| 541 | Align ByteAlignment) { |
| 542 | auto *Symbol = static_cast<MCSymbolGOFF *>(S); |
| 543 | MCSectionGOFF *Section = |
| 544 | Symbol->getSectionForCommonSymbol(Ctx&: getContext(), ByteAlignment); |
| 545 | pushSection(); |
| 546 | switchSection(Section); |
| 547 | emitLabel(Symbol, Loc: SMLoc()); |
| 548 | emitZeros(NumBytes: Size); |
| 549 | popSection(); |
| 550 | } |
| 551 | |
| 552 | void SystemZHLASMAsmStreamer::emitRawTextImpl(StringRef String) { |
| 553 | String.consume_back(Suffix: "\n" ); |
| 554 | OS << String; |
| 555 | EmitEOL(); |
| 556 | } |
| 557 | |
| 558 | // Slight duplicate of MCExpr::print due to HLASM only recognizing limited |
| 559 | // arithmetic operators (+-*/). |
| 560 | void SystemZHLASMAsmStreamer::emitHLASMValueImpl(const MCExpr *Value, |
| 561 | unsigned Size, bool Parens) { |
| 562 | switch (Value->getKind()) { |
| 563 | case MCExpr::Constant: { |
| 564 | int64_t Val = cast<MCConstantExpr>(Val: Value)->getValue(); |
| 565 | OS << "XL" << Size << '\'' |
| 566 | << format_hex_no_prefix(N: Val, Width: Size * 2, /*Upper=*/true) << '\''; |
| 567 | return; |
| 568 | } |
| 569 | case MCExpr::Binary: { |
| 570 | const MCBinaryExpr &BE = cast<MCBinaryExpr>(Val: *Value); |
| 571 | int64_t Const; |
| 572 | // Or is handled differently. |
| 573 | if (BE.getOpcode() == MCBinaryExpr::Or) { |
| 574 | emitHLASMValueImpl(Value: BE.getLHS(), Size, Parens: true); |
| 575 | OS << ','; |
| 576 | emitHLASMValueImpl(Value: BE.getRHS(), Size, Parens: true); |
| 577 | return; |
| 578 | } |
| 579 | |
| 580 | if (Parens) |
| 581 | printAddrConstPrefix(OS, Size); |
| 582 | emitHLASMValueImpl(Value: BE.getLHS(), Size); |
| 583 | |
| 584 | switch (BE.getOpcode()) { |
| 585 | case MCBinaryExpr::LShr: { |
| 586 | Const = cast<MCConstantExpr>(Val: BE.getRHS())->getValue(); |
| 587 | OS << '/' << (1 << Const); |
| 588 | if (Parens) |
| 589 | OS << ')'; |
| 590 | return; |
| 591 | } |
| 592 | case MCBinaryExpr::Add: |
| 593 | OS << '+'; |
| 594 | break; |
| 595 | case MCBinaryExpr::Div: |
| 596 | OS << '/'; |
| 597 | break; |
| 598 | case MCBinaryExpr::Mul: |
| 599 | OS << '*'; |
| 600 | break; |
| 601 | case MCBinaryExpr::Sub: |
| 602 | OS << '-'; |
| 603 | break; |
| 604 | default: |
| 605 | getContext().reportError(L: SMLoc(), |
| 606 | Msg: "Unrecognized HLASM arithmetic expression!" ); |
| 607 | } |
| 608 | emitHLASMValueImpl(Value: BE.getRHS(), Size); |
| 609 | if (Parens) |
| 610 | OS << ')'; |
| 611 | return; |
| 612 | } |
| 613 | case MCExpr::Target: |
| 614 | MAI->printExpr(OS, *Value); |
| 615 | return; |
| 616 | default: |
| 617 | Parens &= isa<MCSymbolRefExpr>(Val: Value); |
| 618 | if (Parens) |
| 619 | printAddrConstPrefix(OS, Size); |
| 620 | MAI->printExpr(OS, *Value); |
| 621 | if (Parens) |
| 622 | OS << ')'; |
| 623 | return; |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | void SystemZHLASMAsmStreamer::emitValueImpl(const MCExpr *Value, unsigned Size, |
| 628 | SMLoc Loc) { |
| 629 | assert(Size <= 8 && "Invalid size" ); |
| 630 | assert(getCurrentSectionOnly() && |
| 631 | "Cannot emit contents before setting section!" ); |
| 632 | |
| 633 | int64_t AbsValue; |
| 634 | const auto *Sec = static_cast<const MCSectionGOFF *>(getCurrentSectionOnly()); |
| 635 | if (!Value->evaluateAsAbsolute(Res&: AbsValue, Asm: getAssemblerPtr()) && Size != 8 && |
| 636 | Sec->getParent() && Sec->getParent()->getName() == GOFF::CLASS_WSA) { |
| 637 | getContext().reportError(L: Loc, Msg: "address constant in C_WSA64 must be 8 bytes " |
| 638 | "(z/OS binder restriction IEW2572E)" ); |
| 639 | return; |
| 640 | } |
| 641 | |
| 642 | MCStreamer::emitValueImpl(Value, Size, Loc); |
| 643 | OS << " DC " ; |
| 644 | emitHLASMValueImpl(Value, Size, Parens: true); |
| 645 | EmitEOL(); |
| 646 | } |
| 647 | |
| 648 | void SystemZHLASMAsmStreamer::finishImpl() { |
| 649 | for (auto &Symbol : getAssembler().symbols()) { |
| 650 | if (Symbol.isTemporary() || !Symbol.isRegistered() || Symbol.isDefined()) |
| 651 | continue; |
| 652 | auto &Sym = static_cast<MCSymbolGOFF &>(const_cast<MCSymbol &>(Symbol)); |
| 653 | if (Sym.getCodeData() == GOFF::ESD_EXE_DATA) { |
| 654 | OS << Sym.getADA()->getParent()->getExternalName() << " CATTR PART(" |
| 655 | << Sym.getName() << ")" ; |
| 656 | EmitEOL(); |
| 657 | } else { |
| 658 | OS << " " << (Sym.isWeak() ? "WXTRN" : "EXTRN" ) << " " << Sym.getName(); |
| 659 | EmitEOL(); |
| 660 | } |
| 661 | emitXATTR(OS, Name: Sym.getName(), ADA: Sym.getADA(), IsIndirectReference: Sym.isIndirect(), |
| 662 | Linkage: Sym.getLinkage(), Executable: Sym.getCodeData(), BindingScope: Sym.getBindingScope()); |
| 663 | EmitEOL(); |
| 664 | if (Sym.hasExternalName()) |
| 665 | emitALIAS(OS, Name: Sym.getName(), ExternalName: Sym.getExternalName()); |
| 666 | } |
| 667 | |
| 668 | // Finish the assembly output. |
| 669 | OS << " END" ; |
| 670 | EmitEOL(); |
| 671 | } |
| 672 | |