blob: 39bf45d2d773278189c163785ea5cee06ab9c353 [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
Chandler Carruthd410eab2012-04-23 18:25:57 +000016
Tony Linthicumb4b54152011-12-12 21:14:40 +000017#define DEBUG_TYPE "asm-printer"
18#include "Hexagon.h"
Evandro Menezese5041e62012-04-12 17:55:53 +000019#include "HexagonAsmPrinter.h"
20#include "HexagonMachineFunctionInfo.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()) {
Chandler Carruthd410eab2012-04-23 18:25:57 +000080 default:
81 assert(0 && "<unknown operand type>");
Evandro Menezese5041e62012-04-12 17:55:53 +000082 case MachineOperand::MO_Register:
83 O << HexagonInstPrinter::getRegisterName(MO.getReg());
84 return;
85 case MachineOperand::MO_Immediate:
86 O << MO.getImm();
87 return;
Tony Linthicumb4b54152011-12-12 21:14:40 +000088 case MachineOperand::MO_MachineBasicBlock:
89 O << *MO.getMBB()->getSymbol();
90 return;
91 case MachineOperand::MO_JumpTableIndex:
92 O << *GetJTISymbol(MO.getIndex());
93 // FIXME: PIC relocation model.
94 return;
95 case MachineOperand::MO_ConstantPoolIndex:
96 O << *GetCPISymbol(MO.getIndex());
97 return;
98 case MachineOperand::MO_ExternalSymbol:
99 O << *GetExternalSymbolSymbol(MO.getSymbolName());
100 return;
Evandro Menezese5041e62012-04-12 17:55:53 +0000101 case MachineOperand::MO_GlobalAddress:
Tony Linthicumb4b54152011-12-12 21:14:40 +0000102 // Computing the address of a global symbol, not calling it.
103 O << *Mang->getSymbol(MO.getGlobal());
104 printOffset(MO.getOffset(), O);
105 return;
106 }
Tony Linthicumb4b54152011-12-12 21:14:40 +0000107}
108
Tony Linthicumb4b54152011-12-12 21:14:40 +0000109//
110// isBlockOnlyReachableByFallthrough - We need to override this since the
111// default AsmPrinter does not print labels for any basic block that
112// is only reachable by a fall through. That works for all cases except
113// for the case in which the basic block is reachable by a fall through but
114// through an indirect from a jump table. In this case, the jump table
115// will contain a label not defined by AsmPrinter.
116//
117bool HexagonAsmPrinter::
118isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const {
119 if (MBB->hasAddressTaken()) {
120 return false;
121 }
122 return AsmPrinter::isBlockOnlyReachableByFallthrough(MBB);
123}
124
125
126/// PrintAsmOperand - Print out an operand for an inline asm expression.
127///
128bool HexagonAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
129 unsigned AsmVariant,
130 const char *ExtraCode,
Evandro Menezese5041e62012-04-12 17:55:53 +0000131 raw_ostream &OS) {
Tony Linthicumb4b54152011-12-12 21:14:40 +0000132 // Does this asm operand have a single letter operand modifier?
133 if (ExtraCode && ExtraCode[0]) {
134 if (ExtraCode[1] != 0) return true; // Unknown modifier.
135
136 switch (ExtraCode[0]) {
137 default: return true; // Unknown modifier.
138 case 'c': // Don't print "$" before a global var name or constant.
139 // Hexagon never has a prefix.
140 printOperand(MI, OpNo, OS);
141 return false;
142 case 'L': // Write second word of DImode reference.
143 // Verify that this operand has two consecutive registers.
144 if (!MI->getOperand(OpNo).isReg() ||
145 OpNo+1 == MI->getNumOperands() ||
146 !MI->getOperand(OpNo+1).isReg())
147 return true;
148 ++OpNo; // Return the high-part.
149 break;
150 case 'I':
151 // Write 'i' if an integer constant, otherwise nothing. Used to print
152 // addi vs add, etc.
153 if (MI->getOperand(OpNo).isImm())
154 OS << "i";
155 return false;
156 }
157 }
158
159 printOperand(MI, OpNo, OS);
160 return false;
161}
162
163bool HexagonAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
164 unsigned OpNo, unsigned AsmVariant,
165 const char *ExtraCode,
166 raw_ostream &O) {
167 if (ExtraCode && ExtraCode[0])
168 return true; // Unknown modifier.
169
170 const MachineOperand &Base = MI->getOperand(OpNo);
171 const MachineOperand &Offset = MI->getOperand(OpNo+1);
172
173 if (Base.isReg())
174 printOperand(MI, OpNo, O);
175 else
Craig Topperbc219812012-02-07 02:50:20 +0000176 llvm_unreachable("Unimplemented");
Tony Linthicumb4b54152011-12-12 21:14:40 +0000177
178 if (Offset.isImm()) {
179 if (Offset.getImm())
180 O << " + #" << Offset.getImm();
181 }
182 else
Craig Topperbc219812012-02-07 02:50:20 +0000183 llvm_unreachable("Unimplemented");
Tony Linthicumb4b54152011-12-12 21:14:40 +0000184
185 return false;
186}
187
188void HexagonAsmPrinter::printPredicateOperand(const MachineInstr *MI,
189 unsigned OpNo,
190 raw_ostream &O) {
Craig Topperbc219812012-02-07 02:50:20 +0000191 llvm_unreachable("Unimplemented");
Tony Linthicumb4b54152011-12-12 21:14:40 +0000192}
193
194
195/// printMachineInstruction -- Print out a single Hexagon MI in Darwin syntax to
196/// the current output stream.
197///
198void HexagonAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Chandler Carruthd410eab2012-04-23 18:25:57 +0000199 MCInst MCI;
Tony Linthicumb4b54152011-12-12 21:14:40 +0000200
Chandler Carruthd410eab2012-04-23 18:25:57 +0000201 HexagonLowerToMC(MI, MCI, *this);
202 OutStreamer.EmitInstruction(MCI);
Tony Linthicumb4b54152011-12-12 21:14:40 +0000203
Tony Linthicumb4b54152011-12-12 21:14:40 +0000204 return;
205}
206
207/// PrintUnmangledNameSafely - Print out the printable characters in the name.
208/// Don't print things like \n or \0.
209// static void PrintUnmangledNameSafely(const Value *V, raw_ostream &OS) {
210// for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
211// Name != E; ++Name)
212// if (isprint(*Name))
213// OS << *Name;
214// }
215
216
217void HexagonAsmPrinter::printAddrModeBasePlusOffset(const MachineInstr *MI,
218 int OpNo, raw_ostream &O) {
219 const MachineOperand &MO1 = MI->getOperand(OpNo);
220 const MachineOperand &MO2 = MI->getOperand(OpNo+1);
221
Evandro Menezese5041e62012-04-12 17:55:53 +0000222 O << HexagonInstPrinter::getRegisterName(MO1.getReg())
Tony Linthicumb4b54152011-12-12 21:14:40 +0000223 << " + #"
224 << MO2.getImm();
225}
226
227
228void HexagonAsmPrinter::printGlobalOperand(const MachineInstr *MI, int OpNo,
229 raw_ostream &O) {
230 const MachineOperand &MO = MI->getOperand(OpNo);
231 assert( (MO.getType() == MachineOperand::MO_GlobalAddress) &&
232 "Expecting global address");
233
234 O << *Mang->getSymbol(MO.getGlobal());
235 if (MO.getOffset() != 0) {
236 O << " + ";
237 O << MO.getOffset();
238 }
239}
240
241void HexagonAsmPrinter::printJumpTable(const MachineInstr *MI, int OpNo,
242 raw_ostream &O) {
243 const MachineOperand &MO = MI->getOperand(OpNo);
244 assert( (MO.getType() == MachineOperand::MO_JumpTableIndex) &&
Chandler Carruthd410eab2012-04-23 18:25:57 +0000245 "Expecting jump table index");
Tony Linthicumb4b54152011-12-12 21:14:40 +0000246
247 // Hexagon_TODO: Do we need name mangling?
248 O << *GetJTISymbol(MO.getIndex());
249}
250
Evandro Menezese5041e62012-04-12 17:55:53 +0000251void HexagonAsmPrinter::printConstantPool(const MachineInstr *MI, int OpNo,
Chandler Carruthd410eab2012-04-23 18:25:57 +0000252 raw_ostream &O) {
Evandro Menezese5041e62012-04-12 17:55:53 +0000253 const MachineOperand &MO = MI->getOperand(OpNo);
254 assert( (MO.getType() == MachineOperand::MO_ConstantPoolIndex) &&
Chandler Carruthd410eab2012-04-23 18:25:57 +0000255 "Expecting constant pool index");
Evandro Menezese5041e62012-04-12 17:55:53 +0000256
257 // Hexagon_TODO: Do we need name mangling?
258 O << *GetCPISymbol(MO.getIndex());
259}
260
261static MCInstPrinter *createHexagonMCInstPrinter(const Target &T,
262 unsigned SyntaxVariant,
263 const MCAsmInfo &MAI,
264 const MCInstrInfo &MII,
265 const MCRegisterInfo &MRI,
266 const MCSubtargetInfo &STI) {
267 if (SyntaxVariant == 0)
268 return(new HexagonInstPrinter(MAI, MII, MRI));
269 else
270 return NULL;
271}
272
Tony Linthicumb4b54152011-12-12 21:14:40 +0000273extern "C" void LLVMInitializeHexagonAsmPrinter() {
274 RegisterAsmPrinter<HexagonAsmPrinter> X(TheHexagonTarget);
Evandro Menezese5041e62012-04-12 17:55:53 +0000275
276 TargetRegistry::RegisterMCInstPrinter(TheHexagonTarget,
277 createHexagonMCInstPrinter);
Tony Linthicumb4b54152011-12-12 21:14:40 +0000278}