Daniel Dunbar | 5146a09 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 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" |
Eli Friedman | dc1ad22 | 2010-07-17 06:27:28 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/Twine.h" |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 12 | #include "llvm/MC/MCAsmInfo.h" |
Daniel Dunbar | 5146a09 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 13 | #include "llvm/MC/MCContext.h" |
Daniel Dunbar | 5146a09 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCParser/MCAsmLexer.h" |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCSectionELF.h" |
| 16 | #include "llvm/MC/MCStreamer.h" |
Daniel Dunbar | f21e4e9 | 2010-07-18 18:31:42 +0000 | [diff] [blame^] | 17 | #include "llvm/ADT/Twine.h" |
Daniel Dunbar | 5146a09 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 18 | using namespace llvm; |
| 19 | |
| 20 | namespace { |
| 21 | |
| 22 | class ELFAsmParser : public MCAsmParserExtension { |
| 23 | bool ParseSectionSwitch(StringRef Section, unsigned Type, |
| 24 | unsigned Flags, SectionKind Kind); |
| 25 | |
| 26 | public: |
| 27 | ELFAsmParser() {} |
| 28 | |
| 29 | virtual void Initialize(MCAsmParser &Parser) { |
| 30 | // Call the base implementation. |
| 31 | this->MCAsmParserExtension::Initialize(Parser); |
| 32 | |
| 33 | Parser.AddDirectiveHandler(this, ".data", MCAsmParser::DirectiveHandler( |
| 34 | &ELFAsmParser::ParseSectionDirectiveData)); |
| 35 | Parser.AddDirectiveHandler(this, ".text", MCAsmParser::DirectiveHandler( |
| 36 | &ELFAsmParser::ParseSectionDirectiveText)); |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 37 | Parser.AddDirectiveHandler(this, ".section", MCAsmParser::DirectiveHandler( |
| 38 | &ELFAsmParser::ParseDirectiveSection)); |
Eli Friedman | f82ccf5 | 2010-07-17 03:09:18 +0000 | [diff] [blame] | 39 | Parser.AddDirectiveHandler(this, ".size", MCAsmParser::DirectiveHandler( |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 40 | &ELFAsmParser::ParseDirectiveSize)); |
Eli Friedman | dc1ad22 | 2010-07-17 06:27:28 +0000 | [diff] [blame] | 41 | Parser.AddDirectiveHandler(this, ".sleb128", MCAsmParser::DirectiveHandler( |
| 42 | &ELFAsmParser::ParseDirectiveLEB128)); |
| 43 | Parser.AddDirectiveHandler(this, ".uleb128", MCAsmParser::DirectiveHandler( |
| 44 | &ELFAsmParser::ParseDirectiveLEB128)); |
Daniel Dunbar | 5146a09 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 45 | } |
| 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 Friedman | dc1ad22 | 2010-07-17 06:27:28 +0000 | [diff] [blame] | 57 | bool ParseDirectiveLEB128(StringRef, SMLoc); |
Eli Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 58 | bool ParseDirectiveSection(StringRef, SMLoc); |
| 59 | bool ParseDirectiveSize(StringRef, SMLoc); |
Daniel Dunbar | 5146a09 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | } |
| 63 | |
| 64 | bool 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 Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 76 | bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) { |
Eli Friedman | f82ccf5 | 2010-07-17 03:09:18 +0000 | [diff] [blame] | 77 | 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 Friedman | 21444ef | 2010-07-17 04:29:04 +0000 | [diff] [blame] | 97 | // FIXME: This is a work in progress. |
| 98 | bool 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 Friedman | dc1ad22 | 2010-07-17 06:27:28 +0000 | [diff] [blame] | 201 | bool 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 Dunbar | 5146a09 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 221 | namespace llvm { |
| 222 | |
| 223 | MCAsmParserExtension *createELFAsmParser() { |
| 224 | return new ELFAsmParser; |
| 225 | } |
| 226 | |
| 227 | } |