LLVM  9.0.0svn
XCoreAsmPrinter.cpp
Go to the documentation of this file.
1 //===-- XCoreAsmPrinter.cpp - XCore 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 XAS-format XCore assembly language.
11 //
12 //===----------------------------------------------------------------------===//
13 
15 #include "XCore.h"
16 #include "XCoreInstrInfo.h"
17 #include "XCoreMCInstLower.h"
18 #include "XCoreSubtarget.h"
19 #include "XCoreTargetMachine.h"
20 #include "XCoreTargetStreamer.h"
21 #include "llvm/ADT/SmallString.h"
22 #include "llvm/ADT/StringExtras.h"
29 #include "llvm/IR/Constants.h"
30 #include "llvm/IR/DataLayout.h"
31 #include "llvm/IR/DebugInfo.h"
32 #include "llvm/IR/DerivedTypes.h"
33 #include "llvm/IR/Mangler.h"
34 #include "llvm/IR/Module.h"
35 #include "llvm/MC/MCAsmInfo.h"
36 #include "llvm/MC/MCExpr.h"
37 #include "llvm/MC/MCInst.h"
38 #include "llvm/MC/MCStreamer.h"
39 #include "llvm/MC/MCSymbolELF.h"
44 #include <algorithm>
45 #include <cctype>
46 using namespace llvm;
47 
48 #define DEBUG_TYPE "asm-printer"
49 
50 namespace {
51  class XCoreAsmPrinter : public AsmPrinter {
52  XCoreMCInstLower MCInstLowering;
53  XCoreTargetStreamer &getTargetStreamer();
54 
55  public:
56  explicit XCoreAsmPrinter(TargetMachine &TM,
57  std::unique_ptr<MCStreamer> Streamer)
58  : AsmPrinter(TM, std::move(Streamer)), MCInstLowering(*this) {}
59 
60  StringRef getPassName() const override { return "XCore Assembly Printer"; }
61 
62  void printInlineJT(const MachineInstr *MI, int opNum, raw_ostream &O,
63  const std::string &directive = ".jmptable");
64  void printInlineJT32(const MachineInstr *MI, int opNum, raw_ostream &O) {
65  printInlineJT(MI, opNum, O, ".jmptable32");
66  }
67  void printOperand(const MachineInstr *MI, int opNum, raw_ostream &O);
68  bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
69  const char *ExtraCode, raw_ostream &O) override;
70  bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNum,
71  const char *ExtraCode, raw_ostream &O) override;
72 
73  void emitArrayBound(MCSymbol *Sym, const GlobalVariable *GV);
74  void EmitGlobalVariable(const GlobalVariable *GV) override;
75 
76  void EmitFunctionEntryLabel() override;
77  void EmitInstruction(const MachineInstr *MI) override;
78  void EmitFunctionBodyStart() override;
79  void EmitFunctionBodyEnd() override;
80  };
81 } // end of anonymous namespace
82 
83 XCoreTargetStreamer &XCoreAsmPrinter::getTargetStreamer() {
84  return static_cast<XCoreTargetStreamer&>(*OutStreamer->getTargetStreamer());
85 }
86 
87 void XCoreAsmPrinter::emitArrayBound(MCSymbol *Sym, const GlobalVariable *GV) {
88  assert( ( GV->hasExternalLinkage() || GV->hasWeakLinkage() ||
89  GV->hasLinkOnceLinkage() || GV->hasCommonLinkage() ) &&
90  "Unexpected linkage");
91  if (ArrayType *ATy = dyn_cast<ArrayType>(GV->getValueType())) {
92 
93  MCSymbol *SymGlob = OutContext.getOrCreateSymbol(
94  Twine(Sym->getName() + StringRef(".globound")));
95  OutStreamer->EmitSymbolAttribute(SymGlob, MCSA_Global);
96  OutStreamer->EmitAssignment(SymGlob,
97  MCConstantExpr::create(ATy->getNumElements(),
98  OutContext));
99  if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
100  GV->hasCommonLinkage()) {
101  OutStreamer->EmitSymbolAttribute(SymGlob, MCSA_Weak);
102  }
103  }
104 }
105 
106 void XCoreAsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) {
107  // Check to see if this is a special global used by LLVM, if so, emit it.
108  if (!GV->hasInitializer() ||
109  EmitSpecialLLVMGlobal(GV))
110  return;
111 
112  const DataLayout &DL = getDataLayout();
113  OutStreamer->SwitchSection(getObjFileLowering().SectionForGlobal(GV, TM));
114 
115  MCSymbol *GVSym = getSymbol(GV);
116  const Constant *C = GV->getInitializer();
118 
119  // Mark the start of the global
120  getTargetStreamer().emitCCTopData(GVSym->getName());
121 
122  switch (GV->getLinkage()) {
124  report_fatal_error("AppendingLinkage is not supported by this target!");
131  emitArrayBound(GVSym, GV);
132  OutStreamer->EmitSymbolAttribute(GVSym, MCSA_Global);
133 
134  if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
135  GV->hasCommonLinkage())
136  OutStreamer->EmitSymbolAttribute(GVSym, MCSA_Weak);
140  break;
141  default:
142  llvm_unreachable("Unknown linkage type!");
143  }
144 
145  EmitAlignment(Align > 2 ? Align : 2, GV);
146 
147  if (GV->isThreadLocal()) {
148  report_fatal_error("TLS is not supported by this target!");
149  }
150  unsigned Size = DL.getTypeAllocSize(C->getType());
151  if (MAI->hasDotTypeDotSizeDirective()) {
152  OutStreamer->EmitSymbolAttribute(GVSym, MCSA_ELF_TypeObject);
153  OutStreamer->emitELFSize(GVSym, MCConstantExpr::create(Size, OutContext));
154  }
155  OutStreamer->EmitLabel(GVSym);
156 
157  EmitGlobalConstant(DL, C);
158  // The ABI requires that unsigned scalar types smaller than 32 bits
159  // are padded to 32 bits.
160  if (Size < 4)
161  OutStreamer->EmitZeros(4 - Size);
162 
163  // Mark the end of the global
164  getTargetStreamer().emitCCBottomData(GVSym->getName());
165 }
166 
167 void XCoreAsmPrinter::EmitFunctionBodyStart() {
168  MCInstLowering.Initialize(&MF->getContext());
169 }
170 
171 /// EmitFunctionBodyEnd - Targets can override this to emit stuff after
172 /// the last basic block in the function.
173 void XCoreAsmPrinter::EmitFunctionBodyEnd() {
174  // Emit function end directives
175  getTargetStreamer().emitCCBottomFunction(CurrentFnSym->getName());
176 }
177 
178 void XCoreAsmPrinter::EmitFunctionEntryLabel() {
179  // Mark the start of the function
180  getTargetStreamer().emitCCTopFunction(CurrentFnSym->getName());
181  OutStreamer->EmitLabel(CurrentFnSym);
182 }
183 
184 void XCoreAsmPrinter::
185 printInlineJT(const MachineInstr *MI, int opNum, raw_ostream &O,
186  const std::string &directive) {
187  unsigned JTI = MI->getOperand(opNum).getIndex();
188  const MachineFunction *MF = MI->getParent()->getParent();
189  const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
190  const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
191  const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
192  O << "\t" << directive << " ";
193  for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
194  MachineBasicBlock *MBB = JTBBs[i];
195  if (i > 0)
196  O << ",";
197  MBB->getSymbol()->print(O, MAI);
198  }
199 }
200 
201 void XCoreAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
202  raw_ostream &O) {
203  const DataLayout &DL = getDataLayout();
204  const MachineOperand &MO = MI->getOperand(opNum);
205  switch (MO.getType()) {
208  break;
210  O << MO.getImm();
211  break;
213  MO.getMBB()->getSymbol()->print(O, MAI);
214  break;
216  getSymbol(MO.getGlobal())->print(O, MAI);
217  break;
219  O << DL.getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
220  << MO.getIndex();
221  break;
223  GetBlockAddressSymbol(MO.getBlockAddress())->print(O, MAI);
224  break;
225  default:
226  llvm_unreachable("not implemented");
227  }
228 }
229 
230 /// PrintAsmOperand - Print out an operand for an inline asm expression.
231 ///
232 bool XCoreAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
233  const char *ExtraCode, raw_ostream &O) {
234  // Print the operand if there is no operand modifier.
235  if (!ExtraCode || !ExtraCode[0]) {
236  printOperand(MI, OpNo, O);
237  return false;
238  }
239 
240  // Otherwise fallback on the default implementation.
241  return AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, O);
242 }
243 
244 bool XCoreAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
245  unsigned OpNum,
246  const char *ExtraCode,
247  raw_ostream &O) {
248  if (ExtraCode && ExtraCode[0]) {
249  return true; // Unknown modifier.
250  }
251  printOperand(MI, OpNum, O);
252  O << '[';
253  printOperand(MI, OpNum + 1, O);
254  O << ']';
255  return false;
256 }
257 
258 void XCoreAsmPrinter::EmitInstruction(const MachineInstr *MI) {
259  SmallString<128> Str;
260  raw_svector_ostream O(Str);
261 
262  switch (MI->getOpcode()) {
263  case XCore::DBG_VALUE:
264  llvm_unreachable("Should be handled target independently");
265  case XCore::ADD_2rus:
266  if (MI->getOperand(2).getImm() == 0) {
267  O << "\tmov "
270  OutStreamer->EmitRawText(O.str());
271  return;
272  }
273  break;
274  case XCore::BR_JT:
275  case XCore::BR_JT32:
276  O << "\tbru "
278  if (MI->getOpcode() == XCore::BR_JT)
279  printInlineJT(MI, 0, O);
280  else
281  printInlineJT32(MI, 0, O);
282  O << '\n';
283  OutStreamer->EmitRawText(O.str());
284  return;
285  }
286 
287  MCInst TmpInst;
288  MCInstLowering.Lower(MI, TmpInst);
289 
290  EmitToStreamer(*OutStreamer, TmpInst);
291 }
292 
293 // Force static initialization.
294 extern "C" void LLVMInitializeXCoreAsmPrinter() {
296 }
uint64_t CallInst * C
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:110
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
const std::vector< MachineJumpTableEntry > & getJumpTables() const
Special purpose, only applies to global arrays.
Definition: GlobalValue.h:54
MachineBasicBlock * getMBB() const
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:139
This class represents lattice values for constants.
Definition: AllocatorList.h:23
.type _foo, STT_OBJECT # aka
Definition: MCDirectives.h:25
StringRef getPrivateGlobalPrefix() const
Definition: DataLayout.h:316
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:53
This class is used to lower an MachineInstr into an MCInst.
unsigned getReg() const
getReg - Returns the register number.
Like Internal, but omit from symbol table.
Definition: GlobalValue.h:56
Externally visible function.
Definition: GlobalValue.h:48
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:509
MachineBasicBlock reference.
Tentative definitions.
Definition: GlobalValue.h:58
This file declares the MachineConstantPool class which is an abstract constant pool to keep track of ...
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:408
bool hasCommonLinkage() const
Definition: GlobalValue.h:439
bool hasExternalLinkage() const
Definition: GlobalValue.h:421
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:244
unsigned getPreferredTypeAlignmentShift(Type *Ty) const
Returns the preferred alignment for the specified type, returned as log2 of the value (a shift amount...
Definition: DataLayout.cpp:763
Class to represent array types.
Definition: DerivedTypes.h:400
RegisterAsmPrinter - Helper template for registering a target specific assembly printer, for use in the target machine initialization function.
LinkageTypes getLinkage() const
Definition: GlobalValue.h:450
bool hasLinkOnceLinkage() const
Definition: GlobalValue.h:425
Expected< const typename ELFT::Sym * > getSymbol(typename ELFT::SymRange Symbols, uint32_t Index)
Definition: ELF.h:334
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:158
Address of a global value.
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:51
This is an important base class in LLVM.
Definition: Constant.h:41
This file contains the declarations for the subclasses of Constant, which represent the different fla...
const GlobalValue * getGlobal() const
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:78
Address of a basic block.
void LLVMInitializeXCoreAsmPrinter()
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
bool hasWeakLinkage() const
Definition: GlobalValue.h:429
Target & getTheXCoreTarget()
MachineOperand class - Representation of each machine instruction operand.
Keep one copy of function when linking (inline)
Definition: GlobalValue.h:50
Module.h This file contains the declarations for the Module class.
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...
StringRef str()
Return a StringRef for the vector contents.
Definition: raw_ostream.h:534
int64_t getImm() const
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:253
uint64_t getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
Definition: DataLayout.h:461
BR_JT - Jumptable branch.
Definition: ISDOpcodes.h:639
Representation of each machine instruction.
Definition: MachineInstr.h:63
static bool printOperand(raw_ostream &OS, const SelectionDAG *G, const SDValue Value)
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
.type _foo,
Definition: MCDirectives.h:30
const BlockAddress * getBlockAddress() const
MCSymbol * getSymbol() const
Return the MCSymbol for this basic block.
Type * getValueType() const
Definition: GlobalValue.h:275
uint32_t Size
Definition: Profile.cpp:46
Keep one copy of named function when linking (weak)
Definition: GlobalValue.h:52
Rename collisions when linking (static functions).
Definition: GlobalValue.h:55
This file contains the declaration of the XCoreInstPrinter class, which is used to print XCore MCInst...
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:202
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool hasInitializer() const
Definitions have initializers, declarations don&#39;t.
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
Definition: Compiler.h:250
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:65
bool isThreadLocal() const
If the value is "Thread Local", its value isn&#39;t shared by the threads.
Definition: GlobalValue.h:246
IRTranslator LLVM IR MI
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
Address of indexed Constant in Constant Pool.
const MachineJumpTableInfo * getJumpTableInfo() const
getJumpTableInfo - Return the jump table info object for the current function.
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:413
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx)
Definition: MCExpr.cpp:163
static const char * getRegisterName(unsigned RegNo)
void print(raw_ostream &OS, const MCAsmInfo *MAI) const
print - Print the value to the stream OS.
Definition: MCSymbol.cpp:59