blob: 06e57ecccac63fc5f3536b8b4dca8e36cceaf43b [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 Panded1a87a62012-04-12 21:06:38 +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"
Sirish Panded1a87a62012-04-12 21:06:38 +000057#include <map>
Tony Linthicumb4b54152011-12-12 21:14:40 +000058
59using namespace llvm;
60
61static cl::opt<bool> AlignCalls(
62 "hexagon-align-calls", cl::Hidden, cl::init(true),
63 cl::desc("Insert falign after call instruction for Hexagon target"));
64
Tony Linthicumb4b54152011-12-12 21:14:40 +000065void HexagonAsmPrinter::EmitAlignment(unsigned NumBits,
66 const GlobalValue *GV) const {
Evandro Menezese5041e62012-04-12 17:55:53 +000067 // For basic block level alignment, use ".falign".
Tony Linthicumb4b54152011-12-12 21:14:40 +000068 if (!GV) {
69 OutStreamer.EmitRawText(StringRef("\t.falign"));
70 return;
71 }
72
73 AsmPrinter::EmitAlignment(NumBits, GV);
74}
75
Evandro Menezese5041e62012-04-12 17:55:53 +000076void HexagonAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
77 raw_ostream &O) {
78 const MachineOperand &MO = MI->getOperand(OpNo);
Tony Linthicumb4b54152011-12-12 21:14:40 +000079
Evandro Menezese5041e62012-04-12 17:55:53 +000080 switch (MO.getType()) {
81 default:
82 assert(0 && "<unknown operand type>");
83 case MachineOperand::MO_Register:
84 O << HexagonInstPrinter::getRegisterName(MO.getReg());
85 return;
86 case MachineOperand::MO_Immediate:
87 O << MO.getImm();
88 return;
Tony Linthicumb4b54152011-12-12 21:14:40 +000089 case MachineOperand::MO_MachineBasicBlock:
90 O << *MO.getMBB()->getSymbol();
91 return;
92 case MachineOperand::MO_JumpTableIndex:
93 O << *GetJTISymbol(MO.getIndex());
94 // FIXME: PIC relocation model.
95 return;
96 case MachineOperand::MO_ConstantPoolIndex:
97 O << *GetCPISymbol(MO.getIndex());
98 return;
99 case MachineOperand::MO_ExternalSymbol:
100 O << *GetExternalSymbolSymbol(MO.getSymbolName());
101 return;
Evandro Menezese5041e62012-04-12 17:55:53 +0000102 case MachineOperand::MO_GlobalAddress:
Tony Linthicumb4b54152011-12-12 21:14:40 +0000103 // Computing the address of a global symbol, not calling it.
104 O << *Mang->getSymbol(MO.getGlobal());
105 printOffset(MO.getOffset(), O);
106 return;
107 }
Tony Linthicumb4b54152011-12-12 21:14:40 +0000108}
109
Tony Linthicumb4b54152011-12-12 21:14:40 +0000110//
111// isBlockOnlyReachableByFallthrough - We need to override this since the
112// default AsmPrinter does not print labels for any basic block that
113// is only reachable by a fall through. That works for all cases except
114// for the case in which the basic block is reachable by a fall through but
115// through an indirect from a jump table. In this case, the jump table
116// will contain a label not defined by AsmPrinter.
117//
118bool HexagonAsmPrinter::
119isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const {
120 if (MBB->hasAddressTaken()) {
121 return false;
122 }
123 return AsmPrinter::isBlockOnlyReachableByFallthrough(MBB);
124}
125
126
127/// PrintAsmOperand - Print out an operand for an inline asm expression.
128///
129bool HexagonAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
130 unsigned AsmVariant,
131 const char *ExtraCode,
Evandro Menezese5041e62012-04-12 17:55:53 +0000132 raw_ostream &OS) {
Tony Linthicumb4b54152011-12-12 21:14:40 +0000133 // Does this asm operand have a single letter operand modifier?
134 if (ExtraCode && ExtraCode[0]) {
135 if (ExtraCode[1] != 0) return true; // Unknown modifier.
136
137 switch (ExtraCode[0]) {
138 default: return true; // Unknown modifier.
139 case 'c': // Don't print "$" before a global var name or constant.
140 // Hexagon never has a prefix.
141 printOperand(MI, OpNo, OS);
142 return false;
143 case 'L': // Write second word of DImode reference.
144 // Verify that this operand has two consecutive registers.
145 if (!MI->getOperand(OpNo).isReg() ||
146 OpNo+1 == MI->getNumOperands() ||
147 !MI->getOperand(OpNo+1).isReg())
148 return true;
149 ++OpNo; // Return the high-part.
150 break;
151 case 'I':
152 // Write 'i' if an integer constant, otherwise nothing. Used to print
153 // addi vs add, etc.
154 if (MI->getOperand(OpNo).isImm())
155 OS << "i";
156 return false;
157 }
158 }
159
160 printOperand(MI, OpNo, OS);
161 return false;
162}
163
164bool HexagonAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
165 unsigned OpNo, unsigned AsmVariant,
166 const char *ExtraCode,
167 raw_ostream &O) {
168 if (ExtraCode && ExtraCode[0])
169 return true; // Unknown modifier.
170
171 const MachineOperand &Base = MI->getOperand(OpNo);
172 const MachineOperand &Offset = MI->getOperand(OpNo+1);
173
174 if (Base.isReg())
175 printOperand(MI, OpNo, O);
176 else
Craig Topperbc219812012-02-07 02:50:20 +0000177 llvm_unreachable("Unimplemented");
Tony Linthicumb4b54152011-12-12 21:14:40 +0000178
179 if (Offset.isImm()) {
180 if (Offset.getImm())
181 O << " + #" << Offset.getImm();
182 }
183 else
Craig Topperbc219812012-02-07 02:50:20 +0000184 llvm_unreachable("Unimplemented");
Tony Linthicumb4b54152011-12-12 21:14:40 +0000185
186 return false;
187}
188
189void HexagonAsmPrinter::printPredicateOperand(const MachineInstr *MI,
190 unsigned OpNo,
191 raw_ostream &O) {
Craig Topperbc219812012-02-07 02:50:20 +0000192 llvm_unreachable("Unimplemented");
Tony Linthicumb4b54152011-12-12 21:14:40 +0000193}
194
195
196/// printMachineInstruction -- Print out a single Hexagon MI in Darwin syntax to
197/// the current output stream.
198///
199void HexagonAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Sirish Panded1a87a62012-04-12 21:06:38 +0000200 if (MI->isBundle()) {
201 std::vector<const MachineInstr*> BundleMIs;
Tony Linthicumb4b54152011-12-12 21:14:40 +0000202
Sirish Panded1a87a62012-04-12 21:06:38 +0000203 const MachineBasicBlock *MBB = MI->getParent();
204 MachineBasicBlock::const_instr_iterator MII = MI;
205 ++MII;
206 unsigned int IgnoreCount = 0;
207 while (MII != MBB->end() && MII->isInsideBundle()) {
208 const MachineInstr *MInst = MII;
209 if (MInst->getOpcode() == TargetOpcode::DBG_VALUE ||
210 MInst->getOpcode() == TargetOpcode::IMPLICIT_DEF) {
211 IgnoreCount++;
212 ++MII;
213 continue;
214 }
215 //BundleMIs.push_back(&*MII);
216 BundleMIs.push_back(MInst);
217 ++MII;
218 }
219 unsigned Size = BundleMIs.size();
220 assert((Size+IgnoreCount) == MI->getBundleSize() && "Corrupt Bundle!");
221 for (unsigned Index = 0; Index < Size; Index++) {
222 HexagonMCInst MCI;
223 MCI.setStartPacket(Index == 0);
224 MCI.setEndPacket(Index == (Size-1));
225
226 HexagonLowerToMC(BundleMIs[Index], MCI, *this);
227 OutStreamer.EmitInstruction(MCI);
228 }
229 }
230 else {
231 HexagonMCInst MCI;
232 if (MI->getOpcode() == Hexagon::ENDLOOP0) {
233 MCI.setStartPacket(true);
234 MCI.setEndPacket(true);
235 }
236 HexagonLowerToMC(MI, MCI, *this);
237 OutStreamer.EmitInstruction(MCI);
238 }
Tony Linthicumb4b54152011-12-12 21:14:40 +0000239
Tony Linthicumb4b54152011-12-12 21:14:40 +0000240 return;
241}
242
243/// PrintUnmangledNameSafely - Print out the printable characters in the name.
244/// Don't print things like \n or \0.
245// static void PrintUnmangledNameSafely(const Value *V, raw_ostream &OS) {
246// for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
247// Name != E; ++Name)
248// if (isprint(*Name))
249// OS << *Name;
250// }
251
252
253void HexagonAsmPrinter::printAddrModeBasePlusOffset(const MachineInstr *MI,
254 int OpNo, raw_ostream &O) {
255 const MachineOperand &MO1 = MI->getOperand(OpNo);
256 const MachineOperand &MO2 = MI->getOperand(OpNo+1);
257
Evandro Menezese5041e62012-04-12 17:55:53 +0000258 O << HexagonInstPrinter::getRegisterName(MO1.getReg())
Tony Linthicumb4b54152011-12-12 21:14:40 +0000259 << " + #"
260 << MO2.getImm();
261}
262
263
264void HexagonAsmPrinter::printGlobalOperand(const MachineInstr *MI, int OpNo,
265 raw_ostream &O) {
266 const MachineOperand &MO = MI->getOperand(OpNo);
267 assert( (MO.getType() == MachineOperand::MO_GlobalAddress) &&
268 "Expecting global address");
269
270 O << *Mang->getSymbol(MO.getGlobal());
271 if (MO.getOffset() != 0) {
272 O << " + ";
273 O << MO.getOffset();
274 }
275}
276
277void HexagonAsmPrinter::printJumpTable(const MachineInstr *MI, int OpNo,
278 raw_ostream &O) {
279 const MachineOperand &MO = MI->getOperand(OpNo);
280 assert( (MO.getType() == MachineOperand::MO_JumpTableIndex) &&
Sirish Panded1a87a62012-04-12 21:06:38 +0000281 "Expecting jump table index");
Tony Linthicumb4b54152011-12-12 21:14:40 +0000282
283 // Hexagon_TODO: Do we need name mangling?
284 O << *GetJTISymbol(MO.getIndex());
285}
286
Evandro Menezese5041e62012-04-12 17:55:53 +0000287void HexagonAsmPrinter::printConstantPool(const MachineInstr *MI, int OpNo,
Sirish Panded1a87a62012-04-12 21:06:38 +0000288 raw_ostream &O) {
Evandro Menezese5041e62012-04-12 17:55:53 +0000289 const MachineOperand &MO = MI->getOperand(OpNo);
290 assert( (MO.getType() == MachineOperand::MO_ConstantPoolIndex) &&
Sirish Panded1a87a62012-04-12 21:06:38 +0000291 "Expecting constant pool index");
Evandro Menezese5041e62012-04-12 17:55:53 +0000292
293 // Hexagon_TODO: Do we need name mangling?
294 O << *GetCPISymbol(MO.getIndex());
295}
296
297static MCInstPrinter *createHexagonMCInstPrinter(const Target &T,
298 unsigned SyntaxVariant,
299 const MCAsmInfo &MAI,
300 const MCInstrInfo &MII,
301 const MCRegisterInfo &MRI,
302 const MCSubtargetInfo &STI) {
303 if (SyntaxVariant == 0)
304 return(new HexagonInstPrinter(MAI, MII, MRI));
305 else
306 return NULL;
307}
308
Tony Linthicumb4b54152011-12-12 21:14:40 +0000309extern "C" void LLVMInitializeHexagonAsmPrinter() {
310 RegisterAsmPrinter<HexagonAsmPrinter> X(TheHexagonTarget);
Evandro Menezese5041e62012-04-12 17:55:53 +0000311
312 TargetRegistry::RegisterMCInstPrinter(TheHexagonTarget,
313 createHexagonMCInstPrinter);
Tony Linthicumb4b54152011-12-12 21:14:40 +0000314}