LLVM 20.0.0git
CSKYAsmPrinter.cpp
Go to the documentation of this file.
1//===-- CSKYAsmPrinter.cpp - CSKY LLVM assembly writer --------------------===//
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 the CSKY assembly language.
11//
12//===----------------------------------------------------------------------===//
13#include "CSKYAsmPrinter.h"
14#include "CSKY.h"
16#include "CSKYTargetMachine.h"
21#include "llvm/ADT/Statistic.h"
25#include "llvm/IR/DataLayout.h"
26#include "llvm/MC/MCAsmInfo.h"
27#include "llvm/MC/MCContext.h"
29#include "llvm/MC/MCStreamer.h"
31
32using namespace llvm;
33
34#define DEBUG_TYPE "csky-asm-printer"
35
36STATISTIC(CSKYNumInstrsCompressed,
37 "Number of C-SKY Compressed instructions emitted");
38
40 std::unique_ptr<llvm::MCStreamer> Streamer)
41 : AsmPrinter(TM, std::move(Streamer)), MCInstLowering(OutContext, *this) {}
42
44 MCP = MF.getConstantPool();
46
47 // Set the current MCSubtargetInfo to a copy which has the correct
48 // feature bits for the current MachineFunction
49 MCSubtargetInfo &NewSTI =
50 OutStreamer->getContext().getSubtargetCopy(*TM.getMCSubtargetInfo());
52 Subtarget = &NewSTI;
53
55}
56
57#define GEN_COMPRESS_INSTR
58#include "CSKYGenCompressInstEmitter.inc"
60 MCInst CInst;
61 bool Res = compressInst(CInst, Inst, *Subtarget);
62 if (Res)
63 ++CSKYNumInstrsCompressed;
64 AsmPrinter::EmitToStreamer(*OutStreamer, Res ? CInst : Inst);
65}
66
67// Simple pseudo-instructions have their lowering (with expansion to real
68// instructions) auto-generated.
69#include "CSKYGenMCPseudoLowering.inc"
70
71void CSKYAsmPrinter::expandTLSLA(const MachineInstr *MI) {
72 DebugLoc DL = MI->getDebugLoc();
73
76 "_" + Twine(MI->getOperand(3).getImm()));
77
78 OutStreamer->emitLabel(PCLabel);
79
80 auto Instr = BuildMI(*MF, DL, TII->get(CSKY::LRW32))
81 .add(MI->getOperand(0))
82 .add(MI->getOperand(2));
83 MCInst LRWInst;
84 MCInstLowering.Lower(Instr, LRWInst);
85 EmitToStreamer(*OutStreamer, LRWInst);
86
87 Instr = BuildMI(*MF, DL, TII->get(CSKY::GRS32))
88 .add(MI->getOperand(1))
89 .addSym(PCLabel);
90 MCInst GRSInst;
91 MCInstLowering.Lower(Instr, GRSInst);
92 EmitToStreamer(*OutStreamer, GRSInst);
93 return;
94}
95
96void CSKYAsmPrinter::emitCustomConstantPool(const MachineInstr *MI) {
97
98 // This instruction represents a floating constant pool in the function.
99 // The first operand is the ID# for this instruction, the second is the
100 // index into the MachineConstantPool that this is, the third is the size
101 // in bytes of this constant pool entry.
102 // The required alignment is specified on the basic block holding this MI.
103 unsigned LabelId = (unsigned)MI->getOperand(0).getImm();
104 unsigned CPIdx = (unsigned)MI->getOperand(1).getIndex();
105
106 // If this is the first entry of the pool, mark it.
107 if (!InConstantPool) {
108 OutStreamer->emitValueToAlignment(Align(4));
109 InConstantPool = true;
110 }
111
112 OutStreamer->emitLabel(GetCPISymbol(LabelId));
113
114 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPIdx];
117 else
119 return;
120}
121
123 // Make sure to terminate any constant pools that were at the end
124 // of the function.
125 if (!InConstantPool)
126 return;
127 InConstantPool = false;
128}
129
132 emitAttributes();
133}
134
136 CSKYTargetStreamer &CTS =
137 static_cast<CSKYTargetStreamer &>(*OutStreamer->getTargetStreamer());
138
141}
142
144 CSKY_MC::verifyInstructionPredicates(MI->getOpcode(),
145 getSubtargetInfo().getFeatureBits());
146
147 // Do any auto-generated pseudo lowerings.
148 if (MCInst OutInst; lowerPseudoInstExpansion(MI, OutInst)) {
149 EmitToStreamer(*OutStreamer, OutInst);
150 return;
151 }
152
153 // If we just ended a constant pool, mark it as such.
154 if (InConstantPool && MI->getOpcode() != CSKY::CONSTPOOL_ENTRY) {
155 InConstantPool = false;
156 }
157
158 if (MI->getOpcode() == CSKY::PseudoTLSLA32)
159 return expandTLSLA(MI);
160
161 if (MI->getOpcode() == CSKY::CONSTPOOL_ENTRY)
162 return emitCustomConstantPool(MI);
163
164 MCInst TmpInst;
165 MCInstLowering.Lower(MI, TmpInst);
166 EmitToStreamer(*OutStreamer, TmpInst);
167}
168
169// Convert a CSKY-specific constant pool modifier into the associated
170// MCSymbolRefExpr variant kind.
173 switch (Modifier) {
174 case CSKYCP::NO_MOD:
176 case CSKYCP::ADDR:
178 case CSKYCP::GOT:
180 case CSKYCP::GOTOFF:
182 case CSKYCP::PLT:
184 case CSKYCP::TLSGD:
186 case CSKYCP::TLSLE:
188 case CSKYCP::TLSIE:
190 }
191 llvm_unreachable("Invalid CSKYCPModifier!");
192}
193
197 CSKYConstantPoolValue *CCPV = static_cast<CSKYConstantPoolValue *>(MCPV);
198 MCSymbol *MCSym;
199
200 if (CCPV->isBlockAddress()) {
201 const BlockAddress *BA =
202 cast<CSKYConstantPoolConstant>(CCPV)->getBlockAddress();
203 MCSym = GetBlockAddressSymbol(BA);
204 } else if (CCPV->isGlobalValue()) {
205 const GlobalValue *GV = cast<CSKYConstantPoolConstant>(CCPV)->getGV();
206 MCSym = getSymbol(GV);
207 } else if (CCPV->isMachineBasicBlock()) {
208 const MachineBasicBlock *MBB = cast<CSKYConstantPoolMBB>(CCPV)->getMBB();
209 MCSym = MBB->getSymbol();
210 } else if (CCPV->isJT()) {
211 signed JTI = cast<CSKYConstantPoolJT>(CCPV)->getJTI();
212 MCSym = GetJTISymbol(JTI);
213 } else if (CCPV->isConstPool()) {
214 const Constant *C = cast<CSKYConstantPoolConstant>(CCPV)->getConstantPool();
215 MCSym = GetCPISymbol(MCP->getConstantPoolIndex(C, Align(4)));
216 } else {
217 assert(CCPV->isExtSymbol() && "unrecognized constant pool value");
218 StringRef Sym = cast<CSKYConstantPoolSymbol>(CCPV)->getSymbol();
220 }
221 // Create an MCSymbol for the reference.
222 const MCExpr *Expr =
224
225 if (CCPV->getPCAdjustment()) {
226
228 Twine(MAI->getPrivateGlobalPrefix()) + "PC" +
229 Twine(getFunctionNumber()) + "_" + Twine(CCPV->getLabelID()));
230
231 const MCExpr *PCRelExpr = MCSymbolRefExpr::create(PCLabel, OutContext);
232 if (CCPV->mustAddCurrentAddress()) {
233 // We want "(<expr> - .)", but MC doesn't have a concept of the '.'
234 // label, so just emit a local label end reference that instead.
236 OutStreamer->emitLabel(DotSym);
237 const MCExpr *DotExpr = MCSymbolRefExpr::create(DotSym, OutContext);
238 PCRelExpr = MCBinaryExpr::createSub(PCRelExpr, DotExpr, OutContext);
239 }
240 Expr = MCBinaryExpr::createSub(Expr, PCRelExpr, OutContext);
241 }
242
243 // Create an MCSymbol for the reference.
245 OutContext);
246
247 OutStreamer->emitValue(Expr, Size);
248}
249
250void CSKYAsmPrinter::emitAttributes() {
251 CSKYTargetStreamer &CTS =
252 static_cast<CSKYTargetStreamer &>(*OutStreamer->getTargetStreamer());
253
254 const Triple &TT = TM.getTargetTriple();
255 StringRef CPU = TM.getTargetCPU();
257 const CSKYTargetMachine &CTM = static_cast<const CSKYTargetMachine &>(TM);
258 /* TuneCPU doesn't impact emission of ELF attributes, ELF attributes only
259 care about arch related features, so we can set TuneCPU as CPU. */
260 const CSKYSubtarget STI(TT, CPU, /*TuneCPU=*/CPU, FS, CTM);
261
262 CTS.emitTargetAttributes(STI);
263}
264
266 const char *ExtraCode, raw_ostream &OS) {
267 // First try the generic code, which knows about modifiers like 'c' and 'n'.
268 if (!AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, OS))
269 return false;
270
271 const MachineOperand &MO = MI->getOperand(OpNo);
272 if (ExtraCode && ExtraCode[0]) {
273 if (ExtraCode[1] != 0)
274 return true; // Unknown modifier.
275
276 switch (ExtraCode[0]) {
277 default:
278 return true; // Unknown modifier.
279 case 'R':
282 return false;
283 }
284 }
285 }
286
287 switch (MO.getType()) {
289 OS << MO.getImm();
290 return false;
292 if (MO.getReg() == CSKY::C)
293 return false;
295 return false;
298 return false;
301 Sym->print(OS, MAI);
302 return false;
303 }
304 default:
305 break;
306 }
307
308 return true;
309}
310
312 unsigned OpNo, const char *ExtraCode,
313 raw_ostream &OS) {
314 if (!ExtraCode) {
315 const MachineOperand &MO = MI->getOperand(OpNo);
316 // For now, we only support register memory operands in registers and
317 // assume there is no addend
318 if (!MO.isReg())
319 return true;
320
321 OS << "(" << CSKYInstPrinter::getRegisterName(MO.getReg()) << ", 0)";
322 return false;
323 }
324
325 return AsmPrinter::PrintAsmMemoryOperand(MI, OpNo, ExtraCode, OS);
326}
327
330}
static MCSymbolRefExpr::VariantKind getModifierVariantKind(ARMCP::ARMCPModifier Modifier)
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
LLVM_EXTERNAL_VISIBILITY void LLVMInitializeCSKYAsmPrinter()
#define LLVM_EXTERNAL_VISIBILITY
Definition: Compiler.h:131
uint64_t Size
Symbol * Sym
Definition: ELF_riscv.cpp:479
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
IRTranslator LLVM IR MI
This file declares the MachineConstantPool class which is an abstract constant pool to keep track of ...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition: Statistic.h:166
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:86
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:676
void EmitToStreamer(MCStreamer &S, const MCInst &Inst)
Definition: AsmPrinter.cpp:403
TargetMachine & TM
Target machine description.
Definition: AsmPrinter.h:89
virtual MCSymbol * GetCPISymbol(unsigned CPID) const
Return the symbol for the specified constant pool entry.
virtual void PrintSymbolOperand(const MachineOperand &MO, raw_ostream &OS)
Print the MachineOperand as a symbol.
const MCAsmInfo * MAI
Target Asm Printer information.
Definition: AsmPrinter.h:92
MachineFunction * MF
The current machine function.
Definition: AsmPrinter.h:104
MCSymbol * GetJTISymbol(unsigned JTID, bool isLinkerPrivate=false) const
Return the symbol for the specified jump table entry.
unsigned getFunctionNumber() const
Return a unique ID for the current function.
Definition: AsmPrinter.cpp:379
void emitGlobalConstant(const DataLayout &DL, const Constant *CV, AliasMapTy *AliasList=nullptr)
EmitGlobalConstant - Print a general LLVM constant to the .s file.
MCContext & OutContext
This is the context for the output file that we are streaming.
Definition: AsmPrinter.h:96
MCSymbol * GetExternalSymbolSymbol(Twine Sym) const
Return the MCSymbol for the specified ExternalSymbol.
bool runOnMachineFunction(MachineFunction &MF) override
Emit the specified function out to the OutStreamer.
Definition: AsmPrinter.h:387
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
Definition: AsmPrinter.h:101
virtual bool PrintAsmMemoryOperand(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 as...
MCSymbol * GetBlockAddressSymbol(const BlockAddress *BA) const
Return the MCSymbol used to satisfy BlockAddress uses of the specified basic block.
const DataLayout & getDataLayout() const
Return information about data layout.
Definition: AsmPrinter.cpp:387
const MCSubtargetInfo & getSubtargetInfo() const
Return information about subtarget.
Definition: AsmPrinter.cpp:398
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.
The address of a basic block.
Definition: Constants.h:890
void emitFunctionBodyEnd() override
Targets can override this to emit stuff after the last basic block in the function.
void EmitToStreamer(MCStreamer &S, const MCInst &Inst)
CSKYAsmPrinter(TargetMachine &TM, std::unique_ptr< MCStreamer > Streamer)
bool lowerPseudoInstExpansion(const MachineInstr *MI, MCInst &Inst)
tblgen'erated driver function for lowering simple MI->MC pseudo instructions.
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.
void emitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) override
bool runOnMachineFunction(MachineFunction &MF) override
Emit the specified function out to the OutStreamer.
void emitEndOfAsmFile(Module &M) override
This virtual method can be overridden by targets that want to emit something at the end of their file...
void emitInstruction(const MachineInstr *MI) override
Targets should implement this to emit instructions.
void emitStartOfAsmFile(Module &M) override
This virtual method can be overridden by targets that want to emit something at the start of their fi...
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...
CSKYConstantPoolValue - CSKY specific constantpool value.
CSKYCP::CSKYCPModifier getModifier() const
static const char * getRegisterName(MCRegister Reg)
static const CSKYMCExpr * create(const MCExpr *Expr, VariantKind Kind, MCContext &Ctx)
Definition: CSKYMCExpr.cpp:22
void Lower(const MachineInstr *MI, MCInst &OutMI) const
virtual void emitTargetAttributes(const MCSubtargetInfo &STI)
This is an important base class in LLVM.
Definition: Constant.h:42
TypeSize getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
Definition: DataLayout.h:461
A debug info location.
Definition: DebugLoc.h:33
StringRef getPrivateGlobalPrefix() const
Definition: MCAsmInfo.h:628
static const MCBinaryExpr * createSub(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:617
MCSymbol * createTempSymbol()
Create a temporary symbol with a unique name.
Definition: MCContext.cpp:346
MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
Definition: MCContext.cpp:213
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
const MCInstrDesc & get(unsigned Opcode) const
Return the machine instruction descriptor that corresponds to the specified instruction opcode.
Definition: MCInstrInfo.h:63
Streaming machine code generation interface.
Definition: MCStreamer.h:213
Generic base class for all target subtargets.
void setFeatureBits(const FeatureBitset &FeatureBits_)
const FeatureBitset & getFeatureBits() const
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:393
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
MCSymbol * getSymbol() const
Return the MCSymbol for this basic block.
This class is a data container for one entry in a MachineConstantPool.
bool isMachineConstantPoolEntry() const
isMachineConstantPoolEntry - Return true if the MachineConstantPoolEntry is indeed a target specific ...
union llvm::MachineConstantPoolEntry::@204 Val
The constant itself.
MachineConstantPoolValue * MachineCPVal
Abstract base class for all machine specific constantpool value subclasses.
const std::vector< MachineConstantPoolEntry > & getConstants() const
unsigned getConstantPoolIndex(const Constant *C, Align Alignment)
getConstantPoolIndex - Create a new entry in the constant pool or return an existing one.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
const DataLayout & getDataLayout() const
Return the DataLayout attached to the Module associated to this MF.
MachineConstantPool * getConstantPool()
getConstantPool - Return the constant pool object for the current function.
const MachineInstrBuilder & add(const MachineOperand &MO) const
const MachineInstrBuilder & addSym(MCSymbol *Sym, unsigned char TargetFlags=0) const
Representation of each machine instruction.
Definition: MachineInstr.h:69
MachineOperand class - Representation of each machine instruction operand.
int64_t getImm() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
const BlockAddress * getBlockAddress() const
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_BlockAddress
Address of a basic block.
@ MO_Register
Register operand.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:77
const Triple & getTargetTriple() const
const MCSubtargetInfo * getMCSubtargetInfo() const
StringRef getTargetFeatureString() const
StringRef getTargetCPU() const
virtual const TargetInstrInfo * getInstrInfo() const
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
bool isOSBinFormatELF() const
Tests whether the OS uses the ELF binary format.
Definition: Triple.h:719
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
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.
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
Target & getTheCSKYTarget()
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1856
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
RegisterAsmPrinter - Helper template for registering a target specific assembly printer,...