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 //===----------------------------------------------------------------------===//
20 #include "llvm/MC/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"
34 class TargetAsmParser;
37 class AsmParser : public MCAsmParser {
42 TargetAsmParser *TargetParser;
45 std::vector<AsmCond> TheCondStack;
47 // FIXME: Figure out where this should leave, the code is a copy of that which
48 // is also used by TargetLoweringObjectFile.
49 mutable void *SectionUniquingMap;
51 /// DirectiveMap - This is a table handlers for directives. Each handler is
52 /// invoked after the directive identifier is read and is responsible for
53 /// parsing and validating the rest of the directive. The handler is passed
54 /// in the directive name and the location of the directive keyword.
55 StringMap<bool(AsmParser::*)(StringRef, SMLoc)> DirectiveMap;
57 AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
58 const MCAsmInfo &_MAI);
64 void AddDirectiveHandler(StringRef Directive,
65 bool (AsmParser::*Handler)(StringRef, SMLoc)) {
66 DirectiveMap[Directive] = Handler;
69 TargetAsmParser &getTargetParser() const { return *TargetParser; }
70 void setTargetParser(TargetAsmParser &P) { TargetParser = &P; }
72 /// @name MCAsmParser Interface
75 virtual MCAsmLexer &getLexer() { return Lexer; }
76 virtual MCContext &getContext() { return Ctx; }
77 virtual MCStreamer &getStreamer() { return Out; }
79 virtual void Warning(SMLoc L, const Twine &Meg);
80 virtual bool Error(SMLoc L, const Twine &Msg);
82 virtual bool ParseExpression(const MCExpr *&Res);
83 virtual bool ParseParenExpression(const MCExpr *&Res);
84 virtual bool ParseAbsoluteExpression(int64_t &Res);
89 MCSymbol *CreateSymbol(StringRef Name);
91 // FIXME: See comment on SectionUniquingMap.
92 const MCSection *getMachOSection(const StringRef &Segment,
93 const StringRef &Section,
94 unsigned TypeAndAttributes,
96 SectionKind Kind) const;
98 bool ParseStatement();
100 bool TokError(const char *Msg);
102 bool ParseConditionalAssemblyDirectives(StringRef Directive,
104 void EatToEndOfStatement();
106 bool ParseAssignment(const StringRef &Name);
108 bool ParsePrimaryExpr(const MCExpr *&Res);
109 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res);
110 bool ParseParenExpr(const MCExpr *&Res);
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 ParseDirectiveDarwinSection(); // Darwin specific ".section".
118 bool ParseDirectiveSectionSwitch(const char *Segment, const char *Section,
119 unsigned TAA = 0, unsigned ImplicitAlign = 0,
120 unsigned StubSize = 0);
121 bool ParseDirectiveAscii(bool ZeroTerminated); // ".ascii", ".asciiz"
122 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
123 bool ParseDirectiveFill(); // ".fill"
124 bool ParseDirectiveSpace(); // ".space"
125 bool ParseDirectiveSet(); // ".set"
126 bool ParseDirectiveOrg(); // ".org"
127 // ".align{,32}", ".p2align{,w,l}"
128 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
130 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
131 /// accepts a single symbol (which should be a label or an external).
132 bool ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr);
133 bool ParseDirectiveDarwinSymbolDesc(); // Darwin specific ".desc"
134 bool ParseDirectiveDarwinLsym(); // Darwin specific ".lsym"
136 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
137 bool ParseDirectiveDarwinZerofill(); // Darwin specific ".zerofill"
139 // Darwin specific ".subsections_via_symbols"
140 bool ParseDirectiveDarwinSubsectionsViaSymbols();
141 // Darwin specific .dump and .load
142 bool ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump);
144 bool ParseDirectiveAbort(); // ".abort"
145 bool ParseDirectiveInclude(); // ".include"
147 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
148 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
149 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
150 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
152 bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc); // ".file"
153 bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc); // ".line"
154 bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc); // ".loc"
156 /// ParseEscapedString - Parse the current token as a string which may include
157 /// escaped characters and return the string contents.
158 bool ParseEscapedString(std::string &Data);
161 } // end namespace llvm