LLVM  16.0.0git
LoongArchAsmPrinter.cpp
Go to the documentation of this file.
1 //===- LoongArchAsmPrinter.cpp - LoongArch LLVM Assembly Printer -*- C++ -*--=//
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 contains a printer that converts from our internal representation
10 // of machine-dependent LLVM code to GAS-format LoongArch assembly language.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "LoongArchAsmPrinter.h"
15 #include "LoongArch.h"
16 #include "LoongArchTargetMachine.h"
20 #include "llvm/MC/TargetRegistry.h"
21 
22 using namespace llvm;
23 
24 #define DEBUG_TYPE "loongarch-asm-printer"
25 
26 // Simple pseudo-instructions have their lowering (with expansion to real
27 // instructions) auto-generated.
28 #include "LoongArchGenMCPseudoLowering.inc"
29 
31  LoongArch_MC::verifyInstructionPredicates(
32  MI->getOpcode(), getSubtargetInfo().getFeatureBits());
33 
34  // Do any auto-generated pseudo lowerings.
36  return;
37 
38  MCInst TmpInst;
39  if (!lowerLoongArchMachineInstrToMCInst(MI, TmpInst, *this))
40  EmitToStreamer(*OutStreamer, TmpInst);
41 }
42 
44  const char *ExtraCode,
45  raw_ostream &OS) {
46  // First try the generic code, which knows about modifiers like 'c' and 'n'.
47  if (!AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, OS))
48  return false;
49 
50  const MachineOperand &MO = MI->getOperand(OpNo);
51  if (ExtraCode && ExtraCode[0]) {
52  if (ExtraCode[1] != 0)
53  return true; // Unknown modifier.
54 
55  switch (ExtraCode[0]) {
56  default:
57  return true; // Unknown modifier.
58  case 'z': // Print $zero register if zero, regular printing otherwise.
59  if (MO.isImm() && MO.getImm() == 0) {
60  OS << '$' << LoongArchInstPrinter::getRegisterName(LoongArch::R0);
61  return false;
62  }
63  break;
64  // TODO: handle other extra codes if any.
65  }
66  }
67 
68  switch (MO.getType()) {
70  OS << MO.getImm();
71  return false;
74  return false;
76  PrintSymbolOperand(MO, OS);
77  return false;
78  default:
79  llvm_unreachable("not implemented");
80  }
81 
82  return true;
83 }
84 
86  unsigned OpNo,
87  const char *ExtraCode,
88  raw_ostream &OS) {
89  // TODO: handle extra code.
90  if (ExtraCode)
91  return true;
92 
93  const MachineOperand &BaseMO = MI->getOperand(OpNo);
94  // Base address must be a register.
95  if (!BaseMO.isReg())
96  return true;
97  // Print the base address register.
98  OS << "$" << LoongArchInstPrinter::getRegisterName(BaseMO.getReg());
99  // Print the offset register or immediate if has.
100  if (OpNo + 1 < MI->getNumOperands()) {
101  const MachineOperand &OffsetMO = MI->getOperand(OpNo + 1);
102  if (OffsetMO.isReg())
103  OS << ", $" << LoongArchInstPrinter::getRegisterName(OffsetMO.getReg());
104  else if (OffsetMO.isImm())
105  OS << ", " << OffsetMO.getImm();
106  else
107  return true;
108  }
109  return false;
110 }
111 
114  return true;
115 }
116 
117 // Force static initialization.
121 }
AsmPrinter.h
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:109
llvm::MachineOperand::MO_Immediate
@ MO_Immediate
Immediate operand.
Definition: MachineOperand.h:52
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::LoongArchAsmPrinter::emitInstruction
void emitInstruction(const MachineInstr *MI) override
Targets should implement this to emit instructions.
Definition: LoongArchAsmPrinter.cpp:30
llvm::getTheLoongArch32Target
Target & getTheLoongArch32Target()
Definition: LoongArchTargetInfo.cpp:13
LoongArchAsmPrinter.h
llvm::AsmPrinter::PrintSymbolOperand
virtual void PrintSymbolOperand(const MachineOperand &MO, raw_ostream &OS)
Print the MachineOperand as a symbol.
Definition: AsmPrinterInlineAsm.cpp:457
llvm::MCInst
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
llvm::MachineOperand::MO_Register
@ MO_Register
Register operand.
Definition: MachineOperand.h:51
llvm::AsmPrinter::EmitToStreamer
void EmitToStreamer(MCStreamer &S, const MCInst &Inst)
Definition: AsmPrinter.cpp:403
llvm::LoongArchAsmPrinter::runOnMachineFunction
bool runOnMachineFunction(MachineFunction &MF) override
Emit the specified function out to the OutStreamer.
Definition: LoongArchAsmPrinter.cpp:112
llvm::LoongArchInstPrinter::getRegisterName
static const char * getRegisterName(unsigned RegNo)
Definition: LoongArchInstPrinter.cpp:60
llvm::MachineOperand::MO_GlobalAddress
@ MO_GlobalAddress
Address of a global value.
Definition: MachineOperand.h:61
llvm::MachineOperand::getImm
int64_t getImm() const
Definition: MachineOperand.h:546
llvm::AsmPrinter::OutStreamer
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
Definition: AsmPrinter.h:99
Y
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
llvm::MachineOperand
MachineOperand class - Representation of each machine instruction operand.
Definition: MachineOperand.h:48
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
LLVMInitializeLoongArchAsmPrinter
LLVM_EXTERNAL_VISIBILITY void LLVMInitializeLoongArchAsmPrinter()
Definition: LoongArchAsmPrinter.cpp:118
X
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
llvm::MachineOperand::isReg
bool isReg() const
isReg - Tests if this is a MO_Register operand.
Definition: MachineOperand.h:320
llvm::MachineInstr
Representation of each machine instruction.
Definition: MachineInstr.h:66
LLVM_EXTERNAL_VISIBILITY
#define LLVM_EXTERNAL_VISIBILITY
Definition: Compiler.h:127
llvm::MachineOperand::getType
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
Definition: MachineOperand.h:218
LoongArchInstPrinter.h
llvm::lowerLoongArchMachineInstrToMCInst
bool lowerLoongArchMachineInstrToMCInst(const MachineInstr *MI, MCInst &OutMI, AsmPrinter &AP)
Definition: LoongArchMCInstLower.cpp:132
llvm::AsmPrinter::MF
MachineFunction * MF
The current machine function.
Definition: AsmPrinter.h:102
llvm::MachineOperand::getReg
Register getReg() const
getReg - Returns the register number.
Definition: MachineOperand.h:359
llvm::LoongArchAsmPrinter::PrintAsmMemoryOperand
bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo, const char *ExtraCode, raw_ostream &OS) override
Print the specified operand of MI, an INLINEASM instruction, using the specified assembler variant as...
Definition: LoongArchAsmPrinter.cpp:85
llvm::MachineFunction
Definition: MachineFunction.h:257
llvm::AsmPrinter::runOnMachineFunction
bool runOnMachineFunction(MachineFunction &MF) override
Emit the specified function out to the OutStreamer.
Definition: AsmPrinter.h:387
LoongArch.h
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:143
llvm::LoongArchAsmPrinter::emitPseudoExpansionLowering
bool emitPseudoExpansionLowering(MCStreamer &OutStreamer, const MachineInstr *MI)
llvm::AsmPrinter::getSubtargetInfo
const MCSubtargetInfo & getSubtargetInfo() const
Return information about subtarget.
Definition: AsmPrinter.cpp:398
LoongArchTargetInfo.h
llvm::MachineOperand::isImm
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
Definition: MachineOperand.h:322
llvm::LoongArchAsmPrinter::PrintAsmOperand
bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, const char *ExtraCode, raw_ostream &OS) override
Print the specified operand of MI, an INLINEASM instruction, using the specified assembler variant.
Definition: LoongArchAsmPrinter.cpp:43
LoongArchTargetMachine.h
llvm::RegisterAsmPrinter
RegisterAsmPrinter - Helper template for registering a target specific assembly printer,...
Definition: TargetRegistry.h:1420
TargetRegistry.h
llvm::AsmPrinter::PrintAsmOperand
virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, const char *ExtraCode, raw_ostream &OS)
Print the specified operand of MI, an INLINEASM instruction, using the specified assembler variant.
Definition: AsmPrinterInlineAsm.cpp:467
llvm::getTheLoongArch64Target
Target & getTheLoongArch64Target()
Definition: LoongArchTargetInfo.cpp:18