Hexagon backend support
[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     return;
227
228   case MachineOperand::MO_MachineBasicBlock:
229     O << *MO.getMBB()->getSymbol();
230     return;
231   case MachineOperand::MO_JumpTableIndex:
232     O << *GetJTISymbol(MO.getIndex());
233     // FIXME: PIC relocation model.
234     return;
235   case MachineOperand::MO_ConstantPoolIndex:
236     O << *GetCPISymbol(MO.getIndex());
237     return;
238   case MachineOperand::MO_ExternalSymbol:
239     O << *GetExternalSymbolSymbol(MO.getSymbolName());
240     return;
241   case MachineOperand::MO_GlobalAddress: {
242     // Computing the address of a global symbol, not calling it.
243     O << *Mang->getSymbol(MO.getGlobal());
244     printOffset(MO.getOffset(), O);
245     return;
246   }
247
248   default:
249     O << "<unknown operand type: " << MO.getType() << ">";
250     return;
251   }
252 }
253
254
255 //
256 // isBlockOnlyReachableByFallthrough - We need to override this since the
257 // default AsmPrinter does not print labels for any basic block that
258 // is only reachable by a fall through. That works for all cases except
259 // for the case in which the basic block is reachable by a fall through but
260 // through an indirect from a jump table. In this case, the jump table
261 // will contain a label not defined by AsmPrinter.
262 //
263 bool HexagonAsmPrinter::
264 isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const {
265   if (MBB->hasAddressTaken()) {
266     return false;
267   }
268   return AsmPrinter::isBlockOnlyReachableByFallthrough(MBB);
269 }
270
271
272 /// PrintAsmOperand - Print out an operand for an inline asm expression.
273 ///
274 bool HexagonAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
275                                         unsigned AsmVariant,
276                                         const char *ExtraCode,
277                                       raw_ostream &OS) {
278   // Does this asm operand have a single letter operand modifier?
279   if (ExtraCode && ExtraCode[0]) {
280     if (ExtraCode[1] != 0) return true; // Unknown modifier.
281
282     switch (ExtraCode[0]) {
283     default: return true;  // Unknown modifier.
284     case 'c': // Don't print "$" before a global var name or constant.
285       // Hexagon never has a prefix.
286       printOperand(MI, OpNo, OS);
287       return false;
288     case 'L': // Write second word of DImode reference.
289       // Verify that this operand has two consecutive registers.
290       if (!MI->getOperand(OpNo).isReg() ||
291           OpNo+1 == MI->getNumOperands() ||
292           !MI->getOperand(OpNo+1).isReg())
293         return true;
294       ++OpNo;   // Return the high-part.
295       break;
296     case 'I':
297       // Write 'i' if an integer constant, otherwise nothing.  Used to print
298       // addi vs add, etc.
299       if (MI->getOperand(OpNo).isImm())
300         OS << "i";
301       return false;
302     }
303   }
304
305   printOperand(MI, OpNo, OS);
306   return false;
307 }
308
309 bool HexagonAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
310                                             unsigned OpNo, unsigned AsmVariant,
311                                             const char *ExtraCode,
312                                             raw_ostream &O) {
313   if (ExtraCode && ExtraCode[0])
314     return true; // Unknown modifier.
315
316   const MachineOperand &Base  = MI->getOperand(OpNo);
317   const MachineOperand &Offset = MI->getOperand(OpNo+1);
318
319   if (Base.isReg())
320     printOperand(MI, OpNo, O);
321   else
322     assert(0 && "Unimplemented");
323
324   if (Offset.isImm()) {
325     if (Offset.getImm())
326       O << " + #" << Offset.getImm();
327   }
328   else
329     assert(0 && "Unimplemented");
330
331   return false;
332 }
333
334 void HexagonAsmPrinter::printPredicateOperand(const MachineInstr *MI,
335                                               unsigned OpNo,
336                                               raw_ostream &O) {
337   assert(0 && "Unimplemented");
338 }
339
340
341 /// printMachineInstruction -- Print out a single Hexagon MI in Darwin syntax to
342 /// the current output stream.
343 ///
344 void HexagonAsmPrinter::EmitInstruction(const MachineInstr *MI) {
345   SmallString<128> Str;
346   raw_svector_ostream O(Str);
347
348   const MachineFunction* MF = MI->getParent()->getParent();
349   const HexagonMachineFunctionInfo* MFI =
350     (const HexagonMachineFunctionInfo*)
351     MF->getInfo<HexagonMachineFunctionInfo>();
352
353
354
355   // Print a brace for the beginning of the packet.
356   if (MFI->isStartPacket(MI)) {
357     O << "\t{" << '\n';
358   }
359
360   DEBUG( O << "// MI = " << *MI << '\n';);
361
362   // Indent
363   O << "\t";
364
365
366   if (MI->getOpcode() == Hexagon::ENDLOOP0) {
367     if (MFI->isEndPacket(MI) && MFI->isStartPacket(MI)) {
368       O << "\t{ nop }";
369     } else {
370     O << "}";
371     }
372     printInstruction(MI, O);
373   } else if (MI->getOpcode() == Hexagon::STriwt) {
374     //
375     // Handle truncated store on Hexagon.
376     //
377     O << "\tmemw(";
378     printHexagonMEMriOperand(MI, 0, O);
379
380     O << ") = ";
381     unsigned SubRegNum =
382       TM.getRegisterInfo()->getSubReg(MI->getOperand(2)
383                                       .getReg(), Hexagon::subreg_loreg);
384     const char *SubRegName = getRegisterName(SubRegNum);
385     O << SubRegName << '\n';
386   } else if (MI->getOpcode() == Hexagon::MPYI_rin) {
387     // Handle multipy with -ve constant on Hexagon:
388     // "$dst =- mpyi($src1, #$src2)"
389       printOperand(MI, 0, O);
390     O << " =- mpyi(";
391     printOperand(MI, 1, O);
392     O << ", #";
393     printHexagonNegImmOperand(MI, 2, O);
394     O << ")";
395   } else if (MI->getOpcode() == Hexagon::MEMw_ADDSUBi_indexed_MEM_V4) {
396     //
397     // Handle memw(Rs+u6:2) [+-]= #U5
398     //
399     O << "\tmemw("; printHexagonMEMriOperand(MI, 0, O); O << ") ";
400     int addend = MI->getOperand(2).getImm();
401     if (addend < 0)
402       O << "-= " << "#" << -addend << '\n';
403     else
404       O << "+= " << "#" << addend << '\n';
405   } else if (MI->getOpcode() == Hexagon::MEMw_ADDSUBi_MEM_V4) {
406     //
407     // Handle memw(Rs+u6:2) [+-]= #U5
408     //
409     O << "\tmemw("; printHexagonMEMriOperand(MI, 0, O); O << ") ";
410     int addend = MI->getOperand(2).getImm();
411     if (addend < 0)
412       O << "-= " << "#" << -addend << '\n';
413     else
414       O << "+= " << "#" << addend << '\n';
415   } else if (MI->getOpcode() == Hexagon::MEMh_ADDSUBi_indexed_MEM_V4) {
416     //
417     // Handle memh(Rs+u6:1) [+-]= #U5
418     //
419     O << "\tmemh("; printHexagonMEMriOperand(MI, 0, O); O << ") ";
420     int addend = MI->getOperand(2).getImm();
421     if (addend < 0)
422       O << "-= " << "#" << -addend << '\n';
423     else
424       O << "+= " << "#" << addend << '\n';
425   } else if (MI->getOpcode() == Hexagon::MEMh_ADDSUBi_MEM_V4) {
426     //
427     // Handle memh(Rs+u6:1) [+-]= #U5
428     //
429     O << "\tmemh("; printHexagonMEMriOperand(MI, 0, O); O << ") ";
430     int addend = MI->getOperand(2).getImm();
431     if (addend < 0)
432       O << "-= " << "#" << -addend << '\n';
433     else
434       O << "+= " << "#" << addend << '\n';
435   } else if (MI->getOpcode() == Hexagon::MEMb_ADDSUBi_indexed_MEM_V4) {
436     //
437     // Handle memb(Rs+u6:1) [+-]= #U5
438     //
439     O << "\tmemb("; printHexagonMEMriOperand(MI, 0, O); O << ") ";
440     int addend = MI->getOperand(2).getImm();
441     if (addend < 0)
442       O << "-= " << "#" << -addend << '\n';
443     else
444       O << "+= " << "#" << addend << '\n';
445   } else if (MI->getOpcode() == Hexagon::MEMb_ADDSUBi_MEM_V4) {
446     //
447     // Handle memb(Rs+u6:1) [+-]= #U5
448     //
449     O << "\tmemb("; printHexagonMEMriOperand(MI, 0, O); O << ") ";
450     int addend = MI->getOperand(2).getImm();
451     if (addend < 0)
452       O << "-= " << "#" << -addend << '\n';
453     else
454       O << "+= " << "#" << addend << '\n';
455   } else if (MI->getOpcode() == Hexagon::CMPbGTri_V4) {
456     //
457     // Handle Pd=cmpb.gt(Rs,#s8)
458     //
459     O << "\t";
460     printRegister(MI->getOperand(0), false, O);
461     O << " = cmpb.gt(";
462     printRegister(MI->getOperand(1), false, O);
463     O << ", ";
464     int val = MI->getOperand(2).getImm() >> 24;
465     O << "#" << val << ")" << '\n';
466   } else if (MI->getOpcode() == Hexagon::CMPhEQri_V4) {
467     //
468     // Handle Pd=cmph.eq(Rs,#8)
469     //
470     O << "\t";
471     printRegister(MI->getOperand(0), false, O);
472     O << " = cmph.eq(";
473     printRegister(MI->getOperand(1), false, O);
474     O << ", ";
475     int val = MI->getOperand(2).getImm();
476     assert((((0 <= val) && (val <= 127)) ||
477             ((65408 <= val) && (val <= 65535))) &&
478            "Not in correct range!");
479     if (val >= 65408) val -= 65536;
480     O << "#" << val << ")" << '\n';
481   } else if (MI->getOpcode() == Hexagon::CMPhGTri_V4) {
482     //
483     // Handle Pd=cmph.gt(Rs,#8)
484     //
485     O << "\t";
486     printRegister(MI->getOperand(0), false, O);
487     O << " = cmph.gt(";
488     printRegister(MI->getOperand(1), false, O);
489     O << ", ";
490     int val = MI->getOperand(2).getImm() >> 16;
491     O << "#" << val << ")" << '\n';
492   } else {
493     printInstruction(MI, O);
494   }
495
496   // Print a brace for the end of the packet.
497   if (MFI->isEndPacket(MI) && MI->getOpcode() != Hexagon::ENDLOOP0) {
498     O << "\n\t}" << '\n';
499   }
500
501   if (AlignCalls && MI->getDesc().isCall()) {
502     O << "\n\t.falign" << "\n";
503   }
504
505   OutStreamer.EmitRawText(O.str());
506   return;
507 }
508
509 /// PrintUnmangledNameSafely - Print out the printable characters in the name.
510 /// Don't print things like \n or \0.
511 // static void PrintUnmangledNameSafely(const Value *V, raw_ostream &OS) {
512 //   for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
513 //        Name != E; ++Name)
514 //     if (isprint(*Name))
515 //       OS << *Name;
516 // }
517
518
519 void HexagonAsmPrinter::printAddrModeBasePlusOffset(const MachineInstr *MI,
520                                                     int OpNo, raw_ostream &O) {
521   const MachineOperand &MO1 = MI->getOperand(OpNo);
522   const MachineOperand &MO2 = MI->getOperand(OpNo+1);
523
524   O << getRegisterName(MO1.getReg())
525     << " + #"
526     << MO2.getImm();
527 }
528
529
530 void HexagonAsmPrinter::printGlobalOperand(const MachineInstr *MI, int OpNo,
531                                            raw_ostream &O) {
532   const MachineOperand &MO = MI->getOperand(OpNo);
533   assert( (MO.getType() == MachineOperand::MO_GlobalAddress) &&
534          "Expecting global address");
535
536   O << *Mang->getSymbol(MO.getGlobal());
537   if (MO.getOffset() != 0) {
538     O << " + ";
539     O << MO.getOffset();
540   }
541 }
542
543 void HexagonAsmPrinter::printJumpTable(const MachineInstr *MI, int OpNo,
544                                        raw_ostream &O) {
545   const MachineOperand &MO = MI->getOperand(OpNo);
546   assert( (MO.getType() == MachineOperand::MO_JumpTableIndex) &&
547           "Expecting jump table index");
548
549   // Hexagon_TODO: Do we need name mangling?
550   O << *GetJTISymbol(MO.getIndex());
551 }
552
553 extern "C" void LLVMInitializeHexagonAsmPrinter() {
554   RegisterAsmPrinter<HexagonAsmPrinter> X(TheHexagonTarget);
555 }