Chris Lattner | b013345 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 1 | //===- AsmParser.cpp - Parser for Assembly Files --------------------------===// |
| 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 implements the parser for assembly files. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Daniel Dunbar | 2af1653 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/APFloat.h" |
Chad Rosier | eb5c168 | 2013-02-13 18:38:58 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/SmallString.h" |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/StringMap.h" |
Daniel Dunbar | eb6bb32 | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/Twine.h" |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCAsmInfo.h" |
Daniel Dunbar | ca29e4d | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCContext.h" |
Evan Cheng | 1142444 | 2011-07-26 00:24:13 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCDwarf.h" |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCExpr.h" |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCInstPrinter.h" |
| 24 | #include "llvm/MC/MCInstrInfo.h" |
Rafael Espindola | e28610d | 2013-12-09 20:26:40 +0000 | [diff] [blame] | 25 | #include "llvm/MC/MCObjectFileInfo.h" |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 26 | #include "llvm/MC/MCParser/AsmCond.h" |
| 27 | #include "llvm/MC/MCParser/AsmLexer.h" |
| 28 | #include "llvm/MC/MCParser/MCAsmParser.h" |
| 29 | #include "llvm/MC/MCParser/MCParsedAsmOperand.h" |
Evan Cheng | 7679299 | 2011-07-20 05:58:47 +0000 | [diff] [blame] | 30 | #include "llvm/MC/MCRegisterInfo.h" |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 31 | #include "llvm/MC/MCSectionMachO.h" |
Daniel Dunbar | ca29e4d | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 32 | #include "llvm/MC/MCStreamer.h" |
Daniel Dunbar | ae7ac01 | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 33 | #include "llvm/MC/MCSymbol.h" |
Evan Cheng | 1142444 | 2011-07-26 00:24:13 +0000 | [diff] [blame] | 34 | #include "llvm/MC/MCTargetAsmParser.h" |
Joerg Sonnenberger | 74ba262 | 2011-05-19 18:00:13 +0000 | [diff] [blame] | 35 | #include "llvm/Support/CommandLine.h" |
Benjamin Kramer | 4efe506 | 2012-01-28 15:28:41 +0000 | [diff] [blame] | 36 | #include "llvm/Support/ErrorHandling.h" |
Jim Grosbach | 76346c3 | 2011-06-29 16:05:14 +0000 | [diff] [blame] | 37 | #include "llvm/Support/MathExtras.h" |
Kevin Enderby | e233dda | 2010-06-28 21:45:58 +0000 | [diff] [blame] | 38 | #include "llvm/Support/MemoryBuffer.h" |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 39 | #include "llvm/Support/SourceMgr.h" |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 40 | #include "llvm/Support/raw_ostream.h" |
Nick Lewycky | 0de20af | 2010-12-19 20:43:38 +0000 | [diff] [blame] | 41 | #include <cctype> |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 42 | #include <set> |
| 43 | #include <string> |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 44 | #include <vector> |
Chris Lattner | b013345 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 45 | using namespace llvm; |
| 46 | |
Joerg Sonnenberger | 74ba262 | 2011-05-19 18:00:13 +0000 | [diff] [blame] | 47 | static cl::opt<bool> |
| 48 | FatalAssemblerWarnings("fatal-assembler-warnings", |
| 49 | cl::desc("Consider warnings as error")); |
| 50 | |
Eric Christopher | a7c3273 | 2012-12-18 00:30:54 +0000 | [diff] [blame] | 51 | MCAsmParserSemaCallback::~MCAsmParserSemaCallback() {} |
Nick Lewycky | ac61227 | 2012-10-19 07:00:09 +0000 | [diff] [blame] | 52 | |
Daniel Dunbar | 8603340 | 2010-07-12 17:54:38 +0000 | [diff] [blame] | 53 | namespace { |
| 54 | |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 55 | /// \brief Helper types for tracking macro definitions. |
| 56 | typedef std::vector<AsmToken> MCAsmMacroArgument; |
| 57 | typedef std::vector<MCAsmMacroArgument> MCAsmMacroArguments; |
| 58 | typedef std::pair<StringRef, MCAsmMacroArgument> MCAsmMacroParameter; |
| 59 | typedef std::vector<MCAsmMacroParameter> MCAsmMacroParameters; |
| 60 | |
| 61 | struct MCAsmMacro { |
| 62 | StringRef Name; |
| 63 | StringRef Body; |
| 64 | MCAsmMacroParameters Parameters; |
| 65 | |
| 66 | public: |
| 67 | MCAsmMacro(StringRef N, StringRef B, const MCAsmMacroParameters &P) : |
| 68 | Name(N), Body(B), Parameters(P) {} |
| 69 | |
| 70 | MCAsmMacro(const MCAsmMacro& Other) |
| 71 | : Name(Other.Name), Body(Other.Body), Parameters(Other.Parameters) {} |
| 72 | }; |
| 73 | |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 74 | /// \brief Helper class for storing information about an active macro |
| 75 | /// instantiation. |
| 76 | struct MacroInstantiation { |
| 77 | /// The macro being instantiated. |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 78 | const MCAsmMacro *TheMacro; |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 79 | |
| 80 | /// The macro instantiation with substitutions. |
| 81 | MemoryBuffer *Instantiation; |
| 82 | |
| 83 | /// The location of the instantiation. |
| 84 | SMLoc InstantiationLoc; |
| 85 | |
Daniel Dunbar | 40f1d85 | 2012-12-01 01:38:48 +0000 | [diff] [blame] | 86 | /// The buffer where parsing should resume upon instantiation completion. |
| 87 | int ExitBuffer; |
| 88 | |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 89 | /// The location where parsing should resume upon instantiation completion. |
| 90 | SMLoc ExitLoc; |
| 91 | |
| 92 | public: |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 93 | MacroInstantiation(const MCAsmMacro *M, SMLoc IL, int EB, SMLoc EL, |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 94 | MemoryBuffer *I); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 95 | }; |
| 96 | |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 97 | struct ParseStatementInfo { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 98 | /// \brief The parsed operands from the last parsed statement. |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 99 | SmallVector<MCParsedAsmOperand*, 8> ParsedOperands; |
| 100 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 101 | /// \brief The opcode from the last parsed instruction. |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 102 | unsigned Opcode; |
| 103 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 104 | /// \brief Was there an error parsing the inline assembly? |
Chad Rosier | 149e8e0 | 2012-12-12 22:45:52 +0000 | [diff] [blame] | 105 | bool ParseError; |
| 106 | |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 107 | SmallVectorImpl<AsmRewrite> *AsmRewrites; |
| 108 | |
Chad Rosier | 149e8e0 | 2012-12-12 22:45:52 +0000 | [diff] [blame] | 109 | ParseStatementInfo() : Opcode(~0U), ParseError(false), AsmRewrites(0) {} |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 110 | ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites) |
Chad Rosier | 149e8e0 | 2012-12-12 22:45:52 +0000 | [diff] [blame] | 111 | : Opcode(~0), ParseError(false), AsmRewrites(rewrites) {} |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 112 | |
| 113 | ~ParseStatementInfo() { |
| 114 | // Free any parsed operands. |
| 115 | for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i) |
| 116 | delete ParsedOperands[i]; |
| 117 | ParsedOperands.clear(); |
| 118 | } |
| 119 | }; |
| 120 | |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 121 | /// \brief The concrete assembly parser instance. |
| 122 | class AsmParser : public MCAsmParser { |
Craig Topper | 2e6644c | 2012-09-15 16:23:52 +0000 | [diff] [blame] | 123 | AsmParser(const AsmParser &) LLVM_DELETED_FUNCTION; |
| 124 | void operator=(const AsmParser &) LLVM_DELETED_FUNCTION; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 125 | private: |
| 126 | AsmLexer Lexer; |
| 127 | MCContext &Ctx; |
| 128 | MCStreamer &Out; |
Jim Grosbach | c7e6b8f | 2011-06-15 18:33:28 +0000 | [diff] [blame] | 129 | const MCAsmInfo &MAI; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 130 | SourceMgr &SrcMgr; |
Benjamin Kramer | 47f5e30 | 2011-10-16 10:48:29 +0000 | [diff] [blame] | 131 | SourceMgr::DiagHandlerTy SavedDiagHandler; |
| 132 | void *SavedDiagContext; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 133 | MCAsmParserExtension *PlatformParser; |
Rafael Espindola | 82065cb | 2011-04-11 21:49:50 +0000 | [diff] [blame] | 134 | |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 135 | /// This is the current buffer index we're lexing from as managed by the |
| 136 | /// SourceMgr object. |
| 137 | int CurBuffer; |
| 138 | |
| 139 | AsmCond TheCondState; |
| 140 | std::vector<AsmCond> TheCondStack; |
| 141 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 142 | /// \brief maps directive names to handler methods in parser |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 143 | /// extensions. Extensions register themselves in this map by calling |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 144 | /// addDirectiveHandler. |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 145 | StringMap<ExtensionDirectiveHandler> ExtensionDirectiveMap; |
Daniel Dunbar | 828984f | 2010-07-18 18:38:02 +0000 | [diff] [blame] | 146 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 147 | /// \brief Map of currently defined macros. |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 148 | StringMap<MCAsmMacro*> MacroMap; |
Daniel Dunbar | c1f58ec | 2010-07-18 18:47:21 +0000 | [diff] [blame] | 149 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 150 | /// \brief Stack of active macro instantiations. |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 151 | std::vector<MacroInstantiation*> ActiveMacros; |
| 152 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 153 | /// \brief List of bodies of anonymous macros. |
Benjamin Kramer | 1df3a1f | 2013-08-04 09:06:29 +0000 | [diff] [blame] | 154 | std::deque<MCAsmMacro> MacroLikeBodies; |
| 155 | |
Daniel Dunbar | 828984f | 2010-07-18 18:38:02 +0000 | [diff] [blame] | 156 | /// Boolean tracking whether macro substitution is enabled. |
Eli Bendersky | c2f6f92 | 2013-01-14 18:08:41 +0000 | [diff] [blame] | 157 | unsigned MacrosEnabledFlag : 1; |
Daniel Dunbar | 828984f | 2010-07-18 18:38:02 +0000 | [diff] [blame] | 158 | |
Daniel Dunbar | 43325c4 | 2010-09-09 22:42:56 +0000 | [diff] [blame] | 159 | /// Flag tracking whether any errors have been encountered. |
| 160 | unsigned HadError : 1; |
| 161 | |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 162 | /// The values from the last parsed cpp hash file line comment if any. |
| 163 | StringRef CppHashFilename; |
| 164 | int64_t CppHashLineNumber; |
| 165 | SMLoc CppHashLoc; |
Kevin Enderby | 27121c1 | 2012-11-05 21:55:41 +0000 | [diff] [blame] | 166 | int CppHashBuf; |
Kevin Enderby | 0fd064c | 2013-06-21 20:51:39 +0000 | [diff] [blame] | 167 | /// When generating dwarf for assembly source files we need to calculate the |
| 168 | /// logical line number based on the last parsed cpp hash file line comment |
Vladimir Medic | 9bad0d33 | 2013-08-20 13:33:18 +0000 | [diff] [blame] | 169 | /// and current line. Since this is slow and messes up the SourceMgr's |
Kevin Enderby | 0fd064c | 2013-06-21 20:51:39 +0000 | [diff] [blame] | 170 | /// cache we save the last info we queried with SrcMgr.FindLineNumber(). |
| 171 | SMLoc LastQueryIDLoc; |
| 172 | int LastQueryBuffer; |
| 173 | unsigned LastQueryLine; |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 174 | |
Devang Patel | a173ee5 | 2012-01-31 18:14:05 +0000 | [diff] [blame] | 175 | /// AssemblerDialect. ~OU means unset value and use value provided by MAI. |
| 176 | unsigned AssemblerDialect; |
| 177 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 178 | /// \brief is Darwin compatibility enabled? |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 179 | bool IsDarwin; |
| 180 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 181 | /// \brief Are we parsing ms-style inline assembly? |
Chad Rosier | 4996355 | 2012-10-13 00:26:04 +0000 | [diff] [blame] | 182 | bool ParsingInlineAsm; |
| 183 | |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 184 | public: |
Jim Grosbach | 345768c | 2011-08-16 18:33:49 +0000 | [diff] [blame] | 185 | AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out, |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 186 | const MCAsmInfo &MAI); |
Craig Topper | 5f96ca5 | 2012-08-29 05:48:09 +0000 | [diff] [blame] | 187 | virtual ~AsmParser(); |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 188 | |
| 189 | virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false); |
| 190 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 191 | virtual void addDirectiveHandler(StringRef Directive, |
Eli Bendersky | 29b9f47 | 2013-01-16 00:50:52 +0000 | [diff] [blame] | 192 | ExtensionDirectiveHandler Handler) { |
| 193 | ExtensionDirectiveMap[Directive] = Handler; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | public: |
| 197 | /// @name MCAsmParser Interface |
| 198 | /// { |
| 199 | |
| 200 | virtual SourceMgr &getSourceManager() { return SrcMgr; } |
| 201 | virtual MCAsmLexer &getLexer() { return Lexer; } |
| 202 | virtual MCContext &getContext() { return Ctx; } |
| 203 | virtual MCStreamer &getStreamer() { return Out; } |
Eric Christopher | a7c3273 | 2012-12-18 00:30:54 +0000 | [diff] [blame] | 204 | virtual unsigned getAssemblerDialect() { |
Devang Patel | a173ee5 | 2012-01-31 18:14:05 +0000 | [diff] [blame] | 205 | if (AssemblerDialect == ~0U) |
Eric Christopher | a7c3273 | 2012-12-18 00:30:54 +0000 | [diff] [blame] | 206 | return MAI.getAssemblerDialect(); |
Devang Patel | a173ee5 | 2012-01-31 18:14:05 +0000 | [diff] [blame] | 207 | else |
| 208 | return AssemblerDialect; |
| 209 | } |
| 210 | virtual void setAssemblerDialect(unsigned i) { |
| 211 | AssemblerDialect = i; |
| 212 | } |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 213 | |
Saleem Abdulrasool | 69c7caf | 2014-01-07 02:28:31 +0000 | [diff] [blame] | 214 | virtual void Note(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges = None); |
Chris Lattner | a3a0681 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 215 | virtual bool Warning(SMLoc L, const Twine &Msg, |
Dmitri Gribenko | 3238fb7 | 2013-05-05 00:40:33 +0000 | [diff] [blame] | 216 | ArrayRef<SMRange> Ranges = None); |
Chris Lattner | a3a0681 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 217 | virtual bool Error(SMLoc L, const Twine &Msg, |
Dmitri Gribenko | 3238fb7 | 2013-05-05 00:40:33 +0000 | [diff] [blame] | 218 | ArrayRef<SMRange> Ranges = None); |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 219 | |
Craig Topper | 5f96ca5 | 2012-08-29 05:48:09 +0000 | [diff] [blame] | 220 | virtual const AsmToken &Lex(); |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 221 | |
Chad Rosier | 4996355 | 2012-10-13 00:26:04 +0000 | [diff] [blame] | 222 | void setParsingInlineAsm(bool V) { ParsingInlineAsm = V; } |
Chad Rosier | e4ad2a0 | 2012-10-16 20:16:20 +0000 | [diff] [blame] | 223 | bool isParsingInlineAsm() { return ParsingInlineAsm; } |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 224 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 225 | bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString, |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 226 | unsigned &NumOutputs, unsigned &NumInputs, |
Chad Rosier | 37e755c | 2012-10-23 17:43:43 +0000 | [diff] [blame] | 227 | SmallVectorImpl<std::pair<void *,bool> > &OpDecls, |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 228 | SmallVectorImpl<std::string> &Constraints, |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 229 | SmallVectorImpl<std::string> &Clobbers, |
| 230 | const MCInstrInfo *MII, |
| 231 | const MCInstPrinter *IP, |
| 232 | MCAsmParserSemaCallback &SI); |
Chad Rosier | 4996355 | 2012-10-13 00:26:04 +0000 | [diff] [blame] | 233 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 234 | bool parseExpression(const MCExpr *&Res); |
| 235 | virtual bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc); |
Chad Rosier | 1863f4f | 2013-04-10 17:35:30 +0000 | [diff] [blame] | 236 | virtual bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 237 | virtual bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc); |
| 238 | virtual bool parseAbsoluteExpression(int64_t &Res); |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 239 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 240 | /// \brief Parse an identifier or string (as a quoted identifier) |
Eli Bendersky | 0cf0cb9 | 2013-01-12 00:05:00 +0000 | [diff] [blame] | 241 | /// and set \p Res to the identifier contents. |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 242 | virtual bool parseIdentifier(StringRef &Res); |
| 243 | virtual void eatToEndOfStatement(); |
Eli Bendersky | 0cf0cb9 | 2013-01-12 00:05:00 +0000 | [diff] [blame] | 244 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 245 | virtual void checkForValidSection(); |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 246 | /// } |
| 247 | |
| 248 | private: |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 249 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 250 | bool parseStatement(ParseStatementInfo &Info); |
| 251 | void eatToEndOfLine(); |
| 252 | bool parseCppHashLineFilenameComment(const SMLoc &L); |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 253 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 254 | void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body, |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 255 | MCAsmMacroParameters Parameters); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 256 | bool expandMacro(raw_svector_ostream &OS, StringRef Body, |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 257 | const MCAsmMacroParameters &Parameters, |
| 258 | const MCAsmMacroArguments &A, |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 259 | const SMLoc &L); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 260 | |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 261 | /// \brief Are macros enabled in the parser? |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 262 | bool areMacrosEnabled() {return MacrosEnabledFlag;} |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 263 | |
| 264 | /// \brief Control a flag in the parser that enables or disables macros. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 265 | void setMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;} |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 266 | |
| 267 | /// \brief Lookup a previously defined macro. |
| 268 | /// \param Name Macro name. |
| 269 | /// \returns Pointer to macro. NULL if no such macro was defined. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 270 | const MCAsmMacro* lookupMacro(StringRef Name); |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 271 | |
| 272 | /// \brief Define a new macro with the given name and information. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 273 | void defineMacro(StringRef Name, const MCAsmMacro& Macro); |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 274 | |
| 275 | /// \brief Undefine a macro. If no such macro was defined, it's a no-op. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 276 | void undefineMacro(StringRef Name); |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 277 | |
| 278 | /// \brief Are we inside a macro instantiation? |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 279 | bool isInsideMacroInstantiation() {return !ActiveMacros.empty();} |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 280 | |
Vladimir Medic | 9bad0d33 | 2013-08-20 13:33:18 +0000 | [diff] [blame] | 281 | /// \brief Handle entry to macro instantiation. |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 282 | /// |
| 283 | /// \param M The macro. |
| 284 | /// \param NameLoc Instantiation location. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 285 | bool handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc); |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 286 | |
| 287 | /// \brief Handle exit from macro instantiation. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 288 | void handleMacroExit(); |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 289 | |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 290 | /// \brief Extract AsmTokens for a macro argument. |
| 291 | bool parseMacroArgument(MCAsmMacroArgument &MA); |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 292 | |
| 293 | /// \brief Parse all macro arguments for a given macro. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 294 | bool parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A); |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 295 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 296 | void printMacroInstantiations(); |
| 297 | void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg, |
Dmitri Gribenko | 3238fb7 | 2013-05-05 00:40:33 +0000 | [diff] [blame] | 298 | ArrayRef<SMRange> Ranges = None) const { |
Chris Lattner | 7284526 | 2011-10-16 05:47:55 +0000 | [diff] [blame] | 299 | SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges); |
Benjamin Kramer | c758311 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 300 | } |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 301 | static void DiagHandler(const SMDiagnostic &Diag, void *Context); |
Benjamin Kramer | c758311 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 302 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 303 | /// \brief Enter the specified file. This returns true on failure. |
| 304 | bool enterIncludeFile(const std::string &Filename); |
| 305 | |
| 306 | /// \brief Process the specified file for the .incbin directive. |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 307 | /// This returns true on failure. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 308 | bool processIncbinFile(const std::string &Filename); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 309 | |
Dmitri Gribenko | 5485acd | 2012-09-14 14:57:36 +0000 | [diff] [blame] | 310 | /// \brief Reset the current lexer position to that given by \p Loc. The |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 311 | /// current token is not set; clients should ensure Lex() is called |
| 312 | /// subsequently. |
Daniel Dunbar | 40f1d85 | 2012-12-01 01:38:48 +0000 | [diff] [blame] | 313 | /// |
| 314 | /// \param InBuffer If not -1, should be the known buffer id that contains the |
| 315 | /// location. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 316 | void jumpToLoc(SMLoc Loc, int InBuffer=-1); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 317 | |
Daniel Dunbar | 40a564f | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 318 | /// \brief Parse up to the end of statement and a return the contents from the |
| 319 | /// current token until the end of the statement; the current token on exit |
| 320 | /// will be either the EndOfStatement or EOF. |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 321 | virtual StringRef parseStringToEndOfStatement(); |
Daniel Dunbar | 40a564f | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 322 | |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 323 | /// \brief Parse until the end of a statement or a comma is encountered, |
| 324 | /// return the contents from the current token up to the end or comma. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 325 | StringRef parseStringToComma(); |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 326 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 327 | bool parseAssignment(StringRef Name, bool allow_redef, |
Jim Grosbach | b7b750d | 2012-09-13 23:11:31 +0000 | [diff] [blame] | 328 | bool NoDeadStrip = false); |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 329 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 330 | bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc); |
| 331 | bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc); |
| 332 | bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc); |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 333 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 334 | bool parseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc); |
Rafael Espindola | 63760ba | 2010-10-28 20:02:27 +0000 | [diff] [blame] | 335 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 336 | // Generic (target and platform independent) directive parsing. |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 337 | enum DirectiveKind { |
Eli Bendersky | 4d21fa0 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 338 | DK_NO_DIRECTIVE, // Placeholder |
| 339 | DK_SET, DK_EQU, DK_EQUIV, DK_ASCII, DK_ASCIZ, DK_STRING, DK_BYTE, DK_SHORT, |
David Woodhouse | d6de0d9 | 2014-02-01 16:20:59 +0000 | [diff] [blame] | 340 | DK_VALUE, DK_2BYTE, DK_LONG, DK_INT, DK_4BYTE, DK_QUAD, DK_8BYTE, DK_OCTA, |
| 341 | DK_SINGLE, DK_FLOAT, DK_DOUBLE, DK_ALIGN, DK_ALIGN32, DK_BALIGN, DK_BALIGNW, |
Eli Bendersky | 9652272 | 2013-01-11 22:55:28 +0000 | [diff] [blame] | 342 | DK_BALIGNL, DK_P2ALIGN, DK_P2ALIGNW, DK_P2ALIGNL, DK_ORG, DK_FILL, DK_ENDR, |
Eli Bendersky | 4d21fa0 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 343 | DK_BUNDLE_ALIGN_MODE, DK_BUNDLE_LOCK, DK_BUNDLE_UNLOCK, |
Kevin Enderby | 3aeada2 | 2013-08-28 17:50:59 +0000 | [diff] [blame] | 344 | DK_ZERO, DK_EXTERN, DK_GLOBL, DK_GLOBAL, |
Eli Bendersky | 4d21fa0 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 345 | DK_LAZY_REFERENCE, DK_NO_DEAD_STRIP, DK_SYMBOL_RESOLVER, DK_PRIVATE_EXTERN, |
| 346 | DK_REFERENCE, DK_WEAK_DEFINITION, DK_WEAK_REFERENCE, |
| 347 | DK_WEAK_DEF_CAN_BE_HIDDEN, DK_COMM, DK_COMMON, DK_LCOMM, DK_ABORT, |
| 348 | DK_INCLUDE, DK_INCBIN, DK_CODE16, DK_CODE16GCC, DK_REPT, DK_IRP, DK_IRPC, |
| 349 | DK_IF, DK_IFB, DK_IFNB, DK_IFC, DK_IFNC, DK_IFDEF, DK_IFNDEF, DK_IFNOTDEF, |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 350 | DK_ELSEIF, DK_ELSE, DK_ENDIF, |
| 351 | DK_SPACE, DK_SKIP, DK_FILE, DK_LINE, DK_LOC, DK_STABS, |
| 352 | DK_CFI_SECTIONS, DK_CFI_STARTPROC, DK_CFI_ENDPROC, DK_CFI_DEF_CFA, |
| 353 | DK_CFI_DEF_CFA_OFFSET, DK_CFI_ADJUST_CFA_OFFSET, DK_CFI_DEF_CFA_REGISTER, |
| 354 | DK_CFI_OFFSET, DK_CFI_REL_OFFSET, DK_CFI_PERSONALITY, DK_CFI_LSDA, |
| 355 | DK_CFI_REMEMBER_STATE, DK_CFI_RESTORE_STATE, DK_CFI_SAME_VALUE, |
| 356 | DK_CFI_RESTORE, DK_CFI_ESCAPE, DK_CFI_SIGNAL_FRAME, DK_CFI_UNDEFINED, |
Venkatraman Govindaraju | 3816d43 | 2013-09-26 14:49:40 +0000 | [diff] [blame] | 357 | DK_CFI_REGISTER, DK_CFI_WINDOW_SAVE, |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 358 | DK_MACROS_ON, DK_MACROS_OFF, DK_MACRO, DK_ENDM, DK_ENDMACRO, DK_PURGEM, |
Saleem Abdulrasool | 88186c4 | 2013-12-18 02:53:03 +0000 | [diff] [blame] | 359 | DK_SLEB128, DK_ULEB128, |
| 360 | DK_END |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 361 | }; |
| 362 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 363 | /// \brief Maps directive name --> DirectiveKind enum, for |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 364 | /// directives parsed by this class. |
| 365 | StringMap<DirectiveKind> DirectiveKindMap; |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 366 | |
| 367 | // ".ascii", ".asciz", ".string" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 368 | bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated); |
| 369 | bool parseDirectiveValue(unsigned Size); // ".byte", ".long", ... |
David Woodhouse | d6de0d9 | 2014-02-01 16:20:59 +0000 | [diff] [blame] | 370 | bool parseDirectiveOctaValue(); // ".octa" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 371 | bool parseDirectiveRealValue(const fltSemantics &); // ".single", ... |
| 372 | bool parseDirectiveFill(); // ".fill" |
| 373 | bool parseDirectiveZero(); // ".zero" |
Eric Christopher | a7c3273 | 2012-12-18 00:30:54 +0000 | [diff] [blame] | 374 | // ".set", ".equ", ".equiv" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 375 | bool parseDirectiveSet(StringRef IDVal, bool allow_redef); |
| 376 | bool parseDirectiveOrg(); // ".org" |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 377 | // ".align{,32}", ".p2align{,w,l}" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 378 | bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize); |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 379 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 380 | // ".file", ".line", ".loc", ".stabs" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 381 | bool parseDirectiveFile(SMLoc DirectiveLoc); |
| 382 | bool parseDirectiveLine(); |
| 383 | bool parseDirectiveLoc(); |
| 384 | bool parseDirectiveStabs(); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 385 | |
| 386 | // .cfi directives |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 387 | bool parseDirectiveCFIRegister(SMLoc DirectiveLoc); |
Venkatraman Govindaraju | 3816d43 | 2013-09-26 14:49:40 +0000 | [diff] [blame] | 388 | bool parseDirectiveCFIWindowSave(); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 389 | bool parseDirectiveCFISections(); |
| 390 | bool parseDirectiveCFIStartProc(); |
| 391 | bool parseDirectiveCFIEndProc(); |
| 392 | bool parseDirectiveCFIDefCfaOffset(); |
| 393 | bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc); |
| 394 | bool parseDirectiveCFIAdjustCfaOffset(); |
| 395 | bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc); |
| 396 | bool parseDirectiveCFIOffset(SMLoc DirectiveLoc); |
| 397 | bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc); |
| 398 | bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality); |
| 399 | bool parseDirectiveCFIRememberState(); |
| 400 | bool parseDirectiveCFIRestoreState(); |
| 401 | bool parseDirectiveCFISameValue(SMLoc DirectiveLoc); |
| 402 | bool parseDirectiveCFIRestore(SMLoc DirectiveLoc); |
| 403 | bool parseDirectiveCFIEscape(); |
| 404 | bool parseDirectiveCFISignalFrame(); |
| 405 | bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 406 | |
| 407 | // macro directives |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 408 | bool parseDirectivePurgeMacro(SMLoc DirectiveLoc); |
| 409 | bool parseDirectiveEndMacro(StringRef Directive); |
| 410 | bool parseDirectiveMacro(SMLoc DirectiveLoc); |
| 411 | bool parseDirectiveMacrosOnOff(StringRef Directive); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 412 | |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 413 | // ".bundle_align_mode" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 414 | bool parseDirectiveBundleAlignMode(); |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 415 | // ".bundle_lock" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 416 | bool parseDirectiveBundleLock(); |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 417 | // ".bundle_unlock" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 418 | bool parseDirectiveBundleUnlock(); |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 419 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 420 | // ".space", ".skip" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 421 | bool parseDirectiveSpace(StringRef IDVal); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 422 | |
| 423 | // .sleb128 (Signed=true) and .uleb128 (Signed=false) |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 424 | bool parseDirectiveLEB128(bool Signed); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 425 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 426 | /// \brief Parse a directive like ".globl" which |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 427 | /// accepts a single symbol (which should be a label or an external). |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 428 | bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr); |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 429 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 430 | bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm" |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 431 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 432 | bool parseDirectiveAbort(); // ".abort" |
| 433 | bool parseDirectiveInclude(); // ".include" |
| 434 | bool parseDirectiveIncbin(); // ".incbin" |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 435 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 436 | bool parseDirectiveIf(SMLoc DirectiveLoc); // ".if" |
Benjamin Kramer | 62c18b0 | 2012-05-12 11:18:42 +0000 | [diff] [blame] | 437 | // ".ifb" or ".ifnb", depending on ExpectBlank. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 438 | bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank); |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 439 | // ".ifc" or ".ifnc", depending on ExpectEqual. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 440 | bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual); |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 441 | // ".ifdef" or ".ifndef", depending on expect_defined |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 442 | bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined); |
| 443 | bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif" |
| 444 | bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else" |
| 445 | bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 446 | virtual bool parseEscapedString(std::string &Data); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 447 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 448 | const MCExpr *applyModifierToExpr(const MCExpr *E, |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 449 | MCSymbolRefExpr::VariantKind Variant); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 450 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 451 | // Macro-like directives |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 452 | MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc); |
| 453 | void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc, |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 454 | raw_svector_ostream &OS); |
Saleem Abdulrasool | d743d0a | 2013-12-28 05:54:33 +0000 | [diff] [blame] | 455 | bool parseDirectiveRept(SMLoc DirectiveLoc, StringRef Directive); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 456 | bool parseDirectiveIrp(SMLoc DirectiveLoc); // ".irp" |
| 457 | bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc" |
| 458 | bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr" |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 459 | |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 460 | // "_emit" or "__emit" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 461 | bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info, |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 462 | size_t Len); |
| 463 | |
| 464 | // "align" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 465 | bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info); |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 466 | |
Saleem Abdulrasool | 88186c4 | 2013-12-18 02:53:03 +0000 | [diff] [blame] | 467 | // "end" |
| 468 | bool parseDirectiveEnd(SMLoc DirectiveLoc); |
| 469 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 470 | void initializeDirectiveKindMap(); |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 471 | }; |
Daniel Dunbar | 8603340 | 2010-07-12 17:54:38 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Daniel Dunbar | 0cb91cf | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 474 | namespace llvm { |
| 475 | |
| 476 | extern MCAsmParserExtension *createDarwinAsmParser(); |
Daniel Dunbar | ab058b8 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 477 | extern MCAsmParserExtension *createELFAsmParser(); |
Michael J. Spencer | c8dbdfd | 2010-10-09 11:01:07 +0000 | [diff] [blame] | 478 | extern MCAsmParserExtension *createCOFFAsmParser(); |
Daniel Dunbar | 0cb91cf | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 479 | |
| 480 | } |
| 481 | |
Chris Lattner | c35681b | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 482 | enum { DEFAULT_ADDRSPACE = 0 }; |
| 483 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 484 | AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out, |
| 485 | const MCAsmInfo &_MAI) |
| 486 | : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM), |
| 487 | PlatformParser(0), CurBuffer(0), MacrosEnabledFlag(true), |
| 488 | CppHashLineNumber(0), AssemblerDialect(~0U), IsDarwin(false), |
| 489 | ParsingInlineAsm(false) { |
Benjamin Kramer | 47f5e30 | 2011-10-16 10:48:29 +0000 | [diff] [blame] | 490 | // Save the old handler. |
| 491 | SavedDiagHandler = SrcMgr.getDiagHandler(); |
| 492 | SavedDiagContext = SrcMgr.getDiagContext(); |
| 493 | // Set our own handler which calls the saved handler. |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 494 | SrcMgr.setDiagHandler(DiagHandler, this); |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 495 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)); |
Daniel Dunbar | 8603340 | 2010-07-12 17:54:38 +0000 | [diff] [blame] | 496 | |
Daniel Dunbar | c501108 | 2010-07-12 18:12:02 +0000 | [diff] [blame] | 497 | // Initialize the platform / file format parser. |
Rafael Espindola | e28610d | 2013-12-09 20:26:40 +0000 | [diff] [blame] | 498 | switch (_Ctx.getObjectFileInfo()->getObjectFileType()) { |
| 499 | case MCObjectFileInfo::IsCOFF: |
| 500 | PlatformParser = createCOFFAsmParser(); |
| 501 | PlatformParser->Initialize(*this); |
| 502 | break; |
| 503 | case MCObjectFileInfo::IsMachO: |
| 504 | PlatformParser = createDarwinAsmParser(); |
| 505 | PlatformParser->Initialize(*this); |
| 506 | IsDarwin = true; |
| 507 | break; |
| 508 | case MCObjectFileInfo::IsELF: |
| 509 | PlatformParser = createELFAsmParser(); |
| 510 | PlatformParser->Initialize(*this); |
| 511 | break; |
Daniel Dunbar | c501108 | 2010-07-12 18:12:02 +0000 | [diff] [blame] | 512 | } |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 513 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 514 | initializeDirectiveKindMap(); |
Chris Lattner | 351a7ef | 2009-09-27 21:16:52 +0000 | [diff] [blame] | 515 | } |
| 516 | |
Daniel Dunbar | 4d7b2e3 | 2009-08-26 22:49:51 +0000 | [diff] [blame] | 517 | AsmParser::~AsmParser() { |
Daniel Dunbar | b759a13 | 2010-07-29 01:51:55 +0000 | [diff] [blame] | 518 | assert(ActiveMacros.empty() && "Unexpected active macro instantiation!"); |
| 519 | |
| 520 | // Destroy any macros. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 521 | for (StringMap<MCAsmMacro *>::iterator it = MacroMap.begin(), |
| 522 | ie = MacroMap.end(); |
| 523 | it != ie; ++it) |
Daniel Dunbar | b759a13 | 2010-07-29 01:51:55 +0000 | [diff] [blame] | 524 | delete it->getValue(); |
| 525 | |
Daniel Dunbar | c501108 | 2010-07-12 18:12:02 +0000 | [diff] [blame] | 526 | delete PlatformParser; |
Daniel Dunbar | 4d7b2e3 | 2009-08-26 22:49:51 +0000 | [diff] [blame] | 527 | } |
| 528 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 529 | void AsmParser::printMacroInstantiations() { |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 530 | // Print the active macro instantiation stack. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 531 | for (std::vector<MacroInstantiation *>::const_reverse_iterator |
| 532 | it = ActiveMacros.rbegin(), |
| 533 | ie = ActiveMacros.rend(); |
| 534 | it != ie; ++it) |
| 535 | printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note, |
Chris Lattner | 03b80a4 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 536 | "while in macro instantiation"); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 537 | } |
| 538 | |
Saleem Abdulrasool | 69c7caf | 2014-01-07 02:28:31 +0000 | [diff] [blame] | 539 | void AsmParser::Note(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) { |
| 540 | printMessage(L, SourceMgr::DK_Note, Msg, Ranges); |
| 541 | printMacroInstantiations(); |
| 542 | } |
| 543 | |
Chris Lattner | a3a0681 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 544 | bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) { |
Joerg Sonnenberger | 74ba262 | 2011-05-19 18:00:13 +0000 | [diff] [blame] | 545 | if (FatalAssemblerWarnings) |
Chris Lattner | a3a0681 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 546 | return Error(L, Msg, Ranges); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 547 | printMessage(L, SourceMgr::DK_Warning, Msg, Ranges); |
| 548 | printMacroInstantiations(); |
Joerg Sonnenberger | 74ba262 | 2011-05-19 18:00:13 +0000 | [diff] [blame] | 549 | return false; |
Daniel Dunbar | c9dc78a | 2009-06-30 00:49:23 +0000 | [diff] [blame] | 550 | } |
| 551 | |
Chris Lattner | a3a0681 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 552 | bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) { |
Daniel Dunbar | 43325c4 | 2010-09-09 22:42:56 +0000 | [diff] [blame] | 553 | HadError = true; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 554 | printMessage(L, SourceMgr::DK_Error, Msg, Ranges); |
| 555 | printMacroInstantiations(); |
Chris Lattner | 2adc9e7 | 2009-06-21 21:22:11 +0000 | [diff] [blame] | 556 | return true; |
| 557 | } |
| 558 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 559 | bool AsmParser::enterIncludeFile(const std::string &Filename) { |
Joerg Sonnenberger | af5f23e | 2011-06-01 13:10:15 +0000 | [diff] [blame] | 560 | std::string IncludedFile; |
| 561 | int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile); |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 562 | if (NewBuf == -1) |
| 563 | return true; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 564 | |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 565 | CurBuffer = NewBuf; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 566 | |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 567 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 568 | |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 569 | return false; |
| 570 | } |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 571 | |
Sylvestre Ledru | 149e281 | 2013-05-14 23:36:24 +0000 | [diff] [blame] | 572 | /// Process the specified .incbin file by searching for it in the include paths |
Benjamin Kramer | bde9176 | 2012-06-02 10:20:22 +0000 | [diff] [blame] | 573 | /// then just emitting the byte contents of the file to the streamer. This |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 574 | /// returns true on failure. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 575 | bool AsmParser::processIncbinFile(const std::string &Filename) { |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 576 | std::string IncludedFile; |
| 577 | int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile); |
| 578 | if (NewBuf == -1) |
| 579 | return true; |
| 580 | |
Kevin Enderby | ad41ab5 | 2011-12-14 22:34:45 +0000 | [diff] [blame] | 581 | // Pick up the bytes from the file and emit them. |
Rafael Espindola | 64e1af8 | 2013-07-02 15:49:13 +0000 | [diff] [blame] | 582 | getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer()); |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 583 | return false; |
| 584 | } |
| 585 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 586 | void AsmParser::jumpToLoc(SMLoc Loc, int InBuffer) { |
Daniel Dunbar | 40f1d85 | 2012-12-01 01:38:48 +0000 | [diff] [blame] | 587 | if (InBuffer != -1) { |
| 588 | CurBuffer = InBuffer; |
| 589 | } else { |
| 590 | CurBuffer = SrcMgr.FindBufferContainingLoc(Loc); |
| 591 | } |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 592 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer()); |
| 593 | } |
| 594 | |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 595 | const AsmToken &AsmParser::Lex() { |
| 596 | const AsmToken *tok = &Lexer.Lex(); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 597 | |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 598 | if (tok->is(AsmToken::Eof)) { |
| 599 | // If this is the end of an included file, pop the parent file off the |
| 600 | // include stack. |
| 601 | SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer); |
| 602 | if (ParentIncludeLoc != SMLoc()) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 603 | jumpToLoc(ParentIncludeLoc); |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 604 | tok = &Lexer.Lex(); |
| 605 | } |
| 606 | } |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 607 | |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 608 | if (tok->is(AsmToken::Error)) |
Daniel Dunbar | d8a1845 | 2010-07-18 18:31:45 +0000 | [diff] [blame] | 609 | Error(Lexer.getErrLoc(), Lexer.getErr()); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 610 | |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 611 | return *tok; |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 612 | } |
| 613 | |
Chris Lattner | 3b21e4d | 2010-04-05 23:15:42 +0000 | [diff] [blame] | 614 | bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) { |
Daniel Dunbar | 322fec6 | 2010-03-13 02:20:57 +0000 | [diff] [blame] | 615 | // Create the initial section, if requested. |
Daniel Dunbar | 322fec6 | 2010-03-13 02:20:57 +0000 | [diff] [blame] | 616 | if (!NoInitialTextSection) |
Rafael Espindola | f667d92 | 2010-09-15 21:48:40 +0000 | [diff] [blame] | 617 | Out.InitSections(); |
Daniel Dunbar | 4d7b2e3 | 2009-08-26 22:49:51 +0000 | [diff] [blame] | 618 | |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 619 | // Prime the lexer. |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 620 | Lex(); |
Daniel Dunbar | 43325c4 | 2010-09-09 22:42:56 +0000 | [diff] [blame] | 621 | |
| 622 | HadError = false; |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 623 | AsmCond StartingCondState = TheCondState; |
| 624 | |
Kevin Enderby | 6469fc2 | 2011-11-01 22:27:22 +0000 | [diff] [blame] | 625 | // If we are generating dwarf for assembly source files save the initial text |
| 626 | // section and generate a .file directive. |
| 627 | if (getContext().getGenDwarfForAssembly()) { |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 628 | getContext().setGenDwarfSection(getStreamer().getCurrentSection().first); |
Kevin Enderby | e7739d4 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 629 | MCSymbol *SectionStartSym = getContext().CreateTempSymbol(); |
| 630 | getStreamer().EmitLabel(SectionStartSym); |
| 631 | getContext().setGenDwarfSectionStartSym(SectionStartSym); |
Kevin Enderby | 6469fc2 | 2011-11-01 22:27:22 +0000 | [diff] [blame] | 632 | getStreamer().EmitDwarfFileDirective(getContext().nextGenDwarfFileNumber(), |
Eric Christopher | 906da23 | 2012-12-18 00:31:01 +0000 | [diff] [blame] | 633 | StringRef(), |
| 634 | getContext().getMainFileName()); |
Kevin Enderby | 6469fc2 | 2011-11-01 22:27:22 +0000 | [diff] [blame] | 635 | } |
| 636 | |
Chris Lattner | 73f3611 | 2009-07-02 21:53:43 +0000 | [diff] [blame] | 637 | // While we have input, parse each statement. |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 638 | while (Lexer.isNot(AsmToken::Eof)) { |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 639 | ParseStatementInfo Info; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 640 | if (!parseStatement(Info)) |
| 641 | continue; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 642 | |
Daniel Dunbar | 43325c4 | 2010-09-09 22:42:56 +0000 | [diff] [blame] | 643 | // We had an error, validate that one was emitted and recover by skipping to |
| 644 | // the next line. |
| 645 | assert(HadError && "Parse statement returned an error, but none emitted!"); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 646 | eatToEndOfStatement(); |
Chris Lattner | 73f3611 | 2009-07-02 21:53:43 +0000 | [diff] [blame] | 647 | } |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 648 | |
| 649 | if (TheCondState.TheCond != StartingCondState.TheCond || |
| 650 | TheCondState.Ignore != StartingCondState.Ignore) |
| 651 | return TokError("unmatched .ifs or .elses"); |
Kevin Enderby | e5930f1 | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 652 | |
| 653 | // Check to see there are no empty DwarfFile slots. |
Manman Ren | 5ce24ff | 2013-03-12 20:17:00 +0000 | [diff] [blame] | 654 | const SmallVectorImpl<MCDwarfFile *> &MCDwarfFiles = |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 655 | getContext().getMCDwarfFiles(); |
Kevin Enderby | e5930f1 | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 656 | for (unsigned i = 1; i < MCDwarfFiles.size(); i++) { |
Daniel Dunbar | 43325c4 | 2010-09-09 22:42:56 +0000 | [diff] [blame] | 657 | if (!MCDwarfFiles[i]) |
Kevin Enderby | e5930f1 | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 658 | TokError("unassigned file number: " + Twine(i) + " for .file directives"); |
Kevin Enderby | e5930f1 | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 659 | } |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 660 | |
Jim Grosbach | c7e6b8f | 2011-06-15 18:33:28 +0000 | [diff] [blame] | 661 | // Check to see that all assembler local symbols were actually defined. |
| 662 | // Targets that don't do subsections via symbols may not want this, though, |
| 663 | // so conservatively exclude them. Only do this if we're finalizing, though, |
| 664 | // as otherwise we won't necessarilly have seen everything yet. |
| 665 | if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) { |
| 666 | const MCContext::SymbolTable &Symbols = getContext().getSymbols(); |
| 667 | for (MCContext::SymbolTable::const_iterator i = Symbols.begin(), |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 668 | e = Symbols.end(); |
Jim Grosbach | c7e6b8f | 2011-06-15 18:33:28 +0000 | [diff] [blame] | 669 | i != e; ++i) { |
| 670 | MCSymbol *Sym = i->getValue(); |
| 671 | // Variable symbols may not be marked as defined, so check those |
| 672 | // explicitly. If we know it's a variable, we have a definition for |
| 673 | // the purposes of this check. |
| 674 | if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined()) |
| 675 | // FIXME: We would really like to refer back to where the symbol was |
| 676 | // first referenced for a source location. We need to add something |
| 677 | // to track that. Currently, we just point to the end of the file. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 678 | printMessage( |
| 679 | getLexer().getLoc(), SourceMgr::DK_Error, |
| 680 | "assembler local symbol '" + Sym->getName() + "' not defined"); |
Jim Grosbach | c7e6b8f | 2011-06-15 18:33:28 +0000 | [diff] [blame] | 681 | } |
| 682 | } |
| 683 | |
David Peixotto | 308e7e4 | 2013-12-19 18:08:08 +0000 | [diff] [blame] | 684 | // Callback to the target parser in case it needs to do anything. |
| 685 | if (!HadError) |
| 686 | getTargetParser().finishParse(); |
| 687 | |
Chris Lattner | 3b21e4d | 2010-04-05 23:15:42 +0000 | [diff] [blame] | 688 | // Finalize the output stream if there are no errors and if the client wants |
| 689 | // us to. |
Jack Carter | 13d5f75 | 2013-10-04 22:52:31 +0000 | [diff] [blame] | 690 | if (!HadError && !NoFinalize) |
Daniel Dunbar | 9df5f33 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 691 | Out.Finish(); |
| 692 | |
Chris Lattner | 73f3611 | 2009-07-02 21:53:43 +0000 | [diff] [blame] | 693 | return HadError; |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 694 | } |
| 695 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 696 | void AsmParser::checkForValidSection() { |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 697 | if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) { |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 698 | TokError("expected section directive before assembly directive"); |
Rafael Espindola | f144034 | 2014-01-23 23:14:14 +0000 | [diff] [blame] | 699 | Out.InitSections(); |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 700 | } |
| 701 | } |
| 702 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 703 | /// \brief Throw away the rest of the line for testing purposes. |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 704 | void AsmParser::eatToEndOfStatement() { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 705 | while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof)) |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 706 | Lex(); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 707 | |
Chris Lattner | e5074c4 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 708 | // Eat EOL. |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 709 | if (Lexer.is(AsmToken::EndOfStatement)) |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 710 | Lex(); |
Chris Lattner | e5074c4 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 713 | StringRef AsmParser::parseStringToEndOfStatement() { |
Daniel Dunbar | 40a564f | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 714 | const char *Start = getTok().getLoc().getPointer(); |
| 715 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 716 | while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof)) |
Daniel Dunbar | 40a564f | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 717 | Lex(); |
| 718 | |
| 719 | const char *End = getTok().getLoc().getPointer(); |
| 720 | return StringRef(Start, End - Start); |
| 721 | } |
Chris Lattner | 78db362 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 722 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 723 | StringRef AsmParser::parseStringToComma() { |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 724 | const char *Start = getTok().getLoc().getPointer(); |
| 725 | |
| 726 | while (Lexer.isNot(AsmToken::EndOfStatement) && |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 727 | Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof)) |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 728 | Lex(); |
| 729 | |
| 730 | const char *End = getTok().getLoc().getPointer(); |
| 731 | return StringRef(Start, End - Start); |
| 732 | } |
| 733 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 734 | /// \brief Parse a paren expression and return it. |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 735 | /// NOTE: This assumes the leading '(' has already been consumed. |
| 736 | /// |
| 737 | /// parenexpr ::= expr) |
| 738 | /// |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 739 | bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) { |
| 740 | if (parseExpression(Res)) |
| 741 | return true; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 742 | if (Lexer.isNot(AsmToken::RParen)) |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 743 | return TokError("expected ')' in parentheses expression"); |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 744 | EndLoc = Lexer.getTok().getEndLoc(); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 745 | Lex(); |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 746 | return false; |
| 747 | } |
Chris Lattner | 78db362 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 748 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 749 | /// \brief Parse a bracket expression and return it. |
Joerg Sonnenberger | afb36fa | 2011-02-24 21:59:22 +0000 | [diff] [blame] | 750 | /// NOTE: This assumes the leading '[' has already been consumed. |
| 751 | /// |
| 752 | /// bracketexpr ::= expr] |
| 753 | /// |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 754 | bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) { |
| 755 | if (parseExpression(Res)) |
| 756 | return true; |
Joerg Sonnenberger | afb36fa | 2011-02-24 21:59:22 +0000 | [diff] [blame] | 757 | if (Lexer.isNot(AsmToken::RBrac)) |
| 758 | return TokError("expected ']' in brackets expression"); |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 759 | EndLoc = Lexer.getTok().getEndLoc(); |
Joerg Sonnenberger | afb36fa | 2011-02-24 21:59:22 +0000 | [diff] [blame] | 760 | Lex(); |
| 761 | return false; |
| 762 | } |
| 763 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 764 | /// \brief Parse a primary expression and return it. |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 765 | /// primaryexpr ::= (parenexpr |
| 766 | /// primaryexpr ::= symbol |
| 767 | /// primaryexpr ::= number |
Chris Lattner | 6b55cb9 | 2010-04-14 04:40:28 +0000 | [diff] [blame] | 768 | /// primaryexpr ::= '.' |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 769 | /// primaryexpr ::= ~,+,- primaryexpr |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 770 | bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) { |
Kevin Enderby | 0017d8a | 2013-01-22 21:09:20 +0000 | [diff] [blame] | 771 | SMLoc FirstTokenLoc = getLexer().getLoc(); |
| 772 | AsmToken::TokenKind FirstTokenKind = Lexer.getKind(); |
| 773 | switch (FirstTokenKind) { |
Chris Lattner | 78db362 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 774 | default: |
| 775 | return TokError("unknown token in expression"); |
Eric Christopher | 104af06 | 2011-04-12 00:03:13 +0000 | [diff] [blame] | 776 | // If we have an error assume that we've already handled it. |
| 777 | case AsmToken::Error: |
| 778 | return true; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 779 | case AsmToken::Exclaim: |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 780 | Lex(); // Eat the operator. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 781 | if (parsePrimaryExpr(Res, EndLoc)) |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 782 | return true; |
Daniel Dunbar | 940cda2 | 2009-08-31 08:07:44 +0000 | [diff] [blame] | 783 | Res = MCUnaryExpr::CreateLNot(Res, getContext()); |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 784 | return false; |
Daniel Dunbar | 2476432 | 2010-08-24 19:13:42 +0000 | [diff] [blame] | 785 | case AsmToken::Dollar: |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 786 | case AsmToken::At: |
Daniel Dunbar | 9ee33ca | 2009-07-31 21:55:09 +0000 | [diff] [blame] | 787 | case AsmToken::String: |
Daniel Dunbar | d20cda0 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 788 | case AsmToken::Identifier: { |
Daniel Dunbar | 2476432 | 2010-08-24 19:13:42 +0000 | [diff] [blame] | 789 | StringRef Identifier; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 790 | if (parseIdentifier(Identifier)) { |
David Majnemer | 0c58bc6 | 2013-09-25 10:47:21 +0000 | [diff] [blame] | 791 | if (FirstTokenKind == AsmToken::Dollar) { |
| 792 | if (Lexer.getMAI().getDollarIsPC()) { |
| 793 | // This is a '$' reference, which references the current PC. Emit a |
| 794 | // temporary label to the streamer and refer to it. |
| 795 | MCSymbol *Sym = Ctx.CreateTempSymbol(); |
| 796 | Out.EmitLabel(Sym); |
Jack Carter | 721726a | 2013-10-04 21:26:15 +0000 | [diff] [blame] | 797 | Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, |
| 798 | getContext()); |
David Majnemer | 0c58bc6 | 2013-09-25 10:47:21 +0000 | [diff] [blame] | 799 | EndLoc = FirstTokenLoc; |
| 800 | return false; |
| 801 | } else |
| 802 | return Error(FirstTokenLoc, "invalid token in expression"); |
| 803 | return true; |
| 804 | } |
Kevin Enderby | 0017d8a | 2013-01-22 21:09:20 +0000 | [diff] [blame] | 805 | } |
David Peixotto | 8ad70b3 | 2013-12-04 22:43:20 +0000 | [diff] [blame] | 806 | // Parse symbol variant |
| 807 | std::pair<StringRef, StringRef> Split; |
| 808 | if (!MAI.useParensForSymbolVariant()) { |
| 809 | Split = Identifier.split('@'); |
| 810 | } else if (Lexer.is(AsmToken::LParen)) { |
| 811 | Lexer.Lex(); // eat ( |
| 812 | StringRef VName; |
| 813 | parseIdentifier(VName); |
| 814 | if (Lexer.isNot(AsmToken::RParen)) { |
| 815 | return Error(Lexer.getTok().getLoc(), |
| 816 | "unexpected token in variant, expected ')'"); |
| 817 | } |
| 818 | Lexer.Lex(); // eat ) |
| 819 | Split = std::make_pair(Identifier, VName); |
| 820 | } |
Daniel Dunbar | 2476432 | 2010-08-24 19:13:42 +0000 | [diff] [blame] | 821 | |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 822 | EndLoc = SMLoc::getFromPointer(Identifier.end()); |
| 823 | |
Daniel Dunbar | d20cda0 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 824 | // This is a symbol reference. |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 825 | StringRef SymbolName = Identifier; |
| 826 | MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None; |
Hans Wennborg | 69918bc | 2013-10-17 01:13:02 +0000 | [diff] [blame] | 827 | |
Hans Wennborg | 7ddcdc8 | 2013-10-18 02:14:40 +0000 | [diff] [blame] | 828 | // Lookup the symbol variant if used. |
David Peixotto | 8ad70b3 | 2013-12-04 22:43:20 +0000 | [diff] [blame] | 829 | if (Split.second.size()) { |
Hans Wennborg | 7ddcdc8 | 2013-10-18 02:14:40 +0000 | [diff] [blame] | 830 | Variant = MCSymbolRefExpr::getVariantKindForName(Split.second); |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 831 | if (Variant != MCSymbolRefExpr::VK_Invalid) { |
| 832 | SymbolName = Split.first; |
David Peixotto | 8ad70b3 | 2013-12-04 22:43:20 +0000 | [diff] [blame] | 833 | } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) { |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 834 | Variant = MCSymbolRefExpr::VK_None; |
| 835 | } else { |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 836 | Variant = MCSymbolRefExpr::VK_None; |
Saleem Abdulrasool | a25e1e4 | 2014-01-26 22:29:43 +0000 | [diff] [blame] | 837 | return Error(SMLoc::getFromPointer(Split.second.begin()), |
| 838 | "invalid variant '" + Split.second + "'"); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 839 | } |
| 840 | } |
Daniel Dunbar | 5599256 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 841 | |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 842 | MCSymbol *Sym = getContext().GetOrCreateSymbol(SymbolName); |
| 843 | |
Daniel Dunbar | d20cda0 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 844 | // If this is an absolute variable reference, substitute it now to preserve |
| 845 | // semantics in the face of reassignment. |
Daniel Dunbar | 7a989da | 2010-05-05 17:41:00 +0000 | [diff] [blame] | 846 | if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) { |
Daniel Dunbar | 5599256 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 847 | if (Variant) |
Daniel Dunbar | 8a3c3f2 | 2010-11-08 17:53:02 +0000 | [diff] [blame] | 848 | return Error(EndLoc, "unexpected modifier on variable reference"); |
Daniel Dunbar | 5599256 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 849 | |
Daniel Dunbar | 7a989da | 2010-05-05 17:41:00 +0000 | [diff] [blame] | 850 | Res = Sym->getVariableValue(); |
Daniel Dunbar | d20cda0 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 851 | return false; |
| 852 | } |
| 853 | |
| 854 | // Otherwise create a symbol ref. |
Daniel Dunbar | 5599256 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 855 | Res = MCSymbolRefExpr::Create(Sym, Variant, getContext()); |
Chris Lattner | 78db362 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 856 | return false; |
Daniel Dunbar | d20cda0 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 857 | } |
David Woodhouse | f42a666 | 2014-02-01 16:20:54 +0000 | [diff] [blame] | 858 | case AsmToken::BigNum: |
| 859 | return TokError("literal value out of range for directive"); |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 860 | case AsmToken::Integer: { |
| 861 | SMLoc Loc = getTok().getLoc(); |
| 862 | int64_t IntVal = getTok().getIntVal(); |
| 863 | Res = MCConstantExpr::Create(IntVal, getContext()); |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 864 | EndLoc = Lexer.getTok().getEndLoc(); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 865 | Lex(); // Eat token. |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 866 | // Look for 'b' or 'f' following an Integer as a directional label |
| 867 | if (Lexer.getKind() == AsmToken::Identifier) { |
| 868 | StringRef IDVal = getTok().getString(); |
Ulrich Weigand | d412098 | 2013-06-20 16:24:17 +0000 | [diff] [blame] | 869 | // Lookup the symbol variant if used. |
| 870 | std::pair<StringRef, StringRef> Split = IDVal.split('@'); |
| 871 | MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None; |
| 872 | if (Split.first.size() != IDVal.size()) { |
| 873 | Variant = MCSymbolRefExpr::getVariantKindForName(Split.second); |
| 874 | if (Variant == MCSymbolRefExpr::VK_Invalid) { |
| 875 | Variant = MCSymbolRefExpr::VK_None; |
| 876 | return TokError("invalid variant '" + Split.second + "'"); |
| 877 | } |
Vladimir Medic | 9bad0d33 | 2013-08-20 13:33:18 +0000 | [diff] [blame] | 878 | IDVal = Split.first; |
Ulrich Weigand | d412098 | 2013-06-20 16:24:17 +0000 | [diff] [blame] | 879 | } |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 880 | if (IDVal == "f" || IDVal == "b") { |
| 881 | MCSymbol *Sym = |
| 882 | Ctx.GetDirectionalLocalSymbol(IntVal, IDVal == "f" ? 1 : 0); |
Ulrich Weigand | d412098 | 2013-06-20 16:24:17 +0000 | [diff] [blame] | 883 | Res = MCSymbolRefExpr::Create(Sym, Variant, getContext()); |
Benjamin Kramer | c7eda3e | 2012-05-12 16:52:21 +0000 | [diff] [blame] | 884 | if (IDVal == "b" && Sym->isUndefined()) |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 885 | return Error(Loc, "invalid reference to undefined symbol"); |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 886 | EndLoc = Lexer.getTok().getEndLoc(); |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 887 | Lex(); // Eat identifier. |
| 888 | } |
| 889 | } |
Chris Lattner | 78db362 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 890 | return false; |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 891 | } |
Bill Wendling | cdbf17b | 2011-01-25 21:26:41 +0000 | [diff] [blame] | 892 | case AsmToken::Real: { |
| 893 | APFloat RealVal(APFloat::IEEEdouble, getTok().getString()); |
Bob Wilson | 813bdf6 | 2011-02-03 23:17:47 +0000 | [diff] [blame] | 894 | uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue(); |
Bill Wendling | cdbf17b | 2011-01-25 21:26:41 +0000 | [diff] [blame] | 895 | Res = MCConstantExpr::Create(IntVal, getContext()); |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 896 | EndLoc = Lexer.getTok().getEndLoc(); |
Bill Wendling | cdbf17b | 2011-01-25 21:26:41 +0000 | [diff] [blame] | 897 | Lex(); // Eat token. |
| 898 | return false; |
| 899 | } |
Chris Lattner | 6b55cb9 | 2010-04-14 04:40:28 +0000 | [diff] [blame] | 900 | case AsmToken::Dot: { |
| 901 | // This is a '.' reference, which references the current PC. Emit a |
| 902 | // temporary label to the streamer and refer to it. |
| 903 | MCSymbol *Sym = Ctx.CreateTempSymbol(); |
| 904 | Out.EmitLabel(Sym); |
| 905 | Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext()); |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 906 | EndLoc = Lexer.getTok().getEndLoc(); |
Chris Lattner | 6b55cb9 | 2010-04-14 04:40:28 +0000 | [diff] [blame] | 907 | Lex(); // Eat identifier. |
| 908 | return false; |
| 909 | } |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 910 | case AsmToken::LParen: |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 911 | Lex(); // Eat the '('. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 912 | return parseParenExpr(Res, EndLoc); |
Joerg Sonnenberger | afb36fa | 2011-02-24 21:59:22 +0000 | [diff] [blame] | 913 | case AsmToken::LBrac: |
| 914 | if (!PlatformParser->HasBracketExpressions()) |
| 915 | return TokError("brackets expression not supported on this target"); |
| 916 | Lex(); // Eat the '['. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 917 | return parseBracketExpr(Res, EndLoc); |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 918 | case AsmToken::Minus: |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 919 | Lex(); // Eat the operator. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 920 | if (parsePrimaryExpr(Res, EndLoc)) |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 921 | return true; |
Daniel Dunbar | 940cda2 | 2009-08-31 08:07:44 +0000 | [diff] [blame] | 922 | Res = MCUnaryExpr::CreateMinus(Res, getContext()); |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 923 | return false; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 924 | case AsmToken::Plus: |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 925 | Lex(); // Eat the operator. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 926 | if (parsePrimaryExpr(Res, EndLoc)) |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 927 | return true; |
Daniel Dunbar | 940cda2 | 2009-08-31 08:07:44 +0000 | [diff] [blame] | 928 | Res = MCUnaryExpr::CreatePlus(Res, getContext()); |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 929 | return false; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 930 | case AsmToken::Tilde: |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 931 | Lex(); // Eat the operator. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 932 | if (parsePrimaryExpr(Res, EndLoc)) |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 933 | return true; |
Daniel Dunbar | 940cda2 | 2009-08-31 08:07:44 +0000 | [diff] [blame] | 934 | Res = MCUnaryExpr::CreateNot(Res, getContext()); |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 935 | return false; |
Chris Lattner | 78db362 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 936 | } |
| 937 | } |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 938 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 939 | bool AsmParser::parseExpression(const MCExpr *&Res) { |
Chris Lattner | e17df0b | 2010-01-15 19:39:23 +0000 | [diff] [blame] | 940 | SMLoc EndLoc; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 941 | return parseExpression(Res, EndLoc); |
Chris Lattner | 528d00b | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 942 | } |
| 943 | |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 944 | const MCExpr * |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 945 | AsmParser::applyModifierToExpr(const MCExpr *E, |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 946 | MCSymbolRefExpr::VariantKind Variant) { |
Joerg Sonnenberger | b822af4 | 2013-08-27 20:23:19 +0000 | [diff] [blame] | 947 | // Ask the target implementation about this expression first. |
| 948 | const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx); |
| 949 | if (NewE) |
| 950 | return NewE; |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 951 | // Recurse over the given expression, rebuilding it to apply the given variant |
| 952 | // if there is exactly one symbol. |
| 953 | switch (E->getKind()) { |
| 954 | case MCExpr::Target: |
| 955 | case MCExpr::Constant: |
| 956 | return 0; |
| 957 | |
| 958 | case MCExpr::SymbolRef: { |
| 959 | const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E); |
| 960 | |
| 961 | if (SRE->getKind() != MCSymbolRefExpr::VK_None) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 962 | TokError("invalid variant on expression '" + getTok().getIdentifier() + |
| 963 | "' (already modified)"); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 964 | return E; |
| 965 | } |
| 966 | |
| 967 | return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext()); |
| 968 | } |
| 969 | |
| 970 | case MCExpr::Unary: { |
| 971 | const MCUnaryExpr *UE = cast<MCUnaryExpr>(E); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 972 | const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 973 | if (!Sub) |
| 974 | return 0; |
| 975 | return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext()); |
| 976 | } |
| 977 | |
| 978 | case MCExpr::Binary: { |
| 979 | const MCBinaryExpr *BE = cast<MCBinaryExpr>(E); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 980 | const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant); |
| 981 | const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 982 | |
| 983 | if (!LHS && !RHS) |
| 984 | return 0; |
| 985 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 986 | if (!LHS) |
| 987 | LHS = BE->getLHS(); |
| 988 | if (!RHS) |
| 989 | RHS = BE->getRHS(); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 990 | |
| 991 | return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext()); |
| 992 | } |
| 993 | } |
Daniel Dunbar | baad46c | 2010-09-17 16:34:24 +0000 | [diff] [blame] | 994 | |
Craig Topper | a2886c2 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 995 | llvm_unreachable("Invalid expression kind!"); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 996 | } |
| 997 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 998 | /// \brief Parse an expression and return it. |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 999 | /// |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1000 | /// expr ::= expr &&,|| expr -> lowest. |
| 1001 | /// expr ::= expr |,^,&,! expr |
| 1002 | /// expr ::= expr ==,!=,<>,<,<=,>,>= expr |
| 1003 | /// expr ::= expr <<,>> expr |
| 1004 | /// expr ::= expr +,- expr |
| 1005 | /// expr ::= expr *,/,% expr -> highest. |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 1006 | /// expr ::= primaryexpr |
| 1007 | /// |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1008 | bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) { |
Daniel Dunbar | d0c6d36 | 2010-02-13 01:28:07 +0000 | [diff] [blame] | 1009 | // Parse the expression. |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1010 | Res = 0; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1011 | if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc)) |
Daniel Dunbar | d0c6d36 | 2010-02-13 01:28:07 +0000 | [diff] [blame] | 1012 | return true; |
| 1013 | |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1014 | // As a special case, we support 'a op b @ modifier' by rewriting the |
| 1015 | // expression to include the modifier. This is inefficient, but in general we |
| 1016 | // expect users to use 'a@modifier op b'. |
| 1017 | if (Lexer.getKind() == AsmToken::At) { |
| 1018 | Lex(); |
| 1019 | |
| 1020 | if (Lexer.isNot(AsmToken::Identifier)) |
| 1021 | return TokError("unexpected symbol modifier following '@'"); |
| 1022 | |
| 1023 | MCSymbolRefExpr::VariantKind Variant = |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1024 | MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier()); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1025 | if (Variant == MCSymbolRefExpr::VK_Invalid) |
| 1026 | return TokError("invalid variant '" + getTok().getIdentifier() + "'"); |
| 1027 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1028 | const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1029 | if (!ModifiedRes) { |
| 1030 | return TokError("invalid modifier '" + getTok().getIdentifier() + |
| 1031 | "' (no symbols present)"); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1032 | } |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1033 | |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1034 | Res = ModifiedRes; |
| 1035 | Lex(); |
| 1036 | } |
| 1037 | |
Daniel Dunbar | d0c6d36 | 2010-02-13 01:28:07 +0000 | [diff] [blame] | 1038 | // Try to constant fold it up front, if possible. |
| 1039 | int64_t Value; |
| 1040 | if (Res->EvaluateAsAbsolute(Value)) |
| 1041 | Res = MCConstantExpr::Create(Value, getContext()); |
| 1042 | |
| 1043 | return false; |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 1044 | } |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1045 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1046 | bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) { |
Chris Lattner | 807a3bc | 2010-01-24 01:07:33 +0000 | [diff] [blame] | 1047 | Res = 0; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1048 | return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc); |
Daniel Dunbar | 7c82d56 | 2009-08-31 08:08:17 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1051 | bool AsmParser::parseAbsoluteExpression(int64_t &Res) { |
Daniel Dunbar | f363645 | 2009-08-31 08:07:22 +0000 | [diff] [blame] | 1052 | const MCExpr *Expr; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1053 | |
Daniel Dunbar | 75630b3 | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 1054 | SMLoc StartLoc = Lexer.getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1055 | if (parseExpression(Expr)) |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1056 | return true; |
| 1057 | |
Daniel Dunbar | c3bd60e | 2009-10-16 01:57:52 +0000 | [diff] [blame] | 1058 | if (!Expr->EvaluateAsAbsolute(Res)) |
Daniel Dunbar | 75630b3 | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 1059 | return Error(StartLoc, "expected absolute expression"); |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1060 | |
| 1061 | return false; |
| 1062 | } |
| 1063 | |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1064 | static unsigned getBinOpPrecedence(AsmToken::TokenKind K, |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1065 | MCBinaryExpr::Opcode &Kind) { |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1066 | switch (K) { |
Daniel Dunbar | 940cda2 | 2009-08-31 08:07:44 +0000 | [diff] [blame] | 1067 | default: |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1068 | return 0; // not a binop. |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1069 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1070 | // Lowest Precedence: &&, || |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1071 | case AsmToken::AmpAmp: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1072 | Kind = MCBinaryExpr::LAnd; |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1073 | return 1; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1074 | case AsmToken::PipePipe: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1075 | Kind = MCBinaryExpr::LOr; |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1076 | return 1; |
| 1077 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1078 | // Low Precedence: |, &, ^ |
| 1079 | // |
| 1080 | // FIXME: gas seems to support '!' as an infix operator? |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1081 | case AsmToken::Pipe: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1082 | Kind = MCBinaryExpr::Or; |
Chris Lattner | 2bb9504d | 2010-09-22 05:05:16 +0000 | [diff] [blame] | 1083 | return 2; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1084 | case AsmToken::Caret: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1085 | Kind = MCBinaryExpr::Xor; |
Chris Lattner | 2bb9504d | 2010-09-22 05:05:16 +0000 | [diff] [blame] | 1086 | return 2; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1087 | case AsmToken::Amp: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1088 | Kind = MCBinaryExpr::And; |
Chris Lattner | 2bb9504d | 2010-09-22 05:05:16 +0000 | [diff] [blame] | 1089 | return 2; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1090 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1091 | // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >= |
Chris Lattner | 2bb9504d | 2010-09-22 05:05:16 +0000 | [diff] [blame] | 1092 | case AsmToken::EqualEqual: |
| 1093 | Kind = MCBinaryExpr::EQ; |
| 1094 | return 3; |
| 1095 | case AsmToken::ExclaimEqual: |
| 1096 | case AsmToken::LessGreater: |
| 1097 | Kind = MCBinaryExpr::NE; |
| 1098 | return 3; |
| 1099 | case AsmToken::Less: |
| 1100 | Kind = MCBinaryExpr::LT; |
| 1101 | return 3; |
| 1102 | case AsmToken::LessEqual: |
| 1103 | Kind = MCBinaryExpr::LTE; |
| 1104 | return 3; |
| 1105 | case AsmToken::Greater: |
| 1106 | Kind = MCBinaryExpr::GT; |
| 1107 | return 3; |
| 1108 | case AsmToken::GreaterEqual: |
| 1109 | Kind = MCBinaryExpr::GTE; |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1110 | return 3; |
| 1111 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1112 | // Intermediate Precedence: <<, >> |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1113 | case AsmToken::LessLess: |
| 1114 | Kind = MCBinaryExpr::Shl; |
| 1115 | return 4; |
| 1116 | case AsmToken::GreaterGreater: |
| 1117 | Kind = MCBinaryExpr::Shr; |
| 1118 | return 4; |
| 1119 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1120 | // High Intermediate Precedence: +, - |
Daniel Dunbar | b3a48f3 | 2010-10-25 20:18:56 +0000 | [diff] [blame] | 1121 | case AsmToken::Plus: |
| 1122 | Kind = MCBinaryExpr::Add; |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1123 | return 5; |
Daniel Dunbar | b3a48f3 | 2010-10-25 20:18:56 +0000 | [diff] [blame] | 1124 | case AsmToken::Minus: |
| 1125 | Kind = MCBinaryExpr::Sub; |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1126 | return 5; |
Daniel Dunbar | b3a48f3 | 2010-10-25 20:18:56 +0000 | [diff] [blame] | 1127 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1128 | // Highest Precedence: *, /, % |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1129 | case AsmToken::Star: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1130 | Kind = MCBinaryExpr::Mul; |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1131 | return 6; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1132 | case AsmToken::Slash: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1133 | Kind = MCBinaryExpr::Div; |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1134 | return 6; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1135 | case AsmToken::Percent: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1136 | Kind = MCBinaryExpr::Mod; |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1137 | return 6; |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1138 | } |
| 1139 | } |
| 1140 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1141 | /// \brief Parse all binary operators with precedence >= 'Precedence'. |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1142 | /// Res contains the LHS of the expression on input. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1143 | bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, |
Chris Lattner | 528d00b | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 1144 | SMLoc &EndLoc) { |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1145 | while (1) { |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1146 | MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add; |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1147 | unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1148 | |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1149 | // If the next token is lower precedence than we are allowed to eat, return |
| 1150 | // successfully with what we ate already. |
| 1151 | if (TokPrec < Precedence) |
| 1152 | return false; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1153 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1154 | Lex(); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1155 | |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1156 | // Eat the next primary expression. |
Daniel Dunbar | f363645 | 2009-08-31 08:07:22 +0000 | [diff] [blame] | 1157 | const MCExpr *RHS; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1158 | if (parsePrimaryExpr(RHS, EndLoc)) |
| 1159 | return true; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1160 | |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1161 | // If BinOp binds less tightly with RHS than the operator after RHS, let |
| 1162 | // the pending operator take RHS as its LHS. |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1163 | MCBinaryExpr::Opcode Dummy; |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1164 | unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1165 | if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc)) |
| 1166 | return true; |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1167 | |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1168 | // Merge LHS and RHS according to operator. |
Daniel Dunbar | 940cda2 | 2009-08-31 08:07:44 +0000 | [diff] [blame] | 1169 | Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext()); |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1170 | } |
| 1171 | } |
| 1172 | |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1173 | /// ParseStatement: |
| 1174 | /// ::= EndOfStatement |
Chris Lattner | e5074c4 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 1175 | /// ::= Label* Directive ...Operands... EndOfStatement |
| 1176 | /// ::= Label* Identifier OperandList* EndOfStatement |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1177 | bool AsmParser::parseStatement(ParseStatementInfo &Info) { |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1178 | if (Lexer.is(AsmToken::EndOfStatement)) { |
Daniel Dunbar | 8271d1bb | 2010-05-23 18:36:34 +0000 | [diff] [blame] | 1179 | Out.AddBlankLine(); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1180 | Lex(); |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1181 | return false; |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1182 | } |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1183 | |
Kevin Enderby | fa3c6f1 | 2010-12-24 00:12:02 +0000 | [diff] [blame] | 1184 | // Statements always start with an identifier or are a full line comment. |
Sean Callanan | 936b0d3 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 1185 | AsmToken ID = getTok(); |
Daniel Dunbar | ee4465c | 2009-07-28 16:38:40 +0000 | [diff] [blame] | 1186 | SMLoc IDLoc = ID.getLoc(); |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1187 | StringRef IDVal; |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1188 | int64_t LocalLabelVal = -1; |
Kevin Enderby | fa3c6f1 | 2010-12-24 00:12:02 +0000 | [diff] [blame] | 1189 | // A full line comment is a '#' as the first token. |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1190 | if (Lexer.is(AsmToken::Hash)) |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1191 | return parseCppHashLineFilenameComment(IDLoc); |
Daniel Dunbar | 3f56104 | 2011-03-25 17:47:14 +0000 | [diff] [blame] | 1192 | |
Kevin Enderby | fa3c6f1 | 2010-12-24 00:12:02 +0000 | [diff] [blame] | 1193 | // Allow an integer followed by a ':' as a directional local label. |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1194 | if (Lexer.is(AsmToken::Integer)) { |
| 1195 | LocalLabelVal = getTok().getIntVal(); |
| 1196 | if (LocalLabelVal < 0) { |
| 1197 | if (!TheCondState.Ignore) |
| 1198 | return TokError("unexpected token at start of statement"); |
| 1199 | IDVal = ""; |
Eli Bendersky | 8802471 | 2013-01-16 19:32:36 +0000 | [diff] [blame] | 1200 | } else { |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1201 | IDVal = getTok().getString(); |
| 1202 | Lex(); // Consume the integer token to be used as an identifier token. |
| 1203 | if (Lexer.getKind() != AsmToken::Colon) { |
Duncan Sands | 41b4a6b | 2010-07-12 08:16:59 +0000 | [diff] [blame] | 1204 | if (!TheCondState.Ignore) |
| 1205 | return TokError("unexpected token at start of statement"); |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1206 | } |
| 1207 | } |
Daniel Dunbar | 6f4c942 | 2011-03-25 17:47:17 +0000 | [diff] [blame] | 1208 | } else if (Lexer.is(AsmToken::Dot)) { |
| 1209 | // Treat '.' as a valid identifier in this context. |
| 1210 | Lex(); |
| 1211 | IDVal = "."; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1212 | } else if (parseIdentifier(IDVal)) { |
Chris Lattner | 926885c | 2010-04-17 18:14:27 +0000 | [diff] [blame] | 1213 | if (!TheCondState.Ignore) |
| 1214 | return TokError("unexpected token at start of statement"); |
| 1215 | IDVal = ""; |
| 1216 | } |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1217 | |
Chris Lattner | 926885c | 2010-04-17 18:14:27 +0000 | [diff] [blame] | 1218 | // Handle conditional assembly here before checking for skipping. We |
| 1219 | // have to do this so that .endif isn't skipped in a ".if 0" block for |
| 1220 | // example. |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1221 | StringMap<DirectiveKind>::const_iterator DirKindIt = |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1222 | DirectiveKindMap.find(IDVal); |
| 1223 | DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end()) |
| 1224 | ? DK_NO_DIRECTIVE |
| 1225 | : DirKindIt->getValue(); |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1226 | switch (DirKind) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1227 | default: |
| 1228 | break; |
| 1229 | case DK_IF: |
| 1230 | return parseDirectiveIf(IDLoc); |
| 1231 | case DK_IFB: |
| 1232 | return parseDirectiveIfb(IDLoc, true); |
| 1233 | case DK_IFNB: |
| 1234 | return parseDirectiveIfb(IDLoc, false); |
| 1235 | case DK_IFC: |
| 1236 | return parseDirectiveIfc(IDLoc, true); |
| 1237 | case DK_IFNC: |
| 1238 | return parseDirectiveIfc(IDLoc, false); |
| 1239 | case DK_IFDEF: |
| 1240 | return parseDirectiveIfdef(IDLoc, true); |
| 1241 | case DK_IFNDEF: |
| 1242 | case DK_IFNOTDEF: |
| 1243 | return parseDirectiveIfdef(IDLoc, false); |
| 1244 | case DK_ELSEIF: |
| 1245 | return parseDirectiveElseIf(IDLoc); |
| 1246 | case DK_ELSE: |
| 1247 | return parseDirectiveElse(IDLoc); |
| 1248 | case DK_ENDIF: |
| 1249 | return parseDirectiveEndIf(IDLoc); |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1250 | } |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1251 | |
Eli Bendersky | 8802471 | 2013-01-16 19:32:36 +0000 | [diff] [blame] | 1252 | // Ignore the statement if in the middle of inactive conditional |
| 1253 | // (e.g. ".if 0"). |
Chad Rosier | eda70b3 | 2012-10-20 00:47:08 +0000 | [diff] [blame] | 1254 | if (TheCondState.Ignore) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1255 | eatToEndOfStatement(); |
Chris Lattner | 926885c | 2010-04-17 18:14:27 +0000 | [diff] [blame] | 1256 | return false; |
| 1257 | } |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1258 | |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1259 | // FIXME: Recurse on local labels? |
| 1260 | |
| 1261 | // See what kind of statement we have. |
| 1262 | switch (Lexer.getKind()) { |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1263 | case AsmToken::Colon: { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1264 | checkForValidSection(); |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 1265 | |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1266 | // identifier ':' -> Label. |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1267 | Lex(); |
Daniel Dunbar | ae7ac01 | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 1268 | |
Daniel Dunbar | 6f4c942 | 2011-03-25 17:47:17 +0000 | [diff] [blame] | 1269 | // Diagnose attempt to use '.' as a label. |
| 1270 | if (IDVal == ".") |
| 1271 | return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label"); |
| 1272 | |
Daniel Dunbar | ae7ac01 | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 1273 | // Diagnose attempt to use a variable as a label. |
| 1274 | // |
| 1275 | // FIXME: Diagnostics. Note the location of the definition as a label. |
| 1276 | // FIXME: This doesn't diagnose assignment to a symbol which has been |
| 1277 | // implicitly marked as external. |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1278 | MCSymbol *Sym; |
| 1279 | if (LocalLabelVal == -1) |
Daniel Dunbar | 101c14c | 2010-07-12 19:52:10 +0000 | [diff] [blame] | 1280 | Sym = getContext().GetOrCreateSymbol(IDVal); |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1281 | else |
| 1282 | Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal); |
Daniel Dunbar | deb7ba9 | 2010-05-05 19:01:00 +0000 | [diff] [blame] | 1283 | if (!Sym->isUndefined() || Sym->isVariable()) |
Daniel Dunbar | ae7ac01 | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 1284 | return Error(IDLoc, "invalid symbol redefinition"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1285 | |
Daniel Dunbar | e73b267 | 2009-08-26 22:13:22 +0000 | [diff] [blame] | 1286 | // Emit the label. |
Chad Rosier | f3feab3 | 2013-01-07 20:34:12 +0000 | [diff] [blame] | 1287 | if (!ParsingInlineAsm) |
| 1288 | Out.EmitLabel(Sym); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1289 | |
Kevin Enderby | e7739d4 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 1290 | // If we are generating dwarf for assembly source files then gather the |
Kevin Enderby | f7d7706 | 2012-01-10 21:12:34 +0000 | [diff] [blame] | 1291 | // info to make a dwarf label entry for this label if needed. |
Kevin Enderby | e7739d4 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 1292 | if (getContext().getGenDwarfForAssembly()) |
Kevin Enderby | f7d7706 | 2012-01-10 21:12:34 +0000 | [diff] [blame] | 1293 | MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(), |
| 1294 | IDLoc); |
Kevin Enderby | e7739d4 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 1295 | |
Tim Northover | 1744d0a | 2013-10-25 12:49:50 +0000 | [diff] [blame] | 1296 | getTargetParser().onLabelParsed(Sym); |
| 1297 | |
Daniel Dunbar | 8271d1bb | 2010-05-23 18:36:34 +0000 | [diff] [blame] | 1298 | // Consume any end of statement token, if present, to avoid spurious |
| 1299 | // AddBlankLine calls(). |
| 1300 | if (Lexer.is(AsmToken::EndOfStatement)) { |
| 1301 | Lex(); |
| 1302 | if (Lexer.is(AsmToken::Eof)) |
| 1303 | return false; |
| 1304 | } |
| 1305 | |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 1306 | return false; |
Daniel Dunbar | ae7ac01 | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 1307 | } |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1308 | |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1309 | case AsmToken::Equal: |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1310 | // identifier '=' ... -> assignment statement |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1311 | Lex(); |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1312 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1313 | return parseAssignment(IDVal, true); |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1314 | |
| 1315 | default: // Normal instruction or directive. |
| 1316 | break; |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1317 | } |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 1318 | |
| 1319 | // If macros are enabled, check to see if this is a macro instantiation. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1320 | if (areMacrosEnabled()) |
| 1321 | if (const MCAsmMacro *M = lookupMacro(IDVal)) { |
| 1322 | return handleMacroEntry(M, IDLoc); |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 1323 | } |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 1324 | |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1325 | // Otherwise, we have a normal instruction or directive. |
Vladimir Medic | 9bad0d33 | 2013-08-20 13:33:18 +0000 | [diff] [blame] | 1326 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1327 | // Directives start with "." |
Daniel Dunbar | 6f4c942 | 2011-03-25 17:47:17 +0000 | [diff] [blame] | 1328 | if (IDVal[0] == '.' && IDVal != ".") { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1329 | // There are several entities interested in parsing directives: |
Vladimir Medic | 9bad0d33 | 2013-08-20 13:33:18 +0000 | [diff] [blame] | 1330 | // |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1331 | // 1. The target-specific assembly parser. Some directives are target |
| 1332 | // specific or may potentially behave differently on certain targets. |
| 1333 | // 2. Asm parser extensions. For example, platform-specific parsers |
| 1334 | // (like the ELF parser) register themselves as extensions. |
| 1335 | // 3. The generic directive parser implemented by this class. These are |
| 1336 | // all the directives that behave in a target and platform independent |
| 1337 | // manner, or at least have a default behavior that's shared between |
| 1338 | // all targets and platforms. |
Akira Hatanaka | d359075 | 2012-07-05 19:09:33 +0000 | [diff] [blame] | 1339 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1340 | // First query the target-specific parser. It will return 'true' if it |
| 1341 | // isn't interested in this directive. |
Akira Hatanaka | d359075 | 2012-07-05 19:09:33 +0000 | [diff] [blame] | 1342 | if (!getTargetParser().ParseDirective(ID)) |
| 1343 | return false; |
| 1344 | |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 1345 | // Next, check the extension directive map to see if any extension has |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1346 | // registered itself to parse this directive. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1347 | std::pair<MCAsmParserExtension *, DirectiveHandler> Handler = |
| 1348 | ExtensionDirectiveMap.lookup(IDVal); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1349 | if (Handler.first) |
| 1350 | return (*Handler.second)(Handler.first, IDVal, IDLoc); |
| 1351 | |
| 1352 | // Finally, if no one else is interested in this directive, it must be |
| 1353 | // generic and familiar to this class. |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1354 | switch (DirKind) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1355 | default: |
| 1356 | break; |
| 1357 | case DK_SET: |
| 1358 | case DK_EQU: |
| 1359 | return parseDirectiveSet(IDVal, true); |
| 1360 | case DK_EQUIV: |
| 1361 | return parseDirectiveSet(IDVal, false); |
| 1362 | case DK_ASCII: |
| 1363 | return parseDirectiveAscii(IDVal, false); |
| 1364 | case DK_ASCIZ: |
| 1365 | case DK_STRING: |
| 1366 | return parseDirectiveAscii(IDVal, true); |
| 1367 | case DK_BYTE: |
| 1368 | return parseDirectiveValue(1); |
| 1369 | case DK_SHORT: |
| 1370 | case DK_VALUE: |
| 1371 | case DK_2BYTE: |
| 1372 | return parseDirectiveValue(2); |
| 1373 | case DK_LONG: |
| 1374 | case DK_INT: |
| 1375 | case DK_4BYTE: |
| 1376 | return parseDirectiveValue(4); |
| 1377 | case DK_QUAD: |
| 1378 | case DK_8BYTE: |
| 1379 | return parseDirectiveValue(8); |
David Woodhouse | d6de0d9 | 2014-02-01 16:20:59 +0000 | [diff] [blame] | 1380 | case DK_OCTA: |
| 1381 | return parseDirectiveOctaValue(); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1382 | case DK_SINGLE: |
| 1383 | case DK_FLOAT: |
| 1384 | return parseDirectiveRealValue(APFloat::IEEEsingle); |
| 1385 | case DK_DOUBLE: |
| 1386 | return parseDirectiveRealValue(APFloat::IEEEdouble); |
| 1387 | case DK_ALIGN: { |
| 1388 | bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes(); |
| 1389 | return parseDirectiveAlign(IsPow2, /*ExprSize=*/1); |
| 1390 | } |
| 1391 | case DK_ALIGN32: { |
| 1392 | bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes(); |
| 1393 | return parseDirectiveAlign(IsPow2, /*ExprSize=*/4); |
| 1394 | } |
| 1395 | case DK_BALIGN: |
| 1396 | return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1); |
| 1397 | case DK_BALIGNW: |
| 1398 | return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2); |
| 1399 | case DK_BALIGNL: |
| 1400 | return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4); |
| 1401 | case DK_P2ALIGN: |
| 1402 | return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1); |
| 1403 | case DK_P2ALIGNW: |
| 1404 | return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2); |
| 1405 | case DK_P2ALIGNL: |
| 1406 | return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4); |
| 1407 | case DK_ORG: |
| 1408 | return parseDirectiveOrg(); |
| 1409 | case DK_FILL: |
| 1410 | return parseDirectiveFill(); |
| 1411 | case DK_ZERO: |
| 1412 | return parseDirectiveZero(); |
| 1413 | case DK_EXTERN: |
| 1414 | eatToEndOfStatement(); // .extern is the default, ignore it. |
| 1415 | return false; |
| 1416 | case DK_GLOBL: |
| 1417 | case DK_GLOBAL: |
| 1418 | return parseDirectiveSymbolAttribute(MCSA_Global); |
| 1419 | case DK_LAZY_REFERENCE: |
| 1420 | return parseDirectiveSymbolAttribute(MCSA_LazyReference); |
| 1421 | case DK_NO_DEAD_STRIP: |
| 1422 | return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip); |
| 1423 | case DK_SYMBOL_RESOLVER: |
| 1424 | return parseDirectiveSymbolAttribute(MCSA_SymbolResolver); |
| 1425 | case DK_PRIVATE_EXTERN: |
| 1426 | return parseDirectiveSymbolAttribute(MCSA_PrivateExtern); |
| 1427 | case DK_REFERENCE: |
| 1428 | return parseDirectiveSymbolAttribute(MCSA_Reference); |
| 1429 | case DK_WEAK_DEFINITION: |
| 1430 | return parseDirectiveSymbolAttribute(MCSA_WeakDefinition); |
| 1431 | case DK_WEAK_REFERENCE: |
| 1432 | return parseDirectiveSymbolAttribute(MCSA_WeakReference); |
| 1433 | case DK_WEAK_DEF_CAN_BE_HIDDEN: |
| 1434 | return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate); |
| 1435 | case DK_COMM: |
| 1436 | case DK_COMMON: |
| 1437 | return parseDirectiveComm(/*IsLocal=*/false); |
| 1438 | case DK_LCOMM: |
| 1439 | return parseDirectiveComm(/*IsLocal=*/true); |
| 1440 | case DK_ABORT: |
| 1441 | return parseDirectiveAbort(); |
| 1442 | case DK_INCLUDE: |
| 1443 | return parseDirectiveInclude(); |
| 1444 | case DK_INCBIN: |
| 1445 | return parseDirectiveIncbin(); |
| 1446 | case DK_CODE16: |
| 1447 | case DK_CODE16GCC: |
| 1448 | return TokError(Twine(IDVal) + " not supported yet"); |
| 1449 | case DK_REPT: |
Saleem Abdulrasool | d743d0a | 2013-12-28 05:54:33 +0000 | [diff] [blame] | 1450 | return parseDirectiveRept(IDLoc, IDVal); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1451 | case DK_IRP: |
| 1452 | return parseDirectiveIrp(IDLoc); |
| 1453 | case DK_IRPC: |
| 1454 | return parseDirectiveIrpc(IDLoc); |
| 1455 | case DK_ENDR: |
| 1456 | return parseDirectiveEndr(IDLoc); |
| 1457 | case DK_BUNDLE_ALIGN_MODE: |
| 1458 | return parseDirectiveBundleAlignMode(); |
| 1459 | case DK_BUNDLE_LOCK: |
| 1460 | return parseDirectiveBundleLock(); |
| 1461 | case DK_BUNDLE_UNLOCK: |
| 1462 | return parseDirectiveBundleUnlock(); |
| 1463 | case DK_SLEB128: |
| 1464 | return parseDirectiveLEB128(true); |
| 1465 | case DK_ULEB128: |
| 1466 | return parseDirectiveLEB128(false); |
| 1467 | case DK_SPACE: |
| 1468 | case DK_SKIP: |
| 1469 | return parseDirectiveSpace(IDVal); |
| 1470 | case DK_FILE: |
| 1471 | return parseDirectiveFile(IDLoc); |
| 1472 | case DK_LINE: |
| 1473 | return parseDirectiveLine(); |
| 1474 | case DK_LOC: |
| 1475 | return parseDirectiveLoc(); |
| 1476 | case DK_STABS: |
| 1477 | return parseDirectiveStabs(); |
| 1478 | case DK_CFI_SECTIONS: |
| 1479 | return parseDirectiveCFISections(); |
| 1480 | case DK_CFI_STARTPROC: |
| 1481 | return parseDirectiveCFIStartProc(); |
| 1482 | case DK_CFI_ENDPROC: |
| 1483 | return parseDirectiveCFIEndProc(); |
| 1484 | case DK_CFI_DEF_CFA: |
| 1485 | return parseDirectiveCFIDefCfa(IDLoc); |
| 1486 | case DK_CFI_DEF_CFA_OFFSET: |
| 1487 | return parseDirectiveCFIDefCfaOffset(); |
| 1488 | case DK_CFI_ADJUST_CFA_OFFSET: |
| 1489 | return parseDirectiveCFIAdjustCfaOffset(); |
| 1490 | case DK_CFI_DEF_CFA_REGISTER: |
| 1491 | return parseDirectiveCFIDefCfaRegister(IDLoc); |
| 1492 | case DK_CFI_OFFSET: |
| 1493 | return parseDirectiveCFIOffset(IDLoc); |
| 1494 | case DK_CFI_REL_OFFSET: |
| 1495 | return parseDirectiveCFIRelOffset(IDLoc); |
| 1496 | case DK_CFI_PERSONALITY: |
| 1497 | return parseDirectiveCFIPersonalityOrLsda(true); |
| 1498 | case DK_CFI_LSDA: |
| 1499 | return parseDirectiveCFIPersonalityOrLsda(false); |
| 1500 | case DK_CFI_REMEMBER_STATE: |
| 1501 | return parseDirectiveCFIRememberState(); |
| 1502 | case DK_CFI_RESTORE_STATE: |
| 1503 | return parseDirectiveCFIRestoreState(); |
| 1504 | case DK_CFI_SAME_VALUE: |
| 1505 | return parseDirectiveCFISameValue(IDLoc); |
| 1506 | case DK_CFI_RESTORE: |
| 1507 | return parseDirectiveCFIRestore(IDLoc); |
| 1508 | case DK_CFI_ESCAPE: |
| 1509 | return parseDirectiveCFIEscape(); |
| 1510 | case DK_CFI_SIGNAL_FRAME: |
| 1511 | return parseDirectiveCFISignalFrame(); |
| 1512 | case DK_CFI_UNDEFINED: |
| 1513 | return parseDirectiveCFIUndefined(IDLoc); |
| 1514 | case DK_CFI_REGISTER: |
| 1515 | return parseDirectiveCFIRegister(IDLoc); |
Venkatraman Govindaraju | 3816d43 | 2013-09-26 14:49:40 +0000 | [diff] [blame] | 1516 | case DK_CFI_WINDOW_SAVE: |
| 1517 | return parseDirectiveCFIWindowSave(); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1518 | case DK_MACROS_ON: |
| 1519 | case DK_MACROS_OFF: |
| 1520 | return parseDirectiveMacrosOnOff(IDVal); |
| 1521 | case DK_MACRO: |
| 1522 | return parseDirectiveMacro(IDLoc); |
| 1523 | case DK_ENDM: |
| 1524 | case DK_ENDMACRO: |
| 1525 | return parseDirectiveEndMacro(IDVal); |
| 1526 | case DK_PURGEM: |
| 1527 | return parseDirectivePurgeMacro(IDLoc); |
Saleem Abdulrasool | 88186c4 | 2013-12-18 02:53:03 +0000 | [diff] [blame] | 1528 | case DK_END: |
| 1529 | return parseDirectiveEnd(IDLoc); |
Eli Friedman | 20b0264 | 2010-07-19 04:17:25 +0000 | [diff] [blame] | 1530 | } |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 1531 | |
Jim Grosbach | 758e0cc | 2012-05-01 18:38:27 +0000 | [diff] [blame] | 1532 | return Error(IDLoc, "unknown directive"); |
Chris Lattner | e5074c4 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 1533 | } |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1534 | |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 1535 | // __asm _emit or __asm __emit |
| 1536 | if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" || |
| 1537 | IDVal == "_EMIT" || IDVal == "__EMIT")) |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1538 | return parseDirectiveMSEmit(IDLoc, Info, IDVal.size()); |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 1539 | |
| 1540 | // __asm align |
| 1541 | if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN")) |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1542 | return parseDirectiveMSAlign(IDLoc, Info); |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 1543 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1544 | checkForValidSection(); |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 1545 | |
Chris Lattner | 7cbfa44 | 2010-05-19 23:34:33 +0000 | [diff] [blame] | 1546 | // Canonicalize the opcode to lower case. |
Eli Bendersky | 8802471 | 2013-01-16 19:32:36 +0000 | [diff] [blame] | 1547 | std::string OpcodeStr = IDVal.lower(); |
Chad Rosier | f0e8720 | 2012-10-25 20:41:34 +0000 | [diff] [blame] | 1548 | ParseInstructionInfo IInfo(Info.AsmRewrites); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1549 | bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, IDLoc, |
Vladimir Medic | 9bad0d33 | 2013-08-20 13:33:18 +0000 | [diff] [blame] | 1550 | Info.ParsedOperands); |
Chad Rosier | 149e8e0 | 2012-12-12 22:45:52 +0000 | [diff] [blame] | 1551 | Info.ParseError = HadError; |
Chris Lattner | e5074c4 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 1552 | |
Daniel Dunbar | 2eca025 | 2010-08-11 06:37:09 +0000 | [diff] [blame] | 1553 | // Dump the parsed representation, if requested. |
| 1554 | if (getShowParsedOperands()) { |
| 1555 | SmallString<256> Str; |
| 1556 | raw_svector_ostream OS(Str); |
| 1557 | OS << "parsed instruction: ["; |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 1558 | for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) { |
Daniel Dunbar | 2eca025 | 2010-08-11 06:37:09 +0000 | [diff] [blame] | 1559 | if (i != 0) |
| 1560 | OS << ", "; |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 1561 | Info.ParsedOperands[i]->print(OS); |
Daniel Dunbar | 2eca025 | 2010-08-11 06:37:09 +0000 | [diff] [blame] | 1562 | } |
| 1563 | OS << "]"; |
| 1564 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1565 | printMessage(IDLoc, SourceMgr::DK_Note, OS.str()); |
Daniel Dunbar | 2eca025 | 2010-08-11 06:37:09 +0000 | [diff] [blame] | 1566 | } |
| 1567 | |
Kevin Enderby | 6469fc2 | 2011-11-01 22:27:22 +0000 | [diff] [blame] | 1568 | // If we are generating dwarf for assembly source files and the current |
| 1569 | // section is the initial text section then generate a .loc directive for |
| 1570 | // the instruction. |
| 1571 | if (!HadError && getContext().getGenDwarfForAssembly() && |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 1572 | getContext().getGenDwarfSection() == |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1573 | getStreamer().getCurrentSection().first) { |
Kevin Enderby | 4eaf8ef | 2012-11-01 17:31:35 +0000 | [diff] [blame] | 1574 | |
Eli Bendersky | 8802471 | 2013-01-16 19:32:36 +0000 | [diff] [blame] | 1575 | unsigned Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer); |
Kevin Enderby | 4eaf8ef | 2012-11-01 17:31:35 +0000 | [diff] [blame] | 1576 | |
Eli Bendersky | 8802471 | 2013-01-16 19:32:36 +0000 | [diff] [blame] | 1577 | // If we previously parsed a cpp hash file line comment then make sure the |
| 1578 | // current Dwarf File is for the CppHashFilename if not then emit the |
| 1579 | // Dwarf File table for it and adjust the line number for the .loc. |
Vladimir Medic | 9bad0d33 | 2013-08-20 13:33:18 +0000 | [diff] [blame] | 1580 | const SmallVectorImpl<MCDwarfFile *> &MCDwarfFiles = |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1581 | getContext().getMCDwarfFiles(); |
Eli Bendersky | 8802471 | 2013-01-16 19:32:36 +0000 | [diff] [blame] | 1582 | if (CppHashFilename.size() != 0) { |
| 1583 | if (MCDwarfFiles[getContext().getGenDwarfFileNumber()]->getName() != |
Kevin Enderby | 4eaf8ef | 2012-11-01 17:31:35 +0000 | [diff] [blame] | 1584 | CppHashFilename) |
Eli Bendersky | 8802471 | 2013-01-16 19:32:36 +0000 | [diff] [blame] | 1585 | getStreamer().EmitDwarfFileDirective( |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1586 | getContext().nextGenDwarfFileNumber(), StringRef(), |
| 1587 | CppHashFilename); |
Kevin Enderby | 4eaf8ef | 2012-11-01 17:31:35 +0000 | [diff] [blame] | 1588 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1589 | // Since SrcMgr.FindLineNumber() is slow and messes up the SourceMgr's |
| 1590 | // cache with the different Loc from the call above we save the last |
| 1591 | // info we queried here with SrcMgr.FindLineNumber(). |
| 1592 | unsigned CppHashLocLineNo; |
| 1593 | if (LastQueryIDLoc == CppHashLoc && LastQueryBuffer == CppHashBuf) |
| 1594 | CppHashLocLineNo = LastQueryLine; |
| 1595 | else { |
| 1596 | CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc, CppHashBuf); |
| 1597 | LastQueryLine = CppHashLocLineNo; |
| 1598 | LastQueryIDLoc = CppHashLoc; |
| 1599 | LastQueryBuffer = CppHashBuf; |
| 1600 | } |
| 1601 | Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo); |
Eli Bendersky | 8802471 | 2013-01-16 19:32:36 +0000 | [diff] [blame] | 1602 | } |
Kevin Enderby | 4eaf8ef | 2012-11-01 17:31:35 +0000 | [diff] [blame] | 1603 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1604 | getStreamer().EmitDwarfLocDirective( |
| 1605 | getContext().getGenDwarfFileNumber(), Line, 0, |
| 1606 | DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0, |
| 1607 | StringRef()); |
Kevin Enderby | 6469fc2 | 2011-11-01 22:27:22 +0000 | [diff] [blame] | 1608 | } |
| 1609 | |
Daniel Dunbar | ce0c1e1 | 2010-05-04 00:33:07 +0000 | [diff] [blame] | 1610 | // If parsing succeeded, match the instruction. |
Chad Rosier | 4996355 | 2012-10-13 00:26:04 +0000 | [diff] [blame] | 1611 | if (!HadError) { |
Chad Rosier | 4996355 | 2012-10-13 00:26:04 +0000 | [diff] [blame] | 1612 | unsigned ErrorInfo; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1613 | HadError = getTargetParser().MatchAndEmitInstruction( |
| 1614 | IDLoc, Info.Opcode, Info.ParsedOperands, Out, ErrorInfo, |
| 1615 | ParsingInlineAsm); |
Chad Rosier | 4996355 | 2012-10-13 00:26:04 +0000 | [diff] [blame] | 1616 | } |
Chris Lattner | f29c0b6 | 2010-01-14 22:21:20 +0000 | [diff] [blame] | 1617 | |
Chris Lattner | a2a9d16 | 2010-09-11 16:18:25 +0000 | [diff] [blame] | 1618 | // Don't skip the rest of the line, the instruction parser is responsible for |
| 1619 | // that. |
| 1620 | return false; |
Chris Lattner | b013345 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 1621 | } |
Chris Lattner | bedf6c2 | 2009-06-24 04:43:34 +0000 | [diff] [blame] | 1622 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1623 | /// eatToEndOfLine uses the Lexer to eat the characters to the end of the line |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1624 | /// since they may not be able to be tokenized to get to the end of line token. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1625 | void AsmParser::eatToEndOfLine() { |
Rafael Espindola | e0d0908 | 2011-10-19 18:48:52 +0000 | [diff] [blame] | 1626 | if (!Lexer.is(AsmToken::EndOfStatement)) |
| 1627 | Lexer.LexUntilEndOfLine(); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1628 | // Eat EOL. |
| 1629 | Lex(); |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1630 | } |
| 1631 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1632 | /// parseCppHashLineFilenameComment as this: |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1633 | /// ::= # number "filename" |
| 1634 | /// or just as a full line comment if it doesn't have a number and a string. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1635 | bool AsmParser::parseCppHashLineFilenameComment(const SMLoc &L) { |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1636 | Lex(); // Eat the hash token. |
| 1637 | |
| 1638 | if (getLexer().isNot(AsmToken::Integer)) { |
| 1639 | // Consume the line since in cases it is not a well-formed line directive, |
| 1640 | // as if were simply a full line comment. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1641 | eatToEndOfLine(); |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1642 | return false; |
| 1643 | } |
| 1644 | |
| 1645 | int64_t LineNumber = getTok().getIntVal(); |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1646 | Lex(); |
| 1647 | |
| 1648 | if (getLexer().isNot(AsmToken::String)) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1649 | eatToEndOfLine(); |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1650 | return false; |
| 1651 | } |
| 1652 | |
| 1653 | StringRef Filename = getTok().getString(); |
| 1654 | // Get rid of the enclosing quotes. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1655 | Filename = Filename.substr(1, Filename.size() - 2); |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1656 | |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1657 | // Save the SMLoc, Filename and LineNumber for later use by diagnostics. |
| 1658 | CppHashLoc = L; |
| 1659 | CppHashFilename = Filename; |
| 1660 | CppHashLineNumber = LineNumber; |
Kevin Enderby | 27121c1 | 2012-11-05 21:55:41 +0000 | [diff] [blame] | 1661 | CppHashBuf = CurBuffer; |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1662 | |
| 1663 | // Ignore any trailing characters, they're just comment. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1664 | eatToEndOfLine(); |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1665 | return false; |
| 1666 | } |
| 1667 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1668 | /// \brief will use the last parsed cpp hash line filename comment |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1669 | /// for the Filename and LineNo if any in the diagnostic. |
| 1670 | void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1671 | const AsmParser *Parser = static_cast<const AsmParser *>(Context); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1672 | raw_ostream &OS = errs(); |
| 1673 | |
| 1674 | const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr(); |
| 1675 | const SMLoc &DiagLoc = Diag.getLoc(); |
| 1676 | int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc); |
| 1677 | int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc); |
| 1678 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1679 | // Like SourceMgr::printMessage() we need to print the include stack if any |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1680 | // before printing the message. |
| 1681 | int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc); |
Benjamin Kramer | 47f5e30 | 2011-10-16 10:48:29 +0000 | [diff] [blame] | 1682 | if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1683 | SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer); |
| 1684 | DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1685 | } |
| 1686 | |
Eric Christopher | a7c3273 | 2012-12-18 00:30:54 +0000 | [diff] [blame] | 1687 | // If we have not parsed a cpp hash line filename comment or the source |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1688 | // manager changed or buffer changed (like in a nested include) then just |
| 1689 | // print the normal diagnostic using its Filename and LineNo. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1690 | if (!Parser->CppHashLineNumber || &DiagSrcMgr != &Parser->SrcMgr || |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1691 | DiagBuf != CppHashBuf) { |
Benjamin Kramer | 47f5e30 | 2011-10-16 10:48:29 +0000 | [diff] [blame] | 1692 | if (Parser->SavedDiagHandler) |
| 1693 | Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext); |
| 1694 | else |
| 1695 | Diag.print(0, OS); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1696 | return; |
| 1697 | } |
| 1698 | |
Eric Christopher | a7c3273 | 2012-12-18 00:30:54 +0000 | [diff] [blame] | 1699 | // Use the CppHashFilename and calculate a line number based on the |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1700 | // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for |
| 1701 | // the diagnostic. |
Jakub Staszak | ec2ffa9 | 2013-09-16 22:03:38 +0000 | [diff] [blame] | 1702 | const std::string &Filename = Parser->CppHashFilename; |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1703 | |
| 1704 | int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf); |
| 1705 | int CppHashLocLineNo = |
| 1706 | Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1707 | int LineNo = |
| 1708 | Parser->CppHashLineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1709 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1710 | SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo, |
| 1711 | Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(), |
Chris Lattner | 7284526 | 2011-10-16 05:47:55 +0000 | [diff] [blame] | 1712 | Diag.getLineContents(), Diag.getRanges()); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1713 | |
Benjamin Kramer | 47f5e30 | 2011-10-16 10:48:29 +0000 | [diff] [blame] | 1714 | if (Parser->SavedDiagHandler) |
| 1715 | Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext); |
| 1716 | else |
| 1717 | NewDiag.print(0, OS); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1718 | } |
| 1719 | |
Rafael Espindola | 2c06448 | 2012-08-21 18:29:30 +0000 | [diff] [blame] | 1720 | // FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The |
| 1721 | // difference being that that function accepts '@' as part of identifiers and |
| 1722 | // we can't do that. AsmLexer.cpp should probably be changed to handle |
| 1723 | // '@' as a special case when needed. |
| 1724 | static bool isIdentifierChar(char c) { |
Guy Benyei | 83c74e9 | 2013-02-12 21:21:59 +0000 | [diff] [blame] | 1725 | return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' || |
| 1726 | c == '.'; |
Rafael Espindola | 2c06448 | 2012-08-21 18:29:30 +0000 | [diff] [blame] | 1727 | } |
| 1728 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 1729 | bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body, |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 1730 | const MCAsmMacroParameters &Parameters, |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1731 | const MCAsmMacroArguments &A, const SMLoc &L) { |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1732 | unsigned NParameters = Parameters.size(); |
| 1733 | if (NParameters != 0 && NParameters != A.size()) |
| 1734 | return Error(L, "Wrong number of arguments"); |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 1735 | |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1736 | // A macro without parameters is handled differently on Darwin: |
| 1737 | // gas accepts no arguments and does no substitutions |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 1738 | while (!Body.empty()) { |
| 1739 | // Scan for the next substitution. |
| 1740 | std::size_t End = Body.size(), Pos = 0; |
| 1741 | for (; Pos != End; ++Pos) { |
| 1742 | // Check for a substitution or escape. |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1743 | if (!NParameters) { |
| 1744 | // This macro has no parameters, look for $0, $1, etc. |
| 1745 | if (Body[Pos] != '$' || Pos + 1 == End) |
| 1746 | continue; |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 1747 | |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1748 | char Next = Body[Pos + 1]; |
Guy Benyei | 83c74e9 | 2013-02-12 21:21:59 +0000 | [diff] [blame] | 1749 | if (Next == '$' || Next == 'n' || |
| 1750 | isdigit(static_cast<unsigned char>(Next))) |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1751 | break; |
| 1752 | } else { |
| 1753 | // This macro has parameters, look for \foo, \bar, etc. |
| 1754 | if (Body[Pos] == '\\' && Pos + 1 != End) |
| 1755 | break; |
| 1756 | } |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 1757 | } |
| 1758 | |
| 1759 | // Add the prefix. |
| 1760 | OS << Body.slice(0, Pos); |
| 1761 | |
| 1762 | // Check if we reached the end. |
| 1763 | if (Pos == End) |
| 1764 | break; |
| 1765 | |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1766 | if (!NParameters) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1767 | switch (Body[Pos + 1]) { |
| 1768 | // $$ => $ |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1769 | case '$': |
| 1770 | OS << '$'; |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 1771 | break; |
| 1772 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1773 | // $n => number of arguments |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1774 | case 'n': |
| 1775 | OS << A.size(); |
| 1776 | break; |
| 1777 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1778 | // $[0-9] => argument |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1779 | default: { |
| 1780 | // Missing arguments are ignored. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1781 | unsigned Index = Body[Pos + 1] - '0'; |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1782 | if (Index >= A.size()) |
| 1783 | break; |
| 1784 | |
| 1785 | // Otherwise substitute with the token values, with spaces eliminated. |
Eli Bendersky | a7b905e | 2013-01-14 19:00:26 +0000 | [diff] [blame] | 1786 | for (MCAsmMacroArgument::const_iterator it = A[Index].begin(), |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1787 | ie = A[Index].end(); |
| 1788 | it != ie; ++it) |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1789 | OS << it->getString(); |
| 1790 | break; |
| 1791 | } |
| 1792 | } |
| 1793 | Pos += 2; |
| 1794 | } else { |
| 1795 | unsigned I = Pos + 1; |
Rafael Espindola | 2c06448 | 2012-08-21 18:29:30 +0000 | [diff] [blame] | 1796 | while (isIdentifierChar(Body[I]) && I + 1 != End) |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1797 | ++I; |
| 1798 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1799 | const char *Begin = Body.data() + Pos + 1; |
| 1800 | StringRef Argument(Begin, I - (Pos + 1)); |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1801 | unsigned Index = 0; |
| 1802 | for (; Index < NParameters; ++Index) |
Preston Gurd | 242ed315 | 2012-09-19 20:29:04 +0000 | [diff] [blame] | 1803 | if (Parameters[Index].first == Argument) |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1804 | break; |
| 1805 | |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1806 | if (Index == NParameters) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1807 | if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')') |
| 1808 | Pos += 3; |
| 1809 | else { |
| 1810 | OS << '\\' << Argument; |
| 1811 | Pos = I; |
| 1812 | } |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1813 | } else { |
Eli Bendersky | a7b905e | 2013-01-14 19:00:26 +0000 | [diff] [blame] | 1814 | for (MCAsmMacroArgument::const_iterator it = A[Index].begin(), |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1815 | ie = A[Index].end(); |
| 1816 | it != ie; ++it) |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1817 | if (it->getKind() == AsmToken::String) |
| 1818 | OS << it->getStringContents(); |
| 1819 | else |
| 1820 | OS << it->getString(); |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1821 | |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1822 | Pos += 1 + Argument.size(); |
| 1823 | } |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1824 | } |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 1825 | // Update the scan point. |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1826 | Body = Body.substr(Pos); |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 1827 | } |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 1828 | |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1829 | return false; |
| 1830 | } |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 1831 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1832 | MacroInstantiation::MacroInstantiation(const MCAsmMacro *M, SMLoc IL, int EB, |
| 1833 | SMLoc EL, MemoryBuffer *I) |
| 1834 | : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitBuffer(EB), |
| 1835 | ExitLoc(EL) {} |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 1836 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1837 | static bool isOperator(AsmToken::TokenKind kind) { |
| 1838 | switch (kind) { |
| 1839 | default: |
| 1840 | return false; |
| 1841 | case AsmToken::Plus: |
| 1842 | case AsmToken::Minus: |
| 1843 | case AsmToken::Tilde: |
| 1844 | case AsmToken::Slash: |
| 1845 | case AsmToken::Star: |
| 1846 | case AsmToken::Dot: |
| 1847 | case AsmToken::Equal: |
| 1848 | case AsmToken::EqualEqual: |
| 1849 | case AsmToken::Pipe: |
| 1850 | case AsmToken::PipePipe: |
| 1851 | case AsmToken::Caret: |
| 1852 | case AsmToken::Amp: |
| 1853 | case AsmToken::AmpAmp: |
| 1854 | case AsmToken::Exclaim: |
| 1855 | case AsmToken::ExclaimEqual: |
| 1856 | case AsmToken::Percent: |
| 1857 | case AsmToken::Less: |
| 1858 | case AsmToken::LessEqual: |
| 1859 | case AsmToken::LessLess: |
| 1860 | case AsmToken::LessGreater: |
| 1861 | case AsmToken::Greater: |
| 1862 | case AsmToken::GreaterEqual: |
| 1863 | case AsmToken::GreaterGreater: |
| 1864 | return true; |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1865 | } |
| 1866 | } |
| 1867 | |
David Majnemer | 1625245 | 2014-01-29 00:07:39 +0000 | [diff] [blame] | 1868 | namespace { |
| 1869 | class AsmLexerSkipSpaceRAII { |
| 1870 | public: |
| 1871 | AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) { |
| 1872 | Lexer.setSkipSpace(SkipSpace); |
| 1873 | } |
| 1874 | |
| 1875 | ~AsmLexerSkipSpaceRAII() { |
| 1876 | Lexer.setSkipSpace(true); |
| 1877 | } |
| 1878 | |
| 1879 | private: |
| 1880 | AsmLexer &Lexer; |
| 1881 | }; |
| 1882 | } |
| 1883 | |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 1884 | bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA) { |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 1885 | unsigned ParenLevel = 0; |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1886 | unsigned AddTokens = 0; |
| 1887 | |
David Majnemer | 1625245 | 2014-01-29 00:07:39 +0000 | [diff] [blame] | 1888 | // Darwin doesn't use spaces to delmit arguments. |
| 1889 | AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin); |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 1890 | |
| 1891 | for (;;) { |
David Majnemer | 1625245 | 2014-01-29 00:07:39 +0000 | [diff] [blame] | 1892 | if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal)) |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 1893 | return TokError("unexpected token in macro instantiation"); |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1894 | |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 1895 | if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1896 | break; |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1897 | |
| 1898 | if (Lexer.is(AsmToken::Space)) { |
| 1899 | Lex(); // Eat spaces |
| 1900 | |
| 1901 | // Spaces can delimit parameters, but could also be part an expression. |
| 1902 | // If the token after a space is an operator, add the token and the next |
| 1903 | // one into this argument |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 1904 | if (!IsDarwin) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1905 | if (isOperator(Lexer.getKind())) { |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1906 | // Check to see whether the token is used as an operator, |
| 1907 | // or part of an identifier |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 1908 | const char *NextChar = getTok().getEndLoc().getPointer(); |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1909 | if (*NextChar == ' ') |
| 1910 | AddTokens = 2; |
| 1911 | } |
| 1912 | |
| 1913 | if (!AddTokens && ParenLevel == 0) { |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1914 | break; |
| 1915 | } |
| 1916 | } |
| 1917 | } |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 1918 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1919 | // handleMacroEntry relies on not advancing the lexer here |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 1920 | // to be able to fill in the remaining default parameter values |
| 1921 | if (Lexer.is(AsmToken::EndOfStatement)) |
| 1922 | break; |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 1923 | |
| 1924 | // Adjust the current parentheses level. |
| 1925 | if (Lexer.is(AsmToken::LParen)) |
| 1926 | ++ParenLevel; |
| 1927 | else if (Lexer.is(AsmToken::RParen) && ParenLevel) |
| 1928 | --ParenLevel; |
| 1929 | |
| 1930 | // Append the token to the current argument list. |
| 1931 | MA.push_back(getTok()); |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1932 | if (AddTokens) |
| 1933 | AddTokens--; |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 1934 | Lex(); |
| 1935 | } |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1936 | |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 1937 | if (ParenLevel != 0) |
Rafael Espindola | 3e5eb42 | 2012-08-21 15:55:04 +0000 | [diff] [blame] | 1938 | return TokError("unbalanced parentheses in macro argument"); |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 1939 | return false; |
| 1940 | } |
| 1941 | |
| 1942 | // Parse the macro instantiation arguments. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1943 | bool AsmParser::parseMacroArguments(const MCAsmMacro *M, |
Vladimir Medic | 9bad0d33 | 2013-08-20 13:33:18 +0000 | [diff] [blame] | 1944 | MCAsmMacroArguments &A) { |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 1945 | const unsigned NParameters = M ? M->Parameters.size() : 0; |
| 1946 | |
| 1947 | // Parse two kinds of macro invocations: |
| 1948 | // - macros defined without any parameters accept an arbitrary number of them |
| 1949 | // - macros defined with parameters accept at most that many of them |
| 1950 | for (unsigned Parameter = 0; !NParameters || Parameter < NParameters; |
| 1951 | ++Parameter) { |
Eli Bendersky | a7b905e | 2013-01-14 19:00:26 +0000 | [diff] [blame] | 1952 | MCAsmMacroArgument MA; |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 1953 | |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 1954 | if (parseMacroArgument(MA)) |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 1955 | return true; |
| 1956 | |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 1957 | if (!MA.empty() || (!NParameters && !Lexer.is(AsmToken::EndOfStatement))) |
Preston Gurd | 242ed315 | 2012-09-19 20:29:04 +0000 | [diff] [blame] | 1958 | A.push_back(MA); |
| 1959 | else if (NParameters) { |
| 1960 | if (!M->Parameters[Parameter].second.empty()) |
| 1961 | A.push_back(M->Parameters[Parameter].second); |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 1962 | else |
| 1963 | A.push_back(MA); |
Preston Gurd | 242ed315 | 2012-09-19 20:29:04 +0000 | [diff] [blame] | 1964 | } |
Jim Grosbach | 20666162 | 2012-07-30 22:44:17 +0000 | [diff] [blame] | 1965 | |
Preston Gurd | 242ed315 | 2012-09-19 20:29:04 +0000 | [diff] [blame] | 1966 | // At the end of the statement, fill in remaining arguments that have |
| 1967 | // default values. If there aren't any, then the next argument is |
| 1968 | // required but missing |
| 1969 | if (Lexer.is(AsmToken::EndOfStatement)) { |
| 1970 | if (NParameters && Parameter < NParameters - 1) { |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 1971 | continue; |
Preston Gurd | 242ed315 | 2012-09-19 20:29:04 +0000 | [diff] [blame] | 1972 | } |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 1973 | return false; |
Preston Gurd | 242ed315 | 2012-09-19 20:29:04 +0000 | [diff] [blame] | 1974 | } |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 1975 | |
| 1976 | if (Lexer.is(AsmToken::Comma)) |
| 1977 | Lex(); |
| 1978 | } |
| 1979 | return TokError("Too many arguments"); |
| 1980 | } |
| 1981 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1982 | const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) { |
| 1983 | StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name); |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 1984 | return (I == MacroMap.end()) ? NULL : I->getValue(); |
| 1985 | } |
| 1986 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1987 | void AsmParser::defineMacro(StringRef Name, const MCAsmMacro &Macro) { |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 1988 | MacroMap[Name] = new MCAsmMacro(Macro); |
| 1989 | } |
| 1990 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1991 | void AsmParser::undefineMacro(StringRef Name) { |
| 1992 | StringMap<MCAsmMacro *>::iterator I = MacroMap.find(Name); |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 1993 | if (I != MacroMap.end()) { |
| 1994 | delete I->getValue(); |
| 1995 | MacroMap.erase(I); |
| 1996 | } |
| 1997 | } |
| 1998 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1999 | bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) { |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2000 | // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate |
| 2001 | // this, although we should protect against infinite loops. |
| 2002 | if (ActiveMacros.size() == 20) |
| 2003 | return TokError("macros cannot be nested more than 20 levels deep"); |
| 2004 | |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 2005 | MCAsmMacroArguments A; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2006 | if (parseMacroArguments(M, A)) |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2007 | return true; |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2008 | |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2009 | // Macro instantiation is lexical, unfortunately. We construct a new buffer |
| 2010 | // to hold the macro body with substitutions. |
| 2011 | SmallString<256> Buf; |
| 2012 | StringRef Body = M->Body; |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 2013 | raw_svector_ostream OS(Buf); |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2014 | |
Rafael Espindola | cb7eadf | 2012-08-08 14:51:03 +0000 | [diff] [blame] | 2015 | if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc())) |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2016 | return true; |
| 2017 | |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 2018 | // We include the .endmacro in the buffer as our cue to exit the macro |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 2019 | // instantiation. |
| 2020 | OS << ".endmacro\n"; |
| 2021 | |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2022 | MemoryBuffer *Instantiation = |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2023 | MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>"); |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2024 | |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2025 | // Create the macro instantiation object and add to the current macro |
| 2026 | // instantiation stack. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2027 | MacroInstantiation *MI = new MacroInstantiation( |
| 2028 | M, NameLoc, CurBuffer, getTok().getLoc(), Instantiation); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2029 | ActiveMacros.push_back(MI); |
| 2030 | |
| 2031 | // Jump to the macro instantiation and prime the lexer. |
| 2032 | CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc()); |
| 2033 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)); |
| 2034 | Lex(); |
| 2035 | |
| 2036 | return false; |
| 2037 | } |
| 2038 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2039 | void AsmParser::handleMacroExit() { |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2040 | // Jump to the EndOfStatement we should return to, and consume it. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2041 | jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2042 | Lex(); |
| 2043 | |
| 2044 | // Pop the instantiation entry. |
| 2045 | delete ActiveMacros.back(); |
| 2046 | ActiveMacros.pop_back(); |
| 2047 | } |
| 2048 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2049 | static bool isUsedIn(const MCSymbol *Sym, const MCExpr *Value) { |
Rafael Espindola | 46c79ef | 2010-11-15 14:40:36 +0000 | [diff] [blame] | 2050 | switch (Value->getKind()) { |
Rafael Espindola | 72f5f17 | 2012-01-28 05:57:00 +0000 | [diff] [blame] | 2051 | case MCExpr::Binary: { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2052 | const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value); |
| 2053 | return isUsedIn(Sym, BE->getLHS()) || isUsedIn(Sym, BE->getRHS()); |
Rafael Espindola | 46c79ef | 2010-11-15 14:40:36 +0000 | [diff] [blame] | 2054 | } |
Rafael Espindola | 72f5f17 | 2012-01-28 05:57:00 +0000 | [diff] [blame] | 2055 | case MCExpr::Target: |
| 2056 | case MCExpr::Constant: |
| 2057 | return false; |
| 2058 | case MCExpr::SymbolRef: { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2059 | const MCSymbol &S = |
| 2060 | static_cast<const MCSymbolRefExpr *>(Value)->getSymbol(); |
Rafael Espindola | 0047258 | 2012-01-28 06:22:14 +0000 | [diff] [blame] | 2061 | if (S.isVariable()) |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2062 | return isUsedIn(Sym, S.getVariableValue()); |
Rafael Espindola | 0047258 | 2012-01-28 06:22:14 +0000 | [diff] [blame] | 2063 | return &S == Sym; |
Rafael Espindola | 72f5f17 | 2012-01-28 05:57:00 +0000 | [diff] [blame] | 2064 | } |
Rafael Espindola | 46c79ef | 2010-11-15 14:40:36 +0000 | [diff] [blame] | 2065 | case MCExpr::Unary: |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2066 | return isUsedIn(Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr()); |
Rafael Espindola | 46c79ef | 2010-11-15 14:40:36 +0000 | [diff] [blame] | 2067 | } |
Benjamin Kramer | 4efe506 | 2012-01-28 15:28:41 +0000 | [diff] [blame] | 2068 | |
| 2069 | llvm_unreachable("Unknown expr kind!"); |
Rafael Espindola | 46c79ef | 2010-11-15 14:40:36 +0000 | [diff] [blame] | 2070 | } |
| 2071 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2072 | bool AsmParser::parseAssignment(StringRef Name, bool allow_redef, |
Jim Grosbach | b7b750d | 2012-09-13 23:11:31 +0000 | [diff] [blame] | 2073 | bool NoDeadStrip) { |
Daniel Dunbar | ae7ac01 | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 2074 | // FIXME: Use better location, we should use proper tokens. |
| 2075 | SMLoc EqualLoc = Lexer.getLoc(); |
| 2076 | |
Daniel Dunbar | 897ffad | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 2077 | const MCExpr *Value; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2078 | if (parseExpression(Value)) |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2079 | return true; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2080 | |
Rafael Espindola | 72f5f17 | 2012-01-28 05:57:00 +0000 | [diff] [blame] | 2081 | // Note: we don't count b as used in "a = b". This is to allow |
| 2082 | // a = b |
| 2083 | // b = c |
Rafael Espindola | 46c79ef | 2010-11-15 14:40:36 +0000 | [diff] [blame] | 2084 | |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 2085 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2086 | return TokError("unexpected token in assignment"); |
| 2087 | |
Daniel Dunbar | 6f4c942 | 2011-03-25 17:47:17 +0000 | [diff] [blame] | 2088 | // Error on assignment to '.'. |
| 2089 | if (Name == ".") { |
| 2090 | return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported " |
| 2091 | "(use '.space' or '.org').)")); |
| 2092 | } |
| 2093 | |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2094 | // Eat the end of statement marker. |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2095 | Lex(); |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2096 | |
Daniel Dunbar | 5f33924 | 2009-10-16 01:57:39 +0000 | [diff] [blame] | 2097 | // Validate that the LHS is allowed to be a variable (either it has not been |
| 2098 | // used as a symbol, or it is an absolute symbol). |
| 2099 | MCSymbol *Sym = getContext().LookupSymbol(Name); |
| 2100 | if (Sym) { |
| 2101 | // Diagnose assignment to a label. |
| 2102 | // |
| 2103 | // FIXME: Diagnostics. Note the location of the definition as a label. |
| 2104 | // FIXME: Diagnose assignment to protected identifier (e.g., register name). |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2105 | if (isUsedIn(Sym, Value)) |
Rafael Espindola | 72f5f17 | 2012-01-28 05:57:00 +0000 | [diff] [blame] | 2106 | return Error(EqualLoc, "Recursive use of '" + Name + "'"); |
| 2107 | else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable()) |
Daniel Dunbar | 9b4a824 | 2010-05-17 17:46:23 +0000 | [diff] [blame] | 2108 | ; // Allow redefinitions of undefined symbols only used in directives. |
Jim Grosbach | 1283317 | 2012-03-20 21:33:21 +0000 | [diff] [blame] | 2109 | else if (Sym->isVariable() && !Sym->isUsed() && allow_redef) |
| 2110 | ; // Allow redefinitions of variables that haven't yet been used. |
Daniel Dunbar | 1bf128e | 2011-04-29 17:53:11 +0000 | [diff] [blame] | 2111 | else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef)) |
Daniel Dunbar | 5f33924 | 2009-10-16 01:57:39 +0000 | [diff] [blame] | 2112 | return Error(EqualLoc, "redefinition of '" + Name + "'"); |
| 2113 | else if (!Sym->isVariable()) |
| 2114 | return Error(EqualLoc, "invalid assignment to '" + Name + "'"); |
Daniel Dunbar | 7a989da | 2010-05-05 17:41:00 +0000 | [diff] [blame] | 2115 | else if (!isa<MCConstantExpr>(Sym->getVariableValue())) |
Daniel Dunbar | 5f33924 | 2009-10-16 01:57:39 +0000 | [diff] [blame] | 2116 | return Error(EqualLoc, "invalid reassignment of non-absolute variable '" + |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2117 | Name + "'"); |
Rafael Espindola | 46c79ef | 2010-11-15 14:40:36 +0000 | [diff] [blame] | 2118 | |
| 2119 | // Don't count these checks as uses. |
| 2120 | Sym->setUsed(false); |
Daniel Dunbar | 5f33924 | 2009-10-16 01:57:39 +0000 | [diff] [blame] | 2121 | } else |
Daniel Dunbar | 101c14c | 2010-07-12 19:52:10 +0000 | [diff] [blame] | 2122 | Sym = getContext().GetOrCreateSymbol(Name); |
Daniel Dunbar | 5f33924 | 2009-10-16 01:57:39 +0000 | [diff] [blame] | 2123 | |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2124 | // FIXME: Handle '.'. |
Daniel Dunbar | ae7ac01 | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 2125 | |
| 2126 | // Do the assignment. |
Daniel Dunbar | b7b2097 | 2009-08-31 08:09:09 +0000 | [diff] [blame] | 2127 | Out.EmitAssignment(Sym, Value); |
Jim Grosbach | b7b750d | 2012-09-13 23:11:31 +0000 | [diff] [blame] | 2128 | if (NoDeadStrip) |
| 2129 | Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip); |
| 2130 | |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2131 | return false; |
| 2132 | } |
| 2133 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2134 | /// parseIdentifier: |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 2135 | /// ::= identifier |
| 2136 | /// ::= string |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2137 | bool AsmParser::parseIdentifier(StringRef &Res) { |
Daniel Dunbar | 3b96ffd | 2010-08-24 18:12:12 +0000 | [diff] [blame] | 2138 | // The assembler has relaxed rules for accepting identifiers, in particular we |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 2139 | // allow things like '.globl $foo' and '.def @feat.00', which would normally be |
| 2140 | // separate tokens. At this level, we have already lexed so we cannot (currently) |
Daniel Dunbar | 3b96ffd | 2010-08-24 18:12:12 +0000 | [diff] [blame] | 2141 | // handle this as a context dependent token, instead we detect adjacent tokens |
| 2142 | // and return the combined identifier. |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 2143 | if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) { |
| 2144 | SMLoc PrefixLoc = getLexer().getLoc(); |
Daniel Dunbar | 3b96ffd | 2010-08-24 18:12:12 +0000 | [diff] [blame] | 2145 | |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 2146 | // Consume the prefix character, and check for a following identifier. |
Daniel Dunbar | 3b96ffd | 2010-08-24 18:12:12 +0000 | [diff] [blame] | 2147 | Lex(); |
| 2148 | if (Lexer.isNot(AsmToken::Identifier)) |
| 2149 | return true; |
| 2150 | |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 2151 | // We have a '$' or '@' followed by an identifier, make sure they are adjacent. |
| 2152 | if (PrefixLoc.getPointer() + 1 != getTok().getLoc().getPointer()) |
Daniel Dunbar | 3b96ffd | 2010-08-24 18:12:12 +0000 | [diff] [blame] | 2153 | return true; |
| 2154 | |
| 2155 | // Construct the joined identifier and consume the token. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2156 | Res = |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 2157 | StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1); |
Daniel Dunbar | 3b96ffd | 2010-08-24 18:12:12 +0000 | [diff] [blame] | 2158 | Lex(); |
| 2159 | return false; |
| 2160 | } |
| 2161 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2162 | if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String)) |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 2163 | return true; |
| 2164 | |
Sean Callanan | 936b0d3 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 2165 | Res = getTok().getIdentifier(); |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 2166 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2167 | Lex(); // Consume the identifier token. |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 2168 | |
| 2169 | return false; |
| 2170 | } |
| 2171 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2172 | /// parseDirectiveSet: |
Nico Weber | 4ada0d9 | 2011-01-28 03:04:41 +0000 | [diff] [blame] | 2173 | /// ::= .equ identifier ',' expression |
| 2174 | /// ::= .equiv identifier ',' expression |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2175 | /// ::= .set identifier ',' expression |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2176 | bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) { |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 2177 | StringRef Name; |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2178 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2179 | if (parseIdentifier(Name)) |
Roman Divacky | 41e6ceb | 2010-10-28 16:57:58 +0000 | [diff] [blame] | 2180 | return TokError("expected identifier after '" + Twine(IDVal) + "'"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2181 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2182 | if (getLexer().isNot(AsmToken::Comma)) |
Roman Divacky | 41e6ceb | 2010-10-28 16:57:58 +0000 | [diff] [blame] | 2183 | return TokError("unexpected token in '" + Twine(IDVal) + "'"); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2184 | Lex(); |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2185 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2186 | return parseAssignment(Name, allow_redef, true); |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2187 | } |
| 2188 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2189 | bool AsmParser::parseEscapedString(std::string &Data) { |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2190 | assert(getLexer().is(AsmToken::String) && "Unexpected current token!"); |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2191 | |
| 2192 | Data = ""; |
Sean Callanan | 936b0d3 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 2193 | StringRef Str = getTok().getStringContents(); |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2194 | for (unsigned i = 0, e = Str.size(); i != e; ++i) { |
| 2195 | if (Str[i] != '\\') { |
| 2196 | Data += Str[i]; |
| 2197 | continue; |
| 2198 | } |
| 2199 | |
| 2200 | // Recognize escaped characters. Note that this escape semantics currently |
| 2201 | // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes. |
| 2202 | ++i; |
| 2203 | if (i == e) |
| 2204 | return TokError("unexpected backslash at end of string"); |
| 2205 | |
| 2206 | // Recognize octal sequences. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2207 | if ((unsigned)(Str[i] - '0') <= 7) { |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2208 | // Consume up to three octal characters. |
| 2209 | unsigned Value = Str[i] - '0'; |
| 2210 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2211 | if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) { |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2212 | ++i; |
| 2213 | Value = Value * 8 + (Str[i] - '0'); |
| 2214 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2215 | if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) { |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2216 | ++i; |
| 2217 | Value = Value * 8 + (Str[i] - '0'); |
| 2218 | } |
| 2219 | } |
| 2220 | |
| 2221 | if (Value > 255) |
| 2222 | return TokError("invalid octal escape sequence (out of range)"); |
| 2223 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2224 | Data += (unsigned char)Value; |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2225 | continue; |
| 2226 | } |
| 2227 | |
| 2228 | // Otherwise recognize individual escapes. |
| 2229 | switch (Str[i]) { |
| 2230 | default: |
| 2231 | // Just reject invalid escape sequences for now. |
| 2232 | return TokError("invalid escape sequence (unrecognized character)"); |
| 2233 | |
| 2234 | case 'b': Data += '\b'; break; |
| 2235 | case 'f': Data += '\f'; break; |
| 2236 | case 'n': Data += '\n'; break; |
| 2237 | case 'r': Data += '\r'; break; |
| 2238 | case 't': Data += '\t'; break; |
| 2239 | case '"': Data += '"'; break; |
| 2240 | case '\\': Data += '\\'; break; |
| 2241 | } |
| 2242 | } |
| 2243 | |
| 2244 | return false; |
| 2245 | } |
| 2246 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2247 | /// parseDirectiveAscii: |
Rafael Espindola | 63760ba | 2010-10-28 20:02:27 +0000 | [diff] [blame] | 2248 | /// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2249 | bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) { |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2250 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2251 | checkForValidSection(); |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 2252 | |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2253 | for (;;) { |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2254 | if (getLexer().isNot(AsmToken::String)) |
Rafael Espindola | 63760ba | 2010-10-28 20:02:27 +0000 | [diff] [blame] | 2255 | return TokError("expected string in '" + Twine(IDVal) + "' directive"); |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2256 | |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2257 | std::string Data; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2258 | if (parseEscapedString(Data)) |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2259 | return true; |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2260 | |
Rafael Espindola | 64e1af8 | 2013-07-02 15:49:13 +0000 | [diff] [blame] | 2261 | getStreamer().EmitBytes(Data); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2262 | if (ZeroTerminated) |
Rafael Espindola | 64e1af8 | 2013-07-02 15:49:13 +0000 | [diff] [blame] | 2263 | getStreamer().EmitBytes(StringRef("\0", 1)); |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2264 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2265 | Lex(); |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2266 | |
| 2267 | if (getLexer().is(AsmToken::EndOfStatement)) |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2268 | break; |
| 2269 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2270 | if (getLexer().isNot(AsmToken::Comma)) |
Rafael Espindola | 63760ba | 2010-10-28 20:02:27 +0000 | [diff] [blame] | 2271 | return TokError("unexpected token in '" + Twine(IDVal) + "' directive"); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2272 | Lex(); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2273 | } |
| 2274 | } |
| 2275 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2276 | Lex(); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2277 | return false; |
| 2278 | } |
| 2279 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2280 | /// parseDirectiveValue |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2281 | /// ::= (.byte | .short | ... ) [ expression (, expression)* ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2282 | bool AsmParser::parseDirectiveValue(unsigned Size) { |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2283 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2284 | checkForValidSection(); |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 2285 | |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2286 | for (;;) { |
Daniel Dunbar | 897ffad | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 2287 | const MCExpr *Value; |
Jim Grosbach | 76346c3 | 2011-06-29 16:05:14 +0000 | [diff] [blame] | 2288 | SMLoc ExprLoc = getLexer().getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2289 | if (parseExpression(Value)) |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2290 | return true; |
| 2291 | |
Daniel Dunbar | 6738a2e | 2010-05-23 18:36:38 +0000 | [diff] [blame] | 2292 | // Special case constant expressions to match code generator. |
Jim Grosbach | 76346c3 | 2011-06-29 16:05:14 +0000 | [diff] [blame] | 2293 | if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) { |
| 2294 | assert(Size <= 8 && "Invalid size"); |
| 2295 | uint64_t IntValue = MCE->getValue(); |
| 2296 | if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue)) |
| 2297 | return Error(ExprLoc, "literal value out of range for directive"); |
Rafael Espindola | 64e1af8 | 2013-07-02 15:49:13 +0000 | [diff] [blame] | 2298 | getStreamer().EmitIntValue(IntValue, Size); |
Jim Grosbach | 76346c3 | 2011-06-29 16:05:14 +0000 | [diff] [blame] | 2299 | } else |
Rafael Espindola | 64e1af8 | 2013-07-02 15:49:13 +0000 | [diff] [blame] | 2300 | getStreamer().EmitValue(Value, Size); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2301 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2302 | if (getLexer().is(AsmToken::EndOfStatement)) |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2303 | break; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2304 | |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2305 | // FIXME: Improve diagnostic. |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2306 | if (getLexer().isNot(AsmToken::Comma)) |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2307 | return TokError("unexpected token in directive"); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2308 | Lex(); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2309 | } |
| 2310 | } |
| 2311 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2312 | Lex(); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2313 | return false; |
| 2314 | } |
| 2315 | |
David Woodhouse | d6de0d9 | 2014-02-01 16:20:59 +0000 | [diff] [blame] | 2316 | /// ParseDirectiveOctaValue |
| 2317 | /// ::= .octa [ hexconstant (, hexconstant)* ] |
| 2318 | bool AsmParser::parseDirectiveOctaValue() { |
| 2319 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2320 | checkForValidSection(); |
| 2321 | |
| 2322 | for (;;) { |
| 2323 | if (Lexer.getKind() == AsmToken::Error) |
| 2324 | return true; |
| 2325 | if (Lexer.getKind() != AsmToken::Integer && |
| 2326 | Lexer.getKind() != AsmToken::BigNum) |
| 2327 | return TokError("unknown token in expression"); |
| 2328 | |
| 2329 | SMLoc ExprLoc = getLexer().getLoc(); |
| 2330 | APInt IntValue = getTok().getAPIntVal(); |
| 2331 | Lex(); |
| 2332 | |
| 2333 | uint64_t hi, lo; |
| 2334 | if (IntValue.isIntN(64)) { |
| 2335 | hi = 0; |
| 2336 | lo = IntValue.getZExtValue(); |
| 2337 | } else if (IntValue.isIntN(128)) { |
David Woodhouse | 6c9a6f9 | 2014-02-01 16:52:33 +0000 | [diff] [blame^] | 2338 | // It might actually have more than 128 bits, but the top ones are zero. |
| 2339 | hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue(); |
David Woodhouse | d6de0d9 | 2014-02-01 16:20:59 +0000 | [diff] [blame] | 2340 | lo = IntValue.getLoBits(64).getZExtValue(); |
| 2341 | } else |
| 2342 | return Error(ExprLoc, "literal value out of range for directive"); |
| 2343 | |
| 2344 | if (MAI.isLittleEndian()) { |
| 2345 | getStreamer().EmitIntValue(lo, 8); |
| 2346 | getStreamer().EmitIntValue(hi, 8); |
| 2347 | } else { |
| 2348 | getStreamer().EmitIntValue(hi, 8); |
| 2349 | getStreamer().EmitIntValue(lo, 8); |
| 2350 | } |
| 2351 | |
| 2352 | if (getLexer().is(AsmToken::EndOfStatement)) |
| 2353 | break; |
| 2354 | |
| 2355 | // FIXME: Improve diagnostic. |
| 2356 | if (getLexer().isNot(AsmToken::Comma)) |
| 2357 | return TokError("unexpected token in directive"); |
| 2358 | Lex(); |
| 2359 | } |
| 2360 | } |
| 2361 | |
| 2362 | Lex(); |
| 2363 | return false; |
| 2364 | } |
| 2365 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2366 | /// parseDirectiveRealValue |
Daniel Dunbar | 2af1653 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 2367 | /// ::= (.single | .double) [ expression (, expression)* ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2368 | bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) { |
Daniel Dunbar | 2af1653 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 2369 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2370 | checkForValidSection(); |
Daniel Dunbar | 2af1653 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 2371 | |
| 2372 | for (;;) { |
| 2373 | // We don't truly support arithmetic on floating point expressions, so we |
| 2374 | // have to manually parse unary prefixes. |
| 2375 | bool IsNeg = false; |
| 2376 | if (getLexer().is(AsmToken::Minus)) { |
| 2377 | Lex(); |
| 2378 | IsNeg = true; |
| 2379 | } else if (getLexer().is(AsmToken::Plus)) |
| 2380 | Lex(); |
| 2381 | |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2382 | if (getLexer().isNot(AsmToken::Integer) && |
Kevin Enderby | 5bbe957 | 2011-03-29 21:11:52 +0000 | [diff] [blame] | 2383 | getLexer().isNot(AsmToken::Real) && |
| 2384 | getLexer().isNot(AsmToken::Identifier)) |
Daniel Dunbar | 2af1653 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 2385 | return TokError("unexpected token in directive"); |
| 2386 | |
| 2387 | // Convert to an APFloat. |
| 2388 | APFloat Value(Semantics); |
Kevin Enderby | 5bbe957 | 2011-03-29 21:11:52 +0000 | [diff] [blame] | 2389 | StringRef IDVal = getTok().getString(); |
| 2390 | if (getLexer().is(AsmToken::Identifier)) { |
| 2391 | if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf")) |
| 2392 | Value = APFloat::getInf(Semantics); |
| 2393 | else if (!IDVal.compare_lower("nan")) |
| 2394 | Value = APFloat::getNaN(Semantics, false, ~0); |
| 2395 | else |
| 2396 | return TokError("invalid floating point literal"); |
| 2397 | } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) == |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2398 | APFloat::opInvalidOp) |
Daniel Dunbar | 2af1653 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 2399 | return TokError("invalid floating point literal"); |
| 2400 | if (IsNeg) |
| 2401 | Value.changeSign(); |
| 2402 | |
| 2403 | // Consume the numeric token. |
| 2404 | Lex(); |
| 2405 | |
| 2406 | // Emit the value as an integer. |
| 2407 | APInt AsInt = Value.bitcastToAPInt(); |
| 2408 | getStreamer().EmitIntValue(AsInt.getLimitedValue(), |
Rafael Espindola | 64e1af8 | 2013-07-02 15:49:13 +0000 | [diff] [blame] | 2409 | AsInt.getBitWidth() / 8); |
Daniel Dunbar | 2af1653 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 2410 | |
| 2411 | if (getLexer().is(AsmToken::EndOfStatement)) |
| 2412 | break; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2413 | |
Daniel Dunbar | 2af1653 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 2414 | if (getLexer().isNot(AsmToken::Comma)) |
| 2415 | return TokError("unexpected token in directive"); |
| 2416 | Lex(); |
| 2417 | } |
| 2418 | } |
| 2419 | |
| 2420 | Lex(); |
| 2421 | return false; |
| 2422 | } |
| 2423 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2424 | /// parseDirectiveZero |
Rafael Espindola | 922e3f4 | 2010-09-16 15:03:59 +0000 | [diff] [blame] | 2425 | /// ::= .zero expression |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2426 | bool AsmParser::parseDirectiveZero() { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2427 | checkForValidSection(); |
Rafael Espindola | 922e3f4 | 2010-09-16 15:03:59 +0000 | [diff] [blame] | 2428 | |
| 2429 | int64_t NumBytes; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2430 | if (parseAbsoluteExpression(NumBytes)) |
Rafael Espindola | 922e3f4 | 2010-09-16 15:03:59 +0000 | [diff] [blame] | 2431 | return true; |
| 2432 | |
Rafael Espindola | b91bac6 | 2010-10-05 19:42:57 +0000 | [diff] [blame] | 2433 | int64_t Val = 0; |
| 2434 | if (getLexer().is(AsmToken::Comma)) { |
| 2435 | Lex(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2436 | if (parseAbsoluteExpression(Val)) |
Rafael Espindola | b91bac6 | 2010-10-05 19:42:57 +0000 | [diff] [blame] | 2437 | return true; |
| 2438 | } |
| 2439 | |
Rafael Espindola | 922e3f4 | 2010-09-16 15:03:59 +0000 | [diff] [blame] | 2440 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 2441 | return TokError("unexpected token in '.zero' directive"); |
| 2442 | |
| 2443 | Lex(); |
| 2444 | |
Rafael Espindola | 64e1af8 | 2013-07-02 15:49:13 +0000 | [diff] [blame] | 2445 | getStreamer().EmitFill(NumBytes, Val); |
Rafael Espindola | 922e3f4 | 2010-09-16 15:03:59 +0000 | [diff] [blame] | 2446 | |
| 2447 | return false; |
| 2448 | } |
| 2449 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2450 | /// parseDirectiveFill |
Roman Divacky | e33098f | 2013-09-24 17:44:41 +0000 | [diff] [blame] | 2451 | /// ::= .fill expression [ , expression [ , expression ] ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2452 | bool AsmParser::parseDirectiveFill() { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2453 | checkForValidSection(); |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 2454 | |
David Majnemer | 522d3db | 2014-02-01 07:19:38 +0000 | [diff] [blame] | 2455 | SMLoc RepeatLoc = getLexer().getLoc(); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2456 | int64_t NumValues; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2457 | if (parseAbsoluteExpression(NumValues)) |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2458 | return true; |
| 2459 | |
David Majnemer | 522d3db | 2014-02-01 07:19:38 +0000 | [diff] [blame] | 2460 | if (NumValues < 0) { |
| 2461 | Warning(RepeatLoc, |
| 2462 | "'.fill' directive with negative repeat count has no effect"); |
| 2463 | NumValues = 0; |
| 2464 | } |
| 2465 | |
Roman Divacky | e33098f | 2013-09-24 17:44:41 +0000 | [diff] [blame] | 2466 | int64_t FillSize = 1; |
| 2467 | int64_t FillExpr = 0; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2468 | |
David Majnemer | 522d3db | 2014-02-01 07:19:38 +0000 | [diff] [blame] | 2469 | SMLoc SizeLoc, ExprLoc; |
Roman Divacky | e33098f | 2013-09-24 17:44:41 +0000 | [diff] [blame] | 2470 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2471 | if (getLexer().isNot(AsmToken::Comma)) |
| 2472 | return TokError("unexpected token in '.fill' directive"); |
| 2473 | Lex(); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2474 | |
David Majnemer | 522d3db | 2014-02-01 07:19:38 +0000 | [diff] [blame] | 2475 | SizeLoc = getLexer().getLoc(); |
Roman Divacky | e33098f | 2013-09-24 17:44:41 +0000 | [diff] [blame] | 2476 | if (parseAbsoluteExpression(FillSize)) |
| 2477 | return true; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2478 | |
Roman Divacky | e33098f | 2013-09-24 17:44:41 +0000 | [diff] [blame] | 2479 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2480 | if (getLexer().isNot(AsmToken::Comma)) |
| 2481 | return TokError("unexpected token in '.fill' directive"); |
| 2482 | Lex(); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2483 | |
David Majnemer | 522d3db | 2014-02-01 07:19:38 +0000 | [diff] [blame] | 2484 | ExprLoc = getLexer().getLoc(); |
Roman Divacky | e33098f | 2013-09-24 17:44:41 +0000 | [diff] [blame] | 2485 | if (parseAbsoluteExpression(FillExpr)) |
| 2486 | return true; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2487 | |
Roman Divacky | e33098f | 2013-09-24 17:44:41 +0000 | [diff] [blame] | 2488 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 2489 | return TokError("unexpected token in '.fill' directive"); |
| 2490 | |
| 2491 | Lex(); |
| 2492 | } |
| 2493 | } |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2494 | |
David Majnemer | 522d3db | 2014-02-01 07:19:38 +0000 | [diff] [blame] | 2495 | if (FillSize < 0) { |
| 2496 | Warning(SizeLoc, "'.fill' directive with negative size has no effect"); |
| 2497 | NumValues = 0; |
| 2498 | } |
| 2499 | if (FillSize > 8) { |
| 2500 | Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8"); |
| 2501 | FillSize = 8; |
| 2502 | } |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2503 | |
David Majnemer | 522d3db | 2014-02-01 07:19:38 +0000 | [diff] [blame] | 2504 | if (!isUInt<32>(FillExpr) && FillSize > 4) |
| 2505 | Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits"); |
| 2506 | |
| 2507 | int64_t NonZeroFillSize = FillSize > 4 ? 4 : FillSize; |
| 2508 | FillExpr &= ~0ULL >> (64 - NonZeroFillSize * 8); |
| 2509 | |
| 2510 | for (uint64_t i = 0, e = NumValues; i != e; ++i) { |
| 2511 | getStreamer().EmitIntValue(FillExpr, NonZeroFillSize); |
| 2512 | getStreamer().EmitIntValue(0, FillSize - NonZeroFillSize); |
| 2513 | } |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2514 | |
| 2515 | return false; |
| 2516 | } |
Daniel Dunbar | 4a5a561 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 2517 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2518 | /// parseDirectiveOrg |
Daniel Dunbar | 4a5a561 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 2519 | /// ::= .org expression [ , expression ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2520 | bool AsmParser::parseDirectiveOrg() { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2521 | checkForValidSection(); |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 2522 | |
Daniel Dunbar | 897ffad | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 2523 | const MCExpr *Offset; |
Jim Grosbach | b591277 | 2012-01-27 00:37:08 +0000 | [diff] [blame] | 2524 | SMLoc Loc = getTok().getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2525 | if (parseExpression(Offset)) |
Daniel Dunbar | 4a5a561 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 2526 | return true; |
| 2527 | |
| 2528 | // Parse optional fill expression. |
| 2529 | int64_t FillExpr = 0; |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2530 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2531 | if (getLexer().isNot(AsmToken::Comma)) |
Daniel Dunbar | 4a5a561 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 2532 | return TokError("unexpected token in '.org' directive"); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2533 | Lex(); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2534 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2535 | if (parseAbsoluteExpression(FillExpr)) |
Daniel Dunbar | 4a5a561 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 2536 | return true; |
| 2537 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2538 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Daniel Dunbar | 4a5a561 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 2539 | return TokError("unexpected token in '.org' directive"); |
| 2540 | } |
| 2541 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2542 | Lex(); |
Daniel Dunbar | 75630b3 | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 2543 | |
Jim Grosbach | b591277 | 2012-01-27 00:37:08 +0000 | [diff] [blame] | 2544 | // Only limited forms of relocatable expressions are accepted here, it |
| 2545 | // has to be relative to the current section. The streamer will return |
| 2546 | // 'true' if the expression wasn't evaluatable. |
| 2547 | if (getStreamer().EmitValueToOffset(Offset, FillExpr)) |
| 2548 | return Error(Loc, "expected assembly-time absolute expression"); |
Daniel Dunbar | 4a5a561 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 2549 | |
| 2550 | return false; |
| 2551 | } |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2552 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2553 | /// parseDirectiveAlign |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2554 | /// ::= {.align, ...} expression [ , expression [ , expression ]] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2555 | bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2556 | checkForValidSection(); |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 2557 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2558 | SMLoc AlignmentLoc = getLexer().getLoc(); |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2559 | int64_t Alignment; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2560 | if (parseAbsoluteExpression(Alignment)) |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2561 | return true; |
| 2562 | |
| 2563 | SMLoc MaxBytesLoc; |
| 2564 | bool HasFillExpr = false; |
| 2565 | int64_t FillExpr = 0; |
| 2566 | int64_t MaxBytesToFill = 0; |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2567 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2568 | if (getLexer().isNot(AsmToken::Comma)) |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2569 | return TokError("unexpected token in directive"); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2570 | Lex(); |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2571 | |
| 2572 | // The fill expression can be omitted while specifying a maximum number of |
| 2573 | // alignment bytes, e.g: |
| 2574 | // .align 3,,4 |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2575 | if (getLexer().isNot(AsmToken::Comma)) { |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2576 | HasFillExpr = true; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2577 | if (parseAbsoluteExpression(FillExpr)) |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2578 | return true; |
| 2579 | } |
| 2580 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2581 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2582 | if (getLexer().isNot(AsmToken::Comma)) |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2583 | return TokError("unexpected token in directive"); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2584 | Lex(); |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2585 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2586 | MaxBytesLoc = getLexer().getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2587 | if (parseAbsoluteExpression(MaxBytesToFill)) |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2588 | return true; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2589 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2590 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2591 | return TokError("unexpected token in directive"); |
| 2592 | } |
| 2593 | } |
| 2594 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2595 | Lex(); |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2596 | |
Daniel Dunbar | bb166be | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 2597 | if (!HasFillExpr) |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2598 | FillExpr = 0; |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2599 | |
| 2600 | // Compute alignment in bytes. |
| 2601 | if (IsPow2) { |
| 2602 | // FIXME: Diagnose overflow. |
Daniel Dunbar | 18f3c9b | 2009-08-26 09:16:34 +0000 | [diff] [blame] | 2603 | if (Alignment >= 32) { |
| 2604 | Error(AlignmentLoc, "invalid alignment value"); |
| 2605 | Alignment = 31; |
| 2606 | } |
| 2607 | |
Benjamin Kramer | 63951ad | 2009-09-06 09:35:10 +0000 | [diff] [blame] | 2608 | Alignment = 1ULL << Alignment; |
Benjamin Kramer | 64bf780 | 2013-02-16 15:00:16 +0000 | [diff] [blame] | 2609 | } else { |
| 2610 | // Reject alignments that aren't a power of two, for gas compatibility. |
| 2611 | if (!isPowerOf2_64(Alignment)) |
| 2612 | Error(AlignmentLoc, "alignment must be a power of 2"); |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2613 | } |
| 2614 | |
Daniel Dunbar | 18f3c9b | 2009-08-26 09:16:34 +0000 | [diff] [blame] | 2615 | // Diagnose non-sensical max bytes to align. |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2616 | if (MaxBytesLoc.isValid()) { |
| 2617 | if (MaxBytesToFill < 1) { |
Daniel Dunbar | 18f3c9b | 2009-08-26 09:16:34 +0000 | [diff] [blame] | 2618 | Error(MaxBytesLoc, "alignment directive can never be satisfied in this " |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2619 | "many bytes, ignoring maximum bytes expression"); |
Daniel Dunbar | 4abcccb | 2009-08-21 23:01:53 +0000 | [diff] [blame] | 2620 | MaxBytesToFill = 0; |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2621 | } |
| 2622 | |
| 2623 | if (MaxBytesToFill >= Alignment) { |
Daniel Dunbar | c9dc78a | 2009-06-30 00:49:23 +0000 | [diff] [blame] | 2624 | Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and " |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2625 | "has no effect"); |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2626 | MaxBytesToFill = 0; |
| 2627 | } |
| 2628 | } |
| 2629 | |
Daniel Dunbar | bb166be | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 2630 | // Check whether we should use optimal code alignment for this .align |
| 2631 | // directive. |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 2632 | bool UseCodeAlign = getStreamer().getCurrentSection().first->UseCodeAlign(); |
Daniel Dunbar | bb166be | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 2633 | if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) && |
| 2634 | ValueSize == 1 && UseCodeAlign) { |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2635 | getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill); |
Daniel Dunbar | bb166be | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 2636 | } else { |
Kevin Enderby | 7f99302 | 2010-02-25 18:46:04 +0000 | [diff] [blame] | 2637 | // FIXME: Target specific behavior about how the "extra" bytes are filled. |
Chris Lattner | c2b3675 | 2010-07-15 21:19:31 +0000 | [diff] [blame] | 2638 | getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize, |
| 2639 | MaxBytesToFill); |
Daniel Dunbar | bb166be | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 2640 | } |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2641 | |
| 2642 | return false; |
| 2643 | } |
| 2644 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2645 | /// parseDirectiveFile |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2646 | /// ::= .file [number] filename |
| 2647 | /// ::= .file number directory filename |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2648 | bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2649 | // FIXME: I'm not sure what this is. |
| 2650 | int64_t FileNumber = -1; |
| 2651 | SMLoc FileNumberLoc = getLexer().getLoc(); |
| 2652 | if (getLexer().is(AsmToken::Integer)) { |
| 2653 | FileNumber = getTok().getIntVal(); |
| 2654 | Lex(); |
| 2655 | |
| 2656 | if (FileNumber < 1) |
| 2657 | return TokError("file number less than one"); |
| 2658 | } |
| 2659 | |
| 2660 | if (getLexer().isNot(AsmToken::String)) |
| 2661 | return TokError("unexpected token in '.file' directive"); |
| 2662 | |
| 2663 | // Usually the directory and filename together, otherwise just the directory. |
Yunzhong Gao | 8c0f506 | 2013-09-05 19:14:26 +0000 | [diff] [blame] | 2664 | // Allow the strings to have escaped octal character sequence. |
| 2665 | std::string Path = getTok().getString(); |
| 2666 | if (parseEscapedString(Path)) |
| 2667 | return true; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2668 | Lex(); |
| 2669 | |
| 2670 | StringRef Directory; |
| 2671 | StringRef Filename; |
Yunzhong Gao | 8c0f506 | 2013-09-05 19:14:26 +0000 | [diff] [blame] | 2672 | std::string FilenameData; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2673 | if (getLexer().is(AsmToken::String)) { |
| 2674 | if (FileNumber == -1) |
| 2675 | return TokError("explicit path specified, but no file number"); |
Yunzhong Gao | 8c0f506 | 2013-09-05 19:14:26 +0000 | [diff] [blame] | 2676 | if (parseEscapedString(FilenameData)) |
| 2677 | return true; |
| 2678 | Filename = FilenameData; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2679 | Directory = Path; |
| 2680 | Lex(); |
| 2681 | } else { |
| 2682 | Filename = Path; |
| 2683 | } |
| 2684 | |
| 2685 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 2686 | return TokError("unexpected token in '.file' directive"); |
| 2687 | |
| 2688 | if (FileNumber == -1) |
| 2689 | getStreamer().EmitFileDirective(Filename); |
| 2690 | else { |
| 2691 | if (getContext().getGenDwarfForAssembly() == true) |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2692 | Error(DirectiveLoc, |
| 2693 | "input can't have .file dwarf directives when -g is " |
| 2694 | "used to generate dwarf debug info for assembly code"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2695 | |
| 2696 | if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename)) |
| 2697 | Error(FileNumberLoc, "file number already allocated"); |
| 2698 | } |
| 2699 | |
| 2700 | return false; |
| 2701 | } |
| 2702 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2703 | /// parseDirectiveLine |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2704 | /// ::= .line [number] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2705 | bool AsmParser::parseDirectiveLine() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2706 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2707 | if (getLexer().isNot(AsmToken::Integer)) |
| 2708 | return TokError("unexpected token in '.line' directive"); |
| 2709 | |
| 2710 | int64_t LineNumber = getTok().getIntVal(); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2711 | (void)LineNumber; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2712 | Lex(); |
| 2713 | |
| 2714 | // FIXME: Do something with the .line. |
| 2715 | } |
| 2716 | |
| 2717 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 2718 | return TokError("unexpected token in '.line' directive"); |
| 2719 | |
| 2720 | return false; |
| 2721 | } |
| 2722 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2723 | /// parseDirectiveLoc |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2724 | /// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end] |
| 2725 | /// [epilogue_begin] [is_stmt VALUE] [isa VALUE] |
| 2726 | /// The first number is a file number, must have been previously assigned with |
| 2727 | /// a .file directive, the second number is the line number and optionally the |
| 2728 | /// third number is a column position (zero if not specified). The remaining |
| 2729 | /// optional items are .loc sub-directives. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2730 | bool AsmParser::parseDirectiveLoc() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2731 | if (getLexer().isNot(AsmToken::Integer)) |
| 2732 | return TokError("unexpected token in '.loc' directive"); |
| 2733 | int64_t FileNumber = getTok().getIntVal(); |
| 2734 | if (FileNumber < 1) |
| 2735 | return TokError("file number less than one in '.loc' directive"); |
| 2736 | if (!getContext().isValidDwarfFileNumber(FileNumber)) |
| 2737 | return TokError("unassigned file number in '.loc' directive"); |
| 2738 | Lex(); |
| 2739 | |
| 2740 | int64_t LineNumber = 0; |
| 2741 | if (getLexer().is(AsmToken::Integer)) { |
| 2742 | LineNumber = getTok().getIntVal(); |
Adrian Prantl | 6ac4003 | 2013-09-26 23:37:11 +0000 | [diff] [blame] | 2743 | if (LineNumber < 0) |
| 2744 | return TokError("line number less than zero in '.loc' directive"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2745 | Lex(); |
| 2746 | } |
| 2747 | |
| 2748 | int64_t ColumnPos = 0; |
| 2749 | if (getLexer().is(AsmToken::Integer)) { |
| 2750 | ColumnPos = getTok().getIntVal(); |
| 2751 | if (ColumnPos < 0) |
| 2752 | return TokError("column position less than zero in '.loc' directive"); |
| 2753 | Lex(); |
| 2754 | } |
| 2755 | |
| 2756 | unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0; |
| 2757 | unsigned Isa = 0; |
| 2758 | int64_t Discriminator = 0; |
| 2759 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2760 | for (;;) { |
| 2761 | if (getLexer().is(AsmToken::EndOfStatement)) |
| 2762 | break; |
| 2763 | |
| 2764 | StringRef Name; |
| 2765 | SMLoc Loc = getTok().getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2766 | if (parseIdentifier(Name)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2767 | return TokError("unexpected token in '.loc' directive"); |
| 2768 | |
| 2769 | if (Name == "basic_block") |
| 2770 | Flags |= DWARF2_FLAG_BASIC_BLOCK; |
| 2771 | else if (Name == "prologue_end") |
| 2772 | Flags |= DWARF2_FLAG_PROLOGUE_END; |
| 2773 | else if (Name == "epilogue_begin") |
| 2774 | Flags |= DWARF2_FLAG_EPILOGUE_BEGIN; |
| 2775 | else if (Name == "is_stmt") { |
| 2776 | Loc = getTok().getLoc(); |
| 2777 | const MCExpr *Value; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2778 | if (parseExpression(Value)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2779 | return true; |
| 2780 | // The expression must be the constant 0 or 1. |
| 2781 | if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) { |
| 2782 | int Value = MCE->getValue(); |
| 2783 | if (Value == 0) |
| 2784 | Flags &= ~DWARF2_FLAG_IS_STMT; |
| 2785 | else if (Value == 1) |
| 2786 | Flags |= DWARF2_FLAG_IS_STMT; |
| 2787 | else |
| 2788 | return Error(Loc, "is_stmt value not 0 or 1"); |
Craig Topper | f15655b | 2013-04-22 04:22:40 +0000 | [diff] [blame] | 2789 | } else { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2790 | return Error(Loc, "is_stmt value not the constant value of 0 or 1"); |
| 2791 | } |
Craig Topper | f15655b | 2013-04-22 04:22:40 +0000 | [diff] [blame] | 2792 | } else if (Name == "isa") { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2793 | Loc = getTok().getLoc(); |
| 2794 | const MCExpr *Value; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2795 | if (parseExpression(Value)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2796 | return true; |
| 2797 | // The expression must be a constant greater or equal to 0. |
| 2798 | if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) { |
| 2799 | int Value = MCE->getValue(); |
| 2800 | if (Value < 0) |
| 2801 | return Error(Loc, "isa number less than zero"); |
| 2802 | Isa = Value; |
Craig Topper | f15655b | 2013-04-22 04:22:40 +0000 | [diff] [blame] | 2803 | } else { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2804 | return Error(Loc, "isa number not a constant value"); |
| 2805 | } |
Craig Topper | f15655b | 2013-04-22 04:22:40 +0000 | [diff] [blame] | 2806 | } else if (Name == "discriminator") { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2807 | if (parseAbsoluteExpression(Discriminator)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2808 | return true; |
Craig Topper | f15655b | 2013-04-22 04:22:40 +0000 | [diff] [blame] | 2809 | } else { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2810 | return Error(Loc, "unknown sub-directive in '.loc' directive"); |
| 2811 | } |
| 2812 | |
| 2813 | if (getLexer().is(AsmToken::EndOfStatement)) |
| 2814 | break; |
| 2815 | } |
| 2816 | } |
| 2817 | |
| 2818 | getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags, |
| 2819 | Isa, Discriminator, StringRef()); |
| 2820 | |
| 2821 | return false; |
| 2822 | } |
| 2823 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2824 | /// parseDirectiveStabs |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2825 | /// ::= .stabs string, number, number, number |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2826 | bool AsmParser::parseDirectiveStabs() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2827 | return TokError("unsupported directive '.stabs'"); |
| 2828 | } |
| 2829 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2830 | /// parseDirectiveCFISections |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2831 | /// ::= .cfi_sections section [, section] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2832 | bool AsmParser::parseDirectiveCFISections() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2833 | StringRef Name; |
| 2834 | bool EH = false; |
| 2835 | bool Debug = false; |
| 2836 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2837 | if (parseIdentifier(Name)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2838 | return TokError("Expected an identifier"); |
| 2839 | |
| 2840 | if (Name == ".eh_frame") |
| 2841 | EH = true; |
| 2842 | else if (Name == ".debug_frame") |
| 2843 | Debug = true; |
| 2844 | |
| 2845 | if (getLexer().is(AsmToken::Comma)) { |
| 2846 | Lex(); |
| 2847 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2848 | if (parseIdentifier(Name)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2849 | return TokError("Expected an identifier"); |
| 2850 | |
| 2851 | if (Name == ".eh_frame") |
| 2852 | EH = true; |
| 2853 | else if (Name == ".debug_frame") |
| 2854 | Debug = true; |
| 2855 | } |
| 2856 | |
| 2857 | getStreamer().EmitCFISections(EH, Debug); |
| 2858 | return false; |
| 2859 | } |
| 2860 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2861 | /// parseDirectiveCFIStartProc |
David Majnemer | e035cf9 | 2014-01-27 17:20:25 +0000 | [diff] [blame] | 2862 | /// ::= .cfi_startproc [simple] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2863 | bool AsmParser::parseDirectiveCFIStartProc() { |
David Majnemer | e035cf9 | 2014-01-27 17:20:25 +0000 | [diff] [blame] | 2864 | StringRef Simple; |
| 2865 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 2866 | if (parseIdentifier(Simple) || Simple != "simple") |
| 2867 | return TokError("unexpected token in .cfi_startproc directive"); |
| 2868 | |
| 2869 | getStreamer().EmitCFIStartProc(!Simple.empty()); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2870 | return false; |
| 2871 | } |
| 2872 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2873 | /// parseDirectiveCFIEndProc |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2874 | /// ::= .cfi_endproc |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2875 | bool AsmParser::parseDirectiveCFIEndProc() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2876 | getStreamer().EmitCFIEndProc(); |
| 2877 | return false; |
| 2878 | } |
| 2879 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2880 | /// \brief parse register name or number. |
| 2881 | bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register, |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2882 | SMLoc DirectiveLoc) { |
| 2883 | unsigned RegNo; |
| 2884 | |
| 2885 | if (getLexer().isNot(AsmToken::Integer)) { |
| 2886 | if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc)) |
| 2887 | return true; |
Bill Wendling | bc07a89 | 2013-06-18 07:20:20 +0000 | [diff] [blame] | 2888 | Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2889 | } else |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2890 | return parseAbsoluteExpression(Register); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2891 | |
| 2892 | return false; |
| 2893 | } |
| 2894 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2895 | /// parseDirectiveCFIDefCfa |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2896 | /// ::= .cfi_def_cfa register, offset |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2897 | bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2898 | int64_t Register = 0; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2899 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2900 | return true; |
| 2901 | |
| 2902 | if (getLexer().isNot(AsmToken::Comma)) |
| 2903 | return TokError("unexpected token in directive"); |
| 2904 | Lex(); |
| 2905 | |
| 2906 | int64_t Offset = 0; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2907 | if (parseAbsoluteExpression(Offset)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2908 | return true; |
| 2909 | |
| 2910 | getStreamer().EmitCFIDefCfa(Register, Offset); |
| 2911 | return false; |
| 2912 | } |
| 2913 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2914 | /// parseDirectiveCFIDefCfaOffset |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2915 | /// ::= .cfi_def_cfa_offset offset |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2916 | bool AsmParser::parseDirectiveCFIDefCfaOffset() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2917 | int64_t Offset = 0; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2918 | if (parseAbsoluteExpression(Offset)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2919 | return true; |
| 2920 | |
| 2921 | getStreamer().EmitCFIDefCfaOffset(Offset); |
| 2922 | return false; |
| 2923 | } |
| 2924 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2925 | /// parseDirectiveCFIRegister |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2926 | /// ::= .cfi_register register, register |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2927 | bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2928 | int64_t Register1 = 0; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2929 | if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2930 | return true; |
| 2931 | |
| 2932 | if (getLexer().isNot(AsmToken::Comma)) |
| 2933 | return TokError("unexpected token in directive"); |
| 2934 | Lex(); |
| 2935 | |
| 2936 | int64_t Register2 = 0; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2937 | if (parseRegisterOrRegisterNumber(Register2, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2938 | return true; |
| 2939 | |
| 2940 | getStreamer().EmitCFIRegister(Register1, Register2); |
| 2941 | return false; |
| 2942 | } |
| 2943 | |
Venkatraman Govindaraju | 3816d43 | 2013-09-26 14:49:40 +0000 | [diff] [blame] | 2944 | /// parseDirectiveCFIWindowSave |
| 2945 | /// ::= .cfi_window_save |
| 2946 | bool AsmParser::parseDirectiveCFIWindowSave() { |
| 2947 | getStreamer().EmitCFIWindowSave(); |
| 2948 | return false; |
| 2949 | } |
| 2950 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2951 | /// parseDirectiveCFIAdjustCfaOffset |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2952 | /// ::= .cfi_adjust_cfa_offset adjustment |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2953 | bool AsmParser::parseDirectiveCFIAdjustCfaOffset() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2954 | int64_t Adjustment = 0; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2955 | if (parseAbsoluteExpression(Adjustment)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2956 | return true; |
| 2957 | |
| 2958 | getStreamer().EmitCFIAdjustCfaOffset(Adjustment); |
| 2959 | return false; |
| 2960 | } |
| 2961 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2962 | /// parseDirectiveCFIDefCfaRegister |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2963 | /// ::= .cfi_def_cfa_register register |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2964 | bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2965 | int64_t Register = 0; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2966 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2967 | return true; |
| 2968 | |
| 2969 | getStreamer().EmitCFIDefCfaRegister(Register); |
| 2970 | return false; |
| 2971 | } |
| 2972 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2973 | /// parseDirectiveCFIOffset |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2974 | /// ::= .cfi_offset register, offset |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2975 | bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2976 | int64_t Register = 0; |
| 2977 | int64_t Offset = 0; |
| 2978 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2979 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2980 | return true; |
| 2981 | |
| 2982 | if (getLexer().isNot(AsmToken::Comma)) |
| 2983 | return TokError("unexpected token in directive"); |
| 2984 | Lex(); |
| 2985 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2986 | if (parseAbsoluteExpression(Offset)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2987 | return true; |
| 2988 | |
| 2989 | getStreamer().EmitCFIOffset(Register, Offset); |
| 2990 | return false; |
| 2991 | } |
| 2992 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2993 | /// parseDirectiveCFIRelOffset |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2994 | /// ::= .cfi_rel_offset register, offset |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2995 | bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2996 | int64_t Register = 0; |
| 2997 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2998 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2999 | return true; |
| 3000 | |
| 3001 | if (getLexer().isNot(AsmToken::Comma)) |
| 3002 | return TokError("unexpected token in directive"); |
| 3003 | Lex(); |
| 3004 | |
| 3005 | int64_t Offset = 0; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3006 | if (parseAbsoluteExpression(Offset)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3007 | return true; |
| 3008 | |
| 3009 | getStreamer().EmitCFIRelOffset(Register, Offset); |
| 3010 | return false; |
| 3011 | } |
| 3012 | |
| 3013 | static bool isValidEncoding(int64_t Encoding) { |
| 3014 | if (Encoding & ~0xff) |
| 3015 | return false; |
| 3016 | |
| 3017 | if (Encoding == dwarf::DW_EH_PE_omit) |
| 3018 | return true; |
| 3019 | |
| 3020 | const unsigned Format = Encoding & 0xf; |
| 3021 | if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 && |
| 3022 | Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 && |
| 3023 | Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 && |
| 3024 | Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed) |
| 3025 | return false; |
| 3026 | |
| 3027 | const unsigned Application = Encoding & 0x70; |
| 3028 | if (Application != dwarf::DW_EH_PE_absptr && |
| 3029 | Application != dwarf::DW_EH_PE_pcrel) |
| 3030 | return false; |
| 3031 | |
| 3032 | return true; |
| 3033 | } |
| 3034 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3035 | /// parseDirectiveCFIPersonalityOrLsda |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3036 | /// IsPersonality true for cfi_personality, false for cfi_lsda |
| 3037 | /// ::= .cfi_personality encoding, [symbol_name] |
| 3038 | /// ::= .cfi_lsda encoding, [symbol_name] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3039 | bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3040 | int64_t Encoding = 0; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3041 | if (parseAbsoluteExpression(Encoding)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3042 | return true; |
| 3043 | if (Encoding == dwarf::DW_EH_PE_omit) |
| 3044 | return false; |
| 3045 | |
| 3046 | if (!isValidEncoding(Encoding)) |
| 3047 | return TokError("unsupported encoding."); |
| 3048 | |
| 3049 | if (getLexer().isNot(AsmToken::Comma)) |
| 3050 | return TokError("unexpected token in directive"); |
| 3051 | Lex(); |
| 3052 | |
| 3053 | StringRef Name; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3054 | if (parseIdentifier(Name)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3055 | return TokError("expected identifier in directive"); |
| 3056 | |
| 3057 | MCSymbol *Sym = getContext().GetOrCreateSymbol(Name); |
| 3058 | |
| 3059 | if (IsPersonality) |
| 3060 | getStreamer().EmitCFIPersonality(Sym, Encoding); |
| 3061 | else |
| 3062 | getStreamer().EmitCFILsda(Sym, Encoding); |
| 3063 | return false; |
| 3064 | } |
| 3065 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3066 | /// parseDirectiveCFIRememberState |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3067 | /// ::= .cfi_remember_state |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3068 | bool AsmParser::parseDirectiveCFIRememberState() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3069 | getStreamer().EmitCFIRememberState(); |
| 3070 | return false; |
| 3071 | } |
| 3072 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3073 | /// parseDirectiveCFIRestoreState |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3074 | /// ::= .cfi_remember_state |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3075 | bool AsmParser::parseDirectiveCFIRestoreState() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3076 | getStreamer().EmitCFIRestoreState(); |
| 3077 | return false; |
| 3078 | } |
| 3079 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3080 | /// parseDirectiveCFISameValue |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3081 | /// ::= .cfi_same_value register |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3082 | bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3083 | int64_t Register = 0; |
| 3084 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3085 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3086 | return true; |
| 3087 | |
| 3088 | getStreamer().EmitCFISameValue(Register); |
| 3089 | return false; |
| 3090 | } |
| 3091 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3092 | /// parseDirectiveCFIRestore |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3093 | /// ::= .cfi_restore register |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3094 | bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3095 | int64_t Register = 0; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3096 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3097 | return true; |
| 3098 | |
| 3099 | getStreamer().EmitCFIRestore(Register); |
| 3100 | return false; |
| 3101 | } |
| 3102 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3103 | /// parseDirectiveCFIEscape |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3104 | /// ::= .cfi_escape expression[,...] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3105 | bool AsmParser::parseDirectiveCFIEscape() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3106 | std::string Values; |
| 3107 | int64_t CurrValue; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3108 | if (parseAbsoluteExpression(CurrValue)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3109 | return true; |
| 3110 | |
| 3111 | Values.push_back((uint8_t)CurrValue); |
| 3112 | |
| 3113 | while (getLexer().is(AsmToken::Comma)) { |
| 3114 | Lex(); |
| 3115 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3116 | if (parseAbsoluteExpression(CurrValue)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3117 | return true; |
| 3118 | |
| 3119 | Values.push_back((uint8_t)CurrValue); |
| 3120 | } |
| 3121 | |
| 3122 | getStreamer().EmitCFIEscape(Values); |
| 3123 | return false; |
| 3124 | } |
| 3125 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3126 | /// parseDirectiveCFISignalFrame |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3127 | /// ::= .cfi_signal_frame |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3128 | bool AsmParser::parseDirectiveCFISignalFrame() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3129 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3130 | return Error(getLexer().getLoc(), |
| 3131 | "unexpected token in '.cfi_signal_frame'"); |
| 3132 | |
| 3133 | getStreamer().EmitCFISignalFrame(); |
| 3134 | return false; |
| 3135 | } |
| 3136 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3137 | /// parseDirectiveCFIUndefined |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3138 | /// ::= .cfi_undefined register |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3139 | bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3140 | int64_t Register = 0; |
| 3141 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3142 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3143 | return true; |
| 3144 | |
| 3145 | getStreamer().EmitCFIUndefined(Register); |
| 3146 | return false; |
| 3147 | } |
| 3148 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3149 | /// parseDirectiveMacrosOnOff |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3150 | /// ::= .macros_on |
| 3151 | /// ::= .macros_off |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3152 | bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3153 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3154 | return Error(getLexer().getLoc(), |
| 3155 | "unexpected token in '" + Directive + "' directive"); |
| 3156 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3157 | setMacrosEnabled(Directive == ".macros_on"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3158 | return false; |
| 3159 | } |
| 3160 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3161 | /// parseDirectiveMacro |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3162 | /// ::= .macro name [parameters] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3163 | bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3164 | StringRef Name; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3165 | if (parseIdentifier(Name)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3166 | return TokError("expected identifier in '.macro' directive"); |
| 3167 | |
| 3168 | MCAsmMacroParameters Parameters; |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 3169 | while (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 3170 | MCAsmMacroParameter Parameter; |
| 3171 | if (parseIdentifier(Parameter.first)) |
| 3172 | return TokError("expected identifier in '.macro' directive"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3173 | |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 3174 | if (getLexer().is(AsmToken::Equal)) { |
| 3175 | Lex(); |
| 3176 | if (parseMacroArgument(Parameter.second)) |
| 3177 | return true; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3178 | } |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 3179 | |
| 3180 | Parameters.push_back(Parameter); |
| 3181 | |
| 3182 | if (getLexer().is(AsmToken::Comma)) |
| 3183 | Lex(); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3184 | } |
| 3185 | |
| 3186 | // Eat the end of statement. |
| 3187 | Lex(); |
| 3188 | |
| 3189 | AsmToken EndToken, StartToken = getTok(); |
| 3190 | |
| 3191 | // Lex the macro definition. |
| 3192 | for (;;) { |
| 3193 | // Check whether we have reached the end of the file. |
| 3194 | if (getLexer().is(AsmToken::Eof)) |
| 3195 | return Error(DirectiveLoc, "no matching '.endmacro' in definition"); |
| 3196 | |
| 3197 | // Otherwise, check whether we have reach the .endmacro. |
| 3198 | if (getLexer().is(AsmToken::Identifier) && |
| 3199 | (getTok().getIdentifier() == ".endm" || |
| 3200 | getTok().getIdentifier() == ".endmacro")) { |
| 3201 | EndToken = getTok(); |
| 3202 | Lex(); |
| 3203 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3204 | return TokError("unexpected token in '" + EndToken.getIdentifier() + |
| 3205 | "' directive"); |
| 3206 | break; |
| 3207 | } |
| 3208 | |
| 3209 | // Otherwise, scan til the end of the statement. |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3210 | eatToEndOfStatement(); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3211 | } |
| 3212 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3213 | if (lookupMacro(Name)) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3214 | return Error(DirectiveLoc, "macro '" + Name + "' is already defined"); |
| 3215 | } |
| 3216 | |
| 3217 | const char *BodyStart = StartToken.getLoc().getPointer(); |
| 3218 | const char *BodyEnd = EndToken.getLoc().getPointer(); |
| 3219 | StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3220 | checkForBadMacro(DirectiveLoc, Name, Body, Parameters); |
| 3221 | defineMacro(Name, MCAsmMacro(Name, Body, Parameters)); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3222 | return false; |
| 3223 | } |
| 3224 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3225 | /// checkForBadMacro |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3226 | /// |
| 3227 | /// With the support added for named parameters there may be code out there that |
| 3228 | /// is transitioning from positional parameters. In versions of gas that did |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 3229 | /// not support named parameters they would be ignored on the macro definition. |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3230 | /// But to support both styles of parameters this is not possible so if a macro |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 3231 | /// definition has named parameters but does not use them and has what appears |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3232 | /// to be positional parameters, strings like $1, $2, ... and $n, then issue a |
| 3233 | /// warning that the positional parameter found in body which have no effect. |
| 3234 | /// Hoping the developer will either remove the named parameters from the macro |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 3235 | /// definition so the positional parameters get used if that was what was |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3236 | /// intended or change the macro to use the named parameters. It is possible |
| 3237 | /// this warning will trigger when the none of the named parameters are used |
| 3238 | /// and the strings like $1 are infact to simply to be passed trough unchanged. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3239 | void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3240 | StringRef Body, |
| 3241 | MCAsmMacroParameters Parameters) { |
| 3242 | // If this macro is not defined with named parameters the warning we are |
| 3243 | // checking for here doesn't apply. |
| 3244 | unsigned NParameters = Parameters.size(); |
| 3245 | if (NParameters == 0) |
| 3246 | return; |
| 3247 | |
| 3248 | bool NamedParametersFound = false; |
| 3249 | bool PositionalParametersFound = false; |
| 3250 | |
| 3251 | // Look at the body of the macro for use of both the named parameters and what |
| 3252 | // are likely to be positional parameters. This is what expandMacro() is |
| 3253 | // doing when it finds the parameters in the body. |
| 3254 | while (!Body.empty()) { |
| 3255 | // Scan for the next possible parameter. |
| 3256 | std::size_t End = Body.size(), Pos = 0; |
| 3257 | for (; Pos != End; ++Pos) { |
| 3258 | // Check for a substitution or escape. |
| 3259 | // This macro is defined with parameters, look for \foo, \bar, etc. |
| 3260 | if (Body[Pos] == '\\' && Pos + 1 != End) |
| 3261 | break; |
| 3262 | |
| 3263 | // This macro should have parameters, but look for $0, $1, ..., $n too. |
| 3264 | if (Body[Pos] != '$' || Pos + 1 == End) |
| 3265 | continue; |
| 3266 | char Next = Body[Pos + 1]; |
Guy Benyei | 83c74e9 | 2013-02-12 21:21:59 +0000 | [diff] [blame] | 3267 | if (Next == '$' || Next == 'n' || |
| 3268 | isdigit(static_cast<unsigned char>(Next))) |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3269 | break; |
| 3270 | } |
| 3271 | |
| 3272 | // Check if we reached the end. |
| 3273 | if (Pos == End) |
| 3274 | break; |
| 3275 | |
| 3276 | if (Body[Pos] == '$') { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3277 | switch (Body[Pos + 1]) { |
| 3278 | // $$ => $ |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3279 | case '$': |
| 3280 | break; |
| 3281 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3282 | // $n => number of arguments |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3283 | case 'n': |
| 3284 | PositionalParametersFound = true; |
| 3285 | break; |
| 3286 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3287 | // $[0-9] => argument |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3288 | default: { |
| 3289 | PositionalParametersFound = true; |
| 3290 | break; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3291 | } |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3292 | } |
| 3293 | Pos += 2; |
| 3294 | } else { |
| 3295 | unsigned I = Pos + 1; |
| 3296 | while (isIdentifierChar(Body[I]) && I + 1 != End) |
| 3297 | ++I; |
| 3298 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3299 | const char *Begin = Body.data() + Pos + 1; |
| 3300 | StringRef Argument(Begin, I - (Pos + 1)); |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3301 | unsigned Index = 0; |
| 3302 | for (; Index < NParameters; ++Index) |
| 3303 | if (Parameters[Index].first == Argument) |
| 3304 | break; |
| 3305 | |
| 3306 | if (Index == NParameters) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3307 | if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')') |
| 3308 | Pos += 3; |
| 3309 | else { |
| 3310 | Pos = I; |
| 3311 | } |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3312 | } else { |
| 3313 | NamedParametersFound = true; |
| 3314 | Pos += 1 + Argument.size(); |
| 3315 | } |
| 3316 | } |
| 3317 | // Update the scan point. |
| 3318 | Body = Body.substr(Pos); |
| 3319 | } |
| 3320 | |
| 3321 | if (!NamedParametersFound && PositionalParametersFound) |
| 3322 | Warning(DirectiveLoc, "macro defined with named parameters which are not " |
| 3323 | "used in macro body, possible positional parameter " |
| 3324 | "found in body which will have no effect"); |
| 3325 | } |
| 3326 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3327 | /// parseDirectiveEndMacro |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3328 | /// ::= .endm |
| 3329 | /// ::= .endmacro |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3330 | bool AsmParser::parseDirectiveEndMacro(StringRef Directive) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3331 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3332 | return TokError("unexpected token in '" + Directive + "' directive"); |
| 3333 | |
| 3334 | // If we are inside a macro instantiation, terminate the current |
| 3335 | // instantiation. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3336 | if (isInsideMacroInstantiation()) { |
| 3337 | handleMacroExit(); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3338 | return false; |
| 3339 | } |
| 3340 | |
| 3341 | // Otherwise, this .endmacro is a stray entry in the file; well formed |
| 3342 | // .endmacro directives are handled during the macro definition parsing. |
| 3343 | return TokError("unexpected '" + Directive + "' in file, " |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3344 | "no current macro definition"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3345 | } |
| 3346 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3347 | /// parseDirectivePurgeMacro |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3348 | /// ::= .purgem |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3349 | bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3350 | StringRef Name; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3351 | if (parseIdentifier(Name)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3352 | return TokError("expected identifier in '.purgem' directive"); |
| 3353 | |
| 3354 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3355 | return TokError("unexpected token in '.purgem' directive"); |
| 3356 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3357 | if (!lookupMacro(Name)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3358 | return Error(DirectiveLoc, "macro '" + Name + "' is not defined"); |
| 3359 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3360 | undefineMacro(Name); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3361 | return false; |
| 3362 | } |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 3363 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3364 | /// parseDirectiveBundleAlignMode |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 3365 | /// ::= {.bundle_align_mode} expression |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3366 | bool AsmParser::parseDirectiveBundleAlignMode() { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3367 | checkForValidSection(); |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 3368 | |
| 3369 | // Expect a single argument: an expression that evaluates to a constant |
| 3370 | // in the inclusive range 0-30. |
| 3371 | SMLoc ExprLoc = getLexer().getLoc(); |
| 3372 | int64_t AlignSizePow2; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3373 | if (parseAbsoluteExpression(AlignSizePow2)) |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 3374 | return true; |
| 3375 | else if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3376 | return TokError("unexpected token after expression in" |
| 3377 | " '.bundle_align_mode' directive"); |
| 3378 | else if (AlignSizePow2 < 0 || AlignSizePow2 > 30) |
| 3379 | return Error(ExprLoc, |
| 3380 | "invalid bundle alignment size (expected between 0 and 30)"); |
| 3381 | |
| 3382 | Lex(); |
| 3383 | |
| 3384 | // Because of AlignSizePow2's verified range we can safely truncate it to |
| 3385 | // unsigned. |
| 3386 | getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2)); |
| 3387 | return false; |
| 3388 | } |
| 3389 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3390 | /// parseDirectiveBundleLock |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 3391 | /// ::= {.bundle_lock} [align_to_end] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3392 | bool AsmParser::parseDirectiveBundleLock() { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3393 | checkForValidSection(); |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 3394 | bool AlignToEnd = false; |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 3395 | |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 3396 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 3397 | StringRef Option; |
| 3398 | SMLoc Loc = getTok().getLoc(); |
| 3399 | const char *kInvalidOptionError = |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3400 | "invalid option for '.bundle_lock' directive"; |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 3401 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3402 | if (parseIdentifier(Option)) |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 3403 | return Error(Loc, kInvalidOptionError); |
| 3404 | |
| 3405 | if (Option != "align_to_end") |
| 3406 | return Error(Loc, kInvalidOptionError); |
| 3407 | else if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3408 | return Error(Loc, |
| 3409 | "unexpected token after '.bundle_lock' directive option"); |
| 3410 | AlignToEnd = true; |
| 3411 | } |
| 3412 | |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 3413 | Lex(); |
| 3414 | |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 3415 | getStreamer().EmitBundleLock(AlignToEnd); |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 3416 | return false; |
| 3417 | } |
| 3418 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3419 | /// parseDirectiveBundleLock |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 3420 | /// ::= {.bundle_lock} |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3421 | bool AsmParser::parseDirectiveBundleUnlock() { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3422 | checkForValidSection(); |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 3423 | |
| 3424 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3425 | return TokError("unexpected token in '.bundle_unlock' directive"); |
| 3426 | Lex(); |
| 3427 | |
| 3428 | getStreamer().EmitBundleUnlock(); |
| 3429 | return false; |
| 3430 | } |
| 3431 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3432 | /// parseDirectiveSpace |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3433 | /// ::= (.skip | .space) expression [ , expression ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3434 | bool AsmParser::parseDirectiveSpace(StringRef IDVal) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3435 | checkForValidSection(); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3436 | |
| 3437 | int64_t NumBytes; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3438 | if (parseAbsoluteExpression(NumBytes)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3439 | return true; |
| 3440 | |
| 3441 | int64_t FillExpr = 0; |
| 3442 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 3443 | if (getLexer().isNot(AsmToken::Comma)) |
| 3444 | return TokError("unexpected token in '" + Twine(IDVal) + "' directive"); |
| 3445 | Lex(); |
| 3446 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3447 | if (parseAbsoluteExpression(FillExpr)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3448 | return true; |
| 3449 | |
| 3450 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3451 | return TokError("unexpected token in '" + Twine(IDVal) + "' directive"); |
| 3452 | } |
| 3453 | |
| 3454 | Lex(); |
| 3455 | |
| 3456 | if (NumBytes <= 0) |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3457 | return TokError("invalid number of bytes in '" + Twine(IDVal) + |
| 3458 | "' directive"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3459 | |
| 3460 | // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0. |
Rafael Espindola | 64e1af8 | 2013-07-02 15:49:13 +0000 | [diff] [blame] | 3461 | getStreamer().EmitFill(NumBytes, FillExpr); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3462 | |
| 3463 | return false; |
| 3464 | } |
| 3465 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3466 | /// parseDirectiveLEB128 |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3467 | /// ::= (.sleb128 | .uleb128) expression |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3468 | bool AsmParser::parseDirectiveLEB128(bool Signed) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3469 | checkForValidSection(); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3470 | const MCExpr *Value; |
| 3471 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3472 | if (parseExpression(Value)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3473 | return true; |
| 3474 | |
| 3475 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3476 | return TokError("unexpected token in directive"); |
| 3477 | |
| 3478 | if (Signed) |
| 3479 | getStreamer().EmitSLEB128Value(Value); |
| 3480 | else |
| 3481 | getStreamer().EmitULEB128Value(Value); |
| 3482 | |
| 3483 | return false; |
| 3484 | } |
| 3485 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3486 | /// parseDirectiveSymbolAttribute |
Daniel Dunbar | a5508c8 | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 3487 | /// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3488 | bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) { |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3489 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
Daniel Dunbar | a5508c8 | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 3490 | for (;;) { |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 3491 | StringRef Name; |
Jim Grosbach | ebdf32f | 2011-09-15 17:56:49 +0000 | [diff] [blame] | 3492 | SMLoc Loc = getTok().getLoc(); |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 3493 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3494 | if (parseIdentifier(Name)) |
Jim Grosbach | ebdf32f | 2011-09-15 17:56:49 +0000 | [diff] [blame] | 3495 | return Error(Loc, "expected identifier in directive"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3496 | |
Daniel Dunbar | 101c14c | 2010-07-12 19:52:10 +0000 | [diff] [blame] | 3497 | MCSymbol *Sym = getContext().GetOrCreateSymbol(Name); |
Daniel Dunbar | a5508c8 | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 3498 | |
Jim Grosbach | ebdf32f | 2011-09-15 17:56:49 +0000 | [diff] [blame] | 3499 | // Assembler local symbols don't make any sense here. Complain loudly. |
| 3500 | if (Sym->isTemporary()) |
| 3501 | return Error(Loc, "non-local symbol required in directive"); |
| 3502 | |
Saleem Abdulrasool | 4208b61 | 2013-08-09 01:52:03 +0000 | [diff] [blame] | 3503 | if (!getStreamer().EmitSymbolAttribute(Sym, Attr)) |
| 3504 | return Error(Loc, "unable to emit symbol attribute"); |
Daniel Dunbar | a5508c8 | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 3505 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3506 | if (getLexer().is(AsmToken::EndOfStatement)) |
Daniel Dunbar | a5508c8 | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 3507 | break; |
| 3508 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3509 | if (getLexer().isNot(AsmToken::Comma)) |
Daniel Dunbar | a5508c8 | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 3510 | return TokError("unexpected token in directive"); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 3511 | Lex(); |
Daniel Dunbar | a5508c8 | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 3512 | } |
| 3513 | } |
| 3514 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 3515 | Lex(); |
Jan Wen Voung | c768287 | 2010-09-30 01:09:20 +0000 | [diff] [blame] | 3516 | return false; |
Daniel Dunbar | a5508c8 | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 3517 | } |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3518 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3519 | /// parseDirectiveComm |
Chris Lattner | 28ad754 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 3520 | /// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3521 | bool AsmParser::parseDirectiveComm(bool IsLocal) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3522 | checkForValidSection(); |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 3523 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3524 | SMLoc IDLoc = getLexer().getLoc(); |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 3525 | StringRef Name; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3526 | if (parseIdentifier(Name)) |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3527 | return TokError("expected identifier in directive"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3528 | |
Daniel Dunbar | 9ee33ca | 2009-07-31 21:55:09 +0000 | [diff] [blame] | 3529 | // Handle the identifier as the key symbol. |
Daniel Dunbar | 101c14c | 2010-07-12 19:52:10 +0000 | [diff] [blame] | 3530 | MCSymbol *Sym = getContext().GetOrCreateSymbol(Name); |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3531 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3532 | if (getLexer().isNot(AsmToken::Comma)) |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3533 | return TokError("unexpected token in directive"); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 3534 | Lex(); |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3535 | |
| 3536 | int64_t Size; |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3537 | SMLoc SizeLoc = getLexer().getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3538 | if (parseAbsoluteExpression(Size)) |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3539 | return true; |
| 3540 | |
| 3541 | int64_t Pow2Alignment = 0; |
| 3542 | SMLoc Pow2AlignmentLoc; |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3543 | if (getLexer().is(AsmToken::Comma)) { |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 3544 | Lex(); |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3545 | Pow2AlignmentLoc = getLexer().getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3546 | if (parseAbsoluteExpression(Pow2Alignment)) |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3547 | return true; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3548 | |
Benjamin Kramer | 68b9f05 | 2012-09-07 21:08:01 +0000 | [diff] [blame] | 3549 | LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType(); |
| 3550 | if (IsLocal && LCOMM == LCOMM::NoAlignment) |
Benjamin Kramer | 47f9ec9 | 2012-09-07 17:25:13 +0000 | [diff] [blame] | 3551 | return Error(Pow2AlignmentLoc, "alignment not supported on this target"); |
| 3552 | |
Chris Lattner | ab9cd3e | 2010-01-19 06:22:22 +0000 | [diff] [blame] | 3553 | // If this target takes alignments in bytes (not log) validate and convert. |
Benjamin Kramer | 68b9f05 | 2012-09-07 21:08:01 +0000 | [diff] [blame] | 3554 | if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) || |
| 3555 | (IsLocal && LCOMM == LCOMM::ByteAlignment)) { |
Chris Lattner | ab9cd3e | 2010-01-19 06:22:22 +0000 | [diff] [blame] | 3556 | if (!isPowerOf2_64(Pow2Alignment)) |
| 3557 | return Error(Pow2AlignmentLoc, "alignment must be a power of 2"); |
| 3558 | Pow2Alignment = Log2_64(Pow2Alignment); |
| 3559 | } |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3560 | } |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3561 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3562 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Chris Lattner | 28ad754 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 3563 | return TokError("unexpected token in '.comm' or '.lcomm' directive"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3564 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 3565 | Lex(); |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3566 | |
Chris Lattner | 28ad754 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 3567 | // NOTE: a size of zero for a .comm should create a undefined symbol |
| 3568 | // but a size of .lcomm creates a bss symbol of size zero. |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3569 | if (Size < 0) |
Chris Lattner | 28ad754 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 3570 | return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't " |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3571 | "be less than zero"); |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3572 | |
Eric Christopher | bc81885 | 2010-05-14 01:38:54 +0000 | [diff] [blame] | 3573 | // NOTE: The alignment in the directive is a power of 2 value, the assembler |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3574 | // may internally end up wanting an alignment in bytes. |
| 3575 | // FIXME: Diagnose overflow. |
| 3576 | if (Pow2Alignment < 0) |
Chris Lattner | 28ad754 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 3577 | return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive " |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3578 | "alignment, can't be less than zero"); |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3579 | |
Daniel Dunbar | 6860ac7 | 2009-08-22 07:22:36 +0000 | [diff] [blame] | 3580 | if (!Sym->isUndefined()) |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3581 | return Error(IDLoc, "invalid symbol redefinition"); |
| 3582 | |
Chris Lattner | 28ad754 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 3583 | // Create the Symbol as a common or local common with Size and Pow2Alignment |
Daniel Dunbar | 6a715dc | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 3584 | if (IsLocal) { |
Benjamin Kramer | 47f9ec9 | 2012-09-07 17:25:13 +0000 | [diff] [blame] | 3585 | getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment); |
Daniel Dunbar | 6a715dc | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 3586 | return false; |
| 3587 | } |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3588 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3589 | getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment); |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3590 | return false; |
| 3591 | } |
Chris Lattner | 07cadaf | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 3592 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3593 | /// parseDirectiveAbort |
Daniel Dunbar | 40a564f | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 3594 | /// ::= .abort [... message ...] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3595 | bool AsmParser::parseDirectiveAbort() { |
Daniel Dunbar | eb6bb32 | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 3596 | // FIXME: Use loc from directive. |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3597 | SMLoc Loc = getLexer().getLoc(); |
Daniel Dunbar | eb6bb32 | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 3598 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3599 | StringRef Str = parseStringToEndOfStatement(); |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3600 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | 56523ce | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 3601 | return TokError("unexpected token in '.abort' directive"); |
Daniel Dunbar | 40a564f | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 3602 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 3603 | Lex(); |
Kevin Enderby | 56523ce | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 3604 | |
Daniel Dunbar | eb6bb32 | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 3605 | if (Str.empty()) |
| 3606 | Error(Loc, ".abort detected. Assembly stopping."); |
| 3607 | else |
| 3608 | Error(Loc, ".abort '" + Str + "' detected. Assembly stopping."); |
Daniel Dunbar | 40a564f | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 3609 | // FIXME: Actually abort assembly here. |
Kevin Enderby | 56523ce | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 3610 | |
| 3611 | return false; |
| 3612 | } |
Kevin Enderby | cbe475d | 2009-07-14 21:35:03 +0000 | [diff] [blame] | 3613 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3614 | /// parseDirectiveInclude |
Kevin Enderby | d1ea539 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 3615 | /// ::= .include "filename" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3616 | bool AsmParser::parseDirectiveInclude() { |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3617 | if (getLexer().isNot(AsmToken::String)) |
Kevin Enderby | d1ea539 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 3618 | return TokError("expected string in '.include' directive"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3619 | |
Yunzhong Gao | 8c0f506 | 2013-09-05 19:14:26 +0000 | [diff] [blame] | 3620 | // Allow the strings to have escaped octal character sequence. |
| 3621 | std::string Filename; |
| 3622 | if (parseEscapedString(Filename)) |
| 3623 | return true; |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3624 | SMLoc IncludeLoc = getLexer().getLoc(); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 3625 | Lex(); |
Kevin Enderby | d1ea539 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 3626 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3627 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | d1ea539 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 3628 | return TokError("unexpected token in '.include' directive"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3629 | |
Chris Lattner | 693fbb8 | 2009-07-16 06:14:39 +0000 | [diff] [blame] | 3630 | // Attempt to switch the lexer to the included file before consuming the end |
| 3631 | // of statement to avoid losing it when we switch. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3632 | if (enterIncludeFile(Filename)) { |
Daniel Dunbar | d8a1845 | 2010-07-18 18:31:45 +0000 | [diff] [blame] | 3633 | Error(IncludeLoc, "Could not find include file '" + Filename + "'"); |
Chris Lattner | 693fbb8 | 2009-07-16 06:14:39 +0000 | [diff] [blame] | 3634 | return true; |
| 3635 | } |
Kevin Enderby | d1ea539 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 3636 | |
| 3637 | return false; |
| 3638 | } |
Kevin Enderby | 09ea570 | 2009-07-15 15:30:11 +0000 | [diff] [blame] | 3639 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3640 | /// parseDirectiveIncbin |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 3641 | /// ::= .incbin "filename" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3642 | bool AsmParser::parseDirectiveIncbin() { |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 3643 | if (getLexer().isNot(AsmToken::String)) |
| 3644 | return TokError("expected string in '.incbin' directive"); |
| 3645 | |
Yunzhong Gao | 8c0f506 | 2013-09-05 19:14:26 +0000 | [diff] [blame] | 3646 | // Allow the strings to have escaped octal character sequence. |
| 3647 | std::string Filename; |
| 3648 | if (parseEscapedString(Filename)) |
| 3649 | return true; |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 3650 | SMLoc IncbinLoc = getLexer().getLoc(); |
| 3651 | Lex(); |
| 3652 | |
| 3653 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3654 | return TokError("unexpected token in '.incbin' directive"); |
| 3655 | |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 3656 | // Attempt to process the included file. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3657 | if (processIncbinFile(Filename)) { |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 3658 | Error(IncbinLoc, "Could not find incbin file '" + Filename + "'"); |
| 3659 | return true; |
| 3660 | } |
| 3661 | |
| 3662 | return false; |
| 3663 | } |
| 3664 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3665 | /// parseDirectiveIf |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3666 | /// ::= .if expression |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3667 | bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc) { |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3668 | TheCondStack.push_back(TheCondState); |
| 3669 | TheCondState.TheCond = AsmCond::IfCond; |
Benjamin Kramer | c7eda3e | 2012-05-12 16:52:21 +0000 | [diff] [blame] | 3670 | if (TheCondState.Ignore) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3671 | eatToEndOfStatement(); |
Benjamin Kramer | c7eda3e | 2012-05-12 16:52:21 +0000 | [diff] [blame] | 3672 | } else { |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3673 | int64_t ExprValue; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3674 | if (parseAbsoluteExpression(ExprValue)) |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3675 | return true; |
| 3676 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3677 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3678 | return TokError("unexpected token in '.if' directive"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3679 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 3680 | Lex(); |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3681 | |
| 3682 | TheCondState.CondMet = ExprValue; |
| 3683 | TheCondState.Ignore = !TheCondState.CondMet; |
| 3684 | } |
| 3685 | |
| 3686 | return false; |
| 3687 | } |
| 3688 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3689 | /// parseDirectiveIfb |
Benjamin Kramer | 62c18b0 | 2012-05-12 11:18:42 +0000 | [diff] [blame] | 3690 | /// ::= .ifb string |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3691 | bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) { |
Benjamin Kramer | 62c18b0 | 2012-05-12 11:18:42 +0000 | [diff] [blame] | 3692 | TheCondStack.push_back(TheCondState); |
| 3693 | TheCondState.TheCond = AsmCond::IfCond; |
| 3694 | |
Benjamin Kramer | c7eda3e | 2012-05-12 16:52:21 +0000 | [diff] [blame] | 3695 | if (TheCondState.Ignore) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3696 | eatToEndOfStatement(); |
Benjamin Kramer | 62c18b0 | 2012-05-12 11:18:42 +0000 | [diff] [blame] | 3697 | } else { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3698 | StringRef Str = parseStringToEndOfStatement(); |
Benjamin Kramer | 62c18b0 | 2012-05-12 11:18:42 +0000 | [diff] [blame] | 3699 | |
| 3700 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3701 | return TokError("unexpected token in '.ifb' directive"); |
| 3702 | |
| 3703 | Lex(); |
| 3704 | |
| 3705 | TheCondState.CondMet = ExpectBlank == Str.empty(); |
| 3706 | TheCondState.Ignore = !TheCondState.CondMet; |
| 3707 | } |
| 3708 | |
| 3709 | return false; |
| 3710 | } |
| 3711 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3712 | /// parseDirectiveIfc |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 3713 | /// ::= .ifc string1, string2 |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3714 | bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) { |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 3715 | TheCondStack.push_back(TheCondState); |
| 3716 | TheCondState.TheCond = AsmCond::IfCond; |
| 3717 | |
Benjamin Kramer | c7eda3e | 2012-05-12 16:52:21 +0000 | [diff] [blame] | 3718 | if (TheCondState.Ignore) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3719 | eatToEndOfStatement(); |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 3720 | } else { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3721 | StringRef Str1 = parseStringToComma(); |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 3722 | |
| 3723 | if (getLexer().isNot(AsmToken::Comma)) |
| 3724 | return TokError("unexpected token in '.ifc' directive"); |
| 3725 | |
| 3726 | Lex(); |
| 3727 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3728 | StringRef Str2 = parseStringToEndOfStatement(); |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 3729 | |
| 3730 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3731 | return TokError("unexpected token in '.ifc' directive"); |
| 3732 | |
| 3733 | Lex(); |
| 3734 | |
| 3735 | TheCondState.CondMet = ExpectEqual == (Str1 == Str2); |
| 3736 | TheCondState.Ignore = !TheCondState.CondMet; |
| 3737 | } |
| 3738 | |
| 3739 | return false; |
| 3740 | } |
| 3741 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3742 | /// parseDirectiveIfdef |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 3743 | /// ::= .ifdef symbol |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3744 | bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) { |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 3745 | StringRef Name; |
| 3746 | TheCondStack.push_back(TheCondState); |
| 3747 | TheCondState.TheCond = AsmCond::IfCond; |
| 3748 | |
| 3749 | if (TheCondState.Ignore) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3750 | eatToEndOfStatement(); |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 3751 | } else { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3752 | if (parseIdentifier(Name)) |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 3753 | return TokError("expected identifier after '.ifdef'"); |
| 3754 | |
| 3755 | Lex(); |
| 3756 | |
| 3757 | MCSymbol *Sym = getContext().LookupSymbol(Name); |
| 3758 | |
| 3759 | if (expect_defined) |
| 3760 | TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined()); |
| 3761 | else |
| 3762 | TheCondState.CondMet = (Sym == NULL || Sym->isUndefined()); |
| 3763 | TheCondState.Ignore = !TheCondState.CondMet; |
| 3764 | } |
| 3765 | |
| 3766 | return false; |
| 3767 | } |
| 3768 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3769 | /// parseDirectiveElseIf |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3770 | /// ::= .elseif expression |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3771 | bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) { |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3772 | if (TheCondState.TheCond != AsmCond::IfCond && |
| 3773 | TheCondState.TheCond != AsmCond::ElseIfCond) |
Craig Topper | 2172ad6 | 2013-04-22 04:24:02 +0000 | [diff] [blame] | 3774 | Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or " |
| 3775 | " an .elseif"); |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3776 | TheCondState.TheCond = AsmCond::ElseIfCond; |
| 3777 | |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3778 | bool LastIgnoreState = false; |
| 3779 | if (!TheCondStack.empty()) |
Craig Topper | 2172ad6 | 2013-04-22 04:24:02 +0000 | [diff] [blame] | 3780 | LastIgnoreState = TheCondStack.back().Ignore; |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3781 | if (LastIgnoreState || TheCondState.CondMet) { |
| 3782 | TheCondState.Ignore = true; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3783 | eatToEndOfStatement(); |
Craig Topper | f15655b | 2013-04-22 04:22:40 +0000 | [diff] [blame] | 3784 | } else { |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3785 | int64_t ExprValue; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3786 | if (parseAbsoluteExpression(ExprValue)) |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3787 | return true; |
| 3788 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3789 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3790 | return TokError("unexpected token in '.elseif' directive"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3791 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 3792 | Lex(); |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3793 | TheCondState.CondMet = ExprValue; |
| 3794 | TheCondState.Ignore = !TheCondState.CondMet; |
| 3795 | } |
| 3796 | |
| 3797 | return false; |
| 3798 | } |
| 3799 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3800 | /// parseDirectiveElse |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3801 | /// ::= .else |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3802 | bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) { |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3803 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3804 | return TokError("unexpected token in '.else' directive"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3805 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 3806 | Lex(); |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3807 | |
| 3808 | if (TheCondState.TheCond != AsmCond::IfCond && |
| 3809 | TheCondState.TheCond != AsmCond::ElseIfCond) |
Craig Topper | 2172ad6 | 2013-04-22 04:24:02 +0000 | [diff] [blame] | 3810 | Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an " |
| 3811 | ".elseif"); |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3812 | TheCondState.TheCond = AsmCond::ElseCond; |
| 3813 | bool LastIgnoreState = false; |
| 3814 | if (!TheCondStack.empty()) |
| 3815 | LastIgnoreState = TheCondStack.back().Ignore; |
| 3816 | if (LastIgnoreState || TheCondState.CondMet) |
| 3817 | TheCondState.Ignore = true; |
| 3818 | else |
| 3819 | TheCondState.Ignore = false; |
| 3820 | |
| 3821 | return false; |
| 3822 | } |
| 3823 | |
Saleem Abdulrasool | 88186c4 | 2013-12-18 02:53:03 +0000 | [diff] [blame] | 3824 | /// parseDirectiveEnd |
| 3825 | /// ::= .end |
| 3826 | bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) { |
| 3827 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3828 | return TokError("unexpected token in '.end' directive"); |
| 3829 | |
| 3830 | Lex(); |
| 3831 | |
| 3832 | while (Lexer.isNot(AsmToken::Eof)) |
| 3833 | Lex(); |
| 3834 | |
| 3835 | return false; |
| 3836 | } |
| 3837 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3838 | /// parseDirectiveEndIf |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3839 | /// ::= .endif |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3840 | bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) { |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3841 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3842 | return TokError("unexpected token in '.endif' directive"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3843 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 3844 | Lex(); |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3845 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3846 | if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty()) |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3847 | Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or " |
| 3848 | ".else"); |
| 3849 | if (!TheCondStack.empty()) { |
| 3850 | TheCondState = TheCondStack.back(); |
| 3851 | TheCondStack.pop_back(); |
| 3852 | } |
| 3853 | |
| 3854 | return false; |
| 3855 | } |
Daniel Dunbar | a4b069c | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 3856 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3857 | void AsmParser::initializeDirectiveKindMap() { |
| 3858 | DirectiveKindMap[".set"] = DK_SET; |
| 3859 | DirectiveKindMap[".equ"] = DK_EQU; |
| 3860 | DirectiveKindMap[".equiv"] = DK_EQUIV; |
| 3861 | DirectiveKindMap[".ascii"] = DK_ASCII; |
| 3862 | DirectiveKindMap[".asciz"] = DK_ASCIZ; |
| 3863 | DirectiveKindMap[".string"] = DK_STRING; |
| 3864 | DirectiveKindMap[".byte"] = DK_BYTE; |
| 3865 | DirectiveKindMap[".short"] = DK_SHORT; |
| 3866 | DirectiveKindMap[".value"] = DK_VALUE; |
| 3867 | DirectiveKindMap[".2byte"] = DK_2BYTE; |
| 3868 | DirectiveKindMap[".long"] = DK_LONG; |
| 3869 | DirectiveKindMap[".int"] = DK_INT; |
| 3870 | DirectiveKindMap[".4byte"] = DK_4BYTE; |
| 3871 | DirectiveKindMap[".quad"] = DK_QUAD; |
| 3872 | DirectiveKindMap[".8byte"] = DK_8BYTE; |
David Woodhouse | d6de0d9 | 2014-02-01 16:20:59 +0000 | [diff] [blame] | 3873 | DirectiveKindMap[".octa"] = DK_OCTA; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3874 | DirectiveKindMap[".single"] = DK_SINGLE; |
| 3875 | DirectiveKindMap[".float"] = DK_FLOAT; |
| 3876 | DirectiveKindMap[".double"] = DK_DOUBLE; |
| 3877 | DirectiveKindMap[".align"] = DK_ALIGN; |
| 3878 | DirectiveKindMap[".align32"] = DK_ALIGN32; |
| 3879 | DirectiveKindMap[".balign"] = DK_BALIGN; |
| 3880 | DirectiveKindMap[".balignw"] = DK_BALIGNW; |
| 3881 | DirectiveKindMap[".balignl"] = DK_BALIGNL; |
| 3882 | DirectiveKindMap[".p2align"] = DK_P2ALIGN; |
| 3883 | DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW; |
| 3884 | DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL; |
| 3885 | DirectiveKindMap[".org"] = DK_ORG; |
| 3886 | DirectiveKindMap[".fill"] = DK_FILL; |
| 3887 | DirectiveKindMap[".zero"] = DK_ZERO; |
| 3888 | DirectiveKindMap[".extern"] = DK_EXTERN; |
| 3889 | DirectiveKindMap[".globl"] = DK_GLOBL; |
| 3890 | DirectiveKindMap[".global"] = DK_GLOBAL; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3891 | DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE; |
| 3892 | DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP; |
| 3893 | DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER; |
| 3894 | DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN; |
| 3895 | DirectiveKindMap[".reference"] = DK_REFERENCE; |
| 3896 | DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION; |
| 3897 | DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE; |
| 3898 | DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN; |
| 3899 | DirectiveKindMap[".comm"] = DK_COMM; |
| 3900 | DirectiveKindMap[".common"] = DK_COMMON; |
| 3901 | DirectiveKindMap[".lcomm"] = DK_LCOMM; |
| 3902 | DirectiveKindMap[".abort"] = DK_ABORT; |
| 3903 | DirectiveKindMap[".include"] = DK_INCLUDE; |
| 3904 | DirectiveKindMap[".incbin"] = DK_INCBIN; |
| 3905 | DirectiveKindMap[".code16"] = DK_CODE16; |
| 3906 | DirectiveKindMap[".code16gcc"] = DK_CODE16GCC; |
| 3907 | DirectiveKindMap[".rept"] = DK_REPT; |
Saleem Abdulrasool | d743d0a | 2013-12-28 05:54:33 +0000 | [diff] [blame] | 3908 | DirectiveKindMap[".rep"] = DK_REPT; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3909 | DirectiveKindMap[".irp"] = DK_IRP; |
| 3910 | DirectiveKindMap[".irpc"] = DK_IRPC; |
| 3911 | DirectiveKindMap[".endr"] = DK_ENDR; |
| 3912 | DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE; |
| 3913 | DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK; |
| 3914 | DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK; |
| 3915 | DirectiveKindMap[".if"] = DK_IF; |
| 3916 | DirectiveKindMap[".ifb"] = DK_IFB; |
| 3917 | DirectiveKindMap[".ifnb"] = DK_IFNB; |
| 3918 | DirectiveKindMap[".ifc"] = DK_IFC; |
| 3919 | DirectiveKindMap[".ifnc"] = DK_IFNC; |
| 3920 | DirectiveKindMap[".ifdef"] = DK_IFDEF; |
| 3921 | DirectiveKindMap[".ifndef"] = DK_IFNDEF; |
| 3922 | DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF; |
| 3923 | DirectiveKindMap[".elseif"] = DK_ELSEIF; |
| 3924 | DirectiveKindMap[".else"] = DK_ELSE; |
Saleem Abdulrasool | 88186c4 | 2013-12-18 02:53:03 +0000 | [diff] [blame] | 3925 | DirectiveKindMap[".end"] = DK_END; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3926 | DirectiveKindMap[".endif"] = DK_ENDIF; |
| 3927 | DirectiveKindMap[".skip"] = DK_SKIP; |
| 3928 | DirectiveKindMap[".space"] = DK_SPACE; |
| 3929 | DirectiveKindMap[".file"] = DK_FILE; |
| 3930 | DirectiveKindMap[".line"] = DK_LINE; |
| 3931 | DirectiveKindMap[".loc"] = DK_LOC; |
| 3932 | DirectiveKindMap[".stabs"] = DK_STABS; |
| 3933 | DirectiveKindMap[".sleb128"] = DK_SLEB128; |
| 3934 | DirectiveKindMap[".uleb128"] = DK_ULEB128; |
| 3935 | DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS; |
| 3936 | DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC; |
| 3937 | DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC; |
| 3938 | DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA; |
| 3939 | DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET; |
| 3940 | DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET; |
| 3941 | DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER; |
| 3942 | DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET; |
| 3943 | DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET; |
| 3944 | DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY; |
| 3945 | DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA; |
| 3946 | DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE; |
| 3947 | DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE; |
| 3948 | DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE; |
| 3949 | DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE; |
| 3950 | DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE; |
| 3951 | DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME; |
| 3952 | DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED; |
| 3953 | DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER; |
Venkatraman Govindaraju | 3816d43 | 2013-09-26 14:49:40 +0000 | [diff] [blame] | 3954 | DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3955 | DirectiveKindMap[".macros_on"] = DK_MACROS_ON; |
| 3956 | DirectiveKindMap[".macros_off"] = DK_MACROS_OFF; |
| 3957 | DirectiveKindMap[".macro"] = DK_MACRO; |
| 3958 | DirectiveKindMap[".endm"] = DK_ENDM; |
| 3959 | DirectiveKindMap[".endmacro"] = DK_ENDMACRO; |
| 3960 | DirectiveKindMap[".purgem"] = DK_PURGEM; |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 3961 | } |
| 3962 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3963 | MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) { |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 3964 | AsmToken EndToken, StartToken = getTok(); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 3965 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 3966 | unsigned NestLevel = 0; |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 3967 | for (;;) { |
| 3968 | // Check whether we have reached the end of the file. |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 3969 | if (getLexer().is(AsmToken::Eof)) { |
| 3970 | Error(DirectiveLoc, "no matching '.endr' in definition"); |
| 3971 | return 0; |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 3972 | } |
| 3973 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 3974 | if (Lexer.is(AsmToken::Identifier) && |
| 3975 | (getTok().getIdentifier() == ".rept")) { |
| 3976 | ++NestLevel; |
| 3977 | } |
| 3978 | |
| 3979 | // Otherwise, check whether we have reached the .endr. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3980 | if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") { |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 3981 | if (NestLevel == 0) { |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 3982 | EndToken = getTok(); |
| 3983 | Lex(); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 3984 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
| 3985 | TokError("unexpected token in '.endr' directive"); |
| 3986 | return 0; |
| 3987 | } |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 3988 | break; |
| 3989 | } |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 3990 | --NestLevel; |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 3991 | } |
| 3992 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 3993 | // Otherwise, scan till the end of the statement. |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3994 | eatToEndOfStatement(); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 3995 | } |
| 3996 | |
| 3997 | const char *BodyStart = StartToken.getLoc().getPointer(); |
| 3998 | const char *BodyEnd = EndToken.getLoc().getPointer(); |
| 3999 | StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart); |
| 4000 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4001 | // We Are Anonymous. |
| 4002 | StringRef Name; |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 4003 | MCAsmMacroParameters Parameters; |
Benjamin Kramer | 1df3a1f | 2013-08-04 09:06:29 +0000 | [diff] [blame] | 4004 | MacroLikeBodies.push_back(MCAsmMacro(Name, Body, Parameters)); |
| 4005 | return &MacroLikeBodies.back(); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4006 | } |
| 4007 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4008 | void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc, |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4009 | raw_svector_ostream &OS) { |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4010 | OS << ".endr\n"; |
| 4011 | |
| 4012 | MemoryBuffer *Instantiation = |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4013 | MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>"); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4014 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4015 | // Create the macro instantiation object and add to the current macro |
| 4016 | // instantiation stack. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4017 | MacroInstantiation *MI = new MacroInstantiation( |
| 4018 | M, DirectiveLoc, CurBuffer, getTok().getLoc(), Instantiation); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4019 | ActiveMacros.push_back(MI); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4020 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4021 | // Jump to the macro instantiation and prime the lexer. |
| 4022 | CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc()); |
| 4023 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)); |
| 4024 | Lex(); |
| 4025 | } |
| 4026 | |
Saleem Abdulrasool | d743d0a | 2013-12-28 05:54:33 +0000 | [diff] [blame] | 4027 | /// parseDirectiveRept |
| 4028 | /// ::= .rep | .rept count |
| 4029 | bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) { |
Saleem Abdulrasool | 51cff71 | 2013-12-28 06:39:29 +0000 | [diff] [blame] | 4030 | const MCExpr *CountExpr; |
| 4031 | SMLoc CountLoc = getTok().getLoc(); |
| 4032 | if (parseExpression(CountExpr)) |
| 4033 | return true; |
| 4034 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4035 | int64_t Count; |
Saleem Abdulrasool | 51cff71 | 2013-12-28 06:39:29 +0000 | [diff] [blame] | 4036 | if (!CountExpr->EvaluateAsAbsolute(Count)) { |
| 4037 | eatToEndOfStatement(); |
| 4038 | return Error(CountLoc, "unexpected token in '" + Dir + "' directive"); |
| 4039 | } |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4040 | |
| 4041 | if (Count < 0) |
Saleem Abdulrasool | 51cff71 | 2013-12-28 06:39:29 +0000 | [diff] [blame] | 4042 | return Error(CountLoc, "Count is negative"); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4043 | |
| 4044 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
Saleem Abdulrasool | d743d0a | 2013-12-28 05:54:33 +0000 | [diff] [blame] | 4045 | return TokError("unexpected token in '" + Dir + "' directive"); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4046 | |
| 4047 | // Eat the end of statement. |
| 4048 | Lex(); |
| 4049 | |
| 4050 | // Lex the rept definition. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4051 | MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4052 | if (!M) |
| 4053 | return true; |
| 4054 | |
| 4055 | // Macro instantiation is lexical, unfortunately. We construct a new buffer |
| 4056 | // to hold the macro body with substitutions. |
| 4057 | SmallString<256> Buf; |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 4058 | MCAsmMacroParameters Parameters; |
| 4059 | MCAsmMacroArguments A; |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4060 | raw_svector_ostream OS(Buf); |
| 4061 | while (Count--) { |
| 4062 | if (expandMacro(OS, M->Body, Parameters, A, getTok().getLoc())) |
| 4063 | return true; |
| 4064 | } |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4065 | instantiateMacroLikeBody(M, DirectiveLoc, OS); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4066 | |
| 4067 | return false; |
| 4068 | } |
| 4069 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4070 | /// parseDirectiveIrp |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 4071 | /// ::= .irp symbol,values |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4072 | bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) { |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 4073 | MCAsmMacroParameters Parameters; |
| 4074 | MCAsmMacroParameter Parameter; |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 4075 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4076 | if (parseIdentifier(Parameter.first)) |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 4077 | return TokError("expected identifier in '.irp' directive"); |
| 4078 | |
| 4079 | Parameters.push_back(Parameter); |
| 4080 | |
| 4081 | if (Lexer.isNot(AsmToken::Comma)) |
| 4082 | return TokError("expected comma in '.irp' directive"); |
| 4083 | |
| 4084 | Lex(); |
| 4085 | |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 4086 | MCAsmMacroArguments A; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4087 | if (parseMacroArguments(0, A)) |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 4088 | return true; |
| 4089 | |
| 4090 | // Eat the end of statement. |
| 4091 | Lex(); |
| 4092 | |
| 4093 | // Lex the irp definition. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4094 | MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc); |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 4095 | if (!M) |
| 4096 | return true; |
| 4097 | |
| 4098 | // Macro instantiation is lexical, unfortunately. We construct a new buffer |
| 4099 | // to hold the macro body with substitutions. |
| 4100 | SmallString<256> Buf; |
| 4101 | raw_svector_ostream OS(Buf); |
| 4102 | |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 4103 | for (MCAsmMacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) { |
| 4104 | MCAsmMacroArguments Args; |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 4105 | Args.push_back(*i); |
| 4106 | |
| 4107 | if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc())) |
| 4108 | return true; |
| 4109 | } |
| 4110 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4111 | instantiateMacroLikeBody(M, DirectiveLoc, OS); |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 4112 | |
| 4113 | return false; |
| 4114 | } |
| 4115 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4116 | /// parseDirectiveIrpc |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 4117 | /// ::= .irpc symbol,values |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4118 | bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) { |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 4119 | MCAsmMacroParameters Parameters; |
| 4120 | MCAsmMacroParameter Parameter; |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 4121 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4122 | if (parseIdentifier(Parameter.first)) |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 4123 | return TokError("expected identifier in '.irpc' directive"); |
| 4124 | |
| 4125 | Parameters.push_back(Parameter); |
| 4126 | |
| 4127 | if (Lexer.isNot(AsmToken::Comma)) |
| 4128 | return TokError("expected comma in '.irpc' directive"); |
| 4129 | |
| 4130 | Lex(); |
| 4131 | |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 4132 | MCAsmMacroArguments A; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4133 | if (parseMacroArguments(0, A)) |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 4134 | return true; |
| 4135 | |
| 4136 | if (A.size() != 1 || A.front().size() != 1) |
| 4137 | return TokError("unexpected token in '.irpc' directive"); |
| 4138 | |
| 4139 | // Eat the end of statement. |
| 4140 | Lex(); |
| 4141 | |
| 4142 | // Lex the irpc definition. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4143 | MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc); |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 4144 | if (!M) |
| 4145 | return true; |
| 4146 | |
| 4147 | // Macro instantiation is lexical, unfortunately. We construct a new buffer |
| 4148 | // to hold the macro body with substitutions. |
| 4149 | SmallString<256> Buf; |
| 4150 | raw_svector_ostream OS(Buf); |
| 4151 | |
| 4152 | StringRef Values = A.front().front().getString(); |
| 4153 | std::size_t I, End = Values.size(); |
| 4154 | for (I = 0; I < End; ++I) { |
Eli Bendersky | a7b905e | 2013-01-14 19:00:26 +0000 | [diff] [blame] | 4155 | MCAsmMacroArgument Arg; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4156 | Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I + 1))); |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 4157 | |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 4158 | MCAsmMacroArguments Args; |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 4159 | Args.push_back(Arg); |
| 4160 | |
| 4161 | if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc())) |
| 4162 | return true; |
| 4163 | } |
| 4164 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4165 | instantiateMacroLikeBody(M, DirectiveLoc, OS); |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 4166 | |
| 4167 | return false; |
| 4168 | } |
| 4169 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4170 | bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) { |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4171 | if (ActiveMacros.empty()) |
Preston Gurd | eb3ebf1 | 2012-09-19 20:23:43 +0000 | [diff] [blame] | 4172 | return TokError("unmatched '.endr' directive"); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4173 | |
| 4174 | // The only .repl that should get here are the ones created by |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4175 | // instantiateMacroLikeBody. |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4176 | assert(getLexer().is(AsmToken::EndOfStatement)); |
| 4177 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4178 | handleMacroExit(); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4179 | return false; |
| 4180 | } |
Rafael Espindola | 12d73d1 | 2010-09-11 16:45:15 +0000 | [diff] [blame] | 4181 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4182 | bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info, |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4183 | size_t Len) { |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 4184 | const MCExpr *Value; |
| 4185 | SMLoc ExprLoc = getLexer().getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4186 | if (parseExpression(Value)) |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 4187 | return true; |
| 4188 | const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value); |
| 4189 | if (!MCE) |
| 4190 | return Error(ExprLoc, "unexpected expression in _emit"); |
| 4191 | uint64_t IntValue = MCE->getValue(); |
| 4192 | if (!isUIntN(8, IntValue) && !isIntN(8, IntValue)) |
| 4193 | return Error(ExprLoc, "literal value out of range for directive"); |
| 4194 | |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 4195 | Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, Len)); |
| 4196 | return false; |
| 4197 | } |
| 4198 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4199 | bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) { |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 4200 | const MCExpr *Value; |
| 4201 | SMLoc ExprLoc = getLexer().getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4202 | if (parseExpression(Value)) |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 4203 | return true; |
| 4204 | const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value); |
| 4205 | if (!MCE) |
| 4206 | return Error(ExprLoc, "unexpected expression in align"); |
| 4207 | uint64_t IntValue = MCE->getValue(); |
| 4208 | if (!isPowerOf2_64(IntValue)) |
| 4209 | return Error(ExprLoc, "literal value not a power of two greater then zero"); |
| 4210 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4211 | Info.AsmRewrites->push_back( |
| 4212 | AsmRewrite(AOK_Align, IDLoc, 5, Log2_64(IntValue))); |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 4213 | return false; |
| 4214 | } |
| 4215 | |
Chad Rosier | f43fcf5 | 2013-02-13 21:27:17 +0000 | [diff] [blame] | 4216 | // We are comparing pointers, but the pointers are relative to a single string. |
| 4217 | // Thus, this should always be deterministic. |
Benjamin Kramer | 8817cca | 2013-09-22 14:09:50 +0000 | [diff] [blame] | 4218 | static int rewritesSort(const AsmRewrite *AsmRewriteA, |
| 4219 | const AsmRewrite *AsmRewriteB) { |
Chad Rosier | eb5c168 | 2013-02-13 18:38:58 +0000 | [diff] [blame] | 4220 | if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer()) |
| 4221 | return -1; |
| 4222 | if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer()) |
| 4223 | return 1; |
Chad Rosier | 42d4e2e | 2013-02-15 22:54:16 +0000 | [diff] [blame] | 4224 | |
Chad Rosier | fce4fab | 2013-04-08 17:43:47 +0000 | [diff] [blame] | 4225 | // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output |
| 4226 | // rewrite to the same location. Make sure the SizeDirective rewrite is |
| 4227 | // performed first, then the Imm/ImmPrefix and finally the Input/Output. This |
| 4228 | // ensures the sort algorithm is stable. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4229 | if (AsmRewritePrecedence[AsmRewriteA->Kind] > |
| 4230 | AsmRewritePrecedence[AsmRewriteB->Kind]) |
Chad Rosier | 42d4e2e | 2013-02-15 22:54:16 +0000 | [diff] [blame] | 4231 | return -1; |
Chad Rosier | fce4fab | 2013-04-08 17:43:47 +0000 | [diff] [blame] | 4232 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4233 | if (AsmRewritePrecedence[AsmRewriteA->Kind] < |
| 4234 | AsmRewritePrecedence[AsmRewriteB->Kind]) |
Chad Rosier | 42d4e2e | 2013-02-15 22:54:16 +0000 | [diff] [blame] | 4235 | return 1; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4236 | llvm_unreachable("Unstable rewrite sort."); |
Chad Rosier | b2144ce | 2013-02-13 01:03:13 +0000 | [diff] [blame] | 4237 | } |
| 4238 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4239 | bool AsmParser::parseMSInlineAsm( |
| 4240 | void *AsmLoc, std::string &AsmString, unsigned &NumOutputs, |
| 4241 | unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls, |
| 4242 | SmallVectorImpl<std::string> &Constraints, |
| 4243 | SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII, |
| 4244 | const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) { |
Chad Rosier | 37e755c | 2012-10-23 17:43:43 +0000 | [diff] [blame] | 4245 | SmallVector<void *, 4> InputDecls; |
| 4246 | SmallVector<void *, 4> OutputDecls; |
Chad Rosier | a4bc943 | 2013-01-10 22:10:27 +0000 | [diff] [blame] | 4247 | SmallVector<bool, 4> InputDeclsAddressOf; |
| 4248 | SmallVector<bool, 4> OutputDeclsAddressOf; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4249 | SmallVector<std::string, 4> InputConstraints; |
| 4250 | SmallVector<std::string, 4> OutputConstraints; |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4251 | SmallVector<unsigned, 4> ClobberRegs; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4252 | |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4253 | SmallVector<AsmRewrite, 4> AsmStrRewrites; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4254 | |
| 4255 | // Prime the lexer. |
| 4256 | Lex(); |
| 4257 | |
| 4258 | // While we have input, parse each statement. |
| 4259 | unsigned InputIdx = 0; |
| 4260 | unsigned OutputIdx = 0; |
| 4261 | while (getLexer().isNot(AsmToken::Eof)) { |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 4262 | ParseStatementInfo Info(&AsmStrRewrites); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4263 | if (parseStatement(Info)) |
Chad Rosier | f1f6a72 | 2012-10-19 22:57:33 +0000 | [diff] [blame] | 4264 | return true; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4265 | |
Chad Rosier | 149e8e0 | 2012-12-12 22:45:52 +0000 | [diff] [blame] | 4266 | if (Info.ParseError) |
| 4267 | return true; |
| 4268 | |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4269 | if (Info.Opcode == ~0U) |
| 4270 | continue; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4271 | |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4272 | const MCInstrDesc &Desc = MII->get(Info.Opcode); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4273 | |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4274 | // Build the list of clobbers, outputs and inputs. |
| 4275 | for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) { |
| 4276 | MCParsedAsmOperand *Operand = Info.ParsedOperands[i]; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4277 | |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4278 | // Immediate. |
Chad Rosier | f3c04f6 | 2013-03-19 21:58:18 +0000 | [diff] [blame] | 4279 | if (Operand->isImm()) |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4280 | continue; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4281 | |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4282 | // Register operand. |
| 4283 | if (Operand->isReg() && !Operand->needAddressOf()) { |
| 4284 | unsigned NumDefs = Desc.getNumDefs(); |
| 4285 | // Clobber. |
| 4286 | if (NumDefs && Operand->getMCOperandNum() < NumDefs) |
| 4287 | ClobberRegs.push_back(Operand->getReg()); |
| 4288 | continue; |
| 4289 | } |
| 4290 | |
| 4291 | // Expr/Input or Output. |
Chad Rosier | e81309b | 2013-04-09 17:53:49 +0000 | [diff] [blame] | 4292 | StringRef SymName = Operand->getSymName(); |
| 4293 | if (SymName.empty()) |
| 4294 | continue; |
| 4295 | |
Chad Rosier | dba3fe5 | 2013-04-22 22:12:12 +0000 | [diff] [blame] | 4296 | void *OpDecl = Operand->getOpDecl(); |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4297 | if (!OpDecl) |
| 4298 | continue; |
| 4299 | |
| 4300 | bool isOutput = (i == 1) && Desc.mayStore(); |
Chad Rosier | e81309b | 2013-04-09 17:53:49 +0000 | [diff] [blame] | 4301 | SMLoc Start = SMLoc::getFromPointer(SymName.data()); |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4302 | if (isOutput) { |
| 4303 | ++InputIdx; |
| 4304 | OutputDecls.push_back(OpDecl); |
| 4305 | OutputDeclsAddressOf.push_back(Operand->needAddressOf()); |
| 4306 | OutputConstraints.push_back('=' + Operand->getConstraint().str()); |
Chad Rosier | e81309b | 2013-04-09 17:53:49 +0000 | [diff] [blame] | 4307 | AsmStrRewrites.push_back(AsmRewrite(AOK_Output, Start, SymName.size())); |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4308 | } else { |
| 4309 | InputDecls.push_back(OpDecl); |
| 4310 | InputDeclsAddressOf.push_back(Operand->needAddressOf()); |
| 4311 | InputConstraints.push_back(Operand->getConstraint().str()); |
Chad Rosier | e81309b | 2013-04-09 17:53:49 +0000 | [diff] [blame] | 4312 | AsmStrRewrites.push_back(AsmRewrite(AOK_Input, Start, SymName.size())); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4313 | } |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4314 | } |
Reid Kleckner | ee08897 | 2013-12-10 18:27:32 +0000 | [diff] [blame] | 4315 | |
| 4316 | // Consider implicit defs to be clobbers. Think of cpuid and push. |
| 4317 | const uint16_t *ImpDefs = Desc.getImplicitDefs(); |
| 4318 | for (unsigned I = 0, E = Desc.getNumImplicitDefs(); I != E; ++I) |
| 4319 | ClobberRegs.push_back(ImpDefs[I]); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4320 | } |
| 4321 | |
| 4322 | // Set the number of Outputs and Inputs. |
Chad Rosier | f641baa | 2012-10-18 19:39:30 +0000 | [diff] [blame] | 4323 | NumOutputs = OutputDecls.size(); |
| 4324 | NumInputs = InputDecls.size(); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4325 | |
| 4326 | // Set the unique clobbers. |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4327 | array_pod_sort(ClobberRegs.begin(), ClobberRegs.end()); |
| 4328 | ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()), |
| 4329 | ClobberRegs.end()); |
| 4330 | Clobbers.assign(ClobberRegs.size(), std::string()); |
| 4331 | for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) { |
| 4332 | raw_string_ostream OS(Clobbers[I]); |
| 4333 | IP->printRegName(OS, ClobberRegs[I]); |
| 4334 | } |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4335 | |
| 4336 | // Merge the various outputs and inputs. Output are expected first. |
| 4337 | if (NumOutputs || NumInputs) { |
| 4338 | unsigned NumExprs = NumOutputs + NumInputs; |
Chad Rosier | f641baa | 2012-10-18 19:39:30 +0000 | [diff] [blame] | 4339 | OpDecls.resize(NumExprs); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4340 | Constraints.resize(NumExprs); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4341 | for (unsigned i = 0; i < NumOutputs; ++i) { |
Chad Rosier | a4bc943 | 2013-01-10 22:10:27 +0000 | [diff] [blame] | 4342 | OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]); |
Chad Rosier | 7245033 | 2013-01-15 23:07:53 +0000 | [diff] [blame] | 4343 | Constraints[i] = OutputConstraints[i]; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4344 | } |
| 4345 | for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) { |
Chad Rosier | a4bc943 | 2013-01-10 22:10:27 +0000 | [diff] [blame] | 4346 | OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]); |
Chad Rosier | 7245033 | 2013-01-15 23:07:53 +0000 | [diff] [blame] | 4347 | Constraints[j] = InputConstraints[i]; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4348 | } |
| 4349 | } |
| 4350 | |
| 4351 | // Build the IR assembly string. |
| 4352 | std::string AsmStringIR; |
| 4353 | raw_string_ostream OS(AsmStringIR); |
Chad Rosier | 17d3799 | 2013-03-19 21:12:14 +0000 | [diff] [blame] | 4354 | const char *AsmStart = SrcMgr.getMemoryBuffer(0)->getBufferStart(); |
| 4355 | const char *AsmEnd = SrcMgr.getMemoryBuffer(0)->getBufferEnd(); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4356 | array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort); |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4357 | for (SmallVectorImpl<AsmRewrite>::iterator I = AsmStrRewrites.begin(), |
| 4358 | E = AsmStrRewrites.end(); |
| 4359 | I != E; ++I) { |
Chad Rosier | ff10ed1 | 2013-04-12 16:26:42 +0000 | [diff] [blame] | 4360 | AsmRewriteKind Kind = (*I).Kind; |
| 4361 | if (Kind == AOK_Delete) |
| 4362 | continue; |
| 4363 | |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4364 | const char *Loc = (*I).Loc.getPointer(); |
Chad Rosier | 17d3799 | 2013-03-19 21:12:14 +0000 | [diff] [blame] | 4365 | assert(Loc >= AsmStart && "Expected Loc to be at or after Start!"); |
Chad Rosier | 0f48c55 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 4366 | |
Chad Rosier | 120eefd | 2013-03-19 17:32:17 +0000 | [diff] [blame] | 4367 | // Emit everything up to the immediate/expression. |
Chad Rosier | 17d3799 | 2013-03-19 21:12:14 +0000 | [diff] [blame] | 4368 | unsigned Len = Loc - AsmStart; |
Chad Rosier | 8fb8330 | 2013-04-11 21:49:30 +0000 | [diff] [blame] | 4369 | if (Len) |
Chad Rosier | 17d3799 | 2013-03-19 21:12:14 +0000 | [diff] [blame] | 4370 | OS << StringRef(AsmStart, Len); |
Chad Rosier | 0f48c55 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 4371 | |
Chad Rosier | 37e755c | 2012-10-23 17:43:43 +0000 | [diff] [blame] | 4372 | // Skip the original expression. |
| 4373 | if (Kind == AOK_Skip) { |
Chad Rosier | 17d3799 | 2013-03-19 21:12:14 +0000 | [diff] [blame] | 4374 | AsmStart = Loc + (*I).Len; |
Chad Rosier | 37e755c | 2012-10-23 17:43:43 +0000 | [diff] [blame] | 4375 | continue; |
| 4376 | } |
| 4377 | |
Chad Rosier | ff10ed1 | 2013-04-12 16:26:42 +0000 | [diff] [blame] | 4378 | unsigned AdditionalSkip = 0; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4379 | // Rewrite expressions in $N notation. |
Chad Rosier | 0f48c55 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 4380 | switch (Kind) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4381 | default: |
| 4382 | break; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4383 | case AOK_Imm: |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4384 | OS << "$$" << (*I).Val; |
Chad Rosier | 11c42f2 | 2012-10-26 18:04:20 +0000 | [diff] [blame] | 4385 | break; |
| 4386 | case AOK_ImmPrefix: |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4387 | OS << "$$"; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4388 | break; |
| 4389 | case AOK_Input: |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4390 | OS << '$' << InputIdx++; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4391 | break; |
| 4392 | case AOK_Output: |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4393 | OS << '$' << OutputIdx++; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4394 | break; |
Chad Rosier | 0f48c55 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 4395 | case AOK_SizeDirective: |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4396 | switch ((*I).Val) { |
Chad Rosier | 0f48c55 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 4397 | default: break; |
| 4398 | case 8: OS << "byte ptr "; break; |
| 4399 | case 16: OS << "word ptr "; break; |
| 4400 | case 32: OS << "dword ptr "; break; |
| 4401 | case 64: OS << "qword ptr "; break; |
| 4402 | case 80: OS << "xword ptr "; break; |
| 4403 | case 128: OS << "xmmword ptr "; break; |
| 4404 | case 256: OS << "ymmword ptr "; break; |
| 4405 | } |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 4406 | break; |
| 4407 | case AOK_Emit: |
| 4408 | OS << ".byte"; |
| 4409 | break; |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 4410 | case AOK_Align: { |
| 4411 | unsigned Val = (*I).Val; |
| 4412 | OS << ".align " << Val; |
| 4413 | |
| 4414 | // Skip the original immediate. |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4415 | assert(Val < 10 && "Expected alignment less then 2^10."); |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 4416 | AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4; |
| 4417 | break; |
| 4418 | } |
Chad Rosier | f0e8720 | 2012-10-25 20:41:34 +0000 | [diff] [blame] | 4419 | case AOK_DotOperator: |
| 4420 | OS << (*I).Val; |
| 4421 | break; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4422 | } |
Chad Rosier | 0f48c55 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 4423 | |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4424 | // Skip the original expression. |
Chad Rosier | 17d3799 | 2013-03-19 21:12:14 +0000 | [diff] [blame] | 4425 | AsmStart = Loc + (*I).Len + AdditionalSkip; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4426 | } |
| 4427 | |
| 4428 | // Emit the remainder of the asm string. |
Chad Rosier | 17d3799 | 2013-03-19 21:12:14 +0000 | [diff] [blame] | 4429 | if (AsmStart != AsmEnd) |
| 4430 | OS << StringRef(AsmStart, AsmEnd - AsmStart); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4431 | |
| 4432 | AsmString = OS.str(); |
| 4433 | return false; |
| 4434 | } |
| 4435 | |
Daniel Dunbar | 01e3607 | 2010-07-17 02:26:10 +0000 | [diff] [blame] | 4436 | /// \brief Create an MCAsmParser instance. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4437 | MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C, |
| 4438 | MCStreamer &Out, const MCAsmInfo &MAI) { |
Jim Grosbach | 345768c | 2011-08-16 18:33:49 +0000 | [diff] [blame] | 4439 | return new AsmParser(SM, C, Out, MAI); |
Daniel Dunbar | 01e3607 | 2010-07-17 02:26:10 +0000 | [diff] [blame] | 4440 | } |