WasmAsmParser.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. //===- WasmAsmParser.cpp - Wasm Assembly Parser -----------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. // --
  8. //
  9. // Note, this is for wasm, the binary format (analogous to ELF), not wasm,
  10. // the instruction set (analogous to x86), for which parsing code lives in
  11. // WebAssemblyAsmParser.
  12. //
  13. // This file contains processing for generic directives implemented using
  14. // MCTargetStreamer, the ones that depend on WebAssemblyTargetStreamer are in
  15. // WebAssemblyAsmParser.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #include "llvm/BinaryFormat/Wasm.h"
  19. #include "llvm/MC/MCContext.h"
  20. #include "llvm/MC/MCParser/MCAsmLexer.h"
  21. #include "llvm/MC/MCParser/MCAsmParser.h"
  22. #include "llvm/MC/MCParser/MCAsmParserExtension.h"
  23. #include "llvm/MC/MCSectionWasm.h"
  24. #include "llvm/MC/MCStreamer.h"
  25. #include "llvm/MC/MCSymbol.h"
  26. #include "llvm/MC/MCSymbolWasm.h"
  27. #include "llvm/Support/MachineValueType.h"
  28. using namespace llvm;
  29. namespace {
  30. class WasmAsmParser : public MCAsmParserExtension {
  31. MCAsmParser *Parser = nullptr;
  32. MCAsmLexer *Lexer = nullptr;
  33. template<bool (WasmAsmParser::*HandlerMethod)(StringRef, SMLoc)>
  34. void addDirectiveHandler(StringRef Directive) {
  35. MCAsmParser::ExtensionDirectiveHandler Handler = std::make_pair(
  36. this, HandleDirective<WasmAsmParser, HandlerMethod>);
  37. getParser().addDirectiveHandler(Directive, Handler);
  38. }
  39. public:
  40. WasmAsmParser() { BracketExpressionsSupported = true; }
  41. void Initialize(MCAsmParser &P) override {
  42. Parser = &P;
  43. Lexer = &Parser->getLexer();
  44. // Call the base implementation.
  45. this->MCAsmParserExtension::Initialize(*Parser);
  46. addDirectiveHandler<&WasmAsmParser::parseSectionDirectiveText>(".text");
  47. addDirectiveHandler<&WasmAsmParser::parseSectionDirective>(".section");
  48. addDirectiveHandler<&WasmAsmParser::parseDirectiveSize>(".size");
  49. addDirectiveHandler<&WasmAsmParser::parseDirectiveType>(".type");
  50. addDirectiveHandler<&WasmAsmParser::ParseDirectiveIdent>(".ident");
  51. addDirectiveHandler<
  52. &WasmAsmParser::ParseDirectiveSymbolAttribute>(".weak");
  53. addDirectiveHandler<
  54. &WasmAsmParser::ParseDirectiveSymbolAttribute>(".local");
  55. addDirectiveHandler<
  56. &WasmAsmParser::ParseDirectiveSymbolAttribute>(".internal");
  57. addDirectiveHandler<
  58. &WasmAsmParser::ParseDirectiveSymbolAttribute>(".hidden");
  59. }
  60. bool error(const StringRef &Msg, const AsmToken &Tok) {
  61. return Parser->Error(Tok.getLoc(), Msg + Tok.getString());
  62. }
  63. bool isNext(AsmToken::TokenKind Kind) {
  64. auto Ok = Lexer->is(Kind);
  65. if (Ok)
  66. Lex();
  67. return Ok;
  68. }
  69. bool expect(AsmToken::TokenKind Kind, const char *KindName) {
  70. if (!isNext(Kind))
  71. return error(std::string("Expected ") + KindName + ", instead got: ",
  72. Lexer->getTok());
  73. return false;
  74. }
  75. bool parseSectionDirectiveText(StringRef, SMLoc) {
  76. // FIXME: .text currently no-op.
  77. return false;
  78. }
  79. uint32_t parseSectionFlags(StringRef FlagStr, bool &Passive, bool &Group) {
  80. uint32_t flags = 0;
  81. for (char C : FlagStr) {
  82. switch (C) {
  83. case 'p':
  84. Passive = true;
  85. break;
  86. case 'G':
  87. Group = true;
  88. break;
  89. case 'T':
  90. flags |= wasm::WASM_SEG_FLAG_TLS;
  91. break;
  92. case 'S':
  93. flags |= wasm::WASM_SEG_FLAG_STRINGS;
  94. break;
  95. default:
  96. return -1U;
  97. }
  98. }
  99. return flags;
  100. }
  101. bool parseGroup(StringRef &GroupName) {
  102. if (Lexer->isNot(AsmToken::Comma))
  103. return TokError("expected group name");
  104. Lex();
  105. if (Lexer->is(AsmToken::Integer)) {
  106. GroupName = getTok().getString();
  107. Lex();
  108. } else if (Parser->parseIdentifier(GroupName)) {
  109. return TokError("invalid group name");
  110. }
  111. if (Lexer->is(AsmToken::Comma)) {
  112. Lex();
  113. StringRef Linkage;
  114. if (Parser->parseIdentifier(Linkage))
  115. return TokError("invalid linkage");
  116. if (Linkage != "comdat")
  117. return TokError("Linkage must be 'comdat'");
  118. }
  119. return false;
  120. }
  121. bool parseSectionDirective(StringRef, SMLoc loc) {
  122. StringRef Name;
  123. if (Parser->parseIdentifier(Name))
  124. return TokError("expected identifier in directive");
  125. if (expect(AsmToken::Comma, ","))
  126. return true;
  127. if (Lexer->isNot(AsmToken::String))
  128. return error("expected string in directive, instead got: ", Lexer->getTok());
  129. auto Kind = StringSwitch<Optional<SectionKind>>(Name)
  130. .StartsWith(".data", SectionKind::getData())
  131. .StartsWith(".tdata", SectionKind::getThreadData())
  132. .StartsWith(".tbss", SectionKind::getThreadBSS())
  133. .StartsWith(".rodata", SectionKind::getReadOnly())
  134. .StartsWith(".text", SectionKind::getText())
  135. .StartsWith(".custom_section", SectionKind::getMetadata())
  136. .StartsWith(".bss", SectionKind::getBSS())
  137. // See use of .init_array in WasmObjectWriter and
  138. // TargetLoweringObjectFileWasm
  139. .StartsWith(".init_array", SectionKind::getData())
  140. .StartsWith(".debug_", SectionKind::getMetadata())
  141. .Default(SectionKind::getData());
  142. // Update section flags if present in this .section directive
  143. bool Passive = false;
  144. bool Group = false;
  145. uint32_t Flags =
  146. parseSectionFlags(getTok().getStringContents(), Passive, Group);
  147. if (Flags == -1U)
  148. return TokError("unknown flag");
  149. Lex();
  150. if (expect(AsmToken::Comma, ",") || expect(AsmToken::At, "@"))
  151. return true;
  152. StringRef GroupName;
  153. if (Group && parseGroup(GroupName))
  154. return true;
  155. if (expect(AsmToken::EndOfStatement, "eol"))
  156. return true;
  157. // TODO: Parse UniqueID
  158. MCSectionWasm *WS = getContext().getWasmSection(
  159. Name, Kind.getValue(), Flags, GroupName, MCContext::GenericSectionID);
  160. if (WS->getSegmentFlags() != Flags)
  161. Parser->Error(loc, "changed section flags for " + Name +
  162. ", expected: 0x" +
  163. utohexstr(WS->getSegmentFlags()));
  164. if (Passive) {
  165. if (!WS->isWasmData())
  166. return Parser->Error(loc, "Only data sections can be passive");
  167. WS->setPassive();
  168. }
  169. getStreamer().SwitchSection(WS);
  170. return false;
  171. }
  172. // TODO: This function is almost the same as ELFAsmParser::ParseDirectiveSize
  173. // so maybe could be shared somehow.
  174. bool parseDirectiveSize(StringRef, SMLoc) {
  175. StringRef Name;
  176. if (Parser->parseIdentifier(Name))
  177. return TokError("expected identifier in directive");
  178. auto Sym = getContext().getOrCreateSymbol(Name);
  179. if (expect(AsmToken::Comma, ","))
  180. return true;
  181. const MCExpr *Expr;
  182. if (Parser->parseExpression(Expr))
  183. return true;
  184. if (expect(AsmToken::EndOfStatement, "eol"))
  185. return true;
  186. // This is done automatically by the assembler for functions currently,
  187. // so this is only currently needed for data sections:
  188. getStreamer().emitELFSize(Sym, Expr);
  189. return false;
  190. }
  191. bool parseDirectiveType(StringRef, SMLoc) {
  192. // This could be the start of a function, check if followed by
  193. // "label,@function"
  194. if (!Lexer->is(AsmToken::Identifier))
  195. return error("Expected label after .type directive, got: ",
  196. Lexer->getTok());
  197. auto WasmSym = cast<MCSymbolWasm>(
  198. getStreamer().getContext().getOrCreateSymbol(
  199. Lexer->getTok().getString()));
  200. Lex();
  201. if (!(isNext(AsmToken::Comma) && isNext(AsmToken::At) &&
  202. Lexer->is(AsmToken::Identifier)))
  203. return error("Expected label,@type declaration, got: ", Lexer->getTok());
  204. auto TypeName = Lexer->getTok().getString();
  205. if (TypeName == "function") {
  206. WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
  207. auto *Current =
  208. cast<MCSectionWasm>(getStreamer().getCurrentSection().first);
  209. if (Current->getGroup())
  210. WasmSym->setComdat(true);
  211. } else if (TypeName == "global")
  212. WasmSym->setType(wasm::WASM_SYMBOL_TYPE_GLOBAL);
  213. else if (TypeName == "object")
  214. WasmSym->setType(wasm::WASM_SYMBOL_TYPE_DATA);
  215. else
  216. return error("Unknown WASM symbol type: ", Lexer->getTok());
  217. Lex();
  218. return expect(AsmToken::EndOfStatement, "EOL");
  219. }
  220. // FIXME: Shared with ELF.
  221. /// ParseDirectiveIdent
  222. /// ::= .ident string
  223. bool ParseDirectiveIdent(StringRef, SMLoc) {
  224. if (getLexer().isNot(AsmToken::String))
  225. return TokError("unexpected token in '.ident' directive");
  226. StringRef Data = getTok().getIdentifier();
  227. Lex();
  228. if (getLexer().isNot(AsmToken::EndOfStatement))
  229. return TokError("unexpected token in '.ident' directive");
  230. Lex();
  231. getStreamer().emitIdent(Data);
  232. return false;
  233. }
  234. // FIXME: Shared with ELF.
  235. /// ParseDirectiveSymbolAttribute
  236. /// ::= { ".local", ".weak", ... } [ identifier ( , identifier )* ]
  237. bool ParseDirectiveSymbolAttribute(StringRef Directive, SMLoc) {
  238. MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Directive)
  239. .Case(".weak", MCSA_Weak)
  240. .Case(".local", MCSA_Local)
  241. .Case(".hidden", MCSA_Hidden)
  242. .Case(".internal", MCSA_Internal)
  243. .Case(".protected", MCSA_Protected)
  244. .Default(MCSA_Invalid);
  245. assert(Attr != MCSA_Invalid && "unexpected symbol attribute directive!");
  246. if (getLexer().isNot(AsmToken::EndOfStatement)) {
  247. while (true) {
  248. StringRef Name;
  249. if (getParser().parseIdentifier(Name))
  250. return TokError("expected identifier in directive");
  251. MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
  252. getStreamer().emitSymbolAttribute(Sym, Attr);
  253. if (getLexer().is(AsmToken::EndOfStatement))
  254. break;
  255. if (getLexer().isNot(AsmToken::Comma))
  256. return TokError("unexpected token in directive");
  257. Lex();
  258. }
  259. }
  260. Lex();
  261. return false;
  262. }
  263. };
  264. } // end anonymous namespace
  265. namespace llvm {
  266. MCAsmParserExtension *createWasmAsmParser() {
  267. return new WasmAsmParser;
  268. }
  269. } // end namespace llvm