blob: 2f1b88e145cf58af8eed4b34ab31907f180f4a2d [file] [log] [blame]
Chris Lattner97f06932009-10-19 20:20:46 +00001//===-- ARMAsmPrinter.cpp - Print machine code to an ARM .s file ----------===//
2//
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
Rafael Espindola7bc59bc2006-05-14 22:18:28 +00006// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains a printer that converts from our internal representation
11// of machine-dependent LLVM code to GAS-format ARM assembly language.
12//
13//===----------------------------------------------------------------------===//
14
Chris Lattner95b2c7d2006-12-19 22:59:26 +000015#define DEBUG_TYPE "asm-printer"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000016#include "ARM.h"
Jim Grosbachbaf120f2010-12-01 03:45:07 +000017#include "ARMAsmPrinter.h"
Evan Chengb72d2a92011-01-11 21:46:47 +000018#include "ARMBuildAttrs.h"
19#include "ARMBaseRegisterInfo.h"
20#include "ARMConstantPoolValue.h"
Chris Lattner97f06932009-10-19 20:20:46 +000021#include "ARMMachineFunctionInfo.h"
Chris Lattner97f06932009-10-19 20:20:46 +000022#include "ARMTargetMachine.h"
Jason W Kim17b443d2010-10-11 23:01:44 +000023#include "ARMTargetObjectFile.h"
Evan Chengb72d2a92011-01-11 21:46:47 +000024#include "InstPrinter/ARMInstPrinter.h"
Evan Chengee04a6d2011-07-20 23:34:39 +000025#include "MCTargetDesc/ARMAddressingModes.h"
26#include "MCTargetDesc/ARMMCExpr.h"
Dale Johannesen3f282aa2010-04-26 20:07:31 +000027#include "llvm/Analysis/DebugInfo.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000028#include "llvm/Constants.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000029#include "llvm/Module.h"
Benjamin Kramere55b15f2009-12-28 12:27:56 +000030#include "llvm/Type.h"
Dan Gohmancf20ac42009-08-13 01:36:44 +000031#include "llvm/Assembly/Writer.h"
Chris Lattnerb0f294c2009-10-19 18:38:33 +000032#include "llvm/CodeGen/MachineModuleInfoImpls.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000033#include "llvm/CodeGen/MachineFunctionPass.h"
Evan Chenga8e29892007-01-19 07:51:42 +000034#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattnerb0f294c2009-10-19 18:38:33 +000035#include "llvm/MC/MCAsmInfo.h"
Rafael Espindolacecbc3d2010-10-25 17:50:35 +000036#include "llvm/MC/MCAssembler.h"
Chris Lattnerb0f294c2009-10-19 18:38:33 +000037#include "llvm/MC/MCContext.h"
Bill Wendlingbecd83e2010-03-09 00:40:17 +000038#include "llvm/MC/MCExpr.h"
Chris Lattner97f06932009-10-19 20:20:46 +000039#include "llvm/MC/MCInst.h"
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000040#include "llvm/MC/MCSectionMachO.h"
Rafael Espindolacecbc3d2010-10-25 17:50:35 +000041#include "llvm/MC/MCObjectStreamer.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000042#include "llvm/MC/MCStreamer.h"
Chris Lattner325d3dc2009-09-13 17:14:04 +000043#include "llvm/MC/MCSymbol.h"
Chris Lattnerd62f1b42010-03-12 21:19:23 +000044#include "llvm/Target/Mangler.h"
Rafael Espindolab01c4bb2006-07-27 11:38:51 +000045#include "llvm/Target/TargetData.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000046#include "llvm/Target/TargetMachine.h"
Evan Cheng5be54b02007-01-19 19:25:36 +000047#include "llvm/Target/TargetOptions.h"
Evan Chengc324ecb2009-07-24 18:19:46 +000048#include "llvm/ADT/SmallPtrSet.h"
Jim Grosbachc40d9f92009-09-01 18:49:12 +000049#include "llvm/ADT/SmallString.h"
Bob Wilson54c78ef2009-11-06 23:33:28 +000050#include "llvm/ADT/StringExtras.h"
Chris Lattner97f06932009-10-19 20:20:46 +000051#include "llvm/Support/CommandLine.h"
Devang Patel59135f42010-08-04 22:39:39 +000052#include "llvm/Support/Debug.h"
Torok Edwin30464702009-07-08 20:55:50 +000053#include "llvm/Support/ErrorHandling.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000054#include "llvm/Support/TargetRegistry.h"
Chris Lattnerb23569a2010-04-04 08:18:47 +000055#include "llvm/Support/raw_ostream.h"
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000056#include <cctype>
Rafael Espindola7bc59bc2006-05-14 22:18:28 +000057using namespace llvm;
58
Chris Lattner95b2c7d2006-12-19 22:59:26 +000059namespace {
Rafael Espindolacecbc3d2010-10-25 17:50:35 +000060
61 // Per section and per symbol attributes are not supported.
62 // To implement them we would need the ability to delay this emission
63 // until the assembly file is fully parsed/generated as only then do we
64 // know the symbol and section numbers.
65 class AttributeEmitter {
66 public:
67 virtual void MaybeSwitchVendor(StringRef Vendor) = 0;
68 virtual void EmitAttribute(unsigned Attribute, unsigned Value) = 0;
Jason W Kimf009a962011-02-07 00:49:53 +000069 virtual void EmitTextAttribute(unsigned Attribute, StringRef String) = 0;
Rafael Espindolacecbc3d2010-10-25 17:50:35 +000070 virtual void Finish() = 0;
Rafael Espindola4921e232010-10-25 18:38:32 +000071 virtual ~AttributeEmitter() {}
Rafael Espindolacecbc3d2010-10-25 17:50:35 +000072 };
73
74 class AsmAttributeEmitter : public AttributeEmitter {
75 MCStreamer &Streamer;
76
77 public:
78 AsmAttributeEmitter(MCStreamer &Streamer_) : Streamer(Streamer_) {}
79 void MaybeSwitchVendor(StringRef Vendor) { }
80
81 void EmitAttribute(unsigned Attribute, unsigned Value) {
82 Streamer.EmitRawText("\t.eabi_attribute " +
83 Twine(Attribute) + ", " + Twine(Value));
84 }
85
Jason W Kimf009a962011-02-07 00:49:53 +000086 void EmitTextAttribute(unsigned Attribute, StringRef String) {
87 switch (Attribute) {
88 case ARMBuildAttrs::CPU_name:
Jason W Kimc046d642011-02-07 19:07:11 +000089 Streamer.EmitRawText(StringRef("\t.cpu ") + LowercaseString(String));
Jason W Kimf009a962011-02-07 00:49:53 +000090 break;
Renato Golin728ff0d2011-02-28 22:04:27 +000091 /* GAS requires .fpu to be emitted regardless of EABI attribute */
92 case ARMBuildAttrs::Advanced_SIMD_arch:
93 case ARMBuildAttrs::VFP_arch:
94 Streamer.EmitRawText(StringRef("\t.fpu ") + LowercaseString(String));
Jim Grosbach8e0c7692011-09-02 18:46:15 +000095 break;
Jason W Kimf009a962011-02-07 00:49:53 +000096 default: assert(0 && "Unsupported Text attribute in ASM Mode"); break;
97 }
98 }
Rafael Espindolacecbc3d2010-10-25 17:50:35 +000099 void Finish() { }
100 };
101
102 class ObjectAttributeEmitter : public AttributeEmitter {
Renato Golin719927a2011-08-09 09:50:10 +0000103 // This structure holds all attributes, accounting for
104 // their string/numeric value, so we can later emmit them
105 // in declaration order, keeping all in the same vector
106 struct AttributeItemType {
107 enum {
108 HiddenAttribute = 0,
109 NumericAttribute,
110 TextAttribute
111 } Type;
112 unsigned Tag;
113 unsigned IntValue;
114 StringRef StringValue;
115 } AttributeItem;
116
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000117 MCObjectStreamer &Streamer;
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000118 StringRef CurrentVendor;
Renato Golin719927a2011-08-09 09:50:10 +0000119 SmallVector<AttributeItemType, 64> Contents;
120
121 // Account for the ULEB/String size of each item,
122 // not just the number of items
123 size_t ContentsSize;
124 // FIXME: this should be in a more generic place, but
125 // getULEBSize() is in MCAsmInfo and will be moved to MCDwarf
126 size_t getULEBSize(int Value) {
127 size_t Size = 0;
128 do {
129 Value >>= 7;
130 Size += sizeof(int8_t); // Is this really necessary?
131 } while (Value);
132 return Size;
133 }
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000134
135 public:
136 ObjectAttributeEmitter(MCObjectStreamer &Streamer_) :
Renato Golin719927a2011-08-09 09:50:10 +0000137 Streamer(Streamer_), CurrentVendor(""), ContentsSize(0) { }
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000138
139 void MaybeSwitchVendor(StringRef Vendor) {
140 assert(!Vendor.empty() && "Vendor cannot be empty.");
141
142 if (CurrentVendor.empty())
143 CurrentVendor = Vendor;
144 else if (CurrentVendor == Vendor)
145 return;
146 else
147 Finish();
148
149 CurrentVendor = Vendor;
150
Rafael Espindola33363842010-10-25 22:26:55 +0000151 assert(Contents.size() == 0);
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000152 }
153
154 void EmitAttribute(unsigned Attribute, unsigned Value) {
Renato Golin719927a2011-08-09 09:50:10 +0000155 AttributeItemType attr = {
156 AttributeItemType::NumericAttribute,
157 Attribute,
158 Value,
159 StringRef("")
160 };
161 ContentsSize += getULEBSize(Attribute);
162 ContentsSize += getULEBSize(Value);
163 Contents.push_back(attr);
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000164 }
165
Jason W Kimf009a962011-02-07 00:49:53 +0000166 void EmitTextAttribute(unsigned Attribute, StringRef String) {
Renato Golin719927a2011-08-09 09:50:10 +0000167 AttributeItemType attr = {
168 AttributeItemType::TextAttribute,
169 Attribute,
170 0,
171 String
172 };
173 ContentsSize += getULEBSize(Attribute);
174 // String + \0
175 ContentsSize += String.size()+1;
176
177 Contents.push_back(attr);
Jason W Kimf009a962011-02-07 00:49:53 +0000178 }
179
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000180 void Finish() {
Rafael Espindola33363842010-10-25 22:26:55 +0000181 // Vendor size + Vendor name + '\0'
182 const size_t VendorHeaderSize = 4 + CurrentVendor.size() + 1;
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000183
Rafael Espindola33363842010-10-25 22:26:55 +0000184 // Tag + Tag Size
185 const size_t TagHeaderSize = 1 + 4;
186
187 Streamer.EmitIntValue(VendorHeaderSize + TagHeaderSize + ContentsSize, 4);
188 Streamer.EmitBytes(CurrentVendor, 0);
189 Streamer.EmitIntValue(0, 1); // '\0'
190
191 Streamer.EmitIntValue(ARMBuildAttrs::File, 1);
192 Streamer.EmitIntValue(TagHeaderSize + ContentsSize, 4);
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000193
Renato Golin719927a2011-08-09 09:50:10 +0000194 // Size should have been accounted for already, now
195 // emit each field as its type (ULEB or String)
196 for (unsigned int i=0; i<Contents.size(); ++i) {
197 AttributeItemType item = Contents[i];
198 Streamer.EmitULEB128IntValue(item.Tag, 0);
199 switch (item.Type) {
200 case AttributeItemType::NumericAttribute:
201 Streamer.EmitULEB128IntValue(item.IntValue, 0);
202 break;
203 case AttributeItemType::TextAttribute:
204 Streamer.EmitBytes(UppercaseString(item.StringValue), 0);
205 Streamer.EmitIntValue(0, 1); // '\0'
206 break;
207 default:
208 assert(0 && "Invalid attribute type");
209 }
210 }
Rafael Espindola33363842010-10-25 22:26:55 +0000211
212 Contents.clear();
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000213 }
214 };
215
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000216} // end of anonymous namespace
217
Jim Grosbachbaf120f2010-12-01 03:45:07 +0000218MachineLocation ARMAsmPrinter::
219getDebugValueLocation(const MachineInstr *MI) const {
220 MachineLocation Location;
221 assert(MI->getNumOperands() == 4 && "Invalid no. of machine operands!");
222 // Frame address. Currently handles register +- offset only.
223 if (MI->getOperand(0).isReg() && MI->getOperand(1).isImm())
224 Location.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
225 else {
226 DEBUG(dbgs() << "DBG_VALUE instruction ignored! " << *MI << "\n");
227 }
228 return Location;
229}
230
Devang Patel27f5acb2011-04-21 22:48:26 +0000231/// EmitDwarfRegOp - Emit dwarf register operation.
Devang Patel0be77df2011-04-27 20:29:27 +0000232void ARMAsmPrinter::EmitDwarfRegOp(const MachineLocation &MLoc) const {
Devang Patel27f5acb2011-04-21 22:48:26 +0000233 const TargetRegisterInfo *RI = TM.getRegisterInfo();
234 if (RI->getDwarfRegNum(MLoc.getReg(), false) != -1)
Devang Patel0be77df2011-04-27 20:29:27 +0000235 AsmPrinter::EmitDwarfRegOp(MLoc);
Devang Patel27f5acb2011-04-21 22:48:26 +0000236 else {
237 unsigned Reg = MLoc.getReg();
238 if (Reg >= ARM::S0 && Reg <= ARM::S31) {
Devang Patel0a6ea832011-04-22 16:44:29 +0000239 assert(ARM::S0 + 31 == ARM::S31 && "Unexpected ARM S register numbering");
Devang Patel27f5acb2011-04-21 22:48:26 +0000240 // S registers are described as bit-pieces of a register
241 // S[2x] = DW_OP_regx(256 + (x>>1)) DW_OP_bit_piece(32, 0)
242 // S[2x+1] = DW_OP_regx(256 + (x>>1)) DW_OP_bit_piece(32, 32)
Jim Grosbach8e0c7692011-09-02 18:46:15 +0000243
Devang Patel27f5acb2011-04-21 22:48:26 +0000244 unsigned SReg = Reg - ARM::S0;
245 bool odd = SReg & 0x1;
246 unsigned Rx = 256 + (SReg >> 1);
Devang Patel27f5acb2011-04-21 22:48:26 +0000247
248 OutStreamer.AddComment("DW_OP_regx for S register");
249 EmitInt8(dwarf::DW_OP_regx);
250
251 OutStreamer.AddComment(Twine(SReg));
252 EmitULEB128(Rx);
253
254 if (odd) {
255 OutStreamer.AddComment("DW_OP_bit_piece 32 32");
256 EmitInt8(dwarf::DW_OP_bit_piece);
257 EmitULEB128(32);
258 EmitULEB128(32);
259 } else {
260 OutStreamer.AddComment("DW_OP_bit_piece 32 0");
261 EmitInt8(dwarf::DW_OP_bit_piece);
262 EmitULEB128(32);
263 EmitULEB128(0);
264 }
Devang Patel71f3f112011-04-21 23:22:35 +0000265 } else if (Reg >= ARM::Q0 && Reg <= ARM::Q15) {
Devang Patel0a6ea832011-04-22 16:44:29 +0000266 assert(ARM::Q0 + 15 == ARM::Q15 && "Unexpected ARM Q register numbering");
Devang Patel71f3f112011-04-21 23:22:35 +0000267 // Q registers Q0-Q15 are described by composing two D registers together.
Jim Grosbach8e0c7692011-09-02 18:46:15 +0000268 // Qx = DW_OP_regx(256+2x) DW_OP_piece(8) DW_OP_regx(256+2x+1)
269 // DW_OP_piece(8)
Devang Patel71f3f112011-04-21 23:22:35 +0000270
271 unsigned QReg = Reg - ARM::Q0;
272 unsigned D1 = 256 + 2 * QReg;
273 unsigned D2 = D1 + 1;
Jim Grosbach8e0c7692011-09-02 18:46:15 +0000274
Devang Patel71f3f112011-04-21 23:22:35 +0000275 OutStreamer.AddComment("DW_OP_regx for Q register: D1");
276 EmitInt8(dwarf::DW_OP_regx);
277 EmitULEB128(D1);
278 OutStreamer.AddComment("DW_OP_piece 8");
279 EmitInt8(dwarf::DW_OP_piece);
280 EmitULEB128(8);
281
282 OutStreamer.AddComment("DW_OP_regx for Q register: D2");
283 EmitInt8(dwarf::DW_OP_regx);
284 EmitULEB128(D2);
285 OutStreamer.AddComment("DW_OP_piece 8");
286 EmitInt8(dwarf::DW_OP_piece);
287 EmitULEB128(8);
Devang Patel27f5acb2011-04-21 22:48:26 +0000288 }
289 }
290}
291
Chris Lattner953ebb72010-01-27 23:58:11 +0000292void ARMAsmPrinter::EmitFunctionEntryLabel() {
293 if (AFI->isThumbFunction()) {
Jim Grosbachce792992010-11-05 22:08:08 +0000294 OutStreamer.EmitAssemblerFlag(MCAF_Code16);
Rafael Espindola64695402011-05-16 16:17:21 +0000295 OutStreamer.EmitThumbFunc(CurrentFnSym);
Chris Lattner953ebb72010-01-27 23:58:11 +0000296 }
Jim Grosbachb0739b72010-09-02 01:02:06 +0000297
Chris Lattner953ebb72010-01-27 23:58:11 +0000298 OutStreamer.EmitLabel(CurrentFnSym);
299}
300
Jim Grosbach2317e402010-09-30 01:57:53 +0000301/// runOnMachineFunction - This uses the EmitInstruction()
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000302/// method to print assembly for each instruction.
303///
304bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Evan Chenga8e29892007-01-19 07:51:42 +0000305 AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng6d63a722008-09-18 07:27:23 +0000306 MCP = MF.getConstantPool();
Rafael Espindola4b442b52006-05-23 02:48:20 +0000307
Chris Lattnerd49fe1b2010-01-28 01:28:58 +0000308 return AsmPrinter::runOnMachineFunction(MF);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000309}
310
Evan Cheng055b0312009-06-29 07:51:04 +0000311void ARMAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
Chris Lattner35c33bd2010-04-04 04:47:45 +0000312 raw_ostream &O, const char *Modifier) {
Evan Cheng055b0312009-06-29 07:51:04 +0000313 const MachineOperand &MO = MI->getOperand(OpNum);
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000314 unsigned TF = MO.getTargetFlags();
315
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000316 switch (MO.getType()) {
Chris Lattner8bc86cb2009-10-19 20:59:55 +0000317 default:
318 assert(0 && "<unknown operand type>");
Bob Wilson5bafff32009-06-22 23:27:02 +0000319 case MachineOperand::MO_Register: {
320 unsigned Reg = MO.getReg();
Chris Lattner8bc86cb2009-10-19 20:59:55 +0000321 assert(TargetRegisterInfo::isPhysicalRegister(Reg));
Jim Grosbach35636282010-10-06 21:22:32 +0000322 assert(!MO.getSubReg() && "Subregs should be eliminated!");
323 O << ARMInstPrinter::getRegisterName(Reg);
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000324 break;
Bob Wilson5bafff32009-06-22 23:27:02 +0000325 }
Evan Chenga8e29892007-01-19 07:51:42 +0000326 case MachineOperand::MO_Immediate: {
Evan Cheng5adb66a2009-09-28 09:14:39 +0000327 int64_t Imm = MO.getImm();
Anton Korobeynikov632606c2009-10-08 20:43:22 +0000328 O << '#';
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000329 if ((Modifier && strcmp(Modifier, "lo16") == 0) ||
Jason W Kim650b7d72011-01-12 23:21:49 +0000330 (TF == ARMII::MO_LO16))
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000331 O << ":lower16:";
332 else if ((Modifier && strcmp(Modifier, "hi16") == 0) ||
Jason W Kim650b7d72011-01-12 23:21:49 +0000333 (TF == ARMII::MO_HI16))
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000334 O << ":upper16:";
Anton Korobeynikov632606c2009-10-08 20:43:22 +0000335 O << Imm;
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000336 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000337 }
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000338 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner1b2eb0e2010-03-13 21:04:28 +0000339 O << *MO.getMBB()->getSymbol();
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000340 return;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000341 case MachineOperand::MO_GlobalAddress: {
Dan Gohman46510a72010-04-15 01:51:59 +0000342 const GlobalValue *GV = MO.getGlobal();
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000343 if ((Modifier && strcmp(Modifier, "lo16") == 0) ||
344 (TF & ARMII::MO_LO16))
345 O << ":lower16:";
346 else if ((Modifier && strcmp(Modifier, "hi16") == 0) ||
347 (TF & ARMII::MO_HI16))
348 O << ":upper16:";
Chris Lattnerd62f1b42010-03-12 21:19:23 +0000349 O << *Mang->getSymbol(GV);
Anton Korobeynikov7751ad92008-11-22 16:15:34 +0000350
Chris Lattner0c08d092010-04-03 22:28:33 +0000351 printOffset(MO.getOffset(), O);
Jim Grosbach1d6111c2010-10-06 21:36:43 +0000352 if (TF == ARMII::MO_PLT)
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000353 O << "(PLT)";
Evan Chenga8e29892007-01-19 07:51:42 +0000354 break;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000355 }
Evan Chenga8e29892007-01-19 07:51:42 +0000356 case MachineOperand::MO_ExternalSymbol: {
Chris Lattner10b318b2010-01-17 21:43:43 +0000357 O << *GetExternalSymbolSymbol(MO.getSymbolName());
Jim Grosbach1d6111c2010-10-06 21:36:43 +0000358 if (TF == ARMII::MO_PLT)
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000359 O << "(PLT)";
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000360 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000361 }
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000362 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner1b46f432010-01-23 07:00:21 +0000363 O << *GetCPISymbol(MO.getIndex());
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000364 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000365 case MachineOperand::MO_JumpTableIndex:
Chris Lattner1b46f432010-01-23 07:00:21 +0000366 O << *GetJTISymbol(MO.getIndex());
Evan Chenga8e29892007-01-19 07:51:42 +0000367 break;
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000368 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000369}
370
Evan Cheng055b0312009-06-29 07:51:04 +0000371//===--------------------------------------------------------------------===//
372
Chris Lattner0890cf12010-01-25 19:51:38 +0000373MCSymbol *ARMAsmPrinter::
374GetARMSetPICJumpTableLabel2(unsigned uid, unsigned uid2,
375 const MachineBasicBlock *MBB) const {
376 SmallString<60> Name;
377 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix()
Chris Lattnerbfcb0962010-01-25 19:39:52 +0000378 << getFunctionNumber() << '_' << uid << '_' << uid2
Chris Lattner0890cf12010-01-25 19:51:38 +0000379 << "_set_" << MBB->getNumber();
Chris Lattner9b97a732010-03-30 18:10:53 +0000380 return OutContext.GetOrCreateSymbol(Name.str());
Chris Lattner0890cf12010-01-25 19:51:38 +0000381}
382
383MCSymbol *ARMAsmPrinter::
384GetARMJTIPICJumpTableLabel2(unsigned uid, unsigned uid2) const {
385 SmallString<60> Name;
386 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "JTI"
Chris Lattner281e7762010-01-25 23:28:03 +0000387 << getFunctionNumber() << '_' << uid << '_' << uid2;
Chris Lattner9b97a732010-03-30 18:10:53 +0000388 return OutContext.GetOrCreateSymbol(Name.str());
Chris Lattnerbfcb0962010-01-25 19:39:52 +0000389}
390
Jim Grosbach433a5782010-09-24 20:47:58 +0000391
392MCSymbol *ARMAsmPrinter::GetARMSJLJEHLabel(void) const {
393 SmallString<60> Name;
394 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "SJLJEH"
395 << getFunctionNumber();
396 return OutContext.GetOrCreateSymbol(Name.str());
397}
398
Evan Cheng055b0312009-06-29 07:51:04 +0000399bool ARMAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
Chris Lattnerc75c0282010-04-04 05:29:35 +0000400 unsigned AsmVariant, const char *ExtraCode,
401 raw_ostream &O) {
Evan Chenga8e29892007-01-19 07:51:42 +0000402 // Does this asm operand have a single letter operand modifier?
403 if (ExtraCode && ExtraCode[0]) {
404 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov8e9ece72009-08-08 23:10:41 +0000405
Evan Chenga8e29892007-01-19 07:51:42 +0000406 switch (ExtraCode[0]) {
407 default: return true; // Unknown modifier.
Bob Wilson9b4b00a2009-07-09 23:54:51 +0000408 case 'a': // Print as a memory address.
409 if (MI->getOperand(OpNum).isReg()) {
Jim Grosbach2f24c4e2010-09-30 15:25:22 +0000410 O << "["
411 << ARMInstPrinter::getRegisterName(MI->getOperand(OpNum).getReg())
412 << "]";
Bob Wilson9b4b00a2009-07-09 23:54:51 +0000413 return false;
414 }
415 // Fallthrough
416 case 'c': // Don't print "#" before an immediate operand.
Bob Wilson4f38b382009-08-21 21:58:55 +0000417 if (!MI->getOperand(OpNum).isImm())
418 return true;
Jim Grosbach2317e402010-09-30 01:57:53 +0000419 O << MI->getOperand(OpNum).getImm();
Bob Wilson8f343462009-04-06 21:46:51 +0000420 return false;
Evan Chenge21e3962007-04-04 00:13:29 +0000421 case 'P': // Print a VFP double precision register.
Evan Chengd831cda2009-12-08 23:06:22 +0000422 case 'q': // Print a NEON quad precision register.
Chris Lattner35c33bd2010-04-04 04:47:45 +0000423 printOperand(MI, OpNum, O);
Evan Cheng23a95702007-03-08 22:42:46 +0000424 return false;
Eric Christopher0628d382011-05-24 22:10:34 +0000425 case 'y': // Print a VFP single precision register as indexed double.
426 // This uses the ordering of the alias table to get the first 'd' register
427 // that overlaps the 's' register. Also, s0 is an odd register, hence the
428 // odd modulus check below.
429 if (MI->getOperand(OpNum).isReg()) {
430 unsigned Reg = MI->getOperand(OpNum).getReg();
431 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
432 O << ARMInstPrinter::getRegisterName(TRI->getAliasSet(Reg)[0]) <<
433 (((Reg % 2) == 1) ? "[0]" : "[1]");
434 return false;
435 }
Eric Christopher4db7dec2011-05-24 23:27:13 +0000436 return true;
Eric Christopherfef50062011-05-24 22:27:43 +0000437 case 'B': // Bitwise inverse of integer or symbol without a preceding #.
Eric Christophere1739d52011-05-24 23:15:43 +0000438 if (!MI->getOperand(OpNum).isImm())
439 return true;
440 O << ~(MI->getOperand(OpNum).getImm());
441 return false;
Eric Christopherfef50062011-05-24 22:27:43 +0000442 case 'L': // The low 16 bits of an immediate constant.
Eric Christopher4db7dec2011-05-24 23:27:13 +0000443 if (!MI->getOperand(OpNum).isImm())
444 return true;
445 O << (MI->getOperand(OpNum).getImm() & 0xffff);
446 return false;
Eric Christopher3c14f242011-05-28 01:40:44 +0000447 case 'M': { // A register range suitable for LDM/STM.
448 if (!MI->getOperand(OpNum).isReg())
449 return true;
450 const MachineOperand &MO = MI->getOperand(OpNum);
451 unsigned RegBegin = MO.getReg();
452 // This takes advantage of the 2 operand-ness of ldm/stm and that we've
453 // already got the operands in registers that are operands to the
454 // inline asm statement.
Jim Grosbach8e0c7692011-09-02 18:46:15 +0000455
Eric Christopher3c14f242011-05-28 01:40:44 +0000456 O << "{" << ARMInstPrinter::getRegisterName(RegBegin);
Jim Grosbach8e0c7692011-09-02 18:46:15 +0000457
Eric Christopher3c14f242011-05-28 01:40:44 +0000458 // FIXME: The register allocator not only may not have given us the
459 // registers in sequence, but may not be in ascending registers. This
460 // will require changes in the register allocator that'll need to be
461 // propagated down here if the operands change.
462 unsigned RegOps = OpNum + 1;
463 while (MI->getOperand(RegOps).isReg()) {
Jim Grosbach8e0c7692011-09-02 18:46:15 +0000464 O << ", "
Eric Christopher3c14f242011-05-28 01:40:44 +0000465 << ARMInstPrinter::getRegisterName(MI->getOperand(RegOps).getReg());
466 RegOps++;
467 }
468
469 O << "}";
470
471 return false;
472 }
Rafael Espindolaf5ade5d2011-08-10 16:26:42 +0000473 case 'R': // The most significant register of a pair.
474 case 'Q': { // The least significant register of a pair.
475 if (OpNum == 0)
476 return true;
477 const MachineOperand &FlagsOP = MI->getOperand(OpNum - 1);
478 if (!FlagsOP.isImm())
479 return true;
480 unsigned Flags = FlagsOP.getImm();
481 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
482 if (NumVals != 2)
483 return true;
484 unsigned RegOp = ExtraCode[0] == 'Q' ? OpNum : OpNum + 1;
485 if (RegOp >= MI->getNumOperands())
486 return true;
487 const MachineOperand &MO = MI->getOperand(RegOp);
488 if (!MO.isReg())
489 return true;
490 unsigned Reg = MO.getReg();
491 O << ARMInstPrinter::getRegisterName(Reg);
492 return false;
493 }
494
Eric Christopher3c14f242011-05-28 01:40:44 +0000495 // These modifiers are not yet supported.
Eric Christopherfef50062011-05-24 22:27:43 +0000496 case 'p': // The high single-precision register of a VFP double-precision
497 // register.
498 case 'e': // The low doubleword register of a NEON quad register.
499 case 'f': // The high doubleword register of a NEON quad register.
500 case 'h': // A range of VFP/NEON registers suitable for VLD1/VST1.
Eric Christopherfef50062011-05-24 22:27:43 +0000501 case 'H': // The highest-numbered register of a pair.
Bob Wilsond984eb62010-05-27 20:23:42 +0000502 return true;
Evan Cheng84f60b72010-05-27 22:08:38 +0000503 }
Evan Chenga8e29892007-01-19 07:51:42 +0000504 }
Jim Grosbache9952212009-09-04 01:38:51 +0000505
Chris Lattner35c33bd2010-04-04 04:47:45 +0000506 printOperand(MI, OpNum, O);
Evan Chenga8e29892007-01-19 07:51:42 +0000507 return false;
508}
509
Bob Wilson224c2442009-05-19 05:53:42 +0000510bool ARMAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Evan Cheng055b0312009-06-29 07:51:04 +0000511 unsigned OpNum, unsigned AsmVariant,
Chris Lattnerc75c0282010-04-04 05:29:35 +0000512 const char *ExtraCode,
513 raw_ostream &O) {
Eric Christopher8f894632011-05-25 20:51:58 +0000514 // Does this asm operand have a single letter operand modifier?
515 if (ExtraCode && ExtraCode[0]) {
516 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Jim Grosbach8e0c7692011-09-02 18:46:15 +0000517
Eric Christopher8f894632011-05-25 20:51:58 +0000518 switch (ExtraCode[0]) {
Eric Christopher32bfb2c2011-05-26 18:22:26 +0000519 case 'A': // A memory operand for a VLD1/VST1 instruction.
Eric Christopher8f894632011-05-25 20:51:58 +0000520 default: return true; // Unknown modifier.
521 case 'm': // The base register of a memory operand.
522 if (!MI->getOperand(OpNum).isReg())
523 return true;
524 O << ARMInstPrinter::getRegisterName(MI->getOperand(OpNum).getReg());
525 return false;
526 }
527 }
Jim Grosbach8e0c7692011-09-02 18:46:15 +0000528
Bob Wilson765cc0b2009-10-13 20:50:28 +0000529 const MachineOperand &MO = MI->getOperand(OpNum);
530 assert(MO.isReg() && "unexpected inline asm memory operand");
Jim Grosbach2317e402010-09-30 01:57:53 +0000531 O << "[" << ARMInstPrinter::getRegisterName(MO.getReg()) << "]";
Bob Wilson224c2442009-05-19 05:53:42 +0000532 return false;
533}
534
Bob Wilson812209a2009-09-30 22:06:26 +0000535void ARMAsmPrinter::EmitStartOfAsmFile(Module &M) {
Bob Wilson0fb34682009-09-30 00:23:42 +0000536 if (Subtarget->isTargetDarwin()) {
537 Reloc::Model RelocM = TM.getRelocationModel();
538 if (RelocM == Reloc::PIC_ || RelocM == Reloc::DynamicNoPIC) {
539 // Declare all the text sections up front (before the DWARF sections
540 // emitted by AsmPrinter::doInitialization) so the assembler will keep
541 // them together at the beginning of the object file. This helps
542 // avoid out-of-range branches that are due a fundamental limitation of
543 // the way symbol offsets are encoded with the current Darwin ARM
544 // relocations.
Jim Grosbachb0739b72010-09-02 01:02:06 +0000545 const TargetLoweringObjectFileMachO &TLOFMacho =
Dan Gohman0d805c32010-04-17 16:44:48 +0000546 static_cast<const TargetLoweringObjectFileMachO &>(
547 getObjFileLowering());
Bob Wilson29e06692009-09-30 22:25:37 +0000548 OutStreamer.SwitchSection(TLOFMacho.getTextSection());
549 OutStreamer.SwitchSection(TLOFMacho.getTextCoalSection());
550 OutStreamer.SwitchSection(TLOFMacho.getConstTextCoalSection());
551 if (RelocM == Reloc::DynamicNoPIC) {
552 const MCSection *sect =
Chris Lattner22772212010-04-08 20:40:11 +0000553 OutContext.getMachOSection("__TEXT", "__symbol_stub4",
554 MCSectionMachO::S_SYMBOL_STUBS,
555 12, SectionKind::getText());
Bob Wilson29e06692009-09-30 22:25:37 +0000556 OutStreamer.SwitchSection(sect);
557 } else {
558 const MCSection *sect =
Chris Lattner22772212010-04-08 20:40:11 +0000559 OutContext.getMachOSection("__TEXT", "__picsymbolstub4",
560 MCSectionMachO::S_SYMBOL_STUBS,
561 16, SectionKind::getText());
Bob Wilson29e06692009-09-30 22:25:37 +0000562 OutStreamer.SwitchSection(sect);
563 }
Bob Wilson63db5942010-07-30 19:55:47 +0000564 const MCSection *StaticInitSect =
565 OutContext.getMachOSection("__TEXT", "__StaticInit",
566 MCSectionMachO::S_REGULAR |
567 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
568 SectionKind::getText());
569 OutStreamer.SwitchSection(StaticInitSect);
Bob Wilson0fb34682009-09-30 00:23:42 +0000570 }
571 }
572
Jim Grosbache5165492009-11-09 00:11:35 +0000573 // Use unified assembler syntax.
Jason W Kimafd1cc22010-09-30 02:45:56 +0000574 OutStreamer.EmitAssemblerFlag(MCAF_SyntaxUnified);
Anton Korobeynikovd61eca52009-06-17 23:43:18 +0000575
Anton Korobeynikov88ce6672009-05-23 19:51:20 +0000576 // Emit ARM Build Attributes
577 if (Subtarget->isTargetELF()) {
Anton Korobeynikov88ce6672009-05-23 19:51:20 +0000578
Jason W Kimdef9ac42010-10-06 22:36:46 +0000579 emitAttributes();
Anton Korobeynikov88ce6672009-05-23 19:51:20 +0000580 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000581}
582
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +0000583
Chris Lattner4a071d62009-10-19 17:59:19 +0000584void ARMAsmPrinter::EmitEndOfAsmFile(Module &M) {
Evan Cheng5be54b02007-01-19 19:25:36 +0000585 if (Subtarget->isTargetDarwin()) {
Chris Lattnerf61159b2009-08-03 22:18:15 +0000586 // All darwin targets use mach-o.
Dan Gohman0d805c32010-04-17 16:44:48 +0000587 const TargetLoweringObjectFileMachO &TLOFMacho =
588 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattnerb0f294c2009-10-19 18:38:33 +0000589 MachineModuleInfoMachO &MMIMacho =
590 MMI->getObjFileInfo<MachineModuleInfoMachO>();
Jim Grosbache9952212009-09-04 01:38:51 +0000591
Evan Chenga8e29892007-01-19 07:51:42 +0000592 // Output non-lazy-pointers for external and common global variables.
Chris Lattnerb0f294c2009-10-19 18:38:33 +0000593 MachineModuleInfoMachO::SymbolListTy Stubs = MMIMacho.GetGVStubList();
Bill Wendlingcebae362010-03-10 22:34:10 +0000594
Chris Lattnerb0f294c2009-10-19 18:38:33 +0000595 if (!Stubs.empty()) {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000596 // Switch with ".non_lazy_symbol_pointer" directive.
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000597 OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
Chris Lattnerc076a972009-08-10 18:01:34 +0000598 EmitAlignment(2);
Chris Lattnerb0f294c2009-10-19 18:38:33 +0000599 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Bill Wendlingbecd83e2010-03-09 00:40:17 +0000600 // L_foo$stub:
601 OutStreamer.EmitLabel(Stubs[i].first);
602 // .indirect_symbol _foo
Bill Wendling52a50e52010-03-11 01:18:13 +0000603 MachineModuleInfoImpl::StubValueTy &MCSym = Stubs[i].second;
604 OutStreamer.EmitSymbolAttribute(MCSym.getPointer(),MCSA_IndirectSymbol);
Bill Wendlingcf6f28d2010-03-09 00:43:34 +0000605
Bill Wendling52a50e52010-03-11 01:18:13 +0000606 if (MCSym.getInt())
Bill Wendlingcf6f28d2010-03-09 00:43:34 +0000607 // External to current translation unit.
608 OutStreamer.EmitIntValue(0, 4/*size*/, 0/*addrspace*/);
609 else
610 // Internal to current translation unit.
Bill Wendling5e1b55d2010-03-31 18:47:10 +0000611 //
Jim Grosbach1b935a32010-09-22 16:45:13 +0000612 // When we place the LSDA into the TEXT section, the type info
613 // pointers need to be indirect and pc-rel. We accomplish this by
614 // using NLPs; however, sometimes the types are local to the file.
615 // We need to fill in the value for the NLP in those cases.
Bill Wendling52a50e52010-03-11 01:18:13 +0000616 OutStreamer.EmitValue(MCSymbolRefExpr::Create(MCSym.getPointer(),
617 OutContext),
Bill Wendlingcf6f28d2010-03-09 00:43:34 +0000618 4/*size*/, 0/*addrspace*/);
Evan Chengae94e592008-12-05 01:06:39 +0000619 }
Bill Wendlingbecd83e2010-03-09 00:40:17 +0000620
621 Stubs.clear();
622 OutStreamer.AddBlankLine();
Evan Chenga8e29892007-01-19 07:51:42 +0000623 }
624
Chris Lattnere4d9ea82009-10-19 18:44:38 +0000625 Stubs = MMIMacho.GetHiddenGVStubList();
626 if (!Stubs.empty()) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000627 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
Chris Lattnerf3231de2009-08-10 18:02:16 +0000628 EmitAlignment(2);
Bill Wendlingbecd83e2010-03-09 00:40:17 +0000629 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
630 // L_foo$stub:
631 OutStreamer.EmitLabel(Stubs[i].first);
632 // .long _foo
Bill Wendlingcebae362010-03-10 22:34:10 +0000633 OutStreamer.EmitValue(MCSymbolRefExpr::
634 Create(Stubs[i].second.getPointer(),
635 OutContext),
Bill Wendlingbecd83e2010-03-09 00:40:17 +0000636 4/*size*/, 0/*addrspace*/);
637 }
Bill Wendlingcf6f28d2010-03-09 00:43:34 +0000638
639 Stubs.clear();
640 OutStreamer.AddBlankLine();
Evan Chengae94e592008-12-05 01:06:39 +0000641 }
642
Evan Chenga8e29892007-01-19 07:51:42 +0000643 // Funny Darwin hack: This flag tells the linker that no global symbols
644 // contain code that falls through to other global symbols (e.g. the obvious
645 // implementation of multiple entry points). If this doesn't occur, the
646 // linker can safely perform dead code stripping. Since LLVM never
647 // generates code that does this, it is always safe to set.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000648 OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000649 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000650}
Anton Korobeynikov0bd89712008-08-17 13:55:10 +0000651
Chris Lattner97f06932009-10-19 20:20:46 +0000652//===----------------------------------------------------------------------===//
Jason W Kimdef9ac42010-10-06 22:36:46 +0000653// Helper routines for EmitStartOfAsmFile() and EmitEndOfAsmFile()
654// FIXME:
655// The following seem like one-off assembler flags, but they actually need
Jim Grosbachfa7fb642010-10-06 22:46:47 +0000656// to appear in the .ARM.attributes section in ELF.
Jason W Kimdef9ac42010-10-06 22:36:46 +0000657// Instead of subclassing the MCELFStreamer, we do the work here.
658
659void ARMAsmPrinter::emitAttributes() {
Jim Grosbachfa7fb642010-10-06 22:46:47 +0000660
Jason W Kim17b443d2010-10-11 23:01:44 +0000661 emitARMAttributeSection();
662
Renato Golin728ff0d2011-02-28 22:04:27 +0000663 /* GAS expect .fpu to be emitted, regardless of VFP build attribute */
664 bool emitFPU = false;
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000665 AttributeEmitter *AttrEmitter;
Renato Golin728ff0d2011-02-28 22:04:27 +0000666 if (OutStreamer.hasRawTextSupport()) {
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000667 AttrEmitter = new AsmAttributeEmitter(OutStreamer);
Renato Golin728ff0d2011-02-28 22:04:27 +0000668 emitFPU = true;
669 } else {
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000670 MCObjectStreamer &O = static_cast<MCObjectStreamer&>(OutStreamer);
671 AttrEmitter = new ObjectAttributeEmitter(O);
672 }
673
674 AttrEmitter->MaybeSwitchVendor("aeabi");
675
Jason W Kimdef9ac42010-10-06 22:36:46 +0000676 std::string CPUString = Subtarget->getCPUString();
Jason W Kimf009a962011-02-07 00:49:53 +0000677
678 if (CPUString == "cortex-a8" ||
679 Subtarget->isCortexA8()) {
Jason W Kimc046d642011-02-07 19:07:11 +0000680 AttrEmitter->EmitTextAttribute(ARMBuildAttrs::CPU_name, "cortex-a8");
Jason W Kimf009a962011-02-07 00:49:53 +0000681 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v7);
682 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch_profile,
683 ARMBuildAttrs::ApplicationProfile);
684 AttrEmitter->EmitAttribute(ARMBuildAttrs::ARM_ISA_use,
685 ARMBuildAttrs::Allowed);
686 AttrEmitter->EmitAttribute(ARMBuildAttrs::THUMB_ISA_use,
687 ARMBuildAttrs::AllowThumb32);
688 // Fixme: figure out when this is emitted.
689 //AttrEmitter->EmitAttribute(ARMBuildAttrs::WMMX_arch,
690 // ARMBuildAttrs::AllowWMMXv1);
691 //
692
693 /// ADD additional Else-cases here!
Rafael Espindolab8adb8a2011-05-20 20:10:34 +0000694 } else if (CPUString == "xscale") {
695 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v5TEJ);
696 AttrEmitter->EmitAttribute(ARMBuildAttrs::ARM_ISA_use,
697 ARMBuildAttrs::Allowed);
698 AttrEmitter->EmitAttribute(ARMBuildAttrs::THUMB_ISA_use,
699 ARMBuildAttrs::Allowed);
Jason W Kimf009a962011-02-07 00:49:53 +0000700 } else if (CPUString == "generic") {
Dale Johannesen7179d1e2010-11-08 19:17:22 +0000701 // FIXME: Why these defaults?
702 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v4T);
Jason W Kimf009a962011-02-07 00:49:53 +0000703 AttrEmitter->EmitAttribute(ARMBuildAttrs::ARM_ISA_use,
704 ARMBuildAttrs::Allowed);
705 AttrEmitter->EmitAttribute(ARMBuildAttrs::THUMB_ISA_use,
706 ARMBuildAttrs::Allowed);
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000707 }
Jason W Kimdef9ac42010-10-06 22:36:46 +0000708
Renato Goline89a0532011-03-02 21:20:09 +0000709 if (Subtarget->hasNEON() && emitFPU) {
Renato Golin728ff0d2011-02-28 22:04:27 +0000710 /* NEON is not exactly a VFP architecture, but GAS emit one of
711 * neon/vfpv3/vfpv2 for .fpu parameters */
712 AttrEmitter->EmitTextAttribute(ARMBuildAttrs::Advanced_SIMD_arch, "neon");
713 /* If emitted for NEON, omit from VFP below, since you can have both
714 * NEON and VFP in build attributes but only one .fpu */
715 emitFPU = false;
716 }
717
718 /* VFPv3 + .fpu */
719 if (Subtarget->hasVFP3()) {
720 AttrEmitter->EmitAttribute(ARMBuildAttrs::VFP_arch,
721 ARMBuildAttrs::AllowFPv3A);
722 if (emitFPU)
723 AttrEmitter->EmitTextAttribute(ARMBuildAttrs::VFP_arch, "vfpv3");
724
725 /* VFPv2 + .fpu */
726 } else if (Subtarget->hasVFP2()) {
Jason W Kimf009a962011-02-07 00:49:53 +0000727 AttrEmitter->EmitAttribute(ARMBuildAttrs::VFP_arch,
728 ARMBuildAttrs::AllowFPv2);
Renato Golin728ff0d2011-02-28 22:04:27 +0000729 if (emitFPU)
730 AttrEmitter->EmitTextAttribute(ARMBuildAttrs::VFP_arch, "vfpv2");
731 }
732
733 /* TODO: ARMBuildAttrs::Allowed is not completely accurate,
Cameron Zwarich375db7f2011-07-07 08:28:52 +0000734 * since NEON can have 1 (allowed) or 2 (MAC operations) */
Renato Golin728ff0d2011-02-28 22:04:27 +0000735 if (Subtarget->hasNEON()) {
736 AttrEmitter->EmitAttribute(ARMBuildAttrs::Advanced_SIMD_arch,
737 ARMBuildAttrs::Allowed);
738 }
Jason W Kimdef9ac42010-10-06 22:36:46 +0000739
740 // Signal various FP modes.
741 if (!UnsafeFPMath) {
Jason W Kimf009a962011-02-07 00:49:53 +0000742 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_FP_denormal,
743 ARMBuildAttrs::Allowed);
744 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_FP_exceptions,
745 ARMBuildAttrs::Allowed);
Jason W Kimdef9ac42010-10-06 22:36:46 +0000746 }
747
748 if (NoInfsFPMath && NoNaNsFPMath)
Jason W Kimf009a962011-02-07 00:49:53 +0000749 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_FP_number_model,
750 ARMBuildAttrs::Allowed);
Jason W Kimdef9ac42010-10-06 22:36:46 +0000751 else
Jason W Kimf009a962011-02-07 00:49:53 +0000752 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_FP_number_model,
753 ARMBuildAttrs::AllowIEE754);
Jason W Kimdef9ac42010-10-06 22:36:46 +0000754
Jason W Kimf009a962011-02-07 00:49:53 +0000755 // FIXME: add more flags to ARMBuildAttrs.h
Jason W Kimdef9ac42010-10-06 22:36:46 +0000756 // 8-bytes alignment stuff.
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000757 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_align8_needed, 1);
758 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_align8_preserved, 1);
Jason W Kimdef9ac42010-10-06 22:36:46 +0000759
760 // Hard float. Use both S and D registers and conform to AAPCS-VFP.
761 if (Subtarget->isAAPCS_ABI() && FloatABIType == FloatABI::Hard) {
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000762 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_HardFP_use, 3);
763 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_VFP_args, 1);
Jason W Kimdef9ac42010-10-06 22:36:46 +0000764 }
765 // FIXME: Should we signal R9 usage?
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000766
Jason W Kimf009a962011-02-07 00:49:53 +0000767 if (Subtarget->hasDivide())
768 AttrEmitter->EmitAttribute(ARMBuildAttrs::DIV_use, 1);
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000769
770 AttrEmitter->Finish();
771 delete AttrEmitter;
Jason W Kimdef9ac42010-10-06 22:36:46 +0000772}
773
Jason W Kim17b443d2010-10-11 23:01:44 +0000774void ARMAsmPrinter::emitARMAttributeSection() {
775 // <format-version>
776 // [ <section-length> "vendor-name"
777 // [ <file-tag> <size> <attribute>*
778 // | <section-tag> <size> <section-number>* 0 <attribute>*
779 // | <symbol-tag> <size> <symbol-number>* 0 <attribute>*
780 // ]+
781 // ]*
782
783 if (OutStreamer.hasRawTextSupport())
784 return;
785
786 const ARMElfTargetObjectFile &TLOFELF =
787 static_cast<const ARMElfTargetObjectFile &>
788 (getObjFileLowering());
789
790 OutStreamer.SwitchSection(TLOFELF.getAttributesSection());
Jason W Kim17b443d2010-10-11 23:01:44 +0000791
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000792 // Format version
793 OutStreamer.EmitIntValue(0x41, 1);
Jason W Kim17b443d2010-10-11 23:01:44 +0000794}
795
Jason W Kimdef9ac42010-10-06 22:36:46 +0000796//===----------------------------------------------------------------------===//
Chris Lattner97f06932009-10-19 20:20:46 +0000797
Jim Grosbach988ce092010-09-18 00:05:05 +0000798static MCSymbol *getPICLabel(const char *Prefix, unsigned FunctionNumber,
799 unsigned LabelId, MCContext &Ctx) {
800
801 MCSymbol *Label = Ctx.GetOrCreateSymbol(Twine(Prefix)
802 + "PC" + Twine(FunctionNumber) + "_" + Twine(LabelId));
803 return Label;
804}
805
Jim Grosbach2c4d5122010-11-10 03:26:07 +0000806static MCSymbolRefExpr::VariantKind
807getModifierVariantKind(ARMCP::ARMCPModifier Modifier) {
808 switch (Modifier) {
809 default: llvm_unreachable("Unknown modifier!");
810 case ARMCP::no_modifier: return MCSymbolRefExpr::VK_None;
811 case ARMCP::TLSGD: return MCSymbolRefExpr::VK_ARM_TLSGD;
812 case ARMCP::TPOFF: return MCSymbolRefExpr::VK_ARM_TPOFF;
813 case ARMCP::GOTTPOFF: return MCSymbolRefExpr::VK_ARM_GOTTPOFF;
814 case ARMCP::GOT: return MCSymbolRefExpr::VK_ARM_GOT;
815 case ARMCP::GOTOFF: return MCSymbolRefExpr::VK_ARM_GOTOFF;
816 }
817 return MCSymbolRefExpr::VK_None;
818}
819
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000820MCSymbol *ARMAsmPrinter::GetARMGVSymbol(const GlobalValue *GV) {
821 bool isIndirect = Subtarget->isTargetDarwin() &&
822 Subtarget->GVIsIndirectSymbol(GV, TM.getRelocationModel());
823 if (!isIndirect)
824 return Mang->getSymbol(GV);
825
826 // FIXME: Remove this when Darwin transition to @GOT like syntax.
827 MCSymbol *MCSym = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
828 MachineModuleInfoMachO &MMIMachO =
829 MMI->getObjFileInfo<MachineModuleInfoMachO>();
830 MachineModuleInfoImpl::StubValueTy &StubSym =
831 GV->hasHiddenVisibility() ? MMIMachO.getHiddenGVStubEntry(MCSym) :
832 MMIMachO.getGVStubEntry(MCSym);
833 if (StubSym.getPointer() == 0)
834 StubSym = MachineModuleInfoImpl::
835 StubValueTy(Mang->getSymbol(GV), !GV->hasInternalLinkage());
836 return MCSym;
837}
838
Jim Grosbach5df08d82010-11-09 18:45:04 +0000839void ARMAsmPrinter::
840EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
841 int Size = TM.getTargetData()->getTypeAllocSize(MCPV->getType());
842
843 ARMConstantPoolValue *ACPV = static_cast<ARMConstantPoolValue*>(MCPV);
Jim Grosbach5df08d82010-11-09 18:45:04 +0000844
Jim Grosbach7c7ddb22010-11-10 17:59:10 +0000845 MCSymbol *MCSym;
Jim Grosbach5df08d82010-11-09 18:45:04 +0000846 if (ACPV->isLSDA()) {
Jim Grosbach7c7ddb22010-11-10 17:59:10 +0000847 SmallString<128> Str;
848 raw_svector_ostream OS(Str);
Jim Grosbach5df08d82010-11-09 18:45:04 +0000849 OS << MAI->getPrivateGlobalPrefix() << "_LSDA_" << getFunctionNumber();
Jim Grosbach7c7ddb22010-11-10 17:59:10 +0000850 MCSym = OutContext.GetOrCreateSymbol(OS.str());
Jim Grosbach5df08d82010-11-09 18:45:04 +0000851 } else if (ACPV->isBlockAddress()) {
Bill Wendling5bb77992011-10-01 08:00:54 +0000852 const BlockAddress *BA =
853 cast<ARMConstantPoolConstant>(ACPV)->getBlockAddress();
854 MCSym = GetBlockAddressSymbol(BA);
Jim Grosbach5df08d82010-11-09 18:45:04 +0000855 } else if (ACPV->isGlobalValue()) {
Bill Wendling5bb77992011-10-01 08:00:54 +0000856 const GlobalValue *GV = cast<ARMConstantPoolConstant>(ACPV)->getGV();
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000857 MCSym = GetARMGVSymbol(GV);
Bill Wendlinge00897c2011-09-29 23:50:42 +0000858 } else if (ACPV->isMachineBasicBlock()) {
859 const MachineBasicBlock *MBB = ACPV->getMBB();
860 MCSym = MBB->getSymbol();
Jim Grosbach5df08d82010-11-09 18:45:04 +0000861 } else {
862 assert(ACPV->isExtSymbol() && "unrecognized constant pool value");
Jim Grosbach7c7ddb22010-11-10 17:59:10 +0000863 MCSym = GetExternalSymbolSymbol(ACPV->getSymbol());
Jim Grosbach5df08d82010-11-09 18:45:04 +0000864 }
865
866 // Create an MCSymbol for the reference.
Jim Grosbach2c4d5122010-11-10 03:26:07 +0000867 const MCExpr *Expr =
868 MCSymbolRefExpr::Create(MCSym, getModifierVariantKind(ACPV->getModifier()),
869 OutContext);
Jim Grosbach5df08d82010-11-09 18:45:04 +0000870
Jim Grosbach2c4d5122010-11-10 03:26:07 +0000871 if (ACPV->getPCAdjustment()) {
872 MCSymbol *PCLabel = getPICLabel(MAI->getPrivateGlobalPrefix(),
873 getFunctionNumber(),
874 ACPV->getLabelId(),
875 OutContext);
876 const MCExpr *PCRelExpr = MCSymbolRefExpr::Create(PCLabel, OutContext);
877 PCRelExpr =
878 MCBinaryExpr::CreateAdd(PCRelExpr,
879 MCConstantExpr::Create(ACPV->getPCAdjustment(),
880 OutContext),
881 OutContext);
882 if (ACPV->mustAddCurrentAddress()) {
883 // We want "(<expr> - .)", but MC doesn't have a concept of the '.'
884 // label, so just emit a local label end reference that instead.
885 MCSymbol *DotSym = OutContext.CreateTempSymbol();
886 OutStreamer.EmitLabel(DotSym);
887 const MCExpr *DotExpr = MCSymbolRefExpr::Create(DotSym, OutContext);
888 PCRelExpr = MCBinaryExpr::CreateSub(PCRelExpr, DotExpr, OutContext);
Jim Grosbach5df08d82010-11-09 18:45:04 +0000889 }
Jim Grosbach2c4d5122010-11-10 03:26:07 +0000890 Expr = MCBinaryExpr::CreateSub(Expr, PCRelExpr, OutContext);
Jim Grosbach5df08d82010-11-09 18:45:04 +0000891 }
Jim Grosbach2c4d5122010-11-10 03:26:07 +0000892 OutStreamer.EmitValue(Expr, Size);
Jim Grosbach5df08d82010-11-09 18:45:04 +0000893}
894
Jim Grosbacha2244cb2010-09-22 17:39:48 +0000895void ARMAsmPrinter::EmitJumpTable(const MachineInstr *MI) {
896 unsigned Opcode = MI->getOpcode();
897 int OpNum = 1;
898 if (Opcode == ARM::BR_JTadd)
899 OpNum = 2;
900 else if (Opcode == ARM::BR_JTm)
901 OpNum = 3;
902
903 const MachineOperand &MO1 = MI->getOperand(OpNum);
904 const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
905 unsigned JTI = MO1.getIndex();
906
907 // Emit a label for the jump table.
908 MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel2(JTI, MO2.getImm());
909 OutStreamer.EmitLabel(JTISymbol);
910
911 // Emit each entry of the table.
912 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
913 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
914 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
915
916 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
917 MachineBasicBlock *MBB = JTBBs[i];
918 // Construct an MCExpr for the entry. We want a value of the form:
919 // (BasicBlockAddr - TableBeginAddr)
920 //
921 // For example, a table with entries jumping to basic blocks BB0 and BB1
922 // would look like:
923 // LJTI_0_0:
924 // .word (LBB0 - LJTI_0_0)
925 // .word (LBB1 - LJTI_0_0)
926 const MCExpr *Expr = MCSymbolRefExpr::Create(MBB->getSymbol(), OutContext);
927
928 if (TM.getRelocationModel() == Reloc::PIC_)
929 Expr = MCBinaryExpr::CreateSub(Expr, MCSymbolRefExpr::Create(JTISymbol,
930 OutContext),
931 OutContext);
Jim Grosbachde982732011-08-31 22:23:09 +0000932 // If we're generating a table of Thumb addresses in static relocation
933 // model, we need to add one to keep interworking correctly.
934 else if (AFI->isThumbFunction())
935 Expr = MCBinaryExpr::CreateAdd(Expr, MCConstantExpr::Create(1,OutContext),
936 OutContext);
Jim Grosbacha2244cb2010-09-22 17:39:48 +0000937 OutStreamer.EmitValue(Expr, 4);
938 }
939}
940
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000941void ARMAsmPrinter::EmitJump2Table(const MachineInstr *MI) {
942 unsigned Opcode = MI->getOpcode();
943 int OpNum = (Opcode == ARM::t2BR_JT) ? 2 : 1;
944 const MachineOperand &MO1 = MI->getOperand(OpNum);
945 const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
946 unsigned JTI = MO1.getIndex();
947
948 // Emit a label for the jump table.
949 MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel2(JTI, MO2.getImm());
950 OutStreamer.EmitLabel(JTISymbol);
951
952 // Emit each entry of the table.
953 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
954 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
955 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000956 unsigned OffsetWidth = 4;
Jim Grosbachd092a872010-11-29 21:28:32 +0000957 if (MI->getOpcode() == ARM::t2TBB_JT)
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000958 OffsetWidth = 1;
Jim Grosbachd092a872010-11-29 21:28:32 +0000959 else if (MI->getOpcode() == ARM::t2TBH_JT)
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000960 OffsetWidth = 2;
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000961
962 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
963 MachineBasicBlock *MBB = JTBBs[i];
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000964 const MCExpr *MBBSymbolExpr = MCSymbolRefExpr::Create(MBB->getSymbol(),
965 OutContext);
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000966 // If this isn't a TBB or TBH, the entries are direct branch instructions.
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000967 if (OffsetWidth == 4) {
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000968 MCInst BrInst;
969 BrInst.setOpcode(ARM::t2B);
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000970 BrInst.addOperand(MCOperand::CreateExpr(MBBSymbolExpr));
Owen Anderson51f6a7a2011-09-09 21:48:23 +0000971 BrInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
972 BrInst.addOperand(MCOperand::CreateReg(0));
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000973 OutStreamer.EmitInstruction(BrInst);
974 continue;
975 }
976 // Otherwise it's an offset from the dispatch instruction. Construct an
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000977 // MCExpr for the entry. We want a value of the form:
978 // (BasicBlockAddr - TableBeginAddr) / 2
979 //
980 // For example, a TBB table with entries jumping to basic blocks BB0 and BB1
981 // would look like:
982 // LJTI_0_0:
983 // .byte (LBB0 - LJTI_0_0) / 2
984 // .byte (LBB1 - LJTI_0_0) / 2
985 const MCExpr *Expr =
986 MCBinaryExpr::CreateSub(MBBSymbolExpr,
987 MCSymbolRefExpr::Create(JTISymbol, OutContext),
988 OutContext);
989 Expr = MCBinaryExpr::CreateDiv(Expr, MCConstantExpr::Create(2, OutContext),
990 OutContext);
991 OutStreamer.EmitValue(Expr, OffsetWidth);
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000992 }
993}
994
Jim Grosbach2d0f53b2010-09-28 17:05:56 +0000995void ARMAsmPrinter::PrintDebugValueComment(const MachineInstr *MI,
996 raw_ostream &OS) {
997 unsigned NOps = MI->getNumOperands();
998 assert(NOps==4);
999 OS << '\t' << MAI->getCommentString() << "DEBUG_VALUE: ";
1000 // cast away const; DIetc do not take const operands for some reason.
1001 DIVariable V(const_cast<MDNode *>(MI->getOperand(NOps-1).getMetadata()));
1002 OS << V.getName();
1003 OS << " <- ";
1004 // Frame address. Currently handles register +- offset only.
1005 assert(MI->getOperand(0).isReg() && MI->getOperand(1).isImm());
1006 OS << '['; printOperand(MI, 0, OS); OS << '+'; printOperand(MI, 1, OS);
1007 OS << ']';
1008 OS << "+";
1009 printOperand(MI, NOps-2, OS);
1010}
1011
Jim Grosbach40edf732010-12-14 21:10:47 +00001012static void populateADROperands(MCInst &Inst, unsigned Dest,
1013 const MCSymbol *Label,
1014 unsigned pred, unsigned ccreg,
1015 MCContext &Ctx) {
1016 const MCExpr *SymbolExpr = MCSymbolRefExpr::Create(Label, Ctx);
1017 Inst.addOperand(MCOperand::CreateReg(Dest));
1018 Inst.addOperand(MCOperand::CreateExpr(SymbolExpr));
1019 // Add predicate operands.
1020 Inst.addOperand(MCOperand::CreateImm(pred));
1021 Inst.addOperand(MCOperand::CreateReg(ccreg));
1022}
1023
Anton Korobeynikov4d728602011-01-01 20:38:38 +00001024void ARMAsmPrinter::EmitPatchedInstruction(const MachineInstr *MI,
1025 unsigned Opcode) {
1026 MCInst TmpInst;
1027
1028 // Emit the instruction as usual, just patch the opcode.
1029 LowerARMMachineInstrToMCInst(MI, TmpInst, *this);
1030 TmpInst.setOpcode(Opcode);
1031 OutStreamer.EmitInstruction(TmpInst);
1032}
1033
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001034void ARMAsmPrinter::EmitUnwindingInstruction(const MachineInstr *MI) {
1035 assert(MI->getFlag(MachineInstr::FrameSetup) &&
1036 "Only instruction which are involved into frame setup code are allowed");
1037
1038 const MachineFunction &MF = *MI->getParent()->getParent();
1039 const TargetRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo();
Anton Korobeynikovb3fcc062011-03-05 18:43:55 +00001040 const ARMFunctionInfo &AFI = *MF.getInfo<ARMFunctionInfo>();
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001041
1042 unsigned FramePtr = RegInfo->getFrameRegister(MF);
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001043 unsigned Opc = MI->getOpcode();
Anton Korobeynikov7a764162011-03-05 18:43:43 +00001044 unsigned SrcReg, DstReg;
1045
Anton Korobeynikov3daccd82011-03-05 18:43:50 +00001046 if (Opc == ARM::tPUSH || Opc == ARM::tLDRpci) {
1047 // Two special cases:
1048 // 1) tPUSH does not have src/dst regs.
1049 // 2) for Thumb1 code we sometimes materialize the constant via constpool
1050 // load. Yes, this is pretty fragile, but for now I don't see better
1051 // way... :(
Anton Korobeynikov7a764162011-03-05 18:43:43 +00001052 SrcReg = DstReg = ARM::SP;
1053 } else {
Anton Korobeynikov3daccd82011-03-05 18:43:50 +00001054 SrcReg = MI->getOperand(1).getReg();
Anton Korobeynikov7a764162011-03-05 18:43:43 +00001055 DstReg = MI->getOperand(0).getReg();
1056 }
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001057
1058 // Try to figure out the unwinding opcode out of src / dst regs.
1059 if (MI->getDesc().mayStore()) {
1060 // Register saves.
1061 assert(DstReg == ARM::SP &&
1062 "Only stack pointer as a destination reg is supported");
1063
1064 SmallVector<unsigned, 4> RegList;
Anton Korobeynikov7a764162011-03-05 18:43:43 +00001065 // Skip src & dst reg, and pred ops.
1066 unsigned StartOp = 2 + 2;
1067 // Use all the operands.
1068 unsigned NumOffset = 0;
1069
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001070 switch (Opc) {
1071 default:
1072 MI->dump();
1073 assert(0 && "Unsupported opcode for unwinding information");
Anton Korobeynikov7a764162011-03-05 18:43:43 +00001074 case ARM::tPUSH:
1075 // Special case here: no src & dst reg, but two extra imp ops.
1076 StartOp = 2; NumOffset = 2;
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001077 case ARM::STMDB_UPD:
Anton Korobeynikov7a764162011-03-05 18:43:43 +00001078 case ARM::t2STMDB_UPD:
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001079 case ARM::VSTMDDB_UPD:
1080 assert(SrcReg == ARM::SP &&
1081 "Only stack pointer as a source reg is supported");
Anton Korobeynikov7a764162011-03-05 18:43:43 +00001082 for (unsigned i = StartOp, NumOps = MI->getNumOperands() - NumOffset;
1083 i != NumOps; ++i)
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001084 RegList.push_back(MI->getOperand(i).getReg());
1085 break;
Owen Anderson793e7962011-07-26 20:54:26 +00001086 case ARM::STR_PRE_IMM:
1087 case ARM::STR_PRE_REG:
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001088 assert(MI->getOperand(2).getReg() == ARM::SP &&
1089 "Only stack pointer as a source reg is supported");
1090 RegList.push_back(SrcReg);
1091 break;
1092 }
1093 OutStreamer.EmitRegSave(RegList, Opc == ARM::VSTMDDB_UPD);
1094 } else {
1095 // Changes of stack / frame pointer.
1096 if (SrcReg == ARM::SP) {
1097 int64_t Offset = 0;
1098 switch (Opc) {
1099 default:
1100 MI->dump();
1101 assert(0 && "Unsupported opcode for unwinding information");
1102 case ARM::MOVr:
1103 Offset = 0;
1104 break;
1105 case ARM::ADDri:
1106 Offset = -MI->getOperand(2).getImm();
1107 break;
1108 case ARM::SUBri:
Jim Grosbachf6fd9092011-06-29 23:25:04 +00001109 Offset = MI->getOperand(2).getImm();
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001110 break;
Anton Korobeynikov7a764162011-03-05 18:43:43 +00001111 case ARM::tSUBspi:
Jim Grosbachf6fd9092011-06-29 23:25:04 +00001112 Offset = MI->getOperand(2).getImm()*4;
Anton Korobeynikov7a764162011-03-05 18:43:43 +00001113 break;
1114 case ARM::tADDspi:
1115 case ARM::tADDrSPi:
1116 Offset = -MI->getOperand(2).getImm()*4;
1117 break;
Anton Korobeynikovb3fcc062011-03-05 18:43:55 +00001118 case ARM::tLDRpci: {
1119 // Grab the constpool index and check, whether it corresponds to
1120 // original or cloned constpool entry.
1121 unsigned CPI = MI->getOperand(1).getIndex();
1122 const MachineConstantPool *MCP = MF.getConstantPool();
1123 if (CPI >= MCP->getConstants().size())
1124 CPI = AFI.getOriginalCPIdx(CPI);
1125 assert(CPI != -1U && "Invalid constpool index");
1126
1127 // Derive the actual offset.
1128 const MachineConstantPoolEntry &CPE = MCP->getConstants()[CPI];
1129 assert(!CPE.isMachineConstantPoolEntry() && "Invalid constpool entry");
1130 // FIXME: Check for user, it should be "add" instruction!
1131 Offset = -cast<ConstantInt>(CPE.Val.ConstVal)->getSExtValue();
Anton Korobeynikov3daccd82011-03-05 18:43:50 +00001132 break;
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001133 }
Anton Korobeynikovb3fcc062011-03-05 18:43:55 +00001134 }
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001135
1136 if (DstReg == FramePtr && FramePtr != ARM::SP)
Anton Korobeynikove5163792011-03-05 18:44:00 +00001137 // Set-up of the frame pointer. Positive values correspond to "add"
1138 // instruction.
1139 OutStreamer.EmitSetFP(FramePtr, ARM::SP, -Offset);
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001140 else if (DstReg == ARM::SP) {
Anton Korobeynikove5163792011-03-05 18:44:00 +00001141 // Change of SP by an offset. Positive values correspond to "sub"
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001142 // instruction.
1143 OutStreamer.EmitPad(Offset);
1144 } else {
1145 MI->dump();
1146 assert(0 && "Unsupported opcode for unwinding information");
1147 }
1148 } else if (DstReg == ARM::SP) {
1149 // FIXME: .movsp goes here
1150 MI->dump();
1151 assert(0 && "Unsupported opcode for unwinding information");
1152 }
1153 else {
1154 MI->dump();
1155 assert(0 && "Unsupported opcode for unwinding information");
1156 }
1157 }
1158}
1159
1160extern cl::opt<bool> EnableARMEHABI;
1161
Jim Grosbach53e3fc42011-07-08 17:40:42 +00001162// Simple pseudo-instructions have their lowering (with expansion to real
1163// instructions) auto-generated.
1164#include "ARMGenMCPseudoLowering.inc"
1165
Jim Grosbachb454cda2010-09-29 15:23:40 +00001166void ARMAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Jim Grosbach5aa29a02011-08-23 21:32:34 +00001167 // Emit unwinding stuff for frame-related instructions
1168 if (EnableARMEHABI && MI->getFlag(MachineInstr::FrameSetup))
1169 EmitUnwindingInstruction(MI);
1170
Jim Grosbach53e3fc42011-07-08 17:40:42 +00001171 // Do any auto-generated pseudo lowerings.
1172 if (emitPseudoExpansionLowering(OutStreamer, MI))
1173 return;
1174
Andrew Trick3be654f2011-09-21 02:20:46 +00001175 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
1176 "Pseudo flag setting opcode should be expanded early");
1177
Jim Grosbach53e3fc42011-07-08 17:40:42 +00001178 // Check for manual lowerings.
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001179 unsigned Opc = MI->getOpcode();
1180 switch (Opc) {
Chris Lattner112f2392010-11-14 20:31:06 +00001181 case ARM::t2MOVi32imm: assert(0 && "Should be lowered by thumb2it pass");
Jim Grosbach2d0f53b2010-09-28 17:05:56 +00001182 case ARM::DBG_VALUE: {
1183 if (isVerbose() && OutStreamer.hasRawTextSupport()) {
1184 SmallString<128> TmpStr;
1185 raw_svector_ostream OS(TmpStr);
1186 PrintDebugValueComment(MI, OS);
1187 OutStreamer.EmitRawText(StringRef(OS.str()));
1188 }
1189 return;
1190 }
Jim Grosbach40edf732010-12-14 21:10:47 +00001191 case ARM::LEApcrel:
Jim Grosbachd40963c2010-12-14 22:28:03 +00001192 case ARM::tLEApcrel:
Jim Grosbach40edf732010-12-14 21:10:47 +00001193 case ARM::t2LEApcrel: {
Jim Grosbachdff84b02010-12-02 00:28:45 +00001194 // FIXME: Need to also handle globals and externals
Jim Grosbachdff84b02010-12-02 00:28:45 +00001195 MCInst TmpInst;
Jim Grosbachd40963c2010-12-14 22:28:03 +00001196 TmpInst.setOpcode(MI->getOpcode() == ARM::t2LEApcrel ? ARM::t2ADR
1197 : (MI->getOpcode() == ARM::tLEApcrel ? ARM::tADR
1198 : ARM::ADR));
Jim Grosbach40edf732010-12-14 21:10:47 +00001199 populateADROperands(TmpInst, MI->getOperand(0).getReg(),
1200 GetCPISymbol(MI->getOperand(1).getIndex()),
1201 MI->getOperand(2).getImm(), MI->getOperand(3).getReg(),
1202 OutContext);
Jim Grosbachdff84b02010-12-02 00:28:45 +00001203 OutStreamer.EmitInstruction(TmpInst);
1204 return;
1205 }
Jim Grosbachd40963c2010-12-14 22:28:03 +00001206 case ARM::LEApcrelJT:
1207 case ARM::tLEApcrelJT:
1208 case ARM::t2LEApcrelJT: {
Jim Grosbach5d14f9b2010-12-01 19:47:31 +00001209 MCInst TmpInst;
Jim Grosbachd40963c2010-12-14 22:28:03 +00001210 TmpInst.setOpcode(MI->getOpcode() == ARM::t2LEApcrelJT ? ARM::t2ADR
1211 : (MI->getOpcode() == ARM::tLEApcrelJT ? ARM::tADR
1212 : ARM::ADR));
Jim Grosbach40edf732010-12-14 21:10:47 +00001213 populateADROperands(TmpInst, MI->getOperand(0).getReg(),
1214 GetARMJTIPICJumpTableLabel2(MI->getOperand(1).getIndex(),
1215 MI->getOperand(2).getImm()),
1216 MI->getOperand(3).getImm(), MI->getOperand(4).getReg(),
1217 OutContext);
Jim Grosbach5d14f9b2010-12-01 19:47:31 +00001218 OutStreamer.EmitInstruction(TmpInst);
1219 return;
1220 }
Jim Grosbachf859a542011-03-12 00:45:26 +00001221 // Darwin call instructions are just normal call instructions with different
1222 // clobber semantics (they clobber R9).
Jim Grosbacha0d2c8a2010-11-30 18:30:19 +00001223 case ARM::BXr9_CALL:
1224 case ARM::BX_CALL: {
1225 {
1226 MCInst TmpInst;
1227 TmpInst.setOpcode(ARM::MOVr);
1228 TmpInst.addOperand(MCOperand::CreateReg(ARM::LR));
1229 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1230 // Add predicate operands.
1231 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1232 TmpInst.addOperand(MCOperand::CreateReg(0));
1233 // Add 's' bit operand (always reg0 for this)
1234 TmpInst.addOperand(MCOperand::CreateReg(0));
1235 OutStreamer.EmitInstruction(TmpInst);
1236 }
1237 {
1238 MCInst TmpInst;
1239 TmpInst.setOpcode(ARM::BX);
1240 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1241 OutStreamer.EmitInstruction(TmpInst);
1242 }
1243 return;
1244 }
Cameron Zwarichad70f6d2011-05-25 21:53:50 +00001245 case ARM::tBXr9_CALL:
1246 case ARM::tBX_CALL: {
1247 {
1248 MCInst TmpInst;
1249 TmpInst.setOpcode(ARM::tMOVr);
1250 TmpInst.addOperand(MCOperand::CreateReg(ARM::LR));
1251 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
Jim Grosbach63b46fa2011-06-30 22:10:46 +00001252 // Add predicate operands.
1253 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1254 TmpInst.addOperand(MCOperand::CreateReg(0));
Cameron Zwarichad70f6d2011-05-25 21:53:50 +00001255 OutStreamer.EmitInstruction(TmpInst);
1256 }
1257 {
1258 MCInst TmpInst;
1259 TmpInst.setOpcode(ARM::tBX);
1260 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1261 // Add predicate operands.
1262 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1263 TmpInst.addOperand(MCOperand::CreateReg(0));
1264 OutStreamer.EmitInstruction(TmpInst);
1265 }
1266 return;
1267 }
Jim Grosbacha0d2c8a2010-11-30 18:30:19 +00001268 case ARM::BMOVPCRXr9_CALL:
1269 case ARM::BMOVPCRX_CALL: {
1270 {
1271 MCInst TmpInst;
1272 TmpInst.setOpcode(ARM::MOVr);
1273 TmpInst.addOperand(MCOperand::CreateReg(ARM::LR));
1274 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1275 // Add predicate operands.
1276 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1277 TmpInst.addOperand(MCOperand::CreateReg(0));
1278 // Add 's' bit operand (always reg0 for this)
1279 TmpInst.addOperand(MCOperand::CreateReg(0));
1280 OutStreamer.EmitInstruction(TmpInst);
1281 }
1282 {
1283 MCInst TmpInst;
1284 TmpInst.setOpcode(ARM::MOVr);
1285 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1286 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1287 // Add predicate operands.
1288 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1289 TmpInst.addOperand(MCOperand::CreateReg(0));
1290 // Add 's' bit operand (always reg0 for this)
1291 TmpInst.addOperand(MCOperand::CreateReg(0));
1292 OutStreamer.EmitInstruction(TmpInst);
1293 }
1294 return;
1295 }
Evan Cheng53519f02011-01-21 18:55:51 +00001296 case ARM::MOVi16_ga_pcrel:
1297 case ARM::t2MOVi16_ga_pcrel: {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001298 MCInst TmpInst;
Evan Cheng53519f02011-01-21 18:55:51 +00001299 TmpInst.setOpcode(Opc == ARM::MOVi16_ga_pcrel? ARM::MOVi16 : ARM::t2MOVi16);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001300 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1301
Evan Cheng53519f02011-01-21 18:55:51 +00001302 unsigned TF = MI->getOperand(1).getTargetFlags();
1303 bool isPIC = TF == ARMII::MO_LO16_NONLAZY_PIC;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001304 const GlobalValue *GV = MI->getOperand(1).getGlobal();
1305 MCSymbol *GVSym = GetARMGVSymbol(GV);
1306 const MCExpr *GVSymExpr = MCSymbolRefExpr::Create(GVSym, OutContext);
Evan Cheng53519f02011-01-21 18:55:51 +00001307 if (isPIC) {
1308 MCSymbol *LabelSym = getPICLabel(MAI->getPrivateGlobalPrefix(),
1309 getFunctionNumber(),
1310 MI->getOperand(2).getImm(), OutContext);
1311 const MCExpr *LabelSymExpr= MCSymbolRefExpr::Create(LabelSym, OutContext);
1312 unsigned PCAdj = (Opc == ARM::MOVi16_ga_pcrel) ? 8 : 4;
1313 const MCExpr *PCRelExpr =
1314 ARMMCExpr::CreateLower16(MCBinaryExpr::CreateSub(GVSymExpr,
1315 MCBinaryExpr::CreateAdd(LabelSymExpr,
1316 MCConstantExpr::Create(PCAdj, OutContext),
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001317 OutContext), OutContext), OutContext);
Evan Cheng53519f02011-01-21 18:55:51 +00001318 TmpInst.addOperand(MCOperand::CreateExpr(PCRelExpr));
1319 } else {
1320 const MCExpr *RefExpr= ARMMCExpr::CreateLower16(GVSymExpr, OutContext);
1321 TmpInst.addOperand(MCOperand::CreateExpr(RefExpr));
1322 }
1323
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001324 // Add predicate operands.
1325 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1326 TmpInst.addOperand(MCOperand::CreateReg(0));
1327 // Add 's' bit operand (always reg0 for this)
1328 TmpInst.addOperand(MCOperand::CreateReg(0));
1329 OutStreamer.EmitInstruction(TmpInst);
1330 return;
1331 }
Evan Cheng53519f02011-01-21 18:55:51 +00001332 case ARM::MOVTi16_ga_pcrel:
1333 case ARM::t2MOVTi16_ga_pcrel: {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001334 MCInst TmpInst;
Evan Cheng53519f02011-01-21 18:55:51 +00001335 TmpInst.setOpcode(Opc == ARM::MOVTi16_ga_pcrel
1336 ? ARM::MOVTi16 : ARM::t2MOVTi16);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001337 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1338 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
1339
Evan Cheng53519f02011-01-21 18:55:51 +00001340 unsigned TF = MI->getOperand(2).getTargetFlags();
1341 bool isPIC = TF == ARMII::MO_HI16_NONLAZY_PIC;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001342 const GlobalValue *GV = MI->getOperand(2).getGlobal();
1343 MCSymbol *GVSym = GetARMGVSymbol(GV);
1344 const MCExpr *GVSymExpr = MCSymbolRefExpr::Create(GVSym, OutContext);
Evan Cheng53519f02011-01-21 18:55:51 +00001345 if (isPIC) {
1346 MCSymbol *LabelSym = getPICLabel(MAI->getPrivateGlobalPrefix(),
1347 getFunctionNumber(),
1348 MI->getOperand(3).getImm(), OutContext);
1349 const MCExpr *LabelSymExpr= MCSymbolRefExpr::Create(LabelSym, OutContext);
1350 unsigned PCAdj = (Opc == ARM::MOVTi16_ga_pcrel) ? 8 : 4;
1351 const MCExpr *PCRelExpr =
1352 ARMMCExpr::CreateUpper16(MCBinaryExpr::CreateSub(GVSymExpr,
1353 MCBinaryExpr::CreateAdd(LabelSymExpr,
1354 MCConstantExpr::Create(PCAdj, OutContext),
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001355 OutContext), OutContext), OutContext);
Evan Cheng53519f02011-01-21 18:55:51 +00001356 TmpInst.addOperand(MCOperand::CreateExpr(PCRelExpr));
1357 } else {
1358 const MCExpr *RefExpr= ARMMCExpr::CreateUpper16(GVSymExpr, OutContext);
1359 TmpInst.addOperand(MCOperand::CreateExpr(RefExpr));
1360 }
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001361 // Add predicate operands.
1362 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1363 TmpInst.addOperand(MCOperand::CreateReg(0));
1364 // Add 's' bit operand (always reg0 for this)
1365 TmpInst.addOperand(MCOperand::CreateReg(0));
1366 OutStreamer.EmitInstruction(TmpInst);
1367 return;
1368 }
Jim Grosbachfbd18732010-09-17 23:41:53 +00001369 case ARM::tPICADD: {
1370 // This is a pseudo op for a label + instruction sequence, which looks like:
1371 // LPC0:
1372 // add r0, pc
1373 // This adds the address of LPC0 to r0.
1374
1375 // Emit the label.
Jim Grosbach988ce092010-09-18 00:05:05 +00001376 OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(),
1377 getFunctionNumber(), MI->getOperand(2).getImm(),
1378 OutContext));
Jim Grosbachfbd18732010-09-17 23:41:53 +00001379
1380 // Form and emit the add.
1381 MCInst AddInst;
1382 AddInst.setOpcode(ARM::tADDhirr);
1383 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1384 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1385 AddInst.addOperand(MCOperand::CreateReg(ARM::PC));
1386 // Add predicate operands.
1387 AddInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1388 AddInst.addOperand(MCOperand::CreateReg(0));
1389 OutStreamer.EmitInstruction(AddInst);
1390 return;
1391 }
Jim Grosbacha3fbadf2010-09-30 19:53:58 +00001392 case ARM::PICADD: {
Chris Lattner4d152222009-10-19 22:23:04 +00001393 // This is a pseudo op for a label + instruction sequence, which looks like:
1394 // LPC0:
1395 // add r0, pc, r0
1396 // This adds the address of LPC0 to r0.
Jim Grosbachb0739b72010-09-02 01:02:06 +00001397
Chris Lattner4d152222009-10-19 22:23:04 +00001398 // Emit the label.
Jim Grosbach988ce092010-09-18 00:05:05 +00001399 OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(),
1400 getFunctionNumber(), MI->getOperand(2).getImm(),
1401 OutContext));
Jim Grosbachb0739b72010-09-02 01:02:06 +00001402
Jim Grosbachf3f09522010-09-14 21:05:34 +00001403 // Form and emit the add.
Chris Lattner4d152222009-10-19 22:23:04 +00001404 MCInst AddInst;
1405 AddInst.setOpcode(ARM::ADDrr);
1406 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1407 AddInst.addOperand(MCOperand::CreateReg(ARM::PC));
1408 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
Jim Grosbach5b46d622010-09-14 21:28:17 +00001409 // Add predicate operands.
1410 AddInst.addOperand(MCOperand::CreateImm(MI->getOperand(3).getImm()));
1411 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(4).getReg()));
1412 // Add 's' bit operand (always reg0 for this)
1413 AddInst.addOperand(MCOperand::CreateReg(0));
Chris Lattner850d2e22010-02-03 01:16:28 +00001414 OutStreamer.EmitInstruction(AddInst);
Chris Lattner4d152222009-10-19 22:23:04 +00001415 return;
1416 }
Jim Grosbacha28abbe2010-09-17 16:25:52 +00001417 case ARM::PICSTR:
1418 case ARM::PICSTRB:
1419 case ARM::PICSTRH:
1420 case ARM::PICLDR:
1421 case ARM::PICLDRB:
1422 case ARM::PICLDRH:
1423 case ARM::PICLDRSB:
1424 case ARM::PICLDRSH: {
Jim Grosbachb74ca9d2010-09-16 17:43:25 +00001425 // This is a pseudo op for a label + instruction sequence, which looks like:
1426 // LPC0:
Jim Grosbacha28abbe2010-09-17 16:25:52 +00001427 // OP r0, [pc, r0]
Jim Grosbachb74ca9d2010-09-16 17:43:25 +00001428 // The LCP0 label is referenced by a constant pool entry in order to get
1429 // a PC-relative address at the ldr instruction.
1430
1431 // Emit the label.
Jim Grosbach988ce092010-09-18 00:05:05 +00001432 OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(),
1433 getFunctionNumber(), MI->getOperand(2).getImm(),
1434 OutContext));
Jim Grosbachb74ca9d2010-09-16 17:43:25 +00001435
1436 // Form and emit the load
Jim Grosbacha28abbe2010-09-17 16:25:52 +00001437 unsigned Opcode;
1438 switch (MI->getOpcode()) {
1439 default:
1440 llvm_unreachable("Unexpected opcode!");
Jim Grosbach7e3383c2010-10-27 23:12:14 +00001441 case ARM::PICSTR: Opcode = ARM::STRrs; break;
1442 case ARM::PICSTRB: Opcode = ARM::STRBrs; break;
Jim Grosbacha28abbe2010-09-17 16:25:52 +00001443 case ARM::PICSTRH: Opcode = ARM::STRH; break;
Jim Grosbach3e556122010-10-26 22:37:02 +00001444 case ARM::PICLDR: Opcode = ARM::LDRrs; break;
Jim Grosbachc1d30212010-10-27 00:19:44 +00001445 case ARM::PICLDRB: Opcode = ARM::LDRBrs; break;
Jim Grosbacha28abbe2010-09-17 16:25:52 +00001446 case ARM::PICLDRH: Opcode = ARM::LDRH; break;
1447 case ARM::PICLDRSB: Opcode = ARM::LDRSB; break;
1448 case ARM::PICLDRSH: Opcode = ARM::LDRSH; break;
1449 }
1450 MCInst LdStInst;
1451 LdStInst.setOpcode(Opcode);
1452 LdStInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1453 LdStInst.addOperand(MCOperand::CreateReg(ARM::PC));
1454 LdStInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
1455 LdStInst.addOperand(MCOperand::CreateImm(0));
Jim Grosbachb74ca9d2010-09-16 17:43:25 +00001456 // Add predicate operands.
Jim Grosbacha28abbe2010-09-17 16:25:52 +00001457 LdStInst.addOperand(MCOperand::CreateImm(MI->getOperand(3).getImm()));
1458 LdStInst.addOperand(MCOperand::CreateReg(MI->getOperand(4).getReg()));
1459 OutStreamer.EmitInstruction(LdStInst);
Jim Grosbachb74ca9d2010-09-16 17:43:25 +00001460
1461 return;
1462 }
Jim Grosbacha3fbadf2010-09-30 19:53:58 +00001463 case ARM::CONSTPOOL_ENTRY: {
Chris Lattnera70e6442009-10-19 22:33:05 +00001464 /// CONSTPOOL_ENTRY - This instruction represents a floating constant pool
1465 /// in the function. The first operand is the ID# for this instruction, the
1466 /// second is the index into the MachineConstantPool that this is, the third
1467 /// is the size in bytes of this constant pool entry.
1468 unsigned LabelId = (unsigned)MI->getOperand(0).getImm();
1469 unsigned CPIdx = (unsigned)MI->getOperand(1).getIndex();
1470
1471 EmitAlignment(2);
Chris Lattner1b46f432010-01-23 07:00:21 +00001472 OutStreamer.EmitLabel(GetCPISymbol(LabelId));
Chris Lattnera70e6442009-10-19 22:33:05 +00001473
1474 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPIdx];
1475 if (MCPE.isMachineConstantPoolEntry())
1476 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
1477 else
1478 EmitGlobalConstant(MCPE.Val.ConstVal);
Jim Grosbachb0739b72010-09-02 01:02:06 +00001479
Chris Lattnera70e6442009-10-19 22:33:05 +00001480 return;
1481 }
Jim Grosbach882ef2b2010-09-21 23:28:16 +00001482 case ARM::t2BR_JT: {
1483 // Lower and emit the instruction itself, then the jump table following it.
1484 MCInst TmpInst;
Jim Grosbach2a7b41b2011-06-30 23:38:17 +00001485 TmpInst.setOpcode(ARM::tMOVr);
Jim Grosbach5ca66692010-11-29 22:37:40 +00001486 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1487 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1488 // Add predicate operands.
1489 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1490 TmpInst.addOperand(MCOperand::CreateReg(0));
Jim Grosbach882ef2b2010-09-21 23:28:16 +00001491 OutStreamer.EmitInstruction(TmpInst);
Jim Grosbach5ca66692010-11-29 22:37:40 +00001492 // Output the data for the jump table itself
1493 EmitJump2Table(MI);
1494 return;
1495 }
1496 case ARM::t2TBB_JT: {
1497 // Lower and emit the instruction itself, then the jump table following it.
1498 MCInst TmpInst;
1499
1500 TmpInst.setOpcode(ARM::t2TBB);
1501 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1502 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1503 // Add predicate operands.
1504 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1505 TmpInst.addOperand(MCOperand::CreateReg(0));
1506 OutStreamer.EmitInstruction(TmpInst);
1507 // Output the data for the jump table itself
1508 EmitJump2Table(MI);
1509 // Make sure the next instruction is 2-byte aligned.
1510 EmitAlignment(1);
1511 return;
1512 }
1513 case ARM::t2TBH_JT: {
1514 // Lower and emit the instruction itself, then the jump table following it.
1515 MCInst TmpInst;
1516
1517 TmpInst.setOpcode(ARM::t2TBH);
1518 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1519 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1520 // Add predicate operands.
1521 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1522 TmpInst.addOperand(MCOperand::CreateReg(0));
1523 OutStreamer.EmitInstruction(TmpInst);
1524 // Output the data for the jump table itself
Jim Grosbach882ef2b2010-09-21 23:28:16 +00001525 EmitJump2Table(MI);
1526 return;
1527 }
Jim Grosbachf1aa47d2010-11-29 19:32:47 +00001528 case ARM::tBR_JTr:
Jim Grosbach2dc77682010-11-29 18:37:44 +00001529 case ARM::BR_JTr: {
1530 // Lower and emit the instruction itself, then the jump table following it.
1531 // mov pc, target
1532 MCInst TmpInst;
Jim Grosbach5ca66692010-11-29 22:37:40 +00001533 unsigned Opc = MI->getOpcode() == ARM::BR_JTr ?
Jim Grosbach2a7b41b2011-06-30 23:38:17 +00001534 ARM::MOVr : ARM::tMOVr;
Jim Grosbachf1aa47d2010-11-29 19:32:47 +00001535 TmpInst.setOpcode(Opc);
Jim Grosbach2dc77682010-11-29 18:37:44 +00001536 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1537 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1538 // Add predicate operands.
1539 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1540 TmpInst.addOperand(MCOperand::CreateReg(0));
Jim Grosbacha0d2c8a2010-11-30 18:30:19 +00001541 // Add 's' bit operand (always reg0 for this)
1542 if (Opc == ARM::MOVr)
1543 TmpInst.addOperand(MCOperand::CreateReg(0));
Jim Grosbach2dc77682010-11-29 18:37:44 +00001544 OutStreamer.EmitInstruction(TmpInst);
1545
Jim Grosbachf1aa47d2010-11-29 19:32:47 +00001546 // Make sure the Thumb jump table is 4-byte aligned.
Jim Grosbach2a7b41b2011-06-30 23:38:17 +00001547 if (Opc == ARM::tMOVr)
Jim Grosbachf1aa47d2010-11-29 19:32:47 +00001548 EmitAlignment(2);
1549
Jim Grosbach2dc77682010-11-29 18:37:44 +00001550 // Output the data for the jump table itself
1551 EmitJumpTable(MI);
1552 return;
1553 }
1554 case ARM::BR_JTm: {
1555 // Lower and emit the instruction itself, then the jump table following it.
1556 // ldr pc, target
1557 MCInst TmpInst;
1558 if (MI->getOperand(1).getReg() == 0) {
1559 // literal offset
1560 TmpInst.setOpcode(ARM::LDRi12);
1561 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1562 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1563 TmpInst.addOperand(MCOperand::CreateImm(MI->getOperand(2).getImm()));
1564 } else {
1565 TmpInst.setOpcode(ARM::LDRrs);
1566 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1567 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1568 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
1569 TmpInst.addOperand(MCOperand::CreateImm(0));
1570 }
1571 // Add predicate operands.
1572 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1573 TmpInst.addOperand(MCOperand::CreateReg(0));
1574 OutStreamer.EmitInstruction(TmpInst);
1575
1576 // Output the data for the jump table itself
Jim Grosbacha2244cb2010-09-22 17:39:48 +00001577 EmitJumpTable(MI);
1578 return;
1579 }
Jim Grosbachf8dabac2010-11-17 21:05:55 +00001580 case ARM::BR_JTadd: {
1581 // Lower and emit the instruction itself, then the jump table following it.
1582 // add pc, target, idx
Jim Grosbach2dc77682010-11-29 18:37:44 +00001583 MCInst TmpInst;
1584 TmpInst.setOpcode(ARM::ADDrr);
1585 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1586 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1587 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
Jim Grosbachf8dabac2010-11-17 21:05:55 +00001588 // Add predicate operands.
Jim Grosbach2dc77682010-11-29 18:37:44 +00001589 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1590 TmpInst.addOperand(MCOperand::CreateReg(0));
Jim Grosbachf8dabac2010-11-17 21:05:55 +00001591 // Add 's' bit operand (always reg0 for this)
Jim Grosbach2dc77682010-11-29 18:37:44 +00001592 TmpInst.addOperand(MCOperand::CreateReg(0));
1593 OutStreamer.EmitInstruction(TmpInst);
Jim Grosbachf8dabac2010-11-17 21:05:55 +00001594
1595 // Output the data for the jump table itself
1596 EmitJumpTable(MI);
1597 return;
1598 }
Jim Grosbach2e6ae132010-09-23 18:05:37 +00001599 case ARM::TRAP: {
1600 // Non-Darwin binutils don't yet support the "trap" mnemonic.
1601 // FIXME: Remove this special case when they do.
1602 if (!Subtarget->isTargetDarwin()) {
Jim Grosbach78890f42010-10-01 23:21:38 +00001603 //.long 0xe7ffdefe @ trap
Jim Grosbachb2dda4b2010-09-23 19:42:17 +00001604 uint32_t Val = 0xe7ffdefeUL;
Jim Grosbach2e6ae132010-09-23 18:05:37 +00001605 OutStreamer.AddComment("trap");
1606 OutStreamer.EmitIntValue(Val, 4);
1607 return;
1608 }
1609 break;
1610 }
1611 case ARM::tTRAP: {
1612 // Non-Darwin binutils don't yet support the "trap" mnemonic.
1613 // FIXME: Remove this special case when they do.
1614 if (!Subtarget->isTargetDarwin()) {
Jim Grosbach78890f42010-10-01 23:21:38 +00001615 //.short 57086 @ trap
Benjamin Kramerc8ab9eb2010-09-23 18:57:26 +00001616 uint16_t Val = 0xdefe;
Jim Grosbach2e6ae132010-09-23 18:05:37 +00001617 OutStreamer.AddComment("trap");
1618 OutStreamer.EmitIntValue(Val, 2);
1619 return;
1620 }
1621 break;
1622 }
Jim Grosbach433a5782010-09-24 20:47:58 +00001623 case ARM::t2Int_eh_sjlj_setjmp:
1624 case ARM::t2Int_eh_sjlj_setjmp_nofp:
Jim Grosbacha3fbadf2010-09-30 19:53:58 +00001625 case ARM::tInt_eh_sjlj_setjmp: {
Jim Grosbach433a5782010-09-24 20:47:58 +00001626 // Two incoming args: GPR:$src, GPR:$val
1627 // mov $val, pc
1628 // adds $val, #7
1629 // str $val, [$src, #4]
1630 // movs r0, #0
1631 // b 1f
1632 // movs r0, #1
1633 // 1:
1634 unsigned SrcReg = MI->getOperand(0).getReg();
1635 unsigned ValReg = MI->getOperand(1).getReg();
1636 MCSymbol *Label = GetARMSJLJEHLabel();
1637 {
1638 MCInst TmpInst;
Jim Grosbach2a7b41b2011-06-30 23:38:17 +00001639 TmpInst.setOpcode(ARM::tMOVr);
Jim Grosbach433a5782010-09-24 20:47:58 +00001640 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1641 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
Jim Grosbach63b46fa2011-06-30 22:10:46 +00001642 // Predicate.
1643 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1644 TmpInst.addOperand(MCOperand::CreateReg(0));
Jim Grosbach433a5782010-09-24 20:47:58 +00001645 OutStreamer.AddComment("eh_setjmp begin");
1646 OutStreamer.EmitInstruction(TmpInst);
1647 }
1648 {
1649 MCInst TmpInst;
1650 TmpInst.setOpcode(ARM::tADDi3);
1651 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1652 // 's' bit operand
1653 TmpInst.addOperand(MCOperand::CreateReg(ARM::CPSR));
1654 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1655 TmpInst.addOperand(MCOperand::CreateImm(7));
1656 // Predicate.
1657 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1658 TmpInst.addOperand(MCOperand::CreateReg(0));
1659 OutStreamer.EmitInstruction(TmpInst);
1660 }
1661 {
1662 MCInst TmpInst;
Bill Wendlingf4caf692010-12-14 03:36:38 +00001663 TmpInst.setOpcode(ARM::tSTRi);
Jim Grosbach433a5782010-09-24 20:47:58 +00001664 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1665 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
1666 // The offset immediate is #4. The operand value is scaled by 4 for the
1667 // tSTR instruction.
1668 TmpInst.addOperand(MCOperand::CreateImm(1));
Jim Grosbach433a5782010-09-24 20:47:58 +00001669 // Predicate.
1670 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1671 TmpInst.addOperand(MCOperand::CreateReg(0));
1672 OutStreamer.EmitInstruction(TmpInst);
1673 }
1674 {
1675 MCInst TmpInst;
1676 TmpInst.setOpcode(ARM::tMOVi8);
1677 TmpInst.addOperand(MCOperand::CreateReg(ARM::R0));
1678 TmpInst.addOperand(MCOperand::CreateReg(ARM::CPSR));
1679 TmpInst.addOperand(MCOperand::CreateImm(0));
1680 // Predicate.
1681 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1682 TmpInst.addOperand(MCOperand::CreateReg(0));
1683 OutStreamer.EmitInstruction(TmpInst);
1684 }
1685 {
1686 const MCExpr *SymbolExpr = MCSymbolRefExpr::Create(Label, OutContext);
1687 MCInst TmpInst;
1688 TmpInst.setOpcode(ARM::tB);
1689 TmpInst.addOperand(MCOperand::CreateExpr(SymbolExpr));
Owen Anderson51f6a7a2011-09-09 21:48:23 +00001690 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1691 TmpInst.addOperand(MCOperand::CreateReg(0));
Jim Grosbach433a5782010-09-24 20:47:58 +00001692 OutStreamer.EmitInstruction(TmpInst);
1693 }
1694 {
1695 MCInst TmpInst;
1696 TmpInst.setOpcode(ARM::tMOVi8);
1697 TmpInst.addOperand(MCOperand::CreateReg(ARM::R0));
1698 TmpInst.addOperand(MCOperand::CreateReg(ARM::CPSR));
1699 TmpInst.addOperand(MCOperand::CreateImm(1));
1700 // Predicate.
1701 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1702 TmpInst.addOperand(MCOperand::CreateReg(0));
1703 OutStreamer.AddComment("eh_setjmp end");
1704 OutStreamer.EmitInstruction(TmpInst);
1705 }
1706 OutStreamer.EmitLabel(Label);
1707 return;
1708 }
1709
Jim Grosbach45390082010-09-23 23:33:56 +00001710 case ARM::Int_eh_sjlj_setjmp_nofp:
Jim Grosbacha3fbadf2010-09-30 19:53:58 +00001711 case ARM::Int_eh_sjlj_setjmp: {
Jim Grosbach45390082010-09-23 23:33:56 +00001712 // Two incoming args: GPR:$src, GPR:$val
1713 // add $val, pc, #8
1714 // str $val, [$src, #+4]
1715 // mov r0, #0
1716 // add pc, pc, #0
1717 // mov r0, #1
1718 unsigned SrcReg = MI->getOperand(0).getReg();
1719 unsigned ValReg = MI->getOperand(1).getReg();
1720
1721 {
1722 MCInst TmpInst;
1723 TmpInst.setOpcode(ARM::ADDri);
1724 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1725 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1726 TmpInst.addOperand(MCOperand::CreateImm(8));
1727 // Predicate.
1728 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1729 TmpInst.addOperand(MCOperand::CreateReg(0));
1730 // 's' bit operand (always reg0 for this).
1731 TmpInst.addOperand(MCOperand::CreateReg(0));
1732 OutStreamer.AddComment("eh_setjmp begin");
1733 OutStreamer.EmitInstruction(TmpInst);
1734 }
1735 {
1736 MCInst TmpInst;
Jim Grosbach7e3383c2010-10-27 23:12:14 +00001737 TmpInst.setOpcode(ARM::STRi12);
Jim Grosbach45390082010-09-23 23:33:56 +00001738 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1739 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
Jim Grosbach45390082010-09-23 23:33:56 +00001740 TmpInst.addOperand(MCOperand::CreateImm(4));
1741 // Predicate.
1742 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1743 TmpInst.addOperand(MCOperand::CreateReg(0));
1744 OutStreamer.EmitInstruction(TmpInst);
1745 }
1746 {
1747 MCInst TmpInst;
1748 TmpInst.setOpcode(ARM::MOVi);
1749 TmpInst.addOperand(MCOperand::CreateReg(ARM::R0));
1750 TmpInst.addOperand(MCOperand::CreateImm(0));
1751 // Predicate.
1752 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1753 TmpInst.addOperand(MCOperand::CreateReg(0));
1754 // 's' bit operand (always reg0 for this).
1755 TmpInst.addOperand(MCOperand::CreateReg(0));
1756 OutStreamer.EmitInstruction(TmpInst);
1757 }
1758 {
1759 MCInst TmpInst;
1760 TmpInst.setOpcode(ARM::ADDri);
1761 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1762 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1763 TmpInst.addOperand(MCOperand::CreateImm(0));
1764 // Predicate.
1765 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1766 TmpInst.addOperand(MCOperand::CreateReg(0));
1767 // 's' bit operand (always reg0 for this).
1768 TmpInst.addOperand(MCOperand::CreateReg(0));
1769 OutStreamer.EmitInstruction(TmpInst);
1770 }
1771 {
1772 MCInst TmpInst;
1773 TmpInst.setOpcode(ARM::MOVi);
1774 TmpInst.addOperand(MCOperand::CreateReg(ARM::R0));
1775 TmpInst.addOperand(MCOperand::CreateImm(1));
1776 // Predicate.
1777 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1778 TmpInst.addOperand(MCOperand::CreateReg(0));
1779 // 's' bit operand (always reg0 for this).
1780 TmpInst.addOperand(MCOperand::CreateReg(0));
1781 OutStreamer.AddComment("eh_setjmp end");
1782 OutStreamer.EmitInstruction(TmpInst);
1783 }
1784 return;
1785 }
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001786 case ARM::Int_eh_sjlj_longjmp: {
1787 // ldr sp, [$src, #8]
1788 // ldr $scratch, [$src, #4]
1789 // ldr r7, [$src]
1790 // bx $scratch
1791 unsigned SrcReg = MI->getOperand(0).getReg();
1792 unsigned ScratchReg = MI->getOperand(1).getReg();
1793 {
1794 MCInst TmpInst;
Jim Grosbach3e556122010-10-26 22:37:02 +00001795 TmpInst.setOpcode(ARM::LDRi12);
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001796 TmpInst.addOperand(MCOperand::CreateReg(ARM::SP));
1797 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001798 TmpInst.addOperand(MCOperand::CreateImm(8));
1799 // Predicate.
1800 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1801 TmpInst.addOperand(MCOperand::CreateReg(0));
1802 OutStreamer.EmitInstruction(TmpInst);
1803 }
1804 {
1805 MCInst TmpInst;
Jim Grosbach3e556122010-10-26 22:37:02 +00001806 TmpInst.setOpcode(ARM::LDRi12);
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001807 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1808 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001809 TmpInst.addOperand(MCOperand::CreateImm(4));
1810 // Predicate.
1811 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1812 TmpInst.addOperand(MCOperand::CreateReg(0));
1813 OutStreamer.EmitInstruction(TmpInst);
1814 }
1815 {
1816 MCInst TmpInst;
Jim Grosbach3e556122010-10-26 22:37:02 +00001817 TmpInst.setOpcode(ARM::LDRi12);
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001818 TmpInst.addOperand(MCOperand::CreateReg(ARM::R7));
1819 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001820 TmpInst.addOperand(MCOperand::CreateImm(0));
1821 // Predicate.
1822 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1823 TmpInst.addOperand(MCOperand::CreateReg(0));
1824 OutStreamer.EmitInstruction(TmpInst);
1825 }
1826 {
1827 MCInst TmpInst;
Bill Wendling6e46d842010-11-30 00:48:15 +00001828 TmpInst.setOpcode(ARM::BX);
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001829 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1830 // Predicate.
1831 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1832 TmpInst.addOperand(MCOperand::CreateReg(0));
1833 OutStreamer.EmitInstruction(TmpInst);
1834 }
1835 return;
1836 }
Jim Grosbach385cc5e2010-09-27 22:28:11 +00001837 case ARM::tInt_eh_sjlj_longjmp: {
1838 // ldr $scratch, [$src, #8]
1839 // mov sp, $scratch
1840 // ldr $scratch, [$src, #4]
1841 // ldr r7, [$src]
1842 // bx $scratch
1843 unsigned SrcReg = MI->getOperand(0).getReg();
1844 unsigned ScratchReg = MI->getOperand(1).getReg();
1845 {
1846 MCInst TmpInst;
Bill Wendlingf4caf692010-12-14 03:36:38 +00001847 TmpInst.setOpcode(ARM::tLDRi);
Jim Grosbach385cc5e2010-09-27 22:28:11 +00001848 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1849 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
1850 // The offset immediate is #8. The operand value is scaled by 4 for the
Bill Wendlingf4caf692010-12-14 03:36:38 +00001851 // tLDR instruction.
Jim Grosbach385cc5e2010-09-27 22:28:11 +00001852 TmpInst.addOperand(MCOperand::CreateImm(2));
Jim Grosbach385cc5e2010-09-27 22:28:11 +00001853 // Predicate.
1854 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1855 TmpInst.addOperand(MCOperand::CreateReg(0));
1856 OutStreamer.EmitInstruction(TmpInst);
1857 }
1858 {
1859 MCInst TmpInst;
Jim Grosbach2a7b41b2011-06-30 23:38:17 +00001860 TmpInst.setOpcode(ARM::tMOVr);
Jim Grosbach385cc5e2010-09-27 22:28:11 +00001861 TmpInst.addOperand(MCOperand::CreateReg(ARM::SP));
1862 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1863 // Predicate.
1864 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1865 TmpInst.addOperand(MCOperand::CreateReg(0));
1866 OutStreamer.EmitInstruction(TmpInst);
1867 }
1868 {
1869 MCInst TmpInst;
Bill Wendlingf4caf692010-12-14 03:36:38 +00001870 TmpInst.setOpcode(ARM::tLDRi);
Jim Grosbach385cc5e2010-09-27 22:28:11 +00001871 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1872 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
1873 TmpInst.addOperand(MCOperand::CreateImm(1));
Jim Grosbach385cc5e2010-09-27 22:28:11 +00001874 // Predicate.
1875 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1876 TmpInst.addOperand(MCOperand::CreateReg(0));
1877 OutStreamer.EmitInstruction(TmpInst);
1878 }
1879 {
1880 MCInst TmpInst;
Bill Wendlingf4caf692010-12-14 03:36:38 +00001881 TmpInst.setOpcode(ARM::tLDRr);
Jim Grosbach385cc5e2010-09-27 22:28:11 +00001882 TmpInst.addOperand(MCOperand::CreateReg(ARM::R7));
1883 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
Jim Grosbach385cc5e2010-09-27 22:28:11 +00001884 TmpInst.addOperand(MCOperand::CreateReg(0));
1885 // Predicate.
1886 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1887 TmpInst.addOperand(MCOperand::CreateReg(0));
1888 OutStreamer.EmitInstruction(TmpInst);
1889 }
1890 {
1891 MCInst TmpInst;
Cameron Zwarich421b1062011-05-26 03:41:12 +00001892 TmpInst.setOpcode(ARM::tBX);
Jim Grosbach385cc5e2010-09-27 22:28:11 +00001893 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1894 // Predicate.
1895 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1896 TmpInst.addOperand(MCOperand::CreateReg(0));
1897 OutStreamer.EmitInstruction(TmpInst);
1898 }
1899 return;
1900 }
Chris Lattner97f06932009-10-19 20:20:46 +00001901 }
Jim Grosbachb0739b72010-09-02 01:02:06 +00001902
Chris Lattner97f06932009-10-19 20:20:46 +00001903 MCInst TmpInst;
Chris Lattner30e2cc22010-11-14 21:00:02 +00001904 LowerARMMachineInstrToMCInst(MI, TmpInst, *this);
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001905
Chris Lattner850d2e22010-02-03 01:16:28 +00001906 OutStreamer.EmitInstruction(TmpInst);
Chris Lattner97f06932009-10-19 20:20:46 +00001907}
Daniel Dunbar2685a292009-10-20 05:15:36 +00001908
1909//===----------------------------------------------------------------------===//
1910// Target Registry Stuff
1911//===----------------------------------------------------------------------===//
1912
Daniel Dunbar2685a292009-10-20 05:15:36 +00001913// Force static initialization.
1914extern "C" void LLVMInitializeARMAsmPrinter() {
1915 RegisterAsmPrinter<ARMAsmPrinter> X(TheARMTarget);
1916 RegisterAsmPrinter<ARMAsmPrinter> Y(TheThumbTarget);
Daniel Dunbar2685a292009-10-20 05:15:36 +00001917}
1918