blob: 056c540cb97f5395fdfe4af2d2fdf500612c66b7 [file] [log] [blame]
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00001//===-- ARMAsmPrinter.cpp - ARM LLVM assembly writer ----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00006// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains a printer that converts from our internal representation
11// of machine-dependent LLVM code to GAS-format ARM assembly language.
12//
13//===----------------------------------------------------------------------===//
14
Chris Lattner95b2c7d2006-12-19 22:59:26 +000015#define DEBUG_TYPE "asm-printer"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000016#include "ARM.h"
Evan Chenga8e29892007-01-19 07:51:42 +000017#include "ARMTargetMachine.h"
18#include "ARMAddressingModes.h"
19#include "ARMConstantPoolValue.h"
20#include "ARMMachineFunctionInfo.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000021#include "llvm/Constants.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000022#include "llvm/Module.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000023#include "llvm/CodeGen/AsmPrinter.h"
Evan Chenga8e29892007-01-19 07:51:42 +000024#include "llvm/CodeGen/DwarfWriter.h"
Jim Laskey44c3b9f2007-01-26 21:22:28 +000025#include "llvm/CodeGen/MachineModuleInfo.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000026#include "llvm/CodeGen/MachineFunctionPass.h"
Evan Chenga8e29892007-01-19 07:51:42 +000027#include "llvm/CodeGen/MachineJumpTableInfo.h"
Jim Laskey563321a2006-09-06 18:34:40 +000028#include "llvm/Target/TargetAsmInfo.h"
Rafael Espindolab01c4bb2006-07-27 11:38:51 +000029#include "llvm/Target/TargetData.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000030#include "llvm/Target/TargetMachine.h"
Evan Cheng5be54b02007-01-19 19:25:36 +000031#include "llvm/Target/TargetOptions.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000032#include "llvm/ADT/Statistic.h"
33#include "llvm/ADT/StringExtras.h"
Evan Chenga8e29892007-01-19 07:51:42 +000034#include "llvm/Support/Compiler.h"
35#include "llvm/Support/Mangler.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000036#include "llvm/Support/MathExtras.h"
37#include <cctype>
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000038using namespace llvm;
39
Chris Lattner95b2c7d2006-12-19 22:59:26 +000040STATISTIC(EmittedInsts, "Number of machine instrs printed");
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000041
Chris Lattner95b2c7d2006-12-19 22:59:26 +000042namespace {
Jim Laskey563321a2006-09-06 18:34:40 +000043 struct VISIBILITY_HIDDEN ARMAsmPrinter : public AsmPrinter {
Jim Laskeya0f3d172006-09-07 22:06:40 +000044 ARMAsmPrinter(std::ostream &O, TargetMachine &TM, const TargetAsmInfo *T)
Evan Chenga8e29892007-01-19 07:51:42 +000045 : AsmPrinter(O, TM, T), DW(O, this, T), AFI(NULL), InCPMode(false) {
46 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Jim Laskey563321a2006-09-06 18:34:40 +000047 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000048
Evan Chenga8e29892007-01-19 07:51:42 +000049 DwarfWriter DW;
50
51 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
52 /// make the right decision when printing asm code for different targets.
53 const ARMSubtarget *Subtarget;
54
55 /// AFI - Keep a pointer to ARMFunctionInfo for the current
56 /// MachineFunction
57 ARMFunctionInfo *AFI;
58
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000059 /// We name each basic block in a Function with a unique number, so
60 /// that we can consistently refer to them later. This is cleared
61 /// at the beginning of each call to runOnMachineFunction().
62 ///
63 typedef std::map<const Value *, unsigned> ValueMapTy;
64 ValueMapTy NumberForBB;
65
Evan Chenga8e29892007-01-19 07:51:42 +000066 /// Keeps the set of GlobalValues that require non-lazy-pointers for
67 /// indirect access.
68 std::set<std::string> GVNonLazyPtrs;
69
70 /// Keeps the set of external function GlobalAddresses that the asm
71 /// printer should generate stubs for.
72 std::set<std::string> FnStubs;
73
74 /// True if asm printer is printing a series of CONSTPOOL_ENTRY.
75 bool InCPMode;
76
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000077 virtual const char *getPassName() const {
78 return "ARM Assembly Printer";
79 }
80
Evan Chenga8e29892007-01-19 07:51:42 +000081 void printOperand(const MachineInstr *MI, int opNum,
82 const char *Modifier = 0);
83 void printSOImmOperand(const MachineInstr *MI, int opNum);
Evan Chengc70d1842007-03-20 08:11:30 +000084 void printSOImm2PartOperand(const MachineInstr *MI, int opNum);
Evan Chenga8e29892007-01-19 07:51:42 +000085 void printSORegOperand(const MachineInstr *MI, int opNum);
86 void printAddrMode2Operand(const MachineInstr *MI, int OpNo);
87 void printAddrMode2OffsetOperand(const MachineInstr *MI, int OpNo);
88 void printAddrMode3Operand(const MachineInstr *MI, int OpNo);
89 void printAddrMode3OffsetOperand(const MachineInstr *MI, int OpNo);
90 void printAddrMode4Operand(const MachineInstr *MI, int OpNo,
91 const char *Modifier = 0);
92 void printAddrMode5Operand(const MachineInstr *MI, int OpNo,
93 const char *Modifier = 0);
94 void printAddrModePCOperand(const MachineInstr *MI, int OpNo,
95 const char *Modifier = 0);
96 void printThumbAddrModeRROperand(const MachineInstr *MI, int OpNo);
97 void printThumbAddrModeRI5Operand(const MachineInstr *MI, int OpNo,
98 unsigned Scale);
Evan Chengc38f2bc2007-01-23 22:59:13 +000099 void printThumbAddrModeS1Operand(const MachineInstr *MI, int OpNo);
100 void printThumbAddrModeS2Operand(const MachineInstr *MI, int OpNo);
101 void printThumbAddrModeS4Operand(const MachineInstr *MI, int OpNo);
Evan Chenga8e29892007-01-19 07:51:42 +0000102 void printThumbAddrModeSPOperand(const MachineInstr *MI, int OpNo);
Evan Cheng42d712b2007-05-08 21:08:43 +0000103 void printPredicateOperand(const MachineInstr *MI, int opNum);
Evan Chengdfb2eba2007-07-06 01:01:34 +0000104 void printSBitModifierOperand(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 {
Gordon Henriksencd8bc052007-09-30 13:39:29 +0000154 AsmPrinter::getAnalysisUsage(AU);
Evan Chenga8e29892007-01-19 07:51:42 +0000155 AU.setPreservesAll();
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000156 AU.addRequired<MachineModuleInfo>();
Evan Chenga8e29892007-01-19 07:51:42 +0000157 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000158 };
159} // end of anonymous namespace
160
161#include "ARMGenAsmWriter.inc"
162
163/// createARMCodePrinterPass - Returns a pass that prints the ARM
164/// assembly code for a MachineFunction to the given output stream,
165/// using the given target machine description. This should work
166/// regardless of whether the function is in SSA form.
167///
168FunctionPass *llvm::createARMCodePrinterPass(std::ostream &o,
Evan Chenga8e29892007-01-19 07:51:42 +0000169 ARMTargetMachine &tm) {
Jim Laskeya0f3d172006-09-07 22:06:40 +0000170 return new ARMAsmPrinter(o, tm, tm.getTargetAsmInfo());
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000171}
172
Evan Chenga8e29892007-01-19 07:51:42 +0000173/// runOnMachineFunction - This uses the printInstruction()
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000174/// method to print assembly for each instruction.
175///
176bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Evan Chenga8e29892007-01-19 07:51:42 +0000177 AFI = MF.getInfo<ARMFunctionInfo>();
Rafael Espindola4b442b52006-05-23 02:48:20 +0000178
Lauro Ramos Venancioe8e54952007-05-03 20:28:35 +0000179 DW.SetModuleInfo(&getAnalysis<MachineModuleInfo>());
Chris Lattner1a199de2006-12-21 22:59:58 +0000180
Evan Chenga8e29892007-01-19 07:51:42 +0000181 SetupMachineFunction(MF);
182 O << "\n";
Rafael Espindola4b442b52006-05-23 02:48:20 +0000183
Evan Chenga8e29892007-01-19 07:51:42 +0000184 // NOTE: we don't print out constant pools here, they are handled as
185 // instructions.
186
187 O << "\n";
Rafael Espindola4b442b52006-05-23 02:48:20 +0000188 // Print out labels for the function.
189 const Function *F = MF.getFunction();
190 switch (F->getLinkage()) {
191 default: assert(0 && "Unknown linkage type!");
192 case Function::InternalLinkage:
Evan Chenga8e29892007-01-19 07:51:42 +0000193 SwitchToTextSection("\t.text", F);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000194 break;
195 case Function::ExternalLinkage:
Evan Chenga8e29892007-01-19 07:51:42 +0000196 SwitchToTextSection("\t.text", F);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000197 O << "\t.globl\t" << CurrentFnName << "\n";
198 break;
199 case Function::WeakLinkage:
200 case Function::LinkOnceLinkage:
Evan Cheng5be54b02007-01-19 19:25:36 +0000201 if (Subtarget->isTargetDarwin()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000202 SwitchToTextSection(
203 ".section __TEXT,__textcoal_nt,coalesced,pure_instructions", F);
204 O << "\t.globl\t" << CurrentFnName << "\n";
205 O << "\t.weak_definition\t" << CurrentFnName << "\n";
206 } else {
207 O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
208 }
Rafael Espindola4b442b52006-05-23 02:48:20 +0000209 break;
210 }
Evan Chenga8e29892007-01-19 07:51:42 +0000211
Lauro Ramos Venancio0a181732007-04-30 00:23:51 +0000212 const char *VisibilityDirective = NULL;
Evan Cheng616cc662007-03-29 07:49:34 +0000213 if (F->hasHiddenVisibility())
Lauro Ramos Venancio0a181732007-04-30 00:23:51 +0000214 VisibilityDirective = TAI->getHiddenDirective();
215 else if (F->hasProtectedVisibility())
216 VisibilityDirective = TAI->getProtectedDirective();
217
218 if (VisibilityDirective)
219 O << VisibilityDirective << CurrentFnName << "\n";
Evan Cheng616cc662007-03-29 07:49:34 +0000220
Evan Chenga8e29892007-01-19 07:51:42 +0000221 if (AFI->isThumbFunction()) {
Chris Lattner3a420532007-05-31 18:57:45 +0000222 EmitAlignment(1, F, AFI->getAlign());
Evan Chenga8e29892007-01-19 07:51:42 +0000223 O << "\t.code\t16\n";
Lauro Ramos Venancio6f46e592007-02-01 18:25:34 +0000224 O << "\t.thumb_func";
225 if (Subtarget->isTargetDarwin())
226 O << "\t" << CurrentFnName;
227 O << "\n";
Evan Chenga8e29892007-01-19 07:51:42 +0000228 InCPMode = false;
229 } else
230 EmitAlignment(2, F);
231
Rafael Espindola4b442b52006-05-23 02:48:20 +0000232 O << CurrentFnName << ":\n";
Lauro Ramos Venancioe8e54952007-05-03 20:28:35 +0000233 // Emit pre-function debug information.
234 DW.BeginFunction(&MF);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000235
236 // Print out code for the function.
237 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
238 I != E; ++I) {
239 // Print a label for the basic block.
240 if (I != MF.begin()) {
241 printBasicBlockLabel(I, true);
242 O << '\n';
243 }
244 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
245 II != E; ++II) {
246 // Print the assembly for the instruction.
Evan Chenga8e29892007-01-19 07:51:42 +0000247 printMachineInstruction(II);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000248 }
249 }
250
Evan Chenga8e29892007-01-19 07:51:42 +0000251 if (TAI->hasDotTypeDotSizeDirective())
252 O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
253
Lauro Ramos Venancioe8e54952007-05-03 20:28:35 +0000254 // Emit post-function debug information.
255 DW.EndFunction();
Evan Chenga8e29892007-01-19 07:51:42 +0000256
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;
Chris Lattner388488d2007-05-03 16:42:23 +0000292
293 if (MO.getOffset() > 0)
294 O << '+' << MO.getOffset();
295 else if (MO.getOffset() < 0)
296 O << MO.getOffset();
297
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000298 if (isCallOp && Subtarget->isTargetELF() &&
299 TM.getRelocationModel() == Reloc::PIC_)
300 O << "(PLT)";
Evan Chenga8e29892007-01-19 07:51:42 +0000301 if (GV->hasExternalWeakLinkage())
Rafael Espindola15404d02006-12-18 03:37:18 +0000302 ExtWeakSymbols.insert(GV);
Evan Chenga8e29892007-01-19 07:51:42 +0000303 break;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000304 }
Evan Chenga8e29892007-01-19 07:51:42 +0000305 case MachineOperand::MO_ExternalSymbol: {
306 bool isCallOp = Modifier && !strcmp(Modifier, "call");
307 std::string Name(TAI->getGlobalPrefix());
308 Name += MO.getSymbolName();
Evan Cheng5be54b02007-01-19 19:25:36 +0000309 if (isCallOp && Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +0000310 TM.getRelocationModel() != Reloc::Static) {
311 O << TAI->getPrivateGlobalPrefix() << Name << "$stub";
312 FnStubs.insert(Name);
313 } else
314 O << Name;
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000315 if (isCallOp && Subtarget->isTargetELF() &&
316 TM.getRelocationModel() == Reloc::PIC_)
317 O << "(PLT)";
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000318 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000319 }
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000320 case MachineOperand::MO_ConstantPoolIndex:
Evan Cheng347d39f2007-10-14 05:57:21 +0000321 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
322 << '_' << MO.getConstantPoolIndex();
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000323 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000324 case MachineOperand::MO_JumpTableIndex:
Evan Cheng347d39f2007-10-14 05:57:21 +0000325 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
326 << '_' << MO.getJumpTableIndex();
Evan Chenga8e29892007-01-19 07:51:42 +0000327 break;
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000328 default:
329 O << "<unknown operand type>"; abort (); break;
330 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000331}
332
Evan Chengc70d1842007-03-20 08:11:30 +0000333static void printSOImm(std::ostream &O, int64_t V, const TargetAsmInfo *TAI) {
334 assert(V < (1 << 12) && "Not a valid so_imm value!");
335 unsigned Imm = ARM_AM::getSOImmValImm(V);
336 unsigned Rot = ARM_AM::getSOImmValRot(V);
Evan Chenga8e29892007-01-19 07:51:42 +0000337
338 // Print low-level immediate formation info, per
339 // A5.1.3: "Data-processing operands - Immediate".
340 if (Rot) {
341 O << "#" << Imm << ", " << Rot;
342 // Pretty printed version.
343 O << ' ' << TAI->getCommentString() << ' ' << (int)ARM_AM::rotr32(Imm, Rot);
344 } else {
345 O << "#" << Imm;
346 }
347}
348
Evan Chengc70d1842007-03-20 08:11:30 +0000349/// printSOImmOperand - SOImm is 4-bit rotate amount in bits 8-11 with 8-bit
350/// immediate in bits 0-7.
351void ARMAsmPrinter::printSOImmOperand(const MachineInstr *MI, int OpNum) {
352 const MachineOperand &MO = MI->getOperand(OpNum);
353 assert(MO.isImmediate() && "Not a valid so_imm value!");
354 printSOImm(O, MO.getImmedValue(), TAI);
355}
356
357/// printSOImm2PartOperand - SOImm is broken into two pieces using a mov
358/// followed by a or to materialize.
359void ARMAsmPrinter::printSOImm2PartOperand(const MachineInstr *MI, int OpNum) {
360 const MachineOperand &MO = MI->getOperand(OpNum);
361 assert(MO.isImmediate() && "Not a valid so_imm value!");
362 unsigned V1 = ARM_AM::getSOImmTwoPartFirst(MO.getImmedValue());
363 unsigned V2 = ARM_AM::getSOImmTwoPartSecond(MO.getImmedValue());
364 printSOImm(O, ARM_AM::getSOImmVal(V1), TAI);
Evan Cheng5e148a32007-06-05 18:55:18 +0000365 O << "\n\torr";
366 printPredicateOperand(MI, 2);
367 O << " ";
Evan Chengc70d1842007-03-20 08:11:30 +0000368 printOperand(MI, 0);
369 O << ", ";
370 printOperand(MI, 0);
371 O << ", ";
372 printSOImm(O, ARM_AM::getSOImmVal(V2), TAI);
373}
374
Evan Chenga8e29892007-01-19 07:51:42 +0000375// so_reg is a 4-operand unit corresponding to register forms of the A5.1
376// "Addressing Mode 1 - Data-processing operands" forms. This includes:
377// REG 0 0 - e.g. R5
378// REG REG 0,SH_OPC - e.g. R5, ROR R3
379// REG 0 IMM,SH_OPC - e.g. R5, LSL #3
380void ARMAsmPrinter::printSORegOperand(const MachineInstr *MI, int Op) {
381 const MachineOperand &MO1 = MI->getOperand(Op);
382 const MachineOperand &MO2 = MI->getOperand(Op+1);
383 const MachineOperand &MO3 = MI->getOperand(Op+2);
384
385 assert(MRegisterInfo::isPhysicalRegister(MO1.getReg()));
386 O << TM.getRegisterInfo()->get(MO1.getReg()).Name;
387
388 // Print the shift opc.
389 O << ", "
390 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(MO3.getImmedValue()))
391 << " ";
392
393 if (MO2.getReg()) {
394 assert(MRegisterInfo::isPhysicalRegister(MO2.getReg()));
395 O << TM.getRegisterInfo()->get(MO2.getReg()).Name;
396 assert(ARM_AM::getSORegOffset(MO3.getImm()) == 0);
397 } else {
398 O << "#" << ARM_AM::getSORegOffset(MO3.getImm());
399 }
400}
401
402void ARMAsmPrinter::printAddrMode2Operand(const MachineInstr *MI, int Op) {
403 const MachineOperand &MO1 = MI->getOperand(Op);
404 const MachineOperand &MO2 = MI->getOperand(Op+1);
405 const MachineOperand &MO3 = MI->getOperand(Op+2);
406
407 if (!MO1.isRegister()) { // FIXME: This is for CP entries, but isn't right.
408 printOperand(MI, Op);
409 return;
410 }
411
412 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
413
414 if (!MO2.getReg()) {
415 if (ARM_AM::getAM2Offset(MO3.getImm())) // Don't print +0.
416 O << ", #"
417 << (char)ARM_AM::getAM2Op(MO3.getImm())
418 << ARM_AM::getAM2Offset(MO3.getImm());
419 O << "]";
420 return;
421 }
422
423 O << ", "
424 << (char)ARM_AM::getAM2Op(MO3.getImm())
425 << TM.getRegisterInfo()->get(MO2.getReg()).Name;
426
427 if (unsigned ShImm = ARM_AM::getAM2Offset(MO3.getImm()))
428 O << ", "
429 << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO3.getImmedValue()))
430 << " #" << ShImm;
431 O << "]";
432}
433
434void ARMAsmPrinter::printAddrMode2OffsetOperand(const MachineInstr *MI, int Op){
435 const MachineOperand &MO1 = MI->getOperand(Op);
436 const MachineOperand &MO2 = MI->getOperand(Op+1);
437
438 if (!MO1.getReg()) {
Evan Chengbdc98692007-05-03 23:30:36 +0000439 unsigned ImmOffs = ARM_AM::getAM2Offset(MO2.getImm());
440 assert(ImmOffs && "Malformed indexed load / store!");
441 O << "#"
442 << (char)ARM_AM::getAM2Op(MO2.getImm())
443 << ImmOffs;
Evan Chenga8e29892007-01-19 07:51:42 +0000444 return;
445 }
446
447 O << (char)ARM_AM::getAM2Op(MO2.getImm())
448 << TM.getRegisterInfo()->get(MO1.getReg()).Name;
449
450 if (unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm()))
451 O << ", "
452 << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO2.getImmedValue()))
453 << " #" << ShImm;
454}
455
456void ARMAsmPrinter::printAddrMode3Operand(const MachineInstr *MI, int Op) {
457 const MachineOperand &MO1 = MI->getOperand(Op);
458 const MachineOperand &MO2 = MI->getOperand(Op+1);
459 const MachineOperand &MO3 = MI->getOperand(Op+2);
460
461 assert(MRegisterInfo::isPhysicalRegister(MO1.getReg()));
462 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
463
464 if (MO2.getReg()) {
465 O << ", "
466 << (char)ARM_AM::getAM3Op(MO3.getImm())
467 << TM.getRegisterInfo()->get(MO2.getReg()).Name
468 << "]";
469 return;
470 }
471
472 if (unsigned ImmOffs = ARM_AM::getAM3Offset(MO3.getImm()))
473 O << ", #"
474 << (char)ARM_AM::getAM3Op(MO3.getImm())
475 << ImmOffs;
476 O << "]";
477}
478
479void ARMAsmPrinter::printAddrMode3OffsetOperand(const MachineInstr *MI, int Op){
480 const MachineOperand &MO1 = MI->getOperand(Op);
481 const MachineOperand &MO2 = MI->getOperand(Op+1);
482
483 if (MO1.getReg()) {
484 O << (char)ARM_AM::getAM3Op(MO2.getImm())
485 << TM.getRegisterInfo()->get(MO1.getReg()).Name;
486 return;
487 }
488
489 unsigned ImmOffs = ARM_AM::getAM3Offset(MO2.getImm());
Evan Chengbdc98692007-05-03 23:30:36 +0000490 assert(ImmOffs && "Malformed indexed load / store!");
Evan Chenga8e29892007-01-19 07:51:42 +0000491 O << "#"
Evan Chengbdc98692007-05-03 23:30:36 +0000492 << (char)ARM_AM::getAM3Op(MO2.getImm())
Evan Chenga8e29892007-01-19 07:51:42 +0000493 << ImmOffs;
494}
495
496void ARMAsmPrinter::printAddrMode4Operand(const MachineInstr *MI, int Op,
497 const char *Modifier) {
498 const MachineOperand &MO1 = MI->getOperand(Op);
499 const MachineOperand &MO2 = MI->getOperand(Op+1);
500 ARM_AM::AMSubMode Mode = ARM_AM::getAM4SubMode(MO2.getImm());
501 if (Modifier && strcmp(Modifier, "submode") == 0) {
502 if (MO1.getReg() == ARM::SP) {
503 bool isLDM = (MI->getOpcode() == ARM::LDM ||
504 MI->getOpcode() == ARM::LDM_RET);
505 O << ARM_AM::getAMSubModeAltStr(Mode, isLDM);
506 } else
507 O << ARM_AM::getAMSubModeStr(Mode);
508 } else {
509 printOperand(MI, Op);
510 if (ARM_AM::getAM4WBFlag(MO2.getImm()))
511 O << "!";
512 }
513}
514
515void ARMAsmPrinter::printAddrMode5Operand(const MachineInstr *MI, int Op,
516 const char *Modifier) {
517 const MachineOperand &MO1 = MI->getOperand(Op);
518 const MachineOperand &MO2 = MI->getOperand(Op+1);
519
520 if (!MO1.isRegister()) { // FIXME: This is for CP entries, but isn't right.
521 printOperand(MI, Op);
522 return;
523 }
524
525 assert(MRegisterInfo::isPhysicalRegister(MO1.getReg()));
526
527 if (Modifier && strcmp(Modifier, "submode") == 0) {
528 ARM_AM::AMSubMode Mode = ARM_AM::getAM5SubMode(MO2.getImm());
529 if (MO1.getReg() == ARM::SP) {
530 bool isFLDM = (MI->getOpcode() == ARM::FLDMD ||
531 MI->getOpcode() == ARM::FLDMS);
532 O << ARM_AM::getAMSubModeAltStr(Mode, isFLDM);
533 } else
534 O << ARM_AM::getAMSubModeStr(Mode);
535 return;
536 } else if (Modifier && strcmp(Modifier, "base") == 0) {
537 // Used for FSTM{D|S} and LSTM{D|S} operations.
538 O << TM.getRegisterInfo()->get(MO1.getReg()).Name;
539 if (ARM_AM::getAM5WBFlag(MO2.getImm()))
540 O << "!";
541 return;
542 }
543
544 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
545
546 if (unsigned ImmOffs = ARM_AM::getAM5Offset(MO2.getImm())) {
547 O << ", #"
548 << (char)ARM_AM::getAM5Op(MO2.getImm())
549 << ImmOffs*4;
550 }
551 O << "]";
552}
553
554void ARMAsmPrinter::printAddrModePCOperand(const MachineInstr *MI, int Op,
555 const char *Modifier) {
556 if (Modifier && strcmp(Modifier, "label") == 0) {
557 printPCLabel(MI, Op+1);
558 return;
559 }
560
561 const MachineOperand &MO1 = MI->getOperand(Op);
562 assert(MRegisterInfo::isPhysicalRegister(MO1.getReg()));
563 O << "[pc, +" << TM.getRegisterInfo()->get(MO1.getReg()).Name << "]";
564}
565
566void
567ARMAsmPrinter::printThumbAddrModeRROperand(const MachineInstr *MI, int Op) {
568 const MachineOperand &MO1 = MI->getOperand(Op);
569 const MachineOperand &MO2 = MI->getOperand(Op+1);
570 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
571 O << ", " << TM.getRegisterInfo()->get(MO2.getReg()).Name << "]";
572}
573
574void
575ARMAsmPrinter::printThumbAddrModeRI5Operand(const MachineInstr *MI, int Op,
576 unsigned Scale) {
577 const MachineOperand &MO1 = MI->getOperand(Op);
Evan Chengcea117d2007-01-30 02:35:32 +0000578 const MachineOperand &MO2 = MI->getOperand(Op+1);
579 const MachineOperand &MO3 = MI->getOperand(Op+2);
Evan Chenga8e29892007-01-19 07:51:42 +0000580
581 if (!MO1.isRegister()) { // FIXME: This is for CP entries, but isn't right.
582 printOperand(MI, Op);
583 return;
584 }
585
586 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
Evan Chengcea117d2007-01-30 02:35:32 +0000587 if (MO3.getReg())
588 O << ", " << TM.getRegisterInfo()->get(MO3.getReg()).Name;
589 else if (unsigned ImmOffs = MO2.getImm()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000590 O << ", #" << ImmOffs;
591 if (Scale > 1)
592 O << " * " << Scale;
593 }
594 O << "]";
595}
596
597void
Evan Chengc38f2bc2007-01-23 22:59:13 +0000598ARMAsmPrinter::printThumbAddrModeS1Operand(const MachineInstr *MI, int Op) {
Evan Chengcea117d2007-01-30 02:35:32 +0000599 printThumbAddrModeRI5Operand(MI, Op, 1);
Evan Chenga8e29892007-01-19 07:51:42 +0000600}
601void
Evan Chengc38f2bc2007-01-23 22:59:13 +0000602ARMAsmPrinter::printThumbAddrModeS2Operand(const MachineInstr *MI, int Op) {
Evan Chengcea117d2007-01-30 02:35:32 +0000603 printThumbAddrModeRI5Operand(MI, Op, 2);
Evan Chenga8e29892007-01-19 07:51:42 +0000604}
605void
Evan Chengc38f2bc2007-01-23 22:59:13 +0000606ARMAsmPrinter::printThumbAddrModeS4Operand(const MachineInstr *MI, int Op) {
Evan Chengcea117d2007-01-30 02:35:32 +0000607 printThumbAddrModeRI5Operand(MI, Op, 4);
Evan Chenga8e29892007-01-19 07:51:42 +0000608}
609
610void ARMAsmPrinter::printThumbAddrModeSPOperand(const MachineInstr *MI,int Op) {
611 const MachineOperand &MO1 = MI->getOperand(Op);
612 const MachineOperand &MO2 = MI->getOperand(Op+1);
613 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
614 if (unsigned ImmOffs = MO2.getImm())
615 O << ", #" << ImmOffs << " * 4";
616 O << "]";
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000617}
618
Evan Cheng42d712b2007-05-08 21:08:43 +0000619void ARMAsmPrinter::printPredicateOperand(const MachineInstr *MI, int opNum) {
Evan Cheng44bec522007-05-15 01:29:07 +0000620 ARMCC::CondCodes CC = (ARMCC::CondCodes)MI->getOperand(opNum).getImmedValue();
621 if (CC != ARMCC::AL)
622 O << ARMCondCodeToString(CC);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000623}
624
Evan Chengdfb2eba2007-07-06 01:01:34 +0000625void ARMAsmPrinter::printSBitModifierOperand(const MachineInstr *MI, int opNum){
626 unsigned Reg = MI->getOperand(opNum).getReg();
627 if (Reg) {
628 assert(Reg == ARM::CPSR && "Expect ARM CPSR register!");
629 O << 's';
630 }
631}
632
Evan Chenga8e29892007-01-19 07:51:42 +0000633void ARMAsmPrinter::printPCLabel(const MachineInstr *MI, int opNum) {
634 int Id = (int)MI->getOperand(opNum).getImmedValue();
635 O << TAI->getPrivateGlobalPrefix() << "PC" << Id;
636}
637
638void ARMAsmPrinter::printRegisterList(const MachineInstr *MI, int opNum) {
639 O << "{";
640 for (unsigned i = opNum, e = MI->getNumOperands(); i != e; ++i) {
641 printOperand(MI, i);
642 if (i != e-1) O << ", ";
643 }
644 O << "}";
645}
646
647void ARMAsmPrinter::printCPInstOperand(const MachineInstr *MI, int OpNo,
648 const char *Modifier) {
649 assert(Modifier && "This operand only works with a modifier!");
650 // There are two aspects to a CONSTANTPOOL_ENTRY operand, the label and the
651 // data itself.
652 if (!strcmp(Modifier, "label")) {
653 unsigned ID = MI->getOperand(OpNo).getImm();
Evan Cheng347d39f2007-10-14 05:57:21 +0000654 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
655 << '_' << ID << ":\n";
Evan Chenga8e29892007-01-19 07:51:42 +0000656 } else {
657 assert(!strcmp(Modifier, "cpentry") && "Unknown modifier for CPE");
658 unsigned CPI = MI->getOperand(OpNo).getConstantPoolIndex();
659
660 const MachineConstantPoolEntry &MCPE = // Chasing pointers is fun?
661 MI->getParent()->getParent()->getConstantPool()->getConstants()[CPI];
662
663 if (MCPE.isMachineConstantPoolEntry())
664 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
Lauro Ramos Venancio305b8a52007-04-25 14:50:40 +0000665 else {
Evan Chenga8e29892007-01-19 07:51:42 +0000666 EmitGlobalConstant(MCPE.Val.ConstVal);
Lauro Ramos Venancio305b8a52007-04-25 14:50:40 +0000667 // remember to emit the weak reference
668 if (const GlobalValue *GV = dyn_cast<GlobalValue>(MCPE.Val.ConstVal))
669 if (GV->hasExternalWeakLinkage())
670 ExtWeakSymbols.insert(GV);
671 }
Evan Chenga8e29892007-01-19 07:51:42 +0000672 }
673}
674
675void ARMAsmPrinter::printJTBlockOperand(const MachineInstr *MI, int OpNo) {
676 const MachineOperand &MO1 = MI->getOperand(OpNo);
677 const MachineOperand &MO2 = MI->getOperand(OpNo+1); // Unique Id
678 unsigned JTI = MO1.getJumpTableIndex();
Evan Cheng347d39f2007-10-14 05:57:21 +0000679 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
680 << '_' << JTI << '_' << MO2.getImmedValue() << ":\n";
Evan Chenga8e29892007-01-19 07:51:42 +0000681
682 const char *JTEntryDirective = TAI->getJumpTableDirective();
683 if (!JTEntryDirective)
684 JTEntryDirective = TAI->getData32bitsDirective();
685
686 const MachineFunction *MF = MI->getParent()->getParent();
687 MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
688 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
689 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
690 bool UseSet= TAI->getSetDirective() && TM.getRelocationModel() == Reloc::PIC_;
691 std::set<MachineBasicBlock*> JTSets;
692 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
693 MachineBasicBlock *MBB = JTBBs[i];
694 if (UseSet && JTSets.insert(MBB).second)
Evan Chengcc415862007-11-09 01:32:10 +0000695 printPICJumpTableSetLabel(JTI, MO2.getImmedValue(), MBB);
Evan Chenga8e29892007-01-19 07:51:42 +0000696
697 O << JTEntryDirective << ' ';
698 if (UseSet)
Evan Cheng347d39f2007-10-14 05:57:21 +0000699 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
700 << '_' << JTI << '_' << MO2.getImmedValue()
701 << "_set_" << MBB->getNumber();
Evan Chenga8e29892007-01-19 07:51:42 +0000702 else if (TM.getRelocationModel() == Reloc::PIC_) {
703 printBasicBlockLabel(MBB, false, false);
704 // If the arch uses custom Jump Table directives, don't calc relative to JT
705 if (!TAI->getJumpTableDirective())
706 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
Evan Cheng347d39f2007-10-14 05:57:21 +0000707 << getFunctionNumber() << '_' << JTI << '_' << MO2.getImmedValue();
Evan Chenga8e29892007-01-19 07:51:42 +0000708 } else
709 printBasicBlockLabel(MBB, false, false);
Evan Chengd85ac4d2007-01-27 02:29:45 +0000710 if (i != e-1)
711 O << '\n';
Evan Chenga8e29892007-01-19 07:51:42 +0000712 }
713}
714
715
716bool ARMAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
717 unsigned AsmVariant, const char *ExtraCode){
718 // Does this asm operand have a single letter operand modifier?
719 if (ExtraCode && ExtraCode[0]) {
720 if (ExtraCode[1] != 0) return true; // Unknown modifier.
721
722 switch (ExtraCode[0]) {
723 default: return true; // Unknown modifier.
Evan Cheng23a95702007-03-08 22:42:46 +0000724 case 'c': // Don't print "$" before a global var name or constant.
Evan Chenge21e3962007-04-04 00:13:29 +0000725 case 'P': // Print a VFP double precision register.
Evan Cheng23a95702007-03-08 22:42:46 +0000726 printOperand(MI, OpNo);
727 return false;
Evan Chenga8e29892007-01-19 07:51:42 +0000728 case 'Q':
729 if (TM.getTargetData()->isLittleEndian())
730 break;
731 // Fallthrough
732 case 'R':
733 if (TM.getTargetData()->isBigEndian())
734 break;
735 // Fallthrough
736 case 'H': // Write second word of DI / DF reference.
737 // Verify that this operand has two consecutive registers.
738 if (!MI->getOperand(OpNo).isRegister() ||
739 OpNo+1 == MI->getNumOperands() ||
740 !MI->getOperand(OpNo+1).isRegister())
741 return true;
742 ++OpNo; // Return the high-part.
743 }
744 }
745
746 printOperand(MI, OpNo);
747 return false;
748}
749
750void ARMAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
751 ++EmittedInsts;
752
Evan Chengc60e76d2007-01-30 20:37:08 +0000753 int Opc = MI->getOpcode();
754 switch (Opc) {
755 case ARM::CONSTPOOL_ENTRY:
Evan Chenga8e29892007-01-19 07:51:42 +0000756 if (!InCPMode && AFI->isThumbFunction()) {
757 EmitAlignment(2);
758 InCPMode = true;
759 }
Evan Chengc60e76d2007-01-30 20:37:08 +0000760 break;
761 default: {
Evan Cheng3bf12d02007-01-31 23:39:39 +0000762 if (InCPMode && AFI->isThumbFunction())
Evan Chenga8e29892007-01-19 07:51:42 +0000763 InCPMode = false;
Evan Chengc60e76d2007-01-30 20:37:08 +0000764 switch (Opc) {
765 case ARM::PICADD:
766 case ARM::PICLD:
Evan Cheng341dccc2007-06-05 07:36:38 +0000767 case ARM::PICLDZH:
768 case ARM::PICLDZB:
769 case ARM::PICLDH:
770 case ARM::PICLDB:
771 case ARM::PICLDSH:
772 case ARM::PICLDSB:
773 case ARM::PICSTR:
774 case ARM::PICSTRH:
775 case ARM::PICSTRB:
Evan Chengc60e76d2007-01-30 20:37:08 +0000776 case ARM::tPICADD:
777 break;
778 default:
779 O << "\t";
780 break;
781 }
782 }}
Evan Chenga8e29892007-01-19 07:51:42 +0000783
784 // Call the autogenerated instruction printer routines.
785 printInstruction(MI);
786}
787
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000788bool ARMAsmPrinter::doInitialization(Module &M) {
Lauro Ramos Venancioe8e54952007-05-03 20:28:35 +0000789 // Emit initial debug information.
790 DW.BeginModule(&M);
Evan Chenga8e29892007-01-19 07:51:42 +0000791
Dan Gohmanb8275a32007-07-25 19:33:14 +0000792 bool Result = AsmPrinter::doInitialization(M);
Dale Johannesen54118352007-06-22 00:54:56 +0000793
794 // Darwin wants symbols to be quoted if they have complex names.
795 if (Subtarget->isTargetDarwin())
796 Mang->setUseQuotes(true);
797
Dan Gohmanb8275a32007-07-25 19:33:14 +0000798 return Result;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000799}
800
801bool ARMAsmPrinter::doFinalization(Module &M) {
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000802 const TargetData *TD = TM.getTargetData();
803
804 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
805 I != E; ++I) {
806 if (!I->hasInitializer()) // External global require no code
807 continue;
808
Evan Chengb267ca12007-01-30 08:04:53 +0000809 if (EmitSpecialLLVMGlobal(I)) {
810 if (Subtarget->isTargetDarwin() &&
811 TM.getRelocationModel() == Reloc::Static) {
812 if (I->getName() == "llvm.global_ctors")
813 O << ".reference .constructors_used\n";
814 else if (I->getName() == "llvm.global_dtors")
815 O << ".reference .destructors_used\n";
816 }
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000817 continue;
Evan Chengb267ca12007-01-30 08:04:53 +0000818 }
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000819
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000820 std::string name = Mang->getValueName(I);
821 Constant *C = I->getInitializer();
Evan Cheng98ded762007-03-08 01:25:25 +0000822 const Type *Type = C->getType();
Duncan Sandsca0ed742007-11-05 00:04:43 +0000823 unsigned Size = TD->getABITypeSize(Type);
Evan Chenga8e29892007-01-19 07:51:42 +0000824 unsigned Align = TD->getPreferredAlignmentLog(I);
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000825
Lauro Ramos Venancio0a181732007-04-30 00:23:51 +0000826 const char *VisibilityDirective = NULL;
Evan Cheng5be54b02007-01-19 19:25:36 +0000827 if (I->hasHiddenVisibility())
Lauro Ramos Venancio0a181732007-04-30 00:23:51 +0000828 VisibilityDirective = TAI->getHiddenDirective();
829 else if (I->hasProtectedVisibility())
830 VisibilityDirective = TAI->getProtectedDirective();
831
832 if (VisibilityDirective)
833 O << VisibilityDirective << name << "\n";
834
Evan Cheng5be54b02007-01-19 19:25:36 +0000835 if (Subtarget->isTargetELF())
Lauro Ramos Venanciob1cc0522007-01-25 20:11:04 +0000836 O << "\t.type " << name << ",%object\n";
Evan Cheng5be54b02007-01-19 19:25:36 +0000837
Lauro Ramos Venancio8699a972007-11-05 18:33:37 +0000838 if (C->isNullValue() && !I->hasSection() && !I->isThreadLocal()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000839 if (I->hasExternalLinkage()) {
Evan Cheng5be54b02007-01-19 19:25:36 +0000840 if (const char *Directive = TAI->getZeroFillDirective()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000841 O << "\t.globl\t" << name << "\n";
Evan Cheng5be54b02007-01-19 19:25:36 +0000842 O << Directive << "__DATA__, __common, " << name << ", "
843 << Size << ", " << Align << "\n";
844 continue;
845 }
846 }
847
Evan Cheng76a40232007-09-21 00:41:19 +0000848 if (I->hasInternalLinkage() || I->hasWeakLinkage() ||
849 I->hasLinkOnceLinkage()) {
Evan Cheng5be54b02007-01-19 19:25:36 +0000850 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
851 if (!NoZerosInBSS && TAI->getBSSSection())
852 SwitchToDataSection(TAI->getBSSSection(), I);
853 else
854 SwitchToDataSection(TAI->getDataSection(), I);
Evan Chenga8e29892007-01-19 07:51:42 +0000855 if (TAI->getLCOMMDirective() != NULL) {
856 if (I->hasInternalLinkage()) {
857 O << TAI->getLCOMMDirective() << name << "," << Size;
Evan Cheng5be54b02007-01-19 19:25:36 +0000858 if (Subtarget->isTargetDarwin())
Evan Chenga8e29892007-01-19 07:51:42 +0000859 O << "," << Align;
860 } else
861 O << TAI->getCOMMDirective() << name << "," << Size;
862 } else {
863 if (I->hasInternalLinkage())
864 O << "\t.local\t" << name << "\n";
865 O << TAI->getCOMMDirective() << name << "," << Size;
866 if (TAI->getCOMMDirectiveTakesAlignment())
867 O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
868 }
Evan Cheng5be54b02007-01-19 19:25:36 +0000869 O << "\t\t" << TAI->getCommentString() << " " << I->getName() << "\n";
870 continue;
Evan Chenga8e29892007-01-19 07:51:42 +0000871 }
Evan Cheng5be54b02007-01-19 19:25:36 +0000872 }
Evan Chenga8e29892007-01-19 07:51:42 +0000873
Evan Cheng5be54b02007-01-19 19:25:36 +0000874 switch (I->getLinkage()) {
875 case GlobalValue::LinkOnceLinkage:
876 case GlobalValue::WeakLinkage:
877 if (Subtarget->isTargetDarwin()) {
878 O << "\t.globl " << name << "\n"
879 << "\t.weak_definition " << name << "\n";
880 SwitchToDataSection("\t.section __DATA,__const_coal,coalesced", I);
881 } else {
882 std::string SectionName("\t.section\t.llvm.linkonce.d." +
883 name +
884 ",\"aw\",%progbits");
885 SwitchToDataSection(SectionName.c_str(), I);
886 O << "\t.weak " << name << "\n";
887 }
888 break;
889 case GlobalValue::AppendingLinkage:
890 // FIXME: appending linkage variables should go into a section of
891 // their name or something. For now, just emit them as external.
892 case GlobalValue::ExternalLinkage:
893 O << "\t.globl " << name << "\n";
894 // FALL THROUGH
895 case GlobalValue::InternalLinkage: {
896 if (I->isConstant()) {
897 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
898 if (TAI->getCStringSection() && CVA && CVA->isCString()) {
899 SwitchToDataSection(TAI->getCStringSection(), I);
900 break;
901 }
902 }
903 // FIXME: special handling for ".ctors" & ".dtors" sections
904 if (I->hasSection() &&
905 (I->getSection() == ".ctors" ||
906 I->getSection() == ".dtors")) {
907 assert(!Subtarget->isTargetDarwin());
908 std::string SectionName = ".section " + I->getSection();
909 SectionName += ",\"aw\",%progbits";
910 SwitchToDataSection(SectionName.c_str());
911 } else {
912 if (C->isNullValue() && !NoZerosInBSS && TAI->getBSSSection())
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000913 SwitchToDataSection(I->isThreadLocal() ? TAI->getTLSBSSSection() :
914 TAI->getBSSSection(), I);
Evan Cheng98ded762007-03-08 01:25:25 +0000915 else if (!I->isConstant())
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000916 SwitchToDataSection(I->isThreadLocal() ? TAI->getTLSDataSection() :
917 TAI->getDataSection(), I);
918 else if (I->isThreadLocal())
919 SwitchToDataSection(TAI->getTLSDataSection());
Evan Cheng98ded762007-03-08 01:25:25 +0000920 else {
921 // Read-only data.
Evan Cheng032953d2007-03-08 08:31:54 +0000922 bool HasReloc = C->ContainsRelocations();
923 if (HasReloc &&
924 Subtarget->isTargetDarwin() &&
Evan Cheng98ded762007-03-08 01:25:25 +0000925 TM.getRelocationModel() != Reloc::Static)
926 SwitchToDataSection("\t.const_data\n");
Evan Cheng032953d2007-03-08 08:31:54 +0000927 else if (!HasReloc && Size == 4 &&
Evan Cheng98ded762007-03-08 01:25:25 +0000928 TAI->getFourByteConstantSection())
929 SwitchToDataSection(TAI->getFourByteConstantSection(), I);
Evan Cheng032953d2007-03-08 08:31:54 +0000930 else if (!HasReloc && Size == 8 &&
Evan Cheng98ded762007-03-08 01:25:25 +0000931 TAI->getEightByteConstantSection())
932 SwitchToDataSection(TAI->getEightByteConstantSection(), I);
Evan Cheng032953d2007-03-08 08:31:54 +0000933 else if (!HasReloc && Size == 16 &&
Evan Cheng98ded762007-03-08 01:25:25 +0000934 TAI->getSixteenByteConstantSection())
935 SwitchToDataSection(TAI->getSixteenByteConstantSection(), I);
936 else if (TAI->getReadOnlySection())
937 SwitchToDataSection(TAI->getReadOnlySection(), I);
938 else
939 SwitchToDataSection(TAI->getDataSection(), I);
940 }
Rafael Espindolab97809c2006-10-19 13:30:40 +0000941 }
Evan Cheng5be54b02007-01-19 19:25:36 +0000942
943 break;
944 }
945 default:
946 assert(0 && "Unknown linkage type!");
947 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000948 }
Rafael Espindolab97809c2006-10-19 13:30:40 +0000949
Evan Chenga8e29892007-01-19 07:51:42 +0000950 EmitAlignment(Align, I);
Evan Cheng5be54b02007-01-19 19:25:36 +0000951 O << name << ":\t\t\t\t" << TAI->getCommentString() << " " << I->getName()
952 << "\n";
953 if (TAI->hasDotTypeDotSizeDirective())
Rafael Espindolab97809c2006-10-19 13:30:40 +0000954 O << "\t.size " << name << ", " << Size << "\n";
Evan Chenga8e29892007-01-19 07:51:42 +0000955 // If the initializer is a extern weak symbol, remember to emit the weak
956 // reference!
957 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
958 if (GV->hasExternalWeakLinkage())
959 ExtWeakSymbols.insert(GV);
960
961 EmitGlobalConstant(C);
962 O << '\n';
963 }
964
Evan Cheng5be54b02007-01-19 19:25:36 +0000965 if (Subtarget->isTargetDarwin()) {
966 SwitchToDataSection("");
967
Evan Chenga8e29892007-01-19 07:51:42 +0000968 // Output stubs for dynamically-linked functions
969 unsigned j = 1;
970 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
971 i != e; ++i, ++j) {
972 if (TM.getRelocationModel() == Reloc::PIC_)
973 SwitchToTextSection(".section __TEXT,__picsymbolstub4,symbol_stubs,"
974 "none,16", 0);
975 else
976 SwitchToTextSection(".section __TEXT,__symbol_stub4,symbol_stubs,"
977 "none,12", 0);
978
979 EmitAlignment(2);
980 O << "\t.code\t32\n";
981
982 O << "L" << *i << "$stub:\n";
983 O << "\t.indirect_symbol " << *i << "\n";
984 O << "\tldr ip, L" << *i << "$slp\n";
985 if (TM.getRelocationModel() == Reloc::PIC_) {
986 O << "L" << *i << "$scv:\n";
987 O << "\tadd ip, pc, ip\n";
988 }
989 O << "\tldr pc, [ip, #0]\n";
990 O << "L" << *i << "$slp:\n";
991 if (TM.getRelocationModel() == Reloc::PIC_)
992 O << "\t.long\tL" << *i << "$lazy_ptr-(L" << *i << "$scv+8)\n";
993 else
994 O << "\t.long\tL" << *i << "$lazy_ptr\n";
995 SwitchToDataSection(".lazy_symbol_pointer", 0);
996 O << "L" << *i << "$lazy_ptr:\n";
997 O << "\t.indirect_symbol " << *i << "\n";
998 O << "\t.long\tdyld_stub_binding_helper\n";
999 }
1000 O << "\n";
1001
1002 // Output non-lazy-pointers for external and common global variables.
Dan Gohmancb406c22007-10-03 19:26:29 +00001003 if (!GVNonLazyPtrs.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001004 SwitchToDataSection(".non_lazy_symbol_pointer", 0);
1005 for (std::set<std::string>::iterator i = GVNonLazyPtrs.begin(),
1006 e = GVNonLazyPtrs.end(); i != e; ++i) {
1007 O << "L" << *i << "$non_lazy_ptr:\n";
1008 O << "\t.indirect_symbol " << *i << "\n";
1009 O << "\t.long\t0\n";
1010 }
1011
1012 // Emit initial debug information.
1013 DW.EndModule();
1014
1015 // Funny Darwin hack: This flag tells the linker that no global symbols
1016 // contain code that falls through to other global symbols (e.g. the obvious
1017 // implementation of multiple entry points). If this doesn't occur, the
1018 // linker can safely perform dead code stripping. Since LLVM never
1019 // generates code that does this, it is always safe to set.
1020 O << "\t.subsections_via_symbols\n";
Lauro Ramos Venancioe8e54952007-05-03 20:28:35 +00001021 } else {
1022 // Emit final debug information for ELF.
1023 DW.EndModule();
Rafael Espindolab01c4bb2006-07-27 11:38:51 +00001024 }
Rafael Espindolab97809c2006-10-19 13:30:40 +00001025
Dan Gohmanb8275a32007-07-25 19:33:14 +00001026 return AsmPrinter::doFinalization(M);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00001027}