blob: 420ccd0bcc982070e44114b6d468091af4cddac0 [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
177/// createARMCodePrinterPass - Returns a pass that prints the ARM
178/// assembly code for a MachineFunction to the given output stream,
179/// using the given target machine description. This should work
180/// regardless of whether the function is in SSA form.
181///
182FunctionPass *llvm::createARMCodePrinterPass(std::ostream &o,
183 ARMTargetMachine &tm) {
184 return new ARMAsmPrinter(o, tm, tm.getTargetAsmInfo());
185}
186
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000187// Substitute old hook with new one temporary
188std::string ARMAsmPrinter::getSectionForFunction(const Function &F) const {
189 return TAI->SectionForGlobal(&F);
190}
191
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000192/// runOnMachineFunction - This uses the printInstruction()
193/// method to print assembly for each instruction.
194///
195bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
196 AFI = MF.getInfo<ARMFunctionInfo>();
197
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000198 SetupMachineFunction(MF);
199 O << "\n";
200
201 // NOTE: we don't print out constant pools here, they are handled as
202 // instructions.
203
204 O << "\n";
205 // Print out labels for the function.
206 const Function *F = MF.getFunction();
207 switch (F->getLinkage()) {
208 default: assert(0 && "Unknown linkage type!");
209 case Function::InternalLinkage:
210 SwitchToTextSection("\t.text", F);
211 break;
212 case Function::ExternalLinkage:
213 SwitchToTextSection("\t.text", F);
214 O << "\t.globl\t" << CurrentFnName << "\n";
215 break;
216 case Function::WeakLinkage:
217 case Function::LinkOnceLinkage:
218 if (Subtarget->isTargetDarwin()) {
219 SwitchToTextSection(
220 ".section __TEXT,__textcoal_nt,coalesced,pure_instructions", F);
221 O << "\t.globl\t" << CurrentFnName << "\n";
222 O << "\t.weak_definition\t" << CurrentFnName << "\n";
223 } else {
224 O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
225 }
226 break;
227 }
228
229 const char *VisibilityDirective = NULL;
230 if (F->hasHiddenVisibility())
231 VisibilityDirective = TAI->getHiddenDirective();
232 else if (F->hasProtectedVisibility())
233 VisibilityDirective = TAI->getProtectedDirective();
234
235 if (VisibilityDirective)
236 O << VisibilityDirective << CurrentFnName << "\n";
237
238 if (AFI->isThumbFunction()) {
239 EmitAlignment(1, F, AFI->getAlign());
240 O << "\t.code\t16\n";
241 O << "\t.thumb_func";
242 if (Subtarget->isTargetDarwin())
243 O << "\t" << CurrentFnName;
244 O << "\n";
245 InCPMode = false;
246 } else
247 EmitAlignment(2, F);
248
249 O << CurrentFnName << ":\n";
250 // Emit pre-function debug information.
251 DW.BeginFunction(&MF);
252
Bill Wendling43a78162008-01-28 09:15:03 +0000253 if (Subtarget->isTargetDarwin()) {
254 // If the function is empty, then we need to emit *something*. Otherwise,
255 // the function's label might be associated with something that it wasn't
256 // meant to be associated with. We emit a noop in this situation.
257 MachineFunction::iterator I = MF.begin();
258
259 if (++I == MF.end() && MF.front().empty())
260 O << "\tnop\n";
261 }
262
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000263 // Print out code for the function.
264 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
265 I != E; ++I) {
266 // Print a label for the basic block.
267 if (I != MF.begin()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000268 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000269 O << '\n';
270 }
271 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
272 II != E; ++II) {
273 // Print the assembly for the instruction.
274 printMachineInstruction(II);
275 }
276 }
277
278 if (TAI->hasDotTypeDotSizeDirective())
279 O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
280
281 // Emit post-function debug information.
282 DW.EndFunction();
283
284 return false;
285}
286
287void ARMAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
288 const char *Modifier) {
289 const MachineOperand &MO = MI->getOperand(opNum);
290 switch (MO.getType()) {
291 case MachineOperand::MO_Register:
Dan Gohman1e57df32008-02-10 18:45:23 +0000292 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Bill Wendling8eeb9792008-02-26 21:11:01 +0000293 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000294 else
295 assert(0 && "not implemented");
296 break;
297 case MachineOperand::MO_Immediate: {
298 if (!Modifier || strcmp(Modifier, "no_hash") != 0)
299 O << "#";
300
Chris Lattnera96056a2007-12-30 20:49:49 +0000301 O << (int)MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000302 break;
303 }
304 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner6017d482007-12-30 23:10:15 +0000305 printBasicBlockLabel(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000306 return;
307 case MachineOperand::MO_GlobalAddress: {
308 bool isCallOp = Modifier && !strcmp(Modifier, "call");
309 GlobalValue *GV = MO.getGlobal();
310 std::string Name = Mang->getValueName(GV);
311 bool isExt = (GV->isDeclaration() || GV->hasWeakLinkage() ||
312 GV->hasLinkOnceLinkage());
313 if (isExt && isCallOp && Subtarget->isTargetDarwin() &&
314 TM.getRelocationModel() != Reloc::Static) {
Dale Johannesena21b5202008-05-19 21:38:18 +0000315 printSuffixedName(Name, "$stub");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000316 FnStubs.insert(Name);
317 } else
318 O << Name;
319
320 if (MO.getOffset() > 0)
321 O << '+' << MO.getOffset();
322 else if (MO.getOffset() < 0)
323 O << MO.getOffset();
324
325 if (isCallOp && Subtarget->isTargetELF() &&
326 TM.getRelocationModel() == Reloc::PIC_)
327 O << "(PLT)";
328 if (GV->hasExternalWeakLinkage())
329 ExtWeakSymbols.insert(GV);
330 break;
331 }
332 case MachineOperand::MO_ExternalSymbol: {
333 bool isCallOp = Modifier && !strcmp(Modifier, "call");
334 std::string Name(TAI->getGlobalPrefix());
335 Name += MO.getSymbolName();
336 if (isCallOp && Subtarget->isTargetDarwin() &&
337 TM.getRelocationModel() != Reloc::Static) {
Dale Johannesena21b5202008-05-19 21:38:18 +0000338 printSuffixedName(Name, "$stub");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000339 FnStubs.insert(Name);
340 } else
341 O << Name;
342 if (isCallOp && Subtarget->isTargetELF() &&
343 TM.getRelocationModel() == Reloc::PIC_)
344 O << "(PLT)";
345 break;
346 }
347 case MachineOperand::MO_ConstantPoolIndex:
Evan Cheng477013c2007-10-14 05:57:21 +0000348 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000349 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000350 break;
351 case MachineOperand::MO_JumpTableIndex:
Evan Cheng477013c2007-10-14 05:57:21 +0000352 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000353 << '_' << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000354 break;
355 default:
356 O << "<unknown operand type>"; abort (); break;
357 }
358}
359
360static void printSOImm(std::ostream &O, int64_t V, const TargetAsmInfo *TAI) {
361 assert(V < (1 << 12) && "Not a valid so_imm value!");
362 unsigned Imm = ARM_AM::getSOImmValImm(V);
363 unsigned Rot = ARM_AM::getSOImmValRot(V);
364
365 // Print low-level immediate formation info, per
366 // A5.1.3: "Data-processing operands - Immediate".
367 if (Rot) {
368 O << "#" << Imm << ", " << Rot;
369 // Pretty printed version.
370 O << ' ' << TAI->getCommentString() << ' ' << (int)ARM_AM::rotr32(Imm, Rot);
371 } else {
372 O << "#" << Imm;
373 }
374}
375
376/// printSOImmOperand - SOImm is 4-bit rotate amount in bits 8-11 with 8-bit
377/// immediate in bits 0-7.
378void ARMAsmPrinter::printSOImmOperand(const MachineInstr *MI, int OpNum) {
379 const MachineOperand &MO = MI->getOperand(OpNum);
380 assert(MO.isImmediate() && "Not a valid so_imm value!");
Chris Lattnera96056a2007-12-30 20:49:49 +0000381 printSOImm(O, MO.getImm(), TAI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000382}
383
384/// printSOImm2PartOperand - SOImm is broken into two pieces using a mov
385/// followed by a or to materialize.
386void ARMAsmPrinter::printSOImm2PartOperand(const MachineInstr *MI, int OpNum) {
387 const MachineOperand &MO = MI->getOperand(OpNum);
388 assert(MO.isImmediate() && "Not a valid so_imm value!");
Chris Lattnera96056a2007-12-30 20:49:49 +0000389 unsigned V1 = ARM_AM::getSOImmTwoPartFirst(MO.getImm());
390 unsigned V2 = ARM_AM::getSOImmTwoPartSecond(MO.getImm());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000391 printSOImm(O, ARM_AM::getSOImmVal(V1), TAI);
392 O << "\n\torr";
393 printPredicateOperand(MI, 2);
394 O << " ";
395 printOperand(MI, 0);
396 O << ", ";
397 printOperand(MI, 0);
398 O << ", ";
399 printSOImm(O, ARM_AM::getSOImmVal(V2), TAI);
400}
401
402// so_reg is a 4-operand unit corresponding to register forms of the A5.1
403// "Addressing Mode 1 - Data-processing operands" forms. This includes:
404// REG 0 0 - e.g. R5
405// REG REG 0,SH_OPC - e.g. R5, ROR R3
406// REG 0 IMM,SH_OPC - e.g. R5, LSL #3
407void ARMAsmPrinter::printSORegOperand(const MachineInstr *MI, int Op) {
408 const MachineOperand &MO1 = MI->getOperand(Op);
409 const MachineOperand &MO2 = MI->getOperand(Op+1);
410 const MachineOperand &MO3 = MI->getOperand(Op+2);
411
Dan Gohman1e57df32008-02-10 18:45:23 +0000412 assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
Bill Wendling8eeb9792008-02-26 21:11:01 +0000413 O << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000414
415 // Print the shift opc.
416 O << ", "
Chris Lattnera96056a2007-12-30 20:49:49 +0000417 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(MO3.getImm()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000418 << " ";
419
420 if (MO2.getReg()) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000421 assert(TargetRegisterInfo::isPhysicalRegister(MO2.getReg()));
Bill Wendling8eeb9792008-02-26 21:11:01 +0000422 O << TM.getRegisterInfo()->get(MO2.getReg()).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000423 assert(ARM_AM::getSORegOffset(MO3.getImm()) == 0);
424 } else {
425 O << "#" << ARM_AM::getSORegOffset(MO3.getImm());
426 }
427}
428
429void ARMAsmPrinter::printAddrMode2Operand(const MachineInstr *MI, int Op) {
430 const MachineOperand &MO1 = MI->getOperand(Op);
431 const MachineOperand &MO2 = MI->getOperand(Op+1);
432 const MachineOperand &MO3 = MI->getOperand(Op+2);
433
434 if (!MO1.isRegister()) { // FIXME: This is for CP entries, but isn't right.
435 printOperand(MI, Op);
436 return;
437 }
438
Bill Wendling8eeb9792008-02-26 21:11:01 +0000439 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000440
441 if (!MO2.getReg()) {
442 if (ARM_AM::getAM2Offset(MO3.getImm())) // Don't print +0.
443 O << ", #"
444 << (char)ARM_AM::getAM2Op(MO3.getImm())
445 << ARM_AM::getAM2Offset(MO3.getImm());
446 O << "]";
447 return;
448 }
449
450 O << ", "
451 << (char)ARM_AM::getAM2Op(MO3.getImm())
Bill Wendling8eeb9792008-02-26 21:11:01 +0000452 << TM.getRegisterInfo()->get(MO2.getReg()).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000453
454 if (unsigned ShImm = ARM_AM::getAM2Offset(MO3.getImm()))
455 O << ", "
Chris Lattnera96056a2007-12-30 20:49:49 +0000456 << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO3.getImm()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000457 << " #" << ShImm;
458 O << "]";
459}
460
461void ARMAsmPrinter::printAddrMode2OffsetOperand(const MachineInstr *MI, int Op){
462 const MachineOperand &MO1 = MI->getOperand(Op);
463 const MachineOperand &MO2 = MI->getOperand(Op+1);
464
465 if (!MO1.getReg()) {
466 unsigned ImmOffs = ARM_AM::getAM2Offset(MO2.getImm());
467 assert(ImmOffs && "Malformed indexed load / store!");
468 O << "#"
469 << (char)ARM_AM::getAM2Op(MO2.getImm())
470 << ImmOffs;
471 return;
472 }
473
474 O << (char)ARM_AM::getAM2Op(MO2.getImm())
Bill Wendling8eeb9792008-02-26 21:11:01 +0000475 << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000476
477 if (unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm()))
478 O << ", "
Chris Lattnera96056a2007-12-30 20:49:49 +0000479 << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO2.getImm()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000480 << " #" << ShImm;
481}
482
483void ARMAsmPrinter::printAddrMode3Operand(const MachineInstr *MI, int Op) {
484 const MachineOperand &MO1 = MI->getOperand(Op);
485 const MachineOperand &MO2 = MI->getOperand(Op+1);
486 const MachineOperand &MO3 = MI->getOperand(Op+2);
487
Dan Gohman1e57df32008-02-10 18:45:23 +0000488 assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
Bill Wendling8eeb9792008-02-26 21:11:01 +0000489 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000490
491 if (MO2.getReg()) {
492 O << ", "
493 << (char)ARM_AM::getAM3Op(MO3.getImm())
Bill Wendling8eeb9792008-02-26 21:11:01 +0000494 << TM.getRegisterInfo()->get(MO2.getReg()).AsmName
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000495 << "]";
496 return;
497 }
498
499 if (unsigned ImmOffs = ARM_AM::getAM3Offset(MO3.getImm()))
500 O << ", #"
501 << (char)ARM_AM::getAM3Op(MO3.getImm())
502 << ImmOffs;
503 O << "]";
504}
505
506void ARMAsmPrinter::printAddrMode3OffsetOperand(const MachineInstr *MI, int Op){
507 const MachineOperand &MO1 = MI->getOperand(Op);
508 const MachineOperand &MO2 = MI->getOperand(Op+1);
509
510 if (MO1.getReg()) {
511 O << (char)ARM_AM::getAM3Op(MO2.getImm())
Bill Wendling8eeb9792008-02-26 21:11:01 +0000512 << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000513 return;
514 }
515
516 unsigned ImmOffs = ARM_AM::getAM3Offset(MO2.getImm());
517 assert(ImmOffs && "Malformed indexed load / store!");
518 O << "#"
519 << (char)ARM_AM::getAM3Op(MO2.getImm())
520 << ImmOffs;
521}
522
523void ARMAsmPrinter::printAddrMode4Operand(const MachineInstr *MI, int Op,
524 const char *Modifier) {
525 const MachineOperand &MO1 = MI->getOperand(Op);
526 const MachineOperand &MO2 = MI->getOperand(Op+1);
527 ARM_AM::AMSubMode Mode = ARM_AM::getAM4SubMode(MO2.getImm());
528 if (Modifier && strcmp(Modifier, "submode") == 0) {
529 if (MO1.getReg() == ARM::SP) {
530 bool isLDM = (MI->getOpcode() == ARM::LDM ||
531 MI->getOpcode() == ARM::LDM_RET);
532 O << ARM_AM::getAMSubModeAltStr(Mode, isLDM);
533 } else
534 O << ARM_AM::getAMSubModeStr(Mode);
535 } else {
536 printOperand(MI, Op);
537 if (ARM_AM::getAM4WBFlag(MO2.getImm()))
538 O << "!";
539 }
540}
541
542void ARMAsmPrinter::printAddrMode5Operand(const MachineInstr *MI, int Op,
543 const char *Modifier) {
544 const MachineOperand &MO1 = MI->getOperand(Op);
545 const MachineOperand &MO2 = MI->getOperand(Op+1);
546
547 if (!MO1.isRegister()) { // FIXME: This is for CP entries, but isn't right.
548 printOperand(MI, Op);
549 return;
550 }
551
Dan Gohman1e57df32008-02-10 18:45:23 +0000552 assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000553
554 if (Modifier && strcmp(Modifier, "submode") == 0) {
555 ARM_AM::AMSubMode Mode = ARM_AM::getAM5SubMode(MO2.getImm());
556 if (MO1.getReg() == ARM::SP) {
557 bool isFLDM = (MI->getOpcode() == ARM::FLDMD ||
558 MI->getOpcode() == ARM::FLDMS);
559 O << ARM_AM::getAMSubModeAltStr(Mode, isFLDM);
560 } else
561 O << ARM_AM::getAMSubModeStr(Mode);
562 return;
563 } else if (Modifier && strcmp(Modifier, "base") == 0) {
564 // Used for FSTM{D|S} and LSTM{D|S} operations.
Bill Wendling8eeb9792008-02-26 21:11:01 +0000565 O << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000566 if (ARM_AM::getAM5WBFlag(MO2.getImm()))
567 O << "!";
568 return;
569 }
570
Bill Wendling8eeb9792008-02-26 21:11:01 +0000571 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000572
573 if (unsigned ImmOffs = ARM_AM::getAM5Offset(MO2.getImm())) {
574 O << ", #"
575 << (char)ARM_AM::getAM5Op(MO2.getImm())
576 << ImmOffs*4;
577 }
578 O << "]";
579}
580
581void ARMAsmPrinter::printAddrModePCOperand(const MachineInstr *MI, int Op,
582 const char *Modifier) {
583 if (Modifier && strcmp(Modifier, "label") == 0) {
584 printPCLabel(MI, Op+1);
585 return;
586 }
587
588 const MachineOperand &MO1 = MI->getOperand(Op);
Dan Gohman1e57df32008-02-10 18:45:23 +0000589 assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
Bill Wendling8eeb9792008-02-26 21:11:01 +0000590 O << "[pc, +" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName << "]";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000591}
592
593void
594ARMAsmPrinter::printThumbAddrModeRROperand(const MachineInstr *MI, int Op) {
595 const MachineOperand &MO1 = MI->getOperand(Op);
596 const MachineOperand &MO2 = MI->getOperand(Op+1);
Bill Wendling8eeb9792008-02-26 21:11:01 +0000597 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
598 O << ", " << TM.getRegisterInfo()->get(MO2.getReg()).AsmName << "]";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000599}
600
601void
602ARMAsmPrinter::printThumbAddrModeRI5Operand(const MachineInstr *MI, int Op,
603 unsigned Scale) {
604 const MachineOperand &MO1 = MI->getOperand(Op);
605 const MachineOperand &MO2 = MI->getOperand(Op+1);
606 const MachineOperand &MO3 = MI->getOperand(Op+2);
607
608 if (!MO1.isRegister()) { // FIXME: This is for CP entries, but isn't right.
609 printOperand(MI, Op);
610 return;
611 }
612
Bill Wendling8eeb9792008-02-26 21:11:01 +0000613 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000614 if (MO3.getReg())
Bill Wendling8eeb9792008-02-26 21:11:01 +0000615 O << ", " << TM.getRegisterInfo()->get(MO3.getReg()).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000616 else if (unsigned ImmOffs = MO2.getImm()) {
617 O << ", #" << ImmOffs;
618 if (Scale > 1)
619 O << " * " << Scale;
620 }
621 O << "]";
622}
623
624void
625ARMAsmPrinter::printThumbAddrModeS1Operand(const MachineInstr *MI, int Op) {
626 printThumbAddrModeRI5Operand(MI, Op, 1);
627}
628void
629ARMAsmPrinter::printThumbAddrModeS2Operand(const MachineInstr *MI, int Op) {
630 printThumbAddrModeRI5Operand(MI, Op, 2);
631}
632void
633ARMAsmPrinter::printThumbAddrModeS4Operand(const MachineInstr *MI, int Op) {
634 printThumbAddrModeRI5Operand(MI, Op, 4);
635}
636
637void ARMAsmPrinter::printThumbAddrModeSPOperand(const MachineInstr *MI,int Op) {
638 const MachineOperand &MO1 = MI->getOperand(Op);
639 const MachineOperand &MO2 = MI->getOperand(Op+1);
Bill Wendling8eeb9792008-02-26 21:11:01 +0000640 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000641 if (unsigned ImmOffs = MO2.getImm())
642 O << ", #" << ImmOffs << " * 4";
643 O << "]";
644}
645
646void ARMAsmPrinter::printPredicateOperand(const MachineInstr *MI, int opNum) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000647 ARMCC::CondCodes CC = (ARMCC::CondCodes)MI->getOperand(opNum).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000648 if (CC != ARMCC::AL)
649 O << ARMCondCodeToString(CC);
650}
651
652void ARMAsmPrinter::printSBitModifierOperand(const MachineInstr *MI, int opNum){
653 unsigned Reg = MI->getOperand(opNum).getReg();
654 if (Reg) {
655 assert(Reg == ARM::CPSR && "Expect ARM CPSR register!");
656 O << 's';
657 }
658}
659
660void ARMAsmPrinter::printPCLabel(const MachineInstr *MI, int opNum) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000661 int Id = (int)MI->getOperand(opNum).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000662 O << TAI->getPrivateGlobalPrefix() << "PC" << Id;
663}
664
665void ARMAsmPrinter::printRegisterList(const MachineInstr *MI, int opNum) {
666 O << "{";
667 for (unsigned i = opNum, e = MI->getNumOperands(); i != e; ++i) {
668 printOperand(MI, i);
669 if (i != e-1) O << ", ";
670 }
671 O << "}";
672}
673
674void ARMAsmPrinter::printCPInstOperand(const MachineInstr *MI, int OpNo,
675 const char *Modifier) {
676 assert(Modifier && "This operand only works with a modifier!");
677 // There are two aspects to a CONSTANTPOOL_ENTRY operand, the label and the
678 // data itself.
679 if (!strcmp(Modifier, "label")) {
680 unsigned ID = MI->getOperand(OpNo).getImm();
Evan Cheng477013c2007-10-14 05:57:21 +0000681 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
682 << '_' << ID << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000683 } else {
684 assert(!strcmp(Modifier, "cpentry") && "Unknown modifier for CPE");
Chris Lattner6017d482007-12-30 23:10:15 +0000685 unsigned CPI = MI->getOperand(OpNo).getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000686
687 const MachineConstantPoolEntry &MCPE = // Chasing pointers is fun?
688 MI->getParent()->getParent()->getConstantPool()->getConstants()[CPI];
689
Evan Chengf1012ce2008-08-08 06:56:16 +0000690 if (MCPE.isMachineConstantPoolEntry()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000691 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
Evan Chengf1012ce2008-08-08 06:56:16 +0000692 ARMConstantPoolValue *ACPV =
693 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
694 if (ACPV->getPCAdjustment() != 0) {
695 const GlobalValue *GV = ACPV->getGV();
696 PCRelGVs.insert(GV);
697 }
698 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000699 EmitGlobalConstant(MCPE.Val.ConstVal);
700 // remember to emit the weak reference
701 if (const GlobalValue *GV = dyn_cast<GlobalValue>(MCPE.Val.ConstVal))
702 if (GV->hasExternalWeakLinkage())
703 ExtWeakSymbols.insert(GV);
704 }
705 }
706}
707
708void ARMAsmPrinter::printJTBlockOperand(const MachineInstr *MI, int OpNo) {
709 const MachineOperand &MO1 = MI->getOperand(OpNo);
710 const MachineOperand &MO2 = MI->getOperand(OpNo+1); // Unique Id
Chris Lattner6017d482007-12-30 23:10:15 +0000711 unsigned JTI = MO1.getIndex();
Evan Cheng477013c2007-10-14 05:57:21 +0000712 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattnera96056a2007-12-30 20:49:49 +0000713 << '_' << JTI << '_' << MO2.getImm() << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000714
715 const char *JTEntryDirective = TAI->getJumpTableDirective();
716 if (!JTEntryDirective)
717 JTEntryDirective = TAI->getData32bitsDirective();
718
719 const MachineFunction *MF = MI->getParent()->getParent();
Dan Gohmane4da6412008-07-07 20:06:06 +0000720 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000721 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
722 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
723 bool UseSet= TAI->getSetDirective() && TM.getRelocationModel() == Reloc::PIC_;
724 std::set<MachineBasicBlock*> JTSets;
725 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
726 MachineBasicBlock *MBB = JTBBs[i];
727 if (UseSet && JTSets.insert(MBB).second)
Chris Lattnera96056a2007-12-30 20:49:49 +0000728 printPICJumpTableSetLabel(JTI, MO2.getImm(), MBB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000729
730 O << JTEntryDirective << ' ';
731 if (UseSet)
Evan Cheng477013c2007-10-14 05:57:21 +0000732 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
Chris Lattnera96056a2007-12-30 20:49:49 +0000733 << '_' << JTI << '_' << MO2.getImm()
Evan Cheng477013c2007-10-14 05:57:21 +0000734 << "_set_" << MBB->getNumber();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000735 else if (TM.getRelocationModel() == Reloc::PIC_) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000736 printBasicBlockLabel(MBB, false, false, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000737 // If the arch uses custom Jump Table directives, don't calc relative to JT
738 if (!TAI->getJumpTableDirective())
739 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
Chris Lattnera96056a2007-12-30 20:49:49 +0000740 << getFunctionNumber() << '_' << JTI << '_' << MO2.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000741 } else
Evan Cheng45c1edb2008-02-28 00:43:03 +0000742 printBasicBlockLabel(MBB, false, false, false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000743 if (i != e-1)
744 O << '\n';
745 }
746}
747
748
749bool ARMAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
750 unsigned AsmVariant, const char *ExtraCode){
751 // Does this asm operand have a single letter operand modifier?
752 if (ExtraCode && ExtraCode[0]) {
753 if (ExtraCode[1] != 0) return true; // Unknown modifier.
754
755 switch (ExtraCode[0]) {
756 default: return true; // Unknown modifier.
757 case 'c': // Don't print "$" before a global var name or constant.
758 case 'P': // Print a VFP double precision register.
759 printOperand(MI, OpNo);
760 return false;
761 case 'Q':
762 if (TM.getTargetData()->isLittleEndian())
763 break;
764 // Fallthrough
765 case 'R':
766 if (TM.getTargetData()->isBigEndian())
767 break;
768 // Fallthrough
769 case 'H': // Write second word of DI / DF reference.
770 // Verify that this operand has two consecutive registers.
771 if (!MI->getOperand(OpNo).isRegister() ||
772 OpNo+1 == MI->getNumOperands() ||
773 !MI->getOperand(OpNo+1).isRegister())
774 return true;
775 ++OpNo; // Return the high-part.
776 }
777 }
778
779 printOperand(MI, OpNo);
780 return false;
781}
782
783void ARMAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
784 ++EmittedInsts;
785
786 int Opc = MI->getOpcode();
787 switch (Opc) {
788 case ARM::CONSTPOOL_ENTRY:
789 if (!InCPMode && AFI->isThumbFunction()) {
790 EmitAlignment(2);
791 InCPMode = true;
792 }
793 break;
794 default: {
795 if (InCPMode && AFI->isThumbFunction())
796 InCPMode = false;
797 switch (Opc) {
798 case ARM::PICADD:
799 case ARM::PICLD:
800 case ARM::PICLDZH:
801 case ARM::PICLDZB:
802 case ARM::PICLDH:
803 case ARM::PICLDB:
804 case ARM::PICLDSH:
805 case ARM::PICLDSB:
806 case ARM::PICSTR:
807 case ARM::PICSTRH:
808 case ARM::PICSTRB:
809 case ARM::tPICADD:
810 break;
811 default:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000812 break;
813 }
814 }}
815
816 // Call the autogenerated instruction printer routines.
817 printInstruction(MI);
818}
819
820bool ARMAsmPrinter::doInitialization(Module &M) {
821 // Emit initial debug information.
822 DW.BeginModule(&M);
823
Dan Gohman4a558a32007-07-25 19:33:14 +0000824 bool Result = AsmPrinter::doInitialization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000825
Dale Johannesen2f85df12008-07-09 21:20:54 +0000826 // AsmPrinter::doInitialization should have done this analysis.
827 MMI = getAnalysisToUpdate<MachineModuleInfo>();
828 assert(MMI);
829 DW.SetModuleInfo(MMI);
830
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000831 // Darwin wants symbols to be quoted if they have complex names.
832 if (Subtarget->isTargetDarwin())
833 Mang->setUseQuotes(true);
834
Dan Gohman4a558a32007-07-25 19:33:14 +0000835 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000836}
837
Chris Lattner2b638a72008-02-15 19:04:54 +0000838/// PrintUnmangledNameSafely - Print out the printable characters in the name.
839/// Don't print things like \n or \0.
840static void PrintUnmangledNameSafely(const Value *V, std::ostream &OS) {
841 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
842 Name != E; ++Name)
843 if (isprint(*Name))
844 OS << *Name;
845}
846
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000847void ARMAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000848 const TargetData *TD = TM.getTargetData();
849
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000850 if (!GVar->hasInitializer()) // External global require no code
851 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000852
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000853 // Check to see if this is a special global used by LLVM, if so, emit it.
854
855 if (EmitSpecialLLVMGlobal(GVar)) {
856 if (Subtarget->isTargetDarwin() &&
857 TM.getRelocationModel() == Reloc::Static) {
858 if (GVar->getName() == "llvm.global_ctors")
859 O << ".reference .constructors_used\n";
860 else if (GVar->getName() == "llvm.global_dtors")
861 O << ".reference .destructors_used\n";
862 }
863 return;
864 }
865
Evan Chengf1012ce2008-08-08 06:56:16 +0000866 bool NoCoalesc = PCRelGVs.count(GVar);
867 std::string SectionName = TAI->SectionForGlobal(GVar, NoCoalesc);
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000868 std::string name = Mang->getValueName(GVar);
869 Constant *C = GVar->getInitializer();
870 const Type *Type = C->getType();
871 unsigned Size = TD->getABITypeSize(Type);
872 unsigned Align = TD->getPreferredAlignmentLog(GVar);
873
874 const char *VisibilityDirective = NULL;
875 if (GVar->hasHiddenVisibility())
876 VisibilityDirective = TAI->getHiddenDirective();
877 else if (GVar->hasProtectedVisibility())
878 VisibilityDirective = TAI->getProtectedDirective();
879
880 if (VisibilityDirective)
881 O << VisibilityDirective << name << "\n";
882
883 if (Subtarget->isTargetELF())
884 O << "\t.type " << name << ",%object\n";
885
886 SwitchToDataSection(SectionName.c_str());
887
888 if (C->isNullValue() && !GVar->hasSection() && !GVar->isThreadLocal()) {
889 // FIXME: This seems to be pretty darwin-specific
890
891 if (GVar->hasExternalLinkage()) {
892 if (const char *Directive = TAI->getZeroFillDirective()) {
893 O << "\t.globl\t" << name << "\n";
894 O << Directive << "__DATA, __common, " << name << ", "
895 << Size << ", " << Align << "\n";
896 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000897 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000898 }
899
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000900 if (GVar->hasInternalLinkage() || GVar->isWeakForLinker()) {
901 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000902
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000903 if (TAI->getLCOMMDirective() != NULL) {
Evan Chengf1012ce2008-08-08 06:56:16 +0000904 if (NoCoalesc || GVar->hasInternalLinkage()) {
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000905 O << TAI->getLCOMMDirective() << name << "," << Size;
906 if (Subtarget->isTargetDarwin())
907 O << "," << Align;
908 } else
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000909 O << TAI->getCOMMDirective() << name << "," << Size;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000910 } else {
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000911 if (GVar->hasInternalLinkage())
912 O << "\t.local\t" << name << "\n";
913 O << TAI->getCOMMDirective() << name << "," << Size;
914 if (TAI->getCOMMDirectiveTakesAlignment())
915 O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000916 }
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000917 O << "\t\t" << TAI->getCommentString() << " ";
918 PrintUnmangledNameSafely(GVar, O);
919 O << "\n";
920 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000921 }
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000922 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000923
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000924 switch (GVar->getLinkage()) {
925 case GlobalValue::LinkOnceLinkage:
926 case GlobalValue::WeakLinkage:
927 if (Subtarget->isTargetDarwin()) {
928 O << "\t.globl " << name << "\n"
929 << "\t.weak_definition " << name << "\n";
930 } else {
931 O << "\t.weak " << name << "\n";
932 }
933 break;
934 case GlobalValue::AppendingLinkage:
935 // FIXME: appending linkage variables should go into a section of
936 // their name or something. For now, just emit them as external.
937 case GlobalValue::ExternalLinkage:
938 O << "\t.globl " << name << "\n";
939 // FALL THROUGH
940 case GlobalValue::InternalLinkage:
941 break;
942 default:
943 assert(0 && "Unknown linkage type!");
944 break;
945 }
946
947 EmitAlignment(Align, GVar);
948 O << name << ":\t\t\t\t" << TAI->getCommentString() << " ";
949 PrintUnmangledNameSafely(GVar, O);
950 O << "\n";
951 if (TAI->hasDotTypeDotSizeDirective())
952 O << "\t.size " << name << ", " << Size << "\n";
953
954 // If the initializer is a extern weak symbol, remember to emit the weak
955 // reference!
956 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
957 if (GV->hasExternalWeakLinkage())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000958 ExtWeakSymbols.insert(GV);
959
Anton Korobeynikov3cc6efa2008-08-07 09:54:23 +0000960 EmitGlobalConstant(C);
961 O << '\n';
962}
963
964
965bool ARMAsmPrinter::doFinalization(Module &M) {
966 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
967 I != E; ++I)
968 printModuleLevelGV(I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000969
970 if (Subtarget->isTargetDarwin()) {
971 SwitchToDataSection("");
972
973 // Output stubs for dynamically-linked functions
974 unsigned j = 1;
975 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
976 i != e; ++i, ++j) {
977 if (TM.getRelocationModel() == Reloc::PIC_)
978 SwitchToTextSection(".section __TEXT,__picsymbolstub4,symbol_stubs,"
979 "none,16", 0);
980 else
981 SwitchToTextSection(".section __TEXT,__symbol_stub4,symbol_stubs,"
982 "none,12", 0);
983
984 EmitAlignment(2);
985 O << "\t.code\t32\n";
986
Dale Johannesena21b5202008-05-19 21:38:18 +0000987 std::string p = *i;
988 printSuffixedName(p, "$stub");
989 O << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000990 O << "\t.indirect_symbol " << *i << "\n";
Dale Johannesena21b5202008-05-19 21:38:18 +0000991 O << "\tldr ip, ";
992 printSuffixedName(p, "$slp");
993 O << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000994 if (TM.getRelocationModel() == Reloc::PIC_) {
Dale Johannesena21b5202008-05-19 21:38:18 +0000995 printSuffixedName(p, "$scv");
996 O << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000997 O << "\tadd ip, pc, ip\n";
998 }
999 O << "\tldr pc, [ip, #0]\n";
Dale Johannesena21b5202008-05-19 21:38:18 +00001000 printSuffixedName(p, "$slp");
1001 O << ":\n";
1002 O << "\t.long\t";
1003 printSuffixedName(p, "$lazy_ptr");
1004 if (TM.getRelocationModel() == Reloc::PIC_) {
1005 O << "-(";
1006 printSuffixedName(p, "$scv");
1007 O << "+8)\n";
1008 } else
1009 O << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001010 SwitchToDataSection(".lazy_symbol_pointer", 0);
Dale Johannesena21b5202008-05-19 21:38:18 +00001011 printSuffixedName(p, "$lazy_ptr");
1012 O << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001013 O << "\t.indirect_symbol " << *i << "\n";
1014 O << "\t.long\tdyld_stub_binding_helper\n";
1015 }
1016 O << "\n";
1017
1018 // Output non-lazy-pointers for external and common global variables.
Dan Gohman3f7d94b2007-10-03 19:26:29 +00001019 if (!GVNonLazyPtrs.empty())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001020 SwitchToDataSection(".non_lazy_symbol_pointer", 0);
1021 for (std::set<std::string>::iterator i = GVNonLazyPtrs.begin(),
1022 e = GVNonLazyPtrs.end(); i != e; ++i) {
Dale Johannesena21b5202008-05-19 21:38:18 +00001023 std::string p = *i;
1024 printSuffixedName(p, "$non_lazy_ptr");
1025 O << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001026 O << "\t.indirect_symbol " << *i << "\n";
1027 O << "\t.long\t0\n";
1028 }
1029
1030 // Emit initial debug information.
1031 DW.EndModule();
1032
1033 // Funny Darwin hack: This flag tells the linker that no global symbols
1034 // contain code that falls through to other global symbols (e.g. the obvious
1035 // implementation of multiple entry points). If this doesn't occur, the
1036 // linker can safely perform dead code stripping. Since LLVM never
1037 // generates code that does this, it is always safe to set.
1038 O << "\t.subsections_via_symbols\n";
1039 } else {
1040 // Emit final debug information for ELF.
1041 DW.EndModule();
1042 }
1043
Dan Gohman4a558a32007-07-25 19:33:14 +00001044 return AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001045}