99b48bb8a445528d30ba0e750a2df9ef634edeb8
[oota-llvm.git] / include / llvm / MC / MCParser / AsmParser.h
1 //===- AsmParser.h - Parser for Assembly Files ------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This class declares the parser for assembly files.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef ASMPARSER_H
15 #define ASMPARSER_H
16
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"
24 #include <vector>
25
26 namespace llvm {
27 class AsmCond;
28 class AsmToken;
29 class MCContext;
30 class MCExpr;
31 class MCInst;
32 class MCStreamer;
33 class MCAsmInfo;
34 class SourceMgr;
35 class TargetAsmParser;
36 class Twine;
37
38 class AsmParser : public MCAsmParser {
39 private:
40   AsmLexer Lexer;
41   MCContext &Ctx;
42   MCStreamer &Out;
43   SourceMgr &SrcMgr;
44   TargetAsmParser *TargetParser;
45   
46   /// This is the current buffer index we're lexing from as managed by the
47   /// SourceMgr object.
48   int CurBuffer;
49
50   AsmCond TheCondState;
51   std::vector<AsmCond> TheCondStack;
52
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;
58 public:
59   AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
60             const MCAsmInfo &MAI);
61   ~AsmParser();
62
63   bool Run(bool NoInitialTextSection, bool NoFinalize = false);
64
65   
66   void AddDirectiveHandler(StringRef Directive,
67                            bool (AsmParser::*Handler)(StringRef, SMLoc)) {
68     DirectiveMap[Directive] = Handler;
69   }
70 public:
71   TargetAsmParser &getTargetParser() const { return *TargetParser; }
72   void setTargetParser(TargetAsmParser &P) { TargetParser = &P; }
73
74   /// @name MCAsmParser Interface
75   /// {
76
77   virtual MCAsmLexer &getLexer() { return Lexer; }
78   virtual MCContext &getContext() { return Ctx; }
79   virtual MCStreamer &getStreamer() { return Out; }
80
81   virtual void Warning(SMLoc L, const Twine &Meg);
82   virtual bool Error(SMLoc L, const Twine &Msg);
83
84   const AsmToken &Lex();
85
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);
90
91   /// }
92
93 private:
94   MCSymbol *CreateSymbol(StringRef Name);
95
96   bool ParseStatement();
97
98   bool TokError(const char *Msg);
99   
100   void PrintMessage(SMLoc Loc, const std::string &Msg, const char *Type) const;
101     
102   /// EnterIncludeFile - Enter the specified file. This returns true on failure.
103   bool EnterIncludeFile(const std::string &Filename);
104   
105   void EatToEndOfStatement();
106   
107   bool ParseAssignment(const StringRef &Name);
108
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);
112
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);
116   
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);
130
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"
137
138   bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
139   bool ParseDirectiveDarwinZerofill(); // Darwin specific ".zerofill"
140   bool ParseDirectiveDarwinTBSS(); // Darwin specific ".tbss"
141
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);
150
151   bool ParseDirectiveAbort(); // ".abort"
152   bool ParseDirectiveInclude(); // ".include"
153
154   bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
155   bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
156   bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
157   bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
158
159   bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc); // ".file"
160   bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc); // ".line"
161   bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc); // ".loc"
162
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);
166 };
167
168 } // end namespace llvm
169
170 #endif