LLVM 22.0.0git
LoongArchInstPrinter.cpp
Go to the documentation of this file.
1//===- LoongArchInstPrinter.cpp - Convert LoongArch MCInst to asm syntax --===//
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 class prints an LoongArch MCInst to a .s file.
10//
11//===----------------------------------------------------------------------===//
12
15#include "llvm/MC/MCAsmInfo.h"
16#include "llvm/MC/MCInst.h"
18#include "llvm/MC/MCSymbol.h"
20using namespace llvm;
21
22#define DEBUG_TYPE "loongarch-asm-printer"
23
24// Include the auto-generated portion of the assembly writer.
25#define PRINT_ALIAS_INSTR
26#include "LoongArchGenAsmWriter.inc"
27
28static cl::opt<bool>
29 NoAliases("loongarch-no-aliases",
30 cl::desc("Disable the emission of assembler pseudo instructions"),
31 cl::init(false), cl::Hidden);
32
33static cl::opt<bool>
34 NumericReg("loongarch-numeric-reg",
35 cl::desc("Print numeric register names rather than the ABI "
36 "names (such as $r0 instead of $zero)"),
37 cl::init(false), cl::Hidden);
38
39// The command-line flag above is used by llvm-mc and llc. It can be used by
40// `llvm-objdump`, but we override the value here to handle options passed to
41// `llvm-objdump` with `-M` (which matches GNU objdump). There did not seem to
42// be an easier way to allow these options in all these tools, without doing it
43// this way.
45 if (Opt == "no-aliases") {
46 PrintAliases = false;
47 return true;
48 }
49
50 if (Opt == "numeric") {
51 NumericReg = true;
52 return true;
53 }
54
55 return false;
56}
57
59 StringRef Annot,
60 const MCSubtargetInfo &STI,
61 raw_ostream &O) {
62 if (!PrintAliases || NoAliases || !printAliasInstr(MI, Address, STI, O))
63 printInstruction(MI, Address, STI, O);
64 printAnnotation(O, Annot);
65}
66
68 O << '$' << getRegisterName(Reg);
69}
70
71void LoongArchInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
72 const MCSubtargetInfo &STI,
73 raw_ostream &O) {
74 const MCOperand &MO = MI->getOperand(OpNo);
75
76 if (MO.isReg()) {
77 printRegName(O, MO.getReg());
78 return;
79 }
80
81 if (MO.isImm()) {
82 O << MO.getImm();
83 return;
84 }
85
86 assert(MO.isExpr() && "Unknown operand kind in printOperand");
87 MAI.printExpr(O, *MO.getExpr());
88}
89
91 const MCSubtargetInfo &STI,
92 raw_ostream &O) {
93 const MCOperand &MO = MI->getOperand(OpNo);
94 assert(MO.isReg() && "printAtomicMemOp can only print register operands");
95 printRegName(O, MO.getReg());
96}
97
99 // Default print reg alias name
100 return getRegisterName(Reg, NumericReg ? LoongArch::NoRegAltName
101 : LoongArch::RegAliasName);
102}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static cl::opt< bool > NoAliases("csky-no-aliases", cl::desc("Disable the emission of assembler pseudo instructions"), cl::init(false), cl::Hidden)
IRTranslator LLVM IR MI
static cl::opt< bool > NumericReg("loongarch-numeric-reg", cl::desc("Print numeric register names rather than the ABI " "names (such as $r0 instead of $zero)"), cl::init(false), cl::Hidden)
static cl::opt< bool > NoAliases("loongarch-no-aliases", cl::desc("Disable the emission of assembler pseudo instructions"), cl::init(false), cl::Hidden)
void printInstruction(const MCInst *MI, uint64_t Address, const MCSubtargetInfo &STI, raw_ostream &O)
void printInst(const MCInst *MI, uint64_t Address, StringRef Annot, const MCSubtargetInfo &STI, raw_ostream &O) override
Print the specified MCInst to the specified raw_ostream.
bool applyTargetSpecificCLOption(StringRef Opt) override
Customize the printer according to a command line option.
bool printAliasInstr(const MCInst *MI, uint64_t Address, const MCSubtargetInfo &STI, raw_ostream &O)
void printRegName(raw_ostream &O, MCRegister Reg) override
Print the assembler register name.
static const char * getRegisterName(MCRegister Reg)
void printAtomicMemOp(const MCInst *MI, unsigned OpNo, const MCSubtargetInfo &STI, raw_ostream &O)
void printExpr(raw_ostream &, const MCExpr &) const
Definition: MCAsmInfo.cpp:153
void printAnnotation(raw_ostream &OS, StringRef Annot)
Utility function for printing annotations.
const MCAsmInfo & MAI
Definition: MCInstPrinter.h:52
bool PrintAliases
True if we prefer aliases (e.g. nop) to raw mnemonics.
Definition: MCInstPrinter.h:64
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:188
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:40
int64_t getImm() const
Definition: MCInst.h:84
bool isImm() const
Definition: MCInst.h:66
bool isReg() const
Definition: MCInst.h:65
MCRegister getReg() const
Returns the register number.
Definition: MCInst.h:73
const MCExpr * getExpr() const
Definition: MCInst.h:118
bool isExpr() const
Definition: MCInst.h:69
Wrapper class representing physical registers. Should be passed by value.
Definition: MCRegister.h:33
Generic base class for all target subtargets.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:444
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18