Convert assert(0) to llvm_unreachable
[oota-llvm.git] / lib / Target / Hexagon / HexagonAsmPrinter.cpp
1 //===-- HexagonAsmPrinter.cpp - Print machine instrs to Hexagon assembly ----=//
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 Hexagon assembly language. This printer is
12 // the output mechanism used by `llc'.
13 //
14 // Documentation at http://developer.apple.com/documentation/DeveloperTools/
15 // Reference/Assembler/ASMIntroduction/chapter_1_section_1.html
16 //
17 //===----------------------------------------------------------------------===//
18
19
20 #define DEBUG_TYPE "asm-printer"
21 #include "Hexagon.h"
22 #include "HexagonTargetMachine.h"
23 #include "HexagonSubtarget.h"
24 #include "HexagonMachineFunctionInfo.h"
25 #include "llvm/Constants.h"
26 #include "llvm/DerivedTypes.h"
27 #include "llvm/Module.h"
28 #include "llvm/Assembly/Writer.h"
29 #include "llvm/CodeGen/AsmPrinter.h"
30 #include "llvm/CodeGen/MachineModuleInfo.h"
31 #include "llvm/CodeGen/MachineFunctionPass.h"
32 #include "llvm/CodeGen/MachineInstr.h"
33 #include "llvm/CodeGen/MachineInstrBuilder.h"
34 #include "llvm/MC/MCStreamer.h"
35 #include "llvm/MC/MCAsmInfo.h"
36 #include "llvm/MC/MCSymbol.h"
37 #include "llvm/Support/MathExtras.h"
38 #include "llvm/Support/MathExtras.h"
39 #include "llvm/Support/CommandLine.h"
40 #include "llvm/Support/Debug.h"
41 #include "llvm/Support/Compiler.h"
42 #include "llvm/Support/raw_ostream.h"
43 #include "llvm/Target/Mangler.h"
44 #include "llvm/Target/TargetData.h"
45 #include "llvm/Target/TargetLoweringObjectFile.h"
46 #include "llvm/Target/TargetRegisterInfo.h"
47 #include "llvm/Target/TargetInstrInfo.h"
48 #include "llvm/Target/TargetOptions.h"
49 #include "llvm/ADT/SmallPtrSet.h"
50 #include "llvm/ADT/SmallString.h"
51 #include "llvm/ADT/StringExtras.h"
52 #include "llvm/Support/TargetRegistry.h"
53 #include "llvm/Support/raw_ostream.h"
54
55 using namespace llvm;
56
57 static cl::opt<bool> AlignCalls(
58          "hexagon-align-calls", cl::Hidden, cl::init(true),
59           cl::desc("Insert falign after call instruction for Hexagon target"));
60
61
62 namespace {
63   class HexagonAsmPrinter : public AsmPrinter {
64     const HexagonSubtarget *Subtarget;
65
66   public:
67     explicit HexagonAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
68       : AsmPrinter(TM, Streamer) {
69       Subtarget = &TM.getSubtarget<HexagonSubtarget>();
70     }
71
72     virtual const char *getPassName() const {
73       return "Hexagon Assembly Printer";
74     }
75
76     /// printInstruction - This method is automatically generated by tablegen
77     /// from the instruction set description.  This method returns true if the
78     /// machine instruction was sufficiently described to print it, otherwise it
79     void printInstruction(const MachineInstr *MI, raw_ostream &O);
80     virtual void EmitInstruction(const MachineInstr *MI);
81
82     void printOp(const MachineOperand &MO, raw_ostream &O);
83
84     /// printRegister - Print register according to target requirements.
85     ///
86     void printRegister(const MachineOperand &MO, bool R0AsZero,
87                        raw_ostream &O) {
88       unsigned RegNo = MO.getReg();
89       assert(TargetRegisterInfo::isPhysicalRegister(RegNo) && "Not physreg??");
90       O << getRegisterName(RegNo);
91     }
92
93     void printOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &OS) {
94       const MachineOperand &MO = MI->getOperand(OpNo);
95       if (MO.isReg()) {
96         printRegister(MO, false, OS);
97       } else if (MO.isImm()) {
98         OS << MO.getImm();
99       } else {
100         printOp(MO, OS);
101       }
102     }
103
104
105     bool isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const;
106
107     bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
108                          unsigned AsmVariant, const char *ExtraCode,
109                          raw_ostream &OS);
110     bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
111                                unsigned AsmVariant, const char *ExtraCode,
112                                raw_ostream &OS);
113
114
115     void printHexagonImmOperand(const MachineInstr *MI, unsigned OpNo,
116                                 raw_ostream &O) {
117       int value = MI->getOperand(OpNo).getImm();
118       O << value;
119     }
120
121
122     void printHexagonNegImmOperand(const MachineInstr *MI, unsigned OpNo,
123                                    raw_ostream &O) {
124       int value = MI->getOperand(OpNo).getImm();
125       O << -value;
126     }
127
128     void printHexagonMEMriOperand(const MachineInstr *MI, unsigned OpNo,
129                                   raw_ostream &O) {
130       const MachineOperand &MO1 = MI->getOperand(OpNo);
131       const MachineOperand &MO2 = MI->getOperand(OpNo+1);
132
133       O << getRegisterName(MO1.getReg())
134         << " + #"
135         << (int) MO2.getImm();
136     }
137
138
139     void printHexagonFrameIndexOperand(const MachineInstr *MI, unsigned OpNo,
140                                        raw_ostream &O) {
141       const MachineOperand &MO1 = MI->getOperand(OpNo);
142       const MachineOperand &MO2 = MI->getOperand(OpNo+1);
143
144       O << getRegisterName(MO1.getReg())
145         << ", #"
146         << MO2.getImm();
147     }
148
149     void printBranchOperand(const MachineInstr *MI, unsigned OpNo,
150                             raw_ostream &O) {
151       // Branches can take an immediate operand.  This is used by the branch
152       // selection pass to print $+8, an eight byte displacement from the PC.
153       if (MI->getOperand(OpNo).isImm()) {
154         O << "$+" << MI->getOperand(OpNo).getImm()*4;
155       } else {
156         printOp(MI->getOperand(OpNo), O);
157       }
158     }
159
160     void printCallOperand(const MachineInstr *MI, unsigned OpNo,
161                           raw_ostream &O) {
162     }
163
164     void printAbsAddrOperand(const MachineInstr *MI, unsigned OpNo,
165                             raw_ostream &O) {
166     }
167
168
169     void printSymbolHi(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
170       O << "#HI(";
171       if (MI->getOperand(OpNo).isImm()) {
172         printHexagonImmOperand(MI, OpNo, O);
173       } else {
174         printOp(MI->getOperand(OpNo), O);
175       }
176       O << ")";
177     }
178
179     void printSymbolLo(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
180       O << "#HI(";
181       if (MI->getOperand(OpNo).isImm()) {
182         printHexagonImmOperand(MI, OpNo, O);
183       } else {
184         printOp(MI->getOperand(OpNo), O);
185       }
186       O << ")";
187     }
188
189     void printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
190                                raw_ostream &O);
191
192     void printAddrModeBasePlusOffset(const MachineInstr *MI, int OpNo,
193                                      raw_ostream &O);
194
195     void printGlobalOperand(const MachineInstr *MI, int OpNo, raw_ostream &O);
196     void printJumpTable(const MachineInstr *MI, int OpNo, raw_ostream &O);
197
198     void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0) const;
199
200     static const char *getRegisterName(unsigned RegNo);
201   };
202
203 } // end of anonymous namespace
204
205 // Include the auto-generated portion of the assembly writer.
206 #include "HexagonGenAsmWriter.inc"
207
208
209 void HexagonAsmPrinter::EmitAlignment(unsigned NumBits,
210                                       const GlobalValue *GV) const {
211
212   // For basic block level alignment, use falign.
213   if (!GV) {
214     OutStreamer.EmitRawText(StringRef("\t.falign"));
215     return;
216   }
217
218   AsmPrinter::EmitAlignment(NumBits, GV);
219 }
220
221 void HexagonAsmPrinter::printOp(const MachineOperand &MO, raw_ostream &O) {
222   switch (MO.getType()) {
223   case MachineOperand::MO_Immediate:
224     dbgs() << "printOp() does not handle immediate values\n";
225     abort();
226
227   case MachineOperand::MO_MachineBasicBlock:
228     O << *MO.getMBB()->getSymbol();
229     return;
230   case MachineOperand::MO_JumpTableIndex:
231     O << *GetJTISymbol(MO.getIndex());
232     // FIXME: PIC relocation model.
233     return;
234   case MachineOperand::MO_ConstantPoolIndex:
235     O << *GetCPISymbol(MO.getIndex());
236     return;
237   case MachineOperand::MO_ExternalSymbol:
238     O << *GetExternalSymbolSymbol(MO.getSymbolName());
239     return;
240   case MachineOperand::MO_GlobalAddress: {
241     // Computing the address of a global symbol, not calling it.
242     O << *Mang->getSymbol(MO.getGlobal());
243     printOffset(MO.getOffset(), O);
244     return;
245   }
246
247   default:
248     O << "<unknown operand type: " << MO.getType() << ">";
249     return;
250   }
251 }
252
253
254 //
255 // isBlockOnlyReachableByFallthrough - We need to override this since the
256 // default AsmPrinter does not print labels for any basic block that
257 // is only reachable by a fall through. That works for all cases except
258 // for the case in which the basic block is reachable by a fall through but
259 // through an indirect from a jump table. In this case, the jump table
260 // will contain a label not defined by AsmPrinter.
261 //
262 bool HexagonAsmPrinter::
263 isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const {
264   if (MBB->hasAddressTaken()) {
265     return false;
266   }
267   return AsmPrinter::isBlockOnlyReachableByFallthrough(MBB);
268 }
269
270
271 /// PrintAsmOperand - Print out an operand for an inline asm expression.
272 ///
273 bool HexagonAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
274                                         unsigned AsmVariant,
275                                         const char *ExtraCode,
276                                       raw_ostream &OS) {
277   // Does this asm operand have a single letter operand modifier?
278   if (ExtraCode && ExtraCode[0]) {
279     if (ExtraCode[1] != 0) return true; // Unknown modifier.
280
281     switch (ExtraCode[0]) {
282     default: return true;  // Unknown modifier.
283     case 'c': // Don't print "$" before a global var name or constant.
284       // Hexagon never has a prefix.
285       printOperand(MI, OpNo, OS);
286       return false;
287     case 'L': // Write second word of DImode reference.
288       // Verify that this operand has two consecutive registers.
289       if (!MI->getOperand(OpNo).isReg() ||
290           OpNo+1 == MI->getNumOperands() ||
291           !MI->getOperand(OpNo+1).isReg())
292         return true;
293       ++OpNo;   // Return the high-part.
294       break;
295     case 'I':
296       // Write 'i' if an integer constant, otherwise nothing.  Used to print
297       // addi vs add, etc.
298       if (MI->getOperand(OpNo).isImm())
299         OS << "i";
300       return false;
301     }
302   }
303
304   printOperand(MI, OpNo, OS);
305   return false;
306 }
307
308 bool HexagonAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
309                                             unsigned OpNo, unsigned AsmVariant,
310                                             const char *ExtraCode,
311                                             raw_ostream &O) {
312   if (ExtraCode && ExtraCode[0])
313     return true; // Unknown modifier.
314
315   const MachineOperand &Base  = MI->getOperand(OpNo);
316   const MachineOperand &Offset = MI->getOperand(OpNo+1);
317
318   if (Base.isReg())
319     printOperand(MI, OpNo, O);
320   else
321     llvm_unreachable("Unimplemented");
322
323   if (Offset.isImm()) {
324     if (Offset.getImm())
325       O << " + #" << Offset.getImm();
326   }
327   else
328     llvm_unreachable("Unimplemented");
329
330   return false;
331 }
332
333 void HexagonAsmPrinter::printPredicateOperand(const MachineInstr *MI,
334                                               unsigned OpNo,
335                                               raw_ostream &O) {
336   llvm_unreachable("Unimplemented");
337 }
338
339
340 /// printMachineInstruction -- Print out a single Hexagon MI in Darwin syntax to
341 /// the current output stream.
342 ///
343 void HexagonAsmPrinter::EmitInstruction(const MachineInstr *MI) {
344   SmallString<128> Str;
345   raw_svector_ostream O(Str);
346
347   const MachineFunction* MF = MI->getParent()->getParent();
348   const HexagonMachineFunctionInfo* MFI =
349     (const HexagonMachineFunctionInfo*)
350     MF->getInfo<HexagonMachineFunctionInfo>();
351
352
353
354   // Print a brace for the beginning of the packet.
355   if (MFI->isStartPacket(MI)) {
356     O << "\t{" << '\n';
357   }
358
359   DEBUG( O << "// MI = " << *MI << '\n';);
360
361   // Indent
362   O << "\t";
363
364
365   if (MI->getOpcode() == Hexagon::ENDLOOP0) {
366     if (MFI->isEndPacket(MI) && MFI->isStartPacket(MI)) {
367       O << "\t{ nop }";
368     } else {
369     O << "}";
370     }
371     printInstruction(MI, O);
372   } else if (MI->getOpcode() == Hexagon::STriwt) {
373     //
374     // Handle truncated store on Hexagon.
375     //
376     O << "\tmemw(";
377     printHexagonMEMriOperand(MI, 0, O);
378
379     O << ") = ";
380     unsigned SubRegNum =
381       TM.getRegisterInfo()->getSubReg(MI->getOperand(2)
382                                       .getReg(), Hexagon::subreg_loreg);
383     const char *SubRegName = getRegisterName(SubRegNum);
384     O << SubRegName << '\n';
385   } else if (MI->getOpcode() == Hexagon::MPYI_rin) {
386     // Handle multipy with -ve constant on Hexagon:
387     // "$dst =- mpyi($src1, #$src2)"
388       printOperand(MI, 0, O);
389     O << " =- mpyi(";
390     printOperand(MI, 1, O);
391     O << ", #";
392     printHexagonNegImmOperand(MI, 2, O);
393     O << ")";
394   } else if (MI->getOpcode() == Hexagon::MEMw_ADDSUBi_indexed_MEM_V4) {
395     //
396     // Handle memw(Rs+u6:2) [+-]= #U5
397     //
398     O << "\tmemw("; printHexagonMEMriOperand(MI, 0, O); O << ") ";
399     int addend = MI->getOperand(2).getImm();
400     if (addend < 0)
401       O << "-= " << "#" << -addend << '\n';
402     else
403       O << "+= " << "#" << addend << '\n';
404   } else if (MI->getOpcode() == Hexagon::MEMw_ADDSUBi_MEM_V4) {
405     //
406     // Handle memw(Rs+u6:2) [+-]= #U5
407     //
408     O << "\tmemw("; printHexagonMEMriOperand(MI, 0, O); O << ") ";
409     int addend = MI->getOperand(2).getImm();
410     if (addend < 0)
411       O << "-= " << "#" << -addend << '\n';
412     else
413       O << "+= " << "#" << addend << '\n';
414   } else if (MI->getOpcode() == Hexagon::MEMh_ADDSUBi_indexed_MEM_V4) {
415     //
416     // Handle memh(Rs+u6:1) [+-]= #U5
417     //
418     O << "\tmemh("; printHexagonMEMriOperand(MI, 0, O); O << ") ";
419     int addend = MI->getOperand(2).getImm();
420     if (addend < 0)
421       O << "-= " << "#" << -addend << '\n';
422     else
423       O << "+= " << "#" << addend << '\n';
424   } else if (MI->getOpcode() == Hexagon::MEMh_ADDSUBi_MEM_V4) {
425     //
426     // Handle memh(Rs+u6:1) [+-]= #U5
427     //
428     O << "\tmemh("; printHexagonMEMriOperand(MI, 0, O); O << ") ";
429     int addend = MI->getOperand(2).getImm();
430     if (addend < 0)
431       O << "-= " << "#" << -addend << '\n';
432     else
433       O << "+= " << "#" << addend << '\n';
434   } else if (MI->getOpcode() == Hexagon::MEMb_ADDSUBi_indexed_MEM_V4) {
435     //
436     // Handle memb(Rs+u6:1) [+-]= #U5
437     //
438     O << "\tmemb("; printHexagonMEMriOperand(MI, 0, O); O << ") ";
439     int addend = MI->getOperand(2).getImm();
440     if (addend < 0)
441       O << "-= " << "#" << -addend << '\n';
442     else
443       O << "+= " << "#" << addend << '\n';
444   } else if (MI->getOpcode() == Hexagon::MEMb_ADDSUBi_MEM_V4) {
445     //
446     // Handle memb(Rs+u6:1) [+-]= #U5
447     //
448     O << "\tmemb("; printHexagonMEMriOperand(MI, 0, O); O << ") ";
449     int addend = MI->getOperand(2).getImm();
450     if (addend < 0)
451       O << "-= " << "#" << -addend << '\n';
452     else
453       O << "+= " << "#" << addend << '\n';
454   } else if (MI->getOpcode() == Hexagon::CMPbGTri_V4) {
455     //
456     // Handle Pd=cmpb.gt(Rs,#s8)
457     //
458     O << "\t";
459     printRegister(MI->getOperand(0), false, O);
460     O << " = cmpb.gt(";
461     printRegister(MI->getOperand(1), false, O);
462     O << ", ";
463     int val = MI->getOperand(2).getImm() >> 24;
464     O << "#" << val << ")" << '\n';
465   } else if (MI->getOpcode() == Hexagon::CMPhEQri_V4) {
466     //
467     // Handle Pd=cmph.eq(Rs,#8)
468     //
469     O << "\t";
470     printRegister(MI->getOperand(0), false, O);
471     O << " = cmph.eq(";
472     printRegister(MI->getOperand(1), false, O);
473     O << ", ";
474     int val = MI->getOperand(2).getImm();
475     assert((((0 <= val) && (val <= 127)) ||
476             ((65408 <= val) && (val <= 65535))) &&
477            "Not in correct range!");
478     if (val >= 65408) val -= 65536;
479     O << "#" << val << ")" << '\n';
480   } else if (MI->getOpcode() == Hexagon::CMPhGTri_V4) {
481     //
482     // Handle Pd=cmph.gt(Rs,#8)
483     //
484     O << "\t";
485     printRegister(MI->getOperand(0), false, O);
486     O << " = cmph.gt(";
487     printRegister(MI->getOperand(1), false, O);
488     O << ", ";
489     int val = MI->getOperand(2).getImm() >> 16;
490     O << "#" << val << ")" << '\n';
491   } else {
492     printInstruction(MI, O);
493   }
494
495   // Print a brace for the end of the packet.
496   if (MFI->isEndPacket(MI) && MI->getOpcode() != Hexagon::ENDLOOP0) {
497     O << "\n\t}" << '\n';
498   }
499
500   if (AlignCalls && MI->getDesc().isCall()) {
501     O << "\n\t.falign" << "\n";
502   }
503
504   OutStreamer.EmitRawText(O.str());
505   return;
506 }
507
508 /// PrintUnmangledNameSafely - Print out the printable characters in the name.
509 /// Don't print things like \n or \0.
510 // static void PrintUnmangledNameSafely(const Value *V, raw_ostream &OS) {
511 //   for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
512 //        Name != E; ++Name)
513 //     if (isprint(*Name))
514 //       OS << *Name;
515 // }
516
517
518 void HexagonAsmPrinter::printAddrModeBasePlusOffset(const MachineInstr *MI,
519                                                     int OpNo, raw_ostream &O) {
520   const MachineOperand &MO1 = MI->getOperand(OpNo);
521   const MachineOperand &MO2 = MI->getOperand(OpNo+1);
522
523   O << getRegisterName(MO1.getReg())
524     << " + #"
525     << MO2.getImm();
526 }
527
528
529 void HexagonAsmPrinter::printGlobalOperand(const MachineInstr *MI, int OpNo,
530                                            raw_ostream &O) {
531   const MachineOperand &MO = MI->getOperand(OpNo);
532   assert( (MO.getType() == MachineOperand::MO_GlobalAddress) &&
533          "Expecting global address");
534
535   O << *Mang->getSymbol(MO.getGlobal());
536   if (MO.getOffset() != 0) {
537     O << " + ";
538     O << MO.getOffset();
539   }
540 }
541
542 void HexagonAsmPrinter::printJumpTable(const MachineInstr *MI, int OpNo,
543                                        raw_ostream &O) {
544   const MachineOperand &MO = MI->getOperand(OpNo);
545   assert( (MO.getType() == MachineOperand::MO_JumpTableIndex) &&
546           "Expecting jump table index");
547
548   // Hexagon_TODO: Do we need name mangling?
549   O << *GetJTISymbol(MO.getIndex());
550 }
551
552 extern "C" void LLVMInitializeHexagonAsmPrinter() {
553   RegisterAsmPrinter<HexagonAsmPrinter> X(TheHexagonTarget);
554 }