blob: ac392607b067533862f9e9382a4895ec26176b33 [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;
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000136 if (ACPV->hasModifier()) O << "(" << ACPV->getModifier() << ")";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000137 if (ACPV->getPCAdjustment() != 0) {
Evan Chenga8e29892007-01-19 07:51:42 +0000138 O << "-(" << TAI->getPrivateGlobalPrefix() << "PC"
139 << utostr(ACPV->getLabelId())
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000140 << "+" << (unsigned)ACPV->getPCAdjustment();
141 if (ACPV->mustAddCurrentAddress())
142 O << "-.";
143 O << ")";
144 }
Evan Chenga8e29892007-01-19 07:51:42 +0000145 O << "\n";
Lauro Ramos Venancio1a92d942007-01-26 19:51:32 +0000146
147 // If the constant pool value is a extern weak symbol, remember to emit
148 // the weak reference.
Evan Chengc60e76d2007-01-30 20:37:08 +0000149 if (GV && GV->hasExternalWeakLinkage())
Lauro Ramos Venancio1a92d942007-01-26 19:51:32 +0000150 ExtWeakSymbols.insert(GV);
Evan Chenga8e29892007-01-19 07:51:42 +0000151 }
152
153 void getAnalysisUsage(AnalysisUsage &AU) const {
154 AU.setPreservesAll();
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000155 AU.addRequired<MachineModuleInfo>();
Evan Chenga8e29892007-01-19 07:51:42 +0000156 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000157 };
158} // end of anonymous namespace
159
160#include "ARMGenAsmWriter.inc"
161
162/// createARMCodePrinterPass - Returns a pass that prints the ARM
163/// assembly code for a MachineFunction to the given output stream,
164/// using the given target machine description. This should work
165/// regardless of whether the function is in SSA form.
166///
167FunctionPass *llvm::createARMCodePrinterPass(std::ostream &o,
Evan Chenga8e29892007-01-19 07:51:42 +0000168 ARMTargetMachine &tm) {
Jim Laskeya0f3d172006-09-07 22:06:40 +0000169 return new ARMAsmPrinter(o, tm, tm.getTargetAsmInfo());
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000170}
171
Evan Chenga8e29892007-01-19 07:51:42 +0000172/// runOnMachineFunction - This uses the printInstruction()
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000173/// method to print assembly for each instruction.
174///
175bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Evan Chenga8e29892007-01-19 07:51:42 +0000176 AFI = MF.getInfo<ARMFunctionInfo>();
Rafael Espindola4b442b52006-05-23 02:48:20 +0000177
Evan Cheng5be54b02007-01-19 19:25:36 +0000178 if (Subtarget->isTargetDarwin()) {
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000179 DW.SetModuleInfo(&getAnalysis<MachineModuleInfo>());
Chris Lattner1a199de2006-12-21 22:59:58 +0000180 }
181
Evan Chenga8e29892007-01-19 07:51:42 +0000182 SetupMachineFunction(MF);
183 O << "\n";
Rafael Espindola4b442b52006-05-23 02:48:20 +0000184
Evan Chenga8e29892007-01-19 07:51:42 +0000185 // NOTE: we don't print out constant pools here, they are handled as
186 // instructions.
187
188 O << "\n";
Rafael Espindola4b442b52006-05-23 02:48:20 +0000189 // Print out labels for the function.
190 const Function *F = MF.getFunction();
191 switch (F->getLinkage()) {
192 default: assert(0 && "Unknown linkage type!");
193 case Function::InternalLinkage:
Evan Chenga8e29892007-01-19 07:51:42 +0000194 SwitchToTextSection("\t.text", F);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000195 break;
196 case Function::ExternalLinkage:
Evan Chenga8e29892007-01-19 07:51:42 +0000197 SwitchToTextSection("\t.text", F);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000198 O << "\t.globl\t" << CurrentFnName << "\n";
199 break;
200 case Function::WeakLinkage:
201 case Function::LinkOnceLinkage:
Evan Cheng5be54b02007-01-19 19:25:36 +0000202 if (Subtarget->isTargetDarwin()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000203 SwitchToTextSection(
204 ".section __TEXT,__textcoal_nt,coalesced,pure_instructions", F);
205 O << "\t.globl\t" << CurrentFnName << "\n";
206 O << "\t.weak_definition\t" << CurrentFnName << "\n";
207 } else {
208 O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
209 }
Rafael Espindola4b442b52006-05-23 02:48:20 +0000210 break;
211 }
Evan Chenga8e29892007-01-19 07:51:42 +0000212
Lauro Ramos Venancio0a181732007-04-30 00:23:51 +0000213 const char *VisibilityDirective = NULL;
Evan Cheng616cc662007-03-29 07:49:34 +0000214 if (F->hasHiddenVisibility())
Lauro Ramos Venancio0a181732007-04-30 00:23:51 +0000215 VisibilityDirective = TAI->getHiddenDirective();
216 else if (F->hasProtectedVisibility())
217 VisibilityDirective = TAI->getProtectedDirective();
218
219 if (VisibilityDirective)
220 O << VisibilityDirective << CurrentFnName << "\n";
Evan Cheng616cc662007-03-29 07:49:34 +0000221
Evan Chenga8e29892007-01-19 07:51:42 +0000222 if (AFI->isThumbFunction()) {
Dale Johannesenbd24f3f2007-04-23 20:07:25 +0000223 EmitAlignment(AFI->getAlign(), F);
Evan Chenga8e29892007-01-19 07:51:42 +0000224 O << "\t.code\t16\n";
Lauro Ramos Venancio6f46e592007-02-01 18:25:34 +0000225 O << "\t.thumb_func";
226 if (Subtarget->isTargetDarwin())
227 O << "\t" << CurrentFnName;
228 O << "\n";
Evan Chenga8e29892007-01-19 07:51:42 +0000229 InCPMode = false;
230 } else
231 EmitAlignment(2, F);
232
Rafael Espindola4b442b52006-05-23 02:48:20 +0000233 O << CurrentFnName << ":\n";
Evan Cheng5be54b02007-01-19 19:25:36 +0000234 if (Subtarget->isTargetDarwin()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000235 // Emit pre-function debug information.
236 DW.BeginFunction(&MF);
237 }
Rafael Espindola4b442b52006-05-23 02:48:20 +0000238
239 // Print out code for the function.
240 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
241 I != E; ++I) {
242 // Print a label for the basic block.
243 if (I != MF.begin()) {
244 printBasicBlockLabel(I, true);
245 O << '\n';
246 }
247 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
248 II != E; ++II) {
249 // Print the assembly for the instruction.
Evan Chenga8e29892007-01-19 07:51:42 +0000250 printMachineInstruction(II);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000251 }
252 }
253
Evan Chenga8e29892007-01-19 07:51:42 +0000254 if (TAI->hasDotTypeDotSizeDirective())
255 O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
256
Evan Cheng5be54b02007-01-19 19:25:36 +0000257 if (Subtarget->isTargetDarwin()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000258 // Emit post-function debug information.
259 DW.EndFunction();
260 }
261
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000262 return false;
263}
264
Evan Chenga8e29892007-01-19 07:51:42 +0000265void ARMAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
266 const char *Modifier) {
267 const MachineOperand &MO = MI->getOperand(opNum);
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000268 switch (MO.getType()) {
269 case MachineOperand::MO_Register:
270 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
Evan Chenga8e29892007-01-19 07:51:42 +0000271 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000272 else
273 assert(0 && "not implemented");
274 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000275 case MachineOperand::MO_Immediate: {
276 if (!Modifier || strcmp(Modifier, "no_hash") != 0)
277 O << "#";
278
279 O << (int)MO.getImmedValue();
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000280 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000281 }
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000282 case MachineOperand::MO_MachineBasicBlock:
Rafael Espindola687bc492006-08-24 13:45:55 +0000283 printBasicBlockLabel(MO.getMachineBasicBlock());
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000284 return;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000285 case MachineOperand::MO_GlobalAddress: {
Evan Chenga8e29892007-01-19 07:51:42 +0000286 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Rafael Espindola84b19be2006-07-16 01:02:57 +0000287 GlobalValue *GV = MO.getGlobal();
288 std::string Name = Mang->getValueName(GV);
Reid Spencer5cbf9852007-01-30 20:08:39 +0000289 bool isExt = (GV->isDeclaration() || GV->hasWeakLinkage() ||
Evan Chenga8e29892007-01-19 07:51:42 +0000290 GV->hasLinkOnceLinkage());
Evan Cheng5be54b02007-01-19 19:25:36 +0000291 if (isExt && 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;
Chris Lattner388488d2007-05-03 16:42:23 +0000297
298 if (MO.getOffset() > 0)
299 O << '+' << MO.getOffset();
300 else if (MO.getOffset() < 0)
301 O << MO.getOffset();
302
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000303 if (isCallOp && Subtarget->isTargetELF() &&
304 TM.getRelocationModel() == Reloc::PIC_)
305 O << "(PLT)";
Evan Chenga8e29892007-01-19 07:51:42 +0000306 if (GV->hasExternalWeakLinkage())
Rafael Espindola15404d02006-12-18 03:37:18 +0000307 ExtWeakSymbols.insert(GV);
Evan Chenga8e29892007-01-19 07:51:42 +0000308 break;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000309 }
Evan Chenga8e29892007-01-19 07:51:42 +0000310 case MachineOperand::MO_ExternalSymbol: {
311 bool isCallOp = Modifier && !strcmp(Modifier, "call");
312 std::string Name(TAI->getGlobalPrefix());
313 Name += MO.getSymbolName();
Evan Cheng5be54b02007-01-19 19:25:36 +0000314 if (isCallOp && Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +0000315 TM.getRelocationModel() != Reloc::Static) {
316 O << TAI->getPrivateGlobalPrefix() << Name << "$stub";
317 FnStubs.insert(Name);
318 } else
319 O << Name;
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000320 if (isCallOp && Subtarget->isTargetELF() &&
321 TM.getRelocationModel() == Reloc::PIC_)
322 O << "(PLT)";
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000323 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000324 }
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000325 case MachineOperand::MO_ConstantPoolIndex:
Jim Laskey563321a2006-09-06 18:34:40 +0000326 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000327 << '_' << MO.getConstantPoolIndex();
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000328 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000329 case MachineOperand::MO_JumpTableIndex:
330 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
331 << '_' << MO.getJumpTableIndex();
332 break;
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000333 default:
334 O << "<unknown operand type>"; abort (); break;
335 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000336}
337
Evan Chengc70d1842007-03-20 08:11:30 +0000338static void printSOImm(std::ostream &O, int64_t V, const TargetAsmInfo *TAI) {
339 assert(V < (1 << 12) && "Not a valid so_imm value!");
340 unsigned Imm = ARM_AM::getSOImmValImm(V);
341 unsigned Rot = ARM_AM::getSOImmValRot(V);
Evan Chenga8e29892007-01-19 07:51:42 +0000342
343 // Print low-level immediate formation info, per
344 // A5.1.3: "Data-processing operands - Immediate".
345 if (Rot) {
346 O << "#" << Imm << ", " << Rot;
347 // Pretty printed version.
348 O << ' ' << TAI->getCommentString() << ' ' << (int)ARM_AM::rotr32(Imm, Rot);
349 } else {
350 O << "#" << Imm;
351 }
352}
353
Evan Chengc70d1842007-03-20 08:11:30 +0000354/// printSOImmOperand - SOImm is 4-bit rotate amount in bits 8-11 with 8-bit
355/// immediate in bits 0-7.
356void ARMAsmPrinter::printSOImmOperand(const MachineInstr *MI, int OpNum) {
357 const MachineOperand &MO = MI->getOperand(OpNum);
358 assert(MO.isImmediate() && "Not a valid so_imm value!");
359 printSOImm(O, MO.getImmedValue(), TAI);
360}
361
362/// printSOImm2PartOperand - SOImm is broken into two pieces using a mov
363/// followed by a or to materialize.
364void ARMAsmPrinter::printSOImm2PartOperand(const MachineInstr *MI, int OpNum) {
365 const MachineOperand &MO = MI->getOperand(OpNum);
366 assert(MO.isImmediate() && "Not a valid so_imm value!");
367 unsigned V1 = ARM_AM::getSOImmTwoPartFirst(MO.getImmedValue());
368 unsigned V2 = ARM_AM::getSOImmTwoPartSecond(MO.getImmedValue());
369 printSOImm(O, ARM_AM::getSOImmVal(V1), TAI);
370 O << "\n\torr ";
371 printOperand(MI, 0);
372 O << ", ";
373 printOperand(MI, 0);
374 O << ", ";
375 printSOImm(O, ARM_AM::getSOImmVal(V2), TAI);
376}
377
Evan Chenga8e29892007-01-19 07:51:42 +0000378// so_reg is a 4-operand unit corresponding to register forms of the A5.1
379// "Addressing Mode 1 - Data-processing operands" forms. This includes:
380// REG 0 0 - e.g. R5
381// REG REG 0,SH_OPC - e.g. R5, ROR R3
382// REG 0 IMM,SH_OPC - e.g. R5, LSL #3
383void ARMAsmPrinter::printSORegOperand(const MachineInstr *MI, int Op) {
384 const MachineOperand &MO1 = MI->getOperand(Op);
385 const MachineOperand &MO2 = MI->getOperand(Op+1);
386 const MachineOperand &MO3 = MI->getOperand(Op+2);
387
388 assert(MRegisterInfo::isPhysicalRegister(MO1.getReg()));
389 O << TM.getRegisterInfo()->get(MO1.getReg()).Name;
390
391 // Print the shift opc.
392 O << ", "
393 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(MO3.getImmedValue()))
394 << " ";
395
396 if (MO2.getReg()) {
397 assert(MRegisterInfo::isPhysicalRegister(MO2.getReg()));
398 O << TM.getRegisterInfo()->get(MO2.getReg()).Name;
399 assert(ARM_AM::getSORegOffset(MO3.getImm()) == 0);
400 } else {
401 O << "#" << ARM_AM::getSORegOffset(MO3.getImm());
402 }
403}
404
405void ARMAsmPrinter::printAddrMode2Operand(const MachineInstr *MI, int Op) {
406 const MachineOperand &MO1 = MI->getOperand(Op);
407 const MachineOperand &MO2 = MI->getOperand(Op+1);
408 const MachineOperand &MO3 = MI->getOperand(Op+2);
409
410 if (!MO1.isRegister()) { // FIXME: This is for CP entries, but isn't right.
411 printOperand(MI, Op);
412 return;
413 }
414
415 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
416
417 if (!MO2.getReg()) {
418 if (ARM_AM::getAM2Offset(MO3.getImm())) // Don't print +0.
419 O << ", #"
420 << (char)ARM_AM::getAM2Op(MO3.getImm())
421 << ARM_AM::getAM2Offset(MO3.getImm());
422 O << "]";
423 return;
424 }
425
426 O << ", "
427 << (char)ARM_AM::getAM2Op(MO3.getImm())
428 << TM.getRegisterInfo()->get(MO2.getReg()).Name;
429
430 if (unsigned ShImm = ARM_AM::getAM2Offset(MO3.getImm()))
431 O << ", "
432 << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO3.getImmedValue()))
433 << " #" << ShImm;
434 O << "]";
435}
436
437void ARMAsmPrinter::printAddrMode2OffsetOperand(const MachineInstr *MI, int Op){
438 const MachineOperand &MO1 = MI->getOperand(Op);
439 const MachineOperand &MO2 = MI->getOperand(Op+1);
440
441 if (!MO1.getReg()) {
442 if (ARM_AM::getAM2Offset(MO2.getImm())) // Don't print +0.
443 O << "#"
444 << (char)ARM_AM::getAM2Op(MO2.getImm())
445 << ARM_AM::getAM2Offset(MO2.getImm());
446 return;
447 }
448
449 O << (char)ARM_AM::getAM2Op(MO2.getImm())
450 << TM.getRegisterInfo()->get(MO1.getReg()).Name;
451
452 if (unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm()))
453 O << ", "
454 << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO2.getImmedValue()))
455 << " #" << ShImm;
456}
457
458void ARMAsmPrinter::printAddrMode3Operand(const MachineInstr *MI, int Op) {
459 const MachineOperand &MO1 = MI->getOperand(Op);
460 const MachineOperand &MO2 = MI->getOperand(Op+1);
461 const MachineOperand &MO3 = MI->getOperand(Op+2);
462
463 assert(MRegisterInfo::isPhysicalRegister(MO1.getReg()));
464 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
465
466 if (MO2.getReg()) {
467 O << ", "
468 << (char)ARM_AM::getAM3Op(MO3.getImm())
469 << TM.getRegisterInfo()->get(MO2.getReg()).Name
470 << "]";
471 return;
472 }
473
474 if (unsigned ImmOffs = ARM_AM::getAM3Offset(MO3.getImm()))
475 O << ", #"
476 << (char)ARM_AM::getAM3Op(MO3.getImm())
477 << ImmOffs;
478 O << "]";
479}
480
481void ARMAsmPrinter::printAddrMode3OffsetOperand(const MachineInstr *MI, int Op){
482 const MachineOperand &MO1 = MI->getOperand(Op);
483 const MachineOperand &MO2 = MI->getOperand(Op+1);
484
485 if (MO1.getReg()) {
486 O << (char)ARM_AM::getAM3Op(MO2.getImm())
487 << TM.getRegisterInfo()->get(MO1.getReg()).Name;
488 return;
489 }
490
491 unsigned ImmOffs = ARM_AM::getAM3Offset(MO2.getImm());
492 O << "#"
493 << (char)ARM_AM::getAM3Op(MO2.getImm())
494 << ImmOffs;
495}
496
497void ARMAsmPrinter::printAddrMode4Operand(const MachineInstr *MI, int Op,
498 const char *Modifier) {
499 const MachineOperand &MO1 = MI->getOperand(Op);
500 const MachineOperand &MO2 = MI->getOperand(Op+1);
501 ARM_AM::AMSubMode Mode = ARM_AM::getAM4SubMode(MO2.getImm());
502 if (Modifier && strcmp(Modifier, "submode") == 0) {
503 if (MO1.getReg() == ARM::SP) {
504 bool isLDM = (MI->getOpcode() == ARM::LDM ||
505 MI->getOpcode() == ARM::LDM_RET);
506 O << ARM_AM::getAMSubModeAltStr(Mode, isLDM);
507 } else
508 O << ARM_AM::getAMSubModeStr(Mode);
509 } else {
510 printOperand(MI, Op);
511 if (ARM_AM::getAM4WBFlag(MO2.getImm()))
512 O << "!";
513 }
514}
515
516void ARMAsmPrinter::printAddrMode5Operand(const MachineInstr *MI, int Op,
517 const char *Modifier) {
518 const MachineOperand &MO1 = MI->getOperand(Op);
519 const MachineOperand &MO2 = MI->getOperand(Op+1);
520
521 if (!MO1.isRegister()) { // FIXME: This is for CP entries, but isn't right.
522 printOperand(MI, Op);
523 return;
524 }
525
526 assert(MRegisterInfo::isPhysicalRegister(MO1.getReg()));
527
528 if (Modifier && strcmp(Modifier, "submode") == 0) {
529 ARM_AM::AMSubMode Mode = ARM_AM::getAM5SubMode(MO2.getImm());
530 if (MO1.getReg() == ARM::SP) {
531 bool isFLDM = (MI->getOpcode() == ARM::FLDMD ||
532 MI->getOpcode() == ARM::FLDMS);
533 O << ARM_AM::getAMSubModeAltStr(Mode, isFLDM);
534 } else
535 O << ARM_AM::getAMSubModeStr(Mode);
536 return;
537 } else if (Modifier && strcmp(Modifier, "base") == 0) {
538 // Used for FSTM{D|S} and LSTM{D|S} operations.
539 O << TM.getRegisterInfo()->get(MO1.getReg()).Name;
540 if (ARM_AM::getAM5WBFlag(MO2.getImm()))
541 O << "!";
542 return;
543 }
544
545 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
546
547 if (unsigned ImmOffs = ARM_AM::getAM5Offset(MO2.getImm())) {
548 O << ", #"
549 << (char)ARM_AM::getAM5Op(MO2.getImm())
550 << ImmOffs*4;
551 }
552 O << "]";
553}
554
555void ARMAsmPrinter::printAddrModePCOperand(const MachineInstr *MI, int Op,
556 const char *Modifier) {
557 if (Modifier && strcmp(Modifier, "label") == 0) {
558 printPCLabel(MI, Op+1);
559 return;
560 }
561
562 const MachineOperand &MO1 = MI->getOperand(Op);
563 assert(MRegisterInfo::isPhysicalRegister(MO1.getReg()));
564 O << "[pc, +" << TM.getRegisterInfo()->get(MO1.getReg()).Name << "]";
565}
566
567void
568ARMAsmPrinter::printThumbAddrModeRROperand(const MachineInstr *MI, int Op) {
569 const MachineOperand &MO1 = MI->getOperand(Op);
570 const MachineOperand &MO2 = MI->getOperand(Op+1);
571 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
572 O << ", " << TM.getRegisterInfo()->get(MO2.getReg()).Name << "]";
573}
574
575void
576ARMAsmPrinter::printThumbAddrModeRI5Operand(const MachineInstr *MI, int Op,
577 unsigned Scale) {
578 const MachineOperand &MO1 = MI->getOperand(Op);
Evan Chengcea117d2007-01-30 02:35:32 +0000579 const MachineOperand &MO2 = MI->getOperand(Op+1);
580 const MachineOperand &MO3 = MI->getOperand(Op+2);
Evan Chenga8e29892007-01-19 07:51:42 +0000581
582 if (!MO1.isRegister()) { // FIXME: This is for CP entries, but isn't right.
583 printOperand(MI, Op);
584 return;
585 }
586
587 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
Evan Chengcea117d2007-01-30 02:35:32 +0000588 if (MO3.getReg())
589 O << ", " << TM.getRegisterInfo()->get(MO3.getReg()).Name;
590 else if (unsigned ImmOffs = MO2.getImm()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000591 O << ", #" << ImmOffs;
592 if (Scale > 1)
593 O << " * " << Scale;
594 }
595 O << "]";
596}
597
598void
Evan Chengc38f2bc2007-01-23 22:59:13 +0000599ARMAsmPrinter::printThumbAddrModeS1Operand(const MachineInstr *MI, int Op) {
Evan Chengcea117d2007-01-30 02:35:32 +0000600 printThumbAddrModeRI5Operand(MI, Op, 1);
Evan Chenga8e29892007-01-19 07:51:42 +0000601}
602void
Evan Chengc38f2bc2007-01-23 22:59:13 +0000603ARMAsmPrinter::printThumbAddrModeS2Operand(const MachineInstr *MI, int Op) {
Evan Chengcea117d2007-01-30 02:35:32 +0000604 printThumbAddrModeRI5Operand(MI, Op, 2);
Evan Chenga8e29892007-01-19 07:51:42 +0000605}
606void
Evan Chengc38f2bc2007-01-23 22:59:13 +0000607ARMAsmPrinter::printThumbAddrModeS4Operand(const MachineInstr *MI, int Op) {
Evan Chengcea117d2007-01-30 02:35:32 +0000608 printThumbAddrModeRI5Operand(MI, Op, 4);
Evan Chenga8e29892007-01-19 07:51:42 +0000609}
610
611void ARMAsmPrinter::printThumbAddrModeSPOperand(const MachineInstr *MI,int Op) {
612 const MachineOperand &MO1 = MI->getOperand(Op);
613 const MachineOperand &MO2 = MI->getOperand(Op+1);
614 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
615 if (unsigned ImmOffs = MO2.getImm())
616 O << ", #" << ImmOffs << " * 4";
617 O << "]";
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000618}
619
620void ARMAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
Rafael Espindola6f602de2006-08-24 16:13:15 +0000621 int CC = (int)MI->getOperand(opNum).getImmedValue();
622 O << ARMCondCodeToString((ARMCC::CondCodes)CC);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000623}
624
Evan Chenga8e29892007-01-19 07:51:42 +0000625void ARMAsmPrinter::printPCLabel(const MachineInstr *MI, int opNum) {
626 int Id = (int)MI->getOperand(opNum).getImmedValue();
627 O << TAI->getPrivateGlobalPrefix() << "PC" << Id;
628}
629
630void ARMAsmPrinter::printRegisterList(const MachineInstr *MI, int opNum) {
631 O << "{";
632 for (unsigned i = opNum, e = MI->getNumOperands(); i != e; ++i) {
633 printOperand(MI, i);
634 if (i != e-1) O << ", ";
635 }
636 O << "}";
637}
638
639void ARMAsmPrinter::printCPInstOperand(const MachineInstr *MI, int OpNo,
640 const char *Modifier) {
641 assert(Modifier && "This operand only works with a modifier!");
642 // There are two aspects to a CONSTANTPOOL_ENTRY operand, the label and the
643 // data itself.
644 if (!strcmp(Modifier, "label")) {
645 unsigned ID = MI->getOperand(OpNo).getImm();
646 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
647 << '_' << ID << ":\n";
648 } else {
649 assert(!strcmp(Modifier, "cpentry") && "Unknown modifier for CPE");
650 unsigned CPI = MI->getOperand(OpNo).getConstantPoolIndex();
651
652 const MachineConstantPoolEntry &MCPE = // Chasing pointers is fun?
653 MI->getParent()->getParent()->getConstantPool()->getConstants()[CPI];
654
655 if (MCPE.isMachineConstantPoolEntry())
656 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
Lauro Ramos Venancio305b8a52007-04-25 14:50:40 +0000657 else {
Evan Chenga8e29892007-01-19 07:51:42 +0000658 EmitGlobalConstant(MCPE.Val.ConstVal);
Lauro Ramos Venancio305b8a52007-04-25 14:50:40 +0000659 // remember to emit the weak reference
660 if (const GlobalValue *GV = dyn_cast<GlobalValue>(MCPE.Val.ConstVal))
661 if (GV->hasExternalWeakLinkage())
662 ExtWeakSymbols.insert(GV);
663 }
Evan Chenga8e29892007-01-19 07:51:42 +0000664 }
665}
666
667void ARMAsmPrinter::printJTBlockOperand(const MachineInstr *MI, int OpNo) {
668 const MachineOperand &MO1 = MI->getOperand(OpNo);
669 const MachineOperand &MO2 = MI->getOperand(OpNo+1); // Unique Id
670 unsigned JTI = MO1.getJumpTableIndex();
671 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
672 << '_' << JTI << '_' << MO2.getImmedValue() << ":\n";
673
674 const char *JTEntryDirective = TAI->getJumpTableDirective();
675 if (!JTEntryDirective)
676 JTEntryDirective = TAI->getData32bitsDirective();
677
678 const MachineFunction *MF = MI->getParent()->getParent();
679 MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
680 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
681 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
682 bool UseSet= TAI->getSetDirective() && TM.getRelocationModel() == Reloc::PIC_;
683 std::set<MachineBasicBlock*> JTSets;
684 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
685 MachineBasicBlock *MBB = JTBBs[i];
686 if (UseSet && JTSets.insert(MBB).second)
687 printSetLabel(JTI, MO2.getImmedValue(), MBB);
688
689 O << JTEntryDirective << ' ';
690 if (UseSet)
691 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
692 << '_' << JTI << '_' << MO2.getImmedValue()
693 << "_set_" << MBB->getNumber();
694 else if (TM.getRelocationModel() == Reloc::PIC_) {
695 printBasicBlockLabel(MBB, false, false);
696 // If the arch uses custom Jump Table directives, don't calc relative to JT
697 if (!TAI->getJumpTableDirective())
698 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
699 << getFunctionNumber() << '_' << JTI << '_' << MO2.getImmedValue();
700 } else
701 printBasicBlockLabel(MBB, false, false);
Evan Chengd85ac4d2007-01-27 02:29:45 +0000702 if (i != e-1)
703 O << '\n';
Evan Chenga8e29892007-01-19 07:51:42 +0000704 }
705}
706
707
708bool ARMAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
709 unsigned AsmVariant, const char *ExtraCode){
710 // Does this asm operand have a single letter operand modifier?
711 if (ExtraCode && ExtraCode[0]) {
712 if (ExtraCode[1] != 0) return true; // Unknown modifier.
713
714 switch (ExtraCode[0]) {
715 default: return true; // Unknown modifier.
Evan Cheng23a95702007-03-08 22:42:46 +0000716 case 'c': // Don't print "$" before a global var name or constant.
Evan Chenge21e3962007-04-04 00:13:29 +0000717 case 'P': // Print a VFP double precision register.
Evan Cheng23a95702007-03-08 22:42:46 +0000718 printOperand(MI, OpNo);
719 return false;
Evan Chenga8e29892007-01-19 07:51:42 +0000720 case 'Q':
721 if (TM.getTargetData()->isLittleEndian())
722 break;
723 // Fallthrough
724 case 'R':
725 if (TM.getTargetData()->isBigEndian())
726 break;
727 // Fallthrough
728 case 'H': // Write second word of DI / DF reference.
729 // Verify that this operand has two consecutive registers.
730 if (!MI->getOperand(OpNo).isRegister() ||
731 OpNo+1 == MI->getNumOperands() ||
732 !MI->getOperand(OpNo+1).isRegister())
733 return true;
734 ++OpNo; // Return the high-part.
735 }
736 }
737
738 printOperand(MI, OpNo);
739 return false;
740}
741
742void ARMAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
743 ++EmittedInsts;
744
Evan Chengc60e76d2007-01-30 20:37:08 +0000745 int Opc = MI->getOpcode();
746 switch (Opc) {
747 case ARM::CONSTPOOL_ENTRY:
Evan Chenga8e29892007-01-19 07:51:42 +0000748 if (!InCPMode && AFI->isThumbFunction()) {
749 EmitAlignment(2);
750 InCPMode = true;
751 }
Evan Chengc60e76d2007-01-30 20:37:08 +0000752 break;
753 default: {
Evan Cheng3bf12d02007-01-31 23:39:39 +0000754 if (InCPMode && AFI->isThumbFunction())
Evan Chenga8e29892007-01-19 07:51:42 +0000755 InCPMode = false;
Evan Chengc60e76d2007-01-30 20:37:08 +0000756 switch (Opc) {
757 case ARM::PICADD:
758 case ARM::PICLD:
759 case ARM::tPICADD:
760 break;
761 default:
762 O << "\t";
763 break;
764 }
765 }}
Evan Chenga8e29892007-01-19 07:51:42 +0000766
767 // Call the autogenerated instruction printer routines.
768 printInstruction(MI);
769}
770
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000771bool ARMAsmPrinter::doInitialization(Module &M) {
Evan Cheng5be54b02007-01-19 19:25:36 +0000772 if (Subtarget->isTargetDarwin()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000773 // Emit initial debug information.
774 DW.BeginModule(&M);
775 }
776
777 return AsmPrinter::doInitialization(M);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000778}
779
780bool ARMAsmPrinter::doFinalization(Module &M) {
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000781 const TargetData *TD = TM.getTargetData();
782
783 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
784 I != E; ++I) {
785 if (!I->hasInitializer()) // External global require no code
786 continue;
787
Evan Chengb267ca12007-01-30 08:04:53 +0000788 if (EmitSpecialLLVMGlobal(I)) {
789 if (Subtarget->isTargetDarwin() &&
790 TM.getRelocationModel() == Reloc::Static) {
791 if (I->getName() == "llvm.global_ctors")
792 O << ".reference .constructors_used\n";
793 else if (I->getName() == "llvm.global_dtors")
794 O << ".reference .destructors_used\n";
795 }
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000796 continue;
Evan Chengb267ca12007-01-30 08:04:53 +0000797 }
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000798
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000799 std::string name = Mang->getValueName(I);
800 Constant *C = I->getInitializer();
Evan Cheng98ded762007-03-08 01:25:25 +0000801 const Type *Type = C->getType();
802 unsigned Size = TD->getTypeSize(Type);
Evan Chenga8e29892007-01-19 07:51:42 +0000803 unsigned Align = TD->getPreferredAlignmentLog(I);
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000804
Lauro Ramos Venancio0a181732007-04-30 00:23:51 +0000805 const char *VisibilityDirective = NULL;
Evan Cheng5be54b02007-01-19 19:25:36 +0000806 if (I->hasHiddenVisibility())
Lauro Ramos Venancio0a181732007-04-30 00:23:51 +0000807 VisibilityDirective = TAI->getHiddenDirective();
808 else if (I->hasProtectedVisibility())
809 VisibilityDirective = TAI->getProtectedDirective();
810
811 if (VisibilityDirective)
812 O << VisibilityDirective << name << "\n";
813
Evan Cheng5be54b02007-01-19 19:25:36 +0000814 if (Subtarget->isTargetELF())
Lauro Ramos Venanciob1cc0522007-01-25 20:11:04 +0000815 O << "\t.type " << name << ",%object\n";
Evan Cheng5be54b02007-01-19 19:25:36 +0000816
817 if (C->isNullValue()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000818 if (I->hasExternalLinkage()) {
Evan Cheng5be54b02007-01-19 19:25:36 +0000819 if (const char *Directive = TAI->getZeroFillDirective()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000820 O << "\t.globl\t" << name << "\n";
Evan Cheng5be54b02007-01-19 19:25:36 +0000821 O << Directive << "__DATA__, __common, " << name << ", "
822 << Size << ", " << Align << "\n";
823 continue;
824 }
825 }
826
827 if (!I->hasSection() &&
828 (I->hasInternalLinkage() || I->hasWeakLinkage() ||
829 I->hasLinkOnceLinkage())) {
830 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
831 if (!NoZerosInBSS && TAI->getBSSSection())
832 SwitchToDataSection(TAI->getBSSSection(), I);
833 else
834 SwitchToDataSection(TAI->getDataSection(), I);
Evan Chenga8e29892007-01-19 07:51:42 +0000835 if (TAI->getLCOMMDirective() != NULL) {
836 if (I->hasInternalLinkage()) {
837 O << TAI->getLCOMMDirective() << name << "," << Size;
Evan Cheng5be54b02007-01-19 19:25:36 +0000838 if (Subtarget->isTargetDarwin())
Evan Chenga8e29892007-01-19 07:51:42 +0000839 O << "," << Align;
840 } else
841 O << TAI->getCOMMDirective() << name << "," << Size;
842 } else {
843 if (I->hasInternalLinkage())
844 O << "\t.local\t" << name << "\n";
845 O << TAI->getCOMMDirective() << name << "," << Size;
846 if (TAI->getCOMMDirectiveTakesAlignment())
847 O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
848 }
Evan Cheng5be54b02007-01-19 19:25:36 +0000849 O << "\t\t" << TAI->getCommentString() << " " << I->getName() << "\n";
850 continue;
Evan Chenga8e29892007-01-19 07:51:42 +0000851 }
Evan Cheng5be54b02007-01-19 19:25:36 +0000852 }
Evan Chenga8e29892007-01-19 07:51:42 +0000853
Evan Cheng5be54b02007-01-19 19:25:36 +0000854 switch (I->getLinkage()) {
855 case GlobalValue::LinkOnceLinkage:
856 case GlobalValue::WeakLinkage:
857 if (Subtarget->isTargetDarwin()) {
858 O << "\t.globl " << name << "\n"
859 << "\t.weak_definition " << name << "\n";
860 SwitchToDataSection("\t.section __DATA,__const_coal,coalesced", I);
861 } else {
862 std::string SectionName("\t.section\t.llvm.linkonce.d." +
863 name +
864 ",\"aw\",%progbits");
865 SwitchToDataSection(SectionName.c_str(), I);
866 O << "\t.weak " << name << "\n";
867 }
868 break;
869 case GlobalValue::AppendingLinkage:
870 // FIXME: appending linkage variables should go into a section of
871 // their name or something. For now, just emit them as external.
872 case GlobalValue::ExternalLinkage:
873 O << "\t.globl " << name << "\n";
874 // FALL THROUGH
875 case GlobalValue::InternalLinkage: {
876 if (I->isConstant()) {
877 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
878 if (TAI->getCStringSection() && CVA && CVA->isCString()) {
879 SwitchToDataSection(TAI->getCStringSection(), I);
880 break;
881 }
882 }
883 // FIXME: special handling for ".ctors" & ".dtors" sections
884 if (I->hasSection() &&
885 (I->getSection() == ".ctors" ||
886 I->getSection() == ".dtors")) {
887 assert(!Subtarget->isTargetDarwin());
888 std::string SectionName = ".section " + I->getSection();
889 SectionName += ",\"aw\",%progbits";
890 SwitchToDataSection(SectionName.c_str());
891 } else {
892 if (C->isNullValue() && !NoZerosInBSS && TAI->getBSSSection())
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000893 SwitchToDataSection(I->isThreadLocal() ? TAI->getTLSBSSSection() :
894 TAI->getBSSSection(), I);
Evan Cheng98ded762007-03-08 01:25:25 +0000895 else if (!I->isConstant())
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000896 SwitchToDataSection(I->isThreadLocal() ? TAI->getTLSDataSection() :
897 TAI->getDataSection(), I);
898 else if (I->isThreadLocal())
899 SwitchToDataSection(TAI->getTLSDataSection());
Evan Cheng98ded762007-03-08 01:25:25 +0000900 else {
901 // Read-only data.
Evan Cheng032953d2007-03-08 08:31:54 +0000902 bool HasReloc = C->ContainsRelocations();
903 if (HasReloc &&
904 Subtarget->isTargetDarwin() &&
Evan Cheng98ded762007-03-08 01:25:25 +0000905 TM.getRelocationModel() != Reloc::Static)
906 SwitchToDataSection("\t.const_data\n");
Evan Cheng032953d2007-03-08 08:31:54 +0000907 else if (!HasReloc && Size == 4 &&
Evan Cheng98ded762007-03-08 01:25:25 +0000908 TAI->getFourByteConstantSection())
909 SwitchToDataSection(TAI->getFourByteConstantSection(), I);
Evan Cheng032953d2007-03-08 08:31:54 +0000910 else if (!HasReloc && Size == 8 &&
Evan Cheng98ded762007-03-08 01:25:25 +0000911 TAI->getEightByteConstantSection())
912 SwitchToDataSection(TAI->getEightByteConstantSection(), I);
Evan Cheng032953d2007-03-08 08:31:54 +0000913 else if (!HasReloc && Size == 16 &&
Evan Cheng98ded762007-03-08 01:25:25 +0000914 TAI->getSixteenByteConstantSection())
915 SwitchToDataSection(TAI->getSixteenByteConstantSection(), I);
916 else if (TAI->getReadOnlySection())
917 SwitchToDataSection(TAI->getReadOnlySection(), I);
918 else
919 SwitchToDataSection(TAI->getDataSection(), I);
920 }
Rafael Espindolab97809c2006-10-19 13:30:40 +0000921 }
Evan Cheng5be54b02007-01-19 19:25:36 +0000922
923 break;
924 }
925 default:
926 assert(0 && "Unknown linkage type!");
927 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000928 }
Rafael Espindolab97809c2006-10-19 13:30:40 +0000929
Evan Chenga8e29892007-01-19 07:51:42 +0000930 EmitAlignment(Align, I);
Evan Cheng5be54b02007-01-19 19:25:36 +0000931 O << name << ":\t\t\t\t" << TAI->getCommentString() << " " << I->getName()
932 << "\n";
933 if (TAI->hasDotTypeDotSizeDirective())
Rafael Espindolab97809c2006-10-19 13:30:40 +0000934 O << "\t.size " << name << ", " << Size << "\n";
Evan Chenga8e29892007-01-19 07:51:42 +0000935 // If the initializer is a extern weak symbol, remember to emit the weak
936 // reference!
937 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
938 if (GV->hasExternalWeakLinkage())
939 ExtWeakSymbols.insert(GV);
940
941 EmitGlobalConstant(C);
942 O << '\n';
943 }
944
Evan Cheng5be54b02007-01-19 19:25:36 +0000945 if (Subtarget->isTargetDarwin()) {
946 SwitchToDataSection("");
947
Evan Chenga8e29892007-01-19 07:51:42 +0000948 // Output stubs for dynamically-linked functions
949 unsigned j = 1;
950 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
951 i != e; ++i, ++j) {
952 if (TM.getRelocationModel() == Reloc::PIC_)
953 SwitchToTextSection(".section __TEXT,__picsymbolstub4,symbol_stubs,"
954 "none,16", 0);
955 else
956 SwitchToTextSection(".section __TEXT,__symbol_stub4,symbol_stubs,"
957 "none,12", 0);
958
959 EmitAlignment(2);
960 O << "\t.code\t32\n";
961
962 O << "L" << *i << "$stub:\n";
963 O << "\t.indirect_symbol " << *i << "\n";
964 O << "\tldr ip, L" << *i << "$slp\n";
965 if (TM.getRelocationModel() == Reloc::PIC_) {
966 O << "L" << *i << "$scv:\n";
967 O << "\tadd ip, pc, ip\n";
968 }
969 O << "\tldr pc, [ip, #0]\n";
970 O << "L" << *i << "$slp:\n";
971 if (TM.getRelocationModel() == Reloc::PIC_)
972 O << "\t.long\tL" << *i << "$lazy_ptr-(L" << *i << "$scv+8)\n";
973 else
974 O << "\t.long\tL" << *i << "$lazy_ptr\n";
975 SwitchToDataSection(".lazy_symbol_pointer", 0);
976 O << "L" << *i << "$lazy_ptr:\n";
977 O << "\t.indirect_symbol " << *i << "\n";
978 O << "\t.long\tdyld_stub_binding_helper\n";
979 }
980 O << "\n";
981
982 // Output non-lazy-pointers for external and common global variables.
983 if (GVNonLazyPtrs.begin() != GVNonLazyPtrs.end())
984 SwitchToDataSection(".non_lazy_symbol_pointer", 0);
985 for (std::set<std::string>::iterator i = GVNonLazyPtrs.begin(),
986 e = GVNonLazyPtrs.end(); i != e; ++i) {
987 O << "L" << *i << "$non_lazy_ptr:\n";
988 O << "\t.indirect_symbol " << *i << "\n";
989 O << "\t.long\t0\n";
990 }
991
992 // Emit initial debug information.
993 DW.EndModule();
994
995 // Funny Darwin hack: This flag tells the linker that no global symbols
996 // contain code that falls through to other global symbols (e.g. the obvious
997 // implementation of multiple entry points). If this doesn't occur, the
998 // linker can safely perform dead code stripping. Since LLVM never
999 // generates code that does this, it is always safe to set.
1000 O << "\t.subsections_via_symbols\n";
Rafael Espindolab01c4bb2006-07-27 11:38:51 +00001001 }
Rafael Espindolab97809c2006-10-19 13:30:40 +00001002
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00001003 AsmPrinter::doFinalization(M);
1004 return false; // success
1005}