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"
36 class TargetAsmParser;
39 class AsmParser : public MCAsmParser {
45 TargetAsmParser *TargetParser;
47 /// This is the current buffer index we're lexing from as managed by the
52 std::vector<AsmCond> TheCondStack;
54 // FIXME: Figure out where this should leave, the code is a copy of that which
55 // is also used by TargetLoweringObjectFile.
56 mutable void *SectionUniquingMap;
58 /// DirectiveMap - This is a table handlers for directives. Each handler is
59 /// invoked after the directive identifier is read and is responsible for
60 /// parsing and validating the rest of the directive. The handler is passed
61 /// in the directive name and the location of the directive keyword.
62 StringMap<bool(AsmParser::*)(StringRef, SMLoc)> DirectiveMap;
64 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
65 const MCAsmInfo &MAI);
71 void AddDirectiveHandler(StringRef Directive,
72 bool (AsmParser::*Handler)(StringRef, SMLoc)) {
73 DirectiveMap[Directive] = Handler;
76 TargetAsmParser &getTargetParser() const { return *TargetParser; }
77 void setTargetParser(TargetAsmParser &P) { TargetParser = &P; }
79 /// @name MCAsmParser Interface
82 virtual MCAsmLexer &getLexer() { return Lexer; }
83 virtual MCContext &getContext() { return Ctx; }
84 virtual MCStreamer &getStreamer() { return Out; }
86 virtual void Warning(SMLoc L, const Twine &Meg);
87 virtual bool Error(SMLoc L, const Twine &Msg);
89 const AsmToken &Lex();
91 bool ParseExpression(const MCExpr *&Res);
92 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
93 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
94 virtual bool ParseAbsoluteExpression(int64_t &Res);
99 MCSymbol *CreateSymbol(StringRef Name);
101 // FIXME: See comment on SectionUniquingMap.
102 const MCSection *getMachOSection(const StringRef &Segment,
103 const StringRef &Section,
104 unsigned TypeAndAttributes,
106 SectionKind Kind) const;
108 bool ParseStatement();
110 bool TokError(const char *Msg);
112 void PrintMessage(SMLoc Loc, const std::string &Msg, const char *Type) const;
114 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
115 bool EnterIncludeFile(const std::string &Filename);
117 bool ParseConditionalAssemblyDirectives(StringRef Directive,
119 void EatToEndOfStatement();
121 bool ParseAssignment(const StringRef &Name);
123 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
124 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
125 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
127 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
128 /// and set \arg Res to the identifier contents.
129 bool ParseIdentifier(StringRef &Res);
131 // Directive Parsing.
132 bool ParseDirectiveDarwinSection(); // Darwin specific ".section".
133 bool ParseDirectiveSectionSwitch(const char *Segment, const char *Section,
134 unsigned TAA = 0, unsigned ImplicitAlign = 0,
135 unsigned StubSize = 0);
136 bool ParseDirectiveAscii(bool ZeroTerminated); // ".ascii", ".asciiz"
137 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
138 bool ParseDirectiveFill(); // ".fill"
139 bool ParseDirectiveSpace(); // ".space"
140 bool ParseDirectiveSet(); // ".set"
141 bool ParseDirectiveOrg(); // ".org"
142 // ".align{,32}", ".p2align{,w,l}"
143 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
145 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
146 /// accepts a single symbol (which should be a label or an external).
147 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
148 bool ParseDirectiveDarwinSymbolDesc(); // Darwin specific ".desc"
149 bool ParseDirectiveDarwinLsym(); // Darwin specific ".lsym"
151 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
152 bool ParseDirectiveDarwinZerofill(); // Darwin specific ".zerofill"
154 // Darwin specific ".subsections_via_symbols"
155 bool ParseDirectiveDarwinSubsectionsViaSymbols();
156 // Darwin specific .dump and .load
157 bool ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump);
159 bool ParseDirectiveAbort(); // ".abort"
160 bool ParseDirectiveInclude(); // ".include"
162 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
163 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
164 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
165 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
167 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc); // ".file"
168 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc); // ".line"
169 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc); // ".loc"
171 /// ParseEscapedString - Parse the current token as a string which may include
172 /// escaped characters and return the string contents.
173 bool ParseEscapedString(std::string &Data);
176 } // end namespace llvm