fix an overly conservative caching issue that caused memdep to
[oota-llvm.git] / tools / llvm-mc / 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 "AsmLexer.h"
19 #include "AsmCond.h"
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"
25
26 namespace llvm {
27 class AsmCond;
28 class MCContext;
29 class MCExpr;
30 class MCInst;
31 class MCStreamer;
32 class MCAsmInfo;
33 class MCValue;
34 class TargetAsmParser;
35 class Twine;
36
37 class AsmParser : public MCAsmParser {
38 private:  
39   AsmLexer Lexer;
40   MCContext &Ctx;
41   MCStreamer &Out;
42   TargetAsmParser *TargetParser;
43
44   AsmCond TheCondState;
45   std::vector<AsmCond> TheCondStack;
46
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;
50
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;
56 public:
57   AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
58             const MCAsmInfo &_MAI);
59   ~AsmParser();
60
61   bool Run();
62
63   
64   void AddDirectiveHandler(StringRef Directive,
65                            bool (AsmParser::*Handler)(StringRef, SMLoc)) {
66     DirectiveMap[Directive] = Handler;
67   }
68 public:
69   TargetAsmParser &getTargetParser() const { return *TargetParser; }
70   void setTargetParser(TargetAsmParser &P) { TargetParser = &P; }
71
72   /// @name MCAsmParser Interface
73   /// {
74
75   virtual MCAsmLexer &getLexer() { return Lexer; }
76   virtual MCContext &getContext() { return Ctx; }
77   virtual MCStreamer &getStreamer() { return Out; }
78
79   virtual void Warning(SMLoc L, const Twine &Meg);
80   virtual bool Error(SMLoc L, const Twine &Msg);
81
82   virtual bool ParseExpression(const MCExpr *&Res);
83   virtual bool ParseParenExpression(const MCExpr *&Res);
84   virtual bool ParseAbsoluteExpression(int64_t &Res);
85
86   /// }
87
88 private:
89   MCSymbol *CreateSymbol(StringRef Name);
90
91   // FIXME: See comment on SectionUniquingMap.
92   const MCSection *getMachOSection(const StringRef &Segment,
93                                    const StringRef &Section,
94                                    unsigned TypeAndAttributes,
95                                    unsigned Reserved2,
96                                    SectionKind Kind) const;
97
98   bool ParseStatement();
99
100   bool TokError(const char *Msg);
101   
102   bool ParseConditionalAssemblyDirectives(StringRef Directive,
103                                           SMLoc DirectiveLoc);
104   void EatToEndOfStatement();
105   
106   bool ParseAssignment(const StringRef &Name);
107
108   bool ParsePrimaryExpr(const MCExpr *&Res);
109   bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res);
110   bool ParseParenExpr(const MCExpr *&Res);
111
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);
115   
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);
129
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"
135
136   bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
137   bool ParseDirectiveDarwinZerofill(); // Darwin specific ".zerofill"
138
139   // Darwin specific ".subsections_via_symbols"
140   bool ParseDirectiveDarwinSubsectionsViaSymbols();
141   // Darwin specific .dump and .load
142   bool ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump);
143
144   bool ParseDirectiveAbort(); // ".abort"
145   bool ParseDirectiveInclude(); // ".include"
146
147   bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
148   bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
149   bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
150   bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
151
152   bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc); // ".file"
153   bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc); // ".line"
154   bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc); // ".loc"
155
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);
159 };
160
161 } // end namespace llvm
162
163 #endif