1 //===- AsmParser.h - Parser for Assembly Files ------------------*- C++ -*-===//
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 // This class declares the parser for assembly files.
12 //===----------------------------------------------------------------------===//
17 #include "llvm/MC/MCParser/AsmLexer.h"
18 #include "llvm/MC/MCParser/AsmCond.h"
19 #include "llvm/MC/MCParser/MCAsmParser.h"
20 #include "llvm/MC/MCSectionMachO.h"
21 #include "llvm/MC/MCStreamer.h"
22 #include "llvm/MC/MCAsmInfo.h"
23 #include "llvm/ADT/StringMap.h"
29 class MCAsmParserExtension;
36 class TargetAsmParser;
39 class AsmParser : public MCAsmParser {
40 AsmParser(const AsmParser &); // DO NOT IMPLEMENT
41 void operator=(const AsmParser &); // DO NOT IMPLEMENT
47 MCAsmParserExtension *GenericParser;
48 MCAsmParserExtension *PlatformParser;
50 /// This is the current buffer index we're lexing from as managed by the
55 std::vector<AsmCond> TheCondStack;
57 /// DirectiveMap - This is a table handlers for directives. Each handler is
58 /// invoked after the directive identifier is read and is responsible for
59 /// parsing and validating the rest of the directive. The handler is passed
60 /// in the directive name and the location of the directive keyword.
61 StringMap<std::pair<MCAsmParserExtension*, DirectiveHandler> > DirectiveMap;
63 AsmParser(const Target &T, SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
64 const MCAsmInfo &MAI);
67 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
69 void AddDirectiveHandler(MCAsmParserExtension *Object,
71 DirectiveHandler Handler) {
72 DirectiveMap[Directive] = std::make_pair(Object, Handler);
76 /// @name MCAsmParser Interface
79 virtual SourceMgr &getSourceManager() { return SrcMgr; }
80 virtual MCAsmLexer &getLexer() { return Lexer; }
81 virtual MCContext &getContext() { return Ctx; }
82 virtual MCStreamer &getStreamer() { return Out; }
84 virtual void Warning(SMLoc L, const Twine &Meg);
85 virtual bool Error(SMLoc L, const Twine &Msg);
87 const AsmToken &Lex();
89 bool ParseExpression(const MCExpr *&Res);
90 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
91 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
92 virtual bool ParseAbsoluteExpression(int64_t &Res);
97 bool ParseStatement();
99 void PrintMessage(SMLoc Loc, const std::string &Msg, const char *Type) const;
101 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
102 bool EnterIncludeFile(const std::string &Filename);
104 void EatToEndOfStatement();
106 bool ParseAssignment(StringRef Name);
108 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
109 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
110 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
112 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
113 /// and set \arg Res to the identifier contents.
114 bool ParseIdentifier(StringRef &Res);
116 // Directive Parsing.
117 bool ParseDirectiveAscii(bool ZeroTerminated); // ".ascii", ".asciiz"
118 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
119 bool ParseDirectiveFill(); // ".fill"
120 bool ParseDirectiveSpace(); // ".space"
121 bool ParseDirectiveSet(); // ".set"
122 bool ParseDirectiveOrg(); // ".org"
123 // ".align{,32}", ".p2align{,w,l}"
124 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
126 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
127 /// accepts a single symbol (which should be a label or an external).
128 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
129 bool ParseDirectiveELFType(); // ELF specific ".type"
131 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
133 bool ParseDirectiveAbort(); // ".abort"
134 bool ParseDirectiveInclude(); // ".include"
136 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
137 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
138 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
139 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
141 /// ParseEscapedString - Parse the current token as a string which may include
142 /// escaped characters and return the string contents.
143 bool ParseEscapedString(std::string &Data);
146 } // end namespace llvm