Add AsmParser support for the ELF .previous directive. Patch by Roman Divacky.
[oota-llvm.git] / lib / MC / MCParser / ELFAsmParser.cpp
1 //===- ELFAsmParser.cpp - ELF Assembly Parser -----------------------------===//
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 #include "llvm/MC/MCParser/MCAsmParserExtension.h"
11 #include "llvm/ADT/Twine.h"
12 #include "llvm/MC/MCAsmInfo.h"
13 #include "llvm/MC/MCContext.h"
14 #include "llvm/MC/MCParser/MCAsmLexer.h"
15 #include "llvm/MC/MCSectionELF.h"
16 #include "llvm/MC/MCStreamer.h"
17 #include "llvm/ADT/Twine.h"
18 using namespace llvm;
19
20 namespace {
21
22 class ELFAsmParser : public MCAsmParserExtension {
23   template<bool (ELFAsmParser::*Handler)(StringRef, SMLoc)>
24   void AddDirectiveHandler(StringRef Directive) {
25     getParser().AddDirectiveHandler(this, Directive,
26                                     HandleDirective<ELFAsmParser, Handler>);
27   }
28
29   bool ParseSectionSwitch(StringRef Section, unsigned Type,
30                           unsigned Flags, SectionKind Kind);
31
32 public:
33   ELFAsmParser() {}
34
35   virtual void Initialize(MCAsmParser &Parser) {
36     // Call the base implementation.
37     this->MCAsmParserExtension::Initialize(Parser);
38
39     AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveData>(".data");
40     AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveText>(".text");
41     AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveBSS>(".bss");
42     AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveRoData>(".rodata");
43     AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTData>(".tdata");
44     AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTBSS>(".tbss");
45     AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveDataRel>(".data.rel");
46     AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveDataRelRo>(".data.rel.ro");
47     AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveDataRelRoLocal>(".data.rel.ro.local");
48     AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveEhFrame>(".eh_frame");
49     AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSection>(".section");
50     AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSize>(".size");
51     AddDirectiveHandler<&ELFAsmParser::ParseDirectiveLEB128>(".sleb128");
52     AddDirectiveHandler<&ELFAsmParser::ParseDirectiveLEB128>(".uleb128");
53     AddDirectiveHandler<&ELFAsmParser::ParseDirectivePrevious>(".previous");
54   }
55
56   bool ParseSectionDirectiveData(StringRef, SMLoc) {
57     return ParseSectionSwitch(".data", MCSectionELF::SHT_PROGBITS,
58                               MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC,
59                               SectionKind::getDataRel());
60   }
61   bool ParseSectionDirectiveText(StringRef, SMLoc) {
62     return ParseSectionSwitch(".text", MCSectionELF::SHT_PROGBITS,
63                               MCSectionELF::SHF_EXECINSTR |
64                               MCSectionELF::SHF_ALLOC, SectionKind::getText());
65   }
66   bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
67     return ParseSectionSwitch(".bss", MCSectionELF::SHT_NOBITS,
68                               MCSectionELF::SHF_WRITE |
69                               MCSectionELF::SHF_ALLOC, SectionKind::getBSS());
70   }
71   bool ParseSectionDirectiveRoData(StringRef, SMLoc) {
72     return ParseSectionSwitch(".rodata", MCSectionELF::SHT_PROGBITS,
73                               MCSectionELF::SHF_ALLOC,
74                               SectionKind::getReadOnly());
75   }
76   bool ParseSectionDirectiveTData(StringRef, SMLoc) {
77     return ParseSectionSwitch(".tdata", MCSectionELF::SHT_PROGBITS,
78                               MCSectionELF::SHF_ALLOC |
79                               MCSectionELF::SHF_TLS | MCSectionELF::SHF_WRITE,
80                               SectionKind::getThreadData());
81   }
82   bool ParseSectionDirectiveTBSS(StringRef, SMLoc) {
83     return ParseSectionSwitch(".tbss", MCSectionELF::SHT_NOBITS,
84                               MCSectionELF::SHF_ALLOC |
85                               MCSectionELF::SHF_TLS | MCSectionELF::SHF_WRITE,
86                               SectionKind::getThreadBSS());
87   }
88   bool ParseSectionDirectiveDataRel(StringRef, SMLoc) {
89     return ParseSectionSwitch(".data.rel", MCSectionELF::SHT_PROGBITS,
90                               MCSectionELF::SHF_ALLOC |
91                               MCSectionELF::SHF_WRITE,
92                               SectionKind::getDataRel());
93   }
94   bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) {
95     return ParseSectionSwitch(".data.rel.ro", MCSectionELF::SHT_PROGBITS,
96                               MCSectionELF::SHF_ALLOC |
97                               MCSectionELF::SHF_WRITE,
98                               SectionKind::getReadOnlyWithRel());
99   }
100   bool ParseSectionDirectiveDataRelRoLocal(StringRef, SMLoc) {
101     return ParseSectionSwitch(".data.rel.ro.local", MCSectionELF::SHT_PROGBITS,
102                               MCSectionELF::SHF_ALLOC |
103                               MCSectionELF::SHF_WRITE,
104                               SectionKind::getReadOnlyWithRelLocal());
105   }
106   bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) {
107     return ParseSectionSwitch(".eh_frame", MCSectionELF::SHT_PROGBITS,
108                               MCSectionELF::SHF_ALLOC |
109                               MCSectionELF::SHF_WRITE,
110                               SectionKind::getDataRel());
111   }
112   bool ParseDirectiveLEB128(StringRef, SMLoc);
113   bool ParseDirectiveSection(StringRef, SMLoc);
114   bool ParseDirectiveSize(StringRef, SMLoc);
115   bool ParseDirectivePrevious(StringRef, SMLoc);
116 };
117
118 }
119
120 bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
121                                       unsigned Flags, SectionKind Kind) {
122   if (getLexer().isNot(AsmToken::EndOfStatement))
123     return TokError("unexpected token in section switching directive");
124   Lex();
125
126   getStreamer().SwitchSection(getContext().getELFSection(
127                                 Section, Type, Flags, Kind));
128
129   return false;
130 }
131
132 bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
133   StringRef Name;
134   if (getParser().ParseIdentifier(Name))
135     return TokError("expected identifier in directive");
136   MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);;
137
138   if (getLexer().isNot(AsmToken::Comma))
139     return TokError("unexpected token in directive");
140   Lex();
141
142   const MCExpr *Expr;
143   if (getParser().ParseExpression(Expr))
144     return true;
145
146   if (getLexer().isNot(AsmToken::EndOfStatement))
147     return TokError("unexpected token in directive");
148
149   getStreamer().EmitELFSize(Sym, Expr);
150   return false;
151 }
152
153 // FIXME: This is a work in progress.
154 bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
155   StringRef SectionName;
156   // FIXME: This doesn't parse section names like ".note.GNU-stack" correctly.
157   if (getParser().ParseIdentifier(SectionName))
158     return TokError("expected identifier in directive");
159
160   std::string FlagsStr;
161   StringRef TypeName;
162   int64_t Size = 0;
163   if (getLexer().is(AsmToken::Comma)) {
164     Lex();
165
166     if (getLexer().isNot(AsmToken::String))
167       return TokError("expected string in directive");
168
169     FlagsStr = getTok().getStringContents();
170     Lex();
171
172     AsmToken::TokenKind TypeStartToken;
173     if (getContext().getAsmInfo().getCommentString()[0] == '@')
174       TypeStartToken = AsmToken::Percent;
175     else
176       TypeStartToken = AsmToken::At;
177
178     if (getLexer().is(AsmToken::Comma)) {
179       Lex();
180       if (getLexer().is(TypeStartToken)) {
181         Lex();
182         if (getParser().ParseIdentifier(TypeName))
183           return TokError("expected identifier in directive");
184
185         if (getLexer().is(AsmToken::Comma)) {
186           Lex();
187
188           if (getParser().ParseAbsoluteExpression(Size))
189             return true;
190
191           if (Size <= 0)
192             return TokError("section size must be positive");
193         }
194       }
195     }
196   }
197
198   if (getLexer().isNot(AsmToken::EndOfStatement))
199     return TokError("unexpected token in directive");
200
201   unsigned Flags = 0;
202   for (unsigned i = 0; i < FlagsStr.size(); i++) {
203     switch (FlagsStr[i]) {
204     case 'a':
205       Flags |= MCSectionELF::SHF_ALLOC;
206       break;
207     case 'x':
208       Flags |= MCSectionELF::SHF_EXECINSTR;
209       break;
210     case 'w':
211       Flags |= MCSectionELF::SHF_WRITE;
212       break;
213     case 'M':
214       Flags |= MCSectionELF::SHF_MERGE;
215       break;
216     case 'S':
217       Flags |= MCSectionELF::SHF_STRINGS;
218       break;
219     case 'T':
220       Flags |= MCSectionELF::SHF_TLS;
221       break;
222     case 'c':
223       Flags |= MCSectionELF::XCORE_SHF_CP_SECTION;
224       break;
225     case 'd':
226       Flags |= MCSectionELF::XCORE_SHF_DP_SECTION;
227       break;
228     default:
229       return TokError("unknown flag");
230     }
231   }
232
233   unsigned Type = MCSectionELF::SHT_NULL;
234   if (!TypeName.empty()) {
235     if (TypeName == "init_array")
236       Type = MCSectionELF::SHT_INIT_ARRAY;
237     else if (TypeName == "fini_array")
238       Type = MCSectionELF::SHT_FINI_ARRAY;
239     else if (TypeName == "preinit_array")
240       Type = MCSectionELF::SHT_PREINIT_ARRAY;
241     else if (TypeName == "nobits")
242       Type = MCSectionELF::SHT_NOBITS;
243     else if (TypeName == "progbits")
244       Type = MCSectionELF::SHT_PROGBITS;
245     else
246       return TokError("unknown section type");
247   }
248
249   SectionKind Kind = (Flags & MCSectionELF::SHF_EXECINSTR)
250                      ? SectionKind::getText()
251                      : SectionKind::getDataRel();
252   getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type,
253                                                          Flags, Kind, false));
254   return false;
255 }
256
257 bool ELFAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
258   int64_t Value;
259   if (getParser().ParseAbsoluteExpression(Value))
260     return true;
261
262   if (getLexer().isNot(AsmToken::EndOfStatement))
263     return TokError("unexpected token in directive");
264
265   // FIXME: Add proper MC support.
266   if (getContext().getAsmInfo().hasLEB128()) {
267     if (DirName[1] == 's')
268       getStreamer().EmitRawText("\t.sleb128\t" + Twine(Value));
269     else
270       getStreamer().EmitRawText("\t.uleb128\t" + Twine(Value));
271     return false;
272   }
273   // FIXME: This shouldn't be an error!
274   return TokError("LEB128 not supported yet");
275 }
276
277 bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
278   const MCSection *PreviousSection = getStreamer().getPreviousSection();
279   if (PreviousSection != NULL)
280     getStreamer().SwitchSection(PreviousSection);
281
282   return false;
283 }
284
285 namespace llvm {
286
287 MCAsmParserExtension *createELFAsmParser() {
288   return new ELFAsmParser;
289 }
290
291 }