blob: 61839b6ba896850b00d223a19af97f19b7781eaf [file] [log] [blame]
Tim Northover72062f52013-01-31 12:12:40 +00001//===-- AArch64AsmPrinter.cpp - Print machine code to an AArch64 .s file --===//
2//
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 GAS-format AArch64 assembly language.
12//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "asm-printer"
16#include "AArch64AsmPrinter.h"
17#include "InstPrinter/AArch64InstPrinter.h"
18#include "llvm/DebugInfo.h"
19#include "llvm/ADT/SmallString.h"
20#include "llvm/CodeGen/MachineConstantPool.h"
21#include "llvm/CodeGen/MachineModuleInfoImpls.h"
22#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
23#include "llvm/MC/MCAsmInfo.h"
24#include "llvm/MC/MCInst.h"
25#include "llvm/MC/MCSymbol.h"
26#include "llvm/Support/TargetRegistry.h"
27#include "llvm/Target/Mangler.h"
28
29using namespace llvm;
30
31MachineLocation
32AArch64AsmPrinter::getDebugValueLocation(const MachineInstr *MI) const {
33 // See emitFrameIndexDebugValue in InstrInfo for where this instruction is
34 // expected to be created.
35 assert(MI->getNumOperands() == 4 && MI->getOperand(0).isReg()
36 && MI->getOperand(1).isImm() && "unexpected custom DBG_VALUE");
Tim Northoverdfe076a2013-02-05 13:24:56 +000037 return MachineLocation(MI->getOperand(0).getReg(),
38 MI->getOperand(1).getImm());
Tim Northover72062f52013-01-31 12:12:40 +000039}
40
41/// Try to print a floating-point register as if it belonged to a specified
42/// register-class. For example the inline asm operand modifier "b" requires its
43/// argument to be printed as "bN".
44static bool printModifiedFPRAsmOperand(const MachineOperand &MO,
45 const TargetRegisterInfo *TRI,
46 const TargetRegisterClass &RegClass,
47 raw_ostream &O) {
48 if (!MO.isReg())
49 return true;
50
51 for (MCRegAliasIterator AR(MO.getReg(), TRI, true); AR.isValid(); ++AR) {
52 if (RegClass.contains(*AR)) {
53 O << AArch64InstPrinter::getRegisterName(*AR);
54 return false;
55 }
56 }
57 return true;
58}
59
60/// Implements the 'w' and 'x' inline asm operand modifiers, which print a GPR
61/// with the obvious type and an immediate 0 as either wzr or xzr.
62static bool printModifiedGPRAsmOperand(const MachineOperand &MO,
63 const TargetRegisterInfo *TRI,
64 const TargetRegisterClass &RegClass,
65 raw_ostream &O) {
66 char Prefix = &RegClass == &AArch64::GPR32RegClass ? 'w' : 'x';
67
68 if (MO.isImm() && MO.getImm() == 0) {
69 O << Prefix << "zr";
70 return false;
71 } else if (MO.isReg()) {
72 if (MO.getReg() == AArch64::XSP || MO.getReg() == AArch64::WSP) {
73 O << (Prefix == 'x' ? "sp" : "wsp");
74 return false;
75 }
76
77 for (MCRegAliasIterator AR(MO.getReg(), TRI, true); AR.isValid(); ++AR) {
78 if (RegClass.contains(*AR)) {
79 O << AArch64InstPrinter::getRegisterName(*AR);
80 return false;
81 }
82 }
83 }
84
85 return true;
86}
87
88bool AArch64AsmPrinter::printSymbolicAddress(const MachineOperand &MO,
89 bool PrintImmediatePrefix,
90 StringRef Suffix, raw_ostream &O) {
91 StringRef Name;
92 StringRef Modifier;
93 switch (MO.getType()) {
Tim Northoverdfe076a2013-02-05 13:24:56 +000094 default:
95 llvm_unreachable("Unexpected operand for symbolic address constraint");
Tim Northover72062f52013-01-31 12:12:40 +000096 case MachineOperand::MO_GlobalAddress:
97 Name = Mang->getSymbol(MO.getGlobal())->getName();
98
99 // Global variables may be accessed either via a GOT or in various fun and
100 // interesting TLS-model specific ways. Set the prefix modifier as
101 // appropriate here.
102 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(MO.getGlobal())) {
103 Reloc::Model RelocM = TM.getRelocationModel();
104 if (GV->isThreadLocal()) {
105 switch (TM.getTLSModel(GV)) {
106 case TLSModel::GeneralDynamic:
107 Modifier = "tlsdesc";
108 break;
109 case TLSModel::LocalDynamic:
110 Modifier = "dtprel";
111 break;
112 case TLSModel::InitialExec:
113 Modifier = "gottprel";
114 break;
115 case TLSModel::LocalExec:
116 Modifier = "tprel";
117 break;
118 }
119 } else if (Subtarget->GVIsIndirectSymbol(GV, RelocM)) {
120 Modifier = "got";
121 }
122 }
123 break;
124 case MachineOperand::MO_BlockAddress:
125 Name = GetBlockAddressSymbol(MO.getBlockAddress())->getName();
126 break;
127 case MachineOperand::MO_ExternalSymbol:
128 Name = MO.getSymbolName();
129 break;
130 case MachineOperand::MO_ConstantPoolIndex:
131 Name = GetCPISymbol(MO.getIndex())->getName();
132 break;
133 }
134
135 // Some instructions (notably ADRP) don't take the # prefix for
136 // immediates. Only print it if asked to.
137 if (PrintImmediatePrefix)
138 O << '#';
139
140 // Only need the joining "_" if both the prefix and the suffix are
141 // non-null. This little block simply takes care of the four possibly
142 // combinations involved there.
143 if (Modifier == "" && Suffix == "")
144 O << Name;
145 else if (Modifier == "" && Suffix != "")
146 O << ":" << Suffix << ':' << Name;
147 else if (Modifier != "" && Suffix == "")
148 O << ":" << Modifier << ':' << Name;
149 else
150 O << ":" << Modifier << '_' << Suffix << ':' << Name;
151
152 return false;
153}
154
155bool AArch64AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
156 unsigned AsmVariant,
157 const char *ExtraCode, raw_ostream &O) {
158 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
159 if (!ExtraCode || !ExtraCode[0]) {
160 // There's actually no operand modifier, which leads to a slightly eclectic
161 // set of behaviour which we have to handle here.
162 const MachineOperand &MO = MI->getOperand(OpNum);
163 switch (MO.getType()) {
164 default:
165 llvm_unreachable("Unexpected operand for inline assembly");
166 case MachineOperand::MO_Register:
167 // GCC prints the unmodified operand of a 'w' constraint as the vector
168 // register. Technically, we could allocate the argument as a VPR128, but
169 // that leads to extremely dodgy copies being generated to get the data
170 // there.
171 if (printModifiedFPRAsmOperand(MO, TRI, AArch64::VPR128RegClass, O))
172 O << AArch64InstPrinter::getRegisterName(MO.getReg());
173 break;
174 case MachineOperand::MO_Immediate:
175 O << '#' << MO.getImm();
176 break;
177 case MachineOperand::MO_FPImmediate:
178 assert(MO.getFPImm()->isExactlyValue(0.0) && "Only FP 0.0 expected");
179 O << "#0.0";
180 break;
181 case MachineOperand::MO_BlockAddress:
182 case MachineOperand::MO_ConstantPoolIndex:
183 case MachineOperand::MO_GlobalAddress:
184 case MachineOperand::MO_ExternalSymbol:
185 return printSymbolicAddress(MO, false, "", O);
186 }
187 return false;
188 }
189
190 // We have a real modifier to handle.
191 switch(ExtraCode[0]) {
192 default:
193 // See if this is a generic operand
194 return AsmPrinter::PrintAsmOperand(MI, OpNum, AsmVariant, ExtraCode, O);
195 case 'c': // Don't print "#" before an immediate operand.
196 if (!MI->getOperand(OpNum).isImm())
197 return true;
198 O << MI->getOperand(OpNum).getImm();
199 return false;
200 case 'w':
201 // Output 32-bit general register operand, constant zero as wzr, or stack
202 // pointer as wsp. Ignored when used with other operand types.
203 return printModifiedGPRAsmOperand(MI->getOperand(OpNum), TRI,
204 AArch64::GPR32RegClass, O);
205 case 'x':
206 // Output 64-bit general register operand, constant zero as xzr, or stack
207 // pointer as sp. Ignored when used with other operand types.
208 return printModifiedGPRAsmOperand(MI->getOperand(OpNum), TRI,
209 AArch64::GPR64RegClass, O);
210 case 'H':
211 // Output higher numbered of a 64-bit general register pair
212 case 'Q':
213 // Output least significant register of a 64-bit general register pair
214 case 'R':
215 // Output most significant register of a 64-bit general register pair
216
217 // FIXME note: these three operand modifiers will require, to some extent,
218 // adding a paired GPR64 register class. Initial investigation suggests that
219 // assertions are hit unless it has a type and is made legal for that type
220 // in ISelLowering. After that step is made, the number of modifications
221 // needed explodes (operation legality, calling conventions, stores, reg
222 // copies ...).
223 llvm_unreachable("FIXME: Unimplemented register pairs");
224 case 'b':
225 // Output 8-bit FP/SIMD scalar register operand, prefixed with b.
226 return printModifiedFPRAsmOperand(MI->getOperand(OpNum), TRI,
227 AArch64::FPR8RegClass, O);
228 case 'h':
229 // Output 16-bit FP/SIMD scalar register operand, prefixed with h.
230 return printModifiedFPRAsmOperand(MI->getOperand(OpNum), TRI,
231 AArch64::FPR16RegClass, O);
232 case 's':
233 // Output 32-bit FP/SIMD scalar register operand, prefixed with s.
234 return printModifiedFPRAsmOperand(MI->getOperand(OpNum), TRI,
235 AArch64::FPR32RegClass, O);
236 case 'd':
237 // Output 64-bit FP/SIMD scalar register operand, prefixed with d.
238 return printModifiedFPRAsmOperand(MI->getOperand(OpNum), TRI,
239 AArch64::FPR64RegClass, O);
240 case 'q':
241 // Output 128-bit FP/SIMD scalar register operand, prefixed with q.
242 return printModifiedFPRAsmOperand(MI->getOperand(OpNum), TRI,
243 AArch64::FPR128RegClass, O);
244 case 'A':
245 // Output symbolic address with appropriate relocation modifier (also
246 // suitable for ADRP).
247 return printSymbolicAddress(MI->getOperand(OpNum), false, "", O);
248 case 'L':
249 // Output bits 11:0 of symbolic address with appropriate :lo12: relocation
250 // modifier.
251 return printSymbolicAddress(MI->getOperand(OpNum), true, "lo12", O);
252 case 'G':
253 // Output bits 23:12 of symbolic address with appropriate :hi12: relocation
254 // modifier (currently only for TLS local exec).
255 return printSymbolicAddress(MI->getOperand(OpNum), true, "hi12", O);
256 }
257
258
259}
260
261bool AArch64AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
262 unsigned OpNum,
263 unsigned AsmVariant,
264 const char *ExtraCode,
265 raw_ostream &O) {
266 // Currently both the memory constraints (m and Q) behave the same and amount
267 // to the address as a single register. In future, we may allow "m" to provide
268 // both a base and an offset.
269 const MachineOperand &MO = MI->getOperand(OpNum);
270 assert(MO.isReg() && "unexpected inline assembly memory operand");
271 O << '[' << AArch64InstPrinter::getRegisterName(MO.getReg()) << ']';
272 return false;
273}
274
275void AArch64AsmPrinter::PrintDebugValueComment(const MachineInstr *MI,
276 raw_ostream &OS) {
277 unsigned NOps = MI->getNumOperands();
278 assert(NOps==4);
279 OS << '\t' << MAI->getCommentString() << "DEBUG_VALUE: ";
280 // cast away const; DIetc do not take const operands for some reason.
281 DIVariable V(const_cast<MDNode *>(MI->getOperand(NOps-1).getMetadata()));
282 OS << V.getName();
283 OS << " <- ";
284 // Frame address. Currently handles register +- offset only.
285 assert(MI->getOperand(0).isReg() && MI->getOperand(1).isImm());
286 OS << '[' << AArch64InstPrinter::getRegisterName(MI->getOperand(0).getReg());
287 OS << '+' << MI->getOperand(1).getImm();
288 OS << ']';
289 OS << "+" << MI->getOperand(NOps - 2).getImm();
290}
291
292
293#include "AArch64GenMCPseudoLowering.inc"
294
295void AArch64AsmPrinter::EmitInstruction(const MachineInstr *MI) {
296 // Do any auto-generated pseudo lowerings.
297 if (emitPseudoExpansionLowering(OutStreamer, MI))
298 return;
299
300 switch (MI->getOpcode()) {
301 case AArch64::CONSTPOOL_ENTRY: {
302 unsigned LabelId = (unsigned)MI->getOperand(0).getImm();
303 unsigned CPIdx = (unsigned)MI->getOperand(1).getIndex();
304
305 OutStreamer.EmitLabel(GetCPISymbol(LabelId));
306
307 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPIdx];
308 if (MCPE.isMachineConstantPoolEntry())
309 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
310 else
311 EmitGlobalConstant(MCPE.Val.ConstVal);
312
313 return;
314 }
315 case AArch64::DBG_VALUE: {
316 if (isVerbose() && OutStreamer.hasRawTextSupport()) {
317 SmallString<128> TmpStr;
318 raw_svector_ostream OS(TmpStr);
319 PrintDebugValueComment(MI, OS);
320 OutStreamer.EmitRawText(StringRef(OS.str()));
321 }
322 return;
323 }
324 }
325
326 MCInst TmpInst;
327 LowerAArch64MachineInstrToMCInst(MI, TmpInst, *this);
328 OutStreamer.EmitInstruction(TmpInst);
329}
330
331void AArch64AsmPrinter::EmitEndOfAsmFile(Module &M) {
332 if (Subtarget->isTargetELF()) {
333 const TargetLoweringObjectFileELF &TLOFELF =
334 static_cast<const TargetLoweringObjectFileELF &>(getObjFileLowering());
335
336 MachineModuleInfoELF &MMIELF = MMI->getObjFileInfo<MachineModuleInfoELF>();
337
338 // Output stubs for external and common global variables.
339 MachineModuleInfoELF::SymbolListTy Stubs = MMIELF.GetGVStubList();
340 if (!Stubs.empty()) {
341 OutStreamer.SwitchSection(TLOFELF.getDataRelSection());
342 const DataLayout *TD = TM.getDataLayout();
343
344 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
345 OutStreamer.EmitLabel(Stubs[i].first);
346 OutStreamer.EmitSymbolValue(Stubs[i].second.getPointer(),
347 TD->getPointerSize(0), 0);
348 }
349 Stubs.clear();
350 }
351 }
352}
353
354bool AArch64AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
355 MCP = MF.getConstantPool();
356 return AsmPrinter::runOnMachineFunction(MF);
357}
358
359// Force static initialization.
360extern "C" void LLVMInitializeAArch64AsmPrinter() {
361 RegisterAsmPrinter<AArch64AsmPrinter> X(TheAArch64Target);
362}
363