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"
35 class TargetAsmParser;
38 class AsmParser : public MCAsmParser {
44 TargetAsmParser *TargetParser;
46 /// This is the current buffer index we're lexing from as managed by the
51 std::vector<AsmCond> TheCondStack;
53 /// DirectiveMap - This is a table handlers for directives. Each handler is
54 /// invoked after the directive identifier is read and is responsible for
55 /// parsing and validating the rest of the directive. The handler is passed
56 /// in the directive name and the location of the directive keyword.
57 StringMap<bool(AsmParser::*)(StringRef, SMLoc)> DirectiveMap;
59 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
60 const MCAsmInfo &MAI);
63 bool Run(bool NoInitialTextSection, bool NoFinalize = false);
66 void AddDirectiveHandler(StringRef Directive,
67 bool (AsmParser::*Handler)(StringRef, SMLoc)) {
68 DirectiveMap[Directive] = Handler;
71 TargetAsmParser &getTargetParser() const { return *TargetParser; }
72 void setTargetParser(TargetAsmParser &P) { TargetParser = &P; }
74 /// @name MCAsmParser Interface
77 virtual MCAsmLexer &getLexer() { return Lexer; }
78 virtual MCContext &getContext() { return Ctx; }
79 virtual MCStreamer &getStreamer() { return Out; }
81 virtual void Warning(SMLoc L, const Twine &Meg);
82 virtual bool Error(SMLoc L, const Twine &Msg);
84 const AsmToken &Lex();
86 bool ParseExpression(const MCExpr *&Res);
87 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
88 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
89 virtual bool ParseAbsoluteExpression(int64_t &Res);
94 MCSymbol *CreateSymbol(StringRef Name);
96 bool ParseStatement();
98 bool TokError(const char *Msg);
100 void PrintMessage(SMLoc Loc, const std::string &Msg, const char *Type) const;
102 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
103 bool EnterIncludeFile(const std::string &Filename);
105 void EatToEndOfStatement();
107 bool ParseAssignment(const StringRef &Name);
109 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
110 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
111 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
113 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
114 /// and set \arg Res to the identifier contents.
115 bool ParseIdentifier(StringRef &Res);
117 // Directive Parsing.
118 bool ParseDirectiveDarwinSection(); // Darwin specific ".section".
119 bool ParseDirectiveSectionSwitch(const char *Segment, const char *Section,
120 unsigned TAA = 0, unsigned ImplicitAlign = 0,
121 unsigned StubSize = 0);
122 bool ParseDirectiveAscii(bool ZeroTerminated); // ".ascii", ".asciiz"
123 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
124 bool ParseDirectiveFill(); // ".fill"
125 bool ParseDirectiveSpace(); // ".space"
126 bool ParseDirectiveSet(); // ".set"
127 bool ParseDirectiveOrg(); // ".org"
128 // ".align{,32}", ".p2align{,w,l}"
129 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
131 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
132 /// accepts a single symbol (which should be a label or an external).
133 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
134 bool ParseDirectiveELFType(); // ELF specific ".type"
135 bool ParseDirectiveDarwinSymbolDesc(); // Darwin specific ".desc"
136 bool ParseDirectiveDarwinLsym(); // Darwin specific ".lsym"
138 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
139 bool ParseDirectiveDarwinZerofill(); // Darwin specific ".zerofill"
140 bool ParseDirectiveDarwinTBSS(); // Darwin specific ".tbss"
142 // Darwin specific ".subsections_via_symbols"
143 bool ParseDirectiveDarwinSubsectionsViaSymbols();
144 // Darwin specific .dump and .load
145 bool ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump);
146 // Darwin specific .secure_log_unique
147 bool ParseDirectiveDarwinSecureLogUnique(SMLoc IDLoc);
148 // Darwin specific .secure_log_reset
149 bool ParseDirectiveDarwinSecureLogReset(SMLoc IDLoc);
151 bool ParseDirectiveAbort(); // ".abort"
152 bool ParseDirectiveInclude(); // ".include"
154 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
155 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
156 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
157 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
159 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc); // ".file"
160 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc); // ".line"
161 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc); // ".loc"
163 /// ParseEscapedString - Parse the current token as a string which may include
164 /// escaped characters and return the string contents.
165 bool ParseEscapedString(std::string &Data);
168 } // end namespace llvm