blob: 3b30f9c5a0dfdc65d6aac3d4c19e9b2ffc488905 [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"
32#include "llvm/ADT/Statistic.h"
33#include "llvm/ADT/StringExtras.h"
34#include "llvm/Support/Compiler.h"
35#include "llvm/Support/Mangler.h"
36#include "llvm/Support/MathExtras.h"
37#include <cctype>
38using namespace llvm;
39
40STATISTIC(EmittedInsts, "Number of machine instrs printed");
41
42namespace {
43 struct VISIBILITY_HIDDEN ARMAsmPrinter : public AsmPrinter {
44 ARMAsmPrinter(std::ostream &O, TargetMachine &TM, const TargetAsmInfo *T)
Dale Johannesen2f85df12008-07-09 21:20:54 +000045 : AsmPrinter(O, TM, T), DW(O, this, T), MMI(NULL), AFI(NULL),
46 InCPMode(false) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047 Subtarget = &TM.getSubtarget<ARMSubtarget>();
48 }
49
50 DwarfWriter DW;
Dale Johannesen2f85df12008-07-09 21:20:54 +000051 MachineModuleInfo *MMI;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000052
53 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
54 /// make the right decision when printing asm code for different targets.
55 const ARMSubtarget *Subtarget;
56
57 /// AFI - Keep a pointer to ARMFunctionInfo for the current
58 /// MachineFunction
59 ARMFunctionInfo *AFI;
60
61 /// We name each basic block in a Function with a unique number, so
62 /// that we can consistently refer to them later. This is cleared
63 /// at the beginning of each call to runOnMachineFunction().
64 ///
65 typedef std::map<const Value *, unsigned> ValueMapTy;
66 ValueMapTy NumberForBB;
67
68 /// Keeps the set of GlobalValues that require non-lazy-pointers for
69 /// indirect access.
70 std::set<std::string> GVNonLazyPtrs;
71
72 /// Keeps the set of external function GlobalAddresses that the asm
73 /// printer should generate stubs for.
74 std::set<std::string> FnStubs;
75
76 /// True if asm printer is printing a series of CONSTPOOL_ENTRY.
77 bool InCPMode;
78
79 virtual const char *getPassName() const {
80 return "ARM Assembly Printer";
81 }
82
83 void printOperand(const MachineInstr *MI, int opNum,
84 const char *Modifier = 0);
85 void printSOImmOperand(const MachineInstr *MI, int opNum);
86 void printSOImm2PartOperand(const MachineInstr *MI, int opNum);
87 void printSORegOperand(const MachineInstr *MI, int opNum);
88 void printAddrMode2Operand(const MachineInstr *MI, int OpNo);
89 void printAddrMode2OffsetOperand(const MachineInstr *MI, int OpNo);
90 void printAddrMode3Operand(const MachineInstr *MI, int OpNo);
91 void printAddrMode3OffsetOperand(const MachineInstr *MI, int OpNo);
92 void printAddrMode4Operand(const MachineInstr *MI, int OpNo,
93 const char *Modifier = 0);
94 void printAddrMode5Operand(const MachineInstr *MI, int OpNo,
95 const char *Modifier = 0);
96 void printAddrModePCOperand(const MachineInstr *MI, int OpNo,
97 const char *Modifier = 0);
98 void printThumbAddrModeRROperand(const MachineInstr *MI, int OpNo);
99 void printThumbAddrModeRI5Operand(const MachineInstr *MI, int OpNo,
100 unsigned Scale);
101 void printThumbAddrModeS1Operand(const MachineInstr *MI, int OpNo);
102 void printThumbAddrModeS2Operand(const MachineInstr *MI, int OpNo);
103 void printThumbAddrModeS4Operand(const MachineInstr *MI, int OpNo);
104 void printThumbAddrModeSPOperand(const MachineInstr *MI, int OpNo);
105 void printPredicateOperand(const MachineInstr *MI, int opNum);
106 void printSBitModifierOperand(const MachineInstr *MI, int opNum);
107 void printPCLabel(const MachineInstr *MI, int opNum);
108 void printRegisterList(const MachineInstr *MI, int opNum);
109 void printCPInstOperand(const MachineInstr *MI, int opNum,
110 const char *Modifier);
111 void printJTBlockOperand(const MachineInstr *MI, int opNum);
112
113 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
114 unsigned AsmVariant, const char *ExtraCode);
115
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000116 void printModuleLevelGV(const GlobalVariable* GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000117 bool printInstruction(const MachineInstr *MI); // autogenerated.
118 void printMachineInstruction(const MachineInstr *MI);
119 bool runOnMachineFunction(MachineFunction &F);
120 bool doInitialization(Module &M);
121 bool doFinalization(Module &M);
122
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000123 /// getSectionForFunction - Return the section that we should emit the
124 /// specified function body into.
125 virtual std::string getSectionForFunction(const Function &F) const;
126
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000127 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
128 printDataDirective(MCPV->getType());
129
130 ARMConstantPoolValue *ACPV = (ARMConstantPoolValue*)MCPV;
131 GlobalValue *GV = ACPV->getGV();
132 std::string Name = GV ? Mang->getValueName(GV) : TAI->getGlobalPrefix();
133 if (!GV)
134 Name += ACPV->getSymbol();
135 if (ACPV->isNonLazyPointer()) {
136 GVNonLazyPtrs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000137 printSuffixedName(Name, "$non_lazy_ptr");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000138 } else if (ACPV->isStub()) {
139 FnStubs.insert(Name);
Dale Johannesena21b5202008-05-19 21:38:18 +0000140 printSuffixedName(Name, "$stub");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000141 } else
142 O << Name;
143 if (ACPV->hasModifier()) O << "(" << ACPV->getModifier() << ")";
144 if (ACPV->getPCAdjustment() != 0) {
145 O << "-(" << TAI->getPrivateGlobalPrefix() << "PC"
146 << utostr(ACPV->getLabelId())
147 << "+" << (unsigned)ACPV->getPCAdjustment();
148 if (ACPV->mustAddCurrentAddress())
149 O << "-.";
150 O << ")";
151 }
152 O << "\n";
153
154 // If the constant pool value is a extern weak symbol, remember to emit
155 // the weak reference.
156 if (GV && GV->hasExternalWeakLinkage())
157 ExtWeakSymbols.insert(GV);
158 }
159
160 void getAnalysisUsage(AnalysisUsage &AU) const {
Gordon Henriksen76e4b612007-09-30 13:39:29 +0000161 AsmPrinter::getAnalysisUsage(AU);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000162 AU.setPreservesAll();
163 AU.addRequired<MachineModuleInfo>();
164 }
165 };
166} // end of anonymous namespace
167
168#include "ARMGenAsmWriter.inc"
169
170/// createARMCodePrinterPass - Returns a pass that prints the ARM
171/// assembly code for a MachineFunction to the given output stream,
172/// using the given target machine description. This should work
173/// regardless of whether the function is in SSA form.
174///
175FunctionPass *llvm::createARMCodePrinterPass(std::ostream &o,
176 ARMTargetMachine &tm) {
177 return new ARMAsmPrinter(o, tm, tm.getTargetAsmInfo());
178}
179
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000180// Substitute old hook with new one temporary
181std::string ARMAsmPrinter::getSectionForFunction(const Function &F) const {
182 return TAI->SectionForGlobal(&F);
183}
184
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000185/// runOnMachineFunction - This uses the printInstruction()
186/// method to print assembly for each instruction.
187///
188bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
189 AFI = MF.getInfo<ARMFunctionInfo>();
190
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000191 SetupMachineFunction(MF);
192 O << "\n";
193
194 // NOTE: we don't print out constant pools here, they are handled as
195 // instructions.
196
197 O << "\n";
198 // Print out labels for the function.
199 const Function *F = MF.getFunction();
200 switch (F->getLinkage()) {
201 default: assert(0 && "Unknown linkage type!");
202 case Function::InternalLinkage:
203 SwitchToTextSection("\t.text", F);
204 break;
205 case Function::ExternalLinkage:
206 SwitchToTextSection("\t.text", F);
207 O << "\t.globl\t" << CurrentFnName << "\n";
208 break;
209 case Function::WeakLinkage:
210 case Function::LinkOnceLinkage:
211 if (Subtarget->isTargetDarwin()) {
212 SwitchToTextSection(
213 ".section __TEXT,__textcoal_nt,coalesced,pure_instructions", F);
214 O << "\t.globl\t" << CurrentFnName << "\n";
215 O << "\t.weak_definition\t" << CurrentFnName << "\n";
216 } else {
217 O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
218 }
219 break;
220 }
221
222 const char *VisibilityDirective = NULL;
223 if (F->hasHiddenVisibility())
224 VisibilityDirective = TAI->getHiddenDirective();
225 else if (F->hasProtectedVisibility())
226 VisibilityDirective = TAI->getProtectedDirective();
227
228 if (VisibilityDirective)
229 O << VisibilityDirective << CurrentFnName << "\n";
230
231 if (AFI->isThumbFunction()) {
232 EmitAlignment(1, F, AFI->getAlign());
233 O << "\t.code\t16\n";
234 O << "\t.thumb_func";
235 if (Subtarget->isTargetDarwin())
236 O << "\t" << CurrentFnName;
237 O << "\n";
238 InCPMode = false;
239 } else
240 EmitAlignment(2, F);
241
242 O << CurrentFnName << ":\n";
243 // Emit pre-function debug information.
244 DW.BeginFunction(&MF);
245
Bill Wendling43a78162008-01-28 09:15:03 +0000246 if (Subtarget->isTargetDarwin()) {
247 // If the function is empty, then we need to emit *something*. Otherwise,
248 // the function's label might be associated with something that it wasn't
249 // meant to be associated with. We emit a noop in this situation.
250 MachineFunction::iterator I = MF.begin();
251
252 if (++I == MF.end() && MF.front().empty())
253 O << "\tnop\n";
254 }
255
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000256 // Print out code for the function.
257 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
258 I != E; ++I) {
259 // Print a label for the basic block.
260 if (I != MF.begin()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000261 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000262 O << '\n';
263 }
264 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
265 II != E; ++II) {
266 // Print the assembly for the instruction.
267 printMachineInstruction(II);
268 }
269 }
270
271 if (TAI->hasDotTypeDotSizeDirective())
272 O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
273
274 // Emit post-function debug information.
275 DW.EndFunction();
276
277 return false;
278}
279
280void ARMAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
281 const char *Modifier) {
282 const MachineOperand &MO = MI->getOperand(opNum);
283 switch (MO.getType()) {
284 case MachineOperand::MO_Register:
Dan Gohman1e57df32008-02-10 18:45:23 +0000285 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Bill Wendling8eeb9792008-02-26 21:11:01 +0000286 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000287 else
288 assert(0 && "not implemented");
289 break;
290 case MachineOperand::MO_Immediate: {
291 if (!Modifier || strcmp(Modifier, "no_hash") != 0)
292 O << "#";
293
Chris Lattnera96056a2007-12-30 20:49:49 +0000294 O << (int)MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000295 break;
296 }
297 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner6017d482007-12-30 23:10:15 +0000298 printBasicBlockLabel(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000299 return;
300 case MachineOperand::MO_GlobalAddress: {
301 bool isCallOp = Modifier && !strcmp(Modifier, "call");
302 GlobalValue *GV = MO.getGlobal();
303 std::string Name = Mang->getValueName(GV);
304 bool isExt = (GV->isDeclaration() || GV->hasWeakLinkage() ||
305 GV->hasLinkOnceLinkage());
306 if (isExt && isCallOp && Subtarget->isTargetDarwin() &&
307 TM.getRelocationModel() != Reloc::Static) {
Dale Johannesena21b5202008-05-19 21:38:18 +0000308 printSuffixedName(Name, "$stub");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000309 FnStubs.insert(Name);
310 } else
311 O << Name;
312
313 if (MO.getOffset() > 0)
314 O << '+' << MO.getOffset();
315 else if (MO.getOffset() < 0)
316 O << MO.getOffset();
317
318 if (isCallOp && Subtarget->isTargetELF() &&
319 TM.getRelocationModel() == Reloc::PIC_)
320 O << "(PLT)";
321 if (GV->hasExternalWeakLinkage())
322 ExtWeakSymbols.insert(GV);
323 break;
324 }
325 case MachineOperand::MO_ExternalSymbol: {
326 bool isCallOp = Modifier && !strcmp(Modifier, "call");
327 std::string Name(TAI->getGlobalPrefix());
328 Name += MO.getSymbolName();
329 if (isCallOp && Subtarget->isTargetDarwin() &&
330 TM.getRelocationModel() != Reloc::Static) {
Dale Johannesena21b5202008-05-19 21:38:18 +0000331 printSuffixedName(Name, "$stub");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000332 FnStubs.insert(Name);
333 } else
334 O << Name;
335 if (isCallOp && Subtarget->isTargetELF() &&
336 TM.getRelocationModel() == Reloc::PIC_)
337 O << "(PLT)";
338 break;
339 }
340 case MachineOperand::MO_ConstantPoolIndex:
Evan Cheng477013c2007-10-14 05:57:21 +0000341 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000342 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000343 break;
344 case MachineOperand::MO_JumpTableIndex:
Evan Cheng477013c2007-10-14 05:57:21 +0000345 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000346 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000347 break;
348 default:
349 O << "<unknown operand type>"; abort (); break;
350 }
351}
352
353static void printSOImm(std::ostream &O, int64_t V, const TargetAsmInfo *TAI) {
354 assert(V < (1 << 12) && "Not a valid so_imm value!");
355 unsigned Imm = ARM_AM::getSOImmValImm(V);
356 unsigned Rot = ARM_AM::getSOImmValRot(V);
357
358 // Print low-level immediate formation info, per
359 // A5.1.3: "Data-processing operands - Immediate".
360 if (Rot) {
361 O << "#" << Imm << ", " << Rot;
362 // Pretty printed version.
363 O << ' ' << TAI->getCommentString() << ' ' << (int)ARM_AM::rotr32(Imm, Rot);
364 } else {
365 O << "#" << Imm;
366 }
367}
368
369/// printSOImmOperand - SOImm is 4-bit rotate amount in bits 8-11 with 8-bit
370/// immediate in bits 0-7.
371void ARMAsmPrinter::printSOImmOperand(const MachineInstr *MI, int OpNum) {
372 const MachineOperand &MO = MI->getOperand(OpNum);
373 assert(MO.isImmediate() && "Not a valid so_imm value!");
Chris Lattnera96056a2007-12-30 20:49:49 +0000374 printSOImm(O, MO.getImm(), TAI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000375}
376
377/// printSOImm2PartOperand - SOImm is broken into two pieces using a mov
378/// followed by a or to materialize.
379void ARMAsmPrinter::printSOImm2PartOperand(const MachineInstr *MI, int OpNum) {
380 const MachineOperand &MO = MI->getOperand(OpNum);
381 assert(MO.isImmediate() && "Not a valid so_imm value!");
Chris Lattnera96056a2007-12-30 20:49:49 +0000382 unsigned V1 = ARM_AM::getSOImmTwoPartFirst(MO.getImm());
383 unsigned V2 = ARM_AM::getSOImmTwoPartSecond(MO.getImm());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000384 printSOImm(O, ARM_AM::getSOImmVal(V1), TAI);
385 O << "\n\torr";
386 printPredicateOperand(MI, 2);
387 O << " ";
388 printOperand(MI, 0);
389 O << ", ";
390 printOperand(MI, 0);
391 O << ", ";
392 printSOImm(O, ARM_AM::getSOImmVal(V2), TAI);
393}
394
395// so_reg is a 4-operand unit corresponding to register forms of the A5.1
396// "Addressing Mode 1 - Data-processing operands" forms. This includes:
397// REG 0 0 - e.g. R5
398// REG REG 0,SH_OPC - e.g. R5, ROR R3
399// REG 0 IMM,SH_OPC - e.g. R5, LSL #3
400void ARMAsmPrinter::printSORegOperand(const MachineInstr *MI, int Op) {
401 const MachineOperand &MO1 = MI->getOperand(Op);
402 const MachineOperand &MO2 = MI->getOperand(Op+1);
403 const MachineOperand &MO3 = MI->getOperand(Op+2);
404
Dan Gohman1e57df32008-02-10 18:45:23 +0000405 assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
Bill Wendling8eeb9792008-02-26 21:11:01 +0000406 O << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000407
408 // Print the shift opc.
409 O << ", "
Chris Lattnera96056a2007-12-30 20:49:49 +0000410 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(MO3.getImm()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000411 << " ";
412
413 if (MO2.getReg()) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000414 assert(TargetRegisterInfo::isPhysicalRegister(MO2.getReg()));
Bill Wendling8eeb9792008-02-26 21:11:01 +0000415 O << TM.getRegisterInfo()->get(MO2.getReg()).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000416 assert(ARM_AM::getSORegOffset(MO3.getImm()) == 0);
417 } else {
418 O << "#" << ARM_AM::getSORegOffset(MO3.getImm());
419 }
420}
421
422void ARMAsmPrinter::printAddrMode2Operand(const MachineInstr *MI, int Op) {
423 const MachineOperand &MO1 = MI->getOperand(Op);
424 const MachineOperand &MO2 = MI->getOperand(Op+1);
425 const MachineOperand &MO3 = MI->getOperand(Op+2);
426
427 if (!MO1.isRegister()) { // FIXME: This is for CP entries, but isn't right.
428 printOperand(MI, Op);
429 return;
430 }
431
Bill Wendling8eeb9792008-02-26 21:11:01 +0000432 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000433
434 if (!MO2.getReg()) {
435 if (ARM_AM::getAM2Offset(MO3.getImm())) // Don't print +0.
436 O << ", #"
437 << (char)ARM_AM::getAM2Op(MO3.getImm())
438 << ARM_AM::getAM2Offset(MO3.getImm());
439 O << "]";
440 return;
441 }
442
443 O << ", "
444 << (char)ARM_AM::getAM2Op(MO3.getImm())
Bill Wendling8eeb9792008-02-26 21:11:01 +0000445 << TM.getRegisterInfo()->get(MO2.getReg()).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000446
447 if (unsigned ShImm = ARM_AM::getAM2Offset(MO3.getImm()))
448 O << ", "
Chris Lattnera96056a2007-12-30 20:49:49 +0000449 << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO3.getImm()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000450 << " #" << ShImm;
451 O << "]";
452}
453
454void ARMAsmPrinter::printAddrMode2OffsetOperand(const MachineInstr *MI, int Op){
455 const MachineOperand &MO1 = MI->getOperand(Op);
456 const MachineOperand &MO2 = MI->getOperand(Op+1);
457
458 if (!MO1.getReg()) {
459 unsigned ImmOffs = ARM_AM::getAM2Offset(MO2.getImm());
460 assert(ImmOffs && "Malformed indexed load / store!");
461 O << "#"
462 << (char)ARM_AM::getAM2Op(MO2.getImm())
463 << ImmOffs;
464 return;
465 }
466
467 O << (char)ARM_AM::getAM2Op(MO2.getImm())
Bill Wendling8eeb9792008-02-26 21:11:01 +0000468 << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000469
470 if (unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm()))
471 O << ", "
Chris Lattnera96056a2007-12-30 20:49:49 +0000472 << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO2.getImm()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000473 << " #" << ShImm;
474}
475
476void ARMAsmPrinter::printAddrMode3Operand(const MachineInstr *MI, int Op) {
477 const MachineOperand &MO1 = MI->getOperand(Op);
478 const MachineOperand &MO2 = MI->getOperand(Op+1);
479 const MachineOperand &MO3 = MI->getOperand(Op+2);
480
Dan Gohman1e57df32008-02-10 18:45:23 +0000481 assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
Bill Wendling8eeb9792008-02-26 21:11:01 +0000482 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000483
484 if (MO2.getReg()) {
485 O << ", "
486 << (char)ARM_AM::getAM3Op(MO3.getImm())
Bill Wendling8eeb9792008-02-26 21:11:01 +0000487 << TM.getRegisterInfo()->get(MO2.getReg()).AsmName
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000488 << "]";
489 return;
490 }
491
492 if (unsigned ImmOffs = ARM_AM::getAM3Offset(MO3.getImm()))
493 O << ", #"
494 << (char)ARM_AM::getAM3Op(MO3.getImm())
495 << ImmOffs;
496 O << "]";
497}
498
499void ARMAsmPrinter::printAddrMode3OffsetOperand(const MachineInstr *MI, int Op){
500 const MachineOperand &MO1 = MI->getOperand(Op);
501 const MachineOperand &MO2 = MI->getOperand(Op+1);
502
503 if (MO1.getReg()) {
504 O << (char)ARM_AM::getAM3Op(MO2.getImm())
Bill Wendling8eeb9792008-02-26 21:11:01 +0000505 << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000506 return;
507 }
508
509 unsigned ImmOffs = ARM_AM::getAM3Offset(MO2.getImm());
510 assert(ImmOffs && "Malformed indexed load / store!");
511 O << "#"
512 << (char)ARM_AM::getAM3Op(MO2.getImm())
513 << ImmOffs;
514}
515
516void ARMAsmPrinter::printAddrMode4Operand(const MachineInstr *MI, int Op,
517 const char *Modifier) {
518 const MachineOperand &MO1 = MI->getOperand(Op);
519 const MachineOperand &MO2 = MI->getOperand(Op+1);
520 ARM_AM::AMSubMode Mode = ARM_AM::getAM4SubMode(MO2.getImm());
521 if (Modifier && strcmp(Modifier, "submode") == 0) {
522 if (MO1.getReg() == ARM::SP) {
523 bool isLDM = (MI->getOpcode() == ARM::LDM ||
524 MI->getOpcode() == ARM::LDM_RET);
525 O << ARM_AM::getAMSubModeAltStr(Mode, isLDM);
526 } else
527 O << ARM_AM::getAMSubModeStr(Mode);
528 } else {
529 printOperand(MI, Op);
530 if (ARM_AM::getAM4WBFlag(MO2.getImm()))
531 O << "!";
532 }
533}
534
535void ARMAsmPrinter::printAddrMode5Operand(const MachineInstr *MI, int Op,
536 const char *Modifier) {
537 const MachineOperand &MO1 = MI->getOperand(Op);
538 const MachineOperand &MO2 = MI->getOperand(Op+1);
539
540 if (!MO1.isRegister()) { // FIXME: This is for CP entries, but isn't right.
541 printOperand(MI, Op);
542 return;
543 }
544
Dan Gohman1e57df32008-02-10 18:45:23 +0000545 assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000546
547 if (Modifier && strcmp(Modifier, "submode") == 0) {
548 ARM_AM::AMSubMode Mode = ARM_AM::getAM5SubMode(MO2.getImm());
549 if (MO1.getReg() == ARM::SP) {
550 bool isFLDM = (MI->getOpcode() == ARM::FLDMD ||
551 MI->getOpcode() == ARM::FLDMS);
552 O << ARM_AM::getAMSubModeAltStr(Mode, isFLDM);
553 } else
554 O << ARM_AM::getAMSubModeStr(Mode);
555 return;
556 } else if (Modifier && strcmp(Modifier, "base") == 0) {
557 // Used for FSTM{D|S} and LSTM{D|S} operations.
Bill Wendling8eeb9792008-02-26 21:11:01 +0000558 O << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000559 if (ARM_AM::getAM5WBFlag(MO2.getImm()))
560 O << "!";
561 return;
562 }
563
Bill Wendling8eeb9792008-02-26 21:11:01 +0000564 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000565
566 if (unsigned ImmOffs = ARM_AM::getAM5Offset(MO2.getImm())) {
567 O << ", #"
568 << (char)ARM_AM::getAM5Op(MO2.getImm())
569 << ImmOffs*4;
570 }
571 O << "]";
572}
573
574void ARMAsmPrinter::printAddrModePCOperand(const MachineInstr *MI, int Op,
575 const char *Modifier) {
576 if (Modifier && strcmp(Modifier, "label") == 0) {
577 printPCLabel(MI, Op+1);
578 return;
579 }
580
581 const MachineOperand &MO1 = MI->getOperand(Op);
Dan Gohman1e57df32008-02-10 18:45:23 +0000582 assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
Bill Wendling8eeb9792008-02-26 21:11:01 +0000583 O << "[pc, +" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName << "]";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000584}
585
586void
587ARMAsmPrinter::printThumbAddrModeRROperand(const MachineInstr *MI, int Op) {
588 const MachineOperand &MO1 = MI->getOperand(Op);
589 const MachineOperand &MO2 = MI->getOperand(Op+1);
Bill Wendling8eeb9792008-02-26 21:11:01 +0000590 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
591 O << ", " << TM.getRegisterInfo()->get(MO2.getReg()).AsmName << "]";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000592}
593
594void
595ARMAsmPrinter::printThumbAddrModeRI5Operand(const MachineInstr *MI, int Op,
596 unsigned Scale) {
597 const MachineOperand &MO1 = MI->getOperand(Op);
598 const MachineOperand &MO2 = MI->getOperand(Op+1);
599 const MachineOperand &MO3 = MI->getOperand(Op+2);
600
601 if (!MO1.isRegister()) { // FIXME: This is for CP entries, but isn't right.
602 printOperand(MI, Op);
603 return;
604 }
605
Bill Wendling8eeb9792008-02-26 21:11:01 +0000606 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000607 if (MO3.getReg())
Bill Wendling8eeb9792008-02-26 21:11:01 +0000608 O << ", " << TM.getRegisterInfo()->get(MO3.getReg()).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000609 else if (unsigned ImmOffs = MO2.getImm()) {
610 O << ", #" << ImmOffs;
611 if (Scale > 1)
612 O << " * " << Scale;
613 }
614 O << "]";
615}
616
617void
618ARMAsmPrinter::printThumbAddrModeS1Operand(const MachineInstr *MI, int Op) {
619 printThumbAddrModeRI5Operand(MI, Op, 1);
620}
621void
622ARMAsmPrinter::printThumbAddrModeS2Operand(const MachineInstr *MI, int Op) {
623 printThumbAddrModeRI5Operand(MI, Op, 2);
624}
625void
626ARMAsmPrinter::printThumbAddrModeS4Operand(const MachineInstr *MI, int Op) {
627 printThumbAddrModeRI5Operand(MI, Op, 4);
628}
629
630void ARMAsmPrinter::printThumbAddrModeSPOperand(const MachineInstr *MI,int Op) {
631 const MachineOperand &MO1 = MI->getOperand(Op);
632 const MachineOperand &MO2 = MI->getOperand(Op+1);
Bill Wendling8eeb9792008-02-26 21:11:01 +0000633 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000634 if (unsigned ImmOffs = MO2.getImm())
635 O << ", #" << ImmOffs << " * 4";
636 O << "]";
637}
638
639void ARMAsmPrinter::printPredicateOperand(const MachineInstr *MI, int opNum) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000640 ARMCC::CondCodes CC = (ARMCC::CondCodes)MI->getOperand(opNum).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000641 if (CC != ARMCC::AL)
642 O << ARMCondCodeToString(CC);
643}
644
645void ARMAsmPrinter::printSBitModifierOperand(const MachineInstr *MI, int opNum){
646 unsigned Reg = MI->getOperand(opNum).getReg();
647 if (Reg) {
648 assert(Reg == ARM::CPSR && "Expect ARM CPSR register!");
649 O << 's';
650 }
651}
652
653void ARMAsmPrinter::printPCLabel(const MachineInstr *MI, int opNum) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000654 int Id = (int)MI->getOperand(opNum).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000655 O << TAI->getPrivateGlobalPrefix() << "PC" << Id;
656}
657
658void ARMAsmPrinter::printRegisterList(const MachineInstr *MI, int opNum) {
659 O << "{";
660 for (unsigned i = opNum, e = MI->getNumOperands(); i != e; ++i) {
661 printOperand(MI, i);
662 if (i != e-1) O << ", ";
663 }
664 O << "}";
665}
666
667void ARMAsmPrinter::printCPInstOperand(const MachineInstr *MI, int OpNo,
668 const char *Modifier) {
669 assert(Modifier && "This operand only works with a modifier!");
670 // There are two aspects to a CONSTANTPOOL_ENTRY operand, the label and the
671 // data itself.
672 if (!strcmp(Modifier, "label")) {
673 unsigned ID = MI->getOperand(OpNo).getImm();
Evan Cheng477013c2007-10-14 05:57:21 +0000674 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
675 << '_' << ID << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000676 } else {
677 assert(!strcmp(Modifier, "cpentry") && "Unknown modifier for CPE");
Chris Lattner6017d482007-12-30 23:10:15 +0000678 unsigned CPI = MI->getOperand(OpNo).getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000679
680 const MachineConstantPoolEntry &MCPE = // Chasing pointers is fun?
681 MI->getParent()->getParent()->getConstantPool()->getConstants()[CPI];
682
683 if (MCPE.isMachineConstantPoolEntry())
684 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
685 else {
686 EmitGlobalConstant(MCPE.Val.ConstVal);
687 // remember to emit the weak reference
688 if (const GlobalValue *GV = dyn_cast<GlobalValue>(MCPE.Val.ConstVal))
689 if (GV->hasExternalWeakLinkage())
690 ExtWeakSymbols.insert(GV);
691 }
692 }
693}
694
695void ARMAsmPrinter::printJTBlockOperand(const MachineInstr *MI, int OpNo) {
696 const MachineOperand &MO1 = MI->getOperand(OpNo);
697 const MachineOperand &MO2 = MI->getOperand(OpNo+1); // Unique Id
Chris Lattner6017d482007-12-30 23:10:15 +0000698 unsigned JTI = MO1.getIndex();
Evan Cheng477013c2007-10-14 05:57:21 +0000699 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattnera96056a2007-12-30 20:49:49 +0000700 << '_' << JTI << '_' << MO2.getImm() << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000701
702 const char *JTEntryDirective = TAI->getJumpTableDirective();
703 if (!JTEntryDirective)
704 JTEntryDirective = TAI->getData32bitsDirective();
705
706 const MachineFunction *MF = MI->getParent()->getParent();
Dan Gohmane4da6412008-07-07 20:06:06 +0000707 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000708 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
709 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
710 bool UseSet= TAI->getSetDirective() && TM.getRelocationModel() == Reloc::PIC_;
711 std::set<MachineBasicBlock*> JTSets;
712 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
713 MachineBasicBlock *MBB = JTBBs[i];
714 if (UseSet && JTSets.insert(MBB).second)
Chris Lattnera96056a2007-12-30 20:49:49 +0000715 printPICJumpTableSetLabel(JTI, MO2.getImm(), MBB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000716
717 O << JTEntryDirective << ' ';
718 if (UseSet)
Evan Cheng477013c2007-10-14 05:57:21 +0000719 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
Chris Lattnera96056a2007-12-30 20:49:49 +0000720 << '_' << JTI << '_' << MO2.getImm()
Evan Cheng477013c2007-10-14 05:57:21 +0000721 << "_set_" << MBB->getNumber();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000722 else if (TM.getRelocationModel() == Reloc::PIC_) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000723 printBasicBlockLabel(MBB, false, false, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000724 // If the arch uses custom Jump Table directives, don't calc relative to JT
725 if (!TAI->getJumpTableDirective())
726 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
Chris Lattnera96056a2007-12-30 20:49:49 +0000727 << getFunctionNumber() << '_' << JTI << '_' << MO2.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000728 } else
Evan Cheng45c1edb2008-02-28 00:43:03 +0000729 printBasicBlockLabel(MBB, false, false, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000730 if (i != e-1)
731 O << '\n';
732 }
733}
734
735
736bool ARMAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
737 unsigned AsmVariant, const char *ExtraCode){
738 // Does this asm operand have a single letter operand modifier?
739 if (ExtraCode && ExtraCode[0]) {
740 if (ExtraCode[1] != 0) return true; // Unknown modifier.
741
742 switch (ExtraCode[0]) {
743 default: return true; // Unknown modifier.
744 case 'c': // Don't print "$" before a global var name or constant.
745 case 'P': // Print a VFP double precision register.
746 printOperand(MI, OpNo);
747 return false;
748 case 'Q':
749 if (TM.getTargetData()->isLittleEndian())
750 break;
751 // Fallthrough
752 case 'R':
753 if (TM.getTargetData()->isBigEndian())
754 break;
755 // Fallthrough
756 case 'H': // Write second word of DI / DF reference.
757 // Verify that this operand has two consecutive registers.
758 if (!MI->getOperand(OpNo).isRegister() ||
759 OpNo+1 == MI->getNumOperands() ||
760 !MI->getOperand(OpNo+1).isRegister())
761 return true;
762 ++OpNo; // Return the high-part.
763 }
764 }
765
766 printOperand(MI, OpNo);
767 return false;
768}
769
770void ARMAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
771 ++EmittedInsts;
772
773 int Opc = MI->getOpcode();
774 switch (Opc) {
775 case ARM::CONSTPOOL_ENTRY:
776 if (!InCPMode && AFI->isThumbFunction()) {
777 EmitAlignment(2);
778 InCPMode = true;
779 }
780 break;
781 default: {
782 if (InCPMode && AFI->isThumbFunction())
783 InCPMode = false;
784 switch (Opc) {
785 case ARM::PICADD:
786 case ARM::PICLD:
787 case ARM::PICLDZH:
788 case ARM::PICLDZB:
789 case ARM::PICLDH:
790 case ARM::PICLDB:
791 case ARM::PICLDSH:
792 case ARM::PICLDSB:
793 case ARM::PICSTR:
794 case ARM::PICSTRH:
795 case ARM::PICSTRB:
796 case ARM::tPICADD:
797 break;
798 default:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000799 break;
800 }
801 }}
802
803 // Call the autogenerated instruction printer routines.
804 printInstruction(MI);
805}
806
807bool ARMAsmPrinter::doInitialization(Module &M) {
808 // Emit initial debug information.
809 DW.BeginModule(&M);
810
Dan Gohman4a558a32007-07-25 19:33:14 +0000811 bool Result = AsmPrinter::doInitialization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000812
Dale Johannesen2f85df12008-07-09 21:20:54 +0000813 // AsmPrinter::doInitialization should have done this analysis.
814 MMI = getAnalysisToUpdate<MachineModuleInfo>();
815 assert(MMI);
816 DW.SetModuleInfo(MMI);
817
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000818 // Darwin wants symbols to be quoted if they have complex names.
819 if (Subtarget->isTargetDarwin())
820 Mang->setUseQuotes(true);
821
Dan Gohman4a558a32007-07-25 19:33:14 +0000822 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000823}
824
Chris Lattner2b638a72008-02-15 19:04:54 +0000825/// PrintUnmangledNameSafely - Print out the printable characters in the name.
826/// Don't print things like \n or \0.
827static void PrintUnmangledNameSafely(const Value *V, std::ostream &OS) {
828 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
829 Name != E; ++Name)
830 if (isprint(*Name))
831 OS << *Name;
832}
833
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000834void ARMAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000835 const TargetData *TD = TM.getTargetData();
836
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000837 if (!GVar->hasInitializer()) // External global require no code
838 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000839
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000840 // Check to see if this is a special global used by LLVM, if so, emit it.
841
842 if (EmitSpecialLLVMGlobal(GVar)) {
843 if (Subtarget->isTargetDarwin() &&
844 TM.getRelocationModel() == Reloc::Static) {
845 if (GVar->getName() == "llvm.global_ctors")
846 O << ".reference .constructors_used\n";
847 else if (GVar->getName() == "llvm.global_dtors")
848 O << ".reference .destructors_used\n";
849 }
850 return;
851 }
852
853 std::string SectionName = TAI->SectionForGlobal(GVar);
854 std::string name = Mang->getValueName(GVar);
855 Constant *C = GVar->getInitializer();
856 const Type *Type = C->getType();
857 unsigned Size = TD->getABITypeSize(Type);
858 unsigned Align = TD->getPreferredAlignmentLog(GVar);
859
860 const char *VisibilityDirective = NULL;
861 if (GVar->hasHiddenVisibility())
862 VisibilityDirective = TAI->getHiddenDirective();
863 else if (GVar->hasProtectedVisibility())
864 VisibilityDirective = TAI->getProtectedDirective();
865
866 if (VisibilityDirective)
867 O << VisibilityDirective << name << "\n";
868
869 if (Subtarget->isTargetELF())
870 O << "\t.type " << name << ",%object\n";
871
872 SwitchToDataSection(SectionName.c_str());
873
874 if (C->isNullValue() && !GVar->hasSection() && !GVar->isThreadLocal()) {
875 // FIXME: This seems to be pretty darwin-specific
876
877 if (GVar->hasExternalLinkage()) {
878 if (const char *Directive = TAI->getZeroFillDirective()) {
879 O << "\t.globl\t" << name << "\n";
880 O << Directive << "__DATA, __common, " << name << ", "
881 << Size << ", " << Align << "\n";
882 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000883 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000884 }
885
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000886 if (GVar->hasInternalLinkage() || GVar->isWeakForLinker()) {
887 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000888
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000889 if (TAI->getLCOMMDirective() != NULL) {
890 if (GVar->hasInternalLinkage()) {
891 O << TAI->getLCOMMDirective() << name << "," << Size;
892 if (Subtarget->isTargetDarwin())
893 O << "," << Align;
894 } else
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000895 O << TAI->getCOMMDirective() << name << "," << Size;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000896 } else {
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000897 if (GVar->hasInternalLinkage())
898 O << "\t.local\t" << name << "\n";
899 O << TAI->getCOMMDirective() << name << "," << Size;
900 if (TAI->getCOMMDirectiveTakesAlignment())
901 O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000902 }
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000903 O << "\t\t" << TAI->getCommentString() << " ";
904 PrintUnmangledNameSafely(GVar, O);
905 O << "\n";
906 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000907 }
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000908 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000909
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000910 switch (GVar->getLinkage()) {
911 case GlobalValue::LinkOnceLinkage:
912 case GlobalValue::WeakLinkage:
913 if (Subtarget->isTargetDarwin()) {
914 O << "\t.globl " << name << "\n"
915 << "\t.weak_definition " << name << "\n";
916 } else {
917 O << "\t.weak " << name << "\n";
918 }
919 break;
920 case GlobalValue::AppendingLinkage:
921 // FIXME: appending linkage variables should go into a section of
922 // their name or something. For now, just emit them as external.
923 case GlobalValue::ExternalLinkage:
924 O << "\t.globl " << name << "\n";
925 // FALL THROUGH
926 case GlobalValue::InternalLinkage:
927 break;
928 default:
929 assert(0 && "Unknown linkage type!");
930 break;
931 }
932
933 EmitAlignment(Align, GVar);
934 O << name << ":\t\t\t\t" << TAI->getCommentString() << " ";
935 PrintUnmangledNameSafely(GVar, O);
936 O << "\n";
937 if (TAI->hasDotTypeDotSizeDirective())
938 O << "\t.size " << name << ", " << Size << "\n";
939
940 // If the initializer is a extern weak symbol, remember to emit the weak
941 // reference!
942 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
943 if (GV->hasExternalWeakLinkage())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000944 ExtWeakSymbols.insert(GV);
945
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000946 EmitGlobalConstant(C);
947 O << '\n';
948}
949
950
951bool ARMAsmPrinter::doFinalization(Module &M) {
952 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
953 I != E; ++I)
954 printModuleLevelGV(I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000955
956 if (Subtarget->isTargetDarwin()) {
957 SwitchToDataSection("");
958
959 // Output stubs for dynamically-linked functions
960 unsigned j = 1;
961 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
962 i != e; ++i, ++j) {
963 if (TM.getRelocationModel() == Reloc::PIC_)
964 SwitchToTextSection(".section __TEXT,__picsymbolstub4,symbol_stubs,"
965 "none,16", 0);
966 else
967 SwitchToTextSection(".section __TEXT,__symbol_stub4,symbol_stubs,"
968 "none,12", 0);
969
970 EmitAlignment(2);
971 O << "\t.code\t32\n";
972
Dale Johannesena21b5202008-05-19 21:38:18 +0000973 std::string p = *i;
974 printSuffixedName(p, "$stub");
975 O << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000976 O << "\t.indirect_symbol " << *i << "\n";
Dale Johannesena21b5202008-05-19 21:38:18 +0000977 O << "\tldr ip, ";
978 printSuffixedName(p, "$slp");
979 O << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000980 if (TM.getRelocationModel() == Reloc::PIC_) {
Dale Johannesena21b5202008-05-19 21:38:18 +0000981 printSuffixedName(p, "$scv");
982 O << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000983 O << "\tadd ip, pc, ip\n";
984 }
985 O << "\tldr pc, [ip, #0]\n";
Dale Johannesena21b5202008-05-19 21:38:18 +0000986 printSuffixedName(p, "$slp");
987 O << ":\n";
988 O << "\t.long\t";
989 printSuffixedName(p, "$lazy_ptr");
990 if (TM.getRelocationModel() == Reloc::PIC_) {
991 O << "-(";
992 printSuffixedName(p, "$scv");
993 O << "+8)\n";
994 } else
995 O << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000996 SwitchToDataSection(".lazy_symbol_pointer", 0);
Dale Johannesena21b5202008-05-19 21:38:18 +0000997 printSuffixedName(p, "$lazy_ptr");
998 O << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000999 O << "\t.indirect_symbol " << *i << "\n";
1000 O << "\t.long\tdyld_stub_binding_helper\n";
1001 }
1002 O << "\n";
1003
1004 // Output non-lazy-pointers for external and common global variables.
Dan Gohman3f7d94b2007-10-03 19:26:29 +00001005 if (!GVNonLazyPtrs.empty())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001006 SwitchToDataSection(".non_lazy_symbol_pointer", 0);
1007 for (std::set<std::string>::iterator i = GVNonLazyPtrs.begin(),
1008 e = GVNonLazyPtrs.end(); i != e; ++i) {
Dale Johannesena21b5202008-05-19 21:38:18 +00001009 std::string p = *i;
1010 printSuffixedName(p, "$non_lazy_ptr");
1011 O << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001012 O << "\t.indirect_symbol " << *i << "\n";
1013 O << "\t.long\t0\n";
1014 }
1015
1016 // Emit initial debug information.
1017 DW.EndModule();
1018
1019 // Funny Darwin hack: This flag tells the linker that no global symbols
1020 // contain code that falls through to other global symbols (e.g. the obvious
1021 // implementation of multiple entry points). If this doesn't occur, the
1022 // linker can safely perform dead code stripping. Since LLVM never
1023 // generates code that does this, it is always safe to set.
1024 O << "\t.subsections_via_symbols\n";
1025 } else {
1026 // Emit final debug information for ELF.
1027 DW.EndModule();
1028 }
1029
Dan Gohman4a558a32007-07-25 19:33:14 +00001030 return AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001031}