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;
49 TargetAsmParser *TargetParser;
51 /// This is the current buffer index we're lexing from as managed by the
56 std::vector<AsmCond> TheCondStack;
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<std::pair<MCAsmParserExtension*, DirectiveHandler> > DirectiveMap;
64 AsmParser(const Target &T, SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
65 const MCAsmInfo &MAI);
68 bool Run(bool NoInitialTextSection, bool NoFinalize = false);
70 void AddDirectiveHandler(MCAsmParserExtension *Object,
72 DirectiveHandler Handler) {
73 DirectiveMap[Directive] = std::make_pair(Object, Handler);
77 TargetAsmParser &getTargetParser() const { return *TargetParser; }
78 void setTargetParser(TargetAsmParser &P);
80 /// @name MCAsmParser Interface
83 virtual SourceMgr &getSourceManager() { return SrcMgr; }
84 virtual MCAsmLexer &getLexer() { return Lexer; }
85 virtual MCContext &getContext() { return Ctx; }
86 virtual MCStreamer &getStreamer() { return Out; }
88 virtual void Warning(SMLoc L, const Twine &Meg);
89 virtual bool Error(SMLoc L, const Twine &Msg);
91 const AsmToken &Lex();
93 bool ParseExpression(const MCExpr *&Res);
94 virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
95 virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
96 virtual bool ParseAbsoluteExpression(int64_t &Res);
101 MCSymbol *CreateSymbol(StringRef Name);
103 bool ParseStatement();
105 void PrintMessage(SMLoc Loc, const std::string &Msg, const char *Type) const;
107 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
108 bool EnterIncludeFile(const std::string &Filename);
110 void EatToEndOfStatement();
112 bool ParseAssignment(const StringRef &Name);
114 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
115 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
116 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
118 /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
119 /// and set \arg Res to the identifier contents.
120 bool ParseIdentifier(StringRef &Res);
122 // Directive Parsing.
123 bool ParseDirectiveDarwinSection(); // Darwin specific ".section".
124 bool ParseDirectiveSectionSwitch(const char *Segment, const char *Section,
125 unsigned TAA = 0, unsigned ImplicitAlign = 0,
126 unsigned StubSize = 0);
127 bool ParseDirectiveAscii(bool ZeroTerminated); // ".ascii", ".asciiz"
128 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
129 bool ParseDirectiveFill(); // ".fill"
130 bool ParseDirectiveSpace(); // ".space"
131 bool ParseDirectiveSet(); // ".set"
132 bool ParseDirectiveOrg(); // ".org"
133 // ".align{,32}", ".p2align{,w,l}"
134 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
136 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
137 /// accepts a single symbol (which should be a label or an external).
138 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
139 bool ParseDirectiveELFType(); // ELF specific ".type"
140 bool ParseDirectiveDarwinSymbolDesc(); // Darwin specific ".desc"
141 bool ParseDirectiveDarwinLsym(); // Darwin specific ".lsym"
143 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
144 bool ParseDirectiveDarwinZerofill(); // Darwin specific ".zerofill"
145 bool ParseDirectiveDarwinTBSS(); // Darwin specific ".tbss"
147 bool ParseDirectiveAbort(); // ".abort"
148 bool ParseDirectiveInclude(); // ".include"
150 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
151 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
152 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
153 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
155 /// ParseEscapedString - Parse the current token as a string which may include
156 /// escaped characters and return the string contents.
157 bool ParseEscapedString(std::string &Data);
160 } // end namespace llvm