1 //===- ELFAsmParser.cpp - ELF Assembly Parser -----------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
19 class ELFAsmParser : public MCAsmParserExtension {
20 bool ParseSectionSwitch(StringRef Section, unsigned Type,
21 unsigned Flags, SectionKind Kind);
26 virtual void Initialize(MCAsmParser &Parser) {
27 // Call the base implementation.
28 this->MCAsmParserExtension::Initialize(Parser);
30 Parser.AddDirectiveHandler(this, ".data", MCAsmParser::DirectiveHandler(
31 &ELFAsmParser::ParseSectionDirectiveData));
32 Parser.AddDirectiveHandler(this, ".text", MCAsmParser::DirectiveHandler(
33 &ELFAsmParser::ParseSectionDirectiveText));
36 bool ParseSectionDirectiveData(StringRef, SMLoc) {
37 return ParseSectionSwitch(".data", MCSectionELF::SHT_PROGBITS,
38 MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC,
39 SectionKind::getDataRel());
41 bool ParseSectionDirectiveText(StringRef, SMLoc) {
42 return ParseSectionSwitch(".text", MCSectionELF::SHT_PROGBITS,
43 MCSectionELF::SHF_EXECINSTR |
44 MCSectionELF::SHF_ALLOC, SectionKind::getText());
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");
56 getStreamer().SwitchSection(getContext().getELFSection(
57 Section, Type, Flags, Kind));
64 MCAsmParserExtension *createELFAsmParser() {
65 return new ELFAsmParser;