Re-sort all of the includes with ./utils/sort_includes.py so that
[oota-llvm.git] / lib / Target / Sparc / SparcAsmPrinter.cpp
1 //===-- SparcAsmPrinter.cpp - Sparc 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 GAS-format SPARC assembly language.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #define DEBUG_TYPE "asm-printer"
16 #include "Sparc.h"
17 #include "InstPrinter/SparcInstPrinter.h"
18 #include "MCTargetDesc/SparcBaseInfo.h"
19 #include "MCTargetDesc/SparcMCExpr.h"
20 #include "SparcInstrInfo.h"
21 #include "SparcTargetMachine.h"
22 #include "SparcTargetStreamer.h"
23 #include "llvm/ADT/SmallString.h"
24 #include "llvm/CodeGen/AsmPrinter.h"
25 #include "llvm/CodeGen/MachineInstr.h"
26 #include "llvm/CodeGen/MachineRegisterInfo.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"
32 #include "llvm/Support/TargetRegistry.h"
33 #include "llvm/Support/raw_ostream.h"
34 #include "llvm/Target/Mangler.h"
35 using namespace llvm;
36
37 namespace {
38   class SparcAsmPrinter : public AsmPrinter {
39     SparcTargetStreamer &getTargetStreamer() {
40       return static_cast<SparcTargetStreamer&>(OutStreamer.getTargetStreamer());
41     }
42   public:
43     explicit SparcAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
44       : AsmPrinter(TM, Streamer) {}
45
46     virtual const char *getPassName() const {
47       return "Sparc Assembly Printer";
48     }
49
50     void printOperand(const MachineInstr *MI, int opNum, raw_ostream &OS);
51     void printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &OS,
52                          const char *Modifier = 0);
53     void printCCOperand(const MachineInstr *MI, int opNum, raw_ostream &OS);
54
55     virtual void EmitFunctionBodyStart();
56     virtual void EmitInstruction(const MachineInstr *MI);
57
58     static const char *getRegisterName(unsigned RegNo) {
59       return SparcInstPrinter::getRegisterName(RegNo);
60     }
61
62     bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
63                          unsigned AsmVariant, const char *ExtraCode,
64                          raw_ostream &O);
65     bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
66                                unsigned AsmVariant, const char *ExtraCode,
67                                raw_ostream &O);
68
69     virtual bool isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB)
70                        const;
71
72   };
73 } // end of anonymous namespace
74
75 static MCOperand createPCXCallOP(MCSymbol *Label,
76                                  MCContext &OutContext)
77 {
78   const MCSymbolRefExpr *MCSym = MCSymbolRefExpr::Create(Label,
79                                                          OutContext);
80   const SparcMCExpr *expr = SparcMCExpr::Create(SparcMCExpr::VK_Sparc_None,
81                                                 MCSym, OutContext);
82   return MCOperand::CreateExpr(expr);
83 }
84
85 static MCOperand createPCXRelExprOp(SparcMCExpr::VariantKind Kind,
86                                     MCSymbol *GOTLabel, MCSymbol *StartLabel,
87                                     MCSymbol *CurLabel,
88                                     MCContext &OutContext)
89 {
90   const MCSymbolRefExpr *GOT = MCSymbolRefExpr::Create(GOTLabel, OutContext);
91   const MCSymbolRefExpr *Start = MCSymbolRefExpr::Create(StartLabel,
92                                                          OutContext);
93   const MCSymbolRefExpr *Cur = MCSymbolRefExpr::Create(CurLabel,
94                                                        OutContext);
95
96   const MCBinaryExpr *Sub = MCBinaryExpr::CreateSub(Cur, Start, OutContext);
97   const MCBinaryExpr *Add = MCBinaryExpr::CreateAdd(GOT, Sub, OutContext);
98   const SparcMCExpr *expr = SparcMCExpr::Create(Kind,
99                                                 Add, OutContext);
100   return MCOperand::CreateExpr(expr);
101 }
102
103 static void EmitCall(MCStreamer &OutStreamer,
104                      MCOperand &Callee)
105 {
106   MCInst CallInst;
107   CallInst.setOpcode(SP::CALL);
108   CallInst.addOperand(Callee);
109   OutStreamer.EmitInstruction(CallInst);
110 }
111
112 static void EmitSETHI(MCStreamer &OutStreamer,
113                       MCOperand &Imm, MCOperand &RD)
114 {
115   MCInst SETHIInst;
116   SETHIInst.setOpcode(SP::SETHIi);
117   SETHIInst.addOperand(RD);
118   SETHIInst.addOperand(Imm);
119   OutStreamer.EmitInstruction(SETHIInst);
120 }
121
122 static void EmitOR(MCStreamer &OutStreamer, MCOperand &RS1,
123                    MCOperand &Imm, MCOperand &RD)
124 {
125   MCInst ORInst;
126   ORInst.setOpcode(SP::ORri);
127   ORInst.addOperand(RD);
128   ORInst.addOperand(RS1);
129   ORInst.addOperand(Imm);
130   OutStreamer.EmitInstruction(ORInst);
131 }
132
133 static void EmitADD(MCStreamer &OutStreamer,
134                     MCOperand &RS1, MCOperand &RS2, MCOperand &RD)
135 {
136   MCInst ADDInst;
137   ADDInst.setOpcode(SP::ADDrr);
138   ADDInst.addOperand(RD);
139   ADDInst.addOperand(RS1);
140   ADDInst.addOperand(RS2);
141   OutStreamer.EmitInstruction(ADDInst);
142 }
143
144 static void LowerGETPCXAndEmitMCInsts(const MachineInstr *MI,
145                                       MCStreamer &OutStreamer,
146                                       MCContext &OutContext)
147 {
148   const MachineOperand &MO = MI->getOperand(0);
149   MCSymbol *StartLabel = OutContext.CreateTempSymbol();
150   MCSymbol *EndLabel   = OutContext.CreateTempSymbol();
151   MCSymbol *SethiLabel = OutContext.CreateTempSymbol();
152   MCSymbol *GOTLabel   =
153     OutContext.GetOrCreateSymbol(Twine("_GLOBAL_OFFSET_TABLE_"));
154
155   assert(MO.getReg() != SP::O7 &&
156          "%o7 is assigned as destination for getpcx!");
157
158   MCOperand MCRegOP = MCOperand::CreateReg(MO.getReg());
159   MCOperand RegO7   = MCOperand::CreateReg(SP::O7);
160
161   // <StartLabel>:
162   //   call <EndLabel>
163   // <SethiLabel>:
164   //     sethi %hi(_GLOBAL_OFFSET_TABLE_+(<SethiLabel>-<StartLabel>)), <MO>
165   // <EndLabel>:
166   //   or  <MO>, %lo(_GLOBAL_OFFSET_TABLE_+(<EndLabel>-<StartLabel>))), <MO>
167   //   add <MO>, %o7, <MO>
168
169   OutStreamer.EmitLabel(StartLabel);
170   MCOperand Callee =  createPCXCallOP(EndLabel, OutContext);
171   EmitCall(OutStreamer, Callee);
172   OutStreamer.EmitLabel(SethiLabel);
173   MCOperand hiImm = createPCXRelExprOp(SparcMCExpr::VK_Sparc_HI,
174                                        GOTLabel, StartLabel, SethiLabel,
175                                        OutContext);
176   EmitSETHI(OutStreamer, hiImm, MCRegOP);
177   OutStreamer.EmitLabel(EndLabel);
178   MCOperand loImm = createPCXRelExprOp(SparcMCExpr::VK_Sparc_LO,
179                                        GOTLabel, StartLabel, EndLabel,
180                                        OutContext);
181   EmitOR(OutStreamer, MCRegOP, loImm, MCRegOP);
182   EmitADD(OutStreamer, MCRegOP, RegO7, MCRegOP);
183 }
184
185 void SparcAsmPrinter::EmitInstruction(const MachineInstr *MI)
186 {
187   MCInst TmpInst;
188
189   switch (MI->getOpcode()) {
190   default: break;
191   case TargetOpcode::DBG_VALUE:
192     // FIXME: Debug Value.
193     return;
194   case SP::GETPCX:
195     LowerGETPCXAndEmitMCInsts(MI, OutStreamer, OutContext);
196     return;
197   }
198   LowerSparcMachineInstrToMCInst(MI, TmpInst, *this);
199   OutStreamer.EmitInstruction(TmpInst);
200 }
201
202 void SparcAsmPrinter::EmitFunctionBodyStart() {
203   if (!TM.getSubtarget<SparcSubtarget>().is64Bit())
204     return;
205
206   const MachineRegisterInfo &MRI = MF->getRegInfo();
207   const unsigned globalRegs[] = { SP::G2, SP::G3, SP::G6, SP::G7, 0 };
208   for (unsigned i = 0; globalRegs[i] != 0; ++i) {
209     unsigned reg = globalRegs[i];
210     if (MRI.use_empty(reg))
211       continue;
212
213     if  (reg == SP::G6 || reg == SP::G7)
214       getTargetStreamer().emitSparcRegisterIgnore(reg);
215     else
216       getTargetStreamer().emitSparcRegisterScratch(reg);
217   }
218 }
219
220 void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
221                                    raw_ostream &O) {
222   const DataLayout *DL = TM.getDataLayout();
223   const MachineOperand &MO = MI->getOperand (opNum);
224   unsigned TF = MO.getTargetFlags();
225 #ifndef NDEBUG
226   // Verify the target flags.
227   if (MO.isGlobal() || MO.isSymbol() || MO.isCPI()) {
228     if (MI->getOpcode() == SP::CALL)
229       assert(TF == SPII::MO_NO_FLAG &&
230              "Cannot handle target flags on call address");
231     else if (MI->getOpcode() == SP::SETHIi || MI->getOpcode() == SP::SETHIXi)
232       assert((TF == SPII::MO_HI || TF == SPII::MO_H44 || TF == SPII::MO_HH
233               || TF == SPII::MO_TLS_GD_HI22
234               || TF == SPII::MO_TLS_LDM_HI22
235               || TF == SPII::MO_TLS_LDO_HIX22
236               || TF == SPII::MO_TLS_IE_HI22
237               || TF == SPII::MO_TLS_LE_HIX22) &&
238              "Invalid target flags for address operand on sethi");
239     else if (MI->getOpcode() == SP::TLS_CALL)
240       assert((TF == SPII::MO_NO_FLAG
241               || TF == SPII::MO_TLS_GD_CALL
242               || TF == SPII::MO_TLS_LDM_CALL) &&
243              "Cannot handle target flags on tls call address");
244     else if (MI->getOpcode() == SP::TLS_ADDrr)
245       assert((TF == SPII::MO_TLS_GD_ADD || TF == SPII::MO_TLS_LDM_ADD
246               || TF == SPII::MO_TLS_LDO_ADD || TF == SPII::MO_TLS_IE_ADD) &&
247              "Cannot handle target flags on add for TLS");
248     else if (MI->getOpcode() == SP::TLS_LDrr)
249       assert(TF == SPII::MO_TLS_IE_LD &&
250              "Cannot handle target flags on ld for TLS");
251     else if (MI->getOpcode() == SP::TLS_LDXrr)
252       assert(TF == SPII::MO_TLS_IE_LDX &&
253              "Cannot handle target flags on ldx for TLS");
254     else if (MI->getOpcode() == SP::XORri || MI->getOpcode() == SP::XORXri)
255       assert((TF == SPII::MO_TLS_LDO_LOX10 || TF == SPII::MO_TLS_LE_LOX10) &&
256              "Cannot handle target flags on xor for TLS");
257     else
258       assert((TF == SPII::MO_LO || TF == SPII::MO_M44 || TF == SPII::MO_L44
259               || TF == SPII::MO_HM
260               || TF == SPII::MO_TLS_GD_LO10
261               || TF == SPII::MO_TLS_LDM_LO10
262               || TF == SPII::MO_TLS_IE_LO10 ) &&
263              "Invalid target flags for small address operand");
264   }
265 #endif
266
267   bool CloseParen = true;
268   switch (TF) {
269   default:
270       llvm_unreachable("Unknown target flags on operand");
271   case SPII::MO_NO_FLAG:
272     CloseParen = false;
273     break;
274   case SPII::MO_LO:  O << "%lo(";  break;
275   case SPII::MO_HI:  O << "%hi(";  break;
276   case SPII::MO_H44: O << "%h44("; break;
277   case SPII::MO_M44: O << "%m44("; break;
278   case SPII::MO_L44: O << "%l44("; break;
279   case SPII::MO_HH:  O << "%hh(";  break;
280   case SPII::MO_HM:  O << "%hm(";  break;
281   case SPII::MO_TLS_GD_HI22:   O << "%tgd_hi22(";   break;
282   case SPII::MO_TLS_GD_LO10:   O << "%tgd_lo10(";   break;
283   case SPII::MO_TLS_GD_ADD:    O << "%tgd_add(";    break;
284   case SPII::MO_TLS_GD_CALL:   O << "%tgd_call(";   break;
285   case SPII::MO_TLS_LDM_HI22:  O << "%tldm_hi22(";  break;
286   case SPII::MO_TLS_LDM_LO10:  O << "%tldm_lo10(";  break;
287   case SPII::MO_TLS_LDM_ADD:   O << "%tldm_add(";   break;
288   case SPII::MO_TLS_LDM_CALL:  O << "%tldm_call(";  break;
289   case SPII::MO_TLS_LDO_HIX22: O << "%tldo_hix22("; break;
290   case SPII::MO_TLS_LDO_LOX10: O << "%tldo_lox10("; break;
291   case SPII::MO_TLS_LDO_ADD:   O << "%tldo_add(";   break;
292   case SPII::MO_TLS_IE_HI22:   O << "%tie_hi22(";   break;
293   case SPII::MO_TLS_IE_LO10:   O << "%tie_lo10(";   break;
294   case SPII::MO_TLS_IE_LD:     O << "%tie_ld(";     break;
295   case SPII::MO_TLS_IE_LDX:    O << "%tie_ldx(";    break;
296   case SPII::MO_TLS_IE_ADD:    O << "%tie_add(";    break;
297   case SPII::MO_TLS_LE_HIX22:  O << "%tle_hix22(";  break;
298   case SPII::MO_TLS_LE_LOX10:  O << "%tle_lox10(";   break;
299   }
300
301   switch (MO.getType()) {
302   case MachineOperand::MO_Register:
303     O << "%" << StringRef(getRegisterName(MO.getReg())).lower();
304     break;
305
306   case MachineOperand::MO_Immediate:
307     O << (int)MO.getImm();
308     break;
309   case MachineOperand::MO_MachineBasicBlock:
310     O << *MO.getMBB()->getSymbol();
311     return;
312   case MachineOperand::MO_GlobalAddress:
313     O << *getSymbol(MO.getGlobal());
314     break;
315   case MachineOperand::MO_BlockAddress:
316     O <<  GetBlockAddressSymbol(MO.getBlockAddress())->getName();
317     break;
318   case MachineOperand::MO_ExternalSymbol:
319     O << MO.getSymbolName();
320     break;
321   case MachineOperand::MO_ConstantPoolIndex:
322     O << DL->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
323       << MO.getIndex();
324     break;
325   default:
326     llvm_unreachable("<unknown operand type>");
327   }
328   if (CloseParen) O << ")";
329 }
330
331 void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
332                                       raw_ostream &O, const char *Modifier) {
333   printOperand(MI, opNum, O);
334
335   // If this is an ADD operand, emit it like normal operands.
336   if (Modifier && !strcmp(Modifier, "arith")) {
337     O << ", ";
338     printOperand(MI, opNum+1, O);
339     return;
340   }
341
342   if (MI->getOperand(opNum+1).isReg() &&
343       MI->getOperand(opNum+1).getReg() == SP::G0)
344     return;   // don't print "+%g0"
345   if (MI->getOperand(opNum+1).isImm() &&
346       MI->getOperand(opNum+1).getImm() == 0)
347     return;   // don't print "+0"
348
349   O << "+";
350   printOperand(MI, opNum+1, O);
351 }
352
353 /// PrintAsmOperand - Print out an operand for an inline asm expression.
354 ///
355 bool SparcAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
356                                       unsigned AsmVariant,
357                                       const char *ExtraCode,
358                                       raw_ostream &O) {
359   if (ExtraCode && ExtraCode[0]) {
360     if (ExtraCode[1] != 0) return true; // Unknown modifier.
361
362     switch (ExtraCode[0]) {
363     default:
364       // See if this is a generic print operand
365       return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, O);
366     case 'r':
367      break;
368     }
369   }
370
371   printOperand(MI, OpNo, O);
372
373   return false;
374 }
375
376 bool SparcAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
377                                             unsigned OpNo, unsigned AsmVariant,
378                                             const char *ExtraCode,
379                                             raw_ostream &O) {
380   if (ExtraCode && ExtraCode[0])
381     return true;  // Unknown modifier
382
383   O << '[';
384   printMemOperand(MI, OpNo, O);
385   O << ']';
386
387   return false;
388 }
389
390 /// isBlockOnlyReachableByFallthough - Return true if the basic block has
391 /// exactly one predecessor and the control transfer mechanism between
392 /// the predecessor and this block is a fall-through.
393 ///
394 /// This overrides AsmPrinter's implementation to handle delay slots.
395 bool SparcAsmPrinter::
396 isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const {
397   // If this is a landing pad, it isn't a fall through.  If it has no preds,
398   // then nothing falls through to it.
399   if (MBB->isLandingPad() || MBB->pred_empty())
400     return false;
401
402   // If there isn't exactly one predecessor, it can't be a fall through.
403   MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(), PI2 = PI;
404   ++PI2;
405   if (PI2 != MBB->pred_end())
406     return false;
407
408   // The predecessor has to be immediately before this block.
409   const MachineBasicBlock *Pred = *PI;
410
411   if (!Pred->isLayoutSuccessor(MBB))
412     return false;
413
414   // Check if the last terminator is an unconditional branch.
415   MachineBasicBlock::const_iterator I = Pred->end();
416   while (I != Pred->begin() && !(--I)->isTerminator())
417     ; // Noop
418   return I == Pred->end() || !I->isBarrier();
419 }
420
421 // Force static initialization.
422 extern "C" void LLVMInitializeSparcAsmPrinter() {
423   RegisterAsmPrinter<SparcAsmPrinter> X(TheSparcTarget);
424   RegisterAsmPrinter<SparcAsmPrinter> Y(TheSparcV9Target);
425 }