blob: 6822e632824a1c383df3e5affbdfa18adb39465d [file] [log] [blame]
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00001//===-- ARMAsmPrinter.cpp - ARM LLVM assembly writer ----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the "Instituto Nokia de Tecnologia" and
6// is distributed under the University of Illinois Open Source
7// License. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10//
11// This file contains a printer that converts from our internal representation
12// of machine-dependent LLVM code to GAS-format ARM assembly language.
13//
14//===----------------------------------------------------------------------===//
15
Chris Lattner95b2c7d2006-12-19 22:59:26 +000016#define DEBUG_TYPE "asm-printer"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000017#include "ARM.h"
Evan Chenga8e29892007-01-19 07:51:42 +000018#include "ARMTargetMachine.h"
19#include "ARMAddressingModes.h"
20#include "ARMConstantPoolValue.h"
21#include "ARMMachineFunctionInfo.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000022#include "llvm/Constants.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000023#include "llvm/Module.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000024#include "llvm/CodeGen/AsmPrinter.h"
Evan Chenga8e29892007-01-19 07:51:42 +000025#include "llvm/CodeGen/DwarfWriter.h"
Jim Laskey44c3b9f2007-01-26 21:22:28 +000026#include "llvm/CodeGen/MachineModuleInfo.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000027#include "llvm/CodeGen/MachineFunctionPass.h"
Evan Chenga8e29892007-01-19 07:51:42 +000028#include "llvm/CodeGen/MachineJumpTableInfo.h"
Jim Laskey563321a2006-09-06 18:34:40 +000029#include "llvm/Target/TargetAsmInfo.h"
Rafael Espindolab01c4bb2006-07-27 11:38:51 +000030#include "llvm/Target/TargetData.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000031#include "llvm/Target/TargetMachine.h"
Evan Cheng5be54b02007-01-19 19:25:36 +000032#include "llvm/Target/TargetOptions.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000033#include "llvm/ADT/Statistic.h"
34#include "llvm/ADT/StringExtras.h"
Evan Chenga8e29892007-01-19 07:51:42 +000035#include "llvm/Support/Compiler.h"
36#include "llvm/Support/Mangler.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000037#include "llvm/Support/MathExtras.h"
38#include <cctype>
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000039using namespace llvm;
40
Chris Lattner95b2c7d2006-12-19 22:59:26 +000041STATISTIC(EmittedInsts, "Number of machine instrs printed");
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000042
Chris Lattner95b2c7d2006-12-19 22:59:26 +000043namespace {
Jim Laskey563321a2006-09-06 18:34:40 +000044 struct VISIBILITY_HIDDEN ARMAsmPrinter : public AsmPrinter {
Jim Laskeya0f3d172006-09-07 22:06:40 +000045 ARMAsmPrinter(std::ostream &O, TargetMachine &TM, const TargetAsmInfo *T)
Evan Chenga8e29892007-01-19 07:51:42 +000046 : AsmPrinter(O, TM, T), DW(O, this, T), AFI(NULL), InCPMode(false) {
47 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Jim Laskey563321a2006-09-06 18:34:40 +000048 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000049
Evan Chenga8e29892007-01-19 07:51:42 +000050 DwarfWriter DW;
51
52 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
53 /// make the right decision when printing asm code for different targets.
54 const ARMSubtarget *Subtarget;
55
56 /// AFI - Keep a pointer to ARMFunctionInfo for the current
57 /// MachineFunction
58 ARMFunctionInfo *AFI;
59
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000060 /// We name each basic block in a Function with a unique number, so
61 /// that we can consistently refer to them later. This is cleared
62 /// at the beginning of each call to runOnMachineFunction().
63 ///
64 typedef std::map<const Value *, unsigned> ValueMapTy;
65 ValueMapTy NumberForBB;
66
Evan Chenga8e29892007-01-19 07:51:42 +000067 /// Keeps the set of GlobalValues that require non-lazy-pointers for
68 /// indirect access.
69 std::set<std::string> GVNonLazyPtrs;
70
71 /// Keeps the set of external function GlobalAddresses that the asm
72 /// printer should generate stubs for.
73 std::set<std::string> FnStubs;
74
75 /// True if asm printer is printing a series of CONSTPOOL_ENTRY.
76 bool InCPMode;
77
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000078 virtual const char *getPassName() const {
79 return "ARM Assembly Printer";
80 }
81
Evan Chenga8e29892007-01-19 07:51:42 +000082 void printOperand(const MachineInstr *MI, int opNum,
83 const char *Modifier = 0);
84 void printSOImmOperand(const MachineInstr *MI, int opNum);
Evan Chengc70d1842007-03-20 08:11:30 +000085 void printSOImm2PartOperand(const MachineInstr *MI, int opNum);
Evan Chenga8e29892007-01-19 07:51:42 +000086 void printSORegOperand(const MachineInstr *MI, int opNum);
87 void printAddrMode2Operand(const MachineInstr *MI, int OpNo);
88 void printAddrMode2OffsetOperand(const MachineInstr *MI, int OpNo);
89 void printAddrMode3Operand(const MachineInstr *MI, int OpNo);
90 void printAddrMode3OffsetOperand(const MachineInstr *MI, int OpNo);
91 void printAddrMode4Operand(const MachineInstr *MI, int OpNo,
92 const char *Modifier = 0);
93 void printAddrMode5Operand(const MachineInstr *MI, int OpNo,
94 const char *Modifier = 0);
95 void printAddrModePCOperand(const MachineInstr *MI, int OpNo,
96 const char *Modifier = 0);
97 void printThumbAddrModeRROperand(const MachineInstr *MI, int OpNo);
98 void printThumbAddrModeRI5Operand(const MachineInstr *MI, int OpNo,
99 unsigned Scale);
Evan Chengc38f2bc2007-01-23 22:59:13 +0000100 void printThumbAddrModeS1Operand(const MachineInstr *MI, int OpNo);
101 void printThumbAddrModeS2Operand(const MachineInstr *MI, int OpNo);
102 void printThumbAddrModeS4Operand(const MachineInstr *MI, int OpNo);
Evan Chenga8e29892007-01-19 07:51:42 +0000103 void printThumbAddrModeSPOperand(const MachineInstr *MI, int OpNo);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000104 void printCCOperand(const MachineInstr *MI, int opNum);
Evan Chenga8e29892007-01-19 07:51:42 +0000105 void printPCLabel(const MachineInstr *MI, int opNum);
106 void printRegisterList(const MachineInstr *MI, int opNum);
107 void printCPInstOperand(const MachineInstr *MI, int opNum,
108 const char *Modifier);
109 void printJTBlockOperand(const MachineInstr *MI, int opNum);
110
111 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
112 unsigned AsmVariant, const char *ExtraCode);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000113
114 bool printInstruction(const MachineInstr *MI); // autogenerated.
Evan Chenga8e29892007-01-19 07:51:42 +0000115 void printMachineInstruction(const MachineInstr *MI);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000116 bool runOnMachineFunction(MachineFunction &F);
117 bool doInitialization(Module &M);
118 bool doFinalization(Module &M);
Evan Chenga8e29892007-01-19 07:51:42 +0000119
120 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
121 printDataDirective(MCPV->getType());
122
123 ARMConstantPoolValue *ACPV = (ARMConstantPoolValue*)MCPV;
Lauro Ramos Venancio1a92d942007-01-26 19:51:32 +0000124 GlobalValue *GV = ACPV->getGV();
Evan Chengc60e76d2007-01-30 20:37:08 +0000125 std::string Name = GV ? Mang->getValueName(GV) : TAI->getGlobalPrefix();
126 if (!GV)
127 Name += ACPV->getSymbol();
Evan Chenga8e29892007-01-19 07:51:42 +0000128 if (ACPV->isNonLazyPointer()) {
129 GVNonLazyPtrs.insert(Name);
130 O << TAI->getPrivateGlobalPrefix() << Name << "$non_lazy_ptr";
Evan Chengc60e76d2007-01-30 20:37:08 +0000131 } else if (ACPV->isStub()) {
132 FnStubs.insert(Name);
133 O << TAI->getPrivateGlobalPrefix() << Name << "$stub";
Evan Chenga8e29892007-01-19 07:51:42 +0000134 } else
135 O << Name;
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000136 if (ACPV->hasModifier()) O << "(" << ACPV->getModifier() << ")";
Evan Chenga8e29892007-01-19 07:51:42 +0000137 if (ACPV->getPCAdjustment() != 0)
138 O << "-(" << TAI->getPrivateGlobalPrefix() << "PC"
139 << utostr(ACPV->getLabelId())
140 << "+" << (unsigned)ACPV->getPCAdjustment() << ")";
141 O << "\n";
Lauro Ramos Venancio1a92d942007-01-26 19:51:32 +0000142
143 // If the constant pool value is a extern weak symbol, remember to emit
144 // the weak reference.
Evan Chengc60e76d2007-01-30 20:37:08 +0000145 if (GV && GV->hasExternalWeakLinkage())
Lauro Ramos Venancio1a92d942007-01-26 19:51:32 +0000146 ExtWeakSymbols.insert(GV);
Evan Chenga8e29892007-01-19 07:51:42 +0000147 }
148
149 void getAnalysisUsage(AnalysisUsage &AU) const {
150 AU.setPreservesAll();
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000151 AU.addRequired<MachineModuleInfo>();
Evan Chenga8e29892007-01-19 07:51:42 +0000152 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000153 };
154} // end of anonymous namespace
155
156#include "ARMGenAsmWriter.inc"
157
158/// createARMCodePrinterPass - Returns a pass that prints the ARM
159/// assembly code for a MachineFunction to the given output stream,
160/// using the given target machine description. This should work
161/// regardless of whether the function is in SSA form.
162///
163FunctionPass *llvm::createARMCodePrinterPass(std::ostream &o,
Evan Chenga8e29892007-01-19 07:51:42 +0000164 ARMTargetMachine &tm) {
Jim Laskeya0f3d172006-09-07 22:06:40 +0000165 return new ARMAsmPrinter(o, tm, tm.getTargetAsmInfo());
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000166}
167
Evan Chenga8e29892007-01-19 07:51:42 +0000168/// runOnMachineFunction - This uses the printInstruction()
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000169/// method to print assembly for each instruction.
170///
171bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Evan Chenga8e29892007-01-19 07:51:42 +0000172 AFI = MF.getInfo<ARMFunctionInfo>();
Rafael Espindola4b442b52006-05-23 02:48:20 +0000173
Evan Cheng5be54b02007-01-19 19:25:36 +0000174 if (Subtarget->isTargetDarwin()) {
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000175 DW.SetModuleInfo(&getAnalysis<MachineModuleInfo>());
Chris Lattner1a199de2006-12-21 22:59:58 +0000176 }
177
Evan Chenga8e29892007-01-19 07:51:42 +0000178 SetupMachineFunction(MF);
179 O << "\n";
Rafael Espindola4b442b52006-05-23 02:48:20 +0000180
Evan Chenga8e29892007-01-19 07:51:42 +0000181 // NOTE: we don't print out constant pools here, they are handled as
182 // instructions.
183
184 O << "\n";
Rafael Espindola4b442b52006-05-23 02:48:20 +0000185 // Print out labels for the function.
186 const Function *F = MF.getFunction();
187 switch (F->getLinkage()) {
188 default: assert(0 && "Unknown linkage type!");
189 case Function::InternalLinkage:
Evan Chenga8e29892007-01-19 07:51:42 +0000190 SwitchToTextSection("\t.text", F);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000191 break;
192 case Function::ExternalLinkage:
Evan Chenga8e29892007-01-19 07:51:42 +0000193 SwitchToTextSection("\t.text", F);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000194 O << "\t.globl\t" << CurrentFnName << "\n";
195 break;
196 case Function::WeakLinkage:
197 case Function::LinkOnceLinkage:
Evan Cheng5be54b02007-01-19 19:25:36 +0000198 if (Subtarget->isTargetDarwin()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000199 SwitchToTextSection(
200 ".section __TEXT,__textcoal_nt,coalesced,pure_instructions", F);
201 O << "\t.globl\t" << CurrentFnName << "\n";
202 O << "\t.weak_definition\t" << CurrentFnName << "\n";
203 } else {
204 O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
205 }
Rafael Espindola4b442b52006-05-23 02:48:20 +0000206 break;
207 }
Evan Chenga8e29892007-01-19 07:51:42 +0000208
Evan Cheng616cc662007-03-29 07:49:34 +0000209 if (F->hasHiddenVisibility())
210 if (const char *Directive = TAI->getHiddenDirective())
211 O << Directive << CurrentFnName << "\n";
212
Evan Chenga8e29892007-01-19 07:51:42 +0000213 if (AFI->isThumbFunction()) {
Dale Johannesenbd24f3f2007-04-23 20:07:25 +0000214 EmitAlignment(AFI->getAlign(), F);
Evan Chenga8e29892007-01-19 07:51:42 +0000215 O << "\t.code\t16\n";
Lauro Ramos Venancio6f46e592007-02-01 18:25:34 +0000216 O << "\t.thumb_func";
217 if (Subtarget->isTargetDarwin())
218 O << "\t" << CurrentFnName;
219 O << "\n";
Evan Chenga8e29892007-01-19 07:51:42 +0000220 InCPMode = false;
221 } else
222 EmitAlignment(2, F);
223
Rafael Espindola4b442b52006-05-23 02:48:20 +0000224 O << CurrentFnName << ":\n";
Evan Cheng5be54b02007-01-19 19:25:36 +0000225 if (Subtarget->isTargetDarwin()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000226 // Emit pre-function debug information.
227 DW.BeginFunction(&MF);
228 }
Rafael Espindola4b442b52006-05-23 02:48:20 +0000229
230 // Print out code for the function.
231 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
232 I != E; ++I) {
233 // Print a label for the basic block.
234 if (I != MF.begin()) {
235 printBasicBlockLabel(I, true);
236 O << '\n';
237 }
238 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
239 II != E; ++II) {
240 // Print the assembly for the instruction.
Evan Chenga8e29892007-01-19 07:51:42 +0000241 printMachineInstruction(II);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000242 }
243 }
244
Evan Chenga8e29892007-01-19 07:51:42 +0000245 if (TAI->hasDotTypeDotSizeDirective())
246 O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
247
Evan Cheng5be54b02007-01-19 19:25:36 +0000248 if (Subtarget->isTargetDarwin()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000249 // Emit post-function debug information.
250 DW.EndFunction();
251 }
252
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000253 return false;
254}
255
Evan Chenga8e29892007-01-19 07:51:42 +0000256void ARMAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
257 const char *Modifier) {
258 const MachineOperand &MO = MI->getOperand(opNum);
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000259 switch (MO.getType()) {
260 case MachineOperand::MO_Register:
261 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
Evan Chenga8e29892007-01-19 07:51:42 +0000262 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000263 else
264 assert(0 && "not implemented");
265 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000266 case MachineOperand::MO_Immediate: {
267 if (!Modifier || strcmp(Modifier, "no_hash") != 0)
268 O << "#";
269
270 O << (int)MO.getImmedValue();
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000271 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000272 }
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000273 case MachineOperand::MO_MachineBasicBlock:
Rafael Espindola687bc492006-08-24 13:45:55 +0000274 printBasicBlockLabel(MO.getMachineBasicBlock());
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000275 return;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000276 case MachineOperand::MO_GlobalAddress: {
Evan Chenga8e29892007-01-19 07:51:42 +0000277 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Rafael Espindola84b19be2006-07-16 01:02:57 +0000278 GlobalValue *GV = MO.getGlobal();
279 std::string Name = Mang->getValueName(GV);
Reid Spencer5cbf9852007-01-30 20:08:39 +0000280 bool isExt = (GV->isDeclaration() || GV->hasWeakLinkage() ||
Evan Chenga8e29892007-01-19 07:51:42 +0000281 GV->hasLinkOnceLinkage());
Evan Cheng5be54b02007-01-19 19:25:36 +0000282 if (isExt && isCallOp && Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +0000283 TM.getRelocationModel() != Reloc::Static) {
284 O << TAI->getPrivateGlobalPrefix() << Name << "$stub";
285 FnStubs.insert(Name);
286 } else
287 O << Name;
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000288 if (isCallOp && Subtarget->isTargetELF() &&
289 TM.getRelocationModel() == Reloc::PIC_)
290 O << "(PLT)";
Evan Chenga8e29892007-01-19 07:51:42 +0000291 if (GV->hasExternalWeakLinkage())
Rafael Espindola15404d02006-12-18 03:37:18 +0000292 ExtWeakSymbols.insert(GV);
Evan Chenga8e29892007-01-19 07:51:42 +0000293 break;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000294 }
Evan Chenga8e29892007-01-19 07:51:42 +0000295 case MachineOperand::MO_ExternalSymbol: {
296 bool isCallOp = Modifier && !strcmp(Modifier, "call");
297 std::string Name(TAI->getGlobalPrefix());
298 Name += MO.getSymbolName();
Evan Cheng5be54b02007-01-19 19:25:36 +0000299 if (isCallOp && Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +0000300 TM.getRelocationModel() != Reloc::Static) {
301 O << TAI->getPrivateGlobalPrefix() << Name << "$stub";
302 FnStubs.insert(Name);
303 } else
304 O << Name;
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000305 if (isCallOp && Subtarget->isTargetELF() &&
306 TM.getRelocationModel() == Reloc::PIC_)
307 O << "(PLT)";
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000308 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000309 }
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000310 case MachineOperand::MO_ConstantPoolIndex:
Jim Laskey563321a2006-09-06 18:34:40 +0000311 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
Rafael Espindola06c1e7e2006-08-01 12:58:43 +0000312 << '_' << MO.getConstantPoolIndex();
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000313 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000314 case MachineOperand::MO_JumpTableIndex:
315 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
316 << '_' << MO.getJumpTableIndex();
317 break;
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000318 default:
319 O << "<unknown operand type>"; abort (); break;
320 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000321}
322
Evan Chengc70d1842007-03-20 08:11:30 +0000323static void printSOImm(std::ostream &O, int64_t V, const TargetAsmInfo *TAI) {
324 assert(V < (1 << 12) && "Not a valid so_imm value!");
325 unsigned Imm = ARM_AM::getSOImmValImm(V);
326 unsigned Rot = ARM_AM::getSOImmValRot(V);
Evan Chenga8e29892007-01-19 07:51:42 +0000327
328 // Print low-level immediate formation info, per
329 // A5.1.3: "Data-processing operands - Immediate".
330 if (Rot) {
331 O << "#" << Imm << ", " << Rot;
332 // Pretty printed version.
333 O << ' ' << TAI->getCommentString() << ' ' << (int)ARM_AM::rotr32(Imm, Rot);
334 } else {
335 O << "#" << Imm;
336 }
337}
338
Evan Chengc70d1842007-03-20 08:11:30 +0000339/// printSOImmOperand - SOImm is 4-bit rotate amount in bits 8-11 with 8-bit
340/// immediate in bits 0-7.
341void ARMAsmPrinter::printSOImmOperand(const MachineInstr *MI, int OpNum) {
342 const MachineOperand &MO = MI->getOperand(OpNum);
343 assert(MO.isImmediate() && "Not a valid so_imm value!");
344 printSOImm(O, MO.getImmedValue(), TAI);
345}
346
347/// printSOImm2PartOperand - SOImm is broken into two pieces using a mov
348/// followed by a or to materialize.
349void ARMAsmPrinter::printSOImm2PartOperand(const MachineInstr *MI, int OpNum) {
350 const MachineOperand &MO = MI->getOperand(OpNum);
351 assert(MO.isImmediate() && "Not a valid so_imm value!");
352 unsigned V1 = ARM_AM::getSOImmTwoPartFirst(MO.getImmedValue());
353 unsigned V2 = ARM_AM::getSOImmTwoPartSecond(MO.getImmedValue());
354 printSOImm(O, ARM_AM::getSOImmVal(V1), TAI);
355 O << "\n\torr ";
356 printOperand(MI, 0);
357 O << ", ";
358 printOperand(MI, 0);
359 O << ", ";
360 printSOImm(O, ARM_AM::getSOImmVal(V2), TAI);
361}
362
Evan Chenga8e29892007-01-19 07:51:42 +0000363// so_reg is a 4-operand unit corresponding to register forms of the A5.1
364// "Addressing Mode 1 - Data-processing operands" forms. This includes:
365// REG 0 0 - e.g. R5
366// REG REG 0,SH_OPC - e.g. R5, ROR R3
367// REG 0 IMM,SH_OPC - e.g. R5, LSL #3
368void ARMAsmPrinter::printSORegOperand(const MachineInstr *MI, int Op) {
369 const MachineOperand &MO1 = MI->getOperand(Op);
370 const MachineOperand &MO2 = MI->getOperand(Op+1);
371 const MachineOperand &MO3 = MI->getOperand(Op+2);
372
373 assert(MRegisterInfo::isPhysicalRegister(MO1.getReg()));
374 O << TM.getRegisterInfo()->get(MO1.getReg()).Name;
375
376 // Print the shift opc.
377 O << ", "
378 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(MO3.getImmedValue()))
379 << " ";
380
381 if (MO2.getReg()) {
382 assert(MRegisterInfo::isPhysicalRegister(MO2.getReg()));
383 O << TM.getRegisterInfo()->get(MO2.getReg()).Name;
384 assert(ARM_AM::getSORegOffset(MO3.getImm()) == 0);
385 } else {
386 O << "#" << ARM_AM::getSORegOffset(MO3.getImm());
387 }
388}
389
390void ARMAsmPrinter::printAddrMode2Operand(const MachineInstr *MI, int Op) {
391 const MachineOperand &MO1 = MI->getOperand(Op);
392 const MachineOperand &MO2 = MI->getOperand(Op+1);
393 const MachineOperand &MO3 = MI->getOperand(Op+2);
394
395 if (!MO1.isRegister()) { // FIXME: This is for CP entries, but isn't right.
396 printOperand(MI, Op);
397 return;
398 }
399
400 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
401
402 if (!MO2.getReg()) {
403 if (ARM_AM::getAM2Offset(MO3.getImm())) // Don't print +0.
404 O << ", #"
405 << (char)ARM_AM::getAM2Op(MO3.getImm())
406 << ARM_AM::getAM2Offset(MO3.getImm());
407 O << "]";
408 return;
409 }
410
411 O << ", "
412 << (char)ARM_AM::getAM2Op(MO3.getImm())
413 << TM.getRegisterInfo()->get(MO2.getReg()).Name;
414
415 if (unsigned ShImm = ARM_AM::getAM2Offset(MO3.getImm()))
416 O << ", "
417 << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO3.getImmedValue()))
418 << " #" << ShImm;
419 O << "]";
420}
421
422void ARMAsmPrinter::printAddrMode2OffsetOperand(const MachineInstr *MI, int Op){
423 const MachineOperand &MO1 = MI->getOperand(Op);
424 const MachineOperand &MO2 = MI->getOperand(Op+1);
425
426 if (!MO1.getReg()) {
427 if (ARM_AM::getAM2Offset(MO2.getImm())) // Don't print +0.
428 O << "#"
429 << (char)ARM_AM::getAM2Op(MO2.getImm())
430 << ARM_AM::getAM2Offset(MO2.getImm());
431 return;
432 }
433
434 O << (char)ARM_AM::getAM2Op(MO2.getImm())
435 << TM.getRegisterInfo()->get(MO1.getReg()).Name;
436
437 if (unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm()))
438 O << ", "
439 << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO2.getImmedValue()))
440 << " #" << ShImm;
441}
442
443void ARMAsmPrinter::printAddrMode3Operand(const MachineInstr *MI, int Op) {
444 const MachineOperand &MO1 = MI->getOperand(Op);
445 const MachineOperand &MO2 = MI->getOperand(Op+1);
446 const MachineOperand &MO3 = MI->getOperand(Op+2);
447
448 assert(MRegisterInfo::isPhysicalRegister(MO1.getReg()));
449 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
450
451 if (MO2.getReg()) {
452 O << ", "
453 << (char)ARM_AM::getAM3Op(MO3.getImm())
454 << TM.getRegisterInfo()->get(MO2.getReg()).Name
455 << "]";
456 return;
457 }
458
459 if (unsigned ImmOffs = ARM_AM::getAM3Offset(MO3.getImm()))
460 O << ", #"
461 << (char)ARM_AM::getAM3Op(MO3.getImm())
462 << ImmOffs;
463 O << "]";
464}
465
466void ARMAsmPrinter::printAddrMode3OffsetOperand(const MachineInstr *MI, int Op){
467 const MachineOperand &MO1 = MI->getOperand(Op);
468 const MachineOperand &MO2 = MI->getOperand(Op+1);
469
470 if (MO1.getReg()) {
471 O << (char)ARM_AM::getAM3Op(MO2.getImm())
472 << TM.getRegisterInfo()->get(MO1.getReg()).Name;
473 return;
474 }
475
476 unsigned ImmOffs = ARM_AM::getAM3Offset(MO2.getImm());
477 O << "#"
478 << (char)ARM_AM::getAM3Op(MO2.getImm())
479 << ImmOffs;
480}
481
482void ARMAsmPrinter::printAddrMode4Operand(const MachineInstr *MI, int Op,
483 const char *Modifier) {
484 const MachineOperand &MO1 = MI->getOperand(Op);
485 const MachineOperand &MO2 = MI->getOperand(Op+1);
486 ARM_AM::AMSubMode Mode = ARM_AM::getAM4SubMode(MO2.getImm());
487 if (Modifier && strcmp(Modifier, "submode") == 0) {
488 if (MO1.getReg() == ARM::SP) {
489 bool isLDM = (MI->getOpcode() == ARM::LDM ||
490 MI->getOpcode() == ARM::LDM_RET);
491 O << ARM_AM::getAMSubModeAltStr(Mode, isLDM);
492 } else
493 O << ARM_AM::getAMSubModeStr(Mode);
494 } else {
495 printOperand(MI, Op);
496 if (ARM_AM::getAM4WBFlag(MO2.getImm()))
497 O << "!";
498 }
499}
500
501void ARMAsmPrinter::printAddrMode5Operand(const MachineInstr *MI, int Op,
502 const char *Modifier) {
503 const MachineOperand &MO1 = MI->getOperand(Op);
504 const MachineOperand &MO2 = MI->getOperand(Op+1);
505
506 if (!MO1.isRegister()) { // FIXME: This is for CP entries, but isn't right.
507 printOperand(MI, Op);
508 return;
509 }
510
511 assert(MRegisterInfo::isPhysicalRegister(MO1.getReg()));
512
513 if (Modifier && strcmp(Modifier, "submode") == 0) {
514 ARM_AM::AMSubMode Mode = ARM_AM::getAM5SubMode(MO2.getImm());
515 if (MO1.getReg() == ARM::SP) {
516 bool isFLDM = (MI->getOpcode() == ARM::FLDMD ||
517 MI->getOpcode() == ARM::FLDMS);
518 O << ARM_AM::getAMSubModeAltStr(Mode, isFLDM);
519 } else
520 O << ARM_AM::getAMSubModeStr(Mode);
521 return;
522 } else if (Modifier && strcmp(Modifier, "base") == 0) {
523 // Used for FSTM{D|S} and LSTM{D|S} operations.
524 O << TM.getRegisterInfo()->get(MO1.getReg()).Name;
525 if (ARM_AM::getAM5WBFlag(MO2.getImm()))
526 O << "!";
527 return;
528 }
529
530 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
531
532 if (unsigned ImmOffs = ARM_AM::getAM5Offset(MO2.getImm())) {
533 O << ", #"
534 << (char)ARM_AM::getAM5Op(MO2.getImm())
535 << ImmOffs*4;
536 }
537 O << "]";
538}
539
540void ARMAsmPrinter::printAddrModePCOperand(const MachineInstr *MI, int Op,
541 const char *Modifier) {
542 if (Modifier && strcmp(Modifier, "label") == 0) {
543 printPCLabel(MI, Op+1);
544 return;
545 }
546
547 const MachineOperand &MO1 = MI->getOperand(Op);
548 assert(MRegisterInfo::isPhysicalRegister(MO1.getReg()));
549 O << "[pc, +" << TM.getRegisterInfo()->get(MO1.getReg()).Name << "]";
550}
551
552void
553ARMAsmPrinter::printThumbAddrModeRROperand(const MachineInstr *MI, int Op) {
554 const MachineOperand &MO1 = MI->getOperand(Op);
555 const MachineOperand &MO2 = MI->getOperand(Op+1);
556 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
557 O << ", " << TM.getRegisterInfo()->get(MO2.getReg()).Name << "]";
558}
559
560void
561ARMAsmPrinter::printThumbAddrModeRI5Operand(const MachineInstr *MI, int Op,
562 unsigned Scale) {
563 const MachineOperand &MO1 = MI->getOperand(Op);
Evan Chengcea117d2007-01-30 02:35:32 +0000564 const MachineOperand &MO2 = MI->getOperand(Op+1);
565 const MachineOperand &MO3 = MI->getOperand(Op+2);
Evan Chenga8e29892007-01-19 07:51:42 +0000566
567 if (!MO1.isRegister()) { // FIXME: This is for CP entries, but isn't right.
568 printOperand(MI, Op);
569 return;
570 }
571
572 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
Evan Chengcea117d2007-01-30 02:35:32 +0000573 if (MO3.getReg())
574 O << ", " << TM.getRegisterInfo()->get(MO3.getReg()).Name;
575 else if (unsigned ImmOffs = MO2.getImm()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000576 O << ", #" << ImmOffs;
577 if (Scale > 1)
578 O << " * " << Scale;
579 }
580 O << "]";
581}
582
583void
Evan Chengc38f2bc2007-01-23 22:59:13 +0000584ARMAsmPrinter::printThumbAddrModeS1Operand(const MachineInstr *MI, int Op) {
Evan Chengcea117d2007-01-30 02:35:32 +0000585 printThumbAddrModeRI5Operand(MI, Op, 1);
Evan Chenga8e29892007-01-19 07:51:42 +0000586}
587void
Evan Chengc38f2bc2007-01-23 22:59:13 +0000588ARMAsmPrinter::printThumbAddrModeS2Operand(const MachineInstr *MI, int Op) {
Evan Chengcea117d2007-01-30 02:35:32 +0000589 printThumbAddrModeRI5Operand(MI, Op, 2);
Evan Chenga8e29892007-01-19 07:51:42 +0000590}
591void
Evan Chengc38f2bc2007-01-23 22:59:13 +0000592ARMAsmPrinter::printThumbAddrModeS4Operand(const MachineInstr *MI, int Op) {
Evan Chengcea117d2007-01-30 02:35:32 +0000593 printThumbAddrModeRI5Operand(MI, Op, 4);
Evan Chenga8e29892007-01-19 07:51:42 +0000594}
595
596void ARMAsmPrinter::printThumbAddrModeSPOperand(const MachineInstr *MI,int Op) {
597 const MachineOperand &MO1 = MI->getOperand(Op);
598 const MachineOperand &MO2 = MI->getOperand(Op+1);
599 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
600 if (unsigned ImmOffs = MO2.getImm())
601 O << ", #" << ImmOffs << " * 4";
602 O << "]";
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000603}
604
605void ARMAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
Rafael Espindola6f602de2006-08-24 16:13:15 +0000606 int CC = (int)MI->getOperand(opNum).getImmedValue();
607 O << ARMCondCodeToString((ARMCC::CondCodes)CC);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000608}
609
Evan Chenga8e29892007-01-19 07:51:42 +0000610void ARMAsmPrinter::printPCLabel(const MachineInstr *MI, int opNum) {
611 int Id = (int)MI->getOperand(opNum).getImmedValue();
612 O << TAI->getPrivateGlobalPrefix() << "PC" << Id;
613}
614
615void ARMAsmPrinter::printRegisterList(const MachineInstr *MI, int opNum) {
616 O << "{";
617 for (unsigned i = opNum, e = MI->getNumOperands(); i != e; ++i) {
618 printOperand(MI, i);
619 if (i != e-1) O << ", ";
620 }
621 O << "}";
622}
623
624void ARMAsmPrinter::printCPInstOperand(const MachineInstr *MI, int OpNo,
625 const char *Modifier) {
626 assert(Modifier && "This operand only works with a modifier!");
627 // There are two aspects to a CONSTANTPOOL_ENTRY operand, the label and the
628 // data itself.
629 if (!strcmp(Modifier, "label")) {
630 unsigned ID = MI->getOperand(OpNo).getImm();
631 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
632 << '_' << ID << ":\n";
633 } else {
634 assert(!strcmp(Modifier, "cpentry") && "Unknown modifier for CPE");
635 unsigned CPI = MI->getOperand(OpNo).getConstantPoolIndex();
636
637 const MachineConstantPoolEntry &MCPE = // Chasing pointers is fun?
638 MI->getParent()->getParent()->getConstantPool()->getConstants()[CPI];
639
640 if (MCPE.isMachineConstantPoolEntry())
641 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
642 else
643 EmitGlobalConstant(MCPE.Val.ConstVal);
644 }
645}
646
647void ARMAsmPrinter::printJTBlockOperand(const MachineInstr *MI, int OpNo) {
648 const MachineOperand &MO1 = MI->getOperand(OpNo);
649 const MachineOperand &MO2 = MI->getOperand(OpNo+1); // Unique Id
650 unsigned JTI = MO1.getJumpTableIndex();
651 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
652 << '_' << JTI << '_' << MO2.getImmedValue() << ":\n";
653
654 const char *JTEntryDirective = TAI->getJumpTableDirective();
655 if (!JTEntryDirective)
656 JTEntryDirective = TAI->getData32bitsDirective();
657
658 const MachineFunction *MF = MI->getParent()->getParent();
659 MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
660 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
661 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
662 bool UseSet= TAI->getSetDirective() && TM.getRelocationModel() == Reloc::PIC_;
663 std::set<MachineBasicBlock*> JTSets;
664 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
665 MachineBasicBlock *MBB = JTBBs[i];
666 if (UseSet && JTSets.insert(MBB).second)
667 printSetLabel(JTI, MO2.getImmedValue(), MBB);
668
669 O << JTEntryDirective << ' ';
670 if (UseSet)
671 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
672 << '_' << JTI << '_' << MO2.getImmedValue()
673 << "_set_" << MBB->getNumber();
674 else if (TM.getRelocationModel() == Reloc::PIC_) {
675 printBasicBlockLabel(MBB, false, false);
676 // If the arch uses custom Jump Table directives, don't calc relative to JT
677 if (!TAI->getJumpTableDirective())
678 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
679 << getFunctionNumber() << '_' << JTI << '_' << MO2.getImmedValue();
680 } else
681 printBasicBlockLabel(MBB, false, false);
Evan Chengd85ac4d2007-01-27 02:29:45 +0000682 if (i != e-1)
683 O << '\n';
Evan Chenga8e29892007-01-19 07:51:42 +0000684 }
685}
686
687
688bool ARMAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
689 unsigned AsmVariant, const char *ExtraCode){
690 // Does this asm operand have a single letter operand modifier?
691 if (ExtraCode && ExtraCode[0]) {
692 if (ExtraCode[1] != 0) return true; // Unknown modifier.
693
694 switch (ExtraCode[0]) {
695 default: return true; // Unknown modifier.
Evan Cheng23a95702007-03-08 22:42:46 +0000696 case 'c': // Don't print "$" before a global var name or constant.
Evan Chenge21e3962007-04-04 00:13:29 +0000697 case 'P': // Print a VFP double precision register.
Evan Cheng23a95702007-03-08 22:42:46 +0000698 printOperand(MI, OpNo);
699 return false;
Evan Chenga8e29892007-01-19 07:51:42 +0000700 case 'Q':
701 if (TM.getTargetData()->isLittleEndian())
702 break;
703 // Fallthrough
704 case 'R':
705 if (TM.getTargetData()->isBigEndian())
706 break;
707 // Fallthrough
708 case 'H': // Write second word of DI / DF reference.
709 // Verify that this operand has two consecutive registers.
710 if (!MI->getOperand(OpNo).isRegister() ||
711 OpNo+1 == MI->getNumOperands() ||
712 !MI->getOperand(OpNo+1).isRegister())
713 return true;
714 ++OpNo; // Return the high-part.
715 }
716 }
717
718 printOperand(MI, OpNo);
719 return false;
720}
721
722void ARMAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
723 ++EmittedInsts;
724
Evan Chengc60e76d2007-01-30 20:37:08 +0000725 int Opc = MI->getOpcode();
726 switch (Opc) {
727 case ARM::CONSTPOOL_ENTRY:
Evan Chenga8e29892007-01-19 07:51:42 +0000728 if (!InCPMode && AFI->isThumbFunction()) {
729 EmitAlignment(2);
730 InCPMode = true;
731 }
Evan Chengc60e76d2007-01-30 20:37:08 +0000732 break;
733 default: {
Evan Cheng3bf12d02007-01-31 23:39:39 +0000734 if (InCPMode && AFI->isThumbFunction())
Evan Chenga8e29892007-01-19 07:51:42 +0000735 InCPMode = false;
Evan Chengc60e76d2007-01-30 20:37:08 +0000736 switch (Opc) {
737 case ARM::PICADD:
738 case ARM::PICLD:
739 case ARM::tPICADD:
740 break;
741 default:
742 O << "\t";
743 break;
744 }
745 }}
Evan Chenga8e29892007-01-19 07:51:42 +0000746
747 // Call the autogenerated instruction printer routines.
748 printInstruction(MI);
749}
750
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000751bool ARMAsmPrinter::doInitialization(Module &M) {
Evan Cheng5be54b02007-01-19 19:25:36 +0000752 if (Subtarget->isTargetDarwin()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000753 // Emit initial debug information.
754 DW.BeginModule(&M);
755 }
756
757 return AsmPrinter::doInitialization(M);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000758}
759
760bool ARMAsmPrinter::doFinalization(Module &M) {
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000761 const TargetData *TD = TM.getTargetData();
762
763 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
764 I != E; ++I) {
765 if (!I->hasInitializer()) // External global require no code
766 continue;
767
Evan Chengb267ca12007-01-30 08:04:53 +0000768 if (EmitSpecialLLVMGlobal(I)) {
769 if (Subtarget->isTargetDarwin() &&
770 TM.getRelocationModel() == Reloc::Static) {
771 if (I->getName() == "llvm.global_ctors")
772 O << ".reference .constructors_used\n";
773 else if (I->getName() == "llvm.global_dtors")
774 O << ".reference .destructors_used\n";
775 }
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000776 continue;
Evan Chengb267ca12007-01-30 08:04:53 +0000777 }
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000778
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000779 std::string name = Mang->getValueName(I);
780 Constant *C = I->getInitializer();
Evan Cheng98ded762007-03-08 01:25:25 +0000781 const Type *Type = C->getType();
782 unsigned Size = TD->getTypeSize(Type);
Evan Chenga8e29892007-01-19 07:51:42 +0000783 unsigned Align = TD->getPreferredAlignmentLog(I);
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000784
Evan Cheng5be54b02007-01-19 19:25:36 +0000785 if (I->hasHiddenVisibility())
786 if (const char *Directive = TAI->getHiddenDirective())
787 O << Directive << name << "\n";
788 if (Subtarget->isTargetELF())
Lauro Ramos Venanciob1cc0522007-01-25 20:11:04 +0000789 O << "\t.type " << name << ",%object\n";
Evan Cheng5be54b02007-01-19 19:25:36 +0000790
791 if (C->isNullValue()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000792 if (I->hasExternalLinkage()) {
Evan Cheng5be54b02007-01-19 19:25:36 +0000793 if (const char *Directive = TAI->getZeroFillDirective()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000794 O << "\t.globl\t" << name << "\n";
Evan Cheng5be54b02007-01-19 19:25:36 +0000795 O << Directive << "__DATA__, __common, " << name << ", "
796 << Size << ", " << Align << "\n";
797 continue;
798 }
799 }
800
801 if (!I->hasSection() &&
802 (I->hasInternalLinkage() || I->hasWeakLinkage() ||
803 I->hasLinkOnceLinkage())) {
804 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
805 if (!NoZerosInBSS && TAI->getBSSSection())
806 SwitchToDataSection(TAI->getBSSSection(), I);
807 else
808 SwitchToDataSection(TAI->getDataSection(), I);
Evan Chenga8e29892007-01-19 07:51:42 +0000809 if (TAI->getLCOMMDirective() != NULL) {
810 if (I->hasInternalLinkage()) {
811 O << TAI->getLCOMMDirective() << name << "," << Size;
Evan Cheng5be54b02007-01-19 19:25:36 +0000812 if (Subtarget->isTargetDarwin())
Evan Chenga8e29892007-01-19 07:51:42 +0000813 O << "," << Align;
814 } else
815 O << TAI->getCOMMDirective() << name << "," << Size;
816 } else {
817 if (I->hasInternalLinkage())
818 O << "\t.local\t" << name << "\n";
819 O << TAI->getCOMMDirective() << name << "," << Size;
820 if (TAI->getCOMMDirectiveTakesAlignment())
821 O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
822 }
Evan Cheng5be54b02007-01-19 19:25:36 +0000823 O << "\t\t" << TAI->getCommentString() << " " << I->getName() << "\n";
824 continue;
Evan Chenga8e29892007-01-19 07:51:42 +0000825 }
Evan Cheng5be54b02007-01-19 19:25:36 +0000826 }
Evan Chenga8e29892007-01-19 07:51:42 +0000827
Evan Cheng5be54b02007-01-19 19:25:36 +0000828 switch (I->getLinkage()) {
829 case GlobalValue::LinkOnceLinkage:
830 case GlobalValue::WeakLinkage:
831 if (Subtarget->isTargetDarwin()) {
832 O << "\t.globl " << name << "\n"
833 << "\t.weak_definition " << name << "\n";
834 SwitchToDataSection("\t.section __DATA,__const_coal,coalesced", I);
835 } else {
836 std::string SectionName("\t.section\t.llvm.linkonce.d." +
837 name +
838 ",\"aw\",%progbits");
839 SwitchToDataSection(SectionName.c_str(), I);
840 O << "\t.weak " << name << "\n";
841 }
842 break;
843 case GlobalValue::AppendingLinkage:
844 // FIXME: appending linkage variables should go into a section of
845 // their name or something. For now, just emit them as external.
846 case GlobalValue::ExternalLinkage:
847 O << "\t.globl " << name << "\n";
848 // FALL THROUGH
849 case GlobalValue::InternalLinkage: {
850 if (I->isConstant()) {
851 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
852 if (TAI->getCStringSection() && CVA && CVA->isCString()) {
853 SwitchToDataSection(TAI->getCStringSection(), I);
854 break;
855 }
856 }
857 // FIXME: special handling for ".ctors" & ".dtors" sections
858 if (I->hasSection() &&
859 (I->getSection() == ".ctors" ||
860 I->getSection() == ".dtors")) {
861 assert(!Subtarget->isTargetDarwin());
862 std::string SectionName = ".section " + I->getSection();
863 SectionName += ",\"aw\",%progbits";
864 SwitchToDataSection(SectionName.c_str());
865 } else {
866 if (C->isNullValue() && !NoZerosInBSS && TAI->getBSSSection())
867 SwitchToDataSection(TAI->getBSSSection(), I);
Evan Cheng98ded762007-03-08 01:25:25 +0000868 else if (!I->isConstant())
Evan Chenga8e29892007-01-19 07:51:42 +0000869 SwitchToDataSection(TAI->getDataSection(), I);
Evan Cheng98ded762007-03-08 01:25:25 +0000870 else {
871 // Read-only data.
Evan Cheng032953d2007-03-08 08:31:54 +0000872 bool HasReloc = C->ContainsRelocations();
873 if (HasReloc &&
874 Subtarget->isTargetDarwin() &&
Evan Cheng98ded762007-03-08 01:25:25 +0000875 TM.getRelocationModel() != Reloc::Static)
876 SwitchToDataSection("\t.const_data\n");
Evan Cheng032953d2007-03-08 08:31:54 +0000877 else if (!HasReloc && Size == 4 &&
Evan Cheng98ded762007-03-08 01:25:25 +0000878 TAI->getFourByteConstantSection())
879 SwitchToDataSection(TAI->getFourByteConstantSection(), I);
Evan Cheng032953d2007-03-08 08:31:54 +0000880 else if (!HasReloc && Size == 8 &&
Evan Cheng98ded762007-03-08 01:25:25 +0000881 TAI->getEightByteConstantSection())
882 SwitchToDataSection(TAI->getEightByteConstantSection(), I);
Evan Cheng032953d2007-03-08 08:31:54 +0000883 else if (!HasReloc && Size == 16 &&
Evan Cheng98ded762007-03-08 01:25:25 +0000884 TAI->getSixteenByteConstantSection())
885 SwitchToDataSection(TAI->getSixteenByteConstantSection(), I);
886 else if (TAI->getReadOnlySection())
887 SwitchToDataSection(TAI->getReadOnlySection(), I);
888 else
889 SwitchToDataSection(TAI->getDataSection(), I);
890 }
Rafael Espindolab97809c2006-10-19 13:30:40 +0000891 }
Evan Cheng5be54b02007-01-19 19:25:36 +0000892
893 break;
894 }
895 default:
896 assert(0 && "Unknown linkage type!");
897 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000898 }
Rafael Espindolab97809c2006-10-19 13:30:40 +0000899
Evan Chenga8e29892007-01-19 07:51:42 +0000900 EmitAlignment(Align, I);
Evan Cheng5be54b02007-01-19 19:25:36 +0000901 O << name << ":\t\t\t\t" << TAI->getCommentString() << " " << I->getName()
902 << "\n";
903 if (TAI->hasDotTypeDotSizeDirective())
Rafael Espindolab97809c2006-10-19 13:30:40 +0000904 O << "\t.size " << name << ", " << Size << "\n";
Evan Chenga8e29892007-01-19 07:51:42 +0000905 // If the initializer is a extern weak symbol, remember to emit the weak
906 // reference!
907 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
908 if (GV->hasExternalWeakLinkage())
909 ExtWeakSymbols.insert(GV);
910
911 EmitGlobalConstant(C);
912 O << '\n';
913 }
914
Evan Cheng5be54b02007-01-19 19:25:36 +0000915 if (Subtarget->isTargetDarwin()) {
916 SwitchToDataSection("");
917
Evan Chenga8e29892007-01-19 07:51:42 +0000918 // Output stubs for dynamically-linked functions
919 unsigned j = 1;
920 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
921 i != e; ++i, ++j) {
922 if (TM.getRelocationModel() == Reloc::PIC_)
923 SwitchToTextSection(".section __TEXT,__picsymbolstub4,symbol_stubs,"
924 "none,16", 0);
925 else
926 SwitchToTextSection(".section __TEXT,__symbol_stub4,symbol_stubs,"
927 "none,12", 0);
928
929 EmitAlignment(2);
930 O << "\t.code\t32\n";
931
932 O << "L" << *i << "$stub:\n";
933 O << "\t.indirect_symbol " << *i << "\n";
934 O << "\tldr ip, L" << *i << "$slp\n";
935 if (TM.getRelocationModel() == Reloc::PIC_) {
936 O << "L" << *i << "$scv:\n";
937 O << "\tadd ip, pc, ip\n";
938 }
939 O << "\tldr pc, [ip, #0]\n";
940 O << "L" << *i << "$slp:\n";
941 if (TM.getRelocationModel() == Reloc::PIC_)
942 O << "\t.long\tL" << *i << "$lazy_ptr-(L" << *i << "$scv+8)\n";
943 else
944 O << "\t.long\tL" << *i << "$lazy_ptr\n";
945 SwitchToDataSection(".lazy_symbol_pointer", 0);
946 O << "L" << *i << "$lazy_ptr:\n";
947 O << "\t.indirect_symbol " << *i << "\n";
948 O << "\t.long\tdyld_stub_binding_helper\n";
949 }
950 O << "\n";
951
952 // Output non-lazy-pointers for external and common global variables.
953 if (GVNonLazyPtrs.begin() != GVNonLazyPtrs.end())
954 SwitchToDataSection(".non_lazy_symbol_pointer", 0);
955 for (std::set<std::string>::iterator i = GVNonLazyPtrs.begin(),
956 e = GVNonLazyPtrs.end(); i != e; ++i) {
957 O << "L" << *i << "$non_lazy_ptr:\n";
958 O << "\t.indirect_symbol " << *i << "\n";
959 O << "\t.long\t0\n";
960 }
961
962 // Emit initial debug information.
963 DW.EndModule();
964
965 // Funny Darwin hack: This flag tells the linker that no global symbols
966 // contain code that falls through to other global symbols (e.g. the obvious
967 // implementation of multiple entry points). If this doesn't occur, the
968 // linker can safely perform dead code stripping. Since LLVM never
969 // generates code that does this, it is always safe to set.
970 O << "\t.subsections_via_symbols\n";
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000971 }
Rafael Espindolab97809c2006-10-19 13:30:40 +0000972
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000973 AsmPrinter::doFinalization(M);
974 return false; // success
975}