LLVM  9.0.0svn
SparcAsmPrinter.cpp
Go to the documentation of this file.
1 //===-- SparcAsmPrinter.cpp - Sparc 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 GAS-format SPARC assembly language.
11 //
12 //===----------------------------------------------------------------------===//
13 
17 #include "Sparc.h"
18 #include "SparcInstrInfo.h"
19 #include "SparcTargetMachine.h"
26 #include "llvm/IR/Mangler.h"
27 #include "llvm/MC/MCAsmInfo.h"
28 #include "llvm/MC/MCContext.h"
29 #include "llvm/MC/MCInst.h"
30 #include "llvm/MC/MCStreamer.h"
31 #include "llvm/MC/MCSymbol.h"
34 using namespace llvm;
35 
36 #define DEBUG_TYPE "asm-printer"
37 
38 namespace {
39  class SparcAsmPrinter : public AsmPrinter {
40  SparcTargetStreamer &getTargetStreamer() {
41  return static_cast<SparcTargetStreamer &>(
42  *OutStreamer->getTargetStreamer());
43  }
44  public:
45  explicit SparcAsmPrinter(TargetMachine &TM,
46  std::unique_ptr<MCStreamer> Streamer)
47  : AsmPrinter(TM, std::move(Streamer)) {}
48 
49  StringRef getPassName() const override { return "Sparc Assembly Printer"; }
50 
51  void printOperand(const MachineInstr *MI, int opNum, raw_ostream &OS);
52  void printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &OS,
53  const char *Modifier = nullptr);
54 
55  void EmitFunctionBodyStart() override;
56  void EmitInstruction(const MachineInstr *MI) override;
57 
58  static const char *getRegisterName(unsigned RegNo) {
60  }
61 
62  bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
63  const char *ExtraCode, raw_ostream &O) override;
64  bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
65  const char *ExtraCode, raw_ostream &O) override;
66 
67  void LowerGETPCXAndEmitMCInsts(const MachineInstr *MI,
68  const MCSubtargetInfo &STI);
69 
70  };
71 } // end of anonymous namespace
72 
74  MCSymbol *Sym, MCContext &OutContext) {
75  const MCSymbolRefExpr *MCSym = MCSymbolRefExpr::create(Sym,
76  OutContext);
77  const SparcMCExpr *expr = SparcMCExpr::create(Kind, MCSym, OutContext);
78  return MCOperand::createExpr(expr);
79 
80 }
82  MCContext &OutContext) {
83  return createSparcMCOperand(SparcMCExpr::VK_Sparc_None, Label, OutContext);
84 }
85 
87  MCSymbol *GOTLabel, MCSymbol *StartLabel,
88  MCSymbol *CurLabel,
89  MCContext &OutContext)
90 {
91  const MCSymbolRefExpr *GOT = MCSymbolRefExpr::create(GOTLabel, OutContext);
92  const MCSymbolRefExpr *Start = MCSymbolRefExpr::create(StartLabel,
93  OutContext);
94  const MCSymbolRefExpr *Cur = MCSymbolRefExpr::create(CurLabel,
95  OutContext);
96 
97  const MCBinaryExpr *Sub = MCBinaryExpr::createSub(Cur, Start, OutContext);
98  const MCBinaryExpr *Add = MCBinaryExpr::createAdd(GOT, Sub, OutContext);
99  const SparcMCExpr *expr = SparcMCExpr::create(Kind,
100  Add, OutContext);
101  return MCOperand::createExpr(expr);
102 }
103 
104 static void EmitCall(MCStreamer &OutStreamer,
105  MCOperand &Callee,
106  const MCSubtargetInfo &STI)
107 {
109  CallInst.setOpcode(SP::CALL);
110  CallInst.addOperand(Callee);
111  OutStreamer.EmitInstruction(CallInst, STI);
112 }
113 
114 static void EmitSETHI(MCStreamer &OutStreamer,
115  MCOperand &Imm, MCOperand &RD,
116  const MCSubtargetInfo &STI)
117 {
118  MCInst SETHIInst;
119  SETHIInst.setOpcode(SP::SETHIi);
120  SETHIInst.addOperand(RD);
121  SETHIInst.addOperand(Imm);
122  OutStreamer.EmitInstruction(SETHIInst, STI);
123 }
124 
125 static void EmitBinary(MCStreamer &OutStreamer, unsigned Opcode,
126  MCOperand &RS1, MCOperand &Src2, MCOperand &RD,
127  const MCSubtargetInfo &STI)
128 {
129  MCInst Inst;
130  Inst.setOpcode(Opcode);
131  Inst.addOperand(RD);
132  Inst.addOperand(RS1);
133  Inst.addOperand(Src2);
134  OutStreamer.EmitInstruction(Inst, STI);
135 }
136 
137 static void EmitOR(MCStreamer &OutStreamer,
138  MCOperand &RS1, MCOperand &Imm, MCOperand &RD,
139  const MCSubtargetInfo &STI) {
140  EmitBinary(OutStreamer, SP::ORri, RS1, Imm, RD, STI);
141 }
142 
143 static void EmitADD(MCStreamer &OutStreamer,
144  MCOperand &RS1, MCOperand &RS2, MCOperand &RD,
145  const MCSubtargetInfo &STI) {
146  EmitBinary(OutStreamer, SP::ADDrr, RS1, RS2, RD, STI);
147 }
148 
149 static void EmitSHL(MCStreamer &OutStreamer,
150  MCOperand &RS1, MCOperand &Imm, MCOperand &RD,
151  const MCSubtargetInfo &STI) {
152  EmitBinary(OutStreamer, SP::SLLri, RS1, Imm, RD, STI);
153 }
154 
155 
156 static void EmitHiLo(MCStreamer &OutStreamer, MCSymbol *GOTSym,
159  MCOperand &RD,
160  MCContext &OutContext,
161  const MCSubtargetInfo &STI) {
162 
163  MCOperand hi = createSparcMCOperand(HiKind, GOTSym, OutContext);
164  MCOperand lo = createSparcMCOperand(LoKind, GOTSym, OutContext);
165  EmitSETHI(OutStreamer, hi, RD, STI);
166  EmitOR(OutStreamer, RD, lo, RD, STI);
167 }
168 
169 void SparcAsmPrinter::LowerGETPCXAndEmitMCInsts(const MachineInstr *MI,
170  const MCSubtargetInfo &STI)
171 {
172  MCSymbol *GOTLabel =
173  OutContext.getOrCreateSymbol(Twine("_GLOBAL_OFFSET_TABLE_"));
174 
175  const MachineOperand &MO = MI->getOperand(0);
176  assert(MO.getReg() != SP::O7 &&
177  "%o7 is assigned as destination for getpcx!");
178 
179  MCOperand MCRegOP = MCOperand::createReg(MO.getReg());
180 
181 
182  if (!isPositionIndependent()) {
183  // Just load the address of GOT to MCRegOP.
184  switch(TM.getCodeModel()) {
185  default:
186  llvm_unreachable("Unsupported absolute code model");
187  case CodeModel::Small:
188  EmitHiLo(*OutStreamer, GOTLabel,
190  MCRegOP, OutContext, STI);
191  break;
192  case CodeModel::Medium: {
193  EmitHiLo(*OutStreamer, GOTLabel,
195  MCRegOP, OutContext, STI);
197  OutContext));
198  EmitSHL(*OutStreamer, MCRegOP, imm, MCRegOP, STI);
200  GOTLabel, OutContext);
201  EmitOR(*OutStreamer, MCRegOP, lo, MCRegOP, STI);
202  break;
203  }
204  case CodeModel::Large: {
205  EmitHiLo(*OutStreamer, GOTLabel,
207  MCRegOP, OutContext, STI);
209  OutContext));
210  EmitSHL(*OutStreamer, MCRegOP, imm, MCRegOP, STI);
211  // Use register %o7 to load the lower 32 bits.
212  MCOperand RegO7 = MCOperand::createReg(SP::O7);
213  EmitHiLo(*OutStreamer, GOTLabel,
215  RegO7, OutContext, STI);
216  EmitADD(*OutStreamer, MCRegOP, RegO7, MCRegOP, STI);
217  }
218  }
219  return;
220  }
221 
222  MCSymbol *StartLabel = OutContext.createTempSymbol();
223  MCSymbol *EndLabel = OutContext.createTempSymbol();
224  MCSymbol *SethiLabel = OutContext.createTempSymbol();
225 
226  MCOperand RegO7 = MCOperand::createReg(SP::O7);
227 
228  // <StartLabel>:
229  // call <EndLabel>
230  // <SethiLabel>:
231  // sethi %hi(_GLOBAL_OFFSET_TABLE_+(<SethiLabel>-<StartLabel>)), <MO>
232  // <EndLabel>:
233  // or <MO>, %lo(_GLOBAL_OFFSET_TABLE_+(<EndLabel>-<StartLabel>))), <MO>
234  // add <MO>, %o7, <MO>
235 
236  OutStreamer->EmitLabel(StartLabel);
237  MCOperand Callee = createPCXCallOP(EndLabel, OutContext);
238  EmitCall(*OutStreamer, Callee, STI);
239  OutStreamer->EmitLabel(SethiLabel);
241  GOTLabel, StartLabel, SethiLabel,
242  OutContext);
243  EmitSETHI(*OutStreamer, hiImm, MCRegOP, STI);
244  OutStreamer->EmitLabel(EndLabel);
246  GOTLabel, StartLabel, EndLabel,
247  OutContext);
248  EmitOR(*OutStreamer, MCRegOP, loImm, MCRegOP, STI);
249  EmitADD(*OutStreamer, MCRegOP, RegO7, MCRegOP, STI);
250 }
251 
252 void SparcAsmPrinter::EmitInstruction(const MachineInstr *MI)
253 {
254 
255  switch (MI->getOpcode()) {
256  default: break;
257  case TargetOpcode::DBG_VALUE:
258  // FIXME: Debug Value.
259  return;
260  case SP::GETPCX:
261  LowerGETPCXAndEmitMCInsts(MI, getSubtargetInfo());
262  return;
263  }
266  do {
267  MCInst TmpInst;
268  LowerSparcMachineInstrToMCInst(&*I, TmpInst, *this);
269  EmitToStreamer(*OutStreamer, TmpInst);
270  } while ((++I != E) && I->isInsideBundle()); // Delay slot check.
271 }
272 
273 void SparcAsmPrinter::EmitFunctionBodyStart() {
274  if (!MF->getSubtarget<SparcSubtarget>().is64Bit())
275  return;
276 
277  const MachineRegisterInfo &MRI = MF->getRegInfo();
278  const unsigned globalRegs[] = { SP::G2, SP::G3, SP::G6, SP::G7, 0 };
279  for (unsigned i = 0; globalRegs[i] != 0; ++i) {
280  unsigned reg = globalRegs[i];
281  if (MRI.use_empty(reg))
282  continue;
283 
284  if (reg == SP::G6 || reg == SP::G7)
285  getTargetStreamer().emitSparcRegisterIgnore(reg);
286  else
287  getTargetStreamer().emitSparcRegisterScratch(reg);
288  }
289 }
290 
291 void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
292  raw_ostream &O) {
293  const DataLayout &DL = getDataLayout();
294  const MachineOperand &MO = MI->getOperand (opNum);
296 
297 #ifndef NDEBUG
298  // Verify the target flags.
299  if (MO.isGlobal() || MO.isSymbol() || MO.isCPI()) {
300  if (MI->getOpcode() == SP::CALL)
302  "Cannot handle target flags on call address");
303  else if (MI->getOpcode() == SP::SETHIi || MI->getOpcode() == SP::SETHIXi)
306  || TF == SparcMCExpr::VK_Sparc_HH
312  "Invalid target flags for address operand on sethi");
313  else if (MI->getOpcode() == SP::TLS_CALL)
317  "Cannot handle target flags on tls call address");
318  else if (MI->getOpcode() == SP::TLS_ADDrr)
323  "Cannot handle target flags on add for TLS");
324  else if (MI->getOpcode() == SP::TLS_LDrr)
326  "Cannot handle target flags on ld for TLS");
327  else if (MI->getOpcode() == SP::TLS_LDXrr)
329  "Cannot handle target flags on ldx for TLS");
330  else if (MI->getOpcode() == SP::XORri || MI->getOpcode() == SP::XORXri)
333  "Cannot handle target flags on xor for TLS");
334  else
338  || TF == SparcMCExpr::VK_Sparc_HM
342  "Invalid target flags for small address operand");
343  }
344 #endif
345 
346 
347  bool CloseParen = SparcMCExpr::printVariantKind(O, TF);
348 
349  switch (MO.getType()) {
351  O << "%" << StringRef(getRegisterName(MO.getReg())).lower();
352  break;
353 
355  O << (int)MO.getImm();
356  break;
358  MO.getMBB()->getSymbol()->print(O, MAI);
359  return;
361  PrintSymbolOperand(MO, O);
362  break;
364  O << GetBlockAddressSymbol(MO.getBlockAddress())->getName();
365  break;
367  O << MO.getSymbolName();
368  break;
370  O << DL.getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
371  << MO.getIndex();
372  break;
374  MO.getMetadata()->printAsOperand(O, MMI->getModule());
375  break;
376  default:
377  llvm_unreachable("<unknown operand type>");
378  }
379  if (CloseParen) O << ")";
380 }
381 
382 void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
383  raw_ostream &O, const char *Modifier) {
384  printOperand(MI, opNum, O);
385 
386  // If this is an ADD operand, emit it like normal operands.
387  if (Modifier && !strcmp(Modifier, "arith")) {
388  O << ", ";
389  printOperand(MI, opNum+1, O);
390  return;
391  }
392 
393  if (MI->getOperand(opNum+1).isReg() &&
394  MI->getOperand(opNum+1).getReg() == SP::G0)
395  return; // don't print "+%g0"
396  if (MI->getOperand(opNum+1).isImm() &&
397  MI->getOperand(opNum+1).getImm() == 0)
398  return; // don't print "+0"
399 
400  O << "+";
401  printOperand(MI, opNum+1, O);
402 }
403 
404 /// PrintAsmOperand - Print out an operand for an inline asm expression.
405 ///
406 bool SparcAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
407  const char *ExtraCode,
408  raw_ostream &O) {
409  if (ExtraCode && ExtraCode[0]) {
410  if (ExtraCode[1] != 0) return true; // Unknown modifier.
411 
412  switch (ExtraCode[0]) {
413  default:
414  // See if this is a generic print operand
415  return AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, O);
416  case 'f':
417  case 'r':
418  break;
419  }
420  }
421 
422  printOperand(MI, OpNo, O);
423 
424  return false;
425 }
426 
427 bool SparcAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
428  unsigned OpNo,
429  const char *ExtraCode,
430  raw_ostream &O) {
431  if (ExtraCode && ExtraCode[0])
432  return true; // Unknown modifier
433 
434  O << '[';
435  printMemOperand(MI, OpNo, O);
436  O << ']';
437 
438  return false;
439 }
440 
441 // Force static initialization.
442 extern "C" void LLVMInitializeSparcAsmPrinter() {
446 }
unsigned getTargetFlags() const
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:110
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
static MCOperand createPCXRelExprOp(SparcMCExpr::VariantKind Kind, MCSymbol *GOTLabel, MCSymbol *StartLabel, MCSymbol *CurLabel, MCContext &OutContext)
instr_iterator instr_end()
MachineBasicBlock * getMBB() const
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:323
This class represents lattice values for constants.
Definition: AllocatorList.h:23
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
static MCOperand createExpr(const MCExpr *Val)
Definition: MCInst.h:136
unsigned getReg() const
getReg - Returns the register number.
static bool printVariantKind(raw_ostream &OS, VariantKind Kind)
Definition: SparcMCExpr.cpp:42
This class represents a function call, abstracting a target machine&#39;s calling convention.
MachineBasicBlock reference.
bool isCPI() const
isCPI - Tests if this is a MO_ConstantPoolIndex operand.
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
virtual void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI)
Emit the given Instruction into the current section.
Definition: MCStreamer.cpp:960
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
static MCOperand createReg(unsigned Reg)
Definition: MCInst.h:115
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
static const char * getRegisterName(unsigned RegNo)
static void EmitHiLo(MCStreamer &OutStreamer, MCSymbol *GOTSym, SparcMCExpr::VariantKind HiKind, SparcMCExpr::VariantKind LoKind, MCOperand &RD, MCContext &OutContext, const MCSubtargetInfo &STI)
Name of external global symbol.
static StringRef getName(Value *V)
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:165
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:410
const char * getSymbolName() const
zlib style complession
Context object for machine code objects.
Definition: MCContext.h:62
static std::string getRegisterName(const TargetRegisterInfo *TRI, unsigned Reg)
Definition: MIParser.cpp:1079
static const MCBinaryExpr * createSub(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:546
RegisterAsmPrinter - Helper template for registering a target specific assembly printer, for use in the target machine initialization function.
Target & getTheSparcTarget()
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:461
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:158
static void EmitSHL(MCStreamer &OutStreamer, MCOperand &RS1, MCOperand &Imm, MCOperand &RD, const MCSubtargetInfo &STI)
Address of a global value.
Streaming machine code generation interface.
Definition: MCStreamer.h:188
unsigned const MachineRegisterInfo * MRI
static void EmitBinary(MCStreamer &OutStreamer, unsigned Opcode, MCOperand &RS1, MCOperand &Src2, MCOperand &RD, const MCSubtargetInfo &STI)
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static void EmitCall(MCStreamer &OutStreamer, MCOperand &Callee, const MCSubtargetInfo &STI)
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:78
Target & getTheSparcelTarget()
static void EmitOR(MCStreamer &OutStreamer, MCOperand &RS1, MCOperand &Imm, MCOperand &RD, const MCSubtargetInfo &STI)
Address of a basic block.
self_iterator getIterator()
Definition: ilist_node.h:81
static void EmitADD(MCStreamer &OutStreamer, MCOperand &RS1, MCOperand &RS2, MCOperand &RD, const MCSubtargetInfo &STI)
Binary assembler expressions.
Definition: MCExpr.h:417
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void LLVMInitializeSparcAsmPrinter()
static void printMemOperand(raw_ostream &OS, const MachineMemOperand &MMO, const MachineFunction *MF, const Module *M, const MachineFrameInfo *MFI, const TargetInstrInfo *TII, LLVMContext &Ctx)
Iterator for intrusive lists based on ilist_node.
void setOpcode(unsigned Op)
Definition: MCInst.h:170
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
MachineOperand class - Representation of each machine instruction operand.
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...
static MCOperand createPCXCallOP(MCSymbol *Label, MCContext &OutContext)
int64_t getImm() const
amdgpu Simplify well known AMD library false FunctionCallee Callee
bool use_empty(unsigned RegNo) const
use_empty - Return true if there are no instructions using the specified register.
Target & getTheSparcV9Target()
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:255
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
Representation of each machine instruction.
Definition: MachineInstr.h:63
static bool printOperand(raw_ostream &OS, const SelectionDAG *G, const SDValue Value)
void LowerSparcMachineInstrToMCInst(const MachineInstr *MI, MCInst &OutMI, AsmPrinter &AP)
const BlockAddress * getBlockAddress() const
MCSymbol * getSymbol() const
Return the MCSymbol for this basic block.
#define I(x, y, z)
Definition: MD5.cpp:58
Generic base class for all target subtargets.
static const SparcMCExpr * create(VariantKind Kind, const MCExpr *Expr, MCContext &Ctx)
Definition: SparcMCExpr.cpp:26
bool isSymbol() const
isSymbol - Tests if this is a MO_ExternalSymbol operand.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
LLVM_NODISCARD std::string lower() const
Definition: StringRef.cpp:107
void printAsOperand(raw_ostream &OS, const Module *M=nullptr) const
Print as operand.
Definition: AsmWriter.cpp:4351
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
static void EmitSETHI(MCStreamer &OutStreamer, MCOperand &Imm, MCOperand &RD, const MCSubtargetInfo &STI)
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:65
IRTranslator LLVM IR MI
void addOperand(const MCOperand &Op)
Definition: MCInst.h:183
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
Address of indexed Constant in Constant Pool.
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:415
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:34
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx)
Definition: MCExpr.cpp:163
const MDNode * getMetadata() const
static MCOperand createSparcMCOperand(SparcMCExpr::VariantKind Kind, MCSymbol *Sym, MCContext &OutContext)
Metadata reference (for debug info)
void print(raw_ostream &OS, const MCAsmInfo *MAI) const
print - Print the value to the stream OS.
Definition: MCSymbol.cpp:59