1//=- SystemZHLASMInstPrinter.cpp - Convert SystemZ MCInst to HLASM assembly -=//
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 "SystemZHLASMInstPrinter.h"
10#include "MCTargetDesc/SystemZMCTargetDesc.h"
11#include "llvm/MC/MCInst.h"
12#include "llvm/MC/MCRegister.h"
13#include "llvm/Support/raw_ostream.h"
14
15using namespace llvm;
16
17#define DEBUG_TYPE "asm-printer"
18
19#include "SystemZGenHLASMAsmWriter.inc"
20
21void SystemZHLASMInstPrinter::printFormattedRegName(const MCAsmInfo *MAI,
22 MCRegister Reg,
23 raw_ostream &O) {
24 const char *RegName = getRegisterName(Reg);
25 // Skip register prefix so that only register number is left
26 assert(isalpha(RegName[0]) && isdigit(RegName[1]));
27 markup(OS&: O, M: Markup::Register) << (RegName + 1);
28}
29
30void SystemZHLASMInstPrinter::printInst(const MCInst *MI, uint64_t Address,
31 StringRef Annot,
32 const MCSubtargetInfo &STI,
33 raw_ostream &O) {
34 MCInst TempMI;
35 unsigned Opcode = MI->getOpcode();
36 if (Opcode == SystemZ::BRC || Opcode == SystemZ::BRCL ||
37 Opcode == SystemZ::BC || Opcode == SystemZ::BCR ||
38 Opcode == SystemZ::BIC) {
39 uint64_t CCValid = MI->getOperand(i: 0).getImm();
40 uint64_t CCMask = MI->getOperand(i: 1).getImm();
41 // Unsupported HLASM condition masks (which have no valid HLASM extended
42 // mnemonics): "nle" (3), "nhe" (5), "lh" (6), "nlh" (9), "he" (10), "le"
43 // (12).
44 if (CCMask == 3 || CCMask == 5 || CCMask == 6 || CCMask == 9 ||
45 CCMask == 10 || CCMask == 12) {
46 if ((CCValid & 1) == 0) {
47 // If CC 3 cannot happen, invert the low bit to yield a mask with a
48 // valid HLASM extended mnemonic (e.g. 10 ("he") -> 11 ("nl")).
49 TempMI = *MI;
50 TempMI.getOperand(i: 1).setImm(CCMask ^ 1);
51 MI = &TempMI;
52 } else {
53 // Use variant instructions that print numerical masks.
54 unsigned AsmOpcode = 0;
55 switch (Opcode) {
56 case SystemZ::BRC:
57 AsmOpcode = SystemZ::BRCAsm;
58 break;
59 case SystemZ::BRCL:
60 AsmOpcode = SystemZ::BRCLAsm;
61 break;
62 case SystemZ::BC:
63 AsmOpcode = SystemZ::BCAsm;
64 break;
65 case SystemZ::BCR:
66 AsmOpcode = SystemZ::BCRAsm;
67 break;
68 case SystemZ::BIC:
69 AsmOpcode = SystemZ::BICAsm;
70 break;
71 }
72 TempMI.setOpcode(AsmOpcode);
73 TempMI.addOperand(Op: MCOperand::createImm(Val: CCMask));
74 for (unsigned I = 2; I < MI->getNumOperands(); ++I)
75 TempMI.addOperand(Op: MI->getOperand(i: I));
76 MI = &TempMI;
77 }
78 }
79 }
80
81 std::string Str;
82 raw_string_ostream RSO(Str);
83 printInstruction(MI, Address, O&: RSO);
84 // Eat the first tab character and replace it with a space since it is
85 // hardcoded in AsmWriterEmitter::EmitPrintInstruction
86 // TODO: introduce a line prefix member to AsmWriter to avoid this problem
87 if (!Str.empty() && Str.front() == '\t')
88 O << " " << Str.substr(pos: 1, n: Str.length());
89 else
90 O << Str;
91
92 printAnnotation(OS&: O, Annot);
93}
94