blob: 03ac17bf96105b89325f9ecbbef23b9563c7b62e [file] [log] [blame]
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00001//===-- ARMAsmPrinter.cpp - ARM LLVM assembly writer ----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the "Instituto Nokia de Tecnologia" and
6// is distributed under the University of Illinois Open Source
7// License. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10//
11// This file contains a printer that converts from our internal representation
12// of machine-dependent LLVM code to GAS-format ARM assembly language.
13//
14//===----------------------------------------------------------------------===//
15
Chris Lattner95b2c7d2006-12-19 22:59:26 +000016#define DEBUG_TYPE "asm-printer"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000017#include "ARM.h"
Evan Chenga8e29892007-01-19 07:51:42 +000018#include "ARMTargetMachine.h"
19#include "ARMAddressingModes.h"
20#include "ARMConstantPoolValue.h"
21#include "ARMMachineFunctionInfo.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000022#include "llvm/Constants.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000023#include "llvm/Module.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000024#include "llvm/CodeGen/AsmPrinter.h"
Evan Chenga8e29892007-01-19 07:51:42 +000025#include "llvm/CodeGen/DwarfWriter.h"
Jim Laskey44c3b9f2007-01-26 21:22:28 +000026#include "llvm/CodeGen/MachineModuleInfo.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000027#include "llvm/CodeGen/MachineFunctionPass.h"
Evan Chenga8e29892007-01-19 07:51:42 +000028#include "llvm/CodeGen/MachineJumpTableInfo.h"
Jim Laskey563321a2006-09-06 18:34:40 +000029#include "llvm/Target/TargetAsmInfo.h"
Rafael Espindolab01c4bb2006-07-27 11:38:51 +000030#include "llvm/Target/TargetData.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000031#include "llvm/Target/TargetMachine.h"
Evan Cheng5be54b02007-01-19 19:25:36 +000032#include "llvm/Target/TargetOptions.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000033#include "llvm/ADT/Statistic.h"
34#include "llvm/ADT/StringExtras.h"
Evan Chenga8e29892007-01-19 07:51:42 +000035#include "llvm/Support/Compiler.h"
36#include "llvm/Support/Mangler.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000037#include "llvm/Support/MathExtras.h"
38#include <cctype>
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000039using namespace llvm;
40
Chris Lattner95b2c7d2006-12-19 22:59:26 +000041STATISTIC(EmittedInsts, "Number of machine instrs printed");
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000042
Chris Lattner95b2c7d2006-12-19 22:59:26 +000043namespace {
Jim Laskey563321a2006-09-06 18:34:40 +000044 struct VISIBILITY_HIDDEN ARMAsmPrinter : public AsmPrinter {
Jim Laskeya0f3d172006-09-07 22:06:40 +000045 ARMAsmPrinter(std::ostream &O, TargetMachine &TM, const TargetAsmInfo *T)
Evan Chenga8e29892007-01-19 07:51:42 +000046 : AsmPrinter(O, TM, T), DW(O, this, T), AFI(NULL), InCPMode(false) {
47 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Jim Laskey563321a2006-09-06 18:34:40 +000048 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000049
Evan Chenga8e29892007-01-19 07:51:42 +000050 DwarfWriter DW;
51
52 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
53 /// make the right decision when printing asm code for different targets.
54 const ARMSubtarget *Subtarget;
55
56 /// AFI - Keep a pointer to ARMFunctionInfo for the current
57 /// MachineFunction
58 ARMFunctionInfo *AFI;
59
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000060 /// We name each basic block in a Function with a unique number, so
61 /// that we can consistently refer to them later. This is cleared
62 /// at the beginning of each call to runOnMachineFunction().
63 ///
64 typedef std::map<const Value *, unsigned> ValueMapTy;
65 ValueMapTy NumberForBB;
66
Evan Chenga8e29892007-01-19 07:51:42 +000067 /// Keeps the set of GlobalValues that require non-lazy-pointers for
68 /// indirect access.
69 std::set<std::string> GVNonLazyPtrs;
70
71 /// Keeps the set of external function GlobalAddresses that the asm
72 /// printer should generate stubs for.
73 std::set<std::string> FnStubs;
74
75 /// True if asm printer is printing a series of CONSTPOOL_ENTRY.
76 bool InCPMode;
77
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000078 virtual const char *getPassName() const {
79 return "ARM Assembly Printer";
80 }
81
Evan Chenga8e29892007-01-19 07:51:42 +000082 void printOperand(const MachineInstr *MI, int opNum,
83 const char *Modifier = 0);
84 void printSOImmOperand(const MachineInstr *MI, int opNum);
85 void printSORegOperand(const MachineInstr *MI, int opNum);
86 void printAddrMode2Operand(const MachineInstr *MI, int OpNo);
87 void printAddrMode2OffsetOperand(const MachineInstr *MI, int OpNo);
88 void printAddrMode3Operand(const MachineInstr *MI, int OpNo);
89 void printAddrMode3OffsetOperand(const MachineInstr *MI, int OpNo);
90 void printAddrMode4Operand(const MachineInstr *MI, int OpNo,
91 const char *Modifier = 0);
92 void printAddrMode5Operand(const MachineInstr *MI, int OpNo,
93 const char *Modifier = 0);
94 void printAddrModePCOperand(const MachineInstr *MI, int OpNo,
95 const char *Modifier = 0);
96 void printThumbAddrModeRROperand(const MachineInstr *MI, int OpNo);
97 void printThumbAddrModeRI5Operand(const MachineInstr *MI, int OpNo,
98 unsigned Scale);
Evan Chengc38f2bc2007-01-23 22:59:13 +000099 void printThumbAddrModeS1Operand(const MachineInstr *MI, int OpNo);
100 void printThumbAddrModeS2Operand(const MachineInstr *MI, int OpNo);
101 void printThumbAddrModeS4Operand(const MachineInstr *MI, int OpNo);
Evan Chenga8e29892007-01-19 07:51:42 +0000102 void printThumbAddrModeSPOperand(const MachineInstr *MI, int OpNo);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000103 void printCCOperand(const MachineInstr *MI, int opNum);
Evan Chenga8e29892007-01-19 07:51:42 +0000104 void printPCLabel(const MachineInstr *MI, int opNum);
105 void printRegisterList(const MachineInstr *MI, int opNum);
106 void printCPInstOperand(const MachineInstr *MI, int opNum,
107 const char *Modifier);
108 void printJTBlockOperand(const MachineInstr *MI, int opNum);
109
110 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
111 unsigned AsmVariant, const char *ExtraCode);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000112
113 bool printInstruction(const MachineInstr *MI); // autogenerated.
Evan Chenga8e29892007-01-19 07:51:42 +0000114 void printMachineInstruction(const MachineInstr *MI);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000115 bool runOnMachineFunction(MachineFunction &F);
116 bool doInitialization(Module &M);
117 bool doFinalization(Module &M);
Evan Chenga8e29892007-01-19 07:51:42 +0000118
119 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
120 printDataDirective(MCPV->getType());
121
122 ARMConstantPoolValue *ACPV = (ARMConstantPoolValue*)MCPV;
Lauro Ramos Venancio1a92d942007-01-26 19:51:32 +0000123 GlobalValue *GV = ACPV->getGV();
Evan Chengc60e76d2007-01-30 20:37:08 +0000124 std::string Name = GV ? Mang->getValueName(GV) : TAI->getGlobalPrefix();
125 if (!GV)
126 Name += ACPV->getSymbol();
Evan Chenga8e29892007-01-19 07:51:42 +0000127 if (ACPV->isNonLazyPointer()) {
128 GVNonLazyPtrs.insert(Name);
129 O << TAI->getPrivateGlobalPrefix() << Name << "$non_lazy_ptr";
Evan Chengc60e76d2007-01-30 20:37:08 +0000130 } else if (ACPV->isStub()) {
131 FnStubs.insert(Name);
132 O << TAI->getPrivateGlobalPrefix() << Name << "$stub";
Evan Chenga8e29892007-01-19 07:51:42 +0000133 } else
134 O << Name;
135 if (ACPV->getPCAdjustment() != 0)
136 O << "-(" << TAI->getPrivateGlobalPrefix() << "PC"
137 << utostr(ACPV->getLabelId())
138 << "+" << (unsigned)ACPV->getPCAdjustment() << ")";
139 O << "\n";
Lauro Ramos Venancio1a92d942007-01-26 19:51:32 +0000140
141 // If the constant pool value is a extern weak symbol, remember to emit
142 // the weak reference.
Evan Chengc60e76d2007-01-30 20:37:08 +0000143 if (GV && GV->hasExternalWeakLinkage())
Lauro Ramos Venancio1a92d942007-01-26 19:51:32 +0000144 ExtWeakSymbols.insert(GV);
Evan Chenga8e29892007-01-19 07:51:42 +0000145 }
146
147 void getAnalysisUsage(AnalysisUsage &AU) const {
148 AU.setPreservesAll();
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000149 AU.addRequired<MachineModuleInfo>();
Evan Chenga8e29892007-01-19 07:51:42 +0000150 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000151 };
152} // end of anonymous namespace
153
154#include "ARMGenAsmWriter.inc"
155
156/// createARMCodePrinterPass - Returns a pass that prints the ARM
157/// assembly code for a MachineFunction to the given output stream,
158/// using the given target machine description. This should work
159/// regardless of whether the function is in SSA form.
160///
161FunctionPass *llvm::createARMCodePrinterPass(std::ostream &o,
Evan Chenga8e29892007-01-19 07:51:42 +0000162 ARMTargetMachine &tm) {
Jim Laskeya0f3d172006-09-07 22:06:40 +0000163 return new ARMAsmPrinter(o, tm, tm.getTargetAsmInfo());
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000164}
165
Evan Chenga8e29892007-01-19 07:51:42 +0000166/// runOnMachineFunction - This uses the printInstruction()
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000167/// method to print assembly for each instruction.
168///
169bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Evan Chenga8e29892007-01-19 07:51:42 +0000170 AFI = MF.getInfo<ARMFunctionInfo>();
Rafael Espindola4b442b52006-05-23 02:48:20 +0000171
Evan Cheng5be54b02007-01-19 19:25:36 +0000172 if (Subtarget->isTargetDarwin()) {
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000173 DW.SetModuleInfo(&getAnalysis<MachineModuleInfo>());
Chris Lattner1a199de2006-12-21 22:59:58 +0000174 }
175
Evan Chenga8e29892007-01-19 07:51:42 +0000176 SetupMachineFunction(MF);
177 O << "\n";
Rafael Espindola4b442b52006-05-23 02:48:20 +0000178
Evan Chenga8e29892007-01-19 07:51:42 +0000179 // NOTE: we don't print out constant pools here, they are handled as
180 // instructions.
181
182 O << "\n";
Rafael Espindola4b442b52006-05-23 02:48:20 +0000183 // Print out labels for the function.
184 const Function *F = MF.getFunction();
185 switch (F->getLinkage()) {
186 default: assert(0 && "Unknown linkage type!");
187 case Function::InternalLinkage:
Evan Chenga8e29892007-01-19 07:51:42 +0000188 SwitchToTextSection("\t.text", F);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000189 break;
190 case Function::ExternalLinkage:
Evan Chenga8e29892007-01-19 07:51:42 +0000191 SwitchToTextSection("\t.text", F);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000192 O << "\t.globl\t" << CurrentFnName << "\n";
193 break;
194 case Function::WeakLinkage:
195 case Function::LinkOnceLinkage:
Evan Cheng5be54b02007-01-19 19:25:36 +0000196 if (Subtarget->isTargetDarwin()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000197 SwitchToTextSection(
198 ".section __TEXT,__textcoal_nt,coalesced,pure_instructions", F);
199 O << "\t.globl\t" << CurrentFnName << "\n";
200 O << "\t.weak_definition\t" << CurrentFnName << "\n";
201 } else {
202 O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
203 }
Rafael Espindola4b442b52006-05-23 02:48:20 +0000204 break;
205 }
Evan Chenga8e29892007-01-19 07:51:42 +0000206
207 if (AFI->isThumbFunction()) {
208 EmitAlignment(1, F);
209 O << "\t.code\t16\n";
Lauro Ramos Venancio6f46e592007-02-01 18:25:34 +0000210 O << "\t.thumb_func";
211 if (Subtarget->isTargetDarwin())
212 O << "\t" << CurrentFnName;
213 O << "\n";
Evan Chenga8e29892007-01-19 07:51:42 +0000214 InCPMode = false;
215 } else
216 EmitAlignment(2, F);
217
Rafael Espindola4b442b52006-05-23 02:48:20 +0000218 O << CurrentFnName << ":\n";
Evan Cheng5be54b02007-01-19 19:25:36 +0000219 if (Subtarget->isTargetDarwin()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000220 // Emit pre-function debug information.
221 DW.BeginFunction(&MF);
222 }
Rafael Espindola4b442b52006-05-23 02:48:20 +0000223
224 // Print out code for the function.
225 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
226 I != E; ++I) {
227 // Print a label for the basic block.
228 if (I != MF.begin()) {
229 printBasicBlockLabel(I, true);
230 O << '\n';
231 }
232 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
233 II != E; ++II) {
234 // Print the assembly for the instruction.
Evan Chenga8e29892007-01-19 07:51:42 +0000235 printMachineInstruction(II);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000236 }
237 }
238
Evan Chenga8e29892007-01-19 07:51:42 +0000239 if (TAI->hasDotTypeDotSizeDirective())
240 O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
241
Evan Cheng5be54b02007-01-19 19:25:36 +0000242 if (Subtarget->isTargetDarwin()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000243 // Emit post-function debug information.
244 DW.EndFunction();
245 }
246
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000247 return false;
248}
249
Evan Chenga8e29892007-01-19 07:51:42 +0000250void ARMAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
251 const char *Modifier) {
252 const MachineOperand &MO = MI->getOperand(opNum);
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000253 switch (MO.getType()) {
254 case MachineOperand::MO_Register:
255 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
Evan Chenga8e29892007-01-19 07:51:42 +0000256 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000257 else
258 assert(0 && "not implemented");
259 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000260 case MachineOperand::MO_Immediate: {
261 if (!Modifier || strcmp(Modifier, "no_hash") != 0)
262 O << "#";
263
264 O << (int)MO.getImmedValue();
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000265 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000266 }
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000267 case MachineOperand::MO_MachineBasicBlock:
Rafael Espindola687bc492006-08-24 13:45:55 +0000268 printBasicBlockLabel(MO.getMachineBasicBlock());
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000269 return;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000270 case MachineOperand::MO_GlobalAddress: {
Evan Chenga8e29892007-01-19 07:51:42 +0000271 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Rafael Espindola84b19be2006-07-16 01:02:57 +0000272 GlobalValue *GV = MO.getGlobal();
273 std::string Name = Mang->getValueName(GV);
Reid Spencer5cbf9852007-01-30 20:08:39 +0000274 bool isExt = (GV->isDeclaration() || GV->hasWeakLinkage() ||
Evan Chenga8e29892007-01-19 07:51:42 +0000275 GV->hasLinkOnceLinkage());
Evan Cheng5be54b02007-01-19 19:25:36 +0000276 if (isExt && isCallOp && Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +0000277 TM.getRelocationModel() != Reloc::Static) {
278 O << TAI->getPrivateGlobalPrefix() << Name << "$stub";
279 FnStubs.insert(Name);
280 } else
281 O << Name;
282
283 if (GV->hasExternalWeakLinkage())
Rafael Espindola15404d02006-12-18 03:37:18 +0000284 ExtWeakSymbols.insert(GV);
Evan Chenga8e29892007-01-19 07:51:42 +0000285 break;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000286 }
Evan Chenga8e29892007-01-19 07:51:42 +0000287 case MachineOperand::MO_ExternalSymbol: {
288 bool isCallOp = Modifier && !strcmp(Modifier, "call");
289 std::string Name(TAI->getGlobalPrefix());
290 Name += MO.getSymbolName();
Evan Cheng5be54b02007-01-19 19:25:36 +0000291 if (isCallOp && Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +0000292 TM.getRelocationModel() != Reloc::Static) {
293 O << TAI->getPrivateGlobalPrefix() << Name << "$stub";
294 FnStubs.insert(Name);
295 } else
296 O << Name;
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000297 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000298 }
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000299 case MachineOperand::MO_ConstantPoolIndex:
Jim Laskey563321a2006-09-06 18:34:40 +0000300 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000301 << '_' << MO.getConstantPoolIndex();
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000302 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000303 case MachineOperand::MO_JumpTableIndex:
304 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
305 << '_' << MO.getJumpTableIndex();
306 break;
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000307 default:
308 O << "<unknown operand type>"; abort (); break;
309 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000310}
311
Evan Chenga8e29892007-01-19 07:51:42 +0000312/// printSOImmOperand - SOImm is 4-bit rotate amount in bits 8-11 with 8-bit
313/// immediate in bits 0-7.
314void ARMAsmPrinter::printSOImmOperand(const MachineInstr *MI, int OpNum) {
315 const MachineOperand &MO = MI->getOperand(OpNum);
316 assert(MO.isImmediate() && (MO.getImmedValue() < (1 << 12)) &&
317 "Not a valid so_imm value!");
318 unsigned Imm = ARM_AM::getSOImmValImm(MO.getImmedValue());
319 unsigned Rot = ARM_AM::getSOImmValRot(MO.getImmedValue());
320
321 // Print low-level immediate formation info, per
322 // A5.1.3: "Data-processing operands - Immediate".
323 if (Rot) {
324 O << "#" << Imm << ", " << Rot;
325 // Pretty printed version.
326 O << ' ' << TAI->getCommentString() << ' ' << (int)ARM_AM::rotr32(Imm, Rot);
327 } else {
328 O << "#" << Imm;
329 }
330}
331
332// so_reg is a 4-operand unit corresponding to register forms of the A5.1
333// "Addressing Mode 1 - Data-processing operands" forms. This includes:
334// REG 0 0 - e.g. R5
335// REG REG 0,SH_OPC - e.g. R5, ROR R3
336// REG 0 IMM,SH_OPC - e.g. R5, LSL #3
337void ARMAsmPrinter::printSORegOperand(const MachineInstr *MI, int Op) {
338 const MachineOperand &MO1 = MI->getOperand(Op);
339 const MachineOperand &MO2 = MI->getOperand(Op+1);
340 const MachineOperand &MO3 = MI->getOperand(Op+2);
341
342 assert(MRegisterInfo::isPhysicalRegister(MO1.getReg()));
343 O << TM.getRegisterInfo()->get(MO1.getReg()).Name;
344
345 // Print the shift opc.
346 O << ", "
347 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(MO3.getImmedValue()))
348 << " ";
349
350 if (MO2.getReg()) {
351 assert(MRegisterInfo::isPhysicalRegister(MO2.getReg()));
352 O << TM.getRegisterInfo()->get(MO2.getReg()).Name;
353 assert(ARM_AM::getSORegOffset(MO3.getImm()) == 0);
354 } else {
355 O << "#" << ARM_AM::getSORegOffset(MO3.getImm());
356 }
357}
358
359void ARMAsmPrinter::printAddrMode2Operand(const MachineInstr *MI, int Op) {
360 const MachineOperand &MO1 = MI->getOperand(Op);
361 const MachineOperand &MO2 = MI->getOperand(Op+1);
362 const MachineOperand &MO3 = MI->getOperand(Op+2);
363
364 if (!MO1.isRegister()) { // FIXME: This is for CP entries, but isn't right.
365 printOperand(MI, Op);
366 return;
367 }
368
369 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
370
371 if (!MO2.getReg()) {
372 if (ARM_AM::getAM2Offset(MO3.getImm())) // Don't print +0.
373 O << ", #"
374 << (char)ARM_AM::getAM2Op(MO3.getImm())
375 << ARM_AM::getAM2Offset(MO3.getImm());
376 O << "]";
377 return;
378 }
379
380 O << ", "
381 << (char)ARM_AM::getAM2Op(MO3.getImm())
382 << TM.getRegisterInfo()->get(MO2.getReg()).Name;
383
384 if (unsigned ShImm = ARM_AM::getAM2Offset(MO3.getImm()))
385 O << ", "
386 << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO3.getImmedValue()))
387 << " #" << ShImm;
388 O << "]";
389}
390
391void ARMAsmPrinter::printAddrMode2OffsetOperand(const MachineInstr *MI, int Op){
392 const MachineOperand &MO1 = MI->getOperand(Op);
393 const MachineOperand &MO2 = MI->getOperand(Op+1);
394
395 if (!MO1.getReg()) {
396 if (ARM_AM::getAM2Offset(MO2.getImm())) // Don't print +0.
397 O << "#"
398 << (char)ARM_AM::getAM2Op(MO2.getImm())
399 << ARM_AM::getAM2Offset(MO2.getImm());
400 return;
401 }
402
403 O << (char)ARM_AM::getAM2Op(MO2.getImm())
404 << TM.getRegisterInfo()->get(MO1.getReg()).Name;
405
406 if (unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm()))
407 O << ", "
408 << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO2.getImmedValue()))
409 << " #" << ShImm;
410}
411
412void ARMAsmPrinter::printAddrMode3Operand(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 assert(MRegisterInfo::isPhysicalRegister(MO1.getReg()));
418 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
419
420 if (MO2.getReg()) {
421 O << ", "
422 << (char)ARM_AM::getAM3Op(MO3.getImm())
423 << TM.getRegisterInfo()->get(MO2.getReg()).Name
424 << "]";
425 return;
426 }
427
428 if (unsigned ImmOffs = ARM_AM::getAM3Offset(MO3.getImm()))
429 O << ", #"
430 << (char)ARM_AM::getAM3Op(MO3.getImm())
431 << ImmOffs;
432 O << "]";
433}
434
435void ARMAsmPrinter::printAddrMode3OffsetOperand(const MachineInstr *MI, int Op){
436 const MachineOperand &MO1 = MI->getOperand(Op);
437 const MachineOperand &MO2 = MI->getOperand(Op+1);
438
439 if (MO1.getReg()) {
440 O << (char)ARM_AM::getAM3Op(MO2.getImm())
441 << TM.getRegisterInfo()->get(MO1.getReg()).Name;
442 return;
443 }
444
445 unsigned ImmOffs = ARM_AM::getAM3Offset(MO2.getImm());
446 O << "#"
447 << (char)ARM_AM::getAM3Op(MO2.getImm())
448 << ImmOffs;
449}
450
451void ARMAsmPrinter::printAddrMode4Operand(const MachineInstr *MI, int Op,
452 const char *Modifier) {
453 const MachineOperand &MO1 = MI->getOperand(Op);
454 const MachineOperand &MO2 = MI->getOperand(Op+1);
455 ARM_AM::AMSubMode Mode = ARM_AM::getAM4SubMode(MO2.getImm());
456 if (Modifier && strcmp(Modifier, "submode") == 0) {
457 if (MO1.getReg() == ARM::SP) {
458 bool isLDM = (MI->getOpcode() == ARM::LDM ||
459 MI->getOpcode() == ARM::LDM_RET);
460 O << ARM_AM::getAMSubModeAltStr(Mode, isLDM);
461 } else
462 O << ARM_AM::getAMSubModeStr(Mode);
463 } else {
464 printOperand(MI, Op);
465 if (ARM_AM::getAM4WBFlag(MO2.getImm()))
466 O << "!";
467 }
468}
469
470void ARMAsmPrinter::printAddrMode5Operand(const MachineInstr *MI, int Op,
471 const char *Modifier) {
472 const MachineOperand &MO1 = MI->getOperand(Op);
473 const MachineOperand &MO2 = MI->getOperand(Op+1);
474
475 if (!MO1.isRegister()) { // FIXME: This is for CP entries, but isn't right.
476 printOperand(MI, Op);
477 return;
478 }
479
480 assert(MRegisterInfo::isPhysicalRegister(MO1.getReg()));
481
482 if (Modifier && strcmp(Modifier, "submode") == 0) {
483 ARM_AM::AMSubMode Mode = ARM_AM::getAM5SubMode(MO2.getImm());
484 if (MO1.getReg() == ARM::SP) {
485 bool isFLDM = (MI->getOpcode() == ARM::FLDMD ||
486 MI->getOpcode() == ARM::FLDMS);
487 O << ARM_AM::getAMSubModeAltStr(Mode, isFLDM);
488 } else
489 O << ARM_AM::getAMSubModeStr(Mode);
490 return;
491 } else if (Modifier && strcmp(Modifier, "base") == 0) {
492 // Used for FSTM{D|S} and LSTM{D|S} operations.
493 O << TM.getRegisterInfo()->get(MO1.getReg()).Name;
494 if (ARM_AM::getAM5WBFlag(MO2.getImm()))
495 O << "!";
496 return;
497 }
498
499 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
500
501 if (unsigned ImmOffs = ARM_AM::getAM5Offset(MO2.getImm())) {
502 O << ", #"
503 << (char)ARM_AM::getAM5Op(MO2.getImm())
504 << ImmOffs*4;
505 }
506 O << "]";
507}
508
509void ARMAsmPrinter::printAddrModePCOperand(const MachineInstr *MI, int Op,
510 const char *Modifier) {
511 if (Modifier && strcmp(Modifier, "label") == 0) {
512 printPCLabel(MI, Op+1);
513 return;
514 }
515
516 const MachineOperand &MO1 = MI->getOperand(Op);
517 assert(MRegisterInfo::isPhysicalRegister(MO1.getReg()));
518 O << "[pc, +" << TM.getRegisterInfo()->get(MO1.getReg()).Name << "]";
519}
520
521void
522ARMAsmPrinter::printThumbAddrModeRROperand(const MachineInstr *MI, int Op) {
523 const MachineOperand &MO1 = MI->getOperand(Op);
524 const MachineOperand &MO2 = MI->getOperand(Op+1);
525 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
526 O << ", " << TM.getRegisterInfo()->get(MO2.getReg()).Name << "]";
527}
528
529void
530ARMAsmPrinter::printThumbAddrModeRI5Operand(const MachineInstr *MI, int Op,
531 unsigned Scale) {
532 const MachineOperand &MO1 = MI->getOperand(Op);
Evan Chengcea117d2007-01-30 02:35:32 +0000533 const MachineOperand &MO2 = MI->getOperand(Op+1);
534 const MachineOperand &MO3 = MI->getOperand(Op+2);
Evan Chenga8e29892007-01-19 07:51:42 +0000535
536 if (!MO1.isRegister()) { // FIXME: This is for CP entries, but isn't right.
537 printOperand(MI, Op);
538 return;
539 }
540
541 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
Evan Chengcea117d2007-01-30 02:35:32 +0000542 if (MO3.getReg())
543 O << ", " << TM.getRegisterInfo()->get(MO3.getReg()).Name;
544 else if (unsigned ImmOffs = MO2.getImm()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000545 O << ", #" << ImmOffs;
546 if (Scale > 1)
547 O << " * " << Scale;
548 }
549 O << "]";
550}
551
552void
Evan Chengc38f2bc2007-01-23 22:59:13 +0000553ARMAsmPrinter::printThumbAddrModeS1Operand(const MachineInstr *MI, int Op) {
Evan Chengcea117d2007-01-30 02:35:32 +0000554 printThumbAddrModeRI5Operand(MI, Op, 1);
Evan Chenga8e29892007-01-19 07:51:42 +0000555}
556void
Evan Chengc38f2bc2007-01-23 22:59:13 +0000557ARMAsmPrinter::printThumbAddrModeS2Operand(const MachineInstr *MI, int Op) {
Evan Chengcea117d2007-01-30 02:35:32 +0000558 printThumbAddrModeRI5Operand(MI, Op, 2);
Evan Chenga8e29892007-01-19 07:51:42 +0000559}
560void
Evan Chengc38f2bc2007-01-23 22:59:13 +0000561ARMAsmPrinter::printThumbAddrModeS4Operand(const MachineInstr *MI, int Op) {
Evan Chengcea117d2007-01-30 02:35:32 +0000562 printThumbAddrModeRI5Operand(MI, Op, 4);
Evan Chenga8e29892007-01-19 07:51:42 +0000563}
564
565void ARMAsmPrinter::printThumbAddrModeSPOperand(const MachineInstr *MI,int Op) {
566 const MachineOperand &MO1 = MI->getOperand(Op);
567 const MachineOperand &MO2 = MI->getOperand(Op+1);
568 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
569 if (unsigned ImmOffs = MO2.getImm())
570 O << ", #" << ImmOffs << " * 4";
571 O << "]";
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000572}
573
574void ARMAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
Rafael Espindola6f602de2006-08-24 16:13:15 +0000575 int CC = (int)MI->getOperand(opNum).getImmedValue();
576 O << ARMCondCodeToString((ARMCC::CondCodes)CC);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000577}
578
Evan Chenga8e29892007-01-19 07:51:42 +0000579void ARMAsmPrinter::printPCLabel(const MachineInstr *MI, int opNum) {
580 int Id = (int)MI->getOperand(opNum).getImmedValue();
581 O << TAI->getPrivateGlobalPrefix() << "PC" << Id;
582}
583
584void ARMAsmPrinter::printRegisterList(const MachineInstr *MI, int opNum) {
585 O << "{";
586 for (unsigned i = opNum, e = MI->getNumOperands(); i != e; ++i) {
587 printOperand(MI, i);
588 if (i != e-1) O << ", ";
589 }
590 O << "}";
591}
592
593void ARMAsmPrinter::printCPInstOperand(const MachineInstr *MI, int OpNo,
594 const char *Modifier) {
595 assert(Modifier && "This operand only works with a modifier!");
596 // There are two aspects to a CONSTANTPOOL_ENTRY operand, the label and the
597 // data itself.
598 if (!strcmp(Modifier, "label")) {
599 unsigned ID = MI->getOperand(OpNo).getImm();
600 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
601 << '_' << ID << ":\n";
602 } else {
603 assert(!strcmp(Modifier, "cpentry") && "Unknown modifier for CPE");
604 unsigned CPI = MI->getOperand(OpNo).getConstantPoolIndex();
605
606 const MachineConstantPoolEntry &MCPE = // Chasing pointers is fun?
607 MI->getParent()->getParent()->getConstantPool()->getConstants()[CPI];
608
609 if (MCPE.isMachineConstantPoolEntry())
610 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
611 else
612 EmitGlobalConstant(MCPE.Val.ConstVal);
613 }
614}
615
616void ARMAsmPrinter::printJTBlockOperand(const MachineInstr *MI, int OpNo) {
617 const MachineOperand &MO1 = MI->getOperand(OpNo);
618 const MachineOperand &MO2 = MI->getOperand(OpNo+1); // Unique Id
619 unsigned JTI = MO1.getJumpTableIndex();
620 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
621 << '_' << JTI << '_' << MO2.getImmedValue() << ":\n";
622
623 const char *JTEntryDirective = TAI->getJumpTableDirective();
624 if (!JTEntryDirective)
625 JTEntryDirective = TAI->getData32bitsDirective();
626
627 const MachineFunction *MF = MI->getParent()->getParent();
628 MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
629 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
630 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
631 bool UseSet= TAI->getSetDirective() && TM.getRelocationModel() == Reloc::PIC_;
632 std::set<MachineBasicBlock*> JTSets;
633 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
634 MachineBasicBlock *MBB = JTBBs[i];
635 if (UseSet && JTSets.insert(MBB).second)
636 printSetLabel(JTI, MO2.getImmedValue(), MBB);
637
638 O << JTEntryDirective << ' ';
639 if (UseSet)
640 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
641 << '_' << JTI << '_' << MO2.getImmedValue()
642 << "_set_" << MBB->getNumber();
643 else if (TM.getRelocationModel() == Reloc::PIC_) {
644 printBasicBlockLabel(MBB, false, false);
645 // If the arch uses custom Jump Table directives, don't calc relative to JT
646 if (!TAI->getJumpTableDirective())
647 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
648 << getFunctionNumber() << '_' << JTI << '_' << MO2.getImmedValue();
649 } else
650 printBasicBlockLabel(MBB, false, false);
Evan Chengd85ac4d2007-01-27 02:29:45 +0000651 if (i != e-1)
652 O << '\n';
Evan Chenga8e29892007-01-19 07:51:42 +0000653 }
654}
655
656
657bool ARMAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
658 unsigned AsmVariant, const char *ExtraCode){
659 // Does this asm operand have a single letter operand modifier?
660 if (ExtraCode && ExtraCode[0]) {
661 if (ExtraCode[1] != 0) return true; // Unknown modifier.
662
663 switch (ExtraCode[0]) {
664 default: return true; // Unknown modifier.
665 case 'Q':
666 if (TM.getTargetData()->isLittleEndian())
667 break;
668 // Fallthrough
669 case 'R':
670 if (TM.getTargetData()->isBigEndian())
671 break;
672 // Fallthrough
673 case 'H': // Write second word of DI / DF reference.
674 // Verify that this operand has two consecutive registers.
675 if (!MI->getOperand(OpNo).isRegister() ||
676 OpNo+1 == MI->getNumOperands() ||
677 !MI->getOperand(OpNo+1).isRegister())
678 return true;
679 ++OpNo; // Return the high-part.
680 }
681 }
682
683 printOperand(MI, OpNo);
684 return false;
685}
686
687void ARMAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
688 ++EmittedInsts;
689
Evan Chengc60e76d2007-01-30 20:37:08 +0000690 int Opc = MI->getOpcode();
691 switch (Opc) {
692 case ARM::CONSTPOOL_ENTRY:
Evan Chenga8e29892007-01-19 07:51:42 +0000693 if (!InCPMode && AFI->isThumbFunction()) {
694 EmitAlignment(2);
695 InCPMode = true;
696 }
Evan Chengc60e76d2007-01-30 20:37:08 +0000697 break;
698 default: {
Evan Cheng3bf12d02007-01-31 23:39:39 +0000699 if (InCPMode && AFI->isThumbFunction())
Evan Chenga8e29892007-01-19 07:51:42 +0000700 InCPMode = false;
Evan Chengc60e76d2007-01-30 20:37:08 +0000701 switch (Opc) {
702 case ARM::PICADD:
703 case ARM::PICLD:
704 case ARM::tPICADD:
705 break;
706 default:
707 O << "\t";
708 break;
709 }
710 }}
Evan Chenga8e29892007-01-19 07:51:42 +0000711
712 // Call the autogenerated instruction printer routines.
713 printInstruction(MI);
714}
715
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000716bool ARMAsmPrinter::doInitialization(Module &M) {
Evan Cheng5be54b02007-01-19 19:25:36 +0000717 if (Subtarget->isTargetDarwin()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000718 // Emit initial debug information.
719 DW.BeginModule(&M);
720 }
721
722 return AsmPrinter::doInitialization(M);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000723}
724
725bool ARMAsmPrinter::doFinalization(Module &M) {
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000726 const TargetData *TD = TM.getTargetData();
727
728 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
729 I != E; ++I) {
730 if (!I->hasInitializer()) // External global require no code
731 continue;
732
Evan Chengb267ca12007-01-30 08:04:53 +0000733 if (EmitSpecialLLVMGlobal(I)) {
734 if (Subtarget->isTargetDarwin() &&
735 TM.getRelocationModel() == Reloc::Static) {
736 if (I->getName() == "llvm.global_ctors")
737 O << ".reference .constructors_used\n";
738 else if (I->getName() == "llvm.global_dtors")
739 O << ".reference .destructors_used\n";
740 }
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000741 continue;
Evan Chengb267ca12007-01-30 08:04:53 +0000742 }
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000743
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000744 std::string name = Mang->getValueName(I);
745 Constant *C = I->getInitializer();
Evan Cheng98ded762007-03-08 01:25:25 +0000746 const Type *Type = C->getType();
747 unsigned Size = TD->getTypeSize(Type);
Evan Chenga8e29892007-01-19 07:51:42 +0000748 unsigned Align = TD->getPreferredAlignmentLog(I);
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000749
Evan Cheng5be54b02007-01-19 19:25:36 +0000750 if (I->hasHiddenVisibility())
751 if (const char *Directive = TAI->getHiddenDirective())
752 O << Directive << name << "\n";
753 if (Subtarget->isTargetELF())
Lauro Ramos Venanciob1cc0522007-01-25 20:11:04 +0000754 O << "\t.type " << name << ",%object\n";
Evan Cheng5be54b02007-01-19 19:25:36 +0000755
756 if (C->isNullValue()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000757 if (I->hasExternalLinkage()) {
Evan Cheng5be54b02007-01-19 19:25:36 +0000758 if (const char *Directive = TAI->getZeroFillDirective()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000759 O << "\t.globl\t" << name << "\n";
Evan Cheng5be54b02007-01-19 19:25:36 +0000760 O << Directive << "__DATA__, __common, " << name << ", "
761 << Size << ", " << Align << "\n";
762 continue;
763 }
764 }
765
766 if (!I->hasSection() &&
767 (I->hasInternalLinkage() || I->hasWeakLinkage() ||
768 I->hasLinkOnceLinkage())) {
769 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
770 if (!NoZerosInBSS && TAI->getBSSSection())
771 SwitchToDataSection(TAI->getBSSSection(), I);
772 else
773 SwitchToDataSection(TAI->getDataSection(), I);
Evan Chenga8e29892007-01-19 07:51:42 +0000774 if (TAI->getLCOMMDirective() != NULL) {
775 if (I->hasInternalLinkage()) {
776 O << TAI->getLCOMMDirective() << name << "," << Size;
Evan Cheng5be54b02007-01-19 19:25:36 +0000777 if (Subtarget->isTargetDarwin())
Evan Chenga8e29892007-01-19 07:51:42 +0000778 O << "," << Align;
779 } else
780 O << TAI->getCOMMDirective() << name << "," << Size;
781 } else {
782 if (I->hasInternalLinkage())
783 O << "\t.local\t" << name << "\n";
784 O << TAI->getCOMMDirective() << name << "," << Size;
785 if (TAI->getCOMMDirectiveTakesAlignment())
786 O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
787 }
Evan Cheng5be54b02007-01-19 19:25:36 +0000788 O << "\t\t" << TAI->getCommentString() << " " << I->getName() << "\n";
789 continue;
Evan Chenga8e29892007-01-19 07:51:42 +0000790 }
Evan Cheng5be54b02007-01-19 19:25:36 +0000791 }
Evan Chenga8e29892007-01-19 07:51:42 +0000792
Evan Cheng5be54b02007-01-19 19:25:36 +0000793 switch (I->getLinkage()) {
794 case GlobalValue::LinkOnceLinkage:
795 case GlobalValue::WeakLinkage:
796 if (Subtarget->isTargetDarwin()) {
797 O << "\t.globl " << name << "\n"
798 << "\t.weak_definition " << name << "\n";
799 SwitchToDataSection("\t.section __DATA,__const_coal,coalesced", I);
800 } else {
801 std::string SectionName("\t.section\t.llvm.linkonce.d." +
802 name +
803 ",\"aw\",%progbits");
804 SwitchToDataSection(SectionName.c_str(), I);
805 O << "\t.weak " << name << "\n";
806 }
807 break;
808 case GlobalValue::AppendingLinkage:
809 // FIXME: appending linkage variables should go into a section of
810 // their name or something. For now, just emit them as external.
811 case GlobalValue::ExternalLinkage:
812 O << "\t.globl " << name << "\n";
813 // FALL THROUGH
814 case GlobalValue::InternalLinkage: {
815 if (I->isConstant()) {
816 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
817 if (TAI->getCStringSection() && CVA && CVA->isCString()) {
818 SwitchToDataSection(TAI->getCStringSection(), I);
819 break;
820 }
821 }
822 // FIXME: special handling for ".ctors" & ".dtors" sections
823 if (I->hasSection() &&
824 (I->getSection() == ".ctors" ||
825 I->getSection() == ".dtors")) {
826 assert(!Subtarget->isTargetDarwin());
827 std::string SectionName = ".section " + I->getSection();
828 SectionName += ",\"aw\",%progbits";
829 SwitchToDataSection(SectionName.c_str());
830 } else {
831 if (C->isNullValue() && !NoZerosInBSS && TAI->getBSSSection())
832 SwitchToDataSection(TAI->getBSSSection(), I);
Evan Cheng98ded762007-03-08 01:25:25 +0000833 else if (!I->isConstant())
Evan Chenga8e29892007-01-19 07:51:42 +0000834 SwitchToDataSection(TAI->getDataSection(), I);
Evan Cheng98ded762007-03-08 01:25:25 +0000835 else {
836 // Read-only data.
Evan Cheng032953d2007-03-08 08:31:54 +0000837 bool HasReloc = C->ContainsRelocations();
838 if (HasReloc &&
839 Subtarget->isTargetDarwin() &&
Evan Cheng98ded762007-03-08 01:25:25 +0000840 TM.getRelocationModel() != Reloc::Static)
841 SwitchToDataSection("\t.const_data\n");
Evan Cheng032953d2007-03-08 08:31:54 +0000842 else if (!HasReloc && Size == 4 &&
Evan Cheng98ded762007-03-08 01:25:25 +0000843 TAI->getFourByteConstantSection())
844 SwitchToDataSection(TAI->getFourByteConstantSection(), I);
Evan Cheng032953d2007-03-08 08:31:54 +0000845 else if (!HasReloc && Size == 8 &&
Evan Cheng98ded762007-03-08 01:25:25 +0000846 TAI->getEightByteConstantSection())
847 SwitchToDataSection(TAI->getEightByteConstantSection(), I);
Evan Cheng032953d2007-03-08 08:31:54 +0000848 else if (!HasReloc && Size == 16 &&
Evan Cheng98ded762007-03-08 01:25:25 +0000849 TAI->getSixteenByteConstantSection())
850 SwitchToDataSection(TAI->getSixteenByteConstantSection(), I);
851 else if (TAI->getReadOnlySection())
852 SwitchToDataSection(TAI->getReadOnlySection(), I);
853 else
854 SwitchToDataSection(TAI->getDataSection(), I);
855 }
Rafael Espindolab97809c2006-10-19 13:30:40 +0000856 }
Evan Cheng5be54b02007-01-19 19:25:36 +0000857
858 break;
859 }
860 default:
861 assert(0 && "Unknown linkage type!");
862 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000863 }
Rafael Espindolab97809c2006-10-19 13:30:40 +0000864
Evan Chenga8e29892007-01-19 07:51:42 +0000865 EmitAlignment(Align, I);
Evan Cheng5be54b02007-01-19 19:25:36 +0000866 O << name << ":\t\t\t\t" << TAI->getCommentString() << " " << I->getName()
867 << "\n";
868 if (TAI->hasDotTypeDotSizeDirective())
Rafael Espindolab97809c2006-10-19 13:30:40 +0000869 O << "\t.size " << name << ", " << Size << "\n";
Evan Chenga8e29892007-01-19 07:51:42 +0000870 // If the initializer is a extern weak symbol, remember to emit the weak
871 // reference!
872 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
873 if (GV->hasExternalWeakLinkage())
874 ExtWeakSymbols.insert(GV);
875
876 EmitGlobalConstant(C);
877 O << '\n';
878 }
879
Evan Cheng5be54b02007-01-19 19:25:36 +0000880 if (Subtarget->isTargetDarwin()) {
881 SwitchToDataSection("");
882
Evan Chenga8e29892007-01-19 07:51:42 +0000883 // Output stubs for dynamically-linked functions
884 unsigned j = 1;
885 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
886 i != e; ++i, ++j) {
887 if (TM.getRelocationModel() == Reloc::PIC_)
888 SwitchToTextSection(".section __TEXT,__picsymbolstub4,symbol_stubs,"
889 "none,16", 0);
890 else
891 SwitchToTextSection(".section __TEXT,__symbol_stub4,symbol_stubs,"
892 "none,12", 0);
893
894 EmitAlignment(2);
895 O << "\t.code\t32\n";
896
897 O << "L" << *i << "$stub:\n";
898 O << "\t.indirect_symbol " << *i << "\n";
899 O << "\tldr ip, L" << *i << "$slp\n";
900 if (TM.getRelocationModel() == Reloc::PIC_) {
901 O << "L" << *i << "$scv:\n";
902 O << "\tadd ip, pc, ip\n";
903 }
904 O << "\tldr pc, [ip, #0]\n";
905 O << "L" << *i << "$slp:\n";
906 if (TM.getRelocationModel() == Reloc::PIC_)
907 O << "\t.long\tL" << *i << "$lazy_ptr-(L" << *i << "$scv+8)\n";
908 else
909 O << "\t.long\tL" << *i << "$lazy_ptr\n";
910 SwitchToDataSection(".lazy_symbol_pointer", 0);
911 O << "L" << *i << "$lazy_ptr:\n";
912 O << "\t.indirect_symbol " << *i << "\n";
913 O << "\t.long\tdyld_stub_binding_helper\n";
914 }
915 O << "\n";
916
917 // Output non-lazy-pointers for external and common global variables.
918 if (GVNonLazyPtrs.begin() != GVNonLazyPtrs.end())
919 SwitchToDataSection(".non_lazy_symbol_pointer", 0);
920 for (std::set<std::string>::iterator i = GVNonLazyPtrs.begin(),
921 e = GVNonLazyPtrs.end(); i != e; ++i) {
922 O << "L" << *i << "$non_lazy_ptr:\n";
923 O << "\t.indirect_symbol " << *i << "\n";
924 O << "\t.long\t0\n";
925 }
926
927 // Emit initial debug information.
928 DW.EndModule();
929
930 // Funny Darwin hack: This flag tells the linker that no global symbols
931 // contain code that falls through to other global symbols (e.g. the obvious
932 // implementation of multiple entry points). If this doesn't occur, the
933 // linker can safely perform dead code stripping. Since LLVM never
934 // generates code that does this, it is always safe to set.
935 O << "\t.subsections_via_symbols\n";
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000936 }
Rafael Espindolab97809c2006-10-19 13:30:40 +0000937
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000938 AsmPrinter::doFinalization(M);
939 return false; // success
940}