blob: 8536b94d716f40923f2cefb0f7115a520c39d148 [file] [log] [blame]
Chris Lattner97f06932009-10-19 20:20:46 +00001//===-- ARMAsmPrinter.cpp - Print machine code to an ARM .s file ----------===//
2//
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00006// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains a printer that converts from our internal representation
11// of machine-dependent LLVM code to GAS-format ARM assembly language.
12//
13//===----------------------------------------------------------------------===//
14
Chris Lattner95b2c7d2006-12-19 22:59:26 +000015#define DEBUG_TYPE "asm-printer"
Jim Grosbachbaf120f2010-12-01 03:45:07 +000016#include "ARMAsmPrinter.h"
Craig Topperc1f6f422012-03-17 07:33:42 +000017#include "ARM.h"
Evan Chengb72d2a92011-01-11 21:46:47 +000018#include "ARMBuildAttrs.h"
Evan Chengb72d2a92011-01-11 21:46:47 +000019#include "ARMConstantPoolValue.h"
Chris Lattner97f06932009-10-19 20:20:46 +000020#include "ARMMachineFunctionInfo.h"
Chris Lattner97f06932009-10-19 20:20:46 +000021#include "ARMTargetMachine.h"
Jason W Kim17b443d2010-10-11 23:01:44 +000022#include "ARMTargetObjectFile.h"
Evan Chengb72d2a92011-01-11 21:46:47 +000023#include "InstPrinter/ARMInstPrinter.h"
Evan Chengee04a6d2011-07-20 23:34:39 +000024#include "MCTargetDesc/ARMAddressingModes.h"
25#include "MCTargetDesc/ARMMCExpr.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000026#include "llvm/Constants.h"
Bill Wendling0bcbd1d2012-06-28 00:05:13 +000027#include "llvm/DebugInfo.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000028#include "llvm/Module.h"
Benjamin Kramere55b15f2009-12-28 12:27:56 +000029#include "llvm/Type.h"
Dan Gohmancf20ac42009-08-13 01:36:44 +000030#include "llvm/Assembly/Writer.h"
Chris Lattnerb0f294c2009-10-19 18:38:33 +000031#include "llvm/CodeGen/MachineModuleInfoImpls.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000032#include "llvm/CodeGen/MachineFunctionPass.h"
Evan Chenga8e29892007-01-19 07:51:42 +000033#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattnerb0f294c2009-10-19 18:38:33 +000034#include "llvm/MC/MCAsmInfo.h"
Rafael Espindolacecbc3d2010-10-25 17:50:35 +000035#include "llvm/MC/MCAssembler.h"
Chris Lattnerb0f294c2009-10-19 18:38:33 +000036#include "llvm/MC/MCContext.h"
Chris Lattner97f06932009-10-19 20:20:46 +000037#include "llvm/MC/MCInst.h"
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000038#include "llvm/MC/MCSectionMachO.h"
Rafael Espindolacecbc3d2010-10-25 17:50:35 +000039#include "llvm/MC/MCObjectStreamer.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000040#include "llvm/MC/MCStreamer.h"
Chris Lattner325d3dc2009-09-13 17:14:04 +000041#include "llvm/MC/MCSymbol.h"
Chris Lattnerd62f1b42010-03-12 21:19:23 +000042#include "llvm/Target/Mangler.h"
Rafael Espindolab01c4bb2006-07-27 11:38:51 +000043#include "llvm/Target/TargetData.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000044#include "llvm/Target/TargetMachine.h"
Jim Grosbachc40d9f92009-09-01 18:49:12 +000045#include "llvm/ADT/SmallString.h"
Chris Lattner97f06932009-10-19 20:20:46 +000046#include "llvm/Support/CommandLine.h"
Devang Patel59135f42010-08-04 22:39:39 +000047#include "llvm/Support/Debug.h"
Torok Edwin30464702009-07-08 20:55:50 +000048#include "llvm/Support/ErrorHandling.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000049#include "llvm/Support/TargetRegistry.h"
Chris Lattnerb23569a2010-04-04 08:18:47 +000050#include "llvm/Support/raw_ostream.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000051#include <cctype>
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000052using namespace llvm;
53
Chris Lattner95b2c7d2006-12-19 22:59:26 +000054namespace {
Rafael Espindolacecbc3d2010-10-25 17:50:35 +000055
56 // Per section and per symbol attributes are not supported.
57 // To implement them we would need the ability to delay this emission
58 // until the assembly file is fully parsed/generated as only then do we
59 // know the symbol and section numbers.
60 class AttributeEmitter {
61 public:
62 virtual void MaybeSwitchVendor(StringRef Vendor) = 0;
63 virtual void EmitAttribute(unsigned Attribute, unsigned Value) = 0;
Jason W Kimf009a962011-02-07 00:49:53 +000064 virtual void EmitTextAttribute(unsigned Attribute, StringRef String) = 0;
Rafael Espindolacecbc3d2010-10-25 17:50:35 +000065 virtual void Finish() = 0;
Rafael Espindola4921e232010-10-25 18:38:32 +000066 virtual ~AttributeEmitter() {}
Rafael Espindolacecbc3d2010-10-25 17:50:35 +000067 };
68
69 class AsmAttributeEmitter : public AttributeEmitter {
70 MCStreamer &Streamer;
71
72 public:
73 AsmAttributeEmitter(MCStreamer &Streamer_) : Streamer(Streamer_) {}
74 void MaybeSwitchVendor(StringRef Vendor) { }
75
76 void EmitAttribute(unsigned Attribute, unsigned Value) {
77 Streamer.EmitRawText("\t.eabi_attribute " +
78 Twine(Attribute) + ", " + Twine(Value));
79 }
80
Jason W Kimf009a962011-02-07 00:49:53 +000081 void EmitTextAttribute(unsigned Attribute, StringRef String) {
82 switch (Attribute) {
Craig Topperbc219812012-02-07 02:50:20 +000083 default: llvm_unreachable("Unsupported Text attribute in ASM Mode");
Jason W Kimf009a962011-02-07 00:49:53 +000084 case ARMBuildAttrs::CPU_name:
Benjamin Kramer59085362011-11-06 20:37:06 +000085 Streamer.EmitRawText(StringRef("\t.cpu ") + String.lower());
Jason W Kimf009a962011-02-07 00:49:53 +000086 break;
Renato Golin728ff0d2011-02-28 22:04:27 +000087 /* GAS requires .fpu to be emitted regardless of EABI attribute */
88 case ARMBuildAttrs::Advanced_SIMD_arch:
89 case ARMBuildAttrs::VFP_arch:
Benjamin Kramer59085362011-11-06 20:37:06 +000090 Streamer.EmitRawText(StringRef("\t.fpu ") + String.lower());
Jim Grosbach8e0c7692011-09-02 18:46:15 +000091 break;
Jason W Kimf009a962011-02-07 00:49:53 +000092 }
93 }
Rafael Espindolacecbc3d2010-10-25 17:50:35 +000094 void Finish() { }
95 };
96
97 class ObjectAttributeEmitter : public AttributeEmitter {
Renato Golin719927a2011-08-09 09:50:10 +000098 // This structure holds all attributes, accounting for
99 // their string/numeric value, so we can later emmit them
100 // in declaration order, keeping all in the same vector
101 struct AttributeItemType {
102 enum {
103 HiddenAttribute = 0,
104 NumericAttribute,
105 TextAttribute
106 } Type;
107 unsigned Tag;
108 unsigned IntValue;
109 StringRef StringValue;
110 } AttributeItem;
111
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000112 MCObjectStreamer &Streamer;
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000113 StringRef CurrentVendor;
Renato Golin719927a2011-08-09 09:50:10 +0000114 SmallVector<AttributeItemType, 64> Contents;
115
116 // Account for the ULEB/String size of each item,
117 // not just the number of items
118 size_t ContentsSize;
119 // FIXME: this should be in a more generic place, but
120 // getULEBSize() is in MCAsmInfo and will be moved to MCDwarf
121 size_t getULEBSize(int Value) {
122 size_t Size = 0;
123 do {
124 Value >>= 7;
125 Size += sizeof(int8_t); // Is this really necessary?
126 } while (Value);
127 return Size;
128 }
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000129
130 public:
131 ObjectAttributeEmitter(MCObjectStreamer &Streamer_) :
Renato Golin719927a2011-08-09 09:50:10 +0000132 Streamer(Streamer_), CurrentVendor(""), ContentsSize(0) { }
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000133
134 void MaybeSwitchVendor(StringRef Vendor) {
135 assert(!Vendor.empty() && "Vendor cannot be empty.");
136
137 if (CurrentVendor.empty())
138 CurrentVendor = Vendor;
139 else if (CurrentVendor == Vendor)
140 return;
141 else
142 Finish();
143
144 CurrentVendor = Vendor;
145
Rafael Espindola33363842010-10-25 22:26:55 +0000146 assert(Contents.size() == 0);
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000147 }
148
149 void EmitAttribute(unsigned Attribute, unsigned Value) {
Renato Golin719927a2011-08-09 09:50:10 +0000150 AttributeItemType attr = {
151 AttributeItemType::NumericAttribute,
152 Attribute,
153 Value,
154 StringRef("")
155 };
156 ContentsSize += getULEBSize(Attribute);
157 ContentsSize += getULEBSize(Value);
158 Contents.push_back(attr);
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000159 }
160
Jason W Kimf009a962011-02-07 00:49:53 +0000161 void EmitTextAttribute(unsigned Attribute, StringRef String) {
Renato Golin719927a2011-08-09 09:50:10 +0000162 AttributeItemType attr = {
163 AttributeItemType::TextAttribute,
164 Attribute,
165 0,
166 String
167 };
168 ContentsSize += getULEBSize(Attribute);
169 // String + \0
170 ContentsSize += String.size()+1;
171
172 Contents.push_back(attr);
Jason W Kimf009a962011-02-07 00:49:53 +0000173 }
174
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000175 void Finish() {
Rafael Espindola33363842010-10-25 22:26:55 +0000176 // Vendor size + Vendor name + '\0'
177 const size_t VendorHeaderSize = 4 + CurrentVendor.size() + 1;
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000178
Rafael Espindola33363842010-10-25 22:26:55 +0000179 // Tag + Tag Size
180 const size_t TagHeaderSize = 1 + 4;
181
182 Streamer.EmitIntValue(VendorHeaderSize + TagHeaderSize + ContentsSize, 4);
183 Streamer.EmitBytes(CurrentVendor, 0);
184 Streamer.EmitIntValue(0, 1); // '\0'
185
186 Streamer.EmitIntValue(ARMBuildAttrs::File, 1);
187 Streamer.EmitIntValue(TagHeaderSize + ContentsSize, 4);
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000188
Renato Golin719927a2011-08-09 09:50:10 +0000189 // Size should have been accounted for already, now
190 // emit each field as its type (ULEB or String)
191 for (unsigned int i=0; i<Contents.size(); ++i) {
192 AttributeItemType item = Contents[i];
193 Streamer.EmitULEB128IntValue(item.Tag, 0);
194 switch (item.Type) {
Craig Topperbc219812012-02-07 02:50:20 +0000195 default: llvm_unreachable("Invalid attribute type");
Renato Golin719927a2011-08-09 09:50:10 +0000196 case AttributeItemType::NumericAttribute:
197 Streamer.EmitULEB128IntValue(item.IntValue, 0);
198 break;
199 case AttributeItemType::TextAttribute:
Benjamin Kramer59085362011-11-06 20:37:06 +0000200 Streamer.EmitBytes(item.StringValue.upper(), 0);
Renato Golin719927a2011-08-09 09:50:10 +0000201 Streamer.EmitIntValue(0, 1); // '\0'
202 break;
Renato Golin719927a2011-08-09 09:50:10 +0000203 }
204 }
Rafael Espindola33363842010-10-25 22:26:55 +0000205
206 Contents.clear();
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000207 }
208 };
209
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000210} // end of anonymous namespace
211
Jim Grosbachbaf120f2010-12-01 03:45:07 +0000212MachineLocation ARMAsmPrinter::
213getDebugValueLocation(const MachineInstr *MI) const {
214 MachineLocation Location;
215 assert(MI->getNumOperands() == 4 && "Invalid no. of machine operands!");
216 // Frame address. Currently handles register +- offset only.
217 if (MI->getOperand(0).isReg() && MI->getOperand(1).isImm())
218 Location.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
219 else {
220 DEBUG(dbgs() << "DBG_VALUE instruction ignored! " << *MI << "\n");
221 }
222 return Location;
223}
224
Devang Patel27f5acb2011-04-21 22:48:26 +0000225/// EmitDwarfRegOp - Emit dwarf register operation.
Devang Patel0be77df2011-04-27 20:29:27 +0000226void ARMAsmPrinter::EmitDwarfRegOp(const MachineLocation &MLoc) const {
Devang Patel27f5acb2011-04-21 22:48:26 +0000227 const TargetRegisterInfo *RI = TM.getRegisterInfo();
228 if (RI->getDwarfRegNum(MLoc.getReg(), false) != -1)
Devang Patel0be77df2011-04-27 20:29:27 +0000229 AsmPrinter::EmitDwarfRegOp(MLoc);
Devang Patel27f5acb2011-04-21 22:48:26 +0000230 else {
231 unsigned Reg = MLoc.getReg();
232 if (Reg >= ARM::S0 && Reg <= ARM::S31) {
Devang Patel0a6ea832011-04-22 16:44:29 +0000233 assert(ARM::S0 + 31 == ARM::S31 && "Unexpected ARM S register numbering");
Devang Patel27f5acb2011-04-21 22:48:26 +0000234 // S registers are described as bit-pieces of a register
235 // S[2x] = DW_OP_regx(256 + (x>>1)) DW_OP_bit_piece(32, 0)
236 // S[2x+1] = DW_OP_regx(256 + (x>>1)) DW_OP_bit_piece(32, 32)
Jim Grosbach8e0c7692011-09-02 18:46:15 +0000237
Devang Patel27f5acb2011-04-21 22:48:26 +0000238 unsigned SReg = Reg - ARM::S0;
239 bool odd = SReg & 0x1;
240 unsigned Rx = 256 + (SReg >> 1);
Devang Patel27f5acb2011-04-21 22:48:26 +0000241
242 OutStreamer.AddComment("DW_OP_regx for S register");
243 EmitInt8(dwarf::DW_OP_regx);
244
245 OutStreamer.AddComment(Twine(SReg));
246 EmitULEB128(Rx);
247
248 if (odd) {
249 OutStreamer.AddComment("DW_OP_bit_piece 32 32");
250 EmitInt8(dwarf::DW_OP_bit_piece);
251 EmitULEB128(32);
252 EmitULEB128(32);
253 } else {
254 OutStreamer.AddComment("DW_OP_bit_piece 32 0");
255 EmitInt8(dwarf::DW_OP_bit_piece);
256 EmitULEB128(32);
257 EmitULEB128(0);
258 }
Devang Patel71f3f112011-04-21 23:22:35 +0000259 } else if (Reg >= ARM::Q0 && Reg <= ARM::Q15) {
Devang Patel0a6ea832011-04-22 16:44:29 +0000260 assert(ARM::Q0 + 15 == ARM::Q15 && "Unexpected ARM Q register numbering");
Devang Patel71f3f112011-04-21 23:22:35 +0000261 // Q registers Q0-Q15 are described by composing two D registers together.
Jim Grosbach8e0c7692011-09-02 18:46:15 +0000262 // Qx = DW_OP_regx(256+2x) DW_OP_piece(8) DW_OP_regx(256+2x+1)
263 // DW_OP_piece(8)
Devang Patel71f3f112011-04-21 23:22:35 +0000264
265 unsigned QReg = Reg - ARM::Q0;
266 unsigned D1 = 256 + 2 * QReg;
267 unsigned D2 = D1 + 1;
Jim Grosbach8e0c7692011-09-02 18:46:15 +0000268
Devang Patel71f3f112011-04-21 23:22:35 +0000269 OutStreamer.AddComment("DW_OP_regx for Q register: D1");
270 EmitInt8(dwarf::DW_OP_regx);
271 EmitULEB128(D1);
272 OutStreamer.AddComment("DW_OP_piece 8");
273 EmitInt8(dwarf::DW_OP_piece);
274 EmitULEB128(8);
275
276 OutStreamer.AddComment("DW_OP_regx for Q register: D2");
277 EmitInt8(dwarf::DW_OP_regx);
278 EmitULEB128(D2);
279 OutStreamer.AddComment("DW_OP_piece 8");
280 EmitInt8(dwarf::DW_OP_piece);
281 EmitULEB128(8);
Devang Patel27f5acb2011-04-21 22:48:26 +0000282 }
283 }
284}
285
Jim Grosbach3e965312012-05-18 19:12:01 +0000286void ARMAsmPrinter::EmitFunctionBodyEnd() {
287 // Make sure to terminate any constant pools that were at the end
288 // of the function.
289 if (!InConstantPool)
290 return;
291 InConstantPool = false;
292 OutStreamer.EmitDataRegion(MCDR_DataRegionEnd);
293}
Owen Anderson2fec6c52011-10-04 23:26:17 +0000294
Jim Grosbach3e965312012-05-18 19:12:01 +0000295void ARMAsmPrinter::EmitFunctionEntryLabel() {
Chris Lattner953ebb72010-01-27 23:58:11 +0000296 if (AFI->isThumbFunction()) {
Jim Grosbachce792992010-11-05 22:08:08 +0000297 OutStreamer.EmitAssemblerFlag(MCAF_Code16);
Rafael Espindola64695402011-05-16 16:17:21 +0000298 OutStreamer.EmitThumbFunc(CurrentFnSym);
Chris Lattner953ebb72010-01-27 23:58:11 +0000299 }
Jim Grosbachb0739b72010-09-02 01:02:06 +0000300
Chris Lattner953ebb72010-01-27 23:58:11 +0000301 OutStreamer.EmitLabel(CurrentFnSym);
302}
303
James Molloy34982572012-01-26 09:25:43 +0000304void ARMAsmPrinter::EmitXXStructor(const Constant *CV) {
305 uint64_t Size = TM.getTargetData()->getTypeAllocSize(CV->getType());
306 assert(Size && "C++ constructor pointer had zero size!");
307
Bill Wendling4a1ff2f2012-02-15 09:14:08 +0000308 const GlobalValue *GV = dyn_cast<GlobalValue>(CV->stripPointerCasts());
James Molloy34982572012-01-26 09:25:43 +0000309 assert(GV && "C++ constructor pointer was not a GlobalValue!");
310
311 const MCExpr *E = MCSymbolRefExpr::Create(Mang->getSymbol(GV),
312 (Subtarget->isTargetDarwin()
313 ? MCSymbolRefExpr::VK_None
314 : MCSymbolRefExpr::VK_ARM_TARGET1),
315 OutContext);
316
317 OutStreamer.EmitValue(E, Size);
318}
319
Jim Grosbach2317e402010-09-30 01:57:53 +0000320/// runOnMachineFunction - This uses the EmitInstruction()
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000321/// method to print assembly for each instruction.
322///
323bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Evan Chenga8e29892007-01-19 07:51:42 +0000324 AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng6d63a722008-09-18 07:27:23 +0000325 MCP = MF.getConstantPool();
Rafael Espindola4b442b52006-05-23 02:48:20 +0000326
Chris Lattnerd49fe1b2010-01-28 01:28:58 +0000327 return AsmPrinter::runOnMachineFunction(MF);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000328}
329
Evan Cheng055b0312009-06-29 07:51:04 +0000330void ARMAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
Chris Lattner35c33bd2010-04-04 04:47:45 +0000331 raw_ostream &O, const char *Modifier) {
Evan Cheng055b0312009-06-29 07:51:04 +0000332 const MachineOperand &MO = MI->getOperand(OpNum);
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000333 unsigned TF = MO.getTargetFlags();
334
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000335 switch (MO.getType()) {
Craig Topperbc219812012-02-07 02:50:20 +0000336 default: llvm_unreachable("<unknown operand type>");
Bob Wilson5bafff32009-06-22 23:27:02 +0000337 case MachineOperand::MO_Register: {
338 unsigned Reg = MO.getReg();
Chris Lattner8bc86cb2009-10-19 20:59:55 +0000339 assert(TargetRegisterInfo::isPhysicalRegister(Reg));
Jim Grosbach35636282010-10-06 21:22:32 +0000340 assert(!MO.getSubReg() && "Subregs should be eliminated!");
341 O << ARMInstPrinter::getRegisterName(Reg);
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000342 break;
Bob Wilson5bafff32009-06-22 23:27:02 +0000343 }
Evan Chenga8e29892007-01-19 07:51:42 +0000344 case MachineOperand::MO_Immediate: {
Evan Cheng5adb66a2009-09-28 09:14:39 +0000345 int64_t Imm = MO.getImm();
Anton Korobeynikov632606c2009-10-08 20:43:22 +0000346 O << '#';
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000347 if ((Modifier && strcmp(Modifier, "lo16") == 0) ||
Jason W Kim650b7d72011-01-12 23:21:49 +0000348 (TF == ARMII::MO_LO16))
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000349 O << ":lower16:";
350 else if ((Modifier && strcmp(Modifier, "hi16") == 0) ||
Jason W Kim650b7d72011-01-12 23:21:49 +0000351 (TF == ARMII::MO_HI16))
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000352 O << ":upper16:";
Anton Korobeynikov632606c2009-10-08 20:43:22 +0000353 O << Imm;
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000354 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000355 }
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000356 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner1b2eb0e2010-03-13 21:04:28 +0000357 O << *MO.getMBB()->getSymbol();
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000358 return;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000359 case MachineOperand::MO_GlobalAddress: {
Dan Gohman46510a72010-04-15 01:51:59 +0000360 const GlobalValue *GV = MO.getGlobal();
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000361 if ((Modifier && strcmp(Modifier, "lo16") == 0) ||
362 (TF & ARMII::MO_LO16))
363 O << ":lower16:";
364 else if ((Modifier && strcmp(Modifier, "hi16") == 0) ||
365 (TF & ARMII::MO_HI16))
366 O << ":upper16:";
Chris Lattnerd62f1b42010-03-12 21:19:23 +0000367 O << *Mang->getSymbol(GV);
Anton Korobeynikov7751ad92008-11-22 16:15:34 +0000368
Chris Lattner0c08d092010-04-03 22:28:33 +0000369 printOffset(MO.getOffset(), O);
Jim Grosbach1d6111c2010-10-06 21:36:43 +0000370 if (TF == ARMII::MO_PLT)
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000371 O << "(PLT)";
Evan Chenga8e29892007-01-19 07:51:42 +0000372 break;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000373 }
Evan Chenga8e29892007-01-19 07:51:42 +0000374 case MachineOperand::MO_ExternalSymbol: {
Chris Lattner10b318b2010-01-17 21:43:43 +0000375 O << *GetExternalSymbolSymbol(MO.getSymbolName());
Jim Grosbach1d6111c2010-10-06 21:36:43 +0000376 if (TF == ARMII::MO_PLT)
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000377 O << "(PLT)";
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000378 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000379 }
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000380 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner1b46f432010-01-23 07:00:21 +0000381 O << *GetCPISymbol(MO.getIndex());
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000382 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000383 case MachineOperand::MO_JumpTableIndex:
Chris Lattner1b46f432010-01-23 07:00:21 +0000384 O << *GetJTISymbol(MO.getIndex());
Evan Chenga8e29892007-01-19 07:51:42 +0000385 break;
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000386 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000387}
388
Evan Cheng055b0312009-06-29 07:51:04 +0000389//===--------------------------------------------------------------------===//
390
Chris Lattner0890cf12010-01-25 19:51:38 +0000391MCSymbol *ARMAsmPrinter::
392GetARMSetPICJumpTableLabel2(unsigned uid, unsigned uid2,
393 const MachineBasicBlock *MBB) const {
394 SmallString<60> Name;
395 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix()
Chris Lattnerbfcb0962010-01-25 19:39:52 +0000396 << getFunctionNumber() << '_' << uid << '_' << uid2
Chris Lattner0890cf12010-01-25 19:51:38 +0000397 << "_set_" << MBB->getNumber();
Chris Lattner9b97a732010-03-30 18:10:53 +0000398 return OutContext.GetOrCreateSymbol(Name.str());
Chris Lattner0890cf12010-01-25 19:51:38 +0000399}
400
401MCSymbol *ARMAsmPrinter::
402GetARMJTIPICJumpTableLabel2(unsigned uid, unsigned uid2) const {
403 SmallString<60> Name;
404 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "JTI"
Chris Lattner281e7762010-01-25 23:28:03 +0000405 << getFunctionNumber() << '_' << uid << '_' << uid2;
Chris Lattner9b97a732010-03-30 18:10:53 +0000406 return OutContext.GetOrCreateSymbol(Name.str());
Chris Lattnerbfcb0962010-01-25 19:39:52 +0000407}
408
Jim Grosbach433a5782010-09-24 20:47:58 +0000409
410MCSymbol *ARMAsmPrinter::GetARMSJLJEHLabel(void) const {
411 SmallString<60> Name;
412 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "SJLJEH"
413 << getFunctionNumber();
414 return OutContext.GetOrCreateSymbol(Name.str());
415}
416
Evan Cheng055b0312009-06-29 07:51:04 +0000417bool ARMAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
Chris Lattnerc75c0282010-04-04 05:29:35 +0000418 unsigned AsmVariant, const char *ExtraCode,
419 raw_ostream &O) {
Evan Chenga8e29892007-01-19 07:51:42 +0000420 // Does this asm operand have a single letter operand modifier?
421 if (ExtraCode && ExtraCode[0]) {
422 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov8e9ece72009-08-08 23:10:41 +0000423
Evan Chenga8e29892007-01-19 07:51:42 +0000424 switch (ExtraCode[0]) {
Jack Carter0518fca2012-06-26 13:49:27 +0000425 default:
426 // See if this is a generic print operand
427 return AsmPrinter::PrintAsmOperand(MI, OpNum, AsmVariant, ExtraCode, O);
Bob Wilson9b4b00a2009-07-09 23:54:51 +0000428 case 'a': // Print as a memory address.
429 if (MI->getOperand(OpNum).isReg()) {
Jim Grosbach2f24c4e2010-09-30 15:25:22 +0000430 O << "["
431 << ARMInstPrinter::getRegisterName(MI->getOperand(OpNum).getReg())
432 << "]";
Bob Wilson9b4b00a2009-07-09 23:54:51 +0000433 return false;
434 }
435 // Fallthrough
436 case 'c': // Don't print "#" before an immediate operand.
Bob Wilson4f38b382009-08-21 21:58:55 +0000437 if (!MI->getOperand(OpNum).isImm())
438 return true;
Jim Grosbach2317e402010-09-30 01:57:53 +0000439 O << MI->getOperand(OpNum).getImm();
Bob Wilson8f343462009-04-06 21:46:51 +0000440 return false;
Evan Chenge21e3962007-04-04 00:13:29 +0000441 case 'P': // Print a VFP double precision register.
Evan Chengd831cda2009-12-08 23:06:22 +0000442 case 'q': // Print a NEON quad precision register.
Chris Lattner35c33bd2010-04-04 04:47:45 +0000443 printOperand(MI, OpNum, O);
Evan Cheng23a95702007-03-08 22:42:46 +0000444 return false;
Eric Christopher0628d382011-05-24 22:10:34 +0000445 case 'y': // Print a VFP single precision register as indexed double.
Eric Christopher0628d382011-05-24 22:10:34 +0000446 if (MI->getOperand(OpNum).isReg()) {
447 unsigned Reg = MI->getOperand(OpNum).getReg();
448 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
Jakob Stoklund Olesen4c91bda2012-05-30 23:00:43 +0000449 // Find the 'd' register that has this 's' register as a sub-register,
450 // and determine the lane number.
451 for (MCSuperRegIterator SR(Reg, TRI); SR.isValid(); ++SR) {
452 if (!ARM::DPRRegClass.contains(*SR))
453 continue;
454 bool Lane0 = TRI->getSubReg(*SR, ARM::ssub_0) == Reg;
455 O << ARMInstPrinter::getRegisterName(*SR) << (Lane0 ? "[0]" : "[1]");
456 return false;
457 }
Eric Christopher0628d382011-05-24 22:10:34 +0000458 }
Eric Christopher4db7dec2011-05-24 23:27:13 +0000459 return true;
Eric Christopherfef50062011-05-24 22:27:43 +0000460 case 'B': // Bitwise inverse of integer or symbol without a preceding #.
Eric Christophere1739d52011-05-24 23:15:43 +0000461 if (!MI->getOperand(OpNum).isImm())
462 return true;
463 O << ~(MI->getOperand(OpNum).getImm());
464 return false;
Eric Christopherfef50062011-05-24 22:27:43 +0000465 case 'L': // The low 16 bits of an immediate constant.
Eric Christopher4db7dec2011-05-24 23:27:13 +0000466 if (!MI->getOperand(OpNum).isImm())
467 return true;
468 O << (MI->getOperand(OpNum).getImm() & 0xffff);
469 return false;
Eric Christopher3c14f242011-05-28 01:40:44 +0000470 case 'M': { // A register range suitable for LDM/STM.
471 if (!MI->getOperand(OpNum).isReg())
472 return true;
473 const MachineOperand &MO = MI->getOperand(OpNum);
474 unsigned RegBegin = MO.getReg();
475 // This takes advantage of the 2 operand-ness of ldm/stm and that we've
476 // already got the operands in registers that are operands to the
477 // inline asm statement.
Jim Grosbach8e0c7692011-09-02 18:46:15 +0000478
Eric Christopher3c14f242011-05-28 01:40:44 +0000479 O << "{" << ARMInstPrinter::getRegisterName(RegBegin);
Jim Grosbach8e0c7692011-09-02 18:46:15 +0000480
Eric Christopher3c14f242011-05-28 01:40:44 +0000481 // FIXME: The register allocator not only may not have given us the
482 // registers in sequence, but may not be in ascending registers. This
483 // will require changes in the register allocator that'll need to be
484 // propagated down here if the operands change.
485 unsigned RegOps = OpNum + 1;
486 while (MI->getOperand(RegOps).isReg()) {
Jim Grosbach8e0c7692011-09-02 18:46:15 +0000487 O << ", "
Eric Christopher3c14f242011-05-28 01:40:44 +0000488 << ARMInstPrinter::getRegisterName(MI->getOperand(RegOps).getReg());
489 RegOps++;
490 }
491
492 O << "}";
493
494 return false;
495 }
Rafael Espindolaf5ade5d2011-08-10 16:26:42 +0000496 case 'R': // The most significant register of a pair.
497 case 'Q': { // The least significant register of a pair.
498 if (OpNum == 0)
499 return true;
500 const MachineOperand &FlagsOP = MI->getOperand(OpNum - 1);
501 if (!FlagsOP.isImm())
502 return true;
503 unsigned Flags = FlagsOP.getImm();
504 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
505 if (NumVals != 2)
506 return true;
507 unsigned RegOp = ExtraCode[0] == 'Q' ? OpNum : OpNum + 1;
508 if (RegOp >= MI->getNumOperands())
509 return true;
510 const MachineOperand &MO = MI->getOperand(RegOp);
511 if (!MO.isReg())
512 return true;
513 unsigned Reg = MO.getReg();
514 O << ARMInstPrinter::getRegisterName(Reg);
515 return false;
516 }
517
Eric Christopherfef50062011-05-24 22:27:43 +0000518 case 'e': // The low doubleword register of a NEON quad register.
Bob Wilson9cd2b952011-12-12 21:45:15 +0000519 case 'f': { // The high doubleword register of a NEON quad register.
520 if (!MI->getOperand(OpNum).isReg())
521 return true;
522 unsigned Reg = MI->getOperand(OpNum).getReg();
523 if (!ARM::QPRRegClass.contains(Reg))
524 return true;
525 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
526 unsigned SubReg = TRI->getSubReg(Reg, ExtraCode[0] == 'e' ?
527 ARM::dsub_0 : ARM::dsub_1);
528 O << ARMInstPrinter::getRegisterName(SubReg);
529 return false;
530 }
531
Eric Christopher001d2192012-08-13 18:18:52 +0000532 // This modifier is not yet supported.
Eric Christopherfef50062011-05-24 22:27:43 +0000533 case 'h': // A range of VFP/NEON registers suitable for VLD1/VST1.
Bob Wilsond984eb62010-05-27 20:23:42 +0000534 return true;
Eric Christopher001d2192012-08-13 18:18:52 +0000535 case 'H': // The highest-numbered register of a pair.
536 const MachineOperand &MO = MI->getOperand(OpNum);
537 if (!MO.isReg())
538 return true;
539 const TargetRegisterClass &RC = ARM::GPRRegClass;
540 const MachineFunction &MF = *MI->getParent()->getParent();
541 const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo();
542
543 unsigned RegIdx = TRI->getEncodingValue(MO.getReg());
544 RegIdx |= 1; //The odd register is also the higher-numbered one of a pair.
545
546 unsigned Reg = RC.getRegister(RegIdx);
547 O << ARMInstPrinter::getRegisterName(Reg);
548 return false;
Evan Cheng84f60b72010-05-27 22:08:38 +0000549 }
Evan Chenga8e29892007-01-19 07:51:42 +0000550 }
Jim Grosbache9952212009-09-04 01:38:51 +0000551
Chris Lattner35c33bd2010-04-04 04:47:45 +0000552 printOperand(MI, OpNum, O);
Evan Chenga8e29892007-01-19 07:51:42 +0000553 return false;
554}
555
Bob Wilson224c2442009-05-19 05:53:42 +0000556bool ARMAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Evan Cheng055b0312009-06-29 07:51:04 +0000557 unsigned OpNum, unsigned AsmVariant,
Chris Lattnerc75c0282010-04-04 05:29:35 +0000558 const char *ExtraCode,
559 raw_ostream &O) {
Eric Christopher8f894632011-05-25 20:51:58 +0000560 // Does this asm operand have a single letter operand modifier?
561 if (ExtraCode && ExtraCode[0]) {
562 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Jim Grosbach8e0c7692011-09-02 18:46:15 +0000563
Eric Christopher8f894632011-05-25 20:51:58 +0000564 switch (ExtraCode[0]) {
Eric Christopher32bfb2c2011-05-26 18:22:26 +0000565 case 'A': // A memory operand for a VLD1/VST1 instruction.
Eric Christopher8f894632011-05-25 20:51:58 +0000566 default: return true; // Unknown modifier.
567 case 'm': // The base register of a memory operand.
568 if (!MI->getOperand(OpNum).isReg())
569 return true;
570 O << ARMInstPrinter::getRegisterName(MI->getOperand(OpNum).getReg());
571 return false;
572 }
573 }
Jim Grosbach8e0c7692011-09-02 18:46:15 +0000574
Bob Wilson765cc0b2009-10-13 20:50:28 +0000575 const MachineOperand &MO = MI->getOperand(OpNum);
576 assert(MO.isReg() && "unexpected inline asm memory operand");
Jim Grosbach2317e402010-09-30 01:57:53 +0000577 O << "[" << ARMInstPrinter::getRegisterName(MO.getReg()) << "]";
Bob Wilson224c2442009-05-19 05:53:42 +0000578 return false;
579}
580
Bob Wilson812209a2009-09-30 22:06:26 +0000581void ARMAsmPrinter::EmitStartOfAsmFile(Module &M) {
Bob Wilson0fb34682009-09-30 00:23:42 +0000582 if (Subtarget->isTargetDarwin()) {
583 Reloc::Model RelocM = TM.getRelocationModel();
584 if (RelocM == Reloc::PIC_ || RelocM == Reloc::DynamicNoPIC) {
585 // Declare all the text sections up front (before the DWARF sections
586 // emitted by AsmPrinter::doInitialization) so the assembler will keep
587 // them together at the beginning of the object file. This helps
588 // avoid out-of-range branches that are due a fundamental limitation of
589 // the way symbol offsets are encoded with the current Darwin ARM
590 // relocations.
Jim Grosbachb0739b72010-09-02 01:02:06 +0000591 const TargetLoweringObjectFileMachO &TLOFMacho =
Dan Gohman0d805c32010-04-17 16:44:48 +0000592 static_cast<const TargetLoweringObjectFileMachO &>(
593 getObjFileLowering());
Bob Wilson29e06692009-09-30 22:25:37 +0000594 OutStreamer.SwitchSection(TLOFMacho.getTextSection());
595 OutStreamer.SwitchSection(TLOFMacho.getTextCoalSection());
596 OutStreamer.SwitchSection(TLOFMacho.getConstTextCoalSection());
597 if (RelocM == Reloc::DynamicNoPIC) {
598 const MCSection *sect =
Chris Lattner22772212010-04-08 20:40:11 +0000599 OutContext.getMachOSection("__TEXT", "__symbol_stub4",
600 MCSectionMachO::S_SYMBOL_STUBS,
601 12, SectionKind::getText());
Bob Wilson29e06692009-09-30 22:25:37 +0000602 OutStreamer.SwitchSection(sect);
603 } else {
604 const MCSection *sect =
Chris Lattner22772212010-04-08 20:40:11 +0000605 OutContext.getMachOSection("__TEXT", "__picsymbolstub4",
606 MCSectionMachO::S_SYMBOL_STUBS,
607 16, SectionKind::getText());
Bob Wilson29e06692009-09-30 22:25:37 +0000608 OutStreamer.SwitchSection(sect);
609 }
Bob Wilson63db5942010-07-30 19:55:47 +0000610 const MCSection *StaticInitSect =
611 OutContext.getMachOSection("__TEXT", "__StaticInit",
612 MCSectionMachO::S_REGULAR |
613 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
614 SectionKind::getText());
615 OutStreamer.SwitchSection(StaticInitSect);
Bob Wilson0fb34682009-09-30 00:23:42 +0000616 }
617 }
618
Jim Grosbache5165492009-11-09 00:11:35 +0000619 // Use unified assembler syntax.
Jason W Kimafd1cc22010-09-30 02:45:56 +0000620 OutStreamer.EmitAssemblerFlag(MCAF_SyntaxUnified);
Anton Korobeynikovd61eca52009-06-17 23:43:18 +0000621
Anton Korobeynikov88ce6672009-05-23 19:51:20 +0000622 // Emit ARM Build Attributes
Evan Cheng07043272012-02-21 20:46:00 +0000623 if (Subtarget->isTargetELF())
Jason W Kimdef9ac42010-10-06 22:36:46 +0000624 emitAttributes();
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000625}
626
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +0000627
Chris Lattner4a071d62009-10-19 17:59:19 +0000628void ARMAsmPrinter::EmitEndOfAsmFile(Module &M) {
Evan Cheng5be54b02007-01-19 19:25:36 +0000629 if (Subtarget->isTargetDarwin()) {
Chris Lattnerf61159b2009-08-03 22:18:15 +0000630 // All darwin targets use mach-o.
Dan Gohman0d805c32010-04-17 16:44:48 +0000631 const TargetLoweringObjectFileMachO &TLOFMacho =
632 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattnerb0f294c2009-10-19 18:38:33 +0000633 MachineModuleInfoMachO &MMIMacho =
634 MMI->getObjFileInfo<MachineModuleInfoMachO>();
Jim Grosbache9952212009-09-04 01:38:51 +0000635
Evan Chenga8e29892007-01-19 07:51:42 +0000636 // Output non-lazy-pointers for external and common global variables.
Chris Lattnerb0f294c2009-10-19 18:38:33 +0000637 MachineModuleInfoMachO::SymbolListTy Stubs = MMIMacho.GetGVStubList();
Bill Wendlingcebae362010-03-10 22:34:10 +0000638
Chris Lattnerb0f294c2009-10-19 18:38:33 +0000639 if (!Stubs.empty()) {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000640 // Switch with ".non_lazy_symbol_pointer" directive.
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000641 OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
Chris Lattnerc076a972009-08-10 18:01:34 +0000642 EmitAlignment(2);
Chris Lattnerb0f294c2009-10-19 18:38:33 +0000643 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Bill Wendlingbecd83e2010-03-09 00:40:17 +0000644 // L_foo$stub:
645 OutStreamer.EmitLabel(Stubs[i].first);
646 // .indirect_symbol _foo
Bill Wendling52a50e52010-03-11 01:18:13 +0000647 MachineModuleInfoImpl::StubValueTy &MCSym = Stubs[i].second;
648 OutStreamer.EmitSymbolAttribute(MCSym.getPointer(),MCSA_IndirectSymbol);
Bill Wendlingcf6f28d2010-03-09 00:43:34 +0000649
Bill Wendling52a50e52010-03-11 01:18:13 +0000650 if (MCSym.getInt())
Bill Wendlingcf6f28d2010-03-09 00:43:34 +0000651 // External to current translation unit.
652 OutStreamer.EmitIntValue(0, 4/*size*/, 0/*addrspace*/);
653 else
654 // Internal to current translation unit.
Bill Wendling5e1b55d2010-03-31 18:47:10 +0000655 //
Jim Grosbach1b935a32010-09-22 16:45:13 +0000656 // When we place the LSDA into the TEXT section, the type info
657 // pointers need to be indirect and pc-rel. We accomplish this by
658 // using NLPs; however, sometimes the types are local to the file.
659 // We need to fill in the value for the NLP in those cases.
Bill Wendling52a50e52010-03-11 01:18:13 +0000660 OutStreamer.EmitValue(MCSymbolRefExpr::Create(MCSym.getPointer(),
661 OutContext),
Bill Wendlingcf6f28d2010-03-09 00:43:34 +0000662 4/*size*/, 0/*addrspace*/);
Evan Chengae94e592008-12-05 01:06:39 +0000663 }
Bill Wendlingbecd83e2010-03-09 00:40:17 +0000664
665 Stubs.clear();
666 OutStreamer.AddBlankLine();
Evan Chenga8e29892007-01-19 07:51:42 +0000667 }
668
Chris Lattnere4d9ea82009-10-19 18:44:38 +0000669 Stubs = MMIMacho.GetHiddenGVStubList();
670 if (!Stubs.empty()) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000671 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
Chris Lattnerf3231de2009-08-10 18:02:16 +0000672 EmitAlignment(2);
Bill Wendlingbecd83e2010-03-09 00:40:17 +0000673 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
674 // L_foo$stub:
675 OutStreamer.EmitLabel(Stubs[i].first);
676 // .long _foo
Bill Wendlingcebae362010-03-10 22:34:10 +0000677 OutStreamer.EmitValue(MCSymbolRefExpr::
678 Create(Stubs[i].second.getPointer(),
679 OutContext),
Bill Wendlingbecd83e2010-03-09 00:40:17 +0000680 4/*size*/, 0/*addrspace*/);
681 }
Bill Wendlingcf6f28d2010-03-09 00:43:34 +0000682
683 Stubs.clear();
684 OutStreamer.AddBlankLine();
Evan Chengae94e592008-12-05 01:06:39 +0000685 }
686
Evan Chenga8e29892007-01-19 07:51:42 +0000687 // Funny Darwin hack: This flag tells the linker that no global symbols
688 // contain code that falls through to other global symbols (e.g. the obvious
689 // implementation of multiple entry points). If this doesn't occur, the
690 // linker can safely perform dead code stripping. Since LLVM never
691 // generates code that does this, it is always safe to set.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000692 OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000693 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000694}
Anton Korobeynikov0bd89712008-08-17 13:55:10 +0000695
Chris Lattner97f06932009-10-19 20:20:46 +0000696//===----------------------------------------------------------------------===//
Jason W Kimdef9ac42010-10-06 22:36:46 +0000697// Helper routines for EmitStartOfAsmFile() and EmitEndOfAsmFile()
698// FIXME:
699// The following seem like one-off assembler flags, but they actually need
Jim Grosbachfa7fb642010-10-06 22:46:47 +0000700// to appear in the .ARM.attributes section in ELF.
Jason W Kimdef9ac42010-10-06 22:36:46 +0000701// Instead of subclassing the MCELFStreamer, we do the work here.
702
703void ARMAsmPrinter::emitAttributes() {
Jim Grosbachfa7fb642010-10-06 22:46:47 +0000704
Jason W Kim17b443d2010-10-11 23:01:44 +0000705 emitARMAttributeSection();
706
Renato Golin728ff0d2011-02-28 22:04:27 +0000707 /* GAS expect .fpu to be emitted, regardless of VFP build attribute */
708 bool emitFPU = false;
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000709 AttributeEmitter *AttrEmitter;
Renato Golin728ff0d2011-02-28 22:04:27 +0000710 if (OutStreamer.hasRawTextSupport()) {
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000711 AttrEmitter = new AsmAttributeEmitter(OutStreamer);
Renato Golin728ff0d2011-02-28 22:04:27 +0000712 emitFPU = true;
713 } else {
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000714 MCObjectStreamer &O = static_cast<MCObjectStreamer&>(OutStreamer);
715 AttrEmitter = new ObjectAttributeEmitter(O);
716 }
717
718 AttrEmitter->MaybeSwitchVendor("aeabi");
719
Jason W Kimdef9ac42010-10-06 22:36:46 +0000720 std::string CPUString = Subtarget->getCPUString();
Jason W Kimf009a962011-02-07 00:49:53 +0000721
722 if (CPUString == "cortex-a8" ||
723 Subtarget->isCortexA8()) {
Jason W Kimc046d642011-02-07 19:07:11 +0000724 AttrEmitter->EmitTextAttribute(ARMBuildAttrs::CPU_name, "cortex-a8");
Jason W Kimf009a962011-02-07 00:49:53 +0000725 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v7);
726 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch_profile,
727 ARMBuildAttrs::ApplicationProfile);
728 AttrEmitter->EmitAttribute(ARMBuildAttrs::ARM_ISA_use,
729 ARMBuildAttrs::Allowed);
730 AttrEmitter->EmitAttribute(ARMBuildAttrs::THUMB_ISA_use,
731 ARMBuildAttrs::AllowThumb32);
732 // Fixme: figure out when this is emitted.
733 //AttrEmitter->EmitAttribute(ARMBuildAttrs::WMMX_arch,
734 // ARMBuildAttrs::AllowWMMXv1);
735 //
736
737 /// ADD additional Else-cases here!
Rafael Espindolab8adb8a2011-05-20 20:10:34 +0000738 } else if (CPUString == "xscale") {
739 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v5TEJ);
740 AttrEmitter->EmitAttribute(ARMBuildAttrs::ARM_ISA_use,
741 ARMBuildAttrs::Allowed);
742 AttrEmitter->EmitAttribute(ARMBuildAttrs::THUMB_ISA_use,
743 ARMBuildAttrs::Allowed);
Jason W Kimf009a962011-02-07 00:49:53 +0000744 } else if (CPUString == "generic") {
Dale Johannesen7179d1e2010-11-08 19:17:22 +0000745 // FIXME: Why these defaults?
746 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v4T);
Jason W Kimf009a962011-02-07 00:49:53 +0000747 AttrEmitter->EmitAttribute(ARMBuildAttrs::ARM_ISA_use,
748 ARMBuildAttrs::Allowed);
749 AttrEmitter->EmitAttribute(ARMBuildAttrs::THUMB_ISA_use,
750 ARMBuildAttrs::Allowed);
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000751 }
Jason W Kimdef9ac42010-10-06 22:36:46 +0000752
Renato Goline89a0532011-03-02 21:20:09 +0000753 if (Subtarget->hasNEON() && emitFPU) {
Renato Golin728ff0d2011-02-28 22:04:27 +0000754 /* NEON is not exactly a VFP architecture, but GAS emit one of
Anton Korobeynikov4b4e6222012-01-22 12:07:33 +0000755 * neon/neon-vfpv4/vfpv3/vfpv2 for .fpu parameters */
Evan Chengbee78fe2012-04-11 05:33:07 +0000756 if (Subtarget->hasVFP4())
Jim Grosbachd4f020a2012-04-06 23:43:50 +0000757 AttrEmitter->EmitTextAttribute(ARMBuildAttrs::Advanced_SIMD_arch,
758 "neon-vfpv4");
Anton Korobeynikov4b4e6222012-01-22 12:07:33 +0000759 else
Sebastian Pop74bebde2012-03-05 17:39:52 +0000760 AttrEmitter->EmitTextAttribute(ARMBuildAttrs::Advanced_SIMD_arch, "neon");
Renato Golin728ff0d2011-02-28 22:04:27 +0000761 /* If emitted for NEON, omit from VFP below, since you can have both
762 * NEON and VFP in build attributes but only one .fpu */
763 emitFPU = false;
764 }
765
Anton Korobeynikov4b4e6222012-01-22 12:07:33 +0000766 /* VFPv4 + .fpu */
767 if (Subtarget->hasVFP4()) {
768 AttrEmitter->EmitAttribute(ARMBuildAttrs::VFP_arch,
769 ARMBuildAttrs::AllowFPv4A);
770 if (emitFPU)
771 AttrEmitter->EmitTextAttribute(ARMBuildAttrs::VFP_arch, "vfpv4");
772
Renato Golin728ff0d2011-02-28 22:04:27 +0000773 /* VFPv3 + .fpu */
Anton Korobeynikov4b4e6222012-01-22 12:07:33 +0000774 } else if (Subtarget->hasVFP3()) {
Renato Golin728ff0d2011-02-28 22:04:27 +0000775 AttrEmitter->EmitAttribute(ARMBuildAttrs::VFP_arch,
776 ARMBuildAttrs::AllowFPv3A);
777 if (emitFPU)
778 AttrEmitter->EmitTextAttribute(ARMBuildAttrs::VFP_arch, "vfpv3");
779
780 /* VFPv2 + .fpu */
781 } else if (Subtarget->hasVFP2()) {
Jason W Kimf009a962011-02-07 00:49:53 +0000782 AttrEmitter->EmitAttribute(ARMBuildAttrs::VFP_arch,
783 ARMBuildAttrs::AllowFPv2);
Renato Golin728ff0d2011-02-28 22:04:27 +0000784 if (emitFPU)
785 AttrEmitter->EmitTextAttribute(ARMBuildAttrs::VFP_arch, "vfpv2");
786 }
787
788 /* TODO: ARMBuildAttrs::Allowed is not completely accurate,
Cameron Zwarich375db7f2011-07-07 08:28:52 +0000789 * since NEON can have 1 (allowed) or 2 (MAC operations) */
Renato Golin728ff0d2011-02-28 22:04:27 +0000790 if (Subtarget->hasNEON()) {
791 AttrEmitter->EmitAttribute(ARMBuildAttrs::Advanced_SIMD_arch,
792 ARMBuildAttrs::Allowed);
793 }
Jason W Kimdef9ac42010-10-06 22:36:46 +0000794
795 // Signal various FP modes.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000796 if (!TM.Options.UnsafeFPMath) {
Jason W Kimf009a962011-02-07 00:49:53 +0000797 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_FP_denormal,
798 ARMBuildAttrs::Allowed);
799 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_FP_exceptions,
800 ARMBuildAttrs::Allowed);
Jason W Kimdef9ac42010-10-06 22:36:46 +0000801 }
802
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000803 if (TM.Options.NoInfsFPMath && TM.Options.NoNaNsFPMath)
Jason W Kimf009a962011-02-07 00:49:53 +0000804 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_FP_number_model,
805 ARMBuildAttrs::Allowed);
Jason W Kimdef9ac42010-10-06 22:36:46 +0000806 else
Jason W Kimf009a962011-02-07 00:49:53 +0000807 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_FP_number_model,
808 ARMBuildAttrs::AllowIEE754);
Jason W Kimdef9ac42010-10-06 22:36:46 +0000809
Jason W Kimf009a962011-02-07 00:49:53 +0000810 // FIXME: add more flags to ARMBuildAttrs.h
Jason W Kimdef9ac42010-10-06 22:36:46 +0000811 // 8-bytes alignment stuff.
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000812 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_align8_needed, 1);
813 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_align8_preserved, 1);
Jason W Kimdef9ac42010-10-06 22:36:46 +0000814
815 // Hard float. Use both S and D registers and conform to AAPCS-VFP.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000816 if (Subtarget->isAAPCS_ABI() && TM.Options.FloatABIType == FloatABI::Hard) {
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000817 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_HardFP_use, 3);
818 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_VFP_args, 1);
Jason W Kimdef9ac42010-10-06 22:36:46 +0000819 }
820 // FIXME: Should we signal R9 usage?
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000821
Jason W Kimf009a962011-02-07 00:49:53 +0000822 if (Subtarget->hasDivide())
823 AttrEmitter->EmitAttribute(ARMBuildAttrs::DIV_use, 1);
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000824
825 AttrEmitter->Finish();
826 delete AttrEmitter;
Jason W Kimdef9ac42010-10-06 22:36:46 +0000827}
828
Jason W Kim17b443d2010-10-11 23:01:44 +0000829void ARMAsmPrinter::emitARMAttributeSection() {
830 // <format-version>
831 // [ <section-length> "vendor-name"
832 // [ <file-tag> <size> <attribute>*
833 // | <section-tag> <size> <section-number>* 0 <attribute>*
834 // | <symbol-tag> <size> <symbol-number>* 0 <attribute>*
835 // ]+
836 // ]*
837
838 if (OutStreamer.hasRawTextSupport())
839 return;
840
841 const ARMElfTargetObjectFile &TLOFELF =
842 static_cast<const ARMElfTargetObjectFile &>
843 (getObjFileLowering());
844
845 OutStreamer.SwitchSection(TLOFELF.getAttributesSection());
Jason W Kim17b443d2010-10-11 23:01:44 +0000846
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000847 // Format version
848 OutStreamer.EmitIntValue(0x41, 1);
Jason W Kim17b443d2010-10-11 23:01:44 +0000849}
850
Jason W Kimdef9ac42010-10-06 22:36:46 +0000851//===----------------------------------------------------------------------===//
Chris Lattner97f06932009-10-19 20:20:46 +0000852
Jim Grosbach988ce092010-09-18 00:05:05 +0000853static MCSymbol *getPICLabel(const char *Prefix, unsigned FunctionNumber,
854 unsigned LabelId, MCContext &Ctx) {
855
856 MCSymbol *Label = Ctx.GetOrCreateSymbol(Twine(Prefix)
857 + "PC" + Twine(FunctionNumber) + "_" + Twine(LabelId));
858 return Label;
859}
860
Jim Grosbach2c4d5122010-11-10 03:26:07 +0000861static MCSymbolRefExpr::VariantKind
862getModifierVariantKind(ARMCP::ARMCPModifier Modifier) {
863 switch (Modifier) {
Jim Grosbach2c4d5122010-11-10 03:26:07 +0000864 case ARMCP::no_modifier: return MCSymbolRefExpr::VK_None;
865 case ARMCP::TLSGD: return MCSymbolRefExpr::VK_ARM_TLSGD;
866 case ARMCP::TPOFF: return MCSymbolRefExpr::VK_ARM_TPOFF;
867 case ARMCP::GOTTPOFF: return MCSymbolRefExpr::VK_ARM_GOTTPOFF;
868 case ARMCP::GOT: return MCSymbolRefExpr::VK_ARM_GOT;
869 case ARMCP::GOTOFF: return MCSymbolRefExpr::VK_ARM_GOTOFF;
870 }
David Blaikie4d6ccb52012-01-20 21:51:11 +0000871 llvm_unreachable("Invalid ARMCPModifier!");
Jim Grosbach2c4d5122010-11-10 03:26:07 +0000872}
873
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000874MCSymbol *ARMAsmPrinter::GetARMGVSymbol(const GlobalValue *GV) {
875 bool isIndirect = Subtarget->isTargetDarwin() &&
876 Subtarget->GVIsIndirectSymbol(GV, TM.getRelocationModel());
877 if (!isIndirect)
878 return Mang->getSymbol(GV);
879
880 // FIXME: Remove this when Darwin transition to @GOT like syntax.
881 MCSymbol *MCSym = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
882 MachineModuleInfoMachO &MMIMachO =
883 MMI->getObjFileInfo<MachineModuleInfoMachO>();
884 MachineModuleInfoImpl::StubValueTy &StubSym =
885 GV->hasHiddenVisibility() ? MMIMachO.getHiddenGVStubEntry(MCSym) :
886 MMIMachO.getGVStubEntry(MCSym);
887 if (StubSym.getPointer() == 0)
888 StubSym = MachineModuleInfoImpl::
889 StubValueTy(Mang->getSymbol(GV), !GV->hasInternalLinkage());
890 return MCSym;
891}
892
Jim Grosbach5df08d82010-11-09 18:45:04 +0000893void ARMAsmPrinter::
894EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
895 int Size = TM.getTargetData()->getTypeAllocSize(MCPV->getType());
896
897 ARMConstantPoolValue *ACPV = static_cast<ARMConstantPoolValue*>(MCPV);
Jim Grosbach5df08d82010-11-09 18:45:04 +0000898
Jim Grosbach7c7ddb22010-11-10 17:59:10 +0000899 MCSymbol *MCSym;
Jim Grosbach5df08d82010-11-09 18:45:04 +0000900 if (ACPV->isLSDA()) {
Jim Grosbach7c7ddb22010-11-10 17:59:10 +0000901 SmallString<128> Str;
902 raw_svector_ostream OS(Str);
Jim Grosbach5df08d82010-11-09 18:45:04 +0000903 OS << MAI->getPrivateGlobalPrefix() << "_LSDA_" << getFunctionNumber();
Jim Grosbach7c7ddb22010-11-10 17:59:10 +0000904 MCSym = OutContext.GetOrCreateSymbol(OS.str());
Jim Grosbach5df08d82010-11-09 18:45:04 +0000905 } else if (ACPV->isBlockAddress()) {
Bill Wendling5bb77992011-10-01 08:00:54 +0000906 const BlockAddress *BA =
907 cast<ARMConstantPoolConstant>(ACPV)->getBlockAddress();
908 MCSym = GetBlockAddressSymbol(BA);
Jim Grosbach5df08d82010-11-09 18:45:04 +0000909 } else if (ACPV->isGlobalValue()) {
Bill Wendling5bb77992011-10-01 08:00:54 +0000910 const GlobalValue *GV = cast<ARMConstantPoolConstant>(ACPV)->getGV();
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000911 MCSym = GetARMGVSymbol(GV);
Bill Wendlinge00897c2011-09-29 23:50:42 +0000912 } else if (ACPV->isMachineBasicBlock()) {
Bill Wendling3320f2a2011-10-01 09:30:42 +0000913 const MachineBasicBlock *MBB = cast<ARMConstantPoolMBB>(ACPV)->getMBB();
Bill Wendlinge00897c2011-09-29 23:50:42 +0000914 MCSym = MBB->getSymbol();
Jim Grosbach5df08d82010-11-09 18:45:04 +0000915 } else {
916 assert(ACPV->isExtSymbol() && "unrecognized constant pool value");
Bill Wendlingfe31e672011-10-01 08:58:29 +0000917 const char *Sym = cast<ARMConstantPoolSymbol>(ACPV)->getSymbol();
918 MCSym = GetExternalSymbolSymbol(Sym);
Jim Grosbach5df08d82010-11-09 18:45:04 +0000919 }
920
921 // Create an MCSymbol for the reference.
Jim Grosbach2c4d5122010-11-10 03:26:07 +0000922 const MCExpr *Expr =
923 MCSymbolRefExpr::Create(MCSym, getModifierVariantKind(ACPV->getModifier()),
924 OutContext);
Jim Grosbach5df08d82010-11-09 18:45:04 +0000925
Jim Grosbach2c4d5122010-11-10 03:26:07 +0000926 if (ACPV->getPCAdjustment()) {
927 MCSymbol *PCLabel = getPICLabel(MAI->getPrivateGlobalPrefix(),
928 getFunctionNumber(),
929 ACPV->getLabelId(),
930 OutContext);
931 const MCExpr *PCRelExpr = MCSymbolRefExpr::Create(PCLabel, OutContext);
932 PCRelExpr =
933 MCBinaryExpr::CreateAdd(PCRelExpr,
934 MCConstantExpr::Create(ACPV->getPCAdjustment(),
935 OutContext),
936 OutContext);
937 if (ACPV->mustAddCurrentAddress()) {
938 // We want "(<expr> - .)", but MC doesn't have a concept of the '.'
939 // label, so just emit a local label end reference that instead.
940 MCSymbol *DotSym = OutContext.CreateTempSymbol();
941 OutStreamer.EmitLabel(DotSym);
942 const MCExpr *DotExpr = MCSymbolRefExpr::Create(DotSym, OutContext);
943 PCRelExpr = MCBinaryExpr::CreateSub(PCRelExpr, DotExpr, OutContext);
Jim Grosbach5df08d82010-11-09 18:45:04 +0000944 }
Jim Grosbach2c4d5122010-11-10 03:26:07 +0000945 Expr = MCBinaryExpr::CreateSub(Expr, PCRelExpr, OutContext);
Jim Grosbach5df08d82010-11-09 18:45:04 +0000946 }
Jim Grosbach2c4d5122010-11-10 03:26:07 +0000947 OutStreamer.EmitValue(Expr, Size);
Jim Grosbach5df08d82010-11-09 18:45:04 +0000948}
949
Jim Grosbacha2244cb2010-09-22 17:39:48 +0000950void ARMAsmPrinter::EmitJumpTable(const MachineInstr *MI) {
951 unsigned Opcode = MI->getOpcode();
952 int OpNum = 1;
953 if (Opcode == ARM::BR_JTadd)
954 OpNum = 2;
955 else if (Opcode == ARM::BR_JTm)
956 OpNum = 3;
957
958 const MachineOperand &MO1 = MI->getOperand(OpNum);
959 const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
960 unsigned JTI = MO1.getIndex();
961
962 // Emit a label for the jump table.
963 MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel2(JTI, MO2.getImm());
964 OutStreamer.EmitLabel(JTISymbol);
965
Jim Grosbach3e965312012-05-18 19:12:01 +0000966 // Mark the jump table as data-in-code.
967 OutStreamer.EmitDataRegion(MCDR_DataRegionJT32);
968
Jim Grosbacha2244cb2010-09-22 17:39:48 +0000969 // Emit each entry of the table.
970 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
971 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
972 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
973
974 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
975 MachineBasicBlock *MBB = JTBBs[i];
976 // Construct an MCExpr for the entry. We want a value of the form:
977 // (BasicBlockAddr - TableBeginAddr)
978 //
979 // For example, a table with entries jumping to basic blocks BB0 and BB1
980 // would look like:
981 // LJTI_0_0:
982 // .word (LBB0 - LJTI_0_0)
983 // .word (LBB1 - LJTI_0_0)
984 const MCExpr *Expr = MCSymbolRefExpr::Create(MBB->getSymbol(), OutContext);
985
986 if (TM.getRelocationModel() == Reloc::PIC_)
987 Expr = MCBinaryExpr::CreateSub(Expr, MCSymbolRefExpr::Create(JTISymbol,
988 OutContext),
989 OutContext);
Jim Grosbachde982732011-08-31 22:23:09 +0000990 // If we're generating a table of Thumb addresses in static relocation
991 // model, we need to add one to keep interworking correctly.
992 else if (AFI->isThumbFunction())
993 Expr = MCBinaryExpr::CreateAdd(Expr, MCConstantExpr::Create(1,OutContext),
994 OutContext);
Jim Grosbacha2244cb2010-09-22 17:39:48 +0000995 OutStreamer.EmitValue(Expr, 4);
996 }
Jim Grosbach3e965312012-05-18 19:12:01 +0000997 // Mark the end of jump table data-in-code region.
998 OutStreamer.EmitDataRegion(MCDR_DataRegionEnd);
Jim Grosbacha2244cb2010-09-22 17:39:48 +0000999}
1000
Jim Grosbach882ef2b2010-09-21 23:28:16 +00001001void ARMAsmPrinter::EmitJump2Table(const MachineInstr *MI) {
1002 unsigned Opcode = MI->getOpcode();
1003 int OpNum = (Opcode == ARM::t2BR_JT) ? 2 : 1;
1004 const MachineOperand &MO1 = MI->getOperand(OpNum);
1005 const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
1006 unsigned JTI = MO1.getIndex();
1007
Jim Grosbach882ef2b2010-09-21 23:28:16 +00001008 MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel2(JTI, MO2.getImm());
1009 OutStreamer.EmitLabel(JTISymbol);
1010
1011 // Emit each entry of the table.
1012 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
1013 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
1014 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
Jim Grosbach205a5fa2010-09-22 17:15:35 +00001015 unsigned OffsetWidth = 4;
Jim Grosbach3e965312012-05-18 19:12:01 +00001016 if (MI->getOpcode() == ARM::t2TBB_JT) {
Jim Grosbach205a5fa2010-09-22 17:15:35 +00001017 OffsetWidth = 1;
Jim Grosbach3e965312012-05-18 19:12:01 +00001018 // Mark the jump table as data-in-code.
1019 OutStreamer.EmitDataRegion(MCDR_DataRegionJT8);
1020 } else if (MI->getOpcode() == ARM::t2TBH_JT) {
Jim Grosbach205a5fa2010-09-22 17:15:35 +00001021 OffsetWidth = 2;
Jim Grosbach3e965312012-05-18 19:12:01 +00001022 // Mark the jump table as data-in-code.
1023 OutStreamer.EmitDataRegion(MCDR_DataRegionJT16);
1024 }
Jim Grosbach882ef2b2010-09-21 23:28:16 +00001025
1026 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
1027 MachineBasicBlock *MBB = JTBBs[i];
Jim Grosbach205a5fa2010-09-22 17:15:35 +00001028 const MCExpr *MBBSymbolExpr = MCSymbolRefExpr::Create(MBB->getSymbol(),
1029 OutContext);
Jim Grosbach882ef2b2010-09-21 23:28:16 +00001030 // If this isn't a TBB or TBH, the entries are direct branch instructions.
Jim Grosbach205a5fa2010-09-22 17:15:35 +00001031 if (OffsetWidth == 4) {
Jim Grosbach882ef2b2010-09-21 23:28:16 +00001032 MCInst BrInst;
1033 BrInst.setOpcode(ARM::t2B);
Jim Grosbach205a5fa2010-09-22 17:15:35 +00001034 BrInst.addOperand(MCOperand::CreateExpr(MBBSymbolExpr));
Owen Anderson51f6a7a2011-09-09 21:48:23 +00001035 BrInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1036 BrInst.addOperand(MCOperand::CreateReg(0));
Jim Grosbach882ef2b2010-09-21 23:28:16 +00001037 OutStreamer.EmitInstruction(BrInst);
1038 continue;
1039 }
1040 // Otherwise it's an offset from the dispatch instruction. Construct an
Jim Grosbach205a5fa2010-09-22 17:15:35 +00001041 // MCExpr for the entry. We want a value of the form:
1042 // (BasicBlockAddr - TableBeginAddr) / 2
1043 //
1044 // For example, a TBB table with entries jumping to basic blocks BB0 and BB1
1045 // would look like:
1046 // LJTI_0_0:
1047 // .byte (LBB0 - LJTI_0_0) / 2
1048 // .byte (LBB1 - LJTI_0_0) / 2
1049 const MCExpr *Expr =
1050 MCBinaryExpr::CreateSub(MBBSymbolExpr,
1051 MCSymbolRefExpr::Create(JTISymbol, OutContext),
1052 OutContext);
1053 Expr = MCBinaryExpr::CreateDiv(Expr, MCConstantExpr::Create(2, OutContext),
1054 OutContext);
1055 OutStreamer.EmitValue(Expr, OffsetWidth);
Jim Grosbach882ef2b2010-09-21 23:28:16 +00001056 }
Jim Grosbachb3a119a2012-05-21 23:34:42 +00001057 // Mark the end of jump table data-in-code region. 32-bit offsets use
1058 // actual branch instructions here, so we don't mark those as a data-region
1059 // at all.
1060 if (OffsetWidth != 4)
1061 OutStreamer.EmitDataRegion(MCDR_DataRegionEnd);
Jim Grosbach882ef2b2010-09-21 23:28:16 +00001062}
1063
Jim Grosbach2d0f53b2010-09-28 17:05:56 +00001064void ARMAsmPrinter::PrintDebugValueComment(const MachineInstr *MI,
1065 raw_ostream &OS) {
1066 unsigned NOps = MI->getNumOperands();
1067 assert(NOps==4);
1068 OS << '\t' << MAI->getCommentString() << "DEBUG_VALUE: ";
1069 // cast away const; DIetc do not take const operands for some reason.
1070 DIVariable V(const_cast<MDNode *>(MI->getOperand(NOps-1).getMetadata()));
1071 OS << V.getName();
1072 OS << " <- ";
1073 // Frame address. Currently handles register +- offset only.
1074 assert(MI->getOperand(0).isReg() && MI->getOperand(1).isImm());
1075 OS << '['; printOperand(MI, 0, OS); OS << '+'; printOperand(MI, 1, OS);
1076 OS << ']';
1077 OS << "+";
1078 printOperand(MI, NOps-2, OS);
1079}
1080
Jim Grosbach40edf732010-12-14 21:10:47 +00001081static void populateADROperands(MCInst &Inst, unsigned Dest,
1082 const MCSymbol *Label,
1083 unsigned pred, unsigned ccreg,
1084 MCContext &Ctx) {
1085 const MCExpr *SymbolExpr = MCSymbolRefExpr::Create(Label, Ctx);
1086 Inst.addOperand(MCOperand::CreateReg(Dest));
1087 Inst.addOperand(MCOperand::CreateExpr(SymbolExpr));
1088 // Add predicate operands.
1089 Inst.addOperand(MCOperand::CreateImm(pred));
1090 Inst.addOperand(MCOperand::CreateReg(ccreg));
1091}
1092
Anton Korobeynikov4d728602011-01-01 20:38:38 +00001093void ARMAsmPrinter::EmitPatchedInstruction(const MachineInstr *MI,
1094 unsigned Opcode) {
1095 MCInst TmpInst;
1096
1097 // Emit the instruction as usual, just patch the opcode.
1098 LowerARMMachineInstrToMCInst(MI, TmpInst, *this);
1099 TmpInst.setOpcode(Opcode);
1100 OutStreamer.EmitInstruction(TmpInst);
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
Jim Grosbachdff84b02010-12-02 00:28:45 +00001279 MCInst TmpInst;
Jim Grosbachd40963c2010-12-14 22:28:03 +00001280 TmpInst.setOpcode(MI->getOpcode() == ARM::t2LEApcrel ? ARM::t2ADR
1281 : (MI->getOpcode() == ARM::tLEApcrel ? ARM::tADR
1282 : ARM::ADR));
Jim Grosbach40edf732010-12-14 21:10:47 +00001283 populateADROperands(TmpInst, MI->getOperand(0).getReg(),
1284 GetCPISymbol(MI->getOperand(1).getIndex()),
1285 MI->getOperand(2).getImm(), MI->getOperand(3).getReg(),
1286 OutContext);
Jim Grosbachdff84b02010-12-02 00:28:45 +00001287 OutStreamer.EmitInstruction(TmpInst);
1288 return;
1289 }
Jim Grosbachd40963c2010-12-14 22:28:03 +00001290 case ARM::LEApcrelJT:
1291 case ARM::tLEApcrelJT:
1292 case ARM::t2LEApcrelJT: {
Jim Grosbach5d14f9b2010-12-01 19:47:31 +00001293 MCInst TmpInst;
Jim Grosbachd40963c2010-12-14 22:28:03 +00001294 TmpInst.setOpcode(MI->getOpcode() == ARM::t2LEApcrelJT ? ARM::t2ADR
1295 : (MI->getOpcode() == ARM::tLEApcrelJT ? ARM::tADR
1296 : ARM::ADR));
Jim Grosbach40edf732010-12-14 21:10:47 +00001297 populateADROperands(TmpInst, MI->getOperand(0).getReg(),
1298 GetARMJTIPICJumpTableLabel2(MI->getOperand(1).getIndex(),
1299 MI->getOperand(2).getImm()),
1300 MI->getOperand(3).getImm(), MI->getOperand(4).getReg(),
1301 OutContext);
Jim Grosbach5d14f9b2010-12-01 19:47:31 +00001302 OutStreamer.EmitInstruction(TmpInst);
1303 return;
1304 }
Jim Grosbachf859a542011-03-12 00:45:26 +00001305 // Darwin call instructions are just normal call instructions with different
1306 // clobber semantics (they clobber R9).
Jim Grosbacha0d2c8a2010-11-30 18:30:19 +00001307 case ARM::BX_CALL: {
1308 {
1309 MCInst TmpInst;
1310 TmpInst.setOpcode(ARM::MOVr);
1311 TmpInst.addOperand(MCOperand::CreateReg(ARM::LR));
1312 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1313 // Add predicate operands.
1314 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1315 TmpInst.addOperand(MCOperand::CreateReg(0));
1316 // Add 's' bit operand (always reg0 for this)
1317 TmpInst.addOperand(MCOperand::CreateReg(0));
1318 OutStreamer.EmitInstruction(TmpInst);
1319 }
1320 {
1321 MCInst TmpInst;
1322 TmpInst.setOpcode(ARM::BX);
1323 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1324 OutStreamer.EmitInstruction(TmpInst);
1325 }
1326 return;
1327 }
Cameron Zwarichad70f6d2011-05-25 21:53:50 +00001328 case ARM::tBX_CALL: {
1329 {
1330 MCInst TmpInst;
1331 TmpInst.setOpcode(ARM::tMOVr);
1332 TmpInst.addOperand(MCOperand::CreateReg(ARM::LR));
1333 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
Jim Grosbach63b46fa2011-06-30 22:10:46 +00001334 // Add predicate operands.
1335 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1336 TmpInst.addOperand(MCOperand::CreateReg(0));
Cameron Zwarichad70f6d2011-05-25 21:53:50 +00001337 OutStreamer.EmitInstruction(TmpInst);
1338 }
1339 {
1340 MCInst TmpInst;
1341 TmpInst.setOpcode(ARM::tBX);
1342 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1343 // Add predicate operands.
1344 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1345 TmpInst.addOperand(MCOperand::CreateReg(0));
1346 OutStreamer.EmitInstruction(TmpInst);
1347 }
1348 return;
1349 }
Jim Grosbacha0d2c8a2010-11-30 18:30:19 +00001350 case ARM::BMOVPCRX_CALL: {
1351 {
1352 MCInst TmpInst;
1353 TmpInst.setOpcode(ARM::MOVr);
1354 TmpInst.addOperand(MCOperand::CreateReg(ARM::LR));
1355 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1356 // Add predicate operands.
1357 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1358 TmpInst.addOperand(MCOperand::CreateReg(0));
1359 // Add 's' bit operand (always reg0 for this)
1360 TmpInst.addOperand(MCOperand::CreateReg(0));
1361 OutStreamer.EmitInstruction(TmpInst);
1362 }
1363 {
1364 MCInst TmpInst;
1365 TmpInst.setOpcode(ARM::MOVr);
1366 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1367 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1368 // Add predicate operands.
1369 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1370 TmpInst.addOperand(MCOperand::CreateReg(0));
1371 // Add 's' bit operand (always reg0 for this)
1372 TmpInst.addOperand(MCOperand::CreateReg(0));
1373 OutStreamer.EmitInstruction(TmpInst);
1374 }
1375 return;
1376 }
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001377 case ARM::BMOVPCB_CALL: {
1378 {
1379 MCInst TmpInst;
1380 TmpInst.setOpcode(ARM::MOVr);
1381 TmpInst.addOperand(MCOperand::CreateReg(ARM::LR));
1382 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1383 // Add predicate operands.
1384 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1385 TmpInst.addOperand(MCOperand::CreateReg(0));
1386 // Add 's' bit operand (always reg0 for this)
1387 TmpInst.addOperand(MCOperand::CreateReg(0));
1388 OutStreamer.EmitInstruction(TmpInst);
1389 }
1390 {
1391 MCInst TmpInst;
1392 TmpInst.setOpcode(ARM::Bcc);
1393 const GlobalValue *GV = MI->getOperand(0).getGlobal();
1394 MCSymbol *GVSym = Mang->getSymbol(GV);
1395 const MCExpr *GVSymExpr = MCSymbolRefExpr::Create(GVSym, OutContext);
1396 TmpInst.addOperand(MCOperand::CreateExpr(GVSymExpr));
1397 // Add predicate operands.
1398 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1399 TmpInst.addOperand(MCOperand::CreateReg(0));
1400 OutStreamer.EmitInstruction(TmpInst);
1401 }
1402 return;
1403 }
Evan Cheng4bfcd4a2012-02-28 18:51:51 +00001404 case ARM::t2BMOVPCB_CALL: {
1405 {
1406 MCInst TmpInst;
1407 TmpInst.setOpcode(ARM::tMOVr);
1408 TmpInst.addOperand(MCOperand::CreateReg(ARM::LR));
1409 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1410 // Add predicate operands.
1411 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1412 TmpInst.addOperand(MCOperand::CreateReg(0));
1413 OutStreamer.EmitInstruction(TmpInst);
1414 }
1415 {
1416 MCInst TmpInst;
1417 TmpInst.setOpcode(ARM::t2B);
1418 const GlobalValue *GV = MI->getOperand(0).getGlobal();
1419 MCSymbol *GVSym = Mang->getSymbol(GV);
1420 const MCExpr *GVSymExpr = MCSymbolRefExpr::Create(GVSym, OutContext);
1421 TmpInst.addOperand(MCOperand::CreateExpr(GVSymExpr));
1422 // Add predicate operands.
1423 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1424 TmpInst.addOperand(MCOperand::CreateReg(0));
1425 OutStreamer.EmitInstruction(TmpInst);
1426 }
1427 return;
1428 }
Evan Cheng53519f02011-01-21 18:55:51 +00001429 case ARM::MOVi16_ga_pcrel:
1430 case ARM::t2MOVi16_ga_pcrel: {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001431 MCInst TmpInst;
Evan Cheng53519f02011-01-21 18:55:51 +00001432 TmpInst.setOpcode(Opc == ARM::MOVi16_ga_pcrel? ARM::MOVi16 : ARM::t2MOVi16);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001433 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1434
Evan Cheng53519f02011-01-21 18:55:51 +00001435 unsigned TF = MI->getOperand(1).getTargetFlags();
1436 bool isPIC = TF == ARMII::MO_LO16_NONLAZY_PIC;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001437 const GlobalValue *GV = MI->getOperand(1).getGlobal();
1438 MCSymbol *GVSym = GetARMGVSymbol(GV);
1439 const MCExpr *GVSymExpr = MCSymbolRefExpr::Create(GVSym, OutContext);
Evan Cheng53519f02011-01-21 18:55:51 +00001440 if (isPIC) {
1441 MCSymbol *LabelSym = getPICLabel(MAI->getPrivateGlobalPrefix(),
1442 getFunctionNumber(),
1443 MI->getOperand(2).getImm(), OutContext);
1444 const MCExpr *LabelSymExpr= MCSymbolRefExpr::Create(LabelSym, OutContext);
1445 unsigned PCAdj = (Opc == ARM::MOVi16_ga_pcrel) ? 8 : 4;
1446 const MCExpr *PCRelExpr =
1447 ARMMCExpr::CreateLower16(MCBinaryExpr::CreateSub(GVSymExpr,
1448 MCBinaryExpr::CreateAdd(LabelSymExpr,
1449 MCConstantExpr::Create(PCAdj, OutContext),
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001450 OutContext), OutContext), OutContext);
Evan Cheng53519f02011-01-21 18:55:51 +00001451 TmpInst.addOperand(MCOperand::CreateExpr(PCRelExpr));
1452 } else {
1453 const MCExpr *RefExpr= ARMMCExpr::CreateLower16(GVSymExpr, OutContext);
1454 TmpInst.addOperand(MCOperand::CreateExpr(RefExpr));
1455 }
1456
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001457 // Add predicate operands.
1458 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1459 TmpInst.addOperand(MCOperand::CreateReg(0));
1460 // Add 's' bit operand (always reg0 for this)
1461 TmpInst.addOperand(MCOperand::CreateReg(0));
1462 OutStreamer.EmitInstruction(TmpInst);
1463 return;
1464 }
Evan Cheng53519f02011-01-21 18:55:51 +00001465 case ARM::MOVTi16_ga_pcrel:
1466 case ARM::t2MOVTi16_ga_pcrel: {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001467 MCInst TmpInst;
Evan Cheng53519f02011-01-21 18:55:51 +00001468 TmpInst.setOpcode(Opc == ARM::MOVTi16_ga_pcrel
1469 ? ARM::MOVTi16 : ARM::t2MOVTi16);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001470 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1471 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
1472
Evan Cheng53519f02011-01-21 18:55:51 +00001473 unsigned TF = MI->getOperand(2).getTargetFlags();
1474 bool isPIC = TF == ARMII::MO_HI16_NONLAZY_PIC;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001475 const GlobalValue *GV = MI->getOperand(2).getGlobal();
1476 MCSymbol *GVSym = GetARMGVSymbol(GV);
1477 const MCExpr *GVSymExpr = MCSymbolRefExpr::Create(GVSym, OutContext);
Evan Cheng53519f02011-01-21 18:55:51 +00001478 if (isPIC) {
1479 MCSymbol *LabelSym = getPICLabel(MAI->getPrivateGlobalPrefix(),
1480 getFunctionNumber(),
1481 MI->getOperand(3).getImm(), OutContext);
1482 const MCExpr *LabelSymExpr= MCSymbolRefExpr::Create(LabelSym, OutContext);
1483 unsigned PCAdj = (Opc == ARM::MOVTi16_ga_pcrel) ? 8 : 4;
1484 const MCExpr *PCRelExpr =
1485 ARMMCExpr::CreateUpper16(MCBinaryExpr::CreateSub(GVSymExpr,
1486 MCBinaryExpr::CreateAdd(LabelSymExpr,
1487 MCConstantExpr::Create(PCAdj, OutContext),
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001488 OutContext), OutContext), OutContext);
Evan Cheng53519f02011-01-21 18:55:51 +00001489 TmpInst.addOperand(MCOperand::CreateExpr(PCRelExpr));
1490 } else {
1491 const MCExpr *RefExpr= ARMMCExpr::CreateUpper16(GVSymExpr, OutContext);
1492 TmpInst.addOperand(MCOperand::CreateExpr(RefExpr));
1493 }
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001494 // Add predicate operands.
1495 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1496 TmpInst.addOperand(MCOperand::CreateReg(0));
1497 // Add 's' bit operand (always reg0 for this)
1498 TmpInst.addOperand(MCOperand::CreateReg(0));
1499 OutStreamer.EmitInstruction(TmpInst);
1500 return;
1501 }
Jim Grosbachfbd18732010-09-17 23:41:53 +00001502 case ARM::tPICADD: {
1503 // This is a pseudo op for a label + instruction sequence, which looks like:
1504 // LPC0:
1505 // add r0, pc
1506 // This adds the address of LPC0 to r0.
1507
1508 // Emit the label.
Jim Grosbach988ce092010-09-18 00:05:05 +00001509 OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(),
1510 getFunctionNumber(), MI->getOperand(2).getImm(),
1511 OutContext));
Jim Grosbachfbd18732010-09-17 23:41:53 +00001512
1513 // Form and emit the add.
1514 MCInst AddInst;
1515 AddInst.setOpcode(ARM::tADDhirr);
1516 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1517 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1518 AddInst.addOperand(MCOperand::CreateReg(ARM::PC));
1519 // Add predicate operands.
1520 AddInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1521 AddInst.addOperand(MCOperand::CreateReg(0));
1522 OutStreamer.EmitInstruction(AddInst);
1523 return;
1524 }
Jim Grosbacha3fbadf2010-09-30 19:53:58 +00001525 case ARM::PICADD: {
Chris Lattner4d152222009-10-19 22:23:04 +00001526 // This is a pseudo op for a label + instruction sequence, which looks like:
1527 // LPC0:
1528 // add r0, pc, r0
1529 // This adds the address of LPC0 to r0.
Jim Grosbachb0739b72010-09-02 01:02:06 +00001530
Chris Lattner4d152222009-10-19 22:23:04 +00001531 // Emit the label.
Jim Grosbach988ce092010-09-18 00:05:05 +00001532 OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(),
1533 getFunctionNumber(), MI->getOperand(2).getImm(),
1534 OutContext));
Jim Grosbachb0739b72010-09-02 01:02:06 +00001535
Jim Grosbachf3f09522010-09-14 21:05:34 +00001536 // Form and emit the add.
Chris Lattner4d152222009-10-19 22:23:04 +00001537 MCInst AddInst;
1538 AddInst.setOpcode(ARM::ADDrr);
1539 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1540 AddInst.addOperand(MCOperand::CreateReg(ARM::PC));
1541 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
Jim Grosbach5b46d622010-09-14 21:28:17 +00001542 // Add predicate operands.
1543 AddInst.addOperand(MCOperand::CreateImm(MI->getOperand(3).getImm()));
1544 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(4).getReg()));
1545 // Add 's' bit operand (always reg0 for this)
1546 AddInst.addOperand(MCOperand::CreateReg(0));
Chris Lattner850d2e22010-02-03 01:16:28 +00001547 OutStreamer.EmitInstruction(AddInst);
Chris Lattner4d152222009-10-19 22:23:04 +00001548 return;
1549 }
Jim Grosbacha28abbe2010-09-17 16:25:52 +00001550 case ARM::PICSTR:
1551 case ARM::PICSTRB:
1552 case ARM::PICSTRH:
1553 case ARM::PICLDR:
1554 case ARM::PICLDRB:
1555 case ARM::PICLDRH:
1556 case ARM::PICLDRSB:
1557 case ARM::PICLDRSH: {
Jim Grosbachb74ca9d2010-09-16 17:43:25 +00001558 // This is a pseudo op for a label + instruction sequence, which looks like:
1559 // LPC0:
Jim Grosbacha28abbe2010-09-17 16:25:52 +00001560 // OP r0, [pc, r0]
Jim Grosbachb74ca9d2010-09-16 17:43:25 +00001561 // The LCP0 label is referenced by a constant pool entry in order to get
1562 // a PC-relative address at the ldr instruction.
1563
1564 // Emit the label.
Jim Grosbach988ce092010-09-18 00:05:05 +00001565 OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(),
1566 getFunctionNumber(), MI->getOperand(2).getImm(),
1567 OutContext));
Jim Grosbachb74ca9d2010-09-16 17:43:25 +00001568
1569 // Form and emit the load
Jim Grosbacha28abbe2010-09-17 16:25:52 +00001570 unsigned Opcode;
1571 switch (MI->getOpcode()) {
1572 default:
1573 llvm_unreachable("Unexpected opcode!");
Jim Grosbach7e3383c2010-10-27 23:12:14 +00001574 case ARM::PICSTR: Opcode = ARM::STRrs; break;
1575 case ARM::PICSTRB: Opcode = ARM::STRBrs; break;
Jim Grosbacha28abbe2010-09-17 16:25:52 +00001576 case ARM::PICSTRH: Opcode = ARM::STRH; break;
Jim Grosbach3e556122010-10-26 22:37:02 +00001577 case ARM::PICLDR: Opcode = ARM::LDRrs; break;
Jim Grosbachc1d30212010-10-27 00:19:44 +00001578 case ARM::PICLDRB: Opcode = ARM::LDRBrs; break;
Jim Grosbacha28abbe2010-09-17 16:25:52 +00001579 case ARM::PICLDRH: Opcode = ARM::LDRH; break;
1580 case ARM::PICLDRSB: Opcode = ARM::LDRSB; break;
1581 case ARM::PICLDRSH: Opcode = ARM::LDRSH; break;
1582 }
1583 MCInst LdStInst;
1584 LdStInst.setOpcode(Opcode);
1585 LdStInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1586 LdStInst.addOperand(MCOperand::CreateReg(ARM::PC));
1587 LdStInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
1588 LdStInst.addOperand(MCOperand::CreateImm(0));
Jim Grosbachb74ca9d2010-09-16 17:43:25 +00001589 // Add predicate operands.
Jim Grosbacha28abbe2010-09-17 16:25:52 +00001590 LdStInst.addOperand(MCOperand::CreateImm(MI->getOperand(3).getImm()));
1591 LdStInst.addOperand(MCOperand::CreateReg(MI->getOperand(4).getReg()));
1592 OutStreamer.EmitInstruction(LdStInst);
Jim Grosbachb74ca9d2010-09-16 17:43:25 +00001593
1594 return;
1595 }
Jim Grosbacha3fbadf2010-09-30 19:53:58 +00001596 case ARM::CONSTPOOL_ENTRY: {
Chris Lattnera70e6442009-10-19 22:33:05 +00001597 /// CONSTPOOL_ENTRY - This instruction represents a floating constant pool
1598 /// in the function. The first operand is the ID# for this instruction, the
1599 /// second is the index into the MachineConstantPool that this is, the third
1600 /// is the size in bytes of this constant pool entry.
Jakob Stoklund Olesen3e572ac2011-12-06 01:43:02 +00001601 /// The required alignment is specified on the basic block holding this MI.
Chris Lattnera70e6442009-10-19 22:33:05 +00001602 unsigned LabelId = (unsigned)MI->getOperand(0).getImm();
1603 unsigned CPIdx = (unsigned)MI->getOperand(1).getIndex();
1604
Jim Grosbach3e965312012-05-18 19:12:01 +00001605 // If this is the first entry of the pool, mark it.
1606 if (!InConstantPool) {
1607 OutStreamer.EmitDataRegion(MCDR_DataRegion);
1608 InConstantPool = true;
1609 }
1610
Chris Lattner1b46f432010-01-23 07:00:21 +00001611 OutStreamer.EmitLabel(GetCPISymbol(LabelId));
Chris Lattnera70e6442009-10-19 22:33:05 +00001612
1613 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPIdx];
1614 if (MCPE.isMachineConstantPoolEntry())
1615 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
1616 else
1617 EmitGlobalConstant(MCPE.Val.ConstVal);
Chris Lattnera70e6442009-10-19 22:33:05 +00001618 return;
1619 }
Jim Grosbach882ef2b2010-09-21 23:28:16 +00001620 case ARM::t2BR_JT: {
1621 // Lower and emit the instruction itself, then the jump table following it.
1622 MCInst TmpInst;
Jim Grosbach2a7b41b2011-06-30 23:38:17 +00001623 TmpInst.setOpcode(ARM::tMOVr);
Jim Grosbach5ca66692010-11-29 22:37:40 +00001624 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1625 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1626 // Add predicate operands.
1627 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1628 TmpInst.addOperand(MCOperand::CreateReg(0));
Jim Grosbach882ef2b2010-09-21 23:28:16 +00001629 OutStreamer.EmitInstruction(TmpInst);
Jim Grosbach5ca66692010-11-29 22:37:40 +00001630 // Output the data for the jump table itself
1631 EmitJump2Table(MI);
1632 return;
1633 }
1634 case ARM::t2TBB_JT: {
1635 // Lower and emit the instruction itself, then the jump table following it.
1636 MCInst TmpInst;
1637
1638 TmpInst.setOpcode(ARM::t2TBB);
1639 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1640 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1641 // Add predicate operands.
1642 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1643 TmpInst.addOperand(MCOperand::CreateReg(0));
1644 OutStreamer.EmitInstruction(TmpInst);
1645 // Output the data for the jump table itself
1646 EmitJump2Table(MI);
1647 // Make sure the next instruction is 2-byte aligned.
1648 EmitAlignment(1);
1649 return;
1650 }
1651 case ARM::t2TBH_JT: {
1652 // Lower and emit the instruction itself, then the jump table following it.
1653 MCInst TmpInst;
1654
1655 TmpInst.setOpcode(ARM::t2TBH);
1656 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1657 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1658 // Add predicate operands.
1659 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1660 TmpInst.addOperand(MCOperand::CreateReg(0));
1661 OutStreamer.EmitInstruction(TmpInst);
1662 // Output the data for the jump table itself
Jim Grosbach882ef2b2010-09-21 23:28:16 +00001663 EmitJump2Table(MI);
1664 return;
1665 }
Jim Grosbachf1aa47d2010-11-29 19:32:47 +00001666 case ARM::tBR_JTr:
Jim Grosbach2dc77682010-11-29 18:37:44 +00001667 case ARM::BR_JTr: {
1668 // Lower and emit the instruction itself, then the jump table following it.
1669 // mov pc, target
1670 MCInst TmpInst;
Jim Grosbach5ca66692010-11-29 22:37:40 +00001671 unsigned Opc = MI->getOpcode() == ARM::BR_JTr ?
Jim Grosbach2a7b41b2011-06-30 23:38:17 +00001672 ARM::MOVr : ARM::tMOVr;
Jim Grosbachf1aa47d2010-11-29 19:32:47 +00001673 TmpInst.setOpcode(Opc);
Jim Grosbach2dc77682010-11-29 18:37:44 +00001674 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1675 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1676 // Add predicate operands.
1677 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1678 TmpInst.addOperand(MCOperand::CreateReg(0));
Jim Grosbacha0d2c8a2010-11-30 18:30:19 +00001679 // Add 's' bit operand (always reg0 for this)
1680 if (Opc == ARM::MOVr)
1681 TmpInst.addOperand(MCOperand::CreateReg(0));
Jim Grosbach2dc77682010-11-29 18:37:44 +00001682 OutStreamer.EmitInstruction(TmpInst);
1683
Jim Grosbachf1aa47d2010-11-29 19:32:47 +00001684 // Make sure the Thumb jump table is 4-byte aligned.
Jim Grosbach2a7b41b2011-06-30 23:38:17 +00001685 if (Opc == ARM::tMOVr)
Jim Grosbachf1aa47d2010-11-29 19:32:47 +00001686 EmitAlignment(2);
1687
Jim Grosbach2dc77682010-11-29 18:37:44 +00001688 // Output the data for the jump table itself
1689 EmitJumpTable(MI);
1690 return;
1691 }
1692 case ARM::BR_JTm: {
1693 // Lower and emit the instruction itself, then the jump table following it.
1694 // ldr pc, target
1695 MCInst TmpInst;
1696 if (MI->getOperand(1).getReg() == 0) {
1697 // literal offset
1698 TmpInst.setOpcode(ARM::LDRi12);
1699 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1700 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1701 TmpInst.addOperand(MCOperand::CreateImm(MI->getOperand(2).getImm()));
1702 } else {
1703 TmpInst.setOpcode(ARM::LDRrs);
1704 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1705 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1706 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
1707 TmpInst.addOperand(MCOperand::CreateImm(0));
1708 }
1709 // Add predicate operands.
1710 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1711 TmpInst.addOperand(MCOperand::CreateReg(0));
1712 OutStreamer.EmitInstruction(TmpInst);
1713
1714 // Output the data for the jump table itself
Jim Grosbacha2244cb2010-09-22 17:39:48 +00001715 EmitJumpTable(MI);
1716 return;
1717 }
Jim Grosbachf8dabac2010-11-17 21:05:55 +00001718 case ARM::BR_JTadd: {
1719 // Lower and emit the instruction itself, then the jump table following it.
1720 // add pc, target, idx
Jim Grosbach2dc77682010-11-29 18:37:44 +00001721 MCInst TmpInst;
1722 TmpInst.setOpcode(ARM::ADDrr);
1723 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1724 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1725 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
Jim Grosbachf8dabac2010-11-17 21:05:55 +00001726 // Add predicate operands.
Jim Grosbach2dc77682010-11-29 18:37:44 +00001727 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1728 TmpInst.addOperand(MCOperand::CreateReg(0));
Jim Grosbachf8dabac2010-11-17 21:05:55 +00001729 // Add 's' bit operand (always reg0 for this)
Jim Grosbach2dc77682010-11-29 18:37:44 +00001730 TmpInst.addOperand(MCOperand::CreateReg(0));
1731 OutStreamer.EmitInstruction(TmpInst);
Jim Grosbachf8dabac2010-11-17 21:05:55 +00001732
1733 // Output the data for the jump table itself
1734 EmitJumpTable(MI);
1735 return;
1736 }
Jim Grosbach2e6ae132010-09-23 18:05:37 +00001737 case ARM::TRAP: {
1738 // Non-Darwin binutils don't yet support the "trap" mnemonic.
1739 // FIXME: Remove this special case when they do.
1740 if (!Subtarget->isTargetDarwin()) {
Jim Grosbach78890f42010-10-01 23:21:38 +00001741 //.long 0xe7ffdefe @ trap
Jim Grosbachb2dda4b2010-09-23 19:42:17 +00001742 uint32_t Val = 0xe7ffdefeUL;
Jim Grosbach2e6ae132010-09-23 18:05:37 +00001743 OutStreamer.AddComment("trap");
1744 OutStreamer.EmitIntValue(Val, 4);
1745 return;
1746 }
1747 break;
1748 }
1749 case ARM::tTRAP: {
1750 // Non-Darwin binutils don't yet support the "trap" mnemonic.
1751 // FIXME: Remove this special case when they do.
1752 if (!Subtarget->isTargetDarwin()) {
Jim Grosbach78890f42010-10-01 23:21:38 +00001753 //.short 57086 @ trap
Benjamin Kramerc8ab9eb2010-09-23 18:57:26 +00001754 uint16_t Val = 0xdefe;
Jim Grosbach2e6ae132010-09-23 18:05:37 +00001755 OutStreamer.AddComment("trap");
1756 OutStreamer.EmitIntValue(Val, 2);
1757 return;
1758 }
1759 break;
1760 }
Jim Grosbach433a5782010-09-24 20:47:58 +00001761 case ARM::t2Int_eh_sjlj_setjmp:
1762 case ARM::t2Int_eh_sjlj_setjmp_nofp:
Jim Grosbacha3fbadf2010-09-30 19:53:58 +00001763 case ARM::tInt_eh_sjlj_setjmp: {
Jim Grosbach433a5782010-09-24 20:47:58 +00001764 // Two incoming args: GPR:$src, GPR:$val
1765 // mov $val, pc
1766 // adds $val, #7
1767 // str $val, [$src, #4]
1768 // movs r0, #0
1769 // b 1f
1770 // movs r0, #1
1771 // 1:
1772 unsigned SrcReg = MI->getOperand(0).getReg();
1773 unsigned ValReg = MI->getOperand(1).getReg();
1774 MCSymbol *Label = GetARMSJLJEHLabel();
1775 {
1776 MCInst TmpInst;
Jim Grosbach2a7b41b2011-06-30 23:38:17 +00001777 TmpInst.setOpcode(ARM::tMOVr);
Jim Grosbach433a5782010-09-24 20:47:58 +00001778 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1779 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
Jim Grosbach63b46fa2011-06-30 22:10:46 +00001780 // Predicate.
1781 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1782 TmpInst.addOperand(MCOperand::CreateReg(0));
Jim Grosbach433a5782010-09-24 20:47:58 +00001783 OutStreamer.AddComment("eh_setjmp begin");
1784 OutStreamer.EmitInstruction(TmpInst);
1785 }
1786 {
1787 MCInst TmpInst;
1788 TmpInst.setOpcode(ARM::tADDi3);
1789 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1790 // 's' bit operand
1791 TmpInst.addOperand(MCOperand::CreateReg(ARM::CPSR));
1792 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1793 TmpInst.addOperand(MCOperand::CreateImm(7));
1794 // Predicate.
1795 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1796 TmpInst.addOperand(MCOperand::CreateReg(0));
1797 OutStreamer.EmitInstruction(TmpInst);
1798 }
1799 {
1800 MCInst TmpInst;
Bill Wendlingf4caf692010-12-14 03:36:38 +00001801 TmpInst.setOpcode(ARM::tSTRi);
Jim Grosbach433a5782010-09-24 20:47:58 +00001802 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1803 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
1804 // The offset immediate is #4. The operand value is scaled by 4 for the
1805 // tSTR instruction.
1806 TmpInst.addOperand(MCOperand::CreateImm(1));
Jim Grosbach433a5782010-09-24 20:47:58 +00001807 // Predicate.
1808 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1809 TmpInst.addOperand(MCOperand::CreateReg(0));
1810 OutStreamer.EmitInstruction(TmpInst);
1811 }
1812 {
1813 MCInst TmpInst;
1814 TmpInst.setOpcode(ARM::tMOVi8);
1815 TmpInst.addOperand(MCOperand::CreateReg(ARM::R0));
1816 TmpInst.addOperand(MCOperand::CreateReg(ARM::CPSR));
1817 TmpInst.addOperand(MCOperand::CreateImm(0));
1818 // Predicate.
1819 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1820 TmpInst.addOperand(MCOperand::CreateReg(0));
1821 OutStreamer.EmitInstruction(TmpInst);
1822 }
1823 {
1824 const MCExpr *SymbolExpr = MCSymbolRefExpr::Create(Label, OutContext);
1825 MCInst TmpInst;
1826 TmpInst.setOpcode(ARM::tB);
1827 TmpInst.addOperand(MCOperand::CreateExpr(SymbolExpr));
Owen Anderson51f6a7a2011-09-09 21:48:23 +00001828 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1829 TmpInst.addOperand(MCOperand::CreateReg(0));
Jim Grosbach433a5782010-09-24 20:47:58 +00001830 OutStreamer.EmitInstruction(TmpInst);
1831 }
1832 {
1833 MCInst TmpInst;
1834 TmpInst.setOpcode(ARM::tMOVi8);
1835 TmpInst.addOperand(MCOperand::CreateReg(ARM::R0));
1836 TmpInst.addOperand(MCOperand::CreateReg(ARM::CPSR));
1837 TmpInst.addOperand(MCOperand::CreateImm(1));
1838 // Predicate.
1839 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1840 TmpInst.addOperand(MCOperand::CreateReg(0));
1841 OutStreamer.AddComment("eh_setjmp end");
1842 OutStreamer.EmitInstruction(TmpInst);
1843 }
1844 OutStreamer.EmitLabel(Label);
1845 return;
1846 }
1847
Jim Grosbach45390082010-09-23 23:33:56 +00001848 case ARM::Int_eh_sjlj_setjmp_nofp:
Jim Grosbacha3fbadf2010-09-30 19:53:58 +00001849 case ARM::Int_eh_sjlj_setjmp: {
Jim Grosbach45390082010-09-23 23:33:56 +00001850 // Two incoming args: GPR:$src, GPR:$val
1851 // add $val, pc, #8
1852 // str $val, [$src, #+4]
1853 // mov r0, #0
1854 // add pc, pc, #0
1855 // mov r0, #1
1856 unsigned SrcReg = MI->getOperand(0).getReg();
1857 unsigned ValReg = MI->getOperand(1).getReg();
1858
1859 {
1860 MCInst TmpInst;
1861 TmpInst.setOpcode(ARM::ADDri);
1862 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1863 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1864 TmpInst.addOperand(MCOperand::CreateImm(8));
1865 // Predicate.
1866 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1867 TmpInst.addOperand(MCOperand::CreateReg(0));
1868 // 's' bit operand (always reg0 for this).
1869 TmpInst.addOperand(MCOperand::CreateReg(0));
1870 OutStreamer.AddComment("eh_setjmp begin");
1871 OutStreamer.EmitInstruction(TmpInst);
1872 }
1873 {
1874 MCInst TmpInst;
Jim Grosbach7e3383c2010-10-27 23:12:14 +00001875 TmpInst.setOpcode(ARM::STRi12);
Jim Grosbach45390082010-09-23 23:33:56 +00001876 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1877 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
Jim Grosbach45390082010-09-23 23:33:56 +00001878 TmpInst.addOperand(MCOperand::CreateImm(4));
1879 // Predicate.
1880 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1881 TmpInst.addOperand(MCOperand::CreateReg(0));
1882 OutStreamer.EmitInstruction(TmpInst);
1883 }
1884 {
1885 MCInst TmpInst;
1886 TmpInst.setOpcode(ARM::MOVi);
1887 TmpInst.addOperand(MCOperand::CreateReg(ARM::R0));
1888 TmpInst.addOperand(MCOperand::CreateImm(0));
1889 // Predicate.
1890 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1891 TmpInst.addOperand(MCOperand::CreateReg(0));
1892 // 's' bit operand (always reg0 for this).
1893 TmpInst.addOperand(MCOperand::CreateReg(0));
1894 OutStreamer.EmitInstruction(TmpInst);
1895 }
1896 {
1897 MCInst TmpInst;
1898 TmpInst.setOpcode(ARM::ADDri);
1899 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1900 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1901 TmpInst.addOperand(MCOperand::CreateImm(0));
1902 // Predicate.
1903 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1904 TmpInst.addOperand(MCOperand::CreateReg(0));
1905 // 's' bit operand (always reg0 for this).
1906 TmpInst.addOperand(MCOperand::CreateReg(0));
1907 OutStreamer.EmitInstruction(TmpInst);
1908 }
1909 {
1910 MCInst TmpInst;
1911 TmpInst.setOpcode(ARM::MOVi);
1912 TmpInst.addOperand(MCOperand::CreateReg(ARM::R0));
1913 TmpInst.addOperand(MCOperand::CreateImm(1));
1914 // Predicate.
1915 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1916 TmpInst.addOperand(MCOperand::CreateReg(0));
1917 // 's' bit operand (always reg0 for this).
1918 TmpInst.addOperand(MCOperand::CreateReg(0));
1919 OutStreamer.AddComment("eh_setjmp end");
1920 OutStreamer.EmitInstruction(TmpInst);
1921 }
1922 return;
1923 }
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001924 case ARM::Int_eh_sjlj_longjmp: {
1925 // ldr sp, [$src, #8]
1926 // ldr $scratch, [$src, #4]
1927 // ldr r7, [$src]
1928 // bx $scratch
1929 unsigned SrcReg = MI->getOperand(0).getReg();
1930 unsigned ScratchReg = MI->getOperand(1).getReg();
1931 {
1932 MCInst TmpInst;
Jim Grosbach3e556122010-10-26 22:37:02 +00001933 TmpInst.setOpcode(ARM::LDRi12);
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001934 TmpInst.addOperand(MCOperand::CreateReg(ARM::SP));
1935 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001936 TmpInst.addOperand(MCOperand::CreateImm(8));
1937 // Predicate.
1938 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1939 TmpInst.addOperand(MCOperand::CreateReg(0));
1940 OutStreamer.EmitInstruction(TmpInst);
1941 }
1942 {
1943 MCInst TmpInst;
Jim Grosbach3e556122010-10-26 22:37:02 +00001944 TmpInst.setOpcode(ARM::LDRi12);
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001945 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1946 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001947 TmpInst.addOperand(MCOperand::CreateImm(4));
1948 // Predicate.
1949 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1950 TmpInst.addOperand(MCOperand::CreateReg(0));
1951 OutStreamer.EmitInstruction(TmpInst);
1952 }
1953 {
1954 MCInst TmpInst;
Jim Grosbach3e556122010-10-26 22:37:02 +00001955 TmpInst.setOpcode(ARM::LDRi12);
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001956 TmpInst.addOperand(MCOperand::CreateReg(ARM::R7));
1957 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001958 TmpInst.addOperand(MCOperand::CreateImm(0));
1959 // Predicate.
1960 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1961 TmpInst.addOperand(MCOperand::CreateReg(0));
1962 OutStreamer.EmitInstruction(TmpInst);
1963 }
1964 {
1965 MCInst TmpInst;
Bill Wendling6e46d842010-11-30 00:48:15 +00001966 TmpInst.setOpcode(ARM::BX);
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001967 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1968 // Predicate.
1969 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1970 TmpInst.addOperand(MCOperand::CreateReg(0));
1971 OutStreamer.EmitInstruction(TmpInst);
1972 }
1973 return;
1974 }
Jim Grosbach385cc5e2010-09-27 22:28:11 +00001975 case ARM::tInt_eh_sjlj_longjmp: {
1976 // ldr $scratch, [$src, #8]
1977 // mov sp, $scratch
1978 // ldr $scratch, [$src, #4]
1979 // ldr r7, [$src]
1980 // bx $scratch
1981 unsigned SrcReg = MI->getOperand(0).getReg();
1982 unsigned ScratchReg = MI->getOperand(1).getReg();
1983 {
1984 MCInst TmpInst;
Bill Wendlingf4caf692010-12-14 03:36:38 +00001985 TmpInst.setOpcode(ARM::tLDRi);
Jim Grosbach385cc5e2010-09-27 22:28:11 +00001986 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1987 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
1988 // The offset immediate is #8. The operand value is scaled by 4 for the
Bill Wendlingf4caf692010-12-14 03:36:38 +00001989 // tLDR instruction.
Jim Grosbach385cc5e2010-09-27 22:28:11 +00001990 TmpInst.addOperand(MCOperand::CreateImm(2));
Jim Grosbach385cc5e2010-09-27 22:28:11 +00001991 // Predicate.
1992 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1993 TmpInst.addOperand(MCOperand::CreateReg(0));
1994 OutStreamer.EmitInstruction(TmpInst);
1995 }
1996 {
1997 MCInst TmpInst;
Jim Grosbach2a7b41b2011-06-30 23:38:17 +00001998 TmpInst.setOpcode(ARM::tMOVr);
Jim Grosbach385cc5e2010-09-27 22:28:11 +00001999 TmpInst.addOperand(MCOperand::CreateReg(ARM::SP));
2000 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
2001 // Predicate.
2002 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
2003 TmpInst.addOperand(MCOperand::CreateReg(0));
2004 OutStreamer.EmitInstruction(TmpInst);
2005 }
2006 {
2007 MCInst TmpInst;
Bill Wendlingf4caf692010-12-14 03:36:38 +00002008 TmpInst.setOpcode(ARM::tLDRi);
Jim Grosbach385cc5e2010-09-27 22:28:11 +00002009 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
2010 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
2011 TmpInst.addOperand(MCOperand::CreateImm(1));
Jim Grosbach385cc5e2010-09-27 22:28:11 +00002012 // Predicate.
2013 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
2014 TmpInst.addOperand(MCOperand::CreateReg(0));
2015 OutStreamer.EmitInstruction(TmpInst);
2016 }
2017 {
2018 MCInst TmpInst;
Bob Wilson93abbc22012-04-07 16:51:59 +00002019 TmpInst.setOpcode(ARM::tLDRi);
Jim Grosbach385cc5e2010-09-27 22:28:11 +00002020 TmpInst.addOperand(MCOperand::CreateReg(ARM::R7));
2021 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
Bob Wilson93abbc22012-04-07 16:51:59 +00002022 TmpInst.addOperand(MCOperand::CreateImm(0));
Jim Grosbach385cc5e2010-09-27 22:28:11 +00002023 // Predicate.
2024 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
2025 TmpInst.addOperand(MCOperand::CreateReg(0));
2026 OutStreamer.EmitInstruction(TmpInst);
2027 }
2028 {
2029 MCInst TmpInst;
Cameron Zwarich421b1062011-05-26 03:41:12 +00002030 TmpInst.setOpcode(ARM::tBX);
Jim Grosbach385cc5e2010-09-27 22:28:11 +00002031 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
2032 // Predicate.
2033 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
2034 TmpInst.addOperand(MCOperand::CreateReg(0));
2035 OutStreamer.EmitInstruction(TmpInst);
2036 }
2037 return;
2038 }
Chris Lattner97f06932009-10-19 20:20:46 +00002039 }
Jim Grosbachb0739b72010-09-02 01:02:06 +00002040
Chris Lattner97f06932009-10-19 20:20:46 +00002041 MCInst TmpInst;
Chris Lattner30e2cc22010-11-14 21:00:02 +00002042 LowerARMMachineInstrToMCInst(MI, TmpInst, *this);
Anton Korobeynikov57caad72011-03-05 18:43:32 +00002043
Chris Lattner850d2e22010-02-03 01:16:28 +00002044 OutStreamer.EmitInstruction(TmpInst);
Chris Lattner97f06932009-10-19 20:20:46 +00002045}
Daniel Dunbar2685a292009-10-20 05:15:36 +00002046
2047//===----------------------------------------------------------------------===//
2048// Target Registry Stuff
2049//===----------------------------------------------------------------------===//
2050
Daniel Dunbar2685a292009-10-20 05:15:36 +00002051// Force static initialization.
2052extern "C" void LLVMInitializeARMAsmPrinter() {
2053 RegisterAsmPrinter<ARMAsmPrinter> X(TheARMTarget);
2054 RegisterAsmPrinter<ARMAsmPrinter> Y(TheThumbTarget);
Daniel Dunbar2685a292009-10-20 05:15:36 +00002055}