blob: d8733789863d5f5d716908cd311a7a1811c1ce92 [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
Evan Cheng616cc662007-03-29 07:49:34 +0000213 if (F->hasHiddenVisibility())
214 if (const char *Directive = TAI->getHiddenDirective())
215 O << Directive << CurrentFnName << "\n";
216
Evan Chenga8e29892007-01-19 07:51:42 +0000217 if (AFI->isThumbFunction()) {
Dale Johannesenbd24f3f2007-04-23 20:07:25 +0000218 EmitAlignment(AFI->getAlign(), F);
Evan Chenga8e29892007-01-19 07:51:42 +0000219 O << "\t.code\t16\n";
Lauro Ramos Venancio6f46e592007-02-01 18:25:34 +0000220 O << "\t.thumb_func";
221 if (Subtarget->isTargetDarwin())
222 O << "\t" << CurrentFnName;
223 O << "\n";
Evan Chenga8e29892007-01-19 07:51:42 +0000224 InCPMode = false;
225 } else
226 EmitAlignment(2, F);
227
Rafael Espindola4b442b52006-05-23 02:48:20 +0000228 O << CurrentFnName << ":\n";
Evan Cheng5be54b02007-01-19 19:25:36 +0000229 if (Subtarget->isTargetDarwin()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000230 // Emit pre-function debug information.
231 DW.BeginFunction(&MF);
232 }
Rafael Espindola4b442b52006-05-23 02:48:20 +0000233
234 // Print out code for the function.
235 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
236 I != E; ++I) {
237 // Print a label for the basic block.
238 if (I != MF.begin()) {
239 printBasicBlockLabel(I, true);
240 O << '\n';
241 }
242 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
243 II != E; ++II) {
244 // Print the assembly for the instruction.
Evan Chenga8e29892007-01-19 07:51:42 +0000245 printMachineInstruction(II);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000246 }
247 }
248
Evan Chenga8e29892007-01-19 07:51:42 +0000249 if (TAI->hasDotTypeDotSizeDirective())
250 O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
251
Evan Cheng5be54b02007-01-19 19:25:36 +0000252 if (Subtarget->isTargetDarwin()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000253 // Emit post-function debug information.
254 DW.EndFunction();
255 }
256
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000257 return false;
258}
259
Evan Chenga8e29892007-01-19 07:51:42 +0000260void ARMAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
261 const char *Modifier) {
262 const MachineOperand &MO = MI->getOperand(opNum);
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000263 switch (MO.getType()) {
264 case MachineOperand::MO_Register:
265 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
Evan Chenga8e29892007-01-19 07:51:42 +0000266 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000267 else
268 assert(0 && "not implemented");
269 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000270 case MachineOperand::MO_Immediate: {
271 if (!Modifier || strcmp(Modifier, "no_hash") != 0)
272 O << "#";
273
274 O << (int)MO.getImmedValue();
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000275 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000276 }
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000277 case MachineOperand::MO_MachineBasicBlock:
Rafael Espindola687bc492006-08-24 13:45:55 +0000278 printBasicBlockLabel(MO.getMachineBasicBlock());
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000279 return;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000280 case MachineOperand::MO_GlobalAddress: {
Evan Chenga8e29892007-01-19 07:51:42 +0000281 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Rafael Espindola84b19be2006-07-16 01:02:57 +0000282 GlobalValue *GV = MO.getGlobal();
283 std::string Name = Mang->getValueName(GV);
Reid Spencer5cbf9852007-01-30 20:08:39 +0000284 bool isExt = (GV->isDeclaration() || GV->hasWeakLinkage() ||
Evan Chenga8e29892007-01-19 07:51:42 +0000285 GV->hasLinkOnceLinkage());
Evan Cheng5be54b02007-01-19 19:25:36 +0000286 if (isExt && isCallOp && Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +0000287 TM.getRelocationModel() != Reloc::Static) {
288 O << TAI->getPrivateGlobalPrefix() << Name << "$stub";
289 FnStubs.insert(Name);
290 } else
291 O << Name;
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000292 if (isCallOp && Subtarget->isTargetELF() &&
293 TM.getRelocationModel() == Reloc::PIC_)
294 O << "(PLT)";
Evan Chenga8e29892007-01-19 07:51:42 +0000295 if (GV->hasExternalWeakLinkage())
Rafael Espindola15404d02006-12-18 03:37:18 +0000296 ExtWeakSymbols.insert(GV);
Evan Chenga8e29892007-01-19 07:51:42 +0000297 break;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000298 }
Evan Chenga8e29892007-01-19 07:51:42 +0000299 case MachineOperand::MO_ExternalSymbol: {
300 bool isCallOp = Modifier && !strcmp(Modifier, "call");
301 std::string Name(TAI->getGlobalPrefix());
302 Name += MO.getSymbolName();
Evan Cheng5be54b02007-01-19 19:25:36 +0000303 if (isCallOp && Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +0000304 TM.getRelocationModel() != Reloc::Static) {
305 O << TAI->getPrivateGlobalPrefix() << Name << "$stub";
306 FnStubs.insert(Name);
307 } else
308 O << Name;
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000309 if (isCallOp && Subtarget->isTargetELF() &&
310 TM.getRelocationModel() == Reloc::PIC_)
311 O << "(PLT)";
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000312 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000313 }
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000314 case MachineOperand::MO_ConstantPoolIndex:
Jim Laskey563321a2006-09-06 18:34:40 +0000315 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000316 << '_' << MO.getConstantPoolIndex();
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000317 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000318 case MachineOperand::MO_JumpTableIndex:
319 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
320 << '_' << MO.getJumpTableIndex();
321 break;
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000322 default:
323 O << "<unknown operand type>"; abort (); break;
324 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000325}
326
Evan Chengc70d1842007-03-20 08:11:30 +0000327static void printSOImm(std::ostream &O, int64_t V, const TargetAsmInfo *TAI) {
328 assert(V < (1 << 12) && "Not a valid so_imm value!");
329 unsigned Imm = ARM_AM::getSOImmValImm(V);
330 unsigned Rot = ARM_AM::getSOImmValRot(V);
Evan Chenga8e29892007-01-19 07:51:42 +0000331
332 // Print low-level immediate formation info, per
333 // A5.1.3: "Data-processing operands - Immediate".
334 if (Rot) {
335 O << "#" << Imm << ", " << Rot;
336 // Pretty printed version.
337 O << ' ' << TAI->getCommentString() << ' ' << (int)ARM_AM::rotr32(Imm, Rot);
338 } else {
339 O << "#" << Imm;
340 }
341}
342
Evan Chengc70d1842007-03-20 08:11:30 +0000343/// printSOImmOperand - SOImm is 4-bit rotate amount in bits 8-11 with 8-bit
344/// immediate in bits 0-7.
345void ARMAsmPrinter::printSOImmOperand(const MachineInstr *MI, int OpNum) {
346 const MachineOperand &MO = MI->getOperand(OpNum);
347 assert(MO.isImmediate() && "Not a valid so_imm value!");
348 printSOImm(O, MO.getImmedValue(), TAI);
349}
350
351/// printSOImm2PartOperand - SOImm is broken into two pieces using a mov
352/// followed by a or to materialize.
353void ARMAsmPrinter::printSOImm2PartOperand(const MachineInstr *MI, int OpNum) {
354 const MachineOperand &MO = MI->getOperand(OpNum);
355 assert(MO.isImmediate() && "Not a valid so_imm value!");
356 unsigned V1 = ARM_AM::getSOImmTwoPartFirst(MO.getImmedValue());
357 unsigned V2 = ARM_AM::getSOImmTwoPartSecond(MO.getImmedValue());
358 printSOImm(O, ARM_AM::getSOImmVal(V1), TAI);
359 O << "\n\torr ";
360 printOperand(MI, 0);
361 O << ", ";
362 printOperand(MI, 0);
363 O << ", ";
364 printSOImm(O, ARM_AM::getSOImmVal(V2), TAI);
365}
366
Evan Chenga8e29892007-01-19 07:51:42 +0000367// so_reg is a 4-operand unit corresponding to register forms of the A5.1
368// "Addressing Mode 1 - Data-processing operands" forms. This includes:
369// REG 0 0 - e.g. R5
370// REG REG 0,SH_OPC - e.g. R5, ROR R3
371// REG 0 IMM,SH_OPC - e.g. R5, LSL #3
372void ARMAsmPrinter::printSORegOperand(const MachineInstr *MI, int Op) {
373 const MachineOperand &MO1 = MI->getOperand(Op);
374 const MachineOperand &MO2 = MI->getOperand(Op+1);
375 const MachineOperand &MO3 = MI->getOperand(Op+2);
376
377 assert(MRegisterInfo::isPhysicalRegister(MO1.getReg()));
378 O << TM.getRegisterInfo()->get(MO1.getReg()).Name;
379
380 // Print the shift opc.
381 O << ", "
382 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(MO3.getImmedValue()))
383 << " ";
384
385 if (MO2.getReg()) {
386 assert(MRegisterInfo::isPhysicalRegister(MO2.getReg()));
387 O << TM.getRegisterInfo()->get(MO2.getReg()).Name;
388 assert(ARM_AM::getSORegOffset(MO3.getImm()) == 0);
389 } else {
390 O << "#" << ARM_AM::getSORegOffset(MO3.getImm());
391 }
392}
393
394void ARMAsmPrinter::printAddrMode2Operand(const MachineInstr *MI, int Op) {
395 const MachineOperand &MO1 = MI->getOperand(Op);
396 const MachineOperand &MO2 = MI->getOperand(Op+1);
397 const MachineOperand &MO3 = MI->getOperand(Op+2);
398
399 if (!MO1.isRegister()) { // FIXME: This is for CP entries, but isn't right.
400 printOperand(MI, Op);
401 return;
402 }
403
404 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
405
406 if (!MO2.getReg()) {
407 if (ARM_AM::getAM2Offset(MO3.getImm())) // Don't print +0.
408 O << ", #"
409 << (char)ARM_AM::getAM2Op(MO3.getImm())
410 << ARM_AM::getAM2Offset(MO3.getImm());
411 O << "]";
412 return;
413 }
414
415 O << ", "
416 << (char)ARM_AM::getAM2Op(MO3.getImm())
417 << TM.getRegisterInfo()->get(MO2.getReg()).Name;
418
419 if (unsigned ShImm = ARM_AM::getAM2Offset(MO3.getImm()))
420 O << ", "
421 << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO3.getImmedValue()))
422 << " #" << ShImm;
423 O << "]";
424}
425
426void ARMAsmPrinter::printAddrMode2OffsetOperand(const MachineInstr *MI, int Op){
427 const MachineOperand &MO1 = MI->getOperand(Op);
428 const MachineOperand &MO2 = MI->getOperand(Op+1);
429
430 if (!MO1.getReg()) {
431 if (ARM_AM::getAM2Offset(MO2.getImm())) // Don't print +0.
432 O << "#"
433 << (char)ARM_AM::getAM2Op(MO2.getImm())
434 << ARM_AM::getAM2Offset(MO2.getImm());
435 return;
436 }
437
438 O << (char)ARM_AM::getAM2Op(MO2.getImm())
439 << TM.getRegisterInfo()->get(MO1.getReg()).Name;
440
441 if (unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm()))
442 O << ", "
443 << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO2.getImmedValue()))
444 << " #" << ShImm;
445}
446
447void ARMAsmPrinter::printAddrMode3Operand(const MachineInstr *MI, int Op) {
448 const MachineOperand &MO1 = MI->getOperand(Op);
449 const MachineOperand &MO2 = MI->getOperand(Op+1);
450 const MachineOperand &MO3 = MI->getOperand(Op+2);
451
452 assert(MRegisterInfo::isPhysicalRegister(MO1.getReg()));
453 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
454
455 if (MO2.getReg()) {
456 O << ", "
457 << (char)ARM_AM::getAM3Op(MO3.getImm())
458 << TM.getRegisterInfo()->get(MO2.getReg()).Name
459 << "]";
460 return;
461 }
462
463 if (unsigned ImmOffs = ARM_AM::getAM3Offset(MO3.getImm()))
464 O << ", #"
465 << (char)ARM_AM::getAM3Op(MO3.getImm())
466 << ImmOffs;
467 O << "]";
468}
469
470void ARMAsmPrinter::printAddrMode3OffsetOperand(const MachineInstr *MI, int Op){
471 const MachineOperand &MO1 = MI->getOperand(Op);
472 const MachineOperand &MO2 = MI->getOperand(Op+1);
473
474 if (MO1.getReg()) {
475 O << (char)ARM_AM::getAM3Op(MO2.getImm())
476 << TM.getRegisterInfo()->get(MO1.getReg()).Name;
477 return;
478 }
479
480 unsigned ImmOffs = ARM_AM::getAM3Offset(MO2.getImm());
481 O << "#"
482 << (char)ARM_AM::getAM3Op(MO2.getImm())
483 << ImmOffs;
484}
485
486void ARMAsmPrinter::printAddrMode4Operand(const MachineInstr *MI, int Op,
487 const char *Modifier) {
488 const MachineOperand &MO1 = MI->getOperand(Op);
489 const MachineOperand &MO2 = MI->getOperand(Op+1);
490 ARM_AM::AMSubMode Mode = ARM_AM::getAM4SubMode(MO2.getImm());
491 if (Modifier && strcmp(Modifier, "submode") == 0) {
492 if (MO1.getReg() == ARM::SP) {
493 bool isLDM = (MI->getOpcode() == ARM::LDM ||
494 MI->getOpcode() == ARM::LDM_RET);
495 O << ARM_AM::getAMSubModeAltStr(Mode, isLDM);
496 } else
497 O << ARM_AM::getAMSubModeStr(Mode);
498 } else {
499 printOperand(MI, Op);
500 if (ARM_AM::getAM4WBFlag(MO2.getImm()))
501 O << "!";
502 }
503}
504
505void ARMAsmPrinter::printAddrMode5Operand(const MachineInstr *MI, int Op,
506 const char *Modifier) {
507 const MachineOperand &MO1 = MI->getOperand(Op);
508 const MachineOperand &MO2 = MI->getOperand(Op+1);
509
510 if (!MO1.isRegister()) { // FIXME: This is for CP entries, but isn't right.
511 printOperand(MI, Op);
512 return;
513 }
514
515 assert(MRegisterInfo::isPhysicalRegister(MO1.getReg()));
516
517 if (Modifier && strcmp(Modifier, "submode") == 0) {
518 ARM_AM::AMSubMode Mode = ARM_AM::getAM5SubMode(MO2.getImm());
519 if (MO1.getReg() == ARM::SP) {
520 bool isFLDM = (MI->getOpcode() == ARM::FLDMD ||
521 MI->getOpcode() == ARM::FLDMS);
522 O << ARM_AM::getAMSubModeAltStr(Mode, isFLDM);
523 } else
524 O << ARM_AM::getAMSubModeStr(Mode);
525 return;
526 } else if (Modifier && strcmp(Modifier, "base") == 0) {
527 // Used for FSTM{D|S} and LSTM{D|S} operations.
528 O << TM.getRegisterInfo()->get(MO1.getReg()).Name;
529 if (ARM_AM::getAM5WBFlag(MO2.getImm()))
530 O << "!";
531 return;
532 }
533
534 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
535
536 if (unsigned ImmOffs = ARM_AM::getAM5Offset(MO2.getImm())) {
537 O << ", #"
538 << (char)ARM_AM::getAM5Op(MO2.getImm())
539 << ImmOffs*4;
540 }
541 O << "]";
542}
543
544void ARMAsmPrinter::printAddrModePCOperand(const MachineInstr *MI, int Op,
545 const char *Modifier) {
546 if (Modifier && strcmp(Modifier, "label") == 0) {
547 printPCLabel(MI, Op+1);
548 return;
549 }
550
551 const MachineOperand &MO1 = MI->getOperand(Op);
552 assert(MRegisterInfo::isPhysicalRegister(MO1.getReg()));
553 O << "[pc, +" << TM.getRegisterInfo()->get(MO1.getReg()).Name << "]";
554}
555
556void
557ARMAsmPrinter::printThumbAddrModeRROperand(const MachineInstr *MI, int Op) {
558 const MachineOperand &MO1 = MI->getOperand(Op);
559 const MachineOperand &MO2 = MI->getOperand(Op+1);
560 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
561 O << ", " << TM.getRegisterInfo()->get(MO2.getReg()).Name << "]";
562}
563
564void
565ARMAsmPrinter::printThumbAddrModeRI5Operand(const MachineInstr *MI, int Op,
566 unsigned Scale) {
567 const MachineOperand &MO1 = MI->getOperand(Op);
Evan Chengcea117d2007-01-30 02:35:32 +0000568 const MachineOperand &MO2 = MI->getOperand(Op+1);
569 const MachineOperand &MO3 = MI->getOperand(Op+2);
Evan Chenga8e29892007-01-19 07:51:42 +0000570
571 if (!MO1.isRegister()) { // FIXME: This is for CP entries, but isn't right.
572 printOperand(MI, Op);
573 return;
574 }
575
576 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
Evan Chengcea117d2007-01-30 02:35:32 +0000577 if (MO3.getReg())
578 O << ", " << TM.getRegisterInfo()->get(MO3.getReg()).Name;
579 else if (unsigned ImmOffs = MO2.getImm()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000580 O << ", #" << ImmOffs;
581 if (Scale > 1)
582 O << " * " << Scale;
583 }
584 O << "]";
585}
586
587void
Evan Chengc38f2bc2007-01-23 22:59:13 +0000588ARMAsmPrinter::printThumbAddrModeS1Operand(const MachineInstr *MI, int Op) {
Evan Chengcea117d2007-01-30 02:35:32 +0000589 printThumbAddrModeRI5Operand(MI, Op, 1);
Evan Chenga8e29892007-01-19 07:51:42 +0000590}
591void
Evan Chengc38f2bc2007-01-23 22:59:13 +0000592ARMAsmPrinter::printThumbAddrModeS2Operand(const MachineInstr *MI, int Op) {
Evan Chengcea117d2007-01-30 02:35:32 +0000593 printThumbAddrModeRI5Operand(MI, Op, 2);
Evan Chenga8e29892007-01-19 07:51:42 +0000594}
595void
Evan Chengc38f2bc2007-01-23 22:59:13 +0000596ARMAsmPrinter::printThumbAddrModeS4Operand(const MachineInstr *MI, int Op) {
Evan Chengcea117d2007-01-30 02:35:32 +0000597 printThumbAddrModeRI5Operand(MI, Op, 4);
Evan Chenga8e29892007-01-19 07:51:42 +0000598}
599
600void ARMAsmPrinter::printThumbAddrModeSPOperand(const MachineInstr *MI,int Op) {
601 const MachineOperand &MO1 = MI->getOperand(Op);
602 const MachineOperand &MO2 = MI->getOperand(Op+1);
603 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
604 if (unsigned ImmOffs = MO2.getImm())
605 O << ", #" << ImmOffs << " * 4";
606 O << "]";
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000607}
608
609void ARMAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
Rafael Espindola6f602de2006-08-24 16:13:15 +0000610 int CC = (int)MI->getOperand(opNum).getImmedValue();
611 O << ARMCondCodeToString((ARMCC::CondCodes)CC);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000612}
613
Evan Chenga8e29892007-01-19 07:51:42 +0000614void ARMAsmPrinter::printPCLabel(const MachineInstr *MI, int opNum) {
615 int Id = (int)MI->getOperand(opNum).getImmedValue();
616 O << TAI->getPrivateGlobalPrefix() << "PC" << Id;
617}
618
619void ARMAsmPrinter::printRegisterList(const MachineInstr *MI, int opNum) {
620 O << "{";
621 for (unsigned i = opNum, e = MI->getNumOperands(); i != e; ++i) {
622 printOperand(MI, i);
623 if (i != e-1) O << ", ";
624 }
625 O << "}";
626}
627
628void ARMAsmPrinter::printCPInstOperand(const MachineInstr *MI, int OpNo,
629 const char *Modifier) {
630 assert(Modifier && "This operand only works with a modifier!");
631 // There are two aspects to a CONSTANTPOOL_ENTRY operand, the label and the
632 // data itself.
633 if (!strcmp(Modifier, "label")) {
634 unsigned ID = MI->getOperand(OpNo).getImm();
635 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
636 << '_' << ID << ":\n";
637 } else {
638 assert(!strcmp(Modifier, "cpentry") && "Unknown modifier for CPE");
639 unsigned CPI = MI->getOperand(OpNo).getConstantPoolIndex();
640
641 const MachineConstantPoolEntry &MCPE = // Chasing pointers is fun?
642 MI->getParent()->getParent()->getConstantPool()->getConstants()[CPI];
643
644 if (MCPE.isMachineConstantPoolEntry())
645 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
Lauro Ramos Venancio305b8a52007-04-25 14:50:40 +0000646 else {
Evan Chenga8e29892007-01-19 07:51:42 +0000647 EmitGlobalConstant(MCPE.Val.ConstVal);
Lauro Ramos Venancio305b8a52007-04-25 14:50:40 +0000648 // remember to emit the weak reference
649 if (const GlobalValue *GV = dyn_cast<GlobalValue>(MCPE.Val.ConstVal))
650 if (GV->hasExternalWeakLinkage())
651 ExtWeakSymbols.insert(GV);
652 }
Evan Chenga8e29892007-01-19 07:51:42 +0000653 }
654}
655
656void ARMAsmPrinter::printJTBlockOperand(const MachineInstr *MI, int OpNo) {
657 const MachineOperand &MO1 = MI->getOperand(OpNo);
658 const MachineOperand &MO2 = MI->getOperand(OpNo+1); // Unique Id
659 unsigned JTI = MO1.getJumpTableIndex();
660 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
661 << '_' << JTI << '_' << MO2.getImmedValue() << ":\n";
662
663 const char *JTEntryDirective = TAI->getJumpTableDirective();
664 if (!JTEntryDirective)
665 JTEntryDirective = TAI->getData32bitsDirective();
666
667 const MachineFunction *MF = MI->getParent()->getParent();
668 MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
669 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
670 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
671 bool UseSet= TAI->getSetDirective() && TM.getRelocationModel() == Reloc::PIC_;
672 std::set<MachineBasicBlock*> JTSets;
673 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
674 MachineBasicBlock *MBB = JTBBs[i];
675 if (UseSet && JTSets.insert(MBB).second)
676 printSetLabel(JTI, MO2.getImmedValue(), MBB);
677
678 O << JTEntryDirective << ' ';
679 if (UseSet)
680 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
681 << '_' << JTI << '_' << MO2.getImmedValue()
682 << "_set_" << MBB->getNumber();
683 else if (TM.getRelocationModel() == Reloc::PIC_) {
684 printBasicBlockLabel(MBB, false, false);
685 // If the arch uses custom Jump Table directives, don't calc relative to JT
686 if (!TAI->getJumpTableDirective())
687 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
688 << getFunctionNumber() << '_' << JTI << '_' << MO2.getImmedValue();
689 } else
690 printBasicBlockLabel(MBB, false, false);
Evan Chengd85ac4d2007-01-27 02:29:45 +0000691 if (i != e-1)
692 O << '\n';
Evan Chenga8e29892007-01-19 07:51:42 +0000693 }
694}
695
696
697bool ARMAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
698 unsigned AsmVariant, const char *ExtraCode){
699 // Does this asm operand have a single letter operand modifier?
700 if (ExtraCode && ExtraCode[0]) {
701 if (ExtraCode[1] != 0) return true; // Unknown modifier.
702
703 switch (ExtraCode[0]) {
704 default: return true; // Unknown modifier.
Evan Cheng23a95702007-03-08 22:42:46 +0000705 case 'c': // Don't print "$" before a global var name or constant.
Evan Chenge21e3962007-04-04 00:13:29 +0000706 case 'P': // Print a VFP double precision register.
Evan Cheng23a95702007-03-08 22:42:46 +0000707 printOperand(MI, OpNo);
708 return false;
Evan Chenga8e29892007-01-19 07:51:42 +0000709 case 'Q':
710 if (TM.getTargetData()->isLittleEndian())
711 break;
712 // Fallthrough
713 case 'R':
714 if (TM.getTargetData()->isBigEndian())
715 break;
716 // Fallthrough
717 case 'H': // Write second word of DI / DF reference.
718 // Verify that this operand has two consecutive registers.
719 if (!MI->getOperand(OpNo).isRegister() ||
720 OpNo+1 == MI->getNumOperands() ||
721 !MI->getOperand(OpNo+1).isRegister())
722 return true;
723 ++OpNo; // Return the high-part.
724 }
725 }
726
727 printOperand(MI, OpNo);
728 return false;
729}
730
731void ARMAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
732 ++EmittedInsts;
733
Evan Chengc60e76d2007-01-30 20:37:08 +0000734 int Opc = MI->getOpcode();
735 switch (Opc) {
736 case ARM::CONSTPOOL_ENTRY:
Evan Chenga8e29892007-01-19 07:51:42 +0000737 if (!InCPMode && AFI->isThumbFunction()) {
738 EmitAlignment(2);
739 InCPMode = true;
740 }
Evan Chengc60e76d2007-01-30 20:37:08 +0000741 break;
742 default: {
Evan Cheng3bf12d02007-01-31 23:39:39 +0000743 if (InCPMode && AFI->isThumbFunction())
Evan Chenga8e29892007-01-19 07:51:42 +0000744 InCPMode = false;
Evan Chengc60e76d2007-01-30 20:37:08 +0000745 switch (Opc) {
746 case ARM::PICADD:
747 case ARM::PICLD:
748 case ARM::tPICADD:
749 break;
750 default:
751 O << "\t";
752 break;
753 }
754 }}
Evan Chenga8e29892007-01-19 07:51:42 +0000755
756 // Call the autogenerated instruction printer routines.
757 printInstruction(MI);
758}
759
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000760bool ARMAsmPrinter::doInitialization(Module &M) {
Evan Cheng5be54b02007-01-19 19:25:36 +0000761 if (Subtarget->isTargetDarwin()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000762 // Emit initial debug information.
763 DW.BeginModule(&M);
764 }
765
766 return AsmPrinter::doInitialization(M);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000767}
768
769bool ARMAsmPrinter::doFinalization(Module &M) {
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000770 const TargetData *TD = TM.getTargetData();
771
772 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
773 I != E; ++I) {
774 if (!I->hasInitializer()) // External global require no code
775 continue;
776
Evan Chengb267ca12007-01-30 08:04:53 +0000777 if (EmitSpecialLLVMGlobal(I)) {
778 if (Subtarget->isTargetDarwin() &&
779 TM.getRelocationModel() == Reloc::Static) {
780 if (I->getName() == "llvm.global_ctors")
781 O << ".reference .constructors_used\n";
782 else if (I->getName() == "llvm.global_dtors")
783 O << ".reference .destructors_used\n";
784 }
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000785 continue;
Evan Chengb267ca12007-01-30 08:04:53 +0000786 }
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000787
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000788 std::string name = Mang->getValueName(I);
789 Constant *C = I->getInitializer();
Evan Cheng98ded762007-03-08 01:25:25 +0000790 const Type *Type = C->getType();
791 unsigned Size = TD->getTypeSize(Type);
Evan Chenga8e29892007-01-19 07:51:42 +0000792 unsigned Align = TD->getPreferredAlignmentLog(I);
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000793
Evan Cheng5be54b02007-01-19 19:25:36 +0000794 if (I->hasHiddenVisibility())
795 if (const char *Directive = TAI->getHiddenDirective())
796 O << Directive << name << "\n";
797 if (Subtarget->isTargetELF())
Lauro Ramos Venanciob1cc0522007-01-25 20:11:04 +0000798 O << "\t.type " << name << ",%object\n";
Evan Cheng5be54b02007-01-19 19:25:36 +0000799
800 if (C->isNullValue()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000801 if (I->hasExternalLinkage()) {
Evan Cheng5be54b02007-01-19 19:25:36 +0000802 if (const char *Directive = TAI->getZeroFillDirective()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000803 O << "\t.globl\t" << name << "\n";
Evan Cheng5be54b02007-01-19 19:25:36 +0000804 O << Directive << "__DATA__, __common, " << name << ", "
805 << Size << ", " << Align << "\n";
806 continue;
807 }
808 }
809
810 if (!I->hasSection() &&
811 (I->hasInternalLinkage() || I->hasWeakLinkage() ||
812 I->hasLinkOnceLinkage())) {
813 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
814 if (!NoZerosInBSS && TAI->getBSSSection())
815 SwitchToDataSection(TAI->getBSSSection(), I);
816 else
817 SwitchToDataSection(TAI->getDataSection(), I);
Evan Chenga8e29892007-01-19 07:51:42 +0000818 if (TAI->getLCOMMDirective() != NULL) {
819 if (I->hasInternalLinkage()) {
820 O << TAI->getLCOMMDirective() << name << "," << Size;
Evan Cheng5be54b02007-01-19 19:25:36 +0000821 if (Subtarget->isTargetDarwin())
Evan Chenga8e29892007-01-19 07:51:42 +0000822 O << "," << Align;
823 } else
824 O << TAI->getCOMMDirective() << name << "," << Size;
825 } else {
826 if (I->hasInternalLinkage())
827 O << "\t.local\t" << name << "\n";
828 O << TAI->getCOMMDirective() << name << "," << Size;
829 if (TAI->getCOMMDirectiveTakesAlignment())
830 O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
831 }
Evan Cheng5be54b02007-01-19 19:25:36 +0000832 O << "\t\t" << TAI->getCommentString() << " " << I->getName() << "\n";
833 continue;
Evan Chenga8e29892007-01-19 07:51:42 +0000834 }
Evan Cheng5be54b02007-01-19 19:25:36 +0000835 }
Evan Chenga8e29892007-01-19 07:51:42 +0000836
Evan Cheng5be54b02007-01-19 19:25:36 +0000837 switch (I->getLinkage()) {
838 case GlobalValue::LinkOnceLinkage:
839 case GlobalValue::WeakLinkage:
840 if (Subtarget->isTargetDarwin()) {
841 O << "\t.globl " << name << "\n"
842 << "\t.weak_definition " << name << "\n";
843 SwitchToDataSection("\t.section __DATA,__const_coal,coalesced", I);
844 } else {
845 std::string SectionName("\t.section\t.llvm.linkonce.d." +
846 name +
847 ",\"aw\",%progbits");
848 SwitchToDataSection(SectionName.c_str(), I);
849 O << "\t.weak " << name << "\n";
850 }
851 break;
852 case GlobalValue::AppendingLinkage:
853 // FIXME: appending linkage variables should go into a section of
854 // their name or something. For now, just emit them as external.
855 case GlobalValue::ExternalLinkage:
856 O << "\t.globl " << name << "\n";
857 // FALL THROUGH
858 case GlobalValue::InternalLinkage: {
859 if (I->isConstant()) {
860 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
861 if (TAI->getCStringSection() && CVA && CVA->isCString()) {
862 SwitchToDataSection(TAI->getCStringSection(), I);
863 break;
864 }
865 }
866 // FIXME: special handling for ".ctors" & ".dtors" sections
867 if (I->hasSection() &&
868 (I->getSection() == ".ctors" ||
869 I->getSection() == ".dtors")) {
870 assert(!Subtarget->isTargetDarwin());
871 std::string SectionName = ".section " + I->getSection();
872 SectionName += ",\"aw\",%progbits";
873 SwitchToDataSection(SectionName.c_str());
874 } else {
875 if (C->isNullValue() && !NoZerosInBSS && TAI->getBSSSection())
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000876 SwitchToDataSection(I->isThreadLocal() ? TAI->getTLSBSSSection() :
877 TAI->getBSSSection(), I);
Evan Cheng98ded762007-03-08 01:25:25 +0000878 else if (!I->isConstant())
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000879 SwitchToDataSection(I->isThreadLocal() ? TAI->getTLSDataSection() :
880 TAI->getDataSection(), I);
881 else if (I->isThreadLocal())
882 SwitchToDataSection(TAI->getTLSDataSection());
Evan Cheng98ded762007-03-08 01:25:25 +0000883 else {
884 // Read-only data.
Evan Cheng032953d2007-03-08 08:31:54 +0000885 bool HasReloc = C->ContainsRelocations();
886 if (HasReloc &&
887 Subtarget->isTargetDarwin() &&
Evan Cheng98ded762007-03-08 01:25:25 +0000888 TM.getRelocationModel() != Reloc::Static)
889 SwitchToDataSection("\t.const_data\n");
Evan Cheng032953d2007-03-08 08:31:54 +0000890 else if (!HasReloc && Size == 4 &&
Evan Cheng98ded762007-03-08 01:25:25 +0000891 TAI->getFourByteConstantSection())
892 SwitchToDataSection(TAI->getFourByteConstantSection(), I);
Evan Cheng032953d2007-03-08 08:31:54 +0000893 else if (!HasReloc && Size == 8 &&
Evan Cheng98ded762007-03-08 01:25:25 +0000894 TAI->getEightByteConstantSection())
895 SwitchToDataSection(TAI->getEightByteConstantSection(), I);
Evan Cheng032953d2007-03-08 08:31:54 +0000896 else if (!HasReloc && Size == 16 &&
Evan Cheng98ded762007-03-08 01:25:25 +0000897 TAI->getSixteenByteConstantSection())
898 SwitchToDataSection(TAI->getSixteenByteConstantSection(), I);
899 else if (TAI->getReadOnlySection())
900 SwitchToDataSection(TAI->getReadOnlySection(), I);
901 else
902 SwitchToDataSection(TAI->getDataSection(), I);
903 }
Rafael Espindolab97809c2006-10-19 13:30:40 +0000904 }
Evan Cheng5be54b02007-01-19 19:25:36 +0000905
906 break;
907 }
908 default:
909 assert(0 && "Unknown linkage type!");
910 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000911 }
Rafael Espindolab97809c2006-10-19 13:30:40 +0000912
Evan Chenga8e29892007-01-19 07:51:42 +0000913 EmitAlignment(Align, I);
Evan Cheng5be54b02007-01-19 19:25:36 +0000914 O << name << ":\t\t\t\t" << TAI->getCommentString() << " " << I->getName()
915 << "\n";
916 if (TAI->hasDotTypeDotSizeDirective())
Rafael Espindolab97809c2006-10-19 13:30:40 +0000917 O << "\t.size " << name << ", " << Size << "\n";
Evan Chenga8e29892007-01-19 07:51:42 +0000918 // If the initializer is a extern weak symbol, remember to emit the weak
919 // reference!
920 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
921 if (GV->hasExternalWeakLinkage())
922 ExtWeakSymbols.insert(GV);
923
924 EmitGlobalConstant(C);
925 O << '\n';
926 }
927
Evan Cheng5be54b02007-01-19 19:25:36 +0000928 if (Subtarget->isTargetDarwin()) {
929 SwitchToDataSection("");
930
Evan Chenga8e29892007-01-19 07:51:42 +0000931 // Output stubs for dynamically-linked functions
932 unsigned j = 1;
933 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
934 i != e; ++i, ++j) {
935 if (TM.getRelocationModel() == Reloc::PIC_)
936 SwitchToTextSection(".section __TEXT,__picsymbolstub4,symbol_stubs,"
937 "none,16", 0);
938 else
939 SwitchToTextSection(".section __TEXT,__symbol_stub4,symbol_stubs,"
940 "none,12", 0);
941
942 EmitAlignment(2);
943 O << "\t.code\t32\n";
944
945 O << "L" << *i << "$stub:\n";
946 O << "\t.indirect_symbol " << *i << "\n";
947 O << "\tldr ip, L" << *i << "$slp\n";
948 if (TM.getRelocationModel() == Reloc::PIC_) {
949 O << "L" << *i << "$scv:\n";
950 O << "\tadd ip, pc, ip\n";
951 }
952 O << "\tldr pc, [ip, #0]\n";
953 O << "L" << *i << "$slp:\n";
954 if (TM.getRelocationModel() == Reloc::PIC_)
955 O << "\t.long\tL" << *i << "$lazy_ptr-(L" << *i << "$scv+8)\n";
956 else
957 O << "\t.long\tL" << *i << "$lazy_ptr\n";
958 SwitchToDataSection(".lazy_symbol_pointer", 0);
959 O << "L" << *i << "$lazy_ptr:\n";
960 O << "\t.indirect_symbol " << *i << "\n";
961 O << "\t.long\tdyld_stub_binding_helper\n";
962 }
963 O << "\n";
964
965 // Output non-lazy-pointers for external and common global variables.
966 if (GVNonLazyPtrs.begin() != GVNonLazyPtrs.end())
967 SwitchToDataSection(".non_lazy_symbol_pointer", 0);
968 for (std::set<std::string>::iterator i = GVNonLazyPtrs.begin(),
969 e = GVNonLazyPtrs.end(); i != e; ++i) {
970 O << "L" << *i << "$non_lazy_ptr:\n";
971 O << "\t.indirect_symbol " << *i << "\n";
972 O << "\t.long\t0\n";
973 }
974
975 // Emit initial debug information.
976 DW.EndModule();
977
978 // Funny Darwin hack: This flag tells the linker that no global symbols
979 // contain code that falls through to other global symbols (e.g. the obvious
980 // implementation of multiple entry points). If this doesn't occur, the
981 // linker can safely perform dead code stripping. Since LLVM never
982 // generates code that does this, it is always safe to set.
983 O << "\t.subsections_via_symbols\n";
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000984 }
Rafael Espindolab97809c2006-10-19 13:30:40 +0000985
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000986 AsmPrinter::doFinalization(M);
987 return false; // success
988}