blob: 6d7c93f3e659d9e0522e639f38eccad63da9023f [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);
Evan Cheng42d712b2007-05-08 21:08:43 +0000104 void printPredicateOperand(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
Lauro Ramos Venancioe8e54952007-05-03 20:28:35 +0000178 DW.SetModuleInfo(&getAnalysis<MachineModuleInfo>());
Chris Lattner1a199de2006-12-21 22:59:58 +0000179
Evan Chenga8e29892007-01-19 07:51:42 +0000180 SetupMachineFunction(MF);
181 O << "\n";
Rafael Espindola4b442b52006-05-23 02:48:20 +0000182
Evan Chenga8e29892007-01-19 07:51:42 +0000183 // NOTE: we don't print out constant pools here, they are handled as
184 // instructions.
185
186 O << "\n";
Rafael Espindola4b442b52006-05-23 02:48:20 +0000187 // Print out labels for the function.
188 const Function *F = MF.getFunction();
189 switch (F->getLinkage()) {
190 default: assert(0 && "Unknown linkage type!");
191 case Function::InternalLinkage:
Evan Chenga8e29892007-01-19 07:51:42 +0000192 SwitchToTextSection("\t.text", F);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000193 break;
194 case Function::ExternalLinkage:
Evan Chenga8e29892007-01-19 07:51:42 +0000195 SwitchToTextSection("\t.text", F);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000196 O << "\t.globl\t" << CurrentFnName << "\n";
197 break;
198 case Function::WeakLinkage:
199 case Function::LinkOnceLinkage:
Evan Cheng5be54b02007-01-19 19:25:36 +0000200 if (Subtarget->isTargetDarwin()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000201 SwitchToTextSection(
202 ".section __TEXT,__textcoal_nt,coalesced,pure_instructions", F);
203 O << "\t.globl\t" << CurrentFnName << "\n";
204 O << "\t.weak_definition\t" << CurrentFnName << "\n";
205 } else {
206 O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
207 }
Rafael Espindola4b442b52006-05-23 02:48:20 +0000208 break;
209 }
Evan Chenga8e29892007-01-19 07:51:42 +0000210
Lauro Ramos Venancio0a181732007-04-30 00:23:51 +0000211 const char *VisibilityDirective = NULL;
Evan Cheng616cc662007-03-29 07:49:34 +0000212 if (F->hasHiddenVisibility())
Lauro Ramos Venancio0a181732007-04-30 00:23:51 +0000213 VisibilityDirective = TAI->getHiddenDirective();
214 else if (F->hasProtectedVisibility())
215 VisibilityDirective = TAI->getProtectedDirective();
216
217 if (VisibilityDirective)
218 O << VisibilityDirective << CurrentFnName << "\n";
Evan Cheng616cc662007-03-29 07:49:34 +0000219
Evan Chenga8e29892007-01-19 07:51:42 +0000220 if (AFI->isThumbFunction()) {
Chris Lattner3a420532007-05-31 18:57:45 +0000221 EmitAlignment(1, F, AFI->getAlign());
Evan Chenga8e29892007-01-19 07:51:42 +0000222 O << "\t.code\t16\n";
Lauro Ramos Venancio6f46e592007-02-01 18:25:34 +0000223 O << "\t.thumb_func";
224 if (Subtarget->isTargetDarwin())
225 O << "\t" << CurrentFnName;
226 O << "\n";
Evan Chenga8e29892007-01-19 07:51:42 +0000227 InCPMode = false;
228 } else
229 EmitAlignment(2, F);
230
Rafael Espindola4b442b52006-05-23 02:48:20 +0000231 O << CurrentFnName << ":\n";
Lauro Ramos Venancioe8e54952007-05-03 20:28:35 +0000232 // Emit pre-function debug information.
233 DW.BeginFunction(&MF);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000234
235 // Print out code for the function.
236 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
237 I != E; ++I) {
238 // Print a label for the basic block.
239 if (I != MF.begin()) {
240 printBasicBlockLabel(I, true);
241 O << '\n';
242 }
243 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
244 II != E; ++II) {
245 // Print the assembly for the instruction.
Evan Chenga8e29892007-01-19 07:51:42 +0000246 printMachineInstruction(II);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000247 }
248 }
249
Evan Chenga8e29892007-01-19 07:51:42 +0000250 if (TAI->hasDotTypeDotSizeDirective())
251 O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
252
Lauro Ramos Venancioe8e54952007-05-03 20:28:35 +0000253 // Emit post-function debug information.
254 DW.EndFunction();
Evan Chenga8e29892007-01-19 07:51:42 +0000255
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000256 return false;
257}
258
Evan Chenga8e29892007-01-19 07:51:42 +0000259void ARMAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
260 const char *Modifier) {
261 const MachineOperand &MO = MI->getOperand(opNum);
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000262 switch (MO.getType()) {
263 case MachineOperand::MO_Register:
264 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
Evan Chenga8e29892007-01-19 07:51:42 +0000265 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000266 else
267 assert(0 && "not implemented");
268 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000269 case MachineOperand::MO_Immediate: {
270 if (!Modifier || strcmp(Modifier, "no_hash") != 0)
271 O << "#";
272
273 O << (int)MO.getImmedValue();
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000274 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000275 }
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000276 case MachineOperand::MO_MachineBasicBlock:
Rafael Espindola687bc492006-08-24 13:45:55 +0000277 printBasicBlockLabel(MO.getMachineBasicBlock());
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000278 return;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000279 case MachineOperand::MO_GlobalAddress: {
Evan Chenga8e29892007-01-19 07:51:42 +0000280 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Rafael Espindola84b19be2006-07-16 01:02:57 +0000281 GlobalValue *GV = MO.getGlobal();
282 std::string Name = Mang->getValueName(GV);
Reid Spencer5cbf9852007-01-30 20:08:39 +0000283 bool isExt = (GV->isDeclaration() || GV->hasWeakLinkage() ||
Evan Chenga8e29892007-01-19 07:51:42 +0000284 GV->hasLinkOnceLinkage());
Evan Cheng5be54b02007-01-19 19:25:36 +0000285 if (isExt && isCallOp && Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +0000286 TM.getRelocationModel() != Reloc::Static) {
287 O << TAI->getPrivateGlobalPrefix() << Name << "$stub";
288 FnStubs.insert(Name);
289 } else
290 O << Name;
Chris Lattner388488d2007-05-03 16:42:23 +0000291
292 if (MO.getOffset() > 0)
293 O << '+' << MO.getOffset();
294 else if (MO.getOffset() < 0)
295 O << MO.getOffset();
296
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000297 if (isCallOp && Subtarget->isTargetELF() &&
298 TM.getRelocationModel() == Reloc::PIC_)
299 O << "(PLT)";
Evan Chenga8e29892007-01-19 07:51:42 +0000300 if (GV->hasExternalWeakLinkage())
Rafael Espindola15404d02006-12-18 03:37:18 +0000301 ExtWeakSymbols.insert(GV);
Evan Chenga8e29892007-01-19 07:51:42 +0000302 break;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000303 }
Evan Chenga8e29892007-01-19 07:51:42 +0000304 case MachineOperand::MO_ExternalSymbol: {
305 bool isCallOp = Modifier && !strcmp(Modifier, "call");
306 std::string Name(TAI->getGlobalPrefix());
307 Name += MO.getSymbolName();
Evan Cheng5be54b02007-01-19 19:25:36 +0000308 if (isCallOp && Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +0000309 TM.getRelocationModel() != Reloc::Static) {
310 O << TAI->getPrivateGlobalPrefix() << Name << "$stub";
311 FnStubs.insert(Name);
312 } else
313 O << Name;
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000314 if (isCallOp && Subtarget->isTargetELF() &&
315 TM.getRelocationModel() == Reloc::PIC_)
316 O << "(PLT)";
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000317 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000318 }
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000319 case MachineOperand::MO_ConstantPoolIndex:
Jim Laskey563321a2006-09-06 18:34:40 +0000320 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000321 << '_' << MO.getConstantPoolIndex();
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000322 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000323 case MachineOperand::MO_JumpTableIndex:
324 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
325 << '_' << MO.getJumpTableIndex();
326 break;
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000327 default:
328 O << "<unknown operand type>"; abort (); break;
329 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000330}
331
Evan Chengc70d1842007-03-20 08:11:30 +0000332static void printSOImm(std::ostream &O, int64_t V, const TargetAsmInfo *TAI) {
333 assert(V < (1 << 12) && "Not a valid so_imm value!");
334 unsigned Imm = ARM_AM::getSOImmValImm(V);
335 unsigned Rot = ARM_AM::getSOImmValRot(V);
Evan Chenga8e29892007-01-19 07:51:42 +0000336
337 // Print low-level immediate formation info, per
338 // A5.1.3: "Data-processing operands - Immediate".
339 if (Rot) {
340 O << "#" << Imm << ", " << Rot;
341 // Pretty printed version.
342 O << ' ' << TAI->getCommentString() << ' ' << (int)ARM_AM::rotr32(Imm, Rot);
343 } else {
344 O << "#" << Imm;
345 }
346}
347
Evan Chengc70d1842007-03-20 08:11:30 +0000348/// printSOImmOperand - SOImm is 4-bit rotate amount in bits 8-11 with 8-bit
349/// immediate in bits 0-7.
350void ARMAsmPrinter::printSOImmOperand(const MachineInstr *MI, int OpNum) {
351 const MachineOperand &MO = MI->getOperand(OpNum);
352 assert(MO.isImmediate() && "Not a valid so_imm value!");
353 printSOImm(O, MO.getImmedValue(), TAI);
354}
355
356/// printSOImm2PartOperand - SOImm is broken into two pieces using a mov
357/// followed by a or to materialize.
358void ARMAsmPrinter::printSOImm2PartOperand(const MachineInstr *MI, int OpNum) {
359 const MachineOperand &MO = MI->getOperand(OpNum);
360 assert(MO.isImmediate() && "Not a valid so_imm value!");
361 unsigned V1 = ARM_AM::getSOImmTwoPartFirst(MO.getImmedValue());
362 unsigned V2 = ARM_AM::getSOImmTwoPartSecond(MO.getImmedValue());
363 printSOImm(O, ARM_AM::getSOImmVal(V1), TAI);
Evan Cheng5e148a32007-06-05 18:55:18 +0000364 O << "\n\torr";
365 printPredicateOperand(MI, 2);
366 O << " ";
Evan Chengc70d1842007-03-20 08:11:30 +0000367 printOperand(MI, 0);
368 O << ", ";
369 printOperand(MI, 0);
370 O << ", ";
371 printSOImm(O, ARM_AM::getSOImmVal(V2), TAI);
372}
373
Evan Chenga8e29892007-01-19 07:51:42 +0000374// so_reg is a 4-operand unit corresponding to register forms of the A5.1
375// "Addressing Mode 1 - Data-processing operands" forms. This includes:
376// REG 0 0 - e.g. R5
377// REG REG 0,SH_OPC - e.g. R5, ROR R3
378// REG 0 IMM,SH_OPC - e.g. R5, LSL #3
379void ARMAsmPrinter::printSORegOperand(const MachineInstr *MI, int Op) {
380 const MachineOperand &MO1 = MI->getOperand(Op);
381 const MachineOperand &MO2 = MI->getOperand(Op+1);
382 const MachineOperand &MO3 = MI->getOperand(Op+2);
383
384 assert(MRegisterInfo::isPhysicalRegister(MO1.getReg()));
385 O << TM.getRegisterInfo()->get(MO1.getReg()).Name;
386
387 // Print the shift opc.
388 O << ", "
389 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(MO3.getImmedValue()))
390 << " ";
391
392 if (MO2.getReg()) {
393 assert(MRegisterInfo::isPhysicalRegister(MO2.getReg()));
394 O << TM.getRegisterInfo()->get(MO2.getReg()).Name;
395 assert(ARM_AM::getSORegOffset(MO3.getImm()) == 0);
396 } else {
397 O << "#" << ARM_AM::getSORegOffset(MO3.getImm());
398 }
399}
400
401void ARMAsmPrinter::printAddrMode2Operand(const MachineInstr *MI, int Op) {
402 const MachineOperand &MO1 = MI->getOperand(Op);
403 const MachineOperand &MO2 = MI->getOperand(Op+1);
404 const MachineOperand &MO3 = MI->getOperand(Op+2);
405
406 if (!MO1.isRegister()) { // FIXME: This is for CP entries, but isn't right.
407 printOperand(MI, Op);
408 return;
409 }
410
411 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
412
413 if (!MO2.getReg()) {
414 if (ARM_AM::getAM2Offset(MO3.getImm())) // Don't print +0.
415 O << ", #"
416 << (char)ARM_AM::getAM2Op(MO3.getImm())
417 << ARM_AM::getAM2Offset(MO3.getImm());
418 O << "]";
419 return;
420 }
421
422 O << ", "
423 << (char)ARM_AM::getAM2Op(MO3.getImm())
424 << TM.getRegisterInfo()->get(MO2.getReg()).Name;
425
426 if (unsigned ShImm = ARM_AM::getAM2Offset(MO3.getImm()))
427 O << ", "
428 << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO3.getImmedValue()))
429 << " #" << ShImm;
430 O << "]";
431}
432
433void ARMAsmPrinter::printAddrMode2OffsetOperand(const MachineInstr *MI, int Op){
434 const MachineOperand &MO1 = MI->getOperand(Op);
435 const MachineOperand &MO2 = MI->getOperand(Op+1);
436
437 if (!MO1.getReg()) {
Evan Chengbdc98692007-05-03 23:30:36 +0000438 unsigned ImmOffs = ARM_AM::getAM2Offset(MO2.getImm());
439 assert(ImmOffs && "Malformed indexed load / store!");
440 O << "#"
441 << (char)ARM_AM::getAM2Op(MO2.getImm())
442 << ImmOffs;
Evan Chenga8e29892007-01-19 07:51:42 +0000443 return;
444 }
445
446 O << (char)ARM_AM::getAM2Op(MO2.getImm())
447 << TM.getRegisterInfo()->get(MO1.getReg()).Name;
448
449 if (unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm()))
450 O << ", "
451 << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO2.getImmedValue()))
452 << " #" << ShImm;
453}
454
455void ARMAsmPrinter::printAddrMode3Operand(const MachineInstr *MI, int Op) {
456 const MachineOperand &MO1 = MI->getOperand(Op);
457 const MachineOperand &MO2 = MI->getOperand(Op+1);
458 const MachineOperand &MO3 = MI->getOperand(Op+2);
459
460 assert(MRegisterInfo::isPhysicalRegister(MO1.getReg()));
461 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
462
463 if (MO2.getReg()) {
464 O << ", "
465 << (char)ARM_AM::getAM3Op(MO3.getImm())
466 << TM.getRegisterInfo()->get(MO2.getReg()).Name
467 << "]";
468 return;
469 }
470
471 if (unsigned ImmOffs = ARM_AM::getAM3Offset(MO3.getImm()))
472 O << ", #"
473 << (char)ARM_AM::getAM3Op(MO3.getImm())
474 << ImmOffs;
475 O << "]";
476}
477
478void ARMAsmPrinter::printAddrMode3OffsetOperand(const MachineInstr *MI, int Op){
479 const MachineOperand &MO1 = MI->getOperand(Op);
480 const MachineOperand &MO2 = MI->getOperand(Op+1);
481
482 if (MO1.getReg()) {
483 O << (char)ARM_AM::getAM3Op(MO2.getImm())
484 << TM.getRegisterInfo()->get(MO1.getReg()).Name;
485 return;
486 }
487
488 unsigned ImmOffs = ARM_AM::getAM3Offset(MO2.getImm());
Evan Chengbdc98692007-05-03 23:30:36 +0000489 assert(ImmOffs && "Malformed indexed load / store!");
Evan Chenga8e29892007-01-19 07:51:42 +0000490 O << "#"
Evan Chengbdc98692007-05-03 23:30:36 +0000491 << (char)ARM_AM::getAM3Op(MO2.getImm())
Evan Chenga8e29892007-01-19 07:51:42 +0000492 << ImmOffs;
493}
494
495void ARMAsmPrinter::printAddrMode4Operand(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 ARM_AM::AMSubMode Mode = ARM_AM::getAM4SubMode(MO2.getImm());
500 if (Modifier && strcmp(Modifier, "submode") == 0) {
501 if (MO1.getReg() == ARM::SP) {
502 bool isLDM = (MI->getOpcode() == ARM::LDM ||
503 MI->getOpcode() == ARM::LDM_RET);
504 O << ARM_AM::getAMSubModeAltStr(Mode, isLDM);
505 } else
506 O << ARM_AM::getAMSubModeStr(Mode);
507 } else {
508 printOperand(MI, Op);
509 if (ARM_AM::getAM4WBFlag(MO2.getImm()))
510 O << "!";
511 }
512}
513
514void ARMAsmPrinter::printAddrMode5Operand(const MachineInstr *MI, int Op,
515 const char *Modifier) {
516 const MachineOperand &MO1 = MI->getOperand(Op);
517 const MachineOperand &MO2 = MI->getOperand(Op+1);
518
519 if (!MO1.isRegister()) { // FIXME: This is for CP entries, but isn't right.
520 printOperand(MI, Op);
521 return;
522 }
523
524 assert(MRegisterInfo::isPhysicalRegister(MO1.getReg()));
525
526 if (Modifier && strcmp(Modifier, "submode") == 0) {
527 ARM_AM::AMSubMode Mode = ARM_AM::getAM5SubMode(MO2.getImm());
528 if (MO1.getReg() == ARM::SP) {
529 bool isFLDM = (MI->getOpcode() == ARM::FLDMD ||
530 MI->getOpcode() == ARM::FLDMS);
531 O << ARM_AM::getAMSubModeAltStr(Mode, isFLDM);
532 } else
533 O << ARM_AM::getAMSubModeStr(Mode);
534 return;
535 } else if (Modifier && strcmp(Modifier, "base") == 0) {
536 // Used for FSTM{D|S} and LSTM{D|S} operations.
537 O << TM.getRegisterInfo()->get(MO1.getReg()).Name;
538 if (ARM_AM::getAM5WBFlag(MO2.getImm()))
539 O << "!";
540 return;
541 }
542
543 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
544
545 if (unsigned ImmOffs = ARM_AM::getAM5Offset(MO2.getImm())) {
546 O << ", #"
547 << (char)ARM_AM::getAM5Op(MO2.getImm())
548 << ImmOffs*4;
549 }
550 O << "]";
551}
552
553void ARMAsmPrinter::printAddrModePCOperand(const MachineInstr *MI, int Op,
554 const char *Modifier) {
555 if (Modifier && strcmp(Modifier, "label") == 0) {
556 printPCLabel(MI, Op+1);
557 return;
558 }
559
560 const MachineOperand &MO1 = MI->getOperand(Op);
561 assert(MRegisterInfo::isPhysicalRegister(MO1.getReg()));
562 O << "[pc, +" << TM.getRegisterInfo()->get(MO1.getReg()).Name << "]";
563}
564
565void
566ARMAsmPrinter::printThumbAddrModeRROperand(const MachineInstr *MI, int Op) {
567 const MachineOperand &MO1 = MI->getOperand(Op);
568 const MachineOperand &MO2 = MI->getOperand(Op+1);
569 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
570 O << ", " << TM.getRegisterInfo()->get(MO2.getReg()).Name << "]";
571}
572
573void
574ARMAsmPrinter::printThumbAddrModeRI5Operand(const MachineInstr *MI, int Op,
575 unsigned Scale) {
576 const MachineOperand &MO1 = MI->getOperand(Op);
Evan Chengcea117d2007-01-30 02:35:32 +0000577 const MachineOperand &MO2 = MI->getOperand(Op+1);
578 const MachineOperand &MO3 = MI->getOperand(Op+2);
Evan Chenga8e29892007-01-19 07:51:42 +0000579
580 if (!MO1.isRegister()) { // FIXME: This is for CP entries, but isn't right.
581 printOperand(MI, Op);
582 return;
583 }
584
585 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
Evan Chengcea117d2007-01-30 02:35:32 +0000586 if (MO3.getReg())
587 O << ", " << TM.getRegisterInfo()->get(MO3.getReg()).Name;
588 else if (unsigned ImmOffs = MO2.getImm()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000589 O << ", #" << ImmOffs;
590 if (Scale > 1)
591 O << " * " << Scale;
592 }
593 O << "]";
594}
595
596void
Evan Chengc38f2bc2007-01-23 22:59:13 +0000597ARMAsmPrinter::printThumbAddrModeS1Operand(const MachineInstr *MI, int Op) {
Evan Chengcea117d2007-01-30 02:35:32 +0000598 printThumbAddrModeRI5Operand(MI, Op, 1);
Evan Chenga8e29892007-01-19 07:51:42 +0000599}
600void
Evan Chengc38f2bc2007-01-23 22:59:13 +0000601ARMAsmPrinter::printThumbAddrModeS2Operand(const MachineInstr *MI, int Op) {
Evan Chengcea117d2007-01-30 02:35:32 +0000602 printThumbAddrModeRI5Operand(MI, Op, 2);
Evan Chenga8e29892007-01-19 07:51:42 +0000603}
604void
Evan Chengc38f2bc2007-01-23 22:59:13 +0000605ARMAsmPrinter::printThumbAddrModeS4Operand(const MachineInstr *MI, int Op) {
Evan Chengcea117d2007-01-30 02:35:32 +0000606 printThumbAddrModeRI5Operand(MI, Op, 4);
Evan Chenga8e29892007-01-19 07:51:42 +0000607}
608
609void ARMAsmPrinter::printThumbAddrModeSPOperand(const MachineInstr *MI,int Op) {
610 const MachineOperand &MO1 = MI->getOperand(Op);
611 const MachineOperand &MO2 = MI->getOperand(Op+1);
612 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
613 if (unsigned ImmOffs = MO2.getImm())
614 O << ", #" << ImmOffs << " * 4";
615 O << "]";
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000616}
617
Evan Cheng42d712b2007-05-08 21:08:43 +0000618void ARMAsmPrinter::printPredicateOperand(const MachineInstr *MI, int opNum) {
Evan Cheng44bec522007-05-15 01:29:07 +0000619 ARMCC::CondCodes CC = (ARMCC::CondCodes)MI->getOperand(opNum).getImmedValue();
620 if (CC != ARMCC::AL)
621 O << ARMCondCodeToString(CC);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000622}
623
Evan Chenga8e29892007-01-19 07:51:42 +0000624void ARMAsmPrinter::printPCLabel(const MachineInstr *MI, int opNum) {
625 int Id = (int)MI->getOperand(opNum).getImmedValue();
626 O << TAI->getPrivateGlobalPrefix() << "PC" << Id;
627}
628
629void ARMAsmPrinter::printRegisterList(const MachineInstr *MI, int opNum) {
630 O << "{";
631 for (unsigned i = opNum, e = MI->getNumOperands(); i != e; ++i) {
632 printOperand(MI, i);
633 if (i != e-1) O << ", ";
634 }
635 O << "}";
636}
637
638void ARMAsmPrinter::printCPInstOperand(const MachineInstr *MI, int OpNo,
639 const char *Modifier) {
640 assert(Modifier && "This operand only works with a modifier!");
641 // There are two aspects to a CONSTANTPOOL_ENTRY operand, the label and the
642 // data itself.
643 if (!strcmp(Modifier, "label")) {
644 unsigned ID = MI->getOperand(OpNo).getImm();
645 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
646 << '_' << ID << ":\n";
647 } else {
648 assert(!strcmp(Modifier, "cpentry") && "Unknown modifier for CPE");
649 unsigned CPI = MI->getOperand(OpNo).getConstantPoolIndex();
650
651 const MachineConstantPoolEntry &MCPE = // Chasing pointers is fun?
652 MI->getParent()->getParent()->getConstantPool()->getConstants()[CPI];
653
654 if (MCPE.isMachineConstantPoolEntry())
655 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
Lauro Ramos Venancio305b8a52007-04-25 14:50:40 +0000656 else {
Evan Chenga8e29892007-01-19 07:51:42 +0000657 EmitGlobalConstant(MCPE.Val.ConstVal);
Lauro Ramos Venancio305b8a52007-04-25 14:50:40 +0000658 // remember to emit the weak reference
659 if (const GlobalValue *GV = dyn_cast<GlobalValue>(MCPE.Val.ConstVal))
660 if (GV->hasExternalWeakLinkage())
661 ExtWeakSymbols.insert(GV);
662 }
Evan Chenga8e29892007-01-19 07:51:42 +0000663 }
664}
665
666void ARMAsmPrinter::printJTBlockOperand(const MachineInstr *MI, int OpNo) {
667 const MachineOperand &MO1 = MI->getOperand(OpNo);
668 const MachineOperand &MO2 = MI->getOperand(OpNo+1); // Unique Id
669 unsigned JTI = MO1.getJumpTableIndex();
670 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
671 << '_' << JTI << '_' << MO2.getImmedValue() << ":\n";
672
673 const char *JTEntryDirective = TAI->getJumpTableDirective();
674 if (!JTEntryDirective)
675 JTEntryDirective = TAI->getData32bitsDirective();
676
677 const MachineFunction *MF = MI->getParent()->getParent();
678 MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
679 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
680 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
681 bool UseSet= TAI->getSetDirective() && TM.getRelocationModel() == Reloc::PIC_;
682 std::set<MachineBasicBlock*> JTSets;
683 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
684 MachineBasicBlock *MBB = JTBBs[i];
685 if (UseSet && JTSets.insert(MBB).second)
686 printSetLabel(JTI, MO2.getImmedValue(), MBB);
687
688 O << JTEntryDirective << ' ';
689 if (UseSet)
690 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
691 << '_' << JTI << '_' << MO2.getImmedValue()
692 << "_set_" << MBB->getNumber();
693 else if (TM.getRelocationModel() == Reloc::PIC_) {
694 printBasicBlockLabel(MBB, false, false);
695 // If the arch uses custom Jump Table directives, don't calc relative to JT
696 if (!TAI->getJumpTableDirective())
697 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
698 << getFunctionNumber() << '_' << JTI << '_' << MO2.getImmedValue();
699 } else
700 printBasicBlockLabel(MBB, false, false);
Evan Chengd85ac4d2007-01-27 02:29:45 +0000701 if (i != e-1)
702 O << '\n';
Evan Chenga8e29892007-01-19 07:51:42 +0000703 }
704}
705
706
707bool ARMAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
708 unsigned AsmVariant, const char *ExtraCode){
709 // Does this asm operand have a single letter operand modifier?
710 if (ExtraCode && ExtraCode[0]) {
711 if (ExtraCode[1] != 0) return true; // Unknown modifier.
712
713 switch (ExtraCode[0]) {
714 default: return true; // Unknown modifier.
Evan Cheng23a95702007-03-08 22:42:46 +0000715 case 'c': // Don't print "$" before a global var name or constant.
Evan Chenge21e3962007-04-04 00:13:29 +0000716 case 'P': // Print a VFP double precision register.
Evan Cheng23a95702007-03-08 22:42:46 +0000717 printOperand(MI, OpNo);
718 return false;
Evan Chenga8e29892007-01-19 07:51:42 +0000719 case 'Q':
720 if (TM.getTargetData()->isLittleEndian())
721 break;
722 // Fallthrough
723 case 'R':
724 if (TM.getTargetData()->isBigEndian())
725 break;
726 // Fallthrough
727 case 'H': // Write second word of DI / DF reference.
728 // Verify that this operand has two consecutive registers.
729 if (!MI->getOperand(OpNo).isRegister() ||
730 OpNo+1 == MI->getNumOperands() ||
731 !MI->getOperand(OpNo+1).isRegister())
732 return true;
733 ++OpNo; // Return the high-part.
734 }
735 }
736
737 printOperand(MI, OpNo);
738 return false;
739}
740
741void ARMAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
742 ++EmittedInsts;
743
Evan Chengc60e76d2007-01-30 20:37:08 +0000744 int Opc = MI->getOpcode();
745 switch (Opc) {
746 case ARM::CONSTPOOL_ENTRY:
Evan Chenga8e29892007-01-19 07:51:42 +0000747 if (!InCPMode && AFI->isThumbFunction()) {
748 EmitAlignment(2);
749 InCPMode = true;
750 }
Evan Chengc60e76d2007-01-30 20:37:08 +0000751 break;
752 default: {
Evan Cheng3bf12d02007-01-31 23:39:39 +0000753 if (InCPMode && AFI->isThumbFunction())
Evan Chenga8e29892007-01-19 07:51:42 +0000754 InCPMode = false;
Evan Chengc60e76d2007-01-30 20:37:08 +0000755 switch (Opc) {
756 case ARM::PICADD:
757 case ARM::PICLD:
Evan Cheng341dccc2007-06-05 07:36:38 +0000758 case ARM::PICLDZH:
759 case ARM::PICLDZB:
760 case ARM::PICLDH:
761 case ARM::PICLDB:
762 case ARM::PICLDSH:
763 case ARM::PICLDSB:
764 case ARM::PICSTR:
765 case ARM::PICSTRH:
766 case ARM::PICSTRB:
Evan Chengc60e76d2007-01-30 20:37:08 +0000767 case ARM::tPICADD:
768 break;
769 default:
770 O << "\t";
771 break;
772 }
773 }}
Evan Chenga8e29892007-01-19 07:51:42 +0000774
775 // Call the autogenerated instruction printer routines.
776 printInstruction(MI);
777}
778
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000779bool ARMAsmPrinter::doInitialization(Module &M) {
Lauro Ramos Venancioe8e54952007-05-03 20:28:35 +0000780 // Emit initial debug information.
781 DW.BeginModule(&M);
Evan Chenga8e29892007-01-19 07:51:42 +0000782
Dale Johannesen54118352007-06-22 00:54:56 +0000783 AsmPrinter::doInitialization(M);
784
785 // Darwin wants symbols to be quoted if they have complex names.
786 if (Subtarget->isTargetDarwin())
787 Mang->setUseQuotes(true);
788
789 return false;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000790}
791
792bool ARMAsmPrinter::doFinalization(Module &M) {
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000793 const TargetData *TD = TM.getTargetData();
794
795 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
796 I != E; ++I) {
797 if (!I->hasInitializer()) // External global require no code
798 continue;
799
Evan Chengb267ca12007-01-30 08:04:53 +0000800 if (EmitSpecialLLVMGlobal(I)) {
801 if (Subtarget->isTargetDarwin() &&
802 TM.getRelocationModel() == Reloc::Static) {
803 if (I->getName() == "llvm.global_ctors")
804 O << ".reference .constructors_used\n";
805 else if (I->getName() == "llvm.global_dtors")
806 O << ".reference .destructors_used\n";
807 }
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000808 continue;
Evan Chengb267ca12007-01-30 08:04:53 +0000809 }
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000810
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000811 std::string name = Mang->getValueName(I);
812 Constant *C = I->getInitializer();
Evan Cheng98ded762007-03-08 01:25:25 +0000813 const Type *Type = C->getType();
814 unsigned Size = TD->getTypeSize(Type);
Evan Chenga8e29892007-01-19 07:51:42 +0000815 unsigned Align = TD->getPreferredAlignmentLog(I);
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000816
Lauro Ramos Venancio0a181732007-04-30 00:23:51 +0000817 const char *VisibilityDirective = NULL;
Evan Cheng5be54b02007-01-19 19:25:36 +0000818 if (I->hasHiddenVisibility())
Lauro Ramos Venancio0a181732007-04-30 00:23:51 +0000819 VisibilityDirective = TAI->getHiddenDirective();
820 else if (I->hasProtectedVisibility())
821 VisibilityDirective = TAI->getProtectedDirective();
822
823 if (VisibilityDirective)
824 O << VisibilityDirective << name << "\n";
825
Evan Cheng5be54b02007-01-19 19:25:36 +0000826 if (Subtarget->isTargetELF())
Lauro Ramos Venanciob1cc0522007-01-25 20:11:04 +0000827 O << "\t.type " << name << ",%object\n";
Evan Cheng5be54b02007-01-19 19:25:36 +0000828
829 if (C->isNullValue()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000830 if (I->hasExternalLinkage()) {
Evan Cheng5be54b02007-01-19 19:25:36 +0000831 if (const char *Directive = TAI->getZeroFillDirective()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000832 O << "\t.globl\t" << name << "\n";
Evan Cheng5be54b02007-01-19 19:25:36 +0000833 O << Directive << "__DATA__, __common, " << name << ", "
834 << Size << ", " << Align << "\n";
835 continue;
836 }
837 }
838
839 if (!I->hasSection() &&
840 (I->hasInternalLinkage() || I->hasWeakLinkage() ||
841 I->hasLinkOnceLinkage())) {
842 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
843 if (!NoZerosInBSS && TAI->getBSSSection())
844 SwitchToDataSection(TAI->getBSSSection(), I);
845 else
846 SwitchToDataSection(TAI->getDataSection(), I);
Evan Chenga8e29892007-01-19 07:51:42 +0000847 if (TAI->getLCOMMDirective() != NULL) {
848 if (I->hasInternalLinkage()) {
849 O << TAI->getLCOMMDirective() << name << "," << Size;
Evan Cheng5be54b02007-01-19 19:25:36 +0000850 if (Subtarget->isTargetDarwin())
Evan Chenga8e29892007-01-19 07:51:42 +0000851 O << "," << Align;
852 } else
853 O << TAI->getCOMMDirective() << name << "," << Size;
854 } else {
855 if (I->hasInternalLinkage())
856 O << "\t.local\t" << name << "\n";
857 O << TAI->getCOMMDirective() << name << "," << Size;
858 if (TAI->getCOMMDirectiveTakesAlignment())
859 O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
860 }
Evan Cheng5be54b02007-01-19 19:25:36 +0000861 O << "\t\t" << TAI->getCommentString() << " " << I->getName() << "\n";
862 continue;
Evan Chenga8e29892007-01-19 07:51:42 +0000863 }
Evan Cheng5be54b02007-01-19 19:25:36 +0000864 }
Evan Chenga8e29892007-01-19 07:51:42 +0000865
Evan Cheng5be54b02007-01-19 19:25:36 +0000866 switch (I->getLinkage()) {
867 case GlobalValue::LinkOnceLinkage:
868 case GlobalValue::WeakLinkage:
869 if (Subtarget->isTargetDarwin()) {
870 O << "\t.globl " << name << "\n"
871 << "\t.weak_definition " << name << "\n";
872 SwitchToDataSection("\t.section __DATA,__const_coal,coalesced", I);
873 } else {
874 std::string SectionName("\t.section\t.llvm.linkonce.d." +
875 name +
876 ",\"aw\",%progbits");
877 SwitchToDataSection(SectionName.c_str(), I);
878 O << "\t.weak " << name << "\n";
879 }
880 break;
881 case GlobalValue::AppendingLinkage:
882 // FIXME: appending linkage variables should go into a section of
883 // their name or something. For now, just emit them as external.
884 case GlobalValue::ExternalLinkage:
885 O << "\t.globl " << name << "\n";
886 // FALL THROUGH
887 case GlobalValue::InternalLinkage: {
888 if (I->isConstant()) {
889 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
890 if (TAI->getCStringSection() && CVA && CVA->isCString()) {
891 SwitchToDataSection(TAI->getCStringSection(), I);
892 break;
893 }
894 }
895 // FIXME: special handling for ".ctors" & ".dtors" sections
896 if (I->hasSection() &&
897 (I->getSection() == ".ctors" ||
898 I->getSection() == ".dtors")) {
899 assert(!Subtarget->isTargetDarwin());
900 std::string SectionName = ".section " + I->getSection();
901 SectionName += ",\"aw\",%progbits";
902 SwitchToDataSection(SectionName.c_str());
903 } else {
904 if (C->isNullValue() && !NoZerosInBSS && TAI->getBSSSection())
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000905 SwitchToDataSection(I->isThreadLocal() ? TAI->getTLSBSSSection() :
906 TAI->getBSSSection(), I);
Evan Cheng98ded762007-03-08 01:25:25 +0000907 else if (!I->isConstant())
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000908 SwitchToDataSection(I->isThreadLocal() ? TAI->getTLSDataSection() :
909 TAI->getDataSection(), I);
910 else if (I->isThreadLocal())
911 SwitchToDataSection(TAI->getTLSDataSection());
Evan Cheng98ded762007-03-08 01:25:25 +0000912 else {
913 // Read-only data.
Evan Cheng032953d2007-03-08 08:31:54 +0000914 bool HasReloc = C->ContainsRelocations();
915 if (HasReloc &&
916 Subtarget->isTargetDarwin() &&
Evan Cheng98ded762007-03-08 01:25:25 +0000917 TM.getRelocationModel() != Reloc::Static)
918 SwitchToDataSection("\t.const_data\n");
Evan Cheng032953d2007-03-08 08:31:54 +0000919 else if (!HasReloc && Size == 4 &&
Evan Cheng98ded762007-03-08 01:25:25 +0000920 TAI->getFourByteConstantSection())
921 SwitchToDataSection(TAI->getFourByteConstantSection(), I);
Evan Cheng032953d2007-03-08 08:31:54 +0000922 else if (!HasReloc && Size == 8 &&
Evan Cheng98ded762007-03-08 01:25:25 +0000923 TAI->getEightByteConstantSection())
924 SwitchToDataSection(TAI->getEightByteConstantSection(), I);
Evan Cheng032953d2007-03-08 08:31:54 +0000925 else if (!HasReloc && Size == 16 &&
Evan Cheng98ded762007-03-08 01:25:25 +0000926 TAI->getSixteenByteConstantSection())
927 SwitchToDataSection(TAI->getSixteenByteConstantSection(), I);
928 else if (TAI->getReadOnlySection())
929 SwitchToDataSection(TAI->getReadOnlySection(), I);
930 else
931 SwitchToDataSection(TAI->getDataSection(), I);
932 }
Rafael Espindolab97809c2006-10-19 13:30:40 +0000933 }
Evan Cheng5be54b02007-01-19 19:25:36 +0000934
935 break;
936 }
937 default:
938 assert(0 && "Unknown linkage type!");
939 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000940 }
Rafael Espindolab97809c2006-10-19 13:30:40 +0000941
Evan Chenga8e29892007-01-19 07:51:42 +0000942 EmitAlignment(Align, I);
Evan Cheng5be54b02007-01-19 19:25:36 +0000943 O << name << ":\t\t\t\t" << TAI->getCommentString() << " " << I->getName()
944 << "\n";
945 if (TAI->hasDotTypeDotSizeDirective())
Rafael Espindolab97809c2006-10-19 13:30:40 +0000946 O << "\t.size " << name << ", " << Size << "\n";
Evan Chenga8e29892007-01-19 07:51:42 +0000947 // If the initializer is a extern weak symbol, remember to emit the weak
948 // reference!
949 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
950 if (GV->hasExternalWeakLinkage())
951 ExtWeakSymbols.insert(GV);
952
953 EmitGlobalConstant(C);
954 O << '\n';
955 }
956
Evan Cheng5be54b02007-01-19 19:25:36 +0000957 if (Subtarget->isTargetDarwin()) {
958 SwitchToDataSection("");
959
Evan Chenga8e29892007-01-19 07:51:42 +0000960 // Output stubs for dynamically-linked functions
961 unsigned j = 1;
962 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
963 i != e; ++i, ++j) {
964 if (TM.getRelocationModel() == Reloc::PIC_)
965 SwitchToTextSection(".section __TEXT,__picsymbolstub4,symbol_stubs,"
966 "none,16", 0);
967 else
968 SwitchToTextSection(".section __TEXT,__symbol_stub4,symbol_stubs,"
969 "none,12", 0);
970
971 EmitAlignment(2);
972 O << "\t.code\t32\n";
973
974 O << "L" << *i << "$stub:\n";
975 O << "\t.indirect_symbol " << *i << "\n";
976 O << "\tldr ip, L" << *i << "$slp\n";
977 if (TM.getRelocationModel() == Reloc::PIC_) {
978 O << "L" << *i << "$scv:\n";
979 O << "\tadd ip, pc, ip\n";
980 }
981 O << "\tldr pc, [ip, #0]\n";
982 O << "L" << *i << "$slp:\n";
983 if (TM.getRelocationModel() == Reloc::PIC_)
984 O << "\t.long\tL" << *i << "$lazy_ptr-(L" << *i << "$scv+8)\n";
985 else
986 O << "\t.long\tL" << *i << "$lazy_ptr\n";
987 SwitchToDataSection(".lazy_symbol_pointer", 0);
988 O << "L" << *i << "$lazy_ptr:\n";
989 O << "\t.indirect_symbol " << *i << "\n";
990 O << "\t.long\tdyld_stub_binding_helper\n";
991 }
992 O << "\n";
993
994 // Output non-lazy-pointers for external and common global variables.
995 if (GVNonLazyPtrs.begin() != GVNonLazyPtrs.end())
996 SwitchToDataSection(".non_lazy_symbol_pointer", 0);
997 for (std::set<std::string>::iterator i = GVNonLazyPtrs.begin(),
998 e = GVNonLazyPtrs.end(); i != e; ++i) {
999 O << "L" << *i << "$non_lazy_ptr:\n";
1000 O << "\t.indirect_symbol " << *i << "\n";
1001 O << "\t.long\t0\n";
1002 }
1003
1004 // Emit initial debug information.
1005 DW.EndModule();
1006
1007 // Funny Darwin hack: This flag tells the linker that no global symbols
1008 // contain code that falls through to other global symbols (e.g. the obvious
1009 // implementation of multiple entry points). If this doesn't occur, the
1010 // linker can safely perform dead code stripping. Since LLVM never
1011 // generates code that does this, it is always safe to set.
1012 O << "\t.subsections_via_symbols\n";
Lauro Ramos Venancioe8e54952007-05-03 20:28:35 +00001013 } else {
1014 // Emit final debug information for ELF.
1015 DW.EndModule();
Rafael Espindolab01c4bb2006-07-27 11:38:51 +00001016 }
Rafael Espindolab97809c2006-10-19 13:30:40 +00001017
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00001018 AsmPrinter::doFinalization(M);
1019 return false; // success
1020}