blob: dab549110dd9a1ef89f1edce9b0eea6599292b51 [file] [log] [blame]
Chris Lattner71eb0772009-10-19 20:20:46 +00001//===-- ARMAsmPrinter.cpp - Print machine code to an ARM .s file ----------===//
2//
Rafael Espindolaffdc24b2006-05-14 22:18:28 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
Rafael Espindolaffdc24b2006-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 Lattner1ef9cd42006-12-19 22:59:26 +000015#define DEBUG_TYPE "asm-printer"
Rafael Espindolaffdc24b2006-05-14 22:18:28 +000016#include "ARM.h"
Anton Korobeynikovfa6f1ee2009-05-23 19:51:20 +000017#include "ARMBuildAttrs.h"
Evan Cheng10043e22007-01-19 07:51:42 +000018#include "ARMAddressingModes.h"
19#include "ARMConstantPoolValue.h"
Jim Grosbach0e854f32010-10-01 22:39:28 +000020#include "InstPrinter/ARMInstPrinter.h"
Chris Lattner71eb0772009-10-19 20:20:46 +000021#include "ARMMachineFunctionInfo.h"
22#include "ARMMCInstLower.h"
23#include "ARMTargetMachine.h"
Jason W Kim109ff292010-10-11 23:01:44 +000024#include "ARMTargetObjectFile.h"
Dale Johannesen91358582010-04-26 20:07:31 +000025#include "llvm/Analysis/DebugInfo.h"
Rafael Espindolaffdc24b2006-05-14 22:18:28 +000026#include "llvm/Constants.h"
Rafael Espindolaffdc24b2006-05-14 22:18:28 +000027#include "llvm/Module.h"
Benjamin Kramer8adc1e22009-12-28 12:27:56 +000028#include "llvm/Type.h"
Dan Gohmanef3d4572009-08-13 01:36:44 +000029#include "llvm/Assembly/Writer.h"
Rafael Espindolaffdc24b2006-05-14 22:18:28 +000030#include "llvm/CodeGen/AsmPrinter.h"
Chris Lattner6462adc2009-10-19 18:38:33 +000031#include "llvm/CodeGen/MachineModuleInfoImpls.h"
Rafael Espindolaffdc24b2006-05-14 22:18:28 +000032#include "llvm/CodeGen/MachineFunctionPass.h"
Evan Cheng10043e22007-01-19 07:51:42 +000033#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner6462adc2009-10-19 18:38:33 +000034#include "llvm/MC/MCAsmInfo.h"
35#include "llvm/MC/MCContext.h"
Bill Wendlingf1eae222010-03-09 00:40:17 +000036#include "llvm/MC/MCExpr.h"
Chris Lattner71eb0772009-10-19 20:20:46 +000037#include "llvm/MC/MCInst.h"
Chris Lattner6c203912009-08-10 18:15:01 +000038#include "llvm/MC/MCSectionMachO.h"
Chris Lattner4b7dadb2009-08-19 05:49:37 +000039#include "llvm/MC/MCStreamer.h"
Chris Lattner4cd44982009-09-13 17:14:04 +000040#include "llvm/MC/MCSymbol.h"
Chris Lattner0b822ab2010-03-12 21:19:23 +000041#include "llvm/Target/Mangler.h"
Rafael Espindola89e5cbd2006-07-27 11:38:51 +000042#include "llvm/Target/TargetData.h"
Rafael Espindolaffdc24b2006-05-14 22:18:28 +000043#include "llvm/Target/TargetMachine.h"
Evan Cheng1199c2d2007-01-19 19:25:36 +000044#include "llvm/Target/TargetOptions.h"
Daniel Dunbare8338102009-07-15 20:24:03 +000045#include "llvm/Target/TargetRegistry.h"
Evan Cheng986fc8e2009-07-24 18:19:46 +000046#include "llvm/ADT/SmallPtrSet.h"
Jim Grosbachc5fcbdc2009-09-01 18:49:12 +000047#include "llvm/ADT/SmallString.h"
Bob Wilsond95ccd62009-11-06 23:33:28 +000048#include "llvm/ADT/StringExtras.h"
Chris Lattner71eb0772009-10-19 20:20:46 +000049#include "llvm/Support/CommandLine.h"
Devang Patela52ddc42010-08-04 22:39:39 +000050#include "llvm/Support/Debug.h"
Torok Edwinf8d479c2009-07-08 20:55:50 +000051#include "llvm/Support/ErrorHandling.h"
Chris Lattnerd20699b2010-04-04 08:18:47 +000052#include "llvm/Support/raw_ostream.h"
Rafael Espindolaffdc24b2006-05-14 22:18:28 +000053#include <cctype>
Rafael Espindolaffdc24b2006-05-14 22:18:28 +000054using namespace llvm;
55
Jim Grosbach965a73a2010-07-21 23:03:52 +000056namespace llvm {
57 namespace ARM {
58 enum DW_ISA {
59 DW_ISA_ARM_thumb = 1,
60 DW_ISA_ARM_arm = 2
61 };
62 }
63}
64
Chris Lattner1ef9cd42006-12-19 22:59:26 +000065namespace {
Chris Lattneree9399a2009-10-19 17:59:19 +000066 class ARMAsmPrinter : public AsmPrinter {
Evan Cheng10043e22007-01-19 07:51:42 +000067
68 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
69 /// make the right decision when printing asm code for different targets.
70 const ARMSubtarget *Subtarget;
71
72 /// AFI - Keep a pointer to ARMFunctionInfo for the current
Evan Cheng5e3ac182008-09-18 07:27:23 +000073 /// MachineFunction.
Evan Cheng10043e22007-01-19 07:51:42 +000074 ARMFunctionInfo *AFI;
75
Evan Cheng5e3ac182008-09-18 07:27:23 +000076 /// MCP - Keep a pointer to constantpool entries of the current
77 /// MachineFunction.
78 const MachineConstantPool *MCP;
79
Bill Wendlingc5437ea2009-02-24 08:30:20 +000080 public:
Chris Lattnerd20699b2010-04-04 08:18:47 +000081 explicit ARMAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
82 : AsmPrinter(TM, Streamer), AFI(NULL), MCP(NULL) {
Bill Wendlingc5437ea2009-02-24 08:30:20 +000083 Subtarget = &TM.getSubtarget<ARMSubtarget>();
84 }
85
Rafael Espindolaffdc24b2006-05-14 22:18:28 +000086 virtual const char *getPassName() const {
87 return "ARM Assembly Printer";
88 }
Jim Grosbach8ee5cd92010-09-02 01:02:06 +000089
Chris Lattner76c564b2010-04-04 04:47:45 +000090 void printOperand(const MachineInstr *MI, int OpNum, raw_ostream &O,
Evan Cheng10043e22007-01-19 07:51:42 +000091 const char *Modifier = 0);
Bob Wilsond95ccd62009-11-06 23:33:28 +000092
Evan Chengb23b50d2009-06-29 07:51:04 +000093 virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
Chris Lattner3bb09762010-04-04 05:29:35 +000094 unsigned AsmVariant, const char *ExtraCode,
95 raw_ostream &O);
Evan Chengb23b50d2009-06-29 07:51:04 +000096 virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNum,
Bob Wilsona2c462b2009-05-19 05:53:42 +000097 unsigned AsmVariant,
Chris Lattner3bb09762010-04-04 05:29:35 +000098 const char *ExtraCode, raw_ostream &O);
Rafael Espindolaffdc24b2006-05-14 22:18:28 +000099
Jim Grosbach080fdf42010-09-30 01:57:53 +0000100 void EmitJumpTable(const MachineInstr *MI);
101 void EmitJump2Table(const MachineInstr *MI);
Chris Lattnerfa1f67e2010-01-28 01:10:34 +0000102 virtual void EmitInstruction(const MachineInstr *MI);
Rafael Espindolaffdc24b2006-05-14 22:18:28 +0000103 bool runOnMachineFunction(MachineFunction &F);
Jim Grosbach8ee5cd92010-09-02 01:02:06 +0000104
Chris Lattner2b796242010-01-28 00:19:24 +0000105 virtual void EmitConstantPool() {} // we emit constant pools customly!
Chris Lattner56db8c32010-01-27 23:58:11 +0000106 virtual void EmitFunctionEntryLabel();
Bob Wilsonb633d7a2009-09-30 22:06:26 +0000107 void EmitStartOfAsmFile(Module &M);
Chris Lattneree9399a2009-10-19 17:59:19 +0000108 void EmitEndOfAsmFile(Module &M);
Evan Cheng10043e22007-01-19 07:51:42 +0000109
Jason W Kimbff84d42010-10-06 22:36:46 +0000110 private:
111 // Helpers for EmitStartOfAsmFile() and EmitEndOfAsmFile()
112 void emitAttributes();
Jason W Kim109ff292010-10-11 23:01:44 +0000113 void emitTextAttribute(ARMBuildAttrs::SpecialAttr attr, StringRef v);
Jim Grosbach25cd3bf2010-10-06 22:46:47 +0000114 void emitAttribute(ARMBuildAttrs::AttrType attr, int v);
Jason W Kimbff84d42010-10-06 22:36:46 +0000115
Jason W Kim109ff292010-10-11 23:01:44 +0000116 // Helper for ELF .o only
117 void emitARMAttributeSection();
118
Jason W Kimbff84d42010-10-06 22:36:46 +0000119 public:
Jim Grosbach45c83d42010-09-28 17:05:56 +0000120 void PrintDebugValueComment(const MachineInstr *MI, raw_ostream &OS);
121
Devang Patela52ddc42010-08-04 22:39:39 +0000122 MachineLocation getDebugValueLocation(const MachineInstr *MI) const {
123 MachineLocation Location;
124 assert (MI->getNumOperands() == 4 && "Invalid no. of machine operands!");
125 // Frame address. Currently handles register +- offset only.
126 if (MI->getOperand(0).isReg() && MI->getOperand(1).isImm())
127 Location.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
128 else {
129 DEBUG(dbgs() << "DBG_VALUE instruction ignored! " << *MI << "\n");
130 }
131 return Location;
132 }
133
Jim Grosbach965a73a2010-07-21 23:03:52 +0000134 virtual unsigned getISAEncoding() {
135 // ARM/Darwin adds ISA to the DWARF info for each function.
136 if (!Subtarget->isTargetDarwin())
137 return 0;
138 return Subtarget->isThumb() ?
139 llvm::ARM::DW_ISA_ARM_thumb : llvm::ARM::DW_ISA_ARM_arm;
140 }
141
Chris Lattner68d64aa2010-01-25 19:51:38 +0000142 MCSymbol *GetARMSetPICJumpTableLabel2(unsigned uid, unsigned uid2,
143 const MachineBasicBlock *MBB) const;
144 MCSymbol *GetARMJTIPICJumpTableLabel2(unsigned uid, unsigned uid2) const;
Chris Lattner6330d532010-01-25 19:39:52 +0000145
Jim Grosbach4a6ab132010-09-24 20:47:58 +0000146 MCSymbol *GetARMSJLJEHLabel(void) const;
147
Evan Cheng655fa0f2008-08-08 06:56:16 +0000148 /// EmitMachineConstantPoolValue - Print a machine constantpool value to
149 /// the .s file.
Evan Cheng10043e22007-01-19 07:51:42 +0000150 virtual void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
Chris Lattner5e596182010-04-04 07:05:53 +0000151 SmallString<128> Str;
152 raw_svector_ostream OS(Str);
153 EmitMachineConstantPoolValue(MCPV, OS);
154 OutStreamer.EmitRawText(OS.str());
155 }
Jim Grosbach8ee5cd92010-09-02 01:02:06 +0000156
Chris Lattner5e596182010-04-04 07:05:53 +0000157 void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV,
158 raw_ostream &O) {
Chris Lattnerdb42c8d2010-01-20 07:33:29 +0000159 switch (TM.getTargetData()->getTypeAllocSize(MCPV->getType())) {
160 case 1: O << MAI->getData8bitsDirective(0); break;
161 case 2: O << MAI->getData16bitsDirective(0); break;
162 case 4: O << MAI->getData32bitsDirective(0); break;
163 default: assert(0 && "Unknown CPV size");
164 }
Evan Cheng10043e22007-01-19 07:51:42 +0000165
Evan Cheng655fa0f2008-08-08 06:56:16 +0000166 ARMConstantPoolValue *ACPV = static_cast<ARMConstantPoolValue*>(MCPV);
Jim Grosbach20eac922009-09-01 01:57:56 +0000167
168 if (ACPV->isLSDA()) {
Chris Lattner5e596182010-04-04 07:05:53 +0000169 O << MAI->getPrivateGlobalPrefix() << "_LSDA_" << getFunctionNumber();
Bob Wilson433ab092009-11-02 16:59:06 +0000170 } else if (ACPV->isBlockAddress()) {
Chris Lattner6a0e89a2010-04-05 16:32:14 +0000171 O << *GetBlockAddressSymbol(ACPV->getBlockAddress());
Bob Wilson433ab092009-11-02 16:59:06 +0000172 } else if (ACPV->isGlobalValue()) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000173 const GlobalValue *GV = ACPV->getGV();
Evan Cheng43b9ca62009-08-28 23:18:09 +0000174 bool isIndirect = Subtarget->isTargetDarwin() &&
Evan Cheng1b389522009-09-03 07:04:02 +0000175 Subtarget->GVIsIndirectSymbol(GV, TM.getRelocationModel());
Evan Cheng43b9ca62009-08-28 23:18:09 +0000176 if (!isIndirect)
Chris Lattner0b822ab2010-03-12 21:19:23 +0000177 O << *Mang->getSymbol(GV);
Evan Cheng43b9ca62009-08-28 23:18:09 +0000178 else {
179 // FIXME: Remove this when Darwin transition to @GOT like syntax.
Chris Lattner555ceabe2010-01-16 18:37:32 +0000180 MCSymbol *Sym = GetSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
Chris Lattner8b5d55e2010-01-17 21:43:43 +0000181 O << *Sym;
Jim Grosbach8ee5cd92010-09-02 01:02:06 +0000182
Chris Lattner8d2e3a22009-10-19 18:49:14 +0000183 MachineModuleInfoMachO &MMIMachO =
184 MMI->getObjFileInfo<MachineModuleInfoMachO>();
Bill Wendlinga810bdf2010-03-10 22:34:10 +0000185 MachineModuleInfoImpl::StubValueTy &StubSym =
Chris Lattner8d2e3a22009-10-19 18:49:14 +0000186 GV->hasHiddenVisibility() ? MMIMachO.getHiddenGVStubEntry(Sym) :
187 MMIMachO.getGVStubEntry(Sym);
Bill Wendlinga810bdf2010-03-10 22:34:10 +0000188 if (StubSym.getPointer() == 0)
189 StubSym = MachineModuleInfoImpl::
Chris Lattner0b822ab2010-03-12 21:19:23 +0000190 StubValueTy(Mang->getSymbol(GV), !GV->hasInternalLinkage());
Evan Cheng43b9ca62009-08-28 23:18:09 +0000191 }
Bob Wilson433ab092009-11-02 16:59:06 +0000192 } else {
193 assert(ACPV->isExtSymbol() && "unrecognized constant pool value");
Chris Lattner8b5d55e2010-01-17 21:43:43 +0000194 O << *GetExternalSymbolSymbol(ACPV->getSymbol());
Bob Wilson433ab092009-11-02 16:59:06 +0000195 }
Jim Grosbach1eaa90b2009-09-04 01:38:51 +0000196
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +0000197 if (ACPV->hasModifier()) O << "(" << ACPV->getModifier() << ")";
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +0000198 if (ACPV->getPCAdjustment() != 0) {
Chris Lattnere9a75a62009-08-22 21:43:10 +0000199 O << "-(" << MAI->getPrivateGlobalPrefix() << "PC"
Evan Cheng408aa562009-11-06 22:24:13 +0000200 << getFunctionNumber() << "_" << ACPV->getLabelId()
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +0000201 << "+" << (unsigned)ACPV->getPCAdjustment();
202 if (ACPV->mustAddCurrentAddress())
203 O << "-.";
Chris Lattner45400332010-01-15 23:26:49 +0000204 O << ')';
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +0000205 }
Evan Cheng10043e22007-01-19 07:51:42 +0000206 }
Rafael Espindolaffdc24b2006-05-14 22:18:28 +0000207 };
208} // end of anonymous namespace
209
Chris Lattner56db8c32010-01-27 23:58:11 +0000210void ARMAsmPrinter::EmitFunctionEntryLabel() {
211 if (AFI->isThumbFunction()) {
Chris Lattner5e596182010-04-04 07:05:53 +0000212 OutStreamer.EmitRawText(StringRef("\t.code\t16"));
Chris Lattner6a0e89a2010-04-05 16:32:14 +0000213 if (!Subtarget->isTargetDarwin())
Chris Lattner5e596182010-04-04 07:05:53 +0000214 OutStreamer.EmitRawText(StringRef("\t.thumb_func"));
Chris Lattner6a0e89a2010-04-05 16:32:14 +0000215 else {
216 // This needs to emit to a temporary string to get properly quoted
217 // MCSymbols when they have spaces in them.
218 SmallString<128> Tmp;
219 raw_svector_ostream OS(Tmp);
220 OS << "\t.thumb_func\t" << *CurrentFnSym;
221 OutStreamer.EmitRawText(OS.str());
222 }
Chris Lattner56db8c32010-01-27 23:58:11 +0000223 }
Jim Grosbach8ee5cd92010-09-02 01:02:06 +0000224
Chris Lattner56db8c32010-01-27 23:58:11 +0000225 OutStreamer.EmitLabel(CurrentFnSym);
226}
227
Jim Grosbach080fdf42010-09-30 01:57:53 +0000228/// runOnMachineFunction - This uses the EmitInstruction()
Rafael Espindolaffdc24b2006-05-14 22:18:28 +0000229/// method to print assembly for each instruction.
230///
231bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Evan Cheng10043e22007-01-19 07:51:42 +0000232 AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng5e3ac182008-09-18 07:27:23 +0000233 MCP = MF.getConstantPool();
Rafael Espindola27f8bdc2006-05-23 02:48:20 +0000234
Chris Lattner73de5fb2010-01-28 01:28:58 +0000235 return AsmPrinter::runOnMachineFunction(MF);
Rafael Espindolaffdc24b2006-05-14 22:18:28 +0000236}
237
Evan Chengb23b50d2009-06-29 07:51:04 +0000238void ARMAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
Chris Lattner76c564b2010-04-04 04:47:45 +0000239 raw_ostream &O, const char *Modifier) {
Evan Chengb23b50d2009-06-29 07:51:04 +0000240 const MachineOperand &MO = MI->getOperand(OpNum);
Anton Korobeynikov25229082009-11-24 00:44:37 +0000241 unsigned TF = MO.getTargetFlags();
242
Rafael Espindola91df1ef2006-05-25 12:57:06 +0000243 switch (MO.getType()) {
Chris Lattner93e3ef62009-10-19 20:59:55 +0000244 default:
245 assert(0 && "<unknown operand type>");
Bob Wilson2e076c42009-06-22 23:27:02 +0000246 case MachineOperand::MO_Register: {
247 unsigned Reg = MO.getReg();
Chris Lattner93e3ef62009-10-19 20:59:55 +0000248 assert(TargetRegisterInfo::isPhysicalRegister(Reg));
Jim Grosbach2c950272010-10-06 21:22:32 +0000249 assert(!MO.getSubReg() && "Subregs should be eliminated!");
250 O << ARMInstPrinter::getRegisterName(Reg);
Rafael Espindola91df1ef2006-05-25 12:57:06 +0000251 break;
Bob Wilson2e076c42009-06-22 23:27:02 +0000252 }
Evan Cheng10043e22007-01-19 07:51:42 +0000253 case MachineOperand::MO_Immediate: {
Evan Cheng83e0d482009-09-28 09:14:39 +0000254 int64_t Imm = MO.getImm();
Anton Korobeynikov222b86c2009-10-08 20:43:22 +0000255 O << '#';
Anton Korobeynikov25229082009-11-24 00:44:37 +0000256 if ((Modifier && strcmp(Modifier, "lo16") == 0) ||
Jim Grosbach8025f892010-10-06 16:51:55 +0000257 (TF == ARMII::MO_LO16))
Anton Korobeynikov25229082009-11-24 00:44:37 +0000258 O << ":lower16:";
259 else if ((Modifier && strcmp(Modifier, "hi16") == 0) ||
Jim Grosbach8025f892010-10-06 16:51:55 +0000260 (TF == ARMII::MO_HI16))
Anton Korobeynikov25229082009-11-24 00:44:37 +0000261 O << ":upper16:";
Anton Korobeynikov222b86c2009-10-08 20:43:22 +0000262 O << Imm;
Rafael Espindola91df1ef2006-05-25 12:57:06 +0000263 break;
Evan Cheng10043e22007-01-19 07:51:42 +0000264 }
Rafael Espindola91df1ef2006-05-25 12:57:06 +0000265 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner29bdac42010-03-13 21:04:28 +0000266 O << *MO.getMBB()->getSymbol();
Rafael Espindola91df1ef2006-05-25 12:57:06 +0000267 return;
Rafael Espindola75269be2006-07-16 01:02:57 +0000268 case MachineOperand::MO_GlobalAddress: {
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000269 const GlobalValue *GV = MO.getGlobal();
Anton Korobeynikov25229082009-11-24 00:44:37 +0000270 if ((Modifier && strcmp(Modifier, "lo16") == 0) ||
271 (TF & ARMII::MO_LO16))
272 O << ":lower16:";
273 else if ((Modifier && strcmp(Modifier, "hi16") == 0) ||
274 (TF & ARMII::MO_HI16))
275 O << ":upper16:";
Chris Lattner0b822ab2010-03-12 21:19:23 +0000276 O << *Mang->getSymbol(GV);
Anton Korobeynikovbff4b372008-11-22 16:15:34 +0000277
Chris Lattnerf33c7fc2010-04-03 22:28:33 +0000278 printOffset(MO.getOffset(), O);
Jim Grosbachf49540c2010-10-06 21:36:43 +0000279 if (TF == ARMII::MO_PLT)
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +0000280 O << "(PLT)";
Evan Cheng10043e22007-01-19 07:51:42 +0000281 break;
Rafael Espindola75269be2006-07-16 01:02:57 +0000282 }
Evan Cheng10043e22007-01-19 07:51:42 +0000283 case MachineOperand::MO_ExternalSymbol: {
Chris Lattner8b5d55e2010-01-17 21:43:43 +0000284 O << *GetExternalSymbolSymbol(MO.getSymbolName());
Jim Grosbachf49540c2010-10-06 21:36:43 +0000285 if (TF == ARMII::MO_PLT)
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +0000286 O << "(PLT)";
Rafael Espindola91df1ef2006-05-25 12:57:06 +0000287 break;
Evan Cheng10043e22007-01-19 07:51:42 +0000288 }
Rafael Espindola91df1ef2006-05-25 12:57:06 +0000289 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattnerc55ea3f2010-01-23 07:00:21 +0000290 O << *GetCPISymbol(MO.getIndex());
Rafael Espindola91df1ef2006-05-25 12:57:06 +0000291 break;
Evan Cheng10043e22007-01-19 07:51:42 +0000292 case MachineOperand::MO_JumpTableIndex:
Chris Lattnerc55ea3f2010-01-23 07:00:21 +0000293 O << *GetJTISymbol(MO.getIndex());
Evan Cheng10043e22007-01-19 07:51:42 +0000294 break;
Rafael Espindola91df1ef2006-05-25 12:57:06 +0000295 }
Rafael Espindolaffdc24b2006-05-14 22:18:28 +0000296}
297
Evan Chengb23b50d2009-06-29 07:51:04 +0000298//===--------------------------------------------------------------------===//
299
Chris Lattner68d64aa2010-01-25 19:51:38 +0000300MCSymbol *ARMAsmPrinter::
301GetARMSetPICJumpTableLabel2(unsigned uid, unsigned uid2,
302 const MachineBasicBlock *MBB) const {
303 SmallString<60> Name;
304 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix()
Chris Lattner6330d532010-01-25 19:39:52 +0000305 << getFunctionNumber() << '_' << uid << '_' << uid2
Chris Lattner68d64aa2010-01-25 19:51:38 +0000306 << "_set_" << MBB->getNumber();
Chris Lattner98970432010-03-30 18:10:53 +0000307 return OutContext.GetOrCreateSymbol(Name.str());
Chris Lattner68d64aa2010-01-25 19:51:38 +0000308}
309
310MCSymbol *ARMAsmPrinter::
311GetARMJTIPICJumpTableLabel2(unsigned uid, unsigned uid2) const {
312 SmallString<60> Name;
313 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "JTI"
Chris Lattner8186eec2010-01-25 23:28:03 +0000314 << getFunctionNumber() << '_' << uid << '_' << uid2;
Chris Lattner98970432010-03-30 18:10:53 +0000315 return OutContext.GetOrCreateSymbol(Name.str());
Chris Lattner6330d532010-01-25 19:39:52 +0000316}
317
Jim Grosbach4a6ab132010-09-24 20:47:58 +0000318
319MCSymbol *ARMAsmPrinter::GetARMSJLJEHLabel(void) const {
320 SmallString<60> Name;
321 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "SJLJEH"
322 << getFunctionNumber();
323 return OutContext.GetOrCreateSymbol(Name.str());
324}
325
Evan Chengb23b50d2009-06-29 07:51:04 +0000326bool ARMAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
Chris Lattner3bb09762010-04-04 05:29:35 +0000327 unsigned AsmVariant, const char *ExtraCode,
328 raw_ostream &O) {
Evan Cheng10043e22007-01-19 07:51:42 +0000329 // Does this asm operand have a single letter operand modifier?
330 if (ExtraCode && ExtraCode[0]) {
331 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovcfed3002009-08-08 23:10:41 +0000332
Evan Cheng10043e22007-01-19 07:51:42 +0000333 switch (ExtraCode[0]) {
334 default: return true; // Unknown modifier.
Bob Wilson9ce44e22009-07-09 23:54:51 +0000335 case 'a': // Print as a memory address.
336 if (MI->getOperand(OpNum).isReg()) {
Jim Grosbach136ed512010-09-30 15:25:22 +0000337 O << "["
338 << ARMInstPrinter::getRegisterName(MI->getOperand(OpNum).getReg())
339 << "]";
Bob Wilson9ce44e22009-07-09 23:54:51 +0000340 return false;
341 }
342 // Fallthrough
343 case 'c': // Don't print "#" before an immediate operand.
Bob Wilsonceffeb62009-08-21 21:58:55 +0000344 if (!MI->getOperand(OpNum).isImm())
345 return true;
Jim Grosbach080fdf42010-09-30 01:57:53 +0000346 O << MI->getOperand(OpNum).getImm();
Bob Wilson0669f6d2009-04-06 21:46:51 +0000347 return false;
Evan Cheng1e150de2007-04-04 00:13:29 +0000348 case 'P': // Print a VFP double precision register.
Evan Cheng0c2544f2009-12-08 23:06:22 +0000349 case 'q': // Print a NEON quad precision register.
Chris Lattner76c564b2010-04-04 04:47:45 +0000350 printOperand(MI, OpNum, O);
Evan Chengea28fc52007-03-08 22:42:46 +0000351 return false;
Evan Cheng10043e22007-01-19 07:51:42 +0000352 case 'Q':
Bob Wilson40e62df2010-05-27 20:23:42 +0000353 case 'R':
Bob Wilson40e62df2010-05-27 20:23:42 +0000354 case 'H':
Evan Chengc2ebe032010-05-27 23:45:31 +0000355 report_fatal_error("llvm does not support 'Q', 'R', and 'H' modifiers!");
Bob Wilson40e62df2010-05-27 20:23:42 +0000356 return true;
Evan Cheng3d3ee872010-05-27 22:08:38 +0000357 }
Evan Cheng10043e22007-01-19 07:51:42 +0000358 }
Jim Grosbach1eaa90b2009-09-04 01:38:51 +0000359
Chris Lattner76c564b2010-04-04 04:47:45 +0000360 printOperand(MI, OpNum, O);
Evan Cheng10043e22007-01-19 07:51:42 +0000361 return false;
362}
363
Bob Wilsona2c462b2009-05-19 05:53:42 +0000364bool ARMAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Evan Chengb23b50d2009-06-29 07:51:04 +0000365 unsigned OpNum, unsigned AsmVariant,
Chris Lattner3bb09762010-04-04 05:29:35 +0000366 const char *ExtraCode,
367 raw_ostream &O) {
Bob Wilsona2c462b2009-05-19 05:53:42 +0000368 if (ExtraCode && ExtraCode[0])
369 return true; // Unknown modifier.
Bob Wilson3b515602009-10-13 20:50:28 +0000370
371 const MachineOperand &MO = MI->getOperand(OpNum);
372 assert(MO.isReg() && "unexpected inline asm memory operand");
Jim Grosbach080fdf42010-09-30 01:57:53 +0000373 O << "[" << ARMInstPrinter::getRegisterName(MO.getReg()) << "]";
Bob Wilsona2c462b2009-05-19 05:53:42 +0000374 return false;
375}
376
Bob Wilsonb633d7a2009-09-30 22:06:26 +0000377void ARMAsmPrinter::EmitStartOfAsmFile(Module &M) {
Bob Wilson20e5f5e2009-09-30 00:23:42 +0000378 if (Subtarget->isTargetDarwin()) {
379 Reloc::Model RelocM = TM.getRelocationModel();
380 if (RelocM == Reloc::PIC_ || RelocM == Reloc::DynamicNoPIC) {
381 // Declare all the text sections up front (before the DWARF sections
382 // emitted by AsmPrinter::doInitialization) so the assembler will keep
383 // them together at the beginning of the object file. This helps
384 // avoid out-of-range branches that are due a fundamental limitation of
385 // the way symbol offsets are encoded with the current Darwin ARM
386 // relocations.
Jim Grosbach8ee5cd92010-09-02 01:02:06 +0000387 const TargetLoweringObjectFileMachO &TLOFMacho =
Dan Gohman53d4a082010-04-17 16:44:48 +0000388 static_cast<const TargetLoweringObjectFileMachO &>(
389 getObjFileLowering());
Bob Wilsonb2120755a2009-09-30 22:25:37 +0000390 OutStreamer.SwitchSection(TLOFMacho.getTextSection());
391 OutStreamer.SwitchSection(TLOFMacho.getTextCoalSection());
392 OutStreamer.SwitchSection(TLOFMacho.getConstTextCoalSection());
393 if (RelocM == Reloc::DynamicNoPIC) {
394 const MCSection *sect =
Chris Lattner433d4062010-04-08 20:40:11 +0000395 OutContext.getMachOSection("__TEXT", "__symbol_stub4",
396 MCSectionMachO::S_SYMBOL_STUBS,
397 12, SectionKind::getText());
Bob Wilsonb2120755a2009-09-30 22:25:37 +0000398 OutStreamer.SwitchSection(sect);
399 } else {
400 const MCSection *sect =
Chris Lattner433d4062010-04-08 20:40:11 +0000401 OutContext.getMachOSection("__TEXT", "__picsymbolstub4",
402 MCSectionMachO::S_SYMBOL_STUBS,
403 16, SectionKind::getText());
Bob Wilsonb2120755a2009-09-30 22:25:37 +0000404 OutStreamer.SwitchSection(sect);
405 }
Bob Wilson4320e2d2010-07-30 19:55:47 +0000406 const MCSection *StaticInitSect =
407 OutContext.getMachOSection("__TEXT", "__StaticInit",
408 MCSectionMachO::S_REGULAR |
409 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
410 SectionKind::getText());
411 OutStreamer.SwitchSection(StaticInitSect);
Bob Wilson20e5f5e2009-09-30 00:23:42 +0000412 }
413 }
414
Jim Grosbachd7cf55c2009-11-09 00:11:35 +0000415 // Use unified assembler syntax.
Jason W Kim645f6c22010-09-30 02:45:56 +0000416 OutStreamer.EmitAssemblerFlag(MCAF_SyntaxUnified);
Anton Korobeynikovf687a822009-06-17 23:43:18 +0000417
Anton Korobeynikovfa6f1ee2009-05-23 19:51:20 +0000418 // Emit ARM Build Attributes
419 if (Subtarget->isTargetELF()) {
Anton Korobeynikovfa6f1ee2009-05-23 19:51:20 +0000420
Jason W Kimbff84d42010-10-06 22:36:46 +0000421 emitAttributes();
Anton Korobeynikovfa6f1ee2009-05-23 19:51:20 +0000422 }
Rafael Espindolaffdc24b2006-05-14 22:18:28 +0000423}
424
Anton Korobeynikov04083522008-08-07 09:54:23 +0000425
Chris Lattneree9399a2009-10-19 17:59:19 +0000426void ARMAsmPrinter::EmitEndOfAsmFile(Module &M) {
Evan Cheng1199c2d2007-01-19 19:25:36 +0000427 if (Subtarget->isTargetDarwin()) {
Chris Lattner73ebe432009-08-03 22:18:15 +0000428 // All darwin targets use mach-o.
Dan Gohman53d4a082010-04-17 16:44:48 +0000429 const TargetLoweringObjectFileMachO &TLOFMacho =
430 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
Chris Lattner6462adc2009-10-19 18:38:33 +0000431 MachineModuleInfoMachO &MMIMacho =
432 MMI->getObjFileInfo<MachineModuleInfoMachO>();
Jim Grosbach1eaa90b2009-09-04 01:38:51 +0000433
Evan Cheng10043e22007-01-19 07:51:42 +0000434 // Output non-lazy-pointers for external and common global variables.
Chris Lattner6462adc2009-10-19 18:38:33 +0000435 MachineModuleInfoMachO::SymbolListTy Stubs = MMIMacho.GetGVStubList();
Bill Wendlinga810bdf2010-03-10 22:34:10 +0000436
Chris Lattner6462adc2009-10-19 18:38:33 +0000437 if (!Stubs.empty()) {
Chris Lattnercb307a272009-08-10 01:39:42 +0000438 // Switch with ".non_lazy_symbol_pointer" directive.
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000439 OutStreamer.SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
Chris Lattner292472d2009-08-10 18:01:34 +0000440 EmitAlignment(2);
Chris Lattner6462adc2009-10-19 18:38:33 +0000441 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
Bill Wendlingf1eae222010-03-09 00:40:17 +0000442 // L_foo$stub:
443 OutStreamer.EmitLabel(Stubs[i].first);
444 // .indirect_symbol _foo
Bill Wendlinge8e79522010-03-11 01:18:13 +0000445 MachineModuleInfoImpl::StubValueTy &MCSym = Stubs[i].second;
446 OutStreamer.EmitSymbolAttribute(MCSym.getPointer(),MCSA_IndirectSymbol);
Bill Wendlingffba5fa2010-03-09 00:43:34 +0000447
Bill Wendlinge8e79522010-03-11 01:18:13 +0000448 if (MCSym.getInt())
Bill Wendlingffba5fa2010-03-09 00:43:34 +0000449 // External to current translation unit.
450 OutStreamer.EmitIntValue(0, 4/*size*/, 0/*addrspace*/);
451 else
452 // Internal to current translation unit.
Bill Wendling866f5762010-03-31 18:47:10 +0000453 //
Jim Grosbach754e1ef2010-09-22 16:45:13 +0000454 // When we place the LSDA into the TEXT section, the type info
455 // pointers need to be indirect and pc-rel. We accomplish this by
456 // using NLPs; however, sometimes the types are local to the file.
457 // We need to fill in the value for the NLP in those cases.
Bill Wendlinge8e79522010-03-11 01:18:13 +0000458 OutStreamer.EmitValue(MCSymbolRefExpr::Create(MCSym.getPointer(),
459 OutContext),
Bill Wendlingffba5fa2010-03-09 00:43:34 +0000460 4/*size*/, 0/*addrspace*/);
Evan Cheng2a03c7e2008-12-05 01:06:39 +0000461 }
Bill Wendlingf1eae222010-03-09 00:40:17 +0000462
463 Stubs.clear();
464 OutStreamer.AddBlankLine();
Evan Cheng10043e22007-01-19 07:51:42 +0000465 }
466
Chris Lattner3334deb2009-10-19 18:44:38 +0000467 Stubs = MMIMacho.GetHiddenGVStubList();
468 if (!Stubs.empty()) {
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000469 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
Chris Lattnerfbcafd42009-08-10 18:02:16 +0000470 EmitAlignment(2);
Bill Wendlingf1eae222010-03-09 00:40:17 +0000471 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
472 // L_foo$stub:
473 OutStreamer.EmitLabel(Stubs[i].first);
474 // .long _foo
Bill Wendlinga810bdf2010-03-10 22:34:10 +0000475 OutStreamer.EmitValue(MCSymbolRefExpr::
476 Create(Stubs[i].second.getPointer(),
477 OutContext),
Bill Wendlingf1eae222010-03-09 00:40:17 +0000478 4/*size*/, 0/*addrspace*/);
479 }
Bill Wendlingffba5fa2010-03-09 00:43:34 +0000480
481 Stubs.clear();
482 OutStreamer.AddBlankLine();
Evan Cheng2a03c7e2008-12-05 01:06:39 +0000483 }
484
Evan Cheng10043e22007-01-19 07:51:42 +0000485 // Funny Darwin hack: This flag tells the linker that no global symbols
486 // contain code that falls through to other global symbols (e.g. the obvious
487 // implementation of multiple entry points). If this doesn't occur, the
488 // linker can safely perform dead code stripping. Since LLVM never
489 // generates code that does this, it is always safe to set.
Chris Lattner685508c2010-01-23 06:39:22 +0000490 OutStreamer.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
Rafael Espindola89e5cbd2006-07-27 11:38:51 +0000491 }
Rafael Espindolaffdc24b2006-05-14 22:18:28 +0000492}
Anton Korobeynikov17d28de2008-08-17 13:55:10 +0000493
Chris Lattner71eb0772009-10-19 20:20:46 +0000494//===----------------------------------------------------------------------===//
Jason W Kimbff84d42010-10-06 22:36:46 +0000495// Helper routines for EmitStartOfAsmFile() and EmitEndOfAsmFile()
496// FIXME:
497// The following seem like one-off assembler flags, but they actually need
Jim Grosbach25cd3bf2010-10-06 22:46:47 +0000498// to appear in the .ARM.attributes section in ELF.
Jason W Kimbff84d42010-10-06 22:36:46 +0000499// Instead of subclassing the MCELFStreamer, we do the work here.
500
501void ARMAsmPrinter::emitAttributes() {
Jim Grosbach25cd3bf2010-10-06 22:46:47 +0000502
Jason W Kim109ff292010-10-11 23:01:44 +0000503 emitARMAttributeSection();
504
Jason W Kimbff84d42010-10-06 22:36:46 +0000505 std::string CPUString = Subtarget->getCPUString();
Jason W Kim109ff292010-10-11 23:01:44 +0000506 emitTextAttribute(ARMBuildAttrs::SEL_CPU, CPUString);
Jason W Kimbff84d42010-10-06 22:36:46 +0000507
508 // FIXME: Emit FPU type
509 if (Subtarget->hasVFP2())
510 emitAttribute(ARMBuildAttrs::VFP_arch, 2);
511
512 // Signal various FP modes.
513 if (!UnsafeFPMath) {
514 emitAttribute(ARMBuildAttrs::ABI_FP_denormal, 1);
515 emitAttribute(ARMBuildAttrs::ABI_FP_exceptions, 1);
516 }
517
518 if (NoInfsFPMath && NoNaNsFPMath)
519 emitAttribute(ARMBuildAttrs::ABI_FP_number_model, 1);
520 else
521 emitAttribute(ARMBuildAttrs::ABI_FP_number_model, 3);
522
523 // 8-bytes alignment stuff.
524 emitAttribute(ARMBuildAttrs::ABI_align8_needed, 1);
525 emitAttribute(ARMBuildAttrs::ABI_align8_preserved, 1);
526
527 // Hard float. Use both S and D registers and conform to AAPCS-VFP.
528 if (Subtarget->isAAPCS_ABI() && FloatABIType == FloatABI::Hard) {
529 emitAttribute(ARMBuildAttrs::ABI_HardFP_use, 3);
530 emitAttribute(ARMBuildAttrs::ABI_VFP_args, 1);
531 }
532 // FIXME: Should we signal R9 usage?
533}
534
Jason W Kim109ff292010-10-11 23:01:44 +0000535void ARMAsmPrinter::emitARMAttributeSection() {
536 // <format-version>
537 // [ <section-length> "vendor-name"
538 // [ <file-tag> <size> <attribute>*
539 // | <section-tag> <size> <section-number>* 0 <attribute>*
540 // | <symbol-tag> <size> <symbol-number>* 0 <attribute>*
541 // ]+
542 // ]*
543
544 if (OutStreamer.hasRawTextSupport())
545 return;
546
547 const ARMElfTargetObjectFile &TLOFELF =
548 static_cast<const ARMElfTargetObjectFile &>
549 (getObjFileLowering());
550
551 OutStreamer.SwitchSection(TLOFELF.getAttributesSection());
552 // Fixme: Still more to do here.
553}
554
Jason W Kimbff84d42010-10-06 22:36:46 +0000555void ARMAsmPrinter::emitAttribute(ARMBuildAttrs::AttrType attr, int v) {
556 if (OutStreamer.hasRawTextSupport()) {
Jim Grosbach25cd3bf2010-10-06 22:46:47 +0000557 OutStreamer.EmitRawText("\t.eabi_attribute " +
Jason W Kimbff84d42010-10-06 22:36:46 +0000558 Twine(attr) + ", " + Twine(v));
Jim Grosbach25cd3bf2010-10-06 22:46:47 +0000559
Jason W Kimbff84d42010-10-06 22:36:46 +0000560 } else {
561 assert(0 && "ELF .ARM.attributes unimplemented");
562 }
563}
564
Jason W Kim109ff292010-10-11 23:01:44 +0000565void ARMAsmPrinter::emitTextAttribute(ARMBuildAttrs::SpecialAttr attr,
566 StringRef val) {
567 switch (attr) {
568 default: assert(0 && "Unimplemented ARMBuildAttrs::SpecialAttr"); break;
569 case ARMBuildAttrs::SEL_CPU:
570 if (OutStreamer.hasRawTextSupport()) {
571 if (val != "generic") {
572 OutStreamer.EmitRawText("\t.cpu " + val);
573 }
574 } else {
575 // FIXME: ELF
576 }
577 }
578}
579
Jason W Kimbff84d42010-10-06 22:36:46 +0000580//===----------------------------------------------------------------------===//
Chris Lattner71eb0772009-10-19 20:20:46 +0000581
Jim Grosbachaf5d6352010-09-18 00:05:05 +0000582static MCSymbol *getPICLabel(const char *Prefix, unsigned FunctionNumber,
583 unsigned LabelId, MCContext &Ctx) {
584
585 MCSymbol *Label = Ctx.GetOrCreateSymbol(Twine(Prefix)
586 + "PC" + Twine(FunctionNumber) + "_" + Twine(LabelId));
587 return Label;
588}
589
Jim Grosbach284eebc2010-09-22 17:39:48 +0000590void ARMAsmPrinter::EmitJumpTable(const MachineInstr *MI) {
591 unsigned Opcode = MI->getOpcode();
592 int OpNum = 1;
593 if (Opcode == ARM::BR_JTadd)
594 OpNum = 2;
595 else if (Opcode == ARM::BR_JTm)
596 OpNum = 3;
597
598 const MachineOperand &MO1 = MI->getOperand(OpNum);
599 const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
600 unsigned JTI = MO1.getIndex();
601
602 // Emit a label for the jump table.
603 MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel2(JTI, MO2.getImm());
604 OutStreamer.EmitLabel(JTISymbol);
605
606 // Emit each entry of the table.
607 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
608 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
609 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
610
611 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
612 MachineBasicBlock *MBB = JTBBs[i];
613 // Construct an MCExpr for the entry. We want a value of the form:
614 // (BasicBlockAddr - TableBeginAddr)
615 //
616 // For example, a table with entries jumping to basic blocks BB0 and BB1
617 // would look like:
618 // LJTI_0_0:
619 // .word (LBB0 - LJTI_0_0)
620 // .word (LBB1 - LJTI_0_0)
621 const MCExpr *Expr = MCSymbolRefExpr::Create(MBB->getSymbol(), OutContext);
622
623 if (TM.getRelocationModel() == Reloc::PIC_)
624 Expr = MCBinaryExpr::CreateSub(Expr, MCSymbolRefExpr::Create(JTISymbol,
625 OutContext),
626 OutContext);
627 OutStreamer.EmitValue(Expr, 4);
628 }
629}
630
Jim Grosbachd64f9b82010-09-21 23:28:16 +0000631void ARMAsmPrinter::EmitJump2Table(const MachineInstr *MI) {
632 unsigned Opcode = MI->getOpcode();
633 int OpNum = (Opcode == ARM::t2BR_JT) ? 2 : 1;
634 const MachineOperand &MO1 = MI->getOperand(OpNum);
635 const MachineOperand &MO2 = MI->getOperand(OpNum+1); // Unique Id
636 unsigned JTI = MO1.getIndex();
637
638 // Emit a label for the jump table.
639 MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel2(JTI, MO2.getImm());
640 OutStreamer.EmitLabel(JTISymbol);
641
642 // Emit each entry of the table.
643 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
644 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
645 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
Jim Grosbach1573b292010-09-22 17:15:35 +0000646 unsigned OffsetWidth = 4;
Jim Grosbachd64f9b82010-09-21 23:28:16 +0000647 if (MI->getOpcode() == ARM::t2TBB)
Jim Grosbach1573b292010-09-22 17:15:35 +0000648 OffsetWidth = 1;
Jim Grosbachd64f9b82010-09-21 23:28:16 +0000649 else if (MI->getOpcode() == ARM::t2TBH)
Jim Grosbach1573b292010-09-22 17:15:35 +0000650 OffsetWidth = 2;
Jim Grosbachd64f9b82010-09-21 23:28:16 +0000651
652 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
653 MachineBasicBlock *MBB = JTBBs[i];
Jim Grosbach1573b292010-09-22 17:15:35 +0000654 const MCExpr *MBBSymbolExpr = MCSymbolRefExpr::Create(MBB->getSymbol(),
655 OutContext);
Jim Grosbachd64f9b82010-09-21 23:28:16 +0000656 // If this isn't a TBB or TBH, the entries are direct branch instructions.
Jim Grosbach1573b292010-09-22 17:15:35 +0000657 if (OffsetWidth == 4) {
Jim Grosbachd64f9b82010-09-21 23:28:16 +0000658 MCInst BrInst;
659 BrInst.setOpcode(ARM::t2B);
Jim Grosbach1573b292010-09-22 17:15:35 +0000660 BrInst.addOperand(MCOperand::CreateExpr(MBBSymbolExpr));
Jim Grosbachd64f9b82010-09-21 23:28:16 +0000661 OutStreamer.EmitInstruction(BrInst);
662 continue;
663 }
664 // Otherwise it's an offset from the dispatch instruction. Construct an
Jim Grosbach1573b292010-09-22 17:15:35 +0000665 // MCExpr for the entry. We want a value of the form:
666 // (BasicBlockAddr - TableBeginAddr) / 2
667 //
668 // For example, a TBB table with entries jumping to basic blocks BB0 and BB1
669 // would look like:
670 // LJTI_0_0:
671 // .byte (LBB0 - LJTI_0_0) / 2
672 // .byte (LBB1 - LJTI_0_0) / 2
673 const MCExpr *Expr =
674 MCBinaryExpr::CreateSub(MBBSymbolExpr,
675 MCSymbolRefExpr::Create(JTISymbol, OutContext),
676 OutContext);
677 Expr = MCBinaryExpr::CreateDiv(Expr, MCConstantExpr::Create(2, OutContext),
678 OutContext);
679 OutStreamer.EmitValue(Expr, OffsetWidth);
Jim Grosbachd64f9b82010-09-21 23:28:16 +0000680 }
Jim Grosbach1573b292010-09-22 17:15:35 +0000681
682 // Make sure the instruction that follows TBB is 2-byte aligned.
683 // FIXME: Constant island pass should insert an "ALIGN" instruction instead.
684 if (MI->getOpcode() == ARM::t2TBB)
685 EmitAlignment(1);
Jim Grosbachd64f9b82010-09-21 23:28:16 +0000686}
687
Jim Grosbach45c83d42010-09-28 17:05:56 +0000688void ARMAsmPrinter::PrintDebugValueComment(const MachineInstr *MI,
689 raw_ostream &OS) {
690 unsigned NOps = MI->getNumOperands();
691 assert(NOps==4);
692 OS << '\t' << MAI->getCommentString() << "DEBUG_VALUE: ";
693 // cast away const; DIetc do not take const operands for some reason.
694 DIVariable V(const_cast<MDNode *>(MI->getOperand(NOps-1).getMetadata()));
695 OS << V.getName();
696 OS << " <- ";
697 // Frame address. Currently handles register +- offset only.
698 assert(MI->getOperand(0).isReg() && MI->getOperand(1).isImm());
699 OS << '['; printOperand(MI, 0, OS); OS << '+'; printOperand(MI, 1, OS);
700 OS << ']';
701 OS << "+";
702 printOperand(MI, NOps-2, OS);
703}
704
Jim Grosbach05eccf02010-09-29 15:23:40 +0000705void ARMAsmPrinter::EmitInstruction(const MachineInstr *MI) {
Chris Lattner1b06acb2009-10-20 00:52:47 +0000706 ARMMCInstLower MCInstLowering(OutContext, *Mang, *this);
Chris Lattner71eb0772009-10-19 20:20:46 +0000707 switch (MI->getOpcode()) {
Chris Lattner227767b2009-10-20 05:58:02 +0000708 case ARM::t2MOVi32imm:
709 assert(0 && "Should be lowered by thumb2it pass");
Chris Lattneradd57492009-10-19 22:23:04 +0000710 default: break;
Jim Grosbach45c83d42010-09-28 17:05:56 +0000711 case ARM::DBG_VALUE: {
712 if (isVerbose() && OutStreamer.hasRawTextSupport()) {
713 SmallString<128> TmpStr;
714 raw_svector_ostream OS(TmpStr);
715 PrintDebugValueComment(MI, OS);
716 OutStreamer.EmitRawText(StringRef(OS.str()));
717 }
718 return;
719 }
Jim Grosbach3d979202010-09-17 23:41:53 +0000720 case ARM::tPICADD: {
721 // This is a pseudo op for a label + instruction sequence, which looks like:
722 // LPC0:
723 // add r0, pc
724 // This adds the address of LPC0 to r0.
725
726 // Emit the label.
Jim Grosbachaf5d6352010-09-18 00:05:05 +0000727 OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(),
728 getFunctionNumber(), MI->getOperand(2).getImm(),
729 OutContext));
Jim Grosbach3d979202010-09-17 23:41:53 +0000730
731 // Form and emit the add.
732 MCInst AddInst;
733 AddInst.setOpcode(ARM::tADDhirr);
734 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
735 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
736 AddInst.addOperand(MCOperand::CreateReg(ARM::PC));
737 // Add predicate operands.
738 AddInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
739 AddInst.addOperand(MCOperand::CreateReg(0));
740 OutStreamer.EmitInstruction(AddInst);
741 return;
742 }
Jim Grosbachc8e2e9d2010-09-30 19:53:58 +0000743 case ARM::PICADD: {
Chris Lattneradd57492009-10-19 22:23:04 +0000744 // This is a pseudo op for a label + instruction sequence, which looks like:
745 // LPC0:
746 // add r0, pc, r0
747 // This adds the address of LPC0 to r0.
Jim Grosbach8ee5cd92010-09-02 01:02:06 +0000748
Chris Lattneradd57492009-10-19 22:23:04 +0000749 // Emit the label.
Jim Grosbachaf5d6352010-09-18 00:05:05 +0000750 OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(),
751 getFunctionNumber(), MI->getOperand(2).getImm(),
752 OutContext));
Jim Grosbach8ee5cd92010-09-02 01:02:06 +0000753
Jim Grosbach7ae94222010-09-14 21:05:34 +0000754 // Form and emit the add.
Chris Lattneradd57492009-10-19 22:23:04 +0000755 MCInst AddInst;
756 AddInst.setOpcode(ARM::ADDrr);
757 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
758 AddInst.addOperand(MCOperand::CreateReg(ARM::PC));
759 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
Jim Grosbacha244f702010-09-14 21:28:17 +0000760 // Add predicate operands.
761 AddInst.addOperand(MCOperand::CreateImm(MI->getOperand(3).getImm()));
762 AddInst.addOperand(MCOperand::CreateReg(MI->getOperand(4).getReg()));
763 // Add 's' bit operand (always reg0 for this)
764 AddInst.addOperand(MCOperand::CreateReg(0));
Chris Lattner6f1f8652010-02-03 01:16:28 +0000765 OutStreamer.EmitInstruction(AddInst);
Chris Lattneradd57492009-10-19 22:23:04 +0000766 return;
767 }
Jim Grosbacha7d430b2010-09-17 16:25:52 +0000768 case ARM::PICSTR:
769 case ARM::PICSTRB:
770 case ARM::PICSTRH:
771 case ARM::PICLDR:
772 case ARM::PICLDRB:
773 case ARM::PICLDRH:
774 case ARM::PICLDRSB:
775 case ARM::PICLDRSH: {
Jim Grosbach218e22d2010-09-16 17:43:25 +0000776 // This is a pseudo op for a label + instruction sequence, which looks like:
777 // LPC0:
Jim Grosbacha7d430b2010-09-17 16:25:52 +0000778 // OP r0, [pc, r0]
Jim Grosbach218e22d2010-09-16 17:43:25 +0000779 // The LCP0 label is referenced by a constant pool entry in order to get
780 // a PC-relative address at the ldr instruction.
781
782 // Emit the label.
Jim Grosbachaf5d6352010-09-18 00:05:05 +0000783 OutStreamer.EmitLabel(getPICLabel(MAI->getPrivateGlobalPrefix(),
784 getFunctionNumber(), MI->getOperand(2).getImm(),
785 OutContext));
Jim Grosbach218e22d2010-09-16 17:43:25 +0000786
787 // Form and emit the load
Jim Grosbacha7d430b2010-09-17 16:25:52 +0000788 unsigned Opcode;
789 switch (MI->getOpcode()) {
790 default:
791 llvm_unreachable("Unexpected opcode!");
792 case ARM::PICSTR: Opcode = ARM::STR; break;
793 case ARM::PICSTRB: Opcode = ARM::STRB; break;
794 case ARM::PICSTRH: Opcode = ARM::STRH; break;
795 case ARM::PICLDR: Opcode = ARM::LDR; break;
796 case ARM::PICLDRB: Opcode = ARM::LDRB; break;
797 case ARM::PICLDRH: Opcode = ARM::LDRH; break;
798 case ARM::PICLDRSB: Opcode = ARM::LDRSB; break;
799 case ARM::PICLDRSH: Opcode = ARM::LDRSH; break;
800 }
801 MCInst LdStInst;
802 LdStInst.setOpcode(Opcode);
803 LdStInst.addOperand(MCOperand::CreateReg(MI->getOperand(0).getReg()));
804 LdStInst.addOperand(MCOperand::CreateReg(ARM::PC));
805 LdStInst.addOperand(MCOperand::CreateReg(MI->getOperand(1).getReg()));
806 LdStInst.addOperand(MCOperand::CreateImm(0));
Jim Grosbach218e22d2010-09-16 17:43:25 +0000807 // Add predicate operands.
Jim Grosbacha7d430b2010-09-17 16:25:52 +0000808 LdStInst.addOperand(MCOperand::CreateImm(MI->getOperand(3).getImm()));
809 LdStInst.addOperand(MCOperand::CreateReg(MI->getOperand(4).getReg()));
810 OutStreamer.EmitInstruction(LdStInst);
Jim Grosbach218e22d2010-09-16 17:43:25 +0000811
812 return;
813 }
Jim Grosbachc8e2e9d2010-09-30 19:53:58 +0000814 case ARM::CONSTPOOL_ENTRY: {
Chris Lattner186c6b02009-10-19 22:33:05 +0000815 /// CONSTPOOL_ENTRY - This instruction represents a floating constant pool
816 /// in the function. The first operand is the ID# for this instruction, the
817 /// second is the index into the MachineConstantPool that this is, the third
818 /// is the size in bytes of this constant pool entry.
819 unsigned LabelId = (unsigned)MI->getOperand(0).getImm();
820 unsigned CPIdx = (unsigned)MI->getOperand(1).getIndex();
821
822 EmitAlignment(2);
Chris Lattnerc55ea3f2010-01-23 07:00:21 +0000823 OutStreamer.EmitLabel(GetCPISymbol(LabelId));
Chris Lattner186c6b02009-10-19 22:33:05 +0000824
825 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPIdx];
826 if (MCPE.isMachineConstantPoolEntry())
827 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
828 else
829 EmitGlobalConstant(MCPE.Val.ConstVal);
Jim Grosbach8ee5cd92010-09-02 01:02:06 +0000830
Chris Lattner186c6b02009-10-19 22:33:05 +0000831 return;
832 }
Jim Grosbachc8e2e9d2010-09-30 19:53:58 +0000833 case ARM::MOVi2pieces: {
834 // FIXME: We'd like to remove the asm string in the .td file, but the
Chris Lattner2f69ed82009-10-20 00:40:56 +0000835 // This is a hack that lowers as a two instruction sequence.
836 unsigned DstReg = MI->getOperand(0).getReg();
837 unsigned ImmVal = (unsigned)MI->getOperand(1).getImm();
838
839 unsigned SOImmValV1 = ARM_AM::getSOImmTwoPartFirst(ImmVal);
840 unsigned SOImmValV2 = ARM_AM::getSOImmTwoPartSecond(ImmVal);
Jim Grosbach8ee5cd92010-09-02 01:02:06 +0000841
Chris Lattner2f69ed82009-10-20 00:40:56 +0000842 {
843 MCInst TmpInst;
844 TmpInst.setOpcode(ARM::MOVi);
845 TmpInst.addOperand(MCOperand::CreateReg(DstReg));
846 TmpInst.addOperand(MCOperand::CreateImm(SOImmValV1));
Jim Grosbach8ee5cd92010-09-02 01:02:06 +0000847
Chris Lattner2f69ed82009-10-20 00:40:56 +0000848 // Predicate.
849 TmpInst.addOperand(MCOperand::CreateImm(MI->getOperand(2).getImm()));
850 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(3).getReg()));
Chris Lattner85ab6702009-10-20 00:46:11 +0000851
852 TmpInst.addOperand(MCOperand::CreateReg(0)); // cc_out
Chris Lattner6f1f8652010-02-03 01:16:28 +0000853 OutStreamer.EmitInstruction(TmpInst);
Chris Lattner2f69ed82009-10-20 00:40:56 +0000854 }
855
856 {
857 MCInst TmpInst;
858 TmpInst.setOpcode(ARM::ORRri);
859 TmpInst.addOperand(MCOperand::CreateReg(DstReg)); // dstreg
860 TmpInst.addOperand(MCOperand::CreateReg(DstReg)); // inreg
861 TmpInst.addOperand(MCOperand::CreateImm(SOImmValV2)); // so_imm
862 // Predicate.
863 TmpInst.addOperand(MCOperand::CreateImm(MI->getOperand(2).getImm()));
864 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(3).getReg()));
Jim Grosbach8ee5cd92010-09-02 01:02:06 +0000865
Chris Lattner2f69ed82009-10-20 00:40:56 +0000866 TmpInst.addOperand(MCOperand::CreateReg(0)); // cc_out
Chris Lattner6f1f8652010-02-03 01:16:28 +0000867 OutStreamer.EmitInstruction(TmpInst);
Chris Lattner2f69ed82009-10-20 00:40:56 +0000868 }
Jim Grosbach8ee5cd92010-09-02 01:02:06 +0000869 return;
Chris Lattner2f69ed82009-10-20 00:40:56 +0000870 }
Jim Grosbachc8e2e9d2010-09-30 19:53:58 +0000871 case ARM::MOVi32imm: {
872 // FIXME: We'd like to remove the asm string in the .td file, but the
Chris Lattner484d2e92009-10-20 01:11:37 +0000873 // This is a hack that lowers as a two instruction sequence.
874 unsigned DstReg = MI->getOperand(0).getReg();
Rafael Espindolab69c7b762010-05-12 05:16:34 +0000875 const MachineOperand &MO = MI->getOperand(1);
876 MCOperand V1, V2;
877 if (MO.isImm()) {
878 unsigned ImmVal = (unsigned)MI->getOperand(1).getImm();
879 V1 = MCOperand::CreateImm(ImmVal & 65535);
880 V2 = MCOperand::CreateImm(ImmVal >> 16);
881 } else if (MO.isGlobal()) {
Jim Grosbach0d35df12010-09-17 18:25:25 +0000882 MCSymbol *Symbol = MCInstLowering.GetGlobalAddressSymbol(MO.getGlobal());
Rafael Espindolab69c7b762010-05-12 05:16:34 +0000883 const MCSymbolRefExpr *SymRef1 =
Duncan Sands41b4a6b2010-07-12 08:16:59 +0000884 MCSymbolRefExpr::Create(Symbol,
885 MCSymbolRefExpr::VK_ARM_LO16, OutContext);
Rafael Espindolab69c7b762010-05-12 05:16:34 +0000886 const MCSymbolRefExpr *SymRef2 =
Duncan Sands41b4a6b2010-07-12 08:16:59 +0000887 MCSymbolRefExpr::Create(Symbol,
888 MCSymbolRefExpr::VK_ARM_HI16, OutContext);
Rafael Espindolab69c7b762010-05-12 05:16:34 +0000889 V1 = MCOperand::CreateExpr(SymRef1);
890 V2 = MCOperand::CreateExpr(SymRef2);
891 } else {
Jim Grosbach1f57cc42010-09-22 20:55:15 +0000892 // FIXME: External symbol?
Rafael Espindolab69c7b762010-05-12 05:16:34 +0000893 MI->dump();
894 llvm_unreachable("cannot handle this operand");
895 }
896
Chris Lattner484d2e92009-10-20 01:11:37 +0000897 {
898 MCInst TmpInst;
899 TmpInst.setOpcode(ARM::MOVi16);
900 TmpInst.addOperand(MCOperand::CreateReg(DstReg)); // dstreg
Rafael Espindolab69c7b762010-05-12 05:16:34 +0000901 TmpInst.addOperand(V1); // lower16(imm)
Jim Grosbach8ee5cd92010-09-02 01:02:06 +0000902
Chris Lattner484d2e92009-10-20 01:11:37 +0000903 // Predicate.
904 TmpInst.addOperand(MCOperand::CreateImm(MI->getOperand(2).getImm()));
905 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(3).getReg()));
Jim Grosbach8ee5cd92010-09-02 01:02:06 +0000906
Chris Lattner6f1f8652010-02-03 01:16:28 +0000907 OutStreamer.EmitInstruction(TmpInst);
Chris Lattner484d2e92009-10-20 01:11:37 +0000908 }
Jim Grosbach8ee5cd92010-09-02 01:02:06 +0000909
Chris Lattner484d2e92009-10-20 01:11:37 +0000910 {
911 MCInst TmpInst;
912 TmpInst.setOpcode(ARM::MOVTi16);
913 TmpInst.addOperand(MCOperand::CreateReg(DstReg)); // dstreg
914 TmpInst.addOperand(MCOperand::CreateReg(DstReg)); // srcreg
Rafael Espindolab69c7b762010-05-12 05:16:34 +0000915 TmpInst.addOperand(V2); // upper16(imm)
Jim Grosbach8ee5cd92010-09-02 01:02:06 +0000916
Chris Lattner484d2e92009-10-20 01:11:37 +0000917 // Predicate.
918 TmpInst.addOperand(MCOperand::CreateImm(MI->getOperand(2).getImm()));
919 TmpInst.addOperand(MCOperand::CreateReg(MI->getOperand(3).getReg()));
Jim Grosbach8ee5cd92010-09-02 01:02:06 +0000920
Chris Lattner6f1f8652010-02-03 01:16:28 +0000921 OutStreamer.EmitInstruction(TmpInst);
Chris Lattner484d2e92009-10-20 01:11:37 +0000922 }
Jim Grosbach8ee5cd92010-09-02 01:02:06 +0000923
Chris Lattner484d2e92009-10-20 01:11:37 +0000924 return;
925 }
Jim Grosbachd64f9b82010-09-21 23:28:16 +0000926 case ARM::t2TBB:
927 case ARM::t2TBH:
928 case ARM::t2BR_JT: {
929 // Lower and emit the instruction itself, then the jump table following it.
930 MCInst TmpInst;
931 MCInstLowering.Lower(MI, TmpInst);
932 OutStreamer.EmitInstruction(TmpInst);
Jim Grosbachd64f9b82010-09-21 23:28:16 +0000933 EmitJump2Table(MI);
934 return;
935 }
936 case ARM::tBR_JTr:
937 case ARM::BR_JTr:
938 case ARM::BR_JTm:
Jim Grosbach284eebc2010-09-22 17:39:48 +0000939 case ARM::BR_JTadd: {
940 // Lower and emit the instruction itself, then the jump table following it.
941 MCInst TmpInst;
942 MCInstLowering.Lower(MI, TmpInst);
943 OutStreamer.EmitInstruction(TmpInst);
944 EmitJumpTable(MI);
945 return;
946 }
Jim Grosbach85030542010-09-23 18:05:37 +0000947 case ARM::TRAP: {
948 // Non-Darwin binutils don't yet support the "trap" mnemonic.
949 // FIXME: Remove this special case when they do.
950 if (!Subtarget->isTargetDarwin()) {
Jim Grosbachfae83052010-10-01 23:21:38 +0000951 //.long 0xe7ffdefe @ trap
Jim Grosbach7d348372010-09-23 19:42:17 +0000952 uint32_t Val = 0xe7ffdefeUL;
Jim Grosbach85030542010-09-23 18:05:37 +0000953 OutStreamer.AddComment("trap");
954 OutStreamer.EmitIntValue(Val, 4);
955 return;
956 }
957 break;
958 }
959 case ARM::tTRAP: {
960 // Non-Darwin binutils don't yet support the "trap" mnemonic.
961 // FIXME: Remove this special case when they do.
962 if (!Subtarget->isTargetDarwin()) {
Jim Grosbachfae83052010-10-01 23:21:38 +0000963 //.short 57086 @ trap
Benjamin Kramere38495d2010-09-23 18:57:26 +0000964 uint16_t Val = 0xdefe;
Jim Grosbach85030542010-09-23 18:05:37 +0000965 OutStreamer.AddComment("trap");
966 OutStreamer.EmitIntValue(Val, 2);
967 return;
968 }
969 break;
970 }
Jim Grosbach4a6ab132010-09-24 20:47:58 +0000971 case ARM::t2Int_eh_sjlj_setjmp:
972 case ARM::t2Int_eh_sjlj_setjmp_nofp:
Jim Grosbachc8e2e9d2010-09-30 19:53:58 +0000973 case ARM::tInt_eh_sjlj_setjmp: {
Jim Grosbach4a6ab132010-09-24 20:47:58 +0000974 // Two incoming args: GPR:$src, GPR:$val
975 // mov $val, pc
976 // adds $val, #7
977 // str $val, [$src, #4]
978 // movs r0, #0
979 // b 1f
980 // movs r0, #1
981 // 1:
982 unsigned SrcReg = MI->getOperand(0).getReg();
983 unsigned ValReg = MI->getOperand(1).getReg();
984 MCSymbol *Label = GetARMSJLJEHLabel();
985 {
986 MCInst TmpInst;
987 TmpInst.setOpcode(ARM::tMOVgpr2tgpr);
988 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
989 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
990 // 's' bit operand
991 TmpInst.addOperand(MCOperand::CreateReg(ARM::CPSR));
992 OutStreamer.AddComment("eh_setjmp begin");
993 OutStreamer.EmitInstruction(TmpInst);
994 }
995 {
996 MCInst TmpInst;
997 TmpInst.setOpcode(ARM::tADDi3);
998 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
999 // 's' bit operand
1000 TmpInst.addOperand(MCOperand::CreateReg(ARM::CPSR));
1001 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1002 TmpInst.addOperand(MCOperand::CreateImm(7));
1003 // Predicate.
1004 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1005 TmpInst.addOperand(MCOperand::CreateReg(0));
1006 OutStreamer.EmitInstruction(TmpInst);
1007 }
1008 {
1009 MCInst TmpInst;
1010 TmpInst.setOpcode(ARM::tSTR);
1011 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1012 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
1013 // The offset immediate is #4. The operand value is scaled by 4 for the
1014 // tSTR instruction.
1015 TmpInst.addOperand(MCOperand::CreateImm(1));
1016 TmpInst.addOperand(MCOperand::CreateReg(0));
1017 // Predicate.
1018 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1019 TmpInst.addOperand(MCOperand::CreateReg(0));
1020 OutStreamer.EmitInstruction(TmpInst);
1021 }
1022 {
1023 MCInst TmpInst;
1024 TmpInst.setOpcode(ARM::tMOVi8);
1025 TmpInst.addOperand(MCOperand::CreateReg(ARM::R0));
1026 TmpInst.addOperand(MCOperand::CreateReg(ARM::CPSR));
1027 TmpInst.addOperand(MCOperand::CreateImm(0));
1028 // Predicate.
1029 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1030 TmpInst.addOperand(MCOperand::CreateReg(0));
1031 OutStreamer.EmitInstruction(TmpInst);
1032 }
1033 {
1034 const MCExpr *SymbolExpr = MCSymbolRefExpr::Create(Label, OutContext);
1035 MCInst TmpInst;
1036 TmpInst.setOpcode(ARM::tB);
1037 TmpInst.addOperand(MCOperand::CreateExpr(SymbolExpr));
1038 OutStreamer.EmitInstruction(TmpInst);
1039 }
1040 {
1041 MCInst TmpInst;
1042 TmpInst.setOpcode(ARM::tMOVi8);
1043 TmpInst.addOperand(MCOperand::CreateReg(ARM::R0));
1044 TmpInst.addOperand(MCOperand::CreateReg(ARM::CPSR));
1045 TmpInst.addOperand(MCOperand::CreateImm(1));
1046 // Predicate.
1047 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1048 TmpInst.addOperand(MCOperand::CreateReg(0));
1049 OutStreamer.AddComment("eh_setjmp end");
1050 OutStreamer.EmitInstruction(TmpInst);
1051 }
1052 OutStreamer.EmitLabel(Label);
1053 return;
1054 }
1055
Jim Grosbachc0aed712010-09-23 23:33:56 +00001056 case ARM::Int_eh_sjlj_setjmp_nofp:
Jim Grosbachc8e2e9d2010-09-30 19:53:58 +00001057 case ARM::Int_eh_sjlj_setjmp: {
Jim Grosbachc0aed712010-09-23 23:33:56 +00001058 // Two incoming args: GPR:$src, GPR:$val
1059 // add $val, pc, #8
1060 // str $val, [$src, #+4]
1061 // mov r0, #0
1062 // add pc, pc, #0
1063 // mov r0, #1
1064 unsigned SrcReg = MI->getOperand(0).getReg();
1065 unsigned ValReg = MI->getOperand(1).getReg();
1066
1067 {
1068 MCInst TmpInst;
1069 TmpInst.setOpcode(ARM::ADDri);
1070 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1071 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1072 TmpInst.addOperand(MCOperand::CreateImm(8));
1073 // Predicate.
1074 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1075 TmpInst.addOperand(MCOperand::CreateReg(0));
1076 // 's' bit operand (always reg0 for this).
1077 TmpInst.addOperand(MCOperand::CreateReg(0));
1078 OutStreamer.AddComment("eh_setjmp begin");
1079 OutStreamer.EmitInstruction(TmpInst);
1080 }
1081 {
1082 MCInst TmpInst;
1083 TmpInst.setOpcode(ARM::STR);
1084 TmpInst.addOperand(MCOperand::CreateReg(ValReg));
1085 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
1086 TmpInst.addOperand(MCOperand::CreateReg(0));
1087 TmpInst.addOperand(MCOperand::CreateImm(4));
1088 // Predicate.
1089 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1090 TmpInst.addOperand(MCOperand::CreateReg(0));
1091 OutStreamer.EmitInstruction(TmpInst);
1092 }
1093 {
1094 MCInst TmpInst;
1095 TmpInst.setOpcode(ARM::MOVi);
1096 TmpInst.addOperand(MCOperand::CreateReg(ARM::R0));
1097 TmpInst.addOperand(MCOperand::CreateImm(0));
1098 // Predicate.
1099 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1100 TmpInst.addOperand(MCOperand::CreateReg(0));
1101 // 's' bit operand (always reg0 for this).
1102 TmpInst.addOperand(MCOperand::CreateReg(0));
1103 OutStreamer.EmitInstruction(TmpInst);
1104 }
1105 {
1106 MCInst TmpInst;
1107 TmpInst.setOpcode(ARM::ADDri);
1108 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1109 TmpInst.addOperand(MCOperand::CreateReg(ARM::PC));
1110 TmpInst.addOperand(MCOperand::CreateImm(0));
1111 // Predicate.
1112 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1113 TmpInst.addOperand(MCOperand::CreateReg(0));
1114 // 's' bit operand (always reg0 for this).
1115 TmpInst.addOperand(MCOperand::CreateReg(0));
1116 OutStreamer.EmitInstruction(TmpInst);
1117 }
1118 {
1119 MCInst TmpInst;
1120 TmpInst.setOpcode(ARM::MOVi);
1121 TmpInst.addOperand(MCOperand::CreateReg(ARM::R0));
1122 TmpInst.addOperand(MCOperand::CreateImm(1));
1123 // Predicate.
1124 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1125 TmpInst.addOperand(MCOperand::CreateReg(0));
1126 // 's' bit operand (always reg0 for this).
1127 TmpInst.addOperand(MCOperand::CreateReg(0));
1128 OutStreamer.AddComment("eh_setjmp end");
1129 OutStreamer.EmitInstruction(TmpInst);
1130 }
1131 return;
1132 }
Jim Grosbach9e9ed982010-09-27 21:47:04 +00001133 case ARM::Int_eh_sjlj_longjmp: {
1134 // ldr sp, [$src, #8]
1135 // ldr $scratch, [$src, #4]
1136 // ldr r7, [$src]
1137 // bx $scratch
1138 unsigned SrcReg = MI->getOperand(0).getReg();
1139 unsigned ScratchReg = MI->getOperand(1).getReg();
1140 {
1141 MCInst TmpInst;
1142 TmpInst.setOpcode(ARM::LDR);
1143 TmpInst.addOperand(MCOperand::CreateReg(ARM::SP));
1144 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
1145 TmpInst.addOperand(MCOperand::CreateReg(0));
1146 TmpInst.addOperand(MCOperand::CreateImm(8));
1147 // Predicate.
1148 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1149 TmpInst.addOperand(MCOperand::CreateReg(0));
1150 OutStreamer.EmitInstruction(TmpInst);
1151 }
1152 {
1153 MCInst TmpInst;
1154 TmpInst.setOpcode(ARM::LDR);
1155 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1156 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
1157 TmpInst.addOperand(MCOperand::CreateReg(0));
1158 TmpInst.addOperand(MCOperand::CreateImm(4));
1159 // Predicate.
1160 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1161 TmpInst.addOperand(MCOperand::CreateReg(0));
1162 OutStreamer.EmitInstruction(TmpInst);
1163 }
1164 {
1165 MCInst TmpInst;
1166 TmpInst.setOpcode(ARM::LDR);
1167 TmpInst.addOperand(MCOperand::CreateReg(ARM::R7));
1168 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
1169 TmpInst.addOperand(MCOperand::CreateReg(0));
1170 TmpInst.addOperand(MCOperand::CreateImm(0));
1171 // Predicate.
1172 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1173 TmpInst.addOperand(MCOperand::CreateReg(0));
1174 OutStreamer.EmitInstruction(TmpInst);
1175 }
1176 {
1177 MCInst TmpInst;
1178 TmpInst.setOpcode(ARM::BRIND);
1179 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1180 // Predicate.
1181 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1182 TmpInst.addOperand(MCOperand::CreateReg(0));
1183 OutStreamer.EmitInstruction(TmpInst);
1184 }
1185 return;
1186 }
Jim Grosbach175d6412010-09-27 22:28:11 +00001187 case ARM::tInt_eh_sjlj_longjmp: {
1188 // ldr $scratch, [$src, #8]
1189 // mov sp, $scratch
1190 // ldr $scratch, [$src, #4]
1191 // ldr r7, [$src]
1192 // bx $scratch
1193 unsigned SrcReg = MI->getOperand(0).getReg();
1194 unsigned ScratchReg = MI->getOperand(1).getReg();
1195 {
1196 MCInst TmpInst;
1197 TmpInst.setOpcode(ARM::tLDR);
1198 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1199 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
1200 // The offset immediate is #8. The operand value is scaled by 4 for the
1201 // tSTR instruction.
1202 TmpInst.addOperand(MCOperand::CreateImm(2));
1203 TmpInst.addOperand(MCOperand::CreateReg(0));
1204 // Predicate.
1205 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1206 TmpInst.addOperand(MCOperand::CreateReg(0));
1207 OutStreamer.EmitInstruction(TmpInst);
1208 }
1209 {
1210 MCInst TmpInst;
1211 TmpInst.setOpcode(ARM::tMOVtgpr2gpr);
1212 TmpInst.addOperand(MCOperand::CreateReg(ARM::SP));
1213 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1214 // Predicate.
1215 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1216 TmpInst.addOperand(MCOperand::CreateReg(0));
1217 OutStreamer.EmitInstruction(TmpInst);
1218 }
1219 {
1220 MCInst TmpInst;
1221 TmpInst.setOpcode(ARM::tLDR);
1222 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1223 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
1224 TmpInst.addOperand(MCOperand::CreateImm(1));
1225 TmpInst.addOperand(MCOperand::CreateReg(0));
1226 // Predicate.
1227 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1228 TmpInst.addOperand(MCOperand::CreateReg(0));
1229 OutStreamer.EmitInstruction(TmpInst);
1230 }
1231 {
1232 MCInst TmpInst;
1233 TmpInst.setOpcode(ARM::tLDR);
1234 TmpInst.addOperand(MCOperand::CreateReg(ARM::R7));
1235 TmpInst.addOperand(MCOperand::CreateReg(SrcReg));
1236 TmpInst.addOperand(MCOperand::CreateImm(0));
1237 TmpInst.addOperand(MCOperand::CreateReg(0));
1238 // Predicate.
1239 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1240 TmpInst.addOperand(MCOperand::CreateReg(0));
1241 OutStreamer.EmitInstruction(TmpInst);
1242 }
1243 {
1244 MCInst TmpInst;
1245 TmpInst.setOpcode(ARM::tBX_RET_vararg);
1246 TmpInst.addOperand(MCOperand::CreateReg(ScratchReg));
1247 // Predicate.
1248 TmpInst.addOperand(MCOperand::CreateImm(ARMCC::AL));
1249 TmpInst.addOperand(MCOperand::CreateReg(0));
1250 OutStreamer.EmitInstruction(TmpInst);
1251 }
1252 return;
1253 }
Chris Lattner71eb0772009-10-19 20:20:46 +00001254 }
Jim Grosbach8ee5cd92010-09-02 01:02:06 +00001255
Chris Lattner71eb0772009-10-19 20:20:46 +00001256 MCInst TmpInst;
1257 MCInstLowering.Lower(MI, TmpInst);
Chris Lattner6f1f8652010-02-03 01:16:28 +00001258 OutStreamer.EmitInstruction(TmpInst);
Chris Lattner71eb0772009-10-19 20:20:46 +00001259}
Daniel Dunbarf0b3d152009-10-20 05:15:36 +00001260
1261//===----------------------------------------------------------------------===//
1262// Target Registry Stuff
1263//===----------------------------------------------------------------------===//
1264
1265static MCInstPrinter *createARMMCInstPrinter(const Target &T,
1266 unsigned SyntaxVariant,
Chris Lattner70129162010-04-04 05:04:31 +00001267 const MCAsmInfo &MAI) {
Daniel Dunbarf0b3d152009-10-20 05:15:36 +00001268 if (SyntaxVariant == 0)
Jim Grosbach132a0ce2010-09-17 21:33:25 +00001269 return new ARMInstPrinter(MAI);
Daniel Dunbarf0b3d152009-10-20 05:15:36 +00001270 return 0;
1271}
1272
1273// Force static initialization.
1274extern "C" void LLVMInitializeARMAsmPrinter() {
1275 RegisterAsmPrinter<ARMAsmPrinter> X(TheARMTarget);
1276 RegisterAsmPrinter<ARMAsmPrinter> Y(TheThumbTarget);
1277
1278 TargetRegistry::RegisterMCInstPrinter(TheARMTarget, createARMMCInstPrinter);
1279 TargetRegistry::RegisterMCInstPrinter(TheThumbTarget, createARMMCInstPrinter);
1280}
1281