Give AsmParser an option to control whether it finalizes
[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 <vector>
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"
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   // 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;
56
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;
62 public:
63   AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
64             const MCAsmInfo &MAI);
65   ~AsmParser();
66
67   bool Run(bool NoInitialTextSection, bool NoFinalize = false);
68
69   
70   void AddDirectiveHandler(StringRef Directive,
71                            bool (AsmParser::*Handler)(StringRef, SMLoc)) {
72     DirectiveMap[Directive] = Handler;
73   }
74 public:
75   TargetAsmParser &getTargetParser() const { return *TargetParser; }
76   void setTargetParser(TargetAsmParser &P) { TargetParser = &P; }
77
78   /// @name MCAsmParser Interface
79   /// {
80
81   virtual MCAsmLexer &getLexer() { return Lexer; }
82   virtual MCContext &getContext() { return Ctx; }
83   virtual MCStreamer &getStreamer() { return Out; }
84
85   virtual void Warning(SMLoc L, const Twine &Meg);
86   virtual bool Error(SMLoc L, const Twine &Msg);
87
88   const AsmToken &Lex();
89
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);
94
95   /// }
96
97 private:
98   MCSymbol *CreateSymbol(StringRef Name);
99
100   // FIXME: See comment on SectionUniquingMap.
101   const MCSection *getMachOSection(const StringRef &Segment,
102                                    const StringRef &Section,
103                                    unsigned TypeAndAttributes,
104                                    unsigned Reserved2,
105                                    SectionKind Kind) const;
106
107   bool ParseStatement();
108
109   bool TokError(const char *Msg);
110   
111   void PrintMessage(SMLoc Loc, const std::string &Msg, const char *Type) const;
112     
113   /// EnterIncludeFile - Enter the specified file. This returns true on failure.
114   bool EnterIncludeFile(const std::string &Filename);
115   
116   bool ParseConditionalAssemblyDirectives(StringRef Directive,
117                                           SMLoc DirectiveLoc);
118   void EatToEndOfStatement();
119   
120   bool ParseAssignment(const StringRef &Name);
121
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);
125
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);
129   
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);
143
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"
149
150   bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
151   bool ParseDirectiveDarwinZerofill(); // Darwin specific ".zerofill"
152
153   // Darwin specific ".subsections_via_symbols"
154   bool ParseDirectiveDarwinSubsectionsViaSymbols();
155   // Darwin specific .dump and .load
156   bool ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump);
157
158   bool ParseDirectiveAbort(); // ".abort"
159   bool ParseDirectiveInclude(); // ".include"
160
161   bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
162   bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
163   bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
164   bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
165
166   bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc); // ".file"
167   bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc); // ".line"
168   bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc); // ".loc"
169
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);
173 };
174
175 } // end namespace llvm
176
177 #endif