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 //===----------------------------------------------------------------------===//
18 #include "llvm/MC/MCParser/AsmLexer.h"
19 #include "llvm/MC/MCParser/AsmCond.h"
20 #include "llvm/MC/MCParser/MCAsmParser.h"
21 #include "llvm/MC/MCSectionMachO.h"
22 #include "llvm/MC/MCStreamer.h"
23 #include "llvm/MC/MCAsmInfo.h"
24 #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 // FIXME: Figure out where this should leave, the code is a copy of that which
54 // is also used by TargetLoweringObjectFile.
55 mutable void *SectionUniquingMap;
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<bool(AsmParser::*)(StringRef, SMLoc)> DirectiveMap;
63 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
64 const MCAsmInfo &MAI);
70 void AddDirectiveHandler(StringRef Directive,
71 bool (AsmParser::*Handler)(StringRef, SMLoc)) {
72 DirectiveMap[Directive] = Handler;
75 TargetAsmParser &getTargetParser() const { return *TargetParser; }
76 void setTargetParser(TargetAsmParser &P) { TargetParser = &P; }
78 /// @name MCAsmParser Interface
81 virtual MCAsmLexer &getLexer() { return Lexer; }
82 virtual MCContext &getContext() { return Ctx; }
83 virtual MCStreamer &getStreamer() { return Out; }
85 virtual void Warning(SMLoc L, const Twine &Meg);
86 virtual bool Error(SMLoc L, const Twine &Msg);
88 const AsmToken &Lex();
90 bool ParseExpression(const MCExpr *&Res);
91 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
92 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
93 virtual bool ParseAbsoluteExpression(int64_t &Res);
98 MCSymbol *CreateSymbol(StringRef Name);
100 // FIXME: See comment on SectionUniquingMap.
101 const MCSection *getMachOSection(const StringRef &Segment,
102 const StringRef &Section,
103 unsigned TypeAndAttributes,
105 SectionKind Kind) const;
107 bool ParseStatement();
109 bool TokError(const char *Msg);
111 void PrintMessage(SMLoc Loc, const std::string &Msg, const char *Type) const;
113 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
114 bool EnterIncludeFile(const std::string &Filename);
116 bool ParseConditionalAssemblyDirectives(StringRef Directive,
118 void EatToEndOfStatement();
120 bool ParseAssignment(const StringRef &Name);
122 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
123 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
124 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
126 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
127 /// and set \arg Res to the identifier contents.
128 bool ParseIdentifier(StringRef &Res);
130 // Directive Parsing.
131 bool ParseDirectiveDarwinSection(); // Darwin specific ".section".
132 bool ParseDirectiveSectionSwitch(const char *Segment, const char *Section,
133 unsigned TAA = 0, unsigned ImplicitAlign = 0,
134 unsigned StubSize = 0);
135 bool ParseDirectiveAscii(bool ZeroTerminated); // ".ascii", ".asciiz"
136 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
137 bool ParseDirectiveFill(); // ".fill"
138 bool ParseDirectiveSpace(); // ".space"
139 bool ParseDirectiveSet(); // ".set"
140 bool ParseDirectiveOrg(); // ".org"
141 // ".align{,32}", ".p2align{,w,l}"
142 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
144 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
145 /// accepts a single symbol (which should be a label or an external).
146 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
147 bool ParseDirectiveDarwinSymbolDesc(); // Darwin specific ".desc"
148 bool ParseDirectiveDarwinLsym(); // Darwin specific ".lsym"
150 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
151 bool ParseDirectiveDarwinZerofill(); // Darwin specific ".zerofill"
153 // Darwin specific ".subsections_via_symbols"
154 bool ParseDirectiveDarwinSubsectionsViaSymbols();
155 // Darwin specific .dump and .load
156 bool ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump);
158 bool ParseDirectiveAbort(); // ".abort"
159 bool ParseDirectiveInclude(); // ".include"
161 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
162 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
163 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
164 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
166 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc); // ".file"
167 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc); // ".line"
168 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc); // ".loc"
170 /// ParseEscapedString - Parse the current token as a string which may include
171 /// escaped characters and return the string contents.
172 bool ParseEscapedString(std::string &Data);
175 } // end namespace llvm