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 //===----------------------------------------------------------------------===//
17 #define DEBUG_TYPE "asm-printer"
19 #include "HexagonTargetMachine.h"
20 #include "HexagonSubtarget.h"
21 #include "HexagonMachineFunctionInfo.h"
22 #include "llvm/Constants.h"
23 #include "llvm/DerivedTypes.h"
24 #include "llvm/Module.h"
25 #include "llvm/Assembly/Writer.h"
26 #include "llvm/CodeGen/AsmPrinter.h"
27 #include "llvm/CodeGen/MachineModuleInfo.h"
28 #include "llvm/CodeGen/MachineFunctionPass.h"
29 #include "llvm/CodeGen/MachineInstr.h"
30 #include "llvm/CodeGen/MachineInstrBuilder.h"
31 #include "llvm/MC/MCStreamer.h"
32 #include "llvm/MC/MCAsmInfo.h"
33 #include "llvm/MC/MCSymbol.h"
34 #include "llvm/Support/MathExtras.h"
35 #include "llvm/Support/MathExtras.h"
36 #include "llvm/Support/CommandLine.h"
37 #include "llvm/Support/Debug.h"
38 #include "llvm/Support/Compiler.h"
39 #include "llvm/Support/raw_ostream.h"
40 #include "llvm/Target/Mangler.h"
41 #include "llvm/Target/TargetData.h"
42 #include "llvm/Target/TargetLoweringObjectFile.h"
43 #include "llvm/Target/TargetRegisterInfo.h"
44 #include "llvm/Target/TargetInstrInfo.h"
45 #include "llvm/Target/TargetOptions.h"
46 #include "llvm/ADT/SmallPtrSet.h"
47 #include "llvm/ADT/SmallString.h"
48 #include "llvm/ADT/StringExtras.h"
49 #include "llvm/Support/TargetRegistry.h"
50 #include "llvm/Support/raw_ostream.h"
54 static cl::opt<bool> AlignCalls(
55 "hexagon-align-calls", cl::Hidden, cl::init(true),
56 cl::desc("Insert falign after call instruction for Hexagon target"));
60 class HexagonAsmPrinter : public AsmPrinter {
61 const HexagonSubtarget *Subtarget;
64 explicit HexagonAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
65 : AsmPrinter(TM, Streamer) {
66 Subtarget = &TM.getSubtarget<HexagonSubtarget>();
69 virtual const char *getPassName() const {
70 return "Hexagon Assembly Printer";
73 /// printInstruction - This method is automatically generated by tablegen
74 /// from the instruction set description. This method returns true if the
75 /// machine instruction was sufficiently described to print it, otherwise it
76 void printInstruction(const MachineInstr *MI, raw_ostream &O);
77 virtual void EmitInstruction(const MachineInstr *MI);
79 void printOp(const MachineOperand &MO, raw_ostream &O);
81 /// printRegister - Print register according to target requirements.
83 void printRegister(const MachineOperand &MO, bool R0AsZero,
85 unsigned RegNo = MO.getReg();
86 assert(TargetRegisterInfo::isPhysicalRegister(RegNo) && "Not physreg??");
87 O << getRegisterName(RegNo);
90 void printOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &OS) {
91 const MachineOperand &MO = MI->getOperand(OpNo);
93 printRegister(MO, false, OS);
94 } else if (MO.isImm()) {
102 bool isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const;
104 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
105 unsigned AsmVariant, const char *ExtraCode,
107 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
108 unsigned AsmVariant, const char *ExtraCode,
112 void printHexagonImmOperand(const MachineInstr *MI, unsigned OpNo,
114 int value = MI->getOperand(OpNo).getImm();
119 void printHexagonNegImmOperand(const MachineInstr *MI, unsigned OpNo,
121 int value = MI->getOperand(OpNo).getImm();
125 void printHexagonNOneImmOperand(const MachineInstr *MI, unsigned OpNo,
126 raw_ostream &O) const {
130 void printHexagonMEMriOperand(const MachineInstr *MI, unsigned OpNo,
132 const MachineOperand &MO1 = MI->getOperand(OpNo);
133 const MachineOperand &MO2 = MI->getOperand(OpNo+1);
135 O << getRegisterName(MO1.getReg())
137 << (int) MO2.getImm();
141 void printHexagonFrameIndexOperand(const MachineInstr *MI, unsigned OpNo,
143 const MachineOperand &MO1 = MI->getOperand(OpNo);
144 const MachineOperand &MO2 = MI->getOperand(OpNo+1);
146 O << getRegisterName(MO1.getReg())
151 void printBranchOperand(const MachineInstr *MI, unsigned OpNo,
153 // Branches can take an immediate operand. This is used by the branch
154 // selection pass to print $+8, an eight byte displacement from the PC.
155 if (MI->getOperand(OpNo).isImm()) {
156 O << "$+" << MI->getOperand(OpNo).getImm()*4;
158 printOp(MI->getOperand(OpNo), O);
162 void printCallOperand(const MachineInstr *MI, unsigned OpNo,
166 void printAbsAddrOperand(const MachineInstr *MI, unsigned OpNo,
171 void printSymbolHi(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
173 if (MI->getOperand(OpNo).isImm()) {
174 printHexagonImmOperand(MI, OpNo, O);
176 printOp(MI->getOperand(OpNo), O);
181 void printSymbolLo(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
183 if (MI->getOperand(OpNo).isImm()) {
184 printHexagonImmOperand(MI, OpNo, O);
186 printOp(MI->getOperand(OpNo), O);
191 void printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
194 void printAddrModeBasePlusOffset(const MachineInstr *MI, int OpNo,
197 void printGlobalOperand(const MachineInstr *MI, int OpNo, raw_ostream &O);
198 void printJumpTable(const MachineInstr *MI, int OpNo, raw_ostream &O);
200 void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0) const;
202 static const char *getRegisterName(unsigned RegNo);
205 } // end of anonymous namespace
207 // Include the auto-generated portion of the assembly writer.
208 #include "HexagonGenAsmWriter.inc"
211 void HexagonAsmPrinter::EmitAlignment(unsigned NumBits,
212 const GlobalValue *GV) const {
214 // For basic block level alignment, use falign.
216 OutStreamer.EmitRawText(StringRef("\t.falign"));
220 AsmPrinter::EmitAlignment(NumBits, GV);
223 void HexagonAsmPrinter::printOp(const MachineOperand &MO, raw_ostream &O) {
224 switch (MO.getType()) {
225 case MachineOperand::MO_Immediate:
226 dbgs() << "printOp() does not handle immediate values\n";
229 case MachineOperand::MO_MachineBasicBlock:
230 O << *MO.getMBB()->getSymbol();
232 case MachineOperand::MO_JumpTableIndex:
233 O << *GetJTISymbol(MO.getIndex());
234 // FIXME: PIC relocation model.
236 case MachineOperand::MO_ConstantPoolIndex:
237 O << *GetCPISymbol(MO.getIndex());
239 case MachineOperand::MO_ExternalSymbol:
240 O << *GetExternalSymbolSymbol(MO.getSymbolName());
242 case MachineOperand::MO_GlobalAddress: {
243 // Computing the address of a global symbol, not calling it.
244 O << *Mang->getSymbol(MO.getGlobal());
245 printOffset(MO.getOffset(), O);
250 O << "<unknown operand type: " << MO.getType() << ">";
257 // isBlockOnlyReachableByFallthrough - We need to override this since the
258 // default AsmPrinter does not print labels for any basic block that
259 // is only reachable by a fall through. That works for all cases except
260 // for the case in which the basic block is reachable by a fall through but
261 // through an indirect from a jump table. In this case, the jump table
262 // will contain a label not defined by AsmPrinter.
264 bool HexagonAsmPrinter::
265 isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const {
266 if (MBB->hasAddressTaken()) {
269 return AsmPrinter::isBlockOnlyReachableByFallthrough(MBB);
273 /// PrintAsmOperand - Print out an operand for an inline asm expression.
275 bool HexagonAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
277 const char *ExtraCode,
279 // Does this asm operand have a single letter operand modifier?
280 if (ExtraCode && ExtraCode[0]) {
281 if (ExtraCode[1] != 0) return true; // Unknown modifier.
283 switch (ExtraCode[0]) {
284 default: return true; // Unknown modifier.
285 case 'c': // Don't print "$" before a global var name or constant.
286 // Hexagon never has a prefix.
287 printOperand(MI, OpNo, OS);
289 case 'L': // Write second word of DImode reference.
290 // Verify that this operand has two consecutive registers.
291 if (!MI->getOperand(OpNo).isReg() ||
292 OpNo+1 == MI->getNumOperands() ||
293 !MI->getOperand(OpNo+1).isReg())
295 ++OpNo; // Return the high-part.
298 // Write 'i' if an integer constant, otherwise nothing. Used to print
300 if (MI->getOperand(OpNo).isImm())
306 printOperand(MI, OpNo, OS);
310 bool HexagonAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
311 unsigned OpNo, unsigned AsmVariant,
312 const char *ExtraCode,
314 if (ExtraCode && ExtraCode[0])
315 return true; // Unknown modifier.
317 const MachineOperand &Base = MI->getOperand(OpNo);
318 const MachineOperand &Offset = MI->getOperand(OpNo+1);
321 printOperand(MI, OpNo, O);
323 llvm_unreachable("Unimplemented");
325 if (Offset.isImm()) {
327 O << " + #" << Offset.getImm();
330 llvm_unreachable("Unimplemented");
335 void HexagonAsmPrinter::printPredicateOperand(const MachineInstr *MI,
338 llvm_unreachable("Unimplemented");
342 /// printMachineInstruction -- Print out a single Hexagon MI in Darwin syntax to
343 /// the current output stream.
345 void HexagonAsmPrinter::EmitInstruction(const MachineInstr *MI) {
346 SmallString<128> Str;
347 raw_svector_ostream O(Str);
349 const MachineFunction* MF = MI->getParent()->getParent();
350 const HexagonMachineFunctionInfo* MFI =
351 (const HexagonMachineFunctionInfo*)
352 MF->getInfo<HexagonMachineFunctionInfo>();
356 // Print a brace for the beginning of the packet.
357 if (MFI->isStartPacket(MI)) {
361 DEBUG( O << "// MI = " << *MI << '\n';);
367 if (MI->getOpcode() == Hexagon::ENDLOOP0) {
368 if (MFI->isEndPacket(MI) && MFI->isStartPacket(MI)) {
373 printInstruction(MI, O);
374 } else if (MI->getOpcode() == Hexagon::MPYI_rin) {
375 // Handle multipy with -ve constant on Hexagon:
376 // "$dst =- mpyi($src1, #$src2)"
377 printOperand(MI, 0, O);
379 printOperand(MI, 1, O);
381 printHexagonNegImmOperand(MI, 2, O);
383 } else if (MI->getOpcode() == Hexagon::MEMw_ADDSUBi_indexed_MEM_V4) {
385 // Handle memw(Rs+u6:2) [+-]= #U5
387 O << "\tmemw("; printHexagonMEMriOperand(MI, 0, O); O << ") ";
388 int addend = MI->getOperand(2).getImm();
390 O << "-= " << "#" << -addend << '\n';
392 O << "+= " << "#" << addend << '\n';
393 } else if (MI->getOpcode() == Hexagon::MEMw_ADDSUBi_MEM_V4) {
395 // Handle memw(Rs+u6:2) [+-]= #U5
397 O << "\tmemw("; printHexagonMEMriOperand(MI, 0, O); O << ") ";
398 int addend = MI->getOperand(2).getImm();
400 O << "-= " << "#" << -addend << '\n';
402 O << "+= " << "#" << addend << '\n';
403 } else if (MI->getOpcode() == Hexagon::MEMh_ADDSUBi_indexed_MEM_V4) {
405 // Handle memh(Rs+u6:1) [+-]= #U5
407 O << "\tmemh("; printHexagonMEMriOperand(MI, 0, O); O << ") ";
408 int addend = MI->getOperand(2).getImm();
410 O << "-= " << "#" << -addend << '\n';
412 O << "+= " << "#" << addend << '\n';
413 } else if (MI->getOpcode() == Hexagon::MEMh_ADDSUBi_MEM_V4) {
415 // Handle memh(Rs+u6:1) [+-]= #U5
417 O << "\tmemh("; printHexagonMEMriOperand(MI, 0, O); O << ") ";
418 int addend = MI->getOperand(2).getImm();
420 O << "-= " << "#" << -addend << '\n';
422 O << "+= " << "#" << addend << '\n';
423 } else if (MI->getOpcode() == Hexagon::MEMb_ADDSUBi_indexed_MEM_V4) {
425 // Handle memb(Rs+u6:1) [+-]= #U5
427 O << "\tmemb("; printHexagonMEMriOperand(MI, 0, O); O << ") ";
428 int addend = MI->getOperand(2).getImm();
430 O << "-= " << "#" << -addend << '\n';
432 O << "+= " << "#" << addend << '\n';
433 } else if (MI->getOpcode() == Hexagon::MEMb_ADDSUBi_MEM_V4) {
435 // Handle memb(Rs+u6:1) [+-]= #U5
437 O << "\tmemb("; printHexagonMEMriOperand(MI, 0, O); O << ") ";
438 int addend = MI->getOperand(2).getImm();
440 O << "-= " << "#" << -addend << '\n';
442 O << "+= " << "#" << addend << '\n';
443 } else if (MI->getOpcode() == Hexagon::CMPbGTri_V4) {
445 // Handle Pd=cmpb.gt(Rs,#s8)
448 printRegister(MI->getOperand(0), false, O);
450 printRegister(MI->getOperand(1), false, O);
452 int val = MI->getOperand(2).getImm() >> 24;
453 O << "#" << val << ")" << '\n';
454 } else if (MI->getOpcode() == Hexagon::CMPhEQri_V4) {
456 // Handle Pd=cmph.eq(Rs,#8)
459 printRegister(MI->getOperand(0), false, O);
461 printRegister(MI->getOperand(1), false, O);
463 int val = MI->getOperand(2).getImm();
464 assert((((0 <= val) && (val <= 127)) ||
465 ((65408 <= val) && (val <= 65535))) &&
466 "Not in correct range!");
467 if (val >= 65408) val -= 65536;
468 O << "#" << val << ")" << '\n';
469 } else if (MI->getOpcode() == Hexagon::CMPhGTri_V4) {
471 // Handle Pd=cmph.gt(Rs,#8)
474 printRegister(MI->getOperand(0), false, O);
476 printRegister(MI->getOperand(1), false, O);
478 int val = MI->getOperand(2).getImm() >> 16;
479 O << "#" << val << ")" << '\n';
481 printInstruction(MI, O);
484 // Print a brace for the end of the packet.
485 if (MFI->isEndPacket(MI) && MI->getOpcode() != Hexagon::ENDLOOP0) {
486 O << "\n\t}" << '\n';
489 if (AlignCalls && MI->getDesc().isCall()) {
490 O << "\n\t.falign" << "\n";
493 OutStreamer.EmitRawText(O.str());
497 /// PrintUnmangledNameSafely - Print out the printable characters in the name.
498 /// Don't print things like \n or \0.
499 // static void PrintUnmangledNameSafely(const Value *V, raw_ostream &OS) {
500 // for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
501 // Name != E; ++Name)
502 // if (isprint(*Name))
507 void HexagonAsmPrinter::printAddrModeBasePlusOffset(const MachineInstr *MI,
508 int OpNo, raw_ostream &O) {
509 const MachineOperand &MO1 = MI->getOperand(OpNo);
510 const MachineOperand &MO2 = MI->getOperand(OpNo+1);
512 O << getRegisterName(MO1.getReg())
518 void HexagonAsmPrinter::printGlobalOperand(const MachineInstr *MI, int OpNo,
520 const MachineOperand &MO = MI->getOperand(OpNo);
521 assert( (MO.getType() == MachineOperand::MO_GlobalAddress) &&
522 "Expecting global address");
524 O << *Mang->getSymbol(MO.getGlobal());
525 if (MO.getOffset() != 0) {
531 void HexagonAsmPrinter::printJumpTable(const MachineInstr *MI, int OpNo,
533 const MachineOperand &MO = MI->getOperand(OpNo);
534 assert( (MO.getType() == MachineOperand::MO_JumpTableIndex) &&
535 "Expecting jump table index");
537 // Hexagon_TODO: Do we need name mangling?
538 O << *GetJTISymbol(MO.getIndex());
541 extern "C" void LLVMInitializeHexagonAsmPrinter() {
542 RegisterAsmPrinter<HexagonAsmPrinter> X(TheHexagonTarget);