MC: Move several clients to using AsmParser constructor function.
[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 "llvm/MC/MCParser/AsmLexer.h"
18 #include "llvm/MC/MCParser/AsmCond.h"
19 #include "llvm/MC/MCParser/MCAsmParser.h"
20 #include "llvm/MC/MCSectionMachO.h"
21 #include "llvm/MC/MCStreamer.h"
22 #include "llvm/MC/MCAsmInfo.h"
23 #include "llvm/ADT/StringMap.h"
24 #include <vector>
25
26 namespace llvm {
27 class AsmCond;
28 class AsmToken;
29 class MCAsmParserExtension;
30 class MCContext;
31 class MCExpr;
32 class MCInst;
33 class MCStreamer;
34 class MCAsmInfo;
35 class SourceMgr;
36 class TargetAsmParser;
37 class Twine;
38
39 class AsmParser : public MCAsmParser {
40   AsmParser(const AsmParser &);   // DO NOT IMPLEMENT
41   void operator=(const AsmParser &);  // DO NOT IMPLEMENT
42 private:
43   AsmLexer Lexer;
44   MCContext &Ctx;
45   MCStreamer &Out;
46   SourceMgr &SrcMgr;
47   MCAsmParserExtension *GenericParser;
48   MCAsmParserExtension *PlatformParser;
49   
50   /// This is the current buffer index we're lexing from as managed by the
51   /// SourceMgr object.
52   int CurBuffer;
53
54   AsmCond TheCondState;
55   std::vector<AsmCond> TheCondStack;
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<std::pair<MCAsmParserExtension*, DirectiveHandler> > DirectiveMap;
62 public:
63   AsmParser(const Target &T, SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
64             const MCAsmInfo &MAI);
65   ~AsmParser();
66
67   virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false);
68
69   void AddDirectiveHandler(MCAsmParserExtension *Object,
70                            StringRef Directive,
71                            DirectiveHandler Handler) {
72     DirectiveMap[Directive] = std::make_pair(Object, Handler);
73   }
74
75 public:
76   /// @name MCAsmParser Interface
77   /// {
78
79   virtual SourceMgr &getSourceManager() { return SrcMgr; }
80   virtual MCAsmLexer &getLexer() { return Lexer; }
81   virtual MCContext &getContext() { return Ctx; }
82   virtual MCStreamer &getStreamer() { return Out; }
83
84   virtual void Warning(SMLoc L, const Twine &Meg);
85   virtual bool Error(SMLoc L, const Twine &Msg);
86
87   const AsmToken &Lex();
88
89   bool ParseExpression(const MCExpr *&Res);
90   virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
91   virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
92   virtual bool ParseAbsoluteExpression(int64_t &Res);
93
94   /// }
95
96 private:
97   bool ParseStatement();
98
99   void PrintMessage(SMLoc Loc, const std::string &Msg, const char *Type) const;
100     
101   /// EnterIncludeFile - Enter the specified file. This returns true on failure.
102   bool EnterIncludeFile(const std::string &Filename);
103   
104   void EatToEndOfStatement();
105   
106   bool ParseAssignment(StringRef Name);
107
108   bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
109   bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
110   bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
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 ParseDirectiveAscii(bool ZeroTerminated); // ".ascii", ".asciiz"
118   bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
119   bool ParseDirectiveFill(); // ".fill"
120   bool ParseDirectiveSpace(); // ".space"
121   bool ParseDirectiveSet(); // ".set"
122   bool ParseDirectiveOrg(); // ".org"
123   // ".align{,32}", ".p2align{,w,l}"
124   bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
125
126   /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
127   /// accepts a single symbol (which should be a label or an external).
128   bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
129   bool ParseDirectiveELFType(); // ELF specific ".type"
130
131   bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
132
133   bool ParseDirectiveAbort(); // ".abort"
134   bool ParseDirectiveInclude(); // ".include"
135
136   bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
137   bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
138   bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
139   bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
140
141   /// ParseEscapedString - Parse the current token as a string which may include
142   /// escaped characters and return the string contents.
143   bool ParseEscapedString(std::string &Data);
144 };
145
146 } // end namespace llvm
147
148 #endif