LLVM 17.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"
21
22using 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 switch (MI->getOpcode()) {
39 case TargetOpcode::PATCHABLE_FUNCTION_ENTER:
41 return;
42 }
43
44 MCInst TmpInst;
45 if (!lowerLoongArchMachineInstrToMCInst(MI, TmpInst, *this))
46 EmitToStreamer(*OutStreamer, TmpInst);
47}
48
50 const char *ExtraCode,
51 raw_ostream &OS) {
52 // First try the generic code, which knows about modifiers like 'c' and 'n'.
53 if (!AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, OS))
54 return false;
55
56 const MachineOperand &MO = MI->getOperand(OpNo);
57 if (ExtraCode && ExtraCode[0]) {
58 if (ExtraCode[1] != 0)
59 return true; // Unknown modifier.
60
61 switch (ExtraCode[0]) {
62 default:
63 return true; // Unknown modifier.
64 case 'z': // Print $zero register if zero, regular printing otherwise.
65 if (MO.isImm() && MO.getImm() == 0) {
66 OS << '$' << LoongArchInstPrinter::getRegisterName(LoongArch::R0);
67 return false;
68 }
69 break;
70 // TODO: handle other extra codes if any.
71 }
72 }
73
74 switch (MO.getType()) {
76 OS << MO.getImm();
77 return false;
80 return false;
83 return false;
84 default:
85 llvm_unreachable("not implemented");
86 }
87
88 return true;
89}
90
92 unsigned OpNo,
93 const char *ExtraCode,
94 raw_ostream &OS) {
95 // TODO: handle extra code.
96 if (ExtraCode)
97 return true;
98
99 // We only support memory operands like "Base + Offset", where base must be a
100 // register, and offset can be a register or an immediate value.
101 const MachineOperand &BaseMO = MI->getOperand(OpNo);
102 // Base address must be a register.
103 if (!BaseMO.isReg())
104 return true;
105 // Print the base address register.
107 // Print the offset operand.
108 const MachineOperand &OffsetMO = MI->getOperand(OpNo + 1);
109 if (OffsetMO.isReg())
110 OS << ", $" << LoongArchInstPrinter::getRegisterName(OffsetMO.getReg());
111 else if (OffsetMO.isImm())
112 OS << ", " << OffsetMO.getImm();
113 else
114 return true;
115
116 return false;
117}
118
120 const MachineInstr &MI) {
121 const Function &F = MF->getFunction();
122 if (F.hasFnAttribute("patchable-function-entry")) {
123 unsigned Num;
124 if (F.getFnAttribute("patchable-function-entry")
125 .getValueAsString()
126 .getAsInteger(10, Num))
127 return;
128 emitNops(Num);
129 return;
130 }
131
132 // TODO: Emit sled here once we get support for XRay.
133}
134
137 return true;
138}
139
140// Force static initialization.
144}
#define LLVM_EXTERNAL_VISIBILITY
Definition: Compiler.h:127
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
IRTranslator LLVM IR MI
LLVM_EXTERNAL_VISIBILITY void LLVMInitializeLoongArchAsmPrinter()
#define F(x, y, z)
Definition: MD5.cpp:55
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
raw_pwrite_stream & OS
void emitNops(unsigned N)
Emit N NOP instructions.
void EmitToStreamer(MCStreamer &S, const MCInst &Inst)
Definition: AsmPrinter.cpp:398
virtual void PrintSymbolOperand(const MachineOperand &MO, raw_ostream &OS)
Print the MachineOperand as a symbol.
MachineFunction * MF
The current machine function.
Definition: AsmPrinter.h:102
bool runOnMachineFunction(MachineFunction &MF) override
Emit the specified function out to the OutStreamer.
Definition: AsmPrinter.h:399
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
Definition: AsmPrinter.h:99
const MCSubtargetInfo & getSubtargetInfo() const
Return information about subtarget.
Definition: AsmPrinter.cpp:393
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.
bool emitPseudoExpansionLowering(MCStreamer &OutStreamer, const MachineInstr *MI)
bool runOnMachineFunction(MachineFunction &MF) override
Emit the specified function out to the OutStreamer.
void LowerPATCHABLE_FUNCTION_ENTER(const MachineInstr &MI)
void emitInstruction(const MachineInstr *MI) override
Targets should implement this to emit instructions.
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...
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.
static const char * getRegisterName(MCRegister Reg)
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
Function & getFunction()
Return the LLVM function that this machine code represents.
Representation of each machine instruction.
Definition: MachineInstr.h:68
MachineOperand class - Representation of each machine instruction operand.
int64_t getImm() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
Register getReg() const
getReg - Returns the register number.
@ MO_Immediate
Immediate operand.
@ MO_GlobalAddress
Address of a global value.
@ MO_Register
Register operand.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
Target & getTheLoongArch64Target()
bool lowerLoongArchMachineInstrToMCInst(const MachineInstr *MI, MCInst &OutMI, AsmPrinter &AP)
Target & getTheLoongArch32Target()
RegisterAsmPrinter - Helper template for registering a target specific assembly printer,...