1 //===-- HexagonAsmPrinter.cpp - Print machine instrs to Hexagon assembly ----=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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'.
14 // Documentation at http://developer.apple.com/documentation/DeveloperTools/
15 // Reference/Assembler/ASMIntroduction/chapter_1_section_1.html
17 //===----------------------------------------------------------------------===//
20 #define DEBUG_TYPE "asm-printer"
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"
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"));
63 class HexagonAsmPrinter : public AsmPrinter {
64 const HexagonSubtarget *Subtarget;
67 explicit HexagonAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
68 : AsmPrinter(TM, Streamer) {
69 Subtarget = &TM.getSubtarget<HexagonSubtarget>();
72 virtual const char *getPassName() const {
73 return "Hexagon Assembly Printer";
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);
82 void printOp(const MachineOperand &MO, raw_ostream &O);
84 /// printRegister - Print register according to target requirements.
86 void printRegister(const MachineOperand &MO, bool R0AsZero,
88 unsigned RegNo = MO.getReg();
89 assert(TargetRegisterInfo::isPhysicalRegister(RegNo) && "Not physreg??");
90 O << getRegisterName(RegNo);
93 void printOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &OS) {
94 const MachineOperand &MO = MI->getOperand(OpNo);
96 printRegister(MO, false, OS);
97 } else if (MO.isImm()) {
105 bool isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const;
107 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
108 unsigned AsmVariant, const char *ExtraCode,
110 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
111 unsigned AsmVariant, const char *ExtraCode,
115 void printHexagonImmOperand(const MachineInstr *MI, unsigned OpNo,
117 int value = MI->getOperand(OpNo).getImm();
122 void printHexagonNegImmOperand(const MachineInstr *MI, unsigned OpNo,
124 int value = MI->getOperand(OpNo).getImm();
128 void printHexagonMEMriOperand(const MachineInstr *MI, unsigned OpNo,
130 const MachineOperand &MO1 = MI->getOperand(OpNo);
131 const MachineOperand &MO2 = MI->getOperand(OpNo+1);
133 O << getRegisterName(MO1.getReg())
135 << (int) MO2.getImm();
139 void printHexagonFrameIndexOperand(const MachineInstr *MI, unsigned OpNo,
141 const MachineOperand &MO1 = MI->getOperand(OpNo);
142 const MachineOperand &MO2 = MI->getOperand(OpNo+1);
144 O << getRegisterName(MO1.getReg())
149 void printBranchOperand(const MachineInstr *MI, unsigned OpNo,
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;
156 printOp(MI->getOperand(OpNo), O);
160 void printCallOperand(const MachineInstr *MI, unsigned OpNo,
164 void printAbsAddrOperand(const MachineInstr *MI, unsigned OpNo,
169 void printSymbolHi(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
171 if (MI->getOperand(OpNo).isImm()) {
172 printHexagonImmOperand(MI, OpNo, O);
174 printOp(MI->getOperand(OpNo), O);
179 void printSymbolLo(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
181 if (MI->getOperand(OpNo).isImm()) {
182 printHexagonImmOperand(MI, OpNo, O);
184 printOp(MI->getOperand(OpNo), O);
189 void printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
192 void printAddrModeBasePlusOffset(const MachineInstr *MI, int OpNo,
195 void printGlobalOperand(const MachineInstr *MI, int OpNo, raw_ostream &O);
196 void printJumpTable(const MachineInstr *MI, int OpNo, raw_ostream &O);
198 void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0) const;
200 static const char *getRegisterName(unsigned RegNo);
203 } // end of anonymous namespace
205 // Include the auto-generated portion of the assembly writer.
206 #include "HexagonGenAsmWriter.inc"
209 void HexagonAsmPrinter::EmitAlignment(unsigned NumBits,
210 const GlobalValue *GV) const {
212 // For basic block level alignment, use falign.
214 OutStreamer.EmitRawText(StringRef("\t.falign"));
218 AsmPrinter::EmitAlignment(NumBits, GV);
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";
228 case MachineOperand::MO_MachineBasicBlock:
229 O << *MO.getMBB()->getSymbol();
231 case MachineOperand::MO_JumpTableIndex:
232 O << *GetJTISymbol(MO.getIndex());
233 // FIXME: PIC relocation model.
235 case MachineOperand::MO_ConstantPoolIndex:
236 O << *GetCPISymbol(MO.getIndex());
238 case MachineOperand::MO_ExternalSymbol:
239 O << *GetExternalSymbolSymbol(MO.getSymbolName());
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);
249 O << "<unknown operand type: " << MO.getType() << ">";
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.
263 bool HexagonAsmPrinter::
264 isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const {
265 if (MBB->hasAddressTaken()) {
268 return AsmPrinter::isBlockOnlyReachableByFallthrough(MBB);
272 /// PrintAsmOperand - Print out an operand for an inline asm expression.
274 bool HexagonAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
276 const char *ExtraCode,
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.
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);
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())
294 ++OpNo; // Return the high-part.
297 // Write 'i' if an integer constant, otherwise nothing. Used to print
299 if (MI->getOperand(OpNo).isImm())
305 printOperand(MI, OpNo, OS);
309 bool HexagonAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
310 unsigned OpNo, unsigned AsmVariant,
311 const char *ExtraCode,
313 if (ExtraCode && ExtraCode[0])
314 return true; // Unknown modifier.
316 const MachineOperand &Base = MI->getOperand(OpNo);
317 const MachineOperand &Offset = MI->getOperand(OpNo+1);
320 printOperand(MI, OpNo, O);
322 assert(0 && "Unimplemented");
324 if (Offset.isImm()) {
326 O << " + #" << Offset.getImm();
329 assert(0 && "Unimplemented");
334 void HexagonAsmPrinter::printPredicateOperand(const MachineInstr *MI,
337 assert(0 && "Unimplemented");
341 /// printMachineInstruction -- Print out a single Hexagon MI in Darwin syntax to
342 /// the current output stream.
344 void HexagonAsmPrinter::EmitInstruction(const MachineInstr *MI) {
345 SmallString<128> Str;
346 raw_svector_ostream O(Str);
348 const MachineFunction* MF = MI->getParent()->getParent();
349 const HexagonMachineFunctionInfo* MFI =
350 (const HexagonMachineFunctionInfo*)
351 MF->getInfo<HexagonMachineFunctionInfo>();
355 // Print a brace for the beginning of the packet.
356 if (MFI->isStartPacket(MI)) {
360 DEBUG( O << "// MI = " << *MI << '\n';);
366 if (MI->getOpcode() == Hexagon::ENDLOOP0) {
367 if (MFI->isEndPacket(MI) && MFI->isStartPacket(MI)) {
372 printInstruction(MI, O);
373 } else if (MI->getOpcode() == Hexagon::STriwt) {
375 // Handle truncated store on Hexagon.
378 printHexagonMEMriOperand(MI, 0, O);
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);
391 printOperand(MI, 1, O);
393 printHexagonNegImmOperand(MI, 2, O);
395 } else if (MI->getOpcode() == Hexagon::MEMw_ADDSUBi_indexed_MEM_V4) {
397 // Handle memw(Rs+u6:2) [+-]= #U5
399 O << "\tmemw("; printHexagonMEMriOperand(MI, 0, O); O << ") ";
400 int addend = MI->getOperand(2).getImm();
402 O << "-= " << "#" << -addend << '\n';
404 O << "+= " << "#" << addend << '\n';
405 } else if (MI->getOpcode() == Hexagon::MEMw_ADDSUBi_MEM_V4) {
407 // Handle memw(Rs+u6:2) [+-]= #U5
409 O << "\tmemw("; printHexagonMEMriOperand(MI, 0, O); O << ") ";
410 int addend = MI->getOperand(2).getImm();
412 O << "-= " << "#" << -addend << '\n';
414 O << "+= " << "#" << addend << '\n';
415 } else if (MI->getOpcode() == Hexagon::MEMh_ADDSUBi_indexed_MEM_V4) {
417 // Handle memh(Rs+u6:1) [+-]= #U5
419 O << "\tmemh("; printHexagonMEMriOperand(MI, 0, O); O << ") ";
420 int addend = MI->getOperand(2).getImm();
422 O << "-= " << "#" << -addend << '\n';
424 O << "+= " << "#" << addend << '\n';
425 } else if (MI->getOpcode() == Hexagon::MEMh_ADDSUBi_MEM_V4) {
427 // Handle memh(Rs+u6:1) [+-]= #U5
429 O << "\tmemh("; printHexagonMEMriOperand(MI, 0, O); O << ") ";
430 int addend = MI->getOperand(2).getImm();
432 O << "-= " << "#" << -addend << '\n';
434 O << "+= " << "#" << addend << '\n';
435 } else if (MI->getOpcode() == Hexagon::MEMb_ADDSUBi_indexed_MEM_V4) {
437 // Handle memb(Rs+u6:1) [+-]= #U5
439 O << "\tmemb("; printHexagonMEMriOperand(MI, 0, O); O << ") ";
440 int addend = MI->getOperand(2).getImm();
442 O << "-= " << "#" << -addend << '\n';
444 O << "+= " << "#" << addend << '\n';
445 } else if (MI->getOpcode() == Hexagon::MEMb_ADDSUBi_MEM_V4) {
447 // Handle memb(Rs+u6:1) [+-]= #U5
449 O << "\tmemb("; printHexagonMEMriOperand(MI, 0, O); O << ") ";
450 int addend = MI->getOperand(2).getImm();
452 O << "-= " << "#" << -addend << '\n';
454 O << "+= " << "#" << addend << '\n';
455 } else if (MI->getOpcode() == Hexagon::CMPbGTri_V4) {
457 // Handle Pd=cmpb.gt(Rs,#s8)
460 printRegister(MI->getOperand(0), false, O);
462 printRegister(MI->getOperand(1), false, O);
464 int val = MI->getOperand(2).getImm() >> 24;
465 O << "#" << val << ")" << '\n';
466 } else if (MI->getOpcode() == Hexagon::CMPhEQri_V4) {
468 // Handle Pd=cmph.eq(Rs,#8)
471 printRegister(MI->getOperand(0), false, O);
473 printRegister(MI->getOperand(1), false, 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) {
483 // Handle Pd=cmph.gt(Rs,#8)
486 printRegister(MI->getOperand(0), false, O);
488 printRegister(MI->getOperand(1), false, O);
490 int val = MI->getOperand(2).getImm() >> 16;
491 O << "#" << val << ")" << '\n';
493 printInstruction(MI, O);
496 // Print a brace for the end of the packet.
497 if (MFI->isEndPacket(MI) && MI->getOpcode() != Hexagon::ENDLOOP0) {
498 O << "\n\t}" << '\n';
501 if (AlignCalls && MI->getDesc().isCall()) {
502 O << "\n\t.falign" << "\n";
505 OutStreamer.EmitRawText(O.str());
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))
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);
524 O << getRegisterName(MO1.getReg())
530 void HexagonAsmPrinter::printGlobalOperand(const MachineInstr *MI, int OpNo,
532 const MachineOperand &MO = MI->getOperand(OpNo);
533 assert( (MO.getType() == MachineOperand::MO_GlobalAddress) &&
534 "Expecting global address");
536 O << *Mang->getSymbol(MO.getGlobal());
537 if (MO.getOffset() != 0) {
543 void HexagonAsmPrinter::printJumpTable(const MachineInstr *MI, int OpNo,
545 const MachineOperand &MO = MI->getOperand(OpNo);
546 assert( (MO.getType() == MachineOperand::MO_JumpTableIndex) &&
547 "Expecting jump table index");
549 // Hexagon_TODO: Do we need name mangling?
550 O << *GetJTISymbol(MO.getIndex());
553 extern "C" void LLVMInitializeHexagonAsmPrinter() {
554 RegisterAsmPrinter<HexagonAsmPrinter> X(TheHexagonTarget);