[arm] Add softvfp to supported FPU names.
[oota-llvm.git] / lib / Target / ARM / MCTargetDesc / ARMELFStreamer.cpp
1 //===- lib/MC/ARMELFStreamer.cpp - ELF Object Output for ARM --------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file assembles .s files and emits ARM ELF .o object files. Different
11 // from generic ELF streamer in emitting mapping symbols ($a, $t and $d) to
12 // delimit regions of data and code.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "ARMBuildAttrs.h"
17 #include "ARMArchName.h"
18 #include "ARMFPUName.h"
19 #include "ARMRegisterInfo.h"
20 #include "ARMUnwindOp.h"
21 #include "ARMUnwindOpAsm.h"
22 #include "llvm/ADT/SmallPtrSet.h"
23 #include "llvm/ADT/StringExtras.h"
24 #include "llvm/ADT/Twine.h"
25 #include "llvm/MC/MCAsmBackend.h"
26 #include "llvm/MC/MCAsmInfo.h"
27 #include "llvm/MC/MCAssembler.h"
28 #include "llvm/MC/MCCodeEmitter.h"
29 #include "llvm/MC/MCContext.h"
30 #include "llvm/MC/MCELF.h"
31 #include "llvm/MC/MCELFStreamer.h"
32 #include "llvm/MC/MCELFSymbolFlags.h"
33 #include "llvm/MC/MCExpr.h"
34 #include "llvm/MC/MCInst.h"
35 #include "llvm/MC/MCInstPrinter.h"
36 #include "llvm/MC/MCObjectStreamer.h"
37 #include "llvm/MC/MCRegisterInfo.h"
38 #include "llvm/MC/MCSection.h"
39 #include "llvm/MC/MCSectionELF.h"
40 #include "llvm/MC/MCStreamer.h"
41 #include "llvm/MC/MCSymbol.h"
42 #include "llvm/MC/MCValue.h"
43 #include "llvm/Support/Debug.h"
44 #include "llvm/Support/ELF.h"
45 #include "llvm/Support/FormattedStream.h"
46 #include "llvm/Support/raw_ostream.h"
47 #include <algorithm>
48
49 using namespace llvm;
50
51 static std::string GetAEABIUnwindPersonalityName(unsigned Index) {
52   assert(Index < NUM_PERSONALITY_INDEX && "Invalid personality index");
53   return (Twine("__aeabi_unwind_cpp_pr") + Twine(Index)).str();
54 }
55
56 static const char *GetFPUName(unsigned ID) {
57   switch (ID) {
58   default:
59     llvm_unreachable("Unknown FPU kind");
60     break;
61 #define ARM_FPU_NAME(NAME, ID) case ARM::ID: return NAME;
62 #include "ARMFPUName.def"
63   }
64   return NULL;
65 }
66
67 static const char *GetArchName(unsigned ID) {
68   switch (ID) {
69   default:
70     llvm_unreachable("Unknown ARCH kind");
71     break;
72 #define ARM_ARCH_NAME(NAME, ID, DEFAULT_CPU_NAME, DEFAULT_CPU_ARCH) \
73   case ARM::ID: return NAME;
74 #define ARM_ARCH_ALIAS(NAME, ID) /* empty */
75 #include "ARMArchName.def"
76   }
77   return NULL;
78 }
79
80 static const char *GetArchDefaultCPUName(unsigned ID) {
81   switch (ID) {
82   default:
83     llvm_unreachable("Unknown ARCH kind");
84     break;
85 #define ARM_ARCH_NAME(NAME, ID, DEFAULT_CPU_NAME, DEFAULT_CPU_ARCH) \
86   case ARM::ID: return DEFAULT_CPU_NAME;
87 #define ARM_ARCH_ALIAS(NAME, ID) /* empty */
88 #include "ARMArchName.def"
89   }
90   return NULL;
91 }
92
93 static unsigned GetArchDefaultCPUArch(unsigned ID) {
94   switch (ID) {
95   default:
96     llvm_unreachable("Unknown ARCH kind");
97     break;
98 #define ARM_ARCH_NAME(NAME, ID, DEFAULT_CPU_NAME, DEFAULT_CPU_ARCH) \
99   case ARM::ID: return ARMBuildAttrs::DEFAULT_CPU_ARCH;
100 #define ARM_ARCH_ALIAS(NAME, ID) /* empty */
101 #include "ARMArchName.def"
102   }
103   return 0;
104 }
105
106 namespace {
107
108 class ARMELFStreamer;
109
110 class ARMTargetAsmStreamer : public ARMTargetStreamer {
111   formatted_raw_ostream &OS;
112   MCInstPrinter &InstPrinter;
113
114   virtual void emitFnStart();
115   virtual void emitFnEnd();
116   virtual void emitCantUnwind();
117   virtual void emitPersonality(const MCSymbol *Personality);
118   virtual void emitHandlerData();
119   virtual void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0);
120   virtual void emitPad(int64_t Offset);
121   virtual void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
122                            bool isVector);
123
124   virtual void switchVendor(StringRef Vendor);
125   virtual void emitAttribute(unsigned Attribute, unsigned Value);
126   virtual void emitTextAttribute(unsigned Attribute, StringRef String);
127   virtual void emitArch(unsigned Arch);
128   virtual void emitFPU(unsigned FPU);
129   virtual void emitInst(uint32_t Inst, char Suffix = '\0');
130   virtual void finishAttributeSection();
131
132 public:
133   ARMTargetAsmStreamer(formatted_raw_ostream &OS, MCInstPrinter &InstPrinter);
134 };
135
136 ARMTargetAsmStreamer::ARMTargetAsmStreamer(formatted_raw_ostream &OS,
137                                            MCInstPrinter &InstPrinter)
138     : OS(OS), InstPrinter(InstPrinter) {}
139 void ARMTargetAsmStreamer::emitFnStart() { OS << "\t.fnstart\n"; }
140 void ARMTargetAsmStreamer::emitFnEnd() { OS << "\t.fnend\n"; }
141 void ARMTargetAsmStreamer::emitCantUnwind() { OS << "\t.cantunwind\n"; }
142 void ARMTargetAsmStreamer::emitPersonality(const MCSymbol *Personality) {
143   OS << "\t.personality " << Personality->getName() << '\n';
144 }
145 void ARMTargetAsmStreamer::emitHandlerData() { OS << "\t.handlerdata\n"; }
146 void ARMTargetAsmStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
147                                      int64_t Offset) {
148   OS << "\t.setfp\t";
149   InstPrinter.printRegName(OS, FpReg);
150   OS << ", ";
151   InstPrinter.printRegName(OS, SpReg);
152   if (Offset)
153     OS << ", #" << Offset;
154   OS << '\n';
155 }
156 void ARMTargetAsmStreamer::emitPad(int64_t Offset) {
157   OS << "\t.pad\t#" << Offset << '\n';
158 }
159 void ARMTargetAsmStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
160                                        bool isVector) {
161   assert(RegList.size() && "RegList should not be empty");
162   if (isVector)
163     OS << "\t.vsave\t{";
164   else
165     OS << "\t.save\t{";
166
167   InstPrinter.printRegName(OS, RegList[0]);
168
169   for (unsigned i = 1, e = RegList.size(); i != e; ++i) {
170     OS << ", ";
171     InstPrinter.printRegName(OS, RegList[i]);
172   }
173
174   OS << "}\n";
175 }
176 void ARMTargetAsmStreamer::switchVendor(StringRef Vendor) {
177 }
178 void ARMTargetAsmStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
179   OS << "\t.eabi_attribute\t" << Attribute << ", " << Twine(Value) << "\n";
180 }
181 void ARMTargetAsmStreamer::emitTextAttribute(unsigned Attribute,
182                                              StringRef String) {
183   switch (Attribute) {
184   default: llvm_unreachable("Unsupported Text attribute in ASM Mode");
185   case ARMBuildAttrs::CPU_name:
186     OS << "\t.cpu\t" << String.lower() << "\n";
187     break;
188   }
189 }
190 void ARMTargetAsmStreamer::emitArch(unsigned Arch) {
191   OS << "\t.arch\t" << GetArchName(Arch) << "\n";
192 }
193 void ARMTargetAsmStreamer::emitFPU(unsigned FPU) {
194   OS << "\t.fpu\t" << GetFPUName(FPU) << "\n";
195 }
196 void ARMTargetAsmStreamer::finishAttributeSection() {
197 }
198
199 void ARMTargetAsmStreamer::emitInst(uint32_t Inst, char Suffix) {
200   OS << "\t.inst";
201   if (Suffix)
202     OS << "." << Suffix;
203   OS << "\t0x" << utohexstr(Inst) << "\n";
204 }
205
206 class ARMTargetELFStreamer : public ARMTargetStreamer {
207 private:
208   // This structure holds all attributes, accounting for
209   // their string/numeric value, so we can later emmit them
210   // in declaration order, keeping all in the same vector
211   struct AttributeItem {
212     enum {
213       HiddenAttribute = 0,
214       NumericAttribute,
215       TextAttribute
216     } Type;
217     unsigned Tag;
218     unsigned IntValue;
219     StringRef StringValue;
220
221     static bool LessTag(const AttributeItem &LHS, const AttributeItem &RHS) {
222       return (LHS.Tag < RHS.Tag);
223     }
224   };
225
226   StringRef CurrentVendor;
227   unsigned FPU;
228   unsigned Arch;
229   SmallVector<AttributeItem, 64> Contents;
230
231   const MCSection *AttributeSection;
232
233   // FIXME: this should be in a more generic place, but
234   // getULEBSize() is in MCAsmInfo and will be moved to MCDwarf
235   static size_t getULEBSize(int Value) {
236     size_t Size = 0;
237     do {
238       Value >>= 7;
239       Size += sizeof(int8_t); // Is this really necessary?
240     } while (Value);
241     return Size;
242   }
243
244   AttributeItem *getAttributeItem(unsigned Attribute) {
245     for (size_t i = 0; i < Contents.size(); ++i)
246       if (Contents[i].Tag == Attribute)
247         return &Contents[i];
248     return 0;
249   }
250
251   void setAttributeItem(unsigned Attribute, unsigned Value,
252                         bool OverwriteExisting) {
253     // Look for existing attribute item
254     if (AttributeItem *Item = getAttributeItem(Attribute)) {
255       if (!OverwriteExisting)
256         return;
257       Item->IntValue = Value;
258       return;
259     }
260
261     // Create new attribute item
262     AttributeItem Item = {
263       AttributeItem::NumericAttribute,
264       Attribute,
265       Value,
266       StringRef("")
267     };
268     Contents.push_back(Item);
269   }
270
271   void setAttributeItem(unsigned Attribute, StringRef Value,
272                         bool OverwriteExisting) {
273     // Look for existing attribute item
274     if (AttributeItem *Item = getAttributeItem(Attribute)) {
275       if (!OverwriteExisting)
276         return;
277       Item->StringValue = Value;
278       return;
279     }
280
281     // Create new attribute item
282     AttributeItem Item = {
283       AttributeItem::TextAttribute,
284       Attribute,
285       0,
286       Value
287     };
288     Contents.push_back(Item);
289   }
290
291   void emitArchDefaultAttributes();
292   void emitFPUDefaultAttributes();
293
294   ARMELFStreamer &getStreamer();
295
296   virtual void emitFnStart();
297   virtual void emitFnEnd();
298   virtual void emitCantUnwind();
299   virtual void emitPersonality(const MCSymbol *Personality);
300   virtual void emitHandlerData();
301   virtual void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0);
302   virtual void emitPad(int64_t Offset);
303   virtual void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
304                            bool isVector);
305
306   virtual void switchVendor(StringRef Vendor);
307   virtual void emitAttribute(unsigned Attribute, unsigned Value);
308   virtual void emitTextAttribute(unsigned Attribute, StringRef String);
309   virtual void emitArch(unsigned Arch);
310   virtual void emitFPU(unsigned FPU);
311   virtual void emitInst(uint32_t Inst, char Suffix = '\0');
312   virtual void finishAttributeSection();
313
314   size_t calculateContentSize() const;
315
316 public:
317   ARMTargetELFStreamer()
318     : ARMTargetStreamer(), CurrentVendor("aeabi"), FPU(ARM::INVALID_FPU),
319       Arch(ARM::INVALID_ARCH), AttributeSection(0) {
320   }
321 };
322
323 /// Extend the generic ELFStreamer class so that it can emit mapping symbols at
324 /// the appropriate points in the object files. These symbols are defined in the
325 /// ARM ELF ABI: infocenter.arm.com/help/topic/com.arm.../IHI0044D_aaelf.pdf.
326 ///
327 /// In brief: $a, $t or $d should be emitted at the start of each contiguous
328 /// region of ARM code, Thumb code or data in a section. In practice, this
329 /// emission does not rely on explicit assembler directives but on inherent
330 /// properties of the directives doing the emission (e.g. ".byte" is data, "add
331 /// r0, r0, r0" an instruction).
332 ///
333 /// As a result this system is orthogonal to the DataRegion infrastructure used
334 /// by MachO. Beware!
335 class ARMELFStreamer : public MCELFStreamer {
336 public:
337   friend class ARMTargetELFStreamer;
338
339   ARMELFStreamer(MCContext &Context, MCTargetStreamer *TargetStreamer,
340                  MCAsmBackend &TAB, raw_ostream &OS, MCCodeEmitter *Emitter,
341                  bool IsThumb)
342       : MCELFStreamer(Context, TargetStreamer, TAB, OS, Emitter),
343         IsThumb(IsThumb), MappingSymbolCounter(0), LastEMS(EMS_None) {
344     Reset();
345   }
346
347   ~ARMELFStreamer() {}
348
349   virtual void FinishImpl();
350
351   // ARM exception handling directives
352   void emitFnStart();
353   void emitFnEnd();
354   void emitCantUnwind();
355   void emitPersonality(const MCSymbol *Per);
356   void emitHandlerData();
357   void emitSetFP(unsigned NewFpReg, unsigned NewSpReg, int64_t Offset = 0);
358   void emitPad(int64_t Offset);
359   void emitRegSave(const SmallVectorImpl<unsigned> &RegList, bool isVector);
360
361   virtual void ChangeSection(const MCSection *Section,
362                              const MCExpr *Subsection) {
363     // We have to keep track of the mapping symbol state of any sections we
364     // use. Each one should start off as EMS_None, which is provided as the
365     // default constructor by DenseMap::lookup.
366     LastMappingSymbols[getPreviousSection().first] = LastEMS;
367     LastEMS = LastMappingSymbols.lookup(Section);
368
369     MCELFStreamer::ChangeSection(Section, Subsection);
370   }
371
372   /// This function is the one used to emit instruction data into the ELF
373   /// streamer. We override it to add the appropriate mapping symbol if
374   /// necessary.
375   virtual void EmitInstruction(const MCInst& Inst) {
376     if (IsThumb)
377       EmitThumbMappingSymbol();
378     else
379       EmitARMMappingSymbol();
380
381     MCELFStreamer::EmitInstruction(Inst);
382   }
383
384   virtual void emitInst(uint32_t Inst, char Suffix) {
385     unsigned Size;
386     char Buffer[4];
387     const bool LittleEndian = getContext().getAsmInfo()->isLittleEndian();
388
389     switch (Suffix) {
390     case '\0':
391       Size = 4;
392
393       assert(!IsThumb);
394       EmitARMMappingSymbol();
395       for (unsigned II = 0, IE = Size; II != IE; II++) {
396         const unsigned I = LittleEndian ? (Size - II - 1) : II;
397         Buffer[Size - II - 1] = uint8_t(Inst >> I * CHAR_BIT);
398       }
399
400       break;
401     case 'n':
402     case 'w':
403       Size = (Suffix == 'n' ? 2 : 4);
404
405       assert(IsThumb);
406       EmitThumbMappingSymbol();
407       for (unsigned II = 0, IE = Size; II != IE; II = II + 2) {
408         const unsigned I0 = LittleEndian ? II + 0 : (Size - II - 1);
409         const unsigned I1 = LittleEndian ? II + 1 : (Size - II - 2);
410         Buffer[Size - II - 2] = uint8_t(Inst >> I0 * CHAR_BIT);
411         Buffer[Size - II - 1] = uint8_t(Inst >> I1 * CHAR_BIT);
412       }
413
414       break;
415     default:
416       llvm_unreachable("Invalid Suffix");
417     }
418
419     MCELFStreamer::EmitBytes(StringRef(Buffer, Size));
420   }
421
422   /// This is one of the functions used to emit data into an ELF section, so the
423   /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
424   /// necessary.
425   virtual void EmitBytes(StringRef Data) {
426     EmitDataMappingSymbol();
427     MCELFStreamer::EmitBytes(Data);
428   }
429
430   /// This is one of the functions used to emit data into an ELF section, so the
431   /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
432   /// necessary.
433   virtual void EmitValueImpl(const MCExpr *Value, unsigned Size) {
434     EmitDataMappingSymbol();
435     MCELFStreamer::EmitValueImpl(Value, Size);
436   }
437
438   virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {
439     MCELFStreamer::EmitAssemblerFlag(Flag);
440
441     switch (Flag) {
442     case MCAF_SyntaxUnified:
443       return; // no-op here.
444     case MCAF_Code16:
445       IsThumb = true;
446       return; // Change to Thumb mode
447     case MCAF_Code32:
448       IsThumb = false;
449       return; // Change to ARM mode
450     case MCAF_Code64:
451       return;
452     case MCAF_SubsectionsViaSymbols:
453       return;
454     }
455   }
456
457 private:
458   enum ElfMappingSymbol {
459     EMS_None,
460     EMS_ARM,
461     EMS_Thumb,
462     EMS_Data
463   };
464
465   void EmitDataMappingSymbol() {
466     if (LastEMS == EMS_Data) return;
467     EmitMappingSymbol("$d");
468     LastEMS = EMS_Data;
469   }
470
471   void EmitThumbMappingSymbol() {
472     if (LastEMS == EMS_Thumb) return;
473     EmitMappingSymbol("$t");
474     LastEMS = EMS_Thumb;
475   }
476
477   void EmitARMMappingSymbol() {
478     if (LastEMS == EMS_ARM) return;
479     EmitMappingSymbol("$a");
480     LastEMS = EMS_ARM;
481   }
482
483   void EmitMappingSymbol(StringRef Name) {
484     MCSymbol *Start = getContext().CreateTempSymbol();
485     EmitLabel(Start);
486
487     MCSymbol *Symbol =
488       getContext().GetOrCreateSymbol(Name + "." +
489                                      Twine(MappingSymbolCounter++));
490
491     MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
492     MCELF::SetType(SD, ELF::STT_NOTYPE);
493     MCELF::SetBinding(SD, ELF::STB_LOCAL);
494     SD.setExternal(false);
495     AssignSection(Symbol, getCurrentSection().first);
496
497     const MCExpr *Value = MCSymbolRefExpr::Create(Start, getContext());
498     Symbol->setVariableValue(Value);
499   }
500
501   void EmitThumbFunc(MCSymbol *Func) {
502     // FIXME: Anything needed here to flag the function as thumb?
503
504     getAssembler().setIsThumbFunc(Func);
505
506     MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Func);
507     SD.setFlags(SD.getFlags() | ELF_Other_ThumbFunc);
508   }
509
510   // Helper functions for ARM exception handling directives
511   void Reset();
512
513   void EmitPersonalityFixup(StringRef Name);
514   void FlushPendingOffset();
515   void FlushUnwindOpcodes(bool NoHandlerData);
516
517   void SwitchToEHSection(const char *Prefix, unsigned Type, unsigned Flags,
518                          SectionKind Kind, const MCSymbol &Fn);
519   void SwitchToExTabSection(const MCSymbol &FnStart);
520   void SwitchToExIdxSection(const MCSymbol &FnStart);
521
522   bool IsThumb;
523   int64_t MappingSymbolCounter;
524
525   DenseMap<const MCSection *, ElfMappingSymbol> LastMappingSymbols;
526   ElfMappingSymbol LastEMS;
527
528   // ARM Exception Handling Frame Information
529   MCSymbol *ExTab;
530   MCSymbol *FnStart;
531   const MCSymbol *Personality;
532   unsigned PersonalityIndex;
533   unsigned FPReg; // Frame pointer register
534   int64_t FPOffset; // Offset: (final frame pointer) - (initial $sp)
535   int64_t SPOffset; // Offset: (final $sp) - (initial $sp)
536   int64_t PendingOffset; // Offset: (final $sp) - (emitted $sp)
537   bool UsedFP;
538   bool CantUnwind;
539   SmallVector<uint8_t, 64> Opcodes;
540   UnwindOpcodeAssembler UnwindOpAsm;
541 };
542 } // end anonymous namespace
543
544 ARMELFStreamer &ARMTargetELFStreamer::getStreamer() {
545   ARMELFStreamer *S = static_cast<ARMELFStreamer *>(Streamer);
546   return *S;
547 }
548
549 void ARMTargetELFStreamer::emitFnStart() { getStreamer().emitFnStart(); }
550 void ARMTargetELFStreamer::emitFnEnd() { getStreamer().emitFnEnd(); }
551 void ARMTargetELFStreamer::emitCantUnwind() { getStreamer().emitCantUnwind(); }
552 void ARMTargetELFStreamer::emitPersonality(const MCSymbol *Personality) {
553   getStreamer().emitPersonality(Personality);
554 }
555 void ARMTargetELFStreamer::emitHandlerData() {
556   getStreamer().emitHandlerData();
557 }
558 void ARMTargetELFStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
559                                      int64_t Offset) {
560   getStreamer().emitSetFP(FpReg, SpReg, Offset);
561 }
562 void ARMTargetELFStreamer::emitPad(int64_t Offset) {
563   getStreamer().emitPad(Offset);
564 }
565 void ARMTargetELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
566                                        bool isVector) {
567   getStreamer().emitRegSave(RegList, isVector);
568 }
569 void ARMTargetELFStreamer::switchVendor(StringRef Vendor) {
570   assert(!Vendor.empty() && "Vendor cannot be empty.");
571
572   if (CurrentVendor == Vendor)
573     return;
574
575   if (!CurrentVendor.empty())
576     finishAttributeSection();
577
578   assert(Contents.empty() &&
579          ".ARM.attributes should be flushed before changing vendor");
580   CurrentVendor = Vendor;
581
582 }
583 void ARMTargetELFStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
584   setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
585 }
586 void ARMTargetELFStreamer::emitTextAttribute(unsigned Attribute,
587                                              StringRef Value) {
588   setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
589 }
590 void ARMTargetELFStreamer::emitArch(unsigned Value) {
591   Arch = Value;
592 }
593 void ARMTargetELFStreamer::emitArchDefaultAttributes() {
594   using namespace ARMBuildAttrs;
595   setAttributeItem(CPU_name, GetArchDefaultCPUName(Arch), false);
596   setAttributeItem(CPU_arch, GetArchDefaultCPUArch(Arch), false);
597
598   switch (Arch) {
599   case ARM::ARMV2:
600   case ARM::ARMV2A:
601   case ARM::ARMV3:
602   case ARM::ARMV3M:
603   case ARM::ARMV4:
604   case ARM::ARMV5:
605     setAttributeItem(ARM_ISA_use, Allowed, false);
606     break;
607
608   case ARM::ARMV4T:
609   case ARM::ARMV5T:
610   case ARM::ARMV5TE:
611   case ARM::ARMV6:
612   case ARM::ARMV6J:
613     setAttributeItem(ARM_ISA_use, Allowed, false);
614     setAttributeItem(THUMB_ISA_use, Allowed, false);
615     break;
616
617   case ARM::ARMV6T2:
618     setAttributeItem(ARM_ISA_use, Allowed, false);
619     setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
620     break;
621
622   case ARM::ARMV6Z:
623   case ARM::ARMV6ZK:
624     setAttributeItem(ARM_ISA_use, Allowed, false);
625     setAttributeItem(THUMB_ISA_use, Allowed, false);
626     setAttributeItem(Virtualization_use, AllowTZ, false);
627     break;
628
629   case ARM::ARMV6M:
630     setAttributeItem(CPU_arch_profile, MicroControllerProfile, false);
631     setAttributeItem(THUMB_ISA_use, Allowed, false);
632     break;
633
634   case ARM::ARMV7:
635     setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
636     break;
637
638   case ARM::ARMV7A:
639     setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
640     setAttributeItem(ARM_ISA_use, Allowed, false);
641     setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
642     break;
643
644   case ARM::ARMV7R:
645     setAttributeItem(CPU_arch_profile, RealTimeProfile, false);
646     setAttributeItem(ARM_ISA_use, Allowed, false);
647     setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
648     break;
649
650   case ARM::ARMV7M:
651     setAttributeItem(CPU_arch_profile, MicroControllerProfile, false);
652     setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
653     break;
654
655   case ARM::ARMV8A:
656     setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
657     setAttributeItem(ARM_ISA_use, Allowed, false);
658     setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
659     setAttributeItem(MPextension_use, Allowed, false);
660     setAttributeItem(Virtualization_use, AllowTZVirtualization, false);
661     break;
662
663   case ARM::IWMMXT:
664     setAttributeItem(ARM_ISA_use, Allowed, false);
665     setAttributeItem(THUMB_ISA_use, Allowed, false);
666     setAttributeItem(WMMX_arch, AllowWMMXv1, false);
667     break;
668
669   case ARM::IWMMXT2:
670     setAttributeItem(ARM_ISA_use, Allowed, false);
671     setAttributeItem(THUMB_ISA_use, Allowed, false);
672     setAttributeItem(WMMX_arch, AllowWMMXv2, false);
673     break;
674
675   default:
676     report_fatal_error("Unknown Arch: " + Twine(Arch));
677     break;
678   }
679 }
680 void ARMTargetELFStreamer::emitFPU(unsigned Value) {
681   FPU = Value;
682 }
683 void ARMTargetELFStreamer::emitFPUDefaultAttributes() {
684   switch (FPU) {
685   case ARM::VFP:
686   case ARM::VFPV2:
687     setAttributeItem(ARMBuildAttrs::FP_arch,
688                      ARMBuildAttrs::AllowFPv2,
689                      /* OverwriteExisting= */ false);
690     break;
691
692   case ARM::VFPV3:
693     setAttributeItem(ARMBuildAttrs::FP_arch,
694                      ARMBuildAttrs::AllowFPv3A,
695                      /* OverwriteExisting= */ false);
696     break;
697
698   case ARM::VFPV3_D16:
699     setAttributeItem(ARMBuildAttrs::FP_arch,
700                      ARMBuildAttrs::AllowFPv3B,
701                      /* OverwriteExisting= */ false);
702     break;
703
704   case ARM::VFPV4:
705     setAttributeItem(ARMBuildAttrs::FP_arch,
706                      ARMBuildAttrs::AllowFPv4A,
707                      /* OverwriteExisting= */ false);
708     break;
709
710   case ARM::VFPV4_D16:
711     setAttributeItem(ARMBuildAttrs::FP_arch,
712                      ARMBuildAttrs::AllowFPv4B,
713                      /* OverwriteExisting= */ false);
714     break;
715
716   case ARM::FP_ARMV8:
717     setAttributeItem(ARMBuildAttrs::FP_arch,
718                      ARMBuildAttrs::AllowFPARMv8A,
719                      /* OverwriteExisting= */ false);
720     break;
721
722   case ARM::NEON:
723     setAttributeItem(ARMBuildAttrs::FP_arch,
724                      ARMBuildAttrs::AllowFPv3A,
725                      /* OverwriteExisting= */ false);
726     setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
727                      ARMBuildAttrs::AllowNeon,
728                      /* OverwriteExisting= */ false);
729     break;
730
731   case ARM::NEON_VFPV4:
732     setAttributeItem(ARMBuildAttrs::FP_arch,
733                      ARMBuildAttrs::AllowFPv4A,
734                      /* OverwriteExisting= */ false);
735     setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
736                      ARMBuildAttrs::AllowNeon2,
737                      /* OverwriteExisting= */ false);
738     break;
739
740   case ARM::NEON_FP_ARMV8:
741   case ARM::CRYPTO_NEON_FP_ARMV8:
742     setAttributeItem(ARMBuildAttrs::FP_arch,
743                      ARMBuildAttrs::AllowFPARMv8A,
744                      /* OverwriteExisting= */ false);
745     setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
746                      ARMBuildAttrs::AllowNeonARMv8,
747                      /* OverwriteExisting= */ false);
748     break;
749
750   case ARM::SOFTVFP:
751     break;
752
753   default:
754     report_fatal_error("Unknown FPU: " + Twine(FPU));
755     break;
756   }
757 }
758 size_t ARMTargetELFStreamer::calculateContentSize() const {
759   size_t Result = 0;
760   for (size_t i = 0; i < Contents.size(); ++i) {
761     AttributeItem item = Contents[i];
762     switch (item.Type) {
763     case AttributeItem::HiddenAttribute:
764       break;
765     case AttributeItem::NumericAttribute:
766       Result += getULEBSize(item.Tag);
767       Result += getULEBSize(item.IntValue);
768       break;
769     case AttributeItem::TextAttribute:
770       Result += getULEBSize(item.Tag);
771       Result += item.StringValue.size() + 1; // string + '\0'
772       break;
773     }
774   }
775   return Result;
776 }
777 void ARMTargetELFStreamer::finishAttributeSection() {
778   // <format-version>
779   // [ <section-length> "vendor-name"
780   // [ <file-tag> <size> <attribute>*
781   //   | <section-tag> <size> <section-number>* 0 <attribute>*
782   //   | <symbol-tag> <size> <symbol-number>* 0 <attribute>*
783   //   ]+
784   // ]*
785
786   if (FPU != ARM::INVALID_FPU)
787     emitFPUDefaultAttributes();
788
789   if (Arch != ARM::INVALID_ARCH)
790     emitArchDefaultAttributes();
791
792   if (Contents.empty())
793     return;
794
795   std::sort(Contents.begin(), Contents.end(), AttributeItem::LessTag);
796
797   ARMELFStreamer &Streamer = getStreamer();
798
799   // Switch to .ARM.attributes section
800   if (AttributeSection) {
801     Streamer.SwitchSection(AttributeSection);
802   } else {
803     AttributeSection =
804       Streamer.getContext().getELFSection(".ARM.attributes",
805                                           ELF::SHT_ARM_ATTRIBUTES,
806                                           0,
807                                           SectionKind::getMetadata());
808     Streamer.SwitchSection(AttributeSection);
809
810     // Format version
811     Streamer.EmitIntValue(0x41, 1);
812   }
813
814   // Vendor size + Vendor name + '\0'
815   const size_t VendorHeaderSize = 4 + CurrentVendor.size() + 1;
816
817   // Tag + Tag Size
818   const size_t TagHeaderSize = 1 + 4;
819
820   const size_t ContentsSize = calculateContentSize();
821
822   Streamer.EmitIntValue(VendorHeaderSize + TagHeaderSize + ContentsSize, 4);
823   Streamer.EmitBytes(CurrentVendor);
824   Streamer.EmitIntValue(0, 1); // '\0'
825
826   Streamer.EmitIntValue(ARMBuildAttrs::File, 1);
827   Streamer.EmitIntValue(TagHeaderSize + ContentsSize, 4);
828
829   // Size should have been accounted for already, now
830   // emit each field as its type (ULEB or String)
831   for (size_t i = 0; i < Contents.size(); ++i) {
832     AttributeItem item = Contents[i];
833     Streamer.EmitULEB128IntValue(item.Tag);
834     switch (item.Type) {
835     default: llvm_unreachable("Invalid attribute type");
836     case AttributeItem::NumericAttribute:
837       Streamer.EmitULEB128IntValue(item.IntValue);
838       break;
839     case AttributeItem::TextAttribute:
840       Streamer.EmitBytes(item.StringValue.upper());
841       Streamer.EmitIntValue(0, 1); // '\0'
842       break;
843     }
844   }
845
846   Contents.clear();
847   FPU = ARM::INVALID_FPU;
848 }
849 void ARMTargetELFStreamer::emitInst(uint32_t Inst, char Suffix) {
850   getStreamer().emitInst(Inst, Suffix);
851 }
852
853 void ARMELFStreamer::FinishImpl() {
854   MCTargetStreamer &TS = getTargetStreamer();
855   ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
856   ATS.finishAttributeSection();
857
858   MCELFStreamer::FinishImpl();
859 }
860
861 inline void ARMELFStreamer::SwitchToEHSection(const char *Prefix,
862                                               unsigned Type,
863                                               unsigned Flags,
864                                               SectionKind Kind,
865                                               const MCSymbol &Fn) {
866   const MCSectionELF &FnSection =
867     static_cast<const MCSectionELF &>(Fn.getSection());
868
869   // Create the name for new section
870   StringRef FnSecName(FnSection.getSectionName());
871   SmallString<128> EHSecName(Prefix);
872   if (FnSecName != ".text") {
873     EHSecName += FnSecName;
874   }
875
876   // Get .ARM.extab or .ARM.exidx section
877   const MCSectionELF *EHSection = NULL;
878   if (const MCSymbol *Group = FnSection.getGroup()) {
879     EHSection = getContext().getELFSection(
880       EHSecName, Type, Flags | ELF::SHF_GROUP, Kind,
881       FnSection.getEntrySize(), Group->getName());
882   } else {
883     EHSection = getContext().getELFSection(EHSecName, Type, Flags, Kind);
884   }
885   assert(EHSection && "Failed to get the required EH section");
886
887   // Switch to .ARM.extab or .ARM.exidx section
888   SwitchSection(EHSection);
889   EmitCodeAlignment(4, 0);
890 }
891
892 inline void ARMELFStreamer::SwitchToExTabSection(const MCSymbol &FnStart) {
893   SwitchToEHSection(".ARM.extab",
894                     ELF::SHT_PROGBITS,
895                     ELF::SHF_ALLOC,
896                     SectionKind::getDataRel(),
897                     FnStart);
898 }
899
900 inline void ARMELFStreamer::SwitchToExIdxSection(const MCSymbol &FnStart) {
901   SwitchToEHSection(".ARM.exidx",
902                     ELF::SHT_ARM_EXIDX,
903                     ELF::SHF_ALLOC | ELF::SHF_LINK_ORDER,
904                     SectionKind::getDataRel(),
905                     FnStart);
906 }
907
908 void ARMELFStreamer::Reset() {
909   ExTab = NULL;
910   FnStart = NULL;
911   Personality = NULL;
912   PersonalityIndex = NUM_PERSONALITY_INDEX;
913   FPReg = ARM::SP;
914   FPOffset = 0;
915   SPOffset = 0;
916   PendingOffset = 0;
917   UsedFP = false;
918   CantUnwind = false;
919
920   Opcodes.clear();
921   UnwindOpAsm.Reset();
922 }
923
924 void ARMELFStreamer::emitFnStart() {
925   assert(FnStart == 0);
926   FnStart = getContext().CreateTempSymbol();
927   EmitLabel(FnStart);
928 }
929
930 void ARMELFStreamer::emitFnEnd() {
931   assert(FnStart && ".fnstart must preceeds .fnend");
932
933   // Emit unwind opcodes if there is no .handlerdata directive
934   if (!ExTab && !CantUnwind)
935     FlushUnwindOpcodes(true);
936
937   // Emit the exception index table entry
938   SwitchToExIdxSection(*FnStart);
939
940   if (PersonalityIndex < NUM_PERSONALITY_INDEX)
941     EmitPersonalityFixup(GetAEABIUnwindPersonalityName(PersonalityIndex));
942
943   const MCSymbolRefExpr *FnStartRef =
944     MCSymbolRefExpr::Create(FnStart,
945                             MCSymbolRefExpr::VK_ARM_PREL31,
946                             getContext());
947
948   EmitValue(FnStartRef, 4);
949
950   if (CantUnwind) {
951     EmitIntValue(EXIDX_CANTUNWIND, 4);
952   } else if (ExTab) {
953     // Emit a reference to the unwind opcodes in the ".ARM.extab" section.
954     const MCSymbolRefExpr *ExTabEntryRef =
955       MCSymbolRefExpr::Create(ExTab,
956                               MCSymbolRefExpr::VK_ARM_PREL31,
957                               getContext());
958     EmitValue(ExTabEntryRef, 4);
959   } else {
960     // For the __aeabi_unwind_cpp_pr0, we have to emit the unwind opcodes in
961     // the second word of exception index table entry.  The size of the unwind
962     // opcodes should always be 4 bytes.
963     assert(PersonalityIndex == AEABI_UNWIND_CPP_PR0 &&
964            "Compact model must use __aeabi_cpp_unwind_pr0 as personality");
965     assert(Opcodes.size() == 4u &&
966            "Unwind opcode size for __aeabi_cpp_unwind_pr0 must be equal to 4");
967     EmitBytes(StringRef(reinterpret_cast<const char*>(Opcodes.data()),
968                         Opcodes.size()));
969   }
970
971   // Switch to the section containing FnStart
972   SwitchSection(&FnStart->getSection());
973
974   // Clean exception handling frame information
975   Reset();
976 }
977
978 void ARMELFStreamer::emitCantUnwind() { CantUnwind = true; }
979
980 // Add the R_ARM_NONE fixup at the same position
981 void ARMELFStreamer::EmitPersonalityFixup(StringRef Name) {
982   const MCSymbol *PersonalitySym = getContext().GetOrCreateSymbol(Name);
983
984   const MCSymbolRefExpr *PersonalityRef = MCSymbolRefExpr::Create(
985       PersonalitySym, MCSymbolRefExpr::VK_ARM_NONE, getContext());
986
987   AddValueSymbols(PersonalityRef);
988   MCDataFragment *DF = getOrCreateDataFragment();
989   DF->getFixups().push_back(MCFixup::Create(DF->getContents().size(),
990                                             PersonalityRef,
991                                             MCFixup::getKindForSize(4, false)));
992 }
993
994 void ARMELFStreamer::FlushPendingOffset() {
995   if (PendingOffset != 0) {
996     UnwindOpAsm.EmitSPOffset(-PendingOffset);
997     PendingOffset = 0;
998   }
999 }
1000
1001 void ARMELFStreamer::FlushUnwindOpcodes(bool NoHandlerData) {
1002   // Emit the unwind opcode to restore $sp.
1003   if (UsedFP) {
1004     const MCRegisterInfo *MRI = getContext().getRegisterInfo();
1005     int64_t LastRegSaveSPOffset = SPOffset - PendingOffset;
1006     UnwindOpAsm.EmitSPOffset(LastRegSaveSPOffset - FPOffset);
1007     UnwindOpAsm.EmitSetSP(MRI->getEncodingValue(FPReg));
1008   } else {
1009     FlushPendingOffset();
1010   }
1011
1012   // Finalize the unwind opcode sequence
1013   UnwindOpAsm.Finalize(PersonalityIndex, Opcodes);
1014
1015   // For compact model 0, we have to emit the unwind opcodes in the .ARM.exidx
1016   // section.  Thus, we don't have to create an entry in the .ARM.extab
1017   // section.
1018   if (NoHandlerData && PersonalityIndex == AEABI_UNWIND_CPP_PR0)
1019     return;
1020
1021   // Switch to .ARM.extab section.
1022   SwitchToExTabSection(*FnStart);
1023
1024   // Create .ARM.extab label for offset in .ARM.exidx
1025   assert(!ExTab);
1026   ExTab = getContext().CreateTempSymbol();
1027   EmitLabel(ExTab);
1028
1029   // Emit personality
1030   if (Personality) {
1031     const MCSymbolRefExpr *PersonalityRef =
1032       MCSymbolRefExpr::Create(Personality,
1033                               MCSymbolRefExpr::VK_ARM_PREL31,
1034                               getContext());
1035
1036     EmitValue(PersonalityRef, 4);
1037   }
1038
1039   // Emit unwind opcodes
1040   EmitBytes(StringRef(reinterpret_cast<const char *>(Opcodes.data()),
1041                       Opcodes.size()));
1042
1043   // According to ARM EHABI section 9.2, if the __aeabi_unwind_cpp_pr1() or
1044   // __aeabi_unwind_cpp_pr2() is used, then the handler data must be emitted
1045   // after the unwind opcodes.  The handler data consists of several 32-bit
1046   // words, and should be terminated by zero.
1047   //
1048   // In case that the .handlerdata directive is not specified by the
1049   // programmer, we should emit zero to terminate the handler data.
1050   if (NoHandlerData && !Personality)
1051     EmitIntValue(0, 4);
1052 }
1053
1054 void ARMELFStreamer::emitHandlerData() { FlushUnwindOpcodes(false); }
1055
1056 void ARMELFStreamer::emitPersonality(const MCSymbol *Per) {
1057   Personality = Per;
1058   UnwindOpAsm.setPersonality(Per);
1059 }
1060
1061 void ARMELFStreamer::emitSetFP(unsigned NewFPReg, unsigned NewSPReg,
1062                                int64_t Offset) {
1063   assert((NewSPReg == ARM::SP || NewSPReg == FPReg) &&
1064          "the operand of .setfp directive should be either $sp or $fp");
1065
1066   UsedFP = true;
1067   FPReg = NewFPReg;
1068
1069   if (NewSPReg == ARM::SP)
1070     FPOffset = SPOffset + Offset;
1071   else
1072     FPOffset += Offset;
1073 }
1074
1075 void ARMELFStreamer::emitPad(int64_t Offset) {
1076   // Track the change of the $sp offset
1077   SPOffset -= Offset;
1078
1079   // To squash multiple .pad directives, we should delay the unwind opcode
1080   // until the .save, .vsave, .handlerdata, or .fnend directives.
1081   PendingOffset -= Offset;
1082 }
1083
1084 void ARMELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
1085                                  bool IsVector) {
1086   // Collect the registers in the register list
1087   unsigned Count = 0;
1088   uint32_t Mask = 0;
1089   const MCRegisterInfo *MRI = getContext().getRegisterInfo();
1090   for (size_t i = 0; i < RegList.size(); ++i) {
1091     unsigned Reg = MRI->getEncodingValue(RegList[i]);
1092     assert(Reg < (IsVector ? 32U : 16U) && "Register out of range");
1093     unsigned Bit = (1u << Reg);
1094     if ((Mask & Bit) == 0) {
1095       Mask |= Bit;
1096       ++Count;
1097     }
1098   }
1099
1100   // Track the change the $sp offset: For the .save directive, the
1101   // corresponding push instruction will decrease the $sp by (4 * Count).
1102   // For the .vsave directive, the corresponding vpush instruction will
1103   // decrease $sp by (8 * Count).
1104   SPOffset -= Count * (IsVector ? 8 : 4);
1105
1106   // Emit the opcode
1107   FlushPendingOffset();
1108   if (IsVector)
1109     UnwindOpAsm.EmitVFPRegSave(Mask);
1110   else
1111     UnwindOpAsm.EmitRegSave(Mask);
1112 }
1113
1114 namespace llvm {
1115
1116 MCStreamer *createMCAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS,
1117                                 bool isVerboseAsm, bool useLoc, bool useCFI,
1118                                 bool useDwarfDirectory,
1119                                 MCInstPrinter *InstPrint, MCCodeEmitter *CE,
1120                                 MCAsmBackend *TAB, bool ShowInst) {
1121   ARMTargetAsmStreamer *S = new ARMTargetAsmStreamer(OS, *InstPrint);
1122
1123   return llvm::createAsmStreamer(Ctx, S, OS, isVerboseAsm, useLoc, useCFI,
1124                                  useDwarfDirectory, InstPrint, CE, TAB,
1125                                  ShowInst);
1126 }
1127
1128   MCELFStreamer* createARMELFStreamer(MCContext &Context, MCAsmBackend &TAB,
1129                                       raw_ostream &OS, MCCodeEmitter *Emitter,
1130                                       bool RelaxAll, bool NoExecStack,
1131                                       bool IsThumb) {
1132     ARMTargetELFStreamer *TS = new ARMTargetELFStreamer();
1133     ARMELFStreamer *S =
1134         new ARMELFStreamer(Context, TS, TAB, OS, Emitter, IsThumb);
1135     // FIXME: This should eventually end up somewhere else where more
1136     // intelligent flag decisions can be made. For now we are just maintaining
1137     // the status quo for ARM and setting EF_ARM_EABI_VER5 as the default.
1138     S->getAssembler().setELFHeaderEFlags(ELF::EF_ARM_EABI_VER5);
1139
1140     if (RelaxAll)
1141       S->getAssembler().setRelaxAll(true);
1142     if (NoExecStack)
1143       S->getAssembler().setNoExecStack(true);
1144     return S;
1145   }
1146
1147 }
1148
1149