blob: b8103d13c2a08dcc3f0d08aa7f68af856c0e32c9 [file] [log] [blame]
Daniel Dunbar5146a092010-07-12 21:23:32 +00001//===- 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"
Eli Friedmandc1ad222010-07-17 06:27:28 +000011#include "llvm/ADT/Twine.h"
Eli Friedman21444ef2010-07-17 04:29:04 +000012#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbar5146a092010-07-12 21:23:32 +000013#include "llvm/MC/MCContext.h"
Daniel Dunbar5146a092010-07-12 21:23:32 +000014#include "llvm/MC/MCParser/MCAsmLexer.h"
Eli Friedman21444ef2010-07-17 04:29:04 +000015#include "llvm/MC/MCSectionELF.h"
16#include "llvm/MC/MCStreamer.h"
Daniel Dunbarf21e4e92010-07-18 18:31:42 +000017#include "llvm/ADT/Twine.h"
Daniel Dunbar5146a092010-07-12 21:23:32 +000018using namespace llvm;
19
20namespace {
21
22class ELFAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +000023 template<bool (ELFAsmParser::*Handler)(StringRef, SMLoc)>
24 void AddDirectiveHandler(StringRef Directive) {
25 getParser().AddDirectiveHandler(this, Directive,
26 HandleDirective<ELFAsmParser, Handler>);
27 }
28
Daniel Dunbar5146a092010-07-12 21:23:32 +000029 bool ParseSectionSwitch(StringRef Section, unsigned Type,
30 unsigned Flags, SectionKind Kind);
31
32public:
33 ELFAsmParser() {}
34
35 virtual void Initialize(MCAsmParser &Parser) {
36 // Call the base implementation.
37 this->MCAsmParserExtension::Initialize(Parser);
38
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +000039 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveData>(".data");
40 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveText>(".text");
41 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSection>(".section");
42 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSize>(".size");
43 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveLEB128>(".sleb128");
44 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveLEB128>(".uleb128");
Daniel Dunbar5146a092010-07-12 21:23:32 +000045 }
46
47 bool ParseSectionDirectiveData(StringRef, SMLoc) {
48 return ParseSectionSwitch(".data", MCSectionELF::SHT_PROGBITS,
49 MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC,
50 SectionKind::getDataRel());
51 }
52 bool ParseSectionDirectiveText(StringRef, SMLoc) {
53 return ParseSectionSwitch(".text", MCSectionELF::SHT_PROGBITS,
54 MCSectionELF::SHF_EXECINSTR |
55 MCSectionELF::SHF_ALLOC, SectionKind::getText());
56 }
Eli Friedmandc1ad222010-07-17 06:27:28 +000057 bool ParseDirectiveLEB128(StringRef, SMLoc);
Eli Friedman21444ef2010-07-17 04:29:04 +000058 bool ParseDirectiveSection(StringRef, SMLoc);
59 bool ParseDirectiveSize(StringRef, SMLoc);
Daniel Dunbar5146a092010-07-12 21:23:32 +000060};
61
62}
63
64bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
65 unsigned Flags, SectionKind Kind) {
66 if (getLexer().isNot(AsmToken::EndOfStatement))
67 return TokError("unexpected token in section switching directive");
68 Lex();
69
70 getStreamer().SwitchSection(getContext().getELFSection(
71 Section, Type, Flags, Kind));
72
73 return false;
74}
75
Eli Friedman21444ef2010-07-17 04:29:04 +000076bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
Eli Friedmanf82ccf52010-07-17 03:09:18 +000077 StringRef Name;
78 if (getParser().ParseIdentifier(Name))
79 return TokError("expected identifier in directive");
80 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);;
81
82 if (getLexer().isNot(AsmToken::Comma))
83 return TokError("unexpected token in directive");
84 Lex();
85
86 const MCExpr *Expr;
87 if (getParser().ParseExpression(Expr))
88 return true;
89
90 if (getLexer().isNot(AsmToken::EndOfStatement))
91 return TokError("unexpected token in directive");
92
93 getStreamer().EmitELFSize(Sym, Expr);
94 return false;
95}
96
Eli Friedman21444ef2010-07-17 04:29:04 +000097// FIXME: This is a work in progress.
98bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
99 StringRef SectionName;
100 // FIXME: This doesn't parse section names like ".note.GNU-stack" correctly.
101 if (getParser().ParseIdentifier(SectionName))
102 return TokError("expected identifier in directive");
103
104 std::string FlagsStr;
105 StringRef TypeName;
106 int64_t Size = 0;
107 if (getLexer().is(AsmToken::Comma)) {
108 Lex();
109
110 if (getLexer().isNot(AsmToken::String))
111 return TokError("expected string in directive");
112
113 FlagsStr = getTok().getStringContents();
114 Lex();
115
116 AsmToken::TokenKind TypeStartToken;
117 if (getContext().getAsmInfo().getCommentString()[0] == '@')
118 TypeStartToken = AsmToken::Percent;
119 else
120 TypeStartToken = AsmToken::At;
121
122 if (getLexer().is(AsmToken::Comma)) {
123 Lex();
124 if (getLexer().is(TypeStartToken)) {
125 Lex();
126 if (getParser().ParseIdentifier(TypeName))
127 return TokError("expected identifier in directive");
128
129 if (getLexer().is(AsmToken::Comma)) {
130 Lex();
131
132 if (getParser().ParseAbsoluteExpression(Size))
133 return true;
134
135 if (Size <= 0)
136 return TokError("section size must be positive");
137 }
138 }
139 }
140 }
141
142 if (getLexer().isNot(AsmToken::EndOfStatement))
143 return TokError("unexpected token in directive");
144
145 unsigned Flags = 0;
146 for (unsigned i = 0; i < FlagsStr.size(); i++) {
147 switch (FlagsStr[i]) {
148 case 'a':
149 Flags |= MCSectionELF::SHF_ALLOC;
150 break;
151 case 'x':
152 Flags |= MCSectionELF::SHF_EXECINSTR;
153 break;
154 case 'w':
155 Flags |= MCSectionELF::SHF_WRITE;
156 break;
157 case 'M':
158 Flags |= MCSectionELF::SHF_MERGE;
159 break;
160 case 'S':
161 Flags |= MCSectionELF::SHF_STRINGS;
162 break;
163 case 'T':
164 Flags |= MCSectionELF::SHF_TLS;
165 break;
166 case 'c':
167 Flags |= MCSectionELF::XCORE_SHF_CP_SECTION;
168 break;
169 case 'd':
170 Flags |= MCSectionELF::XCORE_SHF_DP_SECTION;
171 break;
172 default:
173 return TokError("unknown flag");
174 }
175 }
176
177 unsigned Type = MCSectionELF::SHT_NULL;
178 if (!TypeName.empty()) {
179 if (TypeName == "init_array")
180 Type = MCSectionELF::SHT_INIT_ARRAY;
181 else if (TypeName == "fini_array")
182 Type = MCSectionELF::SHT_FINI_ARRAY;
183 else if (TypeName == "preinit_array")
184 Type = MCSectionELF::SHT_PREINIT_ARRAY;
185 else if (TypeName == "nobits")
186 Type = MCSectionELF::SHT_NOBITS;
187 else if (TypeName == "progbits")
188 Type = MCSectionELF::SHT_PROGBITS;
189 else
190 return TokError("unknown section type");
191 }
192
193 SectionKind Kind = (Flags & MCSectionELF::SHF_EXECINSTR)
194 ? SectionKind::getText()
195 : SectionKind::getDataRel();
196 getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type,
197 Flags, Kind, false));
198 return false;
199}
200
Eli Friedmandc1ad222010-07-17 06:27:28 +0000201bool ELFAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
202 int64_t Value;
203 if (getParser().ParseAbsoluteExpression(Value))
204 return true;
205
206 if (getLexer().isNot(AsmToken::EndOfStatement))
207 return TokError("unexpected token in directive");
208
209 // FIXME: Add proper MC support.
210 if (getContext().getAsmInfo().hasLEB128()) {
211 if (DirName[1] == 's')
212 getStreamer().EmitRawText("\t.sleb128\t" + Twine(Value));
213 else
214 getStreamer().EmitRawText("\t.uleb128\t" + Twine(Value));
215 return false;
216 }
217 // FIXME: This shouldn't be an error!
218 return TokError("LEB128 not supported yet");
219}
220
Daniel Dunbar5146a092010-07-12 21:23:32 +0000221namespace llvm {
222
223MCAsmParserExtension *createELFAsmParser() {
224 return new ELFAsmParser;
225}
226
227}