llvm-mc: Simplify EmitAssignment ('.set' is identical 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
24 namespace llvm {
25 class AsmCond;
26 class MCContext;
27 class MCExpr;
28 class MCInst;
29 class MCStreamer;
30 class MCValue;
31 class TargetAsmParser;
32 class Twine;
33
34 class AsmParser : public MCAsmParser {
35 private:  
36   AsmLexer Lexer;
37   MCContext &Ctx;
38   MCStreamer &Out;
39   TargetAsmParser *TargetParser;
40
41   AsmCond TheCondState;
42   std::vector<AsmCond> TheCondStack;
43
44   // FIXME: Figure out where this should leave, the code is a copy of that which
45   // is also used by TargetLoweringObjectFile.
46   mutable void *SectionUniquingMap;
47
48 public:
49   AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out)
50     : Lexer(_SM), Ctx(_Ctx), Out(_Out), TargetParser(0),
51       SectionUniquingMap(0) {}
52   ~AsmParser();
53
54   bool Run();
55   
56 public:
57   TargetAsmParser &getTargetParser() const { return *TargetParser; }
58   void setTargetParser(TargetAsmParser &P) { TargetParser = &P; }
59
60   /// @name MCAsmParser Interface
61   /// {
62
63   virtual MCAsmLexer &getLexer() { return Lexer; }
64
65   virtual MCContext &getContext() { return Ctx; }
66
67   virtual void Warning(SMLoc L, const Twine &Meg);
68
69   virtual bool Error(SMLoc L, const Twine &Msg);
70
71   virtual bool ParseExpression(const MCExpr *&Res);
72
73   virtual bool ParseParenExpression(const MCExpr *&Res);
74
75   virtual bool ParseAbsoluteExpression(int64_t &Res);
76
77   /// }
78
79 private:
80   MCSymbol *CreateSymbol(StringRef Name);
81
82   // FIXME: See comment on SectionUniquingMap.
83   const MCSection *getMachOSection(const StringRef &Segment,
84                                    const StringRef &Section,
85                                    unsigned TypeAndAttributes,
86                                    unsigned Reserved2,
87                                    SectionKind Kind) const;
88
89   bool ParseStatement();
90
91   bool TokError(const char *Msg);
92   
93   bool ParseConditionalAssemblyDirectives(StringRef Directive,
94                                           SMLoc DirectiveLoc);
95   void EatToEndOfStatement();
96   
97   bool ParseAssignment(const StringRef &Name);
98
99   bool ParsePrimaryExpr(const MCExpr *&Res);
100   bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res);
101   bool ParseParenExpr(const MCExpr *&Res);
102
103   /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
104   /// and set \arg Res to the identifier contents.
105   bool ParseIdentifier(StringRef &Res);
106   
107   // Directive Parsing.
108   bool ParseDirectiveDarwinSection(); // Darwin specific ".section".
109   bool ParseDirectiveSectionSwitch(const char *Segment, const char *Section,
110                                    unsigned TAA = 0, unsigned ImplicitAlign = 0,
111                                    unsigned StubSize = 0);
112   bool ParseDirectiveAscii(bool ZeroTerminated); // ".ascii", ".asciiz"
113   bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
114   bool ParseDirectiveFill(); // ".fill"
115   bool ParseDirectiveSpace(); // ".space"
116   bool ParseDirectiveSet(); // ".set"
117   bool ParseDirectiveOrg(); // ".org"
118   // ".align{,32}", ".p2align{,w,l}"
119   bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
120
121   /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
122   /// accepts a single symbol (which should be a label or an external).
123   bool ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr);
124   bool ParseDirectiveDarwinSymbolDesc(); // Darwin specific ".desc"
125   bool ParseDirectiveDarwinLsym(); // Darwin specific ".lsym"
126
127   bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
128   bool ParseDirectiveDarwinZerofill(); // Darwin specific ".zerofill"
129
130   // Darwin specific ".subsections_via_symbols"
131   bool ParseDirectiveDarwinSubsectionsViaSymbols();
132   // Darwin specific .dump and .load
133   bool ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump);
134
135   bool ParseDirectiveAbort(); // ".abort"
136   bool ParseDirectiveInclude(); // ".include"
137
138   bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
139   bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
140   bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
141   bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
142
143   bool ParseDirectiveFile(SMLoc DirectiveLoc); // ".file"
144   bool ParseDirectiveLine(SMLoc DirectiveLoc); // ".line"
145   bool ParseDirectiveLoc(SMLoc DirectiveLoc); // ".loc"
146
147   /// ParseEscapedString - Parse the current token as a string which may include
148   /// escaped characters and return the string contents.
149   bool ParseEscapedString(std::string &Data);
150 };
151
152 } // end namespace llvm
153
154 #endif