blob: dacc50d71c949fc3c9fff6b5c65a9483f96a23da [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
Bill Wendling200e90c2008-01-28 09:15:03 +0000236 if (Subtarget->isTargetDarwin()) {
237 // If the function is empty, then we need to emit *something*. Otherwise,
238 // the function's label might be associated with something that it wasn't
239 // meant to be associated with. We emit a noop in this situation.
240 MachineFunction::iterator I = MF.begin();
241
242 if (++I == MF.end() && MF.front().empty())
243 O << "\tnop\n";
244 }
245
Rafael Espindola4b442b52006-05-23 02:48:20 +0000246 // Print out code for the function.
247 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
248 I != E; ++I) {
249 // Print a label for the basic block.
250 if (I != MF.begin()) {
251 printBasicBlockLabel(I, true);
252 O << '\n';
253 }
254 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
255 II != E; ++II) {
256 // Print the assembly for the instruction.
Evan Chenga8e29892007-01-19 07:51:42 +0000257 printMachineInstruction(II);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000258 }
259 }
260
Evan Chenga8e29892007-01-19 07:51:42 +0000261 if (TAI->hasDotTypeDotSizeDirective())
262 O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
263
Lauro Ramos Venancioe8e54952007-05-03 20:28:35 +0000264 // Emit post-function debug information.
265 DW.EndFunction();
Evan Chenga8e29892007-01-19 07:51:42 +0000266
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000267 return false;
268}
269
Evan Chenga8e29892007-01-19 07:51:42 +0000270void ARMAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
271 const char *Modifier) {
272 const MachineOperand &MO = MI->getOperand(opNum);
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000273 switch (MO.getType()) {
274 case MachineOperand::MO_Register:
Dan Gohman6f0d0242008-02-10 18:45:23 +0000275 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Evan Chenga8e29892007-01-19 07:51:42 +0000276 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000277 else
278 assert(0 && "not implemented");
279 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000280 case MachineOperand::MO_Immediate: {
281 if (!Modifier || strcmp(Modifier, "no_hash") != 0)
282 O << "#";
283
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000284 O << (int)MO.getImm();
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000285 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000286 }
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000287 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000288 printBasicBlockLabel(MO.getMBB());
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000289 return;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000290 case MachineOperand::MO_GlobalAddress: {
Evan Chenga8e29892007-01-19 07:51:42 +0000291 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Rafael Espindola84b19be2006-07-16 01:02:57 +0000292 GlobalValue *GV = MO.getGlobal();
293 std::string Name = Mang->getValueName(GV);
Reid Spencer5cbf9852007-01-30 20:08:39 +0000294 bool isExt = (GV->isDeclaration() || GV->hasWeakLinkage() ||
Evan Chenga8e29892007-01-19 07:51:42 +0000295 GV->hasLinkOnceLinkage());
Evan Cheng5be54b02007-01-19 19:25:36 +0000296 if (isExt && isCallOp && Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +0000297 TM.getRelocationModel() != Reloc::Static) {
298 O << TAI->getPrivateGlobalPrefix() << Name << "$stub";
299 FnStubs.insert(Name);
300 } else
301 O << Name;
Chris Lattner388488d2007-05-03 16:42:23 +0000302
303 if (MO.getOffset() > 0)
304 O << '+' << MO.getOffset();
305 else if (MO.getOffset() < 0)
306 O << MO.getOffset();
307
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000308 if (isCallOp && Subtarget->isTargetELF() &&
309 TM.getRelocationModel() == Reloc::PIC_)
310 O << "(PLT)";
Evan Chenga8e29892007-01-19 07:51:42 +0000311 if (GV->hasExternalWeakLinkage())
Rafael Espindola15404d02006-12-18 03:37:18 +0000312 ExtWeakSymbols.insert(GV);
Evan Chenga8e29892007-01-19 07:51:42 +0000313 break;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000314 }
Evan Chenga8e29892007-01-19 07:51:42 +0000315 case MachineOperand::MO_ExternalSymbol: {
316 bool isCallOp = Modifier && !strcmp(Modifier, "call");
317 std::string Name(TAI->getGlobalPrefix());
318 Name += MO.getSymbolName();
Evan Cheng5be54b02007-01-19 19:25:36 +0000319 if (isCallOp && Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +0000320 TM.getRelocationModel() != Reloc::Static) {
321 O << TAI->getPrivateGlobalPrefix() << Name << "$stub";
322 FnStubs.insert(Name);
323 } else
324 O << Name;
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000325 if (isCallOp && Subtarget->isTargetELF() &&
326 TM.getRelocationModel() == Reloc::PIC_)
327 O << "(PLT)";
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000328 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000329 }
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000330 case MachineOperand::MO_ConstantPoolIndex:
Evan Cheng347d39f2007-10-14 05:57:21 +0000331 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
Chris Lattner8aa797a2007-12-30 23:10:15 +0000332 << '_' << MO.getIndex();
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000333 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000334 case MachineOperand::MO_JumpTableIndex:
Evan Cheng347d39f2007-10-14 05:57:21 +0000335 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner8aa797a2007-12-30 23:10:15 +0000336 << '_' << MO.getIndex();
Evan Chenga8e29892007-01-19 07:51:42 +0000337 break;
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000338 default:
339 O << "<unknown operand type>"; abort (); break;
340 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000341}
342
Evan Chengc70d1842007-03-20 08:11:30 +0000343static void printSOImm(std::ostream &O, int64_t V, const TargetAsmInfo *TAI) {
344 assert(V < (1 << 12) && "Not a valid so_imm value!");
345 unsigned Imm = ARM_AM::getSOImmValImm(V);
346 unsigned Rot = ARM_AM::getSOImmValRot(V);
Evan Chenga8e29892007-01-19 07:51:42 +0000347
348 // Print low-level immediate formation info, per
349 // A5.1.3: "Data-processing operands - Immediate".
350 if (Rot) {
351 O << "#" << Imm << ", " << Rot;
352 // Pretty printed version.
353 O << ' ' << TAI->getCommentString() << ' ' << (int)ARM_AM::rotr32(Imm, Rot);
354 } else {
355 O << "#" << Imm;
356 }
357}
358
Evan Chengc70d1842007-03-20 08:11:30 +0000359/// printSOImmOperand - SOImm is 4-bit rotate amount in bits 8-11 with 8-bit
360/// immediate in bits 0-7.
361void ARMAsmPrinter::printSOImmOperand(const MachineInstr *MI, int OpNum) {
362 const MachineOperand &MO = MI->getOperand(OpNum);
363 assert(MO.isImmediate() && "Not a valid so_imm value!");
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000364 printSOImm(O, MO.getImm(), TAI);
Evan Chengc70d1842007-03-20 08:11:30 +0000365}
366
367/// printSOImm2PartOperand - SOImm is broken into two pieces using a mov
368/// followed by a or to materialize.
369void ARMAsmPrinter::printSOImm2PartOperand(const MachineInstr *MI, int OpNum) {
370 const MachineOperand &MO = MI->getOperand(OpNum);
371 assert(MO.isImmediate() && "Not a valid so_imm value!");
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000372 unsigned V1 = ARM_AM::getSOImmTwoPartFirst(MO.getImm());
373 unsigned V2 = ARM_AM::getSOImmTwoPartSecond(MO.getImm());
Evan Chengc70d1842007-03-20 08:11:30 +0000374 printSOImm(O, ARM_AM::getSOImmVal(V1), TAI);
Evan Cheng5e148a32007-06-05 18:55:18 +0000375 O << "\n\torr";
376 printPredicateOperand(MI, 2);
377 O << " ";
Evan Chengc70d1842007-03-20 08:11:30 +0000378 printOperand(MI, 0);
379 O << ", ";
380 printOperand(MI, 0);
381 O << ", ";
382 printSOImm(O, ARM_AM::getSOImmVal(V2), TAI);
383}
384
Evan Chenga8e29892007-01-19 07:51:42 +0000385// so_reg is a 4-operand unit corresponding to register forms of the A5.1
386// "Addressing Mode 1 - Data-processing operands" forms. This includes:
387// REG 0 0 - e.g. R5
388// REG REG 0,SH_OPC - e.g. R5, ROR R3
389// REG 0 IMM,SH_OPC - e.g. R5, LSL #3
390void ARMAsmPrinter::printSORegOperand(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
Dan Gohman6f0d0242008-02-10 18:45:23 +0000395 assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
Evan Chenga8e29892007-01-19 07:51:42 +0000396 O << TM.getRegisterInfo()->get(MO1.getReg()).Name;
397
398 // Print the shift opc.
399 O << ", "
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000400 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(MO3.getImm()))
Evan Chenga8e29892007-01-19 07:51:42 +0000401 << " ";
402
403 if (MO2.getReg()) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000404 assert(TargetRegisterInfo::isPhysicalRegister(MO2.getReg()));
Evan Chenga8e29892007-01-19 07:51:42 +0000405 O << TM.getRegisterInfo()->get(MO2.getReg()).Name;
406 assert(ARM_AM::getSORegOffset(MO3.getImm()) == 0);
407 } else {
408 O << "#" << ARM_AM::getSORegOffset(MO3.getImm());
409 }
410}
411
412void ARMAsmPrinter::printAddrMode2Operand(const MachineInstr *MI, int Op) {
413 const MachineOperand &MO1 = MI->getOperand(Op);
414 const MachineOperand &MO2 = MI->getOperand(Op+1);
415 const MachineOperand &MO3 = MI->getOperand(Op+2);
416
417 if (!MO1.isRegister()) { // FIXME: This is for CP entries, but isn't right.
418 printOperand(MI, Op);
419 return;
420 }
421
422 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
423
424 if (!MO2.getReg()) {
425 if (ARM_AM::getAM2Offset(MO3.getImm())) // Don't print +0.
426 O << ", #"
427 << (char)ARM_AM::getAM2Op(MO3.getImm())
428 << ARM_AM::getAM2Offset(MO3.getImm());
429 O << "]";
430 return;
431 }
432
433 O << ", "
434 << (char)ARM_AM::getAM2Op(MO3.getImm())
435 << TM.getRegisterInfo()->get(MO2.getReg()).Name;
436
437 if (unsigned ShImm = ARM_AM::getAM2Offset(MO3.getImm()))
438 O << ", "
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000439 << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO3.getImm()))
Evan Chenga8e29892007-01-19 07:51:42 +0000440 << " #" << ShImm;
441 O << "]";
442}
443
444void ARMAsmPrinter::printAddrMode2OffsetOperand(const MachineInstr *MI, int Op){
445 const MachineOperand &MO1 = MI->getOperand(Op);
446 const MachineOperand &MO2 = MI->getOperand(Op+1);
447
448 if (!MO1.getReg()) {
Evan Chengbdc98692007-05-03 23:30:36 +0000449 unsigned ImmOffs = ARM_AM::getAM2Offset(MO2.getImm());
450 assert(ImmOffs && "Malformed indexed load / store!");
451 O << "#"
452 << (char)ARM_AM::getAM2Op(MO2.getImm())
453 << ImmOffs;
Evan Chenga8e29892007-01-19 07:51:42 +0000454 return;
455 }
456
457 O << (char)ARM_AM::getAM2Op(MO2.getImm())
458 << TM.getRegisterInfo()->get(MO1.getReg()).Name;
459
460 if (unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm()))
461 O << ", "
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000462 << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO2.getImm()))
Evan Chenga8e29892007-01-19 07:51:42 +0000463 << " #" << ShImm;
464}
465
466void ARMAsmPrinter::printAddrMode3Operand(const MachineInstr *MI, int Op) {
467 const MachineOperand &MO1 = MI->getOperand(Op);
468 const MachineOperand &MO2 = MI->getOperand(Op+1);
469 const MachineOperand &MO3 = MI->getOperand(Op+2);
470
Dan Gohman6f0d0242008-02-10 18:45:23 +0000471 assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
Evan Chenga8e29892007-01-19 07:51:42 +0000472 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
473
474 if (MO2.getReg()) {
475 O << ", "
476 << (char)ARM_AM::getAM3Op(MO3.getImm())
477 << TM.getRegisterInfo()->get(MO2.getReg()).Name
478 << "]";
479 return;
480 }
481
482 if (unsigned ImmOffs = ARM_AM::getAM3Offset(MO3.getImm()))
483 O << ", #"
484 << (char)ARM_AM::getAM3Op(MO3.getImm())
485 << ImmOffs;
486 O << "]";
487}
488
489void ARMAsmPrinter::printAddrMode3OffsetOperand(const MachineInstr *MI, int Op){
490 const MachineOperand &MO1 = MI->getOperand(Op);
491 const MachineOperand &MO2 = MI->getOperand(Op+1);
492
493 if (MO1.getReg()) {
494 O << (char)ARM_AM::getAM3Op(MO2.getImm())
495 << TM.getRegisterInfo()->get(MO1.getReg()).Name;
496 return;
497 }
498
499 unsigned ImmOffs = ARM_AM::getAM3Offset(MO2.getImm());
Evan Chengbdc98692007-05-03 23:30:36 +0000500 assert(ImmOffs && "Malformed indexed load / store!");
Evan Chenga8e29892007-01-19 07:51:42 +0000501 O << "#"
Evan Chengbdc98692007-05-03 23:30:36 +0000502 << (char)ARM_AM::getAM3Op(MO2.getImm())
Evan Chenga8e29892007-01-19 07:51:42 +0000503 << ImmOffs;
504}
505
506void ARMAsmPrinter::printAddrMode4Operand(const MachineInstr *MI, int Op,
507 const char *Modifier) {
508 const MachineOperand &MO1 = MI->getOperand(Op);
509 const MachineOperand &MO2 = MI->getOperand(Op+1);
510 ARM_AM::AMSubMode Mode = ARM_AM::getAM4SubMode(MO2.getImm());
511 if (Modifier && strcmp(Modifier, "submode") == 0) {
512 if (MO1.getReg() == ARM::SP) {
513 bool isLDM = (MI->getOpcode() == ARM::LDM ||
514 MI->getOpcode() == ARM::LDM_RET);
515 O << ARM_AM::getAMSubModeAltStr(Mode, isLDM);
516 } else
517 O << ARM_AM::getAMSubModeStr(Mode);
518 } else {
519 printOperand(MI, Op);
520 if (ARM_AM::getAM4WBFlag(MO2.getImm()))
521 O << "!";
522 }
523}
524
525void ARMAsmPrinter::printAddrMode5Operand(const MachineInstr *MI, int Op,
526 const char *Modifier) {
527 const MachineOperand &MO1 = MI->getOperand(Op);
528 const MachineOperand &MO2 = MI->getOperand(Op+1);
529
530 if (!MO1.isRegister()) { // FIXME: This is for CP entries, but isn't right.
531 printOperand(MI, Op);
532 return;
533 }
534
Dan Gohman6f0d0242008-02-10 18:45:23 +0000535 assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
Evan Chenga8e29892007-01-19 07:51:42 +0000536
537 if (Modifier && strcmp(Modifier, "submode") == 0) {
538 ARM_AM::AMSubMode Mode = ARM_AM::getAM5SubMode(MO2.getImm());
539 if (MO1.getReg() == ARM::SP) {
540 bool isFLDM = (MI->getOpcode() == ARM::FLDMD ||
541 MI->getOpcode() == ARM::FLDMS);
542 O << ARM_AM::getAMSubModeAltStr(Mode, isFLDM);
543 } else
544 O << ARM_AM::getAMSubModeStr(Mode);
545 return;
546 } else if (Modifier && strcmp(Modifier, "base") == 0) {
547 // Used for FSTM{D|S} and LSTM{D|S} operations.
548 O << TM.getRegisterInfo()->get(MO1.getReg()).Name;
549 if (ARM_AM::getAM5WBFlag(MO2.getImm()))
550 O << "!";
551 return;
552 }
553
554 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
555
556 if (unsigned ImmOffs = ARM_AM::getAM5Offset(MO2.getImm())) {
557 O << ", #"
558 << (char)ARM_AM::getAM5Op(MO2.getImm())
559 << ImmOffs*4;
560 }
561 O << "]";
562}
563
564void ARMAsmPrinter::printAddrModePCOperand(const MachineInstr *MI, int Op,
565 const char *Modifier) {
566 if (Modifier && strcmp(Modifier, "label") == 0) {
567 printPCLabel(MI, Op+1);
568 return;
569 }
570
571 const MachineOperand &MO1 = MI->getOperand(Op);
Dan Gohman6f0d0242008-02-10 18:45:23 +0000572 assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
Evan Chenga8e29892007-01-19 07:51:42 +0000573 O << "[pc, +" << TM.getRegisterInfo()->get(MO1.getReg()).Name << "]";
574}
575
576void
577ARMAsmPrinter::printThumbAddrModeRROperand(const MachineInstr *MI, int Op) {
578 const MachineOperand &MO1 = MI->getOperand(Op);
579 const MachineOperand &MO2 = MI->getOperand(Op+1);
580 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
581 O << ", " << TM.getRegisterInfo()->get(MO2.getReg()).Name << "]";
582}
583
584void
585ARMAsmPrinter::printThumbAddrModeRI5Operand(const MachineInstr *MI, int Op,
586 unsigned Scale) {
587 const MachineOperand &MO1 = MI->getOperand(Op);
Evan Chengcea117d2007-01-30 02:35:32 +0000588 const MachineOperand &MO2 = MI->getOperand(Op+1);
589 const MachineOperand &MO3 = MI->getOperand(Op+2);
Evan Chenga8e29892007-01-19 07:51:42 +0000590
591 if (!MO1.isRegister()) { // FIXME: This is for CP entries, but isn't right.
592 printOperand(MI, Op);
593 return;
594 }
595
596 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
Evan Chengcea117d2007-01-30 02:35:32 +0000597 if (MO3.getReg())
598 O << ", " << TM.getRegisterInfo()->get(MO3.getReg()).Name;
599 else if (unsigned ImmOffs = MO2.getImm()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000600 O << ", #" << ImmOffs;
601 if (Scale > 1)
602 O << " * " << Scale;
603 }
604 O << "]";
605}
606
607void
Evan Chengc38f2bc2007-01-23 22:59:13 +0000608ARMAsmPrinter::printThumbAddrModeS1Operand(const MachineInstr *MI, int Op) {
Evan Chengcea117d2007-01-30 02:35:32 +0000609 printThumbAddrModeRI5Operand(MI, Op, 1);
Evan Chenga8e29892007-01-19 07:51:42 +0000610}
611void
Evan Chengc38f2bc2007-01-23 22:59:13 +0000612ARMAsmPrinter::printThumbAddrModeS2Operand(const MachineInstr *MI, int Op) {
Evan Chengcea117d2007-01-30 02:35:32 +0000613 printThumbAddrModeRI5Operand(MI, Op, 2);
Evan Chenga8e29892007-01-19 07:51:42 +0000614}
615void
Evan Chengc38f2bc2007-01-23 22:59:13 +0000616ARMAsmPrinter::printThumbAddrModeS4Operand(const MachineInstr *MI, int Op) {
Evan Chengcea117d2007-01-30 02:35:32 +0000617 printThumbAddrModeRI5Operand(MI, Op, 4);
Evan Chenga8e29892007-01-19 07:51:42 +0000618}
619
620void ARMAsmPrinter::printThumbAddrModeSPOperand(const MachineInstr *MI,int Op) {
621 const MachineOperand &MO1 = MI->getOperand(Op);
622 const MachineOperand &MO2 = MI->getOperand(Op+1);
623 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).Name;
624 if (unsigned ImmOffs = MO2.getImm())
625 O << ", #" << ImmOffs << " * 4";
626 O << "]";
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000627}
628
Evan Cheng42d712b2007-05-08 21:08:43 +0000629void ARMAsmPrinter::printPredicateOperand(const MachineInstr *MI, int opNum) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000630 ARMCC::CondCodes CC = (ARMCC::CondCodes)MI->getOperand(opNum).getImm();
Evan Cheng44bec522007-05-15 01:29:07 +0000631 if (CC != ARMCC::AL)
632 O << ARMCondCodeToString(CC);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000633}
634
Evan Chengdfb2eba2007-07-06 01:01:34 +0000635void ARMAsmPrinter::printSBitModifierOperand(const MachineInstr *MI, int opNum){
636 unsigned Reg = MI->getOperand(opNum).getReg();
637 if (Reg) {
638 assert(Reg == ARM::CPSR && "Expect ARM CPSR register!");
639 O << 's';
640 }
641}
642
Evan Chenga8e29892007-01-19 07:51:42 +0000643void ARMAsmPrinter::printPCLabel(const MachineInstr *MI, int opNum) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000644 int Id = (int)MI->getOperand(opNum).getImm();
Evan Chenga8e29892007-01-19 07:51:42 +0000645 O << TAI->getPrivateGlobalPrefix() << "PC" << Id;
646}
647
648void ARMAsmPrinter::printRegisterList(const MachineInstr *MI, int opNum) {
649 O << "{";
650 for (unsigned i = opNum, e = MI->getNumOperands(); i != e; ++i) {
651 printOperand(MI, i);
652 if (i != e-1) O << ", ";
653 }
654 O << "}";
655}
656
657void ARMAsmPrinter::printCPInstOperand(const MachineInstr *MI, int OpNo,
658 const char *Modifier) {
659 assert(Modifier && "This operand only works with a modifier!");
660 // There are two aspects to a CONSTANTPOOL_ENTRY operand, the label and the
661 // data itself.
662 if (!strcmp(Modifier, "label")) {
663 unsigned ID = MI->getOperand(OpNo).getImm();
Evan Cheng347d39f2007-10-14 05:57:21 +0000664 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
665 << '_' << ID << ":\n";
Evan Chenga8e29892007-01-19 07:51:42 +0000666 } else {
667 assert(!strcmp(Modifier, "cpentry") && "Unknown modifier for CPE");
Chris Lattner8aa797a2007-12-30 23:10:15 +0000668 unsigned CPI = MI->getOperand(OpNo).getIndex();
Evan Chenga8e29892007-01-19 07:51:42 +0000669
670 const MachineConstantPoolEntry &MCPE = // Chasing pointers is fun?
671 MI->getParent()->getParent()->getConstantPool()->getConstants()[CPI];
672
673 if (MCPE.isMachineConstantPoolEntry())
674 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
Lauro Ramos Venancio305b8a52007-04-25 14:50:40 +0000675 else {
Evan Chenga8e29892007-01-19 07:51:42 +0000676 EmitGlobalConstant(MCPE.Val.ConstVal);
Lauro Ramos Venancio305b8a52007-04-25 14:50:40 +0000677 // remember to emit the weak reference
678 if (const GlobalValue *GV = dyn_cast<GlobalValue>(MCPE.Val.ConstVal))
679 if (GV->hasExternalWeakLinkage())
680 ExtWeakSymbols.insert(GV);
681 }
Evan Chenga8e29892007-01-19 07:51:42 +0000682 }
683}
684
685void ARMAsmPrinter::printJTBlockOperand(const MachineInstr *MI, int OpNo) {
686 const MachineOperand &MO1 = MI->getOperand(OpNo);
687 const MachineOperand &MO2 = MI->getOperand(OpNo+1); // Unique Id
Chris Lattner8aa797a2007-12-30 23:10:15 +0000688 unsigned JTI = MO1.getIndex();
Evan Cheng347d39f2007-10-14 05:57:21 +0000689 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000690 << '_' << JTI << '_' << MO2.getImm() << ":\n";
Evan Chenga8e29892007-01-19 07:51:42 +0000691
692 const char *JTEntryDirective = TAI->getJumpTableDirective();
693 if (!JTEntryDirective)
694 JTEntryDirective = TAI->getData32bitsDirective();
695
696 const MachineFunction *MF = MI->getParent()->getParent();
697 MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
698 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
699 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
700 bool UseSet= TAI->getSetDirective() && TM.getRelocationModel() == Reloc::PIC_;
701 std::set<MachineBasicBlock*> JTSets;
702 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
703 MachineBasicBlock *MBB = JTBBs[i];
704 if (UseSet && JTSets.insert(MBB).second)
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000705 printPICJumpTableSetLabel(JTI, MO2.getImm(), MBB);
Evan Chenga8e29892007-01-19 07:51:42 +0000706
707 O << JTEntryDirective << ' ';
708 if (UseSet)
Evan Cheng347d39f2007-10-14 05:57:21 +0000709 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000710 << '_' << JTI << '_' << MO2.getImm()
Evan Cheng347d39f2007-10-14 05:57:21 +0000711 << "_set_" << MBB->getNumber();
Evan Chenga8e29892007-01-19 07:51:42 +0000712 else if (TM.getRelocationModel() == Reloc::PIC_) {
713 printBasicBlockLabel(MBB, false, false);
714 // If the arch uses custom Jump Table directives, don't calc relative to JT
715 if (!TAI->getJumpTableDirective())
716 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000717 << getFunctionNumber() << '_' << JTI << '_' << MO2.getImm();
Evan Chenga8e29892007-01-19 07:51:42 +0000718 } else
719 printBasicBlockLabel(MBB, false, false);
Evan Chengd85ac4d2007-01-27 02:29:45 +0000720 if (i != e-1)
721 O << '\n';
Evan Chenga8e29892007-01-19 07:51:42 +0000722 }
723}
724
725
726bool ARMAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
727 unsigned AsmVariant, const char *ExtraCode){
728 // Does this asm operand have a single letter operand modifier?
729 if (ExtraCode && ExtraCode[0]) {
730 if (ExtraCode[1] != 0) return true; // Unknown modifier.
731
732 switch (ExtraCode[0]) {
733 default: return true; // Unknown modifier.
Evan Cheng23a95702007-03-08 22:42:46 +0000734 case 'c': // Don't print "$" before a global var name or constant.
Evan Chenge21e3962007-04-04 00:13:29 +0000735 case 'P': // Print a VFP double precision register.
Evan Cheng23a95702007-03-08 22:42:46 +0000736 printOperand(MI, OpNo);
737 return false;
Evan Chenga8e29892007-01-19 07:51:42 +0000738 case 'Q':
739 if (TM.getTargetData()->isLittleEndian())
740 break;
741 // Fallthrough
742 case 'R':
743 if (TM.getTargetData()->isBigEndian())
744 break;
745 // Fallthrough
746 case 'H': // Write second word of DI / DF reference.
747 // Verify that this operand has two consecutive registers.
748 if (!MI->getOperand(OpNo).isRegister() ||
749 OpNo+1 == MI->getNumOperands() ||
750 !MI->getOperand(OpNo+1).isRegister())
751 return true;
752 ++OpNo; // Return the high-part.
753 }
754 }
755
756 printOperand(MI, OpNo);
757 return false;
758}
759
760void ARMAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
761 ++EmittedInsts;
762
Evan Chengc60e76d2007-01-30 20:37:08 +0000763 int Opc = MI->getOpcode();
764 switch (Opc) {
765 case ARM::CONSTPOOL_ENTRY:
Evan Chenga8e29892007-01-19 07:51:42 +0000766 if (!InCPMode && AFI->isThumbFunction()) {
767 EmitAlignment(2);
768 InCPMode = true;
769 }
Evan Chengc60e76d2007-01-30 20:37:08 +0000770 break;
771 default: {
Evan Cheng3bf12d02007-01-31 23:39:39 +0000772 if (InCPMode && AFI->isThumbFunction())
Evan Chenga8e29892007-01-19 07:51:42 +0000773 InCPMode = false;
Evan Chengc60e76d2007-01-30 20:37:08 +0000774 switch (Opc) {
775 case ARM::PICADD:
776 case ARM::PICLD:
Evan Cheng341dccc2007-06-05 07:36:38 +0000777 case ARM::PICLDZH:
778 case ARM::PICLDZB:
779 case ARM::PICLDH:
780 case ARM::PICLDB:
781 case ARM::PICLDSH:
782 case ARM::PICLDSB:
783 case ARM::PICSTR:
784 case ARM::PICSTRH:
785 case ARM::PICSTRB:
Evan Chengc60e76d2007-01-30 20:37:08 +0000786 case ARM::tPICADD:
787 break;
788 default:
Evan Chengc60e76d2007-01-30 20:37:08 +0000789 break;
790 }
791 }}
Evan Chenga8e29892007-01-19 07:51:42 +0000792
793 // Call the autogenerated instruction printer routines.
794 printInstruction(MI);
795}
796
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000797bool ARMAsmPrinter::doInitialization(Module &M) {
Lauro Ramos Venancioe8e54952007-05-03 20:28:35 +0000798 // Emit initial debug information.
799 DW.BeginModule(&M);
Evan Chenga8e29892007-01-19 07:51:42 +0000800
Dan Gohmanb8275a32007-07-25 19:33:14 +0000801 bool Result = AsmPrinter::doInitialization(M);
Dale Johannesen54118352007-06-22 00:54:56 +0000802
803 // Darwin wants symbols to be quoted if they have complex names.
804 if (Subtarget->isTargetDarwin())
805 Mang->setUseQuotes(true);
806
Dan Gohmanb8275a32007-07-25 19:33:14 +0000807 return Result;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000808}
809
Chris Lattnerec321b42008-02-15 19:04:54 +0000810/// PrintUnmangledNameSafely - Print out the printable characters in the name.
811/// Don't print things like \n or \0.
812static void PrintUnmangledNameSafely(const Value *V, std::ostream &OS) {
813 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
814 Name != E; ++Name)
815 if (isprint(*Name))
816 OS << *Name;
817}
818
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000819bool ARMAsmPrinter::doFinalization(Module &M) {
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000820 const TargetData *TD = TM.getTargetData();
821
822 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
823 I != E; ++I) {
824 if (!I->hasInitializer()) // External global require no code
825 continue;
826
Evan Chengb267ca12007-01-30 08:04:53 +0000827 if (EmitSpecialLLVMGlobal(I)) {
828 if (Subtarget->isTargetDarwin() &&
829 TM.getRelocationModel() == Reloc::Static) {
830 if (I->getName() == "llvm.global_ctors")
831 O << ".reference .constructors_used\n";
832 else if (I->getName() == "llvm.global_dtors")
833 O << ".reference .destructors_used\n";
834 }
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000835 continue;
Evan Chengb267ca12007-01-30 08:04:53 +0000836 }
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000837
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000838 std::string name = Mang->getValueName(I);
839 Constant *C = I->getInitializer();
Evan Cheng98ded762007-03-08 01:25:25 +0000840 const Type *Type = C->getType();
Duncan Sandsca0ed742007-11-05 00:04:43 +0000841 unsigned Size = TD->getABITypeSize(Type);
Evan Chenga8e29892007-01-19 07:51:42 +0000842 unsigned Align = TD->getPreferredAlignmentLog(I);
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000843
Lauro Ramos Venancio0a181732007-04-30 00:23:51 +0000844 const char *VisibilityDirective = NULL;
Evan Cheng5be54b02007-01-19 19:25:36 +0000845 if (I->hasHiddenVisibility())
Lauro Ramos Venancio0a181732007-04-30 00:23:51 +0000846 VisibilityDirective = TAI->getHiddenDirective();
847 else if (I->hasProtectedVisibility())
848 VisibilityDirective = TAI->getProtectedDirective();
849
850 if (VisibilityDirective)
851 O << VisibilityDirective << name << "\n";
852
Evan Cheng5be54b02007-01-19 19:25:36 +0000853 if (Subtarget->isTargetELF())
Lauro Ramos Venanciob1cc0522007-01-25 20:11:04 +0000854 O << "\t.type " << name << ",%object\n";
Evan Cheng5be54b02007-01-19 19:25:36 +0000855
Lauro Ramos Venancio8699a972007-11-05 18:33:37 +0000856 if (C->isNullValue() && !I->hasSection() && !I->isThreadLocal()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000857 if (I->hasExternalLinkage()) {
Evan Cheng5be54b02007-01-19 19:25:36 +0000858 if (const char *Directive = TAI->getZeroFillDirective()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000859 O << "\t.globl\t" << name << "\n";
Dale Johannesen3006c392008-02-12 23:35:09 +0000860 O << Directive << "__DATA, __common, " << name << ", "
Evan Cheng5be54b02007-01-19 19:25:36 +0000861 << Size << ", " << Align << "\n";
862 continue;
863 }
864 }
865
Dale Johannesen25a01952008-01-17 23:36:04 +0000866 if (I->hasInternalLinkage() || I->hasWeakLinkage() ||
867 I->hasLinkOnceLinkage()) {
Evan Cheng5be54b02007-01-19 19:25:36 +0000868 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
869 if (!NoZerosInBSS && TAI->getBSSSection())
870 SwitchToDataSection(TAI->getBSSSection(), I);
871 else
872 SwitchToDataSection(TAI->getDataSection(), I);
Evan Chenga8e29892007-01-19 07:51:42 +0000873 if (TAI->getLCOMMDirective() != NULL) {
874 if (I->hasInternalLinkage()) {
875 O << TAI->getLCOMMDirective() << name << "," << Size;
Evan Cheng5be54b02007-01-19 19:25:36 +0000876 if (Subtarget->isTargetDarwin())
Evan Chenga8e29892007-01-19 07:51:42 +0000877 O << "," << Align;
878 } else
879 O << TAI->getCOMMDirective() << name << "," << Size;
880 } else {
881 if (I->hasInternalLinkage())
882 O << "\t.local\t" << name << "\n";
883 O << TAI->getCOMMDirective() << name << "," << Size;
884 if (TAI->getCOMMDirectiveTakesAlignment())
885 O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
886 }
Chris Lattnerec321b42008-02-15 19:04:54 +0000887 O << "\t\t" << TAI->getCommentString() << " ";
888 PrintUnmangledNameSafely(I, O);
889 O << "\n";
Evan Cheng5be54b02007-01-19 19:25:36 +0000890 continue;
Evan Chenga8e29892007-01-19 07:51:42 +0000891 }
Evan Cheng5be54b02007-01-19 19:25:36 +0000892 }
Evan Chenga8e29892007-01-19 07:51:42 +0000893
Evan Cheng5be54b02007-01-19 19:25:36 +0000894 switch (I->getLinkage()) {
895 case GlobalValue::LinkOnceLinkage:
896 case GlobalValue::WeakLinkage:
897 if (Subtarget->isTargetDarwin()) {
898 O << "\t.globl " << name << "\n"
899 << "\t.weak_definition " << name << "\n";
Dale Johannesen12591d72008-01-11 01:59:45 +0000900 SwitchToDataSection("\t.section __DATA,__datacoal_nt,coalesced", I);
Evan Cheng5be54b02007-01-19 19:25:36 +0000901 } else {
902 std::string SectionName("\t.section\t.llvm.linkonce.d." +
903 name +
904 ",\"aw\",%progbits");
905 SwitchToDataSection(SectionName.c_str(), I);
906 O << "\t.weak " << name << "\n";
907 }
908 break;
909 case GlobalValue::AppendingLinkage:
910 // FIXME: appending linkage variables should go into a section of
911 // their name or something. For now, just emit them as external.
912 case GlobalValue::ExternalLinkage:
913 O << "\t.globl " << name << "\n";
914 // FALL THROUGH
915 case GlobalValue::InternalLinkage: {
916 if (I->isConstant()) {
917 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
918 if (TAI->getCStringSection() && CVA && CVA->isCString()) {
919 SwitchToDataSection(TAI->getCStringSection(), I);
920 break;
921 }
922 }
923 // FIXME: special handling for ".ctors" & ".dtors" sections
924 if (I->hasSection() &&
925 (I->getSection() == ".ctors" ||
926 I->getSection() == ".dtors")) {
927 assert(!Subtarget->isTargetDarwin());
928 std::string SectionName = ".section " + I->getSection();
929 SectionName += ",\"aw\",%progbits";
930 SwitchToDataSection(SectionName.c_str());
Dale Johannesen25edeb32008-01-23 00:58:14 +0000931 } else if (I->hasSection() && Subtarget->isTargetDarwin()) {
932 // Honor all section names on Darwin; ObjC uses this
933 std::string SectionName = ".section " + I->getSection();
934 SwitchToDataSection(SectionName.c_str());
Evan Cheng5be54b02007-01-19 19:25:36 +0000935 } else {
936 if (C->isNullValue() && !NoZerosInBSS && TAI->getBSSSection())
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000937 SwitchToDataSection(I->isThreadLocal() ? TAI->getTLSBSSSection() :
938 TAI->getBSSSection(), I);
Evan Cheng98ded762007-03-08 01:25:25 +0000939 else if (!I->isConstant())
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000940 SwitchToDataSection(I->isThreadLocal() ? TAI->getTLSDataSection() :
941 TAI->getDataSection(), I);
942 else if (I->isThreadLocal())
943 SwitchToDataSection(TAI->getTLSDataSection());
Evan Cheng98ded762007-03-08 01:25:25 +0000944 else {
945 // Read-only data.
Evan Cheng032953d2007-03-08 08:31:54 +0000946 bool HasReloc = C->ContainsRelocations();
947 if (HasReloc &&
948 Subtarget->isTargetDarwin() &&
Evan Cheng98ded762007-03-08 01:25:25 +0000949 TM.getRelocationModel() != Reloc::Static)
950 SwitchToDataSection("\t.const_data\n");
Evan Cheng032953d2007-03-08 08:31:54 +0000951 else if (!HasReloc && Size == 4 &&
Evan Cheng98ded762007-03-08 01:25:25 +0000952 TAI->getFourByteConstantSection())
953 SwitchToDataSection(TAI->getFourByteConstantSection(), I);
Evan Cheng032953d2007-03-08 08:31:54 +0000954 else if (!HasReloc && Size == 8 &&
Evan Cheng98ded762007-03-08 01:25:25 +0000955 TAI->getEightByteConstantSection())
956 SwitchToDataSection(TAI->getEightByteConstantSection(), I);
Evan Cheng032953d2007-03-08 08:31:54 +0000957 else if (!HasReloc && Size == 16 &&
Evan Cheng98ded762007-03-08 01:25:25 +0000958 TAI->getSixteenByteConstantSection())
959 SwitchToDataSection(TAI->getSixteenByteConstantSection(), I);
960 else if (TAI->getReadOnlySection())
961 SwitchToDataSection(TAI->getReadOnlySection(), I);
962 else
963 SwitchToDataSection(TAI->getDataSection(), I);
964 }
Rafael Espindolab97809c2006-10-19 13:30:40 +0000965 }
Evan Cheng5be54b02007-01-19 19:25:36 +0000966
967 break;
968 }
969 default:
970 assert(0 && "Unknown linkage type!");
971 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000972 }
Rafael Espindolab97809c2006-10-19 13:30:40 +0000973
Evan Chenga8e29892007-01-19 07:51:42 +0000974 EmitAlignment(Align, I);
Chris Lattnerec321b42008-02-15 19:04:54 +0000975 O << name << ":\t\t\t\t" << TAI->getCommentString() << " ";
976 PrintUnmangledNameSafely(I, O);
977 O << "\n";
Evan Cheng5be54b02007-01-19 19:25:36 +0000978 if (TAI->hasDotTypeDotSizeDirective())
Rafael Espindolab97809c2006-10-19 13:30:40 +0000979 O << "\t.size " << name << ", " << Size << "\n";
Evan Chenga8e29892007-01-19 07:51:42 +0000980 // If the initializer is a extern weak symbol, remember to emit the weak
981 // reference!
982 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
983 if (GV->hasExternalWeakLinkage())
984 ExtWeakSymbols.insert(GV);
985
986 EmitGlobalConstant(C);
987 O << '\n';
988 }
989
Evan Cheng5be54b02007-01-19 19:25:36 +0000990 if (Subtarget->isTargetDarwin()) {
991 SwitchToDataSection("");
992
Evan Chenga8e29892007-01-19 07:51:42 +0000993 // Output stubs for dynamically-linked functions
994 unsigned j = 1;
995 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
996 i != e; ++i, ++j) {
997 if (TM.getRelocationModel() == Reloc::PIC_)
998 SwitchToTextSection(".section __TEXT,__picsymbolstub4,symbol_stubs,"
999 "none,16", 0);
1000 else
1001 SwitchToTextSection(".section __TEXT,__symbol_stub4,symbol_stubs,"
1002 "none,12", 0);
1003
1004 EmitAlignment(2);
1005 O << "\t.code\t32\n";
1006
1007 O << "L" << *i << "$stub:\n";
1008 O << "\t.indirect_symbol " << *i << "\n";
1009 O << "\tldr ip, L" << *i << "$slp\n";
1010 if (TM.getRelocationModel() == Reloc::PIC_) {
1011 O << "L" << *i << "$scv:\n";
1012 O << "\tadd ip, pc, ip\n";
1013 }
1014 O << "\tldr pc, [ip, #0]\n";
1015 O << "L" << *i << "$slp:\n";
1016 if (TM.getRelocationModel() == Reloc::PIC_)
1017 O << "\t.long\tL" << *i << "$lazy_ptr-(L" << *i << "$scv+8)\n";
1018 else
1019 O << "\t.long\tL" << *i << "$lazy_ptr\n";
1020 SwitchToDataSection(".lazy_symbol_pointer", 0);
1021 O << "L" << *i << "$lazy_ptr:\n";
1022 O << "\t.indirect_symbol " << *i << "\n";
1023 O << "\t.long\tdyld_stub_binding_helper\n";
1024 }
1025 O << "\n";
1026
1027 // Output non-lazy-pointers for external and common global variables.
Dan Gohmancb406c22007-10-03 19:26:29 +00001028 if (!GVNonLazyPtrs.empty())
Evan Chenga8e29892007-01-19 07:51:42 +00001029 SwitchToDataSection(".non_lazy_symbol_pointer", 0);
1030 for (std::set<std::string>::iterator i = GVNonLazyPtrs.begin(),
1031 e = GVNonLazyPtrs.end(); i != e; ++i) {
1032 O << "L" << *i << "$non_lazy_ptr:\n";
1033 O << "\t.indirect_symbol " << *i << "\n";
1034 O << "\t.long\t0\n";
1035 }
1036
1037 // Emit initial debug information.
1038 DW.EndModule();
1039
1040 // Funny Darwin hack: This flag tells the linker that no global symbols
1041 // contain code that falls through to other global symbols (e.g. the obvious
1042 // implementation of multiple entry points). If this doesn't occur, the
1043 // linker can safely perform dead code stripping. Since LLVM never
1044 // generates code that does this, it is always safe to set.
1045 O << "\t.subsections_via_symbols\n";
Lauro Ramos Venancioe8e54952007-05-03 20:28:35 +00001046 } else {
1047 // Emit final debug information for ELF.
1048 DW.EndModule();
Rafael Espindolab01c4bb2006-07-27 11:38:51 +00001049 }
Rafael Espindolab97809c2006-10-19 13:30:40 +00001050
Dan Gohmanb8275a32007-07-25 19:33:14 +00001051 return AsmPrinter::doFinalization(M);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00001052}