blob: f15765a672630482f4a424c08e54c8fdbc67caf3 [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- HexagonAsmPrinter.cpp - Print machine instrs to Hexagon assembly --===//
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002//
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//
Tony Linthicum1213a7a2011-12-12 21:14:40 +000014//===----------------------------------------------------------------------===//
15
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "Hexagon.h"
Jyotsna Verma7503a622013-02-20 16:13:27 +000017#include "HexagonAsmPrinter.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "HexagonMachineFunctionInfo.h"
Jyotsna Verma7503a622013-02-20 16:13:27 +000019#include "HexagonSubtarget.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000020#include "HexagonTargetMachine.h"
Evandro Menezes5cee6212012-04-12 17:55:53 +000021#include "InstPrinter/HexagonInstPrinter.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000022#include "MCTargetDesc/HexagonMCInst.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/ADT/SmallString.h"
24#include "llvm/ADT/SmallVector.h"
25#include "llvm/ADT/StringExtras.h"
Evandro Menezes5cee6212012-04-12 17:55:53 +000026#include "llvm/Analysis/ConstantFolding.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000027#include "llvm/CodeGen/AsmPrinter.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000028#include "llvm/CodeGen/MachineFunctionPass.h"
29#include "llvm/CodeGen/MachineInstr.h"
30#include "llvm/CodeGen/MachineInstrBuilder.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000031#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000032#include "llvm/IR/Constants.h"
33#include "llvm/IR/DataLayout.h"
34#include "llvm/IR/DerivedTypes.h"
Rafael Espindola894843c2014-01-07 21:19:40 +000035#include "llvm/IR/Mangler.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000036#include "llvm/IR/Module.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000037#include "llvm/MC/MCAsmInfo.h"
Evandro Menezes5cee6212012-04-12 17:55:53 +000038#include "llvm/MC/MCContext.h"
39#include "llvm/MC/MCExpr.h"
40#include "llvm/MC/MCInst.h"
41#include "llvm/MC/MCSection.h"
42#include "llvm/MC/MCStreamer.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000043#include "llvm/MC/MCSymbol.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000044#include "llvm/Support/CommandLine.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000045#include "llvm/Support/Compiler.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000046#include "llvm/Support/Debug.h"
Evandro Menezes5cee6212012-04-12 17:55:53 +000047#include "llvm/Support/Format.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000048#include "llvm/Support/MathExtras.h"
Craig Topperb25fda92012-03-17 18:46:09 +000049#include "llvm/Support/TargetRegistry.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000050#include "llvm/Support/raw_ostream.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000051#include "llvm/Target/TargetInstrInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000052#include "llvm/Target/TargetLoweringObjectFile.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000053#include "llvm/Target/TargetOptions.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000054#include "llvm/Target/TargetRegisterInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000055
56using namespace llvm;
57
Chandler Carruth84e68b22014-04-22 02:41:26 +000058#define DEBUG_TYPE "asm-printer"
59
Tony Linthicum1213a7a2011-12-12 21:14:40 +000060static cl::opt<bool> AlignCalls(
61 "hexagon-align-calls", cl::Hidden, cl::init(true),
62 cl::desc("Insert falign after call instruction for Hexagon target"));
63
Evandro Menezes5cee6212012-04-12 17:55:53 +000064void HexagonAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
65 raw_ostream &O) {
66 const MachineOperand &MO = MI->getOperand(OpNo);
Tony Linthicum1213a7a2011-12-12 21:14:40 +000067
Evandro Menezes5cee6212012-04-12 17:55:53 +000068 switch (MO.getType()) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000069 default: llvm_unreachable ("<unknown operand type>");
Evandro Menezes5cee6212012-04-12 17:55:53 +000070 case MachineOperand::MO_Register:
71 O << HexagonInstPrinter::getRegisterName(MO.getReg());
72 return;
73 case MachineOperand::MO_Immediate:
74 O << MO.getImm();
75 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000076 case MachineOperand::MO_MachineBasicBlock:
77 O << *MO.getMBB()->getSymbol();
78 return;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000079 case MachineOperand::MO_ConstantPoolIndex:
80 O << *GetCPISymbol(MO.getIndex());
81 return;
Evandro Menezes5cee6212012-04-12 17:55:53 +000082 case MachineOperand::MO_GlobalAddress:
Tony Linthicum1213a7a2011-12-12 21:14:40 +000083 // Computing the address of a global symbol, not calling it.
Rafael Espindola79858aa2013-10-29 17:07:16 +000084 O << *getSymbol(MO.getGlobal());
Tony Linthicum1213a7a2011-12-12 21:14:40 +000085 printOffset(MO.getOffset(), O);
86 return;
87 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +000088}
89
Tony Linthicum1213a7a2011-12-12 21:14:40 +000090//
91// isBlockOnlyReachableByFallthrough - We need to override this since the
92// default AsmPrinter does not print labels for any basic block that
93// is only reachable by a fall through. That works for all cases except
94// for the case in which the basic block is reachable by a fall through but
95// through an indirect from a jump table. In this case, the jump table
96// will contain a label not defined by AsmPrinter.
97//
98bool HexagonAsmPrinter::
99isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const {
100 if (MBB->hasAddressTaken()) {
101 return false;
102 }
103 return AsmPrinter::isBlockOnlyReachableByFallthrough(MBB);
104}
105
106
107/// PrintAsmOperand - Print out an operand for an inline asm expression.
108///
109bool HexagonAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
110 unsigned AsmVariant,
111 const char *ExtraCode,
Evandro Menezes5cee6212012-04-12 17:55:53 +0000112 raw_ostream &OS) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000113 // Does this asm operand have a single letter operand modifier?
114 if (ExtraCode && ExtraCode[0]) {
115 if (ExtraCode[1] != 0) return true; // Unknown modifier.
116
117 switch (ExtraCode[0]) {
Jack Carter5e69cff2012-06-26 13:49:27 +0000118 default:
119 // See if this is a generic print operand
120 return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, OS);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000121 case 'c': // Don't print "$" before a global var name or constant.
122 // Hexagon never has a prefix.
123 printOperand(MI, OpNo, OS);
124 return false;
125 case 'L': // Write second word of DImode reference.
126 // Verify that this operand has two consecutive registers.
127 if (!MI->getOperand(OpNo).isReg() ||
128 OpNo+1 == MI->getNumOperands() ||
129 !MI->getOperand(OpNo+1).isReg())
130 return true;
131 ++OpNo; // Return the high-part.
132 break;
133 case 'I':
134 // Write 'i' if an integer constant, otherwise nothing. Used to print
135 // addi vs add, etc.
136 if (MI->getOperand(OpNo).isImm())
137 OS << "i";
138 return false;
139 }
140 }
141
142 printOperand(MI, OpNo, OS);
143 return false;
144}
145
146bool HexagonAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
147 unsigned OpNo, unsigned AsmVariant,
148 const char *ExtraCode,
149 raw_ostream &O) {
150 if (ExtraCode && ExtraCode[0])
151 return true; // Unknown modifier.
152
153 const MachineOperand &Base = MI->getOperand(OpNo);
154 const MachineOperand &Offset = MI->getOperand(OpNo+1);
155
156 if (Base.isReg())
157 printOperand(MI, OpNo, O);
158 else
Craig Toppere55c5562012-02-07 02:50:20 +0000159 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000160
161 if (Offset.isImm()) {
162 if (Offset.getImm())
163 O << " + #" << Offset.getImm();
164 }
165 else
Craig Toppere55c5562012-02-07 02:50:20 +0000166 llvm_unreachable("Unimplemented");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000167
168 return false;
169}
170
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000171
172/// printMachineInstruction -- Print out a single Hexagon MI in Darwin syntax to
173/// the current output stream.
174///
175void HexagonAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000176 if (MI->isBundle()) {
177 std::vector<const MachineInstr*> BundleMIs;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000178
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000179 const MachineBasicBlock *MBB = MI->getParent();
180 MachineBasicBlock::const_instr_iterator MII = MI;
181 ++MII;
182 unsigned int IgnoreCount = 0;
183 while (MII != MBB->end() && MII->isInsideBundle()) {
184 const MachineInstr *MInst = MII;
185 if (MInst->getOpcode() == TargetOpcode::DBG_VALUE ||
186 MInst->getOpcode() == TargetOpcode::IMPLICIT_DEF) {
187 IgnoreCount++;
188 ++MII;
189 continue;
190 }
191 //BundleMIs.push_back(&*MII);
192 BundleMIs.push_back(MInst);
193 ++MII;
194 }
195 unsigned Size = BundleMIs.size();
196 assert((Size+IgnoreCount) == MI->getBundleSize() && "Corrupt Bundle!");
197 for (unsigned Index = 0; Index < Size; Index++) {
198 HexagonMCInst MCI;
Jyotsna Verma7503a622013-02-20 16:13:27 +0000199 MCI.setPacketStart(Index == 0);
200 MCI.setPacketEnd(Index == (Size-1));
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000201
202 HexagonLowerToMC(BundleMIs[Index], MCI, *this);
David Woodhousee6c13e42014-01-28 23:12:42 +0000203 EmitToStreamer(OutStreamer, MCI);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000204 }
205 }
206 else {
207 HexagonMCInst MCI;
208 if (MI->getOpcode() == Hexagon::ENDLOOP0) {
Jyotsna Verma7503a622013-02-20 16:13:27 +0000209 MCI.setPacketStart(true);
210 MCI.setPacketEnd(true);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000211 }
212 HexagonLowerToMC(MI, MCI, *this);
David Woodhousee6c13e42014-01-28 23:12:42 +0000213 EmitToStreamer(OutStreamer, MCI);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000214 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000215
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000216 return;
217}
218
Evandro Menezes5cee6212012-04-12 17:55:53 +0000219static MCInstPrinter *createHexagonMCInstPrinter(const Target &T,
220 unsigned SyntaxVariant,
221 const MCAsmInfo &MAI,
222 const MCInstrInfo &MII,
223 const MCRegisterInfo &MRI,
224 const MCSubtargetInfo &STI) {
225 if (SyntaxVariant == 0)
226 return(new HexagonInstPrinter(MAI, MII, MRI));
227 else
228 return NULL;
229}
230
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000231extern "C" void LLVMInitializeHexagonAsmPrinter() {
232 RegisterAsmPrinter<HexagonAsmPrinter> X(TheHexagonTarget);
Evandro Menezes5cee6212012-04-12 17:55:53 +0000233
234 TargetRegistry::RegisterMCInstPrinter(TheHexagonTarget,
235 createHexagonMCInstPrinter);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000236}