43cb596a6686737a7d7e93abbb2b5f28b4434e6b
[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   TargetAsmParser *TargetParser;
50   
51   /// This is the current buffer index we're lexing from as managed by the
52   /// SourceMgr object.
53   int CurBuffer;
54
55   AsmCond TheCondState;
56   std::vector<AsmCond> TheCondStack;
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<std::pair<MCAsmParserExtension*, DirectiveHandler> > DirectiveMap;
63 public:
64   AsmParser(const Target &T, SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
65             const MCAsmInfo &MAI);
66   ~AsmParser();
67
68   bool Run(bool NoInitialTextSection, bool NoFinalize = false);
69
70   void AddDirectiveHandler(MCAsmParserExtension *Object,
71                            StringRef Directive,
72                            DirectiveHandler Handler) {
73     DirectiveMap[Directive] = std::make_pair(Object, Handler);
74   }
75
76 public:
77   TargetAsmParser &getTargetParser() const { return *TargetParser; }
78   void setTargetParser(TargetAsmParser &P);
79
80   /// @name MCAsmParser Interface
81   /// {
82
83   virtual SourceMgr &getSourceManager() { return SrcMgr; }
84   virtual MCAsmLexer &getLexer() { return Lexer; }
85   virtual MCContext &getContext() { return Ctx; }
86   virtual MCStreamer &getStreamer() { return Out; }
87
88   virtual void Warning(SMLoc L, const Twine &Meg);
89   virtual bool Error(SMLoc L, const Twine &Msg);
90
91   const AsmToken &Lex();
92
93   bool ParseExpression(const MCExpr *&Res);
94   virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc);
95   virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc);
96   virtual bool ParseAbsoluteExpression(int64_t &Res);
97
98   /// }
99
100 private:
101   MCSymbol *CreateSymbol(StringRef Name);
102
103   bool ParseStatement();
104   
105   void PrintMessage(SMLoc Loc, const std::string &Msg, const char *Type) const;
106     
107   /// EnterIncludeFile - Enter the specified file. This returns true on failure.
108   bool EnterIncludeFile(const std::string &Filename);
109   
110   void EatToEndOfStatement();
111   
112   bool ParseAssignment(const StringRef &Name);
113
114   bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
115   bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
116   bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
117
118   /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
119   /// and set \arg Res to the identifier contents.
120   bool ParseIdentifier(StringRef &Res);
121   
122   // Directive Parsing.
123   bool ParseDirectiveDarwinSection(); // Darwin specific ".section".
124   bool ParseDirectiveSectionSwitch(const char *Segment, const char *Section,
125                                    unsigned TAA = 0, unsigned ImplicitAlign = 0,
126                                    unsigned StubSize = 0);
127   bool ParseDirectiveAscii(bool ZeroTerminated); // ".ascii", ".asciiz"
128   bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ...
129   bool ParseDirectiveFill(); // ".fill"
130   bool ParseDirectiveSpace(); // ".space"
131   bool ParseDirectiveSet(); // ".set"
132   bool ParseDirectiveOrg(); // ".org"
133   // ".align{,32}", ".p2align{,w,l}"
134   bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize);
135
136   /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
137   /// accepts a single symbol (which should be a label or an external).
138   bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr);
139   bool ParseDirectiveELFType(); // ELF specific ".type"
140
141   bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
142   bool ParseDirectiveDarwinZerofill(); // Darwin specific ".zerofill"
143   bool ParseDirectiveDarwinTBSS(); // Darwin specific ".tbss"
144
145   bool ParseDirectiveAbort(); // ".abort"
146   bool ParseDirectiveInclude(); // ".include"
147
148   bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
149   bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif"
150   bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else"
151   bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif
152
153   /// ParseEscapedString - Parse the current token as a string which may include
154   /// escaped characters and return the string contents.
155   bool ParseEscapedString(std::string &Data);
156 };
157
158 } // end namespace llvm
159
160 #endif