blob: e7e8f276a1f0d36a1151b30677a1842f705d82ae [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"
Evan Cheng711b6dc2008-08-08 06:56:16 +000032#include "llvm/ADT/SmallPtrSet.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)
Dale Johannesenf2452c52008-07-09 21:20:54 +000046 : AsmPrinter(O, TM, T), DW(O, this, T), MMI(NULL), AFI(NULL),
47 InCPMode(false) {
Evan Chenga8e29892007-01-19 07:51:42 +000048 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Jim Laskey563321a2006-09-06 18:34:40 +000049 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000050
Evan Chenga8e29892007-01-19 07:51:42 +000051 DwarfWriter DW;
Dale Johannesenf2452c52008-07-09 21:20:54 +000052 MachineModuleInfo *MMI;
Evan Chenga8e29892007-01-19 07:51:42 +000053
54 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
55 /// make the right decision when printing asm code for different targets.
56 const ARMSubtarget *Subtarget;
57
58 /// AFI - Keep a pointer to ARMFunctionInfo for the current
59 /// MachineFunction
60 ARMFunctionInfo *AFI;
61
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000062 /// We name each basic block in a Function with a unique number, so
63 /// that we can consistently refer to them later. This is cleared
64 /// at the beginning of each call to runOnMachineFunction().
65 ///
66 typedef std::map<const Value *, unsigned> ValueMapTy;
67 ValueMapTy NumberForBB;
68
Evan Cheng711b6dc2008-08-08 06:56:16 +000069 /// GVNonLazyPtrs - Keeps the set of GlobalValues that require
70 /// non-lazy-pointers for indirect access.
Evan Chenga8e29892007-01-19 07:51:42 +000071 std::set<std::string> GVNonLazyPtrs;
72
Evan Cheng711b6dc2008-08-08 06:56:16 +000073 /// FnStubs - Keeps the set of external function GlobalAddresses that the
74 /// asm printer should generate stubs for.
Evan Chenga8e29892007-01-19 07:51:42 +000075 std::set<std::string> FnStubs;
76
Evan Cheng711b6dc2008-08-08 06:56:16 +000077 /// PCRelGVs - Keeps the set of GlobalValues used in pc relative
78 /// constantpool.
79 SmallPtrSet<const GlobalValue*, 8> PCRelGVs;
80
Evan Chenga8e29892007-01-19 07:51:42 +000081 /// True if asm printer is printing a series of CONSTPOOL_ENTRY.
82 bool InCPMode;
83
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000084 virtual const char *getPassName() const {
85 return "ARM Assembly Printer";
86 }
87
Evan Chenga8e29892007-01-19 07:51:42 +000088 void printOperand(const MachineInstr *MI, int opNum,
89 const char *Modifier = 0);
90 void printSOImmOperand(const MachineInstr *MI, int opNum);
Evan Chengc70d1842007-03-20 08:11:30 +000091 void printSOImm2PartOperand(const MachineInstr *MI, int opNum);
Evan Chenga8e29892007-01-19 07:51:42 +000092 void printSORegOperand(const MachineInstr *MI, int opNum);
93 void printAddrMode2Operand(const MachineInstr *MI, int OpNo);
94 void printAddrMode2OffsetOperand(const MachineInstr *MI, int OpNo);
95 void printAddrMode3Operand(const MachineInstr *MI, int OpNo);
96 void printAddrMode3OffsetOperand(const MachineInstr *MI, int OpNo);
97 void printAddrMode4Operand(const MachineInstr *MI, int OpNo,
98 const char *Modifier = 0);
99 void printAddrMode5Operand(const MachineInstr *MI, int OpNo,
100 const char *Modifier = 0);
101 void printAddrModePCOperand(const MachineInstr *MI, int OpNo,
102 const char *Modifier = 0);
103 void printThumbAddrModeRROperand(const MachineInstr *MI, int OpNo);
104 void printThumbAddrModeRI5Operand(const MachineInstr *MI, int OpNo,
105 unsigned Scale);
Evan Chengc38f2bc2007-01-23 22:59:13 +0000106 void printThumbAddrModeS1Operand(const MachineInstr *MI, int OpNo);
107 void printThumbAddrModeS2Operand(const MachineInstr *MI, int OpNo);
108 void printThumbAddrModeS4Operand(const MachineInstr *MI, int OpNo);
Evan Chenga8e29892007-01-19 07:51:42 +0000109 void printThumbAddrModeSPOperand(const MachineInstr *MI, int OpNo);
Evan Cheng42d712b2007-05-08 21:08:43 +0000110 void printPredicateOperand(const MachineInstr *MI, int opNum);
Evan Chengdfb2eba2007-07-06 01:01:34 +0000111 void printSBitModifierOperand(const MachineInstr *MI, int opNum);
Evan Chenga8e29892007-01-19 07:51:42 +0000112 void printPCLabel(const MachineInstr *MI, int opNum);
113 void printRegisterList(const MachineInstr *MI, int opNum);
114 void printCPInstOperand(const MachineInstr *MI, int opNum,
115 const char *Modifier);
116 void printJTBlockOperand(const MachineInstr *MI, int opNum);
117
118 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
119 unsigned AsmVariant, const char *ExtraCode);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000120
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +0000121 void printModuleLevelGV(const GlobalVariable* GVar);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000122 bool printInstruction(const MachineInstr *MI); // autogenerated.
Evan Chenga8e29892007-01-19 07:51:42 +0000123 void printMachineInstruction(const MachineInstr *MI);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000124 bool runOnMachineFunction(MachineFunction &F);
125 bool doInitialization(Module &M);
126 bool doFinalization(Module &M);
Evan Chenga8e29892007-01-19 07:51:42 +0000127
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +0000128 /// getSectionForFunction - Return the section that we should emit the
129 /// specified function body into.
130 virtual std::string getSectionForFunction(const Function &F) const;
131
Evan Cheng711b6dc2008-08-08 06:56:16 +0000132 /// EmitMachineConstantPoolValue - Print a machine constantpool value to
133 /// the .s file.
Evan Chenga8e29892007-01-19 07:51:42 +0000134 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
135 printDataDirective(MCPV->getType());
136
Evan Cheng711b6dc2008-08-08 06:56:16 +0000137 ARMConstantPoolValue *ACPV = static_cast<ARMConstantPoolValue*>(MCPV);
Lauro Ramos Venancio1a92d942007-01-26 19:51:32 +0000138 GlobalValue *GV = ACPV->getGV();
Evan Chengc60e76d2007-01-30 20:37:08 +0000139 std::string Name = GV ? Mang->getValueName(GV) : TAI->getGlobalPrefix();
140 if (!GV)
141 Name += ACPV->getSymbol();
Evan Chenga8e29892007-01-19 07:51:42 +0000142 if (ACPV->isNonLazyPointer()) {
143 GVNonLazyPtrs.insert(Name);
Dale Johannesenc215b3e2008-05-19 21:38:18 +0000144 printSuffixedName(Name, "$non_lazy_ptr");
Evan Chengc60e76d2007-01-30 20:37:08 +0000145 } else if (ACPV->isStub()) {
146 FnStubs.insert(Name);
Dale Johannesenc215b3e2008-05-19 21:38:18 +0000147 printSuffixedName(Name, "$stub");
Evan Chenga8e29892007-01-19 07:51:42 +0000148 } else
149 O << Name;
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000150 if (ACPV->hasModifier()) O << "(" << ACPV->getModifier() << ")";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000151 if (ACPV->getPCAdjustment() != 0) {
Evan Chenga8e29892007-01-19 07:51:42 +0000152 O << "-(" << TAI->getPrivateGlobalPrefix() << "PC"
153 << utostr(ACPV->getLabelId())
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000154 << "+" << (unsigned)ACPV->getPCAdjustment();
155 if (ACPV->mustAddCurrentAddress())
156 O << "-.";
157 O << ")";
158 }
Evan Chenga8e29892007-01-19 07:51:42 +0000159 O << "\n";
Lauro Ramos Venancio1a92d942007-01-26 19:51:32 +0000160
161 // If the constant pool value is a extern weak symbol, remember to emit
162 // the weak reference.
Evan Chengc60e76d2007-01-30 20:37:08 +0000163 if (GV && GV->hasExternalWeakLinkage())
Lauro Ramos Venancio1a92d942007-01-26 19:51:32 +0000164 ExtWeakSymbols.insert(GV);
Evan Chenga8e29892007-01-19 07:51:42 +0000165 }
166
167 void getAnalysisUsage(AnalysisUsage &AU) const {
Gordon Henriksencd8bc052007-09-30 13:39:29 +0000168 AsmPrinter::getAnalysisUsage(AU);
Evan Chenga8e29892007-01-19 07:51:42 +0000169 AU.setPreservesAll();
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000170 AU.addRequired<MachineModuleInfo>();
Evan Chenga8e29892007-01-19 07:51:42 +0000171 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000172 };
173} // end of anonymous namespace
174
175#include "ARMGenAsmWriter.inc"
176
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +0000177// Substitute old hook with new one temporary
178std::string ARMAsmPrinter::getSectionForFunction(const Function &F) const {
179 return TAI->SectionForGlobal(&F);
180}
181
Evan Chenga8e29892007-01-19 07:51:42 +0000182/// runOnMachineFunction - This uses the printInstruction()
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000183/// method to print assembly for each instruction.
184///
185bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Evan Chenga8e29892007-01-19 07:51:42 +0000186 AFI = MF.getInfo<ARMFunctionInfo>();
Rafael Espindola4b442b52006-05-23 02:48:20 +0000187
Evan Chenga8e29892007-01-19 07:51:42 +0000188 SetupMachineFunction(MF);
189 O << "\n";
Rafael Espindola4b442b52006-05-23 02:48:20 +0000190
Evan Chenga8e29892007-01-19 07:51:42 +0000191 // NOTE: we don't print out constant pools here, they are handled as
192 // instructions.
193
194 O << "\n";
Rafael Espindola4b442b52006-05-23 02:48:20 +0000195 // Print out labels for the function.
196 const Function *F = MF.getFunction();
197 switch (F->getLinkage()) {
198 default: assert(0 && "Unknown linkage type!");
199 case Function::InternalLinkage:
Evan Chenga8e29892007-01-19 07:51:42 +0000200 SwitchToTextSection("\t.text", F);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000201 break;
202 case Function::ExternalLinkage:
Evan Chenga8e29892007-01-19 07:51:42 +0000203 SwitchToTextSection("\t.text", F);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000204 O << "\t.globl\t" << CurrentFnName << "\n";
205 break;
206 case Function::WeakLinkage:
207 case Function::LinkOnceLinkage:
Evan Cheng5be54b02007-01-19 19:25:36 +0000208 if (Subtarget->isTargetDarwin()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000209 SwitchToTextSection(
210 ".section __TEXT,__textcoal_nt,coalesced,pure_instructions", F);
211 O << "\t.globl\t" << CurrentFnName << "\n";
212 O << "\t.weak_definition\t" << CurrentFnName << "\n";
213 } else {
214 O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
215 }
Rafael Espindola4b442b52006-05-23 02:48:20 +0000216 break;
217 }
Evan Chenga8e29892007-01-19 07:51:42 +0000218
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000219 printVisibility(CurrentFnName, F->getVisibility());
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.
Evan Cheng6aa38982008-08-18 08:52:48 +0000234 // FIXME: Dwarf support.
235 //DW.BeginFunction(&MF);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000236
Bill Wendling200e90c2008-01-28 09:15:03 +0000237 if (Subtarget->isTargetDarwin()) {
238 // If the function is empty, then we need to emit *something*. Otherwise,
239 // the function's label might be associated with something that it wasn't
240 // meant to be associated with. We emit a noop in this situation.
241 MachineFunction::iterator I = MF.begin();
242
243 if (++I == MF.end() && MF.front().empty())
244 O << "\tnop\n";
245 }
246
Rafael Espindola4b442b52006-05-23 02:48:20 +0000247 // Print out code for the function.
248 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
249 I != E; ++I) {
250 // Print a label for the basic block.
251 if (I != MF.begin()) {
Evan Chengfb8075d2008-02-28 00:43:03 +0000252 printBasicBlockLabel(I, true, true);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000253 O << '\n';
254 }
255 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
256 II != E; ++II) {
257 // Print the assembly for the instruction.
Evan Chenga8e29892007-01-19 07:51:42 +0000258 printMachineInstruction(II);
Rafael Espindola4b442b52006-05-23 02:48:20 +0000259 }
260 }
261
Evan Chenga8e29892007-01-19 07:51:42 +0000262 if (TAI->hasDotTypeDotSizeDirective())
263 O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
264
Lauro Ramos Venancioe8e54952007-05-03 20:28:35 +0000265 // Emit post-function debug information.
Evan Cheng6aa38982008-08-18 08:52:48 +0000266 // FIXME: Dwarf support.
267 //DW.EndFunction();
Evan Chenga8e29892007-01-19 07:51:42 +0000268
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000269 return false;
270}
271
Evan Chenga8e29892007-01-19 07:51:42 +0000272void ARMAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
273 const char *Modifier) {
274 const MachineOperand &MO = MI->getOperand(opNum);
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000275 switch (MO.getType()) {
276 case MachineOperand::MO_Register:
Dan Gohman6f0d0242008-02-10 18:45:23 +0000277 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Bill Wendling74ab84c2008-02-26 21:11:01 +0000278 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000279 else
280 assert(0 && "not implemented");
281 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000282 case MachineOperand::MO_Immediate: {
283 if (!Modifier || strcmp(Modifier, "no_hash") != 0)
284 O << "#";
285
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000286 O << (int)MO.getImm();
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000287 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000288 }
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000289 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000290 printBasicBlockLabel(MO.getMBB());
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000291 return;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000292 case MachineOperand::MO_GlobalAddress: {
Evan Chenga8e29892007-01-19 07:51:42 +0000293 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Rafael Espindola84b19be2006-07-16 01:02:57 +0000294 GlobalValue *GV = MO.getGlobal();
295 std::string Name = Mang->getValueName(GV);
Reid Spencer5cbf9852007-01-30 20:08:39 +0000296 bool isExt = (GV->isDeclaration() || GV->hasWeakLinkage() ||
Evan Chenga8e29892007-01-19 07:51:42 +0000297 GV->hasLinkOnceLinkage());
Evan Cheng5be54b02007-01-19 19:25:36 +0000298 if (isExt && isCallOp && Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +0000299 TM.getRelocationModel() != Reloc::Static) {
Dale Johannesenc215b3e2008-05-19 21:38:18 +0000300 printSuffixedName(Name, "$stub");
Evan Chenga8e29892007-01-19 07:51:42 +0000301 FnStubs.insert(Name);
302 } else
303 O << Name;
Chris Lattner388488d2007-05-03 16:42:23 +0000304
305 if (MO.getOffset() > 0)
306 O << '+' << MO.getOffset();
307 else if (MO.getOffset() < 0)
308 O << MO.getOffset();
309
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000310 if (isCallOp && Subtarget->isTargetELF() &&
311 TM.getRelocationModel() == Reloc::PIC_)
312 O << "(PLT)";
Evan Chenga8e29892007-01-19 07:51:42 +0000313 if (GV->hasExternalWeakLinkage())
Rafael Espindola15404d02006-12-18 03:37:18 +0000314 ExtWeakSymbols.insert(GV);
Evan Chenga8e29892007-01-19 07:51:42 +0000315 break;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000316 }
Evan Chenga8e29892007-01-19 07:51:42 +0000317 case MachineOperand::MO_ExternalSymbol: {
318 bool isCallOp = Modifier && !strcmp(Modifier, "call");
319 std::string Name(TAI->getGlobalPrefix());
320 Name += MO.getSymbolName();
Evan Cheng5be54b02007-01-19 19:25:36 +0000321 if (isCallOp && Subtarget->isTargetDarwin() &&
Evan Chenga8e29892007-01-19 07:51:42 +0000322 TM.getRelocationModel() != Reloc::Static) {
Dale Johannesenc215b3e2008-05-19 21:38:18 +0000323 printSuffixedName(Name, "$stub");
Evan Chenga8e29892007-01-19 07:51:42 +0000324 FnStubs.insert(Name);
325 } else
326 O << Name;
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000327 if (isCallOp && Subtarget->isTargetELF() &&
328 TM.getRelocationModel() == Reloc::PIC_)
329 O << "(PLT)";
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000330 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000331 }
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000332 case MachineOperand::MO_ConstantPoolIndex:
Evan Cheng347d39f2007-10-14 05:57:21 +0000333 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
Chris Lattner8aa797a2007-12-30 23:10:15 +0000334 << '_' << MO.getIndex();
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000335 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000336 case MachineOperand::MO_JumpTableIndex:
Evan Cheng347d39f2007-10-14 05:57:21 +0000337 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner8aa797a2007-12-30 23:10:15 +0000338 << '_' << MO.getIndex();
Evan Chenga8e29892007-01-19 07:51:42 +0000339 break;
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000340 default:
341 O << "<unknown operand type>"; abort (); break;
342 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000343}
344
Evan Chengc70d1842007-03-20 08:11:30 +0000345static void printSOImm(std::ostream &O, int64_t V, const TargetAsmInfo *TAI) {
346 assert(V < (1 << 12) && "Not a valid so_imm value!");
347 unsigned Imm = ARM_AM::getSOImmValImm(V);
348 unsigned Rot = ARM_AM::getSOImmValRot(V);
Evan Chenga8e29892007-01-19 07:51:42 +0000349
350 // Print low-level immediate formation info, per
351 // A5.1.3: "Data-processing operands - Immediate".
352 if (Rot) {
353 O << "#" << Imm << ", " << Rot;
354 // Pretty printed version.
355 O << ' ' << TAI->getCommentString() << ' ' << (int)ARM_AM::rotr32(Imm, Rot);
356 } else {
357 O << "#" << Imm;
358 }
359}
360
Evan Chengc70d1842007-03-20 08:11:30 +0000361/// printSOImmOperand - SOImm is 4-bit rotate amount in bits 8-11 with 8-bit
362/// immediate in bits 0-7.
363void ARMAsmPrinter::printSOImmOperand(const MachineInstr *MI, int OpNum) {
364 const MachineOperand &MO = MI->getOperand(OpNum);
365 assert(MO.isImmediate() && "Not a valid so_imm value!");
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000366 printSOImm(O, MO.getImm(), TAI);
Evan Chengc70d1842007-03-20 08:11:30 +0000367}
368
369/// printSOImm2PartOperand - SOImm is broken into two pieces using a mov
370/// followed by a or to materialize.
371void ARMAsmPrinter::printSOImm2PartOperand(const MachineInstr *MI, int OpNum) {
372 const MachineOperand &MO = MI->getOperand(OpNum);
373 assert(MO.isImmediate() && "Not a valid so_imm value!");
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000374 unsigned V1 = ARM_AM::getSOImmTwoPartFirst(MO.getImm());
375 unsigned V2 = ARM_AM::getSOImmTwoPartSecond(MO.getImm());
Evan Chengc70d1842007-03-20 08:11:30 +0000376 printSOImm(O, ARM_AM::getSOImmVal(V1), TAI);
Evan Cheng5e148a32007-06-05 18:55:18 +0000377 O << "\n\torr";
378 printPredicateOperand(MI, 2);
379 O << " ";
Evan Chengc70d1842007-03-20 08:11:30 +0000380 printOperand(MI, 0);
381 O << ", ";
382 printOperand(MI, 0);
383 O << ", ";
384 printSOImm(O, ARM_AM::getSOImmVal(V2), TAI);
385}
386
Evan Chenga8e29892007-01-19 07:51:42 +0000387// so_reg is a 4-operand unit corresponding to register forms of the A5.1
388// "Addressing Mode 1 - Data-processing operands" forms. This includes:
389// REG 0 0 - e.g. R5
390// REG REG 0,SH_OPC - e.g. R5, ROR R3
391// REG 0 IMM,SH_OPC - e.g. R5, LSL #3
392void ARMAsmPrinter::printSORegOperand(const MachineInstr *MI, int Op) {
393 const MachineOperand &MO1 = MI->getOperand(Op);
394 const MachineOperand &MO2 = MI->getOperand(Op+1);
395 const MachineOperand &MO3 = MI->getOperand(Op+2);
396
Dan Gohman6f0d0242008-02-10 18:45:23 +0000397 assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
Bill Wendling74ab84c2008-02-26 21:11:01 +0000398 O << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
Evan Chenga8e29892007-01-19 07:51:42 +0000399
400 // Print the shift opc.
401 O << ", "
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000402 << ARM_AM::getShiftOpcStr(ARM_AM::getSORegShOp(MO3.getImm()))
Evan Chenga8e29892007-01-19 07:51:42 +0000403 << " ";
404
405 if (MO2.getReg()) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000406 assert(TargetRegisterInfo::isPhysicalRegister(MO2.getReg()));
Bill Wendling74ab84c2008-02-26 21:11:01 +0000407 O << TM.getRegisterInfo()->get(MO2.getReg()).AsmName;
Evan Chenga8e29892007-01-19 07:51:42 +0000408 assert(ARM_AM::getSORegOffset(MO3.getImm()) == 0);
409 } else {
410 O << "#" << ARM_AM::getSORegOffset(MO3.getImm());
411 }
412}
413
414void ARMAsmPrinter::printAddrMode2Operand(const MachineInstr *MI, int Op) {
415 const MachineOperand &MO1 = MI->getOperand(Op);
416 const MachineOperand &MO2 = MI->getOperand(Op+1);
417 const MachineOperand &MO3 = MI->getOperand(Op+2);
418
419 if (!MO1.isRegister()) { // FIXME: This is for CP entries, but isn't right.
420 printOperand(MI, Op);
421 return;
422 }
423
Bill Wendling74ab84c2008-02-26 21:11:01 +0000424 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
Evan Chenga8e29892007-01-19 07:51:42 +0000425
426 if (!MO2.getReg()) {
427 if (ARM_AM::getAM2Offset(MO3.getImm())) // Don't print +0.
428 O << ", #"
429 << (char)ARM_AM::getAM2Op(MO3.getImm())
430 << ARM_AM::getAM2Offset(MO3.getImm());
431 O << "]";
432 return;
433 }
434
435 O << ", "
436 << (char)ARM_AM::getAM2Op(MO3.getImm())
Bill Wendling74ab84c2008-02-26 21:11:01 +0000437 << TM.getRegisterInfo()->get(MO2.getReg()).AsmName;
Evan Chenga8e29892007-01-19 07:51:42 +0000438
439 if (unsigned ShImm = ARM_AM::getAM2Offset(MO3.getImm()))
440 O << ", "
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000441 << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO3.getImm()))
Evan Chenga8e29892007-01-19 07:51:42 +0000442 << " #" << ShImm;
443 O << "]";
444}
445
446void ARMAsmPrinter::printAddrMode2OffsetOperand(const MachineInstr *MI, int Op){
447 const MachineOperand &MO1 = MI->getOperand(Op);
448 const MachineOperand &MO2 = MI->getOperand(Op+1);
449
450 if (!MO1.getReg()) {
Evan Chengbdc98692007-05-03 23:30:36 +0000451 unsigned ImmOffs = ARM_AM::getAM2Offset(MO2.getImm());
452 assert(ImmOffs && "Malformed indexed load / store!");
453 O << "#"
454 << (char)ARM_AM::getAM2Op(MO2.getImm())
455 << ImmOffs;
Evan Chenga8e29892007-01-19 07:51:42 +0000456 return;
457 }
458
459 O << (char)ARM_AM::getAM2Op(MO2.getImm())
Bill Wendling74ab84c2008-02-26 21:11:01 +0000460 << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
Evan Chenga8e29892007-01-19 07:51:42 +0000461
462 if (unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm()))
463 O << ", "
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000464 << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO2.getImm()))
Evan Chenga8e29892007-01-19 07:51:42 +0000465 << " #" << ShImm;
466}
467
468void ARMAsmPrinter::printAddrMode3Operand(const MachineInstr *MI, int Op) {
469 const MachineOperand &MO1 = MI->getOperand(Op);
470 const MachineOperand &MO2 = MI->getOperand(Op+1);
471 const MachineOperand &MO3 = MI->getOperand(Op+2);
472
Dan Gohman6f0d0242008-02-10 18:45:23 +0000473 assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
Bill Wendling74ab84c2008-02-26 21:11:01 +0000474 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
Evan Chenga8e29892007-01-19 07:51:42 +0000475
476 if (MO2.getReg()) {
477 O << ", "
478 << (char)ARM_AM::getAM3Op(MO3.getImm())
Bill Wendling74ab84c2008-02-26 21:11:01 +0000479 << TM.getRegisterInfo()->get(MO2.getReg()).AsmName
Evan Chenga8e29892007-01-19 07:51:42 +0000480 << "]";
481 return;
482 }
483
484 if (unsigned ImmOffs = ARM_AM::getAM3Offset(MO3.getImm()))
485 O << ", #"
486 << (char)ARM_AM::getAM3Op(MO3.getImm())
487 << ImmOffs;
488 O << "]";
489}
490
491void ARMAsmPrinter::printAddrMode3OffsetOperand(const MachineInstr *MI, int Op){
492 const MachineOperand &MO1 = MI->getOperand(Op);
493 const MachineOperand &MO2 = MI->getOperand(Op+1);
494
495 if (MO1.getReg()) {
496 O << (char)ARM_AM::getAM3Op(MO2.getImm())
Bill Wendling74ab84c2008-02-26 21:11:01 +0000497 << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
Evan Chenga8e29892007-01-19 07:51:42 +0000498 return;
499 }
500
501 unsigned ImmOffs = ARM_AM::getAM3Offset(MO2.getImm());
Evan Chengbdc98692007-05-03 23:30:36 +0000502 assert(ImmOffs && "Malformed indexed load / store!");
Evan Chenga8e29892007-01-19 07:51:42 +0000503 O << "#"
Evan Chengbdc98692007-05-03 23:30:36 +0000504 << (char)ARM_AM::getAM3Op(MO2.getImm())
Evan Chenga8e29892007-01-19 07:51:42 +0000505 << ImmOffs;
506}
507
508void ARMAsmPrinter::printAddrMode4Operand(const MachineInstr *MI, int Op,
509 const char *Modifier) {
510 const MachineOperand &MO1 = MI->getOperand(Op);
511 const MachineOperand &MO2 = MI->getOperand(Op+1);
512 ARM_AM::AMSubMode Mode = ARM_AM::getAM4SubMode(MO2.getImm());
513 if (Modifier && strcmp(Modifier, "submode") == 0) {
514 if (MO1.getReg() == ARM::SP) {
515 bool isLDM = (MI->getOpcode() == ARM::LDM ||
516 MI->getOpcode() == ARM::LDM_RET);
517 O << ARM_AM::getAMSubModeAltStr(Mode, isLDM);
518 } else
519 O << ARM_AM::getAMSubModeStr(Mode);
520 } else {
521 printOperand(MI, Op);
522 if (ARM_AM::getAM4WBFlag(MO2.getImm()))
523 O << "!";
524 }
525}
526
527void ARMAsmPrinter::printAddrMode5Operand(const MachineInstr *MI, int Op,
528 const char *Modifier) {
529 const MachineOperand &MO1 = MI->getOperand(Op);
530 const MachineOperand &MO2 = MI->getOperand(Op+1);
531
532 if (!MO1.isRegister()) { // FIXME: This is for CP entries, but isn't right.
533 printOperand(MI, Op);
534 return;
535 }
536
Dan Gohman6f0d0242008-02-10 18:45:23 +0000537 assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
Evan Chenga8e29892007-01-19 07:51:42 +0000538
539 if (Modifier && strcmp(Modifier, "submode") == 0) {
540 ARM_AM::AMSubMode Mode = ARM_AM::getAM5SubMode(MO2.getImm());
541 if (MO1.getReg() == ARM::SP) {
542 bool isFLDM = (MI->getOpcode() == ARM::FLDMD ||
543 MI->getOpcode() == ARM::FLDMS);
544 O << ARM_AM::getAMSubModeAltStr(Mode, isFLDM);
545 } else
546 O << ARM_AM::getAMSubModeStr(Mode);
547 return;
548 } else if (Modifier && strcmp(Modifier, "base") == 0) {
549 // Used for FSTM{D|S} and LSTM{D|S} operations.
Bill Wendling74ab84c2008-02-26 21:11:01 +0000550 O << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
Evan Chenga8e29892007-01-19 07:51:42 +0000551 if (ARM_AM::getAM5WBFlag(MO2.getImm()))
552 O << "!";
553 return;
554 }
555
Bill Wendling74ab84c2008-02-26 21:11:01 +0000556 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
Evan Chenga8e29892007-01-19 07:51:42 +0000557
558 if (unsigned ImmOffs = ARM_AM::getAM5Offset(MO2.getImm())) {
559 O << ", #"
560 << (char)ARM_AM::getAM5Op(MO2.getImm())
561 << ImmOffs*4;
562 }
563 O << "]";
564}
565
566void ARMAsmPrinter::printAddrModePCOperand(const MachineInstr *MI, int Op,
567 const char *Modifier) {
568 if (Modifier && strcmp(Modifier, "label") == 0) {
569 printPCLabel(MI, Op+1);
570 return;
571 }
572
573 const MachineOperand &MO1 = MI->getOperand(Op);
Dan Gohman6f0d0242008-02-10 18:45:23 +0000574 assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
Bill Wendling74ab84c2008-02-26 21:11:01 +0000575 O << "[pc, +" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName << "]";
Evan Chenga8e29892007-01-19 07:51:42 +0000576}
577
578void
579ARMAsmPrinter::printThumbAddrModeRROperand(const MachineInstr *MI, int Op) {
580 const MachineOperand &MO1 = MI->getOperand(Op);
581 const MachineOperand &MO2 = MI->getOperand(Op+1);
Bill Wendling74ab84c2008-02-26 21:11:01 +0000582 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
583 O << ", " << TM.getRegisterInfo()->get(MO2.getReg()).AsmName << "]";
Evan Chenga8e29892007-01-19 07:51:42 +0000584}
585
586void
587ARMAsmPrinter::printThumbAddrModeRI5Operand(const MachineInstr *MI, int Op,
588 unsigned Scale) {
589 const MachineOperand &MO1 = MI->getOperand(Op);
Evan Chengcea117d2007-01-30 02:35:32 +0000590 const MachineOperand &MO2 = MI->getOperand(Op+1);
591 const MachineOperand &MO3 = MI->getOperand(Op+2);
Evan Chenga8e29892007-01-19 07:51:42 +0000592
593 if (!MO1.isRegister()) { // FIXME: This is for CP entries, but isn't right.
594 printOperand(MI, Op);
595 return;
596 }
597
Bill Wendling74ab84c2008-02-26 21:11:01 +0000598 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
Evan Chengcea117d2007-01-30 02:35:32 +0000599 if (MO3.getReg())
Bill Wendling74ab84c2008-02-26 21:11:01 +0000600 O << ", " << TM.getRegisterInfo()->get(MO3.getReg()).AsmName;
Evan Chengcea117d2007-01-30 02:35:32 +0000601 else if (unsigned ImmOffs = MO2.getImm()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000602 O << ", #" << ImmOffs;
603 if (Scale > 1)
604 O << " * " << Scale;
605 }
606 O << "]";
607}
608
609void
Evan Chengc38f2bc2007-01-23 22:59:13 +0000610ARMAsmPrinter::printThumbAddrModeS1Operand(const MachineInstr *MI, int Op) {
Evan Chengcea117d2007-01-30 02:35:32 +0000611 printThumbAddrModeRI5Operand(MI, Op, 1);
Evan Chenga8e29892007-01-19 07:51:42 +0000612}
613void
Evan Chengc38f2bc2007-01-23 22:59:13 +0000614ARMAsmPrinter::printThumbAddrModeS2Operand(const MachineInstr *MI, int Op) {
Evan Chengcea117d2007-01-30 02:35:32 +0000615 printThumbAddrModeRI5Operand(MI, Op, 2);
Evan Chenga8e29892007-01-19 07:51:42 +0000616}
617void
Evan Chengc38f2bc2007-01-23 22:59:13 +0000618ARMAsmPrinter::printThumbAddrModeS4Operand(const MachineInstr *MI, int Op) {
Evan Chengcea117d2007-01-30 02:35:32 +0000619 printThumbAddrModeRI5Operand(MI, Op, 4);
Evan Chenga8e29892007-01-19 07:51:42 +0000620}
621
622void ARMAsmPrinter::printThumbAddrModeSPOperand(const MachineInstr *MI,int Op) {
623 const MachineOperand &MO1 = MI->getOperand(Op);
624 const MachineOperand &MO2 = MI->getOperand(Op+1);
Bill Wendling74ab84c2008-02-26 21:11:01 +0000625 O << "[" << TM.getRegisterInfo()->get(MO1.getReg()).AsmName;
Evan Chenga8e29892007-01-19 07:51:42 +0000626 if (unsigned ImmOffs = MO2.getImm())
627 O << ", #" << ImmOffs << " * 4";
628 O << "]";
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000629}
630
Evan Cheng42d712b2007-05-08 21:08:43 +0000631void ARMAsmPrinter::printPredicateOperand(const MachineInstr *MI, int opNum) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000632 ARMCC::CondCodes CC = (ARMCC::CondCodes)MI->getOperand(opNum).getImm();
Evan Cheng44bec522007-05-15 01:29:07 +0000633 if (CC != ARMCC::AL)
634 O << ARMCondCodeToString(CC);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000635}
636
Evan Chengdfb2eba2007-07-06 01:01:34 +0000637void ARMAsmPrinter::printSBitModifierOperand(const MachineInstr *MI, int opNum){
638 unsigned Reg = MI->getOperand(opNum).getReg();
639 if (Reg) {
640 assert(Reg == ARM::CPSR && "Expect ARM CPSR register!");
641 O << 's';
642 }
643}
644
Evan Chenga8e29892007-01-19 07:51:42 +0000645void ARMAsmPrinter::printPCLabel(const MachineInstr *MI, int opNum) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000646 int Id = (int)MI->getOperand(opNum).getImm();
Evan Chenga8e29892007-01-19 07:51:42 +0000647 O << TAI->getPrivateGlobalPrefix() << "PC" << Id;
648}
649
650void ARMAsmPrinter::printRegisterList(const MachineInstr *MI, int opNum) {
651 O << "{";
652 for (unsigned i = opNum, e = MI->getNumOperands(); i != e; ++i) {
653 printOperand(MI, i);
654 if (i != e-1) O << ", ";
655 }
656 O << "}";
657}
658
659void ARMAsmPrinter::printCPInstOperand(const MachineInstr *MI, int OpNo,
660 const char *Modifier) {
661 assert(Modifier && "This operand only works with a modifier!");
662 // There are two aspects to a CONSTANTPOOL_ENTRY operand, the label and the
663 // data itself.
664 if (!strcmp(Modifier, "label")) {
665 unsigned ID = MI->getOperand(OpNo).getImm();
Evan Cheng347d39f2007-10-14 05:57:21 +0000666 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
667 << '_' << ID << ":\n";
Evan Chenga8e29892007-01-19 07:51:42 +0000668 } else {
669 assert(!strcmp(Modifier, "cpentry") && "Unknown modifier for CPE");
Chris Lattner8aa797a2007-12-30 23:10:15 +0000670 unsigned CPI = MI->getOperand(OpNo).getIndex();
Evan Chenga8e29892007-01-19 07:51:42 +0000671
672 const MachineConstantPoolEntry &MCPE = // Chasing pointers is fun?
673 MI->getParent()->getParent()->getConstantPool()->getConstants()[CPI];
674
Evan Cheng711b6dc2008-08-08 06:56:16 +0000675 if (MCPE.isMachineConstantPoolEntry()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000676 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
Evan Cheng711b6dc2008-08-08 06:56:16 +0000677 ARMConstantPoolValue *ACPV =
678 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
679 if (ACPV->getPCAdjustment() != 0) {
680 const GlobalValue *GV = ACPV->getGV();
681 PCRelGVs.insert(GV);
682 }
683 } else {
Evan Chenga8e29892007-01-19 07:51:42 +0000684 EmitGlobalConstant(MCPE.Val.ConstVal);
Lauro Ramos Venancio305b8a52007-04-25 14:50:40 +0000685 // remember to emit the weak reference
686 if (const GlobalValue *GV = dyn_cast<GlobalValue>(MCPE.Val.ConstVal))
687 if (GV->hasExternalWeakLinkage())
688 ExtWeakSymbols.insert(GV);
689 }
Evan Chenga8e29892007-01-19 07:51:42 +0000690 }
691}
692
693void ARMAsmPrinter::printJTBlockOperand(const MachineInstr *MI, int OpNo) {
694 const MachineOperand &MO1 = MI->getOperand(OpNo);
695 const MachineOperand &MO2 = MI->getOperand(OpNo+1); // Unique Id
Chris Lattner8aa797a2007-12-30 23:10:15 +0000696 unsigned JTI = MO1.getIndex();
Evan Cheng347d39f2007-10-14 05:57:21 +0000697 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000698 << '_' << JTI << '_' << MO2.getImm() << ":\n";
Evan Chenga8e29892007-01-19 07:51:42 +0000699
700 const char *JTEntryDirective = TAI->getJumpTableDirective();
701 if (!JTEntryDirective)
702 JTEntryDirective = TAI->getData32bitsDirective();
703
704 const MachineFunction *MF = MI->getParent()->getParent();
Dan Gohman45426112008-07-07 20:06:06 +0000705 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
Evan Chenga8e29892007-01-19 07:51:42 +0000706 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
707 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
708 bool UseSet= TAI->getSetDirective() && TM.getRelocationModel() == Reloc::PIC_;
709 std::set<MachineBasicBlock*> JTSets;
710 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
711 MachineBasicBlock *MBB = JTBBs[i];
712 if (UseSet && JTSets.insert(MBB).second)
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000713 printPICJumpTableSetLabel(JTI, MO2.getImm(), MBB);
Evan Chenga8e29892007-01-19 07:51:42 +0000714
715 O << JTEntryDirective << ' ';
716 if (UseSet)
Evan Cheng347d39f2007-10-14 05:57:21 +0000717 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000718 << '_' << JTI << '_' << MO2.getImm()
Evan Cheng347d39f2007-10-14 05:57:21 +0000719 << "_set_" << MBB->getNumber();
Evan Chenga8e29892007-01-19 07:51:42 +0000720 else if (TM.getRelocationModel() == Reloc::PIC_) {
Evan Chengfb8075d2008-02-28 00:43:03 +0000721 printBasicBlockLabel(MBB, false, false, false);
Evan Chenga8e29892007-01-19 07:51:42 +0000722 // If the arch uses custom Jump Table directives, don't calc relative to JT
723 if (!TAI->getJumpTableDirective())
724 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000725 << getFunctionNumber() << '_' << JTI << '_' << MO2.getImm();
Evan Chenga8e29892007-01-19 07:51:42 +0000726 } else
Evan Chengfb8075d2008-02-28 00:43:03 +0000727 printBasicBlockLabel(MBB, false, false, false);
Evan Chengd85ac4d2007-01-27 02:29:45 +0000728 if (i != e-1)
729 O << '\n';
Evan Chenga8e29892007-01-19 07:51:42 +0000730 }
731}
732
733
734bool ARMAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
735 unsigned AsmVariant, const char *ExtraCode){
736 // Does this asm operand have a single letter operand modifier?
737 if (ExtraCode && ExtraCode[0]) {
738 if (ExtraCode[1] != 0) return true; // Unknown modifier.
739
740 switch (ExtraCode[0]) {
741 default: return true; // Unknown modifier.
Evan Cheng23a95702007-03-08 22:42:46 +0000742 case 'c': // Don't print "$" before a global var name or constant.
Evan Chenge21e3962007-04-04 00:13:29 +0000743 case 'P': // Print a VFP double precision register.
Evan Cheng23a95702007-03-08 22:42:46 +0000744 printOperand(MI, OpNo);
745 return false;
Evan Chenga8e29892007-01-19 07:51:42 +0000746 case 'Q':
747 if (TM.getTargetData()->isLittleEndian())
748 break;
749 // Fallthrough
750 case 'R':
751 if (TM.getTargetData()->isBigEndian())
752 break;
753 // Fallthrough
754 case 'H': // Write second word of DI / DF reference.
755 // Verify that this operand has two consecutive registers.
756 if (!MI->getOperand(OpNo).isRegister() ||
757 OpNo+1 == MI->getNumOperands() ||
758 !MI->getOperand(OpNo+1).isRegister())
759 return true;
760 ++OpNo; // Return the high-part.
761 }
762 }
763
764 printOperand(MI, OpNo);
765 return false;
766}
767
768void ARMAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
769 ++EmittedInsts;
770
Evan Chengc60e76d2007-01-30 20:37:08 +0000771 int Opc = MI->getOpcode();
772 switch (Opc) {
773 case ARM::CONSTPOOL_ENTRY:
Evan Chenga8e29892007-01-19 07:51:42 +0000774 if (!InCPMode && AFI->isThumbFunction()) {
775 EmitAlignment(2);
776 InCPMode = true;
777 }
Evan Chengc60e76d2007-01-30 20:37:08 +0000778 break;
779 default: {
Evan Cheng3bf12d02007-01-31 23:39:39 +0000780 if (InCPMode && AFI->isThumbFunction())
Evan Chenga8e29892007-01-19 07:51:42 +0000781 InCPMode = false;
Evan Chengc60e76d2007-01-30 20:37:08 +0000782 switch (Opc) {
783 case ARM::PICADD:
784 case ARM::PICLD:
Evan Cheng341dccc2007-06-05 07:36:38 +0000785 case ARM::PICLDZH:
786 case ARM::PICLDZB:
787 case ARM::PICLDH:
788 case ARM::PICLDB:
789 case ARM::PICLDSH:
790 case ARM::PICLDSB:
791 case ARM::PICSTR:
792 case ARM::PICSTRH:
793 case ARM::PICSTRB:
Evan Chengc60e76d2007-01-30 20:37:08 +0000794 case ARM::tPICADD:
795 break;
796 default:
Evan Chengc60e76d2007-01-30 20:37:08 +0000797 break;
798 }
799 }}
Evan Chenga8e29892007-01-19 07:51:42 +0000800
801 // Call the autogenerated instruction printer routines.
802 printInstruction(MI);
803}
804
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000805bool ARMAsmPrinter::doInitialization(Module &M) {
Lauro Ramos Venancioe8e54952007-05-03 20:28:35 +0000806 // Emit initial debug information.
Evan Cheng6aa38982008-08-18 08:52:48 +0000807 // FIXME: Dwarf support.
808 //DW.BeginModule(&M);
Evan Chenga8e29892007-01-19 07:51:42 +0000809
Dan Gohmanb8275a32007-07-25 19:33:14 +0000810 bool Result = AsmPrinter::doInitialization(M);
Dale Johannesen54118352007-06-22 00:54:56 +0000811
Dale Johannesenf2452c52008-07-09 21:20:54 +0000812 // AsmPrinter::doInitialization should have done this analysis.
813 MMI = getAnalysisToUpdate<MachineModuleInfo>();
814 assert(MMI);
Evan Cheng6aa38982008-08-18 08:52:48 +0000815 // FIXME: Dwarf support.
816 //DW.SetModuleInfo(MMI);
Dale Johannesenf2452c52008-07-09 21:20:54 +0000817
Dale Johannesen54118352007-06-22 00:54:56 +0000818 // Darwin wants symbols to be quoted if they have complex names.
819 if (Subtarget->isTargetDarwin())
820 Mang->setUseQuotes(true);
821
Dan Gohmanb8275a32007-07-25 19:33:14 +0000822 return Result;
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000823}
824
Chris Lattnerec321b42008-02-15 19:04:54 +0000825/// PrintUnmangledNameSafely - Print out the printable characters in the name.
826/// Don't print things like \n or \0.
827static void PrintUnmangledNameSafely(const Value *V, std::ostream &OS) {
828 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
829 Name != E; ++Name)
830 if (isprint(*Name))
831 OS << *Name;
832}
833
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +0000834void ARMAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000835 const TargetData *TD = TM.getTargetData();
836
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +0000837 if (!GVar->hasInitializer()) // External global require no code
838 return;
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000839
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +0000840 // Check to see if this is a special global used by LLVM, if so, emit it.
841
842 if (EmitSpecialLLVMGlobal(GVar)) {
843 if (Subtarget->isTargetDarwin() &&
844 TM.getRelocationModel() == Reloc::Static) {
845 if (GVar->getName() == "llvm.global_ctors")
846 O << ".reference .constructors_used\n";
847 else if (GVar->getName() == "llvm.global_dtors")
848 O << ".reference .destructors_used\n";
849 }
850 return;
851 }
852
Evan Cheng42ccc212008-08-08 17:56:50 +0000853 std::string SectionName = TAI->SectionForGlobal(GVar);
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +0000854 std::string name = Mang->getValueName(GVar);
855 Constant *C = GVar->getInitializer();
856 const Type *Type = C->getType();
857 unsigned Size = TD->getABITypeSize(Type);
858 unsigned Align = TD->getPreferredAlignmentLog(GVar);
859
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000860 printVisibility(name, GVar->getVisibility());
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +0000861
862 if (Subtarget->isTargetELF())
863 O << "\t.type " << name << ",%object\n";
864
865 SwitchToDataSection(SectionName.c_str());
866
867 if (C->isNullValue() && !GVar->hasSection() && !GVar->isThreadLocal()) {
868 // FIXME: This seems to be pretty darwin-specific
869
870 if (GVar->hasExternalLinkage()) {
871 if (const char *Directive = TAI->getZeroFillDirective()) {
872 O << "\t.globl\t" << name << "\n";
873 O << Directive << "__DATA, __common, " << name << ", "
874 << Size << ", " << Align << "\n";
875 return;
Evan Chengb267ca12007-01-30 08:04:53 +0000876 }
Evan Chengb267ca12007-01-30 08:04:53 +0000877 }
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000878
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +0000879 if (GVar->hasInternalLinkage() || GVar->isWeakForLinker()) {
880 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000881
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +0000882 if (TAI->getLCOMMDirective() != NULL) {
Evan Cheng42ccc212008-08-08 17:56:50 +0000883 if (PCRelGVs.count(GVar) || GVar->hasInternalLinkage()) {
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +0000884 O << TAI->getLCOMMDirective() << name << "," << Size;
885 if (Subtarget->isTargetDarwin())
886 O << "," << Align;
887 } else
Evan Chenga8e29892007-01-19 07:51:42 +0000888 O << TAI->getCOMMDirective() << name << "," << Size;
Evan Cheng5be54b02007-01-19 19:25:36 +0000889 } else {
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +0000890 if (GVar->hasInternalLinkage())
891 O << "\t.local\t" << name << "\n";
892 O << TAI->getCOMMDirective() << name << "," << Size;
893 if (TAI->getCOMMDirectiveTakesAlignment())
894 O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Evan Cheng5be54b02007-01-19 19:25:36 +0000895 }
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +0000896 O << "\t\t" << TAI->getCommentString() << " ";
897 PrintUnmangledNameSafely(GVar, O);
898 O << "\n";
899 return;
Evan Cheng5be54b02007-01-19 19:25:36 +0000900 }
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +0000901 }
Rafael Espindolab97809c2006-10-19 13:30:40 +0000902
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +0000903 switch (GVar->getLinkage()) {
904 case GlobalValue::LinkOnceLinkage:
905 case GlobalValue::WeakLinkage:
906 if (Subtarget->isTargetDarwin()) {
907 O << "\t.globl " << name << "\n"
908 << "\t.weak_definition " << name << "\n";
909 } else {
910 O << "\t.weak " << name << "\n";
911 }
912 break;
913 case GlobalValue::AppendingLinkage:
914 // FIXME: appending linkage variables should go into a section of
915 // their name or something. For now, just emit them as external.
916 case GlobalValue::ExternalLinkage:
917 O << "\t.globl " << name << "\n";
918 // FALL THROUGH
919 case GlobalValue::InternalLinkage:
920 break;
921 default:
922 assert(0 && "Unknown linkage type!");
923 break;
924 }
925
926 EmitAlignment(Align, GVar);
927 O << name << ":\t\t\t\t" << TAI->getCommentString() << " ";
928 PrintUnmangledNameSafely(GVar, O);
929 O << "\n";
930 if (TAI->hasDotTypeDotSizeDirective())
931 O << "\t.size " << name << ", " << Size << "\n";
932
933 // If the initializer is a extern weak symbol, remember to emit the weak
934 // reference!
935 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
936 if (GV->hasExternalWeakLinkage())
Evan Chenga8e29892007-01-19 07:51:42 +0000937 ExtWeakSymbols.insert(GV);
938
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +0000939 EmitGlobalConstant(C);
940 O << '\n';
941}
942
943
944bool ARMAsmPrinter::doFinalization(Module &M) {
945 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
946 I != E; ++I)
947 printModuleLevelGV(I);
Evan Chenga8e29892007-01-19 07:51:42 +0000948
Evan Cheng5be54b02007-01-19 19:25:36 +0000949 if (Subtarget->isTargetDarwin()) {
950 SwitchToDataSection("");
951
Evan Chenga8e29892007-01-19 07:51:42 +0000952 // Output stubs for dynamically-linked functions
953 unsigned j = 1;
954 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
955 i != e; ++i, ++j) {
956 if (TM.getRelocationModel() == Reloc::PIC_)
957 SwitchToTextSection(".section __TEXT,__picsymbolstub4,symbol_stubs,"
958 "none,16", 0);
959 else
960 SwitchToTextSection(".section __TEXT,__symbol_stub4,symbol_stubs,"
961 "none,12", 0);
962
963 EmitAlignment(2);
964 O << "\t.code\t32\n";
965
Dale Johannesenc215b3e2008-05-19 21:38:18 +0000966 std::string p = *i;
967 printSuffixedName(p, "$stub");
968 O << ":\n";
Evan Chenga8e29892007-01-19 07:51:42 +0000969 O << "\t.indirect_symbol " << *i << "\n";
Dale Johannesenc215b3e2008-05-19 21:38:18 +0000970 O << "\tldr ip, ";
971 printSuffixedName(p, "$slp");
972 O << "\n";
Evan Chenga8e29892007-01-19 07:51:42 +0000973 if (TM.getRelocationModel() == Reloc::PIC_) {
Dale Johannesenc215b3e2008-05-19 21:38:18 +0000974 printSuffixedName(p, "$scv");
975 O << ":\n";
Evan Chenga8e29892007-01-19 07:51:42 +0000976 O << "\tadd ip, pc, ip\n";
977 }
978 O << "\tldr pc, [ip, #0]\n";
Dale Johannesenc215b3e2008-05-19 21:38:18 +0000979 printSuffixedName(p, "$slp");
980 O << ":\n";
981 O << "\t.long\t";
982 printSuffixedName(p, "$lazy_ptr");
983 if (TM.getRelocationModel() == Reloc::PIC_) {
984 O << "-(";
985 printSuffixedName(p, "$scv");
986 O << "+8)\n";
987 } else
988 O << "\n";
Evan Chenga8e29892007-01-19 07:51:42 +0000989 SwitchToDataSection(".lazy_symbol_pointer", 0);
Dale Johannesenc215b3e2008-05-19 21:38:18 +0000990 printSuffixedName(p, "$lazy_ptr");
991 O << ":\n";
Evan Chenga8e29892007-01-19 07:51:42 +0000992 O << "\t.indirect_symbol " << *i << "\n";
993 O << "\t.long\tdyld_stub_binding_helper\n";
994 }
995 O << "\n";
996
997 // Output non-lazy-pointers for external and common global variables.
Dan Gohmancb406c22007-10-03 19:26:29 +0000998 if (!GVNonLazyPtrs.empty())
Evan Chenga8e29892007-01-19 07:51:42 +0000999 SwitchToDataSection(".non_lazy_symbol_pointer", 0);
1000 for (std::set<std::string>::iterator i = GVNonLazyPtrs.begin(),
1001 e = GVNonLazyPtrs.end(); i != e; ++i) {
Dale Johannesenc215b3e2008-05-19 21:38:18 +00001002 std::string p = *i;
1003 printSuffixedName(p, "$non_lazy_ptr");
1004 O << ":\n";
Evan Chenga8e29892007-01-19 07:51:42 +00001005 O << "\t.indirect_symbol " << *i << "\n";
1006 O << "\t.long\t0\n";
1007 }
1008
1009 // Emit initial debug information.
Evan Cheng6aa38982008-08-18 08:52:48 +00001010 // FIXME: Dwarf support.
1011 //DW.EndModule();
Evan Chenga8e29892007-01-19 07:51:42 +00001012
1013 // Funny Darwin hack: This flag tells the linker that no global symbols
1014 // contain code that falls through to other global symbols (e.g. the obvious
1015 // implementation of multiple entry points). If this doesn't occur, the
1016 // linker can safely perform dead code stripping. Since LLVM never
1017 // generates code that does this, it is always safe to set.
1018 O << "\t.subsections_via_symbols\n";
Lauro Ramos Venancioe8e54952007-05-03 20:28:35 +00001019 } else {
1020 // Emit final debug information for ELF.
Evan Cheng6aa38982008-08-18 08:52:48 +00001021 // FIXME: Dwarf support.
1022 //DW.EndModule();
Rafael Espindolab01c4bb2006-07-27 11:38:51 +00001023 }
Rafael Espindolab97809c2006-10-19 13:30:40 +00001024
Dan Gohmanb8275a32007-07-25 19:33:14 +00001025 return AsmPrinter::doFinalization(M);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00001026}
Anton Korobeynikov0bd89712008-08-17 13:55:10 +00001027
1028/// createARMCodePrinterPass - Returns a pass that prints the ARM
1029/// assembly code for a MachineFunction to the given output stream,
1030/// using the given target machine description. This should work
1031/// regardless of whether the function is in SSA form.
1032///
1033FunctionPass *llvm::createARMCodePrinterPass(std::ostream &o,
1034 ARMTargetMachine &tm) {
1035 return new ARMAsmPrinter(o, tm, tm.getTargetAsmInfo());
1036}
1037
1038namespace {
1039 static struct Register {
1040 Register() {
1041 ARMTargetMachine::registerAsmPrinter(createARMCodePrinterPass);
1042 }
1043 } Registrator;
1044}