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> |
Benjamin Kramer | d59664f | 2014-04-29 23:26:49 +0000 | [diff] [blame] | 42 | #include <deque> |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 43 | #include <set> |
| 44 | #include <string> |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 45 | #include <vector> |
Chris Lattner | b013345 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 46 | using namespace llvm; |
| 47 | |
Eric Christopher | a7c3273 | 2012-12-18 00:30:54 +0000 | [diff] [blame] | 48 | MCAsmParserSemaCallback::~MCAsmParserSemaCallback() {} |
Nick Lewycky | ac61227 | 2012-10-19 07:00:09 +0000 | [diff] [blame] | 49 | |
Daniel Dunbar | 8603340 | 2010-07-12 17:54:38 +0000 | [diff] [blame] | 50 | namespace { |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 51 | /// \brief Helper types for tracking macro definitions. |
| 52 | typedef std::vector<AsmToken> MCAsmMacroArgument; |
| 53 | typedef std::vector<MCAsmMacroArgument> MCAsmMacroArguments; |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 54 | |
| 55 | struct MCAsmMacroParameter { |
| 56 | StringRef Name; |
| 57 | MCAsmMacroArgument Value; |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 58 | bool Required; |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 59 | bool Vararg; |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 60 | |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 61 | MCAsmMacroParameter() : Required(false), Vararg(false) {} |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 62 | }; |
| 63 | |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 64 | typedef std::vector<MCAsmMacroParameter> MCAsmMacroParameters; |
| 65 | |
| 66 | struct MCAsmMacro { |
| 67 | StringRef Name; |
| 68 | StringRef Body; |
| 69 | MCAsmMacroParameters Parameters; |
| 70 | |
| 71 | public: |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 72 | MCAsmMacro(StringRef N, StringRef B, MCAsmMacroParameters P) |
| 73 | : Name(N), Body(B), Parameters(std::move(P)) {} |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 74 | }; |
| 75 | |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 76 | /// \brief Helper class for storing information about an active macro |
| 77 | /// instantiation. |
| 78 | struct MacroInstantiation { |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 79 | /// The location of the instantiation. |
| 80 | SMLoc InstantiationLoc; |
| 81 | |
Daniel Dunbar | 40f1d85 | 2012-12-01 01:38:48 +0000 | [diff] [blame] | 82 | /// The buffer where parsing should resume upon instantiation completion. |
| 83 | int ExitBuffer; |
| 84 | |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 85 | /// The location where parsing should resume upon instantiation completion. |
| 86 | SMLoc ExitLoc; |
| 87 | |
Nico Weber | 155dccd1 | 2014-07-24 17:08:39 +0000 | [diff] [blame] | 88 | /// The depth of TheCondStack at the start of the instantiation. |
| 89 | size_t CondStackDepth; |
| 90 | |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 91 | public: |
Rafael Espindola | 9eef18c | 2014-08-27 19:49:03 +0000 | [diff] [blame] | 92 | MacroInstantiation(SMLoc IL, int EB, SMLoc EL, size_t CondStackDepth); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 93 | }; |
| 94 | |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 95 | struct ParseStatementInfo { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 96 | /// \brief The parsed operands from the last parsed statement. |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 97 | SmallVector<std::unique_ptr<MCParsedAsmOperand>, 8> ParsedOperands; |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 98 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 99 | /// \brief The opcode from the last parsed instruction. |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 100 | unsigned Opcode; |
| 101 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 102 | /// \brief Was there an error parsing the inline assembly? |
Chad Rosier | 149e8e0 | 2012-12-12 22:45:52 +0000 | [diff] [blame] | 103 | bool ParseError; |
| 104 | |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 105 | SmallVectorImpl<AsmRewrite> *AsmRewrites; |
| 106 | |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 107 | ParseStatementInfo() : Opcode(~0U), ParseError(false), AsmRewrites(nullptr) {} |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 108 | ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites) |
Chad Rosier | 149e8e0 | 2012-12-12 22:45:52 +0000 | [diff] [blame] | 109 | : Opcode(~0), ParseError(false), AsmRewrites(rewrites) {} |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 110 | }; |
| 111 | |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 112 | /// \brief The concrete assembly parser instance. |
| 113 | class AsmParser : public MCAsmParser { |
Aaron Ballman | f9a1897 | 2015-02-15 22:54:22 +0000 | [diff] [blame] | 114 | AsmParser(const AsmParser &) = delete; |
| 115 | void operator=(const AsmParser &) = delete; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 116 | private: |
| 117 | AsmLexer Lexer; |
| 118 | MCContext &Ctx; |
| 119 | MCStreamer &Out; |
Jim Grosbach | c7e6b8f | 2011-06-15 18:33:28 +0000 | [diff] [blame] | 120 | const MCAsmInfo &MAI; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 121 | SourceMgr &SrcMgr; |
Benjamin Kramer | 47f5e30 | 2011-10-16 10:48:29 +0000 | [diff] [blame] | 122 | SourceMgr::DiagHandlerTy SavedDiagHandler; |
| 123 | void *SavedDiagContext; |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 124 | std::unique_ptr<MCAsmParserExtension> PlatformParser; |
Rafael Espindola | 82065cb | 2011-04-11 21:49:50 +0000 | [diff] [blame] | 125 | |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 126 | /// This is the current buffer index we're lexing from as managed by the |
| 127 | /// SourceMgr object. |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 128 | unsigned CurBuffer; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 129 | |
| 130 | AsmCond TheCondState; |
| 131 | std::vector<AsmCond> TheCondStack; |
| 132 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 133 | /// \brief maps directive names to handler methods in parser |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 134 | /// extensions. Extensions register themselves in this map by calling |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 135 | /// addDirectiveHandler. |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 136 | StringMap<ExtensionDirectiveHandler> ExtensionDirectiveMap; |
Daniel Dunbar | 828984f | 2010-07-18 18:38:02 +0000 | [diff] [blame] | 137 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 138 | /// \brief Map of currently defined macros. |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 139 | StringMap<MCAsmMacro> MacroMap; |
Daniel Dunbar | c1f58ec | 2010-07-18 18:47:21 +0000 | [diff] [blame] | 140 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 141 | /// \brief Stack of active macro instantiations. |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 142 | std::vector<MacroInstantiation*> ActiveMacros; |
| 143 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 144 | /// \brief List of bodies of anonymous macros. |
Benjamin Kramer | 1df3a1f | 2013-08-04 09:06:29 +0000 | [diff] [blame] | 145 | std::deque<MCAsmMacro> MacroLikeBodies; |
| 146 | |
Daniel Dunbar | 828984f | 2010-07-18 18:38:02 +0000 | [diff] [blame] | 147 | /// Boolean tracking whether macro substitution is enabled. |
Eli Bendersky | c2f6f92 | 2013-01-14 18:08:41 +0000 | [diff] [blame] | 148 | unsigned MacrosEnabledFlag : 1; |
Daniel Dunbar | 828984f | 2010-07-18 18:38:02 +0000 | [diff] [blame] | 149 | |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 150 | /// \brief Keeps track of how many .macro's have been instantiated. |
| 151 | unsigned NumOfMacroInstantiations; |
| 152 | |
Daniel Dunbar | 43325c4 | 2010-09-09 22:42:56 +0000 | [diff] [blame] | 153 | /// Flag tracking whether any errors have been encountered. |
| 154 | unsigned HadError : 1; |
| 155 | |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 156 | /// The values from the last parsed cpp hash file line comment if any. |
| 157 | StringRef CppHashFilename; |
| 158 | int64_t CppHashLineNumber; |
| 159 | SMLoc CppHashLoc; |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 160 | unsigned CppHashBuf; |
Kevin Enderby | 0fd064c | 2013-06-21 20:51:39 +0000 | [diff] [blame] | 161 | /// When generating dwarf for assembly source files we need to calculate the |
| 162 | /// 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] | 163 | /// 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] | 164 | /// cache we save the last info we queried with SrcMgr.FindLineNumber(). |
| 165 | SMLoc LastQueryIDLoc; |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 166 | unsigned LastQueryBuffer; |
Kevin Enderby | 0fd064c | 2013-06-21 20:51:39 +0000 | [diff] [blame] | 167 | unsigned LastQueryLine; |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 168 | |
Devang Patel | a173ee5 | 2012-01-31 18:14:05 +0000 | [diff] [blame] | 169 | /// AssemblerDialect. ~OU means unset value and use value provided by MAI. |
| 170 | unsigned AssemblerDialect; |
| 171 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 172 | /// \brief is Darwin compatibility enabled? |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 173 | bool IsDarwin; |
| 174 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 175 | /// \brief Are we parsing ms-style inline assembly? |
Chad Rosier | 4996355 | 2012-10-13 00:26:04 +0000 | [diff] [blame] | 176 | bool ParsingInlineAsm; |
| 177 | |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 178 | public: |
Jim Grosbach | 345768c | 2011-08-16 18:33:49 +0000 | [diff] [blame] | 179 | AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out, |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 180 | const MCAsmInfo &MAI); |
Alexander Kornienko | f817c1c | 2015-04-11 02:11:45 +0000 | [diff] [blame] | 181 | ~AsmParser() override; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 182 | |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 183 | bool Run(bool NoInitialTextSection, bool NoFinalize = false) override; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 184 | |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 185 | void addDirectiveHandler(StringRef Directive, |
| 186 | ExtensionDirectiveHandler Handler) override { |
Eli Bendersky | 29b9f47 | 2013-01-16 00:50:52 +0000 | [diff] [blame] | 187 | ExtensionDirectiveMap[Directive] = Handler; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Toma Tabacu | 11e14a9 | 2015-04-21 11:50:52 +0000 | [diff] [blame] | 190 | void addAliasForDirective(StringRef Directive, StringRef Alias) override { |
| 191 | DirectiveKindMap[Directive] = DirectiveKindMap[Alias]; |
| 192 | } |
| 193 | |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 194 | public: |
| 195 | /// @name MCAsmParser Interface |
| 196 | /// { |
| 197 | |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 198 | SourceMgr &getSourceManager() override { return SrcMgr; } |
| 199 | MCAsmLexer &getLexer() override { return Lexer; } |
| 200 | MCContext &getContext() override { return Ctx; } |
| 201 | MCStreamer &getStreamer() override { return Out; } |
| 202 | unsigned getAssemblerDialect() override { |
Devang Patel | a173ee5 | 2012-01-31 18:14:05 +0000 | [diff] [blame] | 203 | if (AssemblerDialect == ~0U) |
Eric Christopher | a7c3273 | 2012-12-18 00:30:54 +0000 | [diff] [blame] | 204 | return MAI.getAssemblerDialect(); |
Devang Patel | a173ee5 | 2012-01-31 18:14:05 +0000 | [diff] [blame] | 205 | else |
| 206 | return AssemblerDialect; |
| 207 | } |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 208 | void setAssemblerDialect(unsigned i) override { |
Devang Patel | a173ee5 | 2012-01-31 18:14:05 +0000 | [diff] [blame] | 209 | AssemblerDialect = i; |
| 210 | } |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 211 | |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 212 | void Note(SMLoc L, const Twine &Msg, |
| 213 | ArrayRef<SMRange> Ranges = None) override; |
| 214 | bool Warning(SMLoc L, const Twine &Msg, |
| 215 | ArrayRef<SMRange> Ranges = None) override; |
| 216 | bool Error(SMLoc L, const Twine &Msg, |
| 217 | ArrayRef<SMRange> Ranges = None) override; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 218 | |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 219 | const AsmToken &Lex() override; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 220 | |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 221 | void setParsingInlineAsm(bool V) override { ParsingInlineAsm = V; } |
| 222 | bool isParsingInlineAsm() override { return ParsingInlineAsm; } |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 223 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 224 | bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString, |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 225 | unsigned &NumOutputs, unsigned &NumInputs, |
Chad Rosier | 37e755c | 2012-10-23 17:43:43 +0000 | [diff] [blame] | 226 | SmallVectorImpl<std::pair<void *,bool> > &OpDecls, |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 227 | SmallVectorImpl<std::string> &Constraints, |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 228 | SmallVectorImpl<std::string> &Clobbers, |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 229 | const MCInstrInfo *MII, const MCInstPrinter *IP, |
| 230 | MCAsmParserSemaCallback &SI) override; |
Chad Rosier | 4996355 | 2012-10-13 00:26:04 +0000 | [diff] [blame] | 231 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 232 | bool parseExpression(const MCExpr *&Res); |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 233 | bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) override; |
| 234 | bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) override; |
| 235 | bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) override; |
| 236 | bool parseAbsoluteExpression(int64_t &Res) override; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 237 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 238 | /// \brief Parse an identifier or string (as a quoted identifier) |
Eli Bendersky | 0cf0cb9 | 2013-01-12 00:05:00 +0000 | [diff] [blame] | 239 | /// and set \p Res to the identifier contents. |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 240 | bool parseIdentifier(StringRef &Res) override; |
| 241 | void eatToEndOfStatement() override; |
Eli Bendersky | 0cf0cb9 | 2013-01-12 00:05:00 +0000 | [diff] [blame] | 242 | |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 243 | void checkForValidSection() override; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 244 | /// } |
| 245 | |
| 246 | private: |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 247 | |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 248 | bool parseStatement(ParseStatementInfo &Info, |
| 249 | MCAsmParserSemaCallback *SI); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 250 | void eatToEndOfLine(); |
| 251 | bool parseCppHashLineFilenameComment(const SMLoc &L); |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 252 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 253 | void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body, |
Benjamin Kramer | d31aaf1 | 2014-02-09 17:13:11 +0000 | [diff] [blame] | 254 | ArrayRef<MCAsmMacroParameter> Parameters); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 255 | bool expandMacro(raw_svector_ostream &OS, StringRef Body, |
Benjamin Kramer | d31aaf1 | 2014-02-09 17:13:11 +0000 | [diff] [blame] | 256 | ArrayRef<MCAsmMacroParameter> Parameters, |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 257 | ArrayRef<MCAsmMacroArgument> A, bool EnableAtPseudoVariable, |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 258 | const SMLoc &L); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 259 | |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 260 | /// \brief Are macros enabled in the parser? |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 261 | bool areMacrosEnabled() {return MacrosEnabledFlag;} |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 262 | |
| 263 | /// \brief Control a flag in the parser that enables or disables macros. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 264 | void setMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;} |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 265 | |
| 266 | /// \brief Lookup a previously defined macro. |
| 267 | /// \param Name Macro name. |
| 268 | /// \returns Pointer to macro. NULL if no such macro was defined. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 269 | const MCAsmMacro* lookupMacro(StringRef Name); |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 270 | |
| 271 | /// \brief Define a new macro with the given name and information. |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 272 | void defineMacro(StringRef Name, MCAsmMacro Macro); |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 273 | |
| 274 | /// \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] | 275 | void undefineMacro(StringRef Name); |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 276 | |
| 277 | /// \brief Are we inside a macro instantiation? |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 278 | bool isInsideMacroInstantiation() {return !ActiveMacros.empty();} |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 279 | |
Vladimir Medic | 9bad0d33 | 2013-08-20 13:33:18 +0000 | [diff] [blame] | 280 | /// \brief Handle entry to macro instantiation. |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 281 | /// |
| 282 | /// \param M The macro. |
| 283 | /// \param NameLoc Instantiation location. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 284 | bool handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc); |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 285 | |
| 286 | /// \brief Handle exit from macro instantiation. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 287 | void handleMacroExit(); |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 288 | |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 289 | /// \brief Extract AsmTokens for a macro argument. |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 290 | bool parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg); |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 291 | |
| 292 | /// \brief Parse all macro arguments for a given macro. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 293 | bool parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A); |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 294 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 295 | void printMacroInstantiations(); |
| 296 | void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg, |
Dmitri Gribenko | 3238fb7 | 2013-05-05 00:40:33 +0000 | [diff] [blame] | 297 | ArrayRef<SMRange> Ranges = None) const { |
Chris Lattner | 7284526 | 2011-10-16 05:47:55 +0000 | [diff] [blame] | 298 | SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges); |
Benjamin Kramer | c758311 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 299 | } |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 300 | static void DiagHandler(const SMDiagnostic &Diag, void *Context); |
Benjamin Kramer | c758311 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 301 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 302 | /// \brief Enter the specified file. This returns true on failure. |
| 303 | bool enterIncludeFile(const std::string &Filename); |
| 304 | |
| 305 | /// \brief Process the specified file for the .incbin directive. |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 306 | /// This returns true on failure. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 307 | bool processIncbinFile(const std::string &Filename); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 308 | |
Dmitri Gribenko | 5485acd | 2012-09-14 14:57:36 +0000 | [diff] [blame] | 309 | /// \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] | 310 | /// current token is not set; clients should ensure Lex() is called |
| 311 | /// subsequently. |
Daniel Dunbar | 40f1d85 | 2012-12-01 01:38:48 +0000 | [diff] [blame] | 312 | /// |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 313 | /// \param InBuffer If not 0, should be the known buffer id that contains the |
Daniel Dunbar | 40f1d85 | 2012-12-01 01:38:48 +0000 | [diff] [blame] | 314 | /// location. |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 315 | void jumpToLoc(SMLoc Loc, unsigned InBuffer = 0); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 316 | |
Daniel Dunbar | 40a564f | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 317 | /// \brief Parse up to the end of statement and a return the contents from the |
| 318 | /// current token until the end of the statement; the current token on exit |
| 319 | /// will be either the EndOfStatement or EOF. |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 320 | StringRef parseStringToEndOfStatement() override; |
Daniel Dunbar | 40a564f | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 321 | |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 322 | /// \brief Parse until the end of a statement or a comma is encountered, |
| 323 | /// 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] | 324 | StringRef parseStringToComma(); |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 325 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 326 | bool parseAssignment(StringRef Name, bool allow_redef, |
Jim Grosbach | b7b750d | 2012-09-13 23:11:31 +0000 | [diff] [blame] | 327 | bool NoDeadStrip = false); |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 328 | |
Ahmed Bougacha | 457852f | 2015-04-28 00:17:39 +0000 | [diff] [blame] | 329 | unsigned getBinOpPrecedence(AsmToken::TokenKind K, |
| 330 | MCBinaryExpr::Opcode &Kind); |
| 331 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 332 | bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc); |
| 333 | bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc); |
| 334 | bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc); |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 335 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 336 | bool parseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc); |
Rafael Espindola | 63760ba | 2010-10-28 20:02:27 +0000 | [diff] [blame] | 337 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 338 | // Generic (target and platform independent) directive parsing. |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 339 | enum DirectiveKind { |
Eli Bendersky | 4d21fa0 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 340 | DK_NO_DIRECTIVE, // Placeholder |
| 341 | 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] | 342 | DK_VALUE, DK_2BYTE, DK_LONG, DK_INT, DK_4BYTE, DK_QUAD, DK_8BYTE, DK_OCTA, |
| 343 | 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] | 344 | 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] | 345 | DK_BUNDLE_ALIGN_MODE, DK_BUNDLE_LOCK, DK_BUNDLE_UNLOCK, |
Kevin Enderby | 3aeada2 | 2013-08-28 17:50:59 +0000 | [diff] [blame] | 346 | DK_ZERO, DK_EXTERN, DK_GLOBL, DK_GLOBAL, |
Eli Bendersky | 4d21fa0 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 347 | DK_LAZY_REFERENCE, DK_NO_DEAD_STRIP, DK_SYMBOL_RESOLVER, DK_PRIVATE_EXTERN, |
| 348 | DK_REFERENCE, DK_WEAK_DEFINITION, DK_WEAK_REFERENCE, |
| 349 | DK_WEAK_DEF_CAN_BE_HIDDEN, DK_COMM, DK_COMMON, DK_LCOMM, DK_ABORT, |
| 350 | DK_INCLUDE, DK_INCBIN, DK_CODE16, DK_CODE16GCC, DK_REPT, DK_IRP, DK_IRPC, |
Saleem Abdulrasool | 763e2cb | 2014-06-18 20:57:28 +0000 | [diff] [blame] | 351 | DK_IF, DK_IFEQ, DK_IFGE, DK_IFGT, DK_IFLE, DK_IFLT, DK_IFNE, DK_IFB, |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 352 | DK_IFNB, DK_IFC, DK_IFEQS, DK_IFNC, DK_IFNES, DK_IFDEF, DK_IFNDEF, |
| 353 | DK_IFNOTDEF, DK_ELSEIF, DK_ELSE, DK_ENDIF, |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 354 | DK_SPACE, DK_SKIP, DK_FILE, DK_LINE, DK_LOC, DK_STABS, |
| 355 | DK_CFI_SECTIONS, DK_CFI_STARTPROC, DK_CFI_ENDPROC, DK_CFI_DEF_CFA, |
| 356 | DK_CFI_DEF_CFA_OFFSET, DK_CFI_ADJUST_CFA_OFFSET, DK_CFI_DEF_CFA_REGISTER, |
| 357 | DK_CFI_OFFSET, DK_CFI_REL_OFFSET, DK_CFI_PERSONALITY, DK_CFI_LSDA, |
| 358 | DK_CFI_REMEMBER_STATE, DK_CFI_RESTORE_STATE, DK_CFI_SAME_VALUE, |
| 359 | 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] | 360 | DK_CFI_REGISTER, DK_CFI_WINDOW_SAVE, |
Nico Weber | 155dccd1 | 2014-07-24 17:08:39 +0000 | [diff] [blame] | 361 | DK_MACROS_ON, DK_MACROS_OFF, |
| 362 | DK_MACRO, DK_EXITM, DK_ENDM, DK_ENDMACRO, DK_PURGEM, |
Saleem Abdulrasool | 88186c4 | 2013-12-18 02:53:03 +0000 | [diff] [blame] | 363 | DK_SLEB128, DK_ULEB128, |
Nico Weber | 404012b | 2014-07-24 16:26:06 +0000 | [diff] [blame] | 364 | DK_ERR, DK_ERROR, DK_WARNING, |
Saleem Abdulrasool | 88186c4 | 2013-12-18 02:53:03 +0000 | [diff] [blame] | 365 | DK_END |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 366 | }; |
| 367 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 368 | /// \brief Maps directive name --> DirectiveKind enum, for |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 369 | /// directives parsed by this class. |
| 370 | StringMap<DirectiveKind> DirectiveKindMap; |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 371 | |
| 372 | // ".ascii", ".asciz", ".string" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 373 | bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated); |
| 374 | bool parseDirectiveValue(unsigned Size); // ".byte", ".long", ... |
David Woodhouse | d6de0d9 | 2014-02-01 16:20:59 +0000 | [diff] [blame] | 375 | bool parseDirectiveOctaValue(); // ".octa" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 376 | bool parseDirectiveRealValue(const fltSemantics &); // ".single", ... |
| 377 | bool parseDirectiveFill(); // ".fill" |
| 378 | bool parseDirectiveZero(); // ".zero" |
Eric Christopher | a7c3273 | 2012-12-18 00:30:54 +0000 | [diff] [blame] | 379 | // ".set", ".equ", ".equiv" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 380 | bool parseDirectiveSet(StringRef IDVal, bool allow_redef); |
| 381 | bool parseDirectiveOrg(); // ".org" |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 382 | // ".align{,32}", ".p2align{,w,l}" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 383 | bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize); |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 384 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 385 | // ".file", ".line", ".loc", ".stabs" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 386 | bool parseDirectiveFile(SMLoc DirectiveLoc); |
| 387 | bool parseDirectiveLine(); |
| 388 | bool parseDirectiveLoc(); |
| 389 | bool parseDirectiveStabs(); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 390 | |
| 391 | // .cfi directives |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 392 | bool parseDirectiveCFIRegister(SMLoc DirectiveLoc); |
Venkatraman Govindaraju | 3816d43 | 2013-09-26 14:49:40 +0000 | [diff] [blame] | 393 | bool parseDirectiveCFIWindowSave(); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 394 | bool parseDirectiveCFISections(); |
| 395 | bool parseDirectiveCFIStartProc(); |
| 396 | bool parseDirectiveCFIEndProc(); |
| 397 | bool parseDirectiveCFIDefCfaOffset(); |
| 398 | bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc); |
| 399 | bool parseDirectiveCFIAdjustCfaOffset(); |
| 400 | bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc); |
| 401 | bool parseDirectiveCFIOffset(SMLoc DirectiveLoc); |
| 402 | bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc); |
| 403 | bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality); |
| 404 | bool parseDirectiveCFIRememberState(); |
| 405 | bool parseDirectiveCFIRestoreState(); |
| 406 | bool parseDirectiveCFISameValue(SMLoc DirectiveLoc); |
| 407 | bool parseDirectiveCFIRestore(SMLoc DirectiveLoc); |
| 408 | bool parseDirectiveCFIEscape(); |
| 409 | bool parseDirectiveCFISignalFrame(); |
| 410 | bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 411 | |
| 412 | // macro directives |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 413 | bool parseDirectivePurgeMacro(SMLoc DirectiveLoc); |
Nico Weber | 155dccd1 | 2014-07-24 17:08:39 +0000 | [diff] [blame] | 414 | bool parseDirectiveExitMacro(StringRef Directive); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 415 | bool parseDirectiveEndMacro(StringRef Directive); |
| 416 | bool parseDirectiveMacro(SMLoc DirectiveLoc); |
| 417 | bool parseDirectiveMacrosOnOff(StringRef Directive); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 418 | |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 419 | // ".bundle_align_mode" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 420 | bool parseDirectiveBundleAlignMode(); |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 421 | // ".bundle_lock" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 422 | bool parseDirectiveBundleLock(); |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 423 | // ".bundle_unlock" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 424 | bool parseDirectiveBundleUnlock(); |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 425 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 426 | // ".space", ".skip" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 427 | bool parseDirectiveSpace(StringRef IDVal); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 428 | |
| 429 | // .sleb128 (Signed=true) and .uleb128 (Signed=false) |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 430 | bool parseDirectiveLEB128(bool Signed); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 431 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 432 | /// \brief Parse a directive like ".globl" which |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 433 | /// accepts a single symbol (which should be a label or an external). |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 434 | bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr); |
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 parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm" |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 437 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 438 | bool parseDirectiveAbort(); // ".abort" |
| 439 | bool parseDirectiveInclude(); // ".include" |
| 440 | bool parseDirectiveIncbin(); // ".incbin" |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 441 | |
Saleem Abdulrasool | 763e2cb | 2014-06-18 20:57:28 +0000 | [diff] [blame] | 442 | // ".if", ".ifeq", ".ifge", ".ifgt" , ".ifle", ".iflt" or ".ifne" |
| 443 | bool parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind); |
Benjamin Kramer | 62c18b0 | 2012-05-12 11:18:42 +0000 | [diff] [blame] | 444 | // ".ifb" or ".ifnb", depending on ExpectBlank. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 445 | bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank); |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 446 | // ".ifc" or ".ifnc", depending on ExpectEqual. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 447 | bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual); |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 448 | // ".ifeqs" or ".ifnes", depending on ExpectEqual. |
| 449 | bool parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual); |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 450 | // ".ifdef" or ".ifndef", depending on expect_defined |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 451 | bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined); |
| 452 | bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif" |
| 453 | bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else" |
| 454 | bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 455 | bool parseEscapedString(std::string &Data) override; |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 456 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 457 | const MCExpr *applyModifierToExpr(const MCExpr *E, |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 458 | MCSymbolRefExpr::VariantKind Variant); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 459 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 460 | // Macro-like directives |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 461 | MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc); |
| 462 | void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc, |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 463 | raw_svector_ostream &OS); |
Saleem Abdulrasool | d743d0a | 2013-12-28 05:54:33 +0000 | [diff] [blame] | 464 | bool parseDirectiveRept(SMLoc DirectiveLoc, StringRef Directive); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 465 | bool parseDirectiveIrp(SMLoc DirectiveLoc); // ".irp" |
| 466 | bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc" |
| 467 | bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr" |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 468 | |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 469 | // "_emit" or "__emit" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 470 | bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info, |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 471 | size_t Len); |
| 472 | |
| 473 | // "align" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 474 | bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info); |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 475 | |
Saleem Abdulrasool | 88186c4 | 2013-12-18 02:53:03 +0000 | [diff] [blame] | 476 | // "end" |
| 477 | bool parseDirectiveEnd(SMLoc DirectiveLoc); |
| 478 | |
Saleem Abdulrasool | 7ecc549 | 2014-02-23 23:02:23 +0000 | [diff] [blame] | 479 | // ".err" or ".error" |
| 480 | bool parseDirectiveError(SMLoc DirectiveLoc, bool WithMessage); |
Saleem Abdulrasool | b2ae2c0 | 2014-02-23 15:53:30 +0000 | [diff] [blame] | 481 | |
Nico Weber | 404012b | 2014-07-24 16:26:06 +0000 | [diff] [blame] | 482 | // ".warning" |
| 483 | bool parseDirectiveWarning(SMLoc DirectiveLoc); |
| 484 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 485 | void initializeDirectiveKindMap(); |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 486 | }; |
Daniel Dunbar | 8603340 | 2010-07-12 17:54:38 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Daniel Dunbar | 0cb91cf | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 489 | namespace llvm { |
| 490 | |
| 491 | extern MCAsmParserExtension *createDarwinAsmParser(); |
Daniel Dunbar | ab058b8 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 492 | extern MCAsmParserExtension *createELFAsmParser(); |
Michael J. Spencer | c8dbdfd | 2010-10-09 11:01:07 +0000 | [diff] [blame] | 493 | extern MCAsmParserExtension *createCOFFAsmParser(); |
Daniel Dunbar | 0cb91cf | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 494 | |
| 495 | } |
| 496 | |
Chris Lattner | c35681b | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 497 | enum { DEFAULT_ADDRSPACE = 0 }; |
| 498 | |
David Blaikie | 9f380a3 | 2015-03-16 18:06:57 +0000 | [diff] [blame] | 499 | AsmParser::AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out, |
| 500 | const MCAsmInfo &MAI) |
| 501 | : Lexer(MAI), Ctx(Ctx), Out(Out), MAI(MAI), SrcMgr(SM), |
| 502 | PlatformParser(nullptr), CurBuffer(SM.getMainFileID()), |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 503 | MacrosEnabledFlag(true), HadError(false), CppHashLineNumber(0), |
Oliver Stannard | cf6bfb1 | 2014-11-03 12:19:03 +0000 | [diff] [blame] | 504 | AssemblerDialect(~0U), IsDarwin(false), ParsingInlineAsm(false) { |
Benjamin Kramer | 47f5e30 | 2011-10-16 10:48:29 +0000 | [diff] [blame] | 505 | // Save the old handler. |
| 506 | SavedDiagHandler = SrcMgr.getDiagHandler(); |
| 507 | SavedDiagContext = SrcMgr.getDiagContext(); |
| 508 | // Set our own handler which calls the saved handler. |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 509 | SrcMgr.setDiagHandler(DiagHandler, this); |
Rafael Espindola | 8026bd0 | 2014-07-06 14:17:29 +0000 | [diff] [blame] | 510 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer()); |
Daniel Dunbar | 8603340 | 2010-07-12 17:54:38 +0000 | [diff] [blame] | 511 | |
Daniel Dunbar | c501108 | 2010-07-12 18:12:02 +0000 | [diff] [blame] | 512 | // Initialize the platform / file format parser. |
David Blaikie | 9f380a3 | 2015-03-16 18:06:57 +0000 | [diff] [blame] | 513 | switch (Ctx.getObjectFileInfo()->getObjectFileType()) { |
Rafael Espindola | e28610d | 2013-12-09 20:26:40 +0000 | [diff] [blame] | 514 | case MCObjectFileInfo::IsCOFF: |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 515 | PlatformParser.reset(createCOFFAsmParser()); |
| 516 | break; |
Rafael Espindola | e28610d | 2013-12-09 20:26:40 +0000 | [diff] [blame] | 517 | case MCObjectFileInfo::IsMachO: |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 518 | PlatformParser.reset(createDarwinAsmParser()); |
| 519 | IsDarwin = true; |
| 520 | break; |
Rafael Espindola | e28610d | 2013-12-09 20:26:40 +0000 | [diff] [blame] | 521 | case MCObjectFileInfo::IsELF: |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 522 | PlatformParser.reset(createELFAsmParser()); |
| 523 | break; |
Daniel Dunbar | c501108 | 2010-07-12 18:12:02 +0000 | [diff] [blame] | 524 | } |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 525 | |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 526 | PlatformParser->Initialize(*this); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 527 | initializeDirectiveKindMap(); |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 528 | |
| 529 | NumOfMacroInstantiations = 0; |
Chris Lattner | 351a7ef | 2009-09-27 21:16:52 +0000 | [diff] [blame] | 530 | } |
| 531 | |
Daniel Dunbar | 4d7b2e3 | 2009-08-26 22:49:51 +0000 | [diff] [blame] | 532 | AsmParser::~AsmParser() { |
Saleem Abdulrasool | 6eae1e6 | 2014-05-21 17:53:18 +0000 | [diff] [blame] | 533 | assert((HadError || ActiveMacros.empty()) && |
| 534 | "Unexpected active macro instantiation!"); |
Daniel Dunbar | 4d7b2e3 | 2009-08-26 22:49:51 +0000 | [diff] [blame] | 535 | } |
| 536 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 537 | void AsmParser::printMacroInstantiations() { |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 538 | // Print the active macro instantiation stack. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 539 | for (std::vector<MacroInstantiation *>::const_reverse_iterator |
| 540 | it = ActiveMacros.rbegin(), |
| 541 | ie = ActiveMacros.rend(); |
| 542 | it != ie; ++it) |
| 543 | printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note, |
Chris Lattner | 03b80a4 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 544 | "while in macro instantiation"); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 545 | } |
| 546 | |
Saleem Abdulrasool | 69c7caf | 2014-01-07 02:28:31 +0000 | [diff] [blame] | 547 | void AsmParser::Note(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) { |
| 548 | printMessage(L, SourceMgr::DK_Note, Msg, Ranges); |
| 549 | printMacroInstantiations(); |
| 550 | } |
| 551 | |
Chris Lattner | a3a0681 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 552 | bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) { |
Joerg Sonnenberger | 2981591 | 2014-08-26 18:39:50 +0000 | [diff] [blame] | 553 | if (getTargetParser().getTargetOptions().MCFatalWarnings) |
Chris Lattner | a3a0681 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 554 | return Error(L, Msg, Ranges); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 555 | printMessage(L, SourceMgr::DK_Warning, Msg, Ranges); |
| 556 | printMacroInstantiations(); |
Joerg Sonnenberger | 74ba262 | 2011-05-19 18:00:13 +0000 | [diff] [blame] | 557 | return false; |
Daniel Dunbar | c9dc78a | 2009-06-30 00:49:23 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Chris Lattner | a3a0681 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 560 | bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) { |
Daniel Dunbar | 43325c4 | 2010-09-09 22:42:56 +0000 | [diff] [blame] | 561 | HadError = true; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 562 | printMessage(L, SourceMgr::DK_Error, Msg, Ranges); |
| 563 | printMacroInstantiations(); |
Chris Lattner | 2adc9e7 | 2009-06-21 21:22:11 +0000 | [diff] [blame] | 564 | return true; |
| 565 | } |
| 566 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 567 | bool AsmParser::enterIncludeFile(const std::string &Filename) { |
Joerg Sonnenberger | af5f23e | 2011-06-01 13:10:15 +0000 | [diff] [blame] | 568 | std::string IncludedFile; |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 569 | unsigned NewBuf = |
| 570 | SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile); |
| 571 | if (!NewBuf) |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 572 | return true; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 573 | |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 574 | CurBuffer = NewBuf; |
Rafael Espindola | 8026bd0 | 2014-07-06 14:17:29 +0000 | [diff] [blame] | 575 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer()); |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 576 | return false; |
| 577 | } |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 578 | |
Sylvestre Ledru | 149e281 | 2013-05-14 23:36:24 +0000 | [diff] [blame] | 579 | /// 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] | 580 | /// 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] | 581 | /// returns true on failure. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 582 | bool AsmParser::processIncbinFile(const std::string &Filename) { |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 583 | std::string IncludedFile; |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 584 | unsigned NewBuf = |
| 585 | SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile); |
| 586 | if (!NewBuf) |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 587 | return true; |
| 588 | |
Kevin Enderby | ad41ab5 | 2011-12-14 22:34:45 +0000 | [diff] [blame] | 589 | // Pick up the bytes from the file and emit them. |
Rafael Espindola | 64e1af8 | 2013-07-02 15:49:13 +0000 | [diff] [blame] | 590 | getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer()); |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 591 | return false; |
| 592 | } |
| 593 | |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 594 | void AsmParser::jumpToLoc(SMLoc Loc, unsigned InBuffer) { |
| 595 | CurBuffer = InBuffer ? InBuffer : SrcMgr.FindBufferContainingLoc(Loc); |
Rafael Espindola | 8026bd0 | 2014-07-06 14:17:29 +0000 | [diff] [blame] | 596 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer(), |
| 597 | Loc.getPointer()); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 598 | } |
| 599 | |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 600 | const AsmToken &AsmParser::Lex() { |
| 601 | const AsmToken *tok = &Lexer.Lex(); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 602 | |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 603 | if (tok->is(AsmToken::Eof)) { |
| 604 | // If this is the end of an included file, pop the parent file off the |
| 605 | // include stack. |
| 606 | SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer); |
| 607 | if (ParentIncludeLoc != SMLoc()) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 608 | jumpToLoc(ParentIncludeLoc); |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 609 | tok = &Lexer.Lex(); |
| 610 | } |
| 611 | } |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 612 | |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 613 | if (tok->is(AsmToken::Error)) |
Daniel Dunbar | d8a1845 | 2010-07-18 18:31:45 +0000 | [diff] [blame] | 614 | Error(Lexer.getErrLoc(), Lexer.getErr()); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 615 | |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 616 | return *tok; |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 617 | } |
| 618 | |
Chris Lattner | 3b21e4d | 2010-04-05 23:15:42 +0000 | [diff] [blame] | 619 | bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) { |
Daniel Dunbar | 322fec6 | 2010-03-13 02:20:57 +0000 | [diff] [blame] | 620 | // Create the initial section, if requested. |
Daniel Dunbar | 322fec6 | 2010-03-13 02:20:57 +0000 | [diff] [blame] | 621 | if (!NoInitialTextSection) |
Rafael Espindola | 7b61ddf | 2014-10-15 16:12:52 +0000 | [diff] [blame] | 622 | Out.InitSections(false); |
Daniel Dunbar | 4d7b2e3 | 2009-08-26 22:49:51 +0000 | [diff] [blame] | 623 | |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 624 | // Prime the lexer. |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 625 | Lex(); |
Daniel Dunbar | 43325c4 | 2010-09-09 22:42:56 +0000 | [diff] [blame] | 626 | |
| 627 | HadError = false; |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 628 | AsmCond StartingCondState = TheCondState; |
| 629 | |
Kevin Enderby | 6469fc2 | 2011-11-01 22:27:22 +0000 | [diff] [blame] | 630 | // If we are generating dwarf for assembly source files save the initial text |
| 631 | // section and generate a .file directive. |
| 632 | if (getContext().getGenDwarfForAssembly()) { |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 633 | MCSymbol *SectionStartSym = getContext().createTempSymbol(); |
Kevin Enderby | e7739d4 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 634 | getStreamer().EmitLabel(SectionStartSym); |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame^] | 635 | MCSection *Sec = getStreamer().getCurrentSection().first; |
Rafael Espindola | e074679 | 2015-05-21 16:52:32 +0000 | [diff] [blame] | 636 | bool InsertResult = getContext().addGenDwarfSection(Sec); |
| 637 | assert(InsertResult && ".text section should not have debug info yet"); |
Rafael Espindola | fa160c7 | 2015-05-21 17:09:22 +0000 | [diff] [blame] | 638 | (void)InsertResult; |
Rafael Espindola | e074679 | 2015-05-21 16:52:32 +0000 | [diff] [blame] | 639 | Sec->setBeginSymbol(SectionStartSym); |
David Blaikie | c714ef4 | 2014-03-17 01:52:11 +0000 | [diff] [blame] | 640 | getContext().setGenDwarfFileNumber(getStreamer().EmitDwarfFileDirective( |
| 641 | 0, StringRef(), getContext().getMainFileName())); |
Kevin Enderby | 6469fc2 | 2011-11-01 22:27:22 +0000 | [diff] [blame] | 642 | } |
| 643 | |
Chris Lattner | 73f3611 | 2009-07-02 21:53:43 +0000 | [diff] [blame] | 644 | // While we have input, parse each statement. |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 645 | while (Lexer.isNot(AsmToken::Eof)) { |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 646 | ParseStatementInfo Info; |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 647 | if (!parseStatement(Info, nullptr)) |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 648 | continue; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 649 | |
Daniel Dunbar | 43325c4 | 2010-09-09 22:42:56 +0000 | [diff] [blame] | 650 | // We had an error, validate that one was emitted and recover by skipping to |
| 651 | // the next line. |
| 652 | assert(HadError && "Parse statement returned an error, but none emitted!"); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 653 | eatToEndOfStatement(); |
Chris Lattner | 73f3611 | 2009-07-02 21:53:43 +0000 | [diff] [blame] | 654 | } |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 655 | |
| 656 | if (TheCondState.TheCond != StartingCondState.TheCond || |
| 657 | TheCondState.Ignore != StartingCondState.Ignore) |
| 658 | return TokError("unmatched .ifs or .elses"); |
Kevin Enderby | e5930f1 | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 659 | |
| 660 | // Check to see there are no empty DwarfFile slots. |
David Blaikie | 8bf66c4 | 2014-04-01 07:35:52 +0000 | [diff] [blame] | 661 | const auto &LineTables = getContext().getMCDwarfLineTables(); |
| 662 | if (!LineTables.empty()) { |
| 663 | unsigned Index = 0; |
| 664 | for (const auto &File : LineTables.begin()->second.getMCDwarfFiles()) { |
| 665 | if (File.Name.empty() && Index != 0) |
| 666 | TokError("unassigned file number: " + Twine(Index) + |
| 667 | " for .file directives"); |
| 668 | ++Index; |
| 669 | } |
Kevin Enderby | e5930f1 | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 670 | } |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 671 | |
Jim Grosbach | c7e6b8f | 2011-06-15 18:33:28 +0000 | [diff] [blame] | 672 | // Check to see that all assembler local symbols were actually defined. |
| 673 | // Targets that don't do subsections via symbols may not want this, though, |
| 674 | // so conservatively exclude them. Only do this if we're finalizing, though, |
| 675 | // as otherwise we won't necessarilly have seen everything yet. |
| 676 | if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) { |
| 677 | const MCContext::SymbolTable &Symbols = getContext().getSymbols(); |
| 678 | for (MCContext::SymbolTable::const_iterator i = Symbols.begin(), |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 679 | e = Symbols.end(); |
Jim Grosbach | c7e6b8f | 2011-06-15 18:33:28 +0000 | [diff] [blame] | 680 | i != e; ++i) { |
| 681 | MCSymbol *Sym = i->getValue(); |
| 682 | // Variable symbols may not be marked as defined, so check those |
| 683 | // explicitly. If we know it's a variable, we have a definition for |
| 684 | // the purposes of this check. |
| 685 | if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined()) |
| 686 | // FIXME: We would really like to refer back to where the symbol was |
| 687 | // first referenced for a source location. We need to add something |
| 688 | // 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] | 689 | printMessage( |
| 690 | getLexer().getLoc(), SourceMgr::DK_Error, |
| 691 | "assembler local symbol '" + Sym->getName() + "' not defined"); |
Jim Grosbach | c7e6b8f | 2011-06-15 18:33:28 +0000 | [diff] [blame] | 692 | } |
| 693 | } |
| 694 | |
Chris Lattner | 3b21e4d | 2010-04-05 23:15:42 +0000 | [diff] [blame] | 695 | // Finalize the output stream if there are no errors and if the client wants |
| 696 | // us to. |
Jack Carter | 13d5f75 | 2013-10-04 22:52:31 +0000 | [diff] [blame] | 697 | if (!HadError && !NoFinalize) |
Daniel Dunbar | 9df5f33 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 698 | Out.Finish(); |
| 699 | |
Chris Lattner | 73f3611 | 2009-07-02 21:53:43 +0000 | [diff] [blame] | 700 | return HadError; |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 701 | } |
| 702 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 703 | void AsmParser::checkForValidSection() { |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 704 | if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) { |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 705 | TokError("expected section directive before assembly directive"); |
Rafael Espindola | 7b61ddf | 2014-10-15 16:12:52 +0000 | [diff] [blame] | 706 | Out.InitSections(false); |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 707 | } |
| 708 | } |
| 709 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 710 | /// \brief Throw away the rest of the line for testing purposes. |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 711 | void AsmParser::eatToEndOfStatement() { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 712 | while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof)) |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 713 | Lex(); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 714 | |
Chris Lattner | e5074c4 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 715 | // Eat EOL. |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 716 | if (Lexer.is(AsmToken::EndOfStatement)) |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 717 | Lex(); |
Chris Lattner | e5074c4 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 718 | } |
| 719 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 720 | StringRef AsmParser::parseStringToEndOfStatement() { |
Daniel Dunbar | 40a564f | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 721 | const char *Start = getTok().getLoc().getPointer(); |
| 722 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 723 | while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof)) |
Daniel Dunbar | 40a564f | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 724 | Lex(); |
| 725 | |
| 726 | const char *End = getTok().getLoc().getPointer(); |
| 727 | return StringRef(Start, End - Start); |
| 728 | } |
Chris Lattner | 78db362 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 729 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 730 | StringRef AsmParser::parseStringToComma() { |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 731 | const char *Start = getTok().getLoc().getPointer(); |
| 732 | |
| 733 | while (Lexer.isNot(AsmToken::EndOfStatement) && |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 734 | Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof)) |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 735 | Lex(); |
| 736 | |
| 737 | const char *End = getTok().getLoc().getPointer(); |
| 738 | return StringRef(Start, End - Start); |
| 739 | } |
| 740 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 741 | /// \brief Parse a paren expression and return it. |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 742 | /// NOTE: This assumes the leading '(' has already been consumed. |
| 743 | /// |
| 744 | /// parenexpr ::= expr) |
| 745 | /// |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 746 | bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) { |
| 747 | if (parseExpression(Res)) |
| 748 | return true; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 749 | if (Lexer.isNot(AsmToken::RParen)) |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 750 | return TokError("expected ')' in parentheses expression"); |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 751 | EndLoc = Lexer.getTok().getEndLoc(); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 752 | Lex(); |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 753 | return false; |
| 754 | } |
Chris Lattner | 78db362 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 755 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 756 | /// \brief Parse a bracket expression and return it. |
Joerg Sonnenberger | afb36fa | 2011-02-24 21:59:22 +0000 | [diff] [blame] | 757 | /// NOTE: This assumes the leading '[' has already been consumed. |
| 758 | /// |
| 759 | /// bracketexpr ::= expr] |
| 760 | /// |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 761 | bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) { |
| 762 | if (parseExpression(Res)) |
| 763 | return true; |
Joerg Sonnenberger | afb36fa | 2011-02-24 21:59:22 +0000 | [diff] [blame] | 764 | if (Lexer.isNot(AsmToken::RBrac)) |
| 765 | return TokError("expected ']' in brackets expression"); |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 766 | EndLoc = Lexer.getTok().getEndLoc(); |
Joerg Sonnenberger | afb36fa | 2011-02-24 21:59:22 +0000 | [diff] [blame] | 767 | Lex(); |
| 768 | return false; |
| 769 | } |
| 770 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 771 | /// \brief Parse a primary expression and return it. |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 772 | /// primaryexpr ::= (parenexpr |
| 773 | /// primaryexpr ::= symbol |
| 774 | /// primaryexpr ::= number |
Chris Lattner | 6b55cb9 | 2010-04-14 04:40:28 +0000 | [diff] [blame] | 775 | /// primaryexpr ::= '.' |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 776 | /// primaryexpr ::= ~,+,- primaryexpr |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 777 | bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) { |
Kevin Enderby | 0017d8a | 2013-01-22 21:09:20 +0000 | [diff] [blame] | 778 | SMLoc FirstTokenLoc = getLexer().getLoc(); |
| 779 | AsmToken::TokenKind FirstTokenKind = Lexer.getKind(); |
| 780 | switch (FirstTokenKind) { |
Chris Lattner | 78db362 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 781 | default: |
| 782 | return TokError("unknown token in expression"); |
Eric Christopher | 104af06 | 2011-04-12 00:03:13 +0000 | [diff] [blame] | 783 | // If we have an error assume that we've already handled it. |
| 784 | case AsmToken::Error: |
| 785 | return true; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 786 | case AsmToken::Exclaim: |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 787 | Lex(); // Eat the operator. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 788 | if (parsePrimaryExpr(Res, EndLoc)) |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 789 | return true; |
Daniel Dunbar | 940cda2 | 2009-08-31 08:07:44 +0000 | [diff] [blame] | 790 | Res = MCUnaryExpr::CreateLNot(Res, getContext()); |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 791 | return false; |
Daniel Dunbar | 2476432 | 2010-08-24 19:13:42 +0000 | [diff] [blame] | 792 | case AsmToken::Dollar: |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 793 | case AsmToken::At: |
Daniel Dunbar | 9ee33ca | 2009-07-31 21:55:09 +0000 | [diff] [blame] | 794 | case AsmToken::String: |
Daniel Dunbar | d20cda0 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 795 | case AsmToken::Identifier: { |
Daniel Dunbar | 2476432 | 2010-08-24 19:13:42 +0000 | [diff] [blame] | 796 | StringRef Identifier; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 797 | if (parseIdentifier(Identifier)) { |
David Majnemer | 0c58bc6 | 2013-09-25 10:47:21 +0000 | [diff] [blame] | 798 | if (FirstTokenKind == AsmToken::Dollar) { |
| 799 | if (Lexer.getMAI().getDollarIsPC()) { |
| 800 | // This is a '$' reference, which references the current PC. Emit a |
| 801 | // temporary label to the streamer and refer to it. |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 802 | MCSymbol *Sym = Ctx.createTempSymbol(); |
David Majnemer | 0c58bc6 | 2013-09-25 10:47:21 +0000 | [diff] [blame] | 803 | Out.EmitLabel(Sym); |
Jack Carter | 721726a | 2013-10-04 21:26:15 +0000 | [diff] [blame] | 804 | Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, |
| 805 | getContext()); |
David Majnemer | 0c58bc6 | 2013-09-25 10:47:21 +0000 | [diff] [blame] | 806 | EndLoc = FirstTokenLoc; |
| 807 | return false; |
Ted Kremenek | 297febe | 2014-03-06 22:13:17 +0000 | [diff] [blame] | 808 | } |
| 809 | return Error(FirstTokenLoc, "invalid token in expression"); |
David Majnemer | 0c58bc6 | 2013-09-25 10:47:21 +0000 | [diff] [blame] | 810 | } |
Kevin Enderby | 0017d8a | 2013-01-22 21:09:20 +0000 | [diff] [blame] | 811 | } |
David Peixotto | 8ad70b3 | 2013-12-04 22:43:20 +0000 | [diff] [blame] | 812 | // Parse symbol variant |
| 813 | std::pair<StringRef, StringRef> Split; |
| 814 | if (!MAI.useParensForSymbolVariant()) { |
David Majnemer | 6a5b812 | 2014-06-19 01:25:43 +0000 | [diff] [blame] | 815 | if (FirstTokenKind == AsmToken::String) { |
| 816 | if (Lexer.is(AsmToken::At)) { |
| 817 | Lexer.Lex(); // eat @ |
| 818 | SMLoc AtLoc = getLexer().getLoc(); |
| 819 | StringRef VName; |
| 820 | if (parseIdentifier(VName)) |
| 821 | return Error(AtLoc, "expected symbol variant after '@'"); |
| 822 | |
| 823 | Split = std::make_pair(Identifier, VName); |
| 824 | } |
| 825 | } else { |
| 826 | Split = Identifier.split('@'); |
| 827 | } |
David Peixotto | 8ad70b3 | 2013-12-04 22:43:20 +0000 | [diff] [blame] | 828 | } else if (Lexer.is(AsmToken::LParen)) { |
| 829 | Lexer.Lex(); // eat ( |
| 830 | StringRef VName; |
| 831 | parseIdentifier(VName); |
| 832 | if (Lexer.isNot(AsmToken::RParen)) { |
| 833 | return Error(Lexer.getTok().getLoc(), |
| 834 | "unexpected token in variant, expected ')'"); |
| 835 | } |
| 836 | Lexer.Lex(); // eat ) |
| 837 | Split = std::make_pair(Identifier, VName); |
| 838 | } |
Daniel Dunbar | 2476432 | 2010-08-24 19:13:42 +0000 | [diff] [blame] | 839 | |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 840 | EndLoc = SMLoc::getFromPointer(Identifier.end()); |
| 841 | |
Daniel Dunbar | d20cda0 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 842 | // This is a symbol reference. |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 843 | StringRef SymbolName = Identifier; |
| 844 | MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None; |
Hans Wennborg | 69918bc | 2013-10-17 01:13:02 +0000 | [diff] [blame] | 845 | |
Hans Wennborg | 7ddcdc8 | 2013-10-18 02:14:40 +0000 | [diff] [blame] | 846 | // Lookup the symbol variant if used. |
David Peixotto | 8ad70b3 | 2013-12-04 22:43:20 +0000 | [diff] [blame] | 847 | if (Split.second.size()) { |
Hans Wennborg | 7ddcdc8 | 2013-10-18 02:14:40 +0000 | [diff] [blame] | 848 | Variant = MCSymbolRefExpr::getVariantKindForName(Split.second); |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 849 | if (Variant != MCSymbolRefExpr::VK_Invalid) { |
| 850 | SymbolName = Split.first; |
David Peixotto | 8ad70b3 | 2013-12-04 22:43:20 +0000 | [diff] [blame] | 851 | } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) { |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 852 | Variant = MCSymbolRefExpr::VK_None; |
| 853 | } else { |
Saleem Abdulrasool | a25e1e4 | 2014-01-26 22:29:43 +0000 | [diff] [blame] | 854 | return Error(SMLoc::getFromPointer(Split.second.begin()), |
| 855 | "invalid variant '" + Split.second + "'"); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 856 | } |
| 857 | } |
Daniel Dunbar | 5599256 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 858 | |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 859 | MCSymbol *Sym = getContext().getOrCreateSymbol(SymbolName); |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 860 | |
Daniel Dunbar | d20cda0 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 861 | // If this is an absolute variable reference, substitute it now to preserve |
| 862 | // semantics in the face of reassignment. |
Daniel Dunbar | 7a989da | 2010-05-05 17:41:00 +0000 | [diff] [blame] | 863 | if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) { |
Daniel Dunbar | 5599256 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 864 | if (Variant) |
Daniel Dunbar | 8a3c3f2 | 2010-11-08 17:53:02 +0000 | [diff] [blame] | 865 | return Error(EndLoc, "unexpected modifier on variable reference"); |
Daniel Dunbar | 5599256 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 866 | |
Daniel Dunbar | 7a989da | 2010-05-05 17:41:00 +0000 | [diff] [blame] | 867 | Res = Sym->getVariableValue(); |
Daniel Dunbar | d20cda0 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 868 | return false; |
| 869 | } |
| 870 | |
| 871 | // Otherwise create a symbol ref. |
Daniel Dunbar | 5599256 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 872 | Res = MCSymbolRefExpr::Create(Sym, Variant, getContext()); |
Chris Lattner | 78db362 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 873 | return false; |
Daniel Dunbar | d20cda0 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 874 | } |
David Woodhouse | f42a666 | 2014-02-01 16:20:54 +0000 | [diff] [blame] | 875 | case AsmToken::BigNum: |
| 876 | return TokError("literal value out of range for directive"); |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 877 | case AsmToken::Integer: { |
| 878 | SMLoc Loc = getTok().getLoc(); |
| 879 | int64_t IntVal = getTok().getIntVal(); |
| 880 | Res = MCConstantExpr::Create(IntVal, getContext()); |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 881 | EndLoc = Lexer.getTok().getEndLoc(); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 882 | Lex(); // Eat token. |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 883 | // Look for 'b' or 'f' following an Integer as a directional label |
| 884 | if (Lexer.getKind() == AsmToken::Identifier) { |
| 885 | StringRef IDVal = getTok().getString(); |
Ulrich Weigand | d412098 | 2013-06-20 16:24:17 +0000 | [diff] [blame] | 886 | // Lookup the symbol variant if used. |
| 887 | std::pair<StringRef, StringRef> Split = IDVal.split('@'); |
| 888 | MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None; |
| 889 | if (Split.first.size() != IDVal.size()) { |
| 890 | Variant = MCSymbolRefExpr::getVariantKindForName(Split.second); |
Arnaud A. de Grandmaison | c97727a | 2014-03-21 21:54:46 +0000 | [diff] [blame] | 891 | if (Variant == MCSymbolRefExpr::VK_Invalid) |
Ulrich Weigand | d412098 | 2013-06-20 16:24:17 +0000 | [diff] [blame] | 892 | return TokError("invalid variant '" + Split.second + "'"); |
Vladimir Medic | 9bad0d33 | 2013-08-20 13:33:18 +0000 | [diff] [blame] | 893 | IDVal = Split.first; |
Ulrich Weigand | d412098 | 2013-06-20 16:24:17 +0000 | [diff] [blame] | 894 | } |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 895 | if (IDVal == "f" || IDVal == "b") { |
| 896 | MCSymbol *Sym = |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 897 | Ctx.getDirectionalLocalSymbol(IntVal, IDVal == "b"); |
Ulrich Weigand | d412098 | 2013-06-20 16:24:17 +0000 | [diff] [blame] | 898 | Res = MCSymbolRefExpr::Create(Sym, Variant, getContext()); |
Benjamin Kramer | c7eda3e | 2012-05-12 16:52:21 +0000 | [diff] [blame] | 899 | if (IDVal == "b" && Sym->isUndefined()) |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 900 | return Error(Loc, "invalid reference to undefined symbol"); |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 901 | EndLoc = Lexer.getTok().getEndLoc(); |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 902 | Lex(); // Eat identifier. |
| 903 | } |
| 904 | } |
Chris Lattner | 78db362 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 905 | return false; |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 906 | } |
Bill Wendling | cdbf17b | 2011-01-25 21:26:41 +0000 | [diff] [blame] | 907 | case AsmToken::Real: { |
| 908 | APFloat RealVal(APFloat::IEEEdouble, getTok().getString()); |
Bob Wilson | 813bdf6 | 2011-02-03 23:17:47 +0000 | [diff] [blame] | 909 | uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue(); |
Bill Wendling | cdbf17b | 2011-01-25 21:26:41 +0000 | [diff] [blame] | 910 | Res = MCConstantExpr::Create(IntVal, getContext()); |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 911 | EndLoc = Lexer.getTok().getEndLoc(); |
Bill Wendling | cdbf17b | 2011-01-25 21:26:41 +0000 | [diff] [blame] | 912 | Lex(); // Eat token. |
| 913 | return false; |
| 914 | } |
Chris Lattner | 6b55cb9 | 2010-04-14 04:40:28 +0000 | [diff] [blame] | 915 | case AsmToken::Dot: { |
| 916 | // This is a '.' reference, which references the current PC. Emit a |
| 917 | // temporary label to the streamer and refer to it. |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 918 | MCSymbol *Sym = Ctx.createTempSymbol(); |
Chris Lattner | 6b55cb9 | 2010-04-14 04:40:28 +0000 | [diff] [blame] | 919 | Out.EmitLabel(Sym); |
| 920 | Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext()); |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 921 | EndLoc = Lexer.getTok().getEndLoc(); |
Chris Lattner | 6b55cb9 | 2010-04-14 04:40:28 +0000 | [diff] [blame] | 922 | Lex(); // Eat identifier. |
| 923 | return false; |
| 924 | } |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 925 | case AsmToken::LParen: |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 926 | Lex(); // Eat the '('. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 927 | return parseParenExpr(Res, EndLoc); |
Joerg Sonnenberger | afb36fa | 2011-02-24 21:59:22 +0000 | [diff] [blame] | 928 | case AsmToken::LBrac: |
| 929 | if (!PlatformParser->HasBracketExpressions()) |
| 930 | return TokError("brackets expression not supported on this target"); |
| 931 | Lex(); // Eat the '['. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 932 | return parseBracketExpr(Res, EndLoc); |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 933 | case AsmToken::Minus: |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 934 | Lex(); // Eat the operator. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 935 | if (parsePrimaryExpr(Res, EndLoc)) |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 936 | return true; |
Daniel Dunbar | 940cda2 | 2009-08-31 08:07:44 +0000 | [diff] [blame] | 937 | Res = MCUnaryExpr::CreateMinus(Res, getContext()); |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 938 | return false; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 939 | case AsmToken::Plus: |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 940 | Lex(); // Eat the operator. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 941 | if (parsePrimaryExpr(Res, EndLoc)) |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 942 | return true; |
Daniel Dunbar | 940cda2 | 2009-08-31 08:07:44 +0000 | [diff] [blame] | 943 | Res = MCUnaryExpr::CreatePlus(Res, getContext()); |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 944 | return false; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 945 | case AsmToken::Tilde: |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 946 | Lex(); // Eat the operator. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 947 | if (parsePrimaryExpr(Res, EndLoc)) |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 948 | return true; |
Daniel Dunbar | 940cda2 | 2009-08-31 08:07:44 +0000 | [diff] [blame] | 949 | Res = MCUnaryExpr::CreateNot(Res, getContext()); |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 950 | return false; |
Chris Lattner | 78db362 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 951 | } |
| 952 | } |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 953 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 954 | bool AsmParser::parseExpression(const MCExpr *&Res) { |
Chris Lattner | e17df0b | 2010-01-15 19:39:23 +0000 | [diff] [blame] | 955 | SMLoc EndLoc; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 956 | return parseExpression(Res, EndLoc); |
Chris Lattner | 528d00b | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 957 | } |
| 958 | |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 959 | const MCExpr * |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 960 | AsmParser::applyModifierToExpr(const MCExpr *E, |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 961 | MCSymbolRefExpr::VariantKind Variant) { |
Joerg Sonnenberger | b822af4 | 2013-08-27 20:23:19 +0000 | [diff] [blame] | 962 | // Ask the target implementation about this expression first. |
| 963 | const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx); |
| 964 | if (NewE) |
| 965 | return NewE; |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 966 | // Recurse over the given expression, rebuilding it to apply the given variant |
| 967 | // if there is exactly one symbol. |
| 968 | switch (E->getKind()) { |
| 969 | case MCExpr::Target: |
| 970 | case MCExpr::Constant: |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 971 | return nullptr; |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 972 | |
| 973 | case MCExpr::SymbolRef: { |
| 974 | const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E); |
| 975 | |
| 976 | if (SRE->getKind() != MCSymbolRefExpr::VK_None) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 977 | TokError("invalid variant on expression '" + getTok().getIdentifier() + |
| 978 | "' (already modified)"); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 979 | return E; |
| 980 | } |
| 981 | |
| 982 | return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext()); |
| 983 | } |
| 984 | |
| 985 | case MCExpr::Unary: { |
| 986 | const MCUnaryExpr *UE = cast<MCUnaryExpr>(E); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 987 | const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 988 | if (!Sub) |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 989 | return nullptr; |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 990 | return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext()); |
| 991 | } |
| 992 | |
| 993 | case MCExpr::Binary: { |
| 994 | const MCBinaryExpr *BE = cast<MCBinaryExpr>(E); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 995 | const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant); |
| 996 | const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 997 | |
| 998 | if (!LHS && !RHS) |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 999 | return nullptr; |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1000 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1001 | if (!LHS) |
| 1002 | LHS = BE->getLHS(); |
| 1003 | if (!RHS) |
| 1004 | RHS = BE->getRHS(); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1005 | |
| 1006 | return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext()); |
| 1007 | } |
| 1008 | } |
Daniel Dunbar | baad46c | 2010-09-17 16:34:24 +0000 | [diff] [blame] | 1009 | |
Craig Topper | a2886c2 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 1010 | llvm_unreachable("Invalid expression kind!"); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1011 | } |
| 1012 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1013 | /// \brief Parse an expression and return it. |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1014 | /// |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1015 | /// expr ::= expr &&,|| expr -> lowest. |
| 1016 | /// expr ::= expr |,^,&,! expr |
| 1017 | /// expr ::= expr ==,!=,<>,<,<=,>,>= expr |
| 1018 | /// expr ::= expr <<,>> expr |
| 1019 | /// expr ::= expr +,- expr |
| 1020 | /// expr ::= expr *,/,% expr -> highest. |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 1021 | /// expr ::= primaryexpr |
| 1022 | /// |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1023 | bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) { |
Daniel Dunbar | d0c6d36 | 2010-02-13 01:28:07 +0000 | [diff] [blame] | 1024 | // Parse the expression. |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 1025 | Res = nullptr; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1026 | if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc)) |
Daniel Dunbar | d0c6d36 | 2010-02-13 01:28:07 +0000 | [diff] [blame] | 1027 | return true; |
| 1028 | |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1029 | // As a special case, we support 'a op b @ modifier' by rewriting the |
| 1030 | // expression to include the modifier. This is inefficient, but in general we |
| 1031 | // expect users to use 'a@modifier op b'. |
| 1032 | if (Lexer.getKind() == AsmToken::At) { |
| 1033 | Lex(); |
| 1034 | |
| 1035 | if (Lexer.isNot(AsmToken::Identifier)) |
| 1036 | return TokError("unexpected symbol modifier following '@'"); |
| 1037 | |
| 1038 | MCSymbolRefExpr::VariantKind Variant = |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1039 | MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier()); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1040 | if (Variant == MCSymbolRefExpr::VK_Invalid) |
| 1041 | return TokError("invalid variant '" + getTok().getIdentifier() + "'"); |
| 1042 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1043 | const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1044 | if (!ModifiedRes) { |
| 1045 | return TokError("invalid modifier '" + getTok().getIdentifier() + |
| 1046 | "' (no symbols present)"); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1047 | } |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1048 | |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1049 | Res = ModifiedRes; |
| 1050 | Lex(); |
| 1051 | } |
| 1052 | |
Daniel Dunbar | d0c6d36 | 2010-02-13 01:28:07 +0000 | [diff] [blame] | 1053 | // Try to constant fold it up front, if possible. |
| 1054 | int64_t Value; |
| 1055 | if (Res->EvaluateAsAbsolute(Value)) |
| 1056 | Res = MCConstantExpr::Create(Value, getContext()); |
| 1057 | |
| 1058 | return false; |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 1059 | } |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1060 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1061 | bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) { |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 1062 | Res = nullptr; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1063 | return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc); |
Daniel Dunbar | 7c82d56 | 2009-08-31 08:08:17 +0000 | [diff] [blame] | 1064 | } |
| 1065 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1066 | bool AsmParser::parseAbsoluteExpression(int64_t &Res) { |
Daniel Dunbar | f363645 | 2009-08-31 08:07:22 +0000 | [diff] [blame] | 1067 | const MCExpr *Expr; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1068 | |
Daniel Dunbar | 75630b3 | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 1069 | SMLoc StartLoc = Lexer.getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1070 | if (parseExpression(Expr)) |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1071 | return true; |
| 1072 | |
Daniel Dunbar | c3bd60e | 2009-10-16 01:57:52 +0000 | [diff] [blame] | 1073 | if (!Expr->EvaluateAsAbsolute(Res)) |
Daniel Dunbar | 75630b3 | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 1074 | return Error(StartLoc, "expected absolute expression"); |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1075 | |
| 1076 | return false; |
| 1077 | } |
| 1078 | |
Ahmed Bougacha | 457852f | 2015-04-28 00:17:39 +0000 | [diff] [blame] | 1079 | unsigned AsmParser::getBinOpPrecedence(AsmToken::TokenKind K, |
| 1080 | MCBinaryExpr::Opcode &Kind) { |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1081 | switch (K) { |
Daniel Dunbar | 940cda2 | 2009-08-31 08:07:44 +0000 | [diff] [blame] | 1082 | default: |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1083 | return 0; // not a binop. |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1084 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1085 | // Lowest Precedence: &&, || |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1086 | case AsmToken::AmpAmp: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1087 | Kind = MCBinaryExpr::LAnd; |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1088 | return 1; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1089 | case AsmToken::PipePipe: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1090 | Kind = MCBinaryExpr::LOr; |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1091 | return 1; |
| 1092 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1093 | // Low Precedence: |, &, ^ |
| 1094 | // |
| 1095 | // FIXME: gas seems to support '!' as an infix operator? |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1096 | case AsmToken::Pipe: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1097 | Kind = MCBinaryExpr::Or; |
Chris Lattner | 2bb9504d | 2010-09-22 05:05:16 +0000 | [diff] [blame] | 1098 | return 2; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1099 | case AsmToken::Caret: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1100 | Kind = MCBinaryExpr::Xor; |
Chris Lattner | 2bb9504d | 2010-09-22 05:05:16 +0000 | [diff] [blame] | 1101 | return 2; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1102 | case AsmToken::Amp: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1103 | Kind = MCBinaryExpr::And; |
Chris Lattner | 2bb9504d | 2010-09-22 05:05:16 +0000 | [diff] [blame] | 1104 | return 2; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1105 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1106 | // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >= |
Chris Lattner | 2bb9504d | 2010-09-22 05:05:16 +0000 | [diff] [blame] | 1107 | case AsmToken::EqualEqual: |
| 1108 | Kind = MCBinaryExpr::EQ; |
| 1109 | return 3; |
| 1110 | case AsmToken::ExclaimEqual: |
| 1111 | case AsmToken::LessGreater: |
| 1112 | Kind = MCBinaryExpr::NE; |
| 1113 | return 3; |
| 1114 | case AsmToken::Less: |
| 1115 | Kind = MCBinaryExpr::LT; |
| 1116 | return 3; |
| 1117 | case AsmToken::LessEqual: |
| 1118 | Kind = MCBinaryExpr::LTE; |
| 1119 | return 3; |
| 1120 | case AsmToken::Greater: |
| 1121 | Kind = MCBinaryExpr::GT; |
| 1122 | return 3; |
| 1123 | case AsmToken::GreaterEqual: |
| 1124 | Kind = MCBinaryExpr::GTE; |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1125 | return 3; |
| 1126 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1127 | // Intermediate Precedence: <<, >> |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1128 | case AsmToken::LessLess: |
| 1129 | Kind = MCBinaryExpr::Shl; |
| 1130 | return 4; |
| 1131 | case AsmToken::GreaterGreater: |
Ahmed Bougacha | 177c148 | 2015-04-28 00:21:32 +0000 | [diff] [blame] | 1132 | Kind = MAI.shouldUseLogicalShr() ? MCBinaryExpr::LShr : MCBinaryExpr::AShr; |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1133 | return 4; |
| 1134 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1135 | // High Intermediate Precedence: +, - |
Daniel Dunbar | b3a48f3 | 2010-10-25 20:18:56 +0000 | [diff] [blame] | 1136 | case AsmToken::Plus: |
| 1137 | Kind = MCBinaryExpr::Add; |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1138 | return 5; |
Daniel Dunbar | b3a48f3 | 2010-10-25 20:18:56 +0000 | [diff] [blame] | 1139 | case AsmToken::Minus: |
| 1140 | Kind = MCBinaryExpr::Sub; |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1141 | return 5; |
Daniel Dunbar | b3a48f3 | 2010-10-25 20:18:56 +0000 | [diff] [blame] | 1142 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1143 | // Highest Precedence: *, /, % |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1144 | case AsmToken::Star: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1145 | Kind = MCBinaryExpr::Mul; |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1146 | return 6; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1147 | case AsmToken::Slash: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1148 | Kind = MCBinaryExpr::Div; |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1149 | return 6; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1150 | case AsmToken::Percent: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1151 | Kind = MCBinaryExpr::Mod; |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1152 | return 6; |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1153 | } |
| 1154 | } |
| 1155 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1156 | /// \brief Parse all binary operators with precedence >= 'Precedence'. |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1157 | /// Res contains the LHS of the expression on input. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1158 | bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, |
Chris Lattner | 528d00b | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 1159 | SMLoc &EndLoc) { |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1160 | while (1) { |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1161 | MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add; |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1162 | unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1163 | |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1164 | // If the next token is lower precedence than we are allowed to eat, return |
| 1165 | // successfully with what we ate already. |
| 1166 | if (TokPrec < Precedence) |
| 1167 | return false; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1168 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1169 | Lex(); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1170 | |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1171 | // Eat the next primary expression. |
Daniel Dunbar | f363645 | 2009-08-31 08:07:22 +0000 | [diff] [blame] | 1172 | const MCExpr *RHS; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1173 | if (parsePrimaryExpr(RHS, EndLoc)) |
| 1174 | return true; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1175 | |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1176 | // If BinOp binds less tightly with RHS than the operator after RHS, let |
| 1177 | // the pending operator take RHS as its LHS. |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1178 | MCBinaryExpr::Opcode Dummy; |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1179 | unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1180 | if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc)) |
| 1181 | return true; |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1182 | |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1183 | // Merge LHS and RHS according to operator. |
Daniel Dunbar | 940cda2 | 2009-08-31 08:07:44 +0000 | [diff] [blame] | 1184 | Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext()); |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1185 | } |
| 1186 | } |
| 1187 | |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1188 | /// ParseStatement: |
| 1189 | /// ::= EndOfStatement |
Chris Lattner | e5074c4 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 1190 | /// ::= Label* Directive ...Operands... EndOfStatement |
| 1191 | /// ::= Label* Identifier OperandList* EndOfStatement |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 1192 | bool AsmParser::parseStatement(ParseStatementInfo &Info, |
| 1193 | MCAsmParserSemaCallback *SI) { |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1194 | if (Lexer.is(AsmToken::EndOfStatement)) { |
Daniel Dunbar | 8271d1bb | 2010-05-23 18:36:34 +0000 | [diff] [blame] | 1195 | Out.AddBlankLine(); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1196 | Lex(); |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1197 | return false; |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1198 | } |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1199 | |
Kevin Enderby | fa3c6f1 | 2010-12-24 00:12:02 +0000 | [diff] [blame] | 1200 | // Statements always start with an identifier or are a full line comment. |
Sean Callanan | 936b0d3 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 1201 | AsmToken ID = getTok(); |
Daniel Dunbar | ee4465c | 2009-07-28 16:38:40 +0000 | [diff] [blame] | 1202 | SMLoc IDLoc = ID.getLoc(); |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1203 | StringRef IDVal; |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1204 | int64_t LocalLabelVal = -1; |
Kevin Enderby | fa3c6f1 | 2010-12-24 00:12:02 +0000 | [diff] [blame] | 1205 | // A full line comment is a '#' as the first token. |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1206 | if (Lexer.is(AsmToken::Hash)) |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1207 | return parseCppHashLineFilenameComment(IDLoc); |
Daniel Dunbar | 3f56104 | 2011-03-25 17:47:14 +0000 | [diff] [blame] | 1208 | |
Kevin Enderby | fa3c6f1 | 2010-12-24 00:12:02 +0000 | [diff] [blame] | 1209 | // Allow an integer followed by a ':' as a directional local label. |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1210 | if (Lexer.is(AsmToken::Integer)) { |
| 1211 | LocalLabelVal = getTok().getIntVal(); |
| 1212 | if (LocalLabelVal < 0) { |
| 1213 | if (!TheCondState.Ignore) |
| 1214 | return TokError("unexpected token at start of statement"); |
| 1215 | IDVal = ""; |
Eli Bendersky | 8802471 | 2013-01-16 19:32:36 +0000 | [diff] [blame] | 1216 | } else { |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1217 | IDVal = getTok().getString(); |
| 1218 | Lex(); // Consume the integer token to be used as an identifier token. |
| 1219 | if (Lexer.getKind() != AsmToken::Colon) { |
Duncan Sands | 41b4a6b | 2010-07-12 08:16:59 +0000 | [diff] [blame] | 1220 | if (!TheCondState.Ignore) |
| 1221 | return TokError("unexpected token at start of statement"); |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1222 | } |
| 1223 | } |
Daniel Dunbar | 6f4c942 | 2011-03-25 17:47:17 +0000 | [diff] [blame] | 1224 | } else if (Lexer.is(AsmToken::Dot)) { |
| 1225 | // Treat '.' as a valid identifier in this context. |
| 1226 | Lex(); |
| 1227 | IDVal = "."; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1228 | } else if (parseIdentifier(IDVal)) { |
Chris Lattner | 926885c | 2010-04-17 18:14:27 +0000 | [diff] [blame] | 1229 | if (!TheCondState.Ignore) |
| 1230 | return TokError("unexpected token at start of statement"); |
| 1231 | IDVal = ""; |
| 1232 | } |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1233 | |
Chris Lattner | 926885c | 2010-04-17 18:14:27 +0000 | [diff] [blame] | 1234 | // Handle conditional assembly here before checking for skipping. We |
| 1235 | // have to do this so that .endif isn't skipped in a ".if 0" block for |
| 1236 | // example. |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1237 | StringMap<DirectiveKind>::const_iterator DirKindIt = |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1238 | DirectiveKindMap.find(IDVal); |
| 1239 | DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end()) |
| 1240 | ? DK_NO_DIRECTIVE |
| 1241 | : DirKindIt->getValue(); |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1242 | switch (DirKind) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1243 | default: |
| 1244 | break; |
| 1245 | case DK_IF: |
Saleem Abdulrasool | 763e2cb | 2014-06-18 20:57:28 +0000 | [diff] [blame] | 1246 | case DK_IFEQ: |
| 1247 | case DK_IFGE: |
| 1248 | case DK_IFGT: |
| 1249 | case DK_IFLE: |
| 1250 | case DK_IFLT: |
Saleem Abdulrasool | 5852d6b | 2014-02-23 15:53:41 +0000 | [diff] [blame] | 1251 | case DK_IFNE: |
Saleem Abdulrasool | 763e2cb | 2014-06-18 20:57:28 +0000 | [diff] [blame] | 1252 | return parseDirectiveIf(IDLoc, DirKind); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1253 | case DK_IFB: |
| 1254 | return parseDirectiveIfb(IDLoc, true); |
| 1255 | case DK_IFNB: |
| 1256 | return parseDirectiveIfb(IDLoc, false); |
| 1257 | case DK_IFC: |
| 1258 | return parseDirectiveIfc(IDLoc, true); |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 1259 | case DK_IFEQS: |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 1260 | return parseDirectiveIfeqs(IDLoc, true); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1261 | case DK_IFNC: |
| 1262 | return parseDirectiveIfc(IDLoc, false); |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 1263 | case DK_IFNES: |
| 1264 | return parseDirectiveIfeqs(IDLoc, false); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1265 | case DK_IFDEF: |
| 1266 | return parseDirectiveIfdef(IDLoc, true); |
| 1267 | case DK_IFNDEF: |
| 1268 | case DK_IFNOTDEF: |
| 1269 | return parseDirectiveIfdef(IDLoc, false); |
| 1270 | case DK_ELSEIF: |
| 1271 | return parseDirectiveElseIf(IDLoc); |
| 1272 | case DK_ELSE: |
| 1273 | return parseDirectiveElse(IDLoc); |
| 1274 | case DK_ENDIF: |
| 1275 | return parseDirectiveEndIf(IDLoc); |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1276 | } |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1277 | |
Eli Bendersky | 8802471 | 2013-01-16 19:32:36 +0000 | [diff] [blame] | 1278 | // Ignore the statement if in the middle of inactive conditional |
| 1279 | // (e.g. ".if 0"). |
Chad Rosier | eda70b3 | 2012-10-20 00:47:08 +0000 | [diff] [blame] | 1280 | if (TheCondState.Ignore) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1281 | eatToEndOfStatement(); |
Chris Lattner | 926885c | 2010-04-17 18:14:27 +0000 | [diff] [blame] | 1282 | return false; |
| 1283 | } |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1284 | |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1285 | // FIXME: Recurse on local labels? |
| 1286 | |
| 1287 | // See what kind of statement we have. |
| 1288 | switch (Lexer.getKind()) { |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1289 | case AsmToken::Colon: { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1290 | checkForValidSection(); |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 1291 | |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1292 | // identifier ':' -> Label. |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1293 | Lex(); |
Daniel Dunbar | ae7ac01 | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 1294 | |
Daniel Dunbar | 6f4c942 | 2011-03-25 17:47:17 +0000 | [diff] [blame] | 1295 | // Diagnose attempt to use '.' as a label. |
| 1296 | if (IDVal == ".") |
| 1297 | return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label"); |
| 1298 | |
Daniel Dunbar | ae7ac01 | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 1299 | // Diagnose attempt to use a variable as a label. |
| 1300 | // |
| 1301 | // FIXME: Diagnostics. Note the location of the definition as a label. |
| 1302 | // FIXME: This doesn't diagnose assignment to a symbol which has been |
| 1303 | // implicitly marked as external. |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1304 | MCSymbol *Sym; |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 1305 | if (LocalLabelVal == -1) { |
| 1306 | if (ParsingInlineAsm && SI) { |
| 1307 | StringRef RewrittenLabel = SI->LookupInlineAsmLabel(IDVal, getSourceManager(), IDLoc, true); |
| 1308 | assert(RewrittenLabel.size() && "We should have an internal name here."); |
| 1309 | Info.AsmRewrites->push_back(AsmRewrite(AOK_Label, IDLoc, |
| 1310 | IDVal.size(), RewrittenLabel)); |
| 1311 | IDVal = RewrittenLabel; |
| 1312 | } |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 1313 | Sym = getContext().getOrCreateSymbol(IDVal); |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 1314 | } else |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 1315 | Sym = Ctx.createDirectionalLocalSymbol(LocalLabelVal); |
David Majnemer | 58cb80c | 2014-12-24 10:27:50 +0000 | [diff] [blame] | 1316 | |
| 1317 | Sym->redefineIfPossible(); |
| 1318 | |
Daniel Dunbar | deb7ba9 | 2010-05-05 19:01:00 +0000 | [diff] [blame] | 1319 | if (!Sym->isUndefined() || Sym->isVariable()) |
Daniel Dunbar | ae7ac01 | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 1320 | return Error(IDLoc, "invalid symbol redefinition"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1321 | |
Daniel Dunbar | e73b267 | 2009-08-26 22:13:22 +0000 | [diff] [blame] | 1322 | // Emit the label. |
Chad Rosier | f3feab3 | 2013-01-07 20:34:12 +0000 | [diff] [blame] | 1323 | if (!ParsingInlineAsm) |
| 1324 | Out.EmitLabel(Sym); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1325 | |
Kevin Enderby | e7739d4 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 1326 | // If we are generating dwarf for assembly source files then gather the |
Kevin Enderby | f7d7706 | 2012-01-10 21:12:34 +0000 | [diff] [blame] | 1327 | // info to make a dwarf label entry for this label if needed. |
Kevin Enderby | e7739d4 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 1328 | if (getContext().getGenDwarfForAssembly()) |
Kevin Enderby | f7d7706 | 2012-01-10 21:12:34 +0000 | [diff] [blame] | 1329 | MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(), |
| 1330 | IDLoc); |
Kevin Enderby | e7739d4 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 1331 | |
Tim Northover | 1744d0a | 2013-10-25 12:49:50 +0000 | [diff] [blame] | 1332 | getTargetParser().onLabelParsed(Sym); |
| 1333 | |
Daniel Dunbar | 8271d1bb | 2010-05-23 18:36:34 +0000 | [diff] [blame] | 1334 | // Consume any end of statement token, if present, to avoid spurious |
| 1335 | // AddBlankLine calls(). |
| 1336 | if (Lexer.is(AsmToken::EndOfStatement)) { |
| 1337 | Lex(); |
| 1338 | if (Lexer.is(AsmToken::Eof)) |
| 1339 | return false; |
| 1340 | } |
| 1341 | |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 1342 | return false; |
Daniel Dunbar | ae7ac01 | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 1343 | } |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1344 | |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1345 | case AsmToken::Equal: |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1346 | // identifier '=' ... -> assignment statement |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1347 | Lex(); |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1348 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1349 | return parseAssignment(IDVal, true); |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1350 | |
| 1351 | default: // Normal instruction or directive. |
| 1352 | break; |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1353 | } |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 1354 | |
| 1355 | // 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] | 1356 | if (areMacrosEnabled()) |
| 1357 | if (const MCAsmMacro *M = lookupMacro(IDVal)) { |
| 1358 | return handleMacroEntry(M, IDLoc); |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 1359 | } |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 1360 | |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1361 | // Otherwise, we have a normal instruction or directive. |
Vladimir Medic | 9bad0d33 | 2013-08-20 13:33:18 +0000 | [diff] [blame] | 1362 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1363 | // Directives start with "." |
Daniel Dunbar | 6f4c942 | 2011-03-25 17:47:17 +0000 | [diff] [blame] | 1364 | if (IDVal[0] == '.' && IDVal != ".") { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1365 | // There are several entities interested in parsing directives: |
Vladimir Medic | 9bad0d33 | 2013-08-20 13:33:18 +0000 | [diff] [blame] | 1366 | // |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1367 | // 1. The target-specific assembly parser. Some directives are target |
| 1368 | // specific or may potentially behave differently on certain targets. |
| 1369 | // 2. Asm parser extensions. For example, platform-specific parsers |
| 1370 | // (like the ELF parser) register themselves as extensions. |
| 1371 | // 3. The generic directive parser implemented by this class. These are |
| 1372 | // all the directives that behave in a target and platform independent |
| 1373 | // manner, or at least have a default behavior that's shared between |
| 1374 | // all targets and platforms. |
Akira Hatanaka | d359075 | 2012-07-05 19:09:33 +0000 | [diff] [blame] | 1375 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1376 | // First query the target-specific parser. It will return 'true' if it |
| 1377 | // isn't interested in this directive. |
Akira Hatanaka | d359075 | 2012-07-05 19:09:33 +0000 | [diff] [blame] | 1378 | if (!getTargetParser().ParseDirective(ID)) |
| 1379 | return false; |
| 1380 | |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 1381 | // Next, check the extension directive map to see if any extension has |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1382 | // registered itself to parse this directive. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1383 | std::pair<MCAsmParserExtension *, DirectiveHandler> Handler = |
| 1384 | ExtensionDirectiveMap.lookup(IDVal); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1385 | if (Handler.first) |
| 1386 | return (*Handler.second)(Handler.first, IDVal, IDLoc); |
| 1387 | |
| 1388 | // Finally, if no one else is interested in this directive, it must be |
| 1389 | // generic and familiar to this class. |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1390 | switch (DirKind) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1391 | default: |
| 1392 | break; |
| 1393 | case DK_SET: |
| 1394 | case DK_EQU: |
| 1395 | return parseDirectiveSet(IDVal, true); |
| 1396 | case DK_EQUIV: |
| 1397 | return parseDirectiveSet(IDVal, false); |
| 1398 | case DK_ASCII: |
| 1399 | return parseDirectiveAscii(IDVal, false); |
| 1400 | case DK_ASCIZ: |
| 1401 | case DK_STRING: |
| 1402 | return parseDirectiveAscii(IDVal, true); |
| 1403 | case DK_BYTE: |
| 1404 | return parseDirectiveValue(1); |
| 1405 | case DK_SHORT: |
| 1406 | case DK_VALUE: |
| 1407 | case DK_2BYTE: |
| 1408 | return parseDirectiveValue(2); |
| 1409 | case DK_LONG: |
| 1410 | case DK_INT: |
| 1411 | case DK_4BYTE: |
| 1412 | return parseDirectiveValue(4); |
| 1413 | case DK_QUAD: |
| 1414 | case DK_8BYTE: |
| 1415 | return parseDirectiveValue(8); |
David Woodhouse | d6de0d9 | 2014-02-01 16:20:59 +0000 | [diff] [blame] | 1416 | case DK_OCTA: |
| 1417 | return parseDirectiveOctaValue(); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1418 | case DK_SINGLE: |
| 1419 | case DK_FLOAT: |
| 1420 | return parseDirectiveRealValue(APFloat::IEEEsingle); |
| 1421 | case DK_DOUBLE: |
| 1422 | return parseDirectiveRealValue(APFloat::IEEEdouble); |
| 1423 | case DK_ALIGN: { |
| 1424 | bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes(); |
| 1425 | return parseDirectiveAlign(IsPow2, /*ExprSize=*/1); |
| 1426 | } |
| 1427 | case DK_ALIGN32: { |
| 1428 | bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes(); |
| 1429 | return parseDirectiveAlign(IsPow2, /*ExprSize=*/4); |
| 1430 | } |
| 1431 | case DK_BALIGN: |
| 1432 | return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1); |
| 1433 | case DK_BALIGNW: |
| 1434 | return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2); |
| 1435 | case DK_BALIGNL: |
| 1436 | return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4); |
| 1437 | case DK_P2ALIGN: |
| 1438 | return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1); |
| 1439 | case DK_P2ALIGNW: |
| 1440 | return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2); |
| 1441 | case DK_P2ALIGNL: |
| 1442 | return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4); |
| 1443 | case DK_ORG: |
| 1444 | return parseDirectiveOrg(); |
| 1445 | case DK_FILL: |
| 1446 | return parseDirectiveFill(); |
| 1447 | case DK_ZERO: |
| 1448 | return parseDirectiveZero(); |
| 1449 | case DK_EXTERN: |
| 1450 | eatToEndOfStatement(); // .extern is the default, ignore it. |
| 1451 | return false; |
| 1452 | case DK_GLOBL: |
| 1453 | case DK_GLOBAL: |
| 1454 | return parseDirectiveSymbolAttribute(MCSA_Global); |
| 1455 | case DK_LAZY_REFERENCE: |
| 1456 | return parseDirectiveSymbolAttribute(MCSA_LazyReference); |
| 1457 | case DK_NO_DEAD_STRIP: |
| 1458 | return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip); |
| 1459 | case DK_SYMBOL_RESOLVER: |
| 1460 | return parseDirectiveSymbolAttribute(MCSA_SymbolResolver); |
| 1461 | case DK_PRIVATE_EXTERN: |
| 1462 | return parseDirectiveSymbolAttribute(MCSA_PrivateExtern); |
| 1463 | case DK_REFERENCE: |
| 1464 | return parseDirectiveSymbolAttribute(MCSA_Reference); |
| 1465 | case DK_WEAK_DEFINITION: |
| 1466 | return parseDirectiveSymbolAttribute(MCSA_WeakDefinition); |
| 1467 | case DK_WEAK_REFERENCE: |
| 1468 | return parseDirectiveSymbolAttribute(MCSA_WeakReference); |
| 1469 | case DK_WEAK_DEF_CAN_BE_HIDDEN: |
| 1470 | return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate); |
| 1471 | case DK_COMM: |
| 1472 | case DK_COMMON: |
| 1473 | return parseDirectiveComm(/*IsLocal=*/false); |
| 1474 | case DK_LCOMM: |
| 1475 | return parseDirectiveComm(/*IsLocal=*/true); |
| 1476 | case DK_ABORT: |
| 1477 | return parseDirectiveAbort(); |
| 1478 | case DK_INCLUDE: |
| 1479 | return parseDirectiveInclude(); |
| 1480 | case DK_INCBIN: |
| 1481 | return parseDirectiveIncbin(); |
| 1482 | case DK_CODE16: |
| 1483 | case DK_CODE16GCC: |
| 1484 | return TokError(Twine(IDVal) + " not supported yet"); |
| 1485 | case DK_REPT: |
Saleem Abdulrasool | d743d0a | 2013-12-28 05:54:33 +0000 | [diff] [blame] | 1486 | return parseDirectiveRept(IDLoc, IDVal); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1487 | case DK_IRP: |
| 1488 | return parseDirectiveIrp(IDLoc); |
| 1489 | case DK_IRPC: |
| 1490 | return parseDirectiveIrpc(IDLoc); |
| 1491 | case DK_ENDR: |
| 1492 | return parseDirectiveEndr(IDLoc); |
| 1493 | case DK_BUNDLE_ALIGN_MODE: |
| 1494 | return parseDirectiveBundleAlignMode(); |
| 1495 | case DK_BUNDLE_LOCK: |
| 1496 | return parseDirectiveBundleLock(); |
| 1497 | case DK_BUNDLE_UNLOCK: |
| 1498 | return parseDirectiveBundleUnlock(); |
| 1499 | case DK_SLEB128: |
| 1500 | return parseDirectiveLEB128(true); |
| 1501 | case DK_ULEB128: |
| 1502 | return parseDirectiveLEB128(false); |
| 1503 | case DK_SPACE: |
| 1504 | case DK_SKIP: |
| 1505 | return parseDirectiveSpace(IDVal); |
| 1506 | case DK_FILE: |
| 1507 | return parseDirectiveFile(IDLoc); |
| 1508 | case DK_LINE: |
| 1509 | return parseDirectiveLine(); |
| 1510 | case DK_LOC: |
| 1511 | return parseDirectiveLoc(); |
| 1512 | case DK_STABS: |
| 1513 | return parseDirectiveStabs(); |
| 1514 | case DK_CFI_SECTIONS: |
| 1515 | return parseDirectiveCFISections(); |
| 1516 | case DK_CFI_STARTPROC: |
| 1517 | return parseDirectiveCFIStartProc(); |
| 1518 | case DK_CFI_ENDPROC: |
| 1519 | return parseDirectiveCFIEndProc(); |
| 1520 | case DK_CFI_DEF_CFA: |
| 1521 | return parseDirectiveCFIDefCfa(IDLoc); |
| 1522 | case DK_CFI_DEF_CFA_OFFSET: |
| 1523 | return parseDirectiveCFIDefCfaOffset(); |
| 1524 | case DK_CFI_ADJUST_CFA_OFFSET: |
| 1525 | return parseDirectiveCFIAdjustCfaOffset(); |
| 1526 | case DK_CFI_DEF_CFA_REGISTER: |
| 1527 | return parseDirectiveCFIDefCfaRegister(IDLoc); |
| 1528 | case DK_CFI_OFFSET: |
| 1529 | return parseDirectiveCFIOffset(IDLoc); |
| 1530 | case DK_CFI_REL_OFFSET: |
| 1531 | return parseDirectiveCFIRelOffset(IDLoc); |
| 1532 | case DK_CFI_PERSONALITY: |
| 1533 | return parseDirectiveCFIPersonalityOrLsda(true); |
| 1534 | case DK_CFI_LSDA: |
| 1535 | return parseDirectiveCFIPersonalityOrLsda(false); |
| 1536 | case DK_CFI_REMEMBER_STATE: |
| 1537 | return parseDirectiveCFIRememberState(); |
| 1538 | case DK_CFI_RESTORE_STATE: |
| 1539 | return parseDirectiveCFIRestoreState(); |
| 1540 | case DK_CFI_SAME_VALUE: |
| 1541 | return parseDirectiveCFISameValue(IDLoc); |
| 1542 | case DK_CFI_RESTORE: |
| 1543 | return parseDirectiveCFIRestore(IDLoc); |
| 1544 | case DK_CFI_ESCAPE: |
| 1545 | return parseDirectiveCFIEscape(); |
| 1546 | case DK_CFI_SIGNAL_FRAME: |
| 1547 | return parseDirectiveCFISignalFrame(); |
| 1548 | case DK_CFI_UNDEFINED: |
| 1549 | return parseDirectiveCFIUndefined(IDLoc); |
| 1550 | case DK_CFI_REGISTER: |
| 1551 | return parseDirectiveCFIRegister(IDLoc); |
Venkatraman Govindaraju | 3816d43 | 2013-09-26 14:49:40 +0000 | [diff] [blame] | 1552 | case DK_CFI_WINDOW_SAVE: |
| 1553 | return parseDirectiveCFIWindowSave(); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1554 | case DK_MACROS_ON: |
| 1555 | case DK_MACROS_OFF: |
| 1556 | return parseDirectiveMacrosOnOff(IDVal); |
| 1557 | case DK_MACRO: |
| 1558 | return parseDirectiveMacro(IDLoc); |
Nico Weber | 155dccd1 | 2014-07-24 17:08:39 +0000 | [diff] [blame] | 1559 | case DK_EXITM: |
| 1560 | return parseDirectiveExitMacro(IDVal); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1561 | case DK_ENDM: |
| 1562 | case DK_ENDMACRO: |
| 1563 | return parseDirectiveEndMacro(IDVal); |
| 1564 | case DK_PURGEM: |
| 1565 | return parseDirectivePurgeMacro(IDLoc); |
Saleem Abdulrasool | 88186c4 | 2013-12-18 02:53:03 +0000 | [diff] [blame] | 1566 | case DK_END: |
| 1567 | return parseDirectiveEnd(IDLoc); |
Saleem Abdulrasool | b2ae2c0 | 2014-02-23 15:53:30 +0000 | [diff] [blame] | 1568 | case DK_ERR: |
Saleem Abdulrasool | 7ecc549 | 2014-02-23 23:02:23 +0000 | [diff] [blame] | 1569 | return parseDirectiveError(IDLoc, false); |
| 1570 | case DK_ERROR: |
| 1571 | return parseDirectiveError(IDLoc, true); |
Nico Weber | 404012b | 2014-07-24 16:26:06 +0000 | [diff] [blame] | 1572 | case DK_WARNING: |
| 1573 | return parseDirectiveWarning(IDLoc); |
Eli Friedman | 20b0264 | 2010-07-19 04:17:25 +0000 | [diff] [blame] | 1574 | } |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 1575 | |
Jim Grosbach | 758e0cc | 2012-05-01 18:38:27 +0000 | [diff] [blame] | 1576 | return Error(IDLoc, "unknown directive"); |
Chris Lattner | e5074c4 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 1577 | } |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1578 | |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 1579 | // __asm _emit or __asm __emit |
| 1580 | if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" || |
| 1581 | IDVal == "_EMIT" || IDVal == "__EMIT")) |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1582 | return parseDirectiveMSEmit(IDLoc, Info, IDVal.size()); |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 1583 | |
| 1584 | // __asm align |
| 1585 | if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN")) |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1586 | return parseDirectiveMSAlign(IDLoc, Info); |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 1587 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1588 | checkForValidSection(); |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 1589 | |
Chris Lattner | 7cbfa44 | 2010-05-19 23:34:33 +0000 | [diff] [blame] | 1590 | // Canonicalize the opcode to lower case. |
Eli Bendersky | 8802471 | 2013-01-16 19:32:36 +0000 | [diff] [blame] | 1591 | std::string OpcodeStr = IDVal.lower(); |
Chad Rosier | f0e8720 | 2012-10-25 20:41:34 +0000 | [diff] [blame] | 1592 | ParseInstructionInfo IInfo(Info.AsmRewrites); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1593 | bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, IDLoc, |
Vladimir Medic | 9bad0d33 | 2013-08-20 13:33:18 +0000 | [diff] [blame] | 1594 | Info.ParsedOperands); |
Chad Rosier | 149e8e0 | 2012-12-12 22:45:52 +0000 | [diff] [blame] | 1595 | Info.ParseError = HadError; |
Chris Lattner | e5074c4 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 1596 | |
Daniel Dunbar | 2eca025 | 2010-08-11 06:37:09 +0000 | [diff] [blame] | 1597 | // Dump the parsed representation, if requested. |
| 1598 | if (getShowParsedOperands()) { |
| 1599 | SmallString<256> Str; |
| 1600 | raw_svector_ostream OS(Str); |
| 1601 | OS << "parsed instruction: ["; |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 1602 | for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) { |
Daniel Dunbar | 2eca025 | 2010-08-11 06:37:09 +0000 | [diff] [blame] | 1603 | if (i != 0) |
| 1604 | OS << ", "; |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 1605 | Info.ParsedOperands[i]->print(OS); |
Daniel Dunbar | 2eca025 | 2010-08-11 06:37:09 +0000 | [diff] [blame] | 1606 | } |
| 1607 | OS << "]"; |
| 1608 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1609 | printMessage(IDLoc, SourceMgr::DK_Note, OS.str()); |
Daniel Dunbar | 2eca025 | 2010-08-11 06:37:09 +0000 | [diff] [blame] | 1610 | } |
| 1611 | |
Oliver Stannard | 8b27308 | 2014-06-19 15:52:37 +0000 | [diff] [blame] | 1612 | // If we are generating dwarf for the current section then generate a .loc |
| 1613 | // directive for the instruction. |
Kevin Enderby | 6469fc2 | 2011-11-01 22:27:22 +0000 | [diff] [blame] | 1614 | if (!HadError && getContext().getGenDwarfForAssembly() && |
Oliver Stannard | 8b27308 | 2014-06-19 15:52:37 +0000 | [diff] [blame] | 1615 | getContext().getGenDwarfSectionSyms().count( |
Saleem Abdulrasool | 4d6ed7c | 2014-12-24 06:32:43 +0000 | [diff] [blame] | 1616 | getStreamer().getCurrentSection().first)) { |
| 1617 | unsigned Line; |
| 1618 | if (ActiveMacros.empty()) |
| 1619 | Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer); |
| 1620 | else |
| 1621 | Line = SrcMgr.FindLineNumber(ActiveMacros.back()->InstantiationLoc, |
| 1622 | ActiveMacros.back()->ExitBuffer); |
Kevin Enderby | 4eaf8ef | 2012-11-01 17:31:35 +0000 | [diff] [blame] | 1623 | |
Eli Bendersky | 8802471 | 2013-01-16 19:32:36 +0000 | [diff] [blame] | 1624 | // If we previously parsed a cpp hash file line comment then make sure the |
| 1625 | // current Dwarf File is for the CppHashFilename if not then emit the |
| 1626 | // Dwarf File table for it and adjust the line number for the .loc. |
Saleem Abdulrasool | 4d6ed7c | 2014-12-24 06:32:43 +0000 | [diff] [blame] | 1627 | if (CppHashFilename.size()) { |
David Blaikie | c714ef4 | 2014-03-17 01:52:11 +0000 | [diff] [blame] | 1628 | unsigned FileNumber = getStreamer().EmitDwarfFileDirective( |
| 1629 | 0, StringRef(), CppHashFilename); |
| 1630 | getContext().setGenDwarfFileNumber(FileNumber); |
Kevin Enderby | 4eaf8ef | 2012-11-01 17:31:35 +0000 | [diff] [blame] | 1631 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1632 | // Since SrcMgr.FindLineNumber() is slow and messes up the SourceMgr's |
| 1633 | // cache with the different Loc from the call above we save the last |
| 1634 | // info we queried here with SrcMgr.FindLineNumber(). |
| 1635 | unsigned CppHashLocLineNo; |
| 1636 | if (LastQueryIDLoc == CppHashLoc && LastQueryBuffer == CppHashBuf) |
| 1637 | CppHashLocLineNo = LastQueryLine; |
| 1638 | else { |
| 1639 | CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc, CppHashBuf); |
| 1640 | LastQueryLine = CppHashLocLineNo; |
| 1641 | LastQueryIDLoc = CppHashLoc; |
| 1642 | LastQueryBuffer = CppHashBuf; |
| 1643 | } |
| 1644 | Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo); |
Eli Bendersky | 8802471 | 2013-01-16 19:32:36 +0000 | [diff] [blame] | 1645 | } |
Kevin Enderby | 4eaf8ef | 2012-11-01 17:31:35 +0000 | [diff] [blame] | 1646 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1647 | getStreamer().EmitDwarfLocDirective( |
| 1648 | getContext().getGenDwarfFileNumber(), Line, 0, |
| 1649 | DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0, |
| 1650 | StringRef()); |
Kevin Enderby | 6469fc2 | 2011-11-01 22:27:22 +0000 | [diff] [blame] | 1651 | } |
| 1652 | |
Daniel Dunbar | ce0c1e1 | 2010-05-04 00:33:07 +0000 | [diff] [blame] | 1653 | // If parsing succeeded, match the instruction. |
Chad Rosier | 4996355 | 2012-10-13 00:26:04 +0000 | [diff] [blame] | 1654 | if (!HadError) { |
Tim Northover | 26bb14e | 2014-08-18 11:49:42 +0000 | [diff] [blame] | 1655 | uint64_t ErrorInfo; |
Arnaud A. de Grandmaison | c97727a | 2014-03-21 21:54:46 +0000 | [diff] [blame] | 1656 | getTargetParser().MatchAndEmitInstruction(IDLoc, Info.Opcode, |
| 1657 | Info.ParsedOperands, Out, |
| 1658 | ErrorInfo, ParsingInlineAsm); |
Chad Rosier | 4996355 | 2012-10-13 00:26:04 +0000 | [diff] [blame] | 1659 | } |
Chris Lattner | f29c0b6 | 2010-01-14 22:21:20 +0000 | [diff] [blame] | 1660 | |
Chris Lattner | a2a9d16 | 2010-09-11 16:18:25 +0000 | [diff] [blame] | 1661 | // Don't skip the rest of the line, the instruction parser is responsible for |
| 1662 | // that. |
| 1663 | return false; |
Chris Lattner | b013345 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 1664 | } |
Chris Lattner | bedf6c2 | 2009-06-24 04:43:34 +0000 | [diff] [blame] | 1665 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1666 | /// 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] | 1667 | /// 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] | 1668 | void AsmParser::eatToEndOfLine() { |
Rafael Espindola | e0d0908 | 2011-10-19 18:48:52 +0000 | [diff] [blame] | 1669 | if (!Lexer.is(AsmToken::EndOfStatement)) |
| 1670 | Lexer.LexUntilEndOfLine(); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1671 | // Eat EOL. |
| 1672 | Lex(); |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1673 | } |
| 1674 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1675 | /// parseCppHashLineFilenameComment as this: |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1676 | /// ::= # number "filename" |
| 1677 | /// 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] | 1678 | bool AsmParser::parseCppHashLineFilenameComment(const SMLoc &L) { |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1679 | Lex(); // Eat the hash token. |
| 1680 | |
| 1681 | if (getLexer().isNot(AsmToken::Integer)) { |
| 1682 | // Consume the line since in cases it is not a well-formed line directive, |
| 1683 | // as if were simply a full line comment. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1684 | eatToEndOfLine(); |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1685 | return false; |
| 1686 | } |
| 1687 | |
| 1688 | int64_t LineNumber = getTok().getIntVal(); |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1689 | Lex(); |
| 1690 | |
| 1691 | if (getLexer().isNot(AsmToken::String)) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1692 | eatToEndOfLine(); |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1693 | return false; |
| 1694 | } |
| 1695 | |
| 1696 | StringRef Filename = getTok().getString(); |
| 1697 | // Get rid of the enclosing quotes. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1698 | Filename = Filename.substr(1, Filename.size() - 2); |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1699 | |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1700 | // Save the SMLoc, Filename and LineNumber for later use by diagnostics. |
| 1701 | CppHashLoc = L; |
| 1702 | CppHashFilename = Filename; |
| 1703 | CppHashLineNumber = LineNumber; |
Kevin Enderby | 27121c1 | 2012-11-05 21:55:41 +0000 | [diff] [blame] | 1704 | CppHashBuf = CurBuffer; |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1705 | |
| 1706 | // Ignore any trailing characters, they're just comment. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1707 | eatToEndOfLine(); |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1708 | return false; |
| 1709 | } |
| 1710 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1711 | /// \brief will use the last parsed cpp hash line filename comment |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1712 | /// for the Filename and LineNo if any in the diagnostic. |
| 1713 | void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1714 | const AsmParser *Parser = static_cast<const AsmParser *>(Context); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1715 | raw_ostream &OS = errs(); |
| 1716 | |
| 1717 | const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr(); |
| 1718 | const SMLoc &DiagLoc = Diag.getLoc(); |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 1719 | unsigned DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc); |
| 1720 | unsigned CppHashBuf = |
| 1721 | Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1722 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1723 | // Like SourceMgr::printMessage() we need to print the include stack if any |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1724 | // before printing the message. |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 1725 | unsigned DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc); |
| 1726 | if (!Parser->SavedDiagHandler && DiagCurBuffer && |
| 1727 | DiagCurBuffer != DiagSrcMgr.getMainFileID()) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1728 | SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer); |
| 1729 | DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1730 | } |
| 1731 | |
Eric Christopher | a7c3273 | 2012-12-18 00:30:54 +0000 | [diff] [blame] | 1732 | // 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] | 1733 | // manager changed or buffer changed (like in a nested include) then just |
| 1734 | // print the normal diagnostic using its Filename and LineNo. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1735 | if (!Parser->CppHashLineNumber || &DiagSrcMgr != &Parser->SrcMgr || |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1736 | DiagBuf != CppHashBuf) { |
Benjamin Kramer | 47f5e30 | 2011-10-16 10:48:29 +0000 | [diff] [blame] | 1737 | if (Parser->SavedDiagHandler) |
| 1738 | Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext); |
| 1739 | else |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 1740 | Diag.print(nullptr, OS); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1741 | return; |
| 1742 | } |
| 1743 | |
Eric Christopher | a7c3273 | 2012-12-18 00:30:54 +0000 | [diff] [blame] | 1744 | // Use the CppHashFilename and calculate a line number based on the |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1745 | // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for |
| 1746 | // the diagnostic. |
Jakub Staszak | ec2ffa9 | 2013-09-16 22:03:38 +0000 | [diff] [blame] | 1747 | const std::string &Filename = Parser->CppHashFilename; |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1748 | |
| 1749 | int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf); |
| 1750 | int CppHashLocLineNo = |
| 1751 | Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1752 | int LineNo = |
| 1753 | Parser->CppHashLineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1754 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1755 | SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo, |
| 1756 | Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(), |
Chris Lattner | 7284526 | 2011-10-16 05:47:55 +0000 | [diff] [blame] | 1757 | Diag.getLineContents(), Diag.getRanges()); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1758 | |
Benjamin Kramer | 47f5e30 | 2011-10-16 10:48:29 +0000 | [diff] [blame] | 1759 | if (Parser->SavedDiagHandler) |
| 1760 | Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext); |
| 1761 | else |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 1762 | NewDiag.print(nullptr, OS); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1763 | } |
| 1764 | |
Rafael Espindola | 2c06448 | 2012-08-21 18:29:30 +0000 | [diff] [blame] | 1765 | // FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The |
| 1766 | // difference being that that function accepts '@' as part of identifiers and |
| 1767 | // we can't do that. AsmLexer.cpp should probably be changed to handle |
| 1768 | // '@' as a special case when needed. |
| 1769 | static bool isIdentifierChar(char c) { |
Guy Benyei | 83c74e9 | 2013-02-12 21:21:59 +0000 | [diff] [blame] | 1770 | return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' || |
| 1771 | c == '.'; |
Rafael Espindola | 2c06448 | 2012-08-21 18:29:30 +0000 | [diff] [blame] | 1772 | } |
| 1773 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 1774 | bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body, |
Benjamin Kramer | d31aaf1 | 2014-02-09 17:13:11 +0000 | [diff] [blame] | 1775 | ArrayRef<MCAsmMacroParameter> Parameters, |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 1776 | ArrayRef<MCAsmMacroArgument> A, |
| 1777 | bool EnableAtPseudoVariable, const SMLoc &L) { |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1778 | unsigned NParameters = Parameters.size(); |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 1779 | bool HasVararg = NParameters ? Parameters.back().Vararg : false; |
Benjamin Kramer | 513e744 | 2014-02-20 13:36:32 +0000 | [diff] [blame] | 1780 | if ((!IsDarwin || NParameters != 0) && NParameters != A.size()) |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1781 | return Error(L, "Wrong number of arguments"); |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 1782 | |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1783 | // A macro without parameters is handled differently on Darwin: |
| 1784 | // gas accepts no arguments and does no substitutions |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 1785 | while (!Body.empty()) { |
| 1786 | // Scan for the next substitution. |
| 1787 | std::size_t End = Body.size(), Pos = 0; |
| 1788 | for (; Pos != End; ++Pos) { |
| 1789 | // Check for a substitution or escape. |
Benjamin Kramer | 513e744 | 2014-02-20 13:36:32 +0000 | [diff] [blame] | 1790 | if (IsDarwin && !NParameters) { |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1791 | // This macro has no parameters, look for $0, $1, etc. |
| 1792 | if (Body[Pos] != '$' || Pos + 1 == End) |
| 1793 | continue; |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 1794 | |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1795 | char Next = Body[Pos + 1]; |
Guy Benyei | 83c74e9 | 2013-02-12 21:21:59 +0000 | [diff] [blame] | 1796 | if (Next == '$' || Next == 'n' || |
| 1797 | isdigit(static_cast<unsigned char>(Next))) |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1798 | break; |
| 1799 | } else { |
| 1800 | // This macro has parameters, look for \foo, \bar, etc. |
| 1801 | if (Body[Pos] == '\\' && Pos + 1 != End) |
| 1802 | break; |
| 1803 | } |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 1804 | } |
| 1805 | |
| 1806 | // Add the prefix. |
| 1807 | OS << Body.slice(0, Pos); |
| 1808 | |
| 1809 | // Check if we reached the end. |
| 1810 | if (Pos == End) |
| 1811 | break; |
| 1812 | |
Benjamin Kramer | 513e744 | 2014-02-20 13:36:32 +0000 | [diff] [blame] | 1813 | if (IsDarwin && !NParameters) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1814 | switch (Body[Pos + 1]) { |
| 1815 | // $$ => $ |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1816 | case '$': |
| 1817 | OS << '$'; |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 1818 | break; |
| 1819 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1820 | // $n => number of arguments |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1821 | case 'n': |
| 1822 | OS << A.size(); |
| 1823 | break; |
| 1824 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1825 | // $[0-9] => argument |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1826 | default: { |
| 1827 | // Missing arguments are ignored. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1828 | unsigned Index = Body[Pos + 1] - '0'; |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1829 | if (Index >= A.size()) |
| 1830 | break; |
| 1831 | |
| 1832 | // Otherwise substitute with the token values, with spaces eliminated. |
Eli Bendersky | a7b905e | 2013-01-14 19:00:26 +0000 | [diff] [blame] | 1833 | for (MCAsmMacroArgument::const_iterator it = A[Index].begin(), |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1834 | ie = A[Index].end(); |
| 1835 | it != ie; ++it) |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1836 | OS << it->getString(); |
| 1837 | break; |
| 1838 | } |
| 1839 | } |
| 1840 | Pos += 2; |
| 1841 | } else { |
| 1842 | unsigned I = Pos + 1; |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 1843 | |
| 1844 | // Check for the \@ pseudo-variable. |
| 1845 | if (EnableAtPseudoVariable && Body[I] == '@' && I + 1 != End) |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1846 | ++I; |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 1847 | else |
| 1848 | while (isIdentifierChar(Body[I]) && I + 1 != End) |
| 1849 | ++I; |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1850 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1851 | const char *Begin = Body.data() + Pos + 1; |
| 1852 | StringRef Argument(Begin, I - (Pos + 1)); |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1853 | unsigned Index = 0; |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1854 | |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 1855 | if (Argument == "@") { |
| 1856 | OS << NumOfMacroInstantiations; |
| 1857 | Pos += 2; |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1858 | } else { |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 1859 | for (; Index < NParameters; ++Index) |
| 1860 | if (Parameters[Index].Name == Argument) |
| 1861 | break; |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1862 | |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 1863 | if (Index == NParameters) { |
| 1864 | if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')') |
| 1865 | Pos += 3; |
| 1866 | else { |
| 1867 | OS << '\\' << Argument; |
| 1868 | Pos = I; |
| 1869 | } |
| 1870 | } else { |
| 1871 | bool VarargParameter = HasVararg && Index == (NParameters - 1); |
| 1872 | for (MCAsmMacroArgument::const_iterator it = A[Index].begin(), |
| 1873 | ie = A[Index].end(); |
| 1874 | it != ie; ++it) |
| 1875 | // We expect no quotes around the string's contents when |
| 1876 | // parsing for varargs. |
| 1877 | if (it->getKind() != AsmToken::String || VarargParameter) |
| 1878 | OS << it->getString(); |
| 1879 | else |
| 1880 | OS << it->getStringContents(); |
| 1881 | |
| 1882 | Pos += 1 + Argument.size(); |
| 1883 | } |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1884 | } |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1885 | } |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 1886 | // Update the scan point. |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1887 | Body = Body.substr(Pos); |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 1888 | } |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 1889 | |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1890 | return false; |
| 1891 | } |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 1892 | |
Nico Weber | 2a8f922 | 2014-07-24 16:29:04 +0000 | [diff] [blame] | 1893 | MacroInstantiation::MacroInstantiation(SMLoc IL, int EB, SMLoc EL, |
Rafael Espindola | 9eef18c | 2014-08-27 19:49:03 +0000 | [diff] [blame] | 1894 | size_t CondStackDepth) |
Rafael Espindola | f43a94e | 2014-08-17 22:48:55 +0000 | [diff] [blame] | 1895 | : InstantiationLoc(IL), ExitBuffer(EB), ExitLoc(EL), |
Nico Weber | 155dccd1 | 2014-07-24 17:08:39 +0000 | [diff] [blame] | 1896 | CondStackDepth(CondStackDepth) {} |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 1897 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1898 | static bool isOperator(AsmToken::TokenKind kind) { |
| 1899 | switch (kind) { |
| 1900 | default: |
| 1901 | return false; |
| 1902 | case AsmToken::Plus: |
| 1903 | case AsmToken::Minus: |
| 1904 | case AsmToken::Tilde: |
| 1905 | case AsmToken::Slash: |
| 1906 | case AsmToken::Star: |
| 1907 | case AsmToken::Dot: |
| 1908 | case AsmToken::Equal: |
| 1909 | case AsmToken::EqualEqual: |
| 1910 | case AsmToken::Pipe: |
| 1911 | case AsmToken::PipePipe: |
| 1912 | case AsmToken::Caret: |
| 1913 | case AsmToken::Amp: |
| 1914 | case AsmToken::AmpAmp: |
| 1915 | case AsmToken::Exclaim: |
| 1916 | case AsmToken::ExclaimEqual: |
| 1917 | case AsmToken::Percent: |
| 1918 | case AsmToken::Less: |
| 1919 | case AsmToken::LessEqual: |
| 1920 | case AsmToken::LessLess: |
| 1921 | case AsmToken::LessGreater: |
| 1922 | case AsmToken::Greater: |
| 1923 | case AsmToken::GreaterEqual: |
| 1924 | case AsmToken::GreaterGreater: |
| 1925 | return true; |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1926 | } |
| 1927 | } |
| 1928 | |
David Majnemer | 1625245 | 2014-01-29 00:07:39 +0000 | [diff] [blame] | 1929 | namespace { |
| 1930 | class AsmLexerSkipSpaceRAII { |
| 1931 | public: |
| 1932 | AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) { |
| 1933 | Lexer.setSkipSpace(SkipSpace); |
| 1934 | } |
| 1935 | |
| 1936 | ~AsmLexerSkipSpaceRAII() { |
| 1937 | Lexer.setSkipSpace(true); |
| 1938 | } |
| 1939 | |
| 1940 | private: |
| 1941 | AsmLexer &Lexer; |
| 1942 | }; |
| 1943 | } |
| 1944 | |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 1945 | bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg) { |
| 1946 | |
| 1947 | if (Vararg) { |
| 1948 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
| 1949 | StringRef Str = parseStringToEndOfStatement(); |
| 1950 | MA.push_back(AsmToken(AsmToken::String, Str)); |
| 1951 | } |
| 1952 | return false; |
| 1953 | } |
| 1954 | |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 1955 | unsigned ParenLevel = 0; |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1956 | unsigned AddTokens = 0; |
| 1957 | |
David Majnemer | 1625245 | 2014-01-29 00:07:39 +0000 | [diff] [blame] | 1958 | // Darwin doesn't use spaces to delmit arguments. |
| 1959 | AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin); |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 1960 | |
| 1961 | for (;;) { |
David Majnemer | 1625245 | 2014-01-29 00:07:39 +0000 | [diff] [blame] | 1962 | if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal)) |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 1963 | return TokError("unexpected token in macro instantiation"); |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1964 | |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 1965 | if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1966 | break; |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1967 | |
| 1968 | if (Lexer.is(AsmToken::Space)) { |
| 1969 | Lex(); // Eat spaces |
| 1970 | |
| 1971 | // Spaces can delimit parameters, but could also be part an expression. |
| 1972 | // If the token after a space is an operator, add the token and the next |
| 1973 | // one into this argument |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 1974 | if (!IsDarwin) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1975 | if (isOperator(Lexer.getKind())) { |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1976 | // Check to see whether the token is used as an operator, |
| 1977 | // or part of an identifier |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 1978 | const char *NextChar = getTok().getEndLoc().getPointer(); |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1979 | if (*NextChar == ' ') |
| 1980 | AddTokens = 2; |
| 1981 | } |
| 1982 | |
| 1983 | if (!AddTokens && ParenLevel == 0) { |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1984 | break; |
| 1985 | } |
| 1986 | } |
| 1987 | } |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 1988 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1989 | // handleMacroEntry relies on not advancing the lexer here |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 1990 | // to be able to fill in the remaining default parameter values |
| 1991 | if (Lexer.is(AsmToken::EndOfStatement)) |
| 1992 | break; |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 1993 | |
| 1994 | // Adjust the current parentheses level. |
| 1995 | if (Lexer.is(AsmToken::LParen)) |
| 1996 | ++ParenLevel; |
| 1997 | else if (Lexer.is(AsmToken::RParen) && ParenLevel) |
| 1998 | --ParenLevel; |
| 1999 | |
| 2000 | // Append the token to the current argument list. |
| 2001 | MA.push_back(getTok()); |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2002 | if (AddTokens) |
| 2003 | AddTokens--; |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2004 | Lex(); |
| 2005 | } |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2006 | |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2007 | if (ParenLevel != 0) |
Rafael Espindola | 3e5eb42 | 2012-08-21 15:55:04 +0000 | [diff] [blame] | 2008 | return TokError("unbalanced parentheses in macro argument"); |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2009 | return false; |
| 2010 | } |
| 2011 | |
| 2012 | // Parse the macro instantiation arguments. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2013 | bool AsmParser::parseMacroArguments(const MCAsmMacro *M, |
Vladimir Medic | 9bad0d33 | 2013-08-20 13:33:18 +0000 | [diff] [blame] | 2014 | MCAsmMacroArguments &A) { |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2015 | const unsigned NParameters = M ? M->Parameters.size() : 0; |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2016 | bool NamedParametersFound = false; |
| 2017 | SmallVector<SMLoc, 4> FALocs; |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2018 | |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2019 | A.resize(NParameters); |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2020 | FALocs.resize(NParameters); |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2021 | |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2022 | // Parse two kinds of macro invocations: |
| 2023 | // - macros defined without any parameters accept an arbitrary number of them |
| 2024 | // - macros defined with parameters accept at most that many of them |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 2025 | bool HasVararg = NParameters ? M->Parameters.back().Vararg : false; |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2026 | for (unsigned Parameter = 0; !NParameters || Parameter < NParameters; |
| 2027 | ++Parameter) { |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2028 | SMLoc IDLoc = Lexer.getLoc(); |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2029 | MCAsmMacroParameter FA; |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2030 | |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2031 | if (Lexer.is(AsmToken::Identifier) && Lexer.peekTok().is(AsmToken::Equal)) { |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 2032 | if (parseIdentifier(FA.Name)) { |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2033 | Error(IDLoc, "invalid argument identifier for formal argument"); |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2034 | eatToEndOfStatement(); |
| 2035 | return true; |
| 2036 | } |
| 2037 | |
| 2038 | if (!Lexer.is(AsmToken::Equal)) { |
| 2039 | TokError("expected '=' after formal parameter identifier"); |
| 2040 | eatToEndOfStatement(); |
| 2041 | return true; |
| 2042 | } |
| 2043 | Lex(); |
| 2044 | |
| 2045 | NamedParametersFound = true; |
| 2046 | } |
| 2047 | |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 2048 | if (NamedParametersFound && FA.Name.empty()) { |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2049 | Error(IDLoc, "cannot mix positional and keyword arguments"); |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2050 | eatToEndOfStatement(); |
| 2051 | return true; |
| 2052 | } |
| 2053 | |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 2054 | bool Vararg = HasVararg && Parameter == (NParameters - 1); |
| 2055 | if (parseMacroArgument(FA.Value, Vararg)) |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2056 | return true; |
| 2057 | |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2058 | unsigned PI = Parameter; |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 2059 | if (!FA.Name.empty()) { |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2060 | unsigned FAI = 0; |
| 2061 | for (FAI = 0; FAI < NParameters; ++FAI) |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 2062 | if (M->Parameters[FAI].Name == FA.Name) |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2063 | break; |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2064 | |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2065 | if (FAI >= NParameters) { |
Oliver Stannard | 8b27308 | 2014-06-19 15:52:37 +0000 | [diff] [blame] | 2066 | assert(M && "expected macro to be defined"); |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2067 | Error(IDLoc, |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 2068 | "parameter named '" + FA.Name + "' does not exist for macro '" + |
Saleem Abdulrasool | 3f44cd7 | 2014-03-17 17:13:57 +0000 | [diff] [blame] | 2069 | M->Name + "'"); |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2070 | return true; |
| 2071 | } |
| 2072 | PI = FAI; |
| 2073 | } |
| 2074 | |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 2075 | if (!FA.Value.empty()) { |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2076 | if (A.size() <= PI) |
| 2077 | A.resize(PI + 1); |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 2078 | A[PI] = FA.Value; |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2079 | |
| 2080 | if (FALocs.size() <= PI) |
| 2081 | FALocs.resize(PI + 1); |
| 2082 | |
| 2083 | FALocs[PI] = Lexer.getLoc(); |
Preston Gurd | 242ed315 | 2012-09-19 20:29:04 +0000 | [diff] [blame] | 2084 | } |
Jim Grosbach | 20666162 | 2012-07-30 22:44:17 +0000 | [diff] [blame] | 2085 | |
Preston Gurd | 242ed315 | 2012-09-19 20:29:04 +0000 | [diff] [blame] | 2086 | // At the end of the statement, fill in remaining arguments that have |
| 2087 | // default values. If there aren't any, then the next argument is |
| 2088 | // required but missing |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2089 | if (Lexer.is(AsmToken::EndOfStatement)) { |
| 2090 | bool Failure = false; |
| 2091 | for (unsigned FAI = 0; FAI < NParameters; ++FAI) { |
| 2092 | if (A[FAI].empty()) { |
| 2093 | if (M->Parameters[FAI].Required) { |
| 2094 | Error(FALocs[FAI].isValid() ? FALocs[FAI] : Lexer.getLoc(), |
| 2095 | "missing value for required parameter " |
| 2096 | "'" + M->Parameters[FAI].Name + "' in macro '" + M->Name + "'"); |
| 2097 | Failure = true; |
| 2098 | } |
| 2099 | |
| 2100 | if (!M->Parameters[FAI].Value.empty()) |
| 2101 | A[FAI] = M->Parameters[FAI].Value; |
| 2102 | } |
| 2103 | } |
| 2104 | return Failure; |
| 2105 | } |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2106 | |
| 2107 | if (Lexer.is(AsmToken::Comma)) |
| 2108 | Lex(); |
| 2109 | } |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2110 | |
| 2111 | return TokError("too many positional arguments"); |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2112 | } |
| 2113 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2114 | const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) { |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 2115 | StringMap<MCAsmMacro>::iterator I = MacroMap.find(Name); |
| 2116 | return (I == MacroMap.end()) ? nullptr : &I->getValue(); |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 2117 | } |
| 2118 | |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 2119 | void AsmParser::defineMacro(StringRef Name, MCAsmMacro Macro) { |
| 2120 | MacroMap.insert(std::make_pair(Name, std::move(Macro))); |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 2121 | } |
| 2122 | |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 2123 | void AsmParser::undefineMacro(StringRef Name) { MacroMap.erase(Name); } |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 2124 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2125 | bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) { |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2126 | // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate |
| 2127 | // this, although we should protect against infinite loops. |
| 2128 | if (ActiveMacros.size() == 20) |
| 2129 | return TokError("macros cannot be nested more than 20 levels deep"); |
| 2130 | |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 2131 | MCAsmMacroArguments A; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2132 | if (parseMacroArguments(M, A)) |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2133 | return true; |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2134 | |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2135 | // Macro instantiation is lexical, unfortunately. We construct a new buffer |
| 2136 | // to hold the macro body with substitutions. |
| 2137 | SmallString<256> Buf; |
| 2138 | StringRef Body = M->Body; |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 2139 | raw_svector_ostream OS(Buf); |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2140 | |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 2141 | if (expandMacro(OS, Body, M->Parameters, A, true, getTok().getLoc())) |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2142 | return true; |
| 2143 | |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 2144 | // 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] | 2145 | // instantiation. |
| 2146 | OS << ".endmacro\n"; |
| 2147 | |
Rafael Espindola | 3560ff2 | 2014-08-27 20:03:13 +0000 | [diff] [blame] | 2148 | std::unique_ptr<MemoryBuffer> Instantiation = |
| 2149 | MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>"); |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2150 | |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2151 | // Create the macro instantiation object and add to the current macro |
| 2152 | // instantiation stack. |
Rafael Espindola | 9eef18c | 2014-08-27 19:49:03 +0000 | [diff] [blame] | 2153 | MacroInstantiation *MI = new MacroInstantiation( |
| 2154 | NameLoc, CurBuffer, getTok().getLoc(), TheCondStack.size()); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2155 | ActiveMacros.push_back(MI); |
| 2156 | |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 2157 | ++NumOfMacroInstantiations; |
| 2158 | |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2159 | // Jump to the macro instantiation and prime the lexer. |
David Blaikie | 1961f14 | 2014-08-21 20:44:56 +0000 | [diff] [blame] | 2160 | CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc()); |
Rafael Espindola | 8026bd0 | 2014-07-06 14:17:29 +0000 | [diff] [blame] | 2161 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer()); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2162 | Lex(); |
| 2163 | |
| 2164 | return false; |
| 2165 | } |
| 2166 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2167 | void AsmParser::handleMacroExit() { |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2168 | // Jump to the EndOfStatement we should return to, and consume it. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2169 | jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2170 | Lex(); |
| 2171 | |
| 2172 | // Pop the instantiation entry. |
| 2173 | delete ActiveMacros.back(); |
| 2174 | ActiveMacros.pop_back(); |
| 2175 | } |
| 2176 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2177 | static bool isUsedIn(const MCSymbol *Sym, const MCExpr *Value) { |
Rafael Espindola | 46c79ef | 2010-11-15 14:40:36 +0000 | [diff] [blame] | 2178 | switch (Value->getKind()) { |
Rafael Espindola | 72f5f17 | 2012-01-28 05:57:00 +0000 | [diff] [blame] | 2179 | case MCExpr::Binary: { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2180 | const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value); |
| 2181 | return isUsedIn(Sym, BE->getLHS()) || isUsedIn(Sym, BE->getRHS()); |
Rafael Espindola | 46c79ef | 2010-11-15 14:40:36 +0000 | [diff] [blame] | 2182 | } |
Rafael Espindola | 72f5f17 | 2012-01-28 05:57:00 +0000 | [diff] [blame] | 2183 | case MCExpr::Target: |
| 2184 | case MCExpr::Constant: |
| 2185 | return false; |
| 2186 | case MCExpr::SymbolRef: { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2187 | const MCSymbol &S = |
| 2188 | static_cast<const MCSymbolRefExpr *>(Value)->getSymbol(); |
Rafael Espindola | 0047258 | 2012-01-28 06:22:14 +0000 | [diff] [blame] | 2189 | if (S.isVariable()) |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2190 | return isUsedIn(Sym, S.getVariableValue()); |
Rafael Espindola | 0047258 | 2012-01-28 06:22:14 +0000 | [diff] [blame] | 2191 | return &S == Sym; |
Rafael Espindola | 72f5f17 | 2012-01-28 05:57:00 +0000 | [diff] [blame] | 2192 | } |
Rafael Espindola | 46c79ef | 2010-11-15 14:40:36 +0000 | [diff] [blame] | 2193 | case MCExpr::Unary: |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2194 | return isUsedIn(Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr()); |
Rafael Espindola | 46c79ef | 2010-11-15 14:40:36 +0000 | [diff] [blame] | 2195 | } |
Benjamin Kramer | 4efe506 | 2012-01-28 15:28:41 +0000 | [diff] [blame] | 2196 | |
| 2197 | llvm_unreachable("Unknown expr kind!"); |
Rafael Espindola | 46c79ef | 2010-11-15 14:40:36 +0000 | [diff] [blame] | 2198 | } |
| 2199 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2200 | bool AsmParser::parseAssignment(StringRef Name, bool allow_redef, |
Jim Grosbach | b7b750d | 2012-09-13 23:11:31 +0000 | [diff] [blame] | 2201 | bool NoDeadStrip) { |
Daniel Dunbar | ae7ac01 | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 2202 | // FIXME: Use better location, we should use proper tokens. |
| 2203 | SMLoc EqualLoc = Lexer.getLoc(); |
| 2204 | |
Daniel Dunbar | 897ffad | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 2205 | const MCExpr *Value; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2206 | if (parseExpression(Value)) |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2207 | return true; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2208 | |
Rafael Espindola | 72f5f17 | 2012-01-28 05:57:00 +0000 | [diff] [blame] | 2209 | // Note: we don't count b as used in "a = b". This is to allow |
| 2210 | // a = b |
| 2211 | // b = c |
Rafael Espindola | 46c79ef | 2010-11-15 14:40:36 +0000 | [diff] [blame] | 2212 | |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 2213 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2214 | return TokError("unexpected token in assignment"); |
| 2215 | |
| 2216 | // Eat the end of statement marker. |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2217 | Lex(); |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2218 | |
Daniel Dunbar | 5f33924 | 2009-10-16 01:57:39 +0000 | [diff] [blame] | 2219 | // Validate that the LHS is allowed to be a variable (either it has not been |
| 2220 | // used as a symbol, or it is an absolute symbol). |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 2221 | MCSymbol *Sym = getContext().lookupSymbol(Name); |
Daniel Dunbar | 5f33924 | 2009-10-16 01:57:39 +0000 | [diff] [blame] | 2222 | if (Sym) { |
| 2223 | // Diagnose assignment to a label. |
| 2224 | // |
| 2225 | // FIXME: Diagnostics. Note the location of the definition as a label. |
| 2226 | // FIXME: Diagnose assignment to protected identifier (e.g., register name). |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2227 | if (isUsedIn(Sym, Value)) |
Rafael Espindola | 72f5f17 | 2012-01-28 05:57:00 +0000 | [diff] [blame] | 2228 | return Error(EqualLoc, "Recursive use of '" + Name + "'"); |
| 2229 | else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable()) |
Daniel Dunbar | 9b4a824 | 2010-05-17 17:46:23 +0000 | [diff] [blame] | 2230 | ; // Allow redefinitions of undefined symbols only used in directives. |
Jim Grosbach | 1283317 | 2012-03-20 21:33:21 +0000 | [diff] [blame] | 2231 | else if (Sym->isVariable() && !Sym->isUsed() && allow_redef) |
| 2232 | ; // Allow redefinitions of variables that haven't yet been used. |
Daniel Dunbar | 1bf128e | 2011-04-29 17:53:11 +0000 | [diff] [blame] | 2233 | else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef)) |
Daniel Dunbar | 5f33924 | 2009-10-16 01:57:39 +0000 | [diff] [blame] | 2234 | return Error(EqualLoc, "redefinition of '" + Name + "'"); |
| 2235 | else if (!Sym->isVariable()) |
| 2236 | return Error(EqualLoc, "invalid assignment to '" + Name + "'"); |
Daniel Dunbar | 7a989da | 2010-05-05 17:41:00 +0000 | [diff] [blame] | 2237 | else if (!isa<MCConstantExpr>(Sym->getVariableValue())) |
Daniel Dunbar | 5f33924 | 2009-10-16 01:57:39 +0000 | [diff] [blame] | 2238 | return Error(EqualLoc, "invalid reassignment of non-absolute variable '" + |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2239 | Name + "'"); |
Rafael Espindola | 46c79ef | 2010-11-15 14:40:36 +0000 | [diff] [blame] | 2240 | |
| 2241 | // Don't count these checks as uses. |
| 2242 | Sym->setUsed(false); |
Anders Waldenborg | 8480957 | 2014-02-17 20:48:32 +0000 | [diff] [blame] | 2243 | } else if (Name == ".") { |
| 2244 | if (Out.EmitValueToOffset(Value, 0)) { |
| 2245 | Error(EqualLoc, "expected absolute expression"); |
| 2246 | eatToEndOfStatement(); |
| 2247 | } |
| 2248 | return false; |
Daniel Dunbar | 5f33924 | 2009-10-16 01:57:39 +0000 | [diff] [blame] | 2249 | } else |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 2250 | Sym = getContext().getOrCreateSymbol(Name); |
Daniel Dunbar | 5f33924 | 2009-10-16 01:57:39 +0000 | [diff] [blame] | 2251 | |
David Majnemer | 58cb80c | 2014-12-24 10:27:50 +0000 | [diff] [blame] | 2252 | Sym->setRedefinable(allow_redef); |
| 2253 | |
Daniel Dunbar | ae7ac01 | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 2254 | // Do the assignment. |
Daniel Dunbar | b7b2097 | 2009-08-31 08:09:09 +0000 | [diff] [blame] | 2255 | Out.EmitAssignment(Sym, Value); |
Jim Grosbach | b7b750d | 2012-09-13 23:11:31 +0000 | [diff] [blame] | 2256 | if (NoDeadStrip) |
| 2257 | Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip); |
| 2258 | |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2259 | return false; |
| 2260 | } |
| 2261 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2262 | /// parseIdentifier: |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 2263 | /// ::= identifier |
| 2264 | /// ::= string |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2265 | bool AsmParser::parseIdentifier(StringRef &Res) { |
Daniel Dunbar | 3b96ffd | 2010-08-24 18:12:12 +0000 | [diff] [blame] | 2266 | // The assembler has relaxed rules for accepting identifiers, in particular we |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 2267 | // allow things like '.globl $foo' and '.def @feat.00', which would normally be |
| 2268 | // 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] | 2269 | // handle this as a context dependent token, instead we detect adjacent tokens |
| 2270 | // and return the combined identifier. |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 2271 | if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) { |
| 2272 | SMLoc PrefixLoc = getLexer().getLoc(); |
Daniel Dunbar | 3b96ffd | 2010-08-24 18:12:12 +0000 | [diff] [blame] | 2273 | |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 2274 | // Consume the prefix character, and check for a following identifier. |
Daniel Dunbar | 3b96ffd | 2010-08-24 18:12:12 +0000 | [diff] [blame] | 2275 | Lex(); |
| 2276 | if (Lexer.isNot(AsmToken::Identifier)) |
| 2277 | return true; |
| 2278 | |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 2279 | // We have a '$' or '@' followed by an identifier, make sure they are adjacent. |
| 2280 | if (PrefixLoc.getPointer() + 1 != getTok().getLoc().getPointer()) |
Daniel Dunbar | 3b96ffd | 2010-08-24 18:12:12 +0000 | [diff] [blame] | 2281 | return true; |
| 2282 | |
| 2283 | // Construct the joined identifier and consume the token. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2284 | Res = |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 2285 | StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1); |
Daniel Dunbar | 3b96ffd | 2010-08-24 18:12:12 +0000 | [diff] [blame] | 2286 | Lex(); |
| 2287 | return false; |
| 2288 | } |
| 2289 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2290 | if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String)) |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 2291 | return true; |
| 2292 | |
Sean Callanan | 936b0d3 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 2293 | Res = getTok().getIdentifier(); |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 2294 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2295 | Lex(); // Consume the identifier token. |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 2296 | |
| 2297 | return false; |
| 2298 | } |
| 2299 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2300 | /// parseDirectiveSet: |
Nico Weber | 4ada0d9 | 2011-01-28 03:04:41 +0000 | [diff] [blame] | 2301 | /// ::= .equ identifier ',' expression |
| 2302 | /// ::= .equiv identifier ',' expression |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2303 | /// ::= .set identifier ',' expression |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2304 | bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) { |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 2305 | StringRef Name; |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2306 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2307 | if (parseIdentifier(Name)) |
Roman Divacky | 41e6ceb | 2010-10-28 16:57:58 +0000 | [diff] [blame] | 2308 | return TokError("expected identifier after '" + Twine(IDVal) + "'"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2309 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2310 | if (getLexer().isNot(AsmToken::Comma)) |
Roman Divacky | 41e6ceb | 2010-10-28 16:57:58 +0000 | [diff] [blame] | 2311 | return TokError("unexpected token in '" + Twine(IDVal) + "'"); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2312 | Lex(); |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2313 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2314 | return parseAssignment(Name, allow_redef, true); |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2315 | } |
| 2316 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2317 | bool AsmParser::parseEscapedString(std::string &Data) { |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2318 | assert(getLexer().is(AsmToken::String) && "Unexpected current token!"); |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2319 | |
| 2320 | Data = ""; |
Sean Callanan | 936b0d3 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 2321 | StringRef Str = getTok().getStringContents(); |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2322 | for (unsigned i = 0, e = Str.size(); i != e; ++i) { |
| 2323 | if (Str[i] != '\\') { |
| 2324 | Data += Str[i]; |
| 2325 | continue; |
| 2326 | } |
| 2327 | |
| 2328 | // Recognize escaped characters. Note that this escape semantics currently |
| 2329 | // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes. |
| 2330 | ++i; |
| 2331 | if (i == e) |
| 2332 | return TokError("unexpected backslash at end of string"); |
| 2333 | |
| 2334 | // Recognize octal sequences. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2335 | if ((unsigned)(Str[i] - '0') <= 7) { |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2336 | // Consume up to three octal characters. |
| 2337 | unsigned Value = Str[i] - '0'; |
| 2338 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2339 | if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) { |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2340 | ++i; |
| 2341 | Value = Value * 8 + (Str[i] - '0'); |
| 2342 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2343 | if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) { |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2344 | ++i; |
| 2345 | Value = Value * 8 + (Str[i] - '0'); |
| 2346 | } |
| 2347 | } |
| 2348 | |
| 2349 | if (Value > 255) |
| 2350 | return TokError("invalid octal escape sequence (out of range)"); |
| 2351 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2352 | Data += (unsigned char)Value; |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2353 | continue; |
| 2354 | } |
| 2355 | |
| 2356 | // Otherwise recognize individual escapes. |
| 2357 | switch (Str[i]) { |
| 2358 | default: |
| 2359 | // Just reject invalid escape sequences for now. |
| 2360 | return TokError("invalid escape sequence (unrecognized character)"); |
| 2361 | |
| 2362 | case 'b': Data += '\b'; break; |
| 2363 | case 'f': Data += '\f'; break; |
| 2364 | case 'n': Data += '\n'; break; |
| 2365 | case 'r': Data += '\r'; break; |
| 2366 | case 't': Data += '\t'; break; |
| 2367 | case '"': Data += '"'; break; |
| 2368 | case '\\': Data += '\\'; break; |
| 2369 | } |
| 2370 | } |
| 2371 | |
| 2372 | return false; |
| 2373 | } |
| 2374 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2375 | /// parseDirectiveAscii: |
Rafael Espindola | 63760ba | 2010-10-28 20:02:27 +0000 | [diff] [blame] | 2376 | /// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2377 | bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) { |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2378 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2379 | checkForValidSection(); |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 2380 | |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2381 | for (;;) { |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2382 | if (getLexer().isNot(AsmToken::String)) |
Rafael Espindola | 63760ba | 2010-10-28 20:02:27 +0000 | [diff] [blame] | 2383 | return TokError("expected string in '" + Twine(IDVal) + "' directive"); |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2384 | |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2385 | std::string Data; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2386 | if (parseEscapedString(Data)) |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2387 | return true; |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2388 | |
Rafael Espindola | 64e1af8 | 2013-07-02 15:49:13 +0000 | [diff] [blame] | 2389 | getStreamer().EmitBytes(Data); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2390 | if (ZeroTerminated) |
Rafael Espindola | 64e1af8 | 2013-07-02 15:49:13 +0000 | [diff] [blame] | 2391 | getStreamer().EmitBytes(StringRef("\0", 1)); |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2392 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2393 | Lex(); |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2394 | |
| 2395 | if (getLexer().is(AsmToken::EndOfStatement)) |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2396 | break; |
| 2397 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2398 | if (getLexer().isNot(AsmToken::Comma)) |
Rafael Espindola | 63760ba | 2010-10-28 20:02:27 +0000 | [diff] [blame] | 2399 | return TokError("unexpected token in '" + Twine(IDVal) + "' directive"); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2400 | Lex(); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2401 | } |
| 2402 | } |
| 2403 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2404 | Lex(); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2405 | return false; |
| 2406 | } |
| 2407 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2408 | /// parseDirectiveValue |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2409 | /// ::= (.byte | .short | ... ) [ expression (, expression)* ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2410 | bool AsmParser::parseDirectiveValue(unsigned Size) { |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2411 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2412 | checkForValidSection(); |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 2413 | |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2414 | for (;;) { |
Daniel Dunbar | 897ffad | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 2415 | const MCExpr *Value; |
Jim Grosbach | 76346c3 | 2011-06-29 16:05:14 +0000 | [diff] [blame] | 2416 | SMLoc ExprLoc = getLexer().getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2417 | if (parseExpression(Value)) |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2418 | return true; |
| 2419 | |
Daniel Dunbar | 6738a2e | 2010-05-23 18:36:38 +0000 | [diff] [blame] | 2420 | // Special case constant expressions to match code generator. |
Jim Grosbach | 76346c3 | 2011-06-29 16:05:14 +0000 | [diff] [blame] | 2421 | if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) { |
| 2422 | assert(Size <= 8 && "Invalid size"); |
| 2423 | uint64_t IntValue = MCE->getValue(); |
| 2424 | if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue)) |
| 2425 | return Error(ExprLoc, "literal value out of range for directive"); |
Rafael Espindola | 64e1af8 | 2013-07-02 15:49:13 +0000 | [diff] [blame] | 2426 | getStreamer().EmitIntValue(IntValue, Size); |
Jim Grosbach | 76346c3 | 2011-06-29 16:05:14 +0000 | [diff] [blame] | 2427 | } else |
Kevin Enderby | 96918bc | 2014-04-22 17:27:29 +0000 | [diff] [blame] | 2428 | getStreamer().EmitValue(Value, Size, ExprLoc); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2429 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2430 | if (getLexer().is(AsmToken::EndOfStatement)) |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2431 | break; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2432 | |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2433 | // FIXME: Improve diagnostic. |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2434 | if (getLexer().isNot(AsmToken::Comma)) |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2435 | return TokError("unexpected token in directive"); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2436 | Lex(); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2437 | } |
| 2438 | } |
| 2439 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2440 | Lex(); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2441 | return false; |
| 2442 | } |
| 2443 | |
David Woodhouse | d6de0d9 | 2014-02-01 16:20:59 +0000 | [diff] [blame] | 2444 | /// ParseDirectiveOctaValue |
| 2445 | /// ::= .octa [ hexconstant (, hexconstant)* ] |
| 2446 | bool AsmParser::parseDirectiveOctaValue() { |
| 2447 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2448 | checkForValidSection(); |
| 2449 | |
| 2450 | for (;;) { |
| 2451 | if (Lexer.getKind() == AsmToken::Error) |
| 2452 | return true; |
| 2453 | if (Lexer.getKind() != AsmToken::Integer && |
| 2454 | Lexer.getKind() != AsmToken::BigNum) |
| 2455 | return TokError("unknown token in expression"); |
| 2456 | |
| 2457 | SMLoc ExprLoc = getLexer().getLoc(); |
| 2458 | APInt IntValue = getTok().getAPIntVal(); |
| 2459 | Lex(); |
| 2460 | |
| 2461 | uint64_t hi, lo; |
| 2462 | if (IntValue.isIntN(64)) { |
| 2463 | hi = 0; |
| 2464 | lo = IntValue.getZExtValue(); |
| 2465 | } else if (IntValue.isIntN(128)) { |
David Woodhouse | 6c9a6f9 | 2014-02-01 16:52:33 +0000 | [diff] [blame] | 2466 | // It might actually have more than 128 bits, but the top ones are zero. |
| 2467 | hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue(); |
David Woodhouse | d6de0d9 | 2014-02-01 16:20:59 +0000 | [diff] [blame] | 2468 | lo = IntValue.getLoBits(64).getZExtValue(); |
| 2469 | } else |
| 2470 | return Error(ExprLoc, "literal value out of range for directive"); |
| 2471 | |
| 2472 | if (MAI.isLittleEndian()) { |
| 2473 | getStreamer().EmitIntValue(lo, 8); |
| 2474 | getStreamer().EmitIntValue(hi, 8); |
| 2475 | } else { |
| 2476 | getStreamer().EmitIntValue(hi, 8); |
| 2477 | getStreamer().EmitIntValue(lo, 8); |
| 2478 | } |
| 2479 | |
| 2480 | if (getLexer().is(AsmToken::EndOfStatement)) |
| 2481 | break; |
| 2482 | |
| 2483 | // FIXME: Improve diagnostic. |
| 2484 | if (getLexer().isNot(AsmToken::Comma)) |
| 2485 | return TokError("unexpected token in directive"); |
| 2486 | Lex(); |
| 2487 | } |
| 2488 | } |
| 2489 | |
| 2490 | Lex(); |
| 2491 | return false; |
| 2492 | } |
| 2493 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2494 | /// parseDirectiveRealValue |
Daniel Dunbar | 2af1653 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 2495 | /// ::= (.single | .double) [ expression (, expression)* ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2496 | bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) { |
Daniel Dunbar | 2af1653 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 2497 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2498 | checkForValidSection(); |
Daniel Dunbar | 2af1653 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 2499 | |
| 2500 | for (;;) { |
| 2501 | // We don't truly support arithmetic on floating point expressions, so we |
| 2502 | // have to manually parse unary prefixes. |
| 2503 | bool IsNeg = false; |
| 2504 | if (getLexer().is(AsmToken::Minus)) { |
| 2505 | Lex(); |
| 2506 | IsNeg = true; |
| 2507 | } else if (getLexer().is(AsmToken::Plus)) |
| 2508 | Lex(); |
| 2509 | |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2510 | if (getLexer().isNot(AsmToken::Integer) && |
Kevin Enderby | 5bbe957 | 2011-03-29 21:11:52 +0000 | [diff] [blame] | 2511 | getLexer().isNot(AsmToken::Real) && |
| 2512 | getLexer().isNot(AsmToken::Identifier)) |
Daniel Dunbar | 2af1653 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 2513 | return TokError("unexpected token in directive"); |
| 2514 | |
| 2515 | // Convert to an APFloat. |
| 2516 | APFloat Value(Semantics); |
Kevin Enderby | 5bbe957 | 2011-03-29 21:11:52 +0000 | [diff] [blame] | 2517 | StringRef IDVal = getTok().getString(); |
| 2518 | if (getLexer().is(AsmToken::Identifier)) { |
| 2519 | if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf")) |
| 2520 | Value = APFloat::getInf(Semantics); |
| 2521 | else if (!IDVal.compare_lower("nan")) |
| 2522 | Value = APFloat::getNaN(Semantics, false, ~0); |
| 2523 | else |
| 2524 | return TokError("invalid floating point literal"); |
| 2525 | } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) == |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2526 | APFloat::opInvalidOp) |
Daniel Dunbar | 2af1653 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 2527 | return TokError("invalid floating point literal"); |
| 2528 | if (IsNeg) |
| 2529 | Value.changeSign(); |
| 2530 | |
| 2531 | // Consume the numeric token. |
| 2532 | Lex(); |
| 2533 | |
| 2534 | // Emit the value as an integer. |
| 2535 | APInt AsInt = Value.bitcastToAPInt(); |
| 2536 | getStreamer().EmitIntValue(AsInt.getLimitedValue(), |
Rafael Espindola | 64e1af8 | 2013-07-02 15:49:13 +0000 | [diff] [blame] | 2537 | AsInt.getBitWidth() / 8); |
Daniel Dunbar | 2af1653 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 2538 | |
| 2539 | if (getLexer().is(AsmToken::EndOfStatement)) |
| 2540 | break; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2541 | |
Daniel Dunbar | 2af1653 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 2542 | if (getLexer().isNot(AsmToken::Comma)) |
| 2543 | return TokError("unexpected token in directive"); |
| 2544 | Lex(); |
| 2545 | } |
| 2546 | } |
| 2547 | |
| 2548 | Lex(); |
| 2549 | return false; |
| 2550 | } |
| 2551 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2552 | /// parseDirectiveZero |
Rafael Espindola | 922e3f4 | 2010-09-16 15:03:59 +0000 | [diff] [blame] | 2553 | /// ::= .zero expression |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2554 | bool AsmParser::parseDirectiveZero() { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2555 | checkForValidSection(); |
Rafael Espindola | 922e3f4 | 2010-09-16 15:03:59 +0000 | [diff] [blame] | 2556 | |
| 2557 | int64_t NumBytes; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2558 | if (parseAbsoluteExpression(NumBytes)) |
Rafael Espindola | 922e3f4 | 2010-09-16 15:03:59 +0000 | [diff] [blame] | 2559 | return true; |
| 2560 | |
Rafael Espindola | b91bac6 | 2010-10-05 19:42:57 +0000 | [diff] [blame] | 2561 | int64_t Val = 0; |
| 2562 | if (getLexer().is(AsmToken::Comma)) { |
| 2563 | Lex(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2564 | if (parseAbsoluteExpression(Val)) |
Rafael Espindola | b91bac6 | 2010-10-05 19:42:57 +0000 | [diff] [blame] | 2565 | return true; |
| 2566 | } |
| 2567 | |
Rafael Espindola | 922e3f4 | 2010-09-16 15:03:59 +0000 | [diff] [blame] | 2568 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 2569 | return TokError("unexpected token in '.zero' directive"); |
| 2570 | |
| 2571 | Lex(); |
| 2572 | |
Rafael Espindola | 64e1af8 | 2013-07-02 15:49:13 +0000 | [diff] [blame] | 2573 | getStreamer().EmitFill(NumBytes, Val); |
Rafael Espindola | 922e3f4 | 2010-09-16 15:03:59 +0000 | [diff] [blame] | 2574 | |
| 2575 | return false; |
| 2576 | } |
| 2577 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2578 | /// parseDirectiveFill |
Roman Divacky | e33098f | 2013-09-24 17:44:41 +0000 | [diff] [blame] | 2579 | /// ::= .fill expression [ , expression [ , expression ] ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2580 | bool AsmParser::parseDirectiveFill() { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2581 | checkForValidSection(); |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 2582 | |
David Majnemer | 522d3db | 2014-02-01 07:19:38 +0000 | [diff] [blame] | 2583 | SMLoc RepeatLoc = getLexer().getLoc(); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2584 | int64_t NumValues; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2585 | if (parseAbsoluteExpression(NumValues)) |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2586 | return true; |
| 2587 | |
David Majnemer | 522d3db | 2014-02-01 07:19:38 +0000 | [diff] [blame] | 2588 | if (NumValues < 0) { |
| 2589 | Warning(RepeatLoc, |
| 2590 | "'.fill' directive with negative repeat count has no effect"); |
| 2591 | NumValues = 0; |
| 2592 | } |
| 2593 | |
Roman Divacky | e33098f | 2013-09-24 17:44:41 +0000 | [diff] [blame] | 2594 | int64_t FillSize = 1; |
| 2595 | int64_t FillExpr = 0; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2596 | |
David Majnemer | 522d3db | 2014-02-01 07:19:38 +0000 | [diff] [blame] | 2597 | SMLoc SizeLoc, ExprLoc; |
Roman Divacky | e33098f | 2013-09-24 17:44:41 +0000 | [diff] [blame] | 2598 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2599 | if (getLexer().isNot(AsmToken::Comma)) |
| 2600 | return TokError("unexpected token in '.fill' directive"); |
| 2601 | Lex(); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2602 | |
David Majnemer | 522d3db | 2014-02-01 07:19:38 +0000 | [diff] [blame] | 2603 | SizeLoc = getLexer().getLoc(); |
Roman Divacky | e33098f | 2013-09-24 17:44:41 +0000 | [diff] [blame] | 2604 | if (parseAbsoluteExpression(FillSize)) |
| 2605 | return true; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2606 | |
Roman Divacky | e33098f | 2013-09-24 17:44:41 +0000 | [diff] [blame] | 2607 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2608 | if (getLexer().isNot(AsmToken::Comma)) |
| 2609 | return TokError("unexpected token in '.fill' directive"); |
| 2610 | Lex(); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2611 | |
David Majnemer | 522d3db | 2014-02-01 07:19:38 +0000 | [diff] [blame] | 2612 | ExprLoc = getLexer().getLoc(); |
Roman Divacky | e33098f | 2013-09-24 17:44:41 +0000 | [diff] [blame] | 2613 | if (parseAbsoluteExpression(FillExpr)) |
| 2614 | return true; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2615 | |
Roman Divacky | e33098f | 2013-09-24 17:44:41 +0000 | [diff] [blame] | 2616 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 2617 | return TokError("unexpected token in '.fill' directive"); |
| 2618 | |
| 2619 | Lex(); |
| 2620 | } |
| 2621 | } |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2622 | |
David Majnemer | 522d3db | 2014-02-01 07:19:38 +0000 | [diff] [blame] | 2623 | if (FillSize < 0) { |
| 2624 | Warning(SizeLoc, "'.fill' directive with negative size has no effect"); |
| 2625 | NumValues = 0; |
| 2626 | } |
| 2627 | if (FillSize > 8) { |
| 2628 | Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8"); |
| 2629 | FillSize = 8; |
| 2630 | } |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2631 | |
David Majnemer | 522d3db | 2014-02-01 07:19:38 +0000 | [diff] [blame] | 2632 | if (!isUInt<32>(FillExpr) && FillSize > 4) |
| 2633 | Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits"); |
| 2634 | |
Alexey Samsonov | 1b0713c | 2014-09-02 17:25:29 +0000 | [diff] [blame] | 2635 | if (NumValues > 0) { |
| 2636 | int64_t NonZeroFillSize = FillSize > 4 ? 4 : FillSize; |
| 2637 | FillExpr &= ~0ULL >> (64 - NonZeroFillSize * 8); |
| 2638 | for (uint64_t i = 0, e = NumValues; i != e; ++i) { |
| 2639 | getStreamer().EmitIntValue(FillExpr, NonZeroFillSize); |
| 2640 | if (NonZeroFillSize < FillSize) |
| 2641 | getStreamer().EmitIntValue(0, FillSize - NonZeroFillSize); |
| 2642 | } |
David Majnemer | 522d3db | 2014-02-01 07:19:38 +0000 | [diff] [blame] | 2643 | } |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2644 | |
| 2645 | return false; |
| 2646 | } |
Daniel Dunbar | 4a5a561 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 2647 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2648 | /// parseDirectiveOrg |
Daniel Dunbar | 4a5a561 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 2649 | /// ::= .org expression [ , expression ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2650 | bool AsmParser::parseDirectiveOrg() { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2651 | checkForValidSection(); |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 2652 | |
Daniel Dunbar | 897ffad | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 2653 | const MCExpr *Offset; |
Jim Grosbach | b591277 | 2012-01-27 00:37:08 +0000 | [diff] [blame] | 2654 | SMLoc Loc = getTok().getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2655 | if (parseExpression(Offset)) |
Daniel Dunbar | 4a5a561 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 2656 | return true; |
| 2657 | |
| 2658 | // Parse optional fill expression. |
| 2659 | int64_t FillExpr = 0; |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2660 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2661 | if (getLexer().isNot(AsmToken::Comma)) |
Daniel Dunbar | 4a5a561 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 2662 | return TokError("unexpected token in '.org' directive"); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2663 | Lex(); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2664 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2665 | if (parseAbsoluteExpression(FillExpr)) |
Daniel Dunbar | 4a5a561 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 2666 | return true; |
| 2667 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2668 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Daniel Dunbar | 4a5a561 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 2669 | return TokError("unexpected token in '.org' directive"); |
| 2670 | } |
| 2671 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2672 | Lex(); |
Daniel Dunbar | 75630b3 | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 2673 | |
Jim Grosbach | b591277 | 2012-01-27 00:37:08 +0000 | [diff] [blame] | 2674 | // Only limited forms of relocatable expressions are accepted here, it |
| 2675 | // has to be relative to the current section. The streamer will return |
| 2676 | // 'true' if the expression wasn't evaluatable. |
| 2677 | if (getStreamer().EmitValueToOffset(Offset, FillExpr)) |
| 2678 | return Error(Loc, "expected assembly-time absolute expression"); |
Daniel Dunbar | 4a5a561 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 2679 | |
| 2680 | return false; |
| 2681 | } |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2682 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2683 | /// parseDirectiveAlign |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2684 | /// ::= {.align, ...} expression [ , expression [ , expression ]] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2685 | bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2686 | checkForValidSection(); |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 2687 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2688 | SMLoc AlignmentLoc = getLexer().getLoc(); |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2689 | int64_t Alignment; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2690 | if (parseAbsoluteExpression(Alignment)) |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2691 | return true; |
| 2692 | |
| 2693 | SMLoc MaxBytesLoc; |
| 2694 | bool HasFillExpr = false; |
| 2695 | int64_t FillExpr = 0; |
| 2696 | int64_t MaxBytesToFill = 0; |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2697 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2698 | if (getLexer().isNot(AsmToken::Comma)) |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2699 | return TokError("unexpected token in directive"); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2700 | Lex(); |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2701 | |
| 2702 | // The fill expression can be omitted while specifying a maximum number of |
| 2703 | // alignment bytes, e.g: |
| 2704 | // .align 3,,4 |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2705 | if (getLexer().isNot(AsmToken::Comma)) { |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2706 | HasFillExpr = true; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2707 | if (parseAbsoluteExpression(FillExpr)) |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2708 | return true; |
| 2709 | } |
| 2710 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2711 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2712 | if (getLexer().isNot(AsmToken::Comma)) |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2713 | return TokError("unexpected token in directive"); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2714 | Lex(); |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2715 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2716 | MaxBytesLoc = getLexer().getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2717 | if (parseAbsoluteExpression(MaxBytesToFill)) |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2718 | return true; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2719 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2720 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2721 | return TokError("unexpected token in directive"); |
| 2722 | } |
| 2723 | } |
| 2724 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2725 | Lex(); |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2726 | |
Daniel Dunbar | bb166be | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 2727 | if (!HasFillExpr) |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2728 | FillExpr = 0; |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2729 | |
| 2730 | // Compute alignment in bytes. |
| 2731 | if (IsPow2) { |
| 2732 | // FIXME: Diagnose overflow. |
Daniel Dunbar | 18f3c9b | 2009-08-26 09:16:34 +0000 | [diff] [blame] | 2733 | if (Alignment >= 32) { |
| 2734 | Error(AlignmentLoc, "invalid alignment value"); |
| 2735 | Alignment = 31; |
| 2736 | } |
| 2737 | |
Benjamin Kramer | 63951ad | 2009-09-06 09:35:10 +0000 | [diff] [blame] | 2738 | Alignment = 1ULL << Alignment; |
Benjamin Kramer | 64bf780 | 2013-02-16 15:00:16 +0000 | [diff] [blame] | 2739 | } else { |
| 2740 | // Reject alignments that aren't a power of two, for gas compatibility. |
| 2741 | if (!isPowerOf2_64(Alignment)) |
| 2742 | Error(AlignmentLoc, "alignment must be a power of 2"); |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2743 | } |
| 2744 | |
Daniel Dunbar | 18f3c9b | 2009-08-26 09:16:34 +0000 | [diff] [blame] | 2745 | // Diagnose non-sensical max bytes to align. |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2746 | if (MaxBytesLoc.isValid()) { |
| 2747 | if (MaxBytesToFill < 1) { |
Daniel Dunbar | 18f3c9b | 2009-08-26 09:16:34 +0000 | [diff] [blame] | 2748 | Error(MaxBytesLoc, "alignment directive can never be satisfied in this " |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2749 | "many bytes, ignoring maximum bytes expression"); |
Daniel Dunbar | 4abcccb | 2009-08-21 23:01:53 +0000 | [diff] [blame] | 2750 | MaxBytesToFill = 0; |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2751 | } |
| 2752 | |
| 2753 | if (MaxBytesToFill >= Alignment) { |
Daniel Dunbar | c9dc78a | 2009-06-30 00:49:23 +0000 | [diff] [blame] | 2754 | Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and " |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2755 | "has no effect"); |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2756 | MaxBytesToFill = 0; |
| 2757 | } |
| 2758 | } |
| 2759 | |
Daniel Dunbar | bb166be | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 2760 | // Check whether we should use optimal code alignment for this .align |
| 2761 | // directive. |
Saleem Abdulrasool | 7f2f9f4 | 2014-03-21 05:13:23 +0000 | [diff] [blame] | 2762 | const MCSection *Section = getStreamer().getCurrentSection().first; |
| 2763 | assert(Section && "must have section to emit alignment"); |
| 2764 | bool UseCodeAlign = Section->UseCodeAlign(); |
Daniel Dunbar | bb166be | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 2765 | if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) && |
| 2766 | ValueSize == 1 && UseCodeAlign) { |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2767 | getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill); |
Daniel Dunbar | bb166be | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 2768 | } else { |
Kevin Enderby | 7f99302 | 2010-02-25 18:46:04 +0000 | [diff] [blame] | 2769 | // FIXME: Target specific behavior about how the "extra" bytes are filled. |
Chris Lattner | c2b3675 | 2010-07-15 21:19:31 +0000 | [diff] [blame] | 2770 | getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize, |
| 2771 | MaxBytesToFill); |
Daniel Dunbar | bb166be | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 2772 | } |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2773 | |
| 2774 | return false; |
| 2775 | } |
| 2776 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2777 | /// parseDirectiveFile |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2778 | /// ::= .file [number] filename |
| 2779 | /// ::= .file number directory filename |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2780 | bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2781 | // FIXME: I'm not sure what this is. |
| 2782 | int64_t FileNumber = -1; |
| 2783 | SMLoc FileNumberLoc = getLexer().getLoc(); |
| 2784 | if (getLexer().is(AsmToken::Integer)) { |
| 2785 | FileNumber = getTok().getIntVal(); |
| 2786 | Lex(); |
| 2787 | |
| 2788 | if (FileNumber < 1) |
| 2789 | return TokError("file number less than one"); |
| 2790 | } |
| 2791 | |
| 2792 | if (getLexer().isNot(AsmToken::String)) |
| 2793 | return TokError("unexpected token in '.file' directive"); |
| 2794 | |
| 2795 | // Usually the directory and filename together, otherwise just the directory. |
Yunzhong Gao | 8c0f506 | 2013-09-05 19:14:26 +0000 | [diff] [blame] | 2796 | // Allow the strings to have escaped octal character sequence. |
| 2797 | std::string Path = getTok().getString(); |
| 2798 | if (parseEscapedString(Path)) |
| 2799 | return true; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2800 | Lex(); |
| 2801 | |
| 2802 | StringRef Directory; |
| 2803 | StringRef Filename; |
Yunzhong Gao | 8c0f506 | 2013-09-05 19:14:26 +0000 | [diff] [blame] | 2804 | std::string FilenameData; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2805 | if (getLexer().is(AsmToken::String)) { |
| 2806 | if (FileNumber == -1) |
| 2807 | return TokError("explicit path specified, but no file number"); |
Yunzhong Gao | 8c0f506 | 2013-09-05 19:14:26 +0000 | [diff] [blame] | 2808 | if (parseEscapedString(FilenameData)) |
| 2809 | return true; |
| 2810 | Filename = FilenameData; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2811 | Directory = Path; |
| 2812 | Lex(); |
| 2813 | } else { |
| 2814 | Filename = Path; |
| 2815 | } |
| 2816 | |
| 2817 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 2818 | return TokError("unexpected token in '.file' directive"); |
| 2819 | |
| 2820 | if (FileNumber == -1) |
| 2821 | getStreamer().EmitFileDirective(Filename); |
| 2822 | else { |
David Blaikie | dc3f01e | 2015-03-09 01:57:13 +0000 | [diff] [blame] | 2823 | if (getContext().getGenDwarfForAssembly()) |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2824 | Error(DirectiveLoc, |
| 2825 | "input can't have .file dwarf directives when -g is " |
| 2826 | "used to generate dwarf debug info for assembly code"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2827 | |
David Blaikie | c714ef4 | 2014-03-17 01:52:11 +0000 | [diff] [blame] | 2828 | if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename) == |
| 2829 | 0) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2830 | Error(FileNumberLoc, "file number already allocated"); |
| 2831 | } |
| 2832 | |
| 2833 | return false; |
| 2834 | } |
| 2835 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2836 | /// parseDirectiveLine |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2837 | /// ::= .line [number] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2838 | bool AsmParser::parseDirectiveLine() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2839 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2840 | if (getLexer().isNot(AsmToken::Integer)) |
| 2841 | return TokError("unexpected token in '.line' directive"); |
| 2842 | |
| 2843 | int64_t LineNumber = getTok().getIntVal(); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2844 | (void)LineNumber; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2845 | Lex(); |
| 2846 | |
| 2847 | // FIXME: Do something with the .line. |
| 2848 | } |
| 2849 | |
| 2850 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 2851 | return TokError("unexpected token in '.line' directive"); |
| 2852 | |
| 2853 | return false; |
| 2854 | } |
| 2855 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2856 | /// parseDirectiveLoc |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2857 | /// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end] |
| 2858 | /// [epilogue_begin] [is_stmt VALUE] [isa VALUE] |
| 2859 | /// The first number is a file number, must have been previously assigned with |
| 2860 | /// a .file directive, the second number is the line number and optionally the |
| 2861 | /// third number is a column position (zero if not specified). The remaining |
| 2862 | /// optional items are .loc sub-directives. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2863 | bool AsmParser::parseDirectiveLoc() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2864 | if (getLexer().isNot(AsmToken::Integer)) |
| 2865 | return TokError("unexpected token in '.loc' directive"); |
| 2866 | int64_t FileNumber = getTok().getIntVal(); |
| 2867 | if (FileNumber < 1) |
| 2868 | return TokError("file number less than one in '.loc' directive"); |
| 2869 | if (!getContext().isValidDwarfFileNumber(FileNumber)) |
| 2870 | return TokError("unassigned file number in '.loc' directive"); |
| 2871 | Lex(); |
| 2872 | |
| 2873 | int64_t LineNumber = 0; |
| 2874 | if (getLexer().is(AsmToken::Integer)) { |
| 2875 | LineNumber = getTok().getIntVal(); |
Adrian Prantl | 6ac4003 | 2013-09-26 23:37:11 +0000 | [diff] [blame] | 2876 | if (LineNumber < 0) |
| 2877 | return TokError("line number less than zero in '.loc' directive"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2878 | Lex(); |
| 2879 | } |
| 2880 | |
| 2881 | int64_t ColumnPos = 0; |
| 2882 | if (getLexer().is(AsmToken::Integer)) { |
| 2883 | ColumnPos = getTok().getIntVal(); |
| 2884 | if (ColumnPos < 0) |
| 2885 | return TokError("column position less than zero in '.loc' directive"); |
| 2886 | Lex(); |
| 2887 | } |
| 2888 | |
| 2889 | unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0; |
| 2890 | unsigned Isa = 0; |
| 2891 | int64_t Discriminator = 0; |
| 2892 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2893 | for (;;) { |
| 2894 | if (getLexer().is(AsmToken::EndOfStatement)) |
| 2895 | break; |
| 2896 | |
| 2897 | StringRef Name; |
| 2898 | SMLoc Loc = getTok().getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2899 | if (parseIdentifier(Name)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2900 | return TokError("unexpected token in '.loc' directive"); |
| 2901 | |
| 2902 | if (Name == "basic_block") |
| 2903 | Flags |= DWARF2_FLAG_BASIC_BLOCK; |
| 2904 | else if (Name == "prologue_end") |
| 2905 | Flags |= DWARF2_FLAG_PROLOGUE_END; |
| 2906 | else if (Name == "epilogue_begin") |
| 2907 | Flags |= DWARF2_FLAG_EPILOGUE_BEGIN; |
| 2908 | else if (Name == "is_stmt") { |
| 2909 | Loc = getTok().getLoc(); |
| 2910 | const MCExpr *Value; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2911 | if (parseExpression(Value)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2912 | return true; |
| 2913 | // The expression must be the constant 0 or 1. |
| 2914 | if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) { |
| 2915 | int Value = MCE->getValue(); |
| 2916 | if (Value == 0) |
| 2917 | Flags &= ~DWARF2_FLAG_IS_STMT; |
| 2918 | else if (Value == 1) |
| 2919 | Flags |= DWARF2_FLAG_IS_STMT; |
| 2920 | else |
| 2921 | return Error(Loc, "is_stmt value not 0 or 1"); |
Craig Topper | f15655b | 2013-04-22 04:22:40 +0000 | [diff] [blame] | 2922 | } else { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2923 | return Error(Loc, "is_stmt value not the constant value of 0 or 1"); |
| 2924 | } |
Craig Topper | f15655b | 2013-04-22 04:22:40 +0000 | [diff] [blame] | 2925 | } else if (Name == "isa") { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2926 | Loc = getTok().getLoc(); |
| 2927 | const MCExpr *Value; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2928 | if (parseExpression(Value)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2929 | return true; |
| 2930 | // The expression must be a constant greater or equal to 0. |
| 2931 | if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) { |
| 2932 | int Value = MCE->getValue(); |
| 2933 | if (Value < 0) |
| 2934 | return Error(Loc, "isa number less than zero"); |
| 2935 | Isa = Value; |
Craig Topper | f15655b | 2013-04-22 04:22:40 +0000 | [diff] [blame] | 2936 | } else { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2937 | return Error(Loc, "isa number not a constant value"); |
| 2938 | } |
Craig Topper | f15655b | 2013-04-22 04:22:40 +0000 | [diff] [blame] | 2939 | } else if (Name == "discriminator") { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2940 | if (parseAbsoluteExpression(Discriminator)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2941 | return true; |
Craig Topper | f15655b | 2013-04-22 04:22:40 +0000 | [diff] [blame] | 2942 | } else { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2943 | return Error(Loc, "unknown sub-directive in '.loc' directive"); |
| 2944 | } |
| 2945 | |
| 2946 | if (getLexer().is(AsmToken::EndOfStatement)) |
| 2947 | break; |
| 2948 | } |
| 2949 | } |
| 2950 | |
| 2951 | getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags, |
| 2952 | Isa, Discriminator, StringRef()); |
| 2953 | |
| 2954 | return false; |
| 2955 | } |
| 2956 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2957 | /// parseDirectiveStabs |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2958 | /// ::= .stabs string, number, number, number |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2959 | bool AsmParser::parseDirectiveStabs() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2960 | return TokError("unsupported directive '.stabs'"); |
| 2961 | } |
| 2962 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2963 | /// parseDirectiveCFISections |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2964 | /// ::= .cfi_sections section [, section] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2965 | bool AsmParser::parseDirectiveCFISections() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2966 | StringRef Name; |
| 2967 | bool EH = false; |
| 2968 | bool Debug = false; |
| 2969 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2970 | if (parseIdentifier(Name)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2971 | return TokError("Expected an identifier"); |
| 2972 | |
| 2973 | if (Name == ".eh_frame") |
| 2974 | EH = true; |
| 2975 | else if (Name == ".debug_frame") |
| 2976 | Debug = true; |
| 2977 | |
| 2978 | if (getLexer().is(AsmToken::Comma)) { |
| 2979 | Lex(); |
| 2980 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2981 | if (parseIdentifier(Name)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2982 | return TokError("Expected an identifier"); |
| 2983 | |
| 2984 | if (Name == ".eh_frame") |
| 2985 | EH = true; |
| 2986 | else if (Name == ".debug_frame") |
| 2987 | Debug = true; |
| 2988 | } |
| 2989 | |
| 2990 | getStreamer().EmitCFISections(EH, Debug); |
| 2991 | return false; |
| 2992 | } |
| 2993 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2994 | /// parseDirectiveCFIStartProc |
David Majnemer | e035cf9 | 2014-01-27 17:20:25 +0000 | [diff] [blame] | 2995 | /// ::= .cfi_startproc [simple] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2996 | bool AsmParser::parseDirectiveCFIStartProc() { |
David Majnemer | e035cf9 | 2014-01-27 17:20:25 +0000 | [diff] [blame] | 2997 | StringRef Simple; |
| 2998 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 2999 | if (parseIdentifier(Simple) || Simple != "simple") |
| 3000 | return TokError("unexpected token in .cfi_startproc directive"); |
| 3001 | |
Oliver Stannard | cf6bfb1 | 2014-11-03 12:19:03 +0000 | [diff] [blame] | 3002 | getStreamer().EmitCFIStartProc(!Simple.empty()); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3003 | return false; |
| 3004 | } |
| 3005 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3006 | /// parseDirectiveCFIEndProc |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3007 | /// ::= .cfi_endproc |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3008 | bool AsmParser::parseDirectiveCFIEndProc() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3009 | getStreamer().EmitCFIEndProc(); |
| 3010 | return false; |
| 3011 | } |
| 3012 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3013 | /// \brief parse register name or number. |
| 3014 | bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register, |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3015 | SMLoc DirectiveLoc) { |
| 3016 | unsigned RegNo; |
| 3017 | |
| 3018 | if (getLexer().isNot(AsmToken::Integer)) { |
| 3019 | if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc)) |
| 3020 | return true; |
Bill Wendling | bc07a89 | 2013-06-18 07:20:20 +0000 | [diff] [blame] | 3021 | Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3022 | } else |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3023 | return parseAbsoluteExpression(Register); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3024 | |
| 3025 | return false; |
| 3026 | } |
| 3027 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3028 | /// parseDirectiveCFIDefCfa |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3029 | /// ::= .cfi_def_cfa register, offset |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3030 | bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3031 | int64_t Register = 0; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3032 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3033 | return true; |
| 3034 | |
| 3035 | if (getLexer().isNot(AsmToken::Comma)) |
| 3036 | return TokError("unexpected token in directive"); |
| 3037 | Lex(); |
| 3038 | |
| 3039 | int64_t Offset = 0; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3040 | if (parseAbsoluteExpression(Offset)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3041 | return true; |
| 3042 | |
| 3043 | getStreamer().EmitCFIDefCfa(Register, Offset); |
| 3044 | return false; |
| 3045 | } |
| 3046 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3047 | /// parseDirectiveCFIDefCfaOffset |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3048 | /// ::= .cfi_def_cfa_offset offset |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3049 | bool AsmParser::parseDirectiveCFIDefCfaOffset() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3050 | int64_t Offset = 0; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3051 | if (parseAbsoluteExpression(Offset)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3052 | return true; |
| 3053 | |
| 3054 | getStreamer().EmitCFIDefCfaOffset(Offset); |
| 3055 | return false; |
| 3056 | } |
| 3057 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3058 | /// parseDirectiveCFIRegister |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3059 | /// ::= .cfi_register register, register |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3060 | bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3061 | int64_t Register1 = 0; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3062 | if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3063 | return true; |
| 3064 | |
| 3065 | if (getLexer().isNot(AsmToken::Comma)) |
| 3066 | return TokError("unexpected token in directive"); |
| 3067 | Lex(); |
| 3068 | |
| 3069 | int64_t Register2 = 0; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3070 | if (parseRegisterOrRegisterNumber(Register2, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3071 | return true; |
| 3072 | |
| 3073 | getStreamer().EmitCFIRegister(Register1, Register2); |
| 3074 | return false; |
| 3075 | } |
| 3076 | |
Venkatraman Govindaraju | 3816d43 | 2013-09-26 14:49:40 +0000 | [diff] [blame] | 3077 | /// parseDirectiveCFIWindowSave |
| 3078 | /// ::= .cfi_window_save |
| 3079 | bool AsmParser::parseDirectiveCFIWindowSave() { |
| 3080 | getStreamer().EmitCFIWindowSave(); |
| 3081 | return false; |
| 3082 | } |
| 3083 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3084 | /// parseDirectiveCFIAdjustCfaOffset |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3085 | /// ::= .cfi_adjust_cfa_offset adjustment |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3086 | bool AsmParser::parseDirectiveCFIAdjustCfaOffset() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3087 | int64_t Adjustment = 0; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3088 | if (parseAbsoluteExpression(Adjustment)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3089 | return true; |
| 3090 | |
| 3091 | getStreamer().EmitCFIAdjustCfaOffset(Adjustment); |
| 3092 | return false; |
| 3093 | } |
| 3094 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3095 | /// parseDirectiveCFIDefCfaRegister |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3096 | /// ::= .cfi_def_cfa_register register |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3097 | bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3098 | int64_t Register = 0; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3099 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3100 | return true; |
| 3101 | |
| 3102 | getStreamer().EmitCFIDefCfaRegister(Register); |
| 3103 | return false; |
| 3104 | } |
| 3105 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3106 | /// parseDirectiveCFIOffset |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3107 | /// ::= .cfi_offset register, offset |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3108 | bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3109 | int64_t Register = 0; |
| 3110 | int64_t Offset = 0; |
| 3111 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3112 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3113 | return true; |
| 3114 | |
| 3115 | if (getLexer().isNot(AsmToken::Comma)) |
| 3116 | return TokError("unexpected token in directive"); |
| 3117 | Lex(); |
| 3118 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3119 | if (parseAbsoluteExpression(Offset)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3120 | return true; |
| 3121 | |
| 3122 | getStreamer().EmitCFIOffset(Register, Offset); |
| 3123 | return false; |
| 3124 | } |
| 3125 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3126 | /// parseDirectiveCFIRelOffset |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3127 | /// ::= .cfi_rel_offset register, offset |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3128 | bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3129 | int64_t Register = 0; |
| 3130 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3131 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3132 | return true; |
| 3133 | |
| 3134 | if (getLexer().isNot(AsmToken::Comma)) |
| 3135 | return TokError("unexpected token in directive"); |
| 3136 | Lex(); |
| 3137 | |
| 3138 | int64_t Offset = 0; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3139 | if (parseAbsoluteExpression(Offset)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3140 | return true; |
| 3141 | |
| 3142 | getStreamer().EmitCFIRelOffset(Register, Offset); |
| 3143 | return false; |
| 3144 | } |
| 3145 | |
| 3146 | static bool isValidEncoding(int64_t Encoding) { |
| 3147 | if (Encoding & ~0xff) |
| 3148 | return false; |
| 3149 | |
| 3150 | if (Encoding == dwarf::DW_EH_PE_omit) |
| 3151 | return true; |
| 3152 | |
| 3153 | const unsigned Format = Encoding & 0xf; |
| 3154 | if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 && |
| 3155 | Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 && |
| 3156 | Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 && |
| 3157 | Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed) |
| 3158 | return false; |
| 3159 | |
| 3160 | const unsigned Application = Encoding & 0x70; |
| 3161 | if (Application != dwarf::DW_EH_PE_absptr && |
| 3162 | Application != dwarf::DW_EH_PE_pcrel) |
| 3163 | return false; |
| 3164 | |
| 3165 | return true; |
| 3166 | } |
| 3167 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3168 | /// parseDirectiveCFIPersonalityOrLsda |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3169 | /// IsPersonality true for cfi_personality, false for cfi_lsda |
| 3170 | /// ::= .cfi_personality encoding, [symbol_name] |
| 3171 | /// ::= .cfi_lsda encoding, [symbol_name] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3172 | bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3173 | int64_t Encoding = 0; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3174 | if (parseAbsoluteExpression(Encoding)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3175 | return true; |
| 3176 | if (Encoding == dwarf::DW_EH_PE_omit) |
| 3177 | return false; |
| 3178 | |
| 3179 | if (!isValidEncoding(Encoding)) |
| 3180 | return TokError("unsupported encoding."); |
| 3181 | |
| 3182 | if (getLexer().isNot(AsmToken::Comma)) |
| 3183 | return TokError("unexpected token in directive"); |
| 3184 | Lex(); |
| 3185 | |
| 3186 | StringRef Name; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3187 | if (parseIdentifier(Name)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3188 | return TokError("expected identifier in directive"); |
| 3189 | |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 3190 | MCSymbol *Sym = getContext().getOrCreateSymbol(Name); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3191 | |
| 3192 | if (IsPersonality) |
| 3193 | getStreamer().EmitCFIPersonality(Sym, Encoding); |
| 3194 | else |
| 3195 | getStreamer().EmitCFILsda(Sym, Encoding); |
| 3196 | return false; |
| 3197 | } |
| 3198 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3199 | /// parseDirectiveCFIRememberState |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3200 | /// ::= .cfi_remember_state |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3201 | bool AsmParser::parseDirectiveCFIRememberState() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3202 | getStreamer().EmitCFIRememberState(); |
| 3203 | return false; |
| 3204 | } |
| 3205 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3206 | /// parseDirectiveCFIRestoreState |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3207 | /// ::= .cfi_remember_state |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3208 | bool AsmParser::parseDirectiveCFIRestoreState() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3209 | getStreamer().EmitCFIRestoreState(); |
| 3210 | return false; |
| 3211 | } |
| 3212 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3213 | /// parseDirectiveCFISameValue |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3214 | /// ::= .cfi_same_value register |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3215 | bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3216 | int64_t Register = 0; |
| 3217 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3218 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3219 | return true; |
| 3220 | |
| 3221 | getStreamer().EmitCFISameValue(Register); |
| 3222 | return false; |
| 3223 | } |
| 3224 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3225 | /// parseDirectiveCFIRestore |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3226 | /// ::= .cfi_restore register |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3227 | bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3228 | int64_t Register = 0; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3229 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3230 | return true; |
| 3231 | |
| 3232 | getStreamer().EmitCFIRestore(Register); |
| 3233 | return false; |
| 3234 | } |
| 3235 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3236 | /// parseDirectiveCFIEscape |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3237 | /// ::= .cfi_escape expression[,...] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3238 | bool AsmParser::parseDirectiveCFIEscape() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3239 | std::string Values; |
| 3240 | int64_t CurrValue; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3241 | if (parseAbsoluteExpression(CurrValue)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3242 | return true; |
| 3243 | |
| 3244 | Values.push_back((uint8_t)CurrValue); |
| 3245 | |
| 3246 | while (getLexer().is(AsmToken::Comma)) { |
| 3247 | Lex(); |
| 3248 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3249 | if (parseAbsoluteExpression(CurrValue)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3250 | return true; |
| 3251 | |
| 3252 | Values.push_back((uint8_t)CurrValue); |
| 3253 | } |
| 3254 | |
| 3255 | getStreamer().EmitCFIEscape(Values); |
| 3256 | return false; |
| 3257 | } |
| 3258 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3259 | /// parseDirectiveCFISignalFrame |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3260 | /// ::= .cfi_signal_frame |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3261 | bool AsmParser::parseDirectiveCFISignalFrame() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3262 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3263 | return Error(getLexer().getLoc(), |
| 3264 | "unexpected token in '.cfi_signal_frame'"); |
| 3265 | |
| 3266 | getStreamer().EmitCFISignalFrame(); |
| 3267 | return false; |
| 3268 | } |
| 3269 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3270 | /// parseDirectiveCFIUndefined |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3271 | /// ::= .cfi_undefined register |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3272 | bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3273 | int64_t Register = 0; |
| 3274 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3275 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3276 | return true; |
| 3277 | |
| 3278 | getStreamer().EmitCFIUndefined(Register); |
| 3279 | return false; |
| 3280 | } |
| 3281 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3282 | /// parseDirectiveMacrosOnOff |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3283 | /// ::= .macros_on |
| 3284 | /// ::= .macros_off |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3285 | bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3286 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3287 | return Error(getLexer().getLoc(), |
| 3288 | "unexpected token in '" + Directive + "' directive"); |
| 3289 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3290 | setMacrosEnabled(Directive == ".macros_on"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3291 | return false; |
| 3292 | } |
| 3293 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3294 | /// parseDirectiveMacro |
Saleem Abdulrasool | 27304cb | 2014-02-16 04:56:31 +0000 | [diff] [blame] | 3295 | /// ::= .macro name[,] [parameters] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3296 | bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3297 | StringRef Name; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3298 | if (parseIdentifier(Name)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3299 | return TokError("expected identifier in '.macro' directive"); |
| 3300 | |
Saleem Abdulrasool | 27304cb | 2014-02-16 04:56:31 +0000 | [diff] [blame] | 3301 | if (getLexer().is(AsmToken::Comma)) |
| 3302 | Lex(); |
| 3303 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3304 | MCAsmMacroParameters Parameters; |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 3305 | while (getLexer().isNot(AsmToken::EndOfStatement)) { |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 3306 | |
Alexander Kornienko | 8c0809c | 2015-01-15 11:41:30 +0000 | [diff] [blame] | 3307 | if (!Parameters.empty() && Parameters.back().Vararg) |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 3308 | return Error(Lexer.getLoc(), |
| 3309 | "Vararg parameter '" + Parameters.back().Name + |
| 3310 | "' should be last one in the list of parameters."); |
| 3311 | |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 3312 | MCAsmMacroParameter Parameter; |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 3313 | if (parseIdentifier(Parameter.Name)) |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 3314 | return TokError("expected identifier in '.macro' directive"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3315 | |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 3316 | if (Lexer.is(AsmToken::Colon)) { |
| 3317 | Lex(); // consume ':' |
| 3318 | |
| 3319 | SMLoc QualLoc; |
| 3320 | StringRef Qualifier; |
| 3321 | |
| 3322 | QualLoc = Lexer.getLoc(); |
| 3323 | if (parseIdentifier(Qualifier)) |
| 3324 | return Error(QualLoc, "missing parameter qualifier for " |
| 3325 | "'" + Parameter.Name + "' in macro '" + Name + "'"); |
| 3326 | |
| 3327 | if (Qualifier == "req") |
| 3328 | Parameter.Required = true; |
Kevin Enderby | e3c1346 | 2014-08-04 23:14:37 +0000 | [diff] [blame] | 3329 | else if (Qualifier == "vararg") |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 3330 | Parameter.Vararg = true; |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 3331 | else |
| 3332 | return Error(QualLoc, Qualifier + " is not a valid parameter qualifier " |
| 3333 | "for '" + Parameter.Name + "' in macro '" + Name + "'"); |
| 3334 | } |
| 3335 | |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 3336 | if (getLexer().is(AsmToken::Equal)) { |
| 3337 | Lex(); |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 3338 | |
| 3339 | SMLoc ParamLoc; |
| 3340 | |
| 3341 | ParamLoc = Lexer.getLoc(); |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 3342 | if (parseMacroArgument(Parameter.Value, /*Vararg=*/false )) |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 3343 | return true; |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 3344 | |
| 3345 | if (Parameter.Required) |
| 3346 | Warning(ParamLoc, "pointless default value for required parameter " |
| 3347 | "'" + Parameter.Name + "' in macro '" + Name + "'"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3348 | } |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 3349 | |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 3350 | Parameters.push_back(std::move(Parameter)); |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 3351 | |
| 3352 | if (getLexer().is(AsmToken::Comma)) |
| 3353 | Lex(); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3354 | } |
| 3355 | |
| 3356 | // Eat the end of statement. |
| 3357 | Lex(); |
| 3358 | |
| 3359 | AsmToken EndToken, StartToken = getTok(); |
Benjamin Kramer | 9d94a4e | 2014-02-09 16:22:00 +0000 | [diff] [blame] | 3360 | unsigned MacroDepth = 0; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3361 | |
| 3362 | // Lex the macro definition. |
| 3363 | for (;;) { |
| 3364 | // Check whether we have reached the end of the file. |
| 3365 | if (getLexer().is(AsmToken::Eof)) |
| 3366 | return Error(DirectiveLoc, "no matching '.endmacro' in definition"); |
| 3367 | |
| 3368 | // Otherwise, check whether we have reach the .endmacro. |
Benjamin Kramer | 9d94a4e | 2014-02-09 16:22:00 +0000 | [diff] [blame] | 3369 | if (getLexer().is(AsmToken::Identifier)) { |
| 3370 | if (getTok().getIdentifier() == ".endm" || |
| 3371 | getTok().getIdentifier() == ".endmacro") { |
| 3372 | if (MacroDepth == 0) { // Outermost macro. |
| 3373 | EndToken = getTok(); |
| 3374 | Lex(); |
| 3375 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3376 | return TokError("unexpected token in '" + EndToken.getIdentifier() + |
| 3377 | "' directive"); |
| 3378 | break; |
| 3379 | } else { |
| 3380 | // Otherwise we just found the end of an inner macro. |
| 3381 | --MacroDepth; |
| 3382 | } |
| 3383 | } else if (getTok().getIdentifier() == ".macro") { |
| 3384 | // We allow nested macros. Those aren't instantiated until the outermost |
| 3385 | // macro is expanded so just ignore them for now. |
| 3386 | ++MacroDepth; |
| 3387 | } |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3388 | } |
| 3389 | |
| 3390 | // Otherwise, scan til the end of the statement. |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3391 | eatToEndOfStatement(); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3392 | } |
| 3393 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3394 | if (lookupMacro(Name)) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3395 | return Error(DirectiveLoc, "macro '" + Name + "' is already defined"); |
| 3396 | } |
| 3397 | |
| 3398 | const char *BodyStart = StartToken.getLoc().getPointer(); |
| 3399 | const char *BodyEnd = EndToken.getLoc().getPointer(); |
| 3400 | StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3401 | checkForBadMacro(DirectiveLoc, Name, Body, Parameters); |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 3402 | defineMacro(Name, MCAsmMacro(Name, Body, std::move(Parameters))); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3403 | return false; |
| 3404 | } |
| 3405 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3406 | /// checkForBadMacro |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3407 | /// |
| 3408 | /// With the support added for named parameters there may be code out there that |
| 3409 | /// is transitioning from positional parameters. In versions of gas that did |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 3410 | /// not support named parameters they would be ignored on the macro definition. |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3411 | /// 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] | 3412 | /// 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] | 3413 | /// to be positional parameters, strings like $1, $2, ... and $n, then issue a |
| 3414 | /// warning that the positional parameter found in body which have no effect. |
| 3415 | /// Hoping the developer will either remove the named parameters from the macro |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 3416 | /// definition so the positional parameters get used if that was what was |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3417 | /// intended or change the macro to use the named parameters. It is possible |
| 3418 | /// this warning will trigger when the none of the named parameters are used |
| 3419 | /// 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] | 3420 | void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3421 | StringRef Body, |
Benjamin Kramer | d31aaf1 | 2014-02-09 17:13:11 +0000 | [diff] [blame] | 3422 | ArrayRef<MCAsmMacroParameter> Parameters) { |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3423 | // If this macro is not defined with named parameters the warning we are |
| 3424 | // checking for here doesn't apply. |
| 3425 | unsigned NParameters = Parameters.size(); |
| 3426 | if (NParameters == 0) |
| 3427 | return; |
| 3428 | |
| 3429 | bool NamedParametersFound = false; |
| 3430 | bool PositionalParametersFound = false; |
| 3431 | |
| 3432 | // Look at the body of the macro for use of both the named parameters and what |
| 3433 | // are likely to be positional parameters. This is what expandMacro() is |
| 3434 | // doing when it finds the parameters in the body. |
| 3435 | while (!Body.empty()) { |
| 3436 | // Scan for the next possible parameter. |
| 3437 | std::size_t End = Body.size(), Pos = 0; |
| 3438 | for (; Pos != End; ++Pos) { |
| 3439 | // Check for a substitution or escape. |
| 3440 | // This macro is defined with parameters, look for \foo, \bar, etc. |
| 3441 | if (Body[Pos] == '\\' && Pos + 1 != End) |
| 3442 | break; |
| 3443 | |
| 3444 | // This macro should have parameters, but look for $0, $1, ..., $n too. |
| 3445 | if (Body[Pos] != '$' || Pos + 1 == End) |
| 3446 | continue; |
| 3447 | char Next = Body[Pos + 1]; |
Guy Benyei | 83c74e9 | 2013-02-12 21:21:59 +0000 | [diff] [blame] | 3448 | if (Next == '$' || Next == 'n' || |
| 3449 | isdigit(static_cast<unsigned char>(Next))) |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3450 | break; |
| 3451 | } |
| 3452 | |
| 3453 | // Check if we reached the end. |
| 3454 | if (Pos == End) |
| 3455 | break; |
| 3456 | |
| 3457 | if (Body[Pos] == '$') { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3458 | switch (Body[Pos + 1]) { |
| 3459 | // $$ => $ |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3460 | case '$': |
| 3461 | break; |
| 3462 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3463 | // $n => number of arguments |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3464 | case 'n': |
| 3465 | PositionalParametersFound = true; |
| 3466 | break; |
| 3467 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3468 | // $[0-9] => argument |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3469 | default: { |
| 3470 | PositionalParametersFound = true; |
| 3471 | break; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3472 | } |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3473 | } |
| 3474 | Pos += 2; |
| 3475 | } else { |
| 3476 | unsigned I = Pos + 1; |
| 3477 | while (isIdentifierChar(Body[I]) && I + 1 != End) |
| 3478 | ++I; |
| 3479 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3480 | const char *Begin = Body.data() + Pos + 1; |
| 3481 | StringRef Argument(Begin, I - (Pos + 1)); |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3482 | unsigned Index = 0; |
| 3483 | for (; Index < NParameters; ++Index) |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 3484 | if (Parameters[Index].Name == Argument) |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3485 | break; |
| 3486 | |
| 3487 | if (Index == NParameters) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3488 | if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')') |
| 3489 | Pos += 3; |
| 3490 | else { |
| 3491 | Pos = I; |
| 3492 | } |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3493 | } else { |
| 3494 | NamedParametersFound = true; |
| 3495 | Pos += 1 + Argument.size(); |
| 3496 | } |
| 3497 | } |
| 3498 | // Update the scan point. |
| 3499 | Body = Body.substr(Pos); |
| 3500 | } |
| 3501 | |
| 3502 | if (!NamedParametersFound && PositionalParametersFound) |
| 3503 | Warning(DirectiveLoc, "macro defined with named parameters which are not " |
| 3504 | "used in macro body, possible positional parameter " |
| 3505 | "found in body which will have no effect"); |
| 3506 | } |
| 3507 | |
Nico Weber | 155dccd1 | 2014-07-24 17:08:39 +0000 | [diff] [blame] | 3508 | /// parseDirectiveExitMacro |
| 3509 | /// ::= .exitm |
| 3510 | bool AsmParser::parseDirectiveExitMacro(StringRef Directive) { |
| 3511 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3512 | return TokError("unexpected token in '" + Directive + "' directive"); |
| 3513 | |
| 3514 | if (!isInsideMacroInstantiation()) |
| 3515 | return TokError("unexpected '" + Directive + "' in file, " |
| 3516 | "no current macro definition"); |
| 3517 | |
| 3518 | // Exit all conditionals that are active in the current macro. |
| 3519 | while (TheCondStack.size() != ActiveMacros.back()->CondStackDepth) { |
| 3520 | TheCondState = TheCondStack.back(); |
| 3521 | TheCondStack.pop_back(); |
| 3522 | } |
| 3523 | |
| 3524 | handleMacroExit(); |
| 3525 | return false; |
| 3526 | } |
| 3527 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3528 | /// parseDirectiveEndMacro |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3529 | /// ::= .endm |
| 3530 | /// ::= .endmacro |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3531 | bool AsmParser::parseDirectiveEndMacro(StringRef Directive) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3532 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3533 | return TokError("unexpected token in '" + Directive + "' directive"); |
| 3534 | |
| 3535 | // If we are inside a macro instantiation, terminate the current |
| 3536 | // instantiation. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3537 | if (isInsideMacroInstantiation()) { |
| 3538 | handleMacroExit(); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3539 | return false; |
| 3540 | } |
| 3541 | |
| 3542 | // Otherwise, this .endmacro is a stray entry in the file; well formed |
| 3543 | // .endmacro directives are handled during the macro definition parsing. |
| 3544 | return TokError("unexpected '" + Directive + "' in file, " |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3545 | "no current macro definition"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3546 | } |
| 3547 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3548 | /// parseDirectivePurgeMacro |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3549 | /// ::= .purgem |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3550 | bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3551 | StringRef Name; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3552 | if (parseIdentifier(Name)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3553 | return TokError("expected identifier in '.purgem' directive"); |
| 3554 | |
| 3555 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3556 | return TokError("unexpected token in '.purgem' directive"); |
| 3557 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3558 | if (!lookupMacro(Name)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3559 | return Error(DirectiveLoc, "macro '" + Name + "' is not defined"); |
| 3560 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3561 | undefineMacro(Name); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3562 | return false; |
| 3563 | } |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 3564 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3565 | /// parseDirectiveBundleAlignMode |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 3566 | /// ::= {.bundle_align_mode} expression |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3567 | bool AsmParser::parseDirectiveBundleAlignMode() { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3568 | checkForValidSection(); |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 3569 | |
| 3570 | // Expect a single argument: an expression that evaluates to a constant |
| 3571 | // in the inclusive range 0-30. |
| 3572 | SMLoc ExprLoc = getLexer().getLoc(); |
| 3573 | int64_t AlignSizePow2; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3574 | if (parseAbsoluteExpression(AlignSizePow2)) |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 3575 | return true; |
| 3576 | else if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3577 | return TokError("unexpected token after expression in" |
| 3578 | " '.bundle_align_mode' directive"); |
| 3579 | else if (AlignSizePow2 < 0 || AlignSizePow2 > 30) |
| 3580 | return Error(ExprLoc, |
| 3581 | "invalid bundle alignment size (expected between 0 and 30)"); |
| 3582 | |
| 3583 | Lex(); |
| 3584 | |
| 3585 | // Because of AlignSizePow2's verified range we can safely truncate it to |
| 3586 | // unsigned. |
| 3587 | getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2)); |
| 3588 | return false; |
| 3589 | } |
| 3590 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3591 | /// parseDirectiveBundleLock |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 3592 | /// ::= {.bundle_lock} [align_to_end] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3593 | bool AsmParser::parseDirectiveBundleLock() { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3594 | checkForValidSection(); |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 3595 | bool AlignToEnd = false; |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 3596 | |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 3597 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 3598 | StringRef Option; |
| 3599 | SMLoc Loc = getTok().getLoc(); |
| 3600 | const char *kInvalidOptionError = |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3601 | "invalid option for '.bundle_lock' directive"; |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 3602 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3603 | if (parseIdentifier(Option)) |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 3604 | return Error(Loc, kInvalidOptionError); |
| 3605 | |
| 3606 | if (Option != "align_to_end") |
| 3607 | return Error(Loc, kInvalidOptionError); |
| 3608 | else if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3609 | return Error(Loc, |
| 3610 | "unexpected token after '.bundle_lock' directive option"); |
| 3611 | AlignToEnd = true; |
| 3612 | } |
| 3613 | |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 3614 | Lex(); |
| 3615 | |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 3616 | getStreamer().EmitBundleLock(AlignToEnd); |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 3617 | return false; |
| 3618 | } |
| 3619 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3620 | /// parseDirectiveBundleLock |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 3621 | /// ::= {.bundle_lock} |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3622 | bool AsmParser::parseDirectiveBundleUnlock() { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3623 | checkForValidSection(); |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 3624 | |
| 3625 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3626 | return TokError("unexpected token in '.bundle_unlock' directive"); |
| 3627 | Lex(); |
| 3628 | |
| 3629 | getStreamer().EmitBundleUnlock(); |
| 3630 | return false; |
| 3631 | } |
| 3632 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3633 | /// parseDirectiveSpace |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3634 | /// ::= (.skip | .space) expression [ , expression ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3635 | bool AsmParser::parseDirectiveSpace(StringRef IDVal) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3636 | checkForValidSection(); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3637 | |
| 3638 | int64_t NumBytes; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3639 | if (parseAbsoluteExpression(NumBytes)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3640 | return true; |
| 3641 | |
| 3642 | int64_t FillExpr = 0; |
| 3643 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 3644 | if (getLexer().isNot(AsmToken::Comma)) |
| 3645 | return TokError("unexpected token in '" + Twine(IDVal) + "' directive"); |
| 3646 | Lex(); |
| 3647 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3648 | if (parseAbsoluteExpression(FillExpr)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3649 | return true; |
| 3650 | |
| 3651 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3652 | return TokError("unexpected token in '" + Twine(IDVal) + "' directive"); |
| 3653 | } |
| 3654 | |
| 3655 | Lex(); |
| 3656 | |
| 3657 | if (NumBytes <= 0) |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3658 | return TokError("invalid number of bytes in '" + Twine(IDVal) + |
| 3659 | "' directive"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3660 | |
| 3661 | // 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] | 3662 | getStreamer().EmitFill(NumBytes, FillExpr); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3663 | |
| 3664 | return false; |
| 3665 | } |
| 3666 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3667 | /// parseDirectiveLEB128 |
Benjamin Kramer | 68ca67b | 2015-02-19 20:24:04 +0000 | [diff] [blame] | 3668 | /// ::= (.sleb128 | .uleb128) [ expression (, expression)* ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3669 | bool AsmParser::parseDirectiveLEB128(bool Signed) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3670 | checkForValidSection(); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3671 | const MCExpr *Value; |
| 3672 | |
Benjamin Kramer | 68ca67b | 2015-02-19 20:24:04 +0000 | [diff] [blame] | 3673 | for (;;) { |
| 3674 | if (parseExpression(Value)) |
| 3675 | return true; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3676 | |
Benjamin Kramer | 68ca67b | 2015-02-19 20:24:04 +0000 | [diff] [blame] | 3677 | if (Signed) |
| 3678 | getStreamer().EmitSLEB128Value(Value); |
| 3679 | else |
| 3680 | getStreamer().EmitULEB128Value(Value); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3681 | |
Benjamin Kramer | 68ca67b | 2015-02-19 20:24:04 +0000 | [diff] [blame] | 3682 | if (getLexer().is(AsmToken::EndOfStatement)) |
| 3683 | break; |
| 3684 | |
| 3685 | if (getLexer().isNot(AsmToken::Comma)) |
| 3686 | return TokError("unexpected token in directive"); |
| 3687 | Lex(); |
| 3688 | } |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3689 | |
| 3690 | return false; |
| 3691 | } |
| 3692 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3693 | /// parseDirectiveSymbolAttribute |
Daniel Dunbar | a5508c8 | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 3694 | /// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3695 | bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) { |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3696 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
Daniel Dunbar | a5508c8 | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 3697 | for (;;) { |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 3698 | StringRef Name; |
Jim Grosbach | ebdf32f | 2011-09-15 17:56:49 +0000 | [diff] [blame] | 3699 | SMLoc Loc = getTok().getLoc(); |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 3700 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3701 | if (parseIdentifier(Name)) |
Jim Grosbach | ebdf32f | 2011-09-15 17:56:49 +0000 | [diff] [blame] | 3702 | return Error(Loc, "expected identifier in directive"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3703 | |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 3704 | MCSymbol *Sym = getContext().getOrCreateSymbol(Name); |
Daniel Dunbar | a5508c8 | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 3705 | |
Jim Grosbach | ebdf32f | 2011-09-15 17:56:49 +0000 | [diff] [blame] | 3706 | // Assembler local symbols don't make any sense here. Complain loudly. |
| 3707 | if (Sym->isTemporary()) |
| 3708 | return Error(Loc, "non-local symbol required in directive"); |
| 3709 | |
Saleem Abdulrasool | 4208b61 | 2013-08-09 01:52:03 +0000 | [diff] [blame] | 3710 | if (!getStreamer().EmitSymbolAttribute(Sym, Attr)) |
| 3711 | return Error(Loc, "unable to emit symbol attribute"); |
Daniel Dunbar | a5508c8 | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 3712 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3713 | if (getLexer().is(AsmToken::EndOfStatement)) |
Daniel Dunbar | a5508c8 | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 3714 | break; |
| 3715 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3716 | if (getLexer().isNot(AsmToken::Comma)) |
Daniel Dunbar | a5508c8 | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 3717 | return TokError("unexpected token in directive"); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 3718 | Lex(); |
Daniel Dunbar | a5508c8 | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 3719 | } |
| 3720 | } |
| 3721 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 3722 | Lex(); |
Jan Wen Voung | c768287 | 2010-09-30 01:09:20 +0000 | [diff] [blame] | 3723 | return false; |
Daniel Dunbar | a5508c8 | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 3724 | } |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3725 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3726 | /// parseDirectiveComm |
Chris Lattner | 28ad754 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 3727 | /// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3728 | bool AsmParser::parseDirectiveComm(bool IsLocal) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3729 | checkForValidSection(); |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 3730 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3731 | SMLoc IDLoc = getLexer().getLoc(); |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 3732 | StringRef Name; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3733 | if (parseIdentifier(Name)) |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3734 | return TokError("expected identifier in directive"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3735 | |
Daniel Dunbar | 9ee33ca | 2009-07-31 21:55:09 +0000 | [diff] [blame] | 3736 | // Handle the identifier as the key symbol. |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 3737 | MCSymbol *Sym = getContext().getOrCreateSymbol(Name); |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3738 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3739 | if (getLexer().isNot(AsmToken::Comma)) |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3740 | return TokError("unexpected token in directive"); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 3741 | Lex(); |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3742 | |
| 3743 | int64_t Size; |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3744 | SMLoc SizeLoc = getLexer().getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3745 | if (parseAbsoluteExpression(Size)) |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3746 | return true; |
| 3747 | |
| 3748 | int64_t Pow2Alignment = 0; |
| 3749 | SMLoc Pow2AlignmentLoc; |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3750 | if (getLexer().is(AsmToken::Comma)) { |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 3751 | Lex(); |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3752 | Pow2AlignmentLoc = getLexer().getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3753 | if (parseAbsoluteExpression(Pow2Alignment)) |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3754 | return true; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3755 | |
Benjamin Kramer | 68b9f05 | 2012-09-07 21:08:01 +0000 | [diff] [blame] | 3756 | LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType(); |
| 3757 | if (IsLocal && LCOMM == LCOMM::NoAlignment) |
Benjamin Kramer | 47f9ec9 | 2012-09-07 17:25:13 +0000 | [diff] [blame] | 3758 | return Error(Pow2AlignmentLoc, "alignment not supported on this target"); |
| 3759 | |
Chris Lattner | ab9cd3e | 2010-01-19 06:22:22 +0000 | [diff] [blame] | 3760 | // If this target takes alignments in bytes (not log) validate and convert. |
Benjamin Kramer | 68b9f05 | 2012-09-07 21:08:01 +0000 | [diff] [blame] | 3761 | if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) || |
| 3762 | (IsLocal && LCOMM == LCOMM::ByteAlignment)) { |
Chris Lattner | ab9cd3e | 2010-01-19 06:22:22 +0000 | [diff] [blame] | 3763 | if (!isPowerOf2_64(Pow2Alignment)) |
| 3764 | return Error(Pow2AlignmentLoc, "alignment must be a power of 2"); |
| 3765 | Pow2Alignment = Log2_64(Pow2Alignment); |
| 3766 | } |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3767 | } |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3768 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3769 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Chris Lattner | 28ad754 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 3770 | return TokError("unexpected token in '.comm' or '.lcomm' directive"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3771 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 3772 | Lex(); |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3773 | |
Chris Lattner | 28ad754 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 3774 | // NOTE: a size of zero for a .comm should create a undefined symbol |
| 3775 | // but a size of .lcomm creates a bss symbol of size zero. |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3776 | if (Size < 0) |
Chris Lattner | 28ad754 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 3777 | return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't " |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3778 | "be less than zero"); |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3779 | |
Eric Christopher | bc81885 | 2010-05-14 01:38:54 +0000 | [diff] [blame] | 3780 | // 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] | 3781 | // may internally end up wanting an alignment in bytes. |
| 3782 | // FIXME: Diagnose overflow. |
| 3783 | if (Pow2Alignment < 0) |
Chris Lattner | 28ad754 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 3784 | return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive " |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3785 | "alignment, can't be less than zero"); |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3786 | |
Daniel Dunbar | 6860ac7 | 2009-08-22 07:22:36 +0000 | [diff] [blame] | 3787 | if (!Sym->isUndefined()) |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3788 | return Error(IDLoc, "invalid symbol redefinition"); |
| 3789 | |
Chris Lattner | 28ad754 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 3790 | // 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] | 3791 | if (IsLocal) { |
Benjamin Kramer | 47f9ec9 | 2012-09-07 17:25:13 +0000 | [diff] [blame] | 3792 | getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment); |
Daniel Dunbar | 6a715dc | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 3793 | return false; |
| 3794 | } |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3795 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3796 | getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment); |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3797 | return false; |
| 3798 | } |
Chris Lattner | 07cadaf | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 3799 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3800 | /// parseDirectiveAbort |
Daniel Dunbar | 40a564f | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 3801 | /// ::= .abort [... message ...] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3802 | bool AsmParser::parseDirectiveAbort() { |
Daniel Dunbar | eb6bb32 | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 3803 | // FIXME: Use loc from directive. |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3804 | SMLoc Loc = getLexer().getLoc(); |
Daniel Dunbar | eb6bb32 | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 3805 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3806 | StringRef Str = parseStringToEndOfStatement(); |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3807 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | 56523ce | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 3808 | return TokError("unexpected token in '.abort' directive"); |
Daniel Dunbar | 40a564f | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 3809 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 3810 | Lex(); |
Kevin Enderby | 56523ce | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 3811 | |
Daniel Dunbar | eb6bb32 | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 3812 | if (Str.empty()) |
| 3813 | Error(Loc, ".abort detected. Assembly stopping."); |
| 3814 | else |
| 3815 | Error(Loc, ".abort '" + Str + "' detected. Assembly stopping."); |
Daniel Dunbar | 40a564f | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 3816 | // FIXME: Actually abort assembly here. |
Kevin Enderby | 56523ce | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 3817 | |
| 3818 | return false; |
| 3819 | } |
Kevin Enderby | cbe475d | 2009-07-14 21:35:03 +0000 | [diff] [blame] | 3820 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3821 | /// parseDirectiveInclude |
Kevin Enderby | d1ea539 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 3822 | /// ::= .include "filename" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3823 | bool AsmParser::parseDirectiveInclude() { |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3824 | if (getLexer().isNot(AsmToken::String)) |
Kevin Enderby | d1ea539 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 3825 | return TokError("expected string in '.include' directive"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3826 | |
Yunzhong Gao | 8c0f506 | 2013-09-05 19:14:26 +0000 | [diff] [blame] | 3827 | // Allow the strings to have escaped octal character sequence. |
| 3828 | std::string Filename; |
| 3829 | if (parseEscapedString(Filename)) |
| 3830 | return true; |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3831 | SMLoc IncludeLoc = getLexer().getLoc(); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 3832 | Lex(); |
Kevin Enderby | d1ea539 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 3833 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3834 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | d1ea539 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 3835 | return TokError("unexpected token in '.include' directive"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3836 | |
Chris Lattner | 693fbb8 | 2009-07-16 06:14:39 +0000 | [diff] [blame] | 3837 | // Attempt to switch the lexer to the included file before consuming the end |
| 3838 | // of statement to avoid losing it when we switch. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3839 | if (enterIncludeFile(Filename)) { |
Daniel Dunbar | d8a1845 | 2010-07-18 18:31:45 +0000 | [diff] [blame] | 3840 | Error(IncludeLoc, "Could not find include file '" + Filename + "'"); |
Chris Lattner | 693fbb8 | 2009-07-16 06:14:39 +0000 | [diff] [blame] | 3841 | return true; |
| 3842 | } |
Kevin Enderby | d1ea539 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 3843 | |
| 3844 | return false; |
| 3845 | } |
Kevin Enderby | 09ea570 | 2009-07-15 15:30:11 +0000 | [diff] [blame] | 3846 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3847 | /// parseDirectiveIncbin |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 3848 | /// ::= .incbin "filename" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3849 | bool AsmParser::parseDirectiveIncbin() { |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 3850 | if (getLexer().isNot(AsmToken::String)) |
| 3851 | return TokError("expected string in '.incbin' directive"); |
| 3852 | |
Yunzhong Gao | 8c0f506 | 2013-09-05 19:14:26 +0000 | [diff] [blame] | 3853 | // Allow the strings to have escaped octal character sequence. |
| 3854 | std::string Filename; |
| 3855 | if (parseEscapedString(Filename)) |
| 3856 | return true; |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 3857 | SMLoc IncbinLoc = getLexer().getLoc(); |
| 3858 | Lex(); |
| 3859 | |
| 3860 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3861 | return TokError("unexpected token in '.incbin' directive"); |
| 3862 | |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 3863 | // Attempt to process the included file. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3864 | if (processIncbinFile(Filename)) { |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 3865 | Error(IncbinLoc, "Could not find incbin file '" + Filename + "'"); |
| 3866 | return true; |
| 3867 | } |
| 3868 | |
| 3869 | return false; |
| 3870 | } |
| 3871 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3872 | /// parseDirectiveIf |
Saleem Abdulrasool | 763e2cb | 2014-06-18 20:57:28 +0000 | [diff] [blame] | 3873 | /// ::= .if{,eq,ge,gt,le,lt,ne} expression |
| 3874 | bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind) { |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3875 | TheCondStack.push_back(TheCondState); |
| 3876 | TheCondState.TheCond = AsmCond::IfCond; |
Benjamin Kramer | c7eda3e | 2012-05-12 16:52:21 +0000 | [diff] [blame] | 3877 | if (TheCondState.Ignore) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3878 | eatToEndOfStatement(); |
Benjamin Kramer | c7eda3e | 2012-05-12 16:52:21 +0000 | [diff] [blame] | 3879 | } else { |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3880 | int64_t ExprValue; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3881 | if (parseAbsoluteExpression(ExprValue)) |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3882 | return true; |
| 3883 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3884 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3885 | return TokError("unexpected token in '.if' directive"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3886 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 3887 | Lex(); |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3888 | |
Saleem Abdulrasool | 763e2cb | 2014-06-18 20:57:28 +0000 | [diff] [blame] | 3889 | switch (DirKind) { |
| 3890 | default: |
| 3891 | llvm_unreachable("unsupported directive"); |
| 3892 | case DK_IF: |
| 3893 | case DK_IFNE: |
| 3894 | break; |
| 3895 | case DK_IFEQ: |
| 3896 | ExprValue = ExprValue == 0; |
| 3897 | break; |
| 3898 | case DK_IFGE: |
| 3899 | ExprValue = ExprValue >= 0; |
| 3900 | break; |
| 3901 | case DK_IFGT: |
| 3902 | ExprValue = ExprValue > 0; |
| 3903 | break; |
| 3904 | case DK_IFLE: |
| 3905 | ExprValue = ExprValue <= 0; |
| 3906 | break; |
| 3907 | case DK_IFLT: |
| 3908 | ExprValue = ExprValue < 0; |
| 3909 | break; |
| 3910 | } |
| 3911 | |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3912 | TheCondState.CondMet = ExprValue; |
| 3913 | TheCondState.Ignore = !TheCondState.CondMet; |
| 3914 | } |
| 3915 | |
| 3916 | return false; |
| 3917 | } |
| 3918 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3919 | /// parseDirectiveIfb |
Benjamin Kramer | 62c18b0 | 2012-05-12 11:18:42 +0000 | [diff] [blame] | 3920 | /// ::= .ifb string |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3921 | bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) { |
Benjamin Kramer | 62c18b0 | 2012-05-12 11:18:42 +0000 | [diff] [blame] | 3922 | TheCondStack.push_back(TheCondState); |
| 3923 | TheCondState.TheCond = AsmCond::IfCond; |
| 3924 | |
Benjamin Kramer | c7eda3e | 2012-05-12 16:52:21 +0000 | [diff] [blame] | 3925 | if (TheCondState.Ignore) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3926 | eatToEndOfStatement(); |
Benjamin Kramer | 62c18b0 | 2012-05-12 11:18:42 +0000 | [diff] [blame] | 3927 | } else { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3928 | StringRef Str = parseStringToEndOfStatement(); |
Benjamin Kramer | 62c18b0 | 2012-05-12 11:18:42 +0000 | [diff] [blame] | 3929 | |
| 3930 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3931 | return TokError("unexpected token in '.ifb' directive"); |
| 3932 | |
| 3933 | Lex(); |
| 3934 | |
| 3935 | TheCondState.CondMet = ExpectBlank == Str.empty(); |
| 3936 | TheCondState.Ignore = !TheCondState.CondMet; |
| 3937 | } |
| 3938 | |
| 3939 | return false; |
| 3940 | } |
| 3941 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3942 | /// parseDirectiveIfc |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 3943 | /// ::= .ifc string1, string2 |
Saleem Abdulrasool | 5db5298 | 2014-02-23 15:53:36 +0000 | [diff] [blame] | 3944 | /// ::= .ifnc string1, string2 |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3945 | bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) { |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 3946 | TheCondStack.push_back(TheCondState); |
| 3947 | TheCondState.TheCond = AsmCond::IfCond; |
| 3948 | |
Benjamin Kramer | c7eda3e | 2012-05-12 16:52:21 +0000 | [diff] [blame] | 3949 | if (TheCondState.Ignore) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3950 | eatToEndOfStatement(); |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 3951 | } else { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3952 | StringRef Str1 = parseStringToComma(); |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 3953 | |
| 3954 | if (getLexer().isNot(AsmToken::Comma)) |
| 3955 | return TokError("unexpected token in '.ifc' directive"); |
| 3956 | |
| 3957 | Lex(); |
| 3958 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3959 | StringRef Str2 = parseStringToEndOfStatement(); |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 3960 | |
| 3961 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3962 | return TokError("unexpected token in '.ifc' directive"); |
| 3963 | |
| 3964 | Lex(); |
| 3965 | |
Saleem Abdulrasool | 5db5298 | 2014-02-23 15:53:36 +0000 | [diff] [blame] | 3966 | TheCondState.CondMet = ExpectEqual == (Str1.trim() == Str2.trim()); |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 3967 | TheCondState.Ignore = !TheCondState.CondMet; |
| 3968 | } |
| 3969 | |
| 3970 | return false; |
| 3971 | } |
| 3972 | |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 3973 | /// parseDirectiveIfeqs |
| 3974 | /// ::= .ifeqs string1, string2 |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 3975 | bool AsmParser::parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual) { |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 3976 | if (Lexer.isNot(AsmToken::String)) { |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 3977 | if (ExpectEqual) |
| 3978 | TokError("expected string parameter for '.ifeqs' directive"); |
| 3979 | else |
| 3980 | TokError("expected string parameter for '.ifnes' directive"); |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 3981 | eatToEndOfStatement(); |
| 3982 | return true; |
| 3983 | } |
| 3984 | |
| 3985 | StringRef String1 = getTok().getStringContents(); |
| 3986 | Lex(); |
| 3987 | |
| 3988 | if (Lexer.isNot(AsmToken::Comma)) { |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 3989 | if (ExpectEqual) |
| 3990 | TokError("expected comma after first string for '.ifeqs' directive"); |
| 3991 | else |
| 3992 | TokError("expected comma after first string for '.ifnes' directive"); |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 3993 | eatToEndOfStatement(); |
| 3994 | return true; |
| 3995 | } |
| 3996 | |
| 3997 | Lex(); |
| 3998 | |
| 3999 | if (Lexer.isNot(AsmToken::String)) { |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 4000 | if (ExpectEqual) |
| 4001 | TokError("expected string parameter for '.ifeqs' directive"); |
| 4002 | else |
| 4003 | TokError("expected string parameter for '.ifnes' directive"); |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 4004 | eatToEndOfStatement(); |
| 4005 | return true; |
| 4006 | } |
| 4007 | |
| 4008 | StringRef String2 = getTok().getStringContents(); |
| 4009 | Lex(); |
| 4010 | |
| 4011 | TheCondStack.push_back(TheCondState); |
| 4012 | TheCondState.TheCond = AsmCond::IfCond; |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 4013 | TheCondState.CondMet = ExpectEqual == (String1 == String2); |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 4014 | TheCondState.Ignore = !TheCondState.CondMet; |
| 4015 | |
| 4016 | return false; |
| 4017 | } |
| 4018 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4019 | /// parseDirectiveIfdef |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 4020 | /// ::= .ifdef symbol |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4021 | bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) { |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 4022 | StringRef Name; |
| 4023 | TheCondStack.push_back(TheCondState); |
| 4024 | TheCondState.TheCond = AsmCond::IfCond; |
| 4025 | |
| 4026 | if (TheCondState.Ignore) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4027 | eatToEndOfStatement(); |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 4028 | } else { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4029 | if (parseIdentifier(Name)) |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 4030 | return TokError("expected identifier after '.ifdef'"); |
| 4031 | |
| 4032 | Lex(); |
| 4033 | |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 4034 | MCSymbol *Sym = getContext().lookupSymbol(Name); |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 4035 | |
| 4036 | if (expect_defined) |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 4037 | TheCondState.CondMet = (Sym && !Sym->isUndefined()); |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 4038 | else |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 4039 | TheCondState.CondMet = (!Sym || Sym->isUndefined()); |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 4040 | TheCondState.Ignore = !TheCondState.CondMet; |
| 4041 | } |
| 4042 | |
| 4043 | return false; |
| 4044 | } |
| 4045 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4046 | /// parseDirectiveElseIf |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4047 | /// ::= .elseif expression |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4048 | bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) { |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4049 | if (TheCondState.TheCond != AsmCond::IfCond && |
| 4050 | TheCondState.TheCond != AsmCond::ElseIfCond) |
Craig Topper | 2172ad6 | 2013-04-22 04:24:02 +0000 | [diff] [blame] | 4051 | Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or " |
| 4052 | " an .elseif"); |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4053 | TheCondState.TheCond = AsmCond::ElseIfCond; |
| 4054 | |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4055 | bool LastIgnoreState = false; |
| 4056 | if (!TheCondStack.empty()) |
Craig Topper | 2172ad6 | 2013-04-22 04:24:02 +0000 | [diff] [blame] | 4057 | LastIgnoreState = TheCondStack.back().Ignore; |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4058 | if (LastIgnoreState || TheCondState.CondMet) { |
| 4059 | TheCondState.Ignore = true; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4060 | eatToEndOfStatement(); |
Craig Topper | f15655b | 2013-04-22 04:22:40 +0000 | [diff] [blame] | 4061 | } else { |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4062 | int64_t ExprValue; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4063 | if (parseAbsoluteExpression(ExprValue)) |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4064 | return true; |
| 4065 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 4066 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4067 | return TokError("unexpected token in '.elseif' directive"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 4068 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 4069 | Lex(); |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4070 | TheCondState.CondMet = ExprValue; |
| 4071 | TheCondState.Ignore = !TheCondState.CondMet; |
| 4072 | } |
| 4073 | |
| 4074 | return false; |
| 4075 | } |
| 4076 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4077 | /// parseDirectiveElse |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4078 | /// ::= .else |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4079 | bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) { |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 4080 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4081 | return TokError("unexpected token in '.else' directive"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 4082 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 4083 | Lex(); |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4084 | |
| 4085 | if (TheCondState.TheCond != AsmCond::IfCond && |
| 4086 | TheCondState.TheCond != AsmCond::ElseIfCond) |
Craig Topper | 2172ad6 | 2013-04-22 04:24:02 +0000 | [diff] [blame] | 4087 | Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an " |
| 4088 | ".elseif"); |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4089 | TheCondState.TheCond = AsmCond::ElseCond; |
| 4090 | bool LastIgnoreState = false; |
| 4091 | if (!TheCondStack.empty()) |
| 4092 | LastIgnoreState = TheCondStack.back().Ignore; |
| 4093 | if (LastIgnoreState || TheCondState.CondMet) |
| 4094 | TheCondState.Ignore = true; |
| 4095 | else |
| 4096 | TheCondState.Ignore = false; |
| 4097 | |
| 4098 | return false; |
| 4099 | } |
| 4100 | |
Saleem Abdulrasool | 88186c4 | 2013-12-18 02:53:03 +0000 | [diff] [blame] | 4101 | /// parseDirectiveEnd |
| 4102 | /// ::= .end |
| 4103 | bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) { |
| 4104 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 4105 | return TokError("unexpected token in '.end' directive"); |
| 4106 | |
| 4107 | Lex(); |
| 4108 | |
| 4109 | while (Lexer.isNot(AsmToken::Eof)) |
| 4110 | Lex(); |
| 4111 | |
| 4112 | return false; |
| 4113 | } |
| 4114 | |
Saleem Abdulrasool | 7ecc549 | 2014-02-23 23:02:23 +0000 | [diff] [blame] | 4115 | /// parseDirectiveError |
| 4116 | /// ::= .err |
| 4117 | /// ::= .error [string] |
| 4118 | bool AsmParser::parseDirectiveError(SMLoc L, bool WithMessage) { |
| 4119 | if (!TheCondStack.empty()) { |
| 4120 | if (TheCondStack.back().Ignore) { |
| 4121 | eatToEndOfStatement(); |
Saleem Abdulrasool | b2ae2c0 | 2014-02-23 15:53:30 +0000 | [diff] [blame] | 4122 | return false; |
Saleem Abdulrasool | 7ecc549 | 2014-02-23 23:02:23 +0000 | [diff] [blame] | 4123 | } |
| 4124 | } |
| 4125 | |
| 4126 | if (!WithMessage) |
| 4127 | return Error(L, ".err encountered"); |
| 4128 | |
| 4129 | StringRef Message = ".error directive invoked in source file"; |
| 4130 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
| 4131 | if (Lexer.isNot(AsmToken::String)) { |
| 4132 | TokError(".error argument must be a string"); |
| 4133 | eatToEndOfStatement(); |
| 4134 | return true; |
| 4135 | } |
| 4136 | |
| 4137 | Message = getTok().getStringContents(); |
| 4138 | Lex(); |
| 4139 | } |
| 4140 | |
| 4141 | Error(L, Message); |
| 4142 | return true; |
Saleem Abdulrasool | b2ae2c0 | 2014-02-23 15:53:30 +0000 | [diff] [blame] | 4143 | } |
| 4144 | |
Nico Weber | 404012b | 2014-07-24 16:26:06 +0000 | [diff] [blame] | 4145 | /// parseDirectiveWarning |
| 4146 | /// ::= .warning [string] |
| 4147 | bool AsmParser::parseDirectiveWarning(SMLoc L) { |
| 4148 | if (!TheCondStack.empty()) { |
| 4149 | if (TheCondStack.back().Ignore) { |
| 4150 | eatToEndOfStatement(); |
| 4151 | return false; |
| 4152 | } |
| 4153 | } |
| 4154 | |
| 4155 | StringRef Message = ".warning directive invoked in source file"; |
| 4156 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
| 4157 | if (Lexer.isNot(AsmToken::String)) { |
| 4158 | TokError(".warning argument must be a string"); |
| 4159 | eatToEndOfStatement(); |
| 4160 | return true; |
| 4161 | } |
| 4162 | |
| 4163 | Message = getTok().getStringContents(); |
| 4164 | Lex(); |
| 4165 | } |
| 4166 | |
| 4167 | Warning(L, Message); |
| 4168 | return false; |
| 4169 | } |
| 4170 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4171 | /// parseDirectiveEndIf |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4172 | /// ::= .endif |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4173 | bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) { |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 4174 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4175 | return TokError("unexpected token in '.endif' directive"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 4176 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 4177 | Lex(); |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4178 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4179 | if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty()) |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4180 | Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or " |
| 4181 | ".else"); |
| 4182 | if (!TheCondStack.empty()) { |
| 4183 | TheCondState = TheCondStack.back(); |
| 4184 | TheCondStack.pop_back(); |
| 4185 | } |
| 4186 | |
| 4187 | return false; |
| 4188 | } |
Daniel Dunbar | a4b069c | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 4189 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4190 | void AsmParser::initializeDirectiveKindMap() { |
| 4191 | DirectiveKindMap[".set"] = DK_SET; |
| 4192 | DirectiveKindMap[".equ"] = DK_EQU; |
| 4193 | DirectiveKindMap[".equiv"] = DK_EQUIV; |
| 4194 | DirectiveKindMap[".ascii"] = DK_ASCII; |
| 4195 | DirectiveKindMap[".asciz"] = DK_ASCIZ; |
| 4196 | DirectiveKindMap[".string"] = DK_STRING; |
| 4197 | DirectiveKindMap[".byte"] = DK_BYTE; |
| 4198 | DirectiveKindMap[".short"] = DK_SHORT; |
| 4199 | DirectiveKindMap[".value"] = DK_VALUE; |
| 4200 | DirectiveKindMap[".2byte"] = DK_2BYTE; |
| 4201 | DirectiveKindMap[".long"] = DK_LONG; |
| 4202 | DirectiveKindMap[".int"] = DK_INT; |
| 4203 | DirectiveKindMap[".4byte"] = DK_4BYTE; |
| 4204 | DirectiveKindMap[".quad"] = DK_QUAD; |
| 4205 | DirectiveKindMap[".8byte"] = DK_8BYTE; |
David Woodhouse | d6de0d9 | 2014-02-01 16:20:59 +0000 | [diff] [blame] | 4206 | DirectiveKindMap[".octa"] = DK_OCTA; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4207 | DirectiveKindMap[".single"] = DK_SINGLE; |
| 4208 | DirectiveKindMap[".float"] = DK_FLOAT; |
| 4209 | DirectiveKindMap[".double"] = DK_DOUBLE; |
| 4210 | DirectiveKindMap[".align"] = DK_ALIGN; |
| 4211 | DirectiveKindMap[".align32"] = DK_ALIGN32; |
| 4212 | DirectiveKindMap[".balign"] = DK_BALIGN; |
| 4213 | DirectiveKindMap[".balignw"] = DK_BALIGNW; |
| 4214 | DirectiveKindMap[".balignl"] = DK_BALIGNL; |
| 4215 | DirectiveKindMap[".p2align"] = DK_P2ALIGN; |
| 4216 | DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW; |
| 4217 | DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL; |
| 4218 | DirectiveKindMap[".org"] = DK_ORG; |
| 4219 | DirectiveKindMap[".fill"] = DK_FILL; |
| 4220 | DirectiveKindMap[".zero"] = DK_ZERO; |
| 4221 | DirectiveKindMap[".extern"] = DK_EXTERN; |
| 4222 | DirectiveKindMap[".globl"] = DK_GLOBL; |
| 4223 | DirectiveKindMap[".global"] = DK_GLOBAL; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4224 | DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE; |
| 4225 | DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP; |
| 4226 | DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER; |
| 4227 | DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN; |
| 4228 | DirectiveKindMap[".reference"] = DK_REFERENCE; |
| 4229 | DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION; |
| 4230 | DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE; |
| 4231 | DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN; |
| 4232 | DirectiveKindMap[".comm"] = DK_COMM; |
| 4233 | DirectiveKindMap[".common"] = DK_COMMON; |
| 4234 | DirectiveKindMap[".lcomm"] = DK_LCOMM; |
| 4235 | DirectiveKindMap[".abort"] = DK_ABORT; |
| 4236 | DirectiveKindMap[".include"] = DK_INCLUDE; |
| 4237 | DirectiveKindMap[".incbin"] = DK_INCBIN; |
| 4238 | DirectiveKindMap[".code16"] = DK_CODE16; |
| 4239 | DirectiveKindMap[".code16gcc"] = DK_CODE16GCC; |
| 4240 | DirectiveKindMap[".rept"] = DK_REPT; |
Saleem Abdulrasool | d743d0a | 2013-12-28 05:54:33 +0000 | [diff] [blame] | 4241 | DirectiveKindMap[".rep"] = DK_REPT; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4242 | DirectiveKindMap[".irp"] = DK_IRP; |
| 4243 | DirectiveKindMap[".irpc"] = DK_IRPC; |
| 4244 | DirectiveKindMap[".endr"] = DK_ENDR; |
| 4245 | DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE; |
| 4246 | DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK; |
| 4247 | DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK; |
| 4248 | DirectiveKindMap[".if"] = DK_IF; |
Saleem Abdulrasool | 763e2cb | 2014-06-18 20:57:28 +0000 | [diff] [blame] | 4249 | DirectiveKindMap[".ifeq"] = DK_IFEQ; |
| 4250 | DirectiveKindMap[".ifge"] = DK_IFGE; |
| 4251 | DirectiveKindMap[".ifgt"] = DK_IFGT; |
| 4252 | DirectiveKindMap[".ifle"] = DK_IFLE; |
| 4253 | DirectiveKindMap[".iflt"] = DK_IFLT; |
Saleem Abdulrasool | 5852d6b | 2014-02-23 15:53:41 +0000 | [diff] [blame] | 4254 | DirectiveKindMap[".ifne"] = DK_IFNE; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4255 | DirectiveKindMap[".ifb"] = DK_IFB; |
| 4256 | DirectiveKindMap[".ifnb"] = DK_IFNB; |
| 4257 | DirectiveKindMap[".ifc"] = DK_IFC; |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 4258 | DirectiveKindMap[".ifeqs"] = DK_IFEQS; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4259 | DirectiveKindMap[".ifnc"] = DK_IFNC; |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 4260 | DirectiveKindMap[".ifnes"] = DK_IFNES; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4261 | DirectiveKindMap[".ifdef"] = DK_IFDEF; |
| 4262 | DirectiveKindMap[".ifndef"] = DK_IFNDEF; |
| 4263 | DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF; |
| 4264 | DirectiveKindMap[".elseif"] = DK_ELSEIF; |
| 4265 | DirectiveKindMap[".else"] = DK_ELSE; |
Saleem Abdulrasool | 88186c4 | 2013-12-18 02:53:03 +0000 | [diff] [blame] | 4266 | DirectiveKindMap[".end"] = DK_END; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4267 | DirectiveKindMap[".endif"] = DK_ENDIF; |
| 4268 | DirectiveKindMap[".skip"] = DK_SKIP; |
| 4269 | DirectiveKindMap[".space"] = DK_SPACE; |
| 4270 | DirectiveKindMap[".file"] = DK_FILE; |
| 4271 | DirectiveKindMap[".line"] = DK_LINE; |
| 4272 | DirectiveKindMap[".loc"] = DK_LOC; |
| 4273 | DirectiveKindMap[".stabs"] = DK_STABS; |
| 4274 | DirectiveKindMap[".sleb128"] = DK_SLEB128; |
| 4275 | DirectiveKindMap[".uleb128"] = DK_ULEB128; |
| 4276 | DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS; |
| 4277 | DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC; |
| 4278 | DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC; |
| 4279 | DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA; |
| 4280 | DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET; |
| 4281 | DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET; |
| 4282 | DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER; |
| 4283 | DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET; |
| 4284 | DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET; |
| 4285 | DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY; |
| 4286 | DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA; |
| 4287 | DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE; |
| 4288 | DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE; |
| 4289 | DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE; |
| 4290 | DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE; |
| 4291 | DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE; |
| 4292 | DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME; |
| 4293 | DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED; |
| 4294 | DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER; |
Venkatraman Govindaraju | 3816d43 | 2013-09-26 14:49:40 +0000 | [diff] [blame] | 4295 | DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4296 | DirectiveKindMap[".macros_on"] = DK_MACROS_ON; |
| 4297 | DirectiveKindMap[".macros_off"] = DK_MACROS_OFF; |
| 4298 | DirectiveKindMap[".macro"] = DK_MACRO; |
Nico Weber | 155dccd1 | 2014-07-24 17:08:39 +0000 | [diff] [blame] | 4299 | DirectiveKindMap[".exitm"] = DK_EXITM; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4300 | DirectiveKindMap[".endm"] = DK_ENDM; |
| 4301 | DirectiveKindMap[".endmacro"] = DK_ENDMACRO; |
| 4302 | DirectiveKindMap[".purgem"] = DK_PURGEM; |
Saleem Abdulrasool | b2ae2c0 | 2014-02-23 15:53:30 +0000 | [diff] [blame] | 4303 | DirectiveKindMap[".err"] = DK_ERR; |
Saleem Abdulrasool | 7ecc549 | 2014-02-23 23:02:23 +0000 | [diff] [blame] | 4304 | DirectiveKindMap[".error"] = DK_ERROR; |
Nico Weber | 404012b | 2014-07-24 16:26:06 +0000 | [diff] [blame] | 4305 | DirectiveKindMap[".warning"] = DK_WARNING; |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 4306 | } |
| 4307 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4308 | MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) { |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4309 | AsmToken EndToken, StartToken = getTok(); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4310 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4311 | unsigned NestLevel = 0; |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4312 | for (;;) { |
| 4313 | // Check whether we have reached the end of the file. |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4314 | if (getLexer().is(AsmToken::Eof)) { |
| 4315 | Error(DirectiveLoc, "no matching '.endr' in definition"); |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 4316 | return nullptr; |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4317 | } |
| 4318 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4319 | if (Lexer.is(AsmToken::Identifier) && |
| 4320 | (getTok().getIdentifier() == ".rept")) { |
| 4321 | ++NestLevel; |
| 4322 | } |
| 4323 | |
| 4324 | // Otherwise, check whether we have reached the .endr. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4325 | if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") { |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4326 | if (NestLevel == 0) { |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4327 | EndToken = getTok(); |
| 4328 | Lex(); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4329 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
| 4330 | TokError("unexpected token in '.endr' directive"); |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 4331 | return nullptr; |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4332 | } |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4333 | break; |
| 4334 | } |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4335 | --NestLevel; |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4336 | } |
| 4337 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4338 | // Otherwise, scan till the end of the statement. |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4339 | eatToEndOfStatement(); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4340 | } |
| 4341 | |
| 4342 | const char *BodyStart = StartToken.getLoc().getPointer(); |
| 4343 | const char *BodyEnd = EndToken.getLoc().getPointer(); |
| 4344 | StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart); |
| 4345 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4346 | // We Are Anonymous. |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 4347 | MacroLikeBodies.push_back( |
| 4348 | MCAsmMacro(StringRef(), Body, MCAsmMacroParameters())); |
Benjamin Kramer | 1df3a1f | 2013-08-04 09:06:29 +0000 | [diff] [blame] | 4349 | return &MacroLikeBodies.back(); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4350 | } |
| 4351 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4352 | void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc, |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4353 | raw_svector_ostream &OS) { |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4354 | OS << ".endr\n"; |
| 4355 | |
Rafael Espindola | 3560ff2 | 2014-08-27 20:03:13 +0000 | [diff] [blame] | 4356 | std::unique_ptr<MemoryBuffer> Instantiation = |
| 4357 | MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>"); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4358 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4359 | // Create the macro instantiation object and add to the current macro |
| 4360 | // instantiation stack. |
Rafael Espindola | 9eef18c | 2014-08-27 19:49:03 +0000 | [diff] [blame] | 4361 | MacroInstantiation *MI = new MacroInstantiation( |
| 4362 | DirectiveLoc, CurBuffer, getTok().getLoc(), TheCondStack.size()); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4363 | ActiveMacros.push_back(MI); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4364 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4365 | // Jump to the macro instantiation and prime the lexer. |
David Blaikie | 1961f14 | 2014-08-21 20:44:56 +0000 | [diff] [blame] | 4366 | CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc()); |
Rafael Espindola | 8026bd0 | 2014-07-06 14:17:29 +0000 | [diff] [blame] | 4367 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer()); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4368 | Lex(); |
| 4369 | } |
| 4370 | |
Saleem Abdulrasool | d743d0a | 2013-12-28 05:54:33 +0000 | [diff] [blame] | 4371 | /// parseDirectiveRept |
| 4372 | /// ::= .rep | .rept count |
| 4373 | bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) { |
Saleem Abdulrasool | 51cff71 | 2013-12-28 06:39:29 +0000 | [diff] [blame] | 4374 | const MCExpr *CountExpr; |
| 4375 | SMLoc CountLoc = getTok().getLoc(); |
| 4376 | if (parseExpression(CountExpr)) |
| 4377 | return true; |
| 4378 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4379 | int64_t Count; |
Saleem Abdulrasool | 51cff71 | 2013-12-28 06:39:29 +0000 | [diff] [blame] | 4380 | if (!CountExpr->EvaluateAsAbsolute(Count)) { |
| 4381 | eatToEndOfStatement(); |
| 4382 | return Error(CountLoc, "unexpected token in '" + Dir + "' directive"); |
| 4383 | } |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4384 | |
| 4385 | if (Count < 0) |
Saleem Abdulrasool | 51cff71 | 2013-12-28 06:39:29 +0000 | [diff] [blame] | 4386 | return Error(CountLoc, "Count is negative"); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4387 | |
| 4388 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
Saleem Abdulrasool | d743d0a | 2013-12-28 05:54:33 +0000 | [diff] [blame] | 4389 | return TokError("unexpected token in '" + Dir + "' directive"); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4390 | |
| 4391 | // Eat the end of statement. |
| 4392 | Lex(); |
| 4393 | |
| 4394 | // Lex the rept definition. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4395 | MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4396 | if (!M) |
| 4397 | return true; |
| 4398 | |
| 4399 | // Macro instantiation is lexical, unfortunately. We construct a new buffer |
| 4400 | // to hold the macro body with substitutions. |
| 4401 | SmallString<256> Buf; |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4402 | raw_svector_ostream OS(Buf); |
| 4403 | while (Count--) { |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 4404 | // Note that the AtPseudoVariable is disabled for instantiations of .rep(t). |
| 4405 | if (expandMacro(OS, M->Body, None, None, false, getTok().getLoc())) |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4406 | return true; |
| 4407 | } |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4408 | instantiateMacroLikeBody(M, DirectiveLoc, OS); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4409 | |
| 4410 | return false; |
| 4411 | } |
| 4412 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4413 | /// parseDirectiveIrp |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 4414 | /// ::= .irp symbol,values |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4415 | bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) { |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 4416 | MCAsmMacroParameter Parameter; |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 4417 | |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 4418 | if (parseIdentifier(Parameter.Name)) |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 4419 | return TokError("expected identifier in '.irp' directive"); |
| 4420 | |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 4421 | if (Lexer.isNot(AsmToken::Comma)) |
| 4422 | return TokError("expected comma in '.irp' directive"); |
| 4423 | |
| 4424 | Lex(); |
| 4425 | |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 4426 | MCAsmMacroArguments A; |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 4427 | if (parseMacroArguments(nullptr, A)) |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 4428 | return true; |
| 4429 | |
| 4430 | // Eat the end of statement. |
| 4431 | Lex(); |
| 4432 | |
| 4433 | // Lex the irp definition. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4434 | MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc); |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 4435 | if (!M) |
| 4436 | return true; |
| 4437 | |
| 4438 | // Macro instantiation is lexical, unfortunately. We construct a new buffer |
| 4439 | // to hold the macro body with substitutions. |
| 4440 | SmallString<256> Buf; |
| 4441 | raw_svector_ostream OS(Buf); |
| 4442 | |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 4443 | for (MCAsmMacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) { |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 4444 | // Note that the AtPseudoVariable is enabled for instantiations of .irp. |
| 4445 | // This is undocumented, but GAS seems to support it. |
| 4446 | if (expandMacro(OS, M->Body, Parameter, *i, true, getTok().getLoc())) |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 4447 | return true; |
| 4448 | } |
| 4449 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4450 | instantiateMacroLikeBody(M, DirectiveLoc, OS); |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 4451 | |
| 4452 | return false; |
| 4453 | } |
| 4454 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4455 | /// parseDirectiveIrpc |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 4456 | /// ::= .irpc symbol,values |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4457 | bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) { |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 4458 | MCAsmMacroParameter Parameter; |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 4459 | |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 4460 | if (parseIdentifier(Parameter.Name)) |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 4461 | return TokError("expected identifier in '.irpc' directive"); |
| 4462 | |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 4463 | if (Lexer.isNot(AsmToken::Comma)) |
| 4464 | return TokError("expected comma in '.irpc' directive"); |
| 4465 | |
| 4466 | Lex(); |
| 4467 | |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 4468 | MCAsmMacroArguments A; |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 4469 | if (parseMacroArguments(nullptr, A)) |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 4470 | return true; |
| 4471 | |
| 4472 | if (A.size() != 1 || A.front().size() != 1) |
| 4473 | return TokError("unexpected token in '.irpc' directive"); |
| 4474 | |
| 4475 | // Eat the end of statement. |
| 4476 | Lex(); |
| 4477 | |
| 4478 | // Lex the irpc definition. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4479 | MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc); |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 4480 | if (!M) |
| 4481 | return true; |
| 4482 | |
| 4483 | // Macro instantiation is lexical, unfortunately. We construct a new buffer |
| 4484 | // to hold the macro body with substitutions. |
| 4485 | SmallString<256> Buf; |
| 4486 | raw_svector_ostream OS(Buf); |
| 4487 | |
| 4488 | StringRef Values = A.front().front().getString(); |
Benjamin Kramer | d31aaf1 | 2014-02-09 17:13:11 +0000 | [diff] [blame] | 4489 | for (std::size_t I = 0, End = Values.size(); I != End; ++I) { |
Eli Bendersky | a7b905e | 2013-01-14 19:00:26 +0000 | [diff] [blame] | 4490 | MCAsmMacroArgument Arg; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4491 | Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I + 1))); |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 4492 | |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 4493 | // Note that the AtPseudoVariable is enabled for instantiations of .irpc. |
| 4494 | // This is undocumented, but GAS seems to support it. |
| 4495 | if (expandMacro(OS, M->Body, Parameter, Arg, true, getTok().getLoc())) |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 4496 | return true; |
| 4497 | } |
| 4498 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4499 | instantiateMacroLikeBody(M, DirectiveLoc, OS); |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 4500 | |
| 4501 | return false; |
| 4502 | } |
| 4503 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4504 | bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) { |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4505 | if (ActiveMacros.empty()) |
Preston Gurd | eb3ebf1 | 2012-09-19 20:23:43 +0000 | [diff] [blame] | 4506 | return TokError("unmatched '.endr' directive"); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4507 | |
| 4508 | // The only .repl that should get here are the ones created by |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4509 | // instantiateMacroLikeBody. |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4510 | assert(getLexer().is(AsmToken::EndOfStatement)); |
| 4511 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4512 | handleMacroExit(); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4513 | return false; |
| 4514 | } |
Rafael Espindola | 12d73d1 | 2010-09-11 16:45:15 +0000 | [diff] [blame] | 4515 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4516 | bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info, |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4517 | size_t Len) { |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 4518 | const MCExpr *Value; |
| 4519 | SMLoc ExprLoc = getLexer().getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4520 | if (parseExpression(Value)) |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 4521 | return true; |
| 4522 | const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value); |
| 4523 | if (!MCE) |
| 4524 | return Error(ExprLoc, "unexpected expression in _emit"); |
| 4525 | uint64_t IntValue = MCE->getValue(); |
| 4526 | if (!isUIntN(8, IntValue) && !isIntN(8, IntValue)) |
| 4527 | return Error(ExprLoc, "literal value out of range for directive"); |
| 4528 | |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 4529 | Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, Len)); |
| 4530 | return false; |
| 4531 | } |
| 4532 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4533 | bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) { |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 4534 | const MCExpr *Value; |
| 4535 | SMLoc ExprLoc = getLexer().getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4536 | if (parseExpression(Value)) |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 4537 | return true; |
| 4538 | const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value); |
| 4539 | if (!MCE) |
| 4540 | return Error(ExprLoc, "unexpected expression in align"); |
| 4541 | uint64_t IntValue = MCE->getValue(); |
| 4542 | if (!isPowerOf2_64(IntValue)) |
| 4543 | return Error(ExprLoc, "literal value not a power of two greater then zero"); |
| 4544 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4545 | Info.AsmRewrites->push_back( |
| 4546 | AsmRewrite(AOK_Align, IDLoc, 5, Log2_64(IntValue))); |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 4547 | return false; |
| 4548 | } |
| 4549 | |
Chad Rosier | f43fcf5 | 2013-02-13 21:27:17 +0000 | [diff] [blame] | 4550 | // We are comparing pointers, but the pointers are relative to a single string. |
| 4551 | // Thus, this should always be deterministic. |
Benjamin Kramer | 8817cca | 2013-09-22 14:09:50 +0000 | [diff] [blame] | 4552 | static int rewritesSort(const AsmRewrite *AsmRewriteA, |
| 4553 | const AsmRewrite *AsmRewriteB) { |
Chad Rosier | eb5c168 | 2013-02-13 18:38:58 +0000 | [diff] [blame] | 4554 | if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer()) |
| 4555 | return -1; |
| 4556 | if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer()) |
| 4557 | return 1; |
Chad Rosier | 42d4e2e | 2013-02-15 22:54:16 +0000 | [diff] [blame] | 4558 | |
Chad Rosier | fce4fab | 2013-04-08 17:43:47 +0000 | [diff] [blame] | 4559 | // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output |
| 4560 | // rewrite to the same location. Make sure the SizeDirective rewrite is |
| 4561 | // performed first, then the Imm/ImmPrefix and finally the Input/Output. This |
| 4562 | // ensures the sort algorithm is stable. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4563 | if (AsmRewritePrecedence[AsmRewriteA->Kind] > |
| 4564 | AsmRewritePrecedence[AsmRewriteB->Kind]) |
Chad Rosier | 42d4e2e | 2013-02-15 22:54:16 +0000 | [diff] [blame] | 4565 | return -1; |
Chad Rosier | fce4fab | 2013-04-08 17:43:47 +0000 | [diff] [blame] | 4566 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4567 | if (AsmRewritePrecedence[AsmRewriteA->Kind] < |
| 4568 | AsmRewritePrecedence[AsmRewriteB->Kind]) |
Chad Rosier | 42d4e2e | 2013-02-15 22:54:16 +0000 | [diff] [blame] | 4569 | return 1; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4570 | llvm_unreachable("Unstable rewrite sort."); |
Chad Rosier | b2144ce | 2013-02-13 01:03:13 +0000 | [diff] [blame] | 4571 | } |
| 4572 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4573 | bool AsmParser::parseMSInlineAsm( |
| 4574 | void *AsmLoc, std::string &AsmString, unsigned &NumOutputs, |
| 4575 | unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls, |
| 4576 | SmallVectorImpl<std::string> &Constraints, |
| 4577 | SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII, |
| 4578 | const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) { |
Chad Rosier | 37e755c | 2012-10-23 17:43:43 +0000 | [diff] [blame] | 4579 | SmallVector<void *, 4> InputDecls; |
| 4580 | SmallVector<void *, 4> OutputDecls; |
Chad Rosier | a4bc943 | 2013-01-10 22:10:27 +0000 | [diff] [blame] | 4581 | SmallVector<bool, 4> InputDeclsAddressOf; |
| 4582 | SmallVector<bool, 4> OutputDeclsAddressOf; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4583 | SmallVector<std::string, 4> InputConstraints; |
| 4584 | SmallVector<std::string, 4> OutputConstraints; |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4585 | SmallVector<unsigned, 4> ClobberRegs; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4586 | |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4587 | SmallVector<AsmRewrite, 4> AsmStrRewrites; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4588 | |
| 4589 | // Prime the lexer. |
| 4590 | Lex(); |
| 4591 | |
| 4592 | // While we have input, parse each statement. |
| 4593 | unsigned InputIdx = 0; |
| 4594 | unsigned OutputIdx = 0; |
| 4595 | while (getLexer().isNot(AsmToken::Eof)) { |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 4596 | ParseStatementInfo Info(&AsmStrRewrites); |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 4597 | if (parseStatement(Info, &SI)) |
Chad Rosier | f1f6a72 | 2012-10-19 22:57:33 +0000 | [diff] [blame] | 4598 | return true; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4599 | |
Chad Rosier | 149e8e0 | 2012-12-12 22:45:52 +0000 | [diff] [blame] | 4600 | if (Info.ParseError) |
| 4601 | return true; |
| 4602 | |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4603 | if (Info.Opcode == ~0U) |
| 4604 | continue; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4605 | |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4606 | const MCInstrDesc &Desc = MII->get(Info.Opcode); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4607 | |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4608 | // Build the list of clobbers, outputs and inputs. |
| 4609 | for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) { |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 4610 | MCParsedAsmOperand &Operand = *Info.ParsedOperands[i]; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4611 | |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4612 | // Immediate. |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 4613 | if (Operand.isImm()) |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4614 | continue; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4615 | |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4616 | // Register operand. |
Nico Weber | 42f79db | 2014-07-17 20:24:55 +0000 | [diff] [blame] | 4617 | if (Operand.isReg() && !Operand.needAddressOf() && |
| 4618 | !getTargetParser().OmitRegisterFromClobberLists(Operand.getReg())) { |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4619 | unsigned NumDefs = Desc.getNumDefs(); |
| 4620 | // Clobber. |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 4621 | if (NumDefs && Operand.getMCOperandNum() < NumDefs) |
| 4622 | ClobberRegs.push_back(Operand.getReg()); |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4623 | continue; |
| 4624 | } |
| 4625 | |
| 4626 | // Expr/Input or Output. |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 4627 | StringRef SymName = Operand.getSymName(); |
Chad Rosier | e81309b | 2013-04-09 17:53:49 +0000 | [diff] [blame] | 4628 | if (SymName.empty()) |
| 4629 | continue; |
| 4630 | |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 4631 | void *OpDecl = Operand.getOpDecl(); |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4632 | if (!OpDecl) |
| 4633 | continue; |
| 4634 | |
| 4635 | bool isOutput = (i == 1) && Desc.mayStore(); |
Chad Rosier | e81309b | 2013-04-09 17:53:49 +0000 | [diff] [blame] | 4636 | SMLoc Start = SMLoc::getFromPointer(SymName.data()); |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4637 | if (isOutput) { |
| 4638 | ++InputIdx; |
| 4639 | OutputDecls.push_back(OpDecl); |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 4640 | OutputDeclsAddressOf.push_back(Operand.needAddressOf()); |
Yaron Keren | 075759a | 2015-03-30 15:42:36 +0000 | [diff] [blame] | 4641 | OutputConstraints.push_back(("=" + Operand.getConstraint()).str()); |
Chad Rosier | e81309b | 2013-04-09 17:53:49 +0000 | [diff] [blame] | 4642 | AsmStrRewrites.push_back(AsmRewrite(AOK_Output, Start, SymName.size())); |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4643 | } else { |
| 4644 | InputDecls.push_back(OpDecl); |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 4645 | InputDeclsAddressOf.push_back(Operand.needAddressOf()); |
| 4646 | InputConstraints.push_back(Operand.getConstraint().str()); |
Chad Rosier | e81309b | 2013-04-09 17:53:49 +0000 | [diff] [blame] | 4647 | AsmStrRewrites.push_back(AsmRewrite(AOK_Input, Start, SymName.size())); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4648 | } |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4649 | } |
Reid Kleckner | ee08897 | 2013-12-10 18:27:32 +0000 | [diff] [blame] | 4650 | |
| 4651 | // Consider implicit defs to be clobbers. Think of cpuid and push. |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 4652 | ArrayRef<uint16_t> ImpDefs(Desc.getImplicitDefs(), |
| 4653 | Desc.getNumImplicitDefs()); |
| 4654 | ClobberRegs.insert(ClobberRegs.end(), ImpDefs.begin(), ImpDefs.end()); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4655 | } |
| 4656 | |
| 4657 | // Set the number of Outputs and Inputs. |
Chad Rosier | f641baa | 2012-10-18 19:39:30 +0000 | [diff] [blame] | 4658 | NumOutputs = OutputDecls.size(); |
| 4659 | NumInputs = InputDecls.size(); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4660 | |
| 4661 | // Set the unique clobbers. |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4662 | array_pod_sort(ClobberRegs.begin(), ClobberRegs.end()); |
| 4663 | ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()), |
| 4664 | ClobberRegs.end()); |
| 4665 | Clobbers.assign(ClobberRegs.size(), std::string()); |
| 4666 | for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) { |
| 4667 | raw_string_ostream OS(Clobbers[I]); |
| 4668 | IP->printRegName(OS, ClobberRegs[I]); |
| 4669 | } |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4670 | |
| 4671 | // Merge the various outputs and inputs. Output are expected first. |
| 4672 | if (NumOutputs || NumInputs) { |
| 4673 | unsigned NumExprs = NumOutputs + NumInputs; |
Chad Rosier | f641baa | 2012-10-18 19:39:30 +0000 | [diff] [blame] | 4674 | OpDecls.resize(NumExprs); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4675 | Constraints.resize(NumExprs); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4676 | for (unsigned i = 0; i < NumOutputs; ++i) { |
Chad Rosier | a4bc943 | 2013-01-10 22:10:27 +0000 | [diff] [blame] | 4677 | OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]); |
Chad Rosier | 7245033 | 2013-01-15 23:07:53 +0000 | [diff] [blame] | 4678 | Constraints[i] = OutputConstraints[i]; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4679 | } |
| 4680 | for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) { |
Chad Rosier | a4bc943 | 2013-01-10 22:10:27 +0000 | [diff] [blame] | 4681 | OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]); |
Chad Rosier | 7245033 | 2013-01-15 23:07:53 +0000 | [diff] [blame] | 4682 | Constraints[j] = InputConstraints[i]; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4683 | } |
| 4684 | } |
| 4685 | |
| 4686 | // Build the IR assembly string. |
Alp Toker | e69170a | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 4687 | std::string AsmStringIR; |
| 4688 | raw_string_ostream OS(AsmStringIR); |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 4689 | StringRef ASMString = |
| 4690 | SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID())->getBuffer(); |
| 4691 | const char *AsmStart = ASMString.begin(); |
| 4692 | const char *AsmEnd = ASMString.end(); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4693 | array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort); |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 4694 | for (const AsmRewrite &AR : AsmStrRewrites) { |
| 4695 | AsmRewriteKind Kind = AR.Kind; |
Chad Rosier | ff10ed1 | 2013-04-12 16:26:42 +0000 | [diff] [blame] | 4696 | if (Kind == AOK_Delete) |
| 4697 | continue; |
| 4698 | |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 4699 | const char *Loc = AR.Loc.getPointer(); |
Chad Rosier | 17d3799 | 2013-03-19 21:12:14 +0000 | [diff] [blame] | 4700 | assert(Loc >= AsmStart && "Expected Loc to be at or after Start!"); |
Chad Rosier | 0f48c55 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 4701 | |
Chad Rosier | 120eefd | 2013-03-19 17:32:17 +0000 | [diff] [blame] | 4702 | // Emit everything up to the immediate/expression. |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 4703 | if (unsigned Len = Loc - AsmStart) |
Chad Rosier | 17d3799 | 2013-03-19 21:12:14 +0000 | [diff] [blame] | 4704 | OS << StringRef(AsmStart, Len); |
Chad Rosier | 0f48c55 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 4705 | |
Chad Rosier | 37e755c | 2012-10-23 17:43:43 +0000 | [diff] [blame] | 4706 | // Skip the original expression. |
| 4707 | if (Kind == AOK_Skip) { |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 4708 | AsmStart = Loc + AR.Len; |
Chad Rosier | 37e755c | 2012-10-23 17:43:43 +0000 | [diff] [blame] | 4709 | continue; |
| 4710 | } |
| 4711 | |
Chad Rosier | ff10ed1 | 2013-04-12 16:26:42 +0000 | [diff] [blame] | 4712 | unsigned AdditionalSkip = 0; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4713 | // Rewrite expressions in $N notation. |
Chad Rosier | 0f48c55 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 4714 | switch (Kind) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4715 | default: |
| 4716 | break; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4717 | case AOK_Imm: |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 4718 | OS << "$$" << AR.Val; |
Chad Rosier | 11c42f2 | 2012-10-26 18:04:20 +0000 | [diff] [blame] | 4719 | break; |
| 4720 | case AOK_ImmPrefix: |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4721 | OS << "$$"; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4722 | break; |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 4723 | case AOK_Label: |
Matt Arsenault | 4e27343 | 2014-12-04 00:06:57 +0000 | [diff] [blame] | 4724 | OS << Ctx.getAsmInfo()->getPrivateLabelPrefix() << AR.Label; |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 4725 | break; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4726 | case AOK_Input: |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4727 | OS << '$' << InputIdx++; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4728 | break; |
| 4729 | case AOK_Output: |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4730 | OS << '$' << OutputIdx++; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4731 | break; |
Chad Rosier | 0f48c55 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 4732 | case AOK_SizeDirective: |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 4733 | switch (AR.Val) { |
Chad Rosier | 0f48c55 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 4734 | default: break; |
| 4735 | case 8: OS << "byte ptr "; break; |
| 4736 | case 16: OS << "word ptr "; break; |
| 4737 | case 32: OS << "dword ptr "; break; |
| 4738 | case 64: OS << "qword ptr "; break; |
| 4739 | case 80: OS << "xword ptr "; break; |
| 4740 | case 128: OS << "xmmword ptr "; break; |
| 4741 | case 256: OS << "ymmword ptr "; break; |
| 4742 | } |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 4743 | break; |
| 4744 | case AOK_Emit: |
| 4745 | OS << ".byte"; |
| 4746 | break; |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 4747 | case AOK_Align: { |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 4748 | unsigned Val = AR.Val; |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 4749 | OS << ".align " << Val; |
| 4750 | |
| 4751 | // Skip the original immediate. |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4752 | assert(Val < 10 && "Expected alignment less then 2^10."); |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 4753 | AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4; |
| 4754 | break; |
| 4755 | } |
Chad Rosier | f0e8720 | 2012-10-25 20:41:34 +0000 | [diff] [blame] | 4756 | case AOK_DotOperator: |
Reid Kleckner | 94a1c4d | 2014-03-06 19:19:12 +0000 | [diff] [blame] | 4757 | // Insert the dot if the user omitted it. |
Alp Toker | e69170a | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 4758 | OS.flush(); |
| 4759 | if (AsmStringIR.back() != '.') |
Reid Kleckner | 94a1c4d | 2014-03-06 19:19:12 +0000 | [diff] [blame] | 4760 | OS << '.'; |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 4761 | OS << AR.Val; |
Chad Rosier | f0e8720 | 2012-10-25 20:41:34 +0000 | [diff] [blame] | 4762 | break; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4763 | } |
Chad Rosier | 0f48c55 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 4764 | |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4765 | // Skip the original expression. |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 4766 | AsmStart = Loc + AR.Len + AdditionalSkip; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4767 | } |
| 4768 | |
| 4769 | // Emit the remainder of the asm string. |
Chad Rosier | 17d3799 | 2013-03-19 21:12:14 +0000 | [diff] [blame] | 4770 | if (AsmStart != AsmEnd) |
| 4771 | OS << StringRef(AsmStart, AsmEnd - AsmStart); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4772 | |
| 4773 | AsmString = OS.str(); |
| 4774 | return false; |
| 4775 | } |
| 4776 | |
Daniel Dunbar | 01e3607 | 2010-07-17 02:26:10 +0000 | [diff] [blame] | 4777 | /// \brief Create an MCAsmParser instance. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4778 | MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C, |
| 4779 | MCStreamer &Out, const MCAsmInfo &MAI) { |
Jim Grosbach | 345768c | 2011-08-16 18:33:49 +0000 | [diff] [blame] | 4780 | return new AsmParser(SM, C, Out, MAI); |
Daniel Dunbar | 01e3607 | 2010-07-17 02:26:10 +0000 | [diff] [blame] | 4781 | } |