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";
227 case MachineOperand::MO_MachineBasicBlock:
228 O << *MO.getMBB()->getSymbol();
230 case MachineOperand::MO_JumpTableIndex:
231 O << *GetJTISymbol(MO.getIndex());
232 // FIXME: PIC relocation model.
234 case MachineOperand::MO_ConstantPoolIndex:
235 O << *GetCPISymbol(MO.getIndex());
237 case MachineOperand::MO_ExternalSymbol:
238 O << *GetExternalSymbolSymbol(MO.getSymbolName());
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);
248 O << "<unknown operand type: " << MO.getType() << ">";
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.
262 bool HexagonAsmPrinter::
263 isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const {
264 if (MBB->hasAddressTaken()) {
267 return AsmPrinter::isBlockOnlyReachableByFallthrough(MBB);
271 /// PrintAsmOperand - Print out an operand for an inline asm expression.
273 bool HexagonAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
275 const char *ExtraCode,
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.
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);
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())
293 ++OpNo; // Return the high-part.
296 // Write 'i' if an integer constant, otherwise nothing. Used to print
298 if (MI->getOperand(OpNo).isImm())
304 printOperand(MI, OpNo, OS);
308 bool HexagonAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
309 unsigned OpNo, unsigned AsmVariant,
310 const char *ExtraCode,
312 if (ExtraCode && ExtraCode[0])
313 return true; // Unknown modifier.
315 const MachineOperand &Base = MI->getOperand(OpNo);
316 const MachineOperand &Offset = MI->getOperand(OpNo+1);
319 printOperand(MI, OpNo, O);
321 llvm_unreachable("Unimplemented");
323 if (Offset.isImm()) {
325 O << " + #" << Offset.getImm();
328 llvm_unreachable("Unimplemented");
333 void HexagonAsmPrinter::printPredicateOperand(const MachineInstr *MI,
336 llvm_unreachable("Unimplemented");
340 /// printMachineInstruction -- Print out a single Hexagon MI in Darwin syntax to
341 /// the current output stream.
343 void HexagonAsmPrinter::EmitInstruction(const MachineInstr *MI) {
344 SmallString<128> Str;
345 raw_svector_ostream O(Str);
347 const MachineFunction* MF = MI->getParent()->getParent();
348 const HexagonMachineFunctionInfo* MFI =
349 (const HexagonMachineFunctionInfo*)
350 MF->getInfo<HexagonMachineFunctionInfo>();
354 // Print a brace for the beginning of the packet.
355 if (MFI->isStartPacket(MI)) {
359 DEBUG( O << "// MI = " << *MI << '\n';);
365 if (MI->getOpcode() == Hexagon::ENDLOOP0) {
366 if (MFI->isEndPacket(MI) && MFI->isStartPacket(MI)) {
371 printInstruction(MI, O);
372 } else if (MI->getOpcode() == Hexagon::STriwt) {
374 // Handle truncated store on Hexagon.
377 printHexagonMEMriOperand(MI, 0, O);
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);
390 printOperand(MI, 1, O);
392 printHexagonNegImmOperand(MI, 2, O);
394 } else if (MI->getOpcode() == Hexagon::MEMw_ADDSUBi_indexed_MEM_V4) {
396 // Handle memw(Rs+u6:2) [+-]= #U5
398 O << "\tmemw("; printHexagonMEMriOperand(MI, 0, O); O << ") ";
399 int addend = MI->getOperand(2).getImm();
401 O << "-= " << "#" << -addend << '\n';
403 O << "+= " << "#" << addend << '\n';
404 } else if (MI->getOpcode() == Hexagon::MEMw_ADDSUBi_MEM_V4) {
406 // Handle memw(Rs+u6:2) [+-]= #U5
408 O << "\tmemw("; printHexagonMEMriOperand(MI, 0, O); O << ") ";
409 int addend = MI->getOperand(2).getImm();
411 O << "-= " << "#" << -addend << '\n';
413 O << "+= " << "#" << addend << '\n';
414 } else if (MI->getOpcode() == Hexagon::MEMh_ADDSUBi_indexed_MEM_V4) {
416 // Handle memh(Rs+u6:1) [+-]= #U5
418 O << "\tmemh("; printHexagonMEMriOperand(MI, 0, O); O << ") ";
419 int addend = MI->getOperand(2).getImm();
421 O << "-= " << "#" << -addend << '\n';
423 O << "+= " << "#" << addend << '\n';
424 } else if (MI->getOpcode() == Hexagon::MEMh_ADDSUBi_MEM_V4) {
426 // Handle memh(Rs+u6:1) [+-]= #U5
428 O << "\tmemh("; printHexagonMEMriOperand(MI, 0, O); O << ") ";
429 int addend = MI->getOperand(2).getImm();
431 O << "-= " << "#" << -addend << '\n';
433 O << "+= " << "#" << addend << '\n';
434 } else if (MI->getOpcode() == Hexagon::MEMb_ADDSUBi_indexed_MEM_V4) {
436 // Handle memb(Rs+u6:1) [+-]= #U5
438 O << "\tmemb("; printHexagonMEMriOperand(MI, 0, O); O << ") ";
439 int addend = MI->getOperand(2).getImm();
441 O << "-= " << "#" << -addend << '\n';
443 O << "+= " << "#" << addend << '\n';
444 } else if (MI->getOpcode() == Hexagon::MEMb_ADDSUBi_MEM_V4) {
446 // Handle memb(Rs+u6:1) [+-]= #U5
448 O << "\tmemb("; printHexagonMEMriOperand(MI, 0, O); O << ") ";
449 int addend = MI->getOperand(2).getImm();
451 O << "-= " << "#" << -addend << '\n';
453 O << "+= " << "#" << addend << '\n';
454 } else if (MI->getOpcode() == Hexagon::CMPbGTri_V4) {
456 // Handle Pd=cmpb.gt(Rs,#s8)
459 printRegister(MI->getOperand(0), false, O);
461 printRegister(MI->getOperand(1), false, O);
463 int val = MI->getOperand(2).getImm() >> 24;
464 O << "#" << val << ")" << '\n';
465 } else if (MI->getOpcode() == Hexagon::CMPhEQri_V4) {
467 // Handle Pd=cmph.eq(Rs,#8)
470 printRegister(MI->getOperand(0), false, O);
472 printRegister(MI->getOperand(1), false, 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) {
482 // Handle Pd=cmph.gt(Rs,#8)
485 printRegister(MI->getOperand(0), false, O);
487 printRegister(MI->getOperand(1), false, O);
489 int val = MI->getOperand(2).getImm() >> 16;
490 O << "#" << val << ")" << '\n';
492 printInstruction(MI, O);
495 // Print a brace for the end of the packet.
496 if (MFI->isEndPacket(MI) && MI->getOpcode() != Hexagon::ENDLOOP0) {
497 O << "\n\t}" << '\n';
500 if (AlignCalls && MI->getDesc().isCall()) {
501 O << "\n\t.falign" << "\n";
504 OutStreamer.EmitRawText(O.str());
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))
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);
523 O << getRegisterName(MO1.getReg())
529 void HexagonAsmPrinter::printGlobalOperand(const MachineInstr *MI, int OpNo,
531 const MachineOperand &MO = MI->getOperand(OpNo);
532 assert( (MO.getType() == MachineOperand::MO_GlobalAddress) &&
533 "Expecting global address");
535 O << *Mang->getSymbol(MO.getGlobal());
536 if (MO.getOffset() != 0) {
542 void HexagonAsmPrinter::printJumpTable(const MachineInstr *MI, int OpNo,
544 const MachineOperand &MO = MI->getOperand(OpNo);
545 assert( (MO.getType() == MachineOperand::MO_JumpTableIndex) &&
546 "Expecting jump table index");
548 // Hexagon_TODO: Do we need name mangling?
549 O << *GetJTISymbol(MO.getIndex());
552 extern "C" void LLVMInitializeHexagonAsmPrinter() {
553 RegisterAsmPrinter<HexagonAsmPrinter> X(TheHexagonTarget);