LLVM  14.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"
15 #include "CSKYConstantPoolValue.h"
16 #include "CSKYTargetMachine.h"
20 #include "llvm/ADT/Statistic.h"
23 #include "llvm/IR/DataLayout.h"
24 #include "llvm/MC/MCAsmInfo.h"
25 #include "llvm/MC/MCContext.h"
26 #include "llvm/MC/MCInstBuilder.h"
27 #include "llvm/MC/MCStreamer.h"
28 #include "llvm/MC/TargetRegistry.h"
29 
30 using namespace llvm;
31 
32 #define DEBUG_TYPE "csky-asm-printer"
33 
34 STATISTIC(CSKYNumInstrsCompressed,
35  "Number of C-SKY Compressed instructions emitted");
36 
38  std::unique_ptr<llvm::MCStreamer> Streamer)
39  : AsmPrinter(TM, std::move(Streamer)), MCInstLowering(OutContext, *this) {}
40 
42  MCP = MF.getConstantPool();
43  Subtarget = &MF.getSubtarget<CSKYSubtarget>();
45 }
46 
47 #define GEN_COMPRESS_INSTR
48 #include "CSKYGenCompressInstEmitter.inc"
50  MCInst CInst;
51  bool Res = compressInst(CInst, Inst, *Subtarget, OutStreamer->getContext());
52  if (Res)
53  ++CSKYNumInstrsCompressed;
54  AsmPrinter::EmitToStreamer(*OutStreamer, Res ? CInst : Inst);
55 }
56 
57 // Simple pseudo-instructions have their lowering (with expansion to real
58 // instructions) auto-generated.
59 #include "CSKYGenMCPseudoLowering.inc"
60 
61 void CSKYAsmPrinter::emitCustomConstantPool(const MachineInstr *MI) {
62 
63  // This instruction represents a floating constant pool in the function.
64  // The first operand is the ID# for this instruction, the second is the
65  // index into the MachineConstantPool that this is, the third is the size
66  // in bytes of this constant pool entry.
67  // The required alignment is specified on the basic block holding this MI.
68  unsigned LabelId = (unsigned)MI->getOperand(0).getImm();
69  unsigned CPIdx = (unsigned)MI->getOperand(1).getIndex();
70 
71  // If this is the first entry of the pool, mark it.
72  if (!InConstantPool) {
73  OutStreamer->emitValueToAlignment(4);
74  InConstantPool = true;
75  }
76 
77  OutStreamer->emitLabel(GetCPISymbol(LabelId));
78 
79  const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPIdx];
80  if (MCPE.isMachineConstantPoolEntry())
82  else
84  return;
85 }
86 
88  // Make sure to terminate any constant pools that were at the end
89  // of the function.
90  if (!InConstantPool)
91  return;
92  InConstantPool = false;
93 }
94 
96  // Do any auto-generated pseudo lowerings.
98  return;
99 
100  // If we just ended a constant pool, mark it as such.
101  if (InConstantPool && MI->getOpcode() != CSKY::CONSTPOOL_ENTRY) {
102  InConstantPool = false;
103  }
104 
105  if (MI->getOpcode() == CSKY::CONSTPOOL_ENTRY)
106  return emitCustomConstantPool(MI);
107 
108  MCInst TmpInst;
109  MCInstLowering.Lower(MI, TmpInst);
110  EmitToStreamer(*OutStreamer, TmpInst);
111 }
112 
113 // Convert a CSKY-specific constant pool modifier into the associated
114 // MCSymbolRefExpr variant kind.
117  switch (Modifier) {
118  case CSKYCP::NO_MOD:
120  case CSKYCP::ADDR:
122  case CSKYCP::GOT:
124  case CSKYCP::GOTOFF:
126  case CSKYCP::PLT:
128  case CSKYCP::TLSGD:
130  case CSKYCP::TLSLE:
132  case CSKYCP::TLSIE:
134  }
135  llvm_unreachable("Invalid CSKYCPModifier!");
136 }
137 
139  MachineConstantPoolValue *MCPV) {
140  int Size = getDataLayout().getTypeAllocSize(MCPV->getType());
141  CSKYConstantPoolValue *CCPV = static_cast<CSKYConstantPoolValue *>(MCPV);
142  MCSymbol *MCSym;
143 
144  if (CCPV->isBlockAddress()) {
145  const BlockAddress *BA =
146  cast<CSKYConstantPoolConstant>(CCPV)->getBlockAddress();
147  MCSym = GetBlockAddressSymbol(BA);
148  } else if (CCPV->isGlobalValue()) {
149  const GlobalValue *GV = cast<CSKYConstantPoolConstant>(CCPV)->getGV();
150  MCSym = getSymbol(GV);
151  } else if (CCPV->isMachineBasicBlock()) {
152  const MachineBasicBlock *MBB = cast<CSKYConstantPoolMBB>(CCPV)->getMBB();
153  MCSym = MBB->getSymbol();
154  } else if (CCPV->isJT()) {
155  signed JTI = cast<CSKYConstantPoolJT>(CCPV)->getJTI();
156  MCSym = GetJTISymbol(JTI);
157  } else {
158  assert(CCPV->isExtSymbol() && "unrecognized constant pool value");
159  StringRef Sym = cast<CSKYConstantPoolSymbol>(CCPV)->getSymbol();
160  MCSym = GetExternalSymbolSymbol(Sym);
161  }
162  // Create an MCSymbol for the reference.
163  const MCExpr *Expr =
165 
166  if (CCPV->getPCAdjustment()) {
167 
169  Twine(MAI->getPrivateGlobalPrefix()) + "PC" +
170  Twine(getFunctionNumber()) + "_" + Twine(CCPV->getLabelID()));
171 
172  const MCExpr *PCRelExpr = MCSymbolRefExpr::create(PCLabel, OutContext);
173  if (CCPV->mustAddCurrentAddress()) {
174  // We want "(<expr> - .)", but MC doesn't have a concept of the '.'
175  // label, so just emit a local label end reference that instead.
177  OutStreamer->emitLabel(DotSym);
178  const MCExpr *DotExpr = MCSymbolRefExpr::create(DotSym, OutContext);
179  PCRelExpr = MCBinaryExpr::createSub(PCRelExpr, DotExpr, OutContext);
180  }
181  Expr = MCBinaryExpr::createSub(Expr, PCRelExpr, OutContext);
182  }
183 
184  // Create an MCSymbol for the reference.
186  OutContext);
187 
188  OutStreamer->emitValue(Expr, Size);
189 }
190 
193 }
llvm::Check::Size
@ Size
Definition: FileCheck.h:73
llvm::CSKYMCExpr::VK_CSKY_GOTOFF
@ VK_CSKY_GOTOFF
Definition: CSKYMCExpr.h:28
AsmPrinter.h
llvm::CSKYConstantPoolValue::isMachineBasicBlock
bool isMachineBasicBlock() const
Definition: CSKYConstantPoolValue.h:68
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:105
llvm::CSKYCP::PLT
@ PLT
Definition: CSKYConstantPoolValue.h:39
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
llvm::CSKYCP::TLSGD
@ TLSGD
Definition: CSKYConstantPoolValue.h:39
llvm::MCSymbol
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
CSKYConstantPoolValue.h
llvm::CSKYAsmPrinter::emitFunctionBodyEnd
void emitFunctionBodyEnd() override
Targets can override this to emit stuff after the last basic block in the function.
Definition: CSKYAsmPrinter.cpp:87
llvm::MachineConstantPoolEntry::MachineCPVal
MachineConstantPoolValue * MachineCPVal
Definition: MachineConstantPool.h:72
llvm::AsmPrinter::MAI
const MCAsmInfo * MAI
Target Asm Printer information.
Definition: AsmPrinter.h:88
llvm::MCAsmInfo::getPrivateGlobalPrefix
StringRef getPrivateGlobalPrefix() const
Definition: MCAsmInfo.h:654
llvm::AsmPrinter::emitGlobalConstant
void emitGlobalConstant(const DataLayout &DL, const Constant *CV)
Print a general LLVM constant to the .s file.
Definition: AsmPrinter.cpp:3113
Statistic.h
llvm::CSKYAsmPrinter::emitPseudoExpansionLowering
bool emitPseudoExpansionLowering(MCStreamer &OutStreamer, const MachineInstr *MI)
tblgen'erated driver function for lowering simple MI->MC pseudo instructions.
llvm::CSKYConstantPoolValue::isExtSymbol
bool isExtSymbol() const
Definition: CSKYConstantPoolValue.h:66
CSKYTargetMachine.h
MCInstBuilder.h
llvm::MachineConstantPoolValue
Abstract base class for all machine specific constantpool value subclasses.
Definition: MachineConstantPool.h:35
CSKY.h
llvm::CSKYConstantPoolValue::isJT
bool isJT() const
Definition: CSKYConstantPoolValue.h:71
llvm::CSKYCP::GOT
@ GOT
Definition: CSKYConstantPoolValue.h:39
llvm::CSKYMCExpr::create
static const CSKYMCExpr * create(const MCExpr *Expr, VariantKind Kind, MCContext &Ctx)
Definition: CSKYMCExpr.cpp:20
llvm::CSKYMCExpr::VK_CSKY_GOT
@ VK_CSKY_GOT
Definition: CSKYMCExpr.h:25
llvm::MCInst
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
llvm::CSKYConstantPoolValue::isGlobalValue
bool isGlobalValue() const
Definition: CSKYConstantPoolValue.h:65
llvm::CSKYMCExpr::VK_CSKY_None
@ VK_CSKY_None
Definition: CSKYMCExpr.h:20
llvm::AsmPrinter::EmitToStreamer
void EmitToStreamer(MCStreamer &S, const MCInst &Inst)
Definition: AsmPrinter.cpp:242
llvm::MachineConstantPoolValue::getType
Type * getType() const
Definition: MachineConstantPool.h:44
llvm::getTheCSKYTarget
Target & getTheCSKYTarget()
Definition: CSKYTargetInfo.cpp:13
LLVMInitializeCSKYAsmPrinter
LLVM_EXTERNAL_VISIBILITY void LLVMInitializeCSKYAsmPrinter()
Definition: CSKYAsmPrinter.cpp:191
llvm::MachineBasicBlock::getSymbol
MCSymbol * getSymbol() const
Return the MCSymbol for this basic block.
Definition: MachineBasicBlock.cpp:60
llvm::MCStreamer
Streaming machine code generation interface.
Definition: MCStreamer.h:198
llvm::MCContext::getOrCreateSymbol
MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
Definition: MCContext.cpp:191
llvm::CSKYConstantPoolValue::isBlockAddress
bool isBlockAddress() const
Definition: CSKYConstantPoolValue.h:67
llvm::CSKYCP::NO_MOD
@ NO_MOD
Definition: CSKYConstantPoolValue.h:39
llvm::CSKYAsmPrinter::CSKYAsmPrinter
CSKYAsmPrinter(TargetMachine &TM, std::unique_ptr< MCStreamer > Streamer)
Definition: CSKYAsmPrinter.cpp:37
llvm::AsmPrinter::OutStreamer
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
Definition: AsmPrinter.h:97
llvm::CSKYMCExpr::VK_CSKY_ADDR
@ VK_CSKY_ADDR
Definition: CSKYMCExpr.h:21
MCContext.h
llvm::CSKYConstantPoolValue::mustAddCurrentAddress
bool mustAddCurrentAddress() const
Definition: CSKYConstantPoolValue.h:61
llvm::STATISTIC
STATISTIC(NumFunctions, "Total number of functions")
llvm::CSKYMCExpr::VK_CSKY_PLT
@ VK_CSKY_PLT
Definition: CSKYMCExpr.h:29
llvm::CSKYSubtarget
Definition: CSKYSubtarget.h:30
X
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
llvm::CSKYAsmPrinter::EmitToStreamer
void EmitToStreamer(MCStreamer &S, const MCInst &Inst)
Definition: CSKYAsmPrinter.cpp:49
llvm::MachineBasicBlock
Definition: MachineBasicBlock.h:95
llvm::MachineFunction::getSubtarget
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Definition: MachineFunction.h:641
llvm::CSKYMCInstLower::Lower
void Lower(const MachineInstr *MI, MCInst &OutMI) const
Definition: CSKYMCInstLower.cpp:27
llvm::GlobalValue
Definition: GlobalValue.h:44
llvm::CSKYConstantPoolValue::getLabelID
unsigned getLabelID() const
Definition: CSKYConstantPoolValue.h:63
llvm::AsmPrinter::GetCPISymbol
virtual MCSymbol * GetCPISymbol(unsigned CPID) const
Return the symbol for the specified constant pool entry.
Definition: AsmPrinter.cpp:3159
llvm::MachineInstr
Representation of each machine instruction.
Definition: MachineInstr.h:64
CSKYAsmPrinter.h
LLVM_EXTERNAL_VISIBILITY
#define LLVM_EXTERNAL_VISIBILITY
Definition: Compiler.h:136
llvm::AsmPrinter::GetBlockAddressSymbol
MCSymbol * GetBlockAddressSymbol(const BlockAddress *BA) const
Return the MCSymbol used to satisfy BlockAddress uses of the specified basic block.
Definition: AsmPrinter.cpp:3150
llvm::CSKYConstantPoolValue
CSKYConstantPoolValue - CSKY specific constantpool value.
Definition: CSKYConstantPoolValue.h:45
llvm::CSKYCP::GOTOFF
@ GOTOFF
Definition: CSKYConstantPoolValue.h:39
llvm::CSKYAsmPrinter::emitInstruction
void emitInstruction(const MachineInstr *MI) override
Targets should implement this to emit instructions.
Definition: CSKYAsmPrinter.cpp:95
llvm::CSKYCP::ADDR
@ ADDR
Definition: CSKYConstantPoolValue.h:39
MachineConstantPool.h
llvm::CSKYMCExpr::VariantKind
VariantKind
Definition: CSKYMCExpr.h:19
llvm::MachineFunction::getConstantPool
MachineConstantPool * getConstantPool()
getConstantPool - Return the constant pool object for the current function.
Definition: MachineFunction.h:673
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::MachineConstantPoolEntry::isMachineConstantPoolEntry
bool isMachineConstantPoolEntry() const
isMachineConstantPoolEntry - Return true if the MachineConstantPoolEntry is indeed a target specific ...
Definition: MachineConstantPool.h:93
llvm::AsmPrinter::getSymbol
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:489
llvm::TargetMachine
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:80
llvm::move
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:1707
llvm::CSKYConstantPoolValue::getModifier
CSKYCP::CSKYCPModifier getModifier() const
Definition: CSKYConstantPoolValue.h:62
llvm::MCBinaryExpr::createSub
static const MCBinaryExpr * createSub(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:610
llvm::AsmPrinter::MF
MachineFunction * MF
The current machine function.
Definition: AsmPrinter.h:100
llvm::AsmPrinter::OutContext
MCContext & OutContext
This is the context for the output file that we are streaming.
Definition: AsmPrinter.h:92
llvm::CSKYAsmPrinter::emitMachineConstantPoolValue
void emitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) override
Definition: CSKYAsmPrinter.cpp:138
llvm::CSKYMCExpr::VK_CSKY_TLSIE
@ VK_CSKY_TLSIE
Definition: CSKYMCExpr.h:31
llvm::MachineFunction
Definition: MachineFunction.h:241
llvm::AsmPrinter::runOnMachineFunction
bool runOnMachineFunction(MachineFunction &MF) override
Emit the specified function out to the OutStreamer.
Definition: AsmPrinter.h:344
llvm::BlockAddress
The address of a basic block.
Definition: Constants.h:848
llvm::CSKYCP::TLSLE
@ TLSLE
Definition: CSKYConstantPoolValue.h:39
llvm::AsmPrinter::GetExternalSymbolSymbol
MCSymbol * GetExternalSymbolSymbol(StringRef Sym) const
Return the MCSymbol for the specified ExternalSymbol.
Definition: AsmPrinter.cpp:3206
MCAsmInfo.h
DataLayout.h
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
this
Analysis the ScalarEvolution expression for r is this
Definition: README.txt:8
llvm::CSKYMCExpr::VK_CSKY_TLSLE
@ VK_CSKY_TLSLE
Definition: CSKYMCExpr.h:32
llvm::CSKYCP::CSKYCPModifier
CSKYCPModifier
Definition: CSKYConstantPoolValue.h:39
llvm::AsmPrinter::GetJTISymbol
MCSymbol * GetJTISymbol(unsigned JTID, bool isLinkerPrivate=false) const
Return the symbol for the specified jump table entry.
Definition: AsmPrinter.cpp:3187
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:134
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::MCContext::createTempSymbol
MCSymbol * createTempSymbol()
Create a temporary symbol with a unique name.
Definition: MCContext.cpp:303
CSKYInstPrinter.h
llvm::MachineConstantPoolEntry::Val
union llvm::MachineConstantPoolEntry::@183 Val
The constant itself.
MBB
MachineBasicBlock & MBB
Definition: AArch64SLSHardening.cpp:74
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:83
llvm::MachineConstantPoolEntry
This class is a data container for one entry in a MachineConstantPool.
Definition: MachineConstantPool.h:67
std
Definition: BitVector.h:838
llvm::CSKYMCExpr::VK_CSKY_TLSGD
@ VK_CSKY_TLSGD
Definition: CSKYMCExpr.h:33
llvm::MachineConstantPool::getConstants
const std::vector< MachineConstantPoolEntry > & getConstants() const
Definition: MachineConstantPool.h:145
llvm::CSKYAsmPrinter::runOnMachineFunction
bool runOnMachineFunction(MachineFunction &MF) override
Emit the specified function out to the OutStreamer.
Definition: CSKYAsmPrinter.cpp:41
llvm::CSKYConstantPoolValue::getPCAdjustment
unsigned getPCAdjustment() const
Definition: CSKYConstantPoolValue.h:60
llvm::AsmPrinter
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:82
llvm::MCSymbolRefExpr::create
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:386
getModifierVariantKind
static CSKYMCExpr::VariantKind getModifierVariantKind(CSKYCP::CSKYCPModifier Modifier)
Definition: CSKYAsmPrinter.cpp:116
CSKYMCExpr.h
MCStreamer.h
llvm::MachineFunction::getDataLayout
const DataLayout & getDataLayout() const
Return the DataLayout attached to the Module associated to this MF.
Definition: MachineFunction.cpp:266
CSKYTargetInfo.h
llvm::AsmPrinter::getDataLayout
const DataLayout & getDataLayout() const
Return information about data layout.
Definition: AsmPrinter.cpp:227
llvm::CSKYCP::TLSIE
@ TLSIE
Definition: CSKYConstantPoolValue.h:39
TM
const char LLVMTargetMachineRef TM
Definition: PassBuilderBindings.cpp:47
llvm::MCSymbolRefExpr::VK_None
@ VK_None
Definition: MCExpr.h:195
llvm::RegisterAsmPrinter
RegisterAsmPrinter - Helper template for registering a target specific assembly printer,...
Definition: TargetRegistry.h:1338
TargetRegistry.h
llvm::AsmPrinter::getFunctionNumber
unsigned getFunctionNumber() const
Return a unique ID for the current function.
Definition: AsmPrinter.cpp:219
llvm::MachineConstantPoolEntry::ConstVal
const Constant * ConstVal
Definition: MachineConstantPool.h:71
llvm::MCExpr
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
llvm::DataLayout::getTypeAllocSize
TypeSize getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
Definition: DataLayout.h:506