blob: 0dc243f2b8766cb2c41811b2621d8b03c4342c38 [file] [log] [blame]
Jia Liu31d157a2012-02-18 12:03:15 +00001//===-- HexagonAsmPrinter.cpp - Print machine instrs to Hexagon assembly --===//
Tony Linthicumb4b54152011-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 Linthicumb4b54152011-12-12 21:14:40 +000014//===----------------------------------------------------------------------===//
15
Tony Linthicumb4b54152011-12-12 21:14:40 +000016#define DEBUG_TYPE "asm-printer"
17#include "Hexagon.h"
Evandro Menezese5041e62012-04-12 17:55:53 +000018#include "HexagonAsmPrinter.h"
19#include "HexagonMachineFunctionInfo.h"
Sirish Pande26f61a12012-05-03 21:52:53 +000020#include "HexagonMCInst.h"
Tony Linthicumb4b54152011-12-12 21:14:40 +000021#include "HexagonTargetMachine.h"
22#include "HexagonSubtarget.h"
Evandro Menezese5041e62012-04-12 17:55:53 +000023#include "InstPrinter/HexagonInstPrinter.h"
Tony Linthicumb4b54152011-12-12 21:14:40 +000024#include "llvm/Constants.h"
25#include "llvm/DerivedTypes.h"
26#include "llvm/Module.h"
Evandro Menezese5041e62012-04-12 17:55:53 +000027#include "llvm/Analysis/ConstantFolding.h"
Tony Linthicumb4b54152011-12-12 21:14:40 +000028#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"
Tony Linthicumb4b54152011-12-12 21:14:40 +000034#include "llvm/MC/MCAsmInfo.h"
Evandro Menezese5041e62012-04-12 17:55:53 +000035#include "llvm/MC/MCContext.h"
36#include "llvm/MC/MCExpr.h"
37#include "llvm/MC/MCInst.h"
38#include "llvm/MC/MCSection.h"
39#include "llvm/MC/MCStreamer.h"
Tony Linthicumb4b54152011-12-12 21:14:40 +000040#include "llvm/MC/MCSymbol.h"
41#include "llvm/Support/MathExtras.h"
Tony Linthicumb4b54152011-12-12 21:14:40 +000042#include "llvm/Support/CommandLine.h"
43#include "llvm/Support/Debug.h"
44#include "llvm/Support/Compiler.h"
Evandro Menezese5041e62012-04-12 17:55:53 +000045#include "llvm/Support/Format.h"
Tony Linthicumb4b54152011-12-12 21:14:40 +000046#include "llvm/Support/raw_ostream.h"
Craig Topper79aa3412012-03-17 18:46:09 +000047#include "llvm/Support/TargetRegistry.h"
Tony Linthicumb4b54152011-12-12 21:14:40 +000048#include "llvm/Target/Mangler.h"
49#include "llvm/Target/TargetData.h"
50#include "llvm/Target/TargetLoweringObjectFile.h"
51#include "llvm/Target/TargetRegisterInfo.h"
52#include "llvm/Target/TargetInstrInfo.h"
53#include "llvm/Target/TargetOptions.h"
Tony Linthicumb4b54152011-12-12 21:14:40 +000054#include "llvm/ADT/SmallString.h"
Evandro Menezese5041e62012-04-12 17:55:53 +000055#include "llvm/ADT/SmallVector.h"
Tony Linthicumb4b54152011-12-12 21:14:40 +000056#include "llvm/ADT/StringExtras.h"
Tony Linthicumb4b54152011-12-12 21:14:40 +000057
58using namespace llvm;
59
60static 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
Tony Linthicumb4b54152011-12-12 21:14:40 +000064void HexagonAsmPrinter::EmitAlignment(unsigned NumBits,
65 const GlobalValue *GV) const {
Evandro Menezese5041e62012-04-12 17:55:53 +000066 // For basic block level alignment, use ".falign".
Tony Linthicumb4b54152011-12-12 21:14:40 +000067 if (!GV) {
68 OutStreamer.EmitRawText(StringRef("\t.falign"));
69 return;
70 }
71
72 AsmPrinter::EmitAlignment(NumBits, GV);
73}
74
Evandro Menezese5041e62012-04-12 17:55:53 +000075void HexagonAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
76 raw_ostream &O) {
77 const MachineOperand &MO = MI->getOperand(OpNo);
Tony Linthicumb4b54152011-12-12 21:14:40 +000078
Evandro Menezese5041e62012-04-12 17:55:53 +000079 switch (MO.getType()) {
Sirish Pande26f61a12012-05-03 21:52:53 +000080 default: llvm_unreachable ("<unknown operand type>");
Evandro Menezese5041e62012-04-12 17:55:53 +000081 case MachineOperand::MO_Register:
82 O << HexagonInstPrinter::getRegisterName(MO.getReg());
83 return;
84 case MachineOperand::MO_Immediate:
85 O << MO.getImm();
86 return;
Tony Linthicumb4b54152011-12-12 21:14:40 +000087 case MachineOperand::MO_MachineBasicBlock:
88 O << *MO.getMBB()->getSymbol();
89 return;
90 case MachineOperand::MO_JumpTableIndex:
91 O << *GetJTISymbol(MO.getIndex());
92 // FIXME: PIC relocation model.
93 return;
94 case MachineOperand::MO_ConstantPoolIndex:
95 O << *GetCPISymbol(MO.getIndex());
96 return;
97 case MachineOperand::MO_ExternalSymbol:
98 O << *GetExternalSymbolSymbol(MO.getSymbolName());
99 return;
Evandro Menezese5041e62012-04-12 17:55:53 +0000100 case MachineOperand::MO_GlobalAddress:
Tony Linthicumb4b54152011-12-12 21:14:40 +0000101 // Computing the address of a global symbol, not calling it.
102 O << *Mang->getSymbol(MO.getGlobal());
103 printOffset(MO.getOffset(), O);
104 return;
105 }
Tony Linthicumb4b54152011-12-12 21:14:40 +0000106}
107
Tony Linthicumb4b54152011-12-12 21:14:40 +0000108//
109// isBlockOnlyReachableByFallthrough - We need to override this since the
110// default AsmPrinter does not print labels for any basic block that
111// is only reachable by a fall through. That works for all cases except
112// for the case in which the basic block is reachable by a fall through but
113// through an indirect from a jump table. In this case, the jump table
114// will contain a label not defined by AsmPrinter.
115//
116bool HexagonAsmPrinter::
117isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const {
118 if (MBB->hasAddressTaken()) {
119 return false;
120 }
121 return AsmPrinter::isBlockOnlyReachableByFallthrough(MBB);
122}
123
124
125/// PrintAsmOperand - Print out an operand for an inline asm expression.
126///
127bool HexagonAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
128 unsigned AsmVariant,
129 const char *ExtraCode,
Evandro Menezese5041e62012-04-12 17:55:53 +0000130 raw_ostream &OS) {
Tony Linthicumb4b54152011-12-12 21:14:40 +0000131 // Does this asm operand have a single letter operand modifier?
132 if (ExtraCode && ExtraCode[0]) {
133 if (ExtraCode[1] != 0) return true; // Unknown modifier.
134
135 switch (ExtraCode[0]) {
136 default: return true; // Unknown modifier.
137 case 'c': // Don't print "$" before a global var name or constant.
138 // Hexagon never has a prefix.
139 printOperand(MI, OpNo, OS);
140 return false;
141 case 'L': // Write second word of DImode reference.
142 // Verify that this operand has two consecutive registers.
143 if (!MI->getOperand(OpNo).isReg() ||
144 OpNo+1 == MI->getNumOperands() ||
145 !MI->getOperand(OpNo+1).isReg())
146 return true;
147 ++OpNo; // Return the high-part.
148 break;
149 case 'I':
150 // Write 'i' if an integer constant, otherwise nothing. Used to print
151 // addi vs add, etc.
152 if (MI->getOperand(OpNo).isImm())
153 OS << "i";
154 return false;
155 }
156 }
157
158 printOperand(MI, OpNo, OS);
159 return false;
160}
161
162bool HexagonAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
163 unsigned OpNo, unsigned AsmVariant,
164 const char *ExtraCode,
165 raw_ostream &O) {
166 if (ExtraCode && ExtraCode[0])
167 return true; // Unknown modifier.
168
169 const MachineOperand &Base = MI->getOperand(OpNo);
170 const MachineOperand &Offset = MI->getOperand(OpNo+1);
171
172 if (Base.isReg())
173 printOperand(MI, OpNo, O);
174 else
Craig Topperbc219812012-02-07 02:50:20 +0000175 llvm_unreachable("Unimplemented");
Tony Linthicumb4b54152011-12-12 21:14:40 +0000176
177 if (Offset.isImm()) {
178 if (Offset.getImm())
179 O << " + #" << Offset.getImm();
180 }
181 else
Craig Topperbc219812012-02-07 02:50:20 +0000182 llvm_unreachable("Unimplemented");
Tony Linthicumb4b54152011-12-12 21:14:40 +0000183
184 return false;
185}
186
187void HexagonAsmPrinter::printPredicateOperand(const MachineInstr *MI,
188 unsigned OpNo,
189 raw_ostream &O) {
Craig Topperbc219812012-02-07 02:50:20 +0000190 llvm_unreachable("Unimplemented");
Tony Linthicumb4b54152011-12-12 21:14:40 +0000191}
192
193
194/// printMachineInstruction -- Print out a single Hexagon MI in Darwin syntax to
195/// the current output stream.
196///
197void HexagonAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Sirish Pande26f61a12012-05-03 21:52:53 +0000198 if (MI->isBundle()) {
199 std::vector<const MachineInstr*> BundleMIs;
Tony Linthicumb4b54152011-12-12 21:14:40 +0000200
Sirish Pande26f61a12012-05-03 21:52:53 +0000201 const MachineBasicBlock *MBB = MI->getParent();
202 MachineBasicBlock::const_instr_iterator MII = MI;
203 ++MII;
204 unsigned int IgnoreCount = 0;
205 while (MII != MBB->end() && MII->isInsideBundle()) {
206 const MachineInstr *MInst = MII;
207 if (MInst->getOpcode() == TargetOpcode::DBG_VALUE ||
208 MInst->getOpcode() == TargetOpcode::IMPLICIT_DEF) {
209 IgnoreCount++;
210 ++MII;
211 continue;
212 }
213 //BundleMIs.push_back(&*MII);
214 BundleMIs.push_back(MInst);
215 ++MII;
216 }
217 unsigned Size = BundleMIs.size();
218 assert((Size+IgnoreCount) == MI->getBundleSize() && "Corrupt Bundle!");
219 for (unsigned Index = 0; Index < Size; Index++) {
220 HexagonMCInst MCI;
221 MCI.setStartPacket(Index == 0);
222 MCI.setEndPacket(Index == (Size-1));
223
224 HexagonLowerToMC(BundleMIs[Index], MCI, *this);
225 OutStreamer.EmitInstruction(MCI);
226 }
227 }
228 else {
229 HexagonMCInst MCI;
230 if (MI->getOpcode() == Hexagon::ENDLOOP0) {
231 MCI.setStartPacket(true);
232 MCI.setEndPacket(true);
233 }
234 HexagonLowerToMC(MI, MCI, *this);
235 OutStreamer.EmitInstruction(MCI);
236 }
Tony Linthicumb4b54152011-12-12 21:14:40 +0000237
Tony Linthicumb4b54152011-12-12 21:14:40 +0000238 return;
239}
240
241/// PrintUnmangledNameSafely - Print out the printable characters in the name.
242/// Don't print things like \n or \0.
243// static void PrintUnmangledNameSafely(const Value *V, raw_ostream &OS) {
244// for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
245// Name != E; ++Name)
246// if (isprint(*Name))
247// OS << *Name;
248// }
249
250
251void HexagonAsmPrinter::printAddrModeBasePlusOffset(const MachineInstr *MI,
252 int OpNo, raw_ostream &O) {
253 const MachineOperand &MO1 = MI->getOperand(OpNo);
254 const MachineOperand &MO2 = MI->getOperand(OpNo+1);
255
Evandro Menezese5041e62012-04-12 17:55:53 +0000256 O << HexagonInstPrinter::getRegisterName(MO1.getReg())
Tony Linthicumb4b54152011-12-12 21:14:40 +0000257 << " + #"
258 << MO2.getImm();
259}
260
261
262void HexagonAsmPrinter::printGlobalOperand(const MachineInstr *MI, int OpNo,
263 raw_ostream &O) {
264 const MachineOperand &MO = MI->getOperand(OpNo);
265 assert( (MO.getType() == MachineOperand::MO_GlobalAddress) &&
266 "Expecting global address");
267
268 O << *Mang->getSymbol(MO.getGlobal());
269 if (MO.getOffset() != 0) {
270 O << " + ";
271 O << MO.getOffset();
272 }
273}
274
275void HexagonAsmPrinter::printJumpTable(const MachineInstr *MI, int OpNo,
276 raw_ostream &O) {
277 const MachineOperand &MO = MI->getOperand(OpNo);
Sirish Pande26f61a12012-05-03 21:52:53 +0000278 assert( (MO.getType() == MachineOperand::MO_JumpTableIndex) &&
279 "Expecting jump table index");
Tony Linthicumb4b54152011-12-12 21:14:40 +0000280
281 // Hexagon_TODO: Do we need name mangling?
282 O << *GetJTISymbol(MO.getIndex());
283}
284
Evandro Menezese5041e62012-04-12 17:55:53 +0000285void HexagonAsmPrinter::printConstantPool(const MachineInstr *MI, int OpNo,
Sirish Pande26f61a12012-05-03 21:52:53 +0000286 raw_ostream &O) {
Evandro Menezese5041e62012-04-12 17:55:53 +0000287 const MachineOperand &MO = MI->getOperand(OpNo);
288 assert( (MO.getType() == MachineOperand::MO_ConstantPoolIndex) &&
Chandler Carruthd410eab2012-04-23 18:25:57 +0000289 "Expecting constant pool index");
Evandro Menezese5041e62012-04-12 17:55:53 +0000290
291 // Hexagon_TODO: Do we need name mangling?
292 O << *GetCPISymbol(MO.getIndex());
293}
294
295static MCInstPrinter *createHexagonMCInstPrinter(const Target &T,
296 unsigned SyntaxVariant,
297 const MCAsmInfo &MAI,
298 const MCInstrInfo &MII,
299 const MCRegisterInfo &MRI,
300 const MCSubtargetInfo &STI) {
301 if (SyntaxVariant == 0)
302 return(new HexagonInstPrinter(MAI, MII, MRI));
303 else
304 return NULL;
305}
306
Tony Linthicumb4b54152011-12-12 21:14:40 +0000307extern "C" void LLVMInitializeHexagonAsmPrinter() {
308 RegisterAsmPrinter<HexagonAsmPrinter> X(TheHexagonTarget);
Evandro Menezese5041e62012-04-12 17:55:53 +0000309
310 TargetRegistry::RegisterMCInstPrinter(TheHexagonTarget,
311 createHexagonMCInstPrinter);
Tony Linthicumb4b54152011-12-12 21:14:40 +0000312}