blob: 2ab8b635919c0427439fd15ce3048b6397b048b9 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- ARMAsmPrinter.cpp - ARM LLVM assembly writer ----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006// 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 ARM assembly language.
12//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "asm-printer"
16#include "ARM.h"
17#include "ARMTargetMachine.h"
18#include "ARMAddressingModes.h"
19#include "ARMConstantPoolValue.h"
20#include "ARMMachineFunctionInfo.h"
21#include "llvm/Constants.h"
22#include "llvm/Module.h"
23#include "llvm/CodeGen/AsmPrinter.h"
24#include "llvm/CodeGen/DwarfWriter.h"
25#include "llvm/CodeGen/MachineModuleInfo.h"
26#include "llvm/CodeGen/MachineFunctionPass.h"
27#include "llvm/CodeGen/MachineJumpTableInfo.h"
28#include "llvm/Target/TargetAsmInfo.h"
29#include "llvm/Target/TargetData.h"
30#include "llvm/Target/TargetMachine.h"
31#include "llvm/Target/TargetOptions.h"
Evan Chengf1012ce2008-08-08 06:56:16 +000032#include "llvm/ADT/SmallPtrSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033#include "llvm/ADT/Statistic.h"
34#include "llvm/ADT/StringExtras.h"
35#include "llvm/Support/Compiler.h"
36#include "llvm/Support/Mangler.h"
37#include "llvm/Support/MathExtras.h"
38#include <cctype>
39using namespace llvm;
40
41STATISTIC(EmittedInsts, "Number of machine instrs printed");
42
43namespace {
44 struct VISIBILITY_HIDDEN ARMAsmPrinter : public AsmPrinter {
45 ARMAsmPrinter(std::ostream &O, TargetMachine &TM, const TargetAsmInfo *T)
Dale Johannesen2f85df12008-07-09 21:20:54 +000046 : AsmPrinter(O, TM, T), DW(O, this, T), MMI(NULL), AFI(NULL),
47 InCPMode(false) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000048 Subtarget = &TM.getSubtarget<ARMSubtarget>();
49 }
50
51 DwarfWriter DW;
Dale Johannesen2f85df12008-07-09 21:20:54 +000052 MachineModuleInfo *MMI;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053
54 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
55 /// make the right decision when printing asm code for different targets.
56 const ARMSubtarget *Subtarget;
57
58 /// AFI - Keep a pointer to ARMFunctionInfo for the current
59 /// MachineFunction
60 ARMFunctionInfo *AFI;
61
62 /// We name each basic block in a Function with a unique number, so
63 /// that we can consistently refer to them later. This is cleared
64 /// at the beginning of each call to runOnMachineFunction().
65 ///
66 typedef std::map<const Value *, unsigned> ValueMapTy;
67 ValueMapTy NumberForBB;
68
Evan Chengf1012ce2008-08-08 06:56:16 +000069 /// GVNonLazyPtrs - Keeps the set of GlobalValues that require
70 /// non-lazy-pointers for indirect access.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000071 std::set<std::string> GVNonLazyPtrs;
72
Evan Chengf1012ce2008-08-08 06:56:16 +000073 /// FnStubs - Keeps the set of external function GlobalAddresses that the
74 /// asm printer should generate stubs for.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000075 std::set<std::string> FnStubs;
76
Evan Chengf1012ce2008-08-08 06:56:16 +000077 /// PCRelGVs - Keeps the set of GlobalValues used in pc relative
78 /// constantpool.
79 SmallPtrSet<const GlobalValue*, 8> PCRelGVs;
80
Dan Gohmanf17a25c2007-07-18 16:29:46 +000081 /// True if asm printer is printing a series of CONSTPOOL_ENTRY.
82 bool InCPMode;
83
84 virtual const char *getPassName() const {
85 return "ARM Assembly Printer";
86 }
87
88 void printOperand(const MachineInstr *MI, int opNum,
89 const char *Modifier = 0);
90 void printSOImmOperand(const MachineInstr *MI, int opNum);
91 void printSOImm2PartOperand(const MachineInstr *MI, int opNum);
92 void printSORegOperand(const MachineInstr *MI, int opNum);
93 void printAddrMode2Operand(const MachineInstr *MI, int OpNo);
94 void printAddrMode2OffsetOperand(const MachineInstr *MI, int OpNo);
95 void printAddrMode3Operand(const MachineInstr *MI, int OpNo);
96 void printAddrMode3OffsetOperand(const MachineInstr *MI, int OpNo);
97 void printAddrMode4Operand(const MachineInstr *MI, int OpNo,
98 const char *Modifier = 0);
99 void printAddrMode5Operand(const MachineInstr *MI, int OpNo,
100 const char *Modifier = 0);
101 void printAddrModePCOperand(const MachineInstr *MI, int OpNo,
102 const char *Modifier = 0);
103 void printThumbAddrModeRROperand(const MachineInstr *MI, int OpNo);
104 void printThumbAddrModeRI5Operand(const MachineInstr *MI, int OpNo,
105 unsigned Scale);
106 void printThumbAddrModeS1Operand(const MachineInstr *MI, int OpNo);
107 void printThumbAddrModeS2Operand(const MachineInstr *MI, int OpNo);
108 void printThumbAddrModeS4Operand(const MachineInstr *MI, int OpNo);
109 void printThumbAddrModeSPOperand(const MachineInstr *MI, int OpNo);
110 void printPredicateOperand(const MachineInstr *MI, int opNum);
111 void printSBitModifierOperand(const MachineInstr *MI, int opNum);
112 void printPCLabel(const MachineInstr *MI, int opNum);
113 void printRegisterList(const MachineInstr *MI, int opNum);
114 void printCPInstOperand(const MachineInstr *MI, int opNum,
115 const char *Modifier);
116 void printJTBlockOperand(const MachineInstr *MI, int opNum);
117
118 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
119 unsigned AsmVariant, const char *ExtraCode);
120
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000121 void printModuleLevelGV(const GlobalVariable* GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122 bool printInstruction(const MachineInstr *MI); // autogenerated.
123 void printMachineInstruction(const MachineInstr *MI);
124 bool runOnMachineFunction(MachineFunction &F);
125 bool doInitialization(Module &M);
126 bool doFinalization(Module &M);
127
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000128 /// getSectionForFunction - Return the section that we should emit the
129 /// specified function body into.
130 virtual std::string getSectionForFunction(const Function &F) const;
131
Evan Chengf1012ce2008-08-08 06:56:16 +0000132 /// EmitMachineConstantPoolValue - Print a machine constantpool value to
133 /// the .s file.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000134 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
135 printDataDirective(MCPV->getType());
136
Evan Chengf1012ce2008-08-08 06:56:16 +0000137 ARMConstantPoolValue *ACPV = static_cast<ARMConstantPoolValue*>(MCPV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000138 GlobalValue *GV = ACPV->getGV();
139 std::string Name = GV ? Mang->getValueName(GV) : TAI->getGlobalPrefix();
140 if (!GV)
141 Name += ACPV->getSymbol();
142 if (ACPV->isNonLazyPointer()) {
143 GVNonLazyPtrs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000144 printSuffixedName(Name, "$non_lazy_ptr");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000145 } else if (ACPV->isStub()) {
146 FnStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000147 printSuffixedName(Name, "$stub");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000148 } else
149 O << Name;
150 if (ACPV->hasModifier()) O << "(" << ACPV->getModifier() << ")";
151 if (ACPV->getPCAdjustment() != 0) {
152 O << "-(" << TAI->getPrivateGlobalPrefix() << "PC"
153 << utostr(ACPV->getLabelId())
154 << "+" << (unsigned)ACPV->getPCAdjustment();
155 if (ACPV->mustAddCurrentAddress())
156 O << "-.";
157 O << ")";
158 }
159 O << "\n";
160
161 // If the constant pool value is a extern weak symbol, remember to emit
162 // the weak reference.
163 if (GV && GV->hasExternalWeakLinkage())
164 ExtWeakSymbols.insert(GV);
165 }
166
167 void getAnalysisUsage(AnalysisUsage &AU) const {
Gordon Henriksen76e4b612007-09-30 13:39:29 +0000168 AsmPrinter::getAnalysisUsage(AU);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000169 AU.setPreservesAll();
170 AU.addRequired<MachineModuleInfo>();
171 }
172 };
173} // end of anonymous namespace
174
175#include "ARMGenAsmWriter.inc"
176
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000177// Substitute old hook with new one temporary
178std::string ARMAsmPrinter::getSectionForFunction(const Function &F) const {
179 return TAI->SectionForGlobal(&F);
180}
181
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000182/// runOnMachineFunction - This uses the printInstruction()
183/// method to print assembly for each instruction.
184///
185bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
186 AFI = MF.getInfo<ARMFunctionInfo>();
187
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000188 SetupMachineFunction(MF);
189 O << "\n";
190
191 // NOTE: we don't print out constant pools here, they are handled as
192 // instructions.
193
194 O << "\n";
195 // Print out labels for the function.
196 const Function *F = MF.getFunction();
197 switch (F->getLinkage()) {
198 default: assert(0 && "Unknown linkage type!");
199 case Function::InternalLinkage:
200 SwitchToTextSection("\t.text", F);
201 break;
202 case Function::ExternalLinkage:
203 SwitchToTextSection("\t.text", F);
204 O << "\t.globl\t" << CurrentFnName << "\n";
205 break;
206 case Function::WeakLinkage:
207 case Function::LinkOnceLinkage:
208 if (Subtarget->isTargetDarwin()) {
209 SwitchToTextSection(
210 ".section __TEXT,__textcoal_nt,coalesced,pure_instructions", F);
211 O << "\t.globl\t" << CurrentFnName << "\n";
212 O << "\t.weak_definition\t" << CurrentFnName << "\n";
213 } else {
214 O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
215 }
216 break;
217 }
218
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000219 printVisibility(CurrentFnName, F->getVisibility());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000220
221 if (AFI->isThumbFunction()) {
222 EmitAlignment(1, F, AFI->getAlign());
223 O << "\t.code\t16\n";
224 O << "\t.thumb_func";
225 if (Subtarget->isTargetDarwin())
226 O << "\t" << CurrentFnName;
227 O << "\n";
228 InCPMode = false;
229 } else
230 EmitAlignment(2, F);
231
232 O << CurrentFnName << ":\n";
233 // Emit pre-function debug information.
234 DW.BeginFunction(&MF);
235
Bill Wendling43a78162008-01-28 09:15:03 +0000236 if (Subtarget->isTargetDarwin()) {
237 // If the function is empty, then we need to emit *something*. Otherwise,
238 // the function's label might be associated with something that it wasn't
239 // meant to be associated with. We emit a noop in this situation.
240 MachineFunction::iterator I = MF.begin();
241
242 if (++I == MF.end() && MF.front().empty())
243 O << "\tnop\n";
244 }
245
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246 // Print out code for the function.
247 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
248 I != E; ++I) {
249 // Print a label for the basic block.
250 if (I != MF.begin()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000251 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000252 O << '\n';
253 }
254 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
255 II != E; ++II) {
256 // Print the assembly for the instruction.
257 printMachineInstruction(II);
258 }
259 }
260
261 if (TAI->hasDotTypeDotSizeDirective())
262 O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
263
264 // Emit post-function debug information.
265 DW.EndFunction();
266
267 return false;
268}
269
270void ARMAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
271 const char *Modifier) {
272 const MachineOperand &MO = MI->getOperand(opNum);
273 switch (MO.getType()) {
274 case MachineOperand::MO_Register:
Dan Gohman1e57df32008-02-10 18:45:23 +0000275 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Bill Wendling8eeb9792008-02-26 21:11:01 +0000276 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000277 else
278 assert(0 && "not implemented");
279 break;
280 case MachineOperand::MO_Immediate: {
281 if (!Modifier || strcmp(Modifier, "no_hash") != 0)
282 O << "#";
283
Chris Lattnera96056a2007-12-30 20:49:49 +0000284 O << (int)MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000285 break;
286 }
287 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner6017d482007-12-30 23:10:15 +0000288 printBasicBlockLabel(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000289 return;
290 case MachineOperand::MO_GlobalAddress: {
291 bool isCallOp = Modifier && !strcmp(Modifier, "call");
292 GlobalValue *GV = MO.getGlobal();
293 std::string Name = Mang->getValueName(GV);
294 bool isExt = (GV->isDeclaration() || GV->hasWeakLinkage() ||
295 GV->hasLinkOnceLinkage());
296 if (isExt && isCallOp && Subtarget->isTargetDarwin() &&
297 TM.getRelocationModel() != Reloc::Static) {
Dale Johannesena21b5202008-05-19 21:38:18 +0000298 printSuffixedName(Name, "$stub");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000299 FnStubs.insert(Name);
300 } else
301 O << Name;
302
303 if (MO.getOffset() > 0)
304 O << '+' << MO.getOffset();
305 else if (MO.getOffset() < 0)
306 O << MO.getOffset();
307
308 if (isCallOp && Subtarget->isTargetELF() &&
309 TM.getRelocationModel() == Reloc::PIC_)
310 O << "(PLT)";
311 if (GV->hasExternalWeakLinkage())
312 ExtWeakSymbols.insert(GV);
313 break;
314 }
315 case MachineOperand::MO_ExternalSymbol: {
316 bool isCallOp = Modifier && !strcmp(Modifier, "call");
317 std::string Name(TAI->getGlobalPrefix());
318 Name += MO.getSymbolName();
319 if (isCallOp && Subtarget->isTargetDarwin() &&
320 TM.getRelocationModel() != Reloc::Static) {
Dale Johannesena21b5202008-05-19 21:38:18 +0000321 printSuffixedName(Name, "$stub");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000322 FnStubs.insert(Name);
323 } else
324 O << Name;
325 if (isCallOp && Subtarget->isTargetELF() &&
326 TM.getRelocationModel() == Reloc::PIC_)
327 O << "(PLT)";
328 break;
329 }
330 case MachineOperand::MO_ConstantPoolIndex:
Evan Cheng477013c2007-10-14 05:57:21 +0000331 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000332 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000333 break;
334 case MachineOperand::MO_JumpTableIndex:
Evan Cheng477013c2007-10-14 05:57:21 +0000335 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000336 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000337 break;
338 default:
339 O << "<unknown operand type>"; abort (); break;
340 }
341}
342
343static void printSOImm(std::ostream &O, int64_t V, const TargetAsmInfo *TAI) {
344 assert(V < (1 << 12) && "Not a valid so_imm value!");
345 unsigned Imm = ARM_AM::getSOImmValImm(V);
346 unsigned Rot = ARM_AM::getSOImmValRot(V);
347
348 // Print low-level immediate formation info, per
349 // A5.1.3: "Data-processing operands - Immediate".
350 if (Rot) {
351 O << "#" << Imm << ", " << Rot;
352 // Pretty printed version.
353 O << ' ' << TAI->getCommentString() << ' ' << (int)ARM_AM::rotr32(Imm, Rot);
354 } else {
355 O << "#" << Imm;
356 }
357}
358
359/// printSOImmOperand - SOImm is 4-bit rotate amount in bits 8-11 with 8-bit
360/// immediate in bits 0-7.
361void ARMAsmPrinter::printSOImmOperand(const MachineInstr *MI, int OpNum) {
362 const MachineOperand &MO = MI->getOperand(OpNum);
363 assert(MO.isImmediate() && "Not a valid so_imm value!");
Chris Lattnera96056a2007-12-30 20:49:49 +0000364 printSOImm(O, MO.getImm(), TAI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000365}
366
367/// printSOImm2PartOperand - SOImm is broken into two pieces using a mov
368/// followed by a or to materialize.
369void ARMAsmPrinter::printSOImm2PartOperand(const MachineInstr *MI, int OpNum) {
370 const MachineOperand &MO = MI->getOperand(OpNum);
371 assert(MO.isImmediate() && "Not a valid so_imm value!");
Chris Lattnera96056a2007-12-30 20:49:49 +0000372 unsigned V1 = ARM_AM::getSOImmTwoPartFirst(MO.getImm());
373 unsigned V2 = ARM_AM::getSOImmTwoPartSecond(MO.getImm());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000374 printSOImm(O, ARM_AM::getSOImmVal(V1), TAI);
375 O << "\n\torr";
376 printPredicateOperand(MI, 2);
377 O << " ";
378 printOperand(MI, 0);
379 O << ", ";
380 printOperand(MI, 0);
381 O << ", ";
382 printSOImm(O, ARM_AM::getSOImmVal(V2), TAI);
383}
384
385// so_reg is a 4-operand unit corresponding to register forms of the A5.1
386// "Addressing Mode 1 - Data-processing operands" forms. This includes:
387// REG 0 0 - e.g. R5
388// REG REG 0,SH_OPC - e.g. R5, ROR R3
389// REG 0 IMM,SH_OPC - e.g. R5, LSL #3
390void ARMAsmPrinter::printSORegOperand(const MachineInstr *MI, int Op) {
391 const MachineOperand &MO1 = MI->getOperand(Op);
392 const MachineOperand &MO2 = MI->getOperand(Op+1);
393 const MachineOperand &MO3 = MI->getOperand(Op+2);
394
Dan Gohman1e57df32008-02-10 18:45:23 +0000395 assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
Bill Wendling8eeb9792008-02-26 21:11:01 +0000396 O << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000397
398 // Print the shift opc.
399 O << ", "
Chris Lattnera96056a2007-12-30 20:49:49 +0000400 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(MO3.getImm()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000401 << " ";
402
403 if (MO2.getReg()) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000404 assert(TargetRegisterInfo::isPhysicalRegister(MO2.getReg()));
Bill Wendling8eeb9792008-02-26 21:11:01 +0000405 O << TM.getRegisterInfo()->get(MO2.getReg()).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000406 assert(ARM_AM::getSORegOffset(MO3.getImm()) == 0);
407 } else {
408 O << "#" << ARM_AM::getSORegOffset(MO3.getImm());
409 }
410}
411
412void ARMAsmPrinter::printAddrMode2Operand(const MachineInstr *MI, int Op) {
413 const MachineOperand &MO1 = MI->getOperand(Op);
414 const MachineOperand &MO2 = MI->getOperand(Op+1);
415 const MachineOperand &MO3 = MI->getOperand(Op+2);
416
417 if (!MO1.isRegister()) { // FIXME: This is for CP entries, but isn't right.
418 printOperand(MI, Op);
419 return;
420 }
421
Bill Wendling8eeb9792008-02-26 21:11:01 +0000422 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000423
424 if (!MO2.getReg()) {
425 if (ARM_AM::getAM2Offset(MO3.getImm())) // Don't print +0.
426 O << ", #"
427 << (char)ARM_AM::getAM2Op(MO3.getImm())
428 << ARM_AM::getAM2Offset(MO3.getImm());
429 O << "]";
430 return;
431 }
432
433 O << ", "
434 << (char)ARM_AM::getAM2Op(MO3.getImm())
Bill Wendling8eeb9792008-02-26 21:11:01 +0000435 << TM.getRegisterInfo()->get(MO2.getReg()).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000436
437 if (unsigned ShImm = ARM_AM::getAM2Offset(MO3.getImm()))
438 O << ", "
Chris Lattnera96056a2007-12-30 20:49:49 +0000439 << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO3.getImm()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000440 << " #" << ShImm;
441 O << "]";
442}
443
444void ARMAsmPrinter::printAddrMode2OffsetOperand(const MachineInstr *MI, int Op){
445 const MachineOperand &MO1 = MI->getOperand(Op);
446 const MachineOperand &MO2 = MI->getOperand(Op+1);
447
448 if (!MO1.getReg()) {
449 unsigned ImmOffs = ARM_AM::getAM2Offset(MO2.getImm());
450 assert(ImmOffs && "Malformed indexed load / store!");
451 O << "#"
452 << (char)ARM_AM::getAM2Op(MO2.getImm())
453 << ImmOffs;
454 return;
455 }
456
457 O << (char)ARM_AM::getAM2Op(MO2.getImm())
Bill Wendling8eeb9792008-02-26 21:11:01 +0000458 << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000459
460 if (unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm()))
461 O << ", "
Chris Lattnera96056a2007-12-30 20:49:49 +0000462 << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO2.getImm()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000463 << " #" << ShImm;
464}
465
466void ARMAsmPrinter::printAddrMode3Operand(const MachineInstr *MI, int Op) {
467 const MachineOperand &MO1 = MI->getOperand(Op);
468 const MachineOperand &MO2 = MI->getOperand(Op+1);
469 const MachineOperand &MO3 = MI->getOperand(Op+2);
470
Dan Gohman1e57df32008-02-10 18:45:23 +0000471 assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
Bill Wendling8eeb9792008-02-26 21:11:01 +0000472 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000473
474 if (MO2.getReg()) {
475 O << ", "
476 << (char)ARM_AM::getAM3Op(MO3.getImm())
Bill Wendling8eeb9792008-02-26 21:11:01 +0000477 << TM.getRegisterInfo()->get(MO2.getReg()).AsmName
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000478 << "]";
479 return;
480 }
481
482 if (unsigned ImmOffs = ARM_AM::getAM3Offset(MO3.getImm()))
483 O << ", #"
484 << (char)ARM_AM::getAM3Op(MO3.getImm())
485 << ImmOffs;
486 O << "]";
487}
488
489void ARMAsmPrinter::printAddrMode3OffsetOperand(const MachineInstr *MI, int Op){
490 const MachineOperand &MO1 = MI->getOperand(Op);
491 const MachineOperand &MO2 = MI->getOperand(Op+1);
492
493 if (MO1.getReg()) {
494 O << (char)ARM_AM::getAM3Op(MO2.getImm())
Bill Wendling8eeb9792008-02-26 21:11:01 +0000495 << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000496 return;
497 }
498
499 unsigned ImmOffs = ARM_AM::getAM3Offset(MO2.getImm());
500 assert(ImmOffs && "Malformed indexed load / store!");
501 O << "#"
502 << (char)ARM_AM::getAM3Op(MO2.getImm())
503 << ImmOffs;
504}
505
506void ARMAsmPrinter::printAddrMode4Operand(const MachineInstr *MI, int Op,
507 const char *Modifier) {
508 const MachineOperand &MO1 = MI->getOperand(Op);
509 const MachineOperand &MO2 = MI->getOperand(Op+1);
510 ARM_AM::AMSubMode Mode = ARM_AM::getAM4SubMode(MO2.getImm());
511 if (Modifier && strcmp(Modifier, "submode") == 0) {
512 if (MO1.getReg() == ARM::SP) {
513 bool isLDM = (MI->getOpcode() == ARM::LDM ||
514 MI->getOpcode() == ARM::LDM_RET);
515 O << ARM_AM::getAMSubModeAltStr(Mode, isLDM);
516 } else
517 O << ARM_AM::getAMSubModeStr(Mode);
518 } else {
519 printOperand(MI, Op);
520 if (ARM_AM::getAM4WBFlag(MO2.getImm()))
521 O << "!";
522 }
523}
524
525void ARMAsmPrinter::printAddrMode5Operand(const MachineInstr *MI, int Op,
526 const char *Modifier) {
527 const MachineOperand &MO1 = MI->getOperand(Op);
528 const MachineOperand &MO2 = MI->getOperand(Op+1);
529
530 if (!MO1.isRegister()) { // FIXME: This is for CP entries, but isn't right.
531 printOperand(MI, Op);
532 return;
533 }
534
Dan Gohman1e57df32008-02-10 18:45:23 +0000535 assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000536
537 if (Modifier && strcmp(Modifier, "submode") == 0) {
538 ARM_AM::AMSubMode Mode = ARM_AM::getAM5SubMode(MO2.getImm());
539 if (MO1.getReg() == ARM::SP) {
540 bool isFLDM = (MI->getOpcode() == ARM::FLDMD ||
541 MI->getOpcode() == ARM::FLDMS);
542 O << ARM_AM::getAMSubModeAltStr(Mode, isFLDM);
543 } else
544 O << ARM_AM::getAMSubModeStr(Mode);
545 return;
546 } else if (Modifier && strcmp(Modifier, "base") == 0) {
547 // Used for FSTM{D|S} and LSTM{D|S} operations.
Bill Wendling8eeb9792008-02-26 21:11:01 +0000548 O << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000549 if (ARM_AM::getAM5WBFlag(MO2.getImm()))
550 O << "!";
551 return;
552 }
553
Bill Wendling8eeb9792008-02-26 21:11:01 +0000554 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000555
556 if (unsigned ImmOffs = ARM_AM::getAM5Offset(MO2.getImm())) {
557 O << ", #"
558 << (char)ARM_AM::getAM5Op(MO2.getImm())
559 << ImmOffs*4;
560 }
561 O << "]";
562}
563
564void ARMAsmPrinter::printAddrModePCOperand(const MachineInstr *MI, int Op,
565 const char *Modifier) {
566 if (Modifier && strcmp(Modifier, "label") == 0) {
567 printPCLabel(MI, Op+1);
568 return;
569 }
570
571 const MachineOperand &MO1 = MI->getOperand(Op);
Dan Gohman1e57df32008-02-10 18:45:23 +0000572 assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
Bill Wendling8eeb9792008-02-26 21:11:01 +0000573 O << "[pc, +" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName << "]";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000574}
575
576void
577ARMAsmPrinter::printThumbAddrModeRROperand(const MachineInstr *MI, int Op) {
578 const MachineOperand &MO1 = MI->getOperand(Op);
579 const MachineOperand &MO2 = MI->getOperand(Op+1);
Bill Wendling8eeb9792008-02-26 21:11:01 +0000580 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
581 O << ", " << TM.getRegisterInfo()->get(MO2.getReg()).AsmName << "]";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000582}
583
584void
585ARMAsmPrinter::printThumbAddrModeRI5Operand(const MachineInstr *MI, int Op,
586 unsigned Scale) {
587 const MachineOperand &MO1 = MI->getOperand(Op);
588 const MachineOperand &MO2 = MI->getOperand(Op+1);
589 const MachineOperand &MO3 = MI->getOperand(Op+2);
590
591 if (!MO1.isRegister()) { // FIXME: This is for CP entries, but isn't right.
592 printOperand(MI, Op);
593 return;
594 }
595
Bill Wendling8eeb9792008-02-26 21:11:01 +0000596 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000597 if (MO3.getReg())
Bill Wendling8eeb9792008-02-26 21:11:01 +0000598 O << ", " << TM.getRegisterInfo()->get(MO3.getReg()).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000599 else if (unsigned ImmOffs = MO2.getImm()) {
600 O << ", #" << ImmOffs;
601 if (Scale > 1)
602 O << " * " << Scale;
603 }
604 O << "]";
605}
606
607void
608ARMAsmPrinter::printThumbAddrModeS1Operand(const MachineInstr *MI, int Op) {
609 printThumbAddrModeRI5Operand(MI, Op, 1);
610}
611void
612ARMAsmPrinter::printThumbAddrModeS2Operand(const MachineInstr *MI, int Op) {
613 printThumbAddrModeRI5Operand(MI, Op, 2);
614}
615void
616ARMAsmPrinter::printThumbAddrModeS4Operand(const MachineInstr *MI, int Op) {
617 printThumbAddrModeRI5Operand(MI, Op, 4);
618}
619
620void ARMAsmPrinter::printThumbAddrModeSPOperand(const MachineInstr *MI,int Op) {
621 const MachineOperand &MO1 = MI->getOperand(Op);
622 const MachineOperand &MO2 = MI->getOperand(Op+1);
Bill Wendling8eeb9792008-02-26 21:11:01 +0000623 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000624 if (unsigned ImmOffs = MO2.getImm())
625 O << ", #" << ImmOffs << " * 4";
626 O << "]";
627}
628
629void ARMAsmPrinter::printPredicateOperand(const MachineInstr *MI, int opNum) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000630 ARMCC::CondCodes CC = (ARMCC::CondCodes)MI->getOperand(opNum).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000631 if (CC != ARMCC::AL)
632 O << ARMCondCodeToString(CC);
633}
634
635void ARMAsmPrinter::printSBitModifierOperand(const MachineInstr *MI, int opNum){
636 unsigned Reg = MI->getOperand(opNum).getReg();
637 if (Reg) {
638 assert(Reg == ARM::CPSR && "Expect ARM CPSR register!");
639 O << 's';
640 }
641}
642
643void ARMAsmPrinter::printPCLabel(const MachineInstr *MI, int opNum) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000644 int Id = (int)MI->getOperand(opNum).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000645 O << TAI->getPrivateGlobalPrefix() << "PC" << Id;
646}
647
648void ARMAsmPrinter::printRegisterList(const MachineInstr *MI, int opNum) {
649 O << "{";
650 for (unsigned i = opNum, e = MI->getNumOperands(); i != e; ++i) {
651 printOperand(MI, i);
652 if (i != e-1) O << ", ";
653 }
654 O << "}";
655}
656
657void ARMAsmPrinter::printCPInstOperand(const MachineInstr *MI, int OpNo,
658 const char *Modifier) {
659 assert(Modifier && "This operand only works with a modifier!");
660 // There are two aspects to a CONSTANTPOOL_ENTRY operand, the label and the
661 // data itself.
662 if (!strcmp(Modifier, "label")) {
663 unsigned ID = MI->getOperand(OpNo).getImm();
Evan Cheng477013c2007-10-14 05:57:21 +0000664 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
665 << '_' << ID << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000666 } else {
667 assert(!strcmp(Modifier, "cpentry") && "Unknown modifier for CPE");
Chris Lattner6017d482007-12-30 23:10:15 +0000668 unsigned CPI = MI->getOperand(OpNo).getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000669
670 const MachineConstantPoolEntry &MCPE = // Chasing pointers is fun?
671 MI->getParent()->getParent()->getConstantPool()->getConstants()[CPI];
672
Evan Chengf1012ce2008-08-08 06:56:16 +0000673 if (MCPE.isMachineConstantPoolEntry()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000674 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
Evan Chengf1012ce2008-08-08 06:56:16 +0000675 ARMConstantPoolValue *ACPV =
676 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
677 if (ACPV->getPCAdjustment() != 0) {
678 const GlobalValue *GV = ACPV->getGV();
679 PCRelGVs.insert(GV);
680 }
681 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000682 EmitGlobalConstant(MCPE.Val.ConstVal);
683 // remember to emit the weak reference
684 if (const GlobalValue *GV = dyn_cast<GlobalValue>(MCPE.Val.ConstVal))
685 if (GV->hasExternalWeakLinkage())
686 ExtWeakSymbols.insert(GV);
687 }
688 }
689}
690
691void ARMAsmPrinter::printJTBlockOperand(const MachineInstr *MI, int OpNo) {
692 const MachineOperand &MO1 = MI->getOperand(OpNo);
693 const MachineOperand &MO2 = MI->getOperand(OpNo+1); // Unique Id
Chris Lattner6017d482007-12-30 23:10:15 +0000694 unsigned JTI = MO1.getIndex();
Evan Cheng477013c2007-10-14 05:57:21 +0000695 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattnera96056a2007-12-30 20:49:49 +0000696 << '_' << JTI << '_' << MO2.getImm() << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000697
698 const char *JTEntryDirective = TAI->getJumpTableDirective();
699 if (!JTEntryDirective)
700 JTEntryDirective = TAI->getData32bitsDirective();
701
702 const MachineFunction *MF = MI->getParent()->getParent();
Dan Gohmane4da6412008-07-07 20:06:06 +0000703 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000704 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
705 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
706 bool UseSet= TAI->getSetDirective() && TM.getRelocationModel() == Reloc::PIC_;
707 std::set<MachineBasicBlock*> JTSets;
708 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
709 MachineBasicBlock *MBB = JTBBs[i];
710 if (UseSet && JTSets.insert(MBB).second)
Chris Lattnera96056a2007-12-30 20:49:49 +0000711 printPICJumpTableSetLabel(JTI, MO2.getImm(), MBB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000712
713 O << JTEntryDirective << ' ';
714 if (UseSet)
Evan Cheng477013c2007-10-14 05:57:21 +0000715 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
Chris Lattnera96056a2007-12-30 20:49:49 +0000716 << '_' << JTI << '_' << MO2.getImm()
Evan Cheng477013c2007-10-14 05:57:21 +0000717 << "_set_" << MBB->getNumber();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000718 else if (TM.getRelocationModel() == Reloc::PIC_) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000719 printBasicBlockLabel(MBB, false, false, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000720 // If the arch uses custom Jump Table directives, don't calc relative to JT
721 if (!TAI->getJumpTableDirective())
722 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
Chris Lattnera96056a2007-12-30 20:49:49 +0000723 << getFunctionNumber() << '_' << JTI << '_' << MO2.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000724 } else
Evan Cheng45c1edb2008-02-28 00:43:03 +0000725 printBasicBlockLabel(MBB, false, false, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000726 if (i != e-1)
727 O << '\n';
728 }
729}
730
731
732bool ARMAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
733 unsigned AsmVariant, const char *ExtraCode){
734 // Does this asm operand have a single letter operand modifier?
735 if (ExtraCode && ExtraCode[0]) {
736 if (ExtraCode[1] != 0) return true; // Unknown modifier.
737
738 switch (ExtraCode[0]) {
739 default: return true; // Unknown modifier.
740 case 'c': // Don't print "$" before a global var name or constant.
741 case 'P': // Print a VFP double precision register.
742 printOperand(MI, OpNo);
743 return false;
744 case 'Q':
745 if (TM.getTargetData()->isLittleEndian())
746 break;
747 // Fallthrough
748 case 'R':
749 if (TM.getTargetData()->isBigEndian())
750 break;
751 // Fallthrough
752 case 'H': // Write second word of DI / DF reference.
753 // Verify that this operand has two consecutive registers.
754 if (!MI->getOperand(OpNo).isRegister() ||
755 OpNo+1 == MI->getNumOperands() ||
756 !MI->getOperand(OpNo+1).isRegister())
757 return true;
758 ++OpNo; // Return the high-part.
759 }
760 }
761
762 printOperand(MI, OpNo);
763 return false;
764}
765
766void ARMAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
767 ++EmittedInsts;
768
769 int Opc = MI->getOpcode();
770 switch (Opc) {
771 case ARM::CONSTPOOL_ENTRY:
772 if (!InCPMode && AFI->isThumbFunction()) {
773 EmitAlignment(2);
774 InCPMode = true;
775 }
776 break;
777 default: {
778 if (InCPMode && AFI->isThumbFunction())
779 InCPMode = false;
780 switch (Opc) {
781 case ARM::PICADD:
782 case ARM::PICLD:
783 case ARM::PICLDZH:
784 case ARM::PICLDZB:
785 case ARM::PICLDH:
786 case ARM::PICLDB:
787 case ARM::PICLDSH:
788 case ARM::PICLDSB:
789 case ARM::PICSTR:
790 case ARM::PICSTRH:
791 case ARM::PICSTRB:
792 case ARM::tPICADD:
793 break;
794 default:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000795 break;
796 }
797 }}
798
799 // Call the autogenerated instruction printer routines.
800 printInstruction(MI);
801}
802
803bool ARMAsmPrinter::doInitialization(Module &M) {
804 // Emit initial debug information.
805 DW.BeginModule(&M);
806
Dan Gohman4a558a32007-07-25 19:33:14 +0000807 bool Result = AsmPrinter::doInitialization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000808
Dale Johannesen2f85df12008-07-09 21:20:54 +0000809 // AsmPrinter::doInitialization should have done this analysis.
810 MMI = getAnalysisToUpdate<MachineModuleInfo>();
811 assert(MMI);
812 DW.SetModuleInfo(MMI);
813
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000814 // Darwin wants symbols to be quoted if they have complex names.
815 if (Subtarget->isTargetDarwin())
816 Mang->setUseQuotes(true);
817
Dan Gohman4a558a32007-07-25 19:33:14 +0000818 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000819}
820
Chris Lattner2b638a72008-02-15 19:04:54 +0000821/// PrintUnmangledNameSafely - Print out the printable characters in the name.
822/// Don't print things like \n or \0.
823static void PrintUnmangledNameSafely(const Value *V, std::ostream &OS) {
824 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
825 Name != E; ++Name)
826 if (isprint(*Name))
827 OS << *Name;
828}
829
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000830void ARMAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000831 const TargetData *TD = TM.getTargetData();
832
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000833 if (!GVar->hasInitializer()) // External global require no code
834 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000835
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000836 // Check to see if this is a special global used by LLVM, if so, emit it.
837
838 if (EmitSpecialLLVMGlobal(GVar)) {
839 if (Subtarget->isTargetDarwin() &&
840 TM.getRelocationModel() == Reloc::Static) {
841 if (GVar->getName() == "llvm.global_ctors")
842 O << ".reference .constructors_used\n";
843 else if (GVar->getName() == "llvm.global_dtors")
844 O << ".reference .destructors_used\n";
845 }
846 return;
847 }
848
Evan Chengeb774e12008-08-08 17:56:50 +0000849 std::string SectionName = TAI->SectionForGlobal(GVar);
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000850 std::string name = Mang->getValueName(GVar);
851 Constant *C = GVar->getInitializer();
852 const Type *Type = C->getType();
853 unsigned Size = TD->getABITypeSize(Type);
854 unsigned Align = TD->getPreferredAlignmentLog(GVar);
855
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000856 printVisibility(name, GVar->getVisibility());
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000857
858 if (Subtarget->isTargetELF())
859 O << "\t.type " << name << ",%object\n";
860
861 SwitchToDataSection(SectionName.c_str());
862
863 if (C->isNullValue() && !GVar->hasSection() && !GVar->isThreadLocal()) {
864 // FIXME: This seems to be pretty darwin-specific
865
866 if (GVar->hasExternalLinkage()) {
867 if (const char *Directive = TAI->getZeroFillDirective()) {
868 O << "\t.globl\t" << name << "\n";
869 O << Directive << "__DATA, __common, " << name << ", "
870 << Size << ", " << Align << "\n";
871 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000872 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000873 }
874
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000875 if (GVar->hasInternalLinkage() || GVar->isWeakForLinker()) {
876 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000877
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000878 if (TAI->getLCOMMDirective() != NULL) {
Evan Chengeb774e12008-08-08 17:56:50 +0000879 if (PCRelGVs.count(GVar) || GVar->hasInternalLinkage()) {
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000880 O << TAI->getLCOMMDirective() << name << "," << Size;
881 if (Subtarget->isTargetDarwin())
882 O << "," << Align;
883 } else
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000884 O << TAI->getCOMMDirective() << name << "," << Size;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000885 } else {
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000886 if (GVar->hasInternalLinkage())
887 O << "\t.local\t" << name << "\n";
888 O << TAI->getCOMMDirective() << name << "," << Size;
889 if (TAI->getCOMMDirectiveTakesAlignment())
890 O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000891 }
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000892 O << "\t\t" << TAI->getCommentString() << " ";
893 PrintUnmangledNameSafely(GVar, O);
894 O << "\n";
895 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000896 }
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000897 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000898
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000899 switch (GVar->getLinkage()) {
900 case GlobalValue::LinkOnceLinkage:
901 case GlobalValue::WeakLinkage:
902 if (Subtarget->isTargetDarwin()) {
903 O << "\t.globl " << name << "\n"
904 << "\t.weak_definition " << name << "\n";
905 } else {
906 O << "\t.weak " << name << "\n";
907 }
908 break;
909 case GlobalValue::AppendingLinkage:
910 // FIXME: appending linkage variables should go into a section of
911 // their name or something. For now, just emit them as external.
912 case GlobalValue::ExternalLinkage:
913 O << "\t.globl " << name << "\n";
914 // FALL THROUGH
915 case GlobalValue::InternalLinkage:
916 break;
917 default:
918 assert(0 && "Unknown linkage type!");
919 break;
920 }
921
922 EmitAlignment(Align, GVar);
923 O << name << ":\t\t\t\t" << TAI->getCommentString() << " ";
924 PrintUnmangledNameSafely(GVar, O);
925 O << "\n";
926 if (TAI->hasDotTypeDotSizeDirective())
927 O << "\t.size " << name << ", " << Size << "\n";
928
929 // If the initializer is a extern weak symbol, remember to emit the weak
930 // reference!
931 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
932 if (GV->hasExternalWeakLinkage())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000933 ExtWeakSymbols.insert(GV);
934
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000935 EmitGlobalConstant(C);
936 O << '\n';
937}
938
939
940bool ARMAsmPrinter::doFinalization(Module &M) {
941 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
942 I != E; ++I)
943 printModuleLevelGV(I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000944
945 if (Subtarget->isTargetDarwin()) {
946 SwitchToDataSection("");
947
948 // Output stubs for dynamically-linked functions
949 unsigned j = 1;
950 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
951 i != e; ++i, ++j) {
952 if (TM.getRelocationModel() == Reloc::PIC_)
953 SwitchToTextSection(".section __TEXT,__picsymbolstub4,symbol_stubs,"
954 "none,16", 0);
955 else
956 SwitchToTextSection(".section __TEXT,__symbol_stub4,symbol_stubs,"
957 "none,12", 0);
958
959 EmitAlignment(2);
960 O << "\t.code\t32\n";
961
Dale Johannesena21b5202008-05-19 21:38:18 +0000962 std::string p = *i;
963 printSuffixedName(p, "$stub");
964 O << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000965 O << "\t.indirect_symbol " << *i << "\n";
Dale Johannesena21b5202008-05-19 21:38:18 +0000966 O << "\tldr ip, ";
967 printSuffixedName(p, "$slp");
968 O << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000969 if (TM.getRelocationModel() == Reloc::PIC_) {
Dale Johannesena21b5202008-05-19 21:38:18 +0000970 printSuffixedName(p, "$scv");
971 O << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000972 O << "\tadd ip, pc, ip\n";
973 }
974 O << "\tldr pc, [ip, #0]\n";
Dale Johannesena21b5202008-05-19 21:38:18 +0000975 printSuffixedName(p, "$slp");
976 O << ":\n";
977 O << "\t.long\t";
978 printSuffixedName(p, "$lazy_ptr");
979 if (TM.getRelocationModel() == Reloc::PIC_) {
980 O << "-(";
981 printSuffixedName(p, "$scv");
982 O << "+8)\n";
983 } else
984 O << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000985 SwitchToDataSection(".lazy_symbol_pointer", 0);
Dale Johannesena21b5202008-05-19 21:38:18 +0000986 printSuffixedName(p, "$lazy_ptr");
987 O << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000988 O << "\t.indirect_symbol " << *i << "\n";
989 O << "\t.long\tdyld_stub_binding_helper\n";
990 }
991 O << "\n";
992
993 // Output non-lazy-pointers for external and common global variables.
Dan Gohman3f7d94b2007-10-03 19:26:29 +0000994 if (!GVNonLazyPtrs.empty())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000995 SwitchToDataSection(".non_lazy_symbol_pointer", 0);
996 for (std::set<std::string>::iterator i = GVNonLazyPtrs.begin(),
997 e = GVNonLazyPtrs.end(); i != e; ++i) {
Dale Johannesena21b5202008-05-19 21:38:18 +0000998 std::string p = *i;
999 printSuffixedName(p, "$non_lazy_ptr");
1000 O << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001001 O << "\t.indirect_symbol " << *i << "\n";
1002 O << "\t.long\t0\n";
1003 }
1004
1005 // Emit initial debug information.
1006 DW.EndModule();
1007
1008 // Funny Darwin hack: This flag tells the linker that no global symbols
1009 // contain code that falls through to other global symbols (e.g. the obvious
1010 // implementation of multiple entry points). If this doesn't occur, the
1011 // linker can safely perform dead code stripping. Since LLVM never
1012 // generates code that does this, it is always safe to set.
1013 O << "\t.subsections_via_symbols\n";
1014 } else {
1015 // Emit final debug information for ELF.
1016 DW.EndModule();
1017 }
1018
Dan Gohman4a558a32007-07-25 19:33:14 +00001019 return AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001020}
Anton Korobeynikov74b114b2008-08-17 13:55:10 +00001021
1022/// createARMCodePrinterPass - Returns a pass that prints the ARM
1023/// assembly code for a MachineFunction to the given output stream,
1024/// using the given target machine description. This should work
1025/// regardless of whether the function is in SSA form.
1026///
1027FunctionPass *llvm::createARMCodePrinterPass(std::ostream &o,
1028 ARMTargetMachine &tm) {
1029 return new ARMAsmPrinter(o, tm, tm.getTargetAsmInfo());
1030}
1031
1032namespace {
1033 static struct Register {
1034 Register() {
1035 ARMTargetMachine::registerAsmPrinter(createARMCodePrinterPass);
1036 }
1037 } Registrator;
1038}