blob: 109ae215ad841c073ea66838906683dde090b927 [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);
Evan Chengc70d1842007-03-20 08:11:30 +000085 void printSOImm2PartOperand(const MachineInstr *MI, int opNum);
Evan Chenga8e29892007-01-19 07:51:42 +000086 void printSORegOperand(const MachineInstr *MI, int opNum);
87 void printAddrMode2Operand(const MachineInstr *MI, int OpNo);
88 void printAddrMode2OffsetOperand(const MachineInstr *MI, int OpNo);
89 void printAddrMode3Operand(const MachineInstr *MI, int OpNo);
90 void printAddrMode3OffsetOperand(const MachineInstr *MI, int OpNo);
91 void printAddrMode4Operand(const MachineInstr *MI, int OpNo,
92 const char *Modifier = 0);
93 void printAddrMode5Operand(const MachineInstr *MI, int OpNo,
94 const char *Modifier = 0);
95 void printAddrModePCOperand(const MachineInstr *MI, int OpNo,
96 const char *Modifier = 0);
97 void printThumbAddrModeRROperand(const MachineInstr *MI, int OpNo);
98 void printThumbAddrModeRI5Operand(const MachineInstr *MI, int OpNo,
99 unsigned Scale);
Evan Chengc38f2bc2007-01-23 22:59:13 +0000100 void printThumbAddrModeS1Operand(const MachineInstr *MI, int OpNo);
101 void printThumbAddrModeS2Operand(const MachineInstr *MI, int OpNo);
102 void printThumbAddrModeS4Operand(const MachineInstr *MI, int OpNo);
Evan Chenga8e29892007-01-19 07:51:42 +0000103 void printThumbAddrModeSPOperand(const MachineInstr *MI, int OpNo);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000104 void printCCOperand(const MachineInstr *MI, int opNum);
Evan Chenga8e29892007-01-19 07:51:42 +0000105 void printPCLabel(const MachineInstr *MI, int opNum);
106 void printRegisterList(const MachineInstr *MI, int opNum);
107 void printCPInstOperand(const MachineInstr *MI, int opNum,
108 const char *Modifier);
109 void printJTBlockOperand(const MachineInstr *MI, int opNum);
110
111 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
112 unsigned AsmVariant, const char *ExtraCode);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000113
114 bool printInstruction(const MachineInstr *MI); // autogenerated.
Evan Chenga8e29892007-01-19 07:51:42 +0000115 void printMachineInstruction(const MachineInstr *MI);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000116 bool runOnMachineFunction(MachineFunction &F);
117 bool doInitialization(Module &M);
118 bool doFinalization(Module &M);
Evan Chenga8e29892007-01-19 07:51:42 +0000119
120 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
121 printDataDirective(MCPV->getType());
122
123 ARMConstantPoolValue *ACPV = (ARMConstantPoolValue*)MCPV;
Lauro Ramos Venancio1a92d942007-01-26 19:51:32 +0000124 GlobalValue *GV = ACPV->getGV();
Evan Chengc60e76d2007-01-30 20:37:08 +0000125 std::string Name = GV ? Mang->getValueName(GV) : TAI->getGlobalPrefix();
126 if (!GV)
127 Name += ACPV->getSymbol();
Evan Chenga8e29892007-01-19 07:51:42 +0000128 if (ACPV->isNonLazyPointer()) {
129 GVNonLazyPtrs.insert(Name);
130 O << TAI->getPrivateGlobalPrefix() << Name << "$non_lazy_ptr";
Evan Chengc60e76d2007-01-30 20:37:08 +0000131 } else if (ACPV->isStub()) {
132 FnStubs.insert(Name);
133 O << TAI->getPrivateGlobalPrefix() << Name << "$stub";
Evan Chenga8e29892007-01-19 07:51:42 +0000134 } else
135 O << Name;
136 if (ACPV->getPCAdjustment() != 0)
137 O << "-(" << TAI->getPrivateGlobalPrefix() << "PC"
138 << utostr(ACPV->getLabelId())
139 << "+" << (unsigned)ACPV->getPCAdjustment() << ")";
140 O << "\n";
Lauro Ramos Venancio1a92d942007-01-26 19:51:32 +0000141
142 // If the constant pool value is a extern weak symbol, remember to emit
143 // the weak reference.
Evan Chengc60e76d2007-01-30 20:37:08 +0000144 if (GV && GV->hasExternalWeakLinkage())
Lauro Ramos Venancio1a92d942007-01-26 19:51:32 +0000145 ExtWeakSymbols.insert(GV);
Evan Chenga8e29892007-01-19 07:51:42 +0000146 }
147
148 void getAnalysisUsage(AnalysisUsage &AU) const {
149 AU.setPreservesAll();
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000150 AU.addRequired<MachineModuleInfo>();
Evan Chenga8e29892007-01-19 07:51:42 +0000151 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000152 };
153} // end of anonymous namespace
154
155#include "ARMGenAsmWriter.inc"
156
157/// createARMCodePrinterPass - Returns a pass that prints the ARM
158/// assembly code for a MachineFunction to the given output stream,
159/// using the given target machine description. This should work
160/// regardless of whether the function is in SSA form.
161///
162FunctionPass *llvm::createARMCodePrinterPass(std::ostream &o,
Evan Chenga8e29892007-01-19 07:51:42 +0000163 ARMTargetMachine &tm) {
Jim Laskeya0f3d172006-09-07 22:06:40 +0000164 return new ARMAsmPrinter(o, tm, tm.getTargetAsmInfo());
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000165}
166
Evan Chenga8e29892007-01-19 07:51:42 +0000167/// runOnMachineFunction - This uses the printInstruction()
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000168/// method to print assembly for each instruction.
169///
170bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Evan Chenga8e29892007-01-19 07:51:42 +0000171 AFI = MF.getInfo<ARMFunctionInfo>();
Rafael Espindola4b442b52006-05-23 02:48:20 +0000172
Evan Cheng5be54b02007-01-19 19:25:36 +0000173 if (Subtarget->isTargetDarwin()) {
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000174 DW.SetModuleInfo(&getAnalysis<MachineModuleInfo>());
Chris Lattner1a199de2006-12-21 22:59:58 +0000175 }
176
Evan Chenga8e29892007-01-19 07:51:42 +0000177 SetupMachineFunction(MF);
178 O << "\n";
Rafael Espindola4b442b52006-05-23 02:48:20 +0000179
Evan Chenga8e29892007-01-19 07:51:42 +0000180 // NOTE: we don't print out constant pools here, they are handled as
181 // instructions.
182
183 O << "\n";
Rafael Espindola4b442b52006-05-23 02:48:20 +0000184 // Print out labels for the function.
185 const Function *F = MF.getFunction();
186 switch (F->getLinkage()) {
187 default: assert(0 && "Unknown linkage type!");
188 case Function::InternalLinkage:
Evan Chenga8e29892007-01-19 07:51:42 +0000189 SwitchToTextSection("\t.text", F);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000190 break;
191 case Function::ExternalLinkage:
Evan Chenga8e29892007-01-19 07:51:42 +0000192 SwitchToTextSection("\t.text", F);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000193 O << "\t.globl\t" << CurrentFnName << "\n";
194 break;
195 case Function::WeakLinkage:
196 case Function::LinkOnceLinkage:
Evan Cheng5be54b02007-01-19 19:25:36 +0000197 if (Subtarget->isTargetDarwin()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000198 SwitchToTextSection(
199 ".section __TEXT,__textcoal_nt,coalesced,pure_instructions", F);
200 O << "\t.globl\t" << CurrentFnName << "\n";
201 O << "\t.weak_definition\t" << CurrentFnName << "\n";
202 } else {
203 O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
204 }
Rafael Espindola4b442b52006-05-23 02:48:20 +0000205 break;
206 }
Evan Chenga8e29892007-01-19 07:51:42 +0000207
Evan Cheng616cc662007-03-29 07:49:34 +0000208 if (F->hasHiddenVisibility())
209 if (const char *Directive = TAI->getHiddenDirective())
210 O << Directive << CurrentFnName << "\n";
211
Evan Chenga8e29892007-01-19 07:51:42 +0000212 if (AFI->isThumbFunction()) {
213 EmitAlignment(1, F);
214 O << "\t.code\t16\n";
Lauro Ramos Venancio6f46e592007-02-01 18:25:34 +0000215 O << "\t.thumb_func";
216 if (Subtarget->isTargetDarwin())
217 O << "\t" << CurrentFnName;
218 O << "\n";
Evan Chenga8e29892007-01-19 07:51:42 +0000219 InCPMode = false;
220 } else
221 EmitAlignment(2, F);
222
Rafael Espindola4b442b52006-05-23 02:48:20 +0000223 O << CurrentFnName << ":\n";
Evan Cheng5be54b02007-01-19 19:25:36 +0000224 if (Subtarget->isTargetDarwin()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000225 // Emit pre-function debug information.
226 DW.BeginFunction(&MF);
227 }
Rafael Espindola4b442b52006-05-23 02:48:20 +0000228
229 // Print out code for the function.
230 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
231 I != E; ++I) {
232 // Print a label for the basic block.
233 if (I != MF.begin()) {
234 printBasicBlockLabel(I, true);
235 O << '\n';
236 }
237 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
238 II != E; ++II) {
239 // Print the assembly for the instruction.
Evan Chenga8e29892007-01-19 07:51:42 +0000240 printMachineInstruction(II);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000241 }
242 }
243
Evan Chenga8e29892007-01-19 07:51:42 +0000244 if (TAI->hasDotTypeDotSizeDirective())
245 O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
246
Evan Cheng5be54b02007-01-19 19:25:36 +0000247 if (Subtarget->isTargetDarwin()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000248 // Emit post-function debug information.
249 DW.EndFunction();
250 }
251
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000252 return false;
253}
254
Evan Chenga8e29892007-01-19 07:51:42 +0000255void ARMAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
256 const char *Modifier) {
257 const MachineOperand &MO = MI->getOperand(opNum);
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000258 switch (MO.getType()) {
259 case MachineOperand::MO_Register:
260 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
Evan Chenga8e29892007-01-19 07:51:42 +0000261 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000262 else
263 assert(0 && "not implemented");
264 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000265 case MachineOperand::MO_Immediate: {
266 if (!Modifier || strcmp(Modifier, "no_hash") != 0)
267 O << "#";
268
269 O << (int)MO.getImmedValue();
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000270 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000271 }
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000272 case MachineOperand::MO_MachineBasicBlock:
Rafael Espindola687bc492006-08-24 13:45:55 +0000273 printBasicBlockLabel(MO.getMachineBasicBlock());
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000274 return;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000275 case MachineOperand::MO_GlobalAddress: {
Evan Chenga8e29892007-01-19 07:51:42 +0000276 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Rafael Espindola84b19be2006-07-16 01:02:57 +0000277 GlobalValue *GV = MO.getGlobal();
278 std::string Name = Mang->getValueName(GV);
Reid Spencer5cbf9852007-01-30 20:08:39 +0000279 bool isExt = (GV->isDeclaration() || GV->hasWeakLinkage() ||
Evan Chenga8e29892007-01-19 07:51:42 +0000280 GV->hasLinkOnceLinkage());
Evan Cheng5be54b02007-01-19 19:25:36 +0000281 if (isExt && isCallOp && Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +0000282 TM.getRelocationModel() != Reloc::Static) {
283 O << TAI->getPrivateGlobalPrefix() << Name << "$stub";
284 FnStubs.insert(Name);
285 } else
286 O << Name;
287
288 if (GV->hasExternalWeakLinkage())
Rafael Espindola15404d02006-12-18 03:37:18 +0000289 ExtWeakSymbols.insert(GV);
Evan Chenga8e29892007-01-19 07:51:42 +0000290 break;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000291 }
Evan Chenga8e29892007-01-19 07:51:42 +0000292 case MachineOperand::MO_ExternalSymbol: {
293 bool isCallOp = Modifier && !strcmp(Modifier, "call");
294 std::string Name(TAI->getGlobalPrefix());
295 Name += MO.getSymbolName();
Evan Cheng5be54b02007-01-19 19:25:36 +0000296 if (isCallOp && Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +0000297 TM.getRelocationModel() != Reloc::Static) {
298 O << TAI->getPrivateGlobalPrefix() << Name << "$stub";
299 FnStubs.insert(Name);
300 } else
301 O << Name;
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000302 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000303 }
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000304 case MachineOperand::MO_ConstantPoolIndex:
Jim Laskey563321a2006-09-06 18:34:40 +0000305 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000306 << '_' << MO.getConstantPoolIndex();
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000307 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000308 case MachineOperand::MO_JumpTableIndex:
309 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
310 << '_' << MO.getJumpTableIndex();
311 break;
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000312 default:
313 O << "<unknown operand type>"; abort (); break;
314 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000315}
316
Evan Chengc70d1842007-03-20 08:11:30 +0000317static void printSOImm(std::ostream &O, int64_t V, const TargetAsmInfo *TAI) {
318 assert(V < (1 << 12) && "Not a valid so_imm value!");
319 unsigned Imm = ARM_AM::getSOImmValImm(V);
320 unsigned Rot = ARM_AM::getSOImmValRot(V);
Evan Chenga8e29892007-01-19 07:51:42 +0000321
322 // Print low-level immediate formation info, per
323 // A5.1.3: "Data-processing operands - Immediate".
324 if (Rot) {
325 O << "#" << Imm << ", " << Rot;
326 // Pretty printed version.
327 O << ' ' << TAI->getCommentString() << ' ' << (int)ARM_AM::rotr32(Imm, Rot);
328 } else {
329 O << "#" << Imm;
330 }
331}
332
Evan Chengc70d1842007-03-20 08:11:30 +0000333/// printSOImmOperand - SOImm is 4-bit rotate amount in bits 8-11 with 8-bit
334/// immediate in bits 0-7.
335void ARMAsmPrinter::printSOImmOperand(const MachineInstr *MI, int OpNum) {
336 const MachineOperand &MO = MI->getOperand(OpNum);
337 assert(MO.isImmediate() && "Not a valid so_imm value!");
338 printSOImm(O, MO.getImmedValue(), TAI);
339}
340
341/// printSOImm2PartOperand - SOImm is broken into two pieces using a mov
342/// followed by a or to materialize.
343void ARMAsmPrinter::printSOImm2PartOperand(const MachineInstr *MI, int OpNum) {
344 const MachineOperand &MO = MI->getOperand(OpNum);
345 assert(MO.isImmediate() && "Not a valid so_imm value!");
346 unsigned V1 = ARM_AM::getSOImmTwoPartFirst(MO.getImmedValue());
347 unsigned V2 = ARM_AM::getSOImmTwoPartSecond(MO.getImmedValue());
348 printSOImm(O, ARM_AM::getSOImmVal(V1), TAI);
349 O << "\n\torr ";
350 printOperand(MI, 0);
351 O << ", ";
352 printOperand(MI, 0);
353 O << ", ";
354 printSOImm(O, ARM_AM::getSOImmVal(V2), TAI);
355}
356
Evan Chenga8e29892007-01-19 07:51:42 +0000357// so_reg is a 4-operand unit corresponding to register forms of the A5.1
358// "Addressing Mode 1 - Data-processing operands" forms. This includes:
359// REG 0 0 - e.g. R5
360// REG REG 0,SH_OPC - e.g. R5, ROR R3
361// REG 0 IMM,SH_OPC - e.g. R5, LSL #3
362void ARMAsmPrinter::printSORegOperand(const MachineInstr *MI, int Op) {
363 const MachineOperand &MO1 = MI->getOperand(Op);
364 const MachineOperand &MO2 = MI->getOperand(Op+1);
365 const MachineOperand &MO3 = MI->getOperand(Op+2);
366
367 assert(MRegisterInfo::isPhysicalRegister(MO1.getReg()));
368 O << TM.getRegisterInfo()->get(MO1.getReg()).Name;
369
370 // Print the shift opc.
371 O << ", "
372 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(MO3.getImmedValue()))
373 << " ";
374
375 if (MO2.getReg()) {
376 assert(MRegisterInfo::isPhysicalRegister(MO2.getReg()));
377 O << TM.getRegisterInfo()->get(MO2.getReg()).Name;
378 assert(ARM_AM::getSORegOffset(MO3.getImm()) == 0);
379 } else {
380 O << "#" << ARM_AM::getSORegOffset(MO3.getImm());
381 }
382}
383
384void ARMAsmPrinter::printAddrMode2Operand(const MachineInstr *MI, int Op) {
385 const MachineOperand &MO1 = MI->getOperand(Op);
386 const MachineOperand &MO2 = MI->getOperand(Op+1);
387 const MachineOperand &MO3 = MI->getOperand(Op+2);
388
389 if (!MO1.isRegister()) { // FIXME: This is for CP entries, but isn't right.
390 printOperand(MI, Op);
391 return;
392 }
393
394 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
395
396 if (!MO2.getReg()) {
397 if (ARM_AM::getAM2Offset(MO3.getImm())) // Don't print +0.
398 O << ", #"
399 << (char)ARM_AM::getAM2Op(MO3.getImm())
400 << ARM_AM::getAM2Offset(MO3.getImm());
401 O << "]";
402 return;
403 }
404
405 O << ", "
406 << (char)ARM_AM::getAM2Op(MO3.getImm())
407 << TM.getRegisterInfo()->get(MO2.getReg()).Name;
408
409 if (unsigned ShImm = ARM_AM::getAM2Offset(MO3.getImm()))
410 O << ", "
411 << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO3.getImmedValue()))
412 << " #" << ShImm;
413 O << "]";
414}
415
416void ARMAsmPrinter::printAddrMode2OffsetOperand(const MachineInstr *MI, int Op){
417 const MachineOperand &MO1 = MI->getOperand(Op);
418 const MachineOperand &MO2 = MI->getOperand(Op+1);
419
420 if (!MO1.getReg()) {
421 if (ARM_AM::getAM2Offset(MO2.getImm())) // Don't print +0.
422 O << "#"
423 << (char)ARM_AM::getAM2Op(MO2.getImm())
424 << ARM_AM::getAM2Offset(MO2.getImm());
425 return;
426 }
427
428 O << (char)ARM_AM::getAM2Op(MO2.getImm())
429 << TM.getRegisterInfo()->get(MO1.getReg()).Name;
430
431 if (unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm()))
432 O << ", "
433 << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO2.getImmedValue()))
434 << " #" << ShImm;
435}
436
437void ARMAsmPrinter::printAddrMode3Operand(const MachineInstr *MI, int Op) {
438 const MachineOperand &MO1 = MI->getOperand(Op);
439 const MachineOperand &MO2 = MI->getOperand(Op+1);
440 const MachineOperand &MO3 = MI->getOperand(Op+2);
441
442 assert(MRegisterInfo::isPhysicalRegister(MO1.getReg()));
443 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
444
445 if (MO2.getReg()) {
446 O << ", "
447 << (char)ARM_AM::getAM3Op(MO3.getImm())
448 << TM.getRegisterInfo()->get(MO2.getReg()).Name
449 << "]";
450 return;
451 }
452
453 if (unsigned ImmOffs = ARM_AM::getAM3Offset(MO3.getImm()))
454 O << ", #"
455 << (char)ARM_AM::getAM3Op(MO3.getImm())
456 << ImmOffs;
457 O << "]";
458}
459
460void ARMAsmPrinter::printAddrMode3OffsetOperand(const MachineInstr *MI, int Op){
461 const MachineOperand &MO1 = MI->getOperand(Op);
462 const MachineOperand &MO2 = MI->getOperand(Op+1);
463
464 if (MO1.getReg()) {
465 O << (char)ARM_AM::getAM3Op(MO2.getImm())
466 << TM.getRegisterInfo()->get(MO1.getReg()).Name;
467 return;
468 }
469
470 unsigned ImmOffs = ARM_AM::getAM3Offset(MO2.getImm());
471 O << "#"
472 << (char)ARM_AM::getAM3Op(MO2.getImm())
473 << ImmOffs;
474}
475
476void ARMAsmPrinter::printAddrMode4Operand(const MachineInstr *MI, int Op,
477 const char *Modifier) {
478 const MachineOperand &MO1 = MI->getOperand(Op);
479 const MachineOperand &MO2 = MI->getOperand(Op+1);
480 ARM_AM::AMSubMode Mode = ARM_AM::getAM4SubMode(MO2.getImm());
481 if (Modifier && strcmp(Modifier, "submode") == 0) {
482 if (MO1.getReg() == ARM::SP) {
483 bool isLDM = (MI->getOpcode() == ARM::LDM ||
484 MI->getOpcode() == ARM::LDM_RET);
485 O << ARM_AM::getAMSubModeAltStr(Mode, isLDM);
486 } else
487 O << ARM_AM::getAMSubModeStr(Mode);
488 } else {
489 printOperand(MI, Op);
490 if (ARM_AM::getAM4WBFlag(MO2.getImm()))
491 O << "!";
492 }
493}
494
495void ARMAsmPrinter::printAddrMode5Operand(const MachineInstr *MI, int Op,
496 const char *Modifier) {
497 const MachineOperand &MO1 = MI->getOperand(Op);
498 const MachineOperand &MO2 = MI->getOperand(Op+1);
499
500 if (!MO1.isRegister()) { // FIXME: This is for CP entries, but isn't right.
501 printOperand(MI, Op);
502 return;
503 }
504
505 assert(MRegisterInfo::isPhysicalRegister(MO1.getReg()));
506
507 if (Modifier && strcmp(Modifier, "submode") == 0) {
508 ARM_AM::AMSubMode Mode = ARM_AM::getAM5SubMode(MO2.getImm());
509 if (MO1.getReg() == ARM::SP) {
510 bool isFLDM = (MI->getOpcode() == ARM::FLDMD ||
511 MI->getOpcode() == ARM::FLDMS);
512 O << ARM_AM::getAMSubModeAltStr(Mode, isFLDM);
513 } else
514 O << ARM_AM::getAMSubModeStr(Mode);
515 return;
516 } else if (Modifier && strcmp(Modifier, "base") == 0) {
517 // Used for FSTM{D|S} and LSTM{D|S} operations.
518 O << TM.getRegisterInfo()->get(MO1.getReg()).Name;
519 if (ARM_AM::getAM5WBFlag(MO2.getImm()))
520 O << "!";
521 return;
522 }
523
524 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
525
526 if (unsigned ImmOffs = ARM_AM::getAM5Offset(MO2.getImm())) {
527 O << ", #"
528 << (char)ARM_AM::getAM5Op(MO2.getImm())
529 << ImmOffs*4;
530 }
531 O << "]";
532}
533
534void ARMAsmPrinter::printAddrModePCOperand(const MachineInstr *MI, int Op,
535 const char *Modifier) {
536 if (Modifier && strcmp(Modifier, "label") == 0) {
537 printPCLabel(MI, Op+1);
538 return;
539 }
540
541 const MachineOperand &MO1 = MI->getOperand(Op);
542 assert(MRegisterInfo::isPhysicalRegister(MO1.getReg()));
543 O << "[pc, +" << TM.getRegisterInfo()->get(MO1.getReg()).Name << "]";
544}
545
546void
547ARMAsmPrinter::printThumbAddrModeRROperand(const MachineInstr *MI, int Op) {
548 const MachineOperand &MO1 = MI->getOperand(Op);
549 const MachineOperand &MO2 = MI->getOperand(Op+1);
550 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
551 O << ", " << TM.getRegisterInfo()->get(MO2.getReg()).Name << "]";
552}
553
554void
555ARMAsmPrinter::printThumbAddrModeRI5Operand(const MachineInstr *MI, int Op,
556 unsigned Scale) {
557 const MachineOperand &MO1 = MI->getOperand(Op);
Evan Chengcea117d2007-01-30 02:35:32 +0000558 const MachineOperand &MO2 = MI->getOperand(Op+1);
559 const MachineOperand &MO3 = MI->getOperand(Op+2);
Evan Chenga8e29892007-01-19 07:51:42 +0000560
561 if (!MO1.isRegister()) { // FIXME: This is for CP entries, but isn't right.
562 printOperand(MI, Op);
563 return;
564 }
565
566 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
Evan Chengcea117d2007-01-30 02:35:32 +0000567 if (MO3.getReg())
568 O << ", " << TM.getRegisterInfo()->get(MO3.getReg()).Name;
569 else if (unsigned ImmOffs = MO2.getImm()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000570 O << ", #" << ImmOffs;
571 if (Scale > 1)
572 O << " * " << Scale;
573 }
574 O << "]";
575}
576
577void
Evan Chengc38f2bc2007-01-23 22:59:13 +0000578ARMAsmPrinter::printThumbAddrModeS1Operand(const MachineInstr *MI, int Op) {
Evan Chengcea117d2007-01-30 02:35:32 +0000579 printThumbAddrModeRI5Operand(MI, Op, 1);
Evan Chenga8e29892007-01-19 07:51:42 +0000580}
581void
Evan Chengc38f2bc2007-01-23 22:59:13 +0000582ARMAsmPrinter::printThumbAddrModeS2Operand(const MachineInstr *MI, int Op) {
Evan Chengcea117d2007-01-30 02:35:32 +0000583 printThumbAddrModeRI5Operand(MI, Op, 2);
Evan Chenga8e29892007-01-19 07:51:42 +0000584}
585void
Evan Chengc38f2bc2007-01-23 22:59:13 +0000586ARMAsmPrinter::printThumbAddrModeS4Operand(const MachineInstr *MI, int Op) {
Evan Chengcea117d2007-01-30 02:35:32 +0000587 printThumbAddrModeRI5Operand(MI, Op, 4);
Evan Chenga8e29892007-01-19 07:51:42 +0000588}
589
590void ARMAsmPrinter::printThumbAddrModeSPOperand(const MachineInstr *MI,int Op) {
591 const MachineOperand &MO1 = MI->getOperand(Op);
592 const MachineOperand &MO2 = MI->getOperand(Op+1);
593 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
594 if (unsigned ImmOffs = MO2.getImm())
595 O << ", #" << ImmOffs << " * 4";
596 O << "]";
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000597}
598
599void ARMAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
Rafael Espindola6f602de2006-08-24 16:13:15 +0000600 int CC = (int)MI->getOperand(opNum).getImmedValue();
601 O << ARMCondCodeToString((ARMCC::CondCodes)CC);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000602}
603
Evan Chenga8e29892007-01-19 07:51:42 +0000604void ARMAsmPrinter::printPCLabel(const MachineInstr *MI, int opNum) {
605 int Id = (int)MI->getOperand(opNum).getImmedValue();
606 O << TAI->getPrivateGlobalPrefix() << "PC" << Id;
607}
608
609void ARMAsmPrinter::printRegisterList(const MachineInstr *MI, int opNum) {
610 O << "{";
611 for (unsigned i = opNum, e = MI->getNumOperands(); i != e; ++i) {
612 printOperand(MI, i);
613 if (i != e-1) O << ", ";
614 }
615 O << "}";
616}
617
618void ARMAsmPrinter::printCPInstOperand(const MachineInstr *MI, int OpNo,
619 const char *Modifier) {
620 assert(Modifier && "This operand only works with a modifier!");
621 // There are two aspects to a CONSTANTPOOL_ENTRY operand, the label and the
622 // data itself.
623 if (!strcmp(Modifier, "label")) {
624 unsigned ID = MI->getOperand(OpNo).getImm();
625 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
626 << '_' << ID << ":\n";
627 } else {
628 assert(!strcmp(Modifier, "cpentry") && "Unknown modifier for CPE");
629 unsigned CPI = MI->getOperand(OpNo).getConstantPoolIndex();
630
631 const MachineConstantPoolEntry &MCPE = // Chasing pointers is fun?
632 MI->getParent()->getParent()->getConstantPool()->getConstants()[CPI];
633
634 if (MCPE.isMachineConstantPoolEntry())
635 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
636 else
637 EmitGlobalConstant(MCPE.Val.ConstVal);
638 }
639}
640
641void ARMAsmPrinter::printJTBlockOperand(const MachineInstr *MI, int OpNo) {
642 const MachineOperand &MO1 = MI->getOperand(OpNo);
643 const MachineOperand &MO2 = MI->getOperand(OpNo+1); // Unique Id
644 unsigned JTI = MO1.getJumpTableIndex();
645 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
646 << '_' << JTI << '_' << MO2.getImmedValue() << ":\n";
647
648 const char *JTEntryDirective = TAI->getJumpTableDirective();
649 if (!JTEntryDirective)
650 JTEntryDirective = TAI->getData32bitsDirective();
651
652 const MachineFunction *MF = MI->getParent()->getParent();
653 MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
654 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
655 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
656 bool UseSet= TAI->getSetDirective() && TM.getRelocationModel() == Reloc::PIC_;
657 std::set<MachineBasicBlock*> JTSets;
658 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
659 MachineBasicBlock *MBB = JTBBs[i];
660 if (UseSet && JTSets.insert(MBB).second)
661 printSetLabel(JTI, MO2.getImmedValue(), MBB);
662
663 O << JTEntryDirective << ' ';
664 if (UseSet)
665 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
666 << '_' << JTI << '_' << MO2.getImmedValue()
667 << "_set_" << MBB->getNumber();
668 else if (TM.getRelocationModel() == Reloc::PIC_) {
669 printBasicBlockLabel(MBB, false, false);
670 // If the arch uses custom Jump Table directives, don't calc relative to JT
671 if (!TAI->getJumpTableDirective())
672 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
673 << getFunctionNumber() << '_' << JTI << '_' << MO2.getImmedValue();
674 } else
675 printBasicBlockLabel(MBB, false, false);
Evan Chengd85ac4d2007-01-27 02:29:45 +0000676 if (i != e-1)
677 O << '\n';
Evan Chenga8e29892007-01-19 07:51:42 +0000678 }
679}
680
681
682bool ARMAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
683 unsigned AsmVariant, const char *ExtraCode){
684 // Does this asm operand have a single letter operand modifier?
685 if (ExtraCode && ExtraCode[0]) {
686 if (ExtraCode[1] != 0) return true; // Unknown modifier.
687
688 switch (ExtraCode[0]) {
689 default: return true; // Unknown modifier.
Evan Cheng23a95702007-03-08 22:42:46 +0000690 case 'c': // Don't print "$" before a global var name or constant.
Evan Chenge21e3962007-04-04 00:13:29 +0000691 case 'P': // Print a VFP double precision register.
Evan Cheng23a95702007-03-08 22:42:46 +0000692 printOperand(MI, OpNo);
693 return false;
Evan Chenga8e29892007-01-19 07:51:42 +0000694 case 'Q':
695 if (TM.getTargetData()->isLittleEndian())
696 break;
697 // Fallthrough
698 case 'R':
699 if (TM.getTargetData()->isBigEndian())
700 break;
701 // Fallthrough
702 case 'H': // Write second word of DI / DF reference.
703 // Verify that this operand has two consecutive registers.
704 if (!MI->getOperand(OpNo).isRegister() ||
705 OpNo+1 == MI->getNumOperands() ||
706 !MI->getOperand(OpNo+1).isRegister())
707 return true;
708 ++OpNo; // Return the high-part.
709 }
710 }
711
712 printOperand(MI, OpNo);
713 return false;
714}
715
716void ARMAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
717 ++EmittedInsts;
718
Evan Chengc60e76d2007-01-30 20:37:08 +0000719 int Opc = MI->getOpcode();
720 switch (Opc) {
721 case ARM::CONSTPOOL_ENTRY:
Evan Chenga8e29892007-01-19 07:51:42 +0000722 if (!InCPMode && AFI->isThumbFunction()) {
723 EmitAlignment(2);
724 InCPMode = true;
725 }
Evan Chengc60e76d2007-01-30 20:37:08 +0000726 break;
727 default: {
Evan Cheng3bf12d02007-01-31 23:39:39 +0000728 if (InCPMode && AFI->isThumbFunction())
Evan Chenga8e29892007-01-19 07:51:42 +0000729 InCPMode = false;
Evan Chengc60e76d2007-01-30 20:37:08 +0000730 switch (Opc) {
731 case ARM::PICADD:
732 case ARM::PICLD:
733 case ARM::tPICADD:
734 break;
735 default:
736 O << "\t";
737 break;
738 }
739 }}
Evan Chenga8e29892007-01-19 07:51:42 +0000740
741 // Call the autogenerated instruction printer routines.
742 printInstruction(MI);
743}
744
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000745bool ARMAsmPrinter::doInitialization(Module &M) {
Evan Cheng5be54b02007-01-19 19:25:36 +0000746 if (Subtarget->isTargetDarwin()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000747 // Emit initial debug information.
748 DW.BeginModule(&M);
749 }
750
751 return AsmPrinter::doInitialization(M);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000752}
753
754bool ARMAsmPrinter::doFinalization(Module &M) {
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000755 const TargetData *TD = TM.getTargetData();
756
757 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
758 I != E; ++I) {
759 if (!I->hasInitializer()) // External global require no code
760 continue;
761
Evan Chengb267ca12007-01-30 08:04:53 +0000762 if (EmitSpecialLLVMGlobal(I)) {
763 if (Subtarget->isTargetDarwin() &&
764 TM.getRelocationModel() == Reloc::Static) {
765 if (I->getName() == "llvm.global_ctors")
766 O << ".reference .constructors_used\n";
767 else if (I->getName() == "llvm.global_dtors")
768 O << ".reference .destructors_used\n";
769 }
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000770 continue;
Evan Chengb267ca12007-01-30 08:04:53 +0000771 }
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000772
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000773 std::string name = Mang->getValueName(I);
774 Constant *C = I->getInitializer();
Evan Cheng98ded762007-03-08 01:25:25 +0000775 const Type *Type = C->getType();
776 unsigned Size = TD->getTypeSize(Type);
Evan Chenga8e29892007-01-19 07:51:42 +0000777 unsigned Align = TD->getPreferredAlignmentLog(I);
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000778
Evan Cheng5be54b02007-01-19 19:25:36 +0000779 if (I->hasHiddenVisibility())
780 if (const char *Directive = TAI->getHiddenDirective())
781 O << Directive << name << "\n";
782 if (Subtarget->isTargetELF())
Lauro Ramos Venanciob1cc0522007-01-25 20:11:04 +0000783 O << "\t.type " << name << ",%object\n";
Evan Cheng5be54b02007-01-19 19:25:36 +0000784
785 if (C->isNullValue()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000786 if (I->hasExternalLinkage()) {
Evan Cheng5be54b02007-01-19 19:25:36 +0000787 if (const char *Directive = TAI->getZeroFillDirective()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000788 O << "\t.globl\t" << name << "\n";
Evan Cheng5be54b02007-01-19 19:25:36 +0000789 O << Directive << "__DATA__, __common, " << name << ", "
790 << Size << ", " << Align << "\n";
791 continue;
792 }
793 }
794
795 if (!I->hasSection() &&
796 (I->hasInternalLinkage() || I->hasWeakLinkage() ||
797 I->hasLinkOnceLinkage())) {
798 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
799 if (!NoZerosInBSS && TAI->getBSSSection())
800 SwitchToDataSection(TAI->getBSSSection(), I);
801 else
802 SwitchToDataSection(TAI->getDataSection(), I);
Evan Chenga8e29892007-01-19 07:51:42 +0000803 if (TAI->getLCOMMDirective() != NULL) {
804 if (I->hasInternalLinkage()) {
805 O << TAI->getLCOMMDirective() << name << "," << Size;
Evan Cheng5be54b02007-01-19 19:25:36 +0000806 if (Subtarget->isTargetDarwin())
Evan Chenga8e29892007-01-19 07:51:42 +0000807 O << "," << Align;
808 } else
809 O << TAI->getCOMMDirective() << name << "," << Size;
810 } else {
811 if (I->hasInternalLinkage())
812 O << "\t.local\t" << name << "\n";
813 O << TAI->getCOMMDirective() << name << "," << Size;
814 if (TAI->getCOMMDirectiveTakesAlignment())
815 O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
816 }
Evan Cheng5be54b02007-01-19 19:25:36 +0000817 O << "\t\t" << TAI->getCommentString() << " " << I->getName() << "\n";
818 continue;
Evan Chenga8e29892007-01-19 07:51:42 +0000819 }
Evan Cheng5be54b02007-01-19 19:25:36 +0000820 }
Evan Chenga8e29892007-01-19 07:51:42 +0000821
Evan Cheng5be54b02007-01-19 19:25:36 +0000822 switch (I->getLinkage()) {
823 case GlobalValue::LinkOnceLinkage:
824 case GlobalValue::WeakLinkage:
825 if (Subtarget->isTargetDarwin()) {
826 O << "\t.globl " << name << "\n"
827 << "\t.weak_definition " << name << "\n";
828 SwitchToDataSection("\t.section __DATA,__const_coal,coalesced", I);
829 } else {
830 std::string SectionName("\t.section\t.llvm.linkonce.d." +
831 name +
832 ",\"aw\",%progbits");
833 SwitchToDataSection(SectionName.c_str(), I);
834 O << "\t.weak " << name << "\n";
835 }
836 break;
837 case GlobalValue::AppendingLinkage:
838 // FIXME: appending linkage variables should go into a section of
839 // their name or something. For now, just emit them as external.
840 case GlobalValue::ExternalLinkage:
841 O << "\t.globl " << name << "\n";
842 // FALL THROUGH
843 case GlobalValue::InternalLinkage: {
844 if (I->isConstant()) {
845 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
846 if (TAI->getCStringSection() && CVA && CVA->isCString()) {
847 SwitchToDataSection(TAI->getCStringSection(), I);
848 break;
849 }
850 }
851 // FIXME: special handling for ".ctors" & ".dtors" sections
852 if (I->hasSection() &&
853 (I->getSection() == ".ctors" ||
854 I->getSection() == ".dtors")) {
855 assert(!Subtarget->isTargetDarwin());
856 std::string SectionName = ".section " + I->getSection();
857 SectionName += ",\"aw\",%progbits";
858 SwitchToDataSection(SectionName.c_str());
859 } else {
860 if (C->isNullValue() && !NoZerosInBSS && TAI->getBSSSection())
861 SwitchToDataSection(TAI->getBSSSection(), I);
Evan Cheng98ded762007-03-08 01:25:25 +0000862 else if (!I->isConstant())
Evan Chenga8e29892007-01-19 07:51:42 +0000863 SwitchToDataSection(TAI->getDataSection(), I);
Evan Cheng98ded762007-03-08 01:25:25 +0000864 else {
865 // Read-only data.
Evan Cheng032953d2007-03-08 08:31:54 +0000866 bool HasReloc = C->ContainsRelocations();
867 if (HasReloc &&
868 Subtarget->isTargetDarwin() &&
Evan Cheng98ded762007-03-08 01:25:25 +0000869 TM.getRelocationModel() != Reloc::Static)
870 SwitchToDataSection("\t.const_data\n");
Evan Cheng032953d2007-03-08 08:31:54 +0000871 else if (!HasReloc && Size == 4 &&
Evan Cheng98ded762007-03-08 01:25:25 +0000872 TAI->getFourByteConstantSection())
873 SwitchToDataSection(TAI->getFourByteConstantSection(), I);
Evan Cheng032953d2007-03-08 08:31:54 +0000874 else if (!HasReloc && Size == 8 &&
Evan Cheng98ded762007-03-08 01:25:25 +0000875 TAI->getEightByteConstantSection())
876 SwitchToDataSection(TAI->getEightByteConstantSection(), I);
Evan Cheng032953d2007-03-08 08:31:54 +0000877 else if (!HasReloc && Size == 16 &&
Evan Cheng98ded762007-03-08 01:25:25 +0000878 TAI->getSixteenByteConstantSection())
879 SwitchToDataSection(TAI->getSixteenByteConstantSection(), I);
880 else if (TAI->getReadOnlySection())
881 SwitchToDataSection(TAI->getReadOnlySection(), I);
882 else
883 SwitchToDataSection(TAI->getDataSection(), I);
884 }
Rafael Espindolab97809c2006-10-19 13:30:40 +0000885 }
Evan Cheng5be54b02007-01-19 19:25:36 +0000886
887 break;
888 }
889 default:
890 assert(0 && "Unknown linkage type!");
891 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000892 }
Rafael Espindolab97809c2006-10-19 13:30:40 +0000893
Evan Chenga8e29892007-01-19 07:51:42 +0000894 EmitAlignment(Align, I);
Evan Cheng5be54b02007-01-19 19:25:36 +0000895 O << name << ":\t\t\t\t" << TAI->getCommentString() << " " << I->getName()
896 << "\n";
897 if (TAI->hasDotTypeDotSizeDirective())
Rafael Espindolab97809c2006-10-19 13:30:40 +0000898 O << "\t.size " << name << ", " << Size << "\n";
Evan Chenga8e29892007-01-19 07:51:42 +0000899 // If the initializer is a extern weak symbol, remember to emit the weak
900 // reference!
901 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
902 if (GV->hasExternalWeakLinkage())
903 ExtWeakSymbols.insert(GV);
904
905 EmitGlobalConstant(C);
906 O << '\n';
907 }
908
Evan Cheng5be54b02007-01-19 19:25:36 +0000909 if (Subtarget->isTargetDarwin()) {
910 SwitchToDataSection("");
911
Evan Chenga8e29892007-01-19 07:51:42 +0000912 // Output stubs for dynamically-linked functions
913 unsigned j = 1;
914 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
915 i != e; ++i, ++j) {
916 if (TM.getRelocationModel() == Reloc::PIC_)
917 SwitchToTextSection(".section __TEXT,__picsymbolstub4,symbol_stubs,"
918 "none,16", 0);
919 else
920 SwitchToTextSection(".section __TEXT,__symbol_stub4,symbol_stubs,"
921 "none,12", 0);
922
923 EmitAlignment(2);
924 O << "\t.code\t32\n";
925
926 O << "L" << *i << "$stub:\n";
927 O << "\t.indirect_symbol " << *i << "\n";
928 O << "\tldr ip, L" << *i << "$slp\n";
929 if (TM.getRelocationModel() == Reloc::PIC_) {
930 O << "L" << *i << "$scv:\n";
931 O << "\tadd ip, pc, ip\n";
932 }
933 O << "\tldr pc, [ip, #0]\n";
934 O << "L" << *i << "$slp:\n";
935 if (TM.getRelocationModel() == Reloc::PIC_)
936 O << "\t.long\tL" << *i << "$lazy_ptr-(L" << *i << "$scv+8)\n";
937 else
938 O << "\t.long\tL" << *i << "$lazy_ptr\n";
939 SwitchToDataSection(".lazy_symbol_pointer", 0);
940 O << "L" << *i << "$lazy_ptr:\n";
941 O << "\t.indirect_symbol " << *i << "\n";
942 O << "\t.long\tdyld_stub_binding_helper\n";
943 }
944 O << "\n";
945
946 // Output non-lazy-pointers for external and common global variables.
947 if (GVNonLazyPtrs.begin() != GVNonLazyPtrs.end())
948 SwitchToDataSection(".non_lazy_symbol_pointer", 0);
949 for (std::set<std::string>::iterator i = GVNonLazyPtrs.begin(),
950 e = GVNonLazyPtrs.end(); i != e; ++i) {
951 O << "L" << *i << "$non_lazy_ptr:\n";
952 O << "\t.indirect_symbol " << *i << "\n";
953 O << "\t.long\t0\n";
954 }
955
956 // Emit initial debug information.
957 DW.EndModule();
958
959 // Funny Darwin hack: This flag tells the linker that no global symbols
960 // contain code that falls through to other global symbols (e.g. the obvious
961 // implementation of multiple entry points). If this doesn't occur, the
962 // linker can safely perform dead code stripping. Since LLVM never
963 // generates code that does this, it is always safe to set.
964 O << "\t.subsections_via_symbols\n";
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000965 }
Rafael Espindolab97809c2006-10-19 13:30:40 +0000966
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000967 AsmPrinter::doFinalization(M);
968 return false; // success
969}