blob: f32966d00a11a07b2a7b9e8150cd17a86170c5aa [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"
Jim Grosbach837c28a2012-10-04 21:33:24 +000026#include "llvm/ADT/SetVector.h"
27#include "llvm/ADT/SmallString.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000028#include "llvm/Constants.h"
Bill Wendling0bcbd1d2012-06-28 00:05:13 +000029#include "llvm/DebugInfo.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000030#include "llvm/Module.h"
Benjamin Kramere55b15f2009-12-28 12:27:56 +000031#include "llvm/Type.h"
Dan Gohmancf20ac42009-08-13 01:36:44 +000032#include "llvm/Assembly/Writer.h"
Chris Lattnerb0f294c2009-10-19 18:38:33 +000033#include "llvm/CodeGen/MachineModuleInfoImpls.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000034#include "llvm/CodeGen/MachineFunctionPass.h"
Evan Chenga8e29892007-01-19 07:51:42 +000035#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattnerb0f294c2009-10-19 18:38:33 +000036#include "llvm/MC/MCAsmInfo.h"
Rafael Espindolacecbc3d2010-10-25 17:50:35 +000037#include "llvm/MC/MCAssembler.h"
Chris Lattnerb0f294c2009-10-19 18:38:33 +000038#include "llvm/MC/MCContext.h"
Chris Lattner97f06932009-10-19 20:20:46 +000039#include "llvm/MC/MCInst.h"
Benjamin Kramer391271f2012-11-26 13:34:22 +000040#include "llvm/MC/MCInstBuilder.h"
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000041#include "llvm/MC/MCSectionMachO.h"
Rafael Espindolacecbc3d2010-10-25 17:50:35 +000042#include "llvm/MC/MCObjectStreamer.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000043#include "llvm/MC/MCStreamer.h"
Chris Lattner325d3dc2009-09-13 17:14:04 +000044#include "llvm/MC/MCSymbol.h"
Chris Lattnerd62f1b42010-03-12 21:19:23 +000045#include "llvm/Target/Mangler.h"
Micah Villmow3574eca2012-10-08 16:38:25 +000046#include "llvm/DataLayout.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000047#include "llvm/Target/TargetMachine.h"
Chris Lattner97f06932009-10-19 20:20:46 +000048#include "llvm/Support/CommandLine.h"
Devang Patel59135f42010-08-04 22:39:39 +000049#include "llvm/Support/Debug.h"
Torok Edwin30464702009-07-08 20:55:50 +000050#include "llvm/Support/ErrorHandling.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000051#include "llvm/Support/TargetRegistry.h"
Chris Lattnerb23569a2010-04-04 08:18:47 +000052#include "llvm/Support/raw_ostream.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000053#include <cctype>
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000054using namespace llvm;
55
Chris Lattner95b2c7d2006-12-19 22:59:26 +000056namespace {
Rafael Espindolacecbc3d2010-10-25 17:50:35 +000057
58 // Per section and per symbol attributes are not supported.
59 // To implement them we would need the ability to delay this emission
60 // until the assembly file is fully parsed/generated as only then do we
61 // know the symbol and section numbers.
62 class AttributeEmitter {
63 public:
64 virtual void MaybeSwitchVendor(StringRef Vendor) = 0;
65 virtual void EmitAttribute(unsigned Attribute, unsigned Value) = 0;
Jason W Kimf009a962011-02-07 00:49:53 +000066 virtual void EmitTextAttribute(unsigned Attribute, StringRef String) = 0;
Rafael Espindolacecbc3d2010-10-25 17:50:35 +000067 virtual void Finish() = 0;
Rafael Espindola4921e232010-10-25 18:38:32 +000068 virtual ~AttributeEmitter() {}
Rafael Espindolacecbc3d2010-10-25 17:50:35 +000069 };
70
71 class AsmAttributeEmitter : public AttributeEmitter {
72 MCStreamer &Streamer;
73
74 public:
75 AsmAttributeEmitter(MCStreamer &Streamer_) : Streamer(Streamer_) {}
76 void MaybeSwitchVendor(StringRef Vendor) { }
77
78 void EmitAttribute(unsigned Attribute, unsigned Value) {
79 Streamer.EmitRawText("\t.eabi_attribute " +
80 Twine(Attribute) + ", " + Twine(Value));
81 }
82
Jason W Kimf009a962011-02-07 00:49:53 +000083 void EmitTextAttribute(unsigned Attribute, StringRef String) {
84 switch (Attribute) {
Craig Topperbc219812012-02-07 02:50:20 +000085 default: llvm_unreachable("Unsupported Text attribute in ASM Mode");
Jason W Kimf009a962011-02-07 00:49:53 +000086 case ARMBuildAttrs::CPU_name:
Benjamin Kramer59085362011-11-06 20:37:06 +000087 Streamer.EmitRawText(StringRef("\t.cpu ") + String.lower());
Jason W Kimf009a962011-02-07 00:49:53 +000088 break;
Renato Golin728ff0d2011-02-28 22:04:27 +000089 /* GAS requires .fpu to be emitted regardless of EABI attribute */
90 case ARMBuildAttrs::Advanced_SIMD_arch:
91 case ARMBuildAttrs::VFP_arch:
Benjamin Kramer59085362011-11-06 20:37:06 +000092 Streamer.EmitRawText(StringRef("\t.fpu ") + String.lower());
Jim Grosbach8e0c7692011-09-02 18:46:15 +000093 break;
Jason W Kimf009a962011-02-07 00:49:53 +000094 }
95 }
Rafael Espindolacecbc3d2010-10-25 17:50:35 +000096 void Finish() { }
97 };
98
99 class ObjectAttributeEmitter : public AttributeEmitter {
Renato Golin719927a2011-08-09 09:50:10 +0000100 // This structure holds all attributes, accounting for
101 // their string/numeric value, so we can later emmit them
102 // in declaration order, keeping all in the same vector
103 struct AttributeItemType {
104 enum {
105 HiddenAttribute = 0,
106 NumericAttribute,
107 TextAttribute
108 } Type;
109 unsigned Tag;
110 unsigned IntValue;
111 StringRef StringValue;
112 } AttributeItem;
113
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000114 MCObjectStreamer &Streamer;
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000115 StringRef CurrentVendor;
Renato Golin719927a2011-08-09 09:50:10 +0000116 SmallVector<AttributeItemType, 64> Contents;
117
118 // Account for the ULEB/String size of each item,
119 // not just the number of items
120 size_t ContentsSize;
121 // FIXME: this should be in a more generic place, but
122 // getULEBSize() is in MCAsmInfo and will be moved to MCDwarf
123 size_t getULEBSize(int Value) {
124 size_t Size = 0;
125 do {
126 Value >>= 7;
127 Size += sizeof(int8_t); // Is this really necessary?
128 } while (Value);
129 return Size;
130 }
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000131
132 public:
133 ObjectAttributeEmitter(MCObjectStreamer &Streamer_) :
Renato Golin719927a2011-08-09 09:50:10 +0000134 Streamer(Streamer_), CurrentVendor(""), ContentsSize(0) { }
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000135
136 void MaybeSwitchVendor(StringRef Vendor) {
137 assert(!Vendor.empty() && "Vendor cannot be empty.");
138
139 if (CurrentVendor.empty())
140 CurrentVendor = Vendor;
141 else if (CurrentVendor == Vendor)
142 return;
143 else
144 Finish();
145
146 CurrentVendor = Vendor;
147
Rafael Espindola33363842010-10-25 22:26:55 +0000148 assert(Contents.size() == 0);
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000149 }
150
151 void EmitAttribute(unsigned Attribute, unsigned Value) {
Renato Golin719927a2011-08-09 09:50:10 +0000152 AttributeItemType attr = {
153 AttributeItemType::NumericAttribute,
154 Attribute,
155 Value,
156 StringRef("")
157 };
158 ContentsSize += getULEBSize(Attribute);
159 ContentsSize += getULEBSize(Value);
160 Contents.push_back(attr);
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000161 }
162
Jason W Kimf009a962011-02-07 00:49:53 +0000163 void EmitTextAttribute(unsigned Attribute, StringRef String) {
Renato Golin719927a2011-08-09 09:50:10 +0000164 AttributeItemType attr = {
165 AttributeItemType::TextAttribute,
166 Attribute,
167 0,
168 String
169 };
170 ContentsSize += getULEBSize(Attribute);
171 // String + \0
172 ContentsSize += String.size()+1;
173
174 Contents.push_back(attr);
Jason W Kimf009a962011-02-07 00:49:53 +0000175 }
176
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000177 void Finish() {
Rafael Espindola33363842010-10-25 22:26:55 +0000178 // Vendor size + Vendor name + '\0'
179 const size_t VendorHeaderSize = 4 + CurrentVendor.size() + 1;
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000180
Rafael Espindola33363842010-10-25 22:26:55 +0000181 // Tag + Tag Size
182 const size_t TagHeaderSize = 1 + 4;
183
184 Streamer.EmitIntValue(VendorHeaderSize + TagHeaderSize + ContentsSize, 4);
185 Streamer.EmitBytes(CurrentVendor, 0);
186 Streamer.EmitIntValue(0, 1); // '\0'
187
188 Streamer.EmitIntValue(ARMBuildAttrs::File, 1);
189 Streamer.EmitIntValue(TagHeaderSize + ContentsSize, 4);
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000190
Renato Golin719927a2011-08-09 09:50:10 +0000191 // Size should have been accounted for already, now
192 // emit each field as its type (ULEB or String)
193 for (unsigned int i=0; i<Contents.size(); ++i) {
194 AttributeItemType item = Contents[i];
195 Streamer.EmitULEB128IntValue(item.Tag, 0);
196 switch (item.Type) {
Craig Topperbc219812012-02-07 02:50:20 +0000197 default: llvm_unreachable("Invalid attribute type");
Renato Golin719927a2011-08-09 09:50:10 +0000198 case AttributeItemType::NumericAttribute:
199 Streamer.EmitULEB128IntValue(item.IntValue, 0);
200 break;
201 case AttributeItemType::TextAttribute:
Benjamin Kramer59085362011-11-06 20:37:06 +0000202 Streamer.EmitBytes(item.StringValue.upper(), 0);
Renato Golin719927a2011-08-09 09:50:10 +0000203 Streamer.EmitIntValue(0, 1); // '\0'
204 break;
Renato Golin719927a2011-08-09 09:50:10 +0000205 }
206 }
Rafael Espindola33363842010-10-25 22:26:55 +0000207
208 Contents.clear();
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000209 }
210 };
211
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000212} // end of anonymous namespace
213
Jim Grosbachbaf120f2010-12-01 03:45:07 +0000214MachineLocation ARMAsmPrinter::
215getDebugValueLocation(const MachineInstr *MI) const {
216 MachineLocation Location;
217 assert(MI->getNumOperands() == 4 && "Invalid no. of machine operands!");
218 // Frame address. Currently handles register +- offset only.
219 if (MI->getOperand(0).isReg() && MI->getOperand(1).isImm())
220 Location.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
221 else {
222 DEBUG(dbgs() << "DBG_VALUE instruction ignored! " << *MI << "\n");
223 }
224 return Location;
225}
226
Devang Patel27f5acb2011-04-21 22:48:26 +0000227/// EmitDwarfRegOp - Emit dwarf register operation.
Devang Patel0be77df2011-04-27 20:29:27 +0000228void ARMAsmPrinter::EmitDwarfRegOp(const MachineLocation &MLoc) const {
Devang Patel27f5acb2011-04-21 22:48:26 +0000229 const TargetRegisterInfo *RI = TM.getRegisterInfo();
230 if (RI->getDwarfRegNum(MLoc.getReg(), false) != -1)
Devang Patel0be77df2011-04-27 20:29:27 +0000231 AsmPrinter::EmitDwarfRegOp(MLoc);
Devang Patel27f5acb2011-04-21 22:48:26 +0000232 else {
233 unsigned Reg = MLoc.getReg();
234 if (Reg >= ARM::S0 && Reg <= ARM::S31) {
Devang Patel0a6ea832011-04-22 16:44:29 +0000235 assert(ARM::S0 + 31 == ARM::S31 && "Unexpected ARM S register numbering");
Devang Patel27f5acb2011-04-21 22:48:26 +0000236 // S registers are described as bit-pieces of a register
237 // S[2x] = DW_OP_regx(256 + (x>>1)) DW_OP_bit_piece(32, 0)
238 // S[2x+1] = DW_OP_regx(256 + (x>>1)) DW_OP_bit_piece(32, 32)
Jim Grosbach8e0c7692011-09-02 18:46:15 +0000239
Devang Patel27f5acb2011-04-21 22:48:26 +0000240 unsigned SReg = Reg - ARM::S0;
241 bool odd = SReg & 0x1;
242 unsigned Rx = 256 + (SReg >> 1);
Devang Patel27f5acb2011-04-21 22:48:26 +0000243
244 OutStreamer.AddComment("DW_OP_regx for S register");
245 EmitInt8(dwarf::DW_OP_regx);
246
247 OutStreamer.AddComment(Twine(SReg));
248 EmitULEB128(Rx);
249
250 if (odd) {
251 OutStreamer.AddComment("DW_OP_bit_piece 32 32");
252 EmitInt8(dwarf::DW_OP_bit_piece);
253 EmitULEB128(32);
254 EmitULEB128(32);
255 } else {
256 OutStreamer.AddComment("DW_OP_bit_piece 32 0");
257 EmitInt8(dwarf::DW_OP_bit_piece);
258 EmitULEB128(32);
259 EmitULEB128(0);
260 }
Devang Patel71f3f112011-04-21 23:22:35 +0000261 } else if (Reg >= ARM::Q0 && Reg <= ARM::Q15) {
Devang Patel0a6ea832011-04-22 16:44:29 +0000262 assert(ARM::Q0 + 15 == ARM::Q15 && "Unexpected ARM Q register numbering");
Devang Patel71f3f112011-04-21 23:22:35 +0000263 // Q registers Q0-Q15 are described by composing two D registers together.
Jim Grosbach8e0c7692011-09-02 18:46:15 +0000264 // Qx = DW_OP_regx(256+2x) DW_OP_piece(8) DW_OP_regx(256+2x+1)
265 // DW_OP_piece(8)
Devang Patel71f3f112011-04-21 23:22:35 +0000266
267 unsigned QReg = Reg - ARM::Q0;
268 unsigned D1 = 256 + 2 * QReg;
269 unsigned D2 = D1 + 1;
Jim Grosbach8e0c7692011-09-02 18:46:15 +0000270
Devang Patel71f3f112011-04-21 23:22:35 +0000271 OutStreamer.AddComment("DW_OP_regx for Q register: D1");
272 EmitInt8(dwarf::DW_OP_regx);
273 EmitULEB128(D1);
274 OutStreamer.AddComment("DW_OP_piece 8");
275 EmitInt8(dwarf::DW_OP_piece);
276 EmitULEB128(8);
277
278 OutStreamer.AddComment("DW_OP_regx for Q register: D2");
279 EmitInt8(dwarf::DW_OP_regx);
280 EmitULEB128(D2);
281 OutStreamer.AddComment("DW_OP_piece 8");
282 EmitInt8(dwarf::DW_OP_piece);
283 EmitULEB128(8);
Devang Patel27f5acb2011-04-21 22:48:26 +0000284 }
285 }
286}
287
Jim Grosbach3e965312012-05-18 19:12:01 +0000288void ARMAsmPrinter::EmitFunctionBodyEnd() {
289 // Make sure to terminate any constant pools that were at the end
290 // of the function.
291 if (!InConstantPool)
292 return;
293 InConstantPool = false;
294 OutStreamer.EmitDataRegion(MCDR_DataRegionEnd);
295}
Owen Anderson2fec6c52011-10-04 23:26:17 +0000296
Jim Grosbach3e965312012-05-18 19:12:01 +0000297void ARMAsmPrinter::EmitFunctionEntryLabel() {
Chris Lattner953ebb72010-01-27 23:58:11 +0000298 if (AFI->isThumbFunction()) {
Jim Grosbachce792992010-11-05 22:08:08 +0000299 OutStreamer.EmitAssemblerFlag(MCAF_Code16);
Rafael Espindola64695402011-05-16 16:17:21 +0000300 OutStreamer.EmitThumbFunc(CurrentFnSym);
Chris Lattner953ebb72010-01-27 23:58:11 +0000301 }
Jim Grosbachb0739b72010-09-02 01:02:06 +0000302
Chris Lattner953ebb72010-01-27 23:58:11 +0000303 OutStreamer.EmitLabel(CurrentFnSym);
304}
305
James Molloy34982572012-01-26 09:25:43 +0000306void ARMAsmPrinter::EmitXXStructor(const Constant *CV) {
Micah Villmow3574eca2012-10-08 16:38:25 +0000307 uint64_t Size = TM.getDataLayout()->getTypeAllocSize(CV->getType());
James Molloy34982572012-01-26 09:25:43 +0000308 assert(Size && "C++ constructor pointer had zero size!");
309
Bill Wendling4a1ff2f2012-02-15 09:14:08 +0000310 const GlobalValue *GV = dyn_cast<GlobalValue>(CV->stripPointerCasts());
James Molloy34982572012-01-26 09:25:43 +0000311 assert(GV && "C++ constructor pointer was not a GlobalValue!");
312
313 const MCExpr *E = MCSymbolRefExpr::Create(Mang->getSymbol(GV),
314 (Subtarget->isTargetDarwin()
315 ? MCSymbolRefExpr::VK_None
316 : MCSymbolRefExpr::VK_ARM_TARGET1),
317 OutContext);
318
319 OutStreamer.EmitValue(E, Size);
320}
321
Jim Grosbach2317e402010-09-30 01:57:53 +0000322/// runOnMachineFunction - This uses the EmitInstruction()
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000323/// method to print assembly for each instruction.
324///
325bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Evan Chenga8e29892007-01-19 07:51:42 +0000326 AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng6d63a722008-09-18 07:27:23 +0000327 MCP = MF.getConstantPool();
Rafael Espindola4b442b52006-05-23 02:48:20 +0000328
Chris Lattnerd49fe1b2010-01-28 01:28:58 +0000329 return AsmPrinter::runOnMachineFunction(MF);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000330}
331
Evan Cheng055b0312009-06-29 07:51:04 +0000332void ARMAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
Chris Lattner35c33bd2010-04-04 04:47:45 +0000333 raw_ostream &O, const char *Modifier) {
Evan Cheng055b0312009-06-29 07:51:04 +0000334 const MachineOperand &MO = MI->getOperand(OpNum);
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000335 unsigned TF = MO.getTargetFlags();
336
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000337 switch (MO.getType()) {
Craig Topperbc219812012-02-07 02:50:20 +0000338 default: llvm_unreachable("<unknown operand type>");
Bob Wilson5bafff32009-06-22 23:27:02 +0000339 case MachineOperand::MO_Register: {
340 unsigned Reg = MO.getReg();
Chris Lattner8bc86cb2009-10-19 20:59:55 +0000341 assert(TargetRegisterInfo::isPhysicalRegister(Reg));
Jim Grosbach35636282010-10-06 21:22:32 +0000342 assert(!MO.getSubReg() && "Subregs should be eliminated!");
343 O << ARMInstPrinter::getRegisterName(Reg);
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000344 break;
Bob Wilson5bafff32009-06-22 23:27:02 +0000345 }
Evan Chenga8e29892007-01-19 07:51:42 +0000346 case MachineOperand::MO_Immediate: {
Evan Cheng5adb66a2009-09-28 09:14:39 +0000347 int64_t Imm = MO.getImm();
Anton Korobeynikov632606c2009-10-08 20:43:22 +0000348 O << '#';
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000349 if ((Modifier && strcmp(Modifier, "lo16") == 0) ||
Jason W Kim650b7d72011-01-12 23:21:49 +0000350 (TF == ARMII::MO_LO16))
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000351 O << ":lower16:";
352 else if ((Modifier && strcmp(Modifier, "hi16") == 0) ||
Jason W Kim650b7d72011-01-12 23:21:49 +0000353 (TF == ARMII::MO_HI16))
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000354 O << ":upper16:";
Anton Korobeynikov632606c2009-10-08 20:43:22 +0000355 O << Imm;
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000356 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000357 }
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000358 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner1b2eb0e2010-03-13 21:04:28 +0000359 O << *MO.getMBB()->getSymbol();
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000360 return;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000361 case MachineOperand::MO_GlobalAddress: {
Dan Gohman46510a72010-04-15 01:51:59 +0000362 const GlobalValue *GV = MO.getGlobal();
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000363 if ((Modifier && strcmp(Modifier, "lo16") == 0) ||
364 (TF & ARMII::MO_LO16))
365 O << ":lower16:";
366 else if ((Modifier && strcmp(Modifier, "hi16") == 0) ||
367 (TF & ARMII::MO_HI16))
368 O << ":upper16:";
Chris Lattnerd62f1b42010-03-12 21:19:23 +0000369 O << *Mang->getSymbol(GV);
Anton Korobeynikov7751ad92008-11-22 16:15:34 +0000370
Chris Lattner0c08d092010-04-03 22:28:33 +0000371 printOffset(MO.getOffset(), O);
Jim Grosbach1d6111c2010-10-06 21:36:43 +0000372 if (TF == ARMII::MO_PLT)
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000373 O << "(PLT)";
Evan Chenga8e29892007-01-19 07:51:42 +0000374 break;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000375 }
Evan Chenga8e29892007-01-19 07:51:42 +0000376 case MachineOperand::MO_ExternalSymbol: {
Chris Lattner10b318b2010-01-17 21:43:43 +0000377 O << *GetExternalSymbolSymbol(MO.getSymbolName());
Jim Grosbach1d6111c2010-10-06 21:36:43 +0000378 if (TF == ARMII::MO_PLT)
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000379 O << "(PLT)";
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000380 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000381 }
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000382 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner1b46f432010-01-23 07:00:21 +0000383 O << *GetCPISymbol(MO.getIndex());
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000384 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000385 case MachineOperand::MO_JumpTableIndex:
Chris Lattner1b46f432010-01-23 07:00:21 +0000386 O << *GetJTISymbol(MO.getIndex());
Evan Chenga8e29892007-01-19 07:51:42 +0000387 break;
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000388 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000389}
390
Evan Cheng055b0312009-06-29 07:51:04 +0000391//===--------------------------------------------------------------------===//
392
Chris Lattner0890cf12010-01-25 19:51:38 +0000393MCSymbol *ARMAsmPrinter::
Chris Lattner0890cf12010-01-25 19:51:38 +0000394GetARMJTIPICJumpTableLabel2(unsigned uid, unsigned uid2) const {
395 SmallString<60> Name;
396 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "JTI"
Chris Lattner281e7762010-01-25 23:28:03 +0000397 << getFunctionNumber() << '_' << uid << '_' << uid2;
Chris Lattner9b97a732010-03-30 18:10:53 +0000398 return OutContext.GetOrCreateSymbol(Name.str());
Chris Lattnerbfcb0962010-01-25 19:39:52 +0000399}
400
Jim Grosbach433a5782010-09-24 20:47:58 +0000401
Dmitri Gribenko79c07d22012-11-15 16:51:49 +0000402MCSymbol *ARMAsmPrinter::GetARMSJLJEHLabel() const {
Jim Grosbach433a5782010-09-24 20:47:58 +0000403 SmallString<60> Name;
404 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "SJLJEH"
405 << getFunctionNumber();
406 return OutContext.GetOrCreateSymbol(Name.str());
407}
408
Evan Cheng055b0312009-06-29 07:51:04 +0000409bool ARMAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
Chris Lattnerc75c0282010-04-04 05:29:35 +0000410 unsigned AsmVariant, const char *ExtraCode,
411 raw_ostream &O) {
Evan Chenga8e29892007-01-19 07:51:42 +0000412 // Does this asm operand have a single letter operand modifier?
413 if (ExtraCode && ExtraCode[0]) {
414 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov8e9ece72009-08-08 23:10:41 +0000415
Evan Chenga8e29892007-01-19 07:51:42 +0000416 switch (ExtraCode[0]) {
Jack Carter0518fca2012-06-26 13:49:27 +0000417 default:
418 // See if this is a generic print operand
419 return AsmPrinter::PrintAsmOperand(MI, OpNum, AsmVariant, ExtraCode, O);
Bob Wilson9b4b00a2009-07-09 23:54:51 +0000420 case 'a': // Print as a memory address.
421 if (MI->getOperand(OpNum).isReg()) {
Jim Grosbach2f24c4e2010-09-30 15:25:22 +0000422 O << "["
423 << ARMInstPrinter::getRegisterName(MI->getOperand(OpNum).getReg())
424 << "]";
Bob Wilson9b4b00a2009-07-09 23:54:51 +0000425 return false;
426 }
427 // Fallthrough
428 case 'c': // Don't print "#" before an immediate operand.
Bob Wilson4f38b382009-08-21 21:58:55 +0000429 if (!MI->getOperand(OpNum).isImm())
430 return true;
Jim Grosbach2317e402010-09-30 01:57:53 +0000431 O << MI->getOperand(OpNum).getImm();
Bob Wilson8f343462009-04-06 21:46:51 +0000432 return false;
Evan Chenge21e3962007-04-04 00:13:29 +0000433 case 'P': // Print a VFP double precision register.
Evan Chengd831cda2009-12-08 23:06:22 +0000434 case 'q': // Print a NEON quad precision register.
Chris Lattner35c33bd2010-04-04 04:47:45 +0000435 printOperand(MI, OpNum, O);
Evan Cheng23a95702007-03-08 22:42:46 +0000436 return false;
Eric Christopher0628d382011-05-24 22:10:34 +0000437 case 'y': // Print a VFP single precision register as indexed double.
Eric Christopher0628d382011-05-24 22:10:34 +0000438 if (MI->getOperand(OpNum).isReg()) {
439 unsigned Reg = MI->getOperand(OpNum).getReg();
440 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
Jakob Stoklund Olesen4c91bda2012-05-30 23:00:43 +0000441 // Find the 'd' register that has this 's' register as a sub-register,
442 // and determine the lane number.
443 for (MCSuperRegIterator SR(Reg, TRI); SR.isValid(); ++SR) {
444 if (!ARM::DPRRegClass.contains(*SR))
445 continue;
446 bool Lane0 = TRI->getSubReg(*SR, ARM::ssub_0) == Reg;
447 O << ARMInstPrinter::getRegisterName(*SR) << (Lane0 ? "[0]" : "[1]");
448 return false;
449 }
Eric Christopher0628d382011-05-24 22:10:34 +0000450 }
Eric Christopher4db7dec2011-05-24 23:27:13 +0000451 return true;
Eric Christopherfef50062011-05-24 22:27:43 +0000452 case 'B': // Bitwise inverse of integer or symbol without a preceding #.
Eric Christophere1739d52011-05-24 23:15:43 +0000453 if (!MI->getOperand(OpNum).isImm())
454 return true;
455 O << ~(MI->getOperand(OpNum).getImm());
456 return false;
Eric Christopherfef50062011-05-24 22:27:43 +0000457 case 'L': // The low 16 bits of an immediate constant.
Eric Christopher4db7dec2011-05-24 23:27:13 +0000458 if (!MI->getOperand(OpNum).isImm())
459 return true;
460 O << (MI->getOperand(OpNum).getImm() & 0xffff);
461 return false;
Eric Christopher3c14f242011-05-28 01:40:44 +0000462 case 'M': { // A register range suitable for LDM/STM.
463 if (!MI->getOperand(OpNum).isReg())
464 return true;
465 const MachineOperand &MO = MI->getOperand(OpNum);
466 unsigned RegBegin = MO.getReg();
467 // This takes advantage of the 2 operand-ness of ldm/stm and that we've
468 // already got the operands in registers that are operands to the
469 // inline asm statement.
Jim Grosbach8e0c7692011-09-02 18:46:15 +0000470
Eric Christopher3c14f242011-05-28 01:40:44 +0000471 O << "{" << ARMInstPrinter::getRegisterName(RegBegin);
Jim Grosbach8e0c7692011-09-02 18:46:15 +0000472
Eric Christopher3c14f242011-05-28 01:40:44 +0000473 // FIXME: The register allocator not only may not have given us the
474 // registers in sequence, but may not be in ascending registers. This
475 // will require changes in the register allocator that'll need to be
476 // propagated down here if the operands change.
477 unsigned RegOps = OpNum + 1;
478 while (MI->getOperand(RegOps).isReg()) {
Jim Grosbach8e0c7692011-09-02 18:46:15 +0000479 O << ", "
Eric Christopher3c14f242011-05-28 01:40:44 +0000480 << ARMInstPrinter::getRegisterName(MI->getOperand(RegOps).getReg());
481 RegOps++;
482 }
483
484 O << "}";
485
486 return false;
487 }
Rafael Espindolaf5ade5d2011-08-10 16:26:42 +0000488 case 'R': // The most significant register of a pair.
489 case 'Q': { // The least significant register of a pair.
490 if (OpNum == 0)
491 return true;
492 const MachineOperand &FlagsOP = MI->getOperand(OpNum - 1);
493 if (!FlagsOP.isImm())
494 return true;
495 unsigned Flags = FlagsOP.getImm();
496 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
497 if (NumVals != 2)
498 return true;
499 unsigned RegOp = ExtraCode[0] == 'Q' ? OpNum : OpNum + 1;
500 if (RegOp >= MI->getNumOperands())
501 return true;
502 const MachineOperand &MO = MI->getOperand(RegOp);
503 if (!MO.isReg())
504 return true;
505 unsigned Reg = MO.getReg();
506 O << ARMInstPrinter::getRegisterName(Reg);
507 return false;
508 }
509
Eric Christopherfef50062011-05-24 22:27:43 +0000510 case 'e': // The low doubleword register of a NEON quad register.
Bob Wilson9cd2b952011-12-12 21:45:15 +0000511 case 'f': { // The high doubleword register of a NEON quad register.
512 if (!MI->getOperand(OpNum).isReg())
513 return true;
514 unsigned Reg = MI->getOperand(OpNum).getReg();
515 if (!ARM::QPRRegClass.contains(Reg))
516 return true;
517 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
518 unsigned SubReg = TRI->getSubReg(Reg, ExtraCode[0] == 'e' ?
519 ARM::dsub_0 : ARM::dsub_1);
520 O << ARMInstPrinter::getRegisterName(SubReg);
521 return false;
522 }
523
Eric Christopher001d2192012-08-13 18:18:52 +0000524 // This modifier is not yet supported.
Eric Christopherfef50062011-05-24 22:27:43 +0000525 case 'h': // A range of VFP/NEON registers suitable for VLD1/VST1.
Bob Wilsond984eb62010-05-27 20:23:42 +0000526 return true;
Eric Christopher6eef0e22012-08-14 23:32:15 +0000527 case 'H': { // The highest-numbered register of a pair.
Eric Christopher001d2192012-08-13 18:18:52 +0000528 const MachineOperand &MO = MI->getOperand(OpNum);
529 if (!MO.isReg())
530 return true;
531 const TargetRegisterClass &RC = ARM::GPRRegClass;
532 const MachineFunction &MF = *MI->getParent()->getParent();
533 const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo();
534
535 unsigned RegIdx = TRI->getEncodingValue(MO.getReg());
536 RegIdx |= 1; //The odd register is also the higher-numbered one of a pair.
537
538 unsigned Reg = RC.getRegister(RegIdx);
539 O << ARMInstPrinter::getRegisterName(Reg);
540 return false;
Evan Cheng84f60b72010-05-27 22:08:38 +0000541 }
Eric Christopher6eef0e22012-08-14 23:32:15 +0000542 }
Evan Chenga8e29892007-01-19 07:51:42 +0000543 }
Jim Grosbache9952212009-09-04 01:38:51 +0000544
Chris Lattner35c33bd2010-04-04 04:47:45 +0000545 printOperand(MI, OpNum, O);
Evan Chenga8e29892007-01-19 07:51:42 +0000546 return false;
547}
548
Bob Wilson224c2442009-05-19 05:53:42 +0000549bool ARMAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Evan Cheng055b0312009-06-29 07:51:04 +0000550 unsigned OpNum, unsigned AsmVariant,
Chris Lattnerc75c0282010-04-04 05:29:35 +0000551 const char *ExtraCode,
552 raw_ostream &O) {
Eric Christopher8f894632011-05-25 20:51:58 +0000553 // Does this asm operand have a single letter operand modifier?
554 if (ExtraCode && ExtraCode[0]) {
555 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Jim Grosbach8e0c7692011-09-02 18:46:15 +0000556
Eric Christopher8f894632011-05-25 20:51:58 +0000557 switch (ExtraCode[0]) {
Eric Christopher32bfb2c2011-05-26 18:22:26 +0000558 case 'A': // A memory operand for a VLD1/VST1 instruction.
Eric Christopher8f894632011-05-25 20:51:58 +0000559 default: return true; // Unknown modifier.
560 case 'm': // The base register of a memory operand.
561 if (!MI->getOperand(OpNum).isReg())
562 return true;
563 O << ARMInstPrinter::getRegisterName(MI->getOperand(OpNum).getReg());
564 return false;
565 }
566 }
Jim Grosbach8e0c7692011-09-02 18:46:15 +0000567
Bob Wilson765cc0b2009-10-13 20:50:28 +0000568 const MachineOperand &MO = MI->getOperand(OpNum);
569 assert(MO.isReg() && "unexpected inline asm memory operand");
Jim Grosbach2317e402010-09-30 01:57:53 +0000570 O << "[" << ARMInstPrinter::getRegisterName(MO.getReg()) << "]";
Bob Wilson224c2442009-05-19 05:53:42 +0000571 return false;
572}
573
Bob Wilson812209a2009-09-30 22:06:26 +0000574void ARMAsmPrinter::EmitStartOfAsmFile(Module &M) {
Bob Wilson0fb34682009-09-30 00:23:42 +0000575 if (Subtarget->isTargetDarwin()) {
576 Reloc::Model RelocM = TM.getRelocationModel();
577 if (RelocM == Reloc::PIC_ || RelocM == Reloc::DynamicNoPIC) {
578 // Declare all the text sections up front (before the DWARF sections
579 // emitted by AsmPrinter::doInitialization) so the assembler will keep
580 // them together at the beginning of the object file. This helps
581 // avoid out-of-range branches that are due a fundamental limitation of
582 // the way symbol offsets are encoded with the current Darwin ARM
583 // relocations.
Jim Grosbachb0739b72010-09-02 01:02:06 +0000584 const TargetLoweringObjectFileMachO &TLOFMacho =
Dan Gohman0d805c32010-04-17 16:44:48 +0000585 static_cast<const TargetLoweringObjectFileMachO &>(
586 getObjFileLowering());
Jim Grosbach837c28a2012-10-04 21:33:24 +0000587
588 // Collect the set of sections our functions will go into.
589 SetVector<const MCSection *, SmallVector<const MCSection *, 8>,
590 SmallPtrSet<const MCSection *, 8> > TextSections;
591 // Default text section comes first.
592 TextSections.insert(TLOFMacho.getTextSection());
593 // Now any user defined text sections from function attributes.
594 for (Module::iterator F = M.begin(), e = M.end(); F != e; ++F)
595 if (!F->isDeclaration() && !F->hasAvailableExternallyLinkage())
596 TextSections.insert(TLOFMacho.SectionForGlobal(F, Mang, TM));
597 // Now the coalescable sections.
598 TextSections.insert(TLOFMacho.getTextCoalSection());
599 TextSections.insert(TLOFMacho.getConstTextCoalSection());
600
601 // Emit the sections in the .s file header to fix the order.
602 for (unsigned i = 0, e = TextSections.size(); i != e; ++i)
603 OutStreamer.SwitchSection(TextSections[i]);
604
Bob Wilson29e06692009-09-30 22:25:37 +0000605 if (RelocM == Reloc::DynamicNoPIC) {
606 const MCSection *sect =
Chris Lattner22772212010-04-08 20:40:11 +0000607 OutContext.getMachOSection("__TEXT", "__symbol_stub4",
608 MCSectionMachO::S_SYMBOL_STUBS,
609 12, SectionKind::getText());
Bob Wilson29e06692009-09-30 22:25:37 +0000610 OutStreamer.SwitchSection(sect);
611 } else {
612 const MCSection *sect =
Chris Lattner22772212010-04-08 20:40:11 +0000613 OutContext.getMachOSection("__TEXT", "__picsymbolstub4",
614 MCSectionMachO::S_SYMBOL_STUBS,
615 16, SectionKind::getText());
Bob Wilson29e06692009-09-30 22:25:37 +0000616 OutStreamer.SwitchSection(sect);
617 }
Bob Wilson63db5942010-07-30 19:55:47 +0000618 const MCSection *StaticInitSect =
619 OutContext.getMachOSection("__TEXT", "__StaticInit",
620 MCSectionMachO::S_REGULAR |
621 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
622 SectionKind::getText());
623 OutStreamer.SwitchSection(StaticInitSect);
Bob Wilson0fb34682009-09-30 00:23:42 +0000624 }
625 }
626
Jim Grosbache5165492009-11-09 00:11:35 +0000627 // Use unified assembler syntax.
Jason W Kimafd1cc22010-09-30 02:45:56 +0000628 OutStreamer.EmitAssemblerFlag(MCAF_SyntaxUnified);
Anton Korobeynikovd61eca52009-06-17 23:43:18 +0000629
Anton Korobeynikov88ce6672009-05-23 19:51:20 +0000630 // Emit ARM Build Attributes
Evan Cheng07043272012-02-21 20:46:00 +0000631 if (Subtarget->isTargetELF())
Jason W Kimdef9ac42010-10-06 22:36:46 +0000632 emitAttributes();
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000633}
634
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +0000635
Chris Lattner4a071d62009-10-19 17:59:19 +0000636void ARMAsmPrinter::EmitEndOfAsmFile(Module &M) {
Evan Cheng5be54b02007-01-19 19:25:36 +0000637 if (Subtarget->isTargetDarwin()) {
Chris Lattnerf61159b2009-08-03 22:18:15 +0000638 // All darwin targets use mach-o.
Dan Gohman0d805c32010-04-17 16:44:48 +0000639 const TargetLoweringObjectFileMachO &TLOFMacho =
640 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattnerb0f294c2009-10-19 18:38:33 +0000641 MachineModuleInfoMachO &MMIMacho =
642 MMI->getObjFileInfo<MachineModuleInfoMachO>();
Jim Grosbache9952212009-09-04 01:38:51 +0000643
Evan Chenga8e29892007-01-19 07:51:42 +0000644 // Output non-lazy-pointers for external and common global variables.
Chris Lattnerb0f294c2009-10-19 18:38:33 +0000645 MachineModuleInfoMachO::SymbolListTy Stubs = MMIMacho.GetGVStubList();
Bill Wendlingcebae362010-03-10 22:34:10 +0000646
Chris Lattnerb0f294c2009-10-19 18:38:33 +0000647 if (!Stubs.empty()) {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000648 // Switch with ".non_lazy_symbol_pointer" directive.
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000649 OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
Chris Lattnerc076a972009-08-10 18:01:34 +0000650 EmitAlignment(2);
Chris Lattnerb0f294c2009-10-19 18:38:33 +0000651 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Bill Wendlingbecd83e2010-03-09 00:40:17 +0000652 // L_foo$stub:
653 OutStreamer.EmitLabel(Stubs[i].first);
654 // .indirect_symbol _foo
Bill Wendling52a50e52010-03-11 01:18:13 +0000655 MachineModuleInfoImpl::StubValueTy &MCSym = Stubs[i].second;
656 OutStreamer.EmitSymbolAttribute(MCSym.getPointer(),MCSA_IndirectSymbol);
Bill Wendlingcf6f28d2010-03-09 00:43:34 +0000657
Bill Wendling52a50e52010-03-11 01:18:13 +0000658 if (MCSym.getInt())
Bill Wendlingcf6f28d2010-03-09 00:43:34 +0000659 // External to current translation unit.
660 OutStreamer.EmitIntValue(0, 4/*size*/, 0/*addrspace*/);
661 else
662 // Internal to current translation unit.
Bill Wendling5e1b55d2010-03-31 18:47:10 +0000663 //
Jim Grosbach1b935a32010-09-22 16:45:13 +0000664 // When we place the LSDA into the TEXT section, the type info
665 // pointers need to be indirect and pc-rel. We accomplish this by
666 // using NLPs; however, sometimes the types are local to the file.
667 // We need to fill in the value for the NLP in those cases.
Bill Wendling52a50e52010-03-11 01:18:13 +0000668 OutStreamer.EmitValue(MCSymbolRefExpr::Create(MCSym.getPointer(),
669 OutContext),
Bill Wendlingcf6f28d2010-03-09 00:43:34 +0000670 4/*size*/, 0/*addrspace*/);
Evan Chengae94e592008-12-05 01:06:39 +0000671 }
Bill Wendlingbecd83e2010-03-09 00:40:17 +0000672
673 Stubs.clear();
674 OutStreamer.AddBlankLine();
Evan Chenga8e29892007-01-19 07:51:42 +0000675 }
676
Chris Lattnere4d9ea82009-10-19 18:44:38 +0000677 Stubs = MMIMacho.GetHiddenGVStubList();
678 if (!Stubs.empty()) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000679 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
Chris Lattnerf3231de2009-08-10 18:02:16 +0000680 EmitAlignment(2);
Bill Wendlingbecd83e2010-03-09 00:40:17 +0000681 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
682 // L_foo$stub:
683 OutStreamer.EmitLabel(Stubs[i].first);
684 // .long _foo
Bill Wendlingcebae362010-03-10 22:34:10 +0000685 OutStreamer.EmitValue(MCSymbolRefExpr::
686 Create(Stubs[i].second.getPointer(),
687 OutContext),
Bill Wendlingbecd83e2010-03-09 00:40:17 +0000688 4/*size*/, 0/*addrspace*/);
689 }
Bill Wendlingcf6f28d2010-03-09 00:43:34 +0000690
691 Stubs.clear();
692 OutStreamer.AddBlankLine();
Evan Chengae94e592008-12-05 01:06:39 +0000693 }
694
Evan Chenga8e29892007-01-19 07:51:42 +0000695 // Funny Darwin hack: This flag tells the linker that no global symbols
696 // contain code that falls through to other global symbols (e.g. the obvious
697 // implementation of multiple entry points). If this doesn't occur, the
698 // linker can safely perform dead code stripping. Since LLVM never
699 // generates code that does this, it is always safe to set.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000700 OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000701 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000702}
Anton Korobeynikov0bd89712008-08-17 13:55:10 +0000703
Chris Lattner97f06932009-10-19 20:20:46 +0000704//===----------------------------------------------------------------------===//
Jason W Kimdef9ac42010-10-06 22:36:46 +0000705// Helper routines for EmitStartOfAsmFile() and EmitEndOfAsmFile()
706// FIXME:
707// The following seem like one-off assembler flags, but they actually need
Jim Grosbachfa7fb642010-10-06 22:46:47 +0000708// to appear in the .ARM.attributes section in ELF.
Jason W Kimdef9ac42010-10-06 22:36:46 +0000709// Instead of subclassing the MCELFStreamer, we do the work here.
710
711void ARMAsmPrinter::emitAttributes() {
Jim Grosbachfa7fb642010-10-06 22:46:47 +0000712
Jason W Kim17b443d2010-10-11 23:01:44 +0000713 emitARMAttributeSection();
714
Renato Golin728ff0d2011-02-28 22:04:27 +0000715 /* GAS expect .fpu to be emitted, regardless of VFP build attribute */
716 bool emitFPU = false;
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000717 AttributeEmitter *AttrEmitter;
Renato Golin728ff0d2011-02-28 22:04:27 +0000718 if (OutStreamer.hasRawTextSupport()) {
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000719 AttrEmitter = new AsmAttributeEmitter(OutStreamer);
Renato Golin728ff0d2011-02-28 22:04:27 +0000720 emitFPU = true;
721 } else {
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000722 MCObjectStreamer &O = static_cast<MCObjectStreamer&>(OutStreamer);
723 AttrEmitter = new ObjectAttributeEmitter(O);
724 }
725
726 AttrEmitter->MaybeSwitchVendor("aeabi");
727
Jason W Kimdef9ac42010-10-06 22:36:46 +0000728 std::string CPUString = Subtarget->getCPUString();
Jason W Kimf009a962011-02-07 00:49:53 +0000729
730 if (CPUString == "cortex-a8" ||
731 Subtarget->isCortexA8()) {
Jason W Kimc046d642011-02-07 19:07:11 +0000732 AttrEmitter->EmitTextAttribute(ARMBuildAttrs::CPU_name, "cortex-a8");
Jason W Kimf009a962011-02-07 00:49:53 +0000733 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v7);
734 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch_profile,
735 ARMBuildAttrs::ApplicationProfile);
736 AttrEmitter->EmitAttribute(ARMBuildAttrs::ARM_ISA_use,
737 ARMBuildAttrs::Allowed);
738 AttrEmitter->EmitAttribute(ARMBuildAttrs::THUMB_ISA_use,
739 ARMBuildAttrs::AllowThumb32);
740 // Fixme: figure out when this is emitted.
741 //AttrEmitter->EmitAttribute(ARMBuildAttrs::WMMX_arch,
742 // ARMBuildAttrs::AllowWMMXv1);
743 //
744
745 /// ADD additional Else-cases here!
Rafael Espindolab8adb8a2011-05-20 20:10:34 +0000746 } else if (CPUString == "xscale") {
747 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v5TEJ);
748 AttrEmitter->EmitAttribute(ARMBuildAttrs::ARM_ISA_use,
749 ARMBuildAttrs::Allowed);
750 AttrEmitter->EmitAttribute(ARMBuildAttrs::THUMB_ISA_use,
751 ARMBuildAttrs::Allowed);
Jason W Kimf009a962011-02-07 00:49:53 +0000752 } else if (CPUString == "generic") {
Amara Emerson214fd3d2012-11-08 09:51:45 +0000753 // For a generic CPU, we assume a standard v7a architecture in Subtarget.
754 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v7);
755 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch_profile,
756 ARMBuildAttrs::ApplicationProfile);
Jason W Kimf009a962011-02-07 00:49:53 +0000757 AttrEmitter->EmitAttribute(ARMBuildAttrs::ARM_ISA_use,
758 ARMBuildAttrs::Allowed);
759 AttrEmitter->EmitAttribute(ARMBuildAttrs::THUMB_ISA_use,
Amara Emerson214fd3d2012-11-08 09:51:45 +0000760 ARMBuildAttrs::AllowThumb32);
761 } else if (Subtarget->hasV7Ops()) {
762 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v7);
763 AttrEmitter->EmitAttribute(ARMBuildAttrs::THUMB_ISA_use,
764 ARMBuildAttrs::AllowThumb32);
765 } else if (Subtarget->hasV6T2Ops())
766 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v6T2);
767 else if (Subtarget->hasV6Ops())
768 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v6);
769 else if (Subtarget->hasV5TEOps())
770 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v5TE);
771 else if (Subtarget->hasV5TOps())
772 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v5T);
773 else if (Subtarget->hasV4TOps())
774 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v4T);
Jason W Kimdef9ac42010-10-06 22:36:46 +0000775
Renato Goline89a0532011-03-02 21:20:09 +0000776 if (Subtarget->hasNEON() && emitFPU) {
Renato Golin728ff0d2011-02-28 22:04:27 +0000777 /* NEON is not exactly a VFP architecture, but GAS emit one of
Anton Korobeynikov4b4e6222012-01-22 12:07:33 +0000778 * neon/neon-vfpv4/vfpv3/vfpv2 for .fpu parameters */
Evan Chengbee78fe2012-04-11 05:33:07 +0000779 if (Subtarget->hasVFP4())
Jim Grosbachd4f020a2012-04-06 23:43:50 +0000780 AttrEmitter->EmitTextAttribute(ARMBuildAttrs::Advanced_SIMD_arch,
781 "neon-vfpv4");
Anton Korobeynikov4b4e6222012-01-22 12:07:33 +0000782 else
Sebastian Pop74bebde2012-03-05 17:39:52 +0000783 AttrEmitter->EmitTextAttribute(ARMBuildAttrs::Advanced_SIMD_arch, "neon");
Renato Golin728ff0d2011-02-28 22:04:27 +0000784 /* If emitted for NEON, omit from VFP below, since you can have both
785 * NEON and VFP in build attributes but only one .fpu */
786 emitFPU = false;
787 }
788
Anton Korobeynikov4b4e6222012-01-22 12:07:33 +0000789 /* VFPv4 + .fpu */
790 if (Subtarget->hasVFP4()) {
791 AttrEmitter->EmitAttribute(ARMBuildAttrs::VFP_arch,
792 ARMBuildAttrs::AllowFPv4A);
793 if (emitFPU)
794 AttrEmitter->EmitTextAttribute(ARMBuildAttrs::VFP_arch, "vfpv4");
795
Renato Golin728ff0d2011-02-28 22:04:27 +0000796 /* VFPv3 + .fpu */
Anton Korobeynikov4b4e6222012-01-22 12:07:33 +0000797 } else if (Subtarget->hasVFP3()) {
Renato Golin728ff0d2011-02-28 22:04:27 +0000798 AttrEmitter->EmitAttribute(ARMBuildAttrs::VFP_arch,
799 ARMBuildAttrs::AllowFPv3A);
800 if (emitFPU)
801 AttrEmitter->EmitTextAttribute(ARMBuildAttrs::VFP_arch, "vfpv3");
802
803 /* VFPv2 + .fpu */
804 } else if (Subtarget->hasVFP2()) {
Jason W Kimf009a962011-02-07 00:49:53 +0000805 AttrEmitter->EmitAttribute(ARMBuildAttrs::VFP_arch,
806 ARMBuildAttrs::AllowFPv2);
Renato Golin728ff0d2011-02-28 22:04:27 +0000807 if (emitFPU)
808 AttrEmitter->EmitTextAttribute(ARMBuildAttrs::VFP_arch, "vfpv2");
809 }
810
811 /* TODO: ARMBuildAttrs::Allowed is not completely accurate,
Cameron Zwarich375db7f2011-07-07 08:28:52 +0000812 * since NEON can have 1 (allowed) or 2 (MAC operations) */
Renato Golin728ff0d2011-02-28 22:04:27 +0000813 if (Subtarget->hasNEON()) {
814 AttrEmitter->EmitAttribute(ARMBuildAttrs::Advanced_SIMD_arch,
815 ARMBuildAttrs::Allowed);
816 }
Jason W Kimdef9ac42010-10-06 22:36:46 +0000817
818 // Signal various FP modes.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000819 if (!TM.Options.UnsafeFPMath) {
Jason W Kimf009a962011-02-07 00:49:53 +0000820 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_FP_denormal,
821 ARMBuildAttrs::Allowed);
822 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_FP_exceptions,
823 ARMBuildAttrs::Allowed);
Jason W Kimdef9ac42010-10-06 22:36:46 +0000824 }
825
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000826 if (TM.Options.NoInfsFPMath && TM.Options.NoNaNsFPMath)
Jason W Kimf009a962011-02-07 00:49:53 +0000827 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_FP_number_model,
828 ARMBuildAttrs::Allowed);
Jason W Kimdef9ac42010-10-06 22:36:46 +0000829 else
Jason W Kimf009a962011-02-07 00:49:53 +0000830 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_FP_number_model,
831 ARMBuildAttrs::AllowIEE754);
Jason W Kimdef9ac42010-10-06 22:36:46 +0000832
Jason W Kimf009a962011-02-07 00:49:53 +0000833 // FIXME: add more flags to ARMBuildAttrs.h
Jason W Kimdef9ac42010-10-06 22:36:46 +0000834 // 8-bytes alignment stuff.
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000835 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_align8_needed, 1);
836 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_align8_preserved, 1);
Jason W Kimdef9ac42010-10-06 22:36:46 +0000837
838 // Hard float. Use both S and D registers and conform to AAPCS-VFP.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000839 if (Subtarget->isAAPCS_ABI() && TM.Options.FloatABIType == FloatABI::Hard) {
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000840 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_HardFP_use, 3);
841 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_VFP_args, 1);
Jason W Kimdef9ac42010-10-06 22:36:46 +0000842 }
843 // FIXME: Should we signal R9 usage?
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000844
Jason W Kimf009a962011-02-07 00:49:53 +0000845 if (Subtarget->hasDivide())
846 AttrEmitter->EmitAttribute(ARMBuildAttrs::DIV_use, 1);
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000847
848 AttrEmitter->Finish();
849 delete AttrEmitter;
Jason W Kimdef9ac42010-10-06 22:36:46 +0000850}
851
Jason W Kim17b443d2010-10-11 23:01:44 +0000852void ARMAsmPrinter::emitARMAttributeSection() {
853 // <format-version>
854 // [ <section-length> "vendor-name"
855 // [ <file-tag> <size> <attribute>*
856 // | <section-tag> <size> <section-number>* 0 <attribute>*
857 // | <symbol-tag> <size> <symbol-number>* 0 <attribute>*
858 // ]+
859 // ]*
860
861 if (OutStreamer.hasRawTextSupport())
862 return;
863
864 const ARMElfTargetObjectFile &TLOFELF =
865 static_cast<const ARMElfTargetObjectFile &>
866 (getObjFileLowering());
867
868 OutStreamer.SwitchSection(TLOFELF.getAttributesSection());
Jason W Kim17b443d2010-10-11 23:01:44 +0000869
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000870 // Format version
871 OutStreamer.EmitIntValue(0x41, 1);
Jason W Kim17b443d2010-10-11 23:01:44 +0000872}
873
Jason W Kimdef9ac42010-10-06 22:36:46 +0000874//===----------------------------------------------------------------------===//
Chris Lattner97f06932009-10-19 20:20:46 +0000875
Jim Grosbach988ce092010-09-18 00:05:05 +0000876static MCSymbol *getPICLabel(const char *Prefix, unsigned FunctionNumber,
877 unsigned LabelId, MCContext &Ctx) {
878
879 MCSymbol *Label = Ctx.GetOrCreateSymbol(Twine(Prefix)
880 + "PC" + Twine(FunctionNumber) + "_" + Twine(LabelId));
881 return Label;
882}
883
Jim Grosbach2c4d5122010-11-10 03:26:07 +0000884static MCSymbolRefExpr::VariantKind
885getModifierVariantKind(ARMCP::ARMCPModifier Modifier) {
886 switch (Modifier) {
Jim Grosbach2c4d5122010-11-10 03:26:07 +0000887 case ARMCP::no_modifier: return MCSymbolRefExpr::VK_None;
888 case ARMCP::TLSGD: return MCSymbolRefExpr::VK_ARM_TLSGD;
889 case ARMCP::TPOFF: return MCSymbolRefExpr::VK_ARM_TPOFF;
890 case ARMCP::GOTTPOFF: return MCSymbolRefExpr::VK_ARM_GOTTPOFF;
891 case ARMCP::GOT: return MCSymbolRefExpr::VK_ARM_GOT;
892 case ARMCP::GOTOFF: return MCSymbolRefExpr::VK_ARM_GOTOFF;
893 }
David Blaikie4d6ccb52012-01-20 21:51:11 +0000894 llvm_unreachable("Invalid ARMCPModifier!");
Jim Grosbach2c4d5122010-11-10 03:26:07 +0000895}
896
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000897MCSymbol *ARMAsmPrinter::GetARMGVSymbol(const GlobalValue *GV) {
898 bool isIndirect = Subtarget->isTargetDarwin() &&
899 Subtarget->GVIsIndirectSymbol(GV, TM.getRelocationModel());
900 if (!isIndirect)
901 return Mang->getSymbol(GV);
902
903 // FIXME: Remove this when Darwin transition to @GOT like syntax.
904 MCSymbol *MCSym = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
905 MachineModuleInfoMachO &MMIMachO =
906 MMI->getObjFileInfo<MachineModuleInfoMachO>();
907 MachineModuleInfoImpl::StubValueTy &StubSym =
908 GV->hasHiddenVisibility() ? MMIMachO.getHiddenGVStubEntry(MCSym) :
909 MMIMachO.getGVStubEntry(MCSym);
910 if (StubSym.getPointer() == 0)
911 StubSym = MachineModuleInfoImpl::
912 StubValueTy(Mang->getSymbol(GV), !GV->hasInternalLinkage());
913 return MCSym;
914}
915
Jim Grosbach5df08d82010-11-09 18:45:04 +0000916void ARMAsmPrinter::
917EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
Micah Villmow3574eca2012-10-08 16:38:25 +0000918 int Size = TM.getDataLayout()->getTypeAllocSize(MCPV->getType());
Jim Grosbach5df08d82010-11-09 18:45:04 +0000919
920 ARMConstantPoolValue *ACPV = static_cast<ARMConstantPoolValue*>(MCPV);
Jim Grosbach5df08d82010-11-09 18:45:04 +0000921
Jim Grosbach7c7ddb22010-11-10 17:59:10 +0000922 MCSymbol *MCSym;
Jim Grosbach5df08d82010-11-09 18:45:04 +0000923 if (ACPV->isLSDA()) {
Jim Grosbach7c7ddb22010-11-10 17:59:10 +0000924 SmallString<128> Str;
925 raw_svector_ostream OS(Str);
Jim Grosbach5df08d82010-11-09 18:45:04 +0000926 OS << MAI->getPrivateGlobalPrefix() << "_LSDA_" << getFunctionNumber();
Jim Grosbach7c7ddb22010-11-10 17:59:10 +0000927 MCSym = OutContext.GetOrCreateSymbol(OS.str());
Jim Grosbach5df08d82010-11-09 18:45:04 +0000928 } else if (ACPV->isBlockAddress()) {
Bill Wendling5bb77992011-10-01 08:00:54 +0000929 const BlockAddress *BA =
930 cast<ARMConstantPoolConstant>(ACPV)->getBlockAddress();
931 MCSym = GetBlockAddressSymbol(BA);
Jim Grosbach5df08d82010-11-09 18:45:04 +0000932 } else if (ACPV->isGlobalValue()) {
Bill Wendling5bb77992011-10-01 08:00:54 +0000933 const GlobalValue *GV = cast<ARMConstantPoolConstant>(ACPV)->getGV();
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000934 MCSym = GetARMGVSymbol(GV);
Bill Wendlinge00897c2011-09-29 23:50:42 +0000935 } else if (ACPV->isMachineBasicBlock()) {
Bill Wendling3320f2a2011-10-01 09:30:42 +0000936 const MachineBasicBlock *MBB = cast<ARMConstantPoolMBB>(ACPV)->getMBB();
Bill Wendlinge00897c2011-09-29 23:50:42 +0000937 MCSym = MBB->getSymbol();
Jim Grosbach5df08d82010-11-09 18:45:04 +0000938 } else {
939 assert(ACPV->isExtSymbol() && "unrecognized constant pool value");
Bill Wendlingfe31e672011-10-01 08:58:29 +0000940 const char *Sym = cast<ARMConstantPoolSymbol>(ACPV)->getSymbol();
941 MCSym = GetExternalSymbolSymbol(Sym);
Jim Grosbach5df08d82010-11-09 18:45:04 +0000942 }
943
944 // Create an MCSymbol for the reference.
Jim Grosbach2c4d5122010-11-10 03:26:07 +0000945 const MCExpr *Expr =
946 MCSymbolRefExpr::Create(MCSym, getModifierVariantKind(ACPV->getModifier()),
947 OutContext);
Jim Grosbach5df08d82010-11-09 18:45:04 +0000948
Jim Grosbach2c4d5122010-11-10 03:26:07 +0000949 if (ACPV->getPCAdjustment()) {
950 MCSymbol *PCLabel = getPICLabel(MAI->getPrivateGlobalPrefix(),
951 getFunctionNumber(),
952 ACPV->getLabelId(),
953 OutContext);
954 const MCExpr *PCRelExpr = MCSymbolRefExpr::Create(PCLabel, OutContext);
955 PCRelExpr =
956 MCBinaryExpr::CreateAdd(PCRelExpr,
957 MCConstantExpr::Create(ACPV->getPCAdjustment(),
958 OutContext),
959 OutContext);
960 if (ACPV->mustAddCurrentAddress()) {
961 // We want "(<expr> - .)", but MC doesn't have a concept of the '.'
962 // label, so just emit a local label end reference that instead.
963 MCSymbol *DotSym = OutContext.CreateTempSymbol();
964 OutStreamer.EmitLabel(DotSym);
965 const MCExpr *DotExpr = MCSymbolRefExpr::Create(DotSym, OutContext);
966 PCRelExpr = MCBinaryExpr::CreateSub(PCRelExpr, DotExpr, OutContext);
Jim Grosbach5df08d82010-11-09 18:45:04 +0000967 }
Jim Grosbach2c4d5122010-11-10 03:26:07 +0000968 Expr = MCBinaryExpr::CreateSub(Expr, PCRelExpr, OutContext);
Jim Grosbach5df08d82010-11-09 18:45:04 +0000969 }
Jim Grosbach2c4d5122010-11-10 03:26:07 +0000970 OutStreamer.EmitValue(Expr, Size);
Jim Grosbach5df08d82010-11-09 18:45:04 +0000971}
972
Jim Grosbacha2244cb2010-09-22 17:39:48 +0000973void ARMAsmPrinter::EmitJumpTable(const MachineInstr *MI) {
974 unsigned Opcode = MI->getOpcode();
975 int OpNum = 1;
976 if (Opcode == ARM::BR_JTadd)
977 OpNum = 2;
978 else if (Opcode == ARM::BR_JTm)
979 OpNum = 3;
980
981 const MachineOperand &MO1 = MI->getOperand(OpNum);
982 const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
983 unsigned JTI = MO1.getIndex();
984
985 // Emit a label for the jump table.
986 MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel2(JTI, MO2.getImm());
987 OutStreamer.EmitLabel(JTISymbol);
988
Jim Grosbach3e965312012-05-18 19:12:01 +0000989 // Mark the jump table as data-in-code.
990 OutStreamer.EmitDataRegion(MCDR_DataRegionJT32);
991
Jim Grosbacha2244cb2010-09-22 17:39:48 +0000992 // Emit each entry of the table.
993 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
994 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
995 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
996
997 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
998 MachineBasicBlock *MBB = JTBBs[i];
999 // Construct an MCExpr for the entry. We want a value of the form:
1000 // (BasicBlockAddr - TableBeginAddr)
1001 //
1002 // For example, a table with entries jumping to basic blocks BB0 and BB1
1003 // would look like:
1004 // LJTI_0_0:
1005 // .word (LBB0 - LJTI_0_0)
1006 // .word (LBB1 - LJTI_0_0)
1007 const MCExpr *Expr = MCSymbolRefExpr::Create(MBB->getSymbol(), OutContext);
1008
1009 if (TM.getRelocationModel() == Reloc::PIC_)
1010 Expr = MCBinaryExpr::CreateSub(Expr, MCSymbolRefExpr::Create(JTISymbol,
1011 OutContext),
1012 OutContext);
Jim Grosbachde982732011-08-31 22:23:09 +00001013 // If we're generating a table of Thumb addresses in static relocation
1014 // model, we need to add one to keep interworking correctly.
1015 else if (AFI->isThumbFunction())
1016 Expr = MCBinaryExpr::CreateAdd(Expr, MCConstantExpr::Create(1,OutContext),
1017 OutContext);
Jim Grosbacha2244cb2010-09-22 17:39:48 +00001018 OutStreamer.EmitValue(Expr, 4);
1019 }
Jim Grosbach3e965312012-05-18 19:12:01 +00001020 // Mark the end of jump table data-in-code region.
1021 OutStreamer.EmitDataRegion(MCDR_DataRegionEnd);
Jim Grosbacha2244cb2010-09-22 17:39:48 +00001022}
1023
Jim Grosbach882ef2b2010-09-21 23:28:16 +00001024void ARMAsmPrinter::EmitJump2Table(const MachineInstr *MI) {
1025 unsigned Opcode = MI->getOpcode();
1026 int OpNum = (Opcode == ARM::t2BR_JT) ? 2 : 1;
1027 const MachineOperand &MO1 = MI->getOperand(OpNum);
1028 const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
1029 unsigned JTI = MO1.getIndex();
1030
Jim Grosbach882ef2b2010-09-21 23:28:16 +00001031 MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel2(JTI, MO2.getImm());
1032 OutStreamer.EmitLabel(JTISymbol);
1033
1034 // Emit each entry of the table.
1035 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
1036 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
1037 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
Jim Grosbach205a5fa2010-09-22 17:15:35 +00001038 unsigned OffsetWidth = 4;
Jim Grosbach3e965312012-05-18 19:12:01 +00001039 if (MI->getOpcode() == ARM::t2TBB_JT) {
Jim Grosbach205a5fa2010-09-22 17:15:35 +00001040 OffsetWidth = 1;
Jim Grosbach3e965312012-05-18 19:12:01 +00001041 // Mark the jump table as data-in-code.
1042 OutStreamer.EmitDataRegion(MCDR_DataRegionJT8);
1043 } else if (MI->getOpcode() == ARM::t2TBH_JT) {
Jim Grosbach205a5fa2010-09-22 17:15:35 +00001044 OffsetWidth = 2;
Jim Grosbach3e965312012-05-18 19:12:01 +00001045 // Mark the jump table as data-in-code.
1046 OutStreamer.EmitDataRegion(MCDR_DataRegionJT16);
1047 }
Jim Grosbach882ef2b2010-09-21 23:28:16 +00001048
1049 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
1050 MachineBasicBlock *MBB = JTBBs[i];
Jim Grosbach205a5fa2010-09-22 17:15:35 +00001051 const MCExpr *MBBSymbolExpr = MCSymbolRefExpr::Create(MBB->getSymbol(),
1052 OutContext);
Jim Grosbach882ef2b2010-09-21 23:28:16 +00001053 // If this isn't a TBB or TBH, the entries are direct branch instructions.
Jim Grosbach205a5fa2010-09-22 17:15:35 +00001054 if (OffsetWidth == 4) {
Benjamin Kramer391271f2012-11-26 13:34:22 +00001055 MCInstBuilder(ARM::t2B)
1056 .addExpr(MBBSymbolExpr)
1057 .addImm(ARMCC::AL)
1058 .addReg(0)
1059 .emit(OutStreamer);
Jim Grosbach882ef2b2010-09-21 23:28:16 +00001060 continue;
1061 }
1062 // Otherwise it's an offset from the dispatch instruction. Construct an
Jim Grosbach205a5fa2010-09-22 17:15:35 +00001063 // MCExpr for the entry. We want a value of the form:
1064 // (BasicBlockAddr - TableBeginAddr) / 2
1065 //
1066 // For example, a TBB table with entries jumping to basic blocks BB0 and BB1
1067 // would look like:
1068 // LJTI_0_0:
1069 // .byte (LBB0 - LJTI_0_0) / 2
1070 // .byte (LBB1 - LJTI_0_0) / 2
1071 const MCExpr *Expr =
1072 MCBinaryExpr::CreateSub(MBBSymbolExpr,
1073 MCSymbolRefExpr::Create(JTISymbol, OutContext),
1074 OutContext);
1075 Expr = MCBinaryExpr::CreateDiv(Expr, MCConstantExpr::Create(2, OutContext),
1076 OutContext);
1077 OutStreamer.EmitValue(Expr, OffsetWidth);
Jim Grosbach882ef2b2010-09-21 23:28:16 +00001078 }
Jim Grosbachb3a119a2012-05-21 23:34:42 +00001079 // Mark the end of jump table data-in-code region. 32-bit offsets use
1080 // actual branch instructions here, so we don't mark those as a data-region
1081 // at all.
1082 if (OffsetWidth != 4)
1083 OutStreamer.EmitDataRegion(MCDR_DataRegionEnd);
Jim Grosbach882ef2b2010-09-21 23:28:16 +00001084}
1085
Jim Grosbach2d0f53b2010-09-28 17:05:56 +00001086void ARMAsmPrinter::PrintDebugValueComment(const MachineInstr *MI,
1087 raw_ostream &OS) {
1088 unsigned NOps = MI->getNumOperands();
1089 assert(NOps==4);
1090 OS << '\t' << MAI->getCommentString() << "DEBUG_VALUE: ";
1091 // cast away const; DIetc do not take const operands for some reason.
1092 DIVariable V(const_cast<MDNode *>(MI->getOperand(NOps-1).getMetadata()));
1093 OS << V.getName();
1094 OS << " <- ";
1095 // Frame address. Currently handles register +- offset only.
1096 assert(MI->getOperand(0).isReg() && MI->getOperand(1).isImm());
1097 OS << '['; printOperand(MI, 0, OS); OS << '+'; printOperand(MI, 1, OS);
1098 OS << ']';
1099 OS << "+";
1100 printOperand(MI, NOps-2, OS);
1101}
1102
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001103void ARMAsmPrinter::EmitUnwindingInstruction(const MachineInstr *MI) {
1104 assert(MI->getFlag(MachineInstr::FrameSetup) &&
1105 "Only instruction which are involved into frame setup code are allowed");
1106
1107 const MachineFunction &MF = *MI->getParent()->getParent();
1108 const TargetRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo();
Anton Korobeynikovb3fcc062011-03-05 18:43:55 +00001109 const ARMFunctionInfo &AFI = *MF.getInfo<ARMFunctionInfo>();
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001110
1111 unsigned FramePtr = RegInfo->getFrameRegister(MF);
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001112 unsigned Opc = MI->getOpcode();
Anton Korobeynikov7a764162011-03-05 18:43:43 +00001113 unsigned SrcReg, DstReg;
1114
Anton Korobeynikov3daccd82011-03-05 18:43:50 +00001115 if (Opc == ARM::tPUSH || Opc == ARM::tLDRpci) {
1116 // Two special cases:
1117 // 1) tPUSH does not have src/dst regs.
1118 // 2) for Thumb1 code we sometimes materialize the constant via constpool
1119 // load. Yes, this is pretty fragile, but for now I don't see better
1120 // way... :(
Anton Korobeynikov7a764162011-03-05 18:43:43 +00001121 SrcReg = DstReg = ARM::SP;
1122 } else {
Anton Korobeynikov3daccd82011-03-05 18:43:50 +00001123 SrcReg = MI->getOperand(1).getReg();
Anton Korobeynikov7a764162011-03-05 18:43:43 +00001124 DstReg = MI->getOperand(0).getReg();
1125 }
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001126
1127 // Try to figure out the unwinding opcode out of src / dst regs.
Evan Cheng5a96b3d2011-12-07 07:15:52 +00001128 if (MI->mayStore()) {
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001129 // Register saves.
1130 assert(DstReg == ARM::SP &&
1131 "Only stack pointer as a destination reg is supported");
1132
1133 SmallVector<unsigned, 4> RegList;
Anton Korobeynikov7a764162011-03-05 18:43:43 +00001134 // Skip src & dst reg, and pred ops.
1135 unsigned StartOp = 2 + 2;
1136 // Use all the operands.
1137 unsigned NumOffset = 0;
1138
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001139 switch (Opc) {
1140 default:
1141 MI->dump();
Craig Topperbc219812012-02-07 02:50:20 +00001142 llvm_unreachable("Unsupported opcode for unwinding information");
Anton Korobeynikov7a764162011-03-05 18:43:43 +00001143 case ARM::tPUSH:
1144 // Special case here: no src & dst reg, but two extra imp ops.
1145 StartOp = 2; NumOffset = 2;
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001146 case ARM::STMDB_UPD:
Anton Korobeynikov7a764162011-03-05 18:43:43 +00001147 case ARM::t2STMDB_UPD:
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001148 case ARM::VSTMDDB_UPD:
1149 assert(SrcReg == ARM::SP &&
1150 "Only stack pointer as a source reg is supported");
Anton Korobeynikov7a764162011-03-05 18:43:43 +00001151 for (unsigned i = StartOp, NumOps = MI->getNumOperands() - NumOffset;
Anton Korobeynikovad62e922012-08-04 13:25:58 +00001152 i != NumOps; ++i) {
1153 const MachineOperand &MO = MI->getOperand(i);
1154 // Actually, there should never be any impdef stuff here. Skip it
1155 // temporary to workaround PR11902.
1156 if (MO.isImplicit())
1157 continue;
1158 RegList.push_back(MO.getReg());
1159 }
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001160 break;
Owen Anderson793e7962011-07-26 20:54:26 +00001161 case ARM::STR_PRE_IMM:
1162 case ARM::STR_PRE_REG:
Evgeniy Stepanov73dd8bb2012-01-19 12:53:06 +00001163 case ARM::t2STR_PRE:
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001164 assert(MI->getOperand(2).getReg() == ARM::SP &&
1165 "Only stack pointer as a source reg is supported");
1166 RegList.push_back(SrcReg);
1167 break;
1168 }
1169 OutStreamer.EmitRegSave(RegList, Opc == ARM::VSTMDDB_UPD);
1170 } else {
1171 // Changes of stack / frame pointer.
1172 if (SrcReg == ARM::SP) {
1173 int64_t Offset = 0;
1174 switch (Opc) {
1175 default:
1176 MI->dump();
Craig Topperbc219812012-02-07 02:50:20 +00001177 llvm_unreachable("Unsupported opcode for unwinding information");
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001178 case ARM::MOVr:
Evgeniy Stepanov73dd8bb2012-01-19 12:53:06 +00001179 case ARM::tMOVr:
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001180 Offset = 0;
1181 break;
1182 case ARM::ADDri:
1183 Offset = -MI->getOperand(2).getImm();
1184 break;
1185 case ARM::SUBri:
Evgeniy Stepanov73dd8bb2012-01-19 12:53:06 +00001186 case ARM::t2SUBri:
Jim Grosbachf6fd9092011-06-29 23:25:04 +00001187 Offset = MI->getOperand(2).getImm();
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001188 break;
Anton Korobeynikov7a764162011-03-05 18:43:43 +00001189 case ARM::tSUBspi:
Jim Grosbachf6fd9092011-06-29 23:25:04 +00001190 Offset = MI->getOperand(2).getImm()*4;
Anton Korobeynikov7a764162011-03-05 18:43:43 +00001191 break;
1192 case ARM::tADDspi:
1193 case ARM::tADDrSPi:
1194 Offset = -MI->getOperand(2).getImm()*4;
1195 break;
Anton Korobeynikovb3fcc062011-03-05 18:43:55 +00001196 case ARM::tLDRpci: {
1197 // Grab the constpool index and check, whether it corresponds to
1198 // original or cloned constpool entry.
1199 unsigned CPI = MI->getOperand(1).getIndex();
1200 const MachineConstantPool *MCP = MF.getConstantPool();
1201 if (CPI >= MCP->getConstants().size())
1202 CPI = AFI.getOriginalCPIdx(CPI);
1203 assert(CPI != -1U && "Invalid constpool index");
1204
1205 // Derive the actual offset.
1206 const MachineConstantPoolEntry &CPE = MCP->getConstants()[CPI];
1207 assert(!CPE.isMachineConstantPoolEntry() && "Invalid constpool entry");
1208 // FIXME: Check for user, it should be "add" instruction!
1209 Offset = -cast<ConstantInt>(CPE.Val.ConstVal)->getSExtValue();
Anton Korobeynikov3daccd82011-03-05 18:43:50 +00001210 break;
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001211 }
Anton Korobeynikovb3fcc062011-03-05 18:43:55 +00001212 }
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001213
1214 if (DstReg == FramePtr && FramePtr != ARM::SP)
Anton Korobeynikove5163792011-03-05 18:44:00 +00001215 // Set-up of the frame pointer. Positive values correspond to "add"
1216 // instruction.
1217 OutStreamer.EmitSetFP(FramePtr, ARM::SP, -Offset);
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001218 else if (DstReg == ARM::SP) {
Anton Korobeynikove5163792011-03-05 18:44:00 +00001219 // Change of SP by an offset. Positive values correspond to "sub"
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001220 // instruction.
1221 OutStreamer.EmitPad(Offset);
1222 } else {
1223 MI->dump();
Craig Topperbc219812012-02-07 02:50:20 +00001224 llvm_unreachable("Unsupported opcode for unwinding information");
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001225 }
1226 } else if (DstReg == ARM::SP) {
1227 // FIXME: .movsp goes here
1228 MI->dump();
Craig Topperbc219812012-02-07 02:50:20 +00001229 llvm_unreachable("Unsupported opcode for unwinding information");
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001230 }
1231 else {
1232 MI->dump();
Craig Topperbc219812012-02-07 02:50:20 +00001233 llvm_unreachable("Unsupported opcode for unwinding information");
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001234 }
1235 }
1236}
1237
Chandler Carruth3eb4be02012-01-24 00:30:17 +00001238extern cl::opt<bool> EnableARMEHABI;
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001239
Jim Grosbach53e3fc42011-07-08 17:40:42 +00001240// Simple pseudo-instructions have their lowering (with expansion to real
1241// instructions) auto-generated.
1242#include "ARMGenMCPseudoLowering.inc"
1243
Jim Grosbachb454cda2010-09-29 15:23:40 +00001244void ARMAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Jim Grosbach3e965312012-05-18 19:12:01 +00001245 // If we just ended a constant pool, mark it as such.
1246 if (InConstantPool && MI->getOpcode() != ARM::CONSTPOOL_ENTRY) {
1247 OutStreamer.EmitDataRegion(MCDR_DataRegionEnd);
1248 InConstantPool = false;
1249 }
Owen Anderson2fec6c52011-10-04 23:26:17 +00001250
Jim Grosbach5aa29a02011-08-23 21:32:34 +00001251 // Emit unwinding stuff for frame-related instructions
Chandler Carruth3eb4be02012-01-24 00:30:17 +00001252 if (EnableARMEHABI && MI->getFlag(MachineInstr::FrameSetup))
Jim Grosbach5aa29a02011-08-23 21:32:34 +00001253 EmitUnwindingInstruction(MI);
1254
Jim Grosbach53e3fc42011-07-08 17:40:42 +00001255 // Do any auto-generated pseudo lowerings.
1256 if (emitPseudoExpansionLowering(OutStreamer, MI))
1257 return;
1258
Andrew Trick3be654f2011-09-21 02:20:46 +00001259 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
1260 "Pseudo flag setting opcode should be expanded early");
1261
Jim Grosbach53e3fc42011-07-08 17:40:42 +00001262 // Check for manual lowerings.
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001263 unsigned Opc = MI->getOpcode();
1264 switch (Opc) {
Craig Topperbc219812012-02-07 02:50:20 +00001265 case ARM::t2MOVi32imm: llvm_unreachable("Should be lowered by thumb2it pass");
Jim Grosbach2d0f53b2010-09-28 17:05:56 +00001266 case ARM::DBG_VALUE: {
1267 if (isVerbose() && OutStreamer.hasRawTextSupport()) {
1268 SmallString<128> TmpStr;
1269 raw_svector_ostream OS(TmpStr);
1270 PrintDebugValueComment(MI, OS);
1271 OutStreamer.EmitRawText(StringRef(OS.str()));
1272 }
1273 return;
1274 }
Jim Grosbach40edf732010-12-14 21:10:47 +00001275 case ARM::LEApcrel:
Jim Grosbachd40963c2010-12-14 22:28:03 +00001276 case ARM::tLEApcrel:
Jim Grosbach40edf732010-12-14 21:10:47 +00001277 case ARM::t2LEApcrel: {
Jim Grosbachdff84b02010-12-02 00:28:45 +00001278 // FIXME: Need to also handle globals and externals
Benjamin Kramer391271f2012-11-26 13:34:22 +00001279 MCSymbol *CPISymbol = GetCPISymbol(MI->getOperand(1).getIndex());
1280 MCInstBuilder(MI->getOpcode() == ARM::t2LEApcrel ? ARM::t2ADR
1281 : (MI->getOpcode() == ARM::tLEApcrel ? ARM::tADR
1282 : ARM::ADR))
1283 .addReg(MI->getOperand(0).getReg())
1284 .addExpr(MCSymbolRefExpr::Create(CPISymbol, OutContext))
1285 // Add predicate operands.
1286 .addImm(MI->getOperand(2).getImm())
1287 .addReg(MI->getOperand(3).getReg())
1288 .emit(OutStreamer);
Jim Grosbachdff84b02010-12-02 00:28:45 +00001289 return;
1290 }
Jim Grosbachd40963c2010-12-14 22:28:03 +00001291 case ARM::LEApcrelJT:
1292 case ARM::tLEApcrelJT:
1293 case ARM::t2LEApcrelJT: {
Benjamin Kramer391271f2012-11-26 13:34:22 +00001294 MCSymbol *JTIPICSymbol =
1295 GetARMJTIPICJumpTableLabel2(MI->getOperand(1).getIndex(),
1296 MI->getOperand(2).getImm());
1297 MCInstBuilder(MI->getOpcode() == ARM::t2LEApcrelJT ? ARM::t2ADR
1298 : (MI->getOpcode() == ARM::tLEApcrelJT ? ARM::tADR
1299 : ARM::ADR))
1300 .addReg(MI->getOperand(0).getReg())
1301 .addExpr(MCSymbolRefExpr::Create(JTIPICSymbol, OutContext))
1302 // Add predicate operands.
1303 .addImm(MI->getOperand(3).getImm())
1304 .addReg(MI->getOperand(4).getReg())
1305 .emit(OutStreamer);
Jim Grosbach5d14f9b2010-12-01 19:47:31 +00001306 return;
1307 }
Jim Grosbachf859a542011-03-12 00:45:26 +00001308 // Darwin call instructions are just normal call instructions with different
1309 // clobber semantics (they clobber R9).
Jim Grosbacha0d2c8a2010-11-30 18:30:19 +00001310 case ARM::BX_CALL: {
Benjamin Kramer391271f2012-11-26 13:34:22 +00001311 MCInstBuilder(ARM::MOVr)
1312 .addReg(ARM::LR)
1313 .addReg(ARM::PC)
Jim Grosbacha0d2c8a2010-11-30 18:30:19 +00001314 // Add predicate operands.
Benjamin Kramer391271f2012-11-26 13:34:22 +00001315 .addImm(ARMCC::AL)
1316 .addReg(0)
Jim Grosbacha0d2c8a2010-11-30 18:30:19 +00001317 // Add 's' bit operand (always reg0 for this)
Benjamin Kramer391271f2012-11-26 13:34:22 +00001318 .addReg(0)
1319 .emit(OutStreamer);
1320
1321 MCInstBuilder(ARM::BX)
1322 .addReg(MI->getOperand(0).getReg())
1323 .emit(OutStreamer);
Jim Grosbacha0d2c8a2010-11-30 18:30:19 +00001324 return;
1325 }
Cameron Zwarichad70f6d2011-05-25 21:53:50 +00001326 case ARM::tBX_CALL: {
Benjamin Kramer391271f2012-11-26 13:34:22 +00001327 MCInstBuilder(ARM::tMOVr)
1328 .addReg(ARM::LR)
1329 .addReg(ARM::PC)
Jim Grosbach63b46fa2011-06-30 22:10:46 +00001330 // Add predicate operands.
Benjamin Kramer391271f2012-11-26 13:34:22 +00001331 .addImm(ARMCC::AL)
1332 .addReg(0)
1333 .emit(OutStreamer);
1334
1335 MCInstBuilder(ARM::tBX)
1336 .addReg(MI->getOperand(0).getReg())
Cameron Zwarichad70f6d2011-05-25 21:53:50 +00001337 // Add predicate operands.
Benjamin Kramer391271f2012-11-26 13:34:22 +00001338 .addImm(ARMCC::AL)
1339 .addReg(0)
1340 .emit(OutStreamer);
Cameron Zwarichad70f6d2011-05-25 21:53:50 +00001341 return;
1342 }
Jim Grosbacha0d2c8a2010-11-30 18:30:19 +00001343 case ARM::BMOVPCRX_CALL: {
Benjamin Kramer391271f2012-11-26 13:34:22 +00001344 MCInstBuilder(ARM::MOVr)
1345 .addReg(ARM::LR)
1346 .addReg(ARM::PC)
Jim Grosbacha0d2c8a2010-11-30 18:30:19 +00001347 // Add predicate operands.
Benjamin Kramer391271f2012-11-26 13:34:22 +00001348 .addImm(ARMCC::AL)
1349 .addReg(0)
Jim Grosbacha0d2c8a2010-11-30 18:30:19 +00001350 // Add 's' bit operand (always reg0 for this)
Benjamin Kramer391271f2012-11-26 13:34:22 +00001351 .addReg(0)
1352 .emit(OutStreamer);
1353
1354 MCInstBuilder(ARM::MOVr)
1355 .addReg(ARM::PC)
1356 .addImm(MI->getOperand(0).getReg())
Jim Grosbacha0d2c8a2010-11-30 18:30:19 +00001357 // Add predicate operands.
Benjamin Kramer391271f2012-11-26 13:34:22 +00001358 .addImm(ARMCC::AL)
1359 .addReg(0)
Jim Grosbacha0d2c8a2010-11-30 18:30:19 +00001360 // Add 's' bit operand (always reg0 for this)
Benjamin Kramer391271f2012-11-26 13:34:22 +00001361 .addReg(0)
1362 .emit(OutStreamer);
Jim Grosbacha0d2c8a2010-11-30 18:30:19 +00001363 return;
1364 }
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001365 case ARM::BMOVPCB_CALL: {
Benjamin Kramer391271f2012-11-26 13:34:22 +00001366 MCInstBuilder(ARM::MOVr)
1367 .addReg(ARM::LR)
1368 .addReg(ARM::PC)
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001369 // Add predicate operands.
Benjamin Kramer391271f2012-11-26 13:34:22 +00001370 .addImm(ARMCC::AL)
1371 .addReg(0)
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001372 // Add 's' bit operand (always reg0 for this)
Benjamin Kramer391271f2012-11-26 13:34:22 +00001373 .addReg(0)
1374 .emit(OutStreamer);
1375
1376 const GlobalValue *GV = MI->getOperand(0).getGlobal();
1377 MCSymbol *GVSym = Mang->getSymbol(GV);
1378 const MCExpr *GVSymExpr = MCSymbolRefExpr::Create(GVSym, OutContext);
1379 MCInstBuilder(ARM::Bcc)
1380 .addExpr(GVSymExpr)
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001381 // Add predicate operands.
Benjamin Kramer391271f2012-11-26 13:34:22 +00001382 .addImm(ARMCC::AL)
1383 .addReg(0)
1384 .emit(OutStreamer);
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001385 return;
1386 }
Evan Cheng53519f02011-01-21 18:55:51 +00001387 case ARM::MOVi16_ga_pcrel:
1388 case ARM::t2MOVi16_ga_pcrel: {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001389 MCInst TmpInst;
Evan Cheng53519f02011-01-21 18:55:51 +00001390 TmpInst.setOpcode(Opc == ARM::MOVi16_ga_pcrel? ARM::MOVi16 : ARM::t2MOVi16);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001391 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1392
Evan Cheng53519f02011-01-21 18:55:51 +00001393 unsigned TF = MI->getOperand(1).getTargetFlags();
1394 bool isPIC = TF == ARMII::MO_LO16_NONLAZY_PIC;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001395 const GlobalValue *GV = MI->getOperand(1).getGlobal();
1396 MCSymbol *GVSym = GetARMGVSymbol(GV);
1397 const MCExpr *GVSymExpr = MCSymbolRefExpr::Create(GVSym, OutContext);
Evan Cheng53519f02011-01-21 18:55:51 +00001398 if (isPIC) {
1399 MCSymbol *LabelSym = getPICLabel(MAI->getPrivateGlobalPrefix(),
1400 getFunctionNumber(),
1401 MI->getOperand(2).getImm(), OutContext);
1402 const MCExpr *LabelSymExpr= MCSymbolRefExpr::Create(LabelSym, OutContext);
1403 unsigned PCAdj = (Opc == ARM::MOVi16_ga_pcrel) ? 8 : 4;
1404 const MCExpr *PCRelExpr =
1405 ARMMCExpr::CreateLower16(MCBinaryExpr::CreateSub(GVSymExpr,
1406 MCBinaryExpr::CreateAdd(LabelSymExpr,
1407 MCConstantExpr::Create(PCAdj, OutContext),
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001408 OutContext), OutContext), OutContext);
Evan Cheng53519f02011-01-21 18:55:51 +00001409 TmpInst.addOperand(MCOperand::CreateExpr(PCRelExpr));
1410 } else {
1411 const MCExpr *RefExpr= ARMMCExpr::CreateLower16(GVSymExpr, OutContext);
1412 TmpInst.addOperand(MCOperand::CreateExpr(RefExpr));
1413 }
1414
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001415 // Add predicate operands.
1416 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1417 TmpInst.addOperand(MCOperand::CreateReg(0));
1418 // Add 's' bit operand (always reg0 for this)
1419 TmpInst.addOperand(MCOperand::CreateReg(0));
1420 OutStreamer.EmitInstruction(TmpInst);
1421 return;
1422 }
Evan Cheng53519f02011-01-21 18:55:51 +00001423 case ARM::MOVTi16_ga_pcrel:
1424 case ARM::t2MOVTi16_ga_pcrel: {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001425 MCInst TmpInst;
Evan Cheng53519f02011-01-21 18:55:51 +00001426 TmpInst.setOpcode(Opc == ARM::MOVTi16_ga_pcrel
1427 ? ARM::MOVTi16 : ARM::t2MOVTi16);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001428 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1429 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
1430
Evan Cheng53519f02011-01-21 18:55:51 +00001431 unsigned TF = MI->getOperand(2).getTargetFlags();
1432 bool isPIC = TF == ARMII::MO_HI16_NONLAZY_PIC;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001433 const GlobalValue *GV = MI->getOperand(2).getGlobal();
1434 MCSymbol *GVSym = GetARMGVSymbol(GV);
1435 const MCExpr *GVSymExpr = MCSymbolRefExpr::Create(GVSym, OutContext);
Evan Cheng53519f02011-01-21 18:55:51 +00001436 if (isPIC) {
1437 MCSymbol *LabelSym = getPICLabel(MAI->getPrivateGlobalPrefix(),
1438 getFunctionNumber(),
1439 MI->getOperand(3).getImm(), OutContext);
1440 const MCExpr *LabelSymExpr= MCSymbolRefExpr::Create(LabelSym, OutContext);
1441 unsigned PCAdj = (Opc == ARM::MOVTi16_ga_pcrel) ? 8 : 4;
1442 const MCExpr *PCRelExpr =
1443 ARMMCExpr::CreateUpper16(MCBinaryExpr::CreateSub(GVSymExpr,
1444 MCBinaryExpr::CreateAdd(LabelSymExpr,
1445 MCConstantExpr::Create(PCAdj, OutContext),
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001446 OutContext), OutContext), OutContext);
Evan Cheng53519f02011-01-21 18:55:51 +00001447 TmpInst.addOperand(MCOperand::CreateExpr(PCRelExpr));
1448 } else {
1449 const MCExpr *RefExpr= ARMMCExpr::CreateUpper16(GVSymExpr, OutContext);
1450 TmpInst.addOperand(MCOperand::CreateExpr(RefExpr));
1451 }
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001452 // Add predicate operands.
1453 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1454 TmpInst.addOperand(MCOperand::CreateReg(0));
1455 // Add 's' bit operand (always reg0 for this)
1456 TmpInst.addOperand(MCOperand::CreateReg(0));
1457 OutStreamer.EmitInstruction(TmpInst);
1458 return;
1459 }
Jim Grosbachfbd18732010-09-17 23:41:53 +00001460 case ARM::tPICADD: {
1461 // This is a pseudo op for a label + instruction sequence, which looks like:
1462 // LPC0:
1463 // add r0, pc
1464 // This adds the address of LPC0 to r0.
1465
1466 // Emit the label.
Jim Grosbach988ce092010-09-18 00:05:05 +00001467 OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(),
1468 getFunctionNumber(), MI->getOperand(2).getImm(),
1469 OutContext));
Jim Grosbachfbd18732010-09-17 23:41:53 +00001470
1471 // Form and emit the add.
Benjamin Kramer391271f2012-11-26 13:34:22 +00001472 MCInstBuilder(ARM::tADDhirr)
1473 .addReg(MI->getOperand(0).getReg())
1474 .addReg(MI->getOperand(0).getReg())
1475 .addReg(ARM::PC)
1476 // Add predicate operands.
1477 .addImm(ARMCC::AL)
1478 .addReg(0)
1479 .emit(OutStreamer);
Jim Grosbachfbd18732010-09-17 23:41:53 +00001480 return;
1481 }
Jim Grosbacha3fbadf2010-09-30 19:53:58 +00001482 case ARM::PICADD: {
Chris Lattner4d152222009-10-19 22:23:04 +00001483 // This is a pseudo op for a label + instruction sequence, which looks like:
1484 // LPC0:
1485 // add r0, pc, r0
1486 // This adds the address of LPC0 to r0.
Jim Grosbachb0739b72010-09-02 01:02:06 +00001487
Chris Lattner4d152222009-10-19 22:23:04 +00001488 // Emit the label.
Jim Grosbach988ce092010-09-18 00:05:05 +00001489 OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(),
1490 getFunctionNumber(), MI->getOperand(2).getImm(),
1491 OutContext));
Jim Grosbachb0739b72010-09-02 01:02:06 +00001492
Jim Grosbachf3f09522010-09-14 21:05:34 +00001493 // Form and emit the add.
Benjamin Kramer391271f2012-11-26 13:34:22 +00001494 MCInstBuilder(ARM::ADDrr)
1495 .addReg(MI->getOperand(0).getReg())
1496 .addReg(ARM::PC)
1497 .addReg(MI->getOperand(1).getReg())
1498 // Add predicate operands.
1499 .addImm(MI->getOperand(3).getImm())
1500 .addReg(MI->getOperand(4).getReg())
1501 // Add 's' bit operand (always reg0 for this)
1502 .addReg(0)
1503 .emit(OutStreamer);
Chris Lattner4d152222009-10-19 22:23:04 +00001504 return;
1505 }
Jim Grosbacha28abbe2010-09-17 16:25:52 +00001506 case ARM::PICSTR:
1507 case ARM::PICSTRB:
1508 case ARM::PICSTRH:
1509 case ARM::PICLDR:
1510 case ARM::PICLDRB:
1511 case ARM::PICLDRH:
1512 case ARM::PICLDRSB:
1513 case ARM::PICLDRSH: {
Jim Grosbachb74ca9d2010-09-16 17:43:25 +00001514 // This is a pseudo op for a label + instruction sequence, which looks like:
1515 // LPC0:
Jim Grosbacha28abbe2010-09-17 16:25:52 +00001516 // OP r0, [pc, r0]
Jim Grosbachb74ca9d2010-09-16 17:43:25 +00001517 // The LCP0 label is referenced by a constant pool entry in order to get
1518 // a PC-relative address at the ldr instruction.
1519
1520 // Emit the label.
Jim Grosbach988ce092010-09-18 00:05:05 +00001521 OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(),
1522 getFunctionNumber(), MI->getOperand(2).getImm(),
1523 OutContext));
Jim Grosbachb74ca9d2010-09-16 17:43:25 +00001524
1525 // Form and emit the load
Jim Grosbacha28abbe2010-09-17 16:25:52 +00001526 unsigned Opcode;
1527 switch (MI->getOpcode()) {
1528 default:
1529 llvm_unreachable("Unexpected opcode!");
Jim Grosbach7e3383c2010-10-27 23:12:14 +00001530 case ARM::PICSTR: Opcode = ARM::STRrs; break;
1531 case ARM::PICSTRB: Opcode = ARM::STRBrs; break;
Jim Grosbacha28abbe2010-09-17 16:25:52 +00001532 case ARM::PICSTRH: Opcode = ARM::STRH; break;
Jim Grosbach3e556122010-10-26 22:37:02 +00001533 case ARM::PICLDR: Opcode = ARM::LDRrs; break;
Jim Grosbachc1d30212010-10-27 00:19:44 +00001534 case ARM::PICLDRB: Opcode = ARM::LDRBrs; break;
Jim Grosbacha28abbe2010-09-17 16:25:52 +00001535 case ARM::PICLDRH: Opcode = ARM::LDRH; break;
1536 case ARM::PICLDRSB: Opcode = ARM::LDRSB; break;
1537 case ARM::PICLDRSH: Opcode = ARM::LDRSH; break;
1538 }
Benjamin Kramer391271f2012-11-26 13:34:22 +00001539 MCInstBuilder(Opcode)
1540 .addReg(MI->getOperand(0).getReg())
1541 .addReg(ARM::PC)
1542 .addReg(MI->getOperand(1).getReg())
1543 .addImm(0)
1544 // Add predicate operands.
1545 .addImm(MI->getOperand(3).getImm())
1546 .addReg(MI->getOperand(4).getReg())
1547 .emit(OutStreamer);
Jim Grosbachb74ca9d2010-09-16 17:43:25 +00001548
1549 return;
1550 }
Jim Grosbacha3fbadf2010-09-30 19:53:58 +00001551 case ARM::CONSTPOOL_ENTRY: {
Chris Lattnera70e6442009-10-19 22:33:05 +00001552 /// CONSTPOOL_ENTRY - This instruction represents a floating constant pool
1553 /// in the function. The first operand is the ID# for this instruction, the
1554 /// second is the index into the MachineConstantPool that this is, the third
1555 /// is the size in bytes of this constant pool entry.
Jakob Stoklund Olesen3e572ac2011-12-06 01:43:02 +00001556 /// The required alignment is specified on the basic block holding this MI.
Chris Lattnera70e6442009-10-19 22:33:05 +00001557 unsigned LabelId = (unsigned)MI->getOperand(0).getImm();
1558 unsigned CPIdx = (unsigned)MI->getOperand(1).getIndex();
1559
Jim Grosbach3e965312012-05-18 19:12:01 +00001560 // If this is the first entry of the pool, mark it.
1561 if (!InConstantPool) {
1562 OutStreamer.EmitDataRegion(MCDR_DataRegion);
1563 InConstantPool = true;
1564 }
1565
Chris Lattner1b46f432010-01-23 07:00:21 +00001566 OutStreamer.EmitLabel(GetCPISymbol(LabelId));
Chris Lattnera70e6442009-10-19 22:33:05 +00001567
1568 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPIdx];
1569 if (MCPE.isMachineConstantPoolEntry())
1570 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
1571 else
1572 EmitGlobalConstant(MCPE.Val.ConstVal);
Chris Lattnera70e6442009-10-19 22:33:05 +00001573 return;
1574 }
Jim Grosbach882ef2b2010-09-21 23:28:16 +00001575 case ARM::t2BR_JT: {
1576 // Lower and emit the instruction itself, then the jump table following it.
Benjamin Kramer391271f2012-11-26 13:34:22 +00001577 MCInstBuilder(ARM::tMOVr)
1578 .addReg(ARM::PC)
1579 .addReg(MI->getOperand(0).getReg())
1580 // Add predicate operands.
1581 .addImm(ARMCC::AL)
1582 .addReg(0)
1583 .emit(OutStreamer);
1584
Jim Grosbach5ca66692010-11-29 22:37:40 +00001585 // Output the data for the jump table itself
1586 EmitJump2Table(MI);
1587 return;
1588 }
1589 case ARM::t2TBB_JT: {
1590 // Lower and emit the instruction itself, then the jump table following it.
Benjamin Kramer391271f2012-11-26 13:34:22 +00001591 MCInstBuilder(ARM::t2TBB)
1592 .addReg(ARM::PC)
1593 .addReg(MI->getOperand(0).getReg())
1594 // Add predicate operands.
1595 .addImm(ARMCC::AL)
1596 .addReg(0)
1597 .emit(OutStreamer);
Jim Grosbach5ca66692010-11-29 22:37:40 +00001598
Jim Grosbach5ca66692010-11-29 22:37:40 +00001599 // Output the data for the jump table itself
1600 EmitJump2Table(MI);
1601 // Make sure the next instruction is 2-byte aligned.
1602 EmitAlignment(1);
1603 return;
1604 }
1605 case ARM::t2TBH_JT: {
1606 // Lower and emit the instruction itself, then the jump table following it.
Benjamin Kramer391271f2012-11-26 13:34:22 +00001607 MCInstBuilder(ARM::t2TBH)
1608 .addReg(ARM::PC)
1609 .addReg(MI->getOperand(0).getReg())
1610 // Add predicate operands.
1611 .addImm(ARMCC::AL)
1612 .addReg(0)
1613 .emit(OutStreamer);
Jim Grosbach5ca66692010-11-29 22:37:40 +00001614
Jim Grosbach5ca66692010-11-29 22:37:40 +00001615 // Output the data for the jump table itself
Jim Grosbach882ef2b2010-09-21 23:28:16 +00001616 EmitJump2Table(MI);
1617 return;
1618 }
Jim Grosbachf1aa47d2010-11-29 19:32:47 +00001619 case ARM::tBR_JTr:
Jim Grosbach2dc77682010-11-29 18:37:44 +00001620 case ARM::BR_JTr: {
1621 // Lower and emit the instruction itself, then the jump table following it.
1622 // mov pc, target
1623 MCInst TmpInst;
Jim Grosbach5ca66692010-11-29 22:37:40 +00001624 unsigned Opc = MI->getOpcode() == ARM::BR_JTr ?
Jim Grosbach2a7b41b2011-06-30 23:38:17 +00001625 ARM::MOVr : ARM::tMOVr;
Jim Grosbachf1aa47d2010-11-29 19:32:47 +00001626 TmpInst.setOpcode(Opc);
Jim Grosbach2dc77682010-11-29 18:37:44 +00001627 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1628 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1629 // Add predicate operands.
1630 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1631 TmpInst.addOperand(MCOperand::CreateReg(0));
Jim Grosbacha0d2c8a2010-11-30 18:30:19 +00001632 // Add 's' bit operand (always reg0 for this)
1633 if (Opc == ARM::MOVr)
1634 TmpInst.addOperand(MCOperand::CreateReg(0));
Jim Grosbach2dc77682010-11-29 18:37:44 +00001635 OutStreamer.EmitInstruction(TmpInst);
1636
Jim Grosbachf1aa47d2010-11-29 19:32:47 +00001637 // Make sure the Thumb jump table is 4-byte aligned.
Jim Grosbach2a7b41b2011-06-30 23:38:17 +00001638 if (Opc == ARM::tMOVr)
Jim Grosbachf1aa47d2010-11-29 19:32:47 +00001639 EmitAlignment(2);
1640
Jim Grosbach2dc77682010-11-29 18:37:44 +00001641 // Output the data for the jump table itself
1642 EmitJumpTable(MI);
1643 return;
1644 }
1645 case ARM::BR_JTm: {
1646 // Lower and emit the instruction itself, then the jump table following it.
1647 // ldr pc, target
1648 MCInst TmpInst;
1649 if (MI->getOperand(1).getReg() == 0) {
1650 // literal offset
1651 TmpInst.setOpcode(ARM::LDRi12);
1652 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1653 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1654 TmpInst.addOperand(MCOperand::CreateImm(MI->getOperand(2).getImm()));
1655 } else {
1656 TmpInst.setOpcode(ARM::LDRrs);
1657 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1658 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1659 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
1660 TmpInst.addOperand(MCOperand::CreateImm(0));
1661 }
1662 // Add predicate operands.
1663 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1664 TmpInst.addOperand(MCOperand::CreateReg(0));
1665 OutStreamer.EmitInstruction(TmpInst);
1666
1667 // Output the data for the jump table itself
Jim Grosbacha2244cb2010-09-22 17:39:48 +00001668 EmitJumpTable(MI);
1669 return;
1670 }
Jim Grosbachf8dabac2010-11-17 21:05:55 +00001671 case ARM::BR_JTadd: {
1672 // Lower and emit the instruction itself, then the jump table following it.
1673 // add pc, target, idx
Benjamin Kramer391271f2012-11-26 13:34:22 +00001674 MCInstBuilder(ARM::ADDrr)
1675 .addReg(ARM::PC)
1676 .addReg(MI->getOperand(0).getReg())
1677 .addReg(MI->getOperand(1).getReg())
1678 // Add predicate operands.
1679 .addImm(ARMCC::AL)
1680 .addReg(0)
1681 // Add 's' bit operand (always reg0 for this)
1682 .addReg(0)
1683 .emit(OutStreamer);
Jim Grosbachf8dabac2010-11-17 21:05:55 +00001684
1685 // Output the data for the jump table itself
1686 EmitJumpTable(MI);
1687 return;
1688 }
Jim Grosbach2e6ae132010-09-23 18:05:37 +00001689 case ARM::TRAP: {
1690 // Non-Darwin binutils don't yet support the "trap" mnemonic.
1691 // FIXME: Remove this special case when they do.
1692 if (!Subtarget->isTargetDarwin()) {
Jim Grosbach78890f42010-10-01 23:21:38 +00001693 //.long 0xe7ffdefe @ trap
Jim Grosbachb2dda4b2010-09-23 19:42:17 +00001694 uint32_t Val = 0xe7ffdefeUL;
Jim Grosbach2e6ae132010-09-23 18:05:37 +00001695 OutStreamer.AddComment("trap");
1696 OutStreamer.EmitIntValue(Val, 4);
1697 return;
1698 }
1699 break;
1700 }
1701 case ARM::tTRAP: {
1702 // Non-Darwin binutils don't yet support the "trap" mnemonic.
1703 // FIXME: Remove this special case when they do.
1704 if (!Subtarget->isTargetDarwin()) {
Jim Grosbach78890f42010-10-01 23:21:38 +00001705 //.short 57086 @ trap
Benjamin Kramerc8ab9eb2010-09-23 18:57:26 +00001706 uint16_t Val = 0xdefe;
Jim Grosbach2e6ae132010-09-23 18:05:37 +00001707 OutStreamer.AddComment("trap");
1708 OutStreamer.EmitIntValue(Val, 2);
1709 return;
1710 }
1711 break;
1712 }
Jim Grosbach433a5782010-09-24 20:47:58 +00001713 case ARM::t2Int_eh_sjlj_setjmp:
1714 case ARM::t2Int_eh_sjlj_setjmp_nofp:
Jim Grosbacha3fbadf2010-09-30 19:53:58 +00001715 case ARM::tInt_eh_sjlj_setjmp: {
Jim Grosbach433a5782010-09-24 20:47:58 +00001716 // Two incoming args: GPR:$src, GPR:$val
1717 // mov $val, pc
1718 // adds $val, #7
1719 // str $val, [$src, #4]
1720 // movs r0, #0
1721 // b 1f
1722 // movs r0, #1
1723 // 1:
1724 unsigned SrcReg = MI->getOperand(0).getReg();
1725 unsigned ValReg = MI->getOperand(1).getReg();
1726 MCSymbol *Label = GetARMSJLJEHLabel();
Benjamin Kramer391271f2012-11-26 13:34:22 +00001727 OutStreamer.AddComment("eh_setjmp begin");
1728 MCInstBuilder(ARM::tMOVr)
1729 .addReg(ValReg)
1730 .addReg(ARM::PC)
Jim Grosbach63b46fa2011-06-30 22:10:46 +00001731 // Predicate.
Benjamin Kramer391271f2012-11-26 13:34:22 +00001732 .addImm(ARMCC::AL)
1733 .addReg(0)
1734 .emit(OutStreamer);
1735
1736 MCInstBuilder(ARM::tADDi3)
1737 .addReg(ValReg)
Jim Grosbach433a5782010-09-24 20:47:58 +00001738 // 's' bit operand
Benjamin Kramer391271f2012-11-26 13:34:22 +00001739 .addReg(ARM::CPSR)
1740 .addReg(ValReg)
1741 .addImm(7)
Jim Grosbach433a5782010-09-24 20:47:58 +00001742 // Predicate.
Benjamin Kramer391271f2012-11-26 13:34:22 +00001743 .addImm(ARMCC::AL)
1744 .addReg(0)
1745 .emit(OutStreamer);
1746
1747 MCInstBuilder(ARM::tSTRi)
1748 .addReg(ValReg)
1749 .addReg(SrcReg)
Jim Grosbach433a5782010-09-24 20:47:58 +00001750 // The offset immediate is #4. The operand value is scaled by 4 for the
1751 // tSTR instruction.
Benjamin Kramer391271f2012-11-26 13:34:22 +00001752 .addImm(1)
Jim Grosbach433a5782010-09-24 20:47:58 +00001753 // Predicate.
Benjamin Kramer391271f2012-11-26 13:34:22 +00001754 .addImm(ARMCC::AL)
1755 .addReg(0)
1756 .emit(OutStreamer);
1757
1758 MCInstBuilder(ARM::tMOVi8)
1759 .addReg(ARM::R0)
1760 .addReg(ARM::CPSR)
1761 .addImm(0)
Jim Grosbach433a5782010-09-24 20:47:58 +00001762 // Predicate.
Benjamin Kramer391271f2012-11-26 13:34:22 +00001763 .addImm(ARMCC::AL)
1764 .addReg(0)
1765 .emit(OutStreamer);
1766
1767 const MCExpr *SymbolExpr = MCSymbolRefExpr::Create(Label, OutContext);
1768 MCInstBuilder(ARM::tB)
1769 .addExpr(SymbolExpr)
1770 .addImm(ARMCC::AL)
1771 .addReg(0)
1772 .emit(OutStreamer);
1773
1774 OutStreamer.AddComment("eh_setjmp end");
1775 MCInstBuilder(ARM::tMOVi8)
1776 .addReg(ARM::R0)
1777 .addReg(ARM::CPSR)
1778 .addImm(1)
Jim Grosbach433a5782010-09-24 20:47:58 +00001779 // Predicate.
Benjamin Kramer391271f2012-11-26 13:34:22 +00001780 .addImm(ARMCC::AL)
1781 .addReg(0)
1782 .emit(OutStreamer);
1783
Jim Grosbach433a5782010-09-24 20:47:58 +00001784 OutStreamer.EmitLabel(Label);
1785 return;
1786 }
1787
Jim Grosbach45390082010-09-23 23:33:56 +00001788 case ARM::Int_eh_sjlj_setjmp_nofp:
Jim Grosbacha3fbadf2010-09-30 19:53:58 +00001789 case ARM::Int_eh_sjlj_setjmp: {
Jim Grosbach45390082010-09-23 23:33:56 +00001790 // Two incoming args: GPR:$src, GPR:$val
1791 // add $val, pc, #8
1792 // str $val, [$src, #+4]
1793 // mov r0, #0
1794 // add pc, pc, #0
1795 // mov r0, #1
1796 unsigned SrcReg = MI->getOperand(0).getReg();
1797 unsigned ValReg = MI->getOperand(1).getReg();
1798
Benjamin Kramer391271f2012-11-26 13:34:22 +00001799 OutStreamer.AddComment("eh_setjmp begin");
1800 MCInstBuilder(ARM::ADDri)
1801 .addReg(ValReg)
1802 .addReg(ARM::PC)
1803 .addImm(8)
Jim Grosbach45390082010-09-23 23:33:56 +00001804 // Predicate.
Benjamin Kramer391271f2012-11-26 13:34:22 +00001805 .addImm(ARMCC::AL)
1806 .addReg(0)
Jim Grosbach45390082010-09-23 23:33:56 +00001807 // 's' bit operand (always reg0 for this).
Benjamin Kramer391271f2012-11-26 13:34:22 +00001808 .addReg(0)
1809 .emit(OutStreamer);
1810
1811 MCInstBuilder(ARM::STRi12)
1812 .addReg(ValReg)
1813 .addReg(SrcReg)
1814 .addImm(4)
Jim Grosbach45390082010-09-23 23:33:56 +00001815 // Predicate.
Benjamin Kramer391271f2012-11-26 13:34:22 +00001816 .addImm(ARMCC::AL)
1817 .addReg(0)
1818 .emit(OutStreamer);
1819
1820 MCInstBuilder(ARM::MOVi)
1821 .addReg(ARM::R0)
1822 .addImm(0)
Jim Grosbach45390082010-09-23 23:33:56 +00001823 // Predicate.
Benjamin Kramer391271f2012-11-26 13:34:22 +00001824 .addImm(ARMCC::AL)
1825 .addReg(0)
Jim Grosbach45390082010-09-23 23:33:56 +00001826 // 's' bit operand (always reg0 for this).
Benjamin Kramer391271f2012-11-26 13:34:22 +00001827 .addReg(0)
1828 .emit(OutStreamer);
1829
1830 MCInstBuilder(ARM::ADDri)
1831 .addReg(ARM::PC)
1832 .addReg(ARM::PC)
1833 .addImm(0)
Jim Grosbach45390082010-09-23 23:33:56 +00001834 // Predicate.
Benjamin Kramer391271f2012-11-26 13:34:22 +00001835 .addImm(ARMCC::AL)
1836 .addReg(0)
Jim Grosbach45390082010-09-23 23:33:56 +00001837 // 's' bit operand (always reg0 for this).
Benjamin Kramer391271f2012-11-26 13:34:22 +00001838 .addReg(0)
1839 .emit(OutStreamer);
1840
1841 OutStreamer.AddComment("eh_setjmp end");
1842 MCInstBuilder(ARM::MOVi)
1843 .addReg(ARM::R0)
1844 .addImm(1)
Jim Grosbach45390082010-09-23 23:33:56 +00001845 // Predicate.
Benjamin Kramer391271f2012-11-26 13:34:22 +00001846 .addImm(ARMCC::AL)
1847 .addReg(0)
Jim Grosbach45390082010-09-23 23:33:56 +00001848 // 's' bit operand (always reg0 for this).
Benjamin Kramer391271f2012-11-26 13:34:22 +00001849 .addReg(0)
1850 .emit(OutStreamer);
Jim Grosbach45390082010-09-23 23:33:56 +00001851 return;
1852 }
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001853 case ARM::Int_eh_sjlj_longjmp: {
1854 // ldr sp, [$src, #8]
1855 // ldr $scratch, [$src, #4]
1856 // ldr r7, [$src]
1857 // bx $scratch
1858 unsigned SrcReg = MI->getOperand(0).getReg();
1859 unsigned ScratchReg = MI->getOperand(1).getReg();
Benjamin Kramer391271f2012-11-26 13:34:22 +00001860 MCInstBuilder(ARM::LDRi12)
1861 .addReg(ARM::SP)
1862 .addReg(SrcReg)
1863 .addImm(8)
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001864 // Predicate.
Benjamin Kramer391271f2012-11-26 13:34:22 +00001865 .addImm(ARMCC::AL)
1866 .addReg(0)
1867 .emit(OutStreamer);
1868
1869 MCInstBuilder(ARM::LDRi12)
1870 .addReg(ScratchReg)
1871 .addReg(SrcReg)
1872 .addImm(4)
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001873 // Predicate.
Benjamin Kramer391271f2012-11-26 13:34:22 +00001874 .addImm(ARMCC::AL)
1875 .addReg(0)
1876 .emit(OutStreamer);
1877
1878 MCInstBuilder(ARM::LDRi12)
1879 .addReg(ARM::R7)
1880 .addReg(SrcReg)
1881 .addImm(0)
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001882 // Predicate.
Benjamin Kramer391271f2012-11-26 13:34:22 +00001883 .addImm(ARMCC::AL)
1884 .addReg(0)
1885 .emit(OutStreamer);
1886
1887 MCInstBuilder(ARM::BX)
1888 .addReg(ScratchReg)
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001889 // Predicate.
Benjamin Kramer391271f2012-11-26 13:34:22 +00001890 .addImm(ARMCC::AL)
1891 .addReg(0)
1892 .emit(OutStreamer);
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001893 return;
1894 }
Jim Grosbach385cc5e2010-09-27 22:28:11 +00001895 case ARM::tInt_eh_sjlj_longjmp: {
1896 // ldr $scratch, [$src, #8]
1897 // mov sp, $scratch
1898 // ldr $scratch, [$src, #4]
1899 // ldr r7, [$src]
1900 // bx $scratch
1901 unsigned SrcReg = MI->getOperand(0).getReg();
1902 unsigned ScratchReg = MI->getOperand(1).getReg();
Benjamin Kramer391271f2012-11-26 13:34:22 +00001903 MCInstBuilder(ARM::tLDRi)
1904 .addReg(ScratchReg)
1905 .addReg(SrcReg)
Jim Grosbach385cc5e2010-09-27 22:28:11 +00001906 // The offset immediate is #8. The operand value is scaled by 4 for the
Bill Wendlingf4caf692010-12-14 03:36:38 +00001907 // tLDR instruction.
Benjamin Kramer391271f2012-11-26 13:34:22 +00001908 .addImm(2)
Jim Grosbach385cc5e2010-09-27 22:28:11 +00001909 // Predicate.
Benjamin Kramer391271f2012-11-26 13:34:22 +00001910 .addImm(ARMCC::AL)
1911 .addReg(0)
1912 .emit(OutStreamer);
1913
1914 MCInstBuilder(ARM::tMOVr)
1915 .addReg(ARM::SP)
1916 .addReg(ScratchReg)
Jim Grosbach385cc5e2010-09-27 22:28:11 +00001917 // Predicate.
Benjamin Kramer391271f2012-11-26 13:34:22 +00001918 .addImm(ARMCC::AL)
1919 .addReg(0)
1920 .emit(OutStreamer);
1921
1922 MCInstBuilder(ARM::tLDRi)
1923 .addReg(ScratchReg)
1924 .addReg(SrcReg)
1925 .addImm(1)
Jim Grosbach385cc5e2010-09-27 22:28:11 +00001926 // Predicate.
Benjamin Kramer391271f2012-11-26 13:34:22 +00001927 .addImm(ARMCC::AL)
1928 .addReg(0)
1929 .emit(OutStreamer);
1930
1931 MCInstBuilder(ARM::tLDRi)
1932 .addReg(ARM::R7)
1933 .addReg(SrcReg)
1934 .addImm(0)
Jim Grosbach385cc5e2010-09-27 22:28:11 +00001935 // Predicate.
Benjamin Kramer391271f2012-11-26 13:34:22 +00001936 .addImm(ARMCC::AL)
1937 .addReg(0)
1938 .emit(OutStreamer);
1939
1940 MCInstBuilder(ARM::tBX)
1941 .addReg(ScratchReg)
Jim Grosbach385cc5e2010-09-27 22:28:11 +00001942 // Predicate.
Benjamin Kramer391271f2012-11-26 13:34:22 +00001943 .addImm(ARMCC::AL)
1944 .addReg(0)
1945 .emit(OutStreamer);
Jim Grosbach385cc5e2010-09-27 22:28:11 +00001946 return;
1947 }
Chris Lattner97f06932009-10-19 20:20:46 +00001948 }
Jim Grosbachb0739b72010-09-02 01:02:06 +00001949
Chris Lattner97f06932009-10-19 20:20:46 +00001950 MCInst TmpInst;
Chris Lattner30e2cc22010-11-14 21:00:02 +00001951 LowerARMMachineInstrToMCInst(MI, TmpInst, *this);
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001952
Chris Lattner850d2e22010-02-03 01:16:28 +00001953 OutStreamer.EmitInstruction(TmpInst);
Chris Lattner97f06932009-10-19 20:20:46 +00001954}
Daniel Dunbar2685a292009-10-20 05:15:36 +00001955
1956//===----------------------------------------------------------------------===//
1957// Target Registry Stuff
1958//===----------------------------------------------------------------------===//
1959
Daniel Dunbar2685a292009-10-20 05:15:36 +00001960// Force static initialization.
1961extern "C" void LLVMInitializeARMAsmPrinter() {
1962 RegisterAsmPrinter<ARMAsmPrinter> X(TheARMTarget);
1963 RegisterAsmPrinter<ARMAsmPrinter> Y(TheThumbTarget);
Daniel Dunbar2685a292009-10-20 05:15:36 +00001964}