blob: ded9800052e19cf3380ffc190c0a33dddc4940d3 [file] [log] [blame]
Chris Lattner97f06932009-10-19 20:20:46 +00001//===-- ARMAsmPrinter.cpp - Print machine code to an ARM .s file ----------===//
2//
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00003// 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"
Anton Korobeynikov88ce6672009-05-23 19:51:20 +000017#include "ARMBuildAttrs.h"
Evan Chenga8e29892007-01-19 07:51:42 +000018#include "ARMAddressingModes.h"
19#include "ARMConstantPoolValue.h"
Chris Lattnerf447a5f2010-07-19 23:44:46 +000020#include "AsmPrinter/ARMInstPrinter.h"
Chris Lattner97f06932009-10-19 20:20:46 +000021#include "ARMMachineFunctionInfo.h"
22#include "ARMMCInstLower.h"
23#include "ARMTargetMachine.h"
Dale Johannesen3f282aa2010-04-26 20:07:31 +000024#include "llvm/Analysis/DebugInfo.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000025#include "llvm/Constants.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000026#include "llvm/Module.h"
Benjamin Kramere55b15f2009-12-28 12:27:56 +000027#include "llvm/Type.h"
Dan Gohmancf20ac42009-08-13 01:36:44 +000028#include "llvm/Assembly/Writer.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000029#include "llvm/CodeGen/AsmPrinter.h"
Chris Lattnerb0f294c2009-10-19 18:38:33 +000030#include "llvm/CodeGen/MachineModuleInfoImpls.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000031#include "llvm/CodeGen/MachineFunctionPass.h"
Evan Chenga8e29892007-01-19 07:51:42 +000032#include "llvm/CodeGen/MachineJumpTableInfo.h"
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000033#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Chris Lattnerb0f294c2009-10-19 18:38:33 +000034#include "llvm/MC/MCAsmInfo.h"
35#include "llvm/MC/MCContext.h"
Bill Wendlingbecd83e2010-03-09 00:40:17 +000036#include "llvm/MC/MCExpr.h"
Chris Lattner97f06932009-10-19 20:20:46 +000037#include "llvm/MC/MCInst.h"
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000038#include "llvm/MC/MCSectionMachO.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000039#include "llvm/MC/MCStreamer.h"
Chris Lattner325d3dc2009-09-13 17:14:04 +000040#include "llvm/MC/MCSymbol.h"
Chris Lattnerd62f1b42010-03-12 21:19:23 +000041#include "llvm/Target/Mangler.h"
Rafael Espindolab01c4bb2006-07-27 11:38:51 +000042#include "llvm/Target/TargetData.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000043#include "llvm/Target/TargetMachine.h"
Evan Cheng5be54b02007-01-19 19:25:36 +000044#include "llvm/Target/TargetOptions.h"
Daniel Dunbar51b198a2009-07-15 20:24:03 +000045#include "llvm/Target/TargetRegistry.h"
Evan Chengc324ecb2009-07-24 18:19:46 +000046#include "llvm/ADT/SmallPtrSet.h"
Jim Grosbachc40d9f92009-09-01 18:49:12 +000047#include "llvm/ADT/SmallString.h"
Bob Wilson54c78ef2009-11-06 23:33:28 +000048#include "llvm/ADT/StringExtras.h"
Chris Lattner97f06932009-10-19 20:20:46 +000049#include "llvm/Support/CommandLine.h"
Devang Patel59135f42010-08-04 22:39:39 +000050#include "llvm/Support/Debug.h"
Torok Edwin30464702009-07-08 20:55:50 +000051#include "llvm/Support/ErrorHandling.h"
Chris Lattnerb23569a2010-04-04 08:18:47 +000052#include "llvm/Support/raw_ostream.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000053#include <cctype>
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000054using namespace llvm;
55
Chris Lattner97f06932009-10-19 20:20:46 +000056static cl::opt<bool>
57EnableMCInst("enable-arm-mcinst-printer", cl::Hidden,
58 cl::desc("enable experimental asmprinter gunk in the arm backend"));
59
Jim Grosbach91729002010-07-21 23:03:52 +000060namespace llvm {
61 namespace ARM {
62 enum DW_ISA {
63 DW_ISA_ARM_thumb = 1,
64 DW_ISA_ARM_arm = 2
65 };
66 }
67}
68
Chris Lattner95b2c7d2006-12-19 22:59:26 +000069namespace {
Chris Lattner4a071d62009-10-19 17:59:19 +000070 class ARMAsmPrinter : public AsmPrinter {
Evan Chenga8e29892007-01-19 07:51:42 +000071
72 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
73 /// make the right decision when printing asm code for different targets.
74 const ARMSubtarget *Subtarget;
75
76 /// AFI - Keep a pointer to ARMFunctionInfo for the current
Evan Cheng6d63a722008-09-18 07:27:23 +000077 /// MachineFunction.
Evan Chenga8e29892007-01-19 07:51:42 +000078 ARMFunctionInfo *AFI;
79
Evan Cheng6d63a722008-09-18 07:27:23 +000080 /// MCP - Keep a pointer to constantpool entries of the current
81 /// MachineFunction.
82 const MachineConstantPool *MCP;
83
Bill Wendling57f0db82009-02-24 08:30:20 +000084 public:
Chris Lattnerb23569a2010-04-04 08:18:47 +000085 explicit ARMAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
86 : AsmPrinter(TM, Streamer), AFI(NULL), MCP(NULL) {
Bill Wendling57f0db82009-02-24 08:30:20 +000087 Subtarget = &TM.getSubtarget<ARMSubtarget>();
88 }
89
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000090 virtual const char *getPassName() const {
91 return "ARM Assembly Printer";
92 }
Chris Lattner6a71afa2009-10-19 19:59:05 +000093
Chris Lattner97f06932009-10-19 20:20:46 +000094 void printInstructionThroughMCStreamer(const MachineInstr *MI);
95
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000096
Chris Lattner35c33bd2010-04-04 04:47:45 +000097 void printOperand(const MachineInstr *MI, int OpNum, raw_ostream &O,
Evan Chenga8e29892007-01-19 07:51:42 +000098 const char *Modifier = 0);
Chris Lattner35c33bd2010-04-04 04:47:45 +000099 void printSOImmOperand(const MachineInstr *MI, int OpNum, raw_ostream &O);
100 void printSOImm2PartOperand(const MachineInstr *MI, int OpNum,
101 raw_ostream &O);
102 void printSORegOperand(const MachineInstr *MI, int OpNum,
103 raw_ostream &O);
104 void printAddrMode2Operand(const MachineInstr *MI, int OpNum,
105 raw_ostream &O);
106 void printAddrMode2OffsetOperand(const MachineInstr *MI, int OpNum,
107 raw_ostream &O);
108 void printAddrMode3Operand(const MachineInstr *MI, int OpNum,
109 raw_ostream &O);
110 void printAddrMode3OffsetOperand(const MachineInstr *MI, int OpNum,
111 raw_ostream &O);
112 void printAddrMode4Operand(const MachineInstr *MI, int OpNum,raw_ostream &O,
Evan Chenga8e29892007-01-19 07:51:42 +0000113 const char *Modifier = 0);
Chris Lattner35c33bd2010-04-04 04:47:45 +0000114 void printAddrMode5Operand(const MachineInstr *MI, int OpNum,raw_ostream &O,
Evan Chenga8e29892007-01-19 07:51:42 +0000115 const char *Modifier = 0);
Chris Lattner35c33bd2010-04-04 04:47:45 +0000116 void printAddrMode6Operand(const MachineInstr *MI, int OpNum,
117 raw_ostream &O);
118 void printAddrMode6OffsetOperand(const MachineInstr *MI, int OpNum,
119 raw_ostream &O);
Evan Cheng055b0312009-06-29 07:51:04 +0000120 void printAddrModePCOperand(const MachineInstr *MI, int OpNum,
Chris Lattner35c33bd2010-04-04 04:47:45 +0000121 raw_ostream &O,
Evan Chenga8e29892007-01-19 07:51:42 +0000122 const char *Modifier = 0);
Bob Wilsoneaf1c982010-08-11 23:10:46 +0000123 void printBitfieldInvMaskImmOperand(const MachineInstr *MI, int OpNum,
124 raw_ostream &O);
Johnny Chen1adc40c2010-08-12 20:46:17 +0000125 void printMemBOption(const MachineInstr *MI, int OpNum,
126 raw_ostream &O);
Bob Wilsoneaf1c982010-08-11 23:10:46 +0000127 void printSatShiftOperand(const MachineInstr *MI, int OpNum,
128 raw_ostream &O);
Evan Cheng9cb9e672009-06-27 02:26:13 +0000129
Chris Lattner35c33bd2010-04-04 04:47:45 +0000130 void printThumbS4ImmOperand(const MachineInstr *MI, int OpNum,
131 raw_ostream &O);
132 void printThumbITMask(const MachineInstr *MI, int OpNum, raw_ostream &O);
133 void printThumbAddrModeRROperand(const MachineInstr *MI, int OpNum,
134 raw_ostream &O);
Evan Cheng055b0312009-06-29 07:51:04 +0000135 void printThumbAddrModeRI5Operand(const MachineInstr *MI, int OpNum,
Chris Lattner35c33bd2010-04-04 04:47:45 +0000136 raw_ostream &O,
Evan Chenga8e29892007-01-19 07:51:42 +0000137 unsigned Scale);
Chris Lattner35c33bd2010-04-04 04:47:45 +0000138 void printThumbAddrModeS1Operand(const MachineInstr *MI, int OpNum,
139 raw_ostream &O);
140 void printThumbAddrModeS2Operand(const MachineInstr *MI, int OpNum,
141 raw_ostream &O);
142 void printThumbAddrModeS4Operand(const MachineInstr *MI, int OpNum,
143 raw_ostream &O);
144 void printThumbAddrModeSPOperand(const MachineInstr *MI, int OpNum,
145 raw_ostream &O);
Evan Cheng9cb9e672009-06-27 02:26:13 +0000146
Chris Lattner35c33bd2010-04-04 04:47:45 +0000147 void printT2SOOperand(const MachineInstr *MI, int OpNum, raw_ostream &O);
148 void printT2AddrModeImm12Operand(const MachineInstr *MI, int OpNum,
149 raw_ostream &O);
150 void printT2AddrModeImm8Operand(const MachineInstr *MI, int OpNum,
151 raw_ostream &O);
152 void printT2AddrModeImm8s4Operand(const MachineInstr *MI, int OpNum,
153 raw_ostream &O);
154 void printT2AddrModeImm8OffsetOperand(const MachineInstr *MI, int OpNum,
155 raw_ostream &O);
156 void printT2AddrModeImm8s4OffsetOperand(const MachineInstr *MI, int OpNum,
157 raw_ostream &O) {}
158 void printT2AddrModeSoRegOperand(const MachineInstr *MI, int OpNum,
159 raw_ostream &O);
Evan Cheng9cb9e672009-06-27 02:26:13 +0000160
Chris Lattner35c33bd2010-04-04 04:47:45 +0000161 void printCPSOptionOperand(const MachineInstr *MI, int OpNum,
162 raw_ostream &O) {}
163 void printMSRMaskOperand(const MachineInstr *MI, int OpNum,
164 raw_ostream &O) {}
165 void printNegZeroOperand(const MachineInstr *MI, int OpNum,
166 raw_ostream &O) {}
167 void printPredicateOperand(const MachineInstr *MI, int OpNum,
168 raw_ostream &O);
169 void printMandatoryPredicateOperand(const MachineInstr *MI, int OpNum,
170 raw_ostream &O);
171 void printSBitModifierOperand(const MachineInstr *MI, int OpNum,
172 raw_ostream &O);
173 void printPCLabel(const MachineInstr *MI, int OpNum,
174 raw_ostream &O);
175 void printRegisterList(const MachineInstr *MI, int OpNum,
176 raw_ostream &O);
Evan Cheng055b0312009-06-29 07:51:04 +0000177 void printCPInstOperand(const MachineInstr *MI, int OpNum,
Chris Lattner35c33bd2010-04-04 04:47:45 +0000178 raw_ostream &O,
Evan Chenga8e29892007-01-19 07:51:42 +0000179 const char *Modifier);
Chris Lattner35c33bd2010-04-04 04:47:45 +0000180 void printJTBlockOperand(const MachineInstr *MI, int OpNum,
181 raw_ostream &O);
182 void printJT2BlockOperand(const MachineInstr *MI, int OpNum,
183 raw_ostream &O);
184 void printTBAddrMode(const MachineInstr *MI, int OpNum,
185 raw_ostream &O);
186 void printNoHashImmediate(const MachineInstr *MI, int OpNum,
187 raw_ostream &O);
188 void printVFPf32ImmOperand(const MachineInstr *MI, int OpNum,
189 raw_ostream &O);
190 void printVFPf64ImmOperand(const MachineInstr *MI, int OpNum,
191 raw_ostream &O);
Bob Wilson1a913ed2010-06-11 21:34:50 +0000192 void printNEONModImmOperand(const MachineInstr *MI, int OpNum,
193 raw_ostream &O);
Bob Wilson54c78ef2009-11-06 23:33:28 +0000194
Evan Cheng055b0312009-06-29 07:51:04 +0000195 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
Chris Lattnerc75c0282010-04-04 05:29:35 +0000196 unsigned AsmVariant, const char *ExtraCode,
197 raw_ostream &O);
Evan Cheng055b0312009-06-29 07:51:04 +0000198 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNum,
Bob Wilson224c2442009-05-19 05:53:42 +0000199 unsigned AsmVariant,
Chris Lattnerc75c0282010-04-04 05:29:35 +0000200 const char *ExtraCode, raw_ostream &O);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000201
Chris Lattner35c33bd2010-04-04 04:47:45 +0000202 void printInstruction(const MachineInstr *MI, raw_ostream &O); // autogen
Chris Lattnerd95148f2009-09-13 20:19:22 +0000203 static const char *getRegisterName(unsigned RegNo);
Chris Lattner05af2612009-09-13 20:08:00 +0000204
Chris Lattnera786cea2010-01-28 01:10:34 +0000205 virtual void EmitInstruction(const MachineInstr *MI);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000206 bool runOnMachineFunction(MachineFunction &F);
Chris Lattnera2406192010-01-28 00:19:24 +0000207
208 virtual void EmitConstantPool() {} // we emit constant pools customly!
Chris Lattner953ebb72010-01-27 23:58:11 +0000209 virtual void EmitFunctionEntryLabel();
Bob Wilson812209a2009-09-30 22:06:26 +0000210 void EmitStartOfAsmFile(Module &M);
Chris Lattner4a071d62009-10-19 17:59:19 +0000211 void EmitEndOfAsmFile(Module &M);
Evan Chenga8e29892007-01-19 07:51:42 +0000212
Devang Patel59135f42010-08-04 22:39:39 +0000213 MachineLocation getDebugValueLocation(const MachineInstr *MI) const {
214 MachineLocation Location;
215 assert (MI->getNumOperands() == 4 && "Invalid no. of machine operands!");
216 // Frame address. Currently handles register +- offset only.
217 if (MI->getOperand(0).isReg() && MI->getOperand(1).isImm())
218 Location.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
219 else {
220 DEBUG(dbgs() << "DBG_VALUE instruction ignored! " << *MI << "\n");
221 }
222 return Location;
223 }
224
Jim Grosbach91729002010-07-21 23:03:52 +0000225 virtual unsigned getISAEncoding() {
226 // ARM/Darwin adds ISA to the DWARF info for each function.
227 if (!Subtarget->isTargetDarwin())
228 return 0;
229 return Subtarget->isThumb() ?
230 llvm::ARM::DW_ISA_ARM_thumb : llvm::ARM::DW_ISA_ARM_arm;
231 }
232
Chris Lattner0890cf12010-01-25 19:51:38 +0000233 MCSymbol *GetARMSetPICJumpTableLabel2(unsigned uid, unsigned uid2,
234 const MachineBasicBlock *MBB) const;
235 MCSymbol *GetARMJTIPICJumpTableLabel2(unsigned uid, unsigned uid2) const;
Chris Lattnerbfcb0962010-01-25 19:39:52 +0000236
Evan Cheng711b6dc2008-08-08 06:56:16 +0000237 /// EmitMachineConstantPoolValue - Print a machine constantpool value to
238 /// the .s file.
Evan Chenga8e29892007-01-19 07:51:42 +0000239 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
Chris Lattner9d7efd32010-04-04 07:05:53 +0000240 SmallString<128> Str;
241 raw_svector_ostream OS(Str);
242 EmitMachineConstantPoolValue(MCPV, OS);
243 OutStreamer.EmitRawText(OS.str());
244 }
245
246 void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV,
247 raw_ostream &O) {
Chris Lattnerea3cb402010-01-20 07:33:29 +0000248 switch (TM.getTargetData()->getTypeAllocSize(MCPV->getType())) {
249 case 1: O << MAI->getData8bitsDirective(0); break;
250 case 2: O << MAI->getData16bitsDirective(0); break;
251 case 4: O << MAI->getData32bitsDirective(0); break;
252 default: assert(0 && "Unknown CPV size");
253 }
Evan Chenga8e29892007-01-19 07:51:42 +0000254
Evan Cheng711b6dc2008-08-08 06:56:16 +0000255 ARMConstantPoolValue *ACPV = static_cast<ARMConstantPoolValue*>(MCPV);
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +0000256
257 if (ACPV->isLSDA()) {
Chris Lattner9d7efd32010-04-04 07:05:53 +0000258 O << MAI->getPrivateGlobalPrefix() << "_LSDA_" << getFunctionNumber();
Bob Wilson28989a82009-11-02 16:59:06 +0000259 } else if (ACPV->isBlockAddress()) {
Chris Lattner0752cda2010-04-05 16:32:14 +0000260 O << *GetBlockAddressSymbol(ACPV->getBlockAddress());
Bob Wilson28989a82009-11-02 16:59:06 +0000261 } else if (ACPV->isGlobalValue()) {
Dan Gohman46510a72010-04-15 01:51:59 +0000262 const GlobalValue *GV = ACPV->getGV();
Evan Chenge4e4ed32009-08-28 23:18:09 +0000263 bool isIndirect = Subtarget->isTargetDarwin() &&
Evan Cheng63476a82009-09-03 07:04:02 +0000264 Subtarget->GVIsIndirectSymbol(GV, TM.getRelocationModel());
Evan Chenge4e4ed32009-08-28 23:18:09 +0000265 if (!isIndirect)
Chris Lattnerd62f1b42010-03-12 21:19:23 +0000266 O << *Mang->getSymbol(GV);
Evan Chenge4e4ed32009-08-28 23:18:09 +0000267 else {
268 // FIXME: Remove this when Darwin transition to @GOT like syntax.
Chris Lattner7a2ba942010-01-16 18:37:32 +0000269 MCSymbol *Sym = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Chris Lattner10b318b2010-01-17 21:43:43 +0000270 O << *Sym;
Chris Lattnerb8f64a72009-10-19 18:49:14 +0000271
272 MachineModuleInfoMachO &MMIMachO =
273 MMI->getObjFileInfo<MachineModuleInfoMachO>();
Bill Wendlingcebae362010-03-10 22:34:10 +0000274 MachineModuleInfoImpl::StubValueTy &StubSym =
Chris Lattnerb8f64a72009-10-19 18:49:14 +0000275 GV->hasHiddenVisibility() ? MMIMachO.getHiddenGVStubEntry(Sym) :
276 MMIMachO.getGVStubEntry(Sym);
Bill Wendlingcebae362010-03-10 22:34:10 +0000277 if (StubSym.getPointer() == 0)
278 StubSym = MachineModuleInfoImpl::
Chris Lattnerd62f1b42010-03-12 21:19:23 +0000279 StubValueTy(Mang->getSymbol(GV), !GV->hasInternalLinkage());
Evan Chenge4e4ed32009-08-28 23:18:09 +0000280 }
Bob Wilson28989a82009-11-02 16:59:06 +0000281 } else {
282 assert(ACPV->isExtSymbol() && "unrecognized constant pool value");
Chris Lattner10b318b2010-01-17 21:43:43 +0000283 O << *GetExternalSymbolSymbol(ACPV->getSymbol());
Bob Wilson28989a82009-11-02 16:59:06 +0000284 }
Jim Grosbache9952212009-09-04 01:38:51 +0000285
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000286 if (ACPV->hasModifier()) O << "(" << ACPV->getModifier() << ")";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000287 if (ACPV->getPCAdjustment() != 0) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000288 O << "-(" << MAI->getPrivateGlobalPrefix() << "PC"
Evan Chenge7e0d622009-11-06 22:24:13 +0000289 << getFunctionNumber() << "_" << ACPV->getLabelId()
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000290 << "+" << (unsigned)ACPV->getPCAdjustment();
291 if (ACPV->mustAddCurrentAddress())
292 O << "-.";
Chris Lattner8b378752010-01-15 23:26:49 +0000293 O << ')';
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000294 }
Evan Chenga8e29892007-01-19 07:51:42 +0000295 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000296 };
297} // end of anonymous namespace
298
299#include "ARMGenAsmWriter.inc"
300
Chris Lattner953ebb72010-01-27 23:58:11 +0000301void ARMAsmPrinter::EmitFunctionEntryLabel() {
302 if (AFI->isThumbFunction()) {
Chris Lattner9d7efd32010-04-04 07:05:53 +0000303 OutStreamer.EmitRawText(StringRef("\t.code\t16"));
Chris Lattner0752cda2010-04-05 16:32:14 +0000304 if (!Subtarget->isTargetDarwin())
Chris Lattner9d7efd32010-04-04 07:05:53 +0000305 OutStreamer.EmitRawText(StringRef("\t.thumb_func"));
Chris Lattner0752cda2010-04-05 16:32:14 +0000306 else {
307 // This needs to emit to a temporary string to get properly quoted
308 // MCSymbols when they have spaces in them.
309 SmallString<128> Tmp;
310 raw_svector_ostream OS(Tmp);
311 OS << "\t.thumb_func\t" << *CurrentFnSym;
312 OutStreamer.EmitRawText(OS.str());
313 }
Chris Lattner953ebb72010-01-27 23:58:11 +0000314 }
315
316 OutStreamer.EmitLabel(CurrentFnSym);
317}
318
Evan Chenga8e29892007-01-19 07:51:42 +0000319/// runOnMachineFunction - This uses the printInstruction()
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000320/// method to print assembly for each instruction.
321///
322bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Evan Chenga8e29892007-01-19 07:51:42 +0000323 AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng6d63a722008-09-18 07:27:23 +0000324 MCP = MF.getConstantPool();
Rafael Espindola4b442b52006-05-23 02:48:20 +0000325
Chris Lattnerd49fe1b2010-01-28 01:28:58 +0000326 return AsmPrinter::runOnMachineFunction(MF);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000327}
328
Evan Cheng055b0312009-06-29 07:51:04 +0000329void ARMAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
Chris Lattner35c33bd2010-04-04 04:47:45 +0000330 raw_ostream &O, const char *Modifier) {
Evan Cheng055b0312009-06-29 07:51:04 +0000331 const MachineOperand &MO = MI->getOperand(OpNum);
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000332 unsigned TF = MO.getTargetFlags();
333
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000334 switch (MO.getType()) {
Chris Lattner8bc86cb2009-10-19 20:59:55 +0000335 default:
336 assert(0 && "<unknown operand type>");
Bob Wilson5bafff32009-06-22 23:27:02 +0000337 case MachineOperand::MO_Register: {
338 unsigned Reg = MO.getReg();
Chris Lattner8bc86cb2009-10-19 20:59:55 +0000339 assert(TargetRegisterInfo::isPhysicalRegister(Reg));
340 if (Modifier && strcmp(Modifier, "dregpair") == 0) {
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000341 unsigned DRegLo = TM.getRegisterInfo()->getSubReg(Reg, ARM::dsub_0);
342 unsigned DRegHi = TM.getRegisterInfo()->getSubReg(Reg, ARM::dsub_1);
Chris Lattner8bc86cb2009-10-19 20:59:55 +0000343 O << '{'
Bob Wilsona0148c32010-07-09 00:47:20 +0000344 << getRegisterName(DRegLo) << ", " << getRegisterName(DRegHi)
Chris Lattner8bc86cb2009-10-19 20:59:55 +0000345 << '}';
346 } else if (Modifier && strcmp(Modifier, "lane") == 0) {
347 unsigned RegNum = ARMRegisterInfo::getRegisterNumbering(Reg);
Chris Lattner9d1c1ad2010-04-04 18:06:11 +0000348 unsigned DReg =
Jakob Stoklund Olesene00fa642010-05-25 00:15:15 +0000349 TM.getRegisterInfo()->getMatchingSuperReg(Reg,
350 RegNum & 1 ? ARM::ssub_1 : ARM::ssub_0, &ARM::DPR_VFP2RegClass);
Chris Lattner8bc86cb2009-10-19 20:59:55 +0000351 O << getRegisterName(DReg) << '[' << (RegNum & 1) << ']';
352 } else {
Anton Korobeynikove8ea0112009-11-07 15:20:32 +0000353 assert(!MO.getSubReg() && "Subregs should be eliminated!");
Chris Lattner8bc86cb2009-10-19 20:59:55 +0000354 O << getRegisterName(Reg);
355 }
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000356 break;
Bob Wilson5bafff32009-06-22 23:27:02 +0000357 }
Evan Chenga8e29892007-01-19 07:51:42 +0000358 case MachineOperand::MO_Immediate: {
Evan Cheng5adb66a2009-09-28 09:14:39 +0000359 int64_t Imm = MO.getImm();
Anton Korobeynikov632606c2009-10-08 20:43:22 +0000360 O << '#';
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000361 if ((Modifier && strcmp(Modifier, "lo16") == 0) ||
362 (TF & ARMII::MO_LO16))
363 O << ":lower16:";
364 else if ((Modifier && strcmp(Modifier, "hi16") == 0) ||
365 (TF & ARMII::MO_HI16))
366 O << ":upper16:";
Anton Korobeynikov632606c2009-10-08 20:43:22 +0000367 O << Imm;
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000368 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000369 }
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000370 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner1b2eb0e2010-03-13 21:04:28 +0000371 O << *MO.getMBB()->getSymbol();
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000372 return;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000373 case MachineOperand::MO_GlobalAddress: {
Evan Chenga8e29892007-01-19 07:51:42 +0000374 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Dan Gohman46510a72010-04-15 01:51:59 +0000375 const GlobalValue *GV = MO.getGlobal();
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000376
377 if ((Modifier && strcmp(Modifier, "lo16") == 0) ||
378 (TF & ARMII::MO_LO16))
379 O << ":lower16:";
380 else if ((Modifier && strcmp(Modifier, "hi16") == 0) ||
381 (TF & ARMII::MO_HI16))
382 O << ":upper16:";
Chris Lattnerd62f1b42010-03-12 21:19:23 +0000383 O << *Mang->getSymbol(GV);
Anton Korobeynikov7751ad92008-11-22 16:15:34 +0000384
Chris Lattner0c08d092010-04-03 22:28:33 +0000385 printOffset(MO.getOffset(), O);
Anton Korobeynikov7751ad92008-11-22 16:15:34 +0000386
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000387 if (isCallOp && Subtarget->isTargetELF() &&
388 TM.getRelocationModel() == Reloc::PIC_)
389 O << "(PLT)";
Evan Chenga8e29892007-01-19 07:51:42 +0000390 break;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000391 }
Evan Chenga8e29892007-01-19 07:51:42 +0000392 case MachineOperand::MO_ExternalSymbol: {
393 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Chris Lattner10b318b2010-01-17 21:43:43 +0000394 O << *GetExternalSymbolSymbol(MO.getSymbolName());
Chris Lattner09533a42010-01-13 08:08:33 +0000395
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000396 if (isCallOp && Subtarget->isTargetELF() &&
397 TM.getRelocationModel() == Reloc::PIC_)
398 O << "(PLT)";
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000399 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000400 }
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000401 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner1b46f432010-01-23 07:00:21 +0000402 O << *GetCPISymbol(MO.getIndex());
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000403 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000404 case MachineOperand::MO_JumpTableIndex:
Chris Lattner1b46f432010-01-23 07:00:21 +0000405 O << *GetJTISymbol(MO.getIndex());
Evan Chenga8e29892007-01-19 07:51:42 +0000406 break;
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000407 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000408}
409
Chris Lattner35c33bd2010-04-04 04:47:45 +0000410static void printSOImm(raw_ostream &O, int64_t V, bool VerboseAsm,
Chris Lattner33adcfb2009-08-22 21:43:10 +0000411 const MCAsmInfo *MAI) {
Evan Chenge7cbe412009-07-08 21:03:57 +0000412 // Break it up into two parts that make up a shifter immediate.
413 V = ARM_AM::getSOImmVal(V);
414 assert(V != -1 && "Not a valid so_imm value!");
415
Evan Chengc70d1842007-03-20 08:11:30 +0000416 unsigned Imm = ARM_AM::getSOImmValImm(V);
417 unsigned Rot = ARM_AM::getSOImmValRot(V);
Anton Korobeynikov7751ad92008-11-22 16:15:34 +0000418
Evan Chenga8e29892007-01-19 07:51:42 +0000419 // Print low-level immediate formation info, per
420 // A5.1.3: "Data-processing operands - Immediate".
421 if (Rot) {
422 O << "#" << Imm << ", " << Rot;
423 // Pretty printed version.
Evan Cheng39382422009-10-28 01:44:26 +0000424 if (VerboseAsm) {
Chris Lattner35c33bd2010-04-04 04:47:45 +0000425 O << "\t" << MAI->getCommentString() << ' ';
Evan Cheng39382422009-10-28 01:44:26 +0000426 O << (int)ARM_AM::rotr32(Imm, Rot);
427 }
Evan Chenga8e29892007-01-19 07:51:42 +0000428 } else {
429 O << "#" << Imm;
430 }
431}
432
Evan Chengc70d1842007-03-20 08:11:30 +0000433/// printSOImmOperand - SOImm is 4-bit rotate amount in bits 8-11 with 8-bit
434/// immediate in bits 0-7.
Chris Lattner35c33bd2010-04-04 04:47:45 +0000435void ARMAsmPrinter::printSOImmOperand(const MachineInstr *MI, int OpNum,
436 raw_ostream &O) {
Evan Chengc70d1842007-03-20 08:11:30 +0000437 const MachineOperand &MO = MI->getOperand(OpNum);
Dan Gohmand735b802008-10-03 15:45:36 +0000438 assert(MO.isImm() && "Not a valid so_imm value!");
Chris Lattner3f53c832010-04-04 18:52:31 +0000439 printSOImm(O, MO.getImm(), isVerbose(), MAI);
Evan Chengc70d1842007-03-20 08:11:30 +0000440}
441
Evan Cheng90922132008-11-06 02:25:39 +0000442/// printSOImm2PartOperand - SOImm is broken into two pieces using a 'mov'
443/// followed by an 'orr' to materialize.
Chris Lattner35c33bd2010-04-04 04:47:45 +0000444void ARMAsmPrinter::printSOImm2PartOperand(const MachineInstr *MI, int OpNum,
445 raw_ostream &O) {
Evan Chengc70d1842007-03-20 08:11:30 +0000446 const MachineOperand &MO = MI->getOperand(OpNum);
Dan Gohmand735b802008-10-03 15:45:36 +0000447 assert(MO.isImm() && "Not a valid so_imm value!");
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000448 unsigned V1 = ARM_AM::getSOImmTwoPartFirst(MO.getImm());
449 unsigned V2 = ARM_AM::getSOImmTwoPartSecond(MO.getImm());
Chris Lattner3f53c832010-04-04 18:52:31 +0000450 printSOImm(O, V1, isVerbose(), MAI);
Evan Cheng5e148a32007-06-05 18:55:18 +0000451 O << "\n\torr";
Chris Lattner35c33bd2010-04-04 04:47:45 +0000452 printPredicateOperand(MI, 2, O);
Evan Cheng162e3092009-10-26 23:45:59 +0000453 O << "\t";
Chris Lattner35c33bd2010-04-04 04:47:45 +0000454 printOperand(MI, 0, O);
Evan Chengc70d1842007-03-20 08:11:30 +0000455 O << ", ";
Chris Lattner35c33bd2010-04-04 04:47:45 +0000456 printOperand(MI, 0, O);
Evan Chengc70d1842007-03-20 08:11:30 +0000457 O << ", ";
Chris Lattner3f53c832010-04-04 18:52:31 +0000458 printSOImm(O, V2, isVerbose(), MAI);
Evan Chengc70d1842007-03-20 08:11:30 +0000459}
460
Evan Chenga8e29892007-01-19 07:51:42 +0000461// so_reg is a 4-operand unit corresponding to register forms of the A5.1
462// "Addressing Mode 1 - Data-processing operands" forms. This includes:
Evan Cheng9cb9e672009-06-27 02:26:13 +0000463// REG 0 0 - e.g. R5
464// REG REG 0,SH_OPC - e.g. R5, ROR R3
Evan Chenga8e29892007-01-19 07:51:42 +0000465// REG 0 IMM,SH_OPC - e.g. R5, LSL #3
Chris Lattner35c33bd2010-04-04 04:47:45 +0000466void ARMAsmPrinter::printSORegOperand(const MachineInstr *MI, int Op,
467 raw_ostream &O) {
Evan Chenga8e29892007-01-19 07:51:42 +0000468 const MachineOperand &MO1 = MI->getOperand(Op);
469 const MachineOperand &MO2 = MI->getOperand(Op+1);
470 const MachineOperand &MO3 = MI->getOperand(Op+2);
471
Chris Lattner762ccea2009-09-13 20:31:40 +0000472 O << getRegisterName(MO1.getReg());
Evan Chenga8e29892007-01-19 07:51:42 +0000473
474 // Print the shift opc.
Bob Wilson1d9125a2010-08-05 00:34:42 +0000475 ARM_AM::ShiftOpc ShOpc = ARM_AM::getSORegShOp(MO3.getImm());
476 O << ", " << ARM_AM::getShiftOpcStr(ShOpc);
Evan Chenga8e29892007-01-19 07:51:42 +0000477 if (MO2.getReg()) {
Bob Wilson1d9125a2010-08-05 00:34:42 +0000478 O << ' ' << getRegisterName(MO2.getReg());
Evan Chenga8e29892007-01-19 07:51:42 +0000479 assert(ARM_AM::getSORegOffset(MO3.getImm()) == 0);
Bob Wilson1d9125a2010-08-05 00:34:42 +0000480 } else if (ShOpc != ARM_AM::rrx) {
481 O << " #" << ARM_AM::getSORegOffset(MO3.getImm());
Evan Chenga8e29892007-01-19 07:51:42 +0000482 }
483}
484
Chris Lattner35c33bd2010-04-04 04:47:45 +0000485void ARMAsmPrinter::printAddrMode2Operand(const MachineInstr *MI, int Op,
486 raw_ostream &O) {
Evan Chenga8e29892007-01-19 07:51:42 +0000487 const MachineOperand &MO1 = MI->getOperand(Op);
488 const MachineOperand &MO2 = MI->getOperand(Op+1);
489 const MachineOperand &MO3 = MI->getOperand(Op+2);
490
Dan Gohmand735b802008-10-03 15:45:36 +0000491 if (!MO1.isReg()) { // FIXME: This is for CP entries, but isn't right.
Chris Lattner35c33bd2010-04-04 04:47:45 +0000492 printOperand(MI, Op, O);
Evan Chenga8e29892007-01-19 07:51:42 +0000493 return;
494 }
495
Chris Lattner762ccea2009-09-13 20:31:40 +0000496 O << "[" << getRegisterName(MO1.getReg());
Evan Chenga8e29892007-01-19 07:51:42 +0000497
498 if (!MO2.getReg()) {
Johnny Chen9e088762010-03-17 17:52:21 +0000499 if (ARM_AM::getAM2Offset(MO3.getImm())) // Don't print +0.
Evan Chenga8e29892007-01-19 07:51:42 +0000500 O << ", #"
Johnny Chen9e088762010-03-17 17:52:21 +0000501 << ARM_AM::getAddrOpcStr(ARM_AM::getAM2Op(MO3.getImm()))
Evan Chenga8e29892007-01-19 07:51:42 +0000502 << ARM_AM::getAM2Offset(MO3.getImm());
503 O << "]";
504 return;
505 }
506
507 O << ", "
Johnny Chen9e088762010-03-17 17:52:21 +0000508 << ARM_AM::getAddrOpcStr(ARM_AM::getAM2Op(MO3.getImm()))
Chris Lattner762ccea2009-09-13 20:31:40 +0000509 << getRegisterName(MO2.getReg());
Jim Grosbache9952212009-09-04 01:38:51 +0000510
Evan Chenga8e29892007-01-19 07:51:42 +0000511 if (unsigned ShImm = ARM_AM::getAM2Offset(MO3.getImm()))
512 O << ", "
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000513 << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO3.getImm()))
Evan Chenga8e29892007-01-19 07:51:42 +0000514 << " #" << ShImm;
515 O << "]";
516}
517
Chris Lattner35c33bd2010-04-04 04:47:45 +0000518void ARMAsmPrinter::printAddrMode2OffsetOperand(const MachineInstr *MI, int Op,
519 raw_ostream &O) {
Evan Chenga8e29892007-01-19 07:51:42 +0000520 const MachineOperand &MO1 = MI->getOperand(Op);
521 const MachineOperand &MO2 = MI->getOperand(Op+1);
522
523 if (!MO1.getReg()) {
Evan Chengbdc98692007-05-03 23:30:36 +0000524 unsigned ImmOffs = ARM_AM::getAM2Offset(MO2.getImm());
Evan Chengbdc98692007-05-03 23:30:36 +0000525 O << "#"
Johnny Chen9e088762010-03-17 17:52:21 +0000526 << ARM_AM::getAddrOpcStr(ARM_AM::getAM2Op(MO2.getImm()))
Evan Chengbdc98692007-05-03 23:30:36 +0000527 << ImmOffs;
Evan Chenga8e29892007-01-19 07:51:42 +0000528 return;
529 }
530
Johnny Chen9e088762010-03-17 17:52:21 +0000531 O << ARM_AM::getAddrOpcStr(ARM_AM::getAM2Op(MO2.getImm()))
Chris Lattner762ccea2009-09-13 20:31:40 +0000532 << getRegisterName(MO1.getReg());
Jim Grosbache9952212009-09-04 01:38:51 +0000533
Evan Chenga8e29892007-01-19 07:51:42 +0000534 if (unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm()))
535 O << ", "
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000536 << ARM_AM::getShiftOpcStr(ARM_AM::getAM2ShiftOpc(MO2.getImm()))
Evan Chenga8e29892007-01-19 07:51:42 +0000537 << " #" << ShImm;
538}
539
Chris Lattner35c33bd2010-04-04 04:47:45 +0000540void ARMAsmPrinter::printAddrMode3Operand(const MachineInstr *MI, int Op,
541 raw_ostream &O) {
Evan Chenga8e29892007-01-19 07:51:42 +0000542 const MachineOperand &MO1 = MI->getOperand(Op);
543 const MachineOperand &MO2 = MI->getOperand(Op+1);
544 const MachineOperand &MO3 = MI->getOperand(Op+2);
Jim Grosbache9952212009-09-04 01:38:51 +0000545
Dan Gohman6f0d0242008-02-10 18:45:23 +0000546 assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
Chris Lattner762ccea2009-09-13 20:31:40 +0000547 O << "[" << getRegisterName(MO1.getReg());
Evan Chenga8e29892007-01-19 07:51:42 +0000548
549 if (MO2.getReg()) {
550 O << ", "
551 << (char)ARM_AM::getAM3Op(MO3.getImm())
Chris Lattner762ccea2009-09-13 20:31:40 +0000552 << getRegisterName(MO2.getReg())
Evan Chenga8e29892007-01-19 07:51:42 +0000553 << "]";
554 return;
555 }
Jim Grosbache9952212009-09-04 01:38:51 +0000556
Evan Chenga8e29892007-01-19 07:51:42 +0000557 if (unsigned ImmOffs = ARM_AM::getAM3Offset(MO3.getImm()))
558 O << ", #"
Johnny Chen9e088762010-03-17 17:52:21 +0000559 << ARM_AM::getAddrOpcStr(ARM_AM::getAM3Op(MO3.getImm()))
Evan Chenga8e29892007-01-19 07:51:42 +0000560 << ImmOffs;
561 O << "]";
562}
563
Chris Lattner35c33bd2010-04-04 04:47:45 +0000564void ARMAsmPrinter::printAddrMode3OffsetOperand(const MachineInstr *MI, int Op,
565 raw_ostream &O){
Evan Chenga8e29892007-01-19 07:51:42 +0000566 const MachineOperand &MO1 = MI->getOperand(Op);
567 const MachineOperand &MO2 = MI->getOperand(Op+1);
568
569 if (MO1.getReg()) {
570 O << (char)ARM_AM::getAM3Op(MO2.getImm())
Chris Lattner762ccea2009-09-13 20:31:40 +0000571 << getRegisterName(MO1.getReg());
Evan Chenga8e29892007-01-19 07:51:42 +0000572 return;
573 }
574
575 unsigned ImmOffs = ARM_AM::getAM3Offset(MO2.getImm());
576 O << "#"
Johnny Chen9e088762010-03-17 17:52:21 +0000577 << ARM_AM::getAddrOpcStr(ARM_AM::getAM3Op(MO2.getImm()))
Evan Chenga8e29892007-01-19 07:51:42 +0000578 << ImmOffs;
579}
Jim Grosbache9952212009-09-04 01:38:51 +0000580
Evan Chenga8e29892007-01-19 07:51:42 +0000581void ARMAsmPrinter::printAddrMode4Operand(const MachineInstr *MI, int Op,
Chris Lattner35c33bd2010-04-04 04:47:45 +0000582 raw_ostream &O,
Evan Chenga8e29892007-01-19 07:51:42 +0000583 const char *Modifier) {
Evan Chenga8e29892007-01-19 07:51:42 +0000584 const MachineOperand &MO2 = MI->getOperand(Op+1);
585 ARM_AM::AMSubMode Mode = ARM_AM::getAM4SubMode(MO2.getImm());
586 if (Modifier && strcmp(Modifier, "submode") == 0) {
Bob Wilsonea7f22c2010-03-16 16:19:07 +0000587 O << ARM_AM::getAMSubModeStr(Mode);
Evan Chengd77c7ab2009-08-07 21:19:10 +0000588 } else if (Modifier && strcmp(Modifier, "wide") == 0) {
589 ARM_AM::AMSubMode Mode = ARM_AM::getAM4SubMode(MO2.getImm());
590 if (Mode == ARM_AM::ia)
591 O << ".w";
Evan Chenga8e29892007-01-19 07:51:42 +0000592 } else {
Chris Lattner35c33bd2010-04-04 04:47:45 +0000593 printOperand(MI, Op, O);
Evan Chenga8e29892007-01-19 07:51:42 +0000594 }
595}
596
597void ARMAsmPrinter::printAddrMode5Operand(const MachineInstr *MI, int Op,
Chris Lattner35c33bd2010-04-04 04:47:45 +0000598 raw_ostream &O,
Evan Chenga8e29892007-01-19 07:51:42 +0000599 const char *Modifier) {
600 const MachineOperand &MO1 = MI->getOperand(Op);
601 const MachineOperand &MO2 = MI->getOperand(Op+1);
602
Dan Gohmand735b802008-10-03 15:45:36 +0000603 if (!MO1.isReg()) { // FIXME: This is for CP entries, but isn't right.
Chris Lattner35c33bd2010-04-04 04:47:45 +0000604 printOperand(MI, Op, O);
Evan Chenga8e29892007-01-19 07:51:42 +0000605 return;
606 }
Jim Grosbache9952212009-09-04 01:38:51 +0000607
Dan Gohman6f0d0242008-02-10 18:45:23 +0000608 assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
Evan Chenga8e29892007-01-19 07:51:42 +0000609
610 if (Modifier && strcmp(Modifier, "submode") == 0) {
611 ARM_AM::AMSubMode Mode = ARM_AM::getAM5SubMode(MO2.getImm());
Jim Grosbache5165492009-11-09 00:11:35 +0000612 O << ARM_AM::getAMSubModeStr(Mode);
Evan Chenga8e29892007-01-19 07:51:42 +0000613 return;
614 } else if (Modifier && strcmp(Modifier, "base") == 0) {
615 // Used for FSTM{D|S} and LSTM{D|S} operations.
Chris Lattner762ccea2009-09-13 20:31:40 +0000616 O << getRegisterName(MO1.getReg());
Evan Chenga8e29892007-01-19 07:51:42 +0000617 return;
618 }
Jim Grosbache9952212009-09-04 01:38:51 +0000619
Chris Lattner762ccea2009-09-13 20:31:40 +0000620 O << "[" << getRegisterName(MO1.getReg());
Jim Grosbache9952212009-09-04 01:38:51 +0000621
Evan Chenga8e29892007-01-19 07:51:42 +0000622 if (unsigned ImmOffs = ARM_AM::getAM5Offset(MO2.getImm())) {
623 O << ", #"
Johnny Chen9e088762010-03-17 17:52:21 +0000624 << ARM_AM::getAddrOpcStr(ARM_AM::getAM5Op(MO2.getImm()))
Evan Chenga8e29892007-01-19 07:51:42 +0000625 << ImmOffs*4;
626 }
627 O << "]";
628}
629
Chris Lattner35c33bd2010-04-04 04:47:45 +0000630void ARMAsmPrinter::printAddrMode6Operand(const MachineInstr *MI, int Op,
631 raw_ostream &O) {
Bob Wilson8b024a52009-07-01 23:16:05 +0000632 const MachineOperand &MO1 = MI->getOperand(Op);
633 const MachineOperand &MO2 = MI->getOperand(Op+1);
Bob Wilson8b024a52009-07-01 23:16:05 +0000634
Jim Grosbach8a5ec862009-11-07 21:25:39 +0000635 O << "[" << getRegisterName(MO1.getReg());
Bob Wilson226036e2010-03-20 22:13:40 +0000636 if (MO2.getImm()) {
Anton Korobeynikovbce3dbd2009-11-17 20:04:59 +0000637 // FIXME: Both darwin as and GNU as violate ARM docs here.
Bob Wilson273ff312010-07-14 23:54:43 +0000638 O << ", :" << (MO2.getImm() << 3);
Jim Grosbach8a5ec862009-11-07 21:25:39 +0000639 }
640 O << "]";
Bob Wilson226036e2010-03-20 22:13:40 +0000641}
Bob Wilsona43e6bf2010-03-16 23:01:13 +0000642
Chris Lattner35c33bd2010-04-04 04:47:45 +0000643void ARMAsmPrinter::printAddrMode6OffsetOperand(const MachineInstr *MI, int Op,
644 raw_ostream &O){
Bob Wilson226036e2010-03-20 22:13:40 +0000645 const MachineOperand &MO = MI->getOperand(Op);
646 if (MO.getReg() == 0)
647 O << "!";
648 else
649 O << ", " << getRegisterName(MO.getReg());
Bob Wilson8b024a52009-07-01 23:16:05 +0000650}
651
Evan Chenga8e29892007-01-19 07:51:42 +0000652void ARMAsmPrinter::printAddrModePCOperand(const MachineInstr *MI, int Op,
Chris Lattner35c33bd2010-04-04 04:47:45 +0000653 raw_ostream &O,
Evan Chenga8e29892007-01-19 07:51:42 +0000654 const char *Modifier) {
655 if (Modifier && strcmp(Modifier, "label") == 0) {
Chris Lattner35c33bd2010-04-04 04:47:45 +0000656 printPCLabel(MI, Op+1, O);
Evan Chenga8e29892007-01-19 07:51:42 +0000657 return;
658 }
659
660 const MachineOperand &MO1 = MI->getOperand(Op);
Dan Gohman6f0d0242008-02-10 18:45:23 +0000661 assert(TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
Johnny Chen9e088762010-03-17 17:52:21 +0000662 O << "[pc, " << getRegisterName(MO1.getReg()) << "]";
Evan Chenga8e29892007-01-19 07:51:42 +0000663}
664
665void
Chris Lattner35c33bd2010-04-04 04:47:45 +0000666ARMAsmPrinter::printBitfieldInvMaskImmOperand(const MachineInstr *MI, int Op,
667 raw_ostream &O) {
Evan Chengf49810c2009-06-23 17:48:47 +0000668 const MachineOperand &MO = MI->getOperand(Op);
669 uint32_t v = ~MO.getImm();
Evan Cheng9e03cbe2009-06-25 22:04:44 +0000670 int32_t lsb = CountTrailingZeros_32(v);
Nick Lewyckyb825aaa2009-06-24 01:08:42 +0000671 int32_t width = (32 - CountLeadingZeros_32 (v)) - lsb;
Evan Chengf49810c2009-06-23 17:48:47 +0000672 assert(MO.isImm() && "Not a valid bf_inv_mask_imm value!");
673 O << "#" << lsb << ", #" << width;
674}
675
Johnny Chen1adc40c2010-08-12 20:46:17 +0000676void
677ARMAsmPrinter::printMemBOption(const MachineInstr *MI, int OpNum,
678 raw_ostream &O) {
679 unsigned val = MI->getOperand(OpNum).getImm();
680 O << ARM_MB::MemBOptToString(val);
681}
682
Bob Wilsoneaf1c982010-08-11 23:10:46 +0000683void ARMAsmPrinter::printSatShiftOperand(const MachineInstr *MI, int OpNum,
684 raw_ostream &O) {
685 unsigned ShiftOp = MI->getOperand(OpNum).getImm();
686 ARM_AM::ShiftOpc Opc = ARM_AM::getSORegShOp(ShiftOp);
687 switch (Opc) {
688 case ARM_AM::no_shift:
689 return;
690 case ARM_AM::lsl:
691 O << ", lsl #";
692 break;
693 case ARM_AM::asr:
694 O << ", asr #";
695 break;
696 default:
697 assert(0 && "unexpected shift opcode for saturate shift operand");
698 }
699 O << ARM_AM::getSORegOffset(ShiftOp);
700}
701
Evan Cheng055b0312009-06-29 07:51:04 +0000702//===--------------------------------------------------------------------===//
703
Chris Lattner35c33bd2010-04-04 04:47:45 +0000704void ARMAsmPrinter::printThumbS4ImmOperand(const MachineInstr *MI, int Op,
705 raw_ostream &O) {
Evan Cheng2ef9c8a2009-11-19 06:57:41 +0000706 O << "#" << MI->getOperand(Op).getImm() * 4;
707}
708
Evan Chengf49810c2009-06-23 17:48:47 +0000709void
Chris Lattner35c33bd2010-04-04 04:47:45 +0000710ARMAsmPrinter::printThumbITMask(const MachineInstr *MI, int Op,
711 raw_ostream &O) {
Evan Chenge5564742009-07-09 23:43:36 +0000712 // (3 - the number of trailing zeros) is the number of then / else.
713 unsigned Mask = MI->getOperand(Op).getImm();
Johnny Chen9e088762010-03-17 17:52:21 +0000714 unsigned CondBit0 = Mask >> 4 & 1;
Evan Chenge5564742009-07-09 23:43:36 +0000715 unsigned NumTZ = CountTrailingZeros_32(Mask);
716 assert(NumTZ <= 3 && "Invalid IT mask!");
Evan Cheng06e16582009-07-10 01:54:42 +0000717 for (unsigned Pos = 3, e = NumTZ; Pos > e; --Pos) {
Johnny Chen9e088762010-03-17 17:52:21 +0000718 bool T = ((Mask >> Pos) & 1) == CondBit0;
Evan Chenge5564742009-07-09 23:43:36 +0000719 if (T)
720 O << 't';
721 else
722 O << 'e';
723 }
724}
725
726void
Chris Lattner35c33bd2010-04-04 04:47:45 +0000727ARMAsmPrinter::printThumbAddrModeRROperand(const MachineInstr *MI, int Op,
728 raw_ostream &O) {
Evan Chenga8e29892007-01-19 07:51:42 +0000729 const MachineOperand &MO1 = MI->getOperand(Op);
730 const MachineOperand &MO2 = MI->getOperand(Op+1);
Chris Lattner762ccea2009-09-13 20:31:40 +0000731 O << "[" << getRegisterName(MO1.getReg());
732 O << ", " << getRegisterName(MO2.getReg()) << "]";
Evan Chenga8e29892007-01-19 07:51:42 +0000733}
734
735void
736ARMAsmPrinter::printThumbAddrModeRI5Operand(const MachineInstr *MI, int Op,
Chris Lattner35c33bd2010-04-04 04:47:45 +0000737 raw_ostream &O,
Evan Chenga8e29892007-01-19 07:51:42 +0000738 unsigned Scale) {
739 const MachineOperand &MO1 = MI->getOperand(Op);
Evan Chengcea117d2007-01-30 02:35:32 +0000740 const MachineOperand &MO2 = MI->getOperand(Op+1);
741 const MachineOperand &MO3 = MI->getOperand(Op+2);
Evan Chenga8e29892007-01-19 07:51:42 +0000742
Dan Gohmand735b802008-10-03 15:45:36 +0000743 if (!MO1.isReg()) { // FIXME: This is for CP entries, but isn't right.
Chris Lattner35c33bd2010-04-04 04:47:45 +0000744 printOperand(MI, Op, O);
Evan Chenga8e29892007-01-19 07:51:42 +0000745 return;
746 }
747
Chris Lattner762ccea2009-09-13 20:31:40 +0000748 O << "[" << getRegisterName(MO1.getReg());
Evan Chengcea117d2007-01-30 02:35:32 +0000749 if (MO3.getReg())
Chris Lattner762ccea2009-09-13 20:31:40 +0000750 O << ", " << getRegisterName(MO3.getReg());
Evan Cheng4b6bbe12009-11-10 19:48:13 +0000751 else if (unsigned ImmOffs = MO2.getImm())
Johnny Chen9e088762010-03-17 17:52:21 +0000752 O << ", #" << ImmOffs * Scale;
Evan Chenga8e29892007-01-19 07:51:42 +0000753 O << "]";
754}
755
756void
Chris Lattner35c33bd2010-04-04 04:47:45 +0000757ARMAsmPrinter::printThumbAddrModeS1Operand(const MachineInstr *MI, int Op,
758 raw_ostream &O) {
759 printThumbAddrModeRI5Operand(MI, Op, O, 1);
Evan Chenga8e29892007-01-19 07:51:42 +0000760}
761void
Chris Lattner35c33bd2010-04-04 04:47:45 +0000762ARMAsmPrinter::printThumbAddrModeS2Operand(const MachineInstr *MI, int Op,
763 raw_ostream &O) {
764 printThumbAddrModeRI5Operand(MI, Op, O, 2);
Evan Chenga8e29892007-01-19 07:51:42 +0000765}
766void
Chris Lattner35c33bd2010-04-04 04:47:45 +0000767ARMAsmPrinter::printThumbAddrModeS4Operand(const MachineInstr *MI, int Op,
768 raw_ostream &O) {
769 printThumbAddrModeRI5Operand(MI, Op, O, 4);
Evan Chenga8e29892007-01-19 07:51:42 +0000770}
771
Chris Lattner35c33bd2010-04-04 04:47:45 +0000772void ARMAsmPrinter::printThumbAddrModeSPOperand(const MachineInstr *MI,int Op,
773 raw_ostream &O) {
Evan Chenga8e29892007-01-19 07:51:42 +0000774 const MachineOperand &MO1 = MI->getOperand(Op);
775 const MachineOperand &MO2 = MI->getOperand(Op+1);
Chris Lattner762ccea2009-09-13 20:31:40 +0000776 O << "[" << getRegisterName(MO1.getReg());
Evan Chenga8e29892007-01-19 07:51:42 +0000777 if (unsigned ImmOffs = MO2.getImm())
Johnny Chen9e088762010-03-17 17:52:21 +0000778 O << ", #" << ImmOffs*4;
Evan Chenga8e29892007-01-19 07:51:42 +0000779 O << "]";
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000780}
781
Evan Cheng055b0312009-06-29 07:51:04 +0000782//===--------------------------------------------------------------------===//
783
Evan Cheng9cb9e672009-06-27 02:26:13 +0000784// Constant shifts t2_so_reg is a 2-operand unit corresponding to the Thumb2
785// register with shift forms.
786// REG 0 0 - e.g. R5
787// REG IMM, SH_OPC - e.g. R5, LSL #3
Chris Lattner35c33bd2010-04-04 04:47:45 +0000788void ARMAsmPrinter::printT2SOOperand(const MachineInstr *MI, int OpNum,
789 raw_ostream &O) {
Evan Cheng9cb9e672009-06-27 02:26:13 +0000790 const MachineOperand &MO1 = MI->getOperand(OpNum);
791 const MachineOperand &MO2 = MI->getOperand(OpNum+1);
792
793 unsigned Reg = MO1.getReg();
794 assert(TargetRegisterInfo::isPhysicalRegister(Reg));
Chris Lattner762ccea2009-09-13 20:31:40 +0000795 O << getRegisterName(Reg);
Evan Cheng9cb9e672009-06-27 02:26:13 +0000796
797 // Print the shift opc.
Evan Cheng9cb9e672009-06-27 02:26:13 +0000798 assert(MO2.isImm() && "Not a valid t2_so_reg value!");
Bob Wilson1d9125a2010-08-05 00:34:42 +0000799 ARM_AM::ShiftOpc ShOpc = ARM_AM::getSORegShOp(MO2.getImm());
800 O << ", " << ARM_AM::getShiftOpcStr(ShOpc);
801 if (ShOpc != ARM_AM::rrx)
802 O << " #" << ARM_AM::getSORegOffset(MO2.getImm());
Evan Cheng9cb9e672009-06-27 02:26:13 +0000803}
804
Evan Cheng055b0312009-06-29 07:51:04 +0000805void ARMAsmPrinter::printT2AddrModeImm12Operand(const MachineInstr *MI,
Chris Lattner35c33bd2010-04-04 04:47:45 +0000806 int OpNum,
807 raw_ostream &O) {
Evan Cheng055b0312009-06-29 07:51:04 +0000808 const MachineOperand &MO1 = MI->getOperand(OpNum);
809 const MachineOperand &MO2 = MI->getOperand(OpNum+1);
Evan Cheng9cb9e672009-06-27 02:26:13 +0000810
Chris Lattner762ccea2009-09-13 20:31:40 +0000811 O << "[" << getRegisterName(MO1.getReg());
Evan Cheng055b0312009-06-29 07:51:04 +0000812
813 unsigned OffImm = MO2.getImm();
814 if (OffImm) // Don't print +0.
Johnny Chen9e088762010-03-17 17:52:21 +0000815 O << ", #" << OffImm;
Evan Cheng055b0312009-06-29 07:51:04 +0000816 O << "]";
817}
818
819void ARMAsmPrinter::printT2AddrModeImm8Operand(const MachineInstr *MI,
Chris Lattner35c33bd2010-04-04 04:47:45 +0000820 int OpNum,
821 raw_ostream &O) {
Evan Cheng055b0312009-06-29 07:51:04 +0000822 const MachineOperand &MO1 = MI->getOperand(OpNum);
823 const MachineOperand &MO2 = MI->getOperand(OpNum+1);
824
Chris Lattner762ccea2009-09-13 20:31:40 +0000825 O << "[" << getRegisterName(MO1.getReg());
Evan Cheng055b0312009-06-29 07:51:04 +0000826
827 int32_t OffImm = (int32_t)MO2.getImm();
828 // Don't print +0.
829 if (OffImm < 0)
830 O << ", #-" << -OffImm;
831 else if (OffImm > 0)
Johnny Chen9e088762010-03-17 17:52:21 +0000832 O << ", #" << OffImm;
Evan Cheng055b0312009-06-29 07:51:04 +0000833 O << "]";
834}
835
Evan Cheng5c874172009-07-09 22:21:59 +0000836void ARMAsmPrinter::printT2AddrModeImm8s4Operand(const MachineInstr *MI,
Chris Lattner35c33bd2010-04-04 04:47:45 +0000837 int OpNum,
838 raw_ostream &O) {
Evan Cheng5c874172009-07-09 22:21:59 +0000839 const MachineOperand &MO1 = MI->getOperand(OpNum);
840 const MachineOperand &MO2 = MI->getOperand(OpNum+1);
841
Chris Lattner762ccea2009-09-13 20:31:40 +0000842 O << "[" << getRegisterName(MO1.getReg());
Evan Cheng5c874172009-07-09 22:21:59 +0000843
844 int32_t OffImm = (int32_t)MO2.getImm() / 4;
845 // Don't print +0.
846 if (OffImm < 0)
Evan Chenga64ce452009-11-19 06:31:26 +0000847 O << ", #-" << -OffImm * 4;
Evan Cheng5c874172009-07-09 22:21:59 +0000848 else if (OffImm > 0)
Johnny Chen9e088762010-03-17 17:52:21 +0000849 O << ", #" << OffImm * 4;
Evan Cheng5c874172009-07-09 22:21:59 +0000850 O << "]";
851}
852
Evan Chenge88d5ce2009-07-02 07:28:31 +0000853void ARMAsmPrinter::printT2AddrModeImm8OffsetOperand(const MachineInstr *MI,
Chris Lattner35c33bd2010-04-04 04:47:45 +0000854 int OpNum,
855 raw_ostream &O) {
Evan Chenge88d5ce2009-07-02 07:28:31 +0000856 const MachineOperand &MO1 = MI->getOperand(OpNum);
857 int32_t OffImm = (int32_t)MO1.getImm();
858 // Don't print +0.
859 if (OffImm < 0)
860 O << "#-" << -OffImm;
861 else if (OffImm > 0)
Johnny Chen9e088762010-03-17 17:52:21 +0000862 O << "#" << OffImm;
863}
864
Evan Cheng055b0312009-06-29 07:51:04 +0000865void ARMAsmPrinter::printT2AddrModeSoRegOperand(const MachineInstr *MI,
Chris Lattner35c33bd2010-04-04 04:47:45 +0000866 int OpNum,
867 raw_ostream &O) {
Evan Cheng055b0312009-06-29 07:51:04 +0000868 const MachineOperand &MO1 = MI->getOperand(OpNum);
869 const MachineOperand &MO2 = MI->getOperand(OpNum+1);
870 const MachineOperand &MO3 = MI->getOperand(OpNum+2);
871
Chris Lattner762ccea2009-09-13 20:31:40 +0000872 O << "[" << getRegisterName(MO1.getReg());
Evan Cheng055b0312009-06-29 07:51:04 +0000873
Evan Cheng3a214252009-08-11 08:52:18 +0000874 assert(MO2.getReg() && "Invalid so_reg load / store address!");
Chris Lattner762ccea2009-09-13 20:31:40 +0000875 O << ", " << getRegisterName(MO2.getReg());
Evan Cheng055b0312009-06-29 07:51:04 +0000876
Evan Cheng3a214252009-08-11 08:52:18 +0000877 unsigned ShAmt = MO3.getImm();
878 if (ShAmt) {
879 assert(ShAmt <= 3 && "Not a valid Thumb2 addressing mode!");
880 O << ", lsl #" << ShAmt;
Evan Cheng055b0312009-06-29 07:51:04 +0000881 }
882 O << "]";
883}
884
885
886//===--------------------------------------------------------------------===//
887
Chris Lattner35c33bd2010-04-04 04:47:45 +0000888void ARMAsmPrinter::printPredicateOperand(const MachineInstr *MI, int OpNum,
889 raw_ostream &O) {
Evan Cheng055b0312009-06-29 07:51:04 +0000890 ARMCC::CondCodes CC = (ARMCC::CondCodes)MI->getOperand(OpNum).getImm();
Evan Cheng44bec522007-05-15 01:29:07 +0000891 if (CC != ARMCC::AL)
892 O << ARMCondCodeToString(CC);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000893}
894
Johnny Chen9d3acaa2010-03-02 17:57:15 +0000895void ARMAsmPrinter::printMandatoryPredicateOperand(const MachineInstr *MI,
Chris Lattner35c33bd2010-04-04 04:47:45 +0000896 int OpNum,
897 raw_ostream &O) {
Johnny Chen9d3acaa2010-03-02 17:57:15 +0000898 ARMCC::CondCodes CC = (ARMCC::CondCodes)MI->getOperand(OpNum).getImm();
899 O << ARMCondCodeToString(CC);
900}
901
Chris Lattner35c33bd2010-04-04 04:47:45 +0000902void ARMAsmPrinter::printSBitModifierOperand(const MachineInstr *MI, int OpNum,
903 raw_ostream &O){
Evan Cheng055b0312009-06-29 07:51:04 +0000904 unsigned Reg = MI->getOperand(OpNum).getReg();
Evan Chengdfb2eba2007-07-06 01:01:34 +0000905 if (Reg) {
906 assert(Reg == ARM::CPSR && "Expect ARM CPSR register!");
907 O << 's';
908 }
909}
910
Chris Lattner35c33bd2010-04-04 04:47:45 +0000911void ARMAsmPrinter::printPCLabel(const MachineInstr *MI, int OpNum,
912 raw_ostream &O) {
Evan Cheng055b0312009-06-29 07:51:04 +0000913 int Id = (int)MI->getOperand(OpNum).getImm();
Evan Chenge7e0d622009-11-06 22:24:13 +0000914 O << MAI->getPrivateGlobalPrefix()
915 << "PC" << getFunctionNumber() << "_" << Id;
Evan Chenga8e29892007-01-19 07:51:42 +0000916}
917
Chris Lattner35c33bd2010-04-04 04:47:45 +0000918void ARMAsmPrinter::printRegisterList(const MachineInstr *MI, int OpNum,
919 raw_ostream &O) {
Evan Chenga8e29892007-01-19 07:51:42 +0000920 O << "{";
Bob Wilson815baeb2010-03-13 01:08:20 +0000921 for (unsigned i = OpNum, e = MI->getNumOperands(); i != e; ++i) {
Evan Cheng4b322e52009-08-11 21:11:32 +0000922 if (MI->getOperand(i).isImplicit())
923 continue;
Bob Wilson815baeb2010-03-13 01:08:20 +0000924 if ((int)i != OpNum) O << ", ";
Chris Lattner35c33bd2010-04-04 04:47:45 +0000925 printOperand(MI, i, O);
Evan Chenga8e29892007-01-19 07:51:42 +0000926 }
927 O << "}";
928}
929
Evan Cheng055b0312009-06-29 07:51:04 +0000930void ARMAsmPrinter::printCPInstOperand(const MachineInstr *MI, int OpNum,
Chris Lattner35c33bd2010-04-04 04:47:45 +0000931 raw_ostream &O, const char *Modifier) {
Evan Chenga8e29892007-01-19 07:51:42 +0000932 assert(Modifier && "This operand only works with a modifier!");
933 // There are two aspects to a CONSTANTPOOL_ENTRY operand, the label and the
934 // data itself.
935 if (!strcmp(Modifier, "label")) {
Evan Cheng055b0312009-06-29 07:51:04 +0000936 unsigned ID = MI->getOperand(OpNum).getImm();
Chris Lattner8e089a92010-02-10 00:36:00 +0000937 OutStreamer.EmitLabel(GetCPISymbol(ID));
Evan Chenga8e29892007-01-19 07:51:42 +0000938 } else {
939 assert(!strcmp(Modifier, "cpentry") && "Unknown modifier for CPE");
Evan Cheng055b0312009-06-29 07:51:04 +0000940 unsigned CPI = MI->getOperand(OpNum).getIndex();
Evan Chenga8e29892007-01-19 07:51:42 +0000941
Evan Cheng6d63a722008-09-18 07:27:23 +0000942 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPI];
Jim Grosbache9952212009-09-04 01:38:51 +0000943
Evan Cheng711b6dc2008-08-08 06:56:16 +0000944 if (MCPE.isMachineConstantPoolEntry()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000945 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
Evan Cheng711b6dc2008-08-08 06:56:16 +0000946 } else {
Evan Chenga8e29892007-01-19 07:51:42 +0000947 EmitGlobalConstant(MCPE.Val.ConstVal);
Lauro Ramos Venancio305b8a52007-04-25 14:50:40 +0000948 }
Evan Chenga8e29892007-01-19 07:51:42 +0000949 }
950}
951
Chris Lattner0890cf12010-01-25 19:51:38 +0000952MCSymbol *ARMAsmPrinter::
953GetARMSetPICJumpTableLabel2(unsigned uid, unsigned uid2,
954 const MachineBasicBlock *MBB) const {
955 SmallString<60> Name;
956 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix()
Chris Lattnerbfcb0962010-01-25 19:39:52 +0000957 << getFunctionNumber() << '_' << uid << '_' << uid2
Chris Lattner0890cf12010-01-25 19:51:38 +0000958 << "_set_" << MBB->getNumber();
Chris Lattner9b97a732010-03-30 18:10:53 +0000959 return OutContext.GetOrCreateSymbol(Name.str());
Chris Lattner0890cf12010-01-25 19:51:38 +0000960}
961
962MCSymbol *ARMAsmPrinter::
963GetARMJTIPICJumpTableLabel2(unsigned uid, unsigned uid2) const {
964 SmallString<60> Name;
965 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "JTI"
Chris Lattner281e7762010-01-25 23:28:03 +0000966 << getFunctionNumber() << '_' << uid << '_' << uid2;
Chris Lattner9b97a732010-03-30 18:10:53 +0000967 return OutContext.GetOrCreateSymbol(Name.str());
Chris Lattnerbfcb0962010-01-25 19:39:52 +0000968}
969
Chris Lattner35c33bd2010-04-04 04:47:45 +0000970void ARMAsmPrinter::printJTBlockOperand(const MachineInstr *MI, int OpNum,
971 raw_ostream &O) {
Evan Cheng66ac5312009-07-25 00:33:29 +0000972 assert(!Subtarget->isThumb2() && "Thumb2 should use double-jump jumptables!");
973
Evan Cheng055b0312009-06-29 07:51:04 +0000974 const MachineOperand &MO1 = MI->getOperand(OpNum);
975 const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
Chris Lattner1b46f432010-01-23 07:00:21 +0000976
Chris Lattner8aa797a2007-12-30 23:10:15 +0000977 unsigned JTI = MO1.getIndex();
Chris Lattner0890cf12010-01-25 19:51:38 +0000978 MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel2(JTI, MO2.getImm());
Chris Lattner03335352010-04-05 17:52:31 +0000979 // Can't use EmitLabel until instprinter happens, label comes out in the wrong
980 // order.
Bob Wilsond4d188e2010-07-31 06:28:10 +0000981 O << "\n" << *JTISymbol << ":\n";
Evan Chenga8e29892007-01-19 07:51:42 +0000982
Chris Lattner33adcfb2009-08-22 21:43:10 +0000983 const char *JTEntryDirective = MAI->getData32bitsDirective();
Evan Chenga8e29892007-01-19 07:51:42 +0000984
Dan Gohman45426112008-07-07 20:06:06 +0000985 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
Evan Chenga8e29892007-01-19 07:51:42 +0000986 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
987 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
Chris Lattnercee63322010-01-26 20:40:54 +0000988 bool UseSet= MAI->hasSetDirective() && TM.getRelocationModel() == Reloc::PIC_;
Evan Chengc324ecb2009-07-24 18:19:46 +0000989 SmallPtrSet<MachineBasicBlock*, 8> JTSets;
Evan Chenga8e29892007-01-19 07:51:42 +0000990 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
991 MachineBasicBlock *MBB = JTBBs[i];
Evan Cheng66ac5312009-07-25 00:33:29 +0000992 bool isNew = JTSets.insert(MBB);
993
Chris Lattner0890cf12010-01-25 19:51:38 +0000994 if (UseSet && isNew) {
Chris Lattnercee63322010-01-26 20:40:54 +0000995 O << "\t.set\t"
Jim Grosbach1f9b48a2010-01-25 23:50:13 +0000996 << *GetARMSetPICJumpTableLabel2(JTI, MO2.getImm(), MBB) << ','
Chris Lattner1b2eb0e2010-03-13 21:04:28 +0000997 << *MBB->getSymbol() << '-' << *JTISymbol << '\n';
Chris Lattner0890cf12010-01-25 19:51:38 +0000998 }
Evan Chenga8e29892007-01-19 07:51:42 +0000999
1000 O << JTEntryDirective << ' ';
1001 if (UseSet)
Chris Lattner0890cf12010-01-25 19:51:38 +00001002 O << *GetARMSetPICJumpTableLabel2(JTI, MO2.getImm(), MBB);
1003 else if (TM.getRelocationModel() == Reloc::PIC_)
Chris Lattner1b2eb0e2010-03-13 21:04:28 +00001004 O << *MBB->getSymbol() << '-' << *JTISymbol;
Chris Lattner0890cf12010-01-25 19:51:38 +00001005 else
Chris Lattner1b2eb0e2010-03-13 21:04:28 +00001006 O << *MBB->getSymbol();
Chris Lattner0890cf12010-01-25 19:51:38 +00001007
Evan Chengd85ac4d2007-01-27 02:29:45 +00001008 if (i != e-1)
1009 O << '\n';
Evan Chenga8e29892007-01-19 07:51:42 +00001010 }
1011}
1012
Chris Lattner35c33bd2010-04-04 04:47:45 +00001013void ARMAsmPrinter::printJT2BlockOperand(const MachineInstr *MI, int OpNum,
1014 raw_ostream &O) {
Evan Cheng66ac5312009-07-25 00:33:29 +00001015 const MachineOperand &MO1 = MI->getOperand(OpNum);
1016 const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
1017 unsigned JTI = MO1.getIndex();
Chris Lattner0890cf12010-01-25 19:51:38 +00001018
1019 MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel2(JTI, MO2.getImm());
Chris Lattner03335352010-04-05 17:52:31 +00001020
1021 // Can't use EmitLabel until instprinter happens, label comes out in the wrong
1022 // order.
Bob Wilsond4d188e2010-07-31 06:28:10 +00001023 O << "\n" << *JTISymbol << ":\n";
Evan Cheng66ac5312009-07-25 00:33:29 +00001024
Evan Cheng66ac5312009-07-25 00:33:29 +00001025 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
1026 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
1027 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
Evan Cheng5657c012009-07-29 02:18:14 +00001028 bool ByteOffset = false, HalfWordOffset = false;
1029 if (MI->getOpcode() == ARM::t2TBB)
1030 ByteOffset = true;
1031 else if (MI->getOpcode() == ARM::t2TBH)
1032 HalfWordOffset = true;
1033
Evan Cheng66ac5312009-07-25 00:33:29 +00001034 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
1035 MachineBasicBlock *MBB = JTBBs[i];
Evan Cheng5657c012009-07-29 02:18:14 +00001036 if (ByteOffset)
Chris Lattner33adcfb2009-08-22 21:43:10 +00001037 O << MAI->getData8bitsDirective();
Evan Cheng5657c012009-07-29 02:18:14 +00001038 else if (HalfWordOffset)
Chris Lattner33adcfb2009-08-22 21:43:10 +00001039 O << MAI->getData16bitsDirective();
Chris Lattner0890cf12010-01-25 19:51:38 +00001040
1041 if (ByteOffset || HalfWordOffset)
Chris Lattner1b2eb0e2010-03-13 21:04:28 +00001042 O << '(' << *MBB->getSymbol() << "-" << *JTISymbol << ")/2";
Chris Lattner0890cf12010-01-25 19:51:38 +00001043 else
Chris Lattner1b2eb0e2010-03-13 21:04:28 +00001044 O << "\tb.w " << *MBB->getSymbol();
Chris Lattner0890cf12010-01-25 19:51:38 +00001045
Evan Cheng66ac5312009-07-25 00:33:29 +00001046 if (i != e-1)
1047 O << '\n';
1048 }
1049}
1050
Chris Lattner35c33bd2010-04-04 04:47:45 +00001051void ARMAsmPrinter::printTBAddrMode(const MachineInstr *MI, int OpNum,
1052 raw_ostream &O) {
Chris Lattner762ccea2009-09-13 20:31:40 +00001053 O << "[pc, " << getRegisterName(MI->getOperand(OpNum).getReg());
Evan Cheng5657c012009-07-29 02:18:14 +00001054 if (MI->getOpcode() == ARM::t2TBH)
1055 O << ", lsl #1";
1056 O << ']';
1057}
1058
Chris Lattner35c33bd2010-04-04 04:47:45 +00001059void ARMAsmPrinter::printNoHashImmediate(const MachineInstr *MI, int OpNum,
1060 raw_ostream &O) {
Anton Korobeynikov8e9ece72009-08-08 23:10:41 +00001061 O << MI->getOperand(OpNum).getImm();
1062}
Evan Chenga8e29892007-01-19 07:51:42 +00001063
Chris Lattner35c33bd2010-04-04 04:47:45 +00001064void ARMAsmPrinter::printVFPf32ImmOperand(const MachineInstr *MI, int OpNum,
1065 raw_ostream &O) {
Evan Cheng39382422009-10-28 01:44:26 +00001066 const ConstantFP *FP = MI->getOperand(OpNum).getFPImm();
Jim Grosbach77b02be2009-11-23 21:08:25 +00001067 O << '#' << FP->getValueAPF().convertToFloat();
Chris Lattner3f53c832010-04-04 18:52:31 +00001068 if (isVerbose()) {
Chris Lattner35c33bd2010-04-04 04:47:45 +00001069 O << "\t\t" << MAI->getCommentString() << ' ';
Evan Cheng39382422009-10-28 01:44:26 +00001070 WriteAsOperand(O, FP, /*PrintType=*/false);
1071 }
1072}
1073
Chris Lattner35c33bd2010-04-04 04:47:45 +00001074void ARMAsmPrinter::printVFPf64ImmOperand(const MachineInstr *MI, int OpNum,
1075 raw_ostream &O) {
Evan Cheng39382422009-10-28 01:44:26 +00001076 const ConstantFP *FP = MI->getOperand(OpNum).getFPImm();
Jim Grosbach77b02be2009-11-23 21:08:25 +00001077 O << '#' << FP->getValueAPF().convertToDouble();
Chris Lattner3f53c832010-04-04 18:52:31 +00001078 if (isVerbose()) {
Chris Lattner35c33bd2010-04-04 04:47:45 +00001079 O << "\t\t" << MAI->getCommentString() << ' ';
Evan Cheng39382422009-10-28 01:44:26 +00001080 WriteAsOperand(O, FP, /*PrintType=*/false);
1081 }
1082}
1083
Bob Wilson1a913ed2010-06-11 21:34:50 +00001084void ARMAsmPrinter::printNEONModImmOperand(const MachineInstr *MI, int OpNum,
1085 raw_ostream &O) {
Bob Wilson6dce00c2010-07-13 04:44:34 +00001086 unsigned EncodedImm = MI->getOperand(OpNum).getImm();
1087 unsigned EltBits;
1088 uint64_t Val = ARM_AM::decodeNEONModImm(EncodedImm, EltBits);
Bob Wilson1a913ed2010-06-11 21:34:50 +00001089 O << "#0x" << utohexstr(Val);
1090}
1091
Evan Cheng055b0312009-06-29 07:51:04 +00001092bool ARMAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
Chris Lattnerc75c0282010-04-04 05:29:35 +00001093 unsigned AsmVariant, const char *ExtraCode,
1094 raw_ostream &O) {
Evan Chenga8e29892007-01-19 07:51:42 +00001095 // Does this asm operand have a single letter operand modifier?
1096 if (ExtraCode && ExtraCode[0]) {
1097 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov8e9ece72009-08-08 23:10:41 +00001098
Evan Chenga8e29892007-01-19 07:51:42 +00001099 switch (ExtraCode[0]) {
1100 default: return true; // Unknown modifier.
Bob Wilson9b4b00a2009-07-09 23:54:51 +00001101 case 'a': // Print as a memory address.
1102 if (MI->getOperand(OpNum).isReg()) {
Chris Lattner762ccea2009-09-13 20:31:40 +00001103 O << "[" << getRegisterName(MI->getOperand(OpNum).getReg()) << "]";
Bob Wilson9b4b00a2009-07-09 23:54:51 +00001104 return false;
1105 }
1106 // Fallthrough
1107 case 'c': // Don't print "#" before an immediate operand.
Bob Wilson4f38b382009-08-21 21:58:55 +00001108 if (!MI->getOperand(OpNum).isImm())
1109 return true;
Chris Lattner35c33bd2010-04-04 04:47:45 +00001110 printNoHashImmediate(MI, OpNum, O);
Bob Wilson8f343462009-04-06 21:46:51 +00001111 return false;
Evan Chenge21e3962007-04-04 00:13:29 +00001112 case 'P': // Print a VFP double precision register.
Evan Chengd831cda2009-12-08 23:06:22 +00001113 case 'q': // Print a NEON quad precision register.
Chris Lattner35c33bd2010-04-04 04:47:45 +00001114 printOperand(MI, OpNum, O);
Evan Cheng23a95702007-03-08 22:42:46 +00001115 return false;
Evan Chenga8e29892007-01-19 07:51:42 +00001116 case 'Q':
Bob Wilsond984eb62010-05-27 20:23:42 +00001117 case 'R':
Bob Wilsond984eb62010-05-27 20:23:42 +00001118 case 'H':
Evan Cheng12616722010-05-27 23:45:31 +00001119 report_fatal_error("llvm does not support 'Q', 'R', and 'H' modifiers!");
Bob Wilsond984eb62010-05-27 20:23:42 +00001120 return true;
Evan Cheng84f60b72010-05-27 22:08:38 +00001121 }
Evan Chenga8e29892007-01-19 07:51:42 +00001122 }
Jim Grosbache9952212009-09-04 01:38:51 +00001123
Chris Lattner35c33bd2010-04-04 04:47:45 +00001124 printOperand(MI, OpNum, O);
Evan Chenga8e29892007-01-19 07:51:42 +00001125 return false;
1126}
1127
Bob Wilson224c2442009-05-19 05:53:42 +00001128bool ARMAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Evan Cheng055b0312009-06-29 07:51:04 +00001129 unsigned OpNum, unsigned AsmVariant,
Chris Lattnerc75c0282010-04-04 05:29:35 +00001130 const char *ExtraCode,
1131 raw_ostream &O) {
Bob Wilson224c2442009-05-19 05:53:42 +00001132 if (ExtraCode && ExtraCode[0])
1133 return true; // Unknown modifier.
Bob Wilson765cc0b2009-10-13 20:50:28 +00001134
1135 const MachineOperand &MO = MI->getOperand(OpNum);
1136 assert(MO.isReg() && "unexpected inline asm memory operand");
1137 O << "[" << getRegisterName(MO.getReg()) << "]";
Bob Wilson224c2442009-05-19 05:53:42 +00001138 return false;
1139}
1140
Chris Lattnera786cea2010-01-28 01:10:34 +00001141void ARMAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Chris Lattner97f06932009-10-19 20:20:46 +00001142 if (EnableMCInst) {
1143 printInstructionThroughMCStreamer(MI);
Chris Lattner7ad07c42010-04-04 06:12:20 +00001144 return;
Chris Lattner97f06932009-10-19 20:20:46 +00001145 }
Chris Lattner7ad07c42010-04-04 06:12:20 +00001146
1147 if (MI->getOpcode() == ARM::CONSTPOOL_ENTRY)
1148 EmitAlignment(2);
1149
1150 SmallString<128> Str;
1151 raw_svector_ostream OS(Str);
Dale Johannesen3f282aa2010-04-26 20:07:31 +00001152 if (MI->getOpcode() == ARM::DBG_VALUE) {
1153 unsigned NOps = MI->getNumOperands();
1154 assert(NOps==4);
1155 OS << '\t' << MAI->getCommentString() << "DEBUG_VALUE: ";
1156 // cast away const; DIetc do not take const operands for some reason.
1157 DIVariable V(const_cast<MDNode *>(MI->getOperand(NOps-1).getMetadata()));
1158 OS << V.getName();
1159 OS << " <- ";
1160 // Frame address. Currently handles register +- offset only.
1161 assert(MI->getOperand(0).isReg() && MI->getOperand(1).isImm());
1162 OS << '['; printOperand(MI, 0, OS); OS << '+'; printOperand(MI, 1, OS);
1163 OS << ']';
1164 OS << "+";
1165 printOperand(MI, NOps-2, OS);
1166 OutStreamer.EmitRawText(OS.str());
1167 return;
1168 }
1169
Chris Lattner7ad07c42010-04-04 06:12:20 +00001170 printInstruction(MI, OS);
1171 OutStreamer.EmitRawText(OS.str());
1172
1173 // Make sure the instruction that follows TBB is 2-byte aligned.
1174 // FIXME: Constant island pass should insert an "ALIGN" instruction instead.
1175 if (MI->getOpcode() == ARM::t2TBB)
1176 EmitAlignment(1);
Evan Chenga8e29892007-01-19 07:51:42 +00001177}
1178
Bob Wilson812209a2009-09-30 22:06:26 +00001179void ARMAsmPrinter::EmitStartOfAsmFile(Module &M) {
Bob Wilson0fb34682009-09-30 00:23:42 +00001180 if (Subtarget->isTargetDarwin()) {
1181 Reloc::Model RelocM = TM.getRelocationModel();
1182 if (RelocM == Reloc::PIC_ || RelocM == Reloc::DynamicNoPIC) {
1183 // Declare all the text sections up front (before the DWARF sections
1184 // emitted by AsmPrinter::doInitialization) so the assembler will keep
1185 // them together at the beginning of the object file. This helps
1186 // avoid out-of-range branches that are due a fundamental limitation of
1187 // the way symbol offsets are encoded with the current Darwin ARM
1188 // relocations.
Dan Gohman0d805c32010-04-17 16:44:48 +00001189 const TargetLoweringObjectFileMachO &TLOFMacho =
1190 static_cast<const TargetLoweringObjectFileMachO &>(
1191 getObjFileLowering());
Bob Wilson29e06692009-09-30 22:25:37 +00001192 OutStreamer.SwitchSection(TLOFMacho.getTextSection());
1193 OutStreamer.SwitchSection(TLOFMacho.getTextCoalSection());
1194 OutStreamer.SwitchSection(TLOFMacho.getConstTextCoalSection());
1195 if (RelocM == Reloc::DynamicNoPIC) {
1196 const MCSection *sect =
Chris Lattner22772212010-04-08 20:40:11 +00001197 OutContext.getMachOSection("__TEXT", "__symbol_stub4",
1198 MCSectionMachO::S_SYMBOL_STUBS,
1199 12, SectionKind::getText());
Bob Wilson29e06692009-09-30 22:25:37 +00001200 OutStreamer.SwitchSection(sect);
1201 } else {
1202 const MCSection *sect =
Chris Lattner22772212010-04-08 20:40:11 +00001203 OutContext.getMachOSection("__TEXT", "__picsymbolstub4",
1204 MCSectionMachO::S_SYMBOL_STUBS,
1205 16, SectionKind::getText());
Bob Wilson29e06692009-09-30 22:25:37 +00001206 OutStreamer.SwitchSection(sect);
1207 }
Bob Wilson63db5942010-07-30 19:55:47 +00001208 const MCSection *StaticInitSect =
1209 OutContext.getMachOSection("__TEXT", "__StaticInit",
1210 MCSectionMachO::S_REGULAR |
1211 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
1212 SectionKind::getText());
1213 OutStreamer.SwitchSection(StaticInitSect);
Bob Wilson0fb34682009-09-30 00:23:42 +00001214 }
1215 }
1216
Jim Grosbache5165492009-11-09 00:11:35 +00001217 // Use unified assembler syntax.
Chris Lattner9d7efd32010-04-04 07:05:53 +00001218 OutStreamer.EmitRawText(StringRef("\t.syntax unified"));
Anton Korobeynikovd61eca52009-06-17 23:43:18 +00001219
Anton Korobeynikov88ce6672009-05-23 19:51:20 +00001220 // Emit ARM Build Attributes
1221 if (Subtarget->isTargetELF()) {
1222 // CPU Type
Anton Korobeynikovd260c242009-06-01 19:03:17 +00001223 std::string CPUString = Subtarget->getCPUString();
1224 if (CPUString != "generic")
Chris Lattner9d7efd32010-04-04 07:05:53 +00001225 OutStreamer.EmitRawText("\t.cpu " + Twine(CPUString));
Anton Korobeynikov88ce6672009-05-23 19:51:20 +00001226
1227 // FIXME: Emit FPU type
1228 if (Subtarget->hasVFP2())
Chris Lattner9d7efd32010-04-04 07:05:53 +00001229 OutStreamer.EmitRawText("\t.eabi_attribute " +
1230 Twine(ARMBuildAttrs::VFP_arch) + ", 2");
Anton Korobeynikov88ce6672009-05-23 19:51:20 +00001231
1232 // Signal various FP modes.
Chris Lattner9d7efd32010-04-04 07:05:53 +00001233 if (!UnsafeFPMath) {
1234 OutStreamer.EmitRawText("\t.eabi_attribute " +
1235 Twine(ARMBuildAttrs::ABI_FP_denormal) + ", 1");
1236 OutStreamer.EmitRawText("\t.eabi_attribute " +
1237 Twine(ARMBuildAttrs::ABI_FP_exceptions) + ", 1");
1238 }
1239
Evan Cheng60108e92010-07-15 22:07:12 +00001240 if (NoInfsFPMath && NoNaNsFPMath)
Chris Lattner9d7efd32010-04-04 07:05:53 +00001241 OutStreamer.EmitRawText("\t.eabi_attribute " +
1242 Twine(ARMBuildAttrs::ABI_FP_number_model)+ ", 1");
Anton Korobeynikov88ce6672009-05-23 19:51:20 +00001243 else
Chris Lattner9d7efd32010-04-04 07:05:53 +00001244 OutStreamer.EmitRawText("\t.eabi_attribute " +
1245 Twine(ARMBuildAttrs::ABI_FP_number_model)+ ", 3");
Anton Korobeynikov88ce6672009-05-23 19:51:20 +00001246
1247 // 8-bytes alignment stuff.
Chris Lattner9d7efd32010-04-04 07:05:53 +00001248 OutStreamer.EmitRawText("\t.eabi_attribute " +
1249 Twine(ARMBuildAttrs::ABI_align8_needed) + ", 1");
1250 OutStreamer.EmitRawText("\t.eabi_attribute " +
1251 Twine(ARMBuildAttrs::ABI_align8_preserved) + ", 1");
Anton Korobeynikov88ce6672009-05-23 19:51:20 +00001252
Anton Korobeynikov567d14f2009-08-05 19:04:42 +00001253 // Hard float. Use both S and D registers and conform to AAPCS-VFP.
Chris Lattner9d7efd32010-04-04 07:05:53 +00001254 if (Subtarget->isAAPCS_ABI() && FloatABIType == FloatABI::Hard) {
1255 OutStreamer.EmitRawText("\t.eabi_attribute " +
1256 Twine(ARMBuildAttrs::ABI_HardFP_use) + ", 3");
1257 OutStreamer.EmitRawText("\t.eabi_attribute " +
1258 Twine(ARMBuildAttrs::ABI_VFP_args) + ", 1");
1259 }
Anton Korobeynikov88ce6672009-05-23 19:51:20 +00001260 // FIXME: Should we signal R9 usage?
1261 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00001262}
1263
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +00001264
Chris Lattner4a071d62009-10-19 17:59:19 +00001265void ARMAsmPrinter::EmitEndOfAsmFile(Module &M) {
Evan Cheng5be54b02007-01-19 19:25:36 +00001266 if (Subtarget->isTargetDarwin()) {
Chris Lattnerf61159b2009-08-03 22:18:15 +00001267 // All darwin targets use mach-o.
Dan Gohman0d805c32010-04-17 16:44:48 +00001268 const TargetLoweringObjectFileMachO &TLOFMacho =
1269 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattnerb0f294c2009-10-19 18:38:33 +00001270 MachineModuleInfoMachO &MMIMacho =
1271 MMI->getObjFileInfo<MachineModuleInfoMachO>();
Jim Grosbache9952212009-09-04 01:38:51 +00001272
Evan Chenga8e29892007-01-19 07:51:42 +00001273 // Output non-lazy-pointers for external and common global variables.
Chris Lattnerb0f294c2009-10-19 18:38:33 +00001274 MachineModuleInfoMachO::SymbolListTy Stubs = MMIMacho.GetGVStubList();
Bill Wendlingcebae362010-03-10 22:34:10 +00001275
Chris Lattnerb0f294c2009-10-19 18:38:33 +00001276 if (!Stubs.empty()) {
Chris Lattnerff4bc462009-08-10 01:39:42 +00001277 // Switch with ".non_lazy_symbol_pointer" directive.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001278 OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
Chris Lattnerc076a972009-08-10 18:01:34 +00001279 EmitAlignment(2);
Chris Lattnerb0f294c2009-10-19 18:38:33 +00001280 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Bill Wendlingbecd83e2010-03-09 00:40:17 +00001281 // L_foo$stub:
1282 OutStreamer.EmitLabel(Stubs[i].first);
1283 // .indirect_symbol _foo
Bill Wendling52a50e52010-03-11 01:18:13 +00001284 MachineModuleInfoImpl::StubValueTy &MCSym = Stubs[i].second;
1285 OutStreamer.EmitSymbolAttribute(MCSym.getPointer(),MCSA_IndirectSymbol);
Bill Wendlingcf6f28d2010-03-09 00:43:34 +00001286
Bill Wendling52a50e52010-03-11 01:18:13 +00001287 if (MCSym.getInt())
Bill Wendlingcf6f28d2010-03-09 00:43:34 +00001288 // External to current translation unit.
1289 OutStreamer.EmitIntValue(0, 4/*size*/, 0/*addrspace*/);
1290 else
1291 // Internal to current translation unit.
Bill Wendling5e1b55d2010-03-31 18:47:10 +00001292 //
1293 // When we place the LSDA into the TEXT section, the type info pointers
1294 // need to be indirect and pc-rel. We accomplish this by using NLPs.
1295 // However, sometimes the types are local to the file. So we need to
1296 // fill in the value for the NLP in those cases.
Bill Wendling52a50e52010-03-11 01:18:13 +00001297 OutStreamer.EmitValue(MCSymbolRefExpr::Create(MCSym.getPointer(),
1298 OutContext),
Bill Wendlingcf6f28d2010-03-09 00:43:34 +00001299 4/*size*/, 0/*addrspace*/);
Evan Chengae94e592008-12-05 01:06:39 +00001300 }
Bill Wendlingbecd83e2010-03-09 00:40:17 +00001301
1302 Stubs.clear();
1303 OutStreamer.AddBlankLine();
Evan Chenga8e29892007-01-19 07:51:42 +00001304 }
1305
Chris Lattnere4d9ea82009-10-19 18:44:38 +00001306 Stubs = MMIMacho.GetHiddenGVStubList();
1307 if (!Stubs.empty()) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001308 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
Chris Lattnerf3231de2009-08-10 18:02:16 +00001309 EmitAlignment(2);
Bill Wendlingbecd83e2010-03-09 00:40:17 +00001310 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
1311 // L_foo$stub:
1312 OutStreamer.EmitLabel(Stubs[i].first);
1313 // .long _foo
Bill Wendlingcebae362010-03-10 22:34:10 +00001314 OutStreamer.EmitValue(MCSymbolRefExpr::
1315 Create(Stubs[i].second.getPointer(),
1316 OutContext),
Bill Wendlingbecd83e2010-03-09 00:40:17 +00001317 4/*size*/, 0/*addrspace*/);
1318 }
Bill Wendlingcf6f28d2010-03-09 00:43:34 +00001319
1320 Stubs.clear();
1321 OutStreamer.AddBlankLine();
Evan Chengae94e592008-12-05 01:06:39 +00001322 }
1323
Evan Chenga8e29892007-01-19 07:51:42 +00001324 // Funny Darwin hack: This flag tells the linker that no global symbols
1325 // contain code that falls through to other global symbols (e.g. the obvious
1326 // implementation of multiple entry points). If this doesn't occur, the
1327 // linker can safely perform dead code stripping. Since LLVM never
1328 // generates code that does this, it is always safe to set.
Chris Lattnera5ad93a2010-01-23 06:39:22 +00001329 OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Rafael Espindolab01c4bb2006-07-27 11:38:51 +00001330 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00001331}
Anton Korobeynikov0bd89712008-08-17 13:55:10 +00001332
Chris Lattner97f06932009-10-19 20:20:46 +00001333//===----------------------------------------------------------------------===//
1334
1335void ARMAsmPrinter::printInstructionThroughMCStreamer(const MachineInstr *MI) {
Chris Lattner96bc2172009-10-20 00:52:47 +00001336 ARMMCInstLower MCInstLowering(OutContext, *Mang, *this);
Chris Lattner97f06932009-10-19 20:20:46 +00001337 switch (MI->getOpcode()) {
Chris Lattnerc6b8a992009-10-20 05:58:02 +00001338 case ARM::t2MOVi32imm:
1339 assert(0 && "Should be lowered by thumb2it pass");
Chris Lattner4d152222009-10-19 22:23:04 +00001340 default: break;
Chris Lattner4d152222009-10-19 22:23:04 +00001341 case ARM::PICADD: { // FIXME: Remove asm string from td file.
1342 // This is a pseudo op for a label + instruction sequence, which looks like:
1343 // LPC0:
1344 // add r0, pc, r0
1345 // This adds the address of LPC0 to r0.
1346
1347 // Emit the label.
1348 // FIXME: MOVE TO SHARED PLACE.
Chris Lattnera70e6442009-10-19 22:33:05 +00001349 unsigned Id = (unsigned)MI->getOperand(2).getImm();
Chris Lattner7c5b0212009-10-19 22:49:00 +00001350 const char *Prefix = MAI->getPrivateGlobalPrefix();
Chris Lattner9b97a732010-03-30 18:10:53 +00001351 MCSymbol *Label =OutContext.GetOrCreateSymbol(Twine(Prefix)
Evan Chenge7e0d622009-11-06 22:24:13 +00001352 + "PC" + Twine(getFunctionNumber()) + "_" + Twine(Id));
Chris Lattner7c5b0212009-10-19 22:49:00 +00001353 OutStreamer.EmitLabel(Label);
Chris Lattner4d152222009-10-19 22:23:04 +00001354
1355
1356 // Form and emit tha dd.
1357 MCInst AddInst;
1358 AddInst.setOpcode(ARM::ADDrr);
1359 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1360 AddInst.addOperand(MCOperand::CreateReg(ARM::PC));
1361 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
Chris Lattner850d2e22010-02-03 01:16:28 +00001362 OutStreamer.EmitInstruction(AddInst);
Chris Lattner4d152222009-10-19 22:23:04 +00001363 return;
1364 }
Chris Lattnera70e6442009-10-19 22:33:05 +00001365 case ARM::CONSTPOOL_ENTRY: { // FIXME: Remove asm string from td file.
1366 /// CONSTPOOL_ENTRY - This instruction represents a floating constant pool
1367 /// in the function. The first operand is the ID# for this instruction, the
1368 /// second is the index into the MachineConstantPool that this is, the third
1369 /// is the size in bytes of this constant pool entry.
1370 unsigned LabelId = (unsigned)MI->getOperand(0).getImm();
1371 unsigned CPIdx = (unsigned)MI->getOperand(1).getIndex();
1372
1373 EmitAlignment(2);
Chris Lattner1b46f432010-01-23 07:00:21 +00001374 OutStreamer.EmitLabel(GetCPISymbol(LabelId));
Chris Lattnera70e6442009-10-19 22:33:05 +00001375
1376 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPIdx];
1377 if (MCPE.isMachineConstantPoolEntry())
1378 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
1379 else
1380 EmitGlobalConstant(MCPE.Val.ConstVal);
1381
1382 return;
1383 }
Chris Lattner017d9472009-10-20 00:40:56 +00001384 case ARM::MOVi2pieces: { // FIXME: Remove asmstring from td file.
1385 // This is a hack that lowers as a two instruction sequence.
1386 unsigned DstReg = MI->getOperand(0).getReg();
1387 unsigned ImmVal = (unsigned)MI->getOperand(1).getImm();
1388
1389 unsigned SOImmValV1 = ARM_AM::getSOImmTwoPartFirst(ImmVal);
1390 unsigned SOImmValV2 = ARM_AM::getSOImmTwoPartSecond(ImmVal);
1391
1392 {
1393 MCInst TmpInst;
1394 TmpInst.setOpcode(ARM::MOVi);
1395 TmpInst.addOperand(MCOperand::CreateReg(DstReg));
1396 TmpInst.addOperand(MCOperand::CreateImm(SOImmValV1));
1397
1398 // Predicate.
1399 TmpInst.addOperand(MCOperand::CreateImm(MI->getOperand(2).getImm()));
1400 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(3).getReg()));
Chris Lattner233917c2009-10-20 00:46:11 +00001401
1402 TmpInst.addOperand(MCOperand::CreateReg(0)); // cc_out
Chris Lattner850d2e22010-02-03 01:16:28 +00001403 OutStreamer.EmitInstruction(TmpInst);
Chris Lattner017d9472009-10-20 00:40:56 +00001404 }
1405
1406 {
1407 MCInst TmpInst;
1408 TmpInst.setOpcode(ARM::ORRri);
1409 TmpInst.addOperand(MCOperand::CreateReg(DstReg)); // dstreg
1410 TmpInst.addOperand(MCOperand::CreateReg(DstReg)); // inreg
1411 TmpInst.addOperand(MCOperand::CreateImm(SOImmValV2)); // so_imm
1412 // Predicate.
1413 TmpInst.addOperand(MCOperand::CreateImm(MI->getOperand(2).getImm()));
1414 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(3).getReg()));
1415
1416 TmpInst.addOperand(MCOperand::CreateReg(0)); // cc_out
Chris Lattner850d2e22010-02-03 01:16:28 +00001417 OutStreamer.EmitInstruction(TmpInst);
Chris Lattner017d9472009-10-20 00:40:56 +00001418 }
1419 return;
1420 }
Chris Lattner161dcbf2009-10-20 01:11:37 +00001421 case ARM::MOVi32imm: { // FIXME: Remove asmstring from td file.
1422 // This is a hack that lowers as a two instruction sequence.
1423 unsigned DstReg = MI->getOperand(0).getReg();
Rafael Espindola18c10212010-05-12 05:16:34 +00001424 const MachineOperand &MO = MI->getOperand(1);
1425 MCOperand V1, V2;
1426 if (MO.isImm()) {
1427 unsigned ImmVal = (unsigned)MI->getOperand(1).getImm();
1428 V1 = MCOperand::CreateImm(ImmVal & 65535);
1429 V2 = MCOperand::CreateImm(ImmVal >> 16);
1430 } else if (MO.isGlobal()) {
1431 MCSymbol *Symbol = MCInstLowering.GetGlobalAddressSymbol(MO);
1432 const MCSymbolRefExpr *SymRef1 =
Duncan Sands34727662010-07-12 08:16:59 +00001433 MCSymbolRefExpr::Create(Symbol,
1434 MCSymbolRefExpr::VK_ARM_LO16, OutContext);
Rafael Espindola18c10212010-05-12 05:16:34 +00001435 const MCSymbolRefExpr *SymRef2 =
Duncan Sands34727662010-07-12 08:16:59 +00001436 MCSymbolRefExpr::Create(Symbol,
1437 MCSymbolRefExpr::VK_ARM_HI16, OutContext);
Rafael Espindola18c10212010-05-12 05:16:34 +00001438 V1 = MCOperand::CreateExpr(SymRef1);
1439 V2 = MCOperand::CreateExpr(SymRef2);
1440 } else {
1441 MI->dump();
1442 llvm_unreachable("cannot handle this operand");
1443 }
1444
Chris Lattner161dcbf2009-10-20 01:11:37 +00001445 {
1446 MCInst TmpInst;
1447 TmpInst.setOpcode(ARM::MOVi16);
1448 TmpInst.addOperand(MCOperand::CreateReg(DstReg)); // dstreg
Rafael Espindola18c10212010-05-12 05:16:34 +00001449 TmpInst.addOperand(V1); // lower16(imm)
Chris Lattner017d9472009-10-20 00:40:56 +00001450
Chris Lattner161dcbf2009-10-20 01:11:37 +00001451 // Predicate.
1452 TmpInst.addOperand(MCOperand::CreateImm(MI->getOperand(2).getImm()));
1453 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(3).getReg()));
1454
Chris Lattner850d2e22010-02-03 01:16:28 +00001455 OutStreamer.EmitInstruction(TmpInst);
Chris Lattner161dcbf2009-10-20 01:11:37 +00001456 }
1457
1458 {
1459 MCInst TmpInst;
1460 TmpInst.setOpcode(ARM::MOVTi16);
1461 TmpInst.addOperand(MCOperand::CreateReg(DstReg)); // dstreg
1462 TmpInst.addOperand(MCOperand::CreateReg(DstReg)); // srcreg
Rafael Espindola18c10212010-05-12 05:16:34 +00001463 TmpInst.addOperand(V2); // upper16(imm)
Chris Lattner161dcbf2009-10-20 01:11:37 +00001464
1465 // Predicate.
1466 TmpInst.addOperand(MCOperand::CreateImm(MI->getOperand(2).getImm()));
1467 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(3).getReg()));
1468
Chris Lattner850d2e22010-02-03 01:16:28 +00001469 OutStreamer.EmitInstruction(TmpInst);
Chris Lattner161dcbf2009-10-20 01:11:37 +00001470 }
1471
1472 return;
1473 }
Chris Lattner97f06932009-10-19 20:20:46 +00001474 }
1475
1476 MCInst TmpInst;
1477 MCInstLowering.Lower(MI, TmpInst);
Chris Lattner850d2e22010-02-03 01:16:28 +00001478 OutStreamer.EmitInstruction(TmpInst);
Chris Lattner97f06932009-10-19 20:20:46 +00001479}
Daniel Dunbar2685a292009-10-20 05:15:36 +00001480
1481//===----------------------------------------------------------------------===//
1482// Target Registry Stuff
1483//===----------------------------------------------------------------------===//
1484
1485static MCInstPrinter *createARMMCInstPrinter(const Target &T,
1486 unsigned SyntaxVariant,
Chris Lattnerd3740872010-04-04 05:04:31 +00001487 const MCAsmInfo &MAI) {
Daniel Dunbar2685a292009-10-20 05:15:36 +00001488 if (SyntaxVariant == 0)
Chris Lattnerd3740872010-04-04 05:04:31 +00001489 return new ARMInstPrinter(MAI, false);
Daniel Dunbar2685a292009-10-20 05:15:36 +00001490 return 0;
1491}
1492
1493// Force static initialization.
1494extern "C" void LLVMInitializeARMAsmPrinter() {
1495 RegisterAsmPrinter<ARMAsmPrinter> X(TheARMTarget);
1496 RegisterAsmPrinter<ARMAsmPrinter> Y(TheThumbTarget);
1497
1498 TargetRegistry::RegisterMCInstPrinter(TheARMTarget, createARMMCInstPrinter);
1499 TargetRegistry::RegisterMCInstPrinter(TheThumbTarget, createARMMCInstPrinter);
1500}
1501