blob: e9e2803ad5795fa3329c4560d9b026f2ef2fe1fb [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"
Jim Grosbachbaf120f2010-12-01 03:45:07 +000016#include "ARMAsmPrinter.h"
Craig Topperc1f6f422012-03-17 07:33:42 +000017#include "ARM.h"
Evan Chengb72d2a92011-01-11 21:46:47 +000018#include "ARMBuildAttrs.h"
Evan Chengb72d2a92011-01-11 21:46:47 +000019#include "ARMConstantPoolValue.h"
Chris Lattner97f06932009-10-19 20:20:46 +000020#include "ARMMachineFunctionInfo.h"
Chris Lattner97f06932009-10-19 20:20:46 +000021#include "ARMTargetMachine.h"
Jason W Kim17b443d2010-10-11 23:01:44 +000022#include "ARMTargetObjectFile.h"
Evan Chengb72d2a92011-01-11 21:46:47 +000023#include "InstPrinter/ARMInstPrinter.h"
Evan Chengee04a6d2011-07-20 23:34:39 +000024#include "MCTargetDesc/ARMAddressingModes.h"
25#include "MCTargetDesc/ARMMCExpr.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000026#include "llvm/Constants.h"
Bill Wendling0bcbd1d2012-06-28 00:05:13 +000027#include "llvm/DebugInfo.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000028#include "llvm/Module.h"
Benjamin Kramere55b15f2009-12-28 12:27:56 +000029#include "llvm/Type.h"
Dan Gohmancf20ac42009-08-13 01:36:44 +000030#include "llvm/Assembly/Writer.h"
Chris Lattnerb0f294c2009-10-19 18:38:33 +000031#include "llvm/CodeGen/MachineModuleInfoImpls.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000032#include "llvm/CodeGen/MachineFunctionPass.h"
Evan Chenga8e29892007-01-19 07:51:42 +000033#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattnerb0f294c2009-10-19 18:38:33 +000034#include "llvm/MC/MCAsmInfo.h"
Rafael Espindolacecbc3d2010-10-25 17:50:35 +000035#include "llvm/MC/MCAssembler.h"
Chris Lattnerb0f294c2009-10-19 18:38:33 +000036#include "llvm/MC/MCContext.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"
Rafael Espindolacecbc3d2010-10-25 17:50:35 +000039#include "llvm/MC/MCObjectStreamer.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000040#include "llvm/MC/MCStreamer.h"
Chris Lattner325d3dc2009-09-13 17:14:04 +000041#include "llvm/MC/MCSymbol.h"
Chris Lattnerd62f1b42010-03-12 21:19:23 +000042#include "llvm/Target/Mangler.h"
Rafael Espindolab01c4bb2006-07-27 11:38:51 +000043#include "llvm/Target/TargetData.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000044#include "llvm/Target/TargetMachine.h"
Jim Grosbachc40d9f92009-09-01 18:49:12 +000045#include "llvm/ADT/SmallString.h"
Chris Lattner97f06932009-10-19 20:20:46 +000046#include "llvm/Support/CommandLine.h"
Devang Patel59135f42010-08-04 22:39:39 +000047#include "llvm/Support/Debug.h"
Torok Edwin30464702009-07-08 20:55:50 +000048#include "llvm/Support/ErrorHandling.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000049#include "llvm/Support/TargetRegistry.h"
Chris Lattnerb23569a2010-04-04 08:18:47 +000050#include "llvm/Support/raw_ostream.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000051#include <cctype>
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000052using namespace llvm;
53
Chris Lattner95b2c7d2006-12-19 22:59:26 +000054namespace {
Rafael Espindolacecbc3d2010-10-25 17:50:35 +000055
56 // Per section and per symbol attributes are not supported.
57 // To implement them we would need the ability to delay this emission
58 // until the assembly file is fully parsed/generated as only then do we
59 // know the symbol and section numbers.
60 class AttributeEmitter {
61 public:
62 virtual void MaybeSwitchVendor(StringRef Vendor) = 0;
63 virtual void EmitAttribute(unsigned Attribute, unsigned Value) = 0;
Jason W Kimf009a962011-02-07 00:49:53 +000064 virtual void EmitTextAttribute(unsigned Attribute, StringRef String) = 0;
Rafael Espindolacecbc3d2010-10-25 17:50:35 +000065 virtual void Finish() = 0;
Rafael Espindola4921e232010-10-25 18:38:32 +000066 virtual ~AttributeEmitter() {}
Rafael Espindolacecbc3d2010-10-25 17:50:35 +000067 };
68
69 class AsmAttributeEmitter : public AttributeEmitter {
70 MCStreamer &Streamer;
71
72 public:
73 AsmAttributeEmitter(MCStreamer &Streamer_) : Streamer(Streamer_) {}
74 void MaybeSwitchVendor(StringRef Vendor) { }
75
76 void EmitAttribute(unsigned Attribute, unsigned Value) {
77 Streamer.EmitRawText("\t.eabi_attribute " +
78 Twine(Attribute) + ", " + Twine(Value));
79 }
80
Jason W Kimf009a962011-02-07 00:49:53 +000081 void EmitTextAttribute(unsigned Attribute, StringRef String) {
82 switch (Attribute) {
Craig Topperbc219812012-02-07 02:50:20 +000083 default: llvm_unreachable("Unsupported Text attribute in ASM Mode");
Jason W Kimf009a962011-02-07 00:49:53 +000084 case ARMBuildAttrs::CPU_name:
Benjamin Kramer59085362011-11-06 20:37:06 +000085 Streamer.EmitRawText(StringRef("\t.cpu ") + String.lower());
Jason W Kimf009a962011-02-07 00:49:53 +000086 break;
Renato Golin728ff0d2011-02-28 22:04:27 +000087 /* GAS requires .fpu to be emitted regardless of EABI attribute */
88 case ARMBuildAttrs::Advanced_SIMD_arch:
89 case ARMBuildAttrs::VFP_arch:
Benjamin Kramer59085362011-11-06 20:37:06 +000090 Streamer.EmitRawText(StringRef("\t.fpu ") + String.lower());
Jim Grosbach8e0c7692011-09-02 18:46:15 +000091 break;
Jason W Kimf009a962011-02-07 00:49:53 +000092 }
93 }
Rafael Espindolacecbc3d2010-10-25 17:50:35 +000094 void Finish() { }
95 };
96
97 class ObjectAttributeEmitter : public AttributeEmitter {
Renato Golin719927a2011-08-09 09:50:10 +000098 // This structure holds all attributes, accounting for
99 // their string/numeric value, so we can later emmit them
100 // in declaration order, keeping all in the same vector
101 struct AttributeItemType {
102 enum {
103 HiddenAttribute = 0,
104 NumericAttribute,
105 TextAttribute
106 } Type;
107 unsigned Tag;
108 unsigned IntValue;
109 StringRef StringValue;
110 } AttributeItem;
111
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000112 MCObjectStreamer &Streamer;
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000113 StringRef CurrentVendor;
Renato Golin719927a2011-08-09 09:50:10 +0000114 SmallVector<AttributeItemType, 64> Contents;
115
116 // Account for the ULEB/String size of each item,
117 // not just the number of items
118 size_t ContentsSize;
119 // FIXME: this should be in a more generic place, but
120 // getULEBSize() is in MCAsmInfo and will be moved to MCDwarf
121 size_t getULEBSize(int Value) {
122 size_t Size = 0;
123 do {
124 Value >>= 7;
125 Size += sizeof(int8_t); // Is this really necessary?
126 } while (Value);
127 return Size;
128 }
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000129
130 public:
131 ObjectAttributeEmitter(MCObjectStreamer &Streamer_) :
Renato Golin719927a2011-08-09 09:50:10 +0000132 Streamer(Streamer_), CurrentVendor(""), ContentsSize(0) { }
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000133
134 void MaybeSwitchVendor(StringRef Vendor) {
135 assert(!Vendor.empty() && "Vendor cannot be empty.");
136
137 if (CurrentVendor.empty())
138 CurrentVendor = Vendor;
139 else if (CurrentVendor == Vendor)
140 return;
141 else
142 Finish();
143
144 CurrentVendor = Vendor;
145
Rafael Espindola33363842010-10-25 22:26:55 +0000146 assert(Contents.size() == 0);
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000147 }
148
149 void EmitAttribute(unsigned Attribute, unsigned Value) {
Renato Golin719927a2011-08-09 09:50:10 +0000150 AttributeItemType attr = {
151 AttributeItemType::NumericAttribute,
152 Attribute,
153 Value,
154 StringRef("")
155 };
156 ContentsSize += getULEBSize(Attribute);
157 ContentsSize += getULEBSize(Value);
158 Contents.push_back(attr);
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000159 }
160
Jason W Kimf009a962011-02-07 00:49:53 +0000161 void EmitTextAttribute(unsigned Attribute, StringRef String) {
Renato Golin719927a2011-08-09 09:50:10 +0000162 AttributeItemType attr = {
163 AttributeItemType::TextAttribute,
164 Attribute,
165 0,
166 String
167 };
168 ContentsSize += getULEBSize(Attribute);
169 // String + \0
170 ContentsSize += String.size()+1;
171
172 Contents.push_back(attr);
Jason W Kimf009a962011-02-07 00:49:53 +0000173 }
174
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000175 void Finish() {
Rafael Espindola33363842010-10-25 22:26:55 +0000176 // Vendor size + Vendor name + '\0'
177 const size_t VendorHeaderSize = 4 + CurrentVendor.size() + 1;
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000178
Rafael Espindola33363842010-10-25 22:26:55 +0000179 // Tag + Tag Size
180 const size_t TagHeaderSize = 1 + 4;
181
182 Streamer.EmitIntValue(VendorHeaderSize + TagHeaderSize + ContentsSize, 4);
183 Streamer.EmitBytes(CurrentVendor, 0);
184 Streamer.EmitIntValue(0, 1); // '\0'
185
186 Streamer.EmitIntValue(ARMBuildAttrs::File, 1);
187 Streamer.EmitIntValue(TagHeaderSize + ContentsSize, 4);
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000188
Renato Golin719927a2011-08-09 09:50:10 +0000189 // Size should have been accounted for already, now
190 // emit each field as its type (ULEB or String)
191 for (unsigned int i=0; i<Contents.size(); ++i) {
192 AttributeItemType item = Contents[i];
193 Streamer.EmitULEB128IntValue(item.Tag, 0);
194 switch (item.Type) {
Craig Topperbc219812012-02-07 02:50:20 +0000195 default: llvm_unreachable("Invalid attribute type");
Renato Golin719927a2011-08-09 09:50:10 +0000196 case AttributeItemType::NumericAttribute:
197 Streamer.EmitULEB128IntValue(item.IntValue, 0);
198 break;
199 case AttributeItemType::TextAttribute:
Benjamin Kramer59085362011-11-06 20:37:06 +0000200 Streamer.EmitBytes(item.StringValue.upper(), 0);
Renato Golin719927a2011-08-09 09:50:10 +0000201 Streamer.EmitIntValue(0, 1); // '\0'
202 break;
Renato Golin719927a2011-08-09 09:50:10 +0000203 }
204 }
Rafael Espindola33363842010-10-25 22:26:55 +0000205
206 Contents.clear();
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000207 }
208 };
209
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000210} // end of anonymous namespace
211
Jim Grosbachbaf120f2010-12-01 03:45:07 +0000212MachineLocation ARMAsmPrinter::
213getDebugValueLocation(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
Devang Patel27f5acb2011-04-21 22:48:26 +0000225/// EmitDwarfRegOp - Emit dwarf register operation.
Devang Patel0be77df2011-04-27 20:29:27 +0000226void ARMAsmPrinter::EmitDwarfRegOp(const MachineLocation &MLoc) const {
Devang Patel27f5acb2011-04-21 22:48:26 +0000227 const TargetRegisterInfo *RI = TM.getRegisterInfo();
228 if (RI->getDwarfRegNum(MLoc.getReg(), false) != -1)
Devang Patel0be77df2011-04-27 20:29:27 +0000229 AsmPrinter::EmitDwarfRegOp(MLoc);
Devang Patel27f5acb2011-04-21 22:48:26 +0000230 else {
231 unsigned Reg = MLoc.getReg();
232 if (Reg >= ARM::S0 && Reg <= ARM::S31) {
Devang Patel0a6ea832011-04-22 16:44:29 +0000233 assert(ARM::S0 + 31 == ARM::S31 && "Unexpected ARM S register numbering");
Devang Patel27f5acb2011-04-21 22:48:26 +0000234 // S registers are described as bit-pieces of a register
235 // S[2x] = DW_OP_regx(256 + (x>>1)) DW_OP_bit_piece(32, 0)
236 // S[2x+1] = DW_OP_regx(256 + (x>>1)) DW_OP_bit_piece(32, 32)
Jim Grosbach8e0c7692011-09-02 18:46:15 +0000237
Devang Patel27f5acb2011-04-21 22:48:26 +0000238 unsigned SReg = Reg - ARM::S0;
239 bool odd = SReg & 0x1;
240 unsigned Rx = 256 + (SReg >> 1);
Devang Patel27f5acb2011-04-21 22:48:26 +0000241
242 OutStreamer.AddComment("DW_OP_regx for S register");
243 EmitInt8(dwarf::DW_OP_regx);
244
245 OutStreamer.AddComment(Twine(SReg));
246 EmitULEB128(Rx);
247
248 if (odd) {
249 OutStreamer.AddComment("DW_OP_bit_piece 32 32");
250 EmitInt8(dwarf::DW_OP_bit_piece);
251 EmitULEB128(32);
252 EmitULEB128(32);
253 } else {
254 OutStreamer.AddComment("DW_OP_bit_piece 32 0");
255 EmitInt8(dwarf::DW_OP_bit_piece);
256 EmitULEB128(32);
257 EmitULEB128(0);
258 }
Devang Patel71f3f112011-04-21 23:22:35 +0000259 } else if (Reg >= ARM::Q0 && Reg <= ARM::Q15) {
Devang Patel0a6ea832011-04-22 16:44:29 +0000260 assert(ARM::Q0 + 15 == ARM::Q15 && "Unexpected ARM Q register numbering");
Devang Patel71f3f112011-04-21 23:22:35 +0000261 // Q registers Q0-Q15 are described by composing two D registers together.
Jim Grosbach8e0c7692011-09-02 18:46:15 +0000262 // Qx = DW_OP_regx(256+2x) DW_OP_piece(8) DW_OP_regx(256+2x+1)
263 // DW_OP_piece(8)
Devang Patel71f3f112011-04-21 23:22:35 +0000264
265 unsigned QReg = Reg - ARM::Q0;
266 unsigned D1 = 256 + 2 * QReg;
267 unsigned D2 = D1 + 1;
Jim Grosbach8e0c7692011-09-02 18:46:15 +0000268
Devang Patel71f3f112011-04-21 23:22:35 +0000269 OutStreamer.AddComment("DW_OP_regx for Q register: D1");
270 EmitInt8(dwarf::DW_OP_regx);
271 EmitULEB128(D1);
272 OutStreamer.AddComment("DW_OP_piece 8");
273 EmitInt8(dwarf::DW_OP_piece);
274 EmitULEB128(8);
275
276 OutStreamer.AddComment("DW_OP_regx for Q register: D2");
277 EmitInt8(dwarf::DW_OP_regx);
278 EmitULEB128(D2);
279 OutStreamer.AddComment("DW_OP_piece 8");
280 EmitInt8(dwarf::DW_OP_piece);
281 EmitULEB128(8);
Devang Patel27f5acb2011-04-21 22:48:26 +0000282 }
283 }
284}
285
Jim Grosbach3e965312012-05-18 19:12:01 +0000286void ARMAsmPrinter::EmitFunctionBodyEnd() {
287 // Make sure to terminate any constant pools that were at the end
288 // of the function.
289 if (!InConstantPool)
290 return;
291 InConstantPool = false;
292 OutStreamer.EmitDataRegion(MCDR_DataRegionEnd);
293}
Owen Anderson2fec6c52011-10-04 23:26:17 +0000294
Jim Grosbach3e965312012-05-18 19:12:01 +0000295void ARMAsmPrinter::EmitFunctionEntryLabel() {
Chris Lattner953ebb72010-01-27 23:58:11 +0000296 if (AFI->isThumbFunction()) {
Jim Grosbachce792992010-11-05 22:08:08 +0000297 OutStreamer.EmitAssemblerFlag(MCAF_Code16);
Rafael Espindola64695402011-05-16 16:17:21 +0000298 OutStreamer.EmitThumbFunc(CurrentFnSym);
Chris Lattner953ebb72010-01-27 23:58:11 +0000299 }
Jim Grosbachb0739b72010-09-02 01:02:06 +0000300
Chris Lattner953ebb72010-01-27 23:58:11 +0000301 OutStreamer.EmitLabel(CurrentFnSym);
302}
303
James Molloy34982572012-01-26 09:25:43 +0000304void ARMAsmPrinter::EmitXXStructor(const Constant *CV) {
305 uint64_t Size = TM.getTargetData()->getTypeAllocSize(CV->getType());
306 assert(Size && "C++ constructor pointer had zero size!");
307
Bill Wendling4a1ff2f2012-02-15 09:14:08 +0000308 const GlobalValue *GV = dyn_cast<GlobalValue>(CV->stripPointerCasts());
James Molloy34982572012-01-26 09:25:43 +0000309 assert(GV && "C++ constructor pointer was not a GlobalValue!");
310
311 const MCExpr *E = MCSymbolRefExpr::Create(Mang->getSymbol(GV),
312 (Subtarget->isTargetDarwin()
313 ? MCSymbolRefExpr::VK_None
314 : MCSymbolRefExpr::VK_ARM_TARGET1),
315 OutContext);
316
317 OutStreamer.EmitValue(E, Size);
318}
319
Jim Grosbach2317e402010-09-30 01:57:53 +0000320/// runOnMachineFunction - This uses the EmitInstruction()
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000321/// method to print assembly for each instruction.
322///
323bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Evan Chenga8e29892007-01-19 07:51:42 +0000324 AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng6d63a722008-09-18 07:27:23 +0000325 MCP = MF.getConstantPool();
Rafael Espindola4b442b52006-05-23 02:48:20 +0000326
Chris Lattnerd49fe1b2010-01-28 01:28:58 +0000327 return AsmPrinter::runOnMachineFunction(MF);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000328}
329
Evan Cheng055b0312009-06-29 07:51:04 +0000330void ARMAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
Chris Lattner35c33bd2010-04-04 04:47:45 +0000331 raw_ostream &O, const char *Modifier) {
Evan Cheng055b0312009-06-29 07:51:04 +0000332 const MachineOperand &MO = MI->getOperand(OpNum);
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000333 unsigned TF = MO.getTargetFlags();
334
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000335 switch (MO.getType()) {
Craig Topperbc219812012-02-07 02:50:20 +0000336 default: llvm_unreachable("<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));
Jim Grosbach35636282010-10-06 21:22:32 +0000340 assert(!MO.getSubReg() && "Subregs should be eliminated!");
341 O << ARMInstPrinter::getRegisterName(Reg);
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000342 break;
Bob Wilson5bafff32009-06-22 23:27:02 +0000343 }
Evan Chenga8e29892007-01-19 07:51:42 +0000344 case MachineOperand::MO_Immediate: {
Evan Cheng5adb66a2009-09-28 09:14:39 +0000345 int64_t Imm = MO.getImm();
Anton Korobeynikov632606c2009-10-08 20:43:22 +0000346 O << '#';
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000347 if ((Modifier && strcmp(Modifier, "lo16") == 0) ||
Jason W Kim650b7d72011-01-12 23:21:49 +0000348 (TF == ARMII::MO_LO16))
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000349 O << ":lower16:";
350 else if ((Modifier && strcmp(Modifier, "hi16") == 0) ||
Jason W Kim650b7d72011-01-12 23:21:49 +0000351 (TF == ARMII::MO_HI16))
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000352 O << ":upper16:";
Anton Korobeynikov632606c2009-10-08 20:43:22 +0000353 O << Imm;
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000354 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000355 }
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000356 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner1b2eb0e2010-03-13 21:04:28 +0000357 O << *MO.getMBB()->getSymbol();
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000358 return;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000359 case MachineOperand::MO_GlobalAddress: {
Dan Gohman46510a72010-04-15 01:51:59 +0000360 const GlobalValue *GV = MO.getGlobal();
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:";
Chris Lattnerd62f1b42010-03-12 21:19:23 +0000367 O << *Mang->getSymbol(GV);
Anton Korobeynikov7751ad92008-11-22 16:15:34 +0000368
Chris Lattner0c08d092010-04-03 22:28:33 +0000369 printOffset(MO.getOffset(), O);
Jim Grosbach1d6111c2010-10-06 21:36:43 +0000370 if (TF == ARMII::MO_PLT)
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000371 O << "(PLT)";
Evan Chenga8e29892007-01-19 07:51:42 +0000372 break;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000373 }
Evan Chenga8e29892007-01-19 07:51:42 +0000374 case MachineOperand::MO_ExternalSymbol: {
Chris Lattner10b318b2010-01-17 21:43:43 +0000375 O << *GetExternalSymbolSymbol(MO.getSymbolName());
Jim Grosbach1d6111c2010-10-06 21:36:43 +0000376 if (TF == ARMII::MO_PLT)
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000377 O << "(PLT)";
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000378 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000379 }
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000380 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner1b46f432010-01-23 07:00:21 +0000381 O << *GetCPISymbol(MO.getIndex());
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000382 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000383 case MachineOperand::MO_JumpTableIndex:
Chris Lattner1b46f432010-01-23 07:00:21 +0000384 O << *GetJTISymbol(MO.getIndex());
Evan Chenga8e29892007-01-19 07:51:42 +0000385 break;
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000386 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000387}
388
Evan Cheng055b0312009-06-29 07:51:04 +0000389//===--------------------------------------------------------------------===//
390
Chris Lattner0890cf12010-01-25 19:51:38 +0000391MCSymbol *ARMAsmPrinter::
392GetARMSetPICJumpTableLabel2(unsigned uid, unsigned uid2,
393 const MachineBasicBlock *MBB) const {
394 SmallString<60> Name;
395 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix()
Chris Lattnerbfcb0962010-01-25 19:39:52 +0000396 << getFunctionNumber() << '_' << uid << '_' << uid2
Chris Lattner0890cf12010-01-25 19:51:38 +0000397 << "_set_" << MBB->getNumber();
Chris Lattner9b97a732010-03-30 18:10:53 +0000398 return OutContext.GetOrCreateSymbol(Name.str());
Chris Lattner0890cf12010-01-25 19:51:38 +0000399}
400
401MCSymbol *ARMAsmPrinter::
402GetARMJTIPICJumpTableLabel2(unsigned uid, unsigned uid2) const {
403 SmallString<60> Name;
404 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "JTI"
Chris Lattner281e7762010-01-25 23:28:03 +0000405 << getFunctionNumber() << '_' << uid << '_' << uid2;
Chris Lattner9b97a732010-03-30 18:10:53 +0000406 return OutContext.GetOrCreateSymbol(Name.str());
Chris Lattnerbfcb0962010-01-25 19:39:52 +0000407}
408
Jim Grosbach433a5782010-09-24 20:47:58 +0000409
410MCSymbol *ARMAsmPrinter::GetARMSJLJEHLabel(void) const {
411 SmallString<60> Name;
412 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "SJLJEH"
413 << getFunctionNumber();
414 return OutContext.GetOrCreateSymbol(Name.str());
415}
416
Evan Cheng055b0312009-06-29 07:51:04 +0000417bool ARMAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
Chris Lattnerc75c0282010-04-04 05:29:35 +0000418 unsigned AsmVariant, const char *ExtraCode,
419 raw_ostream &O) {
Evan Chenga8e29892007-01-19 07:51:42 +0000420 // Does this asm operand have a single letter operand modifier?
421 if (ExtraCode && ExtraCode[0]) {
422 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov8e9ece72009-08-08 23:10:41 +0000423
Evan Chenga8e29892007-01-19 07:51:42 +0000424 switch (ExtraCode[0]) {
Jack Carter0518fca2012-06-26 13:49:27 +0000425 default:
426 // See if this is a generic print operand
427 return AsmPrinter::PrintAsmOperand(MI, OpNum, AsmVariant, ExtraCode, O);
Bob Wilson9b4b00a2009-07-09 23:54:51 +0000428 case 'a': // Print as a memory address.
429 if (MI->getOperand(OpNum).isReg()) {
Jim Grosbach2f24c4e2010-09-30 15:25:22 +0000430 O << "["
431 << ARMInstPrinter::getRegisterName(MI->getOperand(OpNum).getReg())
432 << "]";
Bob Wilson9b4b00a2009-07-09 23:54:51 +0000433 return false;
434 }
435 // Fallthrough
436 case 'c': // Don't print "#" before an immediate operand.
Bob Wilson4f38b382009-08-21 21:58:55 +0000437 if (!MI->getOperand(OpNum).isImm())
438 return true;
Jim Grosbach2317e402010-09-30 01:57:53 +0000439 O << MI->getOperand(OpNum).getImm();
Bob Wilson8f343462009-04-06 21:46:51 +0000440 return false;
Evan Chenge21e3962007-04-04 00:13:29 +0000441 case 'P': // Print a VFP double precision register.
Evan Chengd831cda2009-12-08 23:06:22 +0000442 case 'q': // Print a NEON quad precision register.
Chris Lattner35c33bd2010-04-04 04:47:45 +0000443 printOperand(MI, OpNum, O);
Evan Cheng23a95702007-03-08 22:42:46 +0000444 return false;
Eric Christopher0628d382011-05-24 22:10:34 +0000445 case 'y': // Print a VFP single precision register as indexed double.
Eric Christopher0628d382011-05-24 22:10:34 +0000446 if (MI->getOperand(OpNum).isReg()) {
447 unsigned Reg = MI->getOperand(OpNum).getReg();
448 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
Jakob Stoklund Olesen4c91bda2012-05-30 23:00:43 +0000449 // Find the 'd' register that has this 's' register as a sub-register,
450 // and determine the lane number.
451 for (MCSuperRegIterator SR(Reg, TRI); SR.isValid(); ++SR) {
452 if (!ARM::DPRRegClass.contains(*SR))
453 continue;
454 bool Lane0 = TRI->getSubReg(*SR, ARM::ssub_0) == Reg;
455 O << ARMInstPrinter::getRegisterName(*SR) << (Lane0 ? "[0]" : "[1]");
456 return false;
457 }
Eric Christopher0628d382011-05-24 22:10:34 +0000458 }
Eric Christopher4db7dec2011-05-24 23:27:13 +0000459 return true;
Eric Christopherfef50062011-05-24 22:27:43 +0000460 case 'B': // Bitwise inverse of integer or symbol without a preceding #.
Eric Christophere1739d52011-05-24 23:15:43 +0000461 if (!MI->getOperand(OpNum).isImm())
462 return true;
463 O << ~(MI->getOperand(OpNum).getImm());
464 return false;
Eric Christopherfef50062011-05-24 22:27:43 +0000465 case 'L': // The low 16 bits of an immediate constant.
Eric Christopher4db7dec2011-05-24 23:27:13 +0000466 if (!MI->getOperand(OpNum).isImm())
467 return true;
468 O << (MI->getOperand(OpNum).getImm() & 0xffff);
469 return false;
Eric Christopher3c14f242011-05-28 01:40:44 +0000470 case 'M': { // A register range suitable for LDM/STM.
471 if (!MI->getOperand(OpNum).isReg())
472 return true;
473 const MachineOperand &MO = MI->getOperand(OpNum);
474 unsigned RegBegin = MO.getReg();
475 // This takes advantage of the 2 operand-ness of ldm/stm and that we've
476 // already got the operands in registers that are operands to the
477 // inline asm statement.
Jim Grosbach8e0c7692011-09-02 18:46:15 +0000478
Eric Christopher3c14f242011-05-28 01:40:44 +0000479 O << "{" << ARMInstPrinter::getRegisterName(RegBegin);
Jim Grosbach8e0c7692011-09-02 18:46:15 +0000480
Eric Christopher3c14f242011-05-28 01:40:44 +0000481 // FIXME: The register allocator not only may not have given us the
482 // registers in sequence, but may not be in ascending registers. This
483 // will require changes in the register allocator that'll need to be
484 // propagated down here if the operands change.
485 unsigned RegOps = OpNum + 1;
486 while (MI->getOperand(RegOps).isReg()) {
Jim Grosbach8e0c7692011-09-02 18:46:15 +0000487 O << ", "
Eric Christopher3c14f242011-05-28 01:40:44 +0000488 << ARMInstPrinter::getRegisterName(MI->getOperand(RegOps).getReg());
489 RegOps++;
490 }
491
492 O << "}";
493
494 return false;
495 }
Rafael Espindolaf5ade5d2011-08-10 16:26:42 +0000496 case 'R': // The most significant register of a pair.
497 case 'Q': { // The least significant register of a pair.
498 if (OpNum == 0)
499 return true;
500 const MachineOperand &FlagsOP = MI->getOperand(OpNum - 1);
501 if (!FlagsOP.isImm())
502 return true;
503 unsigned Flags = FlagsOP.getImm();
504 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
505 if (NumVals != 2)
506 return true;
507 unsigned RegOp = ExtraCode[0] == 'Q' ? OpNum : OpNum + 1;
508 if (RegOp >= MI->getNumOperands())
509 return true;
510 const MachineOperand &MO = MI->getOperand(RegOp);
511 if (!MO.isReg())
512 return true;
513 unsigned Reg = MO.getReg();
514 O << ARMInstPrinter::getRegisterName(Reg);
515 return false;
516 }
517
Eric Christopherfef50062011-05-24 22:27:43 +0000518 case 'e': // The low doubleword register of a NEON quad register.
Bob Wilson9cd2b952011-12-12 21:45:15 +0000519 case 'f': { // The high doubleword register of a NEON quad register.
520 if (!MI->getOperand(OpNum).isReg())
521 return true;
522 unsigned Reg = MI->getOperand(OpNum).getReg();
523 if (!ARM::QPRRegClass.contains(Reg))
524 return true;
525 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
526 unsigned SubReg = TRI->getSubReg(Reg, ExtraCode[0] == 'e' ?
527 ARM::dsub_0 : ARM::dsub_1);
528 O << ARMInstPrinter::getRegisterName(SubReg);
529 return false;
530 }
531
Eric Christopher001d2192012-08-13 18:18:52 +0000532 // This modifier is not yet supported.
Eric Christopherfef50062011-05-24 22:27:43 +0000533 case 'h': // A range of VFP/NEON registers suitable for VLD1/VST1.
Bob Wilsond984eb62010-05-27 20:23:42 +0000534 return true;
Eric Christopher6eef0e22012-08-14 23:32:15 +0000535 case 'H': { // The highest-numbered register of a pair.
Eric Christopher001d2192012-08-13 18:18:52 +0000536 const MachineOperand &MO = MI->getOperand(OpNum);
537 if (!MO.isReg())
538 return true;
539 const TargetRegisterClass &RC = ARM::GPRRegClass;
540 const MachineFunction &MF = *MI->getParent()->getParent();
541 const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo();
542
543 unsigned RegIdx = TRI->getEncodingValue(MO.getReg());
544 RegIdx |= 1; //The odd register is also the higher-numbered one of a pair.
545
546 unsigned Reg = RC.getRegister(RegIdx);
547 O << ARMInstPrinter::getRegisterName(Reg);
548 return false;
Evan Cheng84f60b72010-05-27 22:08:38 +0000549 }
Eric Christopher6eef0e22012-08-14 23:32:15 +0000550 }
Evan Chenga8e29892007-01-19 07:51:42 +0000551 }
Jim Grosbache9952212009-09-04 01:38:51 +0000552
Chris Lattner35c33bd2010-04-04 04:47:45 +0000553 printOperand(MI, OpNum, O);
Evan Chenga8e29892007-01-19 07:51:42 +0000554 return false;
555}
556
Bob Wilson224c2442009-05-19 05:53:42 +0000557bool ARMAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Evan Cheng055b0312009-06-29 07:51:04 +0000558 unsigned OpNum, unsigned AsmVariant,
Chris Lattnerc75c0282010-04-04 05:29:35 +0000559 const char *ExtraCode,
560 raw_ostream &O) {
Eric Christopher8f894632011-05-25 20:51:58 +0000561 // Does this asm operand have a single letter operand modifier?
562 if (ExtraCode && ExtraCode[0]) {
563 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Jim Grosbach8e0c7692011-09-02 18:46:15 +0000564
Eric Christopher8f894632011-05-25 20:51:58 +0000565 switch (ExtraCode[0]) {
Eric Christopher32bfb2c2011-05-26 18:22:26 +0000566 case 'A': // A memory operand for a VLD1/VST1 instruction.
Eric Christopher8f894632011-05-25 20:51:58 +0000567 default: return true; // Unknown modifier.
568 case 'm': // The base register of a memory operand.
569 if (!MI->getOperand(OpNum).isReg())
570 return true;
571 O << ARMInstPrinter::getRegisterName(MI->getOperand(OpNum).getReg());
572 return false;
573 }
574 }
Jim Grosbach8e0c7692011-09-02 18:46:15 +0000575
Bob Wilson765cc0b2009-10-13 20:50:28 +0000576 const MachineOperand &MO = MI->getOperand(OpNum);
577 assert(MO.isReg() && "unexpected inline asm memory operand");
Jim Grosbach2317e402010-09-30 01:57:53 +0000578 O << "[" << ARMInstPrinter::getRegisterName(MO.getReg()) << "]";
Bob Wilson224c2442009-05-19 05:53:42 +0000579 return false;
580}
581
Bob Wilson812209a2009-09-30 22:06:26 +0000582void ARMAsmPrinter::EmitStartOfAsmFile(Module &M) {
Bob Wilson0fb34682009-09-30 00:23:42 +0000583 if (Subtarget->isTargetDarwin()) {
584 Reloc::Model RelocM = TM.getRelocationModel();
585 if (RelocM == Reloc::PIC_ || RelocM == Reloc::DynamicNoPIC) {
586 // Declare all the text sections up front (before the DWARF sections
587 // emitted by AsmPrinter::doInitialization) so the assembler will keep
588 // them together at the beginning of the object file. This helps
589 // avoid out-of-range branches that are due a fundamental limitation of
590 // the way symbol offsets are encoded with the current Darwin ARM
591 // relocations.
Jim Grosbachb0739b72010-09-02 01:02:06 +0000592 const TargetLoweringObjectFileMachO &TLOFMacho =
Dan Gohman0d805c32010-04-17 16:44:48 +0000593 static_cast<const TargetLoweringObjectFileMachO &>(
594 getObjFileLowering());
Bob Wilson29e06692009-09-30 22:25:37 +0000595 OutStreamer.SwitchSection(TLOFMacho.getTextSection());
596 OutStreamer.SwitchSection(TLOFMacho.getTextCoalSection());
597 OutStreamer.SwitchSection(TLOFMacho.getConstTextCoalSection());
598 if (RelocM == Reloc::DynamicNoPIC) {
599 const MCSection *sect =
Chris Lattner22772212010-04-08 20:40:11 +0000600 OutContext.getMachOSection("__TEXT", "__symbol_stub4",
601 MCSectionMachO::S_SYMBOL_STUBS,
602 12, SectionKind::getText());
Bob Wilson29e06692009-09-30 22:25:37 +0000603 OutStreamer.SwitchSection(sect);
604 } else {
605 const MCSection *sect =
Chris Lattner22772212010-04-08 20:40:11 +0000606 OutContext.getMachOSection("__TEXT", "__picsymbolstub4",
607 MCSectionMachO::S_SYMBOL_STUBS,
608 16, SectionKind::getText());
Bob Wilson29e06692009-09-30 22:25:37 +0000609 OutStreamer.SwitchSection(sect);
610 }
Bob Wilson63db5942010-07-30 19:55:47 +0000611 const MCSection *StaticInitSect =
612 OutContext.getMachOSection("__TEXT", "__StaticInit",
613 MCSectionMachO::S_REGULAR |
614 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
615 SectionKind::getText());
616 OutStreamer.SwitchSection(StaticInitSect);
Bob Wilson0fb34682009-09-30 00:23:42 +0000617 }
618 }
619
Jim Grosbache5165492009-11-09 00:11:35 +0000620 // Use unified assembler syntax.
Jason W Kimafd1cc22010-09-30 02:45:56 +0000621 OutStreamer.EmitAssemblerFlag(MCAF_SyntaxUnified);
Anton Korobeynikovd61eca52009-06-17 23:43:18 +0000622
Anton Korobeynikov88ce6672009-05-23 19:51:20 +0000623 // Emit ARM Build Attributes
Evan Cheng07043272012-02-21 20:46:00 +0000624 if (Subtarget->isTargetELF())
Jason W Kimdef9ac42010-10-06 22:36:46 +0000625 emitAttributes();
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000626}
627
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +0000628
Chris Lattner4a071d62009-10-19 17:59:19 +0000629void ARMAsmPrinter::EmitEndOfAsmFile(Module &M) {
Evan Cheng5be54b02007-01-19 19:25:36 +0000630 if (Subtarget->isTargetDarwin()) {
Chris Lattnerf61159b2009-08-03 22:18:15 +0000631 // All darwin targets use mach-o.
Dan Gohman0d805c32010-04-17 16:44:48 +0000632 const TargetLoweringObjectFileMachO &TLOFMacho =
633 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattnerb0f294c2009-10-19 18:38:33 +0000634 MachineModuleInfoMachO &MMIMacho =
635 MMI->getObjFileInfo<MachineModuleInfoMachO>();
Jim Grosbache9952212009-09-04 01:38:51 +0000636
Evan Chenga8e29892007-01-19 07:51:42 +0000637 // Output non-lazy-pointers for external and common global variables.
Chris Lattnerb0f294c2009-10-19 18:38:33 +0000638 MachineModuleInfoMachO::SymbolListTy Stubs = MMIMacho.GetGVStubList();
Bill Wendlingcebae362010-03-10 22:34:10 +0000639
Chris Lattnerb0f294c2009-10-19 18:38:33 +0000640 if (!Stubs.empty()) {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000641 // Switch with ".non_lazy_symbol_pointer" directive.
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000642 OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
Chris Lattnerc076a972009-08-10 18:01:34 +0000643 EmitAlignment(2);
Chris Lattnerb0f294c2009-10-19 18:38:33 +0000644 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Bill Wendlingbecd83e2010-03-09 00:40:17 +0000645 // L_foo$stub:
646 OutStreamer.EmitLabel(Stubs[i].first);
647 // .indirect_symbol _foo
Bill Wendling52a50e52010-03-11 01:18:13 +0000648 MachineModuleInfoImpl::StubValueTy &MCSym = Stubs[i].second;
649 OutStreamer.EmitSymbolAttribute(MCSym.getPointer(),MCSA_IndirectSymbol);
Bill Wendlingcf6f28d2010-03-09 00:43:34 +0000650
Bill Wendling52a50e52010-03-11 01:18:13 +0000651 if (MCSym.getInt())
Bill Wendlingcf6f28d2010-03-09 00:43:34 +0000652 // External to current translation unit.
653 OutStreamer.EmitIntValue(0, 4/*size*/, 0/*addrspace*/);
654 else
655 // Internal to current translation unit.
Bill Wendling5e1b55d2010-03-31 18:47:10 +0000656 //
Jim Grosbach1b935a32010-09-22 16:45:13 +0000657 // When we place the LSDA into the TEXT section, the type info
658 // pointers need to be indirect and pc-rel. We accomplish this by
659 // using NLPs; however, sometimes the types are local to the file.
660 // We need to fill in the value for the NLP in those cases.
Bill Wendling52a50e52010-03-11 01:18:13 +0000661 OutStreamer.EmitValue(MCSymbolRefExpr::Create(MCSym.getPointer(),
662 OutContext),
Bill Wendlingcf6f28d2010-03-09 00:43:34 +0000663 4/*size*/, 0/*addrspace*/);
Evan Chengae94e592008-12-05 01:06:39 +0000664 }
Bill Wendlingbecd83e2010-03-09 00:40:17 +0000665
666 Stubs.clear();
667 OutStreamer.AddBlankLine();
Evan Chenga8e29892007-01-19 07:51:42 +0000668 }
669
Chris Lattnere4d9ea82009-10-19 18:44:38 +0000670 Stubs = MMIMacho.GetHiddenGVStubList();
671 if (!Stubs.empty()) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000672 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
Chris Lattnerf3231de2009-08-10 18:02:16 +0000673 EmitAlignment(2);
Bill Wendlingbecd83e2010-03-09 00:40:17 +0000674 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
675 // L_foo$stub:
676 OutStreamer.EmitLabel(Stubs[i].first);
677 // .long _foo
Bill Wendlingcebae362010-03-10 22:34:10 +0000678 OutStreamer.EmitValue(MCSymbolRefExpr::
679 Create(Stubs[i].second.getPointer(),
680 OutContext),
Bill Wendlingbecd83e2010-03-09 00:40:17 +0000681 4/*size*/, 0/*addrspace*/);
682 }
Bill Wendlingcf6f28d2010-03-09 00:43:34 +0000683
684 Stubs.clear();
685 OutStreamer.AddBlankLine();
Evan Chengae94e592008-12-05 01:06:39 +0000686 }
687
Evan Chenga8e29892007-01-19 07:51:42 +0000688 // Funny Darwin hack: This flag tells the linker that no global symbols
689 // contain code that falls through to other global symbols (e.g. the obvious
690 // implementation of multiple entry points). If this doesn't occur, the
691 // linker can safely perform dead code stripping. Since LLVM never
692 // generates code that does this, it is always safe to set.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000693 OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000694 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000695}
Anton Korobeynikov0bd89712008-08-17 13:55:10 +0000696
Chris Lattner97f06932009-10-19 20:20:46 +0000697//===----------------------------------------------------------------------===//
Jason W Kimdef9ac42010-10-06 22:36:46 +0000698// Helper routines for EmitStartOfAsmFile() and EmitEndOfAsmFile()
699// FIXME:
700// The following seem like one-off assembler flags, but they actually need
Jim Grosbachfa7fb642010-10-06 22:46:47 +0000701// to appear in the .ARM.attributes section in ELF.
Jason W Kimdef9ac42010-10-06 22:36:46 +0000702// Instead of subclassing the MCELFStreamer, we do the work here.
703
704void ARMAsmPrinter::emitAttributes() {
Jim Grosbachfa7fb642010-10-06 22:46:47 +0000705
Jason W Kim17b443d2010-10-11 23:01:44 +0000706 emitARMAttributeSection();
707
Renato Golin728ff0d2011-02-28 22:04:27 +0000708 /* GAS expect .fpu to be emitted, regardless of VFP build attribute */
709 bool emitFPU = false;
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000710 AttributeEmitter *AttrEmitter;
Renato Golin728ff0d2011-02-28 22:04:27 +0000711 if (OutStreamer.hasRawTextSupport()) {
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000712 AttrEmitter = new AsmAttributeEmitter(OutStreamer);
Renato Golin728ff0d2011-02-28 22:04:27 +0000713 emitFPU = true;
714 } else {
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000715 MCObjectStreamer &O = static_cast<MCObjectStreamer&>(OutStreamer);
716 AttrEmitter = new ObjectAttributeEmitter(O);
717 }
718
719 AttrEmitter->MaybeSwitchVendor("aeabi");
720
Jason W Kimdef9ac42010-10-06 22:36:46 +0000721 std::string CPUString = Subtarget->getCPUString();
Jason W Kimf009a962011-02-07 00:49:53 +0000722
723 if (CPUString == "cortex-a8" ||
724 Subtarget->isCortexA8()) {
Jason W Kimc046d642011-02-07 19:07:11 +0000725 AttrEmitter->EmitTextAttribute(ARMBuildAttrs::CPU_name, "cortex-a8");
Jason W Kimf009a962011-02-07 00:49:53 +0000726 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v7);
727 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch_profile,
728 ARMBuildAttrs::ApplicationProfile);
729 AttrEmitter->EmitAttribute(ARMBuildAttrs::ARM_ISA_use,
730 ARMBuildAttrs::Allowed);
731 AttrEmitter->EmitAttribute(ARMBuildAttrs::THUMB_ISA_use,
732 ARMBuildAttrs::AllowThumb32);
733 // Fixme: figure out when this is emitted.
734 //AttrEmitter->EmitAttribute(ARMBuildAttrs::WMMX_arch,
735 // ARMBuildAttrs::AllowWMMXv1);
736 //
737
738 /// ADD additional Else-cases here!
Rafael Espindolab8adb8a2011-05-20 20:10:34 +0000739 } else if (CPUString == "xscale") {
740 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v5TEJ);
741 AttrEmitter->EmitAttribute(ARMBuildAttrs::ARM_ISA_use,
742 ARMBuildAttrs::Allowed);
743 AttrEmitter->EmitAttribute(ARMBuildAttrs::THUMB_ISA_use,
744 ARMBuildAttrs::Allowed);
Jason W Kimf009a962011-02-07 00:49:53 +0000745 } else if (CPUString == "generic") {
Dale Johannesen7179d1e2010-11-08 19:17:22 +0000746 // FIXME: Why these defaults?
747 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v4T);
Jason W Kimf009a962011-02-07 00:49:53 +0000748 AttrEmitter->EmitAttribute(ARMBuildAttrs::ARM_ISA_use,
749 ARMBuildAttrs::Allowed);
750 AttrEmitter->EmitAttribute(ARMBuildAttrs::THUMB_ISA_use,
751 ARMBuildAttrs::Allowed);
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000752 }
Jason W Kimdef9ac42010-10-06 22:36:46 +0000753
Renato Goline89a0532011-03-02 21:20:09 +0000754 if (Subtarget->hasNEON() && emitFPU) {
Renato Golin728ff0d2011-02-28 22:04:27 +0000755 /* NEON is not exactly a VFP architecture, but GAS emit one of
Anton Korobeynikov4b4e6222012-01-22 12:07:33 +0000756 * neon/neon-vfpv4/vfpv3/vfpv2 for .fpu parameters */
Evan Chengbee78fe2012-04-11 05:33:07 +0000757 if (Subtarget->hasVFP4())
Jim Grosbachd4f020a2012-04-06 23:43:50 +0000758 AttrEmitter->EmitTextAttribute(ARMBuildAttrs::Advanced_SIMD_arch,
759 "neon-vfpv4");
Anton Korobeynikov4b4e6222012-01-22 12:07:33 +0000760 else
Sebastian Pop74bebde2012-03-05 17:39:52 +0000761 AttrEmitter->EmitTextAttribute(ARMBuildAttrs::Advanced_SIMD_arch, "neon");
Renato Golin728ff0d2011-02-28 22:04:27 +0000762 /* If emitted for NEON, omit from VFP below, since you can have both
763 * NEON and VFP in build attributes but only one .fpu */
764 emitFPU = false;
765 }
766
Anton Korobeynikov4b4e6222012-01-22 12:07:33 +0000767 /* VFPv4 + .fpu */
768 if (Subtarget->hasVFP4()) {
769 AttrEmitter->EmitAttribute(ARMBuildAttrs::VFP_arch,
770 ARMBuildAttrs::AllowFPv4A);
771 if (emitFPU)
772 AttrEmitter->EmitTextAttribute(ARMBuildAttrs::VFP_arch, "vfpv4");
773
Renato Golin728ff0d2011-02-28 22:04:27 +0000774 /* VFPv3 + .fpu */
Anton Korobeynikov4b4e6222012-01-22 12:07:33 +0000775 } else if (Subtarget->hasVFP3()) {
Renato Golin728ff0d2011-02-28 22:04:27 +0000776 AttrEmitter->EmitAttribute(ARMBuildAttrs::VFP_arch,
777 ARMBuildAttrs::AllowFPv3A);
778 if (emitFPU)
779 AttrEmitter->EmitTextAttribute(ARMBuildAttrs::VFP_arch, "vfpv3");
780
781 /* VFPv2 + .fpu */
782 } else if (Subtarget->hasVFP2()) {
Jason W Kimf009a962011-02-07 00:49:53 +0000783 AttrEmitter->EmitAttribute(ARMBuildAttrs::VFP_arch,
784 ARMBuildAttrs::AllowFPv2);
Renato Golin728ff0d2011-02-28 22:04:27 +0000785 if (emitFPU)
786 AttrEmitter->EmitTextAttribute(ARMBuildAttrs::VFP_arch, "vfpv2");
787 }
788
789 /* TODO: ARMBuildAttrs::Allowed is not completely accurate,
Cameron Zwarich375db7f2011-07-07 08:28:52 +0000790 * since NEON can have 1 (allowed) or 2 (MAC operations) */
Renato Golin728ff0d2011-02-28 22:04:27 +0000791 if (Subtarget->hasNEON()) {
792 AttrEmitter->EmitAttribute(ARMBuildAttrs::Advanced_SIMD_arch,
793 ARMBuildAttrs::Allowed);
794 }
Jason W Kimdef9ac42010-10-06 22:36:46 +0000795
796 // Signal various FP modes.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000797 if (!TM.Options.UnsafeFPMath) {
Jason W Kimf009a962011-02-07 00:49:53 +0000798 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_FP_denormal,
799 ARMBuildAttrs::Allowed);
800 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_FP_exceptions,
801 ARMBuildAttrs::Allowed);
Jason W Kimdef9ac42010-10-06 22:36:46 +0000802 }
803
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000804 if (TM.Options.NoInfsFPMath && TM.Options.NoNaNsFPMath)
Jason W Kimf009a962011-02-07 00:49:53 +0000805 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_FP_number_model,
806 ARMBuildAttrs::Allowed);
Jason W Kimdef9ac42010-10-06 22:36:46 +0000807 else
Jason W Kimf009a962011-02-07 00:49:53 +0000808 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_FP_number_model,
809 ARMBuildAttrs::AllowIEE754);
Jason W Kimdef9ac42010-10-06 22:36:46 +0000810
Jason W Kimf009a962011-02-07 00:49:53 +0000811 // FIXME: add more flags to ARMBuildAttrs.h
Jason W Kimdef9ac42010-10-06 22:36:46 +0000812 // 8-bytes alignment stuff.
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000813 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_align8_needed, 1);
814 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_align8_preserved, 1);
Jason W Kimdef9ac42010-10-06 22:36:46 +0000815
816 // Hard float. Use both S and D registers and conform to AAPCS-VFP.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000817 if (Subtarget->isAAPCS_ABI() && TM.Options.FloatABIType == FloatABI::Hard) {
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000818 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_HardFP_use, 3);
819 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_VFP_args, 1);
Jason W Kimdef9ac42010-10-06 22:36:46 +0000820 }
821 // FIXME: Should we signal R9 usage?
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000822
Jason W Kimf009a962011-02-07 00:49:53 +0000823 if (Subtarget->hasDivide())
824 AttrEmitter->EmitAttribute(ARMBuildAttrs::DIV_use, 1);
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000825
826 AttrEmitter->Finish();
827 delete AttrEmitter;
Jason W Kimdef9ac42010-10-06 22:36:46 +0000828}
829
Jason W Kim17b443d2010-10-11 23:01:44 +0000830void ARMAsmPrinter::emitARMAttributeSection() {
831 // <format-version>
832 // [ <section-length> "vendor-name"
833 // [ <file-tag> <size> <attribute>*
834 // | <section-tag> <size> <section-number>* 0 <attribute>*
835 // | <symbol-tag> <size> <symbol-number>* 0 <attribute>*
836 // ]+
837 // ]*
838
839 if (OutStreamer.hasRawTextSupport())
840 return;
841
842 const ARMElfTargetObjectFile &TLOFELF =
843 static_cast<const ARMElfTargetObjectFile &>
844 (getObjFileLowering());
845
846 OutStreamer.SwitchSection(TLOFELF.getAttributesSection());
Jason W Kim17b443d2010-10-11 23:01:44 +0000847
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000848 // Format version
849 OutStreamer.EmitIntValue(0x41, 1);
Jason W Kim17b443d2010-10-11 23:01:44 +0000850}
851
Jason W Kimdef9ac42010-10-06 22:36:46 +0000852//===----------------------------------------------------------------------===//
Chris Lattner97f06932009-10-19 20:20:46 +0000853
Jim Grosbach988ce092010-09-18 00:05:05 +0000854static MCSymbol *getPICLabel(const char *Prefix, unsigned FunctionNumber,
855 unsigned LabelId, MCContext &Ctx) {
856
857 MCSymbol *Label = Ctx.GetOrCreateSymbol(Twine(Prefix)
858 + "PC" + Twine(FunctionNumber) + "_" + Twine(LabelId));
859 return Label;
860}
861
Jim Grosbach2c4d5122010-11-10 03:26:07 +0000862static MCSymbolRefExpr::VariantKind
863getModifierVariantKind(ARMCP::ARMCPModifier Modifier) {
864 switch (Modifier) {
Jim Grosbach2c4d5122010-11-10 03:26:07 +0000865 case ARMCP::no_modifier: return MCSymbolRefExpr::VK_None;
866 case ARMCP::TLSGD: return MCSymbolRefExpr::VK_ARM_TLSGD;
867 case ARMCP::TPOFF: return MCSymbolRefExpr::VK_ARM_TPOFF;
868 case ARMCP::GOTTPOFF: return MCSymbolRefExpr::VK_ARM_GOTTPOFF;
869 case ARMCP::GOT: return MCSymbolRefExpr::VK_ARM_GOT;
870 case ARMCP::GOTOFF: return MCSymbolRefExpr::VK_ARM_GOTOFF;
871 }
David Blaikie4d6ccb52012-01-20 21:51:11 +0000872 llvm_unreachable("Invalid ARMCPModifier!");
Jim Grosbach2c4d5122010-11-10 03:26:07 +0000873}
874
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000875MCSymbol *ARMAsmPrinter::GetARMGVSymbol(const GlobalValue *GV) {
876 bool isIndirect = Subtarget->isTargetDarwin() &&
877 Subtarget->GVIsIndirectSymbol(GV, TM.getRelocationModel());
878 if (!isIndirect)
879 return Mang->getSymbol(GV);
880
881 // FIXME: Remove this when Darwin transition to @GOT like syntax.
882 MCSymbol *MCSym = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
883 MachineModuleInfoMachO &MMIMachO =
884 MMI->getObjFileInfo<MachineModuleInfoMachO>();
885 MachineModuleInfoImpl::StubValueTy &StubSym =
886 GV->hasHiddenVisibility() ? MMIMachO.getHiddenGVStubEntry(MCSym) :
887 MMIMachO.getGVStubEntry(MCSym);
888 if (StubSym.getPointer() == 0)
889 StubSym = MachineModuleInfoImpl::
890 StubValueTy(Mang->getSymbol(GV), !GV->hasInternalLinkage());
891 return MCSym;
892}
893
Jim Grosbach5df08d82010-11-09 18:45:04 +0000894void ARMAsmPrinter::
895EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
896 int Size = TM.getTargetData()->getTypeAllocSize(MCPV->getType());
897
898 ARMConstantPoolValue *ACPV = static_cast<ARMConstantPoolValue*>(MCPV);
Jim Grosbach5df08d82010-11-09 18:45:04 +0000899
Jim Grosbach7c7ddb22010-11-10 17:59:10 +0000900 MCSymbol *MCSym;
Jim Grosbach5df08d82010-11-09 18:45:04 +0000901 if (ACPV->isLSDA()) {
Jim Grosbach7c7ddb22010-11-10 17:59:10 +0000902 SmallString<128> Str;
903 raw_svector_ostream OS(Str);
Jim Grosbach5df08d82010-11-09 18:45:04 +0000904 OS << MAI->getPrivateGlobalPrefix() << "_LSDA_" << getFunctionNumber();
Jim Grosbach7c7ddb22010-11-10 17:59:10 +0000905 MCSym = OutContext.GetOrCreateSymbol(OS.str());
Jim Grosbach5df08d82010-11-09 18:45:04 +0000906 } else if (ACPV->isBlockAddress()) {
Bill Wendling5bb77992011-10-01 08:00:54 +0000907 const BlockAddress *BA =
908 cast<ARMConstantPoolConstant>(ACPV)->getBlockAddress();
909 MCSym = GetBlockAddressSymbol(BA);
Jim Grosbach5df08d82010-11-09 18:45:04 +0000910 } else if (ACPV->isGlobalValue()) {
Bill Wendling5bb77992011-10-01 08:00:54 +0000911 const GlobalValue *GV = cast<ARMConstantPoolConstant>(ACPV)->getGV();
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000912 MCSym = GetARMGVSymbol(GV);
Bill Wendlinge00897c2011-09-29 23:50:42 +0000913 } else if (ACPV->isMachineBasicBlock()) {
Bill Wendling3320f2a2011-10-01 09:30:42 +0000914 const MachineBasicBlock *MBB = cast<ARMConstantPoolMBB>(ACPV)->getMBB();
Bill Wendlinge00897c2011-09-29 23:50:42 +0000915 MCSym = MBB->getSymbol();
Jim Grosbach5df08d82010-11-09 18:45:04 +0000916 } else {
917 assert(ACPV->isExtSymbol() && "unrecognized constant pool value");
Bill Wendlingfe31e672011-10-01 08:58:29 +0000918 const char *Sym = cast<ARMConstantPoolSymbol>(ACPV)->getSymbol();
919 MCSym = GetExternalSymbolSymbol(Sym);
Jim Grosbach5df08d82010-11-09 18:45:04 +0000920 }
921
922 // Create an MCSymbol for the reference.
Jim Grosbach2c4d5122010-11-10 03:26:07 +0000923 const MCExpr *Expr =
924 MCSymbolRefExpr::Create(MCSym, getModifierVariantKind(ACPV->getModifier()),
925 OutContext);
Jim Grosbach5df08d82010-11-09 18:45:04 +0000926
Jim Grosbach2c4d5122010-11-10 03:26:07 +0000927 if (ACPV->getPCAdjustment()) {
928 MCSymbol *PCLabel = getPICLabel(MAI->getPrivateGlobalPrefix(),
929 getFunctionNumber(),
930 ACPV->getLabelId(),
931 OutContext);
932 const MCExpr *PCRelExpr = MCSymbolRefExpr::Create(PCLabel, OutContext);
933 PCRelExpr =
934 MCBinaryExpr::CreateAdd(PCRelExpr,
935 MCConstantExpr::Create(ACPV->getPCAdjustment(),
936 OutContext),
937 OutContext);
938 if (ACPV->mustAddCurrentAddress()) {
939 // We want "(<expr> - .)", but MC doesn't have a concept of the '.'
940 // label, so just emit a local label end reference that instead.
941 MCSymbol *DotSym = OutContext.CreateTempSymbol();
942 OutStreamer.EmitLabel(DotSym);
943 const MCExpr *DotExpr = MCSymbolRefExpr::Create(DotSym, OutContext);
944 PCRelExpr = MCBinaryExpr::CreateSub(PCRelExpr, DotExpr, OutContext);
Jim Grosbach5df08d82010-11-09 18:45:04 +0000945 }
Jim Grosbach2c4d5122010-11-10 03:26:07 +0000946 Expr = MCBinaryExpr::CreateSub(Expr, PCRelExpr, OutContext);
Jim Grosbach5df08d82010-11-09 18:45:04 +0000947 }
Jim Grosbach2c4d5122010-11-10 03:26:07 +0000948 OutStreamer.EmitValue(Expr, Size);
Jim Grosbach5df08d82010-11-09 18:45:04 +0000949}
950
Jim Grosbacha2244cb2010-09-22 17:39:48 +0000951void ARMAsmPrinter::EmitJumpTable(const MachineInstr *MI) {
952 unsigned Opcode = MI->getOpcode();
953 int OpNum = 1;
954 if (Opcode == ARM::BR_JTadd)
955 OpNum = 2;
956 else if (Opcode == ARM::BR_JTm)
957 OpNum = 3;
958
959 const MachineOperand &MO1 = MI->getOperand(OpNum);
960 const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
961 unsigned JTI = MO1.getIndex();
962
963 // Emit a label for the jump table.
964 MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel2(JTI, MO2.getImm());
965 OutStreamer.EmitLabel(JTISymbol);
966
Jim Grosbach3e965312012-05-18 19:12:01 +0000967 // Mark the jump table as data-in-code.
968 OutStreamer.EmitDataRegion(MCDR_DataRegionJT32);
969
Jim Grosbacha2244cb2010-09-22 17:39:48 +0000970 // Emit each entry of the table.
971 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
972 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
973 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
974
975 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
976 MachineBasicBlock *MBB = JTBBs[i];
977 // Construct an MCExpr for the entry. We want a value of the form:
978 // (BasicBlockAddr - TableBeginAddr)
979 //
980 // For example, a table with entries jumping to basic blocks BB0 and BB1
981 // would look like:
982 // LJTI_0_0:
983 // .word (LBB0 - LJTI_0_0)
984 // .word (LBB1 - LJTI_0_0)
985 const MCExpr *Expr = MCSymbolRefExpr::Create(MBB->getSymbol(), OutContext);
986
987 if (TM.getRelocationModel() == Reloc::PIC_)
988 Expr = MCBinaryExpr::CreateSub(Expr, MCSymbolRefExpr::Create(JTISymbol,
989 OutContext),
990 OutContext);
Jim Grosbachde982732011-08-31 22:23:09 +0000991 // If we're generating a table of Thumb addresses in static relocation
992 // model, we need to add one to keep interworking correctly.
993 else if (AFI->isThumbFunction())
994 Expr = MCBinaryExpr::CreateAdd(Expr, MCConstantExpr::Create(1,OutContext),
995 OutContext);
Jim Grosbacha2244cb2010-09-22 17:39:48 +0000996 OutStreamer.EmitValue(Expr, 4);
997 }
Jim Grosbach3e965312012-05-18 19:12:01 +0000998 // Mark the end of jump table data-in-code region.
999 OutStreamer.EmitDataRegion(MCDR_DataRegionEnd);
Jim Grosbacha2244cb2010-09-22 17:39:48 +00001000}
1001
Jim Grosbach882ef2b2010-09-21 23:28:16 +00001002void ARMAsmPrinter::EmitJump2Table(const MachineInstr *MI) {
1003 unsigned Opcode = MI->getOpcode();
1004 int OpNum = (Opcode == ARM::t2BR_JT) ? 2 : 1;
1005 const MachineOperand &MO1 = MI->getOperand(OpNum);
1006 const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
1007 unsigned JTI = MO1.getIndex();
1008
Jim Grosbach882ef2b2010-09-21 23:28:16 +00001009 MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel2(JTI, MO2.getImm());
1010 OutStreamer.EmitLabel(JTISymbol);
1011
1012 // Emit each entry of the table.
1013 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
1014 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
1015 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
Jim Grosbach205a5fa2010-09-22 17:15:35 +00001016 unsigned OffsetWidth = 4;
Jim Grosbach3e965312012-05-18 19:12:01 +00001017 if (MI->getOpcode() == ARM::t2TBB_JT) {
Jim Grosbach205a5fa2010-09-22 17:15:35 +00001018 OffsetWidth = 1;
Jim Grosbach3e965312012-05-18 19:12:01 +00001019 // Mark the jump table as data-in-code.
1020 OutStreamer.EmitDataRegion(MCDR_DataRegionJT8);
1021 } else if (MI->getOpcode() == ARM::t2TBH_JT) {
Jim Grosbach205a5fa2010-09-22 17:15:35 +00001022 OffsetWidth = 2;
Jim Grosbach3e965312012-05-18 19:12:01 +00001023 // Mark the jump table as data-in-code.
1024 OutStreamer.EmitDataRegion(MCDR_DataRegionJT16);
1025 }
Jim Grosbach882ef2b2010-09-21 23:28:16 +00001026
1027 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
1028 MachineBasicBlock *MBB = JTBBs[i];
Jim Grosbach205a5fa2010-09-22 17:15:35 +00001029 const MCExpr *MBBSymbolExpr = MCSymbolRefExpr::Create(MBB->getSymbol(),
1030 OutContext);
Jim Grosbach882ef2b2010-09-21 23:28:16 +00001031 // If this isn't a TBB or TBH, the entries are direct branch instructions.
Jim Grosbach205a5fa2010-09-22 17:15:35 +00001032 if (OffsetWidth == 4) {
Jim Grosbach882ef2b2010-09-21 23:28:16 +00001033 MCInst BrInst;
1034 BrInst.setOpcode(ARM::t2B);
Jim Grosbach205a5fa2010-09-22 17:15:35 +00001035 BrInst.addOperand(MCOperand::CreateExpr(MBBSymbolExpr));
Owen Anderson51f6a7a2011-09-09 21:48:23 +00001036 BrInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1037 BrInst.addOperand(MCOperand::CreateReg(0));
Jim Grosbach882ef2b2010-09-21 23:28:16 +00001038 OutStreamer.EmitInstruction(BrInst);
1039 continue;
1040 }
1041 // Otherwise it's an offset from the dispatch instruction. Construct an
Jim Grosbach205a5fa2010-09-22 17:15:35 +00001042 // MCExpr for the entry. We want a value of the form:
1043 // (BasicBlockAddr - TableBeginAddr) / 2
1044 //
1045 // For example, a TBB table with entries jumping to basic blocks BB0 and BB1
1046 // would look like:
1047 // LJTI_0_0:
1048 // .byte (LBB0 - LJTI_0_0) / 2
1049 // .byte (LBB1 - LJTI_0_0) / 2
1050 const MCExpr *Expr =
1051 MCBinaryExpr::CreateSub(MBBSymbolExpr,
1052 MCSymbolRefExpr::Create(JTISymbol, OutContext),
1053 OutContext);
1054 Expr = MCBinaryExpr::CreateDiv(Expr, MCConstantExpr::Create(2, OutContext),
1055 OutContext);
1056 OutStreamer.EmitValue(Expr, OffsetWidth);
Jim Grosbach882ef2b2010-09-21 23:28:16 +00001057 }
Jim Grosbachb3a119a2012-05-21 23:34:42 +00001058 // Mark the end of jump table data-in-code region. 32-bit offsets use
1059 // actual branch instructions here, so we don't mark those as a data-region
1060 // at all.
1061 if (OffsetWidth != 4)
1062 OutStreamer.EmitDataRegion(MCDR_DataRegionEnd);
Jim Grosbach882ef2b2010-09-21 23:28:16 +00001063}
1064
Jim Grosbach2d0f53b2010-09-28 17:05:56 +00001065void ARMAsmPrinter::PrintDebugValueComment(const MachineInstr *MI,
1066 raw_ostream &OS) {
1067 unsigned NOps = MI->getNumOperands();
1068 assert(NOps==4);
1069 OS << '\t' << MAI->getCommentString() << "DEBUG_VALUE: ";
1070 // cast away const; DIetc do not take const operands for some reason.
1071 DIVariable V(const_cast<MDNode *>(MI->getOperand(NOps-1).getMetadata()));
1072 OS << V.getName();
1073 OS << " <- ";
1074 // Frame address. Currently handles register +- offset only.
1075 assert(MI->getOperand(0).isReg() && MI->getOperand(1).isImm());
1076 OS << '['; printOperand(MI, 0, OS); OS << '+'; printOperand(MI, 1, OS);
1077 OS << ']';
1078 OS << "+";
1079 printOperand(MI, NOps-2, OS);
1080}
1081
Jim Grosbach40edf732010-12-14 21:10:47 +00001082static void populateADROperands(MCInst &Inst, unsigned Dest,
1083 const MCSymbol *Label,
1084 unsigned pred, unsigned ccreg,
1085 MCContext &Ctx) {
1086 const MCExpr *SymbolExpr = MCSymbolRefExpr::Create(Label, Ctx);
1087 Inst.addOperand(MCOperand::CreateReg(Dest));
1088 Inst.addOperand(MCOperand::CreateExpr(SymbolExpr));
1089 // Add predicate operands.
1090 Inst.addOperand(MCOperand::CreateImm(pred));
1091 Inst.addOperand(MCOperand::CreateReg(ccreg));
1092}
1093
Anton Korobeynikov4d728602011-01-01 20:38:38 +00001094void ARMAsmPrinter::EmitPatchedInstruction(const MachineInstr *MI,
1095 unsigned Opcode) {
1096 MCInst TmpInst;
1097
1098 // Emit the instruction as usual, just patch the opcode.
1099 LowerARMMachineInstrToMCInst(MI, TmpInst, *this);
1100 TmpInst.setOpcode(Opcode);
1101 OutStreamer.EmitInstruction(TmpInst);
1102}
1103
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001104void ARMAsmPrinter::EmitUnwindingInstruction(const MachineInstr *MI) {
1105 assert(MI->getFlag(MachineInstr::FrameSetup) &&
1106 "Only instruction which are involved into frame setup code are allowed");
1107
1108 const MachineFunction &MF = *MI->getParent()->getParent();
1109 const TargetRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo();
Anton Korobeynikovb3fcc062011-03-05 18:43:55 +00001110 const ARMFunctionInfo &AFI = *MF.getInfo<ARMFunctionInfo>();
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001111
1112 unsigned FramePtr = RegInfo->getFrameRegister(MF);
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001113 unsigned Opc = MI->getOpcode();
Anton Korobeynikov7a764162011-03-05 18:43:43 +00001114 unsigned SrcReg, DstReg;
1115
Anton Korobeynikov3daccd82011-03-05 18:43:50 +00001116 if (Opc == ARM::tPUSH || Opc == ARM::tLDRpci) {
1117 // Two special cases:
1118 // 1) tPUSH does not have src/dst regs.
1119 // 2) for Thumb1 code we sometimes materialize the constant via constpool
1120 // load. Yes, this is pretty fragile, but for now I don't see better
1121 // way... :(
Anton Korobeynikov7a764162011-03-05 18:43:43 +00001122 SrcReg = DstReg = ARM::SP;
1123 } else {
Anton Korobeynikov3daccd82011-03-05 18:43:50 +00001124 SrcReg = MI->getOperand(1).getReg();
Anton Korobeynikov7a764162011-03-05 18:43:43 +00001125 DstReg = MI->getOperand(0).getReg();
1126 }
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001127
1128 // Try to figure out the unwinding opcode out of src / dst regs.
Evan Cheng5a96b3d2011-12-07 07:15:52 +00001129 if (MI->mayStore()) {
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001130 // Register saves.
1131 assert(DstReg == ARM::SP &&
1132 "Only stack pointer as a destination reg is supported");
1133
1134 SmallVector<unsigned, 4> RegList;
Anton Korobeynikov7a764162011-03-05 18:43:43 +00001135 // Skip src & dst reg, and pred ops.
1136 unsigned StartOp = 2 + 2;
1137 // Use all the operands.
1138 unsigned NumOffset = 0;
1139
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001140 switch (Opc) {
1141 default:
1142 MI->dump();
Craig Topperbc219812012-02-07 02:50:20 +00001143 llvm_unreachable("Unsupported opcode for unwinding information");
Anton Korobeynikov7a764162011-03-05 18:43:43 +00001144 case ARM::tPUSH:
1145 // Special case here: no src & dst reg, but two extra imp ops.
1146 StartOp = 2; NumOffset = 2;
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001147 case ARM::STMDB_UPD:
Anton Korobeynikov7a764162011-03-05 18:43:43 +00001148 case ARM::t2STMDB_UPD:
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001149 case ARM::VSTMDDB_UPD:
1150 assert(SrcReg == ARM::SP &&
1151 "Only stack pointer as a source reg is supported");
Anton Korobeynikov7a764162011-03-05 18:43:43 +00001152 for (unsigned i = StartOp, NumOps = MI->getNumOperands() - NumOffset;
Anton Korobeynikovad62e922012-08-04 13:25:58 +00001153 i != NumOps; ++i) {
1154 const MachineOperand &MO = MI->getOperand(i);
1155 // Actually, there should never be any impdef stuff here. Skip it
1156 // temporary to workaround PR11902.
1157 if (MO.isImplicit())
1158 continue;
1159 RegList.push_back(MO.getReg());
1160 }
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001161 break;
Owen Anderson793e7962011-07-26 20:54:26 +00001162 case ARM::STR_PRE_IMM:
1163 case ARM::STR_PRE_REG:
Evgeniy Stepanov73dd8bb2012-01-19 12:53:06 +00001164 case ARM::t2STR_PRE:
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001165 assert(MI->getOperand(2).getReg() == ARM::SP &&
1166 "Only stack pointer as a source reg is supported");
1167 RegList.push_back(SrcReg);
1168 break;
1169 }
1170 OutStreamer.EmitRegSave(RegList, Opc == ARM::VSTMDDB_UPD);
1171 } else {
1172 // Changes of stack / frame pointer.
1173 if (SrcReg == ARM::SP) {
1174 int64_t Offset = 0;
1175 switch (Opc) {
1176 default:
1177 MI->dump();
Craig Topperbc219812012-02-07 02:50:20 +00001178 llvm_unreachable("Unsupported opcode for unwinding information");
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001179 case ARM::MOVr:
Evgeniy Stepanov73dd8bb2012-01-19 12:53:06 +00001180 case ARM::tMOVr:
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001181 Offset = 0;
1182 break;
1183 case ARM::ADDri:
1184 Offset = -MI->getOperand(2).getImm();
1185 break;
1186 case ARM::SUBri:
Evgeniy Stepanov73dd8bb2012-01-19 12:53:06 +00001187 case ARM::t2SUBri:
Jim Grosbachf6fd9092011-06-29 23:25:04 +00001188 Offset = MI->getOperand(2).getImm();
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001189 break;
Anton Korobeynikov7a764162011-03-05 18:43:43 +00001190 case ARM::tSUBspi:
Jim Grosbachf6fd9092011-06-29 23:25:04 +00001191 Offset = MI->getOperand(2).getImm()*4;
Anton Korobeynikov7a764162011-03-05 18:43:43 +00001192 break;
1193 case ARM::tADDspi:
1194 case ARM::tADDrSPi:
1195 Offset = -MI->getOperand(2).getImm()*4;
1196 break;
Anton Korobeynikovb3fcc062011-03-05 18:43:55 +00001197 case ARM::tLDRpci: {
1198 // Grab the constpool index and check, whether it corresponds to
1199 // original or cloned constpool entry.
1200 unsigned CPI = MI->getOperand(1).getIndex();
1201 const MachineConstantPool *MCP = MF.getConstantPool();
1202 if (CPI >= MCP->getConstants().size())
1203 CPI = AFI.getOriginalCPIdx(CPI);
1204 assert(CPI != -1U && "Invalid constpool index");
1205
1206 // Derive the actual offset.
1207 const MachineConstantPoolEntry &CPE = MCP->getConstants()[CPI];
1208 assert(!CPE.isMachineConstantPoolEntry() && "Invalid constpool entry");
1209 // FIXME: Check for user, it should be "add" instruction!
1210 Offset = -cast<ConstantInt>(CPE.Val.ConstVal)->getSExtValue();
Anton Korobeynikov3daccd82011-03-05 18:43:50 +00001211 break;
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001212 }
Anton Korobeynikovb3fcc062011-03-05 18:43:55 +00001213 }
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001214
1215 if (DstReg == FramePtr && FramePtr != ARM::SP)
Anton Korobeynikove5163792011-03-05 18:44:00 +00001216 // Set-up of the frame pointer. Positive values correspond to "add"
1217 // instruction.
1218 OutStreamer.EmitSetFP(FramePtr, ARM::SP, -Offset);
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001219 else if (DstReg == ARM::SP) {
Anton Korobeynikove5163792011-03-05 18:44:00 +00001220 // Change of SP by an offset. Positive values correspond to "sub"
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001221 // instruction.
1222 OutStreamer.EmitPad(Offset);
1223 } else {
1224 MI->dump();
Craig Topperbc219812012-02-07 02:50:20 +00001225 llvm_unreachable("Unsupported opcode for unwinding information");
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001226 }
1227 } else if (DstReg == ARM::SP) {
1228 // FIXME: .movsp goes here
1229 MI->dump();
Craig Topperbc219812012-02-07 02:50:20 +00001230 llvm_unreachable("Unsupported opcode for unwinding information");
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001231 }
1232 else {
1233 MI->dump();
Craig Topperbc219812012-02-07 02:50:20 +00001234 llvm_unreachable("Unsupported opcode for unwinding information");
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001235 }
1236 }
1237}
1238
Chandler Carruth3eb4be02012-01-24 00:30:17 +00001239extern cl::opt<bool> EnableARMEHABI;
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001240
Jim Grosbach53e3fc42011-07-08 17:40:42 +00001241// Simple pseudo-instructions have their lowering (with expansion to real
1242// instructions) auto-generated.
1243#include "ARMGenMCPseudoLowering.inc"
1244
Jim Grosbachb454cda2010-09-29 15:23:40 +00001245void ARMAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Jim Grosbach3e965312012-05-18 19:12:01 +00001246 // If we just ended a constant pool, mark it as such.
1247 if (InConstantPool && MI->getOpcode() != ARM::CONSTPOOL_ENTRY) {
1248 OutStreamer.EmitDataRegion(MCDR_DataRegionEnd);
1249 InConstantPool = false;
1250 }
Owen Anderson2fec6c52011-10-04 23:26:17 +00001251
Jim Grosbach5aa29a02011-08-23 21:32:34 +00001252 // Emit unwinding stuff for frame-related instructions
Chandler Carruth3eb4be02012-01-24 00:30:17 +00001253 if (EnableARMEHABI && MI->getFlag(MachineInstr::FrameSetup))
Jim Grosbach5aa29a02011-08-23 21:32:34 +00001254 EmitUnwindingInstruction(MI);
1255
Jim Grosbach53e3fc42011-07-08 17:40:42 +00001256 // Do any auto-generated pseudo lowerings.
1257 if (emitPseudoExpansionLowering(OutStreamer, MI))
1258 return;
1259
Andrew Trick3be654f2011-09-21 02:20:46 +00001260 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
1261 "Pseudo flag setting opcode should be expanded early");
1262
Jim Grosbach53e3fc42011-07-08 17:40:42 +00001263 // Check for manual lowerings.
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001264 unsigned Opc = MI->getOpcode();
1265 switch (Opc) {
Craig Topperbc219812012-02-07 02:50:20 +00001266 case ARM::t2MOVi32imm: llvm_unreachable("Should be lowered by thumb2it pass");
Jim Grosbach2d0f53b2010-09-28 17:05:56 +00001267 case ARM::DBG_VALUE: {
1268 if (isVerbose() && OutStreamer.hasRawTextSupport()) {
1269 SmallString<128> TmpStr;
1270 raw_svector_ostream OS(TmpStr);
1271 PrintDebugValueComment(MI, OS);
1272 OutStreamer.EmitRawText(StringRef(OS.str()));
1273 }
1274 return;
1275 }
Jim Grosbach40edf732010-12-14 21:10:47 +00001276 case ARM::LEApcrel:
Jim Grosbachd40963c2010-12-14 22:28:03 +00001277 case ARM::tLEApcrel:
Jim Grosbach40edf732010-12-14 21:10:47 +00001278 case ARM::t2LEApcrel: {
Jim Grosbachdff84b02010-12-02 00:28:45 +00001279 // FIXME: Need to also handle globals and externals
Jim Grosbachdff84b02010-12-02 00:28:45 +00001280 MCInst TmpInst;
Jim Grosbachd40963c2010-12-14 22:28:03 +00001281 TmpInst.setOpcode(MI->getOpcode() == ARM::t2LEApcrel ? ARM::t2ADR
1282 : (MI->getOpcode() == ARM::tLEApcrel ? ARM::tADR
1283 : ARM::ADR));
Jim Grosbach40edf732010-12-14 21:10:47 +00001284 populateADROperands(TmpInst, MI->getOperand(0).getReg(),
1285 GetCPISymbol(MI->getOperand(1).getIndex()),
1286 MI->getOperand(2).getImm(), MI->getOperand(3).getReg(),
1287 OutContext);
Jim Grosbachdff84b02010-12-02 00:28:45 +00001288 OutStreamer.EmitInstruction(TmpInst);
1289 return;
1290 }
Jim Grosbachd40963c2010-12-14 22:28:03 +00001291 case ARM::LEApcrelJT:
1292 case ARM::tLEApcrelJT:
1293 case ARM::t2LEApcrelJT: {
Jim Grosbach5d14f9b2010-12-01 19:47:31 +00001294 MCInst TmpInst;
Jim Grosbachd40963c2010-12-14 22:28:03 +00001295 TmpInst.setOpcode(MI->getOpcode() == ARM::t2LEApcrelJT ? ARM::t2ADR
1296 : (MI->getOpcode() == ARM::tLEApcrelJT ? ARM::tADR
1297 : ARM::ADR));
Jim Grosbach40edf732010-12-14 21:10:47 +00001298 populateADROperands(TmpInst, MI->getOperand(0).getReg(),
1299 GetARMJTIPICJumpTableLabel2(MI->getOperand(1).getIndex(),
1300 MI->getOperand(2).getImm()),
1301 MI->getOperand(3).getImm(), MI->getOperand(4).getReg(),
1302 OutContext);
Jim Grosbach5d14f9b2010-12-01 19:47:31 +00001303 OutStreamer.EmitInstruction(TmpInst);
1304 return;
1305 }
Jim Grosbachf859a542011-03-12 00:45:26 +00001306 // Darwin call instructions are just normal call instructions with different
1307 // clobber semantics (they clobber R9).
Jim Grosbacha0d2c8a2010-11-30 18:30:19 +00001308 case ARM::BX_CALL: {
1309 {
1310 MCInst TmpInst;
1311 TmpInst.setOpcode(ARM::MOVr);
1312 TmpInst.addOperand(MCOperand::CreateReg(ARM::LR));
1313 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1314 // Add predicate operands.
1315 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1316 TmpInst.addOperand(MCOperand::CreateReg(0));
1317 // Add 's' bit operand (always reg0 for this)
1318 TmpInst.addOperand(MCOperand::CreateReg(0));
1319 OutStreamer.EmitInstruction(TmpInst);
1320 }
1321 {
1322 MCInst TmpInst;
1323 TmpInst.setOpcode(ARM::BX);
1324 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1325 OutStreamer.EmitInstruction(TmpInst);
1326 }
1327 return;
1328 }
Cameron Zwarichad70f6d2011-05-25 21:53:50 +00001329 case ARM::tBX_CALL: {
1330 {
1331 MCInst TmpInst;
1332 TmpInst.setOpcode(ARM::tMOVr);
1333 TmpInst.addOperand(MCOperand::CreateReg(ARM::LR));
1334 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
Jim Grosbach63b46fa2011-06-30 22:10:46 +00001335 // Add predicate operands.
1336 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1337 TmpInst.addOperand(MCOperand::CreateReg(0));
Cameron Zwarichad70f6d2011-05-25 21:53:50 +00001338 OutStreamer.EmitInstruction(TmpInst);
1339 }
1340 {
1341 MCInst TmpInst;
1342 TmpInst.setOpcode(ARM::tBX);
1343 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1344 // Add predicate operands.
1345 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1346 TmpInst.addOperand(MCOperand::CreateReg(0));
1347 OutStreamer.EmitInstruction(TmpInst);
1348 }
1349 return;
1350 }
Jim Grosbacha0d2c8a2010-11-30 18:30:19 +00001351 case ARM::BMOVPCRX_CALL: {
1352 {
1353 MCInst TmpInst;
1354 TmpInst.setOpcode(ARM::MOVr);
1355 TmpInst.addOperand(MCOperand::CreateReg(ARM::LR));
1356 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1357 // Add predicate operands.
1358 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1359 TmpInst.addOperand(MCOperand::CreateReg(0));
1360 // Add 's' bit operand (always reg0 for this)
1361 TmpInst.addOperand(MCOperand::CreateReg(0));
1362 OutStreamer.EmitInstruction(TmpInst);
1363 }
1364 {
1365 MCInst TmpInst;
1366 TmpInst.setOpcode(ARM::MOVr);
1367 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1368 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1369 // Add predicate operands.
1370 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1371 TmpInst.addOperand(MCOperand::CreateReg(0));
1372 // Add 's' bit operand (always reg0 for this)
1373 TmpInst.addOperand(MCOperand::CreateReg(0));
1374 OutStreamer.EmitInstruction(TmpInst);
1375 }
1376 return;
1377 }
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001378 case ARM::BMOVPCB_CALL: {
1379 {
1380 MCInst TmpInst;
1381 TmpInst.setOpcode(ARM::MOVr);
1382 TmpInst.addOperand(MCOperand::CreateReg(ARM::LR));
1383 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1384 // Add predicate operands.
1385 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1386 TmpInst.addOperand(MCOperand::CreateReg(0));
1387 // Add 's' bit operand (always reg0 for this)
1388 TmpInst.addOperand(MCOperand::CreateReg(0));
1389 OutStreamer.EmitInstruction(TmpInst);
1390 }
1391 {
1392 MCInst TmpInst;
1393 TmpInst.setOpcode(ARM::Bcc);
1394 const GlobalValue *GV = MI->getOperand(0).getGlobal();
1395 MCSymbol *GVSym = Mang->getSymbol(GV);
1396 const MCExpr *GVSymExpr = MCSymbolRefExpr::Create(GVSym, OutContext);
1397 TmpInst.addOperand(MCOperand::CreateExpr(GVSymExpr));
1398 // Add predicate operands.
1399 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1400 TmpInst.addOperand(MCOperand::CreateReg(0));
1401 OutStreamer.EmitInstruction(TmpInst);
1402 }
1403 return;
1404 }
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001405 case ARM::t2BMOVPCB_CALL: {
1406 {
1407 MCInst TmpInst;
1408 TmpInst.setOpcode(ARM::tMOVr);
1409 TmpInst.addOperand(MCOperand::CreateReg(ARM::LR));
1410 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1411 // Add predicate operands.
1412 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1413 TmpInst.addOperand(MCOperand::CreateReg(0));
1414 OutStreamer.EmitInstruction(TmpInst);
1415 }
1416 {
1417 MCInst TmpInst;
1418 TmpInst.setOpcode(ARM::t2B);
1419 const GlobalValue *GV = MI->getOperand(0).getGlobal();
1420 MCSymbol *GVSym = Mang->getSymbol(GV);
1421 const MCExpr *GVSymExpr = MCSymbolRefExpr::Create(GVSym, OutContext);
1422 TmpInst.addOperand(MCOperand::CreateExpr(GVSymExpr));
1423 // Add predicate operands.
1424 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1425 TmpInst.addOperand(MCOperand::CreateReg(0));
1426 OutStreamer.EmitInstruction(TmpInst);
1427 }
1428 return;
1429 }
Evan Cheng53519f02011-01-21 18:55:51 +00001430 case ARM::MOVi16_ga_pcrel:
1431 case ARM::t2MOVi16_ga_pcrel: {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001432 MCInst TmpInst;
Evan Cheng53519f02011-01-21 18:55:51 +00001433 TmpInst.setOpcode(Opc == ARM::MOVi16_ga_pcrel? ARM::MOVi16 : ARM::t2MOVi16);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001434 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1435
Evan Cheng53519f02011-01-21 18:55:51 +00001436 unsigned TF = MI->getOperand(1).getTargetFlags();
1437 bool isPIC = TF == ARMII::MO_LO16_NONLAZY_PIC;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001438 const GlobalValue *GV = MI->getOperand(1).getGlobal();
1439 MCSymbol *GVSym = GetARMGVSymbol(GV);
1440 const MCExpr *GVSymExpr = MCSymbolRefExpr::Create(GVSym, OutContext);
Evan Cheng53519f02011-01-21 18:55:51 +00001441 if (isPIC) {
1442 MCSymbol *LabelSym = getPICLabel(MAI->getPrivateGlobalPrefix(),
1443 getFunctionNumber(),
1444 MI->getOperand(2).getImm(), OutContext);
1445 const MCExpr *LabelSymExpr= MCSymbolRefExpr::Create(LabelSym, OutContext);
1446 unsigned PCAdj = (Opc == ARM::MOVi16_ga_pcrel) ? 8 : 4;
1447 const MCExpr *PCRelExpr =
1448 ARMMCExpr::CreateLower16(MCBinaryExpr::CreateSub(GVSymExpr,
1449 MCBinaryExpr::CreateAdd(LabelSymExpr,
1450 MCConstantExpr::Create(PCAdj, OutContext),
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001451 OutContext), OutContext), OutContext);
Evan Cheng53519f02011-01-21 18:55:51 +00001452 TmpInst.addOperand(MCOperand::CreateExpr(PCRelExpr));
1453 } else {
1454 const MCExpr *RefExpr= ARMMCExpr::CreateLower16(GVSymExpr, OutContext);
1455 TmpInst.addOperand(MCOperand::CreateExpr(RefExpr));
1456 }
1457
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001458 // Add predicate operands.
1459 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1460 TmpInst.addOperand(MCOperand::CreateReg(0));
1461 // Add 's' bit operand (always reg0 for this)
1462 TmpInst.addOperand(MCOperand::CreateReg(0));
1463 OutStreamer.EmitInstruction(TmpInst);
1464 return;
1465 }
Evan Cheng53519f02011-01-21 18:55:51 +00001466 case ARM::MOVTi16_ga_pcrel:
1467 case ARM::t2MOVTi16_ga_pcrel: {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001468 MCInst TmpInst;
Evan Cheng53519f02011-01-21 18:55:51 +00001469 TmpInst.setOpcode(Opc == ARM::MOVTi16_ga_pcrel
1470 ? ARM::MOVTi16 : ARM::t2MOVTi16);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001471 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1472 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
1473
Evan Cheng53519f02011-01-21 18:55:51 +00001474 unsigned TF = MI->getOperand(2).getTargetFlags();
1475 bool isPIC = TF == ARMII::MO_HI16_NONLAZY_PIC;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001476 const GlobalValue *GV = MI->getOperand(2).getGlobal();
1477 MCSymbol *GVSym = GetARMGVSymbol(GV);
1478 const MCExpr *GVSymExpr = MCSymbolRefExpr::Create(GVSym, OutContext);
Evan Cheng53519f02011-01-21 18:55:51 +00001479 if (isPIC) {
1480 MCSymbol *LabelSym = getPICLabel(MAI->getPrivateGlobalPrefix(),
1481 getFunctionNumber(),
1482 MI->getOperand(3).getImm(), OutContext);
1483 const MCExpr *LabelSymExpr= MCSymbolRefExpr::Create(LabelSym, OutContext);
1484 unsigned PCAdj = (Opc == ARM::MOVTi16_ga_pcrel) ? 8 : 4;
1485 const MCExpr *PCRelExpr =
1486 ARMMCExpr::CreateUpper16(MCBinaryExpr::CreateSub(GVSymExpr,
1487 MCBinaryExpr::CreateAdd(LabelSymExpr,
1488 MCConstantExpr::Create(PCAdj, OutContext),
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001489 OutContext), OutContext), OutContext);
Evan Cheng53519f02011-01-21 18:55:51 +00001490 TmpInst.addOperand(MCOperand::CreateExpr(PCRelExpr));
1491 } else {
1492 const MCExpr *RefExpr= ARMMCExpr::CreateUpper16(GVSymExpr, OutContext);
1493 TmpInst.addOperand(MCOperand::CreateExpr(RefExpr));
1494 }
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001495 // Add predicate operands.
1496 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1497 TmpInst.addOperand(MCOperand::CreateReg(0));
1498 // Add 's' bit operand (always reg0 for this)
1499 TmpInst.addOperand(MCOperand::CreateReg(0));
1500 OutStreamer.EmitInstruction(TmpInst);
1501 return;
1502 }
Jim Grosbachfbd18732010-09-17 23:41:53 +00001503 case ARM::tPICADD: {
1504 // This is a pseudo op for a label + instruction sequence, which looks like:
1505 // LPC0:
1506 // add r0, pc
1507 // This adds the address of LPC0 to r0.
1508
1509 // Emit the label.
Jim Grosbach988ce092010-09-18 00:05:05 +00001510 OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(),
1511 getFunctionNumber(), MI->getOperand(2).getImm(),
1512 OutContext));
Jim Grosbachfbd18732010-09-17 23:41:53 +00001513
1514 // Form and emit the add.
1515 MCInst AddInst;
1516 AddInst.setOpcode(ARM::tADDhirr);
1517 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1518 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1519 AddInst.addOperand(MCOperand::CreateReg(ARM::PC));
1520 // Add predicate operands.
1521 AddInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1522 AddInst.addOperand(MCOperand::CreateReg(0));
1523 OutStreamer.EmitInstruction(AddInst);
1524 return;
1525 }
Jim Grosbacha3fbadf2010-09-30 19:53:58 +00001526 case ARM::PICADD: {
Chris Lattner4d152222009-10-19 22:23:04 +00001527 // This is a pseudo op for a label + instruction sequence, which looks like:
1528 // LPC0:
1529 // add r0, pc, r0
1530 // This adds the address of LPC0 to r0.
Jim Grosbachb0739b72010-09-02 01:02:06 +00001531
Chris Lattner4d152222009-10-19 22:23:04 +00001532 // Emit the label.
Jim Grosbach988ce092010-09-18 00:05:05 +00001533 OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(),
1534 getFunctionNumber(), MI->getOperand(2).getImm(),
1535 OutContext));
Jim Grosbachb0739b72010-09-02 01:02:06 +00001536
Jim Grosbachf3f09522010-09-14 21:05:34 +00001537 // Form and emit the add.
Chris Lattner4d152222009-10-19 22:23:04 +00001538 MCInst AddInst;
1539 AddInst.setOpcode(ARM::ADDrr);
1540 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1541 AddInst.addOperand(MCOperand::CreateReg(ARM::PC));
1542 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
Jim Grosbach5b46d622010-09-14 21:28:17 +00001543 // Add predicate operands.
1544 AddInst.addOperand(MCOperand::CreateImm(MI->getOperand(3).getImm()));
1545 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(4).getReg()));
1546 // Add 's' bit operand (always reg0 for this)
1547 AddInst.addOperand(MCOperand::CreateReg(0));
Chris Lattner850d2e22010-02-03 01:16:28 +00001548 OutStreamer.EmitInstruction(AddInst);
Chris Lattner4d152222009-10-19 22:23:04 +00001549 return;
1550 }
Jim Grosbacha28abbe2010-09-17 16:25:52 +00001551 case ARM::PICSTR:
1552 case ARM::PICSTRB:
1553 case ARM::PICSTRH:
1554 case ARM::PICLDR:
1555 case ARM::PICLDRB:
1556 case ARM::PICLDRH:
1557 case ARM::PICLDRSB:
1558 case ARM::PICLDRSH: {
Jim Grosbachb74ca9d2010-09-16 17:43:25 +00001559 // This is a pseudo op for a label + instruction sequence, which looks like:
1560 // LPC0:
Jim Grosbacha28abbe2010-09-17 16:25:52 +00001561 // OP r0, [pc, r0]
Jim Grosbachb74ca9d2010-09-16 17:43:25 +00001562 // The LCP0 label is referenced by a constant pool entry in order to get
1563 // a PC-relative address at the ldr instruction.
1564
1565 // Emit the label.
Jim Grosbach988ce092010-09-18 00:05:05 +00001566 OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(),
1567 getFunctionNumber(), MI->getOperand(2).getImm(),
1568 OutContext));
Jim Grosbachb74ca9d2010-09-16 17:43:25 +00001569
1570 // Form and emit the load
Jim Grosbacha28abbe2010-09-17 16:25:52 +00001571 unsigned Opcode;
1572 switch (MI->getOpcode()) {
1573 default:
1574 llvm_unreachable("Unexpected opcode!");
Jim Grosbach7e3383c2010-10-27 23:12:14 +00001575 case ARM::PICSTR: Opcode = ARM::STRrs; break;
1576 case ARM::PICSTRB: Opcode = ARM::STRBrs; break;
Jim Grosbacha28abbe2010-09-17 16:25:52 +00001577 case ARM::PICSTRH: Opcode = ARM::STRH; break;
Jim Grosbach3e556122010-10-26 22:37:02 +00001578 case ARM::PICLDR: Opcode = ARM::LDRrs; break;
Jim Grosbachc1d30212010-10-27 00:19:44 +00001579 case ARM::PICLDRB: Opcode = ARM::LDRBrs; break;
Jim Grosbacha28abbe2010-09-17 16:25:52 +00001580 case ARM::PICLDRH: Opcode = ARM::LDRH; break;
1581 case ARM::PICLDRSB: Opcode = ARM::LDRSB; break;
1582 case ARM::PICLDRSH: Opcode = ARM::LDRSH; break;
1583 }
1584 MCInst LdStInst;
1585 LdStInst.setOpcode(Opcode);
1586 LdStInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1587 LdStInst.addOperand(MCOperand::CreateReg(ARM::PC));
1588 LdStInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
1589 LdStInst.addOperand(MCOperand::CreateImm(0));
Jim Grosbachb74ca9d2010-09-16 17:43:25 +00001590 // Add predicate operands.
Jim Grosbacha28abbe2010-09-17 16:25:52 +00001591 LdStInst.addOperand(MCOperand::CreateImm(MI->getOperand(3).getImm()));
1592 LdStInst.addOperand(MCOperand::CreateReg(MI->getOperand(4).getReg()));
1593 OutStreamer.EmitInstruction(LdStInst);
Jim Grosbachb74ca9d2010-09-16 17:43:25 +00001594
1595 return;
1596 }
Jim Grosbacha3fbadf2010-09-30 19:53:58 +00001597 case ARM::CONSTPOOL_ENTRY: {
Chris Lattnera70e6442009-10-19 22:33:05 +00001598 /// CONSTPOOL_ENTRY - This instruction represents a floating constant pool
1599 /// in the function. The first operand is the ID# for this instruction, the
1600 /// second is the index into the MachineConstantPool that this is, the third
1601 /// is the size in bytes of this constant pool entry.
Jakob Stoklund Olesen3e572ac2011-12-06 01:43:02 +00001602 /// The required alignment is specified on the basic block holding this MI.
Chris Lattnera70e6442009-10-19 22:33:05 +00001603 unsigned LabelId = (unsigned)MI->getOperand(0).getImm();
1604 unsigned CPIdx = (unsigned)MI->getOperand(1).getIndex();
1605
Jim Grosbach3e965312012-05-18 19:12:01 +00001606 // If this is the first entry of the pool, mark it.
1607 if (!InConstantPool) {
1608 OutStreamer.EmitDataRegion(MCDR_DataRegion);
1609 InConstantPool = true;
1610 }
1611
Chris Lattner1b46f432010-01-23 07:00:21 +00001612 OutStreamer.EmitLabel(GetCPISymbol(LabelId));
Chris Lattnera70e6442009-10-19 22:33:05 +00001613
1614 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPIdx];
1615 if (MCPE.isMachineConstantPoolEntry())
1616 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
1617 else
1618 EmitGlobalConstant(MCPE.Val.ConstVal);
Chris Lattnera70e6442009-10-19 22:33:05 +00001619 return;
1620 }
Jim Grosbach882ef2b2010-09-21 23:28:16 +00001621 case ARM::t2BR_JT: {
1622 // Lower and emit the instruction itself, then the jump table following it.
1623 MCInst TmpInst;
Jim Grosbach2a7b41b2011-06-30 23:38:17 +00001624 TmpInst.setOpcode(ARM::tMOVr);
Jim Grosbach5ca66692010-11-29 22:37:40 +00001625 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1626 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1627 // Add predicate operands.
1628 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1629 TmpInst.addOperand(MCOperand::CreateReg(0));
Jim Grosbach882ef2b2010-09-21 23:28:16 +00001630 OutStreamer.EmitInstruction(TmpInst);
Jim Grosbach5ca66692010-11-29 22:37:40 +00001631 // Output the data for the jump table itself
1632 EmitJump2Table(MI);
1633 return;
1634 }
1635 case ARM::t2TBB_JT: {
1636 // Lower and emit the instruction itself, then the jump table following it.
1637 MCInst TmpInst;
1638
1639 TmpInst.setOpcode(ARM::t2TBB);
1640 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1641 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1642 // Add predicate operands.
1643 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1644 TmpInst.addOperand(MCOperand::CreateReg(0));
1645 OutStreamer.EmitInstruction(TmpInst);
1646 // Output the data for the jump table itself
1647 EmitJump2Table(MI);
1648 // Make sure the next instruction is 2-byte aligned.
1649 EmitAlignment(1);
1650 return;
1651 }
1652 case ARM::t2TBH_JT: {
1653 // Lower and emit the instruction itself, then the jump table following it.
1654 MCInst TmpInst;
1655
1656 TmpInst.setOpcode(ARM::t2TBH);
1657 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1658 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1659 // Add predicate operands.
1660 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1661 TmpInst.addOperand(MCOperand::CreateReg(0));
1662 OutStreamer.EmitInstruction(TmpInst);
1663 // Output the data for the jump table itself
Jim Grosbach882ef2b2010-09-21 23:28:16 +00001664 EmitJump2Table(MI);
1665 return;
1666 }
Jim Grosbachf1aa47d2010-11-29 19:32:47 +00001667 case ARM::tBR_JTr:
Jim Grosbach2dc77682010-11-29 18:37:44 +00001668 case ARM::BR_JTr: {
1669 // Lower and emit the instruction itself, then the jump table following it.
1670 // mov pc, target
1671 MCInst TmpInst;
Jim Grosbach5ca66692010-11-29 22:37:40 +00001672 unsigned Opc = MI->getOpcode() == ARM::BR_JTr ?
Jim Grosbach2a7b41b2011-06-30 23:38:17 +00001673 ARM::MOVr : ARM::tMOVr;
Jim Grosbachf1aa47d2010-11-29 19:32:47 +00001674 TmpInst.setOpcode(Opc);
Jim Grosbach2dc77682010-11-29 18:37:44 +00001675 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1676 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1677 // Add predicate operands.
1678 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1679 TmpInst.addOperand(MCOperand::CreateReg(0));
Jim Grosbacha0d2c8a2010-11-30 18:30:19 +00001680 // Add 's' bit operand (always reg0 for this)
1681 if (Opc == ARM::MOVr)
1682 TmpInst.addOperand(MCOperand::CreateReg(0));
Jim Grosbach2dc77682010-11-29 18:37:44 +00001683 OutStreamer.EmitInstruction(TmpInst);
1684
Jim Grosbachf1aa47d2010-11-29 19:32:47 +00001685 // Make sure the Thumb jump table is 4-byte aligned.
Jim Grosbach2a7b41b2011-06-30 23:38:17 +00001686 if (Opc == ARM::tMOVr)
Jim Grosbachf1aa47d2010-11-29 19:32:47 +00001687 EmitAlignment(2);
1688
Jim Grosbach2dc77682010-11-29 18:37:44 +00001689 // Output the data for the jump table itself
1690 EmitJumpTable(MI);
1691 return;
1692 }
1693 case ARM::BR_JTm: {
1694 // Lower and emit the instruction itself, then the jump table following it.
1695 // ldr pc, target
1696 MCInst TmpInst;
1697 if (MI->getOperand(1).getReg() == 0) {
1698 // literal offset
1699 TmpInst.setOpcode(ARM::LDRi12);
1700 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1701 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1702 TmpInst.addOperand(MCOperand::CreateImm(MI->getOperand(2).getImm()));
1703 } else {
1704 TmpInst.setOpcode(ARM::LDRrs);
1705 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1706 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1707 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
1708 TmpInst.addOperand(MCOperand::CreateImm(0));
1709 }
1710 // Add predicate operands.
1711 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1712 TmpInst.addOperand(MCOperand::CreateReg(0));
1713 OutStreamer.EmitInstruction(TmpInst);
1714
1715 // Output the data for the jump table itself
Jim Grosbacha2244cb2010-09-22 17:39:48 +00001716 EmitJumpTable(MI);
1717 return;
1718 }
Jim Grosbachf8dabac2010-11-17 21:05:55 +00001719 case ARM::BR_JTadd: {
1720 // Lower and emit the instruction itself, then the jump table following it.
1721 // add pc, target, idx
Jim Grosbach2dc77682010-11-29 18:37:44 +00001722 MCInst TmpInst;
1723 TmpInst.setOpcode(ARM::ADDrr);
1724 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1725 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1726 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
Jim Grosbachf8dabac2010-11-17 21:05:55 +00001727 // Add predicate operands.
Jim Grosbach2dc77682010-11-29 18:37:44 +00001728 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1729 TmpInst.addOperand(MCOperand::CreateReg(0));
Jim Grosbachf8dabac2010-11-17 21:05:55 +00001730 // Add 's' bit operand (always reg0 for this)
Jim Grosbach2dc77682010-11-29 18:37:44 +00001731 TmpInst.addOperand(MCOperand::CreateReg(0));
1732 OutStreamer.EmitInstruction(TmpInst);
Jim Grosbachf8dabac2010-11-17 21:05:55 +00001733
1734 // Output the data for the jump table itself
1735 EmitJumpTable(MI);
1736 return;
1737 }
Jim Grosbach2e6ae132010-09-23 18:05:37 +00001738 case ARM::TRAP: {
1739 // Non-Darwin binutils don't yet support the "trap" mnemonic.
1740 // FIXME: Remove this special case when they do.
1741 if (!Subtarget->isTargetDarwin()) {
Jim Grosbach78890f42010-10-01 23:21:38 +00001742 //.long 0xe7ffdefe @ trap
Jim Grosbachb2dda4b2010-09-23 19:42:17 +00001743 uint32_t Val = 0xe7ffdefeUL;
Jim Grosbach2e6ae132010-09-23 18:05:37 +00001744 OutStreamer.AddComment("trap");
1745 OutStreamer.EmitIntValue(Val, 4);
1746 return;
1747 }
1748 break;
1749 }
1750 case ARM::tTRAP: {
1751 // Non-Darwin binutils don't yet support the "trap" mnemonic.
1752 // FIXME: Remove this special case when they do.
1753 if (!Subtarget->isTargetDarwin()) {
Jim Grosbach78890f42010-10-01 23:21:38 +00001754 //.short 57086 @ trap
Benjamin Kramerc8ab9eb2010-09-23 18:57:26 +00001755 uint16_t Val = 0xdefe;
Jim Grosbach2e6ae132010-09-23 18:05:37 +00001756 OutStreamer.AddComment("trap");
1757 OutStreamer.EmitIntValue(Val, 2);
1758 return;
1759 }
1760 break;
1761 }
Jim Grosbach433a5782010-09-24 20:47:58 +00001762 case ARM::t2Int_eh_sjlj_setjmp:
1763 case ARM::t2Int_eh_sjlj_setjmp_nofp:
Jim Grosbacha3fbadf2010-09-30 19:53:58 +00001764 case ARM::tInt_eh_sjlj_setjmp: {
Jim Grosbach433a5782010-09-24 20:47:58 +00001765 // Two incoming args: GPR:$src, GPR:$val
1766 // mov $val, pc
1767 // adds $val, #7
1768 // str $val, [$src, #4]
1769 // movs r0, #0
1770 // b 1f
1771 // movs r0, #1
1772 // 1:
1773 unsigned SrcReg = MI->getOperand(0).getReg();
1774 unsigned ValReg = MI->getOperand(1).getReg();
1775 MCSymbol *Label = GetARMSJLJEHLabel();
1776 {
1777 MCInst TmpInst;
Jim Grosbach2a7b41b2011-06-30 23:38:17 +00001778 TmpInst.setOpcode(ARM::tMOVr);
Jim Grosbach433a5782010-09-24 20:47:58 +00001779 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1780 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
Jim Grosbach63b46fa2011-06-30 22:10:46 +00001781 // Predicate.
1782 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1783 TmpInst.addOperand(MCOperand::CreateReg(0));
Jim Grosbach433a5782010-09-24 20:47:58 +00001784 OutStreamer.AddComment("eh_setjmp begin");
1785 OutStreamer.EmitInstruction(TmpInst);
1786 }
1787 {
1788 MCInst TmpInst;
1789 TmpInst.setOpcode(ARM::tADDi3);
1790 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1791 // 's' bit operand
1792 TmpInst.addOperand(MCOperand::CreateReg(ARM::CPSR));
1793 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1794 TmpInst.addOperand(MCOperand::CreateImm(7));
1795 // Predicate.
1796 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1797 TmpInst.addOperand(MCOperand::CreateReg(0));
1798 OutStreamer.EmitInstruction(TmpInst);
1799 }
1800 {
1801 MCInst TmpInst;
Bill Wendlingf4caf692010-12-14 03:36:38 +00001802 TmpInst.setOpcode(ARM::tSTRi);
Jim Grosbach433a5782010-09-24 20:47:58 +00001803 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1804 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
1805 // The offset immediate is #4. The operand value is scaled by 4 for the
1806 // tSTR instruction.
1807 TmpInst.addOperand(MCOperand::CreateImm(1));
Jim Grosbach433a5782010-09-24 20:47:58 +00001808 // Predicate.
1809 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1810 TmpInst.addOperand(MCOperand::CreateReg(0));
1811 OutStreamer.EmitInstruction(TmpInst);
1812 }
1813 {
1814 MCInst TmpInst;
1815 TmpInst.setOpcode(ARM::tMOVi8);
1816 TmpInst.addOperand(MCOperand::CreateReg(ARM::R0));
1817 TmpInst.addOperand(MCOperand::CreateReg(ARM::CPSR));
1818 TmpInst.addOperand(MCOperand::CreateImm(0));
1819 // Predicate.
1820 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1821 TmpInst.addOperand(MCOperand::CreateReg(0));
1822 OutStreamer.EmitInstruction(TmpInst);
1823 }
1824 {
1825 const MCExpr *SymbolExpr = MCSymbolRefExpr::Create(Label, OutContext);
1826 MCInst TmpInst;
1827 TmpInst.setOpcode(ARM::tB);
1828 TmpInst.addOperand(MCOperand::CreateExpr(SymbolExpr));
Owen Anderson51f6a7a2011-09-09 21:48:23 +00001829 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1830 TmpInst.addOperand(MCOperand::CreateReg(0));
Jim Grosbach433a5782010-09-24 20:47:58 +00001831 OutStreamer.EmitInstruction(TmpInst);
1832 }
1833 {
1834 MCInst TmpInst;
1835 TmpInst.setOpcode(ARM::tMOVi8);
1836 TmpInst.addOperand(MCOperand::CreateReg(ARM::R0));
1837 TmpInst.addOperand(MCOperand::CreateReg(ARM::CPSR));
1838 TmpInst.addOperand(MCOperand::CreateImm(1));
1839 // Predicate.
1840 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1841 TmpInst.addOperand(MCOperand::CreateReg(0));
1842 OutStreamer.AddComment("eh_setjmp end");
1843 OutStreamer.EmitInstruction(TmpInst);
1844 }
1845 OutStreamer.EmitLabel(Label);
1846 return;
1847 }
1848
Jim Grosbach45390082010-09-23 23:33:56 +00001849 case ARM::Int_eh_sjlj_setjmp_nofp:
Jim Grosbacha3fbadf2010-09-30 19:53:58 +00001850 case ARM::Int_eh_sjlj_setjmp: {
Jim Grosbach45390082010-09-23 23:33:56 +00001851 // Two incoming args: GPR:$src, GPR:$val
1852 // add $val, pc, #8
1853 // str $val, [$src, #+4]
1854 // mov r0, #0
1855 // add pc, pc, #0
1856 // mov r0, #1
1857 unsigned SrcReg = MI->getOperand(0).getReg();
1858 unsigned ValReg = MI->getOperand(1).getReg();
1859
1860 {
1861 MCInst TmpInst;
1862 TmpInst.setOpcode(ARM::ADDri);
1863 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1864 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1865 TmpInst.addOperand(MCOperand::CreateImm(8));
1866 // Predicate.
1867 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1868 TmpInst.addOperand(MCOperand::CreateReg(0));
1869 // 's' bit operand (always reg0 for this).
1870 TmpInst.addOperand(MCOperand::CreateReg(0));
1871 OutStreamer.AddComment("eh_setjmp begin");
1872 OutStreamer.EmitInstruction(TmpInst);
1873 }
1874 {
1875 MCInst TmpInst;
Jim Grosbach7e3383c2010-10-27 23:12:14 +00001876 TmpInst.setOpcode(ARM::STRi12);
Jim Grosbach45390082010-09-23 23:33:56 +00001877 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1878 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
Jim Grosbach45390082010-09-23 23:33:56 +00001879 TmpInst.addOperand(MCOperand::CreateImm(4));
1880 // Predicate.
1881 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1882 TmpInst.addOperand(MCOperand::CreateReg(0));
1883 OutStreamer.EmitInstruction(TmpInst);
1884 }
1885 {
1886 MCInst TmpInst;
1887 TmpInst.setOpcode(ARM::MOVi);
1888 TmpInst.addOperand(MCOperand::CreateReg(ARM::R0));
1889 TmpInst.addOperand(MCOperand::CreateImm(0));
1890 // Predicate.
1891 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1892 TmpInst.addOperand(MCOperand::CreateReg(0));
1893 // 's' bit operand (always reg0 for this).
1894 TmpInst.addOperand(MCOperand::CreateReg(0));
1895 OutStreamer.EmitInstruction(TmpInst);
1896 }
1897 {
1898 MCInst TmpInst;
1899 TmpInst.setOpcode(ARM::ADDri);
1900 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1901 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1902 TmpInst.addOperand(MCOperand::CreateImm(0));
1903 // Predicate.
1904 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1905 TmpInst.addOperand(MCOperand::CreateReg(0));
1906 // 's' bit operand (always reg0 for this).
1907 TmpInst.addOperand(MCOperand::CreateReg(0));
1908 OutStreamer.EmitInstruction(TmpInst);
1909 }
1910 {
1911 MCInst TmpInst;
1912 TmpInst.setOpcode(ARM::MOVi);
1913 TmpInst.addOperand(MCOperand::CreateReg(ARM::R0));
1914 TmpInst.addOperand(MCOperand::CreateImm(1));
1915 // Predicate.
1916 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1917 TmpInst.addOperand(MCOperand::CreateReg(0));
1918 // 's' bit operand (always reg0 for this).
1919 TmpInst.addOperand(MCOperand::CreateReg(0));
1920 OutStreamer.AddComment("eh_setjmp end");
1921 OutStreamer.EmitInstruction(TmpInst);
1922 }
1923 return;
1924 }
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001925 case ARM::Int_eh_sjlj_longjmp: {
1926 // ldr sp, [$src, #8]
1927 // ldr $scratch, [$src, #4]
1928 // ldr r7, [$src]
1929 // bx $scratch
1930 unsigned SrcReg = MI->getOperand(0).getReg();
1931 unsigned ScratchReg = MI->getOperand(1).getReg();
1932 {
1933 MCInst TmpInst;
Jim Grosbach3e556122010-10-26 22:37:02 +00001934 TmpInst.setOpcode(ARM::LDRi12);
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001935 TmpInst.addOperand(MCOperand::CreateReg(ARM::SP));
1936 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001937 TmpInst.addOperand(MCOperand::CreateImm(8));
1938 // Predicate.
1939 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1940 TmpInst.addOperand(MCOperand::CreateReg(0));
1941 OutStreamer.EmitInstruction(TmpInst);
1942 }
1943 {
1944 MCInst TmpInst;
Jim Grosbach3e556122010-10-26 22:37:02 +00001945 TmpInst.setOpcode(ARM::LDRi12);
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001946 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1947 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001948 TmpInst.addOperand(MCOperand::CreateImm(4));
1949 // Predicate.
1950 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1951 TmpInst.addOperand(MCOperand::CreateReg(0));
1952 OutStreamer.EmitInstruction(TmpInst);
1953 }
1954 {
1955 MCInst TmpInst;
Jim Grosbach3e556122010-10-26 22:37:02 +00001956 TmpInst.setOpcode(ARM::LDRi12);
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001957 TmpInst.addOperand(MCOperand::CreateReg(ARM::R7));
1958 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001959 TmpInst.addOperand(MCOperand::CreateImm(0));
1960 // Predicate.
1961 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1962 TmpInst.addOperand(MCOperand::CreateReg(0));
1963 OutStreamer.EmitInstruction(TmpInst);
1964 }
1965 {
1966 MCInst TmpInst;
Bill Wendling6e46d842010-11-30 00:48:15 +00001967 TmpInst.setOpcode(ARM::BX);
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001968 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1969 // Predicate.
1970 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1971 TmpInst.addOperand(MCOperand::CreateReg(0));
1972 OutStreamer.EmitInstruction(TmpInst);
1973 }
1974 return;
1975 }
Jim Grosbach385cc5e2010-09-27 22:28:11 +00001976 case ARM::tInt_eh_sjlj_longjmp: {
1977 // ldr $scratch, [$src, #8]
1978 // mov sp, $scratch
1979 // ldr $scratch, [$src, #4]
1980 // ldr r7, [$src]
1981 // bx $scratch
1982 unsigned SrcReg = MI->getOperand(0).getReg();
1983 unsigned ScratchReg = MI->getOperand(1).getReg();
1984 {
1985 MCInst TmpInst;
Bill Wendlingf4caf692010-12-14 03:36:38 +00001986 TmpInst.setOpcode(ARM::tLDRi);
Jim Grosbach385cc5e2010-09-27 22:28:11 +00001987 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1988 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
1989 // The offset immediate is #8. The operand value is scaled by 4 for the
Bill Wendlingf4caf692010-12-14 03:36:38 +00001990 // tLDR instruction.
Jim Grosbach385cc5e2010-09-27 22:28:11 +00001991 TmpInst.addOperand(MCOperand::CreateImm(2));
Jim Grosbach385cc5e2010-09-27 22:28:11 +00001992 // Predicate.
1993 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1994 TmpInst.addOperand(MCOperand::CreateReg(0));
1995 OutStreamer.EmitInstruction(TmpInst);
1996 }
1997 {
1998 MCInst TmpInst;
Jim Grosbach2a7b41b2011-06-30 23:38:17 +00001999 TmpInst.setOpcode(ARM::tMOVr);
Jim Grosbach385cc5e2010-09-27 22:28:11 +00002000 TmpInst.addOperand(MCOperand::CreateReg(ARM::SP));
2001 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
2002 // Predicate.
2003 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
2004 TmpInst.addOperand(MCOperand::CreateReg(0));
2005 OutStreamer.EmitInstruction(TmpInst);
2006 }
2007 {
2008 MCInst TmpInst;
Bill Wendlingf4caf692010-12-14 03:36:38 +00002009 TmpInst.setOpcode(ARM::tLDRi);
Jim Grosbach385cc5e2010-09-27 22:28:11 +00002010 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
2011 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
2012 TmpInst.addOperand(MCOperand::CreateImm(1));
Jim Grosbach385cc5e2010-09-27 22:28:11 +00002013 // Predicate.
2014 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
2015 TmpInst.addOperand(MCOperand::CreateReg(0));
2016 OutStreamer.EmitInstruction(TmpInst);
2017 }
2018 {
2019 MCInst TmpInst;
Bob Wilson93abbc22012-04-07 16:51:59 +00002020 TmpInst.setOpcode(ARM::tLDRi);
Jim Grosbach385cc5e2010-09-27 22:28:11 +00002021 TmpInst.addOperand(MCOperand::CreateReg(ARM::R7));
2022 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
Bob Wilson93abbc22012-04-07 16:51:59 +00002023 TmpInst.addOperand(MCOperand::CreateImm(0));
Jim Grosbach385cc5e2010-09-27 22:28:11 +00002024 // Predicate.
2025 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
2026 TmpInst.addOperand(MCOperand::CreateReg(0));
2027 OutStreamer.EmitInstruction(TmpInst);
2028 }
2029 {
2030 MCInst TmpInst;
Cameron Zwarich421b1062011-05-26 03:41:12 +00002031 TmpInst.setOpcode(ARM::tBX);
Jim Grosbach385cc5e2010-09-27 22:28:11 +00002032 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
2033 // Predicate.
2034 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
2035 TmpInst.addOperand(MCOperand::CreateReg(0));
2036 OutStreamer.EmitInstruction(TmpInst);
2037 }
2038 return;
2039 }
Chris Lattner97f06932009-10-19 20:20:46 +00002040 }
Jim Grosbachb0739b72010-09-02 01:02:06 +00002041
Chris Lattner97f06932009-10-19 20:20:46 +00002042 MCInst TmpInst;
Chris Lattner30e2cc22010-11-14 21:00:02 +00002043 LowerARMMachineInstrToMCInst(MI, TmpInst, *this);
Anton Korobeynikov57caad72011-03-05 18:43:32 +00002044
Chris Lattner850d2e22010-02-03 01:16:28 +00002045 OutStreamer.EmitInstruction(TmpInst);
Chris Lattner97f06932009-10-19 20:20:46 +00002046}
Daniel Dunbar2685a292009-10-20 05:15:36 +00002047
2048//===----------------------------------------------------------------------===//
2049// Target Registry Stuff
2050//===----------------------------------------------------------------------===//
2051
Daniel Dunbar2685a292009-10-20 05:15:36 +00002052// Force static initialization.
2053extern "C" void LLVMInitializeARMAsmPrinter() {
2054 RegisterAsmPrinter<ARMAsmPrinter> X(TheARMTarget);
2055 RegisterAsmPrinter<ARMAsmPrinter> Y(TheThumbTarget);
Daniel Dunbar2685a292009-10-20 05:15:36 +00002056}