MC/AsmParser: Move ELF specific parser to ELFAsmParser.cpp.
[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/MC/MCSectionELF.h"
12 #include "llvm/MC/MCContext.h"
13 #include "llvm/MC/MCStreamer.h"
14 #include "llvm/MC/MCParser/MCAsmLexer.h"
15 using namespace llvm;
16
17 namespace {
18
19 class ELFAsmParser : public MCAsmParserExtension {
20   bool ParseSectionSwitch(StringRef Section, unsigned Type,
21                           unsigned Flags, SectionKind Kind);
22
23 public:
24   ELFAsmParser() {}
25
26   virtual void Initialize(MCAsmParser &Parser) {
27     // Call the base implementation.
28     this->MCAsmParserExtension::Initialize(Parser);
29
30     Parser.AddDirectiveHandler(this, ".data", MCAsmParser::DirectiveHandler(
31                                  &ELFAsmParser::ParseSectionDirectiveData));
32     Parser.AddDirectiveHandler(this, ".text", MCAsmParser::DirectiveHandler(
33                                  &ELFAsmParser::ParseSectionDirectiveText));
34   }
35
36   bool ParseSectionDirectiveData(StringRef, SMLoc) {
37     return ParseSectionSwitch(".data", MCSectionELF::SHT_PROGBITS,
38                               MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC,
39                               SectionKind::getDataRel());
40   }
41   bool ParseSectionDirectiveText(StringRef, SMLoc) {
42     return ParseSectionSwitch(".text", MCSectionELF::SHT_PROGBITS,
43                               MCSectionELF::SHF_EXECINSTR |
44                               MCSectionELF::SHF_ALLOC, SectionKind::getText());
45   }
46 };
47
48 }
49
50 bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
51                                       unsigned Flags, SectionKind Kind) {
52   if (getLexer().isNot(AsmToken::EndOfStatement))
53     return TokError("unexpected token in section switching directive");
54   Lex();
55
56   getStreamer().SwitchSection(getContext().getELFSection(
57                                 Section, Type, Flags, Kind));
58
59   return false;
60 }
61
62 namespace llvm {
63
64 MCAsmParserExtension *createELFAsmParser() {
65   return new ELFAsmParser;
66 }
67
68 }