blob: 7a54dd39aa479720fbb654303b3faec97212ce19 [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"
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"
15using namespace llvm;
16
17namespace {
18
19class ELFAsmParser : public MCAsmParserExtension {
20 bool ParseSectionSwitch(StringRef Section, unsigned Type,
21 unsigned Flags, SectionKind Kind);
22
23public:
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
50bool 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
62namespace llvm {
63
64MCAsmParserExtension *createELFAsmParser() {
65 return new ELFAsmParser;
66}
67
68}