LLVM  10.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 
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  const char *ExtraCode, raw_ostream &O) override;
71  bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNum,
72  const char *ExtraCode, raw_ostream &O) override;
73 
74  void emitArrayBound(MCSymbol *Sym, const GlobalVariable *GV);
75  void EmitGlobalVariable(const GlobalVariable *GV) override;
76 
77  void EmitFunctionEntryLabel() override;
78  void EmitInstruction(const MachineInstr *MI) override;
79  void EmitFunctionBodyStart() override;
80  void EmitFunctionBodyEnd() override;
81  };
82 } // end of anonymous namespace
83 
84 XCoreTargetStreamer &XCoreAsmPrinter::getTargetStreamer() {
85  return static_cast<XCoreTargetStreamer&>(*OutStreamer->getTargetStreamer());
86 }
87 
88 void XCoreAsmPrinter::emitArrayBound(MCSymbol *Sym, const GlobalVariable *GV) {
89  assert( ( GV->hasExternalLinkage() || GV->hasWeakLinkage() ||
90  GV->hasLinkOnceLinkage() || GV->hasCommonLinkage() ) &&
91  "Unexpected linkage");
92  if (ArrayType *ATy = dyn_cast<ArrayType>(GV->getValueType())) {
93 
94  MCSymbol *SymGlob = OutContext.getOrCreateSymbol(
95  Twine(Sym->getName() + StringRef(".globound")));
96  OutStreamer->EmitSymbolAttribute(SymGlob, MCSA_Global);
97  OutStreamer->EmitAssignment(SymGlob,
98  MCConstantExpr::create(ATy->getNumElements(),
99  OutContext));
100  if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
101  GV->hasCommonLinkage()) {
102  OutStreamer->EmitSymbolAttribute(SymGlob, MCSA_Weak);
103  }
104  }
105 }
106 
107 void XCoreAsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) {
108  // Check to see if this is a special global used by LLVM, if so, emit it.
109  if (!GV->hasInitializer() ||
110  EmitSpecialLLVMGlobal(GV))
111  return;
112 
113  const DataLayout &DL = getDataLayout();
114  OutStreamer->SwitchSection(getObjFileLowering().SectionForGlobal(GV, TM));
115 
116  MCSymbol *GVSym = getSymbol(GV);
117  const Constant *C = GV->getInitializer();
119 
120  // Mark the start of the global
121  getTargetStreamer().emitCCTopData(GVSym->getName());
122 
123  switch (GV->getLinkage()) {
125  report_fatal_error("AppendingLinkage is not supported by this target!");
132  emitArrayBound(GVSym, GV);
133  OutStreamer->EmitSymbolAttribute(GVSym, MCSA_Global);
134 
135  if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
136  GV->hasCommonLinkage())
137  OutStreamer->EmitSymbolAttribute(GVSym, MCSA_Weak);
141  break;
142  default:
143  llvm_unreachable("Unknown linkage type!");
144  }
145 
146  EmitAlignment(std::max(Align, llvm::Align(4)), GV);
147 
148  if (GV->isThreadLocal()) {
149  report_fatal_error("TLS is not supported by this target!");
150  }
151  unsigned Size = DL.getTypeAllocSize(C->getType());
152  if (MAI->hasDotTypeDotSizeDirective()) {
153  OutStreamer->EmitSymbolAttribute(GVSym, MCSA_ELF_TypeObject);
154  OutStreamer->emitELFSize(GVSym, MCConstantExpr::create(Size, OutContext));
155  }
156  OutStreamer->EmitLabel(GVSym);
157 
158  EmitGlobalConstant(DL, C);
159  // The ABI requires that unsigned scalar types smaller than 32 bits
160  // are padded to 32 bits.
161  if (Size < 4)
162  OutStreamer->EmitZeros(4 - Size);
163 
164  // Mark the end of the global
165  getTargetStreamer().emitCCBottomData(GVSym->getName());
166 }
167 
168 void XCoreAsmPrinter::EmitFunctionBodyStart() {
169  MCInstLowering.Initialize(&MF->getContext());
170 }
171 
172 /// EmitFunctionBodyEnd - Targets can override this to emit stuff after
173 /// the last basic block in the function.
174 void XCoreAsmPrinter::EmitFunctionBodyEnd() {
175  // Emit function end directives
176  getTargetStreamer().emitCCBottomFunction(CurrentFnSym->getName());
177 }
178 
179 void XCoreAsmPrinter::EmitFunctionEntryLabel() {
180  // Mark the start of the function
181  getTargetStreamer().emitCCTopFunction(CurrentFnSym->getName());
182  OutStreamer->EmitLabel(CurrentFnSym);
183 }
184 
185 void XCoreAsmPrinter::
186 printInlineJT(const MachineInstr *MI, int opNum, raw_ostream &O,
187  const std::string &directive) {
188  unsigned JTI = MI->getOperand(opNum).getIndex();
189  const MachineFunction *MF = MI->getParent()->getParent();
190  const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
191  const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
192  const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
193  O << "\t" << directive << " ";
194  for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
195  MachineBasicBlock *MBB = JTBBs[i];
196  if (i > 0)
197  O << ",";
198  MBB->getSymbol()->print(O, MAI);
199  }
200 }
201 
202 void XCoreAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
203  raw_ostream &O) {
204  const DataLayout &DL = getDataLayout();
205  const MachineOperand &MO = MI->getOperand(opNum);
206  switch (MO.getType()) {
209  break;
211  O << MO.getImm();
212  break;
214  MO.getMBB()->getSymbol()->print(O, MAI);
215  break;
217  PrintSymbolOperand(MO, O);
218  break;
220  O << DL.getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
221  << MO.getIndex();
222  break;
224  GetBlockAddressSymbol(MO.getBlockAddress())->print(O, MAI);
225  break;
226  default:
227  llvm_unreachable("not implemented");
228  }
229 }
230 
231 /// PrintAsmOperand - Print out an operand for an inline asm expression.
232 ///
233 bool XCoreAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
234  const char *ExtraCode, raw_ostream &O) {
235  // Print the operand if there is no operand modifier.
236  if (!ExtraCode || !ExtraCode[0]) {
237  printOperand(MI, OpNo, O);
238  return false;
239  }
240 
241  // Otherwise fallback on the default implementation.
242  return AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, O);
243 }
244 
245 bool XCoreAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
246  unsigned OpNum,
247  const char *ExtraCode,
248  raw_ostream &O) {
249  if (ExtraCode && ExtraCode[0]) {
250  return true; // Unknown modifier.
251  }
252  printOperand(MI, OpNum, O);
253  O << '[';
254  printOperand(MI, OpNum + 1, O);
255  O << ']';
256  return false;
257 }
258 
259 void XCoreAsmPrinter::EmitInstruction(const MachineInstr *MI) {
260  SmallString<128> Str;
261  raw_svector_ostream O(Str);
262 
263  switch (MI->getOpcode()) {
264  case XCore::DBG_VALUE:
265  llvm_unreachable("Should be handled target independently");
266  case XCore::ADD_2rus:
267  if (MI->getOperand(2).getImm() == 0) {
268  O << "\tmov "
271  OutStreamer->EmitRawText(O.str());
272  return;
273  }
274  break;
275  case XCore::BR_JT:
276  case XCore::BR_JT32:
277  O << "\tbru "
279  if (MI->getOpcode() == XCore::BR_JT)
280  printInlineJT(MI, 0, O);
281  else
282  printInlineJT32(MI, 0, O);
283  O << '\n';
284  OutStreamer->EmitRawText(O.str());
285  return;
286  }
287 
288  MCInst TmpInst;
289  MCInstLowering.Lower(MI, TmpInst);
290 
291  EmitToStreamer(*OutStreamer, TmpInst);
292 }
293 
294 // Force static initialization.
295 extern "C" void LLVMInitializeXCoreAsmPrinter() {
297 }
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:54
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
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:317
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.
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:530
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:411
bool hasCommonLinkage() const
Definition: GlobalValue.h:449
bool hasExternalLinkage() const
Definition: GlobalValue.h:431
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:245
Class to represent array types.
Definition: DerivedTypes.h:403
RegisterAsmPrinter - Helper template for registering a target specific assembly printer, for use in the target machine initialization function.
LinkageTypes getLinkage() const
Definition: GlobalValue.h:460
bool hasLinkOnceLinkage() const
Definition: GlobalValue.h:435
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:158
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false)
Definition: MCExpr.cpp:169
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...
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:78
unsigned getPrefTypeAlignment(Type *Ty) const
Returns the preferred stack/global alignment for the specified type.
Definition: DataLayout.cpp:770
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.
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:40
bool hasWeakLinkage() const
Definition: GlobalValue.h:439
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:555
int64_t getImm() const
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:256
uint64_t getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
Definition: DataLayout.h:470
BR_JT - Jumptable branch.
Definition: ISDOpcodes.h:666
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:279
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:204
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:265
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:250
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.
Register getReg() const
getReg - Returns the register number.
const MachineJumpTableInfo * getJumpTableInfo() const
getJumpTableInfo - Return the jump table info object for the current function.
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:416
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
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