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