LLVM  8.0.1
XCoreAsmPrinter.cpp
Go to the documentation of this file.
1 //===-- XCoreAsmPrinter.cpp - XCore LLVM assembly writer ------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains a printer that converts from our internal representation
11 // of machine-dependent LLVM code to the XAS-format XCore assembly language.
12 //
13 //===----------------------------------------------------------------------===//
14 
16 #include "XCore.h"
17 #include "XCoreInstrInfo.h"
18 #include "XCoreMCInstLower.h"
19 #include "XCoreSubtarget.h"
20 #include "XCoreTargetMachine.h"
21 #include "XCoreTargetStreamer.h"
22 #include "llvm/ADT/SmallString.h"
23 #include "llvm/ADT/StringExtras.h"
30 #include "llvm/IR/Constants.h"
31 #include "llvm/IR/DataLayout.h"
32 #include "llvm/IR/DebugInfo.h"
33 #include "llvm/IR/DerivedTypes.h"
34 #include "llvm/IR/Mangler.h"
35 #include "llvm/IR/Module.h"
36 #include "llvm/MC/MCAsmInfo.h"
37 #include "llvm/MC/MCExpr.h"
38 #include "llvm/MC/MCInst.h"
39 #include "llvm/MC/MCStreamer.h"
40 #include "llvm/MC/MCSymbolELF.h"
45 #include <algorithm>
46 #include <cctype>
47 using namespace llvm;
48 
49 #define DEBUG_TYPE "asm-printer"
50 
51 namespace {
52  class XCoreAsmPrinter : public AsmPrinter {
53  XCoreMCInstLower MCInstLowering;
54  XCoreTargetStreamer &getTargetStreamer();
55 
56  public:
57  explicit XCoreAsmPrinter(TargetMachine &TM,
58  std::unique_ptr<MCStreamer> Streamer)
59  : AsmPrinter(TM, std::move(Streamer)), MCInstLowering(*this) {}
60 
61  StringRef getPassName() const override { return "XCore Assembly Printer"; }
62 
63  void printInlineJT(const MachineInstr *MI, int opNum, raw_ostream &O,
64  const std::string &directive = ".jmptable");
65  void printInlineJT32(const MachineInstr *MI, int opNum, raw_ostream &O) {
66  printInlineJT(MI, opNum, O, ".jmptable32");
67  }
68  void printOperand(const MachineInstr *MI, int opNum, raw_ostream &O);
69  bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
70  unsigned AsmVariant, const char *ExtraCode,
71  raw_ostream &O) override;
72  bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNum,
73  unsigned AsmVariant, const char *ExtraCode,
74  raw_ostream &O) override;
75 
76  void emitArrayBound(MCSymbol *Sym, const GlobalVariable *GV);
77  void EmitGlobalVariable(const GlobalVariable *GV) override;
78 
79  void EmitFunctionEntryLabel() override;
80  void EmitInstruction(const MachineInstr *MI) override;
81  void EmitFunctionBodyStart() override;
82  void EmitFunctionBodyEnd() override;
83  };
84 } // end of anonymous namespace
85 
86 XCoreTargetStreamer &XCoreAsmPrinter::getTargetStreamer() {
87  return static_cast<XCoreTargetStreamer&>(*OutStreamer->getTargetStreamer());
88 }
89 
90 void XCoreAsmPrinter::emitArrayBound(MCSymbol *Sym, const GlobalVariable *GV) {
91  assert( ( GV->hasExternalLinkage() || GV->hasWeakLinkage() ||
92  GV->hasLinkOnceLinkage() || GV->hasCommonLinkage() ) &&
93  "Unexpected linkage");
94  if (ArrayType *ATy = dyn_cast<ArrayType>(GV->getValueType())) {
95 
96  MCSymbol *SymGlob = OutContext.getOrCreateSymbol(
97  Twine(Sym->getName() + StringRef(".globound")));
98  OutStreamer->EmitSymbolAttribute(SymGlob, MCSA_Global);
99  OutStreamer->EmitAssignment(SymGlob,
100  MCConstantExpr::create(ATy->getNumElements(),
101  OutContext));
102  if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
103  GV->hasCommonLinkage()) {
104  OutStreamer->EmitSymbolAttribute(SymGlob, MCSA_Weak);
105  }
106  }
107 }
108 
109 void XCoreAsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) {
110  // Check to see if this is a special global used by LLVM, if so, emit it.
111  if (!GV->hasInitializer() ||
112  EmitSpecialLLVMGlobal(GV))
113  return;
114 
115  const DataLayout &DL = getDataLayout();
116  OutStreamer->SwitchSection(getObjFileLowering().SectionForGlobal(GV, TM));
117 
118  MCSymbol *GVSym = getSymbol(GV);
119  const Constant *C = GV->getInitializer();
121 
122  // Mark the start of the global
123  getTargetStreamer().emitCCTopData(GVSym->getName());
124 
125  switch (GV->getLinkage()) {
127  report_fatal_error("AppendingLinkage is not supported by this target!");
134  emitArrayBound(GVSym, GV);
135  OutStreamer->EmitSymbolAttribute(GVSym, MCSA_Global);
136 
137  if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
138  GV->hasCommonLinkage())
139  OutStreamer->EmitSymbolAttribute(GVSym, MCSA_Weak);
143  break;
144  default:
145  llvm_unreachable("Unknown linkage type!");
146  }
147 
148  EmitAlignment(Align > 2 ? Align : 2, GV);
149 
150  if (GV->isThreadLocal()) {
151  report_fatal_error("TLS is not supported by this target!");
152  }
153  unsigned Size = DL.getTypeAllocSize(C->getType());
154  if (MAI->hasDotTypeDotSizeDirective()) {
155  OutStreamer->EmitSymbolAttribute(GVSym, MCSA_ELF_TypeObject);
156  OutStreamer->emitELFSize(GVSym, MCConstantExpr::create(Size, OutContext));
157  }
158  OutStreamer->EmitLabel(GVSym);
159 
160  EmitGlobalConstant(DL, C);
161  // The ABI requires that unsigned scalar types smaller than 32 bits
162  // are padded to 32 bits.
163  if (Size < 4)
164  OutStreamer->EmitZeros(4 - Size);
165 
166  // Mark the end of the global
167  getTargetStreamer().emitCCBottomData(GVSym->getName());
168 }
169 
170 void XCoreAsmPrinter::EmitFunctionBodyStart() {
171  MCInstLowering.Initialize(&MF->getContext());
172 }
173 
174 /// EmitFunctionBodyEnd - Targets can override this to emit stuff after
175 /// the last basic block in the function.
176 void XCoreAsmPrinter::EmitFunctionBodyEnd() {
177  // Emit function end directives
178  getTargetStreamer().emitCCBottomFunction(CurrentFnSym->getName());
179 }
180 
181 void XCoreAsmPrinter::EmitFunctionEntryLabel() {
182  // Mark the start of the function
183  getTargetStreamer().emitCCTopFunction(CurrentFnSym->getName());
184  OutStreamer->EmitLabel(CurrentFnSym);
185 }
186 
187 void XCoreAsmPrinter::
188 printInlineJT(const MachineInstr *MI, int opNum, raw_ostream &O,
189  const std::string &directive) {
190  unsigned JTI = MI->getOperand(opNum).getIndex();
191  const MachineFunction *MF = MI->getParent()->getParent();
192  const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
193  const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
194  const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
195  O << "\t" << directive << " ";
196  for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
197  MachineBasicBlock *MBB = JTBBs[i];
198  if (i > 0)
199  O << ",";
200  MBB->getSymbol()->print(O, MAI);
201  }
202 }
203 
204 void XCoreAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
205  raw_ostream &O) {
206  const DataLayout &DL = getDataLayout();
207  const MachineOperand &MO = MI->getOperand(opNum);
208  switch (MO.getType()) {
211  break;
213  O << MO.getImm();
214  break;
216  MO.getMBB()->getSymbol()->print(O, MAI);
217  break;
219  getSymbol(MO.getGlobal())->print(O, MAI);
220  break;
222  O << DL.getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
223  << MO.getIndex();
224  break;
226  GetBlockAddressSymbol(MO.getBlockAddress())->print(O, MAI);
227  break;
228  default:
229  llvm_unreachable("not implemented");
230  }
231 }
232 
233 /// PrintAsmOperand - Print out an operand for an inline asm expression.
234 ///
235 bool XCoreAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
236  unsigned AsmVariant,const char *ExtraCode,
237  raw_ostream &O) {
238  // Print the operand if there is no operand modifier.
239  if (!ExtraCode || !ExtraCode[0]) {
240  printOperand(MI, OpNo, O);
241  return false;
242  }
243 
244  // Otherwise fallback on the default implementation.
245  return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, O);
246 }
247 
248 bool XCoreAsmPrinter::
249 PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNum,
250  unsigned AsmVariant, const char *ExtraCode,
251  raw_ostream &O) {
252  if (ExtraCode && ExtraCode[0]) {
253  return true; // Unknown modifier.
254  }
255  printOperand(MI, OpNum, O);
256  O << '[';
257  printOperand(MI, OpNum + 1, O);
258  O << ']';
259  return false;
260 }
261 
262 void XCoreAsmPrinter::EmitInstruction(const MachineInstr *MI) {
263  SmallString<128> Str;
264  raw_svector_ostream O(Str);
265 
266  switch (MI->getOpcode()) {
267  case XCore::DBG_VALUE:
268  llvm_unreachable("Should be handled target independently");
269  case XCore::ADD_2rus:
270  if (MI->getOperand(2).getImm() == 0) {
271  O << "\tmov "
274  OutStreamer->EmitRawText(O.str());
275  return;
276  }
277  break;
278  case XCore::BR_JT:
279  case XCore::BR_JT32:
280  O << "\tbru "
282  if (MI->getOpcode() == XCore::BR_JT)
283  printInlineJT(MI, 0, O);
284  else
285  printInlineJT32(MI, 0, O);
286  O << '\n';
287  OutStreamer->EmitRawText(O.str());
288  return;
289  }
290 
291  MCInst TmpInst;
292  MCInstLowering.Lower(MI, TmpInst);
293 
294  EmitToStreamer(*OutStreamer, TmpInst);
295 }
296 
297 // Force static initialization.
298 extern "C" void LLVMInitializeXCoreAsmPrinter() {
300 }
uint64_t CallInst * C
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:111
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:55
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:140
This class represents lattice values for constants.
Definition: AllocatorList.h:24
.type _foo, STT_OBJECT # aka
Definition: MCDirectives.h:25
StringRef getPrivateGlobalPrefix() const
Definition: DataLayout.h:294
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
#define LLVM_FALLTHROUGH
Definition: Compiler.h:86
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:54
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:57
Externally visible function.
Definition: GlobalValue.h:49
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:510
MachineBasicBlock reference.
Tentative definitions.
Definition: GlobalValue.h:59
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:81
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:409
bool hasCommonLinkage() const
Definition: GlobalValue.h:440
bool hasExternalLinkage() const
Definition: GlobalValue.h:422
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:245
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:744
Class to represent array types.
Definition: DerivedTypes.h:369
RegisterAsmPrinter - Helper template for registering a target specific assembly printer, for use in the target machine initialization function.
LinkageTypes getLinkage() const
Definition: GlobalValue.h:451
bool hasLinkOnceLinkage() const
Definition: GlobalValue.h:426
Expected< const typename ELFT::Sym * > getSymbol(typename ELFT::SymRange Symbols, uint32_t Index)
Definition: ELF.h:337
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:161
Address of a global value.
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:52
This is an important base class in LLVM.
Definition: Constant.h:42
This file contains the declarations for the subclasses of Constant, which represent the different fla...
const GlobalValue * getGlobal() const
virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, unsigned AsmVariant, const char *ExtraCode, raw_ostream &OS)
Print the specified operand of MI, an INLINEASM instruction, using the specified assembler variant...
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:79
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:430
Target & getTheXCoreTarget()
MachineOperand class - Representation of each machine instruction operand.
Keep one copy of function when linking (inline)
Definition: GlobalValue.h:51
Module.h This file contains the declarations for the Module class.
StringRef str()
Return a StringRef for the vector contents.
Definition: raw_ostream.h:535
int64_t getImm() const
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:254
uint64_t getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
Definition: DataLayout.h:436
BR_JT - Jumptable branch.
Definition: ISDOpcodes.h:638
Representation of each machine instruction.
Definition: MachineInstr.h:64
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:276
uint32_t Size
Definition: Profile.cpp:47
Keep one copy of named function when linking (weak)
Definition: GlobalValue.h:53
Rename collisions when linking (static functions).
Definition: GlobalValue.h:56
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:203
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool hasInitializer() const
Definitions have initializers, declarations don&#39;t.
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:59
bool isThreadLocal() const
If the value is "Thread Local", its value isn&#39;t shared by the threads.
Definition: GlobalValue.h:247
IRTranslator LLVM IR MI
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
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:414
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx)
Definition: MCExpr.cpp:164
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:60