Chris Lattner | 27aa7d2 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 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 | |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 17 | #include <vector> |
Chris Lattner | 27aa7d2 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 18 | #include "AsmLexer.h" |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 19 | #include "AsmCond.h" |
Daniel Dunbar | a3af370 | 2009-07-20 18:55:04 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCAsmParser.h" |
Daniel Dunbar | 7c0a334 | 2009-08-26 22:49:51 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCSectionMachO.h" |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCStreamer.h" |
Kevin Enderby | 9823ca9 | 2009-09-04 21:45:34 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCAsmInfo.h" |
Chris Lattner | ebb89b4 | 2009-09-27 21:16:52 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/StringMap.h" |
Chris Lattner | 27aa7d2 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 25 | |
| 26 | namespace llvm { |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 27 | class AsmCond; |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 28 | class AsmToken; |
Chris Lattner | c69485e | 2009-06-24 04:31:49 +0000 | [diff] [blame] | 29 | class MCContext; |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 30 | class MCExpr; |
Chris Lattner | 29dfe7c | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 31 | class MCInst; |
Chris Lattner | cbc23f7 | 2009-06-24 00:52:40 +0000 | [diff] [blame] | 32 | class MCStreamer; |
Kevin Enderby | 9823ca9 | 2009-09-04 21:45:34 +0000 | [diff] [blame] | 33 | class MCAsmInfo; |
Daniel Dunbar | 15d1707 | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 34 | class MCValue; |
Sean Callanan | 10d33a4 | 2010-01-20 22:45:23 +0000 | [diff] [blame] | 35 | class SourceMgr; |
Daniel Dunbar | dbd692a | 2009-07-20 20:01:54 +0000 | [diff] [blame] | 36 | class TargetAsmParser; |
Daniel Dunbar | f9507ff | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 37 | class Twine; |
Daniel Dunbar | 15d1707 | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 38 | |
Daniel Dunbar | a2edbab | 2009-07-28 20:47:52 +0000 | [diff] [blame] | 39 | class AsmParser : public MCAsmParser { |
Sean Callanan | 10d33a4 | 2010-01-20 22:45:23 +0000 | [diff] [blame] | 40 | private: |
Chris Lattner | 27aa7d2 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 41 | AsmLexer Lexer; |
Chris Lattner | c69485e | 2009-06-24 04:31:49 +0000 | [diff] [blame] | 42 | MCContext &Ctx; |
Chris Lattner | cbc23f7 | 2009-06-24 00:52:40 +0000 | [diff] [blame] | 43 | MCStreamer &Out; |
Sean Callanan | 10d33a4 | 2010-01-20 22:45:23 +0000 | [diff] [blame] | 44 | SourceMgr &SrcMgr; |
Daniel Dunbar | a2edbab | 2009-07-28 20:47:52 +0000 | [diff] [blame] | 45 | TargetAsmParser *TargetParser; |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 46 | |
| 47 | AsmCond TheCondState; |
| 48 | std::vector<AsmCond> TheCondStack; |
| 49 | |
Daniel Dunbar | 7c0a334 | 2009-08-26 22:49:51 +0000 | [diff] [blame] | 50 | // FIXME: Figure out where this should leave, the code is a copy of that which |
| 51 | // is also used by TargetLoweringObjectFile. |
| 52 | mutable void *SectionUniquingMap; |
| 53 | |
Chris Lattner | ebb89b4 | 2009-09-27 21:16:52 +0000 | [diff] [blame] | 54 | /// DirectiveMap - This is a table handlers for directives. Each handler is |
| 55 | /// invoked after the directive identifier is read and is responsible for |
| 56 | /// parsing and validating the rest of the directive. The handler is passed |
| 57 | /// in the directive name and the location of the directive keyword. |
| 58 | StringMap<bool(AsmParser::*)(StringRef, SMLoc)> DirectiveMap; |
Chris Lattner | 27aa7d2 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 59 | public: |
Kevin Enderby | 9823ca9 | 2009-09-04 21:45:34 +0000 | [diff] [blame] | 60 | AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out, |
Chris Lattner | ebb89b4 | 2009-09-27 21:16:52 +0000 | [diff] [blame] | 61 | const MCAsmInfo &_MAI); |
Daniel Dunbar | 7c0a334 | 2009-08-26 22:49:51 +0000 | [diff] [blame] | 62 | ~AsmParser(); |
Daniel Dunbar | a2edbab | 2009-07-28 20:47:52 +0000 | [diff] [blame] | 63 | |
Chris Lattner | 27aa7d2 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 64 | bool Run(); |
Chris Lattner | ebb89b4 | 2009-09-27 21:16:52 +0000 | [diff] [blame] | 65 | |
Chris Lattner | 27aa7d2 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 66 | |
Chris Lattner | ebb89b4 | 2009-09-27 21:16:52 +0000 | [diff] [blame] | 67 | void AddDirectiveHandler(StringRef Directive, |
| 68 | bool (AsmParser::*Handler)(StringRef, SMLoc)) { |
| 69 | DirectiveMap[Directive] = Handler; |
| 70 | } |
Daniel Dunbar | a3af370 | 2009-07-20 18:55:04 +0000 | [diff] [blame] | 71 | public: |
Daniel Dunbar | a2edbab | 2009-07-28 20:47:52 +0000 | [diff] [blame] | 72 | TargetAsmParser &getTargetParser() const { return *TargetParser; } |
| 73 | void setTargetParser(TargetAsmParser &P) { TargetParser = &P; } |
Daniel Dunbar | a3af370 | 2009-07-20 18:55:04 +0000 | [diff] [blame] | 74 | |
Daniel Dunbar | e240beb | 2009-07-28 22:22:31 +0000 | [diff] [blame] | 75 | /// @name MCAsmParser Interface |
| 76 | /// { |
| 77 | |
Daniel Dunbar | dbd692a | 2009-07-20 20:01:54 +0000 | [diff] [blame] | 78 | virtual MCAsmLexer &getLexer() { return Lexer; } |
Daniel Dunbar | 6ce004d | 2009-08-31 08:07:44 +0000 | [diff] [blame] | 79 | virtual MCContext &getContext() { return Ctx; } |
Daniel Dunbar | b8b7052 | 2009-09-10 00:59:15 +0000 | [diff] [blame] | 80 | virtual MCStreamer &getStreamer() { return Out; } |
| 81 | |
Daniel Dunbar | e240beb | 2009-07-28 22:22:31 +0000 | [diff] [blame] | 82 | virtual void Warning(SMLoc L, const Twine &Meg); |
Daniel Dunbar | e240beb | 2009-07-28 22:22:31 +0000 | [diff] [blame] | 83 | virtual bool Error(SMLoc L, const Twine &Msg); |
| 84 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 85 | const AsmToken &Lex(); |
| 86 | |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 87 | bool ParseExpression(const MCExpr *&Res); |
Chris Lattner | 54482b4 | 2010-01-15 19:39:23 +0000 | [diff] [blame] | 88 | virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc); |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 89 | virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc); |
Daniel Dunbar | e240beb | 2009-07-28 22:22:31 +0000 | [diff] [blame] | 90 | virtual bool ParseAbsoluteExpression(int64_t &Res); |
| 91 | |
Daniel Dunbar | e240beb | 2009-07-28 22:22:31 +0000 | [diff] [blame] | 92 | /// } |
| 93 | |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 94 | private: |
Daniel Dunbar | 959fd88 | 2009-08-26 22:13:22 +0000 | [diff] [blame] | 95 | MCSymbol *CreateSymbol(StringRef Name); |
| 96 | |
Daniel Dunbar | 7c0a334 | 2009-08-26 22:49:51 +0000 | [diff] [blame] | 97 | // FIXME: See comment on SectionUniquingMap. |
| 98 | const MCSection *getMachOSection(const StringRef &Segment, |
| 99 | const StringRef &Section, |
| 100 | unsigned TypeAndAttributes, |
| 101 | unsigned Reserved2, |
| 102 | SectionKind Kind) const; |
| 103 | |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 104 | bool ParseStatement(); |
Daniel Dunbar | 3fb7683 | 2009-06-30 00:49:23 +0000 | [diff] [blame] | 105 | |
Chris Lattner | 14ee48a | 2009-06-21 21:22:11 +0000 | [diff] [blame] | 106 | bool TokError(const char *Msg); |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 107 | |
Sean Callanan | bf2013e | 2010-01-20 23:19:55 +0000 | [diff] [blame] | 108 | void PrintMessage(SMLoc Loc, const std::string &Msg, const char *Type) const; |
| 109 | |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 110 | bool ParseConditionalAssemblyDirectives(StringRef Directive, |
| 111 | SMLoc DirectiveLoc); |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 112 | void EatToEndOfStatement(); |
| 113 | |
Daniel Dunbar | e2ace50 | 2009-08-31 08:09:09 +0000 | [diff] [blame] | 114 | bool ParseAssignment(const StringRef &Name); |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 115 | |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 116 | bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc); |
| 117 | bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc); |
| 118 | bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc); |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 119 | |
| 120 | /// ParseIdentifier - Parse an identifier or string (as a quoted identifier) |
| 121 | /// and set \arg Res to the identifier contents. |
| 122 | bool ParseIdentifier(StringRef &Res); |
Chris Lattner | 29dfe7c | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 123 | |
Chris Lattner | 9a023f7 | 2009-06-24 04:43:34 +0000 | [diff] [blame] | 124 | // Directive Parsing. |
Chris Lattner | 529fb54 | 2009-06-24 05:13:15 +0000 | [diff] [blame] | 125 | bool ParseDirectiveDarwinSection(); // Darwin specific ".section". |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 126 | bool ParseDirectiveSectionSwitch(const char *Segment, const char *Section, |
Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 127 | unsigned TAA = 0, unsigned ImplicitAlign = 0, |
| 128 | unsigned StubSize = 0); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 129 | bool ParseDirectiveAscii(bool ZeroTerminated); // ".ascii", ".asciiz" |
| 130 | bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ... |
| 131 | bool ParseDirectiveFill(); // ".fill" |
| 132 | bool ParseDirectiveSpace(); // ".space" |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 133 | bool ParseDirectiveSet(); // ".set" |
Daniel Dunbar | c238b58 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 134 | bool ParseDirectiveOrg(); // ".org" |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 135 | // ".align{,32}", ".p2align{,w,l}" |
| 136 | bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize); |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 137 | |
| 138 | /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which |
| 139 | /// accepts a single symbol (which should be a label or an external). |
| 140 | bool ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr); |
Kevin Enderby | 95cf30c | 2009-07-14 18:17:10 +0000 | [diff] [blame] | 141 | bool ParseDirectiveDarwinSymbolDesc(); // Darwin specific ".desc" |
Kevin Enderby | 7114824 | 2009-07-14 21:35:03 +0000 | [diff] [blame] | 142 | bool ParseDirectiveDarwinLsym(); // Darwin specific ".lsym" |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 143 | |
Chris Lattner | 1fc3d75 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 144 | bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm" |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 145 | bool ParseDirectiveDarwinZerofill(); // Darwin specific ".zerofill" |
Kevin Enderby | a5c7832 | 2009-07-13 21:03:15 +0000 | [diff] [blame] | 146 | |
| 147 | // Darwin specific ".subsections_via_symbols" |
| 148 | bool ParseDirectiveDarwinSubsectionsViaSymbols(); |
Kevin Enderby | 6e68cd9 | 2009-07-15 15:30:11 +0000 | [diff] [blame] | 149 | // Darwin specific .dump and .load |
Kevin Enderby | 5026ae4 | 2009-07-20 20:25:37 +0000 | [diff] [blame] | 150 | bool ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump); |
Kevin Enderby | 5f1f0b8 | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 151 | |
| 152 | bool ParseDirectiveAbort(); // ".abort" |
Kevin Enderby | 1f049b2 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 153 | bool ParseDirectiveInclude(); // ".include" |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 154 | |
| 155 | bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if" |
| 156 | bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif" |
| 157 | bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else" |
| 158 | bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif |
| 159 | |
Chris Lattner | ebb89b4 | 2009-09-27 21:16:52 +0000 | [diff] [blame] | 160 | bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc); // ".file" |
| 161 | bool ParseDirectiveLine(StringRef, SMLoc DirectiveLoc); // ".line" |
| 162 | bool ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc); // ".loc" |
Daniel Dunbar | 1ab7594 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 163 | |
| 164 | /// ParseEscapedString - Parse the current token as a string which may include |
| 165 | /// escaped characters and return the string contents. |
| 166 | bool ParseEscapedString(std::string &Data); |
Chris Lattner | 27aa7d2 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 167 | }; |
| 168 | |
| 169 | } // end namespace llvm |
| 170 | |
| 171 | #endif |