blob: 924a2df82f669fb2f21c99f700966fae02f565e3 [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 Chengdfb2eba2007-07-06 01:01:34 +0000105 void printSBitModifierOperand(const MachineInstr *MI, int opNum);
Evan Chenga8e29892007-01-19 07:51:42 +0000106 void printPCLabel(const MachineInstr *MI, int opNum);
107 void printRegisterList(const MachineInstr *MI, int opNum);
108 void printCPInstOperand(const MachineInstr *MI, int opNum,
109 const char *Modifier);
110 void printJTBlockOperand(const MachineInstr *MI, int opNum);
111
112 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
113 unsigned AsmVariant, const char *ExtraCode);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000114
115 bool printInstruction(const MachineInstr *MI); // autogenerated.
Evan Chenga8e29892007-01-19 07:51:42 +0000116 void printMachineInstruction(const MachineInstr *MI);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000117 bool runOnMachineFunction(MachineFunction &F);
118 bool doInitialization(Module &M);
119 bool doFinalization(Module &M);
Evan Chenga8e29892007-01-19 07:51:42 +0000120
121 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
122 printDataDirective(MCPV->getType());
123
124 ARMConstantPoolValue *ACPV = (ARMConstantPoolValue*)MCPV;
Lauro Ramos Venancio1a92d942007-01-26 19:51:32 +0000125 GlobalValue *GV = ACPV->getGV();
Evan Chengc60e76d2007-01-30 20:37:08 +0000126 std::string Name = GV ? Mang->getValueName(GV) : TAI->getGlobalPrefix();
127 if (!GV)
128 Name += ACPV->getSymbol();
Evan Chenga8e29892007-01-19 07:51:42 +0000129 if (ACPV->isNonLazyPointer()) {
130 GVNonLazyPtrs.insert(Name);
131 O << TAI->getPrivateGlobalPrefix() << Name << "$non_lazy_ptr";
Evan Chengc60e76d2007-01-30 20:37:08 +0000132 } else if (ACPV->isStub()) {
133 FnStubs.insert(Name);
134 O << TAI->getPrivateGlobalPrefix() << Name << "$stub";
Evan Chenga8e29892007-01-19 07:51:42 +0000135 } else
136 O << Name;
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000137 if (ACPV->hasModifier()) O << "(" << ACPV->getModifier() << ")";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000138 if (ACPV->getPCAdjustment() != 0) {
Evan Chenga8e29892007-01-19 07:51:42 +0000139 O << "-(" << TAI->getPrivateGlobalPrefix() << "PC"
140 << utostr(ACPV->getLabelId())
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000141 << "+" << (unsigned)ACPV->getPCAdjustment();
142 if (ACPV->mustAddCurrentAddress())
143 O << "-.";
144 O << ")";
145 }
Evan Chenga8e29892007-01-19 07:51:42 +0000146 O << "\n";
Lauro Ramos Venancio1a92d942007-01-26 19:51:32 +0000147
148 // If the constant pool value is a extern weak symbol, remember to emit
149 // the weak reference.
Evan Chengc60e76d2007-01-30 20:37:08 +0000150 if (GV && GV->hasExternalWeakLinkage())
Lauro Ramos Venancio1a92d942007-01-26 19:51:32 +0000151 ExtWeakSymbols.insert(GV);
Evan Chenga8e29892007-01-19 07:51:42 +0000152 }
153
154 void getAnalysisUsage(AnalysisUsage &AU) const {
Gordon Henriksencd8bc052007-09-30 13:39:29 +0000155 AsmPrinter::getAnalysisUsage(AU);
Evan Chenga8e29892007-01-19 07:51:42 +0000156 AU.setPreservesAll();
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000157 AU.addRequired<MachineModuleInfo>();
Evan Chenga8e29892007-01-19 07:51:42 +0000158 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000159 };
160} // end of anonymous namespace
161
162#include "ARMGenAsmWriter.inc"
163
164/// createARMCodePrinterPass - Returns a pass that prints the ARM
165/// assembly code for a MachineFunction to the given output stream,
166/// using the given target machine description. This should work
167/// regardless of whether the function is in SSA form.
168///
169FunctionPass *llvm::createARMCodePrinterPass(std::ostream &o,
Evan Chenga8e29892007-01-19 07:51:42 +0000170 ARMTargetMachine &tm) {
Jim Laskeya0f3d172006-09-07 22:06:40 +0000171 return new ARMAsmPrinter(o, tm, tm.getTargetAsmInfo());
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000172}
173
Evan Chenga8e29892007-01-19 07:51:42 +0000174/// runOnMachineFunction - This uses the printInstruction()
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000175/// method to print assembly for each instruction.
176///
177bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Evan Chenga8e29892007-01-19 07:51:42 +0000178 AFI = MF.getInfo<ARMFunctionInfo>();
Rafael Espindola4b442b52006-05-23 02:48:20 +0000179
Lauro Ramos Venancioe8e54952007-05-03 20:28:35 +0000180 DW.SetModuleInfo(&getAnalysis<MachineModuleInfo>());
Chris Lattner1a199de2006-12-21 22:59:58 +0000181
Evan Chenga8e29892007-01-19 07:51:42 +0000182 SetupMachineFunction(MF);
183 O << "\n";
Rafael Espindola4b442b52006-05-23 02:48:20 +0000184
Evan Chenga8e29892007-01-19 07:51:42 +0000185 // NOTE: we don't print out constant pools here, they are handled as
186 // instructions.
187
188 O << "\n";
Rafael Espindola4b442b52006-05-23 02:48:20 +0000189 // Print out labels for the function.
190 const Function *F = MF.getFunction();
191 switch (F->getLinkage()) {
192 default: assert(0 && "Unknown linkage type!");
193 case Function::InternalLinkage:
Evan Chenga8e29892007-01-19 07:51:42 +0000194 SwitchToTextSection("\t.text", F);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000195 break;
196 case Function::ExternalLinkage:
Evan Chenga8e29892007-01-19 07:51:42 +0000197 SwitchToTextSection("\t.text", F);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000198 O << "\t.globl\t" << CurrentFnName << "\n";
199 break;
200 case Function::WeakLinkage:
201 case Function::LinkOnceLinkage:
Evan Cheng5be54b02007-01-19 19:25:36 +0000202 if (Subtarget->isTargetDarwin()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000203 SwitchToTextSection(
204 ".section __TEXT,__textcoal_nt,coalesced,pure_instructions", F);
205 O << "\t.globl\t" << CurrentFnName << "\n";
206 O << "\t.weak_definition\t" << CurrentFnName << "\n";
207 } else {
208 O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
209 }
Rafael Espindola4b442b52006-05-23 02:48:20 +0000210 break;
211 }
Evan Chenga8e29892007-01-19 07:51:42 +0000212
Lauro Ramos Venancio0a181732007-04-30 00:23:51 +0000213 const char *VisibilityDirective = NULL;
Evan Cheng616cc662007-03-29 07:49:34 +0000214 if (F->hasHiddenVisibility())
Lauro Ramos Venancio0a181732007-04-30 00:23:51 +0000215 VisibilityDirective = TAI->getHiddenDirective();
216 else if (F->hasProtectedVisibility())
217 VisibilityDirective = TAI->getProtectedDirective();
218
219 if (VisibilityDirective)
220 O << VisibilityDirective << CurrentFnName << "\n";
Evan Cheng616cc662007-03-29 07:49:34 +0000221
Evan Chenga8e29892007-01-19 07:51:42 +0000222 if (AFI->isThumbFunction()) {
Chris Lattner3a420532007-05-31 18:57:45 +0000223 EmitAlignment(1, F, AFI->getAlign());
Evan Chenga8e29892007-01-19 07:51:42 +0000224 O << "\t.code\t16\n";
Lauro Ramos Venancio6f46e592007-02-01 18:25:34 +0000225 O << "\t.thumb_func";
226 if (Subtarget->isTargetDarwin())
227 O << "\t" << CurrentFnName;
228 O << "\n";
Evan Chenga8e29892007-01-19 07:51:42 +0000229 InCPMode = false;
230 } else
231 EmitAlignment(2, F);
232
Rafael Espindola4b442b52006-05-23 02:48:20 +0000233 O << CurrentFnName << ":\n";
Lauro Ramos Venancioe8e54952007-05-03 20:28:35 +0000234 // Emit pre-function debug information.
235 DW.BeginFunction(&MF);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000236
237 // Print out code for the function.
238 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
239 I != E; ++I) {
240 // Print a label for the basic block.
241 if (I != MF.begin()) {
242 printBasicBlockLabel(I, true);
243 O << '\n';
244 }
245 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
246 II != E; ++II) {
247 // Print the assembly for the instruction.
Evan Chenga8e29892007-01-19 07:51:42 +0000248 printMachineInstruction(II);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000249 }
250 }
251
Evan Chenga8e29892007-01-19 07:51:42 +0000252 if (TAI->hasDotTypeDotSizeDirective())
253 O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
254
Lauro Ramos Venancioe8e54952007-05-03 20:28:35 +0000255 // Emit post-function debug information.
256 DW.EndFunction();
Evan Chenga8e29892007-01-19 07:51:42 +0000257
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000258 return false;
259}
260
Evan Chenga8e29892007-01-19 07:51:42 +0000261void ARMAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
262 const char *Modifier) {
263 const MachineOperand &MO = MI->getOperand(opNum);
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000264 switch (MO.getType()) {
265 case MachineOperand::MO_Register:
266 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
Evan Chenga8e29892007-01-19 07:51:42 +0000267 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000268 else
269 assert(0 && "not implemented");
270 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000271 case MachineOperand::MO_Immediate: {
272 if (!Modifier || strcmp(Modifier, "no_hash") != 0)
273 O << "#";
274
275 O << (int)MO.getImmedValue();
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000276 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000277 }
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000278 case MachineOperand::MO_MachineBasicBlock:
Rafael Espindola687bc492006-08-24 13:45:55 +0000279 printBasicBlockLabel(MO.getMachineBasicBlock());
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000280 return;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000281 case MachineOperand::MO_GlobalAddress: {
Evan Chenga8e29892007-01-19 07:51:42 +0000282 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Rafael Espindola84b19be2006-07-16 01:02:57 +0000283 GlobalValue *GV = MO.getGlobal();
284 std::string Name = Mang->getValueName(GV);
Reid Spencer5cbf9852007-01-30 20:08:39 +0000285 bool isExt = (GV->isDeclaration() || GV->hasWeakLinkage() ||
Evan Chenga8e29892007-01-19 07:51:42 +0000286 GV->hasLinkOnceLinkage());
Evan Cheng5be54b02007-01-19 19:25:36 +0000287 if (isExt && isCallOp && Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +0000288 TM.getRelocationModel() != Reloc::Static) {
289 O << TAI->getPrivateGlobalPrefix() << Name << "$stub";
290 FnStubs.insert(Name);
291 } else
292 O << Name;
Chris Lattner388488d2007-05-03 16:42:23 +0000293
294 if (MO.getOffset() > 0)
295 O << '+' << MO.getOffset();
296 else if (MO.getOffset() < 0)
297 O << MO.getOffset();
298
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000299 if (isCallOp && Subtarget->isTargetELF() &&
300 TM.getRelocationModel() == Reloc::PIC_)
301 O << "(PLT)";
Evan Chenga8e29892007-01-19 07:51:42 +0000302 if (GV->hasExternalWeakLinkage())
Rafael Espindola15404d02006-12-18 03:37:18 +0000303 ExtWeakSymbols.insert(GV);
Evan Chenga8e29892007-01-19 07:51:42 +0000304 break;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000305 }
Evan Chenga8e29892007-01-19 07:51:42 +0000306 case MachineOperand::MO_ExternalSymbol: {
307 bool isCallOp = Modifier && !strcmp(Modifier, "call");
308 std::string Name(TAI->getGlobalPrefix());
309 Name += MO.getSymbolName();
Evan Cheng5be54b02007-01-19 19:25:36 +0000310 if (isCallOp && Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +0000311 TM.getRelocationModel() != Reloc::Static) {
312 O << TAI->getPrivateGlobalPrefix() << Name << "$stub";
313 FnStubs.insert(Name);
314 } else
315 O << Name;
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000316 if (isCallOp && Subtarget->isTargetELF() &&
317 TM.getRelocationModel() == Reloc::PIC_)
318 O << "(PLT)";
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000319 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000320 }
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000321 case MachineOperand::MO_ConstantPoolIndex:
Evan Cheng347d39f2007-10-14 05:57:21 +0000322 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
323 << '_' << MO.getConstantPoolIndex();
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000324 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000325 case MachineOperand::MO_JumpTableIndex:
Evan Cheng347d39f2007-10-14 05:57:21 +0000326 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
327 << '_' << MO.getJumpTableIndex();
Evan Chenga8e29892007-01-19 07:51:42 +0000328 break;
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000329 default:
330 O << "<unknown operand type>"; abort (); break;
331 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000332}
333
Evan Chengc70d1842007-03-20 08:11:30 +0000334static void printSOImm(std::ostream &O, int64_t V, const TargetAsmInfo *TAI) {
335 assert(V < (1 << 12) && "Not a valid so_imm value!");
336 unsigned Imm = ARM_AM::getSOImmValImm(V);
337 unsigned Rot = ARM_AM::getSOImmValRot(V);
Evan Chenga8e29892007-01-19 07:51:42 +0000338
339 // Print low-level immediate formation info, per
340 // A5.1.3: "Data-processing operands - Immediate".
341 if (Rot) {
342 O << "#" << Imm << ", " << Rot;
343 // Pretty printed version.
344 O << ' ' << TAI->getCommentString() << ' ' << (int)ARM_AM::rotr32(Imm, Rot);
345 } else {
346 O << "#" << Imm;
347 }
348}
349
Evan Chengc70d1842007-03-20 08:11:30 +0000350/// printSOImmOperand - SOImm is 4-bit rotate amount in bits 8-11 with 8-bit
351/// immediate in bits 0-7.
352void ARMAsmPrinter::printSOImmOperand(const MachineInstr *MI, int OpNum) {
353 const MachineOperand &MO = MI->getOperand(OpNum);
354 assert(MO.isImmediate() && "Not a valid so_imm value!");
355 printSOImm(O, MO.getImmedValue(), TAI);
356}
357
358/// printSOImm2PartOperand - SOImm is broken into two pieces using a mov
359/// followed by a or to materialize.
360void ARMAsmPrinter::printSOImm2PartOperand(const MachineInstr *MI, int OpNum) {
361 const MachineOperand &MO = MI->getOperand(OpNum);
362 assert(MO.isImmediate() && "Not a valid so_imm value!");
363 unsigned V1 = ARM_AM::getSOImmTwoPartFirst(MO.getImmedValue());
364 unsigned V2 = ARM_AM::getSOImmTwoPartSecond(MO.getImmedValue());
365 printSOImm(O, ARM_AM::getSOImmVal(V1), TAI);
Evan Cheng5e148a32007-06-05 18:55:18 +0000366 O << "\n\torr";
367 printPredicateOperand(MI, 2);
368 O << " ";
Evan Chengc70d1842007-03-20 08:11:30 +0000369 printOperand(MI, 0);
370 O << ", ";
371 printOperand(MI, 0);
372 O << ", ";
373 printSOImm(O, ARM_AM::getSOImmVal(V2), TAI);
374}
375
Evan Chenga8e29892007-01-19 07:51:42 +0000376// so_reg is a 4-operand unit corresponding to register forms of the A5.1
377// "Addressing Mode 1 - Data-processing operands" forms. This includes:
378// REG 0 0 - e.g. R5
379// REG REG 0,SH_OPC - e.g. R5, ROR R3
380// REG 0 IMM,SH_OPC - e.g. R5, LSL #3
381void ARMAsmPrinter::printSORegOperand(const MachineInstr *MI, int Op) {
382 const MachineOperand &MO1 = MI->getOperand(Op);
383 const MachineOperand &MO2 = MI->getOperand(Op+1);
384 const MachineOperand &MO3 = MI->getOperand(Op+2);
385
386 assert(MRegisterInfo::isPhysicalRegister(MO1.getReg()));
387 O << TM.getRegisterInfo()->get(MO1.getReg()).Name;
388
389 // Print the shift opc.
390 O << ", "
391 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(MO3.getImmedValue()))
392 << " ";
393
394 if (MO2.getReg()) {
395 assert(MRegisterInfo::isPhysicalRegister(MO2.getReg()));
396 O << TM.getRegisterInfo()->get(MO2.getReg()).Name;
397 assert(ARM_AM::getSORegOffset(MO3.getImm()) == 0);
398 } else {
399 O << "#" << ARM_AM::getSORegOffset(MO3.getImm());
400 }
401}
402
403void ARMAsmPrinter::printAddrMode2Operand(const MachineInstr *MI, int Op) {
404 const MachineOperand &MO1 = MI->getOperand(Op);
405 const MachineOperand &MO2 = MI->getOperand(Op+1);
406 const MachineOperand &MO3 = MI->getOperand(Op+2);
407
408 if (!MO1.isRegister()) { // FIXME: This is for CP entries, but isn't right.
409 printOperand(MI, Op);
410 return;
411 }
412
413 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
414
415 if (!MO2.getReg()) {
416 if (ARM_AM::getAM2Offset(MO3.getImm())) // Don't print +0.
417 O << ", #"
418 << (char)ARM_AM::getAM2Op(MO3.getImm())
419 << ARM_AM::getAM2Offset(MO3.getImm());
420 O << "]";
421 return;
422 }
423
424 O << ", "
425 << (char)ARM_AM::getAM2Op(MO3.getImm())
426 << TM.getRegisterInfo()->get(MO2.getReg()).Name;
427
428 if (unsigned ShImm = ARM_AM::getAM2Offset(MO3.getImm()))
429 O << ", "
430 << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO3.getImmedValue()))
431 << " #" << ShImm;
432 O << "]";
433}
434
435void ARMAsmPrinter::printAddrMode2OffsetOperand(const MachineInstr *MI, int Op){
436 const MachineOperand &MO1 = MI->getOperand(Op);
437 const MachineOperand &MO2 = MI->getOperand(Op+1);
438
439 if (!MO1.getReg()) {
Evan Chengbdc98692007-05-03 23:30:36 +0000440 unsigned ImmOffs = ARM_AM::getAM2Offset(MO2.getImm());
441 assert(ImmOffs && "Malformed indexed load / store!");
442 O << "#"
443 << (char)ARM_AM::getAM2Op(MO2.getImm())
444 << ImmOffs;
Evan Chenga8e29892007-01-19 07:51:42 +0000445 return;
446 }
447
448 O << (char)ARM_AM::getAM2Op(MO2.getImm())
449 << TM.getRegisterInfo()->get(MO1.getReg()).Name;
450
451 if (unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm()))
452 O << ", "
453 << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO2.getImmedValue()))
454 << " #" << ShImm;
455}
456
457void ARMAsmPrinter::printAddrMode3Operand(const MachineInstr *MI, int Op) {
458 const MachineOperand &MO1 = MI->getOperand(Op);
459 const MachineOperand &MO2 = MI->getOperand(Op+1);
460 const MachineOperand &MO3 = MI->getOperand(Op+2);
461
462 assert(MRegisterInfo::isPhysicalRegister(MO1.getReg()));
463 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
464
465 if (MO2.getReg()) {
466 O << ", "
467 << (char)ARM_AM::getAM3Op(MO3.getImm())
468 << TM.getRegisterInfo()->get(MO2.getReg()).Name
469 << "]";
470 return;
471 }
472
473 if (unsigned ImmOffs = ARM_AM::getAM3Offset(MO3.getImm()))
474 O << ", #"
475 << (char)ARM_AM::getAM3Op(MO3.getImm())
476 << ImmOffs;
477 O << "]";
478}
479
480void ARMAsmPrinter::printAddrMode3OffsetOperand(const MachineInstr *MI, int Op){
481 const MachineOperand &MO1 = MI->getOperand(Op);
482 const MachineOperand &MO2 = MI->getOperand(Op+1);
483
484 if (MO1.getReg()) {
485 O << (char)ARM_AM::getAM3Op(MO2.getImm())
486 << TM.getRegisterInfo()->get(MO1.getReg()).Name;
487 return;
488 }
489
490 unsigned ImmOffs = ARM_AM::getAM3Offset(MO2.getImm());
Evan Chengbdc98692007-05-03 23:30:36 +0000491 assert(ImmOffs && "Malformed indexed load / store!");
Evan Chenga8e29892007-01-19 07:51:42 +0000492 O << "#"
Evan Chengbdc98692007-05-03 23:30:36 +0000493 << (char)ARM_AM::getAM3Op(MO2.getImm())
Evan Chenga8e29892007-01-19 07:51:42 +0000494 << ImmOffs;
495}
496
497void ARMAsmPrinter::printAddrMode4Operand(const MachineInstr *MI, int Op,
498 const char *Modifier) {
499 const MachineOperand &MO1 = MI->getOperand(Op);
500 const MachineOperand &MO2 = MI->getOperand(Op+1);
501 ARM_AM::AMSubMode Mode = ARM_AM::getAM4SubMode(MO2.getImm());
502 if (Modifier && strcmp(Modifier, "submode") == 0) {
503 if (MO1.getReg() == ARM::SP) {
504 bool isLDM = (MI->getOpcode() == ARM::LDM ||
505 MI->getOpcode() == ARM::LDM_RET);
506 O << ARM_AM::getAMSubModeAltStr(Mode, isLDM);
507 } else
508 O << ARM_AM::getAMSubModeStr(Mode);
509 } else {
510 printOperand(MI, Op);
511 if (ARM_AM::getAM4WBFlag(MO2.getImm()))
512 O << "!";
513 }
514}
515
516void ARMAsmPrinter::printAddrMode5Operand(const MachineInstr *MI, int Op,
517 const char *Modifier) {
518 const MachineOperand &MO1 = MI->getOperand(Op);
519 const MachineOperand &MO2 = MI->getOperand(Op+1);
520
521 if (!MO1.isRegister()) { // FIXME: This is for CP entries, but isn't right.
522 printOperand(MI, Op);
523 return;
524 }
525
526 assert(MRegisterInfo::isPhysicalRegister(MO1.getReg()));
527
528 if (Modifier && strcmp(Modifier, "submode") == 0) {
529 ARM_AM::AMSubMode Mode = ARM_AM::getAM5SubMode(MO2.getImm());
530 if (MO1.getReg() == ARM::SP) {
531 bool isFLDM = (MI->getOpcode() == ARM::FLDMD ||
532 MI->getOpcode() == ARM::FLDMS);
533 O << ARM_AM::getAMSubModeAltStr(Mode, isFLDM);
534 } else
535 O << ARM_AM::getAMSubModeStr(Mode);
536 return;
537 } else if (Modifier && strcmp(Modifier, "base") == 0) {
538 // Used for FSTM{D|S} and LSTM{D|S} operations.
539 O << TM.getRegisterInfo()->get(MO1.getReg()).Name;
540 if (ARM_AM::getAM5WBFlag(MO2.getImm()))
541 O << "!";
542 return;
543 }
544
545 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
546
547 if (unsigned ImmOffs = ARM_AM::getAM5Offset(MO2.getImm())) {
548 O << ", #"
549 << (char)ARM_AM::getAM5Op(MO2.getImm())
550 << ImmOffs*4;
551 }
552 O << "]";
553}
554
555void ARMAsmPrinter::printAddrModePCOperand(const MachineInstr *MI, int Op,
556 const char *Modifier) {
557 if (Modifier && strcmp(Modifier, "label") == 0) {
558 printPCLabel(MI, Op+1);
559 return;
560 }
561
562 const MachineOperand &MO1 = MI->getOperand(Op);
563 assert(MRegisterInfo::isPhysicalRegister(MO1.getReg()));
564 O << "[pc, +" << TM.getRegisterInfo()->get(MO1.getReg()).Name << "]";
565}
566
567void
568ARMAsmPrinter::printThumbAddrModeRROperand(const MachineInstr *MI, int Op) {
569 const MachineOperand &MO1 = MI->getOperand(Op);
570 const MachineOperand &MO2 = MI->getOperand(Op+1);
571 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
572 O << ", " << TM.getRegisterInfo()->get(MO2.getReg()).Name << "]";
573}
574
575void
576ARMAsmPrinter::printThumbAddrModeRI5Operand(const MachineInstr *MI, int Op,
577 unsigned Scale) {
578 const MachineOperand &MO1 = MI->getOperand(Op);
Evan Chengcea117d2007-01-30 02:35:32 +0000579 const MachineOperand &MO2 = MI->getOperand(Op+1);
580 const MachineOperand &MO3 = MI->getOperand(Op+2);
Evan Chenga8e29892007-01-19 07:51:42 +0000581
582 if (!MO1.isRegister()) { // FIXME: This is for CP entries, but isn't right.
583 printOperand(MI, Op);
584 return;
585 }
586
587 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
Evan Chengcea117d2007-01-30 02:35:32 +0000588 if (MO3.getReg())
589 O << ", " << TM.getRegisterInfo()->get(MO3.getReg()).Name;
590 else if (unsigned ImmOffs = MO2.getImm()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000591 O << ", #" << ImmOffs;
592 if (Scale > 1)
593 O << " * " << Scale;
594 }
595 O << "]";
596}
597
598void
Evan Chengc38f2bc2007-01-23 22:59:13 +0000599ARMAsmPrinter::printThumbAddrModeS1Operand(const MachineInstr *MI, int Op) {
Evan Chengcea117d2007-01-30 02:35:32 +0000600 printThumbAddrModeRI5Operand(MI, Op, 1);
Evan Chenga8e29892007-01-19 07:51:42 +0000601}
602void
Evan Chengc38f2bc2007-01-23 22:59:13 +0000603ARMAsmPrinter::printThumbAddrModeS2Operand(const MachineInstr *MI, int Op) {
Evan Chengcea117d2007-01-30 02:35:32 +0000604 printThumbAddrModeRI5Operand(MI, Op, 2);
Evan Chenga8e29892007-01-19 07:51:42 +0000605}
606void
Evan Chengc38f2bc2007-01-23 22:59:13 +0000607ARMAsmPrinter::printThumbAddrModeS4Operand(const MachineInstr *MI, int Op) {
Evan Chengcea117d2007-01-30 02:35:32 +0000608 printThumbAddrModeRI5Operand(MI, Op, 4);
Evan Chenga8e29892007-01-19 07:51:42 +0000609}
610
611void ARMAsmPrinter::printThumbAddrModeSPOperand(const MachineInstr *MI,int Op) {
612 const MachineOperand &MO1 = MI->getOperand(Op);
613 const MachineOperand &MO2 = MI->getOperand(Op+1);
614 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
615 if (unsigned ImmOffs = MO2.getImm())
616 O << ", #" << ImmOffs << " * 4";
617 O << "]";
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000618}
619
Evan Cheng42d712b2007-05-08 21:08:43 +0000620void ARMAsmPrinter::printPredicateOperand(const MachineInstr *MI, int opNum) {
Evan Cheng44bec522007-05-15 01:29:07 +0000621 ARMCC::CondCodes CC = (ARMCC::CondCodes)MI->getOperand(opNum).getImmedValue();
622 if (CC != ARMCC::AL)
623 O << ARMCondCodeToString(CC);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000624}
625
Evan Chengdfb2eba2007-07-06 01:01:34 +0000626void ARMAsmPrinter::printSBitModifierOperand(const MachineInstr *MI, int opNum){
627 unsigned Reg = MI->getOperand(opNum).getReg();
628 if (Reg) {
629 assert(Reg == ARM::CPSR && "Expect ARM CPSR register!");
630 O << 's';
631 }
632}
633
Evan Chenga8e29892007-01-19 07:51:42 +0000634void ARMAsmPrinter::printPCLabel(const MachineInstr *MI, int opNum) {
635 int Id = (int)MI->getOperand(opNum).getImmedValue();
636 O << TAI->getPrivateGlobalPrefix() << "PC" << Id;
637}
638
639void ARMAsmPrinter::printRegisterList(const MachineInstr *MI, int opNum) {
640 O << "{";
641 for (unsigned i = opNum, e = MI->getNumOperands(); i != e; ++i) {
642 printOperand(MI, i);
643 if (i != e-1) O << ", ";
644 }
645 O << "}";
646}
647
648void ARMAsmPrinter::printCPInstOperand(const MachineInstr *MI, int OpNo,
649 const char *Modifier) {
650 assert(Modifier && "This operand only works with a modifier!");
651 // There are two aspects to a CONSTANTPOOL_ENTRY operand, the label and the
652 // data itself.
653 if (!strcmp(Modifier, "label")) {
654 unsigned ID = MI->getOperand(OpNo).getImm();
Evan Cheng347d39f2007-10-14 05:57:21 +0000655 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
656 << '_' << ID << ":\n";
Evan Chenga8e29892007-01-19 07:51:42 +0000657 } else {
658 assert(!strcmp(Modifier, "cpentry") && "Unknown modifier for CPE");
659 unsigned CPI = MI->getOperand(OpNo).getConstantPoolIndex();
660
661 const MachineConstantPoolEntry &MCPE = // Chasing pointers is fun?
662 MI->getParent()->getParent()->getConstantPool()->getConstants()[CPI];
663
664 if (MCPE.isMachineConstantPoolEntry())
665 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
Lauro Ramos Venancio305b8a52007-04-25 14:50:40 +0000666 else {
Evan Chenga8e29892007-01-19 07:51:42 +0000667 EmitGlobalConstant(MCPE.Val.ConstVal);
Lauro Ramos Venancio305b8a52007-04-25 14:50:40 +0000668 // remember to emit the weak reference
669 if (const GlobalValue *GV = dyn_cast<GlobalValue>(MCPE.Val.ConstVal))
670 if (GV->hasExternalWeakLinkage())
671 ExtWeakSymbols.insert(GV);
672 }
Evan Chenga8e29892007-01-19 07:51:42 +0000673 }
674}
675
676void ARMAsmPrinter::printJTBlockOperand(const MachineInstr *MI, int OpNo) {
677 const MachineOperand &MO1 = MI->getOperand(OpNo);
678 const MachineOperand &MO2 = MI->getOperand(OpNo+1); // Unique Id
679 unsigned JTI = MO1.getJumpTableIndex();
Evan Cheng347d39f2007-10-14 05:57:21 +0000680 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
681 << '_' << JTI << '_' << MO2.getImmedValue() << ":\n";
Evan Chenga8e29892007-01-19 07:51:42 +0000682
683 const char *JTEntryDirective = TAI->getJumpTableDirective();
684 if (!JTEntryDirective)
685 JTEntryDirective = TAI->getData32bitsDirective();
686
687 const MachineFunction *MF = MI->getParent()->getParent();
688 MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
689 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
690 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
691 bool UseSet= TAI->getSetDirective() && TM.getRelocationModel() == Reloc::PIC_;
692 std::set<MachineBasicBlock*> JTSets;
693 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
694 MachineBasicBlock *MBB = JTBBs[i];
695 if (UseSet && JTSets.insert(MBB).second)
696 printSetLabel(JTI, MO2.getImmedValue(), MBB);
697
698 O << JTEntryDirective << ' ';
699 if (UseSet)
Evan Cheng347d39f2007-10-14 05:57:21 +0000700 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
701 << '_' << JTI << '_' << MO2.getImmedValue()
702 << "_set_" << MBB->getNumber();
Evan Chenga8e29892007-01-19 07:51:42 +0000703 else if (TM.getRelocationModel() == Reloc::PIC_) {
704 printBasicBlockLabel(MBB, false, false);
705 // If the arch uses custom Jump Table directives, don't calc relative to JT
706 if (!TAI->getJumpTableDirective())
707 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
Evan Cheng347d39f2007-10-14 05:57:21 +0000708 << getFunctionNumber() << '_' << JTI << '_' << MO2.getImmedValue();
Evan Chenga8e29892007-01-19 07:51:42 +0000709 } else
710 printBasicBlockLabel(MBB, false, false);
Evan Chengd85ac4d2007-01-27 02:29:45 +0000711 if (i != e-1)
712 O << '\n';
Evan Chenga8e29892007-01-19 07:51:42 +0000713 }
714}
715
716
717bool ARMAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
718 unsigned AsmVariant, const char *ExtraCode){
719 // Does this asm operand have a single letter operand modifier?
720 if (ExtraCode && ExtraCode[0]) {
721 if (ExtraCode[1] != 0) return true; // Unknown modifier.
722
723 switch (ExtraCode[0]) {
724 default: return true; // Unknown modifier.
Evan Cheng23a95702007-03-08 22:42:46 +0000725 case 'c': // Don't print "$" before a global var name or constant.
Evan Chenge21e3962007-04-04 00:13:29 +0000726 case 'P': // Print a VFP double precision register.
Evan Cheng23a95702007-03-08 22:42:46 +0000727 printOperand(MI, OpNo);
728 return false;
Evan Chenga8e29892007-01-19 07:51:42 +0000729 case 'Q':
730 if (TM.getTargetData()->isLittleEndian())
731 break;
732 // Fallthrough
733 case 'R':
734 if (TM.getTargetData()->isBigEndian())
735 break;
736 // Fallthrough
737 case 'H': // Write second word of DI / DF reference.
738 // Verify that this operand has two consecutive registers.
739 if (!MI->getOperand(OpNo).isRegister() ||
740 OpNo+1 == MI->getNumOperands() ||
741 !MI->getOperand(OpNo+1).isRegister())
742 return true;
743 ++OpNo; // Return the high-part.
744 }
745 }
746
747 printOperand(MI, OpNo);
748 return false;
749}
750
751void ARMAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
752 ++EmittedInsts;
753
Evan Chengc60e76d2007-01-30 20:37:08 +0000754 int Opc = MI->getOpcode();
755 switch (Opc) {
756 case ARM::CONSTPOOL_ENTRY:
Evan Chenga8e29892007-01-19 07:51:42 +0000757 if (!InCPMode && AFI->isThumbFunction()) {
758 EmitAlignment(2);
759 InCPMode = true;
760 }
Evan Chengc60e76d2007-01-30 20:37:08 +0000761 break;
762 default: {
Evan Cheng3bf12d02007-01-31 23:39:39 +0000763 if (InCPMode && AFI->isThumbFunction())
Evan Chenga8e29892007-01-19 07:51:42 +0000764 InCPMode = false;
Evan Chengc60e76d2007-01-30 20:37:08 +0000765 switch (Opc) {
766 case ARM::PICADD:
767 case ARM::PICLD:
Evan Cheng341dccc2007-06-05 07:36:38 +0000768 case ARM::PICLDZH:
769 case ARM::PICLDZB:
770 case ARM::PICLDH:
771 case ARM::PICLDB:
772 case ARM::PICLDSH:
773 case ARM::PICLDSB:
774 case ARM::PICSTR:
775 case ARM::PICSTRH:
776 case ARM::PICSTRB:
Evan Chengc60e76d2007-01-30 20:37:08 +0000777 case ARM::tPICADD:
778 break;
779 default:
780 O << "\t";
781 break;
782 }
783 }}
Evan Chenga8e29892007-01-19 07:51:42 +0000784
785 // Call the autogenerated instruction printer routines.
786 printInstruction(MI);
787}
788
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000789bool ARMAsmPrinter::doInitialization(Module &M) {
Lauro Ramos Venancioe8e54952007-05-03 20:28:35 +0000790 // Emit initial debug information.
791 DW.BeginModule(&M);
Evan Chenga8e29892007-01-19 07:51:42 +0000792
Dan Gohmanb8275a32007-07-25 19:33:14 +0000793 bool Result = AsmPrinter::doInitialization(M);
Dale Johannesen54118352007-06-22 00:54:56 +0000794
795 // Darwin wants symbols to be quoted if they have complex names.
796 if (Subtarget->isTargetDarwin())
797 Mang->setUseQuotes(true);
798
Dan Gohmanb8275a32007-07-25 19:33:14 +0000799 return Result;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000800}
801
802bool ARMAsmPrinter::doFinalization(Module &M) {
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000803 const TargetData *TD = TM.getTargetData();
804
805 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
806 I != E; ++I) {
807 if (!I->hasInitializer()) // External global require no code
808 continue;
809
Evan Chengb267ca12007-01-30 08:04:53 +0000810 if (EmitSpecialLLVMGlobal(I)) {
811 if (Subtarget->isTargetDarwin() &&
812 TM.getRelocationModel() == Reloc::Static) {
813 if (I->getName() == "llvm.global_ctors")
814 O << ".reference .constructors_used\n";
815 else if (I->getName() == "llvm.global_dtors")
816 O << ".reference .destructors_used\n";
817 }
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000818 continue;
Evan Chengb267ca12007-01-30 08:04:53 +0000819 }
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000820
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000821 std::string name = Mang->getValueName(I);
822 Constant *C = I->getInitializer();
Evan Cheng98ded762007-03-08 01:25:25 +0000823 const Type *Type = C->getType();
Duncan Sandsca0ed742007-11-05 00:04:43 +0000824 unsigned Size = TD->getABITypeSize(Type);
Evan Chenga8e29892007-01-19 07:51:42 +0000825 unsigned Align = TD->getPreferredAlignmentLog(I);
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000826
Lauro Ramos Venancio0a181732007-04-30 00:23:51 +0000827 const char *VisibilityDirective = NULL;
Evan Cheng5be54b02007-01-19 19:25:36 +0000828 if (I->hasHiddenVisibility())
Lauro Ramos Venancio0a181732007-04-30 00:23:51 +0000829 VisibilityDirective = TAI->getHiddenDirective();
830 else if (I->hasProtectedVisibility())
831 VisibilityDirective = TAI->getProtectedDirective();
832
833 if (VisibilityDirective)
834 O << VisibilityDirective << name << "\n";
835
Evan Cheng5be54b02007-01-19 19:25:36 +0000836 if (Subtarget->isTargetELF())
Lauro Ramos Venanciob1cc0522007-01-25 20:11:04 +0000837 O << "\t.type " << name << ",%object\n";
Evan Cheng5be54b02007-01-19 19:25:36 +0000838
Evan Cheng76a40232007-09-21 00:41:19 +0000839 if (C->isNullValue() && !I->hasSection()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000840 if (I->hasExternalLinkage()) {
Evan Cheng5be54b02007-01-19 19:25:36 +0000841 if (const char *Directive = TAI->getZeroFillDirective()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000842 O << "\t.globl\t" << name << "\n";
Evan Cheng5be54b02007-01-19 19:25:36 +0000843 O << Directive << "__DATA__, __common, " << name << ", "
844 << Size << ", " << Align << "\n";
845 continue;
846 }
847 }
848
Evan Cheng76a40232007-09-21 00:41:19 +0000849 if (I->hasInternalLinkage() || I->hasWeakLinkage() ||
850 I->hasLinkOnceLinkage()) {
Evan Cheng5be54b02007-01-19 19:25:36 +0000851 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
852 if (!NoZerosInBSS && TAI->getBSSSection())
853 SwitchToDataSection(TAI->getBSSSection(), I);
854 else
855 SwitchToDataSection(TAI->getDataSection(), I);
Evan Chenga8e29892007-01-19 07:51:42 +0000856 if (TAI->getLCOMMDirective() != NULL) {
857 if (I->hasInternalLinkage()) {
858 O << TAI->getLCOMMDirective() << name << "," << Size;
Evan Cheng5be54b02007-01-19 19:25:36 +0000859 if (Subtarget->isTargetDarwin())
Evan Chenga8e29892007-01-19 07:51:42 +0000860 O << "," << Align;
861 } else
862 O << TAI->getCOMMDirective() << name << "," << Size;
863 } else {
864 if (I->hasInternalLinkage())
865 O << "\t.local\t" << name << "\n";
866 O << TAI->getCOMMDirective() << name << "," << Size;
867 if (TAI->getCOMMDirectiveTakesAlignment())
868 O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
869 }
Evan Cheng5be54b02007-01-19 19:25:36 +0000870 O << "\t\t" << TAI->getCommentString() << " " << I->getName() << "\n";
871 continue;
Evan Chenga8e29892007-01-19 07:51:42 +0000872 }
Evan Cheng5be54b02007-01-19 19:25:36 +0000873 }
Evan Chenga8e29892007-01-19 07:51:42 +0000874
Evan Cheng5be54b02007-01-19 19:25:36 +0000875 switch (I->getLinkage()) {
876 case GlobalValue::LinkOnceLinkage:
877 case GlobalValue::WeakLinkage:
878 if (Subtarget->isTargetDarwin()) {
879 O << "\t.globl " << name << "\n"
880 << "\t.weak_definition " << name << "\n";
881 SwitchToDataSection("\t.section __DATA,__const_coal,coalesced", I);
882 } else {
883 std::string SectionName("\t.section\t.llvm.linkonce.d." +
884 name +
885 ",\"aw\",%progbits");
886 SwitchToDataSection(SectionName.c_str(), I);
887 O << "\t.weak " << name << "\n";
888 }
889 break;
890 case GlobalValue::AppendingLinkage:
891 // FIXME: appending linkage variables should go into a section of
892 // their name or something. For now, just emit them as external.
893 case GlobalValue::ExternalLinkage:
894 O << "\t.globl " << name << "\n";
895 // FALL THROUGH
896 case GlobalValue::InternalLinkage: {
897 if (I->isConstant()) {
898 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
899 if (TAI->getCStringSection() && CVA && CVA->isCString()) {
900 SwitchToDataSection(TAI->getCStringSection(), I);
901 break;
902 }
903 }
904 // FIXME: special handling for ".ctors" & ".dtors" sections
905 if (I->hasSection() &&
906 (I->getSection() == ".ctors" ||
907 I->getSection() == ".dtors")) {
908 assert(!Subtarget->isTargetDarwin());
909 std::string SectionName = ".section " + I->getSection();
910 SectionName += ",\"aw\",%progbits";
911 SwitchToDataSection(SectionName.c_str());
912 } else {
913 if (C->isNullValue() && !NoZerosInBSS && TAI->getBSSSection())
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000914 SwitchToDataSection(I->isThreadLocal() ? TAI->getTLSBSSSection() :
915 TAI->getBSSSection(), I);
Evan Cheng98ded762007-03-08 01:25:25 +0000916 else if (!I->isConstant())
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000917 SwitchToDataSection(I->isThreadLocal() ? TAI->getTLSDataSection() :
918 TAI->getDataSection(), I);
919 else if (I->isThreadLocal())
920 SwitchToDataSection(TAI->getTLSDataSection());
Evan Cheng98ded762007-03-08 01:25:25 +0000921 else {
922 // Read-only data.
Evan Cheng032953d2007-03-08 08:31:54 +0000923 bool HasReloc = C->ContainsRelocations();
924 if (HasReloc &&
925 Subtarget->isTargetDarwin() &&
Evan Cheng98ded762007-03-08 01:25:25 +0000926 TM.getRelocationModel() != Reloc::Static)
927 SwitchToDataSection("\t.const_data\n");
Evan Cheng032953d2007-03-08 08:31:54 +0000928 else if (!HasReloc && Size == 4 &&
Evan Cheng98ded762007-03-08 01:25:25 +0000929 TAI->getFourByteConstantSection())
930 SwitchToDataSection(TAI->getFourByteConstantSection(), I);
Evan Cheng032953d2007-03-08 08:31:54 +0000931 else if (!HasReloc && Size == 8 &&
Evan Cheng98ded762007-03-08 01:25:25 +0000932 TAI->getEightByteConstantSection())
933 SwitchToDataSection(TAI->getEightByteConstantSection(), I);
Evan Cheng032953d2007-03-08 08:31:54 +0000934 else if (!HasReloc && Size == 16 &&
Evan Cheng98ded762007-03-08 01:25:25 +0000935 TAI->getSixteenByteConstantSection())
936 SwitchToDataSection(TAI->getSixteenByteConstantSection(), I);
937 else if (TAI->getReadOnlySection())
938 SwitchToDataSection(TAI->getReadOnlySection(), I);
939 else
940 SwitchToDataSection(TAI->getDataSection(), I);
941 }
Rafael Espindolab97809c2006-10-19 13:30:40 +0000942 }
Evan Cheng5be54b02007-01-19 19:25:36 +0000943
944 break;
945 }
946 default:
947 assert(0 && "Unknown linkage type!");
948 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000949 }
Rafael Espindolab97809c2006-10-19 13:30:40 +0000950
Evan Chenga8e29892007-01-19 07:51:42 +0000951 EmitAlignment(Align, I);
Evan Cheng5be54b02007-01-19 19:25:36 +0000952 O << name << ":\t\t\t\t" << TAI->getCommentString() << " " << I->getName()
953 << "\n";
954 if (TAI->hasDotTypeDotSizeDirective())
Rafael Espindolab97809c2006-10-19 13:30:40 +0000955 O << "\t.size " << name << ", " << Size << "\n";
Evan Chenga8e29892007-01-19 07:51:42 +0000956 // If the initializer is a extern weak symbol, remember to emit the weak
957 // reference!
958 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
959 if (GV->hasExternalWeakLinkage())
960 ExtWeakSymbols.insert(GV);
961
962 EmitGlobalConstant(C);
963 O << '\n';
964 }
965
Evan Cheng5be54b02007-01-19 19:25:36 +0000966 if (Subtarget->isTargetDarwin()) {
967 SwitchToDataSection("");
968
Evan Chenga8e29892007-01-19 07:51:42 +0000969 // Output stubs for dynamically-linked functions
970 unsigned j = 1;
971 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
972 i != e; ++i, ++j) {
973 if (TM.getRelocationModel() == Reloc::PIC_)
974 SwitchToTextSection(".section __TEXT,__picsymbolstub4,symbol_stubs,"
975 "none,16", 0);
976 else
977 SwitchToTextSection(".section __TEXT,__symbol_stub4,symbol_stubs,"
978 "none,12", 0);
979
980 EmitAlignment(2);
981 O << "\t.code\t32\n";
982
983 O << "L" << *i << "$stub:\n";
984 O << "\t.indirect_symbol " << *i << "\n";
985 O << "\tldr ip, L" << *i << "$slp\n";
986 if (TM.getRelocationModel() == Reloc::PIC_) {
987 O << "L" << *i << "$scv:\n";
988 O << "\tadd ip, pc, ip\n";
989 }
990 O << "\tldr pc, [ip, #0]\n";
991 O << "L" << *i << "$slp:\n";
992 if (TM.getRelocationModel() == Reloc::PIC_)
993 O << "\t.long\tL" << *i << "$lazy_ptr-(L" << *i << "$scv+8)\n";
994 else
995 O << "\t.long\tL" << *i << "$lazy_ptr\n";
996 SwitchToDataSection(".lazy_symbol_pointer", 0);
997 O << "L" << *i << "$lazy_ptr:\n";
998 O << "\t.indirect_symbol " << *i << "\n";
999 O << "\t.long\tdyld_stub_binding_helper\n";
1000 }
1001 O << "\n";
1002
1003 // Output non-lazy-pointers for external and common global variables.
Dan Gohmancb406c22007-10-03 19:26:29 +00001004 if (!GVNonLazyPtrs.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001005 SwitchToDataSection(".non_lazy_symbol_pointer", 0);
1006 for (std::set<std::string>::iterator i = GVNonLazyPtrs.begin(),
1007 e = GVNonLazyPtrs.end(); i != e; ++i) {
1008 O << "L" << *i << "$non_lazy_ptr:\n";
1009 O << "\t.indirect_symbol " << *i << "\n";
1010 O << "\t.long\t0\n";
1011 }
1012
1013 // Emit initial debug information.
1014 DW.EndModule();
1015
1016 // Funny Darwin hack: This flag tells the linker that no global symbols
1017 // contain code that falls through to other global symbols (e.g. the obvious
1018 // implementation of multiple entry points). If this doesn't occur, the
1019 // linker can safely perform dead code stripping. Since LLVM never
1020 // generates code that does this, it is always safe to set.
1021 O << "\t.subsections_via_symbols\n";
Lauro Ramos Venancioe8e54952007-05-03 20:28:35 +00001022 } else {
1023 // Emit final debug information for ELF.
1024 DW.EndModule();
Rafael Espindolab01c4bb2006-07-27 11:38:51 +00001025 }
Rafael Espindolab97809c2006-10-19 13:30:40 +00001026
Dan Gohmanb8275a32007-07-25 19:33:14 +00001027 return AsmPrinter::doFinalization(M);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00001028}