llvm-mc: Switch MCExpr construction to using static member functions, and taking...
[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 void Warning(SMLoc L, const Twine &Meg);
66
67   virtual bool Error(SMLoc L, const Twine &Msg);
68
69   virtual bool ParseExpression(const MCExpr *&Res);
70
71   virtual bool ParseAbsoluteExpression(int64_t &Res);
72
73   virtual bool ParseRelocatableExpression(MCValue &Res);
74
75   /// }
76
77 private:
78   MCSymbol *CreateSymbol(StringRef Name);
79
80   // FIXME: See comment on SectionUniquingMap.
81   const MCSection *getMachOSection(const StringRef &Segment,
82                                    const StringRef &Section,
83                                    unsigned TypeAndAttributes,
84                                    unsigned Reserved2,
85                                    SectionKind Kind) const;
86
87   bool ParseStatement();
88
89   bool TokError(const char *Msg);
90   
91   bool ParseConditionalAssemblyDirectives(StringRef Directive,
92                                           SMLoc DirectiveLoc);
93   void EatToEndOfStatement();
94   
95   bool ParseAssignment(const StringRef &Name, bool IsDotSet);
96
97   /// ParseParenRelocatableExpression - Parse an expression which must be
98   /// relocatable, assuming that an initial '(' has already been consumed.
99   ///
100   /// @param Res - The relocatable expression value. The result is undefined on
101   /// error.  
102   /// @result - False on success.
103   ///
104   /// @see ParseRelocatableExpression, ParseParenExpr.
105   bool ParseParenRelocatableExpression(MCValue &Res);
106
107   bool ParsePrimaryExpr(const MCExpr *&Res);
108   bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res);
109   bool ParseParenExpr(const MCExpr *&Res);
110
111   /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
112   /// and set \arg Res to the identifier contents.
113   bool ParseIdentifier(StringRef &Res);
114   
115   // Directive Parsing.
116   bool ParseDirectiveDarwinSection(); // Darwin specific ".section".
117   bool ParseDirectiveSectionSwitch(const char *Segment, const char *Section,
118                                    unsigned TAA = 0, unsigned ImplicitAlign = 0,
119                                    unsigned StubSize = 0);
120   bool ParseDirectiveAscii(bool ZeroTerminated); // ".ascii", ".asciiz"
121   bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
122   bool ParseDirectiveFill(); // ".fill"
123   bool ParseDirectiveSpace(); // ".space"
124   bool ParseDirectiveSet(); // ".set"
125   bool ParseDirectiveOrg(); // ".org"
126   // ".align{,32}", ".p2align{,w,l}"
127   bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
128
129   /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
130   /// accepts a single symbol (which should be a label or an external).
131   bool ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr);
132   bool ParseDirectiveDarwinSymbolDesc(); // Darwin specific ".desc"
133   bool ParseDirectiveDarwinLsym(); // Darwin specific ".lsym"
134
135   bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
136   bool ParseDirectiveDarwinZerofill(); // Darwin specific ".zerofill"
137
138   // Darwin specific ".subsections_via_symbols"
139   bool ParseDirectiveDarwinSubsectionsViaSymbols();
140   // Darwin specific .dump and .load
141   bool ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump);
142
143   bool ParseDirectiveAbort(); // ".abort"
144   bool ParseDirectiveInclude(); // ".include"
145
146   bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
147   bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
148   bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
149   bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
150
151   bool ParseDirectiveFile(SMLoc DirectiveLoc); // ".file"
152   bool ParseDirectiveLine(SMLoc DirectiveLoc); // ".line"
153   bool ParseDirectiveLoc(SMLoc DirectiveLoc); // ".loc"
154
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);
158 };
159
160 } // end namespace llvm
161
162 #endif