blob: d38fde1564f3fa1cc9abf3fb49b41971883e223b [file] [log] [blame]
Tim Northover5cc3dc82012-12-07 16:50:23 +00001//===- 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
Logan Chiend8bb4b72013-04-16 12:02:21 +000016#include "ARMRegisterInfo.h"
Logan Chien2bcc42c2013-01-30 15:39:04 +000017#include "ARMUnwindOp.h"
Logan Chiend8bb4b72013-04-16 12:02:21 +000018#include "ARMUnwindOpAsm.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000019#include "llvm/ADT/SmallPtrSet.h"
Benjamin Kramerf242d8c2012-12-08 10:45:24 +000020#include "llvm/ADT/Twine.h"
21#include "llvm/MC/MCAsmBackend.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000022#include "llvm/MC/MCAssembler.h"
23#include "llvm/MC/MCCodeEmitter.h"
24#include "llvm/MC/MCContext.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000025#include "llvm/MC/MCELF.h"
26#include "llvm/MC/MCELFStreamer.h"
27#include "llvm/MC/MCELFSymbolFlags.h"
28#include "llvm/MC/MCExpr.h"
29#include "llvm/MC/MCInst.h"
30#include "llvm/MC/MCObjectStreamer.h"
Logan Chiend8bb4b72013-04-16 12:02:21 +000031#include "llvm/MC/MCRegisterInfo.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000032#include "llvm/MC/MCSection.h"
Benjamin Kramerf242d8c2012-12-08 10:45:24 +000033#include "llvm/MC/MCSectionELF.h"
34#include "llvm/MC/MCStreamer.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000035#include "llvm/MC/MCSymbol.h"
36#include "llvm/MC/MCValue.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000037#include "llvm/Support/Debug.h"
38#include "llvm/Support/ELF.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000039#include "llvm/Support/raw_ostream.h"
40
41using namespace llvm;
42
Logan Chiend8bb4b72013-04-16 12:02:21 +000043static std::string GetAEABIUnwindPersonalityName(unsigned Index) {
44 assert(Index < NUM_PERSONALITY_INDEX && "Invalid personality index");
45 return (Twine("__aeabi_unwind_cpp_pr") + Twine(Index)).str();
46}
47
Tim Northover5cc3dc82012-12-07 16:50:23 +000048namespace {
49
50/// Extend the generic ELFStreamer class so that it can emit mapping symbols at
51/// the appropriate points in the object files. These symbols are defined in the
52/// ARM ELF ABI: infocenter.arm.com/help/topic/com.arm.../IHI0044D_aaelf.pdf.
53///
54/// In brief: $a, $t or $d should be emitted at the start of each contiguous
55/// region of ARM code, Thumb code or data in a section. In practice, this
56/// emission does not rely on explicit assembler directives but on inherent
57/// properties of the directives doing the emission (e.g. ".byte" is data, "add
58/// r0, r0, r0" an instruction).
59///
60/// As a result this system is orthogonal to the DataRegion infrastructure used
61/// by MachO. Beware!
62class ARMELFStreamer : public MCELFStreamer {
63public:
Chandler Carruthde093ef2013-01-31 23:29:57 +000064 ARMELFStreamer(MCContext &Context, MCAsmBackend &TAB, raw_ostream &OS,
65 MCCodeEmitter *Emitter, bool IsThumb)
Rafael Espindolaac4ad252013-10-05 16:42:21 +000066 : MCELFStreamer(Context, TAB, OS, Emitter), IsThumb(IsThumb),
67 MappingSymbolCounter(0), LastEMS(EMS_None) {
Logan Chiend8bb4b72013-04-16 12:02:21 +000068 Reset();
69 }
Tim Northover5cc3dc82012-12-07 16:50:23 +000070
71 ~ARMELFStreamer() {}
72
Logan Chien2bcc42c2013-01-30 15:39:04 +000073 // ARM exception handling directives
74 virtual void EmitFnStart();
75 virtual void EmitFnEnd();
76 virtual void EmitCantUnwind();
77 virtual void EmitPersonality(const MCSymbol *Per);
78 virtual void EmitHandlerData();
79 virtual void EmitSetFP(unsigned NewFpReg,
80 unsigned NewSpReg,
81 int64_t Offset = 0);
82 virtual void EmitPad(int64_t Offset);
83 virtual void EmitRegSave(const SmallVectorImpl<unsigned> &RegList,
84 bool isVector);
85
Peter Collingbourne2f495b92013-04-17 21:18:16 +000086 virtual void ChangeSection(const MCSection *Section,
87 const MCExpr *Subsection) {
Tim Northover5cc3dc82012-12-07 16:50:23 +000088 // We have to keep track of the mapping symbol state of any sections we
89 // use. Each one should start off as EMS_None, which is provided as the
90 // default constructor by DenseMap::lookup.
Peter Collingbourne2f495b92013-04-17 21:18:16 +000091 LastMappingSymbols[getPreviousSection().first] = LastEMS;
Tim Northover5cc3dc82012-12-07 16:50:23 +000092 LastEMS = LastMappingSymbols.lookup(Section);
93
Peter Collingbourne2f495b92013-04-17 21:18:16 +000094 MCELFStreamer::ChangeSection(Section, Subsection);
Tim Northover5cc3dc82012-12-07 16:50:23 +000095 }
96
97 /// This function is the one used to emit instruction data into the ELF
98 /// streamer. We override it to add the appropriate mapping symbol if
99 /// necessary.
100 virtual void EmitInstruction(const MCInst& Inst) {
101 if (IsThumb)
102 EmitThumbMappingSymbol();
103 else
104 EmitARMMappingSymbol();
105
106 MCELFStreamer::EmitInstruction(Inst);
107 }
108
109 /// This is one of the functions used to emit data into an ELF section, so the
110 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
111 /// necessary.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000112 virtual void EmitBytes(StringRef Data) {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000113 EmitDataMappingSymbol();
Rafael Espindola64e1af82013-07-02 15:49:13 +0000114 MCELFStreamer::EmitBytes(Data);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000115 }
116
117 /// This is one of the functions used to emit data into an ELF section, so the
118 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
119 /// necessary.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000120 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size) {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000121 EmitDataMappingSymbol();
Rafael Espindola64e1af82013-07-02 15:49:13 +0000122 MCELFStreamer::EmitValueImpl(Value, Size);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000123 }
124
125 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {
126 MCELFStreamer::EmitAssemblerFlag(Flag);
127
128 switch (Flag) {
129 case MCAF_SyntaxUnified:
130 return; // no-op here.
131 case MCAF_Code16:
132 IsThumb = true;
133 return; // Change to Thumb mode
134 case MCAF_Code32:
135 IsThumb = false;
136 return; // Change to ARM mode
137 case MCAF_Code64:
138 return;
139 case MCAF_SubsectionsViaSymbols:
140 return;
141 }
142 }
143
144private:
145 enum ElfMappingSymbol {
146 EMS_None,
147 EMS_ARM,
148 EMS_Thumb,
149 EMS_Data
150 };
151
152 void EmitDataMappingSymbol() {
153 if (LastEMS == EMS_Data) return;
154 EmitMappingSymbol("$d");
155 LastEMS = EMS_Data;
156 }
157
158 void EmitThumbMappingSymbol() {
159 if (LastEMS == EMS_Thumb) return;
160 EmitMappingSymbol("$t");
161 LastEMS = EMS_Thumb;
162 }
163
164 void EmitARMMappingSymbol() {
165 if (LastEMS == EMS_ARM) return;
166 EmitMappingSymbol("$a");
167 LastEMS = EMS_ARM;
168 }
169
170 void EmitMappingSymbol(StringRef Name) {
171 MCSymbol *Start = getContext().CreateTempSymbol();
172 EmitLabel(Start);
173
Chandler Carruth1d94e932012-12-08 03:10:14 +0000174 MCSymbol *Symbol =
Benjamin Kramerf242d8c2012-12-08 10:45:24 +0000175 getContext().GetOrCreateSymbol(Name + "." +
176 Twine(MappingSymbolCounter++));
Tim Northover5cc3dc82012-12-07 16:50:23 +0000177
178 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
179 MCELF::SetType(SD, ELF::STT_NOTYPE);
180 MCELF::SetBinding(SD, ELF::STB_LOCAL);
181 SD.setExternal(false);
Richard Mitton21101b32013-09-19 23:21:01 +0000182 AssignSection(Symbol, getCurrentSection().first);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000183
184 const MCExpr *Value = MCSymbolRefExpr::Create(Start, getContext());
185 Symbol->setVariableValue(Value);
186 }
187
188 void EmitThumbFunc(MCSymbol *Func) {
189 // FIXME: Anything needed here to flag the function as thumb?
190
191 getAssembler().setIsThumbFunc(Func);
192
193 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Func);
194 SD.setFlags(SD.getFlags() | ELF_Other_ThumbFunc);
195 }
196
Logan Chien2bcc42c2013-01-30 15:39:04 +0000197 // Helper functions for ARM exception handling directives
198 void Reset();
199
200 void EmitPersonalityFixup(StringRef Name);
Logan Chien325823a2013-06-09 12:22:30 +0000201 void FlushPendingOffset();
Logan Chienc931fce2013-07-02 12:43:27 +0000202 void FlushUnwindOpcodes(bool NoHandlerData);
Logan Chien2bcc42c2013-01-30 15:39:04 +0000203
204 void SwitchToEHSection(const char *Prefix, unsigned Type, unsigned Flags,
205 SectionKind Kind, const MCSymbol &Fn);
206 void SwitchToExTabSection(const MCSymbol &FnStart);
207 void SwitchToExIdxSection(const MCSymbol &FnStart);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000208
209 bool IsThumb;
210 int64_t MappingSymbolCounter;
211
212 DenseMap<const MCSection *, ElfMappingSymbol> LastMappingSymbols;
213 ElfMappingSymbol LastEMS;
214
Logan Chien2bcc42c2013-01-30 15:39:04 +0000215 // ARM Exception Handling Frame Information
216 MCSymbol *ExTab;
217 MCSymbol *FnStart;
218 const MCSymbol *Personality;
Logan Chien325823a2013-06-09 12:22:30 +0000219 unsigned PersonalityIndex;
220 unsigned FPReg; // Frame pointer register
221 int64_t FPOffset; // Offset: (final frame pointer) - (initial $sp)
222 int64_t SPOffset; // Offset: (final $sp) - (initial $sp)
223 int64_t PendingOffset; // Offset: (final $sp) - (emitted $sp)
Logan Chiend8bb4b72013-04-16 12:02:21 +0000224 bool UsedFP;
Logan Chien2bcc42c2013-01-30 15:39:04 +0000225 bool CantUnwind;
Logan Chien325823a2013-06-09 12:22:30 +0000226 SmallVector<uint8_t, 64> Opcodes;
Logan Chiend8bb4b72013-04-16 12:02:21 +0000227 UnwindOpcodeAssembler UnwindOpAsm;
Tim Northover5cc3dc82012-12-07 16:50:23 +0000228};
Logan Chiend8bb4b72013-04-16 12:02:21 +0000229} // end anonymous namespace
Tim Northover5cc3dc82012-12-07 16:50:23 +0000230
Logan Chien2bcc42c2013-01-30 15:39:04 +0000231inline void ARMELFStreamer::SwitchToEHSection(const char *Prefix,
232 unsigned Type,
233 unsigned Flags,
234 SectionKind Kind,
235 const MCSymbol &Fn) {
236 const MCSectionELF &FnSection =
237 static_cast<const MCSectionELF &>(Fn.getSection());
238
239 // Create the name for new section
240 StringRef FnSecName(FnSection.getSectionName());
241 SmallString<128> EHSecName(Prefix);
242 if (FnSecName != ".text") {
243 EHSecName += FnSecName;
244 }
245
246 // Get .ARM.extab or .ARM.exidx section
247 const MCSectionELF *EHSection = NULL;
248 if (const MCSymbol *Group = FnSection.getGroup()) {
249 EHSection = getContext().getELFSection(
250 EHSecName, Type, Flags | ELF::SHF_GROUP, Kind,
251 FnSection.getEntrySize(), Group->getName());
252 } else {
253 EHSection = getContext().getELFSection(EHSecName, Type, Flags, Kind);
254 }
Logan Chiend8bb4b72013-04-16 12:02:21 +0000255 assert(EHSection && "Failed to get the required EH section");
Logan Chien2bcc42c2013-01-30 15:39:04 +0000256
257 // Switch to .ARM.extab or .ARM.exidx section
258 SwitchSection(EHSection);
259 EmitCodeAlignment(4, 0);
260}
261
262inline void ARMELFStreamer::SwitchToExTabSection(const MCSymbol &FnStart) {
263 SwitchToEHSection(".ARM.extab",
264 ELF::SHT_PROGBITS,
265 ELF::SHF_ALLOC,
266 SectionKind::getDataRel(),
267 FnStart);
268}
269
270inline void ARMELFStreamer::SwitchToExIdxSection(const MCSymbol &FnStart) {
271 SwitchToEHSection(".ARM.exidx",
272 ELF::SHT_ARM_EXIDX,
273 ELF::SHF_ALLOC | ELF::SHF_LINK_ORDER,
274 SectionKind::getDataRel(),
275 FnStart);
276}
277
278void ARMELFStreamer::Reset() {
279 ExTab = NULL;
280 FnStart = NULL;
281 Personality = NULL;
Logan Chien325823a2013-06-09 12:22:30 +0000282 PersonalityIndex = NUM_PERSONALITY_INDEX;
283 FPReg = ARM::SP;
Logan Chiend8bb4b72013-04-16 12:02:21 +0000284 FPOffset = 0;
285 SPOffset = 0;
Logan Chien325823a2013-06-09 12:22:30 +0000286 PendingOffset = 0;
Logan Chiend8bb4b72013-04-16 12:02:21 +0000287 UsedFP = false;
Logan Chien2bcc42c2013-01-30 15:39:04 +0000288 CantUnwind = false;
Logan Chiend8bb4b72013-04-16 12:02:21 +0000289
Logan Chien325823a2013-06-09 12:22:30 +0000290 Opcodes.clear();
Logan Chiend8bb4b72013-04-16 12:02:21 +0000291 UnwindOpAsm.Reset();
Logan Chien2bcc42c2013-01-30 15:39:04 +0000292}
293
294// Add the R_ARM_NONE fixup at the same position
295void ARMELFStreamer::EmitPersonalityFixup(StringRef Name) {
296 const MCSymbol *PersonalitySym = getContext().GetOrCreateSymbol(Name);
297
298 const MCSymbolRefExpr *PersonalityRef =
299 MCSymbolRefExpr::Create(PersonalitySym,
300 MCSymbolRefExpr::VK_ARM_NONE,
301 getContext());
302
303 AddValueSymbols(PersonalityRef);
304 MCDataFragment *DF = getOrCreateDataFragment();
305 DF->getFixups().push_back(
306 MCFixup::Create(DF->getContents().size(), PersonalityRef,
307 MCFixup::getKindForSize(4, false)));
308}
309
310void ARMELFStreamer::EmitFnStart() {
311 assert(FnStart == 0);
312 FnStart = getContext().CreateTempSymbol();
313 EmitLabel(FnStart);
314}
315
316void ARMELFStreamer::EmitFnEnd() {
317 assert(FnStart && ".fnstart must preceeds .fnend");
318
319 // Emit unwind opcodes if there is no .handlerdata directive
Logan Chien4ea23b52013-05-10 16:17:24 +0000320 if (!ExTab && !CantUnwind)
321 FlushUnwindOpcodes(true);
Logan Chien2bcc42c2013-01-30 15:39:04 +0000322
323 // Emit the exception index table entry
324 SwitchToExIdxSection(*FnStart);
325
Logan Chiend8bb4b72013-04-16 12:02:21 +0000326 if (PersonalityIndex < NUM_PERSONALITY_INDEX)
327 EmitPersonalityFixup(GetAEABIUnwindPersonalityName(PersonalityIndex));
Logan Chien2bcc42c2013-01-30 15:39:04 +0000328
329 const MCSymbolRefExpr *FnStartRef =
330 MCSymbolRefExpr::Create(FnStart,
331 MCSymbolRefExpr::VK_ARM_PREL31,
332 getContext());
333
Rafael Espindola64e1af82013-07-02 15:49:13 +0000334 EmitValue(FnStartRef, 4);
Logan Chien2bcc42c2013-01-30 15:39:04 +0000335
336 if (CantUnwind) {
Rafael Espindola64e1af82013-07-02 15:49:13 +0000337 EmitIntValue(EXIDX_CANTUNWIND, 4);
Logan Chiend8bb4b72013-04-16 12:02:21 +0000338 } else if (ExTab) {
339 // Emit a reference to the unwind opcodes in the ".ARM.extab" section.
Logan Chien2bcc42c2013-01-30 15:39:04 +0000340 const MCSymbolRefExpr *ExTabEntryRef =
341 MCSymbolRefExpr::Create(ExTab,
342 MCSymbolRefExpr::VK_ARM_PREL31,
343 getContext());
Rafael Espindola64e1af82013-07-02 15:49:13 +0000344 EmitValue(ExTabEntryRef, 4);
Logan Chiend8bb4b72013-04-16 12:02:21 +0000345 } else {
346 // For the __aeabi_unwind_cpp_pr0, we have to emit the unwind opcodes in
347 // the second word of exception index table entry. The size of the unwind
348 // opcodes should always be 4 bytes.
349 assert(PersonalityIndex == AEABI_UNWIND_CPP_PR0 &&
350 "Compact model must use __aeabi_cpp_unwind_pr0 as personality");
Logan Chien325823a2013-06-09 12:22:30 +0000351 assert(Opcodes.size() == 4u &&
Logan Chiend8bb4b72013-04-16 12:02:21 +0000352 "Unwind opcode size for __aeabi_cpp_unwind_pr0 must be equal to 4");
Logan Chien325823a2013-06-09 12:22:30 +0000353 EmitBytes(StringRef(reinterpret_cast<const char*>(Opcodes.data()),
Rafael Espindola64e1af82013-07-02 15:49:13 +0000354 Opcodes.size()));
Logan Chien2bcc42c2013-01-30 15:39:04 +0000355 }
356
Logan Chien4ea23b52013-05-10 16:17:24 +0000357 // Switch to the section containing FnStart
358 SwitchSection(&FnStart->getSection());
359
Logan Chien2bcc42c2013-01-30 15:39:04 +0000360 // Clean exception handling frame information
361 Reset();
362}
363
364void ARMELFStreamer::EmitCantUnwind() {
365 CantUnwind = true;
366}
367
Logan Chien325823a2013-06-09 12:22:30 +0000368void ARMELFStreamer::FlushPendingOffset() {
369 if (PendingOffset != 0) {
370 UnwindOpAsm.EmitSPOffset(-PendingOffset);
371 PendingOffset = 0;
372 }
373}
374
Logan Chienc931fce2013-07-02 12:43:27 +0000375void ARMELFStreamer::FlushUnwindOpcodes(bool NoHandlerData) {
Logan Chien325823a2013-06-09 12:22:30 +0000376 // Emit the unwind opcode to restore $sp.
377 if (UsedFP) {
Bill Wendlingbc07a892013-06-18 07:20:20 +0000378 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
Logan Chien325823a2013-06-09 12:22:30 +0000379 int64_t LastRegSaveSPOffset = SPOffset - PendingOffset;
380 UnwindOpAsm.EmitSPOffset(LastRegSaveSPOffset - FPOffset);
Bill Wendlingbc07a892013-06-18 07:20:20 +0000381 UnwindOpAsm.EmitSetSP(MRI->getEncodingValue(FPReg));
Logan Chien325823a2013-06-09 12:22:30 +0000382 } else {
383 FlushPendingOffset();
384 }
385
386 // Finalize the unwind opcode sequence
387 UnwindOpAsm.Finalize(PersonalityIndex, Opcodes);
Logan Chien4ea23b52013-05-10 16:17:24 +0000388
389 // For compact model 0, we have to emit the unwind opcodes in the .ARM.exidx
390 // section. Thus, we don't have to create an entry in the .ARM.extab
391 // section.
Logan Chienc931fce2013-07-02 12:43:27 +0000392 if (NoHandlerData && PersonalityIndex == AEABI_UNWIND_CPP_PR0)
Logan Chien4ea23b52013-05-10 16:17:24 +0000393 return;
394
395 // Switch to .ARM.extab section.
Logan Chien2bcc42c2013-01-30 15:39:04 +0000396 SwitchToExTabSection(*FnStart);
397
398 // Create .ARM.extab label for offset in .ARM.exidx
399 assert(!ExTab);
400 ExTab = getContext().CreateTempSymbol();
401 EmitLabel(ExTab);
402
Logan Chien4ea23b52013-05-10 16:17:24 +0000403 // Emit personality
404 if (Personality) {
405 const MCSymbolRefExpr *PersonalityRef =
406 MCSymbolRefExpr::Create(Personality,
407 MCSymbolRefExpr::VK_ARM_PREL31,
408 getContext());
Logan Chien2bcc42c2013-01-30 15:39:04 +0000409
Rafael Espindola64e1af82013-07-02 15:49:13 +0000410 EmitValue(PersonalityRef, 4);
Logan Chien4ea23b52013-05-10 16:17:24 +0000411 }
Logan Chien2bcc42c2013-01-30 15:39:04 +0000412
413 // Emit unwind opcodes
Logan Chien325823a2013-06-09 12:22:30 +0000414 EmitBytes(StringRef(reinterpret_cast<const char *>(Opcodes.data()),
Rafael Espindola64e1af82013-07-02 15:49:13 +0000415 Opcodes.size()));
Logan Chienc931fce2013-07-02 12:43:27 +0000416
417 // According to ARM EHABI section 9.2, if the __aeabi_unwind_cpp_pr1() or
418 // __aeabi_unwind_cpp_pr2() is used, then the handler data must be emitted
419 // after the unwind opcodes. The handler data consists of several 32-bit
420 // words, and should be terminated by zero.
421 //
422 // In case that the .handlerdata directive is not specified by the
423 // programmer, we should emit zero to terminate the handler data.
424 if (NoHandlerData && !Personality)
425 EmitIntValue(0, 4);
Logan Chien2bcc42c2013-01-30 15:39:04 +0000426}
427
Logan Chien4ea23b52013-05-10 16:17:24 +0000428void ARMELFStreamer::EmitHandlerData() {
429 FlushUnwindOpcodes(false);
430}
431
Logan Chien2bcc42c2013-01-30 15:39:04 +0000432void ARMELFStreamer::EmitPersonality(const MCSymbol *Per) {
433 Personality = Per;
Logan Chiend8bb4b72013-04-16 12:02:21 +0000434 UnwindOpAsm.setPersonality(Per);
Logan Chien2bcc42c2013-01-30 15:39:04 +0000435}
436
Logan Chiend8bb4b72013-04-16 12:02:21 +0000437void ARMELFStreamer::EmitSetFP(unsigned NewFPReg,
438 unsigned NewSPReg,
Logan Chien2bcc42c2013-01-30 15:39:04 +0000439 int64_t Offset) {
Logan Chien325823a2013-06-09 12:22:30 +0000440 assert((NewSPReg == ARM::SP || NewSPReg == FPReg) &&
Logan Chiend8bb4b72013-04-16 12:02:21 +0000441 "the operand of .setfp directive should be either $sp or $fp");
442
443 UsedFP = true;
Logan Chien325823a2013-06-09 12:22:30 +0000444 FPReg = NewFPReg;
445
446 if (NewSPReg == ARM::SP)
447 FPOffset = SPOffset + Offset;
448 else
449 FPOffset += Offset;
Logan Chien2bcc42c2013-01-30 15:39:04 +0000450}
451
452void ARMELFStreamer::EmitPad(int64_t Offset) {
Logan Chien325823a2013-06-09 12:22:30 +0000453 // Track the change of the $sp offset
454 SPOffset -= Offset;
455
456 // To squash multiple .pad directives, we should delay the unwind opcode
457 // until the .save, .vsave, .handlerdata, or .fnend directives.
458 PendingOffset -= Offset;
Logan Chien2bcc42c2013-01-30 15:39:04 +0000459}
460
461void ARMELFStreamer::EmitRegSave(const SmallVectorImpl<unsigned> &RegList,
462 bool IsVector) {
Logan Chien325823a2013-06-09 12:22:30 +0000463 // Collect the registers in the register list
464 unsigned Count = 0;
465 uint32_t Mask = 0;
Bill Wendlingbc07a892013-06-18 07:20:20 +0000466 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
Logan Chiend8bb4b72013-04-16 12:02:21 +0000467 for (size_t i = 0; i < RegList.size(); ++i) {
Bill Wendlingbc07a892013-06-18 07:20:20 +0000468 unsigned Reg = MRI->getEncodingValue(RegList[i]);
Aaron Ballmanab1d27e2013-06-10 16:45:40 +0000469 assert(Reg < (IsVector ? 32U : 16U) && "Register out of range");
Logan Chien325823a2013-06-09 12:22:30 +0000470 unsigned Bit = (1u << Reg);
471 if ((Mask & Bit) == 0) {
472 Mask |= Bit;
473 ++Count;
474 }
Logan Chiend8bb4b72013-04-16 12:02:21 +0000475 }
Logan Chien325823a2013-06-09 12:22:30 +0000476
477 // Track the change the $sp offset: For the .save directive, the
478 // corresponding push instruction will decrease the $sp by (4 * Count).
479 // For the .vsave directive, the corresponding vpush instruction will
480 // decrease $sp by (8 * Count).
481 SPOffset -= Count * (IsVector ? 8 : 4);
482
483 // Emit the opcode
484 FlushPendingOffset();
485 if (IsVector)
486 UnwindOpAsm.EmitVFPRegSave(Mask);
487 else
488 UnwindOpAsm.EmitRegSave(Mask);
Logan Chien2bcc42c2013-01-30 15:39:04 +0000489}
490
Tim Northover5cc3dc82012-12-07 16:50:23 +0000491namespace llvm {
492 MCELFStreamer* createARMELFStreamer(MCContext &Context, MCAsmBackend &TAB,
493 raw_ostream &OS, MCCodeEmitter *Emitter,
494 bool RelaxAll, bool NoExecStack,
495 bool IsThumb) {
496 ARMELFStreamer *S = new ARMELFStreamer(Context, TAB, OS, Emitter, IsThumb);
Rafael Espindolaac4ad252013-10-05 16:42:21 +0000497 // FIXME: This should eventually end up somewhere else where more
498 // intelligent flag decisions can be made. For now we are just maintaining
499 // the status quo for ARM and setting EF_ARM_EABI_VER5 as the default.
500 S->getAssembler().setELFHeaderEFlags(ELF::EF_ARM_EABI_VER5);
501
Tim Northover5cc3dc82012-12-07 16:50:23 +0000502 if (RelaxAll)
503 S->getAssembler().setRelaxAll(true);
504 if (NoExecStack)
505 S->getAssembler().setNoExecStack(true);
506 return S;
507 }
508
509}
510
511