blob: ea3319fb0e03b2324bd316de5bcf1e0b4ee0d65a [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() {
Owen Anderson2fec6c52011-10-04 23:26:17 +0000293 OutStreamer.ForceCodeRegion();
294
Chris Lattner953ebb72010-01-27 23:58:11 +0000295 if (AFI->isThumbFunction()) {
Jim Grosbachce792992010-11-05 22:08:08 +0000296 OutStreamer.EmitAssemblerFlag(MCAF_Code16);
Rafael Espindola64695402011-05-16 16:17:21 +0000297 OutStreamer.EmitThumbFunc(CurrentFnSym);
Chris Lattner953ebb72010-01-27 23:58:11 +0000298 }
Jim Grosbachb0739b72010-09-02 01:02:06 +0000299
Chris Lattner953ebb72010-01-27 23:58:11 +0000300 OutStreamer.EmitLabel(CurrentFnSym);
301}
302
Jim Grosbach2317e402010-09-30 01:57:53 +0000303/// runOnMachineFunction - This uses the EmitInstruction()
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000304/// method to print assembly for each instruction.
305///
306bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Evan Chenga8e29892007-01-19 07:51:42 +0000307 AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng6d63a722008-09-18 07:27:23 +0000308 MCP = MF.getConstantPool();
Rafael Espindola4b442b52006-05-23 02:48:20 +0000309
Chris Lattnerd49fe1b2010-01-28 01:28:58 +0000310 return AsmPrinter::runOnMachineFunction(MF);
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000311}
312
Evan Cheng055b0312009-06-29 07:51:04 +0000313void ARMAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
Chris Lattner35c33bd2010-04-04 04:47:45 +0000314 raw_ostream &O, const char *Modifier) {
Evan Cheng055b0312009-06-29 07:51:04 +0000315 const MachineOperand &MO = MI->getOperand(OpNum);
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000316 unsigned TF = MO.getTargetFlags();
317
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000318 switch (MO.getType()) {
Chris Lattner8bc86cb2009-10-19 20:59:55 +0000319 default:
320 assert(0 && "<unknown operand type>");
Bob Wilson5bafff32009-06-22 23:27:02 +0000321 case MachineOperand::MO_Register: {
322 unsigned Reg = MO.getReg();
Chris Lattner8bc86cb2009-10-19 20:59:55 +0000323 assert(TargetRegisterInfo::isPhysicalRegister(Reg));
Jim Grosbach35636282010-10-06 21:22:32 +0000324 assert(!MO.getSubReg() && "Subregs should be eliminated!");
325 O << ARMInstPrinter::getRegisterName(Reg);
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000326 break;
Bob Wilson5bafff32009-06-22 23:27:02 +0000327 }
Evan Chenga8e29892007-01-19 07:51:42 +0000328 case MachineOperand::MO_Immediate: {
Evan Cheng5adb66a2009-09-28 09:14:39 +0000329 int64_t Imm = MO.getImm();
Anton Korobeynikov632606c2009-10-08 20:43:22 +0000330 O << '#';
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000331 if ((Modifier && strcmp(Modifier, "lo16") == 0) ||
Jason W Kim650b7d72011-01-12 23:21:49 +0000332 (TF == ARMII::MO_LO16))
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000333 O << ":lower16:";
334 else if ((Modifier && strcmp(Modifier, "hi16") == 0) ||
Jason W Kim650b7d72011-01-12 23:21:49 +0000335 (TF == ARMII::MO_HI16))
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000336 O << ":upper16:";
Anton Korobeynikov632606c2009-10-08 20:43:22 +0000337 O << Imm;
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000338 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000339 }
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000340 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner1b2eb0e2010-03-13 21:04:28 +0000341 O << *MO.getMBB()->getSymbol();
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000342 return;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000343 case MachineOperand::MO_GlobalAddress: {
Dan Gohman46510a72010-04-15 01:51:59 +0000344 const GlobalValue *GV = MO.getGlobal();
Anton Korobeynikov5cdc3a92009-11-24 00:44:37 +0000345 if ((Modifier && strcmp(Modifier, "lo16") == 0) ||
346 (TF & ARMII::MO_LO16))
347 O << ":lower16:";
348 else if ((Modifier && strcmp(Modifier, "hi16") == 0) ||
349 (TF & ARMII::MO_HI16))
350 O << ":upper16:";
Chris Lattnerd62f1b42010-03-12 21:19:23 +0000351 O << *Mang->getSymbol(GV);
Anton Korobeynikov7751ad92008-11-22 16:15:34 +0000352
Chris Lattner0c08d092010-04-03 22:28:33 +0000353 printOffset(MO.getOffset(), O);
Jim Grosbach1d6111c2010-10-06 21:36:43 +0000354 if (TF == ARMII::MO_PLT)
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000355 O << "(PLT)";
Evan Chenga8e29892007-01-19 07:51:42 +0000356 break;
Rafael Espindola84b19be2006-07-16 01:02:57 +0000357 }
Evan Chenga8e29892007-01-19 07:51:42 +0000358 case MachineOperand::MO_ExternalSymbol: {
Chris Lattner10b318b2010-01-17 21:43:43 +0000359 O << *GetExternalSymbolSymbol(MO.getSymbolName());
Jim Grosbach1d6111c2010-10-06 21:36:43 +0000360 if (TF == ARMII::MO_PLT)
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000361 O << "(PLT)";
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000362 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000363 }
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000364 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner1b46f432010-01-23 07:00:21 +0000365 O << *GetCPISymbol(MO.getIndex());
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000366 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000367 case MachineOperand::MO_JumpTableIndex:
Chris Lattner1b46f432010-01-23 07:00:21 +0000368 O << *GetJTISymbol(MO.getIndex());
Evan Chenga8e29892007-01-19 07:51:42 +0000369 break;
Rafael Espindola2f99b6b2006-05-25 12:57:06 +0000370 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000371}
372
Evan Cheng055b0312009-06-29 07:51:04 +0000373//===--------------------------------------------------------------------===//
374
Chris Lattner0890cf12010-01-25 19:51:38 +0000375MCSymbol *ARMAsmPrinter::
376GetARMSetPICJumpTableLabel2(unsigned uid, unsigned uid2,
377 const MachineBasicBlock *MBB) const {
378 SmallString<60> Name;
379 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix()
Chris Lattnerbfcb0962010-01-25 19:39:52 +0000380 << getFunctionNumber() << '_' << uid << '_' << uid2
Chris Lattner0890cf12010-01-25 19:51:38 +0000381 << "_set_" << MBB->getNumber();
Chris Lattner9b97a732010-03-30 18:10:53 +0000382 return OutContext.GetOrCreateSymbol(Name.str());
Chris Lattner0890cf12010-01-25 19:51:38 +0000383}
384
385MCSymbol *ARMAsmPrinter::
386GetARMJTIPICJumpTableLabel2(unsigned uid, unsigned uid2) const {
387 SmallString<60> Name;
388 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "JTI"
Chris Lattner281e7762010-01-25 23:28:03 +0000389 << getFunctionNumber() << '_' << uid << '_' << uid2;
Chris Lattner9b97a732010-03-30 18:10:53 +0000390 return OutContext.GetOrCreateSymbol(Name.str());
Chris Lattnerbfcb0962010-01-25 19:39:52 +0000391}
392
Jim Grosbach433a5782010-09-24 20:47:58 +0000393
394MCSymbol *ARMAsmPrinter::GetARMSJLJEHLabel(void) const {
395 SmallString<60> Name;
396 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "SJLJEH"
397 << getFunctionNumber();
398 return OutContext.GetOrCreateSymbol(Name.str());
399}
400
Evan Cheng055b0312009-06-29 07:51:04 +0000401bool ARMAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
Chris Lattnerc75c0282010-04-04 05:29:35 +0000402 unsigned AsmVariant, const char *ExtraCode,
403 raw_ostream &O) {
Evan Chenga8e29892007-01-19 07:51:42 +0000404 // Does this asm operand have a single letter operand modifier?
405 if (ExtraCode && ExtraCode[0]) {
406 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikov8e9ece72009-08-08 23:10:41 +0000407
Evan Chenga8e29892007-01-19 07:51:42 +0000408 switch (ExtraCode[0]) {
409 default: return true; // Unknown modifier.
Bob Wilson9b4b00a2009-07-09 23:54:51 +0000410 case 'a': // Print as a memory address.
411 if (MI->getOperand(OpNum).isReg()) {
Jim Grosbach2f24c4e2010-09-30 15:25:22 +0000412 O << "["
413 << ARMInstPrinter::getRegisterName(MI->getOperand(OpNum).getReg())
414 << "]";
Bob Wilson9b4b00a2009-07-09 23:54:51 +0000415 return false;
416 }
417 // Fallthrough
418 case 'c': // Don't print "#" before an immediate operand.
Bob Wilson4f38b382009-08-21 21:58:55 +0000419 if (!MI->getOperand(OpNum).isImm())
420 return true;
Jim Grosbach2317e402010-09-30 01:57:53 +0000421 O << MI->getOperand(OpNum).getImm();
Bob Wilson8f343462009-04-06 21:46:51 +0000422 return false;
Evan Chenge21e3962007-04-04 00:13:29 +0000423 case 'P': // Print a VFP double precision register.
Evan Chengd831cda2009-12-08 23:06:22 +0000424 case 'q': // Print a NEON quad precision register.
Chris Lattner35c33bd2010-04-04 04:47:45 +0000425 printOperand(MI, OpNum, O);
Evan Cheng23a95702007-03-08 22:42:46 +0000426 return false;
Eric Christopher0628d382011-05-24 22:10:34 +0000427 case 'y': // Print a VFP single precision register as indexed double.
428 // This uses the ordering of the alias table to get the first 'd' register
429 // that overlaps the 's' register. Also, s0 is an odd register, hence the
430 // odd modulus check below.
431 if (MI->getOperand(OpNum).isReg()) {
432 unsigned Reg = MI->getOperand(OpNum).getReg();
433 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
434 O << ARMInstPrinter::getRegisterName(TRI->getAliasSet(Reg)[0]) <<
435 (((Reg % 2) == 1) ? "[0]" : "[1]");
436 return false;
437 }
Eric Christopher4db7dec2011-05-24 23:27:13 +0000438 return true;
Eric Christopherfef50062011-05-24 22:27:43 +0000439 case 'B': // Bitwise inverse of integer or symbol without a preceding #.
Eric Christophere1739d52011-05-24 23:15:43 +0000440 if (!MI->getOperand(OpNum).isImm())
441 return true;
442 O << ~(MI->getOperand(OpNum).getImm());
443 return false;
Eric Christopherfef50062011-05-24 22:27:43 +0000444 case 'L': // The low 16 bits of an immediate constant.
Eric Christopher4db7dec2011-05-24 23:27:13 +0000445 if (!MI->getOperand(OpNum).isImm())
446 return true;
447 O << (MI->getOperand(OpNum).getImm() & 0xffff);
448 return false;
Eric Christopher3c14f242011-05-28 01:40:44 +0000449 case 'M': { // A register range suitable for LDM/STM.
450 if (!MI->getOperand(OpNum).isReg())
451 return true;
452 const MachineOperand &MO = MI->getOperand(OpNum);
453 unsigned RegBegin = MO.getReg();
454 // This takes advantage of the 2 operand-ness of ldm/stm and that we've
455 // already got the operands in registers that are operands to the
456 // inline asm statement.
Jim Grosbach8e0c7692011-09-02 18:46:15 +0000457
Eric Christopher3c14f242011-05-28 01:40:44 +0000458 O << "{" << ARMInstPrinter::getRegisterName(RegBegin);
Jim Grosbach8e0c7692011-09-02 18:46:15 +0000459
Eric Christopher3c14f242011-05-28 01:40:44 +0000460 // FIXME: The register allocator not only may not have given us the
461 // registers in sequence, but may not be in ascending registers. This
462 // will require changes in the register allocator that'll need to be
463 // propagated down here if the operands change.
464 unsigned RegOps = OpNum + 1;
465 while (MI->getOperand(RegOps).isReg()) {
Jim Grosbach8e0c7692011-09-02 18:46:15 +0000466 O << ", "
Eric Christopher3c14f242011-05-28 01:40:44 +0000467 << ARMInstPrinter::getRegisterName(MI->getOperand(RegOps).getReg());
468 RegOps++;
469 }
470
471 O << "}";
472
473 return false;
474 }
Rafael Espindolaf5ade5d2011-08-10 16:26:42 +0000475 case 'R': // The most significant register of a pair.
476 case 'Q': { // The least significant register of a pair.
477 if (OpNum == 0)
478 return true;
479 const MachineOperand &FlagsOP = MI->getOperand(OpNum - 1);
480 if (!FlagsOP.isImm())
481 return true;
482 unsigned Flags = FlagsOP.getImm();
483 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
484 if (NumVals != 2)
485 return true;
486 unsigned RegOp = ExtraCode[0] == 'Q' ? OpNum : OpNum + 1;
487 if (RegOp >= MI->getNumOperands())
488 return true;
489 const MachineOperand &MO = MI->getOperand(RegOp);
490 if (!MO.isReg())
491 return true;
492 unsigned Reg = MO.getReg();
493 O << ARMInstPrinter::getRegisterName(Reg);
494 return false;
495 }
496
Eric Christopher3c14f242011-05-28 01:40:44 +0000497 // These modifiers are not yet supported.
Eric Christopherfef50062011-05-24 22:27:43 +0000498 case 'p': // The high single-precision register of a VFP double-precision
499 // register.
500 case 'e': // The low doubleword register of a NEON quad register.
501 case 'f': // The high doubleword register of a NEON quad register.
502 case 'h': // A range of VFP/NEON registers suitable for VLD1/VST1.
Eric Christopherfef50062011-05-24 22:27:43 +0000503 case 'H': // The highest-numbered register of a pair.
Bob Wilsond984eb62010-05-27 20:23:42 +0000504 return true;
Evan Cheng84f60b72010-05-27 22:08:38 +0000505 }
Evan Chenga8e29892007-01-19 07:51:42 +0000506 }
Jim Grosbache9952212009-09-04 01:38:51 +0000507
Chris Lattner35c33bd2010-04-04 04:47:45 +0000508 printOperand(MI, OpNum, O);
Evan Chenga8e29892007-01-19 07:51:42 +0000509 return false;
510}
511
Bob Wilson224c2442009-05-19 05:53:42 +0000512bool ARMAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Evan Cheng055b0312009-06-29 07:51:04 +0000513 unsigned OpNum, unsigned AsmVariant,
Chris Lattnerc75c0282010-04-04 05:29:35 +0000514 const char *ExtraCode,
515 raw_ostream &O) {
Eric Christopher8f894632011-05-25 20:51:58 +0000516 // Does this asm operand have a single letter operand modifier?
517 if (ExtraCode && ExtraCode[0]) {
518 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Jim Grosbach8e0c7692011-09-02 18:46:15 +0000519
Eric Christopher8f894632011-05-25 20:51:58 +0000520 switch (ExtraCode[0]) {
Eric Christopher32bfb2c2011-05-26 18:22:26 +0000521 case 'A': // A memory operand for a VLD1/VST1 instruction.
Eric Christopher8f894632011-05-25 20:51:58 +0000522 default: return true; // Unknown modifier.
523 case 'm': // The base register of a memory operand.
524 if (!MI->getOperand(OpNum).isReg())
525 return true;
526 O << ARMInstPrinter::getRegisterName(MI->getOperand(OpNum).getReg());
527 return false;
528 }
529 }
Jim Grosbach8e0c7692011-09-02 18:46:15 +0000530
Bob Wilson765cc0b2009-10-13 20:50:28 +0000531 const MachineOperand &MO = MI->getOperand(OpNum);
532 assert(MO.isReg() && "unexpected inline asm memory operand");
Jim Grosbach2317e402010-09-30 01:57:53 +0000533 O << "[" << ARMInstPrinter::getRegisterName(MO.getReg()) << "]";
Bob Wilson224c2442009-05-19 05:53:42 +0000534 return false;
535}
536
Bob Wilson812209a2009-09-30 22:06:26 +0000537void ARMAsmPrinter::EmitStartOfAsmFile(Module &M) {
Bob Wilson0fb34682009-09-30 00:23:42 +0000538 if (Subtarget->isTargetDarwin()) {
539 Reloc::Model RelocM = TM.getRelocationModel();
540 if (RelocM == Reloc::PIC_ || RelocM == Reloc::DynamicNoPIC) {
541 // Declare all the text sections up front (before the DWARF sections
542 // emitted by AsmPrinter::doInitialization) so the assembler will keep
543 // them together at the beginning of the object file. This helps
544 // avoid out-of-range branches that are due a fundamental limitation of
545 // the way symbol offsets are encoded with the current Darwin ARM
546 // relocations.
Jim Grosbachb0739b72010-09-02 01:02:06 +0000547 const TargetLoweringObjectFileMachO &TLOFMacho =
Dan Gohman0d805c32010-04-17 16:44:48 +0000548 static_cast<const TargetLoweringObjectFileMachO &>(
549 getObjFileLowering());
Bob Wilson29e06692009-09-30 22:25:37 +0000550 OutStreamer.SwitchSection(TLOFMacho.getTextSection());
551 OutStreamer.SwitchSection(TLOFMacho.getTextCoalSection());
552 OutStreamer.SwitchSection(TLOFMacho.getConstTextCoalSection());
553 if (RelocM == Reloc::DynamicNoPIC) {
554 const MCSection *sect =
Chris Lattner22772212010-04-08 20:40:11 +0000555 OutContext.getMachOSection("__TEXT", "__symbol_stub4",
556 MCSectionMachO::S_SYMBOL_STUBS,
557 12, SectionKind::getText());
Bob Wilson29e06692009-09-30 22:25:37 +0000558 OutStreamer.SwitchSection(sect);
559 } else {
560 const MCSection *sect =
Chris Lattner22772212010-04-08 20:40:11 +0000561 OutContext.getMachOSection("__TEXT", "__picsymbolstub4",
562 MCSectionMachO::S_SYMBOL_STUBS,
563 16, SectionKind::getText());
Bob Wilson29e06692009-09-30 22:25:37 +0000564 OutStreamer.SwitchSection(sect);
565 }
Bob Wilson63db5942010-07-30 19:55:47 +0000566 const MCSection *StaticInitSect =
567 OutContext.getMachOSection("__TEXT", "__StaticInit",
568 MCSectionMachO::S_REGULAR |
569 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
570 SectionKind::getText());
571 OutStreamer.SwitchSection(StaticInitSect);
Bob Wilson0fb34682009-09-30 00:23:42 +0000572 }
573 }
574
Jim Grosbache5165492009-11-09 00:11:35 +0000575 // Use unified assembler syntax.
Jason W Kimafd1cc22010-09-30 02:45:56 +0000576 OutStreamer.EmitAssemblerFlag(MCAF_SyntaxUnified);
Anton Korobeynikovd61eca52009-06-17 23:43:18 +0000577
Anton Korobeynikov88ce6672009-05-23 19:51:20 +0000578 // Emit ARM Build Attributes
579 if (Subtarget->isTargetELF()) {
Anton Korobeynikov88ce6672009-05-23 19:51:20 +0000580
Jason W Kimdef9ac42010-10-06 22:36:46 +0000581 emitAttributes();
Anton Korobeynikov88ce6672009-05-23 19:51:20 +0000582 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000583}
584
Anton Korobeynikov0f3cc652008-08-07 09:54:23 +0000585
Chris Lattner4a071d62009-10-19 17:59:19 +0000586void ARMAsmPrinter::EmitEndOfAsmFile(Module &M) {
Evan Cheng5be54b02007-01-19 19:25:36 +0000587 if (Subtarget->isTargetDarwin()) {
Chris Lattnerf61159b2009-08-03 22:18:15 +0000588 // All darwin targets use mach-o.
Dan Gohman0d805c32010-04-17 16:44:48 +0000589 const TargetLoweringObjectFileMachO &TLOFMacho =
590 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattnerb0f294c2009-10-19 18:38:33 +0000591 MachineModuleInfoMachO &MMIMacho =
592 MMI->getObjFileInfo<MachineModuleInfoMachO>();
Jim Grosbache9952212009-09-04 01:38:51 +0000593
Evan Chenga8e29892007-01-19 07:51:42 +0000594 // Output non-lazy-pointers for external and common global variables.
Chris Lattnerb0f294c2009-10-19 18:38:33 +0000595 MachineModuleInfoMachO::SymbolListTy Stubs = MMIMacho.GetGVStubList();
Bill Wendlingcebae362010-03-10 22:34:10 +0000596
Chris Lattnerb0f294c2009-10-19 18:38:33 +0000597 if (!Stubs.empty()) {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000598 // Switch with ".non_lazy_symbol_pointer" directive.
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000599 OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
Chris Lattnerc076a972009-08-10 18:01:34 +0000600 EmitAlignment(2);
Chris Lattnerb0f294c2009-10-19 18:38:33 +0000601 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Bill Wendlingbecd83e2010-03-09 00:40:17 +0000602 // L_foo$stub:
603 OutStreamer.EmitLabel(Stubs[i].first);
604 // .indirect_symbol _foo
Bill Wendling52a50e52010-03-11 01:18:13 +0000605 MachineModuleInfoImpl::StubValueTy &MCSym = Stubs[i].second;
606 OutStreamer.EmitSymbolAttribute(MCSym.getPointer(),MCSA_IndirectSymbol);
Bill Wendlingcf6f28d2010-03-09 00:43:34 +0000607
Bill Wendling52a50e52010-03-11 01:18:13 +0000608 if (MCSym.getInt())
Bill Wendlingcf6f28d2010-03-09 00:43:34 +0000609 // External to current translation unit.
610 OutStreamer.EmitIntValue(0, 4/*size*/, 0/*addrspace*/);
611 else
612 // Internal to current translation unit.
Bill Wendling5e1b55d2010-03-31 18:47:10 +0000613 //
Jim Grosbach1b935a32010-09-22 16:45:13 +0000614 // When we place the LSDA into the TEXT section, the type info
615 // pointers need to be indirect and pc-rel. We accomplish this by
616 // using NLPs; however, sometimes the types are local to the file.
617 // We need to fill in the value for the NLP in those cases.
Bill Wendling52a50e52010-03-11 01:18:13 +0000618 OutStreamer.EmitValue(MCSymbolRefExpr::Create(MCSym.getPointer(),
619 OutContext),
Bill Wendlingcf6f28d2010-03-09 00:43:34 +0000620 4/*size*/, 0/*addrspace*/);
Evan Chengae94e592008-12-05 01:06:39 +0000621 }
Bill Wendlingbecd83e2010-03-09 00:40:17 +0000622
623 Stubs.clear();
624 OutStreamer.AddBlankLine();
Evan Chenga8e29892007-01-19 07:51:42 +0000625 }
626
Chris Lattnere4d9ea82009-10-19 18:44:38 +0000627 Stubs = MMIMacho.GetHiddenGVStubList();
628 if (!Stubs.empty()) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000629 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
Chris Lattnerf3231de2009-08-10 18:02:16 +0000630 EmitAlignment(2);
Bill Wendlingbecd83e2010-03-09 00:40:17 +0000631 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
632 // L_foo$stub:
633 OutStreamer.EmitLabel(Stubs[i].first);
634 // .long _foo
Bill Wendlingcebae362010-03-10 22:34:10 +0000635 OutStreamer.EmitValue(MCSymbolRefExpr::
636 Create(Stubs[i].second.getPointer(),
637 OutContext),
Bill Wendlingbecd83e2010-03-09 00:40:17 +0000638 4/*size*/, 0/*addrspace*/);
639 }
Bill Wendlingcf6f28d2010-03-09 00:43:34 +0000640
641 Stubs.clear();
642 OutStreamer.AddBlankLine();
Evan Chengae94e592008-12-05 01:06:39 +0000643 }
644
Evan Chenga8e29892007-01-19 07:51:42 +0000645 // Funny Darwin hack: This flag tells the linker that no global symbols
646 // contain code that falls through to other global symbols (e.g. the obvious
647 // implementation of multiple entry points). If this doesn't occur, the
648 // linker can safely perform dead code stripping. Since LLVM never
649 // generates code that does this, it is always safe to set.
Chris Lattnera5ad93a2010-01-23 06:39:22 +0000650 OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Rafael Espindolab01c4bb2006-07-27 11:38:51 +0000651 }
Rafael Espindola7bc59bc2006-05-14 22:18:28 +0000652}
Anton Korobeynikov0bd89712008-08-17 13:55:10 +0000653
Chris Lattner97f06932009-10-19 20:20:46 +0000654//===----------------------------------------------------------------------===//
Jason W Kimdef9ac42010-10-06 22:36:46 +0000655// Helper routines for EmitStartOfAsmFile() and EmitEndOfAsmFile()
656// FIXME:
657// The following seem like one-off assembler flags, but they actually need
Jim Grosbachfa7fb642010-10-06 22:46:47 +0000658// to appear in the .ARM.attributes section in ELF.
Jason W Kimdef9ac42010-10-06 22:36:46 +0000659// Instead of subclassing the MCELFStreamer, we do the work here.
660
661void ARMAsmPrinter::emitAttributes() {
Jim Grosbachfa7fb642010-10-06 22:46:47 +0000662
Jason W Kim17b443d2010-10-11 23:01:44 +0000663 emitARMAttributeSection();
664
Renato Golin728ff0d2011-02-28 22:04:27 +0000665 /* GAS expect .fpu to be emitted, regardless of VFP build attribute */
666 bool emitFPU = false;
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000667 AttributeEmitter *AttrEmitter;
Renato Golin728ff0d2011-02-28 22:04:27 +0000668 if (OutStreamer.hasRawTextSupport()) {
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000669 AttrEmitter = new AsmAttributeEmitter(OutStreamer);
Renato Golin728ff0d2011-02-28 22:04:27 +0000670 emitFPU = true;
671 } else {
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000672 MCObjectStreamer &O = static_cast<MCObjectStreamer&>(OutStreamer);
673 AttrEmitter = new ObjectAttributeEmitter(O);
674 }
675
676 AttrEmitter->MaybeSwitchVendor("aeabi");
677
Jason W Kimdef9ac42010-10-06 22:36:46 +0000678 std::string CPUString = Subtarget->getCPUString();
Jason W Kimf009a962011-02-07 00:49:53 +0000679
680 if (CPUString == "cortex-a8" ||
681 Subtarget->isCortexA8()) {
Jason W Kimc046d642011-02-07 19:07:11 +0000682 AttrEmitter->EmitTextAttribute(ARMBuildAttrs::CPU_name, "cortex-a8");
Jason W Kimf009a962011-02-07 00:49:53 +0000683 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v7);
684 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch_profile,
685 ARMBuildAttrs::ApplicationProfile);
686 AttrEmitter->EmitAttribute(ARMBuildAttrs::ARM_ISA_use,
687 ARMBuildAttrs::Allowed);
688 AttrEmitter->EmitAttribute(ARMBuildAttrs::THUMB_ISA_use,
689 ARMBuildAttrs::AllowThumb32);
690 // Fixme: figure out when this is emitted.
691 //AttrEmitter->EmitAttribute(ARMBuildAttrs::WMMX_arch,
692 // ARMBuildAttrs::AllowWMMXv1);
693 //
694
695 /// ADD additional Else-cases here!
Rafael Espindolab8adb8a2011-05-20 20:10:34 +0000696 } else if (CPUString == "xscale") {
697 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v5TEJ);
698 AttrEmitter->EmitAttribute(ARMBuildAttrs::ARM_ISA_use,
699 ARMBuildAttrs::Allowed);
700 AttrEmitter->EmitAttribute(ARMBuildAttrs::THUMB_ISA_use,
701 ARMBuildAttrs::Allowed);
Jason W Kimf009a962011-02-07 00:49:53 +0000702 } else if (CPUString == "generic") {
Dale Johannesen7179d1e2010-11-08 19:17:22 +0000703 // FIXME: Why these defaults?
704 AttrEmitter->EmitAttribute(ARMBuildAttrs::CPU_arch, ARMBuildAttrs::v4T);
Jason W Kimf009a962011-02-07 00:49:53 +0000705 AttrEmitter->EmitAttribute(ARMBuildAttrs::ARM_ISA_use,
706 ARMBuildAttrs::Allowed);
707 AttrEmitter->EmitAttribute(ARMBuildAttrs::THUMB_ISA_use,
708 ARMBuildAttrs::Allowed);
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000709 }
Jason W Kimdef9ac42010-10-06 22:36:46 +0000710
Renato Goline89a0532011-03-02 21:20:09 +0000711 if (Subtarget->hasNEON() && emitFPU) {
Renato Golin728ff0d2011-02-28 22:04:27 +0000712 /* NEON is not exactly a VFP architecture, but GAS emit one of
713 * neon/vfpv3/vfpv2 for .fpu parameters */
714 AttrEmitter->EmitTextAttribute(ARMBuildAttrs::Advanced_SIMD_arch, "neon");
715 /* If emitted for NEON, omit from VFP below, since you can have both
716 * NEON and VFP in build attributes but only one .fpu */
717 emitFPU = false;
718 }
719
720 /* VFPv3 + .fpu */
721 if (Subtarget->hasVFP3()) {
722 AttrEmitter->EmitAttribute(ARMBuildAttrs::VFP_arch,
723 ARMBuildAttrs::AllowFPv3A);
724 if (emitFPU)
725 AttrEmitter->EmitTextAttribute(ARMBuildAttrs::VFP_arch, "vfpv3");
726
727 /* VFPv2 + .fpu */
728 } else if (Subtarget->hasVFP2()) {
Jason W Kimf009a962011-02-07 00:49:53 +0000729 AttrEmitter->EmitAttribute(ARMBuildAttrs::VFP_arch,
730 ARMBuildAttrs::AllowFPv2);
Renato Golin728ff0d2011-02-28 22:04:27 +0000731 if (emitFPU)
732 AttrEmitter->EmitTextAttribute(ARMBuildAttrs::VFP_arch, "vfpv2");
733 }
734
735 /* TODO: ARMBuildAttrs::Allowed is not completely accurate,
Cameron Zwarich375db7f2011-07-07 08:28:52 +0000736 * since NEON can have 1 (allowed) or 2 (MAC operations) */
Renato Golin728ff0d2011-02-28 22:04:27 +0000737 if (Subtarget->hasNEON()) {
738 AttrEmitter->EmitAttribute(ARMBuildAttrs::Advanced_SIMD_arch,
739 ARMBuildAttrs::Allowed);
740 }
Jason W Kimdef9ac42010-10-06 22:36:46 +0000741
742 // Signal various FP modes.
743 if (!UnsafeFPMath) {
Jason W Kimf009a962011-02-07 00:49:53 +0000744 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_FP_denormal,
745 ARMBuildAttrs::Allowed);
746 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_FP_exceptions,
747 ARMBuildAttrs::Allowed);
Jason W Kimdef9ac42010-10-06 22:36:46 +0000748 }
749
750 if (NoInfsFPMath && NoNaNsFPMath)
Jason W Kimf009a962011-02-07 00:49:53 +0000751 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_FP_number_model,
752 ARMBuildAttrs::Allowed);
Jason W Kimdef9ac42010-10-06 22:36:46 +0000753 else
Jason W Kimf009a962011-02-07 00:49:53 +0000754 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_FP_number_model,
755 ARMBuildAttrs::AllowIEE754);
Jason W Kimdef9ac42010-10-06 22:36:46 +0000756
Jason W Kimf009a962011-02-07 00:49:53 +0000757 // FIXME: add more flags to ARMBuildAttrs.h
Jason W Kimdef9ac42010-10-06 22:36:46 +0000758 // 8-bytes alignment stuff.
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000759 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_align8_needed, 1);
760 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_align8_preserved, 1);
Jason W Kimdef9ac42010-10-06 22:36:46 +0000761
762 // Hard float. Use both S and D registers and conform to AAPCS-VFP.
763 if (Subtarget->isAAPCS_ABI() && FloatABIType == FloatABI::Hard) {
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000764 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_HardFP_use, 3);
765 AttrEmitter->EmitAttribute(ARMBuildAttrs::ABI_VFP_args, 1);
Jason W Kimdef9ac42010-10-06 22:36:46 +0000766 }
767 // FIXME: Should we signal R9 usage?
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000768
Jason W Kimf009a962011-02-07 00:49:53 +0000769 if (Subtarget->hasDivide())
770 AttrEmitter->EmitAttribute(ARMBuildAttrs::DIV_use, 1);
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000771
772 AttrEmitter->Finish();
773 delete AttrEmitter;
Jason W Kimdef9ac42010-10-06 22:36:46 +0000774}
775
Jason W Kim17b443d2010-10-11 23:01:44 +0000776void ARMAsmPrinter::emitARMAttributeSection() {
777 // <format-version>
778 // [ <section-length> "vendor-name"
779 // [ <file-tag> <size> <attribute>*
780 // | <section-tag> <size> <section-number>* 0 <attribute>*
781 // | <symbol-tag> <size> <symbol-number>* 0 <attribute>*
782 // ]+
783 // ]*
784
785 if (OutStreamer.hasRawTextSupport())
786 return;
787
788 const ARMElfTargetObjectFile &TLOFELF =
789 static_cast<const ARMElfTargetObjectFile &>
790 (getObjFileLowering());
791
792 OutStreamer.SwitchSection(TLOFELF.getAttributesSection());
Jason W Kim17b443d2010-10-11 23:01:44 +0000793
Rafael Espindolacecbc3d2010-10-25 17:50:35 +0000794 // Format version
795 OutStreamer.EmitIntValue(0x41, 1);
Jason W Kim17b443d2010-10-11 23:01:44 +0000796}
797
Jason W Kimdef9ac42010-10-06 22:36:46 +0000798//===----------------------------------------------------------------------===//
Chris Lattner97f06932009-10-19 20:20:46 +0000799
Jim Grosbach988ce092010-09-18 00:05:05 +0000800static MCSymbol *getPICLabel(const char *Prefix, unsigned FunctionNumber,
801 unsigned LabelId, MCContext &Ctx) {
802
803 MCSymbol *Label = Ctx.GetOrCreateSymbol(Twine(Prefix)
804 + "PC" + Twine(FunctionNumber) + "_" + Twine(LabelId));
805 return Label;
806}
807
Jim Grosbach2c4d5122010-11-10 03:26:07 +0000808static MCSymbolRefExpr::VariantKind
809getModifierVariantKind(ARMCP::ARMCPModifier Modifier) {
810 switch (Modifier) {
811 default: llvm_unreachable("Unknown modifier!");
812 case ARMCP::no_modifier: return MCSymbolRefExpr::VK_None;
813 case ARMCP::TLSGD: return MCSymbolRefExpr::VK_ARM_TLSGD;
814 case ARMCP::TPOFF: return MCSymbolRefExpr::VK_ARM_TPOFF;
815 case ARMCP::GOTTPOFF: return MCSymbolRefExpr::VK_ARM_GOTTPOFF;
816 case ARMCP::GOT: return MCSymbolRefExpr::VK_ARM_GOT;
817 case ARMCP::GOTOFF: return MCSymbolRefExpr::VK_ARM_GOTOFF;
818 }
819 return MCSymbolRefExpr::VK_None;
820}
821
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000822MCSymbol *ARMAsmPrinter::GetARMGVSymbol(const GlobalValue *GV) {
823 bool isIndirect = Subtarget->isTargetDarwin() &&
824 Subtarget->GVIsIndirectSymbol(GV, TM.getRelocationModel());
825 if (!isIndirect)
826 return Mang->getSymbol(GV);
827
828 // FIXME: Remove this when Darwin transition to @GOT like syntax.
829 MCSymbol *MCSym = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
830 MachineModuleInfoMachO &MMIMachO =
831 MMI->getObjFileInfo<MachineModuleInfoMachO>();
832 MachineModuleInfoImpl::StubValueTy &StubSym =
833 GV->hasHiddenVisibility() ? MMIMachO.getHiddenGVStubEntry(MCSym) :
834 MMIMachO.getGVStubEntry(MCSym);
835 if (StubSym.getPointer() == 0)
836 StubSym = MachineModuleInfoImpl::
837 StubValueTy(Mang->getSymbol(GV), !GV->hasInternalLinkage());
838 return MCSym;
839}
840
Jim Grosbach5df08d82010-11-09 18:45:04 +0000841void ARMAsmPrinter::
842EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
843 int Size = TM.getTargetData()->getTypeAllocSize(MCPV->getType());
844
845 ARMConstantPoolValue *ACPV = static_cast<ARMConstantPoolValue*>(MCPV);
Jim Grosbach5df08d82010-11-09 18:45:04 +0000846
Jim Grosbach7c7ddb22010-11-10 17:59:10 +0000847 MCSymbol *MCSym;
Jim Grosbach5df08d82010-11-09 18:45:04 +0000848 if (ACPV->isLSDA()) {
Jim Grosbach7c7ddb22010-11-10 17:59:10 +0000849 SmallString<128> Str;
850 raw_svector_ostream OS(Str);
Jim Grosbach5df08d82010-11-09 18:45:04 +0000851 OS << MAI->getPrivateGlobalPrefix() << "_LSDA_" << getFunctionNumber();
Jim Grosbach7c7ddb22010-11-10 17:59:10 +0000852 MCSym = OutContext.GetOrCreateSymbol(OS.str());
Jim Grosbach5df08d82010-11-09 18:45:04 +0000853 } else if (ACPV->isBlockAddress()) {
Bill Wendling5bb77992011-10-01 08:00:54 +0000854 const BlockAddress *BA =
855 cast<ARMConstantPoolConstant>(ACPV)->getBlockAddress();
856 MCSym = GetBlockAddressSymbol(BA);
Jim Grosbach5df08d82010-11-09 18:45:04 +0000857 } else if (ACPV->isGlobalValue()) {
Bill Wendling5bb77992011-10-01 08:00:54 +0000858 const GlobalValue *GV = cast<ARMConstantPoolConstant>(ACPV)->getGV();
Evan Cheng5de5d4b2011-01-17 08:03:18 +0000859 MCSym = GetARMGVSymbol(GV);
Bill Wendlinge00897c2011-09-29 23:50:42 +0000860 } else if (ACPV->isMachineBasicBlock()) {
Bill Wendling3320f2a2011-10-01 09:30:42 +0000861 const MachineBasicBlock *MBB = cast<ARMConstantPoolMBB>(ACPV)->getMBB();
Bill Wendlinge00897c2011-09-29 23:50:42 +0000862 MCSym = MBB->getSymbol();
Jim Grosbach5df08d82010-11-09 18:45:04 +0000863 } else {
864 assert(ACPV->isExtSymbol() && "unrecognized constant pool value");
Bill Wendlingfe31e672011-10-01 08:58:29 +0000865 const char *Sym = cast<ARMConstantPoolSymbol>(ACPV)->getSymbol();
866 MCSym = GetExternalSymbolSymbol(Sym);
Jim Grosbach5df08d82010-11-09 18:45:04 +0000867 }
868
869 // Create an MCSymbol for the reference.
Jim Grosbach2c4d5122010-11-10 03:26:07 +0000870 const MCExpr *Expr =
871 MCSymbolRefExpr::Create(MCSym, getModifierVariantKind(ACPV->getModifier()),
872 OutContext);
Jim Grosbach5df08d82010-11-09 18:45:04 +0000873
Jim Grosbach2c4d5122010-11-10 03:26:07 +0000874 if (ACPV->getPCAdjustment()) {
875 MCSymbol *PCLabel = getPICLabel(MAI->getPrivateGlobalPrefix(),
876 getFunctionNumber(),
877 ACPV->getLabelId(),
878 OutContext);
879 const MCExpr *PCRelExpr = MCSymbolRefExpr::Create(PCLabel, OutContext);
880 PCRelExpr =
881 MCBinaryExpr::CreateAdd(PCRelExpr,
882 MCConstantExpr::Create(ACPV->getPCAdjustment(),
883 OutContext),
884 OutContext);
885 if (ACPV->mustAddCurrentAddress()) {
886 // We want "(<expr> - .)", but MC doesn't have a concept of the '.'
887 // label, so just emit a local label end reference that instead.
888 MCSymbol *DotSym = OutContext.CreateTempSymbol();
889 OutStreamer.EmitLabel(DotSym);
890 const MCExpr *DotExpr = MCSymbolRefExpr::Create(DotSym, OutContext);
891 PCRelExpr = MCBinaryExpr::CreateSub(PCRelExpr, DotExpr, OutContext);
Jim Grosbach5df08d82010-11-09 18:45:04 +0000892 }
Jim Grosbach2c4d5122010-11-10 03:26:07 +0000893 Expr = MCBinaryExpr::CreateSub(Expr, PCRelExpr, OutContext);
Jim Grosbach5df08d82010-11-09 18:45:04 +0000894 }
Jim Grosbach2c4d5122010-11-10 03:26:07 +0000895 OutStreamer.EmitValue(Expr, Size);
Jim Grosbach5df08d82010-11-09 18:45:04 +0000896}
897
Jim Grosbacha2244cb2010-09-22 17:39:48 +0000898void ARMAsmPrinter::EmitJumpTable(const MachineInstr *MI) {
899 unsigned Opcode = MI->getOpcode();
900 int OpNum = 1;
901 if (Opcode == ARM::BR_JTadd)
902 OpNum = 2;
903 else if (Opcode == ARM::BR_JTm)
904 OpNum = 3;
905
906 const MachineOperand &MO1 = MI->getOperand(OpNum);
907 const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
908 unsigned JTI = MO1.getIndex();
909
Owen Anderson2fec6c52011-10-04 23:26:17 +0000910 // Tag the jump table appropriately for precise disassembly.
911 OutStreamer.EmitJumpTable32Region();
912
Jim Grosbacha2244cb2010-09-22 17:39:48 +0000913 // Emit a label for the jump table.
914 MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel2(JTI, MO2.getImm());
915 OutStreamer.EmitLabel(JTISymbol);
916
917 // Emit each entry of the table.
918 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
919 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
920 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
921
922 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
923 MachineBasicBlock *MBB = JTBBs[i];
924 // Construct an MCExpr for the entry. We want a value of the form:
925 // (BasicBlockAddr - TableBeginAddr)
926 //
927 // For example, a table with entries jumping to basic blocks BB0 and BB1
928 // would look like:
929 // LJTI_0_0:
930 // .word (LBB0 - LJTI_0_0)
931 // .word (LBB1 - LJTI_0_0)
932 const MCExpr *Expr = MCSymbolRefExpr::Create(MBB->getSymbol(), OutContext);
933
934 if (TM.getRelocationModel() == Reloc::PIC_)
935 Expr = MCBinaryExpr::CreateSub(Expr, MCSymbolRefExpr::Create(JTISymbol,
936 OutContext),
937 OutContext);
Jim Grosbachde982732011-08-31 22:23:09 +0000938 // If we're generating a table of Thumb addresses in static relocation
939 // model, we need to add one to keep interworking correctly.
940 else if (AFI->isThumbFunction())
941 Expr = MCBinaryExpr::CreateAdd(Expr, MCConstantExpr::Create(1,OutContext),
942 OutContext);
Jim Grosbacha2244cb2010-09-22 17:39:48 +0000943 OutStreamer.EmitValue(Expr, 4);
944 }
945}
946
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000947void ARMAsmPrinter::EmitJump2Table(const MachineInstr *MI) {
948 unsigned Opcode = MI->getOpcode();
949 int OpNum = (Opcode == ARM::t2BR_JT) ? 2 : 1;
950 const MachineOperand &MO1 = MI->getOperand(OpNum);
951 const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
952 unsigned JTI = MO1.getIndex();
953
954 // Emit a label for the jump table.
Owen Anderson2fec6c52011-10-04 23:26:17 +0000955 if (MI->getOpcode() == ARM::t2TBB_JT) {
956 OutStreamer.EmitJumpTable8Region();
957 } else if (MI->getOpcode() == ARM::t2TBH_JT) {
958 OutStreamer.EmitJumpTable16Region();
959 } else {
960 OutStreamer.EmitJumpTable32Region();
961 }
962
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000963 MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel2(JTI, MO2.getImm());
964 OutStreamer.EmitLabel(JTISymbol);
965
966 // Emit each entry of the table.
967 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
968 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
969 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000970 unsigned OffsetWidth = 4;
Jim Grosbachd092a872010-11-29 21:28:32 +0000971 if (MI->getOpcode() == ARM::t2TBB_JT)
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000972 OffsetWidth = 1;
Jim Grosbachd092a872010-11-29 21:28:32 +0000973 else if (MI->getOpcode() == ARM::t2TBH_JT)
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000974 OffsetWidth = 2;
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000975
976 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
977 MachineBasicBlock *MBB = JTBBs[i];
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000978 const MCExpr *MBBSymbolExpr = MCSymbolRefExpr::Create(MBB->getSymbol(),
979 OutContext);
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000980 // If this isn't a TBB or TBH, the entries are direct branch instructions.
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000981 if (OffsetWidth == 4) {
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000982 MCInst BrInst;
983 BrInst.setOpcode(ARM::t2B);
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000984 BrInst.addOperand(MCOperand::CreateExpr(MBBSymbolExpr));
Owen Anderson51f6a7a2011-09-09 21:48:23 +0000985 BrInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
986 BrInst.addOperand(MCOperand::CreateReg(0));
Jim Grosbach882ef2b2010-09-21 23:28:16 +0000987 OutStreamer.EmitInstruction(BrInst);
988 continue;
989 }
990 // Otherwise it's an offset from the dispatch instruction. Construct an
Jim Grosbach205a5fa2010-09-22 17:15:35 +0000991 // MCExpr for the entry. We want a value of the form:
992 // (BasicBlockAddr - TableBeginAddr) / 2
993 //
994 // For example, a TBB table with entries jumping to basic blocks BB0 and BB1
995 // would look like:
996 // LJTI_0_0:
997 // .byte (LBB0 - LJTI_0_0) / 2
998 // .byte (LBB1 - LJTI_0_0) / 2
999 const MCExpr *Expr =
1000 MCBinaryExpr::CreateSub(MBBSymbolExpr,
1001 MCSymbolRefExpr::Create(JTISymbol, OutContext),
1002 OutContext);
1003 Expr = MCBinaryExpr::CreateDiv(Expr, MCConstantExpr::Create(2, OutContext),
1004 OutContext);
1005 OutStreamer.EmitValue(Expr, OffsetWidth);
Jim Grosbach882ef2b2010-09-21 23:28:16 +00001006 }
1007}
1008
Jim Grosbach2d0f53b2010-09-28 17:05:56 +00001009void ARMAsmPrinter::PrintDebugValueComment(const MachineInstr *MI,
1010 raw_ostream &OS) {
1011 unsigned NOps = MI->getNumOperands();
1012 assert(NOps==4);
1013 OS << '\t' << MAI->getCommentString() << "DEBUG_VALUE: ";
1014 // cast away const; DIetc do not take const operands for some reason.
1015 DIVariable V(const_cast<MDNode *>(MI->getOperand(NOps-1).getMetadata()));
1016 OS << V.getName();
1017 OS << " <- ";
1018 // Frame address. Currently handles register +- offset only.
1019 assert(MI->getOperand(0).isReg() && MI->getOperand(1).isImm());
1020 OS << '['; printOperand(MI, 0, OS); OS << '+'; printOperand(MI, 1, OS);
1021 OS << ']';
1022 OS << "+";
1023 printOperand(MI, NOps-2, OS);
1024}
1025
Jim Grosbach40edf732010-12-14 21:10:47 +00001026static void populateADROperands(MCInst &Inst, unsigned Dest,
1027 const MCSymbol *Label,
1028 unsigned pred, unsigned ccreg,
1029 MCContext &Ctx) {
1030 const MCExpr *SymbolExpr = MCSymbolRefExpr::Create(Label, Ctx);
1031 Inst.addOperand(MCOperand::CreateReg(Dest));
1032 Inst.addOperand(MCOperand::CreateExpr(SymbolExpr));
1033 // Add predicate operands.
1034 Inst.addOperand(MCOperand::CreateImm(pred));
1035 Inst.addOperand(MCOperand::CreateReg(ccreg));
1036}
1037
Anton Korobeynikov4d728602011-01-01 20:38:38 +00001038void ARMAsmPrinter::EmitPatchedInstruction(const MachineInstr *MI,
1039 unsigned Opcode) {
1040 MCInst TmpInst;
1041
1042 // Emit the instruction as usual, just patch the opcode.
1043 LowerARMMachineInstrToMCInst(MI, TmpInst, *this);
1044 TmpInst.setOpcode(Opcode);
1045 OutStreamer.EmitInstruction(TmpInst);
1046}
1047
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001048void ARMAsmPrinter::EmitUnwindingInstruction(const MachineInstr *MI) {
1049 assert(MI->getFlag(MachineInstr::FrameSetup) &&
1050 "Only instruction which are involved into frame setup code are allowed");
1051
1052 const MachineFunction &MF = *MI->getParent()->getParent();
1053 const TargetRegisterInfo *RegInfo = MF.getTarget().getRegisterInfo();
Anton Korobeynikovb3fcc062011-03-05 18:43:55 +00001054 const ARMFunctionInfo &AFI = *MF.getInfo<ARMFunctionInfo>();
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001055
1056 unsigned FramePtr = RegInfo->getFrameRegister(MF);
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001057 unsigned Opc = MI->getOpcode();
Anton Korobeynikov7a764162011-03-05 18:43:43 +00001058 unsigned SrcReg, DstReg;
1059
Anton Korobeynikov3daccd82011-03-05 18:43:50 +00001060 if (Opc == ARM::tPUSH || Opc == ARM::tLDRpci) {
1061 // Two special cases:
1062 // 1) tPUSH does not have src/dst regs.
1063 // 2) for Thumb1 code we sometimes materialize the constant via constpool
1064 // load. Yes, this is pretty fragile, but for now I don't see better
1065 // way... :(
Anton Korobeynikov7a764162011-03-05 18:43:43 +00001066 SrcReg = DstReg = ARM::SP;
1067 } else {
Anton Korobeynikov3daccd82011-03-05 18:43:50 +00001068 SrcReg = MI->getOperand(1).getReg();
Anton Korobeynikov7a764162011-03-05 18:43:43 +00001069 DstReg = MI->getOperand(0).getReg();
1070 }
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001071
1072 // Try to figure out the unwinding opcode out of src / dst regs.
1073 if (MI->getDesc().mayStore()) {
1074 // Register saves.
1075 assert(DstReg == ARM::SP &&
1076 "Only stack pointer as a destination reg is supported");
1077
1078 SmallVector<unsigned, 4> RegList;
Anton Korobeynikov7a764162011-03-05 18:43:43 +00001079 // Skip src & dst reg, and pred ops.
1080 unsigned StartOp = 2 + 2;
1081 // Use all the operands.
1082 unsigned NumOffset = 0;
1083
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001084 switch (Opc) {
1085 default:
1086 MI->dump();
1087 assert(0 && "Unsupported opcode for unwinding information");
Anton Korobeynikov7a764162011-03-05 18:43:43 +00001088 case ARM::tPUSH:
1089 // Special case here: no src & dst reg, but two extra imp ops.
1090 StartOp = 2; NumOffset = 2;
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001091 case ARM::STMDB_UPD:
Anton Korobeynikov7a764162011-03-05 18:43:43 +00001092 case ARM::t2STMDB_UPD:
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001093 case ARM::VSTMDDB_UPD:
1094 assert(SrcReg == ARM::SP &&
1095 "Only stack pointer as a source reg is supported");
Anton Korobeynikov7a764162011-03-05 18:43:43 +00001096 for (unsigned i = StartOp, NumOps = MI->getNumOperands() - NumOffset;
1097 i != NumOps; ++i)
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001098 RegList.push_back(MI->getOperand(i).getReg());
1099 break;
Owen Anderson793e7962011-07-26 20:54:26 +00001100 case ARM::STR_PRE_IMM:
1101 case ARM::STR_PRE_REG:
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001102 assert(MI->getOperand(2).getReg() == ARM::SP &&
1103 "Only stack pointer as a source reg is supported");
1104 RegList.push_back(SrcReg);
1105 break;
1106 }
1107 OutStreamer.EmitRegSave(RegList, Opc == ARM::VSTMDDB_UPD);
1108 } else {
1109 // Changes of stack / frame pointer.
1110 if (SrcReg == ARM::SP) {
1111 int64_t Offset = 0;
1112 switch (Opc) {
1113 default:
1114 MI->dump();
1115 assert(0 && "Unsupported opcode for unwinding information");
1116 case ARM::MOVr:
1117 Offset = 0;
1118 break;
1119 case ARM::ADDri:
1120 Offset = -MI->getOperand(2).getImm();
1121 break;
1122 case ARM::SUBri:
Jim Grosbachf6fd9092011-06-29 23:25:04 +00001123 Offset = MI->getOperand(2).getImm();
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001124 break;
Anton Korobeynikov7a764162011-03-05 18:43:43 +00001125 case ARM::tSUBspi:
Jim Grosbachf6fd9092011-06-29 23:25:04 +00001126 Offset = MI->getOperand(2).getImm()*4;
Anton Korobeynikov7a764162011-03-05 18:43:43 +00001127 break;
1128 case ARM::tADDspi:
1129 case ARM::tADDrSPi:
1130 Offset = -MI->getOperand(2).getImm()*4;
1131 break;
Anton Korobeynikovb3fcc062011-03-05 18:43:55 +00001132 case ARM::tLDRpci: {
1133 // Grab the constpool index and check, whether it corresponds to
1134 // original or cloned constpool entry.
1135 unsigned CPI = MI->getOperand(1).getIndex();
1136 const MachineConstantPool *MCP = MF.getConstantPool();
1137 if (CPI >= MCP->getConstants().size())
1138 CPI = AFI.getOriginalCPIdx(CPI);
1139 assert(CPI != -1U && "Invalid constpool index");
1140
1141 // Derive the actual offset.
1142 const MachineConstantPoolEntry &CPE = MCP->getConstants()[CPI];
1143 assert(!CPE.isMachineConstantPoolEntry() && "Invalid constpool entry");
1144 // FIXME: Check for user, it should be "add" instruction!
1145 Offset = -cast<ConstantInt>(CPE.Val.ConstVal)->getSExtValue();
Anton Korobeynikov3daccd82011-03-05 18:43:50 +00001146 break;
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001147 }
Anton Korobeynikovb3fcc062011-03-05 18:43:55 +00001148 }
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001149
1150 if (DstReg == FramePtr && FramePtr != ARM::SP)
Anton Korobeynikove5163792011-03-05 18:44:00 +00001151 // Set-up of the frame pointer. Positive values correspond to "add"
1152 // instruction.
1153 OutStreamer.EmitSetFP(FramePtr, ARM::SP, -Offset);
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001154 else if (DstReg == ARM::SP) {
Anton Korobeynikove5163792011-03-05 18:44:00 +00001155 // Change of SP by an offset. Positive values correspond to "sub"
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001156 // instruction.
1157 OutStreamer.EmitPad(Offset);
1158 } else {
1159 MI->dump();
1160 assert(0 && "Unsupported opcode for unwinding information");
1161 }
1162 } else if (DstReg == ARM::SP) {
1163 // FIXME: .movsp goes here
1164 MI->dump();
1165 assert(0 && "Unsupported opcode for unwinding information");
1166 }
1167 else {
1168 MI->dump();
1169 assert(0 && "Unsupported opcode for unwinding information");
1170 }
1171 }
1172}
1173
1174extern cl::opt<bool> EnableARMEHABI;
1175
Jim Grosbach53e3fc42011-07-08 17:40:42 +00001176// Simple pseudo-instructions have their lowering (with expansion to real
1177// instructions) auto-generated.
1178#include "ARMGenMCPseudoLowering.inc"
1179
Jim Grosbachb454cda2010-09-29 15:23:40 +00001180void ARMAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Owen Anderson2fec6c52011-10-04 23:26:17 +00001181 if (MI->getOpcode() != ARM::CONSTPOOL_ENTRY)
1182 OutStreamer.EmitCodeRegion();
1183
Jim Grosbach5aa29a02011-08-23 21:32:34 +00001184 // Emit unwinding stuff for frame-related instructions
1185 if (EnableARMEHABI && MI->getFlag(MachineInstr::FrameSetup))
1186 EmitUnwindingInstruction(MI);
1187
Jim Grosbach53e3fc42011-07-08 17:40:42 +00001188 // Do any auto-generated pseudo lowerings.
1189 if (emitPseudoExpansionLowering(OutStreamer, MI))
1190 return;
1191
Andrew Trick3be654f2011-09-21 02:20:46 +00001192 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
1193 "Pseudo flag setting opcode should be expanded early");
1194
Jim Grosbach53e3fc42011-07-08 17:40:42 +00001195 // Check for manual lowerings.
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001196 unsigned Opc = MI->getOpcode();
1197 switch (Opc) {
Chris Lattner112f2392010-11-14 20:31:06 +00001198 case ARM::t2MOVi32imm: assert(0 && "Should be lowered by thumb2it pass");
Jim Grosbach2d0f53b2010-09-28 17:05:56 +00001199 case ARM::DBG_VALUE: {
1200 if (isVerbose() && OutStreamer.hasRawTextSupport()) {
1201 SmallString<128> TmpStr;
1202 raw_svector_ostream OS(TmpStr);
1203 PrintDebugValueComment(MI, OS);
1204 OutStreamer.EmitRawText(StringRef(OS.str()));
1205 }
1206 return;
1207 }
Jim Grosbach40edf732010-12-14 21:10:47 +00001208 case ARM::LEApcrel:
Jim Grosbachd40963c2010-12-14 22:28:03 +00001209 case ARM::tLEApcrel:
Jim Grosbach40edf732010-12-14 21:10:47 +00001210 case ARM::t2LEApcrel: {
Jim Grosbachdff84b02010-12-02 00:28:45 +00001211 // FIXME: Need to also handle globals and externals
Jim Grosbachdff84b02010-12-02 00:28:45 +00001212 MCInst TmpInst;
Jim Grosbachd40963c2010-12-14 22:28:03 +00001213 TmpInst.setOpcode(MI->getOpcode() == ARM::t2LEApcrel ? ARM::t2ADR
1214 : (MI->getOpcode() == ARM::tLEApcrel ? ARM::tADR
1215 : ARM::ADR));
Jim Grosbach40edf732010-12-14 21:10:47 +00001216 populateADROperands(TmpInst, MI->getOperand(0).getReg(),
1217 GetCPISymbol(MI->getOperand(1).getIndex()),
1218 MI->getOperand(2).getImm(), MI->getOperand(3).getReg(),
1219 OutContext);
Jim Grosbachdff84b02010-12-02 00:28:45 +00001220 OutStreamer.EmitInstruction(TmpInst);
1221 return;
1222 }
Jim Grosbachd40963c2010-12-14 22:28:03 +00001223 case ARM::LEApcrelJT:
1224 case ARM::tLEApcrelJT:
1225 case ARM::t2LEApcrelJT: {
Jim Grosbach5d14f9b2010-12-01 19:47:31 +00001226 MCInst TmpInst;
Jim Grosbachd40963c2010-12-14 22:28:03 +00001227 TmpInst.setOpcode(MI->getOpcode() == ARM::t2LEApcrelJT ? ARM::t2ADR
1228 : (MI->getOpcode() == ARM::tLEApcrelJT ? ARM::tADR
1229 : ARM::ADR));
Jim Grosbach40edf732010-12-14 21:10:47 +00001230 populateADROperands(TmpInst, MI->getOperand(0).getReg(),
1231 GetARMJTIPICJumpTableLabel2(MI->getOperand(1).getIndex(),
1232 MI->getOperand(2).getImm()),
1233 MI->getOperand(3).getImm(), MI->getOperand(4).getReg(),
1234 OutContext);
Jim Grosbach5d14f9b2010-12-01 19:47:31 +00001235 OutStreamer.EmitInstruction(TmpInst);
1236 return;
1237 }
Jim Grosbachf859a542011-03-12 00:45:26 +00001238 // Darwin call instructions are just normal call instructions with different
1239 // clobber semantics (they clobber R9).
Jim Grosbacha0d2c8a2010-11-30 18:30:19 +00001240 case ARM::BXr9_CALL:
1241 case ARM::BX_CALL: {
1242 {
1243 MCInst TmpInst;
1244 TmpInst.setOpcode(ARM::MOVr);
1245 TmpInst.addOperand(MCOperand::CreateReg(ARM::LR));
1246 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1247 // Add predicate operands.
1248 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1249 TmpInst.addOperand(MCOperand::CreateReg(0));
1250 // Add 's' bit operand (always reg0 for this)
1251 TmpInst.addOperand(MCOperand::CreateReg(0));
1252 OutStreamer.EmitInstruction(TmpInst);
1253 }
1254 {
1255 MCInst TmpInst;
1256 TmpInst.setOpcode(ARM::BX);
1257 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1258 OutStreamer.EmitInstruction(TmpInst);
1259 }
1260 return;
1261 }
Cameron Zwarichad70f6d2011-05-25 21:53:50 +00001262 case ARM::tBXr9_CALL:
1263 case ARM::tBX_CALL: {
1264 {
1265 MCInst TmpInst;
1266 TmpInst.setOpcode(ARM::tMOVr);
1267 TmpInst.addOperand(MCOperand::CreateReg(ARM::LR));
1268 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
Jim Grosbach63b46fa2011-06-30 22:10:46 +00001269 // Add predicate operands.
1270 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1271 TmpInst.addOperand(MCOperand::CreateReg(0));
Cameron Zwarichad70f6d2011-05-25 21:53:50 +00001272 OutStreamer.EmitInstruction(TmpInst);
1273 }
1274 {
1275 MCInst TmpInst;
1276 TmpInst.setOpcode(ARM::tBX);
1277 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1278 // Add predicate operands.
1279 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1280 TmpInst.addOperand(MCOperand::CreateReg(0));
1281 OutStreamer.EmitInstruction(TmpInst);
1282 }
1283 return;
1284 }
Jim Grosbacha0d2c8a2010-11-30 18:30:19 +00001285 case ARM::BMOVPCRXr9_CALL:
1286 case ARM::BMOVPCRX_CALL: {
1287 {
1288 MCInst TmpInst;
1289 TmpInst.setOpcode(ARM::MOVr);
1290 TmpInst.addOperand(MCOperand::CreateReg(ARM::LR));
1291 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1292 // Add predicate operands.
1293 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1294 TmpInst.addOperand(MCOperand::CreateReg(0));
1295 // Add 's' bit operand (always reg0 for this)
1296 TmpInst.addOperand(MCOperand::CreateReg(0));
1297 OutStreamer.EmitInstruction(TmpInst);
1298 }
1299 {
1300 MCInst TmpInst;
1301 TmpInst.setOpcode(ARM::MOVr);
1302 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1303 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1304 // Add predicate operands.
1305 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1306 TmpInst.addOperand(MCOperand::CreateReg(0));
1307 // Add 's' bit operand (always reg0 for this)
1308 TmpInst.addOperand(MCOperand::CreateReg(0));
1309 OutStreamer.EmitInstruction(TmpInst);
1310 }
1311 return;
1312 }
Evan Cheng53519f02011-01-21 18:55:51 +00001313 case ARM::MOVi16_ga_pcrel:
1314 case ARM::t2MOVi16_ga_pcrel: {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001315 MCInst TmpInst;
Evan Cheng53519f02011-01-21 18:55:51 +00001316 TmpInst.setOpcode(Opc == ARM::MOVi16_ga_pcrel? ARM::MOVi16 : ARM::t2MOVi16);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001317 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1318
Evan Cheng53519f02011-01-21 18:55:51 +00001319 unsigned TF = MI->getOperand(1).getTargetFlags();
1320 bool isPIC = TF == ARMII::MO_LO16_NONLAZY_PIC;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001321 const GlobalValue *GV = MI->getOperand(1).getGlobal();
1322 MCSymbol *GVSym = GetARMGVSymbol(GV);
1323 const MCExpr *GVSymExpr = MCSymbolRefExpr::Create(GVSym, OutContext);
Evan Cheng53519f02011-01-21 18:55:51 +00001324 if (isPIC) {
1325 MCSymbol *LabelSym = getPICLabel(MAI->getPrivateGlobalPrefix(),
1326 getFunctionNumber(),
1327 MI->getOperand(2).getImm(), OutContext);
1328 const MCExpr *LabelSymExpr= MCSymbolRefExpr::Create(LabelSym, OutContext);
1329 unsigned PCAdj = (Opc == ARM::MOVi16_ga_pcrel) ? 8 : 4;
1330 const MCExpr *PCRelExpr =
1331 ARMMCExpr::CreateLower16(MCBinaryExpr::CreateSub(GVSymExpr,
1332 MCBinaryExpr::CreateAdd(LabelSymExpr,
1333 MCConstantExpr::Create(PCAdj, OutContext),
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001334 OutContext), OutContext), OutContext);
Evan Cheng53519f02011-01-21 18:55:51 +00001335 TmpInst.addOperand(MCOperand::CreateExpr(PCRelExpr));
1336 } else {
1337 const MCExpr *RefExpr= ARMMCExpr::CreateLower16(GVSymExpr, OutContext);
1338 TmpInst.addOperand(MCOperand::CreateExpr(RefExpr));
1339 }
1340
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001341 // Add predicate operands.
1342 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1343 TmpInst.addOperand(MCOperand::CreateReg(0));
1344 // Add 's' bit operand (always reg0 for this)
1345 TmpInst.addOperand(MCOperand::CreateReg(0));
1346 OutStreamer.EmitInstruction(TmpInst);
1347 return;
1348 }
Evan Cheng53519f02011-01-21 18:55:51 +00001349 case ARM::MOVTi16_ga_pcrel:
1350 case ARM::t2MOVTi16_ga_pcrel: {
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001351 MCInst TmpInst;
Evan Cheng53519f02011-01-21 18:55:51 +00001352 TmpInst.setOpcode(Opc == ARM::MOVTi16_ga_pcrel
1353 ? ARM::MOVTi16 : ARM::t2MOVTi16);
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001354 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1355 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
1356
Evan Cheng53519f02011-01-21 18:55:51 +00001357 unsigned TF = MI->getOperand(2).getTargetFlags();
1358 bool isPIC = TF == ARMII::MO_HI16_NONLAZY_PIC;
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001359 const GlobalValue *GV = MI->getOperand(2).getGlobal();
1360 MCSymbol *GVSym = GetARMGVSymbol(GV);
1361 const MCExpr *GVSymExpr = MCSymbolRefExpr::Create(GVSym, OutContext);
Evan Cheng53519f02011-01-21 18:55:51 +00001362 if (isPIC) {
1363 MCSymbol *LabelSym = getPICLabel(MAI->getPrivateGlobalPrefix(),
1364 getFunctionNumber(),
1365 MI->getOperand(3).getImm(), OutContext);
1366 const MCExpr *LabelSymExpr= MCSymbolRefExpr::Create(LabelSym, OutContext);
1367 unsigned PCAdj = (Opc == ARM::MOVTi16_ga_pcrel) ? 8 : 4;
1368 const MCExpr *PCRelExpr =
1369 ARMMCExpr::CreateUpper16(MCBinaryExpr::CreateSub(GVSymExpr,
1370 MCBinaryExpr::CreateAdd(LabelSymExpr,
1371 MCConstantExpr::Create(PCAdj, OutContext),
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001372 OutContext), OutContext), OutContext);
Evan Cheng53519f02011-01-21 18:55:51 +00001373 TmpInst.addOperand(MCOperand::CreateExpr(PCRelExpr));
1374 } else {
1375 const MCExpr *RefExpr= ARMMCExpr::CreateUpper16(GVSymExpr, OutContext);
1376 TmpInst.addOperand(MCOperand::CreateExpr(RefExpr));
1377 }
Evan Cheng5de5d4b2011-01-17 08:03:18 +00001378 // Add predicate operands.
1379 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1380 TmpInst.addOperand(MCOperand::CreateReg(0));
1381 // Add 's' bit operand (always reg0 for this)
1382 TmpInst.addOperand(MCOperand::CreateReg(0));
1383 OutStreamer.EmitInstruction(TmpInst);
1384 return;
1385 }
Jim Grosbachfbd18732010-09-17 23:41:53 +00001386 case ARM::tPICADD: {
1387 // This is a pseudo op for a label + instruction sequence, which looks like:
1388 // LPC0:
1389 // add r0, pc
1390 // This adds the address of LPC0 to r0.
1391
1392 // Emit the label.
Jim Grosbach988ce092010-09-18 00:05:05 +00001393 OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(),
1394 getFunctionNumber(), MI->getOperand(2).getImm(),
1395 OutContext));
Jim Grosbachfbd18732010-09-17 23:41:53 +00001396
1397 // Form and emit the add.
1398 MCInst AddInst;
1399 AddInst.setOpcode(ARM::tADDhirr);
1400 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1401 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1402 AddInst.addOperand(MCOperand::CreateReg(ARM::PC));
1403 // Add predicate operands.
1404 AddInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1405 AddInst.addOperand(MCOperand::CreateReg(0));
1406 OutStreamer.EmitInstruction(AddInst);
1407 return;
1408 }
Jim Grosbacha3fbadf2010-09-30 19:53:58 +00001409 case ARM::PICADD: {
Chris Lattner4d152222009-10-19 22:23:04 +00001410 // This is a pseudo op for a label + instruction sequence, which looks like:
1411 // LPC0:
1412 // add r0, pc, r0
1413 // This adds the address of LPC0 to r0.
Jim Grosbachb0739b72010-09-02 01:02:06 +00001414
Chris Lattner4d152222009-10-19 22:23:04 +00001415 // Emit the label.
Jim Grosbach988ce092010-09-18 00:05:05 +00001416 OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(),
1417 getFunctionNumber(), MI->getOperand(2).getImm(),
1418 OutContext));
Jim Grosbachb0739b72010-09-02 01:02:06 +00001419
Jim Grosbachf3f09522010-09-14 21:05:34 +00001420 // Form and emit the add.
Chris Lattner4d152222009-10-19 22:23:04 +00001421 MCInst AddInst;
1422 AddInst.setOpcode(ARM::ADDrr);
1423 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1424 AddInst.addOperand(MCOperand::CreateReg(ARM::PC));
1425 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
Jim Grosbach5b46d622010-09-14 21:28:17 +00001426 // Add predicate operands.
1427 AddInst.addOperand(MCOperand::CreateImm(MI->getOperand(3).getImm()));
1428 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(4).getReg()));
1429 // Add 's' bit operand (always reg0 for this)
1430 AddInst.addOperand(MCOperand::CreateReg(0));
Chris Lattner850d2e22010-02-03 01:16:28 +00001431 OutStreamer.EmitInstruction(AddInst);
Chris Lattner4d152222009-10-19 22:23:04 +00001432 return;
1433 }
Jim Grosbacha28abbe2010-09-17 16:25:52 +00001434 case ARM::PICSTR:
1435 case ARM::PICSTRB:
1436 case ARM::PICSTRH:
1437 case ARM::PICLDR:
1438 case ARM::PICLDRB:
1439 case ARM::PICLDRH:
1440 case ARM::PICLDRSB:
1441 case ARM::PICLDRSH: {
Jim Grosbachb74ca9d2010-09-16 17:43:25 +00001442 // This is a pseudo op for a label + instruction sequence, which looks like:
1443 // LPC0:
Jim Grosbacha28abbe2010-09-17 16:25:52 +00001444 // OP r0, [pc, r0]
Jim Grosbachb74ca9d2010-09-16 17:43:25 +00001445 // The LCP0 label is referenced by a constant pool entry in order to get
1446 // a PC-relative address at the ldr instruction.
1447
1448 // Emit the label.
Jim Grosbach988ce092010-09-18 00:05:05 +00001449 OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(),
1450 getFunctionNumber(), MI->getOperand(2).getImm(),
1451 OutContext));
Jim Grosbachb74ca9d2010-09-16 17:43:25 +00001452
1453 // Form and emit the load
Jim Grosbacha28abbe2010-09-17 16:25:52 +00001454 unsigned Opcode;
1455 switch (MI->getOpcode()) {
1456 default:
1457 llvm_unreachable("Unexpected opcode!");
Jim Grosbach7e3383c2010-10-27 23:12:14 +00001458 case ARM::PICSTR: Opcode = ARM::STRrs; break;
1459 case ARM::PICSTRB: Opcode = ARM::STRBrs; break;
Jim Grosbacha28abbe2010-09-17 16:25:52 +00001460 case ARM::PICSTRH: Opcode = ARM::STRH; break;
Jim Grosbach3e556122010-10-26 22:37:02 +00001461 case ARM::PICLDR: Opcode = ARM::LDRrs; break;
Jim Grosbachc1d30212010-10-27 00:19:44 +00001462 case ARM::PICLDRB: Opcode = ARM::LDRBrs; break;
Jim Grosbacha28abbe2010-09-17 16:25:52 +00001463 case ARM::PICLDRH: Opcode = ARM::LDRH; break;
1464 case ARM::PICLDRSB: Opcode = ARM::LDRSB; break;
1465 case ARM::PICLDRSH: Opcode = ARM::LDRSH; break;
1466 }
1467 MCInst LdStInst;
1468 LdStInst.setOpcode(Opcode);
1469 LdStInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1470 LdStInst.addOperand(MCOperand::CreateReg(ARM::PC));
1471 LdStInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
1472 LdStInst.addOperand(MCOperand::CreateImm(0));
Jim Grosbachb74ca9d2010-09-16 17:43:25 +00001473 // Add predicate operands.
Jim Grosbacha28abbe2010-09-17 16:25:52 +00001474 LdStInst.addOperand(MCOperand::CreateImm(MI->getOperand(3).getImm()));
1475 LdStInst.addOperand(MCOperand::CreateReg(MI->getOperand(4).getReg()));
1476 OutStreamer.EmitInstruction(LdStInst);
Jim Grosbachb74ca9d2010-09-16 17:43:25 +00001477
1478 return;
1479 }
Jim Grosbacha3fbadf2010-09-30 19:53:58 +00001480 case ARM::CONSTPOOL_ENTRY: {
Chris Lattnera70e6442009-10-19 22:33:05 +00001481 /// CONSTPOOL_ENTRY - This instruction represents a floating constant pool
1482 /// in the function. The first operand is the ID# for this instruction, the
1483 /// second is the index into the MachineConstantPool that this is, the third
1484 /// is the size in bytes of this constant pool entry.
1485 unsigned LabelId = (unsigned)MI->getOperand(0).getImm();
1486 unsigned CPIdx = (unsigned)MI->getOperand(1).getIndex();
1487
1488 EmitAlignment(2);
Owen Anderson2fec6c52011-10-04 23:26:17 +00001489
1490 // Mark the constant pool entry as data if we're not already in a data
1491 // region.
1492 OutStreamer.EmitDataRegion();
Chris Lattner1b46f432010-01-23 07:00:21 +00001493 OutStreamer.EmitLabel(GetCPISymbol(LabelId));
Chris Lattnera70e6442009-10-19 22:33:05 +00001494
1495 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPIdx];
1496 if (MCPE.isMachineConstantPoolEntry())
1497 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
1498 else
1499 EmitGlobalConstant(MCPE.Val.ConstVal);
Chris Lattnera70e6442009-10-19 22:33:05 +00001500 return;
1501 }
Jim Grosbach882ef2b2010-09-21 23:28:16 +00001502 case ARM::t2BR_JT: {
1503 // Lower and emit the instruction itself, then the jump table following it.
1504 MCInst TmpInst;
Jim Grosbach2a7b41b2011-06-30 23:38:17 +00001505 TmpInst.setOpcode(ARM::tMOVr);
Jim Grosbach5ca66692010-11-29 22:37:40 +00001506 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1507 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1508 // Add predicate operands.
1509 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1510 TmpInst.addOperand(MCOperand::CreateReg(0));
Jim Grosbach882ef2b2010-09-21 23:28:16 +00001511 OutStreamer.EmitInstruction(TmpInst);
Jim Grosbach5ca66692010-11-29 22:37:40 +00001512 // Output the data for the jump table itself
1513 EmitJump2Table(MI);
1514 return;
1515 }
1516 case ARM::t2TBB_JT: {
1517 // Lower and emit the instruction itself, then the jump table following it.
1518 MCInst TmpInst;
1519
1520 TmpInst.setOpcode(ARM::t2TBB);
1521 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1522 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1523 // Add predicate operands.
1524 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1525 TmpInst.addOperand(MCOperand::CreateReg(0));
1526 OutStreamer.EmitInstruction(TmpInst);
1527 // Output the data for the jump table itself
1528 EmitJump2Table(MI);
1529 // Make sure the next instruction is 2-byte aligned.
1530 EmitAlignment(1);
1531 return;
1532 }
1533 case ARM::t2TBH_JT: {
1534 // Lower and emit the instruction itself, then the jump table following it.
1535 MCInst TmpInst;
1536
1537 TmpInst.setOpcode(ARM::t2TBH);
1538 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1539 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1540 // Add predicate operands.
1541 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1542 TmpInst.addOperand(MCOperand::CreateReg(0));
1543 OutStreamer.EmitInstruction(TmpInst);
1544 // Output the data for the jump table itself
Jim Grosbach882ef2b2010-09-21 23:28:16 +00001545 EmitJump2Table(MI);
1546 return;
1547 }
Jim Grosbachf1aa47d2010-11-29 19:32:47 +00001548 case ARM::tBR_JTr:
Jim Grosbach2dc77682010-11-29 18:37:44 +00001549 case ARM::BR_JTr: {
1550 // Lower and emit the instruction itself, then the jump table following it.
1551 // mov pc, target
1552 MCInst TmpInst;
Jim Grosbach5ca66692010-11-29 22:37:40 +00001553 unsigned Opc = MI->getOpcode() == ARM::BR_JTr ?
Jim Grosbach2a7b41b2011-06-30 23:38:17 +00001554 ARM::MOVr : ARM::tMOVr;
Jim Grosbachf1aa47d2010-11-29 19:32:47 +00001555 TmpInst.setOpcode(Opc);
Jim Grosbach2dc77682010-11-29 18:37:44 +00001556 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1557 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1558 // Add predicate operands.
1559 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1560 TmpInst.addOperand(MCOperand::CreateReg(0));
Jim Grosbacha0d2c8a2010-11-30 18:30:19 +00001561 // Add 's' bit operand (always reg0 for this)
1562 if (Opc == ARM::MOVr)
1563 TmpInst.addOperand(MCOperand::CreateReg(0));
Jim Grosbach2dc77682010-11-29 18:37:44 +00001564 OutStreamer.EmitInstruction(TmpInst);
1565
Jim Grosbachf1aa47d2010-11-29 19:32:47 +00001566 // Make sure the Thumb jump table is 4-byte aligned.
Jim Grosbach2a7b41b2011-06-30 23:38:17 +00001567 if (Opc == ARM::tMOVr)
Jim Grosbachf1aa47d2010-11-29 19:32:47 +00001568 EmitAlignment(2);
1569
Jim Grosbach2dc77682010-11-29 18:37:44 +00001570 // Output the data for the jump table itself
1571 EmitJumpTable(MI);
1572 return;
1573 }
1574 case ARM::BR_JTm: {
1575 // Lower and emit the instruction itself, then the jump table following it.
1576 // ldr pc, target
1577 MCInst TmpInst;
1578 if (MI->getOperand(1).getReg() == 0) {
1579 // literal offset
1580 TmpInst.setOpcode(ARM::LDRi12);
1581 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1582 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1583 TmpInst.addOperand(MCOperand::CreateImm(MI->getOperand(2).getImm()));
1584 } else {
1585 TmpInst.setOpcode(ARM::LDRrs);
1586 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1587 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1588 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
1589 TmpInst.addOperand(MCOperand::CreateImm(0));
1590 }
1591 // Add predicate operands.
1592 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1593 TmpInst.addOperand(MCOperand::CreateReg(0));
1594 OutStreamer.EmitInstruction(TmpInst);
1595
1596 // Output the data for the jump table itself
Jim Grosbacha2244cb2010-09-22 17:39:48 +00001597 EmitJumpTable(MI);
1598 return;
1599 }
Jim Grosbachf8dabac2010-11-17 21:05:55 +00001600 case ARM::BR_JTadd: {
1601 // Lower and emit the instruction itself, then the jump table following it.
1602 // add pc, target, idx
Jim Grosbach2dc77682010-11-29 18:37:44 +00001603 MCInst TmpInst;
1604 TmpInst.setOpcode(ARM::ADDrr);
1605 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1606 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
1607 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
Jim Grosbachf8dabac2010-11-17 21:05:55 +00001608 // Add predicate operands.
Jim Grosbach2dc77682010-11-29 18:37:44 +00001609 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1610 TmpInst.addOperand(MCOperand::CreateReg(0));
Jim Grosbachf8dabac2010-11-17 21:05:55 +00001611 // Add 's' bit operand (always reg0 for this)
Jim Grosbach2dc77682010-11-29 18:37:44 +00001612 TmpInst.addOperand(MCOperand::CreateReg(0));
1613 OutStreamer.EmitInstruction(TmpInst);
Jim Grosbachf8dabac2010-11-17 21:05:55 +00001614
1615 // Output the data for the jump table itself
1616 EmitJumpTable(MI);
1617 return;
1618 }
Jim Grosbach2e6ae132010-09-23 18:05:37 +00001619 case ARM::TRAP: {
1620 // Non-Darwin binutils don't yet support the "trap" mnemonic.
1621 // FIXME: Remove this special case when they do.
1622 if (!Subtarget->isTargetDarwin()) {
Jim Grosbach78890f42010-10-01 23:21:38 +00001623 //.long 0xe7ffdefe @ trap
Jim Grosbachb2dda4b2010-09-23 19:42:17 +00001624 uint32_t Val = 0xe7ffdefeUL;
Jim Grosbach2e6ae132010-09-23 18:05:37 +00001625 OutStreamer.AddComment("trap");
1626 OutStreamer.EmitIntValue(Val, 4);
1627 return;
1628 }
1629 break;
1630 }
1631 case ARM::tTRAP: {
1632 // Non-Darwin binutils don't yet support the "trap" mnemonic.
1633 // FIXME: Remove this special case when they do.
1634 if (!Subtarget->isTargetDarwin()) {
Jim Grosbach78890f42010-10-01 23:21:38 +00001635 //.short 57086 @ trap
Benjamin Kramerc8ab9eb2010-09-23 18:57:26 +00001636 uint16_t Val = 0xdefe;
Jim Grosbach2e6ae132010-09-23 18:05:37 +00001637 OutStreamer.AddComment("trap");
1638 OutStreamer.EmitIntValue(Val, 2);
1639 return;
1640 }
1641 break;
1642 }
Jim Grosbach433a5782010-09-24 20:47:58 +00001643 case ARM::t2Int_eh_sjlj_setjmp:
1644 case ARM::t2Int_eh_sjlj_setjmp_nofp:
Jim Grosbacha3fbadf2010-09-30 19:53:58 +00001645 case ARM::tInt_eh_sjlj_setjmp: {
Jim Grosbach433a5782010-09-24 20:47:58 +00001646 // Two incoming args: GPR:$src, GPR:$val
1647 // mov $val, pc
1648 // adds $val, #7
1649 // str $val, [$src, #4]
1650 // movs r0, #0
1651 // b 1f
1652 // movs r0, #1
1653 // 1:
1654 unsigned SrcReg = MI->getOperand(0).getReg();
1655 unsigned ValReg = MI->getOperand(1).getReg();
1656 MCSymbol *Label = GetARMSJLJEHLabel();
1657 {
1658 MCInst TmpInst;
Jim Grosbach2a7b41b2011-06-30 23:38:17 +00001659 TmpInst.setOpcode(ARM::tMOVr);
Jim Grosbach433a5782010-09-24 20:47:58 +00001660 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1661 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
Jim Grosbach63b46fa2011-06-30 22:10:46 +00001662 // Predicate.
1663 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1664 TmpInst.addOperand(MCOperand::CreateReg(0));
Jim Grosbach433a5782010-09-24 20:47:58 +00001665 OutStreamer.AddComment("eh_setjmp begin");
1666 OutStreamer.EmitInstruction(TmpInst);
1667 }
1668 {
1669 MCInst TmpInst;
1670 TmpInst.setOpcode(ARM::tADDi3);
1671 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1672 // 's' bit operand
1673 TmpInst.addOperand(MCOperand::CreateReg(ARM::CPSR));
1674 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1675 TmpInst.addOperand(MCOperand::CreateImm(7));
1676 // Predicate.
1677 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1678 TmpInst.addOperand(MCOperand::CreateReg(0));
1679 OutStreamer.EmitInstruction(TmpInst);
1680 }
1681 {
1682 MCInst TmpInst;
Bill Wendlingf4caf692010-12-14 03:36:38 +00001683 TmpInst.setOpcode(ARM::tSTRi);
Jim Grosbach433a5782010-09-24 20:47:58 +00001684 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1685 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
1686 // The offset immediate is #4. The operand value is scaled by 4 for the
1687 // tSTR instruction.
1688 TmpInst.addOperand(MCOperand::CreateImm(1));
Jim Grosbach433a5782010-09-24 20:47:58 +00001689 // Predicate.
1690 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1691 TmpInst.addOperand(MCOperand::CreateReg(0));
1692 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(0));
1700 // Predicate.
1701 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1702 TmpInst.addOperand(MCOperand::CreateReg(0));
1703 OutStreamer.EmitInstruction(TmpInst);
1704 }
1705 {
1706 const MCExpr *SymbolExpr = MCSymbolRefExpr::Create(Label, OutContext);
1707 MCInst TmpInst;
1708 TmpInst.setOpcode(ARM::tB);
1709 TmpInst.addOperand(MCOperand::CreateExpr(SymbolExpr));
Owen Anderson51f6a7a2011-09-09 21:48:23 +00001710 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1711 TmpInst.addOperand(MCOperand::CreateReg(0));
Jim Grosbach433a5782010-09-24 20:47:58 +00001712 OutStreamer.EmitInstruction(TmpInst);
1713 }
1714 {
1715 MCInst TmpInst;
1716 TmpInst.setOpcode(ARM::tMOVi8);
1717 TmpInst.addOperand(MCOperand::CreateReg(ARM::R0));
1718 TmpInst.addOperand(MCOperand::CreateReg(ARM::CPSR));
1719 TmpInst.addOperand(MCOperand::CreateImm(1));
1720 // Predicate.
1721 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1722 TmpInst.addOperand(MCOperand::CreateReg(0));
1723 OutStreamer.AddComment("eh_setjmp end");
1724 OutStreamer.EmitInstruction(TmpInst);
1725 }
1726 OutStreamer.EmitLabel(Label);
1727 return;
1728 }
1729
Jim Grosbach45390082010-09-23 23:33:56 +00001730 case ARM::Int_eh_sjlj_setjmp_nofp:
Jim Grosbacha3fbadf2010-09-30 19:53:58 +00001731 case ARM::Int_eh_sjlj_setjmp: {
Jim Grosbach45390082010-09-23 23:33:56 +00001732 // Two incoming args: GPR:$src, GPR:$val
1733 // add $val, pc, #8
1734 // str $val, [$src, #+4]
1735 // mov r0, #0
1736 // add pc, pc, #0
1737 // mov r0, #1
1738 unsigned SrcReg = MI->getOperand(0).getReg();
1739 unsigned ValReg = MI->getOperand(1).getReg();
1740
1741 {
1742 MCInst TmpInst;
1743 TmpInst.setOpcode(ARM::ADDri);
1744 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1745 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1746 TmpInst.addOperand(MCOperand::CreateImm(8));
1747 // Predicate.
1748 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1749 TmpInst.addOperand(MCOperand::CreateReg(0));
1750 // 's' bit operand (always reg0 for this).
1751 TmpInst.addOperand(MCOperand::CreateReg(0));
1752 OutStreamer.AddComment("eh_setjmp begin");
1753 OutStreamer.EmitInstruction(TmpInst);
1754 }
1755 {
1756 MCInst TmpInst;
Jim Grosbach7e3383c2010-10-27 23:12:14 +00001757 TmpInst.setOpcode(ARM::STRi12);
Jim Grosbach45390082010-09-23 23:33:56 +00001758 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1759 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
Jim Grosbach45390082010-09-23 23:33:56 +00001760 TmpInst.addOperand(MCOperand::CreateImm(4));
1761 // Predicate.
1762 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1763 TmpInst.addOperand(MCOperand::CreateReg(0));
1764 OutStreamer.EmitInstruction(TmpInst);
1765 }
1766 {
1767 MCInst TmpInst;
1768 TmpInst.setOpcode(ARM::MOVi);
1769 TmpInst.addOperand(MCOperand::CreateReg(ARM::R0));
1770 TmpInst.addOperand(MCOperand::CreateImm(0));
1771 // Predicate.
1772 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1773 TmpInst.addOperand(MCOperand::CreateReg(0));
1774 // 's' bit operand (always reg0 for this).
1775 TmpInst.addOperand(MCOperand::CreateReg(0));
1776 OutStreamer.EmitInstruction(TmpInst);
1777 }
1778 {
1779 MCInst TmpInst;
1780 TmpInst.setOpcode(ARM::ADDri);
1781 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1782 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1783 TmpInst.addOperand(MCOperand::CreateImm(0));
1784 // Predicate.
1785 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1786 TmpInst.addOperand(MCOperand::CreateReg(0));
1787 // 's' bit operand (always reg0 for this).
1788 TmpInst.addOperand(MCOperand::CreateReg(0));
1789 OutStreamer.EmitInstruction(TmpInst);
1790 }
1791 {
1792 MCInst TmpInst;
1793 TmpInst.setOpcode(ARM::MOVi);
1794 TmpInst.addOperand(MCOperand::CreateReg(ARM::R0));
1795 TmpInst.addOperand(MCOperand::CreateImm(1));
1796 // Predicate.
1797 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1798 TmpInst.addOperand(MCOperand::CreateReg(0));
1799 // 's' bit operand (always reg0 for this).
1800 TmpInst.addOperand(MCOperand::CreateReg(0));
1801 OutStreamer.AddComment("eh_setjmp end");
1802 OutStreamer.EmitInstruction(TmpInst);
1803 }
1804 return;
1805 }
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001806 case ARM::Int_eh_sjlj_longjmp: {
1807 // ldr sp, [$src, #8]
1808 // ldr $scratch, [$src, #4]
1809 // ldr r7, [$src]
1810 // bx $scratch
1811 unsigned SrcReg = MI->getOperand(0).getReg();
1812 unsigned ScratchReg = MI->getOperand(1).getReg();
1813 {
1814 MCInst TmpInst;
Jim Grosbach3e556122010-10-26 22:37:02 +00001815 TmpInst.setOpcode(ARM::LDRi12);
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001816 TmpInst.addOperand(MCOperand::CreateReg(ARM::SP));
1817 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001818 TmpInst.addOperand(MCOperand::CreateImm(8));
1819 // Predicate.
1820 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1821 TmpInst.addOperand(MCOperand::CreateReg(0));
1822 OutStreamer.EmitInstruction(TmpInst);
1823 }
1824 {
1825 MCInst TmpInst;
Jim Grosbach3e556122010-10-26 22:37:02 +00001826 TmpInst.setOpcode(ARM::LDRi12);
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001827 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1828 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001829 TmpInst.addOperand(MCOperand::CreateImm(4));
1830 // Predicate.
1831 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1832 TmpInst.addOperand(MCOperand::CreateReg(0));
1833 OutStreamer.EmitInstruction(TmpInst);
1834 }
1835 {
1836 MCInst TmpInst;
Jim Grosbach3e556122010-10-26 22:37:02 +00001837 TmpInst.setOpcode(ARM::LDRi12);
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001838 TmpInst.addOperand(MCOperand::CreateReg(ARM::R7));
1839 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001840 TmpInst.addOperand(MCOperand::CreateImm(0));
1841 // Predicate.
1842 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1843 TmpInst.addOperand(MCOperand::CreateReg(0));
1844 OutStreamer.EmitInstruction(TmpInst);
1845 }
1846 {
1847 MCInst TmpInst;
Bill Wendling6e46d842010-11-30 00:48:15 +00001848 TmpInst.setOpcode(ARM::BX);
Jim Grosbach5acb3de2010-09-27 21:47:04 +00001849 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1850 // Predicate.
1851 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1852 TmpInst.addOperand(MCOperand::CreateReg(0));
1853 OutStreamer.EmitInstruction(TmpInst);
1854 }
1855 return;
1856 }
Jim Grosbach385cc5e2010-09-27 22:28:11 +00001857 case ARM::tInt_eh_sjlj_longjmp: {
1858 // ldr $scratch, [$src, #8]
1859 // mov sp, $scratch
1860 // ldr $scratch, [$src, #4]
1861 // ldr r7, [$src]
1862 // bx $scratch
1863 unsigned SrcReg = MI->getOperand(0).getReg();
1864 unsigned ScratchReg = MI->getOperand(1).getReg();
1865 {
1866 MCInst TmpInst;
Bill Wendlingf4caf692010-12-14 03:36:38 +00001867 TmpInst.setOpcode(ARM::tLDRi);
Jim Grosbach385cc5e2010-09-27 22:28:11 +00001868 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1869 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
1870 // The offset immediate is #8. The operand value is scaled by 4 for the
Bill Wendlingf4caf692010-12-14 03:36:38 +00001871 // tLDR instruction.
Jim Grosbach385cc5e2010-09-27 22:28:11 +00001872 TmpInst.addOperand(MCOperand::CreateImm(2));
Jim Grosbach385cc5e2010-09-27 22:28:11 +00001873 // Predicate.
1874 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1875 TmpInst.addOperand(MCOperand::CreateReg(0));
1876 OutStreamer.EmitInstruction(TmpInst);
1877 }
1878 {
1879 MCInst TmpInst;
Jim Grosbach2a7b41b2011-06-30 23:38:17 +00001880 TmpInst.setOpcode(ARM::tMOVr);
Jim Grosbach385cc5e2010-09-27 22:28:11 +00001881 TmpInst.addOperand(MCOperand::CreateReg(ARM::SP));
1882 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1883 // Predicate.
1884 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1885 TmpInst.addOperand(MCOperand::CreateReg(0));
1886 OutStreamer.EmitInstruction(TmpInst);
1887 }
1888 {
1889 MCInst TmpInst;
Bill Wendlingf4caf692010-12-14 03:36:38 +00001890 TmpInst.setOpcode(ARM::tLDRi);
Jim Grosbach385cc5e2010-09-27 22:28:11 +00001891 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1892 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
1893 TmpInst.addOperand(MCOperand::CreateImm(1));
Jim Grosbach385cc5e2010-09-27 22:28:11 +00001894 // Predicate.
1895 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1896 TmpInst.addOperand(MCOperand::CreateReg(0));
1897 OutStreamer.EmitInstruction(TmpInst);
1898 }
1899 {
1900 MCInst TmpInst;
Bill Wendlingf4caf692010-12-14 03:36:38 +00001901 TmpInst.setOpcode(ARM::tLDRr);
Jim Grosbach385cc5e2010-09-27 22:28:11 +00001902 TmpInst.addOperand(MCOperand::CreateReg(ARM::R7));
1903 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
Jim Grosbach385cc5e2010-09-27 22:28:11 +00001904 TmpInst.addOperand(MCOperand::CreateReg(0));
1905 // Predicate.
1906 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1907 TmpInst.addOperand(MCOperand::CreateReg(0));
1908 OutStreamer.EmitInstruction(TmpInst);
1909 }
1910 {
1911 MCInst TmpInst;
Cameron Zwarich421b1062011-05-26 03:41:12 +00001912 TmpInst.setOpcode(ARM::tBX);
Jim Grosbach385cc5e2010-09-27 22:28:11 +00001913 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1914 // Predicate.
1915 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1916 TmpInst.addOperand(MCOperand::CreateReg(0));
1917 OutStreamer.EmitInstruction(TmpInst);
1918 }
1919 return;
1920 }
Chris Lattner97f06932009-10-19 20:20:46 +00001921 }
Jim Grosbachb0739b72010-09-02 01:02:06 +00001922
Chris Lattner97f06932009-10-19 20:20:46 +00001923 MCInst TmpInst;
Chris Lattner30e2cc22010-11-14 21:00:02 +00001924 LowerARMMachineInstrToMCInst(MI, TmpInst, *this);
Anton Korobeynikov57caad72011-03-05 18:43:32 +00001925
Chris Lattner850d2e22010-02-03 01:16:28 +00001926 OutStreamer.EmitInstruction(TmpInst);
Chris Lattner97f06932009-10-19 20:20:46 +00001927}
Daniel Dunbar2685a292009-10-20 05:15:36 +00001928
1929//===----------------------------------------------------------------------===//
1930// Target Registry Stuff
1931//===----------------------------------------------------------------------===//
1932
Daniel Dunbar2685a292009-10-20 05:15:36 +00001933// Force static initialization.
1934extern "C" void LLVMInitializeARMAsmPrinter() {
1935 RegisterAsmPrinter<ARMAsmPrinter> X(TheARMTarget);
1936 RegisterAsmPrinter<ARMAsmPrinter> Y(TheThumbTarget);
Daniel Dunbar2685a292009-10-20 05:15:36 +00001937}
1938