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" |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 29 | #include "llvm/MC/MCParser/MCAsmParserUtils.h" |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 30 | #include "llvm/MC/MCParser/MCParsedAsmOperand.h" |
Evan Cheng | 7679299 | 2011-07-20 05:58:47 +0000 | [diff] [blame] | 31 | #include "llvm/MC/MCRegisterInfo.h" |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 32 | #include "llvm/MC/MCSectionMachO.h" |
Daniel Dunbar | ca29e4d | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 33 | #include "llvm/MC/MCStreamer.h" |
Daniel Dunbar | ae7ac01 | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 34 | #include "llvm/MC/MCSymbol.h" |
Evan Cheng | 1142444 | 2011-07-26 00:24:13 +0000 | [diff] [blame] | 35 | #include "llvm/MC/MCTargetAsmParser.h" |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 36 | #include "llvm/MC/MCValue.h" |
Joerg Sonnenberger | 74ba262 | 2011-05-19 18:00:13 +0000 | [diff] [blame] | 37 | #include "llvm/Support/CommandLine.h" |
Benjamin Kramer | 4efe506 | 2012-01-28 15:28:41 +0000 | [diff] [blame] | 38 | #include "llvm/Support/ErrorHandling.h" |
Jim Grosbach | 76346c3 | 2011-06-29 16:05:14 +0000 | [diff] [blame] | 39 | #include "llvm/Support/MathExtras.h" |
Kevin Enderby | e233dda | 2010-06-28 21:45:58 +0000 | [diff] [blame] | 40 | #include "llvm/Support/MemoryBuffer.h" |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 41 | #include "llvm/Support/SourceMgr.h" |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 42 | #include "llvm/Support/raw_ostream.h" |
Nick Lewycky | 0de20af | 2010-12-19 20:43:38 +0000 | [diff] [blame] | 43 | #include <cctype> |
Benjamin Kramer | d59664f | 2014-04-29 23:26:49 +0000 | [diff] [blame] | 44 | #include <deque> |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 45 | #include <set> |
| 46 | #include <string> |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 47 | #include <vector> |
Chris Lattner | b013345 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 48 | using namespace llvm; |
| 49 | |
Eric Christopher | a7c3273 | 2012-12-18 00:30:54 +0000 | [diff] [blame] | 50 | MCAsmParserSemaCallback::~MCAsmParserSemaCallback() {} |
Nick Lewycky | ac61227 | 2012-10-19 07:00:09 +0000 | [diff] [blame] | 51 | |
Daniel Dunbar | 8603340 | 2010-07-12 17:54:38 +0000 | [diff] [blame] | 52 | namespace { |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 53 | /// \brief Helper types for tracking macro definitions. |
| 54 | typedef std::vector<AsmToken> MCAsmMacroArgument; |
| 55 | typedef std::vector<MCAsmMacroArgument> MCAsmMacroArguments; |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 56 | |
| 57 | struct MCAsmMacroParameter { |
| 58 | StringRef Name; |
| 59 | MCAsmMacroArgument Value; |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 60 | bool Required; |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 61 | bool Vararg; |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 62 | |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 63 | MCAsmMacroParameter() : Required(false), Vararg(false) {} |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 64 | }; |
| 65 | |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 66 | typedef std::vector<MCAsmMacroParameter> MCAsmMacroParameters; |
| 67 | |
| 68 | struct MCAsmMacro { |
| 69 | StringRef Name; |
| 70 | StringRef Body; |
| 71 | MCAsmMacroParameters Parameters; |
| 72 | |
| 73 | public: |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 74 | MCAsmMacro(StringRef N, StringRef B, MCAsmMacroParameters P) |
| 75 | : Name(N), Body(B), Parameters(std::move(P)) {} |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 76 | }; |
| 77 | |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 78 | /// \brief Helper class for storing information about an active macro |
| 79 | /// instantiation. |
| 80 | struct MacroInstantiation { |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 81 | /// The location of the instantiation. |
| 82 | SMLoc InstantiationLoc; |
| 83 | |
Daniel Dunbar | 40f1d85 | 2012-12-01 01:38:48 +0000 | [diff] [blame] | 84 | /// The buffer where parsing should resume upon instantiation completion. |
| 85 | int ExitBuffer; |
| 86 | |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 87 | /// The location where parsing should resume upon instantiation completion. |
| 88 | SMLoc ExitLoc; |
| 89 | |
Nico Weber | 155dccd1 | 2014-07-24 17:08:39 +0000 | [diff] [blame] | 90 | /// The depth of TheCondStack at the start of the instantiation. |
| 91 | size_t CondStackDepth; |
| 92 | |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 93 | public: |
Rafael Espindola | 9eef18c | 2014-08-27 19:49:03 +0000 | [diff] [blame] | 94 | MacroInstantiation(SMLoc IL, int EB, SMLoc EL, size_t CondStackDepth); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 95 | }; |
| 96 | |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 97 | struct ParseStatementInfo { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 98 | /// \brief The parsed operands from the last parsed statement. |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 99 | SmallVector<std::unique_ptr<MCParsedAsmOperand>, 8> ParsedOperands; |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 100 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 101 | /// \brief The opcode from the last parsed instruction. |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 102 | unsigned Opcode; |
| 103 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 104 | /// \brief Was there an error parsing the inline assembly? |
Chad Rosier | 149e8e0 | 2012-12-12 22:45:52 +0000 | [diff] [blame] | 105 | bool ParseError; |
| 106 | |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 107 | SmallVectorImpl<AsmRewrite> *AsmRewrites; |
| 108 | |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 109 | ParseStatementInfo() : Opcode(~0U), ParseError(false), AsmRewrites(nullptr) {} |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 110 | ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites) |
Chad Rosier | 149e8e0 | 2012-12-12 22:45:52 +0000 | [diff] [blame] | 111 | : Opcode(~0), ParseError(false), AsmRewrites(rewrites) {} |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 112 | }; |
| 113 | |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 114 | /// \brief The concrete assembly parser instance. |
| 115 | class AsmParser : public MCAsmParser { |
Aaron Ballman | f9a1897 | 2015-02-15 22:54:22 +0000 | [diff] [blame] | 116 | AsmParser(const AsmParser &) = delete; |
| 117 | void operator=(const AsmParser &) = delete; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 118 | private: |
| 119 | AsmLexer Lexer; |
| 120 | MCContext &Ctx; |
| 121 | MCStreamer &Out; |
Jim Grosbach | c7e6b8f | 2011-06-15 18:33:28 +0000 | [diff] [blame] | 122 | const MCAsmInfo &MAI; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 123 | SourceMgr &SrcMgr; |
Benjamin Kramer | 47f5e30 | 2011-10-16 10:48:29 +0000 | [diff] [blame] | 124 | SourceMgr::DiagHandlerTy SavedDiagHandler; |
| 125 | void *SavedDiagContext; |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 126 | std::unique_ptr<MCAsmParserExtension> PlatformParser; |
Rafael Espindola | 82065cb | 2011-04-11 21:49:50 +0000 | [diff] [blame] | 127 | |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 128 | /// This is the current buffer index we're lexing from as managed by the |
| 129 | /// SourceMgr object. |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 130 | unsigned CurBuffer; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 131 | |
| 132 | AsmCond TheCondState; |
| 133 | std::vector<AsmCond> TheCondStack; |
| 134 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 135 | /// \brief maps directive names to handler methods in parser |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 136 | /// extensions. Extensions register themselves in this map by calling |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 137 | /// addDirectiveHandler. |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 138 | StringMap<ExtensionDirectiveHandler> ExtensionDirectiveMap; |
Daniel Dunbar | 828984f | 2010-07-18 18:38:02 +0000 | [diff] [blame] | 139 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 140 | /// \brief Map of currently defined macros. |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 141 | StringMap<MCAsmMacro> MacroMap; |
Daniel Dunbar | c1f58ec | 2010-07-18 18:47:21 +0000 | [diff] [blame] | 142 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 143 | /// \brief Stack of active macro instantiations. |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 144 | std::vector<MacroInstantiation*> ActiveMacros; |
| 145 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 146 | /// \brief List of bodies of anonymous macros. |
Benjamin Kramer | 1df3a1f | 2013-08-04 09:06:29 +0000 | [diff] [blame] | 147 | std::deque<MCAsmMacro> MacroLikeBodies; |
| 148 | |
Daniel Dunbar | 828984f | 2010-07-18 18:38:02 +0000 | [diff] [blame] | 149 | /// Boolean tracking whether macro substitution is enabled. |
Eli Bendersky | c2f6f92 | 2013-01-14 18:08:41 +0000 | [diff] [blame] | 150 | unsigned MacrosEnabledFlag : 1; |
Daniel Dunbar | 828984f | 2010-07-18 18:38:02 +0000 | [diff] [blame] | 151 | |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 152 | /// \brief Keeps track of how many .macro's have been instantiated. |
| 153 | unsigned NumOfMacroInstantiations; |
| 154 | |
Daniel Dunbar | 43325c4 | 2010-09-09 22:42:56 +0000 | [diff] [blame] | 155 | /// Flag tracking whether any errors have been encountered. |
| 156 | unsigned HadError : 1; |
| 157 | |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 158 | /// The values from the last parsed cpp hash file line comment if any. |
| 159 | StringRef CppHashFilename; |
| 160 | int64_t CppHashLineNumber; |
| 161 | SMLoc CppHashLoc; |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 162 | unsigned CppHashBuf; |
Kevin Enderby | 0fd064c | 2013-06-21 20:51:39 +0000 | [diff] [blame] | 163 | /// When generating dwarf for assembly source files we need to calculate the |
| 164 | /// 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] | 165 | /// 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] | 166 | /// cache we save the last info we queried with SrcMgr.FindLineNumber(). |
| 167 | SMLoc LastQueryIDLoc; |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 168 | unsigned LastQueryBuffer; |
Kevin Enderby | 0fd064c | 2013-06-21 20:51:39 +0000 | [diff] [blame] | 169 | unsigned LastQueryLine; |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 170 | |
Devang Patel | a173ee5 | 2012-01-31 18:14:05 +0000 | [diff] [blame] | 171 | /// AssemblerDialect. ~OU means unset value and use value provided by MAI. |
| 172 | unsigned AssemblerDialect; |
| 173 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 174 | /// \brief is Darwin compatibility enabled? |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 175 | bool IsDarwin; |
| 176 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 177 | /// \brief Are we parsing ms-style inline assembly? |
Chad Rosier | 4996355 | 2012-10-13 00:26:04 +0000 | [diff] [blame] | 178 | bool ParsingInlineAsm; |
| 179 | |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 180 | public: |
Jim Grosbach | 345768c | 2011-08-16 18:33:49 +0000 | [diff] [blame] | 181 | AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out, |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 182 | const MCAsmInfo &MAI); |
Alexander Kornienko | f817c1c | 2015-04-11 02:11:45 +0000 | [diff] [blame] | 183 | ~AsmParser() 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 | bool Run(bool NoInitialTextSection, bool NoFinalize = false) override; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 186 | |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 187 | void addDirectiveHandler(StringRef Directive, |
| 188 | ExtensionDirectiveHandler Handler) override { |
Eli Bendersky | 29b9f47 | 2013-01-16 00:50:52 +0000 | [diff] [blame] | 189 | ExtensionDirectiveMap[Directive] = Handler; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Toma Tabacu | 11e14a9 | 2015-04-21 11:50:52 +0000 | [diff] [blame] | 192 | void addAliasForDirective(StringRef Directive, StringRef Alias) override { |
| 193 | DirectiveKindMap[Directive] = DirectiveKindMap[Alias]; |
| 194 | } |
| 195 | |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 196 | public: |
| 197 | /// @name MCAsmParser Interface |
| 198 | /// { |
| 199 | |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 200 | SourceMgr &getSourceManager() override { return SrcMgr; } |
| 201 | MCAsmLexer &getLexer() override { return Lexer; } |
| 202 | MCContext &getContext() override { return Ctx; } |
| 203 | MCStreamer &getStreamer() override { return Out; } |
| 204 | unsigned getAssemblerDialect() override { |
Devang Patel | a173ee5 | 2012-01-31 18:14:05 +0000 | [diff] [blame] | 205 | if (AssemblerDialect == ~0U) |
Eric Christopher | a7c3273 | 2012-12-18 00:30:54 +0000 | [diff] [blame] | 206 | return MAI.getAssemblerDialect(); |
Devang Patel | a173ee5 | 2012-01-31 18:14:05 +0000 | [diff] [blame] | 207 | else |
| 208 | return AssemblerDialect; |
| 209 | } |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 210 | void setAssemblerDialect(unsigned i) override { |
Devang Patel | a173ee5 | 2012-01-31 18:14:05 +0000 | [diff] [blame] | 211 | AssemblerDialect = i; |
| 212 | } |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 213 | |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 214 | void Note(SMLoc L, const Twine &Msg, |
| 215 | ArrayRef<SMRange> Ranges = None) override; |
| 216 | bool Warning(SMLoc L, const Twine &Msg, |
| 217 | ArrayRef<SMRange> Ranges = None) override; |
| 218 | bool Error(SMLoc L, const Twine &Msg, |
| 219 | ArrayRef<SMRange> Ranges = None) 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 | const AsmToken &Lex() override; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 222 | |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 223 | void setParsingInlineAsm(bool V) override { ParsingInlineAsm = V; } |
| 224 | bool isParsingInlineAsm() override { return ParsingInlineAsm; } |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 225 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 226 | bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString, |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 227 | unsigned &NumOutputs, unsigned &NumInputs, |
Chad Rosier | 37e755c | 2012-10-23 17:43:43 +0000 | [diff] [blame] | 228 | SmallVectorImpl<std::pair<void *,bool> > &OpDecls, |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 229 | SmallVectorImpl<std::string> &Constraints, |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 230 | SmallVectorImpl<std::string> &Clobbers, |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 231 | const MCInstrInfo *MII, const MCInstPrinter *IP, |
| 232 | MCAsmParserSemaCallback &SI) override; |
Chad Rosier | 4996355 | 2012-10-13 00:26:04 +0000 | [diff] [blame] | 233 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 234 | bool parseExpression(const MCExpr *&Res); |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 235 | bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) override; |
| 236 | bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) override; |
| 237 | bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) override; |
Toma Tabacu | 7bc44dc | 2015-06-25 09:52:02 +0000 | [diff] [blame] | 238 | bool parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res, |
| 239 | SMLoc &EndLoc) override; |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 240 | bool parseAbsoluteExpression(int64_t &Res) override; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 241 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 242 | /// \brief Parse an identifier or string (as a quoted identifier) |
Eli Bendersky | 0cf0cb9 | 2013-01-12 00:05:00 +0000 | [diff] [blame] | 243 | /// and set \p Res to the identifier contents. |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 244 | bool parseIdentifier(StringRef &Res) override; |
| 245 | void eatToEndOfStatement() override; |
Eli Bendersky | 0cf0cb9 | 2013-01-12 00:05:00 +0000 | [diff] [blame] | 246 | |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 247 | void checkForValidSection() override; |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 248 | /// } |
| 249 | |
| 250 | private: |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 251 | |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 252 | bool parseStatement(ParseStatementInfo &Info, |
| 253 | MCAsmParserSemaCallback *SI); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 254 | void eatToEndOfLine(); |
Craig Topper | 3c76c52 | 2015-09-20 23:35:59 +0000 | [diff] [blame] | 255 | bool parseCppHashLineFilenameComment(SMLoc L); |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 256 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 257 | void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body, |
Benjamin Kramer | d31aaf1 | 2014-02-09 17:13:11 +0000 | [diff] [blame] | 258 | ArrayRef<MCAsmMacroParameter> Parameters); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 259 | bool expandMacro(raw_svector_ostream &OS, StringRef Body, |
Benjamin Kramer | d31aaf1 | 2014-02-09 17:13:11 +0000 | [diff] [blame] | 260 | ArrayRef<MCAsmMacroParameter> Parameters, |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 261 | ArrayRef<MCAsmMacroArgument> A, bool EnableAtPseudoVariable, |
Craig Topper | 3c76c52 | 2015-09-20 23:35:59 +0000 | [diff] [blame] | 262 | SMLoc L); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 263 | |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 264 | /// \brief Are macros enabled in the parser? |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 265 | bool areMacrosEnabled() {return MacrosEnabledFlag;} |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 266 | |
| 267 | /// \brief Control a flag in the parser that enables or disables macros. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 268 | void setMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;} |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 269 | |
| 270 | /// \brief Lookup a previously defined macro. |
| 271 | /// \param Name Macro name. |
| 272 | /// \returns Pointer to macro. NULL if no such macro was defined. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 273 | const MCAsmMacro* lookupMacro(StringRef Name); |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 274 | |
| 275 | /// \brief Define a new macro with the given name and information. |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 276 | void defineMacro(StringRef Name, MCAsmMacro Macro); |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 277 | |
| 278 | /// \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] | 279 | void undefineMacro(StringRef Name); |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 280 | |
| 281 | /// \brief Are we inside a macro instantiation? |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 282 | bool isInsideMacroInstantiation() {return !ActiveMacros.empty();} |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 283 | |
Vladimir Medic | 9bad0d33 | 2013-08-20 13:33:18 +0000 | [diff] [blame] | 284 | /// \brief Handle entry to macro instantiation. |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 285 | /// |
| 286 | /// \param M The macro. |
| 287 | /// \param NameLoc Instantiation location. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 288 | bool handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc); |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 289 | |
| 290 | /// \brief Handle exit from macro instantiation. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 291 | void handleMacroExit(); |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 292 | |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 293 | /// \brief Extract AsmTokens for a macro argument. |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 294 | bool parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg); |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 295 | |
| 296 | /// \brief Parse all macro arguments for a given macro. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 297 | bool parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A); |
Eli Bendersky | a313ae6 | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 298 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 299 | void printMacroInstantiations(); |
| 300 | void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg, |
Dmitri Gribenko | 3238fb7 | 2013-05-05 00:40:33 +0000 | [diff] [blame] | 301 | ArrayRef<SMRange> Ranges = None) const { |
Chris Lattner | 7284526 | 2011-10-16 05:47:55 +0000 | [diff] [blame] | 302 | SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges); |
Benjamin Kramer | c758311 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 303 | } |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 304 | static void DiagHandler(const SMDiagnostic &Diag, void *Context); |
Benjamin Kramer | c758311 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 305 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 306 | /// \brief Enter the specified file. This returns true on failure. |
| 307 | bool enterIncludeFile(const std::string &Filename); |
| 308 | |
| 309 | /// \brief Process the specified file for the .incbin directive. |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 310 | /// This returns true on failure. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 311 | bool processIncbinFile(const std::string &Filename); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 312 | |
Dmitri Gribenko | 5485acd | 2012-09-14 14:57:36 +0000 | [diff] [blame] | 313 | /// \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] | 314 | /// current token is not set; clients should ensure Lex() is called |
| 315 | /// subsequently. |
Daniel Dunbar | 40f1d85 | 2012-12-01 01:38:48 +0000 | [diff] [blame] | 316 | /// |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 317 | /// \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] | 318 | /// location. |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 319 | void jumpToLoc(SMLoc Loc, unsigned InBuffer = 0); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 320 | |
Daniel Dunbar | 40a564f | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 321 | /// \brief Parse up to the end of statement and a return the contents from the |
| 322 | /// current token until the end of the statement; the current token on exit |
| 323 | /// will be either the EndOfStatement or EOF. |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 324 | StringRef parseStringToEndOfStatement() override; |
Daniel Dunbar | 40a564f | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 325 | |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 326 | /// \brief Parse until the end of a statement or a comma is encountered, |
| 327 | /// 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] | 328 | StringRef parseStringToComma(); |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 329 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 330 | bool parseAssignment(StringRef Name, bool allow_redef, |
Jim Grosbach | b7b750d | 2012-09-13 23:11:31 +0000 | [diff] [blame] | 331 | bool NoDeadStrip = false); |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 332 | |
Ahmed Bougacha | 457852f | 2015-04-28 00:17:39 +0000 | [diff] [blame] | 333 | unsigned getBinOpPrecedence(AsmToken::TokenKind K, |
| 334 | MCBinaryExpr::Opcode &Kind); |
| 335 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 336 | bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc); |
| 337 | bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc); |
| 338 | bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc); |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 339 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 340 | bool parseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc); |
Rafael Espindola | 63760ba | 2010-10-28 20:02:27 +0000 | [diff] [blame] | 341 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 342 | // Generic (target and platform independent) directive parsing. |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 343 | enum DirectiveKind { |
Eli Bendersky | 4d21fa0 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 344 | DK_NO_DIRECTIVE, // Placeholder |
| 345 | DK_SET, DK_EQU, DK_EQUIV, DK_ASCII, DK_ASCIZ, DK_STRING, DK_BYTE, DK_SHORT, |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 346 | DK_RELOC, |
David Woodhouse | d6de0d9 | 2014-02-01 16:20:59 +0000 | [diff] [blame] | 347 | DK_VALUE, DK_2BYTE, DK_LONG, DK_INT, DK_4BYTE, DK_QUAD, DK_8BYTE, DK_OCTA, |
| 348 | 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] | 349 | 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] | 350 | DK_BUNDLE_ALIGN_MODE, DK_BUNDLE_LOCK, DK_BUNDLE_UNLOCK, |
Kevin Enderby | 3aeada2 | 2013-08-28 17:50:59 +0000 | [diff] [blame] | 351 | DK_ZERO, DK_EXTERN, DK_GLOBL, DK_GLOBAL, |
Eli Bendersky | 4d21fa0 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 352 | DK_LAZY_REFERENCE, DK_NO_DEAD_STRIP, DK_SYMBOL_RESOLVER, DK_PRIVATE_EXTERN, |
| 353 | DK_REFERENCE, DK_WEAK_DEFINITION, DK_WEAK_REFERENCE, |
| 354 | DK_WEAK_DEF_CAN_BE_HIDDEN, DK_COMM, DK_COMMON, DK_LCOMM, DK_ABORT, |
| 355 | 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] | 356 | 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] | 357 | DK_IFNB, DK_IFC, DK_IFEQS, DK_IFNC, DK_IFNES, DK_IFDEF, DK_IFNDEF, |
| 358 | DK_IFNOTDEF, DK_ELSEIF, DK_ELSE, DK_ENDIF, |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 359 | DK_SPACE, DK_SKIP, DK_FILE, DK_LINE, DK_LOC, DK_STABS, |
| 360 | DK_CFI_SECTIONS, DK_CFI_STARTPROC, DK_CFI_ENDPROC, DK_CFI_DEF_CFA, |
| 361 | DK_CFI_DEF_CFA_OFFSET, DK_CFI_ADJUST_CFA_OFFSET, DK_CFI_DEF_CFA_REGISTER, |
| 362 | DK_CFI_OFFSET, DK_CFI_REL_OFFSET, DK_CFI_PERSONALITY, DK_CFI_LSDA, |
| 363 | DK_CFI_REMEMBER_STATE, DK_CFI_RESTORE_STATE, DK_CFI_SAME_VALUE, |
| 364 | 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] | 365 | DK_CFI_REGISTER, DK_CFI_WINDOW_SAVE, |
Nico Weber | 155dccd1 | 2014-07-24 17:08:39 +0000 | [diff] [blame] | 366 | DK_MACROS_ON, DK_MACROS_OFF, |
| 367 | DK_MACRO, DK_EXITM, DK_ENDM, DK_ENDMACRO, DK_PURGEM, |
Saleem Abdulrasool | 88186c4 | 2013-12-18 02:53:03 +0000 | [diff] [blame] | 368 | DK_SLEB128, DK_ULEB128, |
Nico Weber | 404012b | 2014-07-24 16:26:06 +0000 | [diff] [blame] | 369 | DK_ERR, DK_ERROR, DK_WARNING, |
Saleem Abdulrasool | 88186c4 | 2013-12-18 02:53:03 +0000 | [diff] [blame] | 370 | DK_END |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 371 | }; |
| 372 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 373 | /// \brief Maps directive name --> DirectiveKind enum, for |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 374 | /// directives parsed by this class. |
| 375 | StringMap<DirectiveKind> DirectiveKindMap; |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 376 | |
| 377 | // ".ascii", ".asciz", ".string" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 378 | bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated); |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 379 | bool parseDirectiveReloc(SMLoc DirectiveLoc); // ".reloc" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 380 | bool parseDirectiveValue(unsigned Size); // ".byte", ".long", ... |
David Woodhouse | d6de0d9 | 2014-02-01 16:20:59 +0000 | [diff] [blame] | 381 | bool parseDirectiveOctaValue(); // ".octa" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 382 | bool parseDirectiveRealValue(const fltSemantics &); // ".single", ... |
| 383 | bool parseDirectiveFill(); // ".fill" |
| 384 | bool parseDirectiveZero(); // ".zero" |
Eric Christopher | a7c3273 | 2012-12-18 00:30:54 +0000 | [diff] [blame] | 385 | // ".set", ".equ", ".equiv" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 386 | bool parseDirectiveSet(StringRef IDVal, bool allow_redef); |
| 387 | bool parseDirectiveOrg(); // ".org" |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 388 | // ".align{,32}", ".p2align{,w,l}" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 389 | bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize); |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 390 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 391 | // ".file", ".line", ".loc", ".stabs" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 392 | bool parseDirectiveFile(SMLoc DirectiveLoc); |
| 393 | bool parseDirectiveLine(); |
| 394 | bool parseDirectiveLoc(); |
| 395 | bool parseDirectiveStabs(); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 396 | |
| 397 | // .cfi directives |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 398 | bool parseDirectiveCFIRegister(SMLoc DirectiveLoc); |
Venkatraman Govindaraju | 3816d43 | 2013-09-26 14:49:40 +0000 | [diff] [blame] | 399 | bool parseDirectiveCFIWindowSave(); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 400 | bool parseDirectiveCFISections(); |
| 401 | bool parseDirectiveCFIStartProc(); |
| 402 | bool parseDirectiveCFIEndProc(); |
| 403 | bool parseDirectiveCFIDefCfaOffset(); |
| 404 | bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc); |
| 405 | bool parseDirectiveCFIAdjustCfaOffset(); |
| 406 | bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc); |
| 407 | bool parseDirectiveCFIOffset(SMLoc DirectiveLoc); |
| 408 | bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc); |
| 409 | bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality); |
| 410 | bool parseDirectiveCFIRememberState(); |
| 411 | bool parseDirectiveCFIRestoreState(); |
| 412 | bool parseDirectiveCFISameValue(SMLoc DirectiveLoc); |
| 413 | bool parseDirectiveCFIRestore(SMLoc DirectiveLoc); |
| 414 | bool parseDirectiveCFIEscape(); |
| 415 | bool parseDirectiveCFISignalFrame(); |
| 416 | bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 417 | |
| 418 | // macro directives |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 419 | bool parseDirectivePurgeMacro(SMLoc DirectiveLoc); |
Nico Weber | 155dccd1 | 2014-07-24 17:08:39 +0000 | [diff] [blame] | 420 | bool parseDirectiveExitMacro(StringRef Directive); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 421 | bool parseDirectiveEndMacro(StringRef Directive); |
| 422 | bool parseDirectiveMacro(SMLoc DirectiveLoc); |
| 423 | bool parseDirectiveMacrosOnOff(StringRef Directive); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 424 | |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 425 | // ".bundle_align_mode" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 426 | bool parseDirectiveBundleAlignMode(); |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 427 | // ".bundle_lock" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 428 | bool parseDirectiveBundleLock(); |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 429 | // ".bundle_unlock" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 430 | bool parseDirectiveBundleUnlock(); |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 431 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 432 | // ".space", ".skip" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 433 | bool parseDirectiveSpace(StringRef IDVal); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 434 | |
| 435 | // .sleb128 (Signed=true) and .uleb128 (Signed=false) |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 436 | bool parseDirectiveLEB128(bool Signed); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 437 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 438 | /// \brief Parse a directive like ".globl" which |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 439 | /// accepts a single symbol (which should be a label or an external). |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 440 | bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr); |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 441 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 442 | bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm" |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 443 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 444 | bool parseDirectiveAbort(); // ".abort" |
| 445 | bool parseDirectiveInclude(); // ".include" |
| 446 | bool parseDirectiveIncbin(); // ".incbin" |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 447 | |
Saleem Abdulrasool | 763e2cb | 2014-06-18 20:57:28 +0000 | [diff] [blame] | 448 | // ".if", ".ifeq", ".ifge", ".ifgt" , ".ifle", ".iflt" or ".ifne" |
| 449 | bool parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind); |
Benjamin Kramer | 62c18b0 | 2012-05-12 11:18:42 +0000 | [diff] [blame] | 450 | // ".ifb" or ".ifnb", depending on ExpectBlank. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 451 | bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank); |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 452 | // ".ifc" or ".ifnc", depending on ExpectEqual. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 453 | bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual); |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 454 | // ".ifeqs" or ".ifnes", depending on ExpectEqual. |
| 455 | bool parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual); |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 456 | // ".ifdef" or ".ifndef", depending on expect_defined |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 457 | bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined); |
| 458 | bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif" |
| 459 | bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else" |
| 460 | bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif |
Craig Topper | 59be68f | 2014-03-08 07:14:16 +0000 | [diff] [blame] | 461 | bool parseEscapedString(std::string &Data) override; |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 462 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 463 | const MCExpr *applyModifierToExpr(const MCExpr *E, |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 464 | MCSymbolRefExpr::VariantKind Variant); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 465 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 466 | // Macro-like directives |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 467 | MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc); |
| 468 | void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc, |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 469 | raw_svector_ostream &OS); |
Saleem Abdulrasool | d743d0a | 2013-12-28 05:54:33 +0000 | [diff] [blame] | 470 | bool parseDirectiveRept(SMLoc DirectiveLoc, StringRef Directive); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 471 | bool parseDirectiveIrp(SMLoc DirectiveLoc); // ".irp" |
| 472 | bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc" |
| 473 | bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr" |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 474 | |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 475 | // "_emit" or "__emit" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 476 | bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info, |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 477 | size_t Len); |
| 478 | |
| 479 | // "align" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 480 | bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info); |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 481 | |
Saleem Abdulrasool | 88186c4 | 2013-12-18 02:53:03 +0000 | [diff] [blame] | 482 | // "end" |
| 483 | bool parseDirectiveEnd(SMLoc DirectiveLoc); |
| 484 | |
Saleem Abdulrasool | 7ecc549 | 2014-02-23 23:02:23 +0000 | [diff] [blame] | 485 | // ".err" or ".error" |
| 486 | bool parseDirectiveError(SMLoc DirectiveLoc, bool WithMessage); |
Saleem Abdulrasool | b2ae2c0 | 2014-02-23 15:53:30 +0000 | [diff] [blame] | 487 | |
Nico Weber | 404012b | 2014-07-24 16:26:06 +0000 | [diff] [blame] | 488 | // ".warning" |
| 489 | bool parseDirectiveWarning(SMLoc DirectiveLoc); |
| 490 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 491 | void initializeDirectiveKindMap(); |
Daniel Dunbar | 2a2c6cf | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 492 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 493 | } |
Daniel Dunbar | 8603340 | 2010-07-12 17:54:38 +0000 | [diff] [blame] | 494 | |
Daniel Dunbar | 0cb91cf | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 495 | namespace llvm { |
| 496 | |
| 497 | extern MCAsmParserExtension *createDarwinAsmParser(); |
Daniel Dunbar | ab058b8 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 498 | extern MCAsmParserExtension *createELFAsmParser(); |
Michael J. Spencer | c8dbdfd | 2010-10-09 11:01:07 +0000 | [diff] [blame] | 499 | extern MCAsmParserExtension *createCOFFAsmParser(); |
Daniel Dunbar | 0cb91cf | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 500 | |
| 501 | } |
| 502 | |
Chris Lattner | c35681b | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 503 | enum { DEFAULT_ADDRSPACE = 0 }; |
| 504 | |
David Blaikie | 9f380a3 | 2015-03-16 18:06:57 +0000 | [diff] [blame] | 505 | AsmParser::AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out, |
| 506 | const MCAsmInfo &MAI) |
| 507 | : Lexer(MAI), Ctx(Ctx), Out(Out), MAI(MAI), SrcMgr(SM), |
| 508 | PlatformParser(nullptr), CurBuffer(SM.getMainFileID()), |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 509 | MacrosEnabledFlag(true), HadError(false), CppHashLineNumber(0), |
Oliver Stannard | cf6bfb1 | 2014-11-03 12:19:03 +0000 | [diff] [blame] | 510 | AssemblerDialect(~0U), IsDarwin(false), ParsingInlineAsm(false) { |
Benjamin Kramer | 47f5e30 | 2011-10-16 10:48:29 +0000 | [diff] [blame] | 511 | // Save the old handler. |
| 512 | SavedDiagHandler = SrcMgr.getDiagHandler(); |
| 513 | SavedDiagContext = SrcMgr.getDiagContext(); |
| 514 | // Set our own handler which calls the saved handler. |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 515 | SrcMgr.setDiagHandler(DiagHandler, this); |
Rafael Espindola | 8026bd0 | 2014-07-06 14:17:29 +0000 | [diff] [blame] | 516 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer()); |
Daniel Dunbar | 8603340 | 2010-07-12 17:54:38 +0000 | [diff] [blame] | 517 | |
Daniel Dunbar | c501108 | 2010-07-12 18:12:02 +0000 | [diff] [blame] | 518 | // Initialize the platform / file format parser. |
Rafael Espindola | dbaf049 | 2015-08-14 15:48:41 +0000 | [diff] [blame] | 519 | switch (Ctx.getObjectFileInfo()->getObjectFileType()) { |
| 520 | case MCObjectFileInfo::IsCOFF: |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 521 | PlatformParser.reset(createCOFFAsmParser()); |
| 522 | break; |
Rafael Espindola | dbaf049 | 2015-08-14 15:48:41 +0000 | [diff] [blame] | 523 | case MCObjectFileInfo::IsMachO: |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 524 | PlatformParser.reset(createDarwinAsmParser()); |
| 525 | IsDarwin = true; |
| 526 | break; |
Rafael Espindola | dbaf049 | 2015-08-14 15:48:41 +0000 | [diff] [blame] | 527 | case MCObjectFileInfo::IsELF: |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 528 | PlatformParser.reset(createELFAsmParser()); |
| 529 | break; |
Daniel Dunbar | c501108 | 2010-07-12 18:12:02 +0000 | [diff] [blame] | 530 | } |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 531 | |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 532 | PlatformParser->Initialize(*this); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 533 | initializeDirectiveKindMap(); |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 534 | |
| 535 | NumOfMacroInstantiations = 0; |
Chris Lattner | 351a7ef | 2009-09-27 21:16:52 +0000 | [diff] [blame] | 536 | } |
| 537 | |
Daniel Dunbar | 4d7b2e3 | 2009-08-26 22:49:51 +0000 | [diff] [blame] | 538 | AsmParser::~AsmParser() { |
Saleem Abdulrasool | 6eae1e6 | 2014-05-21 17:53:18 +0000 | [diff] [blame] | 539 | assert((HadError || ActiveMacros.empty()) && |
| 540 | "Unexpected active macro instantiation!"); |
Daniel Dunbar | 4d7b2e3 | 2009-08-26 22:49:51 +0000 | [diff] [blame] | 541 | } |
| 542 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 543 | void AsmParser::printMacroInstantiations() { |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 544 | // Print the active macro instantiation stack. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 545 | for (std::vector<MacroInstantiation *>::const_reverse_iterator |
| 546 | it = ActiveMacros.rbegin(), |
| 547 | ie = ActiveMacros.rend(); |
| 548 | it != ie; ++it) |
| 549 | printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note, |
Chris Lattner | 03b80a4 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 550 | "while in macro instantiation"); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 551 | } |
| 552 | |
Saleem Abdulrasool | 69c7caf | 2014-01-07 02:28:31 +0000 | [diff] [blame] | 553 | void AsmParser::Note(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) { |
| 554 | printMessage(L, SourceMgr::DK_Note, Msg, Ranges); |
| 555 | printMacroInstantiations(); |
| 556 | } |
| 557 | |
Chris Lattner | a3a0681 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 558 | bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) { |
Colin LeMahieu | fe36f83 | 2015-07-27 22:39:14 +0000 | [diff] [blame] | 559 | if(getTargetParser().getTargetOptions().MCNoWarn) |
| 560 | return false; |
Joerg Sonnenberger | 2981591 | 2014-08-26 18:39:50 +0000 | [diff] [blame] | 561 | if (getTargetParser().getTargetOptions().MCFatalWarnings) |
Chris Lattner | a3a0681 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 562 | return Error(L, Msg, Ranges); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 563 | printMessage(L, SourceMgr::DK_Warning, Msg, Ranges); |
| 564 | printMacroInstantiations(); |
Joerg Sonnenberger | 74ba262 | 2011-05-19 18:00:13 +0000 | [diff] [blame] | 565 | return false; |
Daniel Dunbar | c9dc78a | 2009-06-30 00:49:23 +0000 | [diff] [blame] | 566 | } |
| 567 | |
Chris Lattner | a3a0681 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 568 | bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) { |
Daniel Dunbar | 43325c4 | 2010-09-09 22:42:56 +0000 | [diff] [blame] | 569 | HadError = true; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 570 | printMessage(L, SourceMgr::DK_Error, Msg, Ranges); |
| 571 | printMacroInstantiations(); |
Chris Lattner | 2adc9e7 | 2009-06-21 21:22:11 +0000 | [diff] [blame] | 572 | return true; |
| 573 | } |
| 574 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 575 | bool AsmParser::enterIncludeFile(const std::string &Filename) { |
Joerg Sonnenberger | af5f23e | 2011-06-01 13:10:15 +0000 | [diff] [blame] | 576 | std::string IncludedFile; |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 577 | unsigned NewBuf = |
| 578 | SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile); |
| 579 | if (!NewBuf) |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 580 | return true; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 581 | |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 582 | CurBuffer = NewBuf; |
Rafael Espindola | 8026bd0 | 2014-07-06 14:17:29 +0000 | [diff] [blame] | 583 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer()); |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 584 | return false; |
| 585 | } |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 586 | |
Sylvestre Ledru | 149e281 | 2013-05-14 23:36:24 +0000 | [diff] [blame] | 587 | /// 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] | 588 | /// 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] | 589 | /// returns true on failure. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 590 | bool AsmParser::processIncbinFile(const std::string &Filename) { |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 591 | std::string IncludedFile; |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 592 | unsigned NewBuf = |
| 593 | SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile); |
| 594 | if (!NewBuf) |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 595 | return true; |
| 596 | |
Kevin Enderby | ad41ab5 | 2011-12-14 22:34:45 +0000 | [diff] [blame] | 597 | // Pick up the bytes from the file and emit them. |
Rafael Espindola | 64e1af8 | 2013-07-02 15:49:13 +0000 | [diff] [blame] | 598 | getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer()); |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 599 | return false; |
| 600 | } |
| 601 | |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 602 | void AsmParser::jumpToLoc(SMLoc Loc, unsigned InBuffer) { |
| 603 | CurBuffer = InBuffer ? InBuffer : SrcMgr.FindBufferContainingLoc(Loc); |
Rafael Espindola | 8026bd0 | 2014-07-06 14:17:29 +0000 | [diff] [blame] | 604 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer(), |
| 605 | Loc.getPointer()); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 606 | } |
| 607 | |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 608 | const AsmToken &AsmParser::Lex() { |
| 609 | const AsmToken *tok = &Lexer.Lex(); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 610 | |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 611 | if (tok->is(AsmToken::Eof)) { |
| 612 | // If this is the end of an included file, pop the parent file off the |
| 613 | // include stack. |
| 614 | SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer); |
| 615 | if (ParentIncludeLoc != SMLoc()) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 616 | jumpToLoc(ParentIncludeLoc); |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 617 | tok = &Lexer.Lex(); |
| 618 | } |
| 619 | } |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 620 | |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 621 | if (tok->is(AsmToken::Error)) |
Daniel Dunbar | d8a1845 | 2010-07-18 18:31:45 +0000 | [diff] [blame] | 622 | Error(Lexer.getErrLoc(), Lexer.getErr()); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 623 | |
Sean Callanan | 7a77eae | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 624 | return *tok; |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 625 | } |
| 626 | |
Chris Lattner | 3b21e4d | 2010-04-05 23:15:42 +0000 | [diff] [blame] | 627 | bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) { |
Daniel Dunbar | 322fec6 | 2010-03-13 02:20:57 +0000 | [diff] [blame] | 628 | // Create the initial section, if requested. |
Daniel Dunbar | 322fec6 | 2010-03-13 02:20:57 +0000 | [diff] [blame] | 629 | if (!NoInitialTextSection) |
Rafael Espindola | 7b61ddf | 2014-10-15 16:12:52 +0000 | [diff] [blame] | 630 | Out.InitSections(false); |
Daniel Dunbar | 4d7b2e3 | 2009-08-26 22:49:51 +0000 | [diff] [blame] | 631 | |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 632 | // Prime the lexer. |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 633 | Lex(); |
Daniel Dunbar | 43325c4 | 2010-09-09 22:42:56 +0000 | [diff] [blame] | 634 | |
| 635 | HadError = false; |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 636 | AsmCond StartingCondState = TheCondState; |
| 637 | |
Kevin Enderby | 6469fc2 | 2011-11-01 22:27:22 +0000 | [diff] [blame] | 638 | // If we are generating dwarf for assembly source files save the initial text |
| 639 | // section and generate a .file directive. |
| 640 | if (getContext().getGenDwarfForAssembly()) { |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 641 | MCSection *Sec = getStreamer().getCurrentSection().first; |
Rafael Espindola | 2f9bdd8 | 2015-05-27 20:52:32 +0000 | [diff] [blame] | 642 | if (!Sec->getBeginSymbol()) { |
| 643 | MCSymbol *SectionStartSym = getContext().createTempSymbol(); |
| 644 | getStreamer().EmitLabel(SectionStartSym); |
| 645 | Sec->setBeginSymbol(SectionStartSym); |
| 646 | } |
Rafael Espindola | e074679 | 2015-05-21 16:52:32 +0000 | [diff] [blame] | 647 | bool InsertResult = getContext().addGenDwarfSection(Sec); |
| 648 | assert(InsertResult && ".text section should not have debug info yet"); |
Rafael Espindola | fa160c7 | 2015-05-21 17:09:22 +0000 | [diff] [blame] | 649 | (void)InsertResult; |
David Blaikie | c714ef4 | 2014-03-17 01:52:11 +0000 | [diff] [blame] | 650 | getContext().setGenDwarfFileNumber(getStreamer().EmitDwarfFileDirective( |
| 651 | 0, StringRef(), getContext().getMainFileName())); |
Kevin Enderby | 6469fc2 | 2011-11-01 22:27:22 +0000 | [diff] [blame] | 652 | } |
| 653 | |
Chris Lattner | 73f3611 | 2009-07-02 21:53:43 +0000 | [diff] [blame] | 654 | // While we have input, parse each statement. |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 655 | while (Lexer.isNot(AsmToken::Eof)) { |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 656 | ParseStatementInfo Info; |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 657 | if (!parseStatement(Info, nullptr)) |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 658 | continue; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 659 | |
Daniel Dunbar | 43325c4 | 2010-09-09 22:42:56 +0000 | [diff] [blame] | 660 | // We had an error, validate that one was emitted and recover by skipping to |
| 661 | // the next line. |
| 662 | assert(HadError && "Parse statement returned an error, but none emitted!"); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 663 | eatToEndOfStatement(); |
Chris Lattner | 73f3611 | 2009-07-02 21:53:43 +0000 | [diff] [blame] | 664 | } |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 665 | |
| 666 | if (TheCondState.TheCond != StartingCondState.TheCond || |
| 667 | TheCondState.Ignore != StartingCondState.Ignore) |
| 668 | return TokError("unmatched .ifs or .elses"); |
Kevin Enderby | e5930f1 | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 669 | |
| 670 | // Check to see there are no empty DwarfFile slots. |
David Blaikie | 8bf66c4 | 2014-04-01 07:35:52 +0000 | [diff] [blame] | 671 | const auto &LineTables = getContext().getMCDwarfLineTables(); |
| 672 | if (!LineTables.empty()) { |
| 673 | unsigned Index = 0; |
| 674 | for (const auto &File : LineTables.begin()->second.getMCDwarfFiles()) { |
| 675 | if (File.Name.empty() && Index != 0) |
| 676 | TokError("unassigned file number: " + Twine(Index) + |
| 677 | " for .file directives"); |
| 678 | ++Index; |
| 679 | } |
Kevin Enderby | e5930f1 | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 680 | } |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 681 | |
Jim Grosbach | c7e6b8f | 2011-06-15 18:33:28 +0000 | [diff] [blame] | 682 | // Check to see that all assembler local symbols were actually defined. |
| 683 | // Targets that don't do subsections via symbols may not want this, though, |
| 684 | // so conservatively exclude them. Only do this if we're finalizing, though, |
| 685 | // as otherwise we won't necessarilly have seen everything yet. |
| 686 | if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) { |
Craig Topper | 8400848 | 2015-10-10 05:38:14 +0000 | [diff] [blame] | 687 | for (const auto &TableEntry : getContext().getSymbols()) { |
| 688 | MCSymbol *Sym = TableEntry.getValue(); |
Jim Grosbach | c7e6b8f | 2011-06-15 18:33:28 +0000 | [diff] [blame] | 689 | // Variable symbols may not be marked as defined, so check those |
| 690 | // explicitly. If we know it's a variable, we have a definition for |
| 691 | // the purposes of this check. |
| 692 | if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined()) |
| 693 | // FIXME: We would really like to refer back to where the symbol was |
| 694 | // first referenced for a source location. We need to add something |
| 695 | // to track that. Currently, we just point to the end of the file. |
Jim Grosbach | 0fdd572 | 2015-10-16 22:07:59 +0000 | [diff] [blame] | 696 | return Error(getLexer().getLoc(), "assembler local symbol '" + |
| 697 | Sym->getName() + "' not defined"); |
Jim Grosbach | c7e6b8f | 2011-06-15 18:33:28 +0000 | [diff] [blame] | 698 | } |
| 699 | } |
| 700 | |
Chris Lattner | 3b21e4d | 2010-04-05 23:15:42 +0000 | [diff] [blame] | 701 | // Finalize the output stream if there are no errors and if the client wants |
| 702 | // us to. |
Jack Carter | 13d5f75 | 2013-10-04 22:52:31 +0000 | [diff] [blame] | 703 | if (!HadError && !NoFinalize) |
Daniel Dunbar | 9df5f33 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 704 | Out.Finish(); |
| 705 | |
Oliver Stannard | 07b43d3 | 2015-11-17 09:58:07 +0000 | [diff] [blame] | 706 | return HadError || getContext().hadError(); |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 707 | } |
| 708 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 709 | void AsmParser::checkForValidSection() { |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 710 | if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) { |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 711 | TokError("expected section directive before assembly directive"); |
Rafael Espindola | 7b61ddf | 2014-10-15 16:12:52 +0000 | [diff] [blame] | 712 | Out.InitSections(false); |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 713 | } |
| 714 | } |
| 715 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 716 | /// \brief Throw away the rest of the line for testing purposes. |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 717 | void AsmParser::eatToEndOfStatement() { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 718 | while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof)) |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 719 | Lex(); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 720 | |
Chris Lattner | e5074c4 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 721 | // Eat EOL. |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 722 | if (Lexer.is(AsmToken::EndOfStatement)) |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 723 | Lex(); |
Chris Lattner | e5074c4 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 724 | } |
| 725 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 726 | StringRef AsmParser::parseStringToEndOfStatement() { |
Daniel Dunbar | 40a564f | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 727 | const char *Start = getTok().getLoc().getPointer(); |
| 728 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 729 | while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof)) |
Daniel Dunbar | 40a564f | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 730 | Lex(); |
| 731 | |
| 732 | const char *End = getTok().getLoc().getPointer(); |
| 733 | return StringRef(Start, End - Start); |
| 734 | } |
Chris Lattner | 78db362 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 735 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 736 | StringRef AsmParser::parseStringToComma() { |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 737 | const char *Start = getTok().getLoc().getPointer(); |
| 738 | |
| 739 | while (Lexer.isNot(AsmToken::EndOfStatement) && |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 740 | Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof)) |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 741 | Lex(); |
| 742 | |
| 743 | const char *End = getTok().getLoc().getPointer(); |
| 744 | return StringRef(Start, End - Start); |
| 745 | } |
| 746 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 747 | /// \brief Parse a paren expression and return it. |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 748 | /// NOTE: This assumes the leading '(' has already been consumed. |
| 749 | /// |
| 750 | /// parenexpr ::= expr) |
| 751 | /// |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 752 | bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) { |
| 753 | if (parseExpression(Res)) |
| 754 | return true; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 755 | if (Lexer.isNot(AsmToken::RParen)) |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 756 | return TokError("expected ')' in parentheses expression"); |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 757 | EndLoc = Lexer.getTok().getEndLoc(); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 758 | Lex(); |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 759 | return false; |
| 760 | } |
Chris Lattner | 78db362 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 761 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 762 | /// \brief Parse a bracket expression and return it. |
Joerg Sonnenberger | afb36fa | 2011-02-24 21:59:22 +0000 | [diff] [blame] | 763 | /// NOTE: This assumes the leading '[' has already been consumed. |
| 764 | /// |
| 765 | /// bracketexpr ::= expr] |
| 766 | /// |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 767 | bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) { |
| 768 | if (parseExpression(Res)) |
| 769 | return true; |
Joerg Sonnenberger | afb36fa | 2011-02-24 21:59:22 +0000 | [diff] [blame] | 770 | if (Lexer.isNot(AsmToken::RBrac)) |
| 771 | return TokError("expected ']' in brackets expression"); |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 772 | EndLoc = Lexer.getTok().getEndLoc(); |
Joerg Sonnenberger | afb36fa | 2011-02-24 21:59:22 +0000 | [diff] [blame] | 773 | Lex(); |
| 774 | return false; |
| 775 | } |
| 776 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 777 | /// \brief Parse a primary expression and return it. |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 778 | /// primaryexpr ::= (parenexpr |
| 779 | /// primaryexpr ::= symbol |
| 780 | /// primaryexpr ::= number |
Chris Lattner | 6b55cb9 | 2010-04-14 04:40:28 +0000 | [diff] [blame] | 781 | /// primaryexpr ::= '.' |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 782 | /// primaryexpr ::= ~,+,- primaryexpr |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 783 | bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) { |
Kevin Enderby | 0017d8a | 2013-01-22 21:09:20 +0000 | [diff] [blame] | 784 | SMLoc FirstTokenLoc = getLexer().getLoc(); |
| 785 | AsmToken::TokenKind FirstTokenKind = Lexer.getKind(); |
| 786 | switch (FirstTokenKind) { |
Chris Lattner | 78db362 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 787 | default: |
| 788 | return TokError("unknown token in expression"); |
Eric Christopher | 104af06 | 2011-04-12 00:03:13 +0000 | [diff] [blame] | 789 | // If we have an error assume that we've already handled it. |
| 790 | case AsmToken::Error: |
| 791 | return true; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 792 | case AsmToken::Exclaim: |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 793 | Lex(); // Eat the operator. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 794 | if (parsePrimaryExpr(Res, EndLoc)) |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 795 | return true; |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 796 | Res = MCUnaryExpr::createLNot(Res, getContext()); |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 797 | return false; |
Daniel Dunbar | 2476432 | 2010-08-24 19:13:42 +0000 | [diff] [blame] | 798 | case AsmToken::Dollar: |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 799 | case AsmToken::At: |
Daniel Dunbar | 9ee33ca | 2009-07-31 21:55:09 +0000 | [diff] [blame] | 800 | case AsmToken::String: |
Daniel Dunbar | d20cda0 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 801 | case AsmToken::Identifier: { |
Daniel Dunbar | 2476432 | 2010-08-24 19:13:42 +0000 | [diff] [blame] | 802 | StringRef Identifier; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 803 | if (parseIdentifier(Identifier)) { |
David Majnemer | 0c58bc6 | 2013-09-25 10:47:21 +0000 | [diff] [blame] | 804 | if (FirstTokenKind == AsmToken::Dollar) { |
| 805 | if (Lexer.getMAI().getDollarIsPC()) { |
| 806 | // This is a '$' reference, which references the current PC. Emit a |
| 807 | // temporary label to the streamer and refer to it. |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 808 | MCSymbol *Sym = Ctx.createTempSymbol(); |
David Majnemer | 0c58bc6 | 2013-09-25 10:47:21 +0000 | [diff] [blame] | 809 | Out.EmitLabel(Sym); |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 810 | Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, |
Jack Carter | 721726a | 2013-10-04 21:26:15 +0000 | [diff] [blame] | 811 | getContext()); |
David Majnemer | 0c58bc6 | 2013-09-25 10:47:21 +0000 | [diff] [blame] | 812 | EndLoc = FirstTokenLoc; |
| 813 | return false; |
Ted Kremenek | 297febe | 2014-03-06 22:13:17 +0000 | [diff] [blame] | 814 | } |
| 815 | return Error(FirstTokenLoc, "invalid token in expression"); |
David Majnemer | 0c58bc6 | 2013-09-25 10:47:21 +0000 | [diff] [blame] | 816 | } |
Kevin Enderby | 0017d8a | 2013-01-22 21:09:20 +0000 | [diff] [blame] | 817 | } |
David Peixotto | 8ad70b3 | 2013-12-04 22:43:20 +0000 | [diff] [blame] | 818 | // Parse symbol variant |
| 819 | std::pair<StringRef, StringRef> Split; |
| 820 | if (!MAI.useParensForSymbolVariant()) { |
David Majnemer | 6a5b812 | 2014-06-19 01:25:43 +0000 | [diff] [blame] | 821 | if (FirstTokenKind == AsmToken::String) { |
| 822 | if (Lexer.is(AsmToken::At)) { |
| 823 | Lexer.Lex(); // eat @ |
| 824 | SMLoc AtLoc = getLexer().getLoc(); |
| 825 | StringRef VName; |
| 826 | if (parseIdentifier(VName)) |
| 827 | return Error(AtLoc, "expected symbol variant after '@'"); |
| 828 | |
| 829 | Split = std::make_pair(Identifier, VName); |
| 830 | } |
| 831 | } else { |
| 832 | Split = Identifier.split('@'); |
| 833 | } |
David Peixotto | 8ad70b3 | 2013-12-04 22:43:20 +0000 | [diff] [blame] | 834 | } else if (Lexer.is(AsmToken::LParen)) { |
| 835 | Lexer.Lex(); // eat ( |
| 836 | StringRef VName; |
| 837 | parseIdentifier(VName); |
| 838 | if (Lexer.isNot(AsmToken::RParen)) { |
| 839 | return Error(Lexer.getTok().getLoc(), |
| 840 | "unexpected token in variant, expected ')'"); |
| 841 | } |
| 842 | Lexer.Lex(); // eat ) |
| 843 | Split = std::make_pair(Identifier, VName); |
| 844 | } |
Daniel Dunbar | 2476432 | 2010-08-24 19:13:42 +0000 | [diff] [blame] | 845 | |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 846 | EndLoc = SMLoc::getFromPointer(Identifier.end()); |
| 847 | |
Daniel Dunbar | d20cda0 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 848 | // This is a symbol reference. |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 849 | StringRef SymbolName = Identifier; |
| 850 | MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None; |
Hans Wennborg | 69918bc | 2013-10-17 01:13:02 +0000 | [diff] [blame] | 851 | |
Hans Wennborg | 7ddcdc8 | 2013-10-18 02:14:40 +0000 | [diff] [blame] | 852 | // Lookup the symbol variant if used. |
David Peixotto | 8ad70b3 | 2013-12-04 22:43:20 +0000 | [diff] [blame] | 853 | if (Split.second.size()) { |
Hans Wennborg | 7ddcdc8 | 2013-10-18 02:14:40 +0000 | [diff] [blame] | 854 | Variant = MCSymbolRefExpr::getVariantKindForName(Split.second); |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 855 | if (Variant != MCSymbolRefExpr::VK_Invalid) { |
| 856 | SymbolName = Split.first; |
David Peixotto | 8ad70b3 | 2013-12-04 22:43:20 +0000 | [diff] [blame] | 857 | } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) { |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 858 | Variant = MCSymbolRefExpr::VK_None; |
| 859 | } else { |
Saleem Abdulrasool | a25e1e4 | 2014-01-26 22:29:43 +0000 | [diff] [blame] | 860 | return Error(SMLoc::getFromPointer(Split.second.begin()), |
| 861 | "invalid variant '" + Split.second + "'"); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 862 | } |
| 863 | } |
Daniel Dunbar | 5599256 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 864 | |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 865 | MCSymbol *Sym = getContext().getOrCreateSymbol(SymbolName); |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 866 | |
Daniel Dunbar | d20cda0 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 867 | // If this is an absolute variable reference, substitute it now to preserve |
| 868 | // semantics in the face of reassignment. |
Vedant Kumar | 86dbd92 | 2015-08-31 17:44:53 +0000 | [diff] [blame] | 869 | if (Sym->isVariable() && |
| 870 | isa<MCConstantExpr>(Sym->getVariableValue(/*SetUsed*/ false))) { |
Daniel Dunbar | 5599256 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 871 | if (Variant) |
Daniel Dunbar | 8a3c3f2 | 2010-11-08 17:53:02 +0000 | [diff] [blame] | 872 | return Error(EndLoc, "unexpected modifier on variable reference"); |
Daniel Dunbar | 5599256 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 873 | |
Vedant Kumar | 86dbd92 | 2015-08-31 17:44:53 +0000 | [diff] [blame] | 874 | Res = Sym->getVariableValue(/*SetUsed*/ false); |
Daniel Dunbar | d20cda0 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 875 | return false; |
| 876 | } |
| 877 | |
| 878 | // Otherwise create a symbol ref. |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 879 | Res = MCSymbolRefExpr::create(Sym, Variant, getContext()); |
Chris Lattner | 78db362 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 880 | return false; |
Daniel Dunbar | d20cda0 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 881 | } |
David Woodhouse | f42a666 | 2014-02-01 16:20:54 +0000 | [diff] [blame] | 882 | case AsmToken::BigNum: |
| 883 | return TokError("literal value out of range for directive"); |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 884 | case AsmToken::Integer: { |
| 885 | SMLoc Loc = getTok().getLoc(); |
| 886 | int64_t IntVal = getTok().getIntVal(); |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 887 | Res = MCConstantExpr::create(IntVal, getContext()); |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 888 | EndLoc = Lexer.getTok().getEndLoc(); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 889 | Lex(); // Eat token. |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 890 | // Look for 'b' or 'f' following an Integer as a directional label |
| 891 | if (Lexer.getKind() == AsmToken::Identifier) { |
| 892 | StringRef IDVal = getTok().getString(); |
Ulrich Weigand | d412098 | 2013-06-20 16:24:17 +0000 | [diff] [blame] | 893 | // Lookup the symbol variant if used. |
| 894 | std::pair<StringRef, StringRef> Split = IDVal.split('@'); |
| 895 | MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None; |
| 896 | if (Split.first.size() != IDVal.size()) { |
| 897 | Variant = MCSymbolRefExpr::getVariantKindForName(Split.second); |
Arnaud A. de Grandmaison | c97727a | 2014-03-21 21:54:46 +0000 | [diff] [blame] | 898 | if (Variant == MCSymbolRefExpr::VK_Invalid) |
Ulrich Weigand | d412098 | 2013-06-20 16:24:17 +0000 | [diff] [blame] | 899 | return TokError("invalid variant '" + Split.second + "'"); |
Vladimir Medic | 9bad0d33 | 2013-08-20 13:33:18 +0000 | [diff] [blame] | 900 | IDVal = Split.first; |
Ulrich Weigand | d412098 | 2013-06-20 16:24:17 +0000 | [diff] [blame] | 901 | } |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 902 | if (IDVal == "f" || IDVal == "b") { |
| 903 | MCSymbol *Sym = |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 904 | Ctx.getDirectionalLocalSymbol(IntVal, IDVal == "b"); |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 905 | Res = MCSymbolRefExpr::create(Sym, Variant, getContext()); |
Benjamin Kramer | c7eda3e | 2012-05-12 16:52:21 +0000 | [diff] [blame] | 906 | if (IDVal == "b" && Sym->isUndefined()) |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 907 | return Error(Loc, "invalid reference to undefined symbol"); |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 908 | EndLoc = Lexer.getTok().getEndLoc(); |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 909 | Lex(); // Eat identifier. |
| 910 | } |
| 911 | } |
Chris Lattner | 78db362 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 912 | return false; |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 913 | } |
Bill Wendling | cdbf17b | 2011-01-25 21:26:41 +0000 | [diff] [blame] | 914 | case AsmToken::Real: { |
| 915 | APFloat RealVal(APFloat::IEEEdouble, getTok().getString()); |
Bob Wilson | 813bdf6 | 2011-02-03 23:17:47 +0000 | [diff] [blame] | 916 | uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue(); |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 917 | Res = MCConstantExpr::create(IntVal, getContext()); |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 918 | EndLoc = Lexer.getTok().getEndLoc(); |
Bill Wendling | cdbf17b | 2011-01-25 21:26:41 +0000 | [diff] [blame] | 919 | Lex(); // Eat token. |
| 920 | return false; |
| 921 | } |
Chris Lattner | 6b55cb9 | 2010-04-14 04:40:28 +0000 | [diff] [blame] | 922 | case AsmToken::Dot: { |
| 923 | // This is a '.' reference, which references the current PC. Emit a |
| 924 | // temporary label to the streamer and refer to it. |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 925 | MCSymbol *Sym = Ctx.createTempSymbol(); |
Chris Lattner | 6b55cb9 | 2010-04-14 04:40:28 +0000 | [diff] [blame] | 926 | Out.EmitLabel(Sym); |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 927 | Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext()); |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 928 | EndLoc = Lexer.getTok().getEndLoc(); |
Chris Lattner | 6b55cb9 | 2010-04-14 04:40:28 +0000 | [diff] [blame] | 929 | Lex(); // Eat identifier. |
| 930 | return false; |
| 931 | } |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 932 | case AsmToken::LParen: |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 933 | Lex(); // Eat the '('. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 934 | return parseParenExpr(Res, EndLoc); |
Joerg Sonnenberger | afb36fa | 2011-02-24 21:59:22 +0000 | [diff] [blame] | 935 | case AsmToken::LBrac: |
| 936 | if (!PlatformParser->HasBracketExpressions()) |
| 937 | return TokError("brackets expression not supported on this target"); |
| 938 | Lex(); // Eat the '['. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 939 | return parseBracketExpr(Res, EndLoc); |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 940 | case AsmToken::Minus: |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 941 | Lex(); // Eat the operator. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 942 | if (parsePrimaryExpr(Res, EndLoc)) |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 943 | return true; |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 944 | Res = MCUnaryExpr::createMinus(Res, getContext()); |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 945 | return false; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 946 | case AsmToken::Plus: |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 947 | Lex(); // Eat the operator. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 948 | if (parsePrimaryExpr(Res, EndLoc)) |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 949 | return true; |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 950 | Res = MCUnaryExpr::createPlus(Res, getContext()); |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 951 | return false; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 952 | case AsmToken::Tilde: |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 953 | Lex(); // Eat the operator. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 954 | if (parsePrimaryExpr(Res, EndLoc)) |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 955 | return true; |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 956 | Res = MCUnaryExpr::createNot(Res, getContext()); |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 957 | return false; |
Chris Lattner | 78db362 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 958 | } |
| 959 | } |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 960 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 961 | bool AsmParser::parseExpression(const MCExpr *&Res) { |
Chris Lattner | e17df0b | 2010-01-15 19:39:23 +0000 | [diff] [blame] | 962 | SMLoc EndLoc; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 963 | return parseExpression(Res, EndLoc); |
Chris Lattner | 528d00b | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 964 | } |
| 965 | |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 966 | const MCExpr * |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 967 | AsmParser::applyModifierToExpr(const MCExpr *E, |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 968 | MCSymbolRefExpr::VariantKind Variant) { |
Joerg Sonnenberger | b822af4 | 2013-08-27 20:23:19 +0000 | [diff] [blame] | 969 | // Ask the target implementation about this expression first. |
| 970 | const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx); |
| 971 | if (NewE) |
| 972 | return NewE; |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 973 | // Recurse over the given expression, rebuilding it to apply the given variant |
| 974 | // if there is exactly one symbol. |
| 975 | switch (E->getKind()) { |
| 976 | case MCExpr::Target: |
| 977 | case MCExpr::Constant: |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 978 | return nullptr; |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 979 | |
| 980 | case MCExpr::SymbolRef: { |
| 981 | const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E); |
| 982 | |
| 983 | if (SRE->getKind() != MCSymbolRefExpr::VK_None) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 984 | TokError("invalid variant on expression '" + getTok().getIdentifier() + |
| 985 | "' (already modified)"); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 986 | return E; |
| 987 | } |
| 988 | |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 989 | return MCSymbolRefExpr::create(&SRE->getSymbol(), Variant, getContext()); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | case MCExpr::Unary: { |
| 993 | const MCUnaryExpr *UE = cast<MCUnaryExpr>(E); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 994 | const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 995 | if (!Sub) |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 996 | return nullptr; |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 997 | return MCUnaryExpr::create(UE->getOpcode(), Sub, getContext()); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | case MCExpr::Binary: { |
| 1001 | const MCBinaryExpr *BE = cast<MCBinaryExpr>(E); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1002 | const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant); |
| 1003 | const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1004 | |
| 1005 | if (!LHS && !RHS) |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 1006 | return nullptr; |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1007 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1008 | if (!LHS) |
| 1009 | LHS = BE->getLHS(); |
| 1010 | if (!RHS) |
| 1011 | RHS = BE->getRHS(); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1012 | |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1013 | return MCBinaryExpr::create(BE->getOpcode(), LHS, RHS, getContext()); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1014 | } |
| 1015 | } |
Daniel Dunbar | baad46c | 2010-09-17 16:34:24 +0000 | [diff] [blame] | 1016 | |
Craig Topper | a2886c2 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 1017 | llvm_unreachable("Invalid expression kind!"); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1020 | /// \brief Parse an expression and return it. |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1021 | /// |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1022 | /// expr ::= expr &&,|| expr -> lowest. |
| 1023 | /// expr ::= expr |,^,&,! expr |
| 1024 | /// expr ::= expr ==,!=,<>,<,<=,>,>= expr |
| 1025 | /// expr ::= expr <<,>> expr |
| 1026 | /// expr ::= expr +,- expr |
| 1027 | /// expr ::= expr *,/,% expr -> highest. |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 1028 | /// expr ::= primaryexpr |
| 1029 | /// |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1030 | bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) { |
Daniel Dunbar | d0c6d36 | 2010-02-13 01:28:07 +0000 | [diff] [blame] | 1031 | // Parse the expression. |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 1032 | Res = nullptr; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1033 | if (parsePrimaryExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc)) |
Daniel Dunbar | d0c6d36 | 2010-02-13 01:28:07 +0000 | [diff] [blame] | 1034 | return true; |
| 1035 | |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1036 | // As a special case, we support 'a op b @ modifier' by rewriting the |
| 1037 | // expression to include the modifier. This is inefficient, but in general we |
| 1038 | // expect users to use 'a@modifier op b'. |
| 1039 | if (Lexer.getKind() == AsmToken::At) { |
| 1040 | Lex(); |
| 1041 | |
| 1042 | if (Lexer.isNot(AsmToken::Identifier)) |
| 1043 | return TokError("unexpected symbol modifier following '@'"); |
| 1044 | |
| 1045 | MCSymbolRefExpr::VariantKind Variant = |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1046 | MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier()); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1047 | if (Variant == MCSymbolRefExpr::VK_Invalid) |
| 1048 | return TokError("invalid variant '" + getTok().getIdentifier() + "'"); |
| 1049 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1050 | const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1051 | if (!ModifiedRes) { |
| 1052 | return TokError("invalid modifier '" + getTok().getIdentifier() + |
| 1053 | "' (no symbols present)"); |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1054 | } |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1055 | |
Daniel Dunbar | 55f1667 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 1056 | Res = ModifiedRes; |
| 1057 | Lex(); |
| 1058 | } |
| 1059 | |
Daniel Dunbar | d0c6d36 | 2010-02-13 01:28:07 +0000 | [diff] [blame] | 1060 | // Try to constant fold it up front, if possible. |
| 1061 | int64_t Value; |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1062 | if (Res->evaluateAsAbsolute(Value)) |
| 1063 | Res = MCConstantExpr::create(Value, getContext()); |
Daniel Dunbar | d0c6d36 | 2010-02-13 01:28:07 +0000 | [diff] [blame] | 1064 | |
| 1065 | return false; |
Chris Lattner | 7fdbce7 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 1066 | } |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1067 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1068 | bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) { |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 1069 | Res = nullptr; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1070 | return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc); |
Daniel Dunbar | 7c82d56 | 2009-08-31 08:08:17 +0000 | [diff] [blame] | 1071 | } |
| 1072 | |
Toma Tabacu | 7bc44dc | 2015-06-25 09:52:02 +0000 | [diff] [blame] | 1073 | bool AsmParser::parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res, |
| 1074 | SMLoc &EndLoc) { |
| 1075 | if (parseParenExpr(Res, EndLoc)) |
| 1076 | return true; |
| 1077 | |
| 1078 | for (; ParenDepth > 0; --ParenDepth) { |
| 1079 | if (parseBinOpRHS(1, Res, EndLoc)) |
| 1080 | return true; |
| 1081 | |
| 1082 | // We don't Lex() the last RParen. |
| 1083 | // This is the same behavior as parseParenExpression(). |
| 1084 | if (ParenDepth - 1 > 0) { |
| 1085 | if (Lexer.isNot(AsmToken::RParen)) |
| 1086 | return TokError("expected ')' in parentheses expression"); |
| 1087 | EndLoc = Lexer.getTok().getEndLoc(); |
| 1088 | Lex(); |
| 1089 | } |
| 1090 | } |
| 1091 | return false; |
| 1092 | } |
| 1093 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1094 | bool AsmParser::parseAbsoluteExpression(int64_t &Res) { |
Daniel Dunbar | f363645 | 2009-08-31 08:07:22 +0000 | [diff] [blame] | 1095 | const MCExpr *Expr; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1096 | |
Daniel Dunbar | 75630b3 | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 1097 | SMLoc StartLoc = Lexer.getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1098 | if (parseExpression(Expr)) |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1099 | return true; |
| 1100 | |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1101 | if (!Expr->evaluateAsAbsolute(Res)) |
Daniel Dunbar | 75630b3 | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 1102 | return Error(StartLoc, "expected absolute expression"); |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1103 | |
| 1104 | return false; |
| 1105 | } |
| 1106 | |
David Majnemer | 0993e0b | 2015-10-26 03:15:34 +0000 | [diff] [blame] | 1107 | static unsigned getDarwinBinOpPrecedence(AsmToken::TokenKind K, |
| 1108 | MCBinaryExpr::Opcode &Kind, |
| 1109 | bool ShouldUseLogicalShr) { |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1110 | switch (K) { |
Daniel Dunbar | 940cda2 | 2009-08-31 08:07:44 +0000 | [diff] [blame] | 1111 | default: |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1112 | return 0; // not a binop. |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1113 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1114 | // Lowest Precedence: &&, || |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1115 | case AsmToken::AmpAmp: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1116 | Kind = MCBinaryExpr::LAnd; |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1117 | return 1; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1118 | case AsmToken::PipePipe: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1119 | Kind = MCBinaryExpr::LOr; |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1120 | return 1; |
| 1121 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1122 | // Low Precedence: |, &, ^ |
| 1123 | // |
| 1124 | // FIXME: gas seems to support '!' as an infix operator? |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1125 | case AsmToken::Pipe: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1126 | Kind = MCBinaryExpr::Or; |
Chris Lattner | 2bb9504d | 2010-09-22 05:05:16 +0000 | [diff] [blame] | 1127 | return 2; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1128 | case AsmToken::Caret: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1129 | Kind = MCBinaryExpr::Xor; |
Chris Lattner | 2bb9504d | 2010-09-22 05:05:16 +0000 | [diff] [blame] | 1130 | return 2; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1131 | case AsmToken::Amp: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1132 | Kind = MCBinaryExpr::And; |
Chris Lattner | 2bb9504d | 2010-09-22 05:05:16 +0000 | [diff] [blame] | 1133 | return 2; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1134 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1135 | // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >= |
Chris Lattner | 2bb9504d | 2010-09-22 05:05:16 +0000 | [diff] [blame] | 1136 | case AsmToken::EqualEqual: |
| 1137 | Kind = MCBinaryExpr::EQ; |
| 1138 | return 3; |
| 1139 | case AsmToken::ExclaimEqual: |
| 1140 | case AsmToken::LessGreater: |
| 1141 | Kind = MCBinaryExpr::NE; |
| 1142 | return 3; |
| 1143 | case AsmToken::Less: |
| 1144 | Kind = MCBinaryExpr::LT; |
| 1145 | return 3; |
| 1146 | case AsmToken::LessEqual: |
| 1147 | Kind = MCBinaryExpr::LTE; |
| 1148 | return 3; |
| 1149 | case AsmToken::Greater: |
| 1150 | Kind = MCBinaryExpr::GT; |
| 1151 | return 3; |
| 1152 | case AsmToken::GreaterEqual: |
| 1153 | Kind = MCBinaryExpr::GTE; |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1154 | return 3; |
| 1155 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1156 | // Intermediate Precedence: <<, >> |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1157 | case AsmToken::LessLess: |
| 1158 | Kind = MCBinaryExpr::Shl; |
| 1159 | return 4; |
| 1160 | case AsmToken::GreaterGreater: |
David Majnemer | 0993e0b | 2015-10-26 03:15:34 +0000 | [diff] [blame] | 1161 | Kind = ShouldUseLogicalShr ? MCBinaryExpr::LShr : MCBinaryExpr::AShr; |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1162 | return 4; |
| 1163 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1164 | // High Intermediate Precedence: +, - |
Daniel Dunbar | b3a48f3 | 2010-10-25 20:18:56 +0000 | [diff] [blame] | 1165 | case AsmToken::Plus: |
| 1166 | Kind = MCBinaryExpr::Add; |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1167 | return 5; |
Daniel Dunbar | b3a48f3 | 2010-10-25 20:18:56 +0000 | [diff] [blame] | 1168 | case AsmToken::Minus: |
| 1169 | Kind = MCBinaryExpr::Sub; |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1170 | return 5; |
Daniel Dunbar | b3a48f3 | 2010-10-25 20:18:56 +0000 | [diff] [blame] | 1171 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1172 | // Highest Precedence: *, /, % |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1173 | case AsmToken::Star: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1174 | Kind = MCBinaryExpr::Mul; |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1175 | return 6; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1176 | case AsmToken::Slash: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1177 | Kind = MCBinaryExpr::Div; |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1178 | return 6; |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1179 | case AsmToken::Percent: |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1180 | Kind = MCBinaryExpr::Mod; |
Jim Grosbach | bd16424 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1181 | return 6; |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1182 | } |
| 1183 | } |
| 1184 | |
David Majnemer | 0993e0b | 2015-10-26 03:15:34 +0000 | [diff] [blame] | 1185 | static unsigned getGNUBinOpPrecedence(AsmToken::TokenKind K, |
| 1186 | MCBinaryExpr::Opcode &Kind, |
| 1187 | bool ShouldUseLogicalShr) { |
| 1188 | switch (K) { |
| 1189 | default: |
| 1190 | return 0; // not a binop. |
| 1191 | |
| 1192 | // Lowest Precedence: &&, || |
| 1193 | case AsmToken::AmpAmp: |
| 1194 | Kind = MCBinaryExpr::LAnd; |
| 1195 | return 2; |
| 1196 | case AsmToken::PipePipe: |
| 1197 | Kind = MCBinaryExpr::LOr; |
| 1198 | return 1; |
| 1199 | |
| 1200 | // Low Precedence: ==, !=, <>, <, <=, >, >= |
| 1201 | case AsmToken::EqualEqual: |
| 1202 | Kind = MCBinaryExpr::EQ; |
| 1203 | return 3; |
| 1204 | case AsmToken::ExclaimEqual: |
| 1205 | case AsmToken::LessGreater: |
| 1206 | Kind = MCBinaryExpr::NE; |
| 1207 | return 3; |
| 1208 | case AsmToken::Less: |
| 1209 | Kind = MCBinaryExpr::LT; |
| 1210 | return 3; |
| 1211 | case AsmToken::LessEqual: |
| 1212 | Kind = MCBinaryExpr::LTE; |
| 1213 | return 3; |
| 1214 | case AsmToken::Greater: |
| 1215 | Kind = MCBinaryExpr::GT; |
| 1216 | return 3; |
| 1217 | case AsmToken::GreaterEqual: |
| 1218 | Kind = MCBinaryExpr::GTE; |
| 1219 | return 3; |
| 1220 | |
| 1221 | // Low Intermediate Precedence: +, - |
| 1222 | case AsmToken::Plus: |
| 1223 | Kind = MCBinaryExpr::Add; |
| 1224 | return 4; |
| 1225 | case AsmToken::Minus: |
| 1226 | Kind = MCBinaryExpr::Sub; |
| 1227 | return 4; |
| 1228 | |
| 1229 | // High Intermediate Precedence: |, &, ^ |
| 1230 | // |
| 1231 | // FIXME: gas seems to support '!' as an infix operator? |
| 1232 | case AsmToken::Pipe: |
| 1233 | Kind = MCBinaryExpr::Or; |
| 1234 | return 5; |
| 1235 | case AsmToken::Caret: |
| 1236 | Kind = MCBinaryExpr::Xor; |
| 1237 | return 5; |
| 1238 | case AsmToken::Amp: |
| 1239 | Kind = MCBinaryExpr::And; |
| 1240 | return 5; |
| 1241 | |
| 1242 | // Highest Precedence: *, /, %, <<, >> |
| 1243 | case AsmToken::Star: |
| 1244 | Kind = MCBinaryExpr::Mul; |
| 1245 | return 6; |
| 1246 | case AsmToken::Slash: |
| 1247 | Kind = MCBinaryExpr::Div; |
| 1248 | return 6; |
| 1249 | case AsmToken::Percent: |
| 1250 | Kind = MCBinaryExpr::Mod; |
| 1251 | return 6; |
| 1252 | case AsmToken::LessLess: |
| 1253 | Kind = MCBinaryExpr::Shl; |
| 1254 | return 6; |
| 1255 | case AsmToken::GreaterGreater: |
| 1256 | Kind = ShouldUseLogicalShr ? MCBinaryExpr::LShr : MCBinaryExpr::AShr; |
| 1257 | return 6; |
| 1258 | } |
| 1259 | } |
| 1260 | |
| 1261 | unsigned AsmParser::getBinOpPrecedence(AsmToken::TokenKind K, |
| 1262 | MCBinaryExpr::Opcode &Kind) { |
| 1263 | bool ShouldUseLogicalShr = MAI.shouldUseLogicalShr(); |
| 1264 | return IsDarwin ? getDarwinBinOpPrecedence(K, Kind, ShouldUseLogicalShr) |
| 1265 | : getGNUBinOpPrecedence(K, Kind, ShouldUseLogicalShr); |
| 1266 | } |
| 1267 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1268 | /// \brief Parse all binary operators with precedence >= 'Precedence'. |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1269 | /// Res contains the LHS of the expression on input. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1270 | bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, |
Chris Lattner | 528d00b | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 1271 | SMLoc &EndLoc) { |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1272 | while (1) { |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1273 | MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add; |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1274 | unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1275 | |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1276 | // If the next token is lower precedence than we are allowed to eat, return |
| 1277 | // successfully with what we ate already. |
| 1278 | if (TokPrec < Precedence) |
| 1279 | return false; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1280 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1281 | Lex(); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1282 | |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1283 | // Eat the next primary expression. |
Daniel Dunbar | f363645 | 2009-08-31 08:07:22 +0000 | [diff] [blame] | 1284 | const MCExpr *RHS; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1285 | if (parsePrimaryExpr(RHS, EndLoc)) |
| 1286 | return true; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1287 | |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1288 | // If BinOp binds less tightly with RHS than the operator after RHS, let |
| 1289 | // the pending operator take RHS as its LHS. |
Daniel Dunbar | 115e4d6 | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1290 | MCBinaryExpr::Opcode Dummy; |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1291 | unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1292 | if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc)) |
| 1293 | return true; |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1294 | |
Daniel Dunbar | 7e8d6c7 | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1295 | // Merge LHS and RHS according to operator. |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 1296 | Res = MCBinaryExpr::create(Kind, Res, RHS, getContext()); |
Chris Lattner | f97d8bb | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1297 | } |
| 1298 | } |
| 1299 | |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1300 | /// ParseStatement: |
| 1301 | /// ::= EndOfStatement |
Chris Lattner | e5074c4 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 1302 | /// ::= Label* Directive ...Operands... EndOfStatement |
| 1303 | /// ::= Label* Identifier OperandList* EndOfStatement |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 1304 | bool AsmParser::parseStatement(ParseStatementInfo &Info, |
| 1305 | MCAsmParserSemaCallback *SI) { |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1306 | if (Lexer.is(AsmToken::EndOfStatement)) { |
Daniel Dunbar | 8271d1bb | 2010-05-23 18:36:34 +0000 | [diff] [blame] | 1307 | Out.AddBlankLine(); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1308 | Lex(); |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1309 | return false; |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1310 | } |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1311 | |
Kevin Enderby | fa3c6f1 | 2010-12-24 00:12:02 +0000 | [diff] [blame] | 1312 | // Statements always start with an identifier or are a full line comment. |
Sean Callanan | 936b0d3 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 1313 | AsmToken ID = getTok(); |
Daniel Dunbar | ee4465c | 2009-07-28 16:38:40 +0000 | [diff] [blame] | 1314 | SMLoc IDLoc = ID.getLoc(); |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1315 | StringRef IDVal; |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1316 | int64_t LocalLabelVal = -1; |
Kevin Enderby | fa3c6f1 | 2010-12-24 00:12:02 +0000 | [diff] [blame] | 1317 | // A full line comment is a '#' as the first token. |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1318 | if (Lexer.is(AsmToken::Hash)) |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1319 | return parseCppHashLineFilenameComment(IDLoc); |
Daniel Dunbar | 3f56104 | 2011-03-25 17:47:14 +0000 | [diff] [blame] | 1320 | |
Kevin Enderby | fa3c6f1 | 2010-12-24 00:12:02 +0000 | [diff] [blame] | 1321 | // Allow an integer followed by a ':' as a directional local label. |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1322 | if (Lexer.is(AsmToken::Integer)) { |
| 1323 | LocalLabelVal = getTok().getIntVal(); |
| 1324 | if (LocalLabelVal < 0) { |
| 1325 | if (!TheCondState.Ignore) |
| 1326 | return TokError("unexpected token at start of statement"); |
| 1327 | IDVal = ""; |
Eli Bendersky | 8802471 | 2013-01-16 19:32:36 +0000 | [diff] [blame] | 1328 | } else { |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1329 | IDVal = getTok().getString(); |
| 1330 | Lex(); // Consume the integer token to be used as an identifier token. |
| 1331 | if (Lexer.getKind() != AsmToken::Colon) { |
Duncan Sands | 41b4a6b | 2010-07-12 08:16:59 +0000 | [diff] [blame] | 1332 | if (!TheCondState.Ignore) |
| 1333 | return TokError("unexpected token at start of statement"); |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1334 | } |
| 1335 | } |
Daniel Dunbar | 6f4c942 | 2011-03-25 17:47:17 +0000 | [diff] [blame] | 1336 | } else if (Lexer.is(AsmToken::Dot)) { |
| 1337 | // Treat '.' as a valid identifier in this context. |
| 1338 | Lex(); |
| 1339 | IDVal = "."; |
Colin LeMahieu | 8a0453e | 2015-11-09 00:31:07 +0000 | [diff] [blame] | 1340 | } else if (Lexer.is(AsmToken::LCurly)) { |
| 1341 | // Treat '{' as a valid identifier in this context. |
| 1342 | Lex(); |
| 1343 | IDVal = "{"; |
| 1344 | |
| 1345 | } else if (Lexer.is(AsmToken::RCurly)) { |
| 1346 | // Treat '}' as a valid identifier in this context. |
| 1347 | Lex(); |
| 1348 | IDVal = "}"; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1349 | } else if (parseIdentifier(IDVal)) { |
Chris Lattner | 926885c | 2010-04-17 18:14:27 +0000 | [diff] [blame] | 1350 | if (!TheCondState.Ignore) |
| 1351 | return TokError("unexpected token at start of statement"); |
| 1352 | IDVal = ""; |
| 1353 | } |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1354 | |
Chris Lattner | 926885c | 2010-04-17 18:14:27 +0000 | [diff] [blame] | 1355 | // Handle conditional assembly here before checking for skipping. We |
| 1356 | // have to do this so that .endif isn't skipped in a ".if 0" block for |
| 1357 | // example. |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1358 | StringMap<DirectiveKind>::const_iterator DirKindIt = |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1359 | DirectiveKindMap.find(IDVal); |
| 1360 | DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end()) |
| 1361 | ? DK_NO_DIRECTIVE |
| 1362 | : DirKindIt->getValue(); |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1363 | switch (DirKind) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1364 | default: |
| 1365 | break; |
| 1366 | case DK_IF: |
Saleem Abdulrasool | 763e2cb | 2014-06-18 20:57:28 +0000 | [diff] [blame] | 1367 | case DK_IFEQ: |
| 1368 | case DK_IFGE: |
| 1369 | case DK_IFGT: |
| 1370 | case DK_IFLE: |
| 1371 | case DK_IFLT: |
Saleem Abdulrasool | 5852d6b | 2014-02-23 15:53:41 +0000 | [diff] [blame] | 1372 | case DK_IFNE: |
Saleem Abdulrasool | 763e2cb | 2014-06-18 20:57:28 +0000 | [diff] [blame] | 1373 | return parseDirectiveIf(IDLoc, DirKind); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1374 | case DK_IFB: |
| 1375 | return parseDirectiveIfb(IDLoc, true); |
| 1376 | case DK_IFNB: |
| 1377 | return parseDirectiveIfb(IDLoc, false); |
| 1378 | case DK_IFC: |
| 1379 | return parseDirectiveIfc(IDLoc, true); |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 1380 | case DK_IFEQS: |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 1381 | return parseDirectiveIfeqs(IDLoc, true); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1382 | case DK_IFNC: |
| 1383 | return parseDirectiveIfc(IDLoc, false); |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 1384 | case DK_IFNES: |
| 1385 | return parseDirectiveIfeqs(IDLoc, false); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1386 | case DK_IFDEF: |
| 1387 | return parseDirectiveIfdef(IDLoc, true); |
| 1388 | case DK_IFNDEF: |
| 1389 | case DK_IFNOTDEF: |
| 1390 | return parseDirectiveIfdef(IDLoc, false); |
| 1391 | case DK_ELSEIF: |
| 1392 | return parseDirectiveElseIf(IDLoc); |
| 1393 | case DK_ELSE: |
| 1394 | return parseDirectiveElse(IDLoc); |
| 1395 | case DK_ENDIF: |
| 1396 | return parseDirectiveEndIf(IDLoc); |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1397 | } |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1398 | |
Eli Bendersky | 8802471 | 2013-01-16 19:32:36 +0000 | [diff] [blame] | 1399 | // Ignore the statement if in the middle of inactive conditional |
| 1400 | // (e.g. ".if 0"). |
Chad Rosier | eda70b3 | 2012-10-20 00:47:08 +0000 | [diff] [blame] | 1401 | if (TheCondState.Ignore) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1402 | eatToEndOfStatement(); |
Chris Lattner | 926885c | 2010-04-17 18:14:27 +0000 | [diff] [blame] | 1403 | return false; |
| 1404 | } |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1405 | |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1406 | // FIXME: Recurse on local labels? |
| 1407 | |
| 1408 | // See what kind of statement we have. |
| 1409 | switch (Lexer.getKind()) { |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1410 | case AsmToken::Colon: { |
Colin LeMahieu | 7820dff | 2015-11-09 00:15:45 +0000 | [diff] [blame] | 1411 | if (!getTargetParser().isLabel(ID)) |
| 1412 | break; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1413 | checkForValidSection(); |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 1414 | |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1415 | // identifier ':' -> Label. |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1416 | Lex(); |
Daniel Dunbar | ae7ac01 | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 1417 | |
Daniel Dunbar | 6f4c942 | 2011-03-25 17:47:17 +0000 | [diff] [blame] | 1418 | // Diagnose attempt to use '.' as a label. |
| 1419 | if (IDVal == ".") |
| 1420 | return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label"); |
| 1421 | |
Daniel Dunbar | ae7ac01 | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 1422 | // Diagnose attempt to use a variable as a label. |
| 1423 | // |
| 1424 | // FIXME: Diagnostics. Note the location of the definition as a label. |
| 1425 | // FIXME: This doesn't diagnose assignment to a symbol which has been |
| 1426 | // implicitly marked as external. |
Kevin Enderby | 0510b48 | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1427 | MCSymbol *Sym; |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 1428 | if (LocalLabelVal == -1) { |
| 1429 | if (ParsingInlineAsm && SI) { |
Nico Weber | 67e715f | 2015-06-19 23:43:47 +0000 | [diff] [blame] | 1430 | StringRef RewrittenLabel = |
| 1431 | SI->LookupInlineAsmLabel(IDVal, getSourceManager(), IDLoc, true); |
| 1432 | assert(RewrittenLabel.size() && |
| 1433 | "We should have an internal name here."); |
Craig Topper | 7d5b231 | 2015-10-10 05:25:02 +0000 | [diff] [blame] | 1434 | Info.AsmRewrites->emplace_back(AOK_Label, IDLoc, IDVal.size(), |
| 1435 | RewrittenLabel); |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 1436 | IDVal = RewrittenLabel; |
| 1437 | } |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 1438 | Sym = getContext().getOrCreateSymbol(IDVal); |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 1439 | } else |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 1440 | Sym = Ctx.createDirectionalLocalSymbol(LocalLabelVal); |
David Majnemer | 58cb80c | 2014-12-24 10:27:50 +0000 | [diff] [blame] | 1441 | |
| 1442 | Sym->redefineIfPossible(); |
| 1443 | |
Daniel Dunbar | deb7ba9 | 2010-05-05 19:01:00 +0000 | [diff] [blame] | 1444 | if (!Sym->isUndefined() || Sym->isVariable()) |
Daniel Dunbar | ae7ac01 | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 1445 | return Error(IDLoc, "invalid symbol redefinition"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1446 | |
Daniel Dunbar | e73b267 | 2009-08-26 22:13:22 +0000 | [diff] [blame] | 1447 | // Emit the label. |
Chad Rosier | f3feab3 | 2013-01-07 20:34:12 +0000 | [diff] [blame] | 1448 | if (!ParsingInlineAsm) |
| 1449 | Out.EmitLabel(Sym); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1450 | |
Kevin Enderby | e7739d4 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 1451 | // If we are generating dwarf for assembly source files then gather the |
Kevin Enderby | f7d7706 | 2012-01-10 21:12:34 +0000 | [diff] [blame] | 1452 | // info to make a dwarf label entry for this label if needed. |
Kevin Enderby | e7739d4 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 1453 | if (getContext().getGenDwarfForAssembly()) |
Kevin Enderby | f7d7706 | 2012-01-10 21:12:34 +0000 | [diff] [blame] | 1454 | MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(), |
| 1455 | IDLoc); |
Kevin Enderby | e7739d4 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 1456 | |
Tim Northover | 1744d0a | 2013-10-25 12:49:50 +0000 | [diff] [blame] | 1457 | getTargetParser().onLabelParsed(Sym); |
| 1458 | |
Daniel Dunbar | 8271d1bb | 2010-05-23 18:36:34 +0000 | [diff] [blame] | 1459 | // Consume any end of statement token, if present, to avoid spurious |
| 1460 | // AddBlankLine calls(). |
| 1461 | if (Lexer.is(AsmToken::EndOfStatement)) { |
| 1462 | Lex(); |
| 1463 | if (Lexer.is(AsmToken::Eof)) |
| 1464 | return false; |
| 1465 | } |
| 1466 | |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 1467 | return false; |
Daniel Dunbar | ae7ac01 | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 1468 | } |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1469 | |
Daniel Dunbar | f2dcd77 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1470 | case AsmToken::Equal: |
Colin LeMahieu | 7820dff | 2015-11-09 00:15:45 +0000 | [diff] [blame] | 1471 | if (!getTargetParser().equalIsAsmAssignment()) |
| 1472 | break; |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1473 | // identifier '=' ... -> assignment statement |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1474 | Lex(); |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1475 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1476 | return parseAssignment(IDVal, true); |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1477 | |
| 1478 | default: // Normal instruction or directive. |
| 1479 | break; |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1480 | } |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 1481 | |
| 1482 | // 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] | 1483 | if (areMacrosEnabled()) |
| 1484 | if (const MCAsmMacro *M = lookupMacro(IDVal)) { |
| 1485 | return handleMacroEntry(M, IDLoc); |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 1486 | } |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 1487 | |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1488 | // Otherwise, we have a normal instruction or directive. |
Vladimir Medic | 9bad0d33 | 2013-08-20 13:33:18 +0000 | [diff] [blame] | 1489 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1490 | // Directives start with "." |
Daniel Dunbar | 6f4c942 | 2011-03-25 17:47:17 +0000 | [diff] [blame] | 1491 | if (IDVal[0] == '.' && IDVal != ".") { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1492 | // There are several entities interested in parsing directives: |
Vladimir Medic | 9bad0d33 | 2013-08-20 13:33:18 +0000 | [diff] [blame] | 1493 | // |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1494 | // 1. The target-specific assembly parser. Some directives are target |
| 1495 | // specific or may potentially behave differently on certain targets. |
| 1496 | // 2. Asm parser extensions. For example, platform-specific parsers |
| 1497 | // (like the ELF parser) register themselves as extensions. |
| 1498 | // 3. The generic directive parser implemented by this class. These are |
| 1499 | // all the directives that behave in a target and platform independent |
| 1500 | // manner, or at least have a default behavior that's shared between |
| 1501 | // all targets and platforms. |
Akira Hatanaka | d359075 | 2012-07-05 19:09:33 +0000 | [diff] [blame] | 1502 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1503 | // First query the target-specific parser. It will return 'true' if it |
| 1504 | // isn't interested in this directive. |
Akira Hatanaka | d359075 | 2012-07-05 19:09:33 +0000 | [diff] [blame] | 1505 | if (!getTargetParser().ParseDirective(ID)) |
| 1506 | return false; |
| 1507 | |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 1508 | // Next, check the extension directive map to see if any extension has |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1509 | // registered itself to parse this directive. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1510 | std::pair<MCAsmParserExtension *, DirectiveHandler> Handler = |
| 1511 | ExtensionDirectiveMap.lookup(IDVal); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1512 | if (Handler.first) |
| 1513 | return (*Handler.second)(Handler.first, IDVal, IDLoc); |
| 1514 | |
| 1515 | // Finally, if no one else is interested in this directive, it must be |
| 1516 | // generic and familiar to this class. |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1517 | switch (DirKind) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1518 | default: |
| 1519 | break; |
| 1520 | case DK_SET: |
| 1521 | case DK_EQU: |
| 1522 | return parseDirectiveSet(IDVal, true); |
| 1523 | case DK_EQUIV: |
| 1524 | return parseDirectiveSet(IDVal, false); |
| 1525 | case DK_ASCII: |
| 1526 | return parseDirectiveAscii(IDVal, false); |
| 1527 | case DK_ASCIZ: |
| 1528 | case DK_STRING: |
| 1529 | return parseDirectiveAscii(IDVal, true); |
| 1530 | case DK_BYTE: |
| 1531 | return parseDirectiveValue(1); |
| 1532 | case DK_SHORT: |
| 1533 | case DK_VALUE: |
| 1534 | case DK_2BYTE: |
| 1535 | return parseDirectiveValue(2); |
| 1536 | case DK_LONG: |
| 1537 | case DK_INT: |
| 1538 | case DK_4BYTE: |
| 1539 | return parseDirectiveValue(4); |
| 1540 | case DK_QUAD: |
| 1541 | case DK_8BYTE: |
| 1542 | return parseDirectiveValue(8); |
David Woodhouse | d6de0d9 | 2014-02-01 16:20:59 +0000 | [diff] [blame] | 1543 | case DK_OCTA: |
| 1544 | return parseDirectiveOctaValue(); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1545 | case DK_SINGLE: |
| 1546 | case DK_FLOAT: |
| 1547 | return parseDirectiveRealValue(APFloat::IEEEsingle); |
| 1548 | case DK_DOUBLE: |
| 1549 | return parseDirectiveRealValue(APFloat::IEEEdouble); |
| 1550 | case DK_ALIGN: { |
| 1551 | bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes(); |
| 1552 | return parseDirectiveAlign(IsPow2, /*ExprSize=*/1); |
| 1553 | } |
| 1554 | case DK_ALIGN32: { |
| 1555 | bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes(); |
| 1556 | return parseDirectiveAlign(IsPow2, /*ExprSize=*/4); |
| 1557 | } |
| 1558 | case DK_BALIGN: |
| 1559 | return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1); |
| 1560 | case DK_BALIGNW: |
| 1561 | return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2); |
| 1562 | case DK_BALIGNL: |
| 1563 | return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4); |
| 1564 | case DK_P2ALIGN: |
| 1565 | return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1); |
| 1566 | case DK_P2ALIGNW: |
| 1567 | return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2); |
| 1568 | case DK_P2ALIGNL: |
| 1569 | return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4); |
| 1570 | case DK_ORG: |
| 1571 | return parseDirectiveOrg(); |
| 1572 | case DK_FILL: |
| 1573 | return parseDirectiveFill(); |
| 1574 | case DK_ZERO: |
| 1575 | return parseDirectiveZero(); |
| 1576 | case DK_EXTERN: |
| 1577 | eatToEndOfStatement(); // .extern is the default, ignore it. |
| 1578 | return false; |
| 1579 | case DK_GLOBL: |
| 1580 | case DK_GLOBAL: |
| 1581 | return parseDirectiveSymbolAttribute(MCSA_Global); |
| 1582 | case DK_LAZY_REFERENCE: |
| 1583 | return parseDirectiveSymbolAttribute(MCSA_LazyReference); |
| 1584 | case DK_NO_DEAD_STRIP: |
| 1585 | return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip); |
| 1586 | case DK_SYMBOL_RESOLVER: |
| 1587 | return parseDirectiveSymbolAttribute(MCSA_SymbolResolver); |
| 1588 | case DK_PRIVATE_EXTERN: |
| 1589 | return parseDirectiveSymbolAttribute(MCSA_PrivateExtern); |
| 1590 | case DK_REFERENCE: |
| 1591 | return parseDirectiveSymbolAttribute(MCSA_Reference); |
| 1592 | case DK_WEAK_DEFINITION: |
| 1593 | return parseDirectiveSymbolAttribute(MCSA_WeakDefinition); |
| 1594 | case DK_WEAK_REFERENCE: |
| 1595 | return parseDirectiveSymbolAttribute(MCSA_WeakReference); |
| 1596 | case DK_WEAK_DEF_CAN_BE_HIDDEN: |
| 1597 | return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate); |
| 1598 | case DK_COMM: |
| 1599 | case DK_COMMON: |
| 1600 | return parseDirectiveComm(/*IsLocal=*/false); |
| 1601 | case DK_LCOMM: |
| 1602 | return parseDirectiveComm(/*IsLocal=*/true); |
| 1603 | case DK_ABORT: |
| 1604 | return parseDirectiveAbort(); |
| 1605 | case DK_INCLUDE: |
| 1606 | return parseDirectiveInclude(); |
| 1607 | case DK_INCBIN: |
| 1608 | return parseDirectiveIncbin(); |
| 1609 | case DK_CODE16: |
| 1610 | case DK_CODE16GCC: |
| 1611 | return TokError(Twine(IDVal) + " not supported yet"); |
| 1612 | case DK_REPT: |
Saleem Abdulrasool | d743d0a | 2013-12-28 05:54:33 +0000 | [diff] [blame] | 1613 | return parseDirectiveRept(IDLoc, IDVal); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1614 | case DK_IRP: |
| 1615 | return parseDirectiveIrp(IDLoc); |
| 1616 | case DK_IRPC: |
| 1617 | return parseDirectiveIrpc(IDLoc); |
| 1618 | case DK_ENDR: |
| 1619 | return parseDirectiveEndr(IDLoc); |
| 1620 | case DK_BUNDLE_ALIGN_MODE: |
| 1621 | return parseDirectiveBundleAlignMode(); |
| 1622 | case DK_BUNDLE_LOCK: |
| 1623 | return parseDirectiveBundleLock(); |
| 1624 | case DK_BUNDLE_UNLOCK: |
| 1625 | return parseDirectiveBundleUnlock(); |
| 1626 | case DK_SLEB128: |
| 1627 | return parseDirectiveLEB128(true); |
| 1628 | case DK_ULEB128: |
| 1629 | return parseDirectiveLEB128(false); |
| 1630 | case DK_SPACE: |
| 1631 | case DK_SKIP: |
| 1632 | return parseDirectiveSpace(IDVal); |
| 1633 | case DK_FILE: |
| 1634 | return parseDirectiveFile(IDLoc); |
| 1635 | case DK_LINE: |
| 1636 | return parseDirectiveLine(); |
| 1637 | case DK_LOC: |
| 1638 | return parseDirectiveLoc(); |
| 1639 | case DK_STABS: |
| 1640 | return parseDirectiveStabs(); |
| 1641 | case DK_CFI_SECTIONS: |
| 1642 | return parseDirectiveCFISections(); |
| 1643 | case DK_CFI_STARTPROC: |
| 1644 | return parseDirectiveCFIStartProc(); |
| 1645 | case DK_CFI_ENDPROC: |
| 1646 | return parseDirectiveCFIEndProc(); |
| 1647 | case DK_CFI_DEF_CFA: |
| 1648 | return parseDirectiveCFIDefCfa(IDLoc); |
| 1649 | case DK_CFI_DEF_CFA_OFFSET: |
| 1650 | return parseDirectiveCFIDefCfaOffset(); |
| 1651 | case DK_CFI_ADJUST_CFA_OFFSET: |
| 1652 | return parseDirectiveCFIAdjustCfaOffset(); |
| 1653 | case DK_CFI_DEF_CFA_REGISTER: |
| 1654 | return parseDirectiveCFIDefCfaRegister(IDLoc); |
| 1655 | case DK_CFI_OFFSET: |
| 1656 | return parseDirectiveCFIOffset(IDLoc); |
| 1657 | case DK_CFI_REL_OFFSET: |
| 1658 | return parseDirectiveCFIRelOffset(IDLoc); |
| 1659 | case DK_CFI_PERSONALITY: |
| 1660 | return parseDirectiveCFIPersonalityOrLsda(true); |
| 1661 | case DK_CFI_LSDA: |
| 1662 | return parseDirectiveCFIPersonalityOrLsda(false); |
| 1663 | case DK_CFI_REMEMBER_STATE: |
| 1664 | return parseDirectiveCFIRememberState(); |
| 1665 | case DK_CFI_RESTORE_STATE: |
| 1666 | return parseDirectiveCFIRestoreState(); |
| 1667 | case DK_CFI_SAME_VALUE: |
| 1668 | return parseDirectiveCFISameValue(IDLoc); |
| 1669 | case DK_CFI_RESTORE: |
| 1670 | return parseDirectiveCFIRestore(IDLoc); |
| 1671 | case DK_CFI_ESCAPE: |
| 1672 | return parseDirectiveCFIEscape(); |
| 1673 | case DK_CFI_SIGNAL_FRAME: |
| 1674 | return parseDirectiveCFISignalFrame(); |
| 1675 | case DK_CFI_UNDEFINED: |
| 1676 | return parseDirectiveCFIUndefined(IDLoc); |
| 1677 | case DK_CFI_REGISTER: |
| 1678 | return parseDirectiveCFIRegister(IDLoc); |
Venkatraman Govindaraju | 3816d43 | 2013-09-26 14:49:40 +0000 | [diff] [blame] | 1679 | case DK_CFI_WINDOW_SAVE: |
| 1680 | return parseDirectiveCFIWindowSave(); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1681 | case DK_MACROS_ON: |
| 1682 | case DK_MACROS_OFF: |
| 1683 | return parseDirectiveMacrosOnOff(IDVal); |
| 1684 | case DK_MACRO: |
| 1685 | return parseDirectiveMacro(IDLoc); |
Nico Weber | 155dccd1 | 2014-07-24 17:08:39 +0000 | [diff] [blame] | 1686 | case DK_EXITM: |
| 1687 | return parseDirectiveExitMacro(IDVal); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1688 | case DK_ENDM: |
| 1689 | case DK_ENDMACRO: |
| 1690 | return parseDirectiveEndMacro(IDVal); |
| 1691 | case DK_PURGEM: |
| 1692 | return parseDirectivePurgeMacro(IDLoc); |
Saleem Abdulrasool | 88186c4 | 2013-12-18 02:53:03 +0000 | [diff] [blame] | 1693 | case DK_END: |
| 1694 | return parseDirectiveEnd(IDLoc); |
Saleem Abdulrasool | b2ae2c0 | 2014-02-23 15:53:30 +0000 | [diff] [blame] | 1695 | case DK_ERR: |
Saleem Abdulrasool | 7ecc549 | 2014-02-23 23:02:23 +0000 | [diff] [blame] | 1696 | return parseDirectiveError(IDLoc, false); |
| 1697 | case DK_ERROR: |
| 1698 | return parseDirectiveError(IDLoc, true); |
Nico Weber | 404012b | 2014-07-24 16:26:06 +0000 | [diff] [blame] | 1699 | case DK_WARNING: |
| 1700 | return parseDirectiveWarning(IDLoc); |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 1701 | case DK_RELOC: |
| 1702 | return parseDirectiveReloc(IDLoc); |
Eli Friedman | 20b0264 | 2010-07-19 04:17:25 +0000 | [diff] [blame] | 1703 | } |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 1704 | |
Jim Grosbach | 758e0cc | 2012-05-01 18:38:27 +0000 | [diff] [blame] | 1705 | return Error(IDLoc, "unknown directive"); |
Chris Lattner | e5074c4 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 1706 | } |
Chris Lattner | 36e0212 | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1707 | |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 1708 | // __asm _emit or __asm __emit |
| 1709 | if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" || |
| 1710 | IDVal == "_EMIT" || IDVal == "__EMIT")) |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1711 | return parseDirectiveMSEmit(IDLoc, Info, IDVal.size()); |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 1712 | |
| 1713 | // __asm align |
| 1714 | if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN")) |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1715 | return parseDirectiveMSAlign(IDLoc, Info); |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 1716 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 1717 | checkForValidSection(); |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 1718 | |
Chris Lattner | 7cbfa44 | 2010-05-19 23:34:33 +0000 | [diff] [blame] | 1719 | // Canonicalize the opcode to lower case. |
Eli Bendersky | 8802471 | 2013-01-16 19:32:36 +0000 | [diff] [blame] | 1720 | std::string OpcodeStr = IDVal.lower(); |
Chad Rosier | f0e8720 | 2012-10-25 20:41:34 +0000 | [diff] [blame] | 1721 | ParseInstructionInfo IInfo(Info.AsmRewrites); |
Colin LeMahieu | 7820dff | 2015-11-09 00:15:45 +0000 | [diff] [blame] | 1722 | bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, ID, |
Vladimir Medic | 9bad0d33 | 2013-08-20 13:33:18 +0000 | [diff] [blame] | 1723 | Info.ParsedOperands); |
Chad Rosier | 149e8e0 | 2012-12-12 22:45:52 +0000 | [diff] [blame] | 1724 | Info.ParseError = HadError; |
Chris Lattner | e5074c4 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 1725 | |
Daniel Dunbar | 2eca025 | 2010-08-11 06:37:09 +0000 | [diff] [blame] | 1726 | // Dump the parsed representation, if requested. |
| 1727 | if (getShowParsedOperands()) { |
| 1728 | SmallString<256> Str; |
| 1729 | raw_svector_ostream OS(Str); |
| 1730 | OS << "parsed instruction: ["; |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 1731 | for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) { |
Daniel Dunbar | 2eca025 | 2010-08-11 06:37:09 +0000 | [diff] [blame] | 1732 | if (i != 0) |
| 1733 | OS << ", "; |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 1734 | Info.ParsedOperands[i]->print(OS); |
Daniel Dunbar | 2eca025 | 2010-08-11 06:37:09 +0000 | [diff] [blame] | 1735 | } |
| 1736 | OS << "]"; |
| 1737 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1738 | printMessage(IDLoc, SourceMgr::DK_Note, OS.str()); |
Daniel Dunbar | 2eca025 | 2010-08-11 06:37:09 +0000 | [diff] [blame] | 1739 | } |
| 1740 | |
Oliver Stannard | 8b27308 | 2014-06-19 15:52:37 +0000 | [diff] [blame] | 1741 | // If we are generating dwarf for the current section then generate a .loc |
| 1742 | // directive for the instruction. |
Kevin Enderby | 6469fc2 | 2011-11-01 22:27:22 +0000 | [diff] [blame] | 1743 | if (!HadError && getContext().getGenDwarfForAssembly() && |
Oliver Stannard | 8b27308 | 2014-06-19 15:52:37 +0000 | [diff] [blame] | 1744 | getContext().getGenDwarfSectionSyms().count( |
Saleem Abdulrasool | 4d6ed7c | 2014-12-24 06:32:43 +0000 | [diff] [blame] | 1745 | getStreamer().getCurrentSection().first)) { |
| 1746 | unsigned Line; |
| 1747 | if (ActiveMacros.empty()) |
| 1748 | Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer); |
| 1749 | else |
Frederic Riss | 16238d9 | 2015-06-25 21:57:33 +0000 | [diff] [blame] | 1750 | Line = SrcMgr.FindLineNumber(ActiveMacros.front()->InstantiationLoc, |
| 1751 | ActiveMacros.front()->ExitBuffer); |
Kevin Enderby | 4eaf8ef | 2012-11-01 17:31:35 +0000 | [diff] [blame] | 1752 | |
Eli Bendersky | 8802471 | 2013-01-16 19:32:36 +0000 | [diff] [blame] | 1753 | // If we previously parsed a cpp hash file line comment then make sure the |
| 1754 | // current Dwarf File is for the CppHashFilename if not then emit the |
| 1755 | // 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] | 1756 | if (CppHashFilename.size()) { |
David Blaikie | c714ef4 | 2014-03-17 01:52:11 +0000 | [diff] [blame] | 1757 | unsigned FileNumber = getStreamer().EmitDwarfFileDirective( |
| 1758 | 0, StringRef(), CppHashFilename); |
| 1759 | getContext().setGenDwarfFileNumber(FileNumber); |
Kevin Enderby | 4eaf8ef | 2012-11-01 17:31:35 +0000 | [diff] [blame] | 1760 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1761 | // Since SrcMgr.FindLineNumber() is slow and messes up the SourceMgr's |
| 1762 | // cache with the different Loc from the call above we save the last |
| 1763 | // info we queried here with SrcMgr.FindLineNumber(). |
| 1764 | unsigned CppHashLocLineNo; |
| 1765 | if (LastQueryIDLoc == CppHashLoc && LastQueryBuffer == CppHashBuf) |
| 1766 | CppHashLocLineNo = LastQueryLine; |
| 1767 | else { |
| 1768 | CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc, CppHashBuf); |
| 1769 | LastQueryLine = CppHashLocLineNo; |
| 1770 | LastQueryIDLoc = CppHashLoc; |
| 1771 | LastQueryBuffer = CppHashBuf; |
| 1772 | } |
| 1773 | Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo); |
Eli Bendersky | 8802471 | 2013-01-16 19:32:36 +0000 | [diff] [blame] | 1774 | } |
Kevin Enderby | 4eaf8ef | 2012-11-01 17:31:35 +0000 | [diff] [blame] | 1775 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1776 | getStreamer().EmitDwarfLocDirective( |
| 1777 | getContext().getGenDwarfFileNumber(), Line, 0, |
| 1778 | DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0, |
| 1779 | StringRef()); |
Kevin Enderby | 6469fc2 | 2011-11-01 22:27:22 +0000 | [diff] [blame] | 1780 | } |
| 1781 | |
Daniel Dunbar | ce0c1e1 | 2010-05-04 00:33:07 +0000 | [diff] [blame] | 1782 | // If parsing succeeded, match the instruction. |
Chad Rosier | 4996355 | 2012-10-13 00:26:04 +0000 | [diff] [blame] | 1783 | if (!HadError) { |
Tim Northover | 26bb14e | 2014-08-18 11:49:42 +0000 | [diff] [blame] | 1784 | uint64_t ErrorInfo; |
Arnaud A. de Grandmaison | c97727a | 2014-03-21 21:54:46 +0000 | [diff] [blame] | 1785 | getTargetParser().MatchAndEmitInstruction(IDLoc, Info.Opcode, |
| 1786 | Info.ParsedOperands, Out, |
Ranjeet Singh | 86ecbb7 | 2015-06-30 12:32:53 +0000 | [diff] [blame] | 1787 | ErrorInfo, ParsingInlineAsm); |
Chad Rosier | 4996355 | 2012-10-13 00:26:04 +0000 | [diff] [blame] | 1788 | } |
Chris Lattner | f29c0b6 | 2010-01-14 22:21:20 +0000 | [diff] [blame] | 1789 | |
Chris Lattner | a2a9d16 | 2010-09-11 16:18:25 +0000 | [diff] [blame] | 1790 | // Don't skip the rest of the line, the instruction parser is responsible for |
| 1791 | // that. |
| 1792 | return false; |
Chris Lattner | b013345 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 1793 | } |
Chris Lattner | bedf6c2 | 2009-06-24 04:43:34 +0000 | [diff] [blame] | 1794 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1795 | /// 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] | 1796 | /// 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] | 1797 | void AsmParser::eatToEndOfLine() { |
Rafael Espindola | e0d0908 | 2011-10-19 18:48:52 +0000 | [diff] [blame] | 1798 | if (!Lexer.is(AsmToken::EndOfStatement)) |
| 1799 | Lexer.LexUntilEndOfLine(); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1800 | // Eat EOL. |
| 1801 | Lex(); |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1802 | } |
| 1803 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1804 | /// parseCppHashLineFilenameComment as this: |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1805 | /// ::= # number "filename" |
| 1806 | /// or just as a full line comment if it doesn't have a number and a string. |
Craig Topper | 3c76c52 | 2015-09-20 23:35:59 +0000 | [diff] [blame] | 1807 | bool AsmParser::parseCppHashLineFilenameComment(SMLoc L) { |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1808 | Lex(); // Eat the hash token. |
| 1809 | |
| 1810 | if (getLexer().isNot(AsmToken::Integer)) { |
| 1811 | // Consume the line since in cases it is not a well-formed line directive, |
| 1812 | // as if were simply a full line comment. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1813 | eatToEndOfLine(); |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1814 | return false; |
| 1815 | } |
| 1816 | |
| 1817 | int64_t LineNumber = getTok().getIntVal(); |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1818 | Lex(); |
| 1819 | |
| 1820 | if (getLexer().isNot(AsmToken::String)) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1821 | eatToEndOfLine(); |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1822 | return false; |
| 1823 | } |
| 1824 | |
| 1825 | StringRef Filename = getTok().getString(); |
| 1826 | // Get rid of the enclosing quotes. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1827 | Filename = Filename.substr(1, Filename.size() - 2); |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1828 | |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1829 | // Save the SMLoc, Filename and LineNumber for later use by diagnostics. |
| 1830 | CppHashLoc = L; |
| 1831 | CppHashFilename = Filename; |
| 1832 | CppHashLineNumber = LineNumber; |
Kevin Enderby | 27121c1 | 2012-11-05 21:55:41 +0000 | [diff] [blame] | 1833 | CppHashBuf = CurBuffer; |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1834 | |
| 1835 | // Ignore any trailing characters, they're just comment. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1836 | eatToEndOfLine(); |
Kevin Enderby | 7255361 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1837 | return false; |
| 1838 | } |
| 1839 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1840 | /// \brief will use the last parsed cpp hash line filename comment |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1841 | /// for the Filename and LineNo if any in the diagnostic. |
| 1842 | void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1843 | const AsmParser *Parser = static_cast<const AsmParser *>(Context); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1844 | raw_ostream &OS = errs(); |
| 1845 | |
| 1846 | const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr(); |
Craig Topper | 3c76c52 | 2015-09-20 23:35:59 +0000 | [diff] [blame] | 1847 | SMLoc DiagLoc = Diag.getLoc(); |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 1848 | unsigned DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc); |
| 1849 | unsigned CppHashBuf = |
| 1850 | Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1851 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1852 | // Like SourceMgr::printMessage() we need to print the include stack if any |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1853 | // before printing the message. |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 1854 | unsigned DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc); |
| 1855 | if (!Parser->SavedDiagHandler && DiagCurBuffer && |
| 1856 | DiagCurBuffer != DiagSrcMgr.getMainFileID()) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1857 | SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer); |
| 1858 | DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1859 | } |
| 1860 | |
Eric Christopher | a7c3273 | 2012-12-18 00:30:54 +0000 | [diff] [blame] | 1861 | // 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] | 1862 | // manager changed or buffer changed (like in a nested include) then just |
| 1863 | // print the normal diagnostic using its Filename and LineNo. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1864 | if (!Parser->CppHashLineNumber || &DiagSrcMgr != &Parser->SrcMgr || |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1865 | DiagBuf != CppHashBuf) { |
Benjamin Kramer | 47f5e30 | 2011-10-16 10:48:29 +0000 | [diff] [blame] | 1866 | if (Parser->SavedDiagHandler) |
| 1867 | Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext); |
| 1868 | else |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 1869 | Diag.print(nullptr, OS); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1870 | return; |
| 1871 | } |
| 1872 | |
Eric Christopher | a7c3273 | 2012-12-18 00:30:54 +0000 | [diff] [blame] | 1873 | // Use the CppHashFilename and calculate a line number based on the |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1874 | // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for |
| 1875 | // the diagnostic. |
Jakub Staszak | ec2ffa9 | 2013-09-16 22:03:38 +0000 | [diff] [blame] | 1876 | const std::string &Filename = Parser->CppHashFilename; |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1877 | |
| 1878 | int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf); |
| 1879 | int CppHashLocLineNo = |
| 1880 | Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1881 | int LineNo = |
| 1882 | Parser->CppHashLineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1883 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1884 | SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo, |
| 1885 | Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(), |
Chris Lattner | 7284526 | 2011-10-16 05:47:55 +0000 | [diff] [blame] | 1886 | Diag.getLineContents(), Diag.getRanges()); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1887 | |
Benjamin Kramer | 47f5e30 | 2011-10-16 10:48:29 +0000 | [diff] [blame] | 1888 | if (Parser->SavedDiagHandler) |
| 1889 | Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext); |
| 1890 | else |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 1891 | NewDiag.print(nullptr, OS); |
Kevin Enderby | e7c0c49 | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1892 | } |
| 1893 | |
Rafael Espindola | 2c06448 | 2012-08-21 18:29:30 +0000 | [diff] [blame] | 1894 | // FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The |
| 1895 | // difference being that that function accepts '@' as part of identifiers and |
| 1896 | // we can't do that. AsmLexer.cpp should probably be changed to handle |
| 1897 | // '@' as a special case when needed. |
| 1898 | static bool isIdentifierChar(char c) { |
Guy Benyei | 83c74e9 | 2013-02-12 21:21:59 +0000 | [diff] [blame] | 1899 | return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' || |
| 1900 | c == '.'; |
Rafael Espindola | 2c06448 | 2012-08-21 18:29:30 +0000 | [diff] [blame] | 1901 | } |
| 1902 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 1903 | bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body, |
Benjamin Kramer | d31aaf1 | 2014-02-09 17:13:11 +0000 | [diff] [blame] | 1904 | ArrayRef<MCAsmMacroParameter> Parameters, |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 1905 | ArrayRef<MCAsmMacroArgument> A, |
Craig Topper | 3c76c52 | 2015-09-20 23:35:59 +0000 | [diff] [blame] | 1906 | bool EnableAtPseudoVariable, SMLoc L) { |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1907 | unsigned NParameters = Parameters.size(); |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 1908 | bool HasVararg = NParameters ? Parameters.back().Vararg : false; |
Benjamin Kramer | 513e744 | 2014-02-20 13:36:32 +0000 | [diff] [blame] | 1909 | if ((!IsDarwin || NParameters != 0) && NParameters != A.size()) |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1910 | return Error(L, "Wrong number of arguments"); |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 1911 | |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1912 | // A macro without parameters is handled differently on Darwin: |
| 1913 | // gas accepts no arguments and does no substitutions |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 1914 | while (!Body.empty()) { |
| 1915 | // Scan for the next substitution. |
| 1916 | std::size_t End = Body.size(), Pos = 0; |
| 1917 | for (; Pos != End; ++Pos) { |
| 1918 | // Check for a substitution or escape. |
Benjamin Kramer | 513e744 | 2014-02-20 13:36:32 +0000 | [diff] [blame] | 1919 | if (IsDarwin && !NParameters) { |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1920 | // This macro has no parameters, look for $0, $1, etc. |
| 1921 | if (Body[Pos] != '$' || Pos + 1 == End) |
| 1922 | continue; |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 1923 | |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1924 | char Next = Body[Pos + 1]; |
Guy Benyei | 83c74e9 | 2013-02-12 21:21:59 +0000 | [diff] [blame] | 1925 | if (Next == '$' || Next == 'n' || |
| 1926 | isdigit(static_cast<unsigned char>(Next))) |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1927 | break; |
| 1928 | } else { |
| 1929 | // This macro has parameters, look for \foo, \bar, etc. |
| 1930 | if (Body[Pos] == '\\' && Pos + 1 != End) |
| 1931 | break; |
| 1932 | } |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 1933 | } |
| 1934 | |
| 1935 | // Add the prefix. |
| 1936 | OS << Body.slice(0, Pos); |
| 1937 | |
| 1938 | // Check if we reached the end. |
| 1939 | if (Pos == End) |
| 1940 | break; |
| 1941 | |
Benjamin Kramer | 513e744 | 2014-02-20 13:36:32 +0000 | [diff] [blame] | 1942 | if (IsDarwin && !NParameters) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1943 | switch (Body[Pos + 1]) { |
| 1944 | // $$ => $ |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1945 | case '$': |
| 1946 | OS << '$'; |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 1947 | break; |
| 1948 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1949 | // $n => number of arguments |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1950 | case 'n': |
| 1951 | OS << A.size(); |
| 1952 | break; |
| 1953 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1954 | // $[0-9] => argument |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1955 | default: { |
| 1956 | // Missing arguments are ignored. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1957 | unsigned Index = Body[Pos + 1] - '0'; |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1958 | if (Index >= A.size()) |
| 1959 | break; |
| 1960 | |
| 1961 | // Otherwise substitute with the token values, with spaces eliminated. |
Craig Topper | 8400848 | 2015-10-10 05:38:14 +0000 | [diff] [blame] | 1962 | for (const AsmToken &Token : A[Index]) |
| 1963 | OS << Token.getString(); |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1964 | break; |
| 1965 | } |
| 1966 | } |
| 1967 | Pos += 2; |
| 1968 | } else { |
| 1969 | unsigned I = Pos + 1; |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 1970 | |
| 1971 | // Check for the \@ pseudo-variable. |
| 1972 | if (EnableAtPseudoVariable && Body[I] == '@' && I + 1 != End) |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1973 | ++I; |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 1974 | else |
| 1975 | while (isIdentifierChar(Body[I]) && I + 1 != End) |
| 1976 | ++I; |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1977 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 1978 | const char *Begin = Body.data() + Pos + 1; |
| 1979 | StringRef Argument(Begin, I - (Pos + 1)); |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1980 | unsigned Index = 0; |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1981 | |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 1982 | if (Argument == "@") { |
| 1983 | OS << NumOfMacroInstantiations; |
| 1984 | Pos += 2; |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1985 | } else { |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 1986 | for (; Index < NParameters; ++Index) |
| 1987 | if (Parameters[Index].Name == Argument) |
| 1988 | break; |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1989 | |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 1990 | if (Index == NParameters) { |
| 1991 | if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')') |
| 1992 | Pos += 3; |
| 1993 | else { |
| 1994 | OS << '\\' << Argument; |
| 1995 | Pos = I; |
| 1996 | } |
| 1997 | } else { |
| 1998 | bool VarargParameter = HasVararg && Index == (NParameters - 1); |
Craig Topper | 8400848 | 2015-10-10 05:38:14 +0000 | [diff] [blame] | 1999 | for (const AsmToken &Token : A[Index]) |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 2000 | // We expect no quotes around the string's contents when |
| 2001 | // parsing for varargs. |
Craig Topper | 8400848 | 2015-10-10 05:38:14 +0000 | [diff] [blame] | 2002 | if (Token.getKind() != AsmToken::String || VarargParameter) |
| 2003 | OS << Token.getString(); |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 2004 | else |
Craig Topper | 8400848 | 2015-10-10 05:38:14 +0000 | [diff] [blame] | 2005 | OS << Token.getStringContents(); |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 2006 | |
| 2007 | Pos += 1 + Argument.size(); |
| 2008 | } |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2009 | } |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2010 | } |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 2011 | // Update the scan point. |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2012 | Body = Body.substr(Pos); |
Daniel Dunbar | 6fb1c3a | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 2013 | } |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2014 | |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2015 | return false; |
| 2016 | } |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2017 | |
Nico Weber | 2a8f922 | 2014-07-24 16:29:04 +0000 | [diff] [blame] | 2018 | MacroInstantiation::MacroInstantiation(SMLoc IL, int EB, SMLoc EL, |
Rafael Espindola | 9eef18c | 2014-08-27 19:49:03 +0000 | [diff] [blame] | 2019 | size_t CondStackDepth) |
Rafael Espindola | f43a94e | 2014-08-17 22:48:55 +0000 | [diff] [blame] | 2020 | : InstantiationLoc(IL), ExitBuffer(EB), ExitLoc(EL), |
Nico Weber | 155dccd1 | 2014-07-24 17:08:39 +0000 | [diff] [blame] | 2021 | CondStackDepth(CondStackDepth) {} |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2022 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2023 | static bool isOperator(AsmToken::TokenKind kind) { |
| 2024 | switch (kind) { |
| 2025 | default: |
| 2026 | return false; |
| 2027 | case AsmToken::Plus: |
| 2028 | case AsmToken::Minus: |
| 2029 | case AsmToken::Tilde: |
| 2030 | case AsmToken::Slash: |
| 2031 | case AsmToken::Star: |
| 2032 | case AsmToken::Dot: |
| 2033 | case AsmToken::Equal: |
| 2034 | case AsmToken::EqualEqual: |
| 2035 | case AsmToken::Pipe: |
| 2036 | case AsmToken::PipePipe: |
| 2037 | case AsmToken::Caret: |
| 2038 | case AsmToken::Amp: |
| 2039 | case AsmToken::AmpAmp: |
| 2040 | case AsmToken::Exclaim: |
| 2041 | case AsmToken::ExclaimEqual: |
| 2042 | case AsmToken::Percent: |
| 2043 | case AsmToken::Less: |
| 2044 | case AsmToken::LessEqual: |
| 2045 | case AsmToken::LessLess: |
| 2046 | case AsmToken::LessGreater: |
| 2047 | case AsmToken::Greater: |
| 2048 | case AsmToken::GreaterEqual: |
| 2049 | case AsmToken::GreaterGreater: |
| 2050 | return true; |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2051 | } |
| 2052 | } |
| 2053 | |
David Majnemer | 1625245 | 2014-01-29 00:07:39 +0000 | [diff] [blame] | 2054 | namespace { |
| 2055 | class AsmLexerSkipSpaceRAII { |
| 2056 | public: |
| 2057 | AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) { |
| 2058 | Lexer.setSkipSpace(SkipSpace); |
| 2059 | } |
| 2060 | |
| 2061 | ~AsmLexerSkipSpaceRAII() { |
| 2062 | Lexer.setSkipSpace(true); |
| 2063 | } |
| 2064 | |
| 2065 | private: |
| 2066 | AsmLexer &Lexer; |
| 2067 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 2068 | } |
David Majnemer | 1625245 | 2014-01-29 00:07:39 +0000 | [diff] [blame] | 2069 | |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 2070 | bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg) { |
| 2071 | |
| 2072 | if (Vararg) { |
| 2073 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
| 2074 | StringRef Str = parseStringToEndOfStatement(); |
Benjamin Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 2075 | MA.emplace_back(AsmToken::String, Str); |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 2076 | } |
| 2077 | return false; |
| 2078 | } |
| 2079 | |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2080 | unsigned ParenLevel = 0; |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2081 | unsigned AddTokens = 0; |
| 2082 | |
David Majnemer | 1625245 | 2014-01-29 00:07:39 +0000 | [diff] [blame] | 2083 | // Darwin doesn't use spaces to delmit arguments. |
| 2084 | AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin); |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2085 | |
| 2086 | for (;;) { |
David Majnemer | 1625245 | 2014-01-29 00:07:39 +0000 | [diff] [blame] | 2087 | if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal)) |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2088 | return TokError("unexpected token in macro instantiation"); |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2089 | |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 2090 | if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2091 | break; |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2092 | |
| 2093 | if (Lexer.is(AsmToken::Space)) { |
| 2094 | Lex(); // Eat spaces |
| 2095 | |
| 2096 | // Spaces can delimit parameters, but could also be part an expression. |
| 2097 | // If the token after a space is an operator, add the token and the next |
| 2098 | // one into this argument |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 2099 | if (!IsDarwin) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2100 | if (isOperator(Lexer.getKind())) { |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2101 | // Check to see whether the token is used as an operator, |
| 2102 | // or part of an identifier |
Jordan Rose | e8f1eae | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 2103 | const char *NextChar = getTok().getEndLoc().getPointer(); |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2104 | if (*NextChar == ' ') |
| 2105 | AddTokens = 2; |
| 2106 | } |
| 2107 | |
| 2108 | if (!AddTokens && ParenLevel == 0) { |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2109 | break; |
| 2110 | } |
| 2111 | } |
| 2112 | } |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2113 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2114 | // handleMacroEntry relies on not advancing the lexer here |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2115 | // to be able to fill in the remaining default parameter values |
| 2116 | if (Lexer.is(AsmToken::EndOfStatement)) |
| 2117 | break; |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2118 | |
| 2119 | // Adjust the current parentheses level. |
| 2120 | if (Lexer.is(AsmToken::LParen)) |
| 2121 | ++ParenLevel; |
| 2122 | else if (Lexer.is(AsmToken::RParen) && ParenLevel) |
| 2123 | --ParenLevel; |
| 2124 | |
| 2125 | // Append the token to the current argument list. |
| 2126 | MA.push_back(getTok()); |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2127 | if (AddTokens) |
| 2128 | AddTokens--; |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2129 | Lex(); |
| 2130 | } |
Preston Gurd | 0550064 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 2131 | |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2132 | if (ParenLevel != 0) |
Rafael Espindola | 3e5eb42 | 2012-08-21 15:55:04 +0000 | [diff] [blame] | 2133 | return TokError("unbalanced parentheses in macro argument"); |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2134 | return false; |
| 2135 | } |
| 2136 | |
| 2137 | // Parse the macro instantiation arguments. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2138 | bool AsmParser::parseMacroArguments(const MCAsmMacro *M, |
Vladimir Medic | 9bad0d33 | 2013-08-20 13:33:18 +0000 | [diff] [blame] | 2139 | MCAsmMacroArguments &A) { |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2140 | const unsigned NParameters = M ? M->Parameters.size() : 0; |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2141 | bool NamedParametersFound = false; |
| 2142 | SmallVector<SMLoc, 4> FALocs; |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2143 | |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2144 | A.resize(NParameters); |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2145 | FALocs.resize(NParameters); |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2146 | |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2147 | // Parse two kinds of macro invocations: |
| 2148 | // - macros defined without any parameters accept an arbitrary number of them |
| 2149 | // - macros defined with parameters accept at most that many of them |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 2150 | bool HasVararg = NParameters ? M->Parameters.back().Vararg : false; |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2151 | for (unsigned Parameter = 0; !NParameters || Parameter < NParameters; |
| 2152 | ++Parameter) { |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2153 | SMLoc IDLoc = Lexer.getLoc(); |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2154 | MCAsmMacroParameter FA; |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2155 | |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2156 | if (Lexer.is(AsmToken::Identifier) && Lexer.peekTok().is(AsmToken::Equal)) { |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 2157 | if (parseIdentifier(FA.Name)) { |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2158 | Error(IDLoc, "invalid argument identifier for formal argument"); |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2159 | eatToEndOfStatement(); |
| 2160 | return true; |
| 2161 | } |
| 2162 | |
| 2163 | if (!Lexer.is(AsmToken::Equal)) { |
| 2164 | TokError("expected '=' after formal parameter identifier"); |
| 2165 | eatToEndOfStatement(); |
| 2166 | return true; |
| 2167 | } |
| 2168 | Lex(); |
| 2169 | |
| 2170 | NamedParametersFound = true; |
| 2171 | } |
| 2172 | |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 2173 | if (NamedParametersFound && FA.Name.empty()) { |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2174 | Error(IDLoc, "cannot mix positional and keyword arguments"); |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2175 | eatToEndOfStatement(); |
| 2176 | return true; |
| 2177 | } |
| 2178 | |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 2179 | bool Vararg = HasVararg && Parameter == (NParameters - 1); |
| 2180 | if (parseMacroArgument(FA.Value, Vararg)) |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2181 | return true; |
| 2182 | |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2183 | unsigned PI = Parameter; |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 2184 | if (!FA.Name.empty()) { |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2185 | unsigned FAI = 0; |
| 2186 | for (FAI = 0; FAI < NParameters; ++FAI) |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 2187 | if (M->Parameters[FAI].Name == FA.Name) |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2188 | break; |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2189 | |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2190 | if (FAI >= NParameters) { |
Oliver Stannard | 8b27308 | 2014-06-19 15:52:37 +0000 | [diff] [blame] | 2191 | assert(M && "expected macro to be defined"); |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2192 | Error(IDLoc, |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 2193 | "parameter named '" + FA.Name + "' does not exist for macro '" + |
Saleem Abdulrasool | 3f44cd7 | 2014-03-17 17:13:57 +0000 | [diff] [blame] | 2194 | M->Name + "'"); |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2195 | return true; |
| 2196 | } |
| 2197 | PI = FAI; |
| 2198 | } |
| 2199 | |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 2200 | if (!FA.Value.empty()) { |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2201 | if (A.size() <= PI) |
| 2202 | A.resize(PI + 1); |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 2203 | A[PI] = FA.Value; |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2204 | |
| 2205 | if (FALocs.size() <= PI) |
| 2206 | FALocs.resize(PI + 1); |
| 2207 | |
| 2208 | FALocs[PI] = Lexer.getLoc(); |
Preston Gurd | 242ed315 | 2012-09-19 20:29:04 +0000 | [diff] [blame] | 2209 | } |
Jim Grosbach | 20666162 | 2012-07-30 22:44:17 +0000 | [diff] [blame] | 2210 | |
Preston Gurd | 242ed315 | 2012-09-19 20:29:04 +0000 | [diff] [blame] | 2211 | // At the end of the statement, fill in remaining arguments that have |
| 2212 | // default values. If there aren't any, then the next argument is |
| 2213 | // required but missing |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 2214 | if (Lexer.is(AsmToken::EndOfStatement)) { |
| 2215 | bool Failure = false; |
| 2216 | for (unsigned FAI = 0; FAI < NParameters; ++FAI) { |
| 2217 | if (A[FAI].empty()) { |
| 2218 | if (M->Parameters[FAI].Required) { |
| 2219 | Error(FALocs[FAI].isValid() ? FALocs[FAI] : Lexer.getLoc(), |
| 2220 | "missing value for required parameter " |
| 2221 | "'" + M->Parameters[FAI].Name + "' in macro '" + M->Name + "'"); |
| 2222 | Failure = true; |
| 2223 | } |
| 2224 | |
| 2225 | if (!M->Parameters[FAI].Value.empty()) |
| 2226 | A[FAI] = M->Parameters[FAI].Value; |
| 2227 | } |
| 2228 | } |
| 2229 | return Failure; |
| 2230 | } |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2231 | |
| 2232 | if (Lexer.is(AsmToken::Comma)) |
| 2233 | Lex(); |
| 2234 | } |
Saleem Abdulrasool | 6d7c0c2 | 2014-02-17 00:40:17 +0000 | [diff] [blame] | 2235 | |
| 2236 | return TokError("too many positional arguments"); |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2237 | } |
| 2238 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2239 | const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) { |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 2240 | StringMap<MCAsmMacro>::iterator I = MacroMap.find(Name); |
| 2241 | return (I == MacroMap.end()) ? nullptr : &I->getValue(); |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 2242 | } |
| 2243 | |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 2244 | void AsmParser::defineMacro(StringRef Name, MCAsmMacro Macro) { |
| 2245 | MacroMap.insert(std::make_pair(Name, std::move(Macro))); |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 2246 | } |
| 2247 | |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 2248 | void AsmParser::undefineMacro(StringRef Name) { MacroMap.erase(Name); } |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 2249 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2250 | bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) { |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2251 | // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate |
| 2252 | // this, although we should protect against infinite loops. |
| 2253 | if (ActiveMacros.size() == 20) |
| 2254 | return TokError("macros cannot be nested more than 20 levels deep"); |
| 2255 | |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 2256 | MCAsmMacroArguments A; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2257 | if (parseMacroArguments(M, A)) |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 2258 | return true; |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2259 | |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2260 | // Macro instantiation is lexical, unfortunately. We construct a new buffer |
| 2261 | // to hold the macro body with substitutions. |
| 2262 | SmallString<256> Buf; |
| 2263 | StringRef Body = M->Body; |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 2264 | raw_svector_ostream OS(Buf); |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2265 | |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 2266 | if (expandMacro(OS, Body, M->Parameters, A, true, getTok().getLoc())) |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2267 | return true; |
| 2268 | |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 2269 | // 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] | 2270 | // instantiation. |
| 2271 | OS << ".endmacro\n"; |
| 2272 | |
Rafael Espindola | 3560ff2 | 2014-08-27 20:03:13 +0000 | [diff] [blame] | 2273 | std::unique_ptr<MemoryBuffer> Instantiation = |
| 2274 | MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>"); |
Rafael Espindola | 1134ab23 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 2275 | |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2276 | // Create the macro instantiation object and add to the current macro |
| 2277 | // instantiation stack. |
Rafael Espindola | 9eef18c | 2014-08-27 19:49:03 +0000 | [diff] [blame] | 2278 | MacroInstantiation *MI = new MacroInstantiation( |
| 2279 | NameLoc, CurBuffer, getTok().getLoc(), TheCondStack.size()); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2280 | ActiveMacros.push_back(MI); |
| 2281 | |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 2282 | ++NumOfMacroInstantiations; |
| 2283 | |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2284 | // Jump to the macro instantiation and prime the lexer. |
David Blaikie | 1961f14 | 2014-08-21 20:44:56 +0000 | [diff] [blame] | 2285 | CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc()); |
Rafael Espindola | 8026bd0 | 2014-07-06 14:17:29 +0000 | [diff] [blame] | 2286 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer()); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2287 | Lex(); |
| 2288 | |
| 2289 | return false; |
| 2290 | } |
| 2291 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2292 | void AsmParser::handleMacroExit() { |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2293 | // Jump to the EndOfStatement we should return to, and consume it. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2294 | jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer); |
Daniel Dunbar | 4323571 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 2295 | Lex(); |
| 2296 | |
| 2297 | // Pop the instantiation entry. |
| 2298 | delete ActiveMacros.back(); |
| 2299 | ActiveMacros.pop_back(); |
| 2300 | } |
| 2301 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2302 | bool AsmParser::parseAssignment(StringRef Name, bool allow_redef, |
Jim Grosbach | b7b750d | 2012-09-13 23:11:31 +0000 | [diff] [blame] | 2303 | bool NoDeadStrip) { |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 2304 | MCSymbol *Sym; |
Daniel Dunbar | 897ffad | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 2305 | const MCExpr *Value; |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 2306 | if (MCParserUtils::parseAssignmentExpression(Name, allow_redef, *this, Sym, |
| 2307 | Value)) |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2308 | return true; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2309 | |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 2310 | if (!Sym) { |
| 2311 | // In the case where we parse an expression starting with a '.', we will |
| 2312 | // not generate an error, nor will we create a symbol. In this case we |
| 2313 | // should just return out. |
Anders Waldenborg | 8480957 | 2014-02-17 20:48:32 +0000 | [diff] [blame] | 2314 | return false; |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 2315 | } |
David Majnemer | 58cb80c | 2014-12-24 10:27:50 +0000 | [diff] [blame] | 2316 | |
Daniel Dunbar | ae7ac01 | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 2317 | // Do the assignment. |
Daniel Dunbar | b7b2097 | 2009-08-31 08:09:09 +0000 | [diff] [blame] | 2318 | Out.EmitAssignment(Sym, Value); |
Jim Grosbach | b7b750d | 2012-09-13 23:11:31 +0000 | [diff] [blame] | 2319 | if (NoDeadStrip) |
| 2320 | Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip); |
| 2321 | |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2322 | return false; |
| 2323 | } |
| 2324 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2325 | /// parseIdentifier: |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 2326 | /// ::= identifier |
| 2327 | /// ::= string |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2328 | bool AsmParser::parseIdentifier(StringRef &Res) { |
Daniel Dunbar | 3b96ffd | 2010-08-24 18:12:12 +0000 | [diff] [blame] | 2329 | // The assembler has relaxed rules for accepting identifiers, in particular we |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 2330 | // allow things like '.globl $foo' and '.def @feat.00', which would normally be |
| 2331 | // 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] | 2332 | // handle this as a context dependent token, instead we detect adjacent tokens |
| 2333 | // and return the combined identifier. |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 2334 | if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) { |
| 2335 | SMLoc PrefixLoc = getLexer().getLoc(); |
Daniel Dunbar | 3b96ffd | 2010-08-24 18:12:12 +0000 | [diff] [blame] | 2336 | |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 2337 | // Consume the prefix character, and check for a following identifier. |
Daniel Dunbar | 3b96ffd | 2010-08-24 18:12:12 +0000 | [diff] [blame] | 2338 | Lex(); |
| 2339 | if (Lexer.isNot(AsmToken::Identifier)) |
| 2340 | return true; |
| 2341 | |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 2342 | // We have a '$' or '@' followed by an identifier, make sure they are adjacent. |
| 2343 | if (PrefixLoc.getPointer() + 1 != getTok().getLoc().getPointer()) |
Daniel Dunbar | 3b96ffd | 2010-08-24 18:12:12 +0000 | [diff] [blame] | 2344 | return true; |
| 2345 | |
| 2346 | // Construct the joined identifier and consume the token. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2347 | Res = |
Hans Wennborg | ce69d77 | 2013-10-18 20:46:28 +0000 | [diff] [blame] | 2348 | StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1); |
Daniel Dunbar | 3b96ffd | 2010-08-24 18:12:12 +0000 | [diff] [blame] | 2349 | Lex(); |
| 2350 | return false; |
| 2351 | } |
| 2352 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2353 | if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String)) |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 2354 | return true; |
| 2355 | |
Sean Callanan | 936b0d3 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 2356 | Res = getTok().getIdentifier(); |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 2357 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2358 | Lex(); // Consume the identifier token. |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 2359 | |
| 2360 | return false; |
| 2361 | } |
| 2362 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2363 | /// parseDirectiveSet: |
Nico Weber | 4ada0d9 | 2011-01-28 03:04:41 +0000 | [diff] [blame] | 2364 | /// ::= .equ identifier ',' expression |
| 2365 | /// ::= .equiv identifier ',' expression |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2366 | /// ::= .set identifier ',' expression |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2367 | bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) { |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 2368 | StringRef Name; |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2369 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2370 | if (parseIdentifier(Name)) |
Roman Divacky | 41e6ceb | 2010-10-28 16:57:58 +0000 | [diff] [blame] | 2371 | return TokError("expected identifier after '" + Twine(IDVal) + "'"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2372 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2373 | if (getLexer().isNot(AsmToken::Comma)) |
Roman Divacky | 41e6ceb | 2010-10-28 16:57:58 +0000 | [diff] [blame] | 2374 | return TokError("unexpected token in '" + Twine(IDVal) + "'"); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2375 | Lex(); |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2376 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2377 | return parseAssignment(Name, allow_redef, true); |
Daniel Dunbar | 2d2ee15 | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2378 | } |
| 2379 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2380 | bool AsmParser::parseEscapedString(std::string &Data) { |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2381 | assert(getLexer().is(AsmToken::String) && "Unexpected current token!"); |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2382 | |
| 2383 | Data = ""; |
Sean Callanan | 936b0d3 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 2384 | StringRef Str = getTok().getStringContents(); |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2385 | for (unsigned i = 0, e = Str.size(); i != e; ++i) { |
| 2386 | if (Str[i] != '\\') { |
| 2387 | Data += Str[i]; |
| 2388 | continue; |
| 2389 | } |
| 2390 | |
| 2391 | // Recognize escaped characters. Note that this escape semantics currently |
| 2392 | // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes. |
| 2393 | ++i; |
| 2394 | if (i == e) |
| 2395 | return TokError("unexpected backslash at end of string"); |
| 2396 | |
| 2397 | // Recognize octal sequences. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2398 | if ((unsigned)(Str[i] - '0') <= 7) { |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2399 | // Consume up to three octal characters. |
| 2400 | unsigned Value = Str[i] - '0'; |
| 2401 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2402 | if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) { |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2403 | ++i; |
| 2404 | Value = Value * 8 + (Str[i] - '0'); |
| 2405 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2406 | if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) { |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2407 | ++i; |
| 2408 | Value = Value * 8 + (Str[i] - '0'); |
| 2409 | } |
| 2410 | } |
| 2411 | |
| 2412 | if (Value > 255) |
| 2413 | return TokError("invalid octal escape sequence (out of range)"); |
| 2414 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2415 | Data += (unsigned char)Value; |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2416 | continue; |
| 2417 | } |
| 2418 | |
| 2419 | // Otherwise recognize individual escapes. |
| 2420 | switch (Str[i]) { |
| 2421 | default: |
| 2422 | // Just reject invalid escape sequences for now. |
| 2423 | return TokError("invalid escape sequence (unrecognized character)"); |
| 2424 | |
| 2425 | case 'b': Data += '\b'; break; |
| 2426 | case 'f': Data += '\f'; break; |
| 2427 | case 'n': Data += '\n'; break; |
| 2428 | case 'r': Data += '\r'; break; |
| 2429 | case 't': Data += '\t'; break; |
| 2430 | case '"': Data += '"'; break; |
| 2431 | case '\\': Data += '\\'; break; |
| 2432 | } |
| 2433 | } |
| 2434 | |
| 2435 | return false; |
| 2436 | } |
| 2437 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2438 | /// parseDirectiveAscii: |
Rafael Espindola | 63760ba | 2010-10-28 20:02:27 +0000 | [diff] [blame] | 2439 | /// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2440 | bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) { |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2441 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2442 | checkForValidSection(); |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 2443 | |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2444 | for (;;) { |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2445 | if (getLexer().isNot(AsmToken::String)) |
Rafael Espindola | 63760ba | 2010-10-28 20:02:27 +0000 | [diff] [blame] | 2446 | return TokError("expected string in '" + Twine(IDVal) + "' directive"); |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2447 | |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2448 | std::string Data; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2449 | if (parseEscapedString(Data)) |
Daniel Dunbar | ef668c1 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2450 | return true; |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2451 | |
Rafael Espindola | 64e1af8 | 2013-07-02 15:49:13 +0000 | [diff] [blame] | 2452 | getStreamer().EmitBytes(Data); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2453 | if (ZeroTerminated) |
Rafael Espindola | 64e1af8 | 2013-07-02 15:49:13 +0000 | [diff] [blame] | 2454 | getStreamer().EmitBytes(StringRef("\0", 1)); |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2455 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2456 | Lex(); |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2457 | |
| 2458 | if (getLexer().is(AsmToken::EndOfStatement)) |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2459 | break; |
| 2460 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2461 | if (getLexer().isNot(AsmToken::Comma)) |
Rafael Espindola | 63760ba | 2010-10-28 20:02:27 +0000 | [diff] [blame] | 2462 | return TokError("unexpected token in '" + Twine(IDVal) + "' directive"); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2463 | Lex(); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2464 | } |
| 2465 | } |
| 2466 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2467 | Lex(); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2468 | return false; |
| 2469 | } |
| 2470 | |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 2471 | /// parseDirectiveReloc |
| 2472 | /// ::= .reloc expression , identifier [ , expression ] |
| 2473 | bool AsmParser::parseDirectiveReloc(SMLoc DirectiveLoc) { |
| 2474 | const MCExpr *Offset; |
| 2475 | const MCExpr *Expr = nullptr; |
| 2476 | |
| 2477 | SMLoc OffsetLoc = Lexer.getTok().getLoc(); |
| 2478 | if (parseExpression(Offset)) |
| 2479 | return true; |
| 2480 | |
| 2481 | // We can only deal with constant expressions at the moment. |
| 2482 | int64_t OffsetValue; |
| 2483 | if (!Offset->evaluateAsAbsolute(OffsetValue)) |
| 2484 | return Error(OffsetLoc, "expression is not a constant value"); |
| 2485 | |
| 2486 | if (Lexer.isNot(AsmToken::Comma)) |
| 2487 | return TokError("expected comma"); |
| 2488 | Lexer.Lex(); |
| 2489 | |
| 2490 | if (Lexer.isNot(AsmToken::Identifier)) |
| 2491 | return TokError("expected relocation name"); |
| 2492 | SMLoc NameLoc = Lexer.getTok().getLoc(); |
| 2493 | StringRef Name = Lexer.getTok().getIdentifier(); |
| 2494 | Lexer.Lex(); |
| 2495 | |
| 2496 | if (Lexer.is(AsmToken::Comma)) { |
| 2497 | Lexer.Lex(); |
| 2498 | SMLoc ExprLoc = Lexer.getLoc(); |
| 2499 | if (parseExpression(Expr)) |
| 2500 | return true; |
| 2501 | |
| 2502 | MCValue Value; |
| 2503 | if (!Expr->evaluateAsRelocatable(Value, nullptr, nullptr)) |
| 2504 | return Error(ExprLoc, "expression must be relocatable"); |
| 2505 | } |
| 2506 | |
| 2507 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
| 2508 | return TokError("unexpected token in .reloc directive"); |
| 2509 | |
| 2510 | if (getStreamer().EmitRelocDirective(*Offset, Name, Expr, DirectiveLoc)) |
| 2511 | return Error(NameLoc, "unknown relocation name"); |
| 2512 | |
| 2513 | return false; |
| 2514 | } |
| 2515 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2516 | /// parseDirectiveValue |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2517 | /// ::= (.byte | .short | ... ) [ expression (, expression)* ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2518 | bool AsmParser::parseDirectiveValue(unsigned Size) { |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2519 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2520 | checkForValidSection(); |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 2521 | |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2522 | for (;;) { |
Daniel Dunbar | 897ffad | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 2523 | const MCExpr *Value; |
Jim Grosbach | 76346c3 | 2011-06-29 16:05:14 +0000 | [diff] [blame] | 2524 | SMLoc ExprLoc = getLexer().getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2525 | if (parseExpression(Value)) |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2526 | return true; |
| 2527 | |
Daniel Dunbar | 6738a2e | 2010-05-23 18:36:38 +0000 | [diff] [blame] | 2528 | // Special case constant expressions to match code generator. |
Jim Grosbach | 76346c3 | 2011-06-29 16:05:14 +0000 | [diff] [blame] | 2529 | if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) { |
| 2530 | assert(Size <= 8 && "Invalid size"); |
| 2531 | uint64_t IntValue = MCE->getValue(); |
| 2532 | if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue)) |
| 2533 | return Error(ExprLoc, "literal value out of range for directive"); |
Rafael Espindola | 64e1af8 | 2013-07-02 15:49:13 +0000 | [diff] [blame] | 2534 | getStreamer().EmitIntValue(IntValue, Size); |
Jim Grosbach | 76346c3 | 2011-06-29 16:05:14 +0000 | [diff] [blame] | 2535 | } else |
Kevin Enderby | 96918bc | 2014-04-22 17:27:29 +0000 | [diff] [blame] | 2536 | getStreamer().EmitValue(Value, Size, ExprLoc); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2537 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2538 | if (getLexer().is(AsmToken::EndOfStatement)) |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2539 | break; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2540 | |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2541 | // FIXME: Improve diagnostic. |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2542 | if (getLexer().isNot(AsmToken::Comma)) |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2543 | return TokError("unexpected token in directive"); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2544 | Lex(); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2545 | } |
| 2546 | } |
| 2547 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2548 | Lex(); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2549 | return false; |
| 2550 | } |
| 2551 | |
David Woodhouse | d6de0d9 | 2014-02-01 16:20:59 +0000 | [diff] [blame] | 2552 | /// ParseDirectiveOctaValue |
| 2553 | /// ::= .octa [ hexconstant (, hexconstant)* ] |
| 2554 | bool AsmParser::parseDirectiveOctaValue() { |
| 2555 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2556 | checkForValidSection(); |
| 2557 | |
| 2558 | for (;;) { |
| 2559 | if (Lexer.getKind() == AsmToken::Error) |
| 2560 | return true; |
| 2561 | if (Lexer.getKind() != AsmToken::Integer && |
| 2562 | Lexer.getKind() != AsmToken::BigNum) |
| 2563 | return TokError("unknown token in expression"); |
| 2564 | |
| 2565 | SMLoc ExprLoc = getLexer().getLoc(); |
| 2566 | APInt IntValue = getTok().getAPIntVal(); |
| 2567 | Lex(); |
| 2568 | |
| 2569 | uint64_t hi, lo; |
| 2570 | if (IntValue.isIntN(64)) { |
| 2571 | hi = 0; |
| 2572 | lo = IntValue.getZExtValue(); |
| 2573 | } else if (IntValue.isIntN(128)) { |
David Woodhouse | 6c9a6f9 | 2014-02-01 16:52:33 +0000 | [diff] [blame] | 2574 | // It might actually have more than 128 bits, but the top ones are zero. |
| 2575 | hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue(); |
David Woodhouse | d6de0d9 | 2014-02-01 16:20:59 +0000 | [diff] [blame] | 2576 | lo = IntValue.getLoBits(64).getZExtValue(); |
| 2577 | } else |
| 2578 | return Error(ExprLoc, "literal value out of range for directive"); |
| 2579 | |
| 2580 | if (MAI.isLittleEndian()) { |
| 2581 | getStreamer().EmitIntValue(lo, 8); |
| 2582 | getStreamer().EmitIntValue(hi, 8); |
| 2583 | } else { |
| 2584 | getStreamer().EmitIntValue(hi, 8); |
| 2585 | getStreamer().EmitIntValue(lo, 8); |
| 2586 | } |
| 2587 | |
| 2588 | if (getLexer().is(AsmToken::EndOfStatement)) |
| 2589 | break; |
| 2590 | |
| 2591 | // FIXME: Improve diagnostic. |
| 2592 | if (getLexer().isNot(AsmToken::Comma)) |
| 2593 | return TokError("unexpected token in directive"); |
| 2594 | Lex(); |
| 2595 | } |
| 2596 | } |
| 2597 | |
| 2598 | Lex(); |
| 2599 | return false; |
| 2600 | } |
| 2601 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2602 | /// parseDirectiveRealValue |
Daniel Dunbar | 2af1653 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 2603 | /// ::= (.single | .double) [ expression (, expression)* ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2604 | bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) { |
Daniel Dunbar | 2af1653 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 2605 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2606 | checkForValidSection(); |
Daniel Dunbar | 2af1653 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 2607 | |
| 2608 | for (;;) { |
| 2609 | // We don't truly support arithmetic on floating point expressions, so we |
| 2610 | // have to manually parse unary prefixes. |
| 2611 | bool IsNeg = false; |
| 2612 | if (getLexer().is(AsmToken::Minus)) { |
| 2613 | Lex(); |
| 2614 | IsNeg = true; |
| 2615 | } else if (getLexer().is(AsmToken::Plus)) |
| 2616 | Lex(); |
| 2617 | |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2618 | if (getLexer().isNot(AsmToken::Integer) && |
Kevin Enderby | 5bbe957 | 2011-03-29 21:11:52 +0000 | [diff] [blame] | 2619 | getLexer().isNot(AsmToken::Real) && |
| 2620 | getLexer().isNot(AsmToken::Identifier)) |
Daniel Dunbar | 2af1653 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 2621 | return TokError("unexpected token in directive"); |
| 2622 | |
| 2623 | // Convert to an APFloat. |
| 2624 | APFloat Value(Semantics); |
Kevin Enderby | 5bbe957 | 2011-03-29 21:11:52 +0000 | [diff] [blame] | 2625 | StringRef IDVal = getTok().getString(); |
| 2626 | if (getLexer().is(AsmToken::Identifier)) { |
| 2627 | if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf")) |
| 2628 | Value = APFloat::getInf(Semantics); |
| 2629 | else if (!IDVal.compare_lower("nan")) |
| 2630 | Value = APFloat::getNaN(Semantics, false, ~0); |
| 2631 | else |
| 2632 | return TokError("invalid floating point literal"); |
| 2633 | } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) == |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2634 | APFloat::opInvalidOp) |
Daniel Dunbar | 2af1653 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 2635 | return TokError("invalid floating point literal"); |
| 2636 | if (IsNeg) |
| 2637 | Value.changeSign(); |
| 2638 | |
| 2639 | // Consume the numeric token. |
| 2640 | Lex(); |
| 2641 | |
| 2642 | // Emit the value as an integer. |
| 2643 | APInt AsInt = Value.bitcastToAPInt(); |
| 2644 | getStreamer().EmitIntValue(AsInt.getLimitedValue(), |
Rafael Espindola | 64e1af8 | 2013-07-02 15:49:13 +0000 | [diff] [blame] | 2645 | AsInt.getBitWidth() / 8); |
Daniel Dunbar | 2af1653 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 2646 | |
| 2647 | if (getLexer().is(AsmToken::EndOfStatement)) |
| 2648 | break; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2649 | |
Daniel Dunbar | 2af1653 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 2650 | if (getLexer().isNot(AsmToken::Comma)) |
| 2651 | return TokError("unexpected token in directive"); |
| 2652 | Lex(); |
| 2653 | } |
| 2654 | } |
| 2655 | |
| 2656 | Lex(); |
| 2657 | return false; |
| 2658 | } |
| 2659 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2660 | /// parseDirectiveZero |
Rafael Espindola | 922e3f4 | 2010-09-16 15:03:59 +0000 | [diff] [blame] | 2661 | /// ::= .zero expression |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2662 | bool AsmParser::parseDirectiveZero() { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2663 | checkForValidSection(); |
Rafael Espindola | 922e3f4 | 2010-09-16 15:03:59 +0000 | [diff] [blame] | 2664 | |
| 2665 | int64_t NumBytes; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2666 | if (parseAbsoluteExpression(NumBytes)) |
Rafael Espindola | 922e3f4 | 2010-09-16 15:03:59 +0000 | [diff] [blame] | 2667 | return true; |
| 2668 | |
Rafael Espindola | b91bac6 | 2010-10-05 19:42:57 +0000 | [diff] [blame] | 2669 | int64_t Val = 0; |
| 2670 | if (getLexer().is(AsmToken::Comma)) { |
| 2671 | Lex(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2672 | if (parseAbsoluteExpression(Val)) |
Rafael Espindola | b91bac6 | 2010-10-05 19:42:57 +0000 | [diff] [blame] | 2673 | return true; |
| 2674 | } |
| 2675 | |
Rafael Espindola | 922e3f4 | 2010-09-16 15:03:59 +0000 | [diff] [blame] | 2676 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 2677 | return TokError("unexpected token in '.zero' directive"); |
| 2678 | |
| 2679 | Lex(); |
| 2680 | |
Rafael Espindola | 64e1af8 | 2013-07-02 15:49:13 +0000 | [diff] [blame] | 2681 | getStreamer().EmitFill(NumBytes, Val); |
Rafael Espindola | 922e3f4 | 2010-09-16 15:03:59 +0000 | [diff] [blame] | 2682 | |
| 2683 | return false; |
| 2684 | } |
| 2685 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2686 | /// parseDirectiveFill |
Roman Divacky | e33098f | 2013-09-24 17:44:41 +0000 | [diff] [blame] | 2687 | /// ::= .fill expression [ , expression [ , expression ] ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2688 | bool AsmParser::parseDirectiveFill() { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2689 | checkForValidSection(); |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 2690 | |
David Majnemer | 522d3db | 2014-02-01 07:19:38 +0000 | [diff] [blame] | 2691 | SMLoc RepeatLoc = getLexer().getLoc(); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2692 | int64_t NumValues; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2693 | if (parseAbsoluteExpression(NumValues)) |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2694 | return true; |
| 2695 | |
David Majnemer | 522d3db | 2014-02-01 07:19:38 +0000 | [diff] [blame] | 2696 | if (NumValues < 0) { |
| 2697 | Warning(RepeatLoc, |
| 2698 | "'.fill' directive with negative repeat count has no effect"); |
| 2699 | NumValues = 0; |
| 2700 | } |
| 2701 | |
Roman Divacky | e33098f | 2013-09-24 17:44:41 +0000 | [diff] [blame] | 2702 | int64_t FillSize = 1; |
| 2703 | int64_t FillExpr = 0; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2704 | |
David Majnemer | 522d3db | 2014-02-01 07:19:38 +0000 | [diff] [blame] | 2705 | SMLoc SizeLoc, ExprLoc; |
Roman Divacky | e33098f | 2013-09-24 17:44:41 +0000 | [diff] [blame] | 2706 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2707 | if (getLexer().isNot(AsmToken::Comma)) |
| 2708 | return TokError("unexpected token in '.fill' directive"); |
| 2709 | Lex(); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2710 | |
David Majnemer | 522d3db | 2014-02-01 07:19:38 +0000 | [diff] [blame] | 2711 | SizeLoc = getLexer().getLoc(); |
Roman Divacky | e33098f | 2013-09-24 17:44:41 +0000 | [diff] [blame] | 2712 | if (parseAbsoluteExpression(FillSize)) |
| 2713 | return true; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2714 | |
Roman Divacky | e33098f | 2013-09-24 17:44:41 +0000 | [diff] [blame] | 2715 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2716 | if (getLexer().isNot(AsmToken::Comma)) |
| 2717 | return TokError("unexpected token in '.fill' directive"); |
| 2718 | Lex(); |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2719 | |
David Majnemer | 522d3db | 2014-02-01 07:19:38 +0000 | [diff] [blame] | 2720 | ExprLoc = getLexer().getLoc(); |
Roman Divacky | e33098f | 2013-09-24 17:44:41 +0000 | [diff] [blame] | 2721 | if (parseAbsoluteExpression(FillExpr)) |
| 2722 | return true; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2723 | |
Roman Divacky | e33098f | 2013-09-24 17:44:41 +0000 | [diff] [blame] | 2724 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 2725 | return TokError("unexpected token in '.fill' directive"); |
| 2726 | |
| 2727 | Lex(); |
| 2728 | } |
| 2729 | } |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2730 | |
David Majnemer | 522d3db | 2014-02-01 07:19:38 +0000 | [diff] [blame] | 2731 | if (FillSize < 0) { |
| 2732 | Warning(SizeLoc, "'.fill' directive with negative size has no effect"); |
| 2733 | NumValues = 0; |
| 2734 | } |
| 2735 | if (FillSize > 8) { |
| 2736 | Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8"); |
| 2737 | FillSize = 8; |
| 2738 | } |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2739 | |
David Majnemer | 522d3db | 2014-02-01 07:19:38 +0000 | [diff] [blame] | 2740 | if (!isUInt<32>(FillExpr) && FillSize > 4) |
| 2741 | Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits"); |
| 2742 | |
Alexey Samsonov | 1b0713c | 2014-09-02 17:25:29 +0000 | [diff] [blame] | 2743 | if (NumValues > 0) { |
| 2744 | int64_t NonZeroFillSize = FillSize > 4 ? 4 : FillSize; |
| 2745 | FillExpr &= ~0ULL >> (64 - NonZeroFillSize * 8); |
| 2746 | for (uint64_t i = 0, e = NumValues; i != e; ++i) { |
| 2747 | getStreamer().EmitIntValue(FillExpr, NonZeroFillSize); |
| 2748 | if (NonZeroFillSize < FillSize) |
| 2749 | getStreamer().EmitIntValue(0, FillSize - NonZeroFillSize); |
| 2750 | } |
David Majnemer | 522d3db | 2014-02-01 07:19:38 +0000 | [diff] [blame] | 2751 | } |
Daniel Dunbar | a10e519 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2752 | |
| 2753 | return false; |
| 2754 | } |
Daniel Dunbar | 4a5a561 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 2755 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2756 | /// parseDirectiveOrg |
Daniel Dunbar | 4a5a561 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 2757 | /// ::= .org expression [ , expression ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2758 | bool AsmParser::parseDirectiveOrg() { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2759 | checkForValidSection(); |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 2760 | |
Daniel Dunbar | 897ffad | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 2761 | const MCExpr *Offset; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2762 | if (parseExpression(Offset)) |
Daniel Dunbar | 4a5a561 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 2763 | return true; |
| 2764 | |
| 2765 | // Parse optional fill expression. |
| 2766 | int64_t FillExpr = 0; |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2767 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2768 | if (getLexer().isNot(AsmToken::Comma)) |
Daniel Dunbar | 4a5a561 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 2769 | return TokError("unexpected token in '.org' directive"); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2770 | Lex(); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2771 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2772 | if (parseAbsoluteExpression(FillExpr)) |
Daniel Dunbar | 4a5a561 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 2773 | return true; |
| 2774 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2775 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Daniel Dunbar | 4a5a561 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 2776 | return TokError("unexpected token in '.org' directive"); |
| 2777 | } |
| 2778 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2779 | Lex(); |
Rafael Espindola | 7ae65d8 | 2015-11-04 23:59:18 +0000 | [diff] [blame] | 2780 | getStreamer().emitValueToOffset(Offset, FillExpr); |
Daniel Dunbar | 4a5a561 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 2781 | return false; |
| 2782 | } |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2783 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2784 | /// parseDirectiveAlign |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2785 | /// ::= {.align, ...} expression [ , expression [ , expression ]] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2786 | bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2787 | checkForValidSection(); |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 2788 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2789 | SMLoc AlignmentLoc = getLexer().getLoc(); |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2790 | int64_t Alignment; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2791 | if (parseAbsoluteExpression(Alignment)) |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2792 | return true; |
| 2793 | |
| 2794 | SMLoc MaxBytesLoc; |
| 2795 | bool HasFillExpr = false; |
| 2796 | int64_t FillExpr = 0; |
| 2797 | int64_t MaxBytesToFill = 0; |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2798 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2799 | if (getLexer().isNot(AsmToken::Comma)) |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2800 | return TokError("unexpected token in directive"); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2801 | Lex(); |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2802 | |
| 2803 | // The fill expression can be omitted while specifying a maximum number of |
| 2804 | // alignment bytes, e.g: |
| 2805 | // .align 3,,4 |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2806 | if (getLexer().isNot(AsmToken::Comma)) { |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2807 | HasFillExpr = true; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2808 | if (parseAbsoluteExpression(FillExpr)) |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2809 | return true; |
| 2810 | } |
| 2811 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2812 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2813 | if (getLexer().isNot(AsmToken::Comma)) |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2814 | return TokError("unexpected token in directive"); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2815 | Lex(); |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2816 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2817 | MaxBytesLoc = getLexer().getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 2818 | if (parseAbsoluteExpression(MaxBytesToFill)) |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2819 | return true; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2820 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2821 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2822 | return TokError("unexpected token in directive"); |
| 2823 | } |
| 2824 | } |
| 2825 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2826 | Lex(); |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2827 | |
Daniel Dunbar | bb166be | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 2828 | if (!HasFillExpr) |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2829 | FillExpr = 0; |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2830 | |
| 2831 | // Compute alignment in bytes. |
| 2832 | if (IsPow2) { |
| 2833 | // FIXME: Diagnose overflow. |
Daniel Dunbar | 18f3c9b | 2009-08-26 09:16:34 +0000 | [diff] [blame] | 2834 | if (Alignment >= 32) { |
| 2835 | Error(AlignmentLoc, "invalid alignment value"); |
| 2836 | Alignment = 31; |
| 2837 | } |
| 2838 | |
Benjamin Kramer | 63951ad | 2009-09-06 09:35:10 +0000 | [diff] [blame] | 2839 | Alignment = 1ULL << Alignment; |
Benjamin Kramer | 64bf780 | 2013-02-16 15:00:16 +0000 | [diff] [blame] | 2840 | } else { |
Davide Italiano | cb2da71 | 2015-09-08 18:59:47 +0000 | [diff] [blame] | 2841 | // Reject alignments that aren't either a power of two or zero, |
| 2842 | // for gas compatibility. Alignment of zero is silently rounded |
| 2843 | // up to one. |
| 2844 | if (Alignment == 0) |
| 2845 | Alignment = 1; |
Benjamin Kramer | 64bf780 | 2013-02-16 15:00:16 +0000 | [diff] [blame] | 2846 | if (!isPowerOf2_64(Alignment)) |
| 2847 | Error(AlignmentLoc, "alignment must be a power of 2"); |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2848 | } |
| 2849 | |
Daniel Dunbar | 18f3c9b | 2009-08-26 09:16:34 +0000 | [diff] [blame] | 2850 | // Diagnose non-sensical max bytes to align. |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2851 | if (MaxBytesLoc.isValid()) { |
| 2852 | if (MaxBytesToFill < 1) { |
Daniel Dunbar | 18f3c9b | 2009-08-26 09:16:34 +0000 | [diff] [blame] | 2853 | Error(MaxBytesLoc, "alignment directive can never be satisfied in this " |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2854 | "many bytes, ignoring maximum bytes expression"); |
Daniel Dunbar | 4abcccb | 2009-08-21 23:01:53 +0000 | [diff] [blame] | 2855 | MaxBytesToFill = 0; |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2856 | } |
| 2857 | |
| 2858 | if (MaxBytesToFill >= Alignment) { |
Daniel Dunbar | c9dc78a | 2009-06-30 00:49:23 +0000 | [diff] [blame] | 2859 | Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and " |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2860 | "has no effect"); |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2861 | MaxBytesToFill = 0; |
| 2862 | } |
| 2863 | } |
| 2864 | |
Daniel Dunbar | bb166be | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 2865 | // Check whether we should use optimal code alignment for this .align |
| 2866 | // directive. |
Saleem Abdulrasool | 7f2f9f4 | 2014-03-21 05:13:23 +0000 | [diff] [blame] | 2867 | const MCSection *Section = getStreamer().getCurrentSection().first; |
| 2868 | assert(Section && "must have section to emit alignment"); |
| 2869 | bool UseCodeAlign = Section->UseCodeAlign(); |
Daniel Dunbar | bb166be | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 2870 | if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) && |
| 2871 | ValueSize == 1 && UseCodeAlign) { |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2872 | getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill); |
Daniel Dunbar | bb166be | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 2873 | } else { |
Kevin Enderby | 7f99302 | 2010-02-25 18:46:04 +0000 | [diff] [blame] | 2874 | // FIXME: Target specific behavior about how the "extra" bytes are filled. |
Chris Lattner | c2b3675 | 2010-07-15 21:19:31 +0000 | [diff] [blame] | 2875 | getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize, |
| 2876 | MaxBytesToFill); |
Daniel Dunbar | bb166be | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 2877 | } |
Daniel Dunbar | cc566a71 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2878 | |
| 2879 | return false; |
| 2880 | } |
| 2881 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2882 | /// parseDirectiveFile |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2883 | /// ::= .file [number] filename |
| 2884 | /// ::= .file number directory filename |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2885 | bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2886 | // FIXME: I'm not sure what this is. |
| 2887 | int64_t FileNumber = -1; |
| 2888 | SMLoc FileNumberLoc = getLexer().getLoc(); |
| 2889 | if (getLexer().is(AsmToken::Integer)) { |
| 2890 | FileNumber = getTok().getIntVal(); |
| 2891 | Lex(); |
| 2892 | |
| 2893 | if (FileNumber < 1) |
| 2894 | return TokError("file number less than one"); |
| 2895 | } |
| 2896 | |
| 2897 | if (getLexer().isNot(AsmToken::String)) |
| 2898 | return TokError("unexpected token in '.file' directive"); |
| 2899 | |
| 2900 | // Usually the directory and filename together, otherwise just the directory. |
Yunzhong Gao | 8c0f506 | 2013-09-05 19:14:26 +0000 | [diff] [blame] | 2901 | // Allow the strings to have escaped octal character sequence. |
| 2902 | std::string Path = getTok().getString(); |
| 2903 | if (parseEscapedString(Path)) |
| 2904 | return true; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2905 | Lex(); |
| 2906 | |
| 2907 | StringRef Directory; |
| 2908 | StringRef Filename; |
Yunzhong Gao | 8c0f506 | 2013-09-05 19:14:26 +0000 | [diff] [blame] | 2909 | std::string FilenameData; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2910 | if (getLexer().is(AsmToken::String)) { |
| 2911 | if (FileNumber == -1) |
| 2912 | return TokError("explicit path specified, but no file number"); |
Yunzhong Gao | 8c0f506 | 2013-09-05 19:14:26 +0000 | [diff] [blame] | 2913 | if (parseEscapedString(FilenameData)) |
| 2914 | return true; |
| 2915 | Filename = FilenameData; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2916 | Directory = Path; |
| 2917 | Lex(); |
| 2918 | } else { |
| 2919 | Filename = Path; |
| 2920 | } |
| 2921 | |
| 2922 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 2923 | return TokError("unexpected token in '.file' directive"); |
| 2924 | |
| 2925 | if (FileNumber == -1) |
| 2926 | getStreamer().EmitFileDirective(Filename); |
| 2927 | else { |
David Blaikie | dc3f01e | 2015-03-09 01:57:13 +0000 | [diff] [blame] | 2928 | if (getContext().getGenDwarfForAssembly()) |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2929 | Error(DirectiveLoc, |
| 2930 | "input can't have .file dwarf directives when -g is " |
| 2931 | "used to generate dwarf debug info for assembly code"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2932 | |
David Blaikie | c714ef4 | 2014-03-17 01:52:11 +0000 | [diff] [blame] | 2933 | if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename) == |
| 2934 | 0) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2935 | Error(FileNumberLoc, "file number already allocated"); |
| 2936 | } |
| 2937 | |
| 2938 | return false; |
| 2939 | } |
| 2940 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2941 | /// parseDirectiveLine |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2942 | /// ::= .line [number] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2943 | bool AsmParser::parseDirectiveLine() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2944 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2945 | if (getLexer().isNot(AsmToken::Integer)) |
| 2946 | return TokError("unexpected token in '.line' directive"); |
| 2947 | |
| 2948 | int64_t LineNumber = getTok().getIntVal(); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2949 | (void)LineNumber; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2950 | Lex(); |
| 2951 | |
| 2952 | // FIXME: Do something with the .line. |
| 2953 | } |
| 2954 | |
| 2955 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 2956 | return TokError("unexpected token in '.line' directive"); |
| 2957 | |
| 2958 | return false; |
| 2959 | } |
| 2960 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2961 | /// parseDirectiveLoc |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2962 | /// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end] |
| 2963 | /// [epilogue_begin] [is_stmt VALUE] [isa VALUE] |
| 2964 | /// The first number is a file number, must have been previously assigned with |
| 2965 | /// a .file directive, the second number is the line number and optionally the |
| 2966 | /// third number is a column position (zero if not specified). The remaining |
| 2967 | /// optional items are .loc sub-directives. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 2968 | bool AsmParser::parseDirectiveLoc() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2969 | if (getLexer().isNot(AsmToken::Integer)) |
| 2970 | return TokError("unexpected token in '.loc' directive"); |
| 2971 | int64_t FileNumber = getTok().getIntVal(); |
| 2972 | if (FileNumber < 1) |
| 2973 | return TokError("file number less than one in '.loc' directive"); |
| 2974 | if (!getContext().isValidDwarfFileNumber(FileNumber)) |
| 2975 | return TokError("unassigned file number in '.loc' directive"); |
| 2976 | Lex(); |
| 2977 | |
| 2978 | int64_t LineNumber = 0; |
| 2979 | if (getLexer().is(AsmToken::Integer)) { |
| 2980 | LineNumber = getTok().getIntVal(); |
Adrian Prantl | 6ac4003 | 2013-09-26 23:37:11 +0000 | [diff] [blame] | 2981 | if (LineNumber < 0) |
| 2982 | return TokError("line number less than zero in '.loc' directive"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2983 | Lex(); |
| 2984 | } |
| 2985 | |
| 2986 | int64_t ColumnPos = 0; |
| 2987 | if (getLexer().is(AsmToken::Integer)) { |
| 2988 | ColumnPos = getTok().getIntVal(); |
| 2989 | if (ColumnPos < 0) |
| 2990 | return TokError("column position less than zero in '.loc' directive"); |
| 2991 | Lex(); |
| 2992 | } |
| 2993 | |
| 2994 | unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0; |
| 2995 | unsigned Isa = 0; |
| 2996 | int64_t Discriminator = 0; |
| 2997 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2998 | for (;;) { |
| 2999 | if (getLexer().is(AsmToken::EndOfStatement)) |
| 3000 | break; |
| 3001 | |
| 3002 | StringRef Name; |
| 3003 | SMLoc Loc = getTok().getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3004 | if (parseIdentifier(Name)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3005 | return TokError("unexpected token in '.loc' directive"); |
| 3006 | |
| 3007 | if (Name == "basic_block") |
| 3008 | Flags |= DWARF2_FLAG_BASIC_BLOCK; |
| 3009 | else if (Name == "prologue_end") |
| 3010 | Flags |= DWARF2_FLAG_PROLOGUE_END; |
| 3011 | else if (Name == "epilogue_begin") |
| 3012 | Flags |= DWARF2_FLAG_EPILOGUE_BEGIN; |
| 3013 | else if (Name == "is_stmt") { |
| 3014 | Loc = getTok().getLoc(); |
| 3015 | const MCExpr *Value; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3016 | if (parseExpression(Value)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3017 | return true; |
| 3018 | // The expression must be the constant 0 or 1. |
| 3019 | if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) { |
| 3020 | int Value = MCE->getValue(); |
| 3021 | if (Value == 0) |
| 3022 | Flags &= ~DWARF2_FLAG_IS_STMT; |
| 3023 | else if (Value == 1) |
| 3024 | Flags |= DWARF2_FLAG_IS_STMT; |
| 3025 | else |
| 3026 | return Error(Loc, "is_stmt value not 0 or 1"); |
Craig Topper | f15655b | 2013-04-22 04:22:40 +0000 | [diff] [blame] | 3027 | } else { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3028 | return Error(Loc, "is_stmt value not the constant value of 0 or 1"); |
| 3029 | } |
Craig Topper | f15655b | 2013-04-22 04:22:40 +0000 | [diff] [blame] | 3030 | } else if (Name == "isa") { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3031 | Loc = getTok().getLoc(); |
| 3032 | const MCExpr *Value; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3033 | if (parseExpression(Value)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3034 | return true; |
| 3035 | // The expression must be a constant greater or equal to 0. |
| 3036 | if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) { |
| 3037 | int Value = MCE->getValue(); |
| 3038 | if (Value < 0) |
| 3039 | return Error(Loc, "isa number less than zero"); |
| 3040 | Isa = Value; |
Craig Topper | f15655b | 2013-04-22 04:22:40 +0000 | [diff] [blame] | 3041 | } else { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3042 | return Error(Loc, "isa number not a constant value"); |
| 3043 | } |
Craig Topper | f15655b | 2013-04-22 04:22:40 +0000 | [diff] [blame] | 3044 | } else if (Name == "discriminator") { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3045 | if (parseAbsoluteExpression(Discriminator)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3046 | return true; |
Craig Topper | f15655b | 2013-04-22 04:22:40 +0000 | [diff] [blame] | 3047 | } else { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3048 | return Error(Loc, "unknown sub-directive in '.loc' directive"); |
| 3049 | } |
| 3050 | |
| 3051 | if (getLexer().is(AsmToken::EndOfStatement)) |
| 3052 | break; |
| 3053 | } |
| 3054 | } |
| 3055 | |
| 3056 | getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags, |
| 3057 | Isa, Discriminator, StringRef()); |
| 3058 | |
| 3059 | return false; |
| 3060 | } |
| 3061 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3062 | /// parseDirectiveStabs |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3063 | /// ::= .stabs string, number, number, number |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3064 | bool AsmParser::parseDirectiveStabs() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3065 | return TokError("unsupported directive '.stabs'"); |
| 3066 | } |
| 3067 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3068 | /// parseDirectiveCFISections |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3069 | /// ::= .cfi_sections section [, section] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3070 | bool AsmParser::parseDirectiveCFISections() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3071 | StringRef Name; |
| 3072 | bool EH = false; |
| 3073 | bool Debug = false; |
| 3074 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3075 | if (parseIdentifier(Name)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3076 | return TokError("Expected an identifier"); |
| 3077 | |
| 3078 | if (Name == ".eh_frame") |
| 3079 | EH = true; |
| 3080 | else if (Name == ".debug_frame") |
| 3081 | Debug = true; |
| 3082 | |
| 3083 | if (getLexer().is(AsmToken::Comma)) { |
| 3084 | Lex(); |
| 3085 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3086 | if (parseIdentifier(Name)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3087 | return TokError("Expected an identifier"); |
| 3088 | |
| 3089 | if (Name == ".eh_frame") |
| 3090 | EH = true; |
| 3091 | else if (Name == ".debug_frame") |
| 3092 | Debug = true; |
| 3093 | } |
| 3094 | |
| 3095 | getStreamer().EmitCFISections(EH, Debug); |
| 3096 | return false; |
| 3097 | } |
| 3098 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3099 | /// parseDirectiveCFIStartProc |
David Majnemer | e035cf9 | 2014-01-27 17:20:25 +0000 | [diff] [blame] | 3100 | /// ::= .cfi_startproc [simple] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3101 | bool AsmParser::parseDirectiveCFIStartProc() { |
David Majnemer | e035cf9 | 2014-01-27 17:20:25 +0000 | [diff] [blame] | 3102 | StringRef Simple; |
| 3103 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3104 | if (parseIdentifier(Simple) || Simple != "simple") |
| 3105 | return TokError("unexpected token in .cfi_startproc directive"); |
| 3106 | |
Oliver Stannard | cf6bfb1 | 2014-11-03 12:19:03 +0000 | [diff] [blame] | 3107 | getStreamer().EmitCFIStartProc(!Simple.empty()); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3108 | return false; |
| 3109 | } |
| 3110 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3111 | /// parseDirectiveCFIEndProc |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3112 | /// ::= .cfi_endproc |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3113 | bool AsmParser::parseDirectiveCFIEndProc() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3114 | getStreamer().EmitCFIEndProc(); |
| 3115 | return false; |
| 3116 | } |
| 3117 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3118 | /// \brief parse register name or number. |
| 3119 | bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register, |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3120 | SMLoc DirectiveLoc) { |
| 3121 | unsigned RegNo; |
| 3122 | |
| 3123 | if (getLexer().isNot(AsmToken::Integer)) { |
| 3124 | if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc)) |
| 3125 | return true; |
Bill Wendling | bc07a89 | 2013-06-18 07:20:20 +0000 | [diff] [blame] | 3126 | Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3127 | } else |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3128 | return parseAbsoluteExpression(Register); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3129 | |
| 3130 | return false; |
| 3131 | } |
| 3132 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3133 | /// parseDirectiveCFIDefCfa |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3134 | /// ::= .cfi_def_cfa register, offset |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3135 | bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3136 | int64_t Register = 0; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3137 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3138 | return true; |
| 3139 | |
| 3140 | if (getLexer().isNot(AsmToken::Comma)) |
| 3141 | return TokError("unexpected token in directive"); |
| 3142 | Lex(); |
| 3143 | |
| 3144 | int64_t Offset = 0; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3145 | if (parseAbsoluteExpression(Offset)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3146 | return true; |
| 3147 | |
| 3148 | getStreamer().EmitCFIDefCfa(Register, Offset); |
| 3149 | return false; |
| 3150 | } |
| 3151 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3152 | /// parseDirectiveCFIDefCfaOffset |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3153 | /// ::= .cfi_def_cfa_offset offset |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3154 | bool AsmParser::parseDirectiveCFIDefCfaOffset() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3155 | int64_t Offset = 0; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3156 | if (parseAbsoluteExpression(Offset)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3157 | return true; |
| 3158 | |
| 3159 | getStreamer().EmitCFIDefCfaOffset(Offset); |
| 3160 | return false; |
| 3161 | } |
| 3162 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3163 | /// parseDirectiveCFIRegister |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3164 | /// ::= .cfi_register register, register |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3165 | bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3166 | int64_t Register1 = 0; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3167 | if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3168 | return true; |
| 3169 | |
| 3170 | if (getLexer().isNot(AsmToken::Comma)) |
| 3171 | return TokError("unexpected token in directive"); |
| 3172 | Lex(); |
| 3173 | |
| 3174 | int64_t Register2 = 0; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3175 | if (parseRegisterOrRegisterNumber(Register2, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3176 | return true; |
| 3177 | |
| 3178 | getStreamer().EmitCFIRegister(Register1, Register2); |
| 3179 | return false; |
| 3180 | } |
| 3181 | |
Venkatraman Govindaraju | 3816d43 | 2013-09-26 14:49:40 +0000 | [diff] [blame] | 3182 | /// parseDirectiveCFIWindowSave |
| 3183 | /// ::= .cfi_window_save |
| 3184 | bool AsmParser::parseDirectiveCFIWindowSave() { |
| 3185 | getStreamer().EmitCFIWindowSave(); |
| 3186 | return false; |
| 3187 | } |
| 3188 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3189 | /// parseDirectiveCFIAdjustCfaOffset |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3190 | /// ::= .cfi_adjust_cfa_offset adjustment |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3191 | bool AsmParser::parseDirectiveCFIAdjustCfaOffset() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3192 | int64_t Adjustment = 0; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3193 | if (parseAbsoluteExpression(Adjustment)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3194 | return true; |
| 3195 | |
| 3196 | getStreamer().EmitCFIAdjustCfaOffset(Adjustment); |
| 3197 | return false; |
| 3198 | } |
| 3199 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3200 | /// parseDirectiveCFIDefCfaRegister |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3201 | /// ::= .cfi_def_cfa_register register |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3202 | bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3203 | int64_t Register = 0; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3204 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3205 | return true; |
| 3206 | |
| 3207 | getStreamer().EmitCFIDefCfaRegister(Register); |
| 3208 | return false; |
| 3209 | } |
| 3210 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3211 | /// parseDirectiveCFIOffset |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3212 | /// ::= .cfi_offset register, offset |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3213 | bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3214 | int64_t Register = 0; |
| 3215 | int64_t Offset = 0; |
| 3216 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3217 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3218 | return true; |
| 3219 | |
| 3220 | if (getLexer().isNot(AsmToken::Comma)) |
| 3221 | return TokError("unexpected token in directive"); |
| 3222 | Lex(); |
| 3223 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3224 | if (parseAbsoluteExpression(Offset)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3225 | return true; |
| 3226 | |
| 3227 | getStreamer().EmitCFIOffset(Register, Offset); |
| 3228 | return false; |
| 3229 | } |
| 3230 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3231 | /// parseDirectiveCFIRelOffset |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3232 | /// ::= .cfi_rel_offset register, offset |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3233 | bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3234 | int64_t Register = 0; |
| 3235 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3236 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3237 | return true; |
| 3238 | |
| 3239 | if (getLexer().isNot(AsmToken::Comma)) |
| 3240 | return TokError("unexpected token in directive"); |
| 3241 | Lex(); |
| 3242 | |
| 3243 | int64_t Offset = 0; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3244 | if (parseAbsoluteExpression(Offset)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3245 | return true; |
| 3246 | |
| 3247 | getStreamer().EmitCFIRelOffset(Register, Offset); |
| 3248 | return false; |
| 3249 | } |
| 3250 | |
| 3251 | static bool isValidEncoding(int64_t Encoding) { |
| 3252 | if (Encoding & ~0xff) |
| 3253 | return false; |
| 3254 | |
| 3255 | if (Encoding == dwarf::DW_EH_PE_omit) |
| 3256 | return true; |
| 3257 | |
| 3258 | const unsigned Format = Encoding & 0xf; |
| 3259 | if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 && |
| 3260 | Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 && |
| 3261 | Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 && |
| 3262 | Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed) |
| 3263 | return false; |
| 3264 | |
| 3265 | const unsigned Application = Encoding & 0x70; |
| 3266 | if (Application != dwarf::DW_EH_PE_absptr && |
| 3267 | Application != dwarf::DW_EH_PE_pcrel) |
| 3268 | return false; |
| 3269 | |
| 3270 | return true; |
| 3271 | } |
| 3272 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3273 | /// parseDirectiveCFIPersonalityOrLsda |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3274 | /// IsPersonality true for cfi_personality, false for cfi_lsda |
| 3275 | /// ::= .cfi_personality encoding, [symbol_name] |
| 3276 | /// ::= .cfi_lsda encoding, [symbol_name] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3277 | bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3278 | int64_t Encoding = 0; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3279 | if (parseAbsoluteExpression(Encoding)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3280 | return true; |
| 3281 | if (Encoding == dwarf::DW_EH_PE_omit) |
| 3282 | return false; |
| 3283 | |
| 3284 | if (!isValidEncoding(Encoding)) |
| 3285 | return TokError("unsupported encoding."); |
| 3286 | |
| 3287 | if (getLexer().isNot(AsmToken::Comma)) |
| 3288 | return TokError("unexpected token in directive"); |
| 3289 | Lex(); |
| 3290 | |
| 3291 | StringRef Name; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3292 | if (parseIdentifier(Name)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3293 | return TokError("expected identifier in directive"); |
| 3294 | |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 3295 | MCSymbol *Sym = getContext().getOrCreateSymbol(Name); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3296 | |
| 3297 | if (IsPersonality) |
| 3298 | getStreamer().EmitCFIPersonality(Sym, Encoding); |
| 3299 | else |
| 3300 | getStreamer().EmitCFILsda(Sym, Encoding); |
| 3301 | return false; |
| 3302 | } |
| 3303 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3304 | /// parseDirectiveCFIRememberState |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3305 | /// ::= .cfi_remember_state |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3306 | bool AsmParser::parseDirectiveCFIRememberState() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3307 | getStreamer().EmitCFIRememberState(); |
| 3308 | return false; |
| 3309 | } |
| 3310 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3311 | /// parseDirectiveCFIRestoreState |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3312 | /// ::= .cfi_remember_state |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3313 | bool AsmParser::parseDirectiveCFIRestoreState() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3314 | getStreamer().EmitCFIRestoreState(); |
| 3315 | return false; |
| 3316 | } |
| 3317 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3318 | /// parseDirectiveCFISameValue |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3319 | /// ::= .cfi_same_value register |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3320 | bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3321 | int64_t Register = 0; |
| 3322 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3323 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3324 | return true; |
| 3325 | |
| 3326 | getStreamer().EmitCFISameValue(Register); |
| 3327 | return false; |
| 3328 | } |
| 3329 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3330 | /// parseDirectiveCFIRestore |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3331 | /// ::= .cfi_restore register |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3332 | bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3333 | int64_t Register = 0; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3334 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3335 | return true; |
| 3336 | |
| 3337 | getStreamer().EmitCFIRestore(Register); |
| 3338 | return false; |
| 3339 | } |
| 3340 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3341 | /// parseDirectiveCFIEscape |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3342 | /// ::= .cfi_escape expression[,...] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3343 | bool AsmParser::parseDirectiveCFIEscape() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3344 | std::string Values; |
| 3345 | int64_t CurrValue; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3346 | if (parseAbsoluteExpression(CurrValue)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3347 | return true; |
| 3348 | |
| 3349 | Values.push_back((uint8_t)CurrValue); |
| 3350 | |
| 3351 | while (getLexer().is(AsmToken::Comma)) { |
| 3352 | Lex(); |
| 3353 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3354 | if (parseAbsoluteExpression(CurrValue)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3355 | return true; |
| 3356 | |
| 3357 | Values.push_back((uint8_t)CurrValue); |
| 3358 | } |
| 3359 | |
| 3360 | getStreamer().EmitCFIEscape(Values); |
| 3361 | return false; |
| 3362 | } |
| 3363 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3364 | /// parseDirectiveCFISignalFrame |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3365 | /// ::= .cfi_signal_frame |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3366 | bool AsmParser::parseDirectiveCFISignalFrame() { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3367 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3368 | return Error(getLexer().getLoc(), |
| 3369 | "unexpected token in '.cfi_signal_frame'"); |
| 3370 | |
| 3371 | getStreamer().EmitCFISignalFrame(); |
| 3372 | return false; |
| 3373 | } |
| 3374 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3375 | /// parseDirectiveCFIUndefined |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3376 | /// ::= .cfi_undefined register |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3377 | bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3378 | int64_t Register = 0; |
| 3379 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3380 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3381 | return true; |
| 3382 | |
| 3383 | getStreamer().EmitCFIUndefined(Register); |
| 3384 | return false; |
| 3385 | } |
| 3386 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3387 | /// parseDirectiveMacrosOnOff |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3388 | /// ::= .macros_on |
| 3389 | /// ::= .macros_off |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3390 | bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3391 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3392 | return Error(getLexer().getLoc(), |
| 3393 | "unexpected token in '" + Directive + "' directive"); |
| 3394 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3395 | setMacrosEnabled(Directive == ".macros_on"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3396 | return false; |
| 3397 | } |
| 3398 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3399 | /// parseDirectiveMacro |
Saleem Abdulrasool | 27304cb | 2014-02-16 04:56:31 +0000 | [diff] [blame] | 3400 | /// ::= .macro name[,] [parameters] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3401 | bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3402 | StringRef Name; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3403 | if (parseIdentifier(Name)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3404 | return TokError("expected identifier in '.macro' directive"); |
| 3405 | |
Saleem Abdulrasool | 27304cb | 2014-02-16 04:56:31 +0000 | [diff] [blame] | 3406 | if (getLexer().is(AsmToken::Comma)) |
| 3407 | Lex(); |
| 3408 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3409 | MCAsmMacroParameters Parameters; |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 3410 | while (getLexer().isNot(AsmToken::EndOfStatement)) { |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 3411 | |
Alexander Kornienko | 8c0809c | 2015-01-15 11:41:30 +0000 | [diff] [blame] | 3412 | if (!Parameters.empty() && Parameters.back().Vararg) |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 3413 | return Error(Lexer.getLoc(), |
| 3414 | "Vararg parameter '" + Parameters.back().Name + |
| 3415 | "' should be last one in the list of parameters."); |
| 3416 | |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 3417 | MCAsmMacroParameter Parameter; |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 3418 | if (parseIdentifier(Parameter.Name)) |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 3419 | return TokError("expected identifier in '.macro' directive"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3420 | |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 3421 | if (Lexer.is(AsmToken::Colon)) { |
| 3422 | Lex(); // consume ':' |
| 3423 | |
| 3424 | SMLoc QualLoc; |
| 3425 | StringRef Qualifier; |
| 3426 | |
| 3427 | QualLoc = Lexer.getLoc(); |
| 3428 | if (parseIdentifier(Qualifier)) |
| 3429 | return Error(QualLoc, "missing parameter qualifier for " |
| 3430 | "'" + Parameter.Name + "' in macro '" + Name + "'"); |
| 3431 | |
| 3432 | if (Qualifier == "req") |
| 3433 | Parameter.Required = true; |
Kevin Enderby | e3c1346 | 2014-08-04 23:14:37 +0000 | [diff] [blame] | 3434 | else if (Qualifier == "vararg") |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 3435 | Parameter.Vararg = true; |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 3436 | else |
| 3437 | return Error(QualLoc, Qualifier + " is not a valid parameter qualifier " |
| 3438 | "for '" + Parameter.Name + "' in macro '" + Name + "'"); |
| 3439 | } |
| 3440 | |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 3441 | if (getLexer().is(AsmToken::Equal)) { |
| 3442 | Lex(); |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 3443 | |
| 3444 | SMLoc ParamLoc; |
| 3445 | |
| 3446 | ParamLoc = Lexer.getLoc(); |
Stepan Dyatkovskiy | afc364b | 2014-04-23 06:56:28 +0000 | [diff] [blame] | 3447 | if (parseMacroArgument(Parameter.Value, /*Vararg=*/false )) |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 3448 | return true; |
Saleem Abdulrasool | f903a44 | 2014-02-19 03:00:29 +0000 | [diff] [blame] | 3449 | |
| 3450 | if (Parameter.Required) |
| 3451 | Warning(ParamLoc, "pointless default value for required parameter " |
| 3452 | "'" + Parameter.Name + "' in macro '" + Name + "'"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3453 | } |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 3454 | |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 3455 | Parameters.push_back(std::move(Parameter)); |
David Majnemer | 91fc4c2 | 2014-01-29 18:57:46 +0000 | [diff] [blame] | 3456 | |
| 3457 | if (getLexer().is(AsmToken::Comma)) |
| 3458 | Lex(); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3459 | } |
| 3460 | |
| 3461 | // Eat the end of statement. |
| 3462 | Lex(); |
| 3463 | |
| 3464 | AsmToken EndToken, StartToken = getTok(); |
Benjamin Kramer | 9d94a4e | 2014-02-09 16:22:00 +0000 | [diff] [blame] | 3465 | unsigned MacroDepth = 0; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3466 | |
| 3467 | // Lex the macro definition. |
| 3468 | for (;;) { |
| 3469 | // Check whether we have reached the end of the file. |
| 3470 | if (getLexer().is(AsmToken::Eof)) |
| 3471 | return Error(DirectiveLoc, "no matching '.endmacro' in definition"); |
| 3472 | |
| 3473 | // Otherwise, check whether we have reach the .endmacro. |
Benjamin Kramer | 9d94a4e | 2014-02-09 16:22:00 +0000 | [diff] [blame] | 3474 | if (getLexer().is(AsmToken::Identifier)) { |
| 3475 | if (getTok().getIdentifier() == ".endm" || |
| 3476 | getTok().getIdentifier() == ".endmacro") { |
| 3477 | if (MacroDepth == 0) { // Outermost macro. |
| 3478 | EndToken = getTok(); |
| 3479 | Lex(); |
| 3480 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3481 | return TokError("unexpected token in '" + EndToken.getIdentifier() + |
| 3482 | "' directive"); |
| 3483 | break; |
| 3484 | } else { |
| 3485 | // Otherwise we just found the end of an inner macro. |
| 3486 | --MacroDepth; |
| 3487 | } |
| 3488 | } else if (getTok().getIdentifier() == ".macro") { |
| 3489 | // We allow nested macros. Those aren't instantiated until the outermost |
| 3490 | // macro is expanded so just ignore them for now. |
| 3491 | ++MacroDepth; |
| 3492 | } |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3493 | } |
| 3494 | |
| 3495 | // Otherwise, scan til the end of the statement. |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3496 | eatToEndOfStatement(); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3497 | } |
| 3498 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3499 | if (lookupMacro(Name)) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3500 | return Error(DirectiveLoc, "macro '" + Name + "' is already defined"); |
| 3501 | } |
| 3502 | |
| 3503 | const char *BodyStart = StartToken.getLoc().getPointer(); |
| 3504 | const char *BodyEnd = EndToken.getLoc().getPointer(); |
| 3505 | StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3506 | checkForBadMacro(DirectiveLoc, Name, Body, Parameters); |
Benjamin Kramer | cb3e06b | 2014-10-03 18:32:55 +0000 | [diff] [blame] | 3507 | defineMacro(Name, MCAsmMacro(Name, Body, std::move(Parameters))); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3508 | return false; |
| 3509 | } |
| 3510 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3511 | /// checkForBadMacro |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3512 | /// |
| 3513 | /// With the support added for named parameters there may be code out there that |
| 3514 | /// is transitioning from positional parameters. In versions of gas that did |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 3515 | /// not support named parameters they would be ignored on the macro definition. |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3516 | /// 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] | 3517 | /// 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] | 3518 | /// to be positional parameters, strings like $1, $2, ... and $n, then issue a |
| 3519 | /// warning that the positional parameter found in body which have no effect. |
| 3520 | /// Hoping the developer will either remove the named parameters from the macro |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 3521 | /// definition so the positional parameters get used if that was what was |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3522 | /// intended or change the macro to use the named parameters. It is possible |
| 3523 | /// this warning will trigger when the none of the named parameters are used |
| 3524 | /// 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] | 3525 | void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3526 | StringRef Body, |
Benjamin Kramer | d31aaf1 | 2014-02-09 17:13:11 +0000 | [diff] [blame] | 3527 | ArrayRef<MCAsmMacroParameter> Parameters) { |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3528 | // If this macro is not defined with named parameters the warning we are |
| 3529 | // checking for here doesn't apply. |
| 3530 | unsigned NParameters = Parameters.size(); |
| 3531 | if (NParameters == 0) |
| 3532 | return; |
| 3533 | |
| 3534 | bool NamedParametersFound = false; |
| 3535 | bool PositionalParametersFound = false; |
| 3536 | |
| 3537 | // Look at the body of the macro for use of both the named parameters and what |
| 3538 | // are likely to be positional parameters. This is what expandMacro() is |
| 3539 | // doing when it finds the parameters in the body. |
| 3540 | while (!Body.empty()) { |
| 3541 | // Scan for the next possible parameter. |
| 3542 | std::size_t End = Body.size(), Pos = 0; |
| 3543 | for (; Pos != End; ++Pos) { |
| 3544 | // Check for a substitution or escape. |
| 3545 | // This macro is defined with parameters, look for \foo, \bar, etc. |
| 3546 | if (Body[Pos] == '\\' && Pos + 1 != End) |
| 3547 | break; |
| 3548 | |
| 3549 | // This macro should have parameters, but look for $0, $1, ..., $n too. |
| 3550 | if (Body[Pos] != '$' || Pos + 1 == End) |
| 3551 | continue; |
| 3552 | char Next = Body[Pos + 1]; |
Guy Benyei | 83c74e9 | 2013-02-12 21:21:59 +0000 | [diff] [blame] | 3553 | if (Next == '$' || Next == 'n' || |
| 3554 | isdigit(static_cast<unsigned char>(Next))) |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3555 | break; |
| 3556 | } |
| 3557 | |
| 3558 | // Check if we reached the end. |
| 3559 | if (Pos == End) |
| 3560 | break; |
| 3561 | |
| 3562 | if (Body[Pos] == '$') { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3563 | switch (Body[Pos + 1]) { |
| 3564 | // $$ => $ |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3565 | case '$': |
| 3566 | break; |
| 3567 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3568 | // $n => number of arguments |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3569 | case 'n': |
| 3570 | PositionalParametersFound = true; |
| 3571 | break; |
| 3572 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3573 | // $[0-9] => argument |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3574 | default: { |
| 3575 | PositionalParametersFound = true; |
| 3576 | break; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3577 | } |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3578 | } |
| 3579 | Pos += 2; |
| 3580 | } else { |
| 3581 | unsigned I = Pos + 1; |
| 3582 | while (isIdentifierChar(Body[I]) && I + 1 != End) |
| 3583 | ++I; |
| 3584 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3585 | const char *Begin = Body.data() + Pos + 1; |
| 3586 | StringRef Argument(Begin, I - (Pos + 1)); |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3587 | unsigned Index = 0; |
| 3588 | for (; Index < NParameters; ++Index) |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 3589 | if (Parameters[Index].Name == Argument) |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3590 | break; |
| 3591 | |
| 3592 | if (Index == NParameters) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3593 | if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')') |
| 3594 | Pos += 3; |
| 3595 | else { |
| 3596 | Pos = I; |
| 3597 | } |
Kevin Enderby | 81c944c | 2013-01-22 21:44:53 +0000 | [diff] [blame] | 3598 | } else { |
| 3599 | NamedParametersFound = true; |
| 3600 | Pos += 1 + Argument.size(); |
| 3601 | } |
| 3602 | } |
| 3603 | // Update the scan point. |
| 3604 | Body = Body.substr(Pos); |
| 3605 | } |
| 3606 | |
| 3607 | if (!NamedParametersFound && PositionalParametersFound) |
| 3608 | Warning(DirectiveLoc, "macro defined with named parameters which are not " |
| 3609 | "used in macro body, possible positional parameter " |
| 3610 | "found in body which will have no effect"); |
| 3611 | } |
| 3612 | |
Nico Weber | 155dccd1 | 2014-07-24 17:08:39 +0000 | [diff] [blame] | 3613 | /// parseDirectiveExitMacro |
| 3614 | /// ::= .exitm |
| 3615 | bool AsmParser::parseDirectiveExitMacro(StringRef Directive) { |
| 3616 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3617 | return TokError("unexpected token in '" + Directive + "' directive"); |
| 3618 | |
| 3619 | if (!isInsideMacroInstantiation()) |
| 3620 | return TokError("unexpected '" + Directive + "' in file, " |
| 3621 | "no current macro definition"); |
| 3622 | |
| 3623 | // Exit all conditionals that are active in the current macro. |
| 3624 | while (TheCondStack.size() != ActiveMacros.back()->CondStackDepth) { |
| 3625 | TheCondState = TheCondStack.back(); |
| 3626 | TheCondStack.pop_back(); |
| 3627 | } |
| 3628 | |
| 3629 | handleMacroExit(); |
| 3630 | return false; |
| 3631 | } |
| 3632 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3633 | /// parseDirectiveEndMacro |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3634 | /// ::= .endm |
| 3635 | /// ::= .endmacro |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3636 | bool AsmParser::parseDirectiveEndMacro(StringRef Directive) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3637 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3638 | return TokError("unexpected token in '" + Directive + "' directive"); |
| 3639 | |
| 3640 | // If we are inside a macro instantiation, terminate the current |
| 3641 | // instantiation. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3642 | if (isInsideMacroInstantiation()) { |
| 3643 | handleMacroExit(); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3644 | return false; |
| 3645 | } |
| 3646 | |
| 3647 | // Otherwise, this .endmacro is a stray entry in the file; well formed |
| 3648 | // .endmacro directives are handled during the macro definition parsing. |
| 3649 | return TokError("unexpected '" + Directive + "' in file, " |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3650 | "no current macro definition"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3651 | } |
| 3652 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3653 | /// parseDirectivePurgeMacro |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3654 | /// ::= .purgem |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3655 | bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) { |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3656 | StringRef Name; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3657 | if (parseIdentifier(Name)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3658 | return TokError("expected identifier in '.purgem' directive"); |
| 3659 | |
| 3660 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3661 | return TokError("unexpected token in '.purgem' directive"); |
| 3662 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3663 | if (!lookupMacro(Name)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3664 | return Error(DirectiveLoc, "macro '" + Name + "' is not defined"); |
| 3665 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3666 | undefineMacro(Name); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3667 | return false; |
| 3668 | } |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 3669 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3670 | /// parseDirectiveBundleAlignMode |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 3671 | /// ::= {.bundle_align_mode} expression |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3672 | bool AsmParser::parseDirectiveBundleAlignMode() { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3673 | checkForValidSection(); |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 3674 | |
| 3675 | // Expect a single argument: an expression that evaluates to a constant |
| 3676 | // in the inclusive range 0-30. |
| 3677 | SMLoc ExprLoc = getLexer().getLoc(); |
| 3678 | int64_t AlignSizePow2; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3679 | if (parseAbsoluteExpression(AlignSizePow2)) |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 3680 | return true; |
| 3681 | else if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3682 | return TokError("unexpected token after expression in" |
| 3683 | " '.bundle_align_mode' directive"); |
| 3684 | else if (AlignSizePow2 < 0 || AlignSizePow2 > 30) |
| 3685 | return Error(ExprLoc, |
| 3686 | "invalid bundle alignment size (expected between 0 and 30)"); |
| 3687 | |
| 3688 | Lex(); |
| 3689 | |
| 3690 | // Because of AlignSizePow2's verified range we can safely truncate it to |
| 3691 | // unsigned. |
| 3692 | getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2)); |
| 3693 | return false; |
| 3694 | } |
| 3695 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3696 | /// parseDirectiveBundleLock |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 3697 | /// ::= {.bundle_lock} [align_to_end] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3698 | bool AsmParser::parseDirectiveBundleLock() { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3699 | checkForValidSection(); |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 3700 | bool AlignToEnd = false; |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 3701 | |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 3702 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 3703 | StringRef Option; |
| 3704 | SMLoc Loc = getTok().getLoc(); |
| 3705 | const char *kInvalidOptionError = |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3706 | "invalid option for '.bundle_lock' directive"; |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 3707 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3708 | if (parseIdentifier(Option)) |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 3709 | return Error(Loc, kInvalidOptionError); |
| 3710 | |
| 3711 | if (Option != "align_to_end") |
| 3712 | return Error(Loc, kInvalidOptionError); |
| 3713 | else if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3714 | return Error(Loc, |
| 3715 | "unexpected token after '.bundle_lock' directive option"); |
| 3716 | AlignToEnd = true; |
| 3717 | } |
| 3718 | |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 3719 | Lex(); |
| 3720 | |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 3721 | getStreamer().EmitBundleLock(AlignToEnd); |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 3722 | return false; |
| 3723 | } |
| 3724 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3725 | /// parseDirectiveBundleLock |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 3726 | /// ::= {.bundle_lock} |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3727 | bool AsmParser::parseDirectiveBundleUnlock() { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3728 | checkForValidSection(); |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 3729 | |
| 3730 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3731 | return TokError("unexpected token in '.bundle_unlock' directive"); |
| 3732 | Lex(); |
| 3733 | |
| 3734 | getStreamer().EmitBundleUnlock(); |
| 3735 | return false; |
| 3736 | } |
| 3737 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3738 | /// parseDirectiveSpace |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3739 | /// ::= (.skip | .space) expression [ , expression ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3740 | bool AsmParser::parseDirectiveSpace(StringRef IDVal) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3741 | checkForValidSection(); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3742 | |
| 3743 | int64_t NumBytes; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3744 | if (parseAbsoluteExpression(NumBytes)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3745 | return true; |
| 3746 | |
| 3747 | int64_t FillExpr = 0; |
| 3748 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 3749 | if (getLexer().isNot(AsmToken::Comma)) |
| 3750 | return TokError("unexpected token in '" + Twine(IDVal) + "' directive"); |
| 3751 | Lex(); |
| 3752 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3753 | if (parseAbsoluteExpression(FillExpr)) |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3754 | return true; |
| 3755 | |
| 3756 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3757 | return TokError("unexpected token in '" + Twine(IDVal) + "' directive"); |
| 3758 | } |
| 3759 | |
| 3760 | Lex(); |
| 3761 | |
| 3762 | if (NumBytes <= 0) |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3763 | return TokError("invalid number of bytes in '" + Twine(IDVal) + |
| 3764 | "' directive"); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3765 | |
| 3766 | // 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] | 3767 | getStreamer().EmitFill(NumBytes, FillExpr); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3768 | |
| 3769 | return false; |
| 3770 | } |
| 3771 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3772 | /// parseDirectiveLEB128 |
Benjamin Kramer | 68ca67b | 2015-02-19 20:24:04 +0000 | [diff] [blame] | 3773 | /// ::= (.sleb128 | .uleb128) [ expression (, expression)* ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3774 | bool AsmParser::parseDirectiveLEB128(bool Signed) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3775 | checkForValidSection(); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3776 | const MCExpr *Value; |
| 3777 | |
Benjamin Kramer | 68ca67b | 2015-02-19 20:24:04 +0000 | [diff] [blame] | 3778 | for (;;) { |
| 3779 | if (parseExpression(Value)) |
| 3780 | return true; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3781 | |
Benjamin Kramer | 68ca67b | 2015-02-19 20:24:04 +0000 | [diff] [blame] | 3782 | if (Signed) |
| 3783 | getStreamer().EmitSLEB128Value(Value); |
| 3784 | else |
| 3785 | getStreamer().EmitULEB128Value(Value); |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3786 | |
Benjamin Kramer | 68ca67b | 2015-02-19 20:24:04 +0000 | [diff] [blame] | 3787 | if (getLexer().is(AsmToken::EndOfStatement)) |
| 3788 | break; |
| 3789 | |
| 3790 | if (getLexer().isNot(AsmToken::Comma)) |
| 3791 | return TokError("unexpected token in directive"); |
| 3792 | Lex(); |
| 3793 | } |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3794 | |
| 3795 | return false; |
| 3796 | } |
| 3797 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3798 | /// parseDirectiveSymbolAttribute |
Daniel Dunbar | a5508c8 | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 3799 | /// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3800 | bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) { |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3801 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
Daniel Dunbar | a5508c8 | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 3802 | for (;;) { |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 3803 | StringRef Name; |
Jim Grosbach | ebdf32f | 2011-09-15 17:56:49 +0000 | [diff] [blame] | 3804 | SMLoc Loc = getTok().getLoc(); |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 3805 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3806 | if (parseIdentifier(Name)) |
Jim Grosbach | ebdf32f | 2011-09-15 17:56:49 +0000 | [diff] [blame] | 3807 | return Error(Loc, "expected identifier in directive"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3808 | |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 3809 | MCSymbol *Sym = getContext().getOrCreateSymbol(Name); |
Daniel Dunbar | a5508c8 | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 3810 | |
Jim Grosbach | ebdf32f | 2011-09-15 17:56:49 +0000 | [diff] [blame] | 3811 | // Assembler local symbols don't make any sense here. Complain loudly. |
| 3812 | if (Sym->isTemporary()) |
| 3813 | return Error(Loc, "non-local symbol required in directive"); |
| 3814 | |
Saleem Abdulrasool | 4208b61 | 2013-08-09 01:52:03 +0000 | [diff] [blame] | 3815 | if (!getStreamer().EmitSymbolAttribute(Sym, Attr)) |
| 3816 | return Error(Loc, "unable to emit symbol attribute"); |
Daniel Dunbar | a5508c8 | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 3817 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3818 | if (getLexer().is(AsmToken::EndOfStatement)) |
Daniel Dunbar | a5508c8 | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 3819 | break; |
| 3820 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3821 | if (getLexer().isNot(AsmToken::Comma)) |
Daniel Dunbar | a5508c8 | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 3822 | return TokError("unexpected token in directive"); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 3823 | Lex(); |
Daniel Dunbar | a5508c8 | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 3824 | } |
| 3825 | } |
| 3826 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 3827 | Lex(); |
Jan Wen Voung | c768287 | 2010-09-30 01:09:20 +0000 | [diff] [blame] | 3828 | return false; |
Daniel Dunbar | a5508c8 | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 3829 | } |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3830 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3831 | /// parseDirectiveComm |
Chris Lattner | 28ad754 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 3832 | /// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3833 | bool AsmParser::parseDirectiveComm(bool IsLocal) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3834 | checkForValidSection(); |
Daniel Dunbar | e5444a8 | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 3835 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3836 | SMLoc IDLoc = getLexer().getLoc(); |
Daniel Dunbar | c54ecb3 | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 3837 | StringRef Name; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3838 | if (parseIdentifier(Name)) |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3839 | return TokError("expected identifier in directive"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3840 | |
Daniel Dunbar | 9ee33ca | 2009-07-31 21:55:09 +0000 | [diff] [blame] | 3841 | // Handle the identifier as the key symbol. |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 3842 | MCSymbol *Sym = getContext().getOrCreateSymbol(Name); |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3843 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3844 | if (getLexer().isNot(AsmToken::Comma)) |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3845 | return TokError("unexpected token in directive"); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 3846 | Lex(); |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3847 | |
| 3848 | int64_t Size; |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3849 | SMLoc SizeLoc = getLexer().getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3850 | if (parseAbsoluteExpression(Size)) |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3851 | return true; |
| 3852 | |
| 3853 | int64_t Pow2Alignment = 0; |
| 3854 | SMLoc Pow2AlignmentLoc; |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3855 | if (getLexer().is(AsmToken::Comma)) { |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 3856 | Lex(); |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3857 | Pow2AlignmentLoc = getLexer().getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3858 | if (parseAbsoluteExpression(Pow2Alignment)) |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3859 | return true; |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3860 | |
Benjamin Kramer | 68b9f05 | 2012-09-07 21:08:01 +0000 | [diff] [blame] | 3861 | LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType(); |
| 3862 | if (IsLocal && LCOMM == LCOMM::NoAlignment) |
Benjamin Kramer | 47f9ec9 | 2012-09-07 17:25:13 +0000 | [diff] [blame] | 3863 | return Error(Pow2AlignmentLoc, "alignment not supported on this target"); |
| 3864 | |
Chris Lattner | ab9cd3e | 2010-01-19 06:22:22 +0000 | [diff] [blame] | 3865 | // If this target takes alignments in bytes (not log) validate and convert. |
Benjamin Kramer | 68b9f05 | 2012-09-07 21:08:01 +0000 | [diff] [blame] | 3866 | if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) || |
| 3867 | (IsLocal && LCOMM == LCOMM::ByteAlignment)) { |
Chris Lattner | ab9cd3e | 2010-01-19 06:22:22 +0000 | [diff] [blame] | 3868 | if (!isPowerOf2_64(Pow2Alignment)) |
| 3869 | return Error(Pow2AlignmentLoc, "alignment must be a power of 2"); |
| 3870 | Pow2Alignment = Log2_64(Pow2Alignment); |
| 3871 | } |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3872 | } |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3873 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3874 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Chris Lattner | 28ad754 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 3875 | return TokError("unexpected token in '.comm' or '.lcomm' directive"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3876 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 3877 | Lex(); |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3878 | |
Chris Lattner | 28ad754 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 3879 | // NOTE: a size of zero for a .comm should create a undefined symbol |
| 3880 | // but a size of .lcomm creates a bss symbol of size zero. |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3881 | if (Size < 0) |
Chris Lattner | 28ad754 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 3882 | return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't " |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3883 | "be less than zero"); |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3884 | |
Eric Christopher | bc81885 | 2010-05-14 01:38:54 +0000 | [diff] [blame] | 3885 | // 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] | 3886 | // may internally end up wanting an alignment in bytes. |
| 3887 | // FIXME: Diagnose overflow. |
| 3888 | if (Pow2Alignment < 0) |
Chris Lattner | 28ad754 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 3889 | return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive " |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3890 | "alignment, can't be less than zero"); |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3891 | |
Daniel Dunbar | 6860ac7 | 2009-08-22 07:22:36 +0000 | [diff] [blame] | 3892 | if (!Sym->isUndefined()) |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3893 | return Error(IDLoc, "invalid symbol redefinition"); |
| 3894 | |
Chris Lattner | 28ad754 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 3895 | // 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] | 3896 | if (IsLocal) { |
Benjamin Kramer | 47f9ec9 | 2012-09-07 17:25:13 +0000 | [diff] [blame] | 3897 | getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment); |
Daniel Dunbar | 6a715dc | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 3898 | return false; |
| 3899 | } |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3900 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3901 | getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment); |
Chris Lattner | a1e11f5 | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3902 | return false; |
| 3903 | } |
Chris Lattner | 07cadaf | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 3904 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3905 | /// parseDirectiveAbort |
Daniel Dunbar | 40a564f | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 3906 | /// ::= .abort [... message ...] |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3907 | bool AsmParser::parseDirectiveAbort() { |
Daniel Dunbar | eb6bb32 | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 3908 | // FIXME: Use loc from directive. |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3909 | SMLoc Loc = getLexer().getLoc(); |
Daniel Dunbar | eb6bb32 | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 3910 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3911 | StringRef Str = parseStringToEndOfStatement(); |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3912 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | 56523ce | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 3913 | return TokError("unexpected token in '.abort' directive"); |
Daniel Dunbar | 40a564f | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 3914 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 3915 | Lex(); |
Kevin Enderby | 56523ce | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 3916 | |
Daniel Dunbar | eb6bb32 | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 3917 | if (Str.empty()) |
| 3918 | Error(Loc, ".abort detected. Assembly stopping."); |
| 3919 | else |
| 3920 | Error(Loc, ".abort '" + Str + "' detected. Assembly stopping."); |
Daniel Dunbar | 40a564f | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 3921 | // FIXME: Actually abort assembly here. |
Kevin Enderby | 56523ce | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 3922 | |
| 3923 | return false; |
| 3924 | } |
Kevin Enderby | cbe475d | 2009-07-14 21:35:03 +0000 | [diff] [blame] | 3925 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3926 | /// parseDirectiveInclude |
Kevin Enderby | d1ea539 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 3927 | /// ::= .include "filename" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3928 | bool AsmParser::parseDirectiveInclude() { |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3929 | if (getLexer().isNot(AsmToken::String)) |
Kevin Enderby | d1ea539 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 3930 | return TokError("expected string in '.include' directive"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3931 | |
Yunzhong Gao | 8c0f506 | 2013-09-05 19:14:26 +0000 | [diff] [blame] | 3932 | // Allow the strings to have escaped octal character sequence. |
| 3933 | std::string Filename; |
| 3934 | if (parseEscapedString(Filename)) |
| 3935 | return true; |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3936 | SMLoc IncludeLoc = getLexer().getLoc(); |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 3937 | Lex(); |
Kevin Enderby | d1ea539 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 3938 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3939 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | d1ea539 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 3940 | return TokError("unexpected token in '.include' directive"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3941 | |
Chris Lattner | 693fbb8 | 2009-07-16 06:14:39 +0000 | [diff] [blame] | 3942 | // Attempt to switch the lexer to the included file before consuming the end |
| 3943 | // of statement to avoid losing it when we switch. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3944 | if (enterIncludeFile(Filename)) { |
Daniel Dunbar | d8a1845 | 2010-07-18 18:31:45 +0000 | [diff] [blame] | 3945 | Error(IncludeLoc, "Could not find include file '" + Filename + "'"); |
Chris Lattner | 693fbb8 | 2009-07-16 06:14:39 +0000 | [diff] [blame] | 3946 | return true; |
| 3947 | } |
Kevin Enderby | d1ea539 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 3948 | |
| 3949 | return false; |
| 3950 | } |
Kevin Enderby | 09ea570 | 2009-07-15 15:30:11 +0000 | [diff] [blame] | 3951 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3952 | /// parseDirectiveIncbin |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 3953 | /// ::= .incbin "filename" |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3954 | bool AsmParser::parseDirectiveIncbin() { |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 3955 | if (getLexer().isNot(AsmToken::String)) |
| 3956 | return TokError("expected string in '.incbin' directive"); |
| 3957 | |
Yunzhong Gao | 8c0f506 | 2013-09-05 19:14:26 +0000 | [diff] [blame] | 3958 | // Allow the strings to have escaped octal character sequence. |
| 3959 | std::string Filename; |
| 3960 | if (parseEscapedString(Filename)) |
| 3961 | return true; |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 3962 | SMLoc IncbinLoc = getLexer().getLoc(); |
| 3963 | Lex(); |
| 3964 | |
| 3965 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3966 | return TokError("unexpected token in '.incbin' directive"); |
| 3967 | |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 3968 | // Attempt to process the included file. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3969 | if (processIncbinFile(Filename)) { |
Kevin Enderby | 109f25c | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 3970 | Error(IncbinLoc, "Could not find incbin file '" + Filename + "'"); |
| 3971 | return true; |
| 3972 | } |
| 3973 | |
| 3974 | return false; |
| 3975 | } |
| 3976 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 3977 | /// parseDirectiveIf |
Saleem Abdulrasool | 763e2cb | 2014-06-18 20:57:28 +0000 | [diff] [blame] | 3978 | /// ::= .if{,eq,ge,gt,le,lt,ne} expression |
| 3979 | bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind) { |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3980 | TheCondStack.push_back(TheCondState); |
| 3981 | TheCondState.TheCond = AsmCond::IfCond; |
Benjamin Kramer | c7eda3e | 2012-05-12 16:52:21 +0000 | [diff] [blame] | 3982 | if (TheCondState.Ignore) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3983 | eatToEndOfStatement(); |
Benjamin Kramer | c7eda3e | 2012-05-12 16:52:21 +0000 | [diff] [blame] | 3984 | } else { |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3985 | int64_t ExprValue; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 3986 | if (parseAbsoluteExpression(ExprValue)) |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3987 | return true; |
| 3988 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3989 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3990 | return TokError("unexpected token in '.if' directive"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3991 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 3992 | Lex(); |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3993 | |
Saleem Abdulrasool | 763e2cb | 2014-06-18 20:57:28 +0000 | [diff] [blame] | 3994 | switch (DirKind) { |
| 3995 | default: |
| 3996 | llvm_unreachable("unsupported directive"); |
| 3997 | case DK_IF: |
| 3998 | case DK_IFNE: |
| 3999 | break; |
| 4000 | case DK_IFEQ: |
| 4001 | ExprValue = ExprValue == 0; |
| 4002 | break; |
| 4003 | case DK_IFGE: |
| 4004 | ExprValue = ExprValue >= 0; |
| 4005 | break; |
| 4006 | case DK_IFGT: |
| 4007 | ExprValue = ExprValue > 0; |
| 4008 | break; |
| 4009 | case DK_IFLE: |
| 4010 | ExprValue = ExprValue <= 0; |
| 4011 | break; |
| 4012 | case DK_IFLT: |
| 4013 | ExprValue = ExprValue < 0; |
| 4014 | break; |
| 4015 | } |
| 4016 | |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4017 | TheCondState.CondMet = ExprValue; |
| 4018 | TheCondState.Ignore = !TheCondState.CondMet; |
| 4019 | } |
| 4020 | |
| 4021 | return false; |
| 4022 | } |
| 4023 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4024 | /// parseDirectiveIfb |
Benjamin Kramer | 62c18b0 | 2012-05-12 11:18:42 +0000 | [diff] [blame] | 4025 | /// ::= .ifb string |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4026 | bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) { |
Benjamin Kramer | 62c18b0 | 2012-05-12 11:18:42 +0000 | [diff] [blame] | 4027 | TheCondStack.push_back(TheCondState); |
| 4028 | TheCondState.TheCond = AsmCond::IfCond; |
| 4029 | |
Benjamin Kramer | c7eda3e | 2012-05-12 16:52:21 +0000 | [diff] [blame] | 4030 | if (TheCondState.Ignore) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4031 | eatToEndOfStatement(); |
Benjamin Kramer | 62c18b0 | 2012-05-12 11:18:42 +0000 | [diff] [blame] | 4032 | } else { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4033 | StringRef Str = parseStringToEndOfStatement(); |
Benjamin Kramer | 62c18b0 | 2012-05-12 11:18:42 +0000 | [diff] [blame] | 4034 | |
| 4035 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 4036 | return TokError("unexpected token in '.ifb' directive"); |
| 4037 | |
| 4038 | Lex(); |
| 4039 | |
| 4040 | TheCondState.CondMet = ExpectBlank == Str.empty(); |
| 4041 | TheCondState.Ignore = !TheCondState.CondMet; |
| 4042 | } |
| 4043 | |
| 4044 | return false; |
| 4045 | } |
| 4046 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4047 | /// parseDirectiveIfc |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 4048 | /// ::= .ifc string1, string2 |
Saleem Abdulrasool | 5db5298 | 2014-02-23 15:53:36 +0000 | [diff] [blame] | 4049 | /// ::= .ifnc string1, string2 |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4050 | bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) { |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 4051 | TheCondStack.push_back(TheCondState); |
| 4052 | TheCondState.TheCond = AsmCond::IfCond; |
| 4053 | |
Benjamin Kramer | c7eda3e | 2012-05-12 16:52:21 +0000 | [diff] [blame] | 4054 | if (TheCondState.Ignore) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4055 | eatToEndOfStatement(); |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 4056 | } else { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4057 | StringRef Str1 = parseStringToComma(); |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 4058 | |
| 4059 | if (getLexer().isNot(AsmToken::Comma)) |
| 4060 | return TokError("unexpected token in '.ifc' directive"); |
| 4061 | |
| 4062 | Lex(); |
| 4063 | |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4064 | StringRef Str2 = parseStringToEndOfStatement(); |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 4065 | |
| 4066 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 4067 | return TokError("unexpected token in '.ifc' directive"); |
| 4068 | |
| 4069 | Lex(); |
| 4070 | |
Saleem Abdulrasool | 5db5298 | 2014-02-23 15:53:36 +0000 | [diff] [blame] | 4071 | TheCondState.CondMet = ExpectEqual == (Str1.trim() == Str2.trim()); |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 4072 | TheCondState.Ignore = !TheCondState.CondMet; |
| 4073 | } |
| 4074 | |
| 4075 | return false; |
| 4076 | } |
| 4077 | |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 4078 | /// parseDirectiveIfeqs |
| 4079 | /// ::= .ifeqs string1, string2 |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 4080 | bool AsmParser::parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual) { |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 4081 | if (Lexer.isNot(AsmToken::String)) { |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 4082 | if (ExpectEqual) |
| 4083 | TokError("expected string parameter for '.ifeqs' directive"); |
| 4084 | else |
| 4085 | TokError("expected string parameter for '.ifnes' directive"); |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 4086 | eatToEndOfStatement(); |
| 4087 | return true; |
| 4088 | } |
| 4089 | |
| 4090 | StringRef String1 = getTok().getStringContents(); |
| 4091 | Lex(); |
| 4092 | |
| 4093 | if (Lexer.isNot(AsmToken::Comma)) { |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 4094 | if (ExpectEqual) |
| 4095 | TokError("expected comma after first string for '.ifeqs' directive"); |
| 4096 | else |
| 4097 | TokError("expected comma after first string for '.ifnes' directive"); |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 4098 | eatToEndOfStatement(); |
| 4099 | return true; |
| 4100 | } |
| 4101 | |
| 4102 | Lex(); |
| 4103 | |
| 4104 | if (Lexer.isNot(AsmToken::String)) { |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 4105 | if (ExpectEqual) |
| 4106 | TokError("expected string parameter for '.ifeqs' directive"); |
| 4107 | else |
| 4108 | TokError("expected string parameter for '.ifnes' directive"); |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 4109 | eatToEndOfStatement(); |
| 4110 | return true; |
| 4111 | } |
| 4112 | |
| 4113 | StringRef String2 = getTok().getStringContents(); |
| 4114 | Lex(); |
| 4115 | |
| 4116 | TheCondStack.push_back(TheCondState); |
| 4117 | TheCondState.TheCond = AsmCond::IfCond; |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 4118 | TheCondState.CondMet = ExpectEqual == (String1 == String2); |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 4119 | TheCondState.Ignore = !TheCondState.CondMet; |
| 4120 | |
| 4121 | return false; |
| 4122 | } |
| 4123 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4124 | /// parseDirectiveIfdef |
Benjamin Kramer | e297b9f | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 4125 | /// ::= .ifdef symbol |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4126 | bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) { |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 4127 | StringRef Name; |
| 4128 | TheCondStack.push_back(TheCondState); |
| 4129 | TheCondState.TheCond = AsmCond::IfCond; |
| 4130 | |
| 4131 | if (TheCondState.Ignore) { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4132 | eatToEndOfStatement(); |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 4133 | } else { |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4134 | if (parseIdentifier(Name)) |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 4135 | return TokError("expected identifier after '.ifdef'"); |
| 4136 | |
| 4137 | Lex(); |
| 4138 | |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 4139 | MCSymbol *Sym = getContext().lookupSymbol(Name); |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 4140 | |
| 4141 | if (expect_defined) |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 4142 | TheCondState.CondMet = (Sym && !Sym->isUndefined()); |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 4143 | else |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 4144 | TheCondState.CondMet = (!Sym || Sym->isUndefined()); |
Benjamin Kramer | 7b7caf5 | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 4145 | TheCondState.Ignore = !TheCondState.CondMet; |
| 4146 | } |
| 4147 | |
| 4148 | return false; |
| 4149 | } |
| 4150 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4151 | /// parseDirectiveElseIf |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4152 | /// ::= .elseif expression |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4153 | bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) { |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4154 | if (TheCondState.TheCond != AsmCond::IfCond && |
| 4155 | TheCondState.TheCond != AsmCond::ElseIfCond) |
Craig Topper | 2172ad6 | 2013-04-22 04:24:02 +0000 | [diff] [blame] | 4156 | Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or " |
| 4157 | " an .elseif"); |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4158 | TheCondState.TheCond = AsmCond::ElseIfCond; |
| 4159 | |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4160 | bool LastIgnoreState = false; |
| 4161 | if (!TheCondStack.empty()) |
Craig Topper | 2172ad6 | 2013-04-22 04:24:02 +0000 | [diff] [blame] | 4162 | LastIgnoreState = TheCondStack.back().Ignore; |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4163 | if (LastIgnoreState || TheCondState.CondMet) { |
| 4164 | TheCondState.Ignore = true; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4165 | eatToEndOfStatement(); |
Craig Topper | f15655b | 2013-04-22 04:22:40 +0000 | [diff] [blame] | 4166 | } else { |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4167 | int64_t ExprValue; |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4168 | if (parseAbsoluteExpression(ExprValue)) |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4169 | return true; |
| 4170 | |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 4171 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4172 | return TokError("unexpected token in '.elseif' directive"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 4173 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 4174 | Lex(); |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4175 | TheCondState.CondMet = ExprValue; |
| 4176 | TheCondState.Ignore = !TheCondState.CondMet; |
| 4177 | } |
| 4178 | |
| 4179 | return false; |
| 4180 | } |
| 4181 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4182 | /// parseDirectiveElse |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4183 | /// ::= .else |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4184 | bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) { |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 4185 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4186 | return TokError("unexpected token in '.else' directive"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 4187 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 4188 | Lex(); |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4189 | |
| 4190 | if (TheCondState.TheCond != AsmCond::IfCond && |
| 4191 | TheCondState.TheCond != AsmCond::ElseIfCond) |
Craig Topper | 2172ad6 | 2013-04-22 04:24:02 +0000 | [diff] [blame] | 4192 | Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an " |
| 4193 | ".elseif"); |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4194 | TheCondState.TheCond = AsmCond::ElseCond; |
| 4195 | bool LastIgnoreState = false; |
| 4196 | if (!TheCondStack.empty()) |
| 4197 | LastIgnoreState = TheCondStack.back().Ignore; |
| 4198 | if (LastIgnoreState || TheCondState.CondMet) |
| 4199 | TheCondState.Ignore = true; |
| 4200 | else |
| 4201 | TheCondState.Ignore = false; |
| 4202 | |
| 4203 | return false; |
| 4204 | } |
| 4205 | |
Saleem Abdulrasool | 88186c4 | 2013-12-18 02:53:03 +0000 | [diff] [blame] | 4206 | /// parseDirectiveEnd |
| 4207 | /// ::= .end |
| 4208 | bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) { |
| 4209 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 4210 | return TokError("unexpected token in '.end' directive"); |
| 4211 | |
| 4212 | Lex(); |
| 4213 | |
| 4214 | while (Lexer.isNot(AsmToken::Eof)) |
| 4215 | Lex(); |
| 4216 | |
| 4217 | return false; |
| 4218 | } |
| 4219 | |
Saleem Abdulrasool | 7ecc549 | 2014-02-23 23:02:23 +0000 | [diff] [blame] | 4220 | /// parseDirectiveError |
| 4221 | /// ::= .err |
| 4222 | /// ::= .error [string] |
| 4223 | bool AsmParser::parseDirectiveError(SMLoc L, bool WithMessage) { |
| 4224 | if (!TheCondStack.empty()) { |
| 4225 | if (TheCondStack.back().Ignore) { |
| 4226 | eatToEndOfStatement(); |
Saleem Abdulrasool | b2ae2c0 | 2014-02-23 15:53:30 +0000 | [diff] [blame] | 4227 | return false; |
Saleem Abdulrasool | 7ecc549 | 2014-02-23 23:02:23 +0000 | [diff] [blame] | 4228 | } |
| 4229 | } |
| 4230 | |
| 4231 | if (!WithMessage) |
| 4232 | return Error(L, ".err encountered"); |
| 4233 | |
| 4234 | StringRef Message = ".error directive invoked in source file"; |
| 4235 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
| 4236 | if (Lexer.isNot(AsmToken::String)) { |
| 4237 | TokError(".error argument must be a string"); |
| 4238 | eatToEndOfStatement(); |
| 4239 | return true; |
| 4240 | } |
| 4241 | |
| 4242 | Message = getTok().getStringContents(); |
| 4243 | Lex(); |
| 4244 | } |
| 4245 | |
| 4246 | Error(L, Message); |
| 4247 | return true; |
Saleem Abdulrasool | b2ae2c0 | 2014-02-23 15:53:30 +0000 | [diff] [blame] | 4248 | } |
| 4249 | |
Nico Weber | 404012b | 2014-07-24 16:26:06 +0000 | [diff] [blame] | 4250 | /// parseDirectiveWarning |
| 4251 | /// ::= .warning [string] |
| 4252 | bool AsmParser::parseDirectiveWarning(SMLoc L) { |
| 4253 | if (!TheCondStack.empty()) { |
| 4254 | if (TheCondStack.back().Ignore) { |
| 4255 | eatToEndOfStatement(); |
| 4256 | return false; |
| 4257 | } |
| 4258 | } |
| 4259 | |
| 4260 | StringRef Message = ".warning directive invoked in source file"; |
| 4261 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
| 4262 | if (Lexer.isNot(AsmToken::String)) { |
| 4263 | TokError(".warning argument must be a string"); |
| 4264 | eatToEndOfStatement(); |
| 4265 | return true; |
| 4266 | } |
| 4267 | |
| 4268 | Message = getTok().getStringContents(); |
| 4269 | Lex(); |
| 4270 | } |
| 4271 | |
| 4272 | Warning(L, Message); |
| 4273 | return false; |
| 4274 | } |
| 4275 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4276 | /// parseDirectiveEndIf |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4277 | /// ::= .endif |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4278 | bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) { |
Daniel Dunbar | dd41dcf | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 4279 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4280 | return TokError("unexpected token in '.endif' directive"); |
Michael J. Spencer | 530ce85 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 4281 | |
Sean Callanan | 686ed8d | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 4282 | Lex(); |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4283 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4284 | if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty()) |
Kevin Enderby | d9f9529 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 4285 | Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or " |
| 4286 | ".else"); |
| 4287 | if (!TheCondStack.empty()) { |
| 4288 | TheCondState = TheCondStack.back(); |
| 4289 | TheCondStack.pop_back(); |
| 4290 | } |
| 4291 | |
| 4292 | return false; |
| 4293 | } |
Daniel Dunbar | a4b069c | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 4294 | |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4295 | void AsmParser::initializeDirectiveKindMap() { |
| 4296 | DirectiveKindMap[".set"] = DK_SET; |
| 4297 | DirectiveKindMap[".equ"] = DK_EQU; |
| 4298 | DirectiveKindMap[".equiv"] = DK_EQUIV; |
| 4299 | DirectiveKindMap[".ascii"] = DK_ASCII; |
| 4300 | DirectiveKindMap[".asciz"] = DK_ASCIZ; |
| 4301 | DirectiveKindMap[".string"] = DK_STRING; |
| 4302 | DirectiveKindMap[".byte"] = DK_BYTE; |
| 4303 | DirectiveKindMap[".short"] = DK_SHORT; |
| 4304 | DirectiveKindMap[".value"] = DK_VALUE; |
| 4305 | DirectiveKindMap[".2byte"] = DK_2BYTE; |
| 4306 | DirectiveKindMap[".long"] = DK_LONG; |
| 4307 | DirectiveKindMap[".int"] = DK_INT; |
| 4308 | DirectiveKindMap[".4byte"] = DK_4BYTE; |
| 4309 | DirectiveKindMap[".quad"] = DK_QUAD; |
| 4310 | DirectiveKindMap[".8byte"] = DK_8BYTE; |
David Woodhouse | d6de0d9 | 2014-02-01 16:20:59 +0000 | [diff] [blame] | 4311 | DirectiveKindMap[".octa"] = DK_OCTA; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4312 | DirectiveKindMap[".single"] = DK_SINGLE; |
| 4313 | DirectiveKindMap[".float"] = DK_FLOAT; |
| 4314 | DirectiveKindMap[".double"] = DK_DOUBLE; |
| 4315 | DirectiveKindMap[".align"] = DK_ALIGN; |
| 4316 | DirectiveKindMap[".align32"] = DK_ALIGN32; |
| 4317 | DirectiveKindMap[".balign"] = DK_BALIGN; |
| 4318 | DirectiveKindMap[".balignw"] = DK_BALIGNW; |
| 4319 | DirectiveKindMap[".balignl"] = DK_BALIGNL; |
| 4320 | DirectiveKindMap[".p2align"] = DK_P2ALIGN; |
| 4321 | DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW; |
| 4322 | DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL; |
| 4323 | DirectiveKindMap[".org"] = DK_ORG; |
| 4324 | DirectiveKindMap[".fill"] = DK_FILL; |
| 4325 | DirectiveKindMap[".zero"] = DK_ZERO; |
| 4326 | DirectiveKindMap[".extern"] = DK_EXTERN; |
| 4327 | DirectiveKindMap[".globl"] = DK_GLOBL; |
| 4328 | DirectiveKindMap[".global"] = DK_GLOBAL; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4329 | DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE; |
| 4330 | DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP; |
| 4331 | DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER; |
| 4332 | DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN; |
| 4333 | DirectiveKindMap[".reference"] = DK_REFERENCE; |
| 4334 | DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION; |
| 4335 | DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE; |
| 4336 | DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN; |
| 4337 | DirectiveKindMap[".comm"] = DK_COMM; |
| 4338 | DirectiveKindMap[".common"] = DK_COMMON; |
| 4339 | DirectiveKindMap[".lcomm"] = DK_LCOMM; |
| 4340 | DirectiveKindMap[".abort"] = DK_ABORT; |
| 4341 | DirectiveKindMap[".include"] = DK_INCLUDE; |
| 4342 | DirectiveKindMap[".incbin"] = DK_INCBIN; |
| 4343 | DirectiveKindMap[".code16"] = DK_CODE16; |
| 4344 | DirectiveKindMap[".code16gcc"] = DK_CODE16GCC; |
| 4345 | DirectiveKindMap[".rept"] = DK_REPT; |
Saleem Abdulrasool | d743d0a | 2013-12-28 05:54:33 +0000 | [diff] [blame] | 4346 | DirectiveKindMap[".rep"] = DK_REPT; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4347 | DirectiveKindMap[".irp"] = DK_IRP; |
| 4348 | DirectiveKindMap[".irpc"] = DK_IRPC; |
| 4349 | DirectiveKindMap[".endr"] = DK_ENDR; |
| 4350 | DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE; |
| 4351 | DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK; |
| 4352 | DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK; |
| 4353 | DirectiveKindMap[".if"] = DK_IF; |
Saleem Abdulrasool | 763e2cb | 2014-06-18 20:57:28 +0000 | [diff] [blame] | 4354 | DirectiveKindMap[".ifeq"] = DK_IFEQ; |
| 4355 | DirectiveKindMap[".ifge"] = DK_IFGE; |
| 4356 | DirectiveKindMap[".ifgt"] = DK_IFGT; |
| 4357 | DirectiveKindMap[".ifle"] = DK_IFLE; |
| 4358 | DirectiveKindMap[".iflt"] = DK_IFLT; |
Saleem Abdulrasool | 5852d6b | 2014-02-23 15:53:41 +0000 | [diff] [blame] | 4359 | DirectiveKindMap[".ifne"] = DK_IFNE; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4360 | DirectiveKindMap[".ifb"] = DK_IFB; |
| 4361 | DirectiveKindMap[".ifnb"] = DK_IFNB; |
| 4362 | DirectiveKindMap[".ifc"] = DK_IFC; |
Saleem Abdulrasool | 00f53c1 | 2014-02-23 23:02:18 +0000 | [diff] [blame] | 4363 | DirectiveKindMap[".ifeqs"] = DK_IFEQS; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4364 | DirectiveKindMap[".ifnc"] = DK_IFNC; |
Sid Manning | 51c3560 | 2015-03-18 14:20:54 +0000 | [diff] [blame] | 4365 | DirectiveKindMap[".ifnes"] = DK_IFNES; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4366 | DirectiveKindMap[".ifdef"] = DK_IFDEF; |
| 4367 | DirectiveKindMap[".ifndef"] = DK_IFNDEF; |
| 4368 | DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF; |
| 4369 | DirectiveKindMap[".elseif"] = DK_ELSEIF; |
| 4370 | DirectiveKindMap[".else"] = DK_ELSE; |
Saleem Abdulrasool | 88186c4 | 2013-12-18 02:53:03 +0000 | [diff] [blame] | 4371 | DirectiveKindMap[".end"] = DK_END; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4372 | DirectiveKindMap[".endif"] = DK_ENDIF; |
| 4373 | DirectiveKindMap[".skip"] = DK_SKIP; |
| 4374 | DirectiveKindMap[".space"] = DK_SPACE; |
| 4375 | DirectiveKindMap[".file"] = DK_FILE; |
| 4376 | DirectiveKindMap[".line"] = DK_LINE; |
| 4377 | DirectiveKindMap[".loc"] = DK_LOC; |
| 4378 | DirectiveKindMap[".stabs"] = DK_STABS; |
| 4379 | DirectiveKindMap[".sleb128"] = DK_SLEB128; |
| 4380 | DirectiveKindMap[".uleb128"] = DK_ULEB128; |
| 4381 | DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS; |
| 4382 | DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC; |
| 4383 | DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC; |
| 4384 | DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA; |
| 4385 | DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET; |
| 4386 | DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET; |
| 4387 | DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER; |
| 4388 | DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET; |
| 4389 | DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET; |
| 4390 | DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY; |
| 4391 | DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA; |
| 4392 | DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE; |
| 4393 | DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE; |
| 4394 | DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE; |
| 4395 | DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE; |
| 4396 | DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE; |
| 4397 | DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME; |
| 4398 | DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED; |
| 4399 | DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER; |
Venkatraman Govindaraju | 3816d43 | 2013-09-26 14:49:40 +0000 | [diff] [blame] | 4400 | DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4401 | DirectiveKindMap[".macros_on"] = DK_MACROS_ON; |
| 4402 | DirectiveKindMap[".macros_off"] = DK_MACROS_OFF; |
| 4403 | DirectiveKindMap[".macro"] = DK_MACRO; |
Nico Weber | 155dccd1 | 2014-07-24 17:08:39 +0000 | [diff] [blame] | 4404 | DirectiveKindMap[".exitm"] = DK_EXITM; |
Eli Bendersky | 1723394 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 4405 | DirectiveKindMap[".endm"] = DK_ENDM; |
| 4406 | DirectiveKindMap[".endmacro"] = DK_ENDMACRO; |
| 4407 | DirectiveKindMap[".purgem"] = DK_PURGEM; |
Saleem Abdulrasool | b2ae2c0 | 2014-02-23 15:53:30 +0000 | [diff] [blame] | 4408 | DirectiveKindMap[".err"] = DK_ERR; |
Saleem Abdulrasool | 7ecc549 | 2014-02-23 23:02:23 +0000 | [diff] [blame] | 4409 | DirectiveKindMap[".error"] = DK_ERROR; |
Nico Weber | 404012b | 2014-07-24 16:26:06 +0000 | [diff] [blame] | 4410 | DirectiveKindMap[".warning"] = DK_WARNING; |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame] | 4411 | DirectiveKindMap[".reloc"] = DK_RELOC; |
Eli Bendersky | ec9e3cf | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 4412 | } |
| 4413 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4414 | MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) { |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4415 | AsmToken EndToken, StartToken = getTok(); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4416 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4417 | unsigned NestLevel = 0; |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4418 | for (;;) { |
| 4419 | // Check whether we have reached the end of the file. |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4420 | if (getLexer().is(AsmToken::Eof)) { |
| 4421 | Error(DirectiveLoc, "no matching '.endr' in definition"); |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 4422 | return nullptr; |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4423 | } |
| 4424 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4425 | if (Lexer.is(AsmToken::Identifier) && |
| 4426 | (getTok().getIdentifier() == ".rept")) { |
| 4427 | ++NestLevel; |
| 4428 | } |
| 4429 | |
| 4430 | // Otherwise, check whether we have reached the .endr. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4431 | if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") { |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4432 | if (NestLevel == 0) { |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4433 | EndToken = getTok(); |
| 4434 | Lex(); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4435 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
| 4436 | TokError("unexpected token in '.endr' directive"); |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 4437 | return nullptr; |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4438 | } |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4439 | break; |
| 4440 | } |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4441 | --NestLevel; |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4442 | } |
| 4443 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4444 | // Otherwise, scan till the end of the statement. |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4445 | eatToEndOfStatement(); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4446 | } |
| 4447 | |
| 4448 | const char *BodyStart = StartToken.getLoc().getPointer(); |
| 4449 | const char *BodyEnd = EndToken.getLoc().getPointer(); |
| 4450 | StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart); |
| 4451 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4452 | // We Are Anonymous. |
Benjamin Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 4453 | MacroLikeBodies.emplace_back(StringRef(), Body, MCAsmMacroParameters()); |
Benjamin Kramer | 1df3a1f | 2013-08-04 09:06:29 +0000 | [diff] [blame] | 4454 | return &MacroLikeBodies.back(); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4455 | } |
| 4456 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4457 | void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc, |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4458 | raw_svector_ostream &OS) { |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4459 | OS << ".endr\n"; |
| 4460 | |
Rafael Espindola | 3560ff2 | 2014-08-27 20:03:13 +0000 | [diff] [blame] | 4461 | std::unique_ptr<MemoryBuffer> Instantiation = |
| 4462 | MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>"); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4463 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4464 | // Create the macro instantiation object and add to the current macro |
| 4465 | // instantiation stack. |
Rafael Espindola | 9eef18c | 2014-08-27 19:49:03 +0000 | [diff] [blame] | 4466 | MacroInstantiation *MI = new MacroInstantiation( |
| 4467 | DirectiveLoc, CurBuffer, getTok().getLoc(), TheCondStack.size()); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4468 | ActiveMacros.push_back(MI); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4469 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4470 | // Jump to the macro instantiation and prime the lexer. |
David Blaikie | 1961f14 | 2014-08-21 20:44:56 +0000 | [diff] [blame] | 4471 | CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc()); |
Rafael Espindola | 8026bd0 | 2014-07-06 14:17:29 +0000 | [diff] [blame] | 4472 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer()); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4473 | Lex(); |
| 4474 | } |
| 4475 | |
Saleem Abdulrasool | d743d0a | 2013-12-28 05:54:33 +0000 | [diff] [blame] | 4476 | /// parseDirectiveRept |
| 4477 | /// ::= .rep | .rept count |
| 4478 | bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) { |
Saleem Abdulrasool | 51cff71 | 2013-12-28 06:39:29 +0000 | [diff] [blame] | 4479 | const MCExpr *CountExpr; |
| 4480 | SMLoc CountLoc = getTok().getLoc(); |
| 4481 | if (parseExpression(CountExpr)) |
| 4482 | return true; |
| 4483 | |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4484 | int64_t Count; |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 4485 | if (!CountExpr->evaluateAsAbsolute(Count)) { |
Saleem Abdulrasool | 51cff71 | 2013-12-28 06:39:29 +0000 | [diff] [blame] | 4486 | eatToEndOfStatement(); |
| 4487 | return Error(CountLoc, "unexpected token in '" + Dir + "' directive"); |
| 4488 | } |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4489 | |
| 4490 | if (Count < 0) |
Saleem Abdulrasool | 51cff71 | 2013-12-28 06:39:29 +0000 | [diff] [blame] | 4491 | return Error(CountLoc, "Count is negative"); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4492 | |
| 4493 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
Saleem Abdulrasool | d743d0a | 2013-12-28 05:54:33 +0000 | [diff] [blame] | 4494 | return TokError("unexpected token in '" + Dir + "' directive"); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4495 | |
| 4496 | // Eat the end of statement. |
| 4497 | Lex(); |
| 4498 | |
| 4499 | // Lex the rept definition. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4500 | MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc); |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4501 | if (!M) |
| 4502 | return true; |
| 4503 | |
| 4504 | // Macro instantiation is lexical, unfortunately. We construct a new buffer |
| 4505 | // to hold the macro body with substitutions. |
| 4506 | SmallString<256> Buf; |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4507 | raw_svector_ostream OS(Buf); |
| 4508 | while (Count--) { |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 4509 | // Note that the AtPseudoVariable is disabled for instantiations of .rep(t). |
| 4510 | if (expandMacro(OS, M->Body, None, None, false, getTok().getLoc())) |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4511 | return true; |
| 4512 | } |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4513 | instantiateMacroLikeBody(M, DirectiveLoc, OS); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4514 | |
| 4515 | return false; |
| 4516 | } |
| 4517 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4518 | /// parseDirectiveIrp |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 4519 | /// ::= .irp symbol,values |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4520 | bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) { |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 4521 | MCAsmMacroParameter Parameter; |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 4522 | |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 4523 | if (parseIdentifier(Parameter.Name)) |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 4524 | return TokError("expected identifier in '.irp' directive"); |
| 4525 | |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 4526 | if (Lexer.isNot(AsmToken::Comma)) |
| 4527 | return TokError("expected comma in '.irp' directive"); |
| 4528 | |
| 4529 | Lex(); |
| 4530 | |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 4531 | MCAsmMacroArguments A; |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 4532 | if (parseMacroArguments(nullptr, A)) |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 4533 | return true; |
| 4534 | |
| 4535 | // Eat the end of statement. |
| 4536 | Lex(); |
| 4537 | |
| 4538 | // Lex the irp definition. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4539 | MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc); |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 4540 | if (!M) |
| 4541 | return true; |
| 4542 | |
| 4543 | // Macro instantiation is lexical, unfortunately. We construct a new buffer |
| 4544 | // to hold the macro body with substitutions. |
| 4545 | SmallString<256> Buf; |
| 4546 | raw_svector_ostream OS(Buf); |
| 4547 | |
Craig Topper | 8400848 | 2015-10-10 05:38:14 +0000 | [diff] [blame] | 4548 | for (const MCAsmMacroArgument &Arg : A) { |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 4549 | // Note that the AtPseudoVariable is enabled for instantiations of .irp. |
| 4550 | // This is undocumented, but GAS seems to support it. |
Craig Topper | 8400848 | 2015-10-10 05:38:14 +0000 | [diff] [blame] | 4551 | if (expandMacro(OS, M->Body, Parameter, Arg, true, getTok().getLoc())) |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 4552 | return true; |
| 4553 | } |
| 4554 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4555 | instantiateMacroLikeBody(M, DirectiveLoc, OS); |
Rafael Espindola | 768b41c | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 4556 | |
| 4557 | return false; |
| 4558 | } |
| 4559 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4560 | /// parseDirectiveIrpc |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 4561 | /// ::= .irpc symbol,values |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4562 | bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) { |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 4563 | MCAsmMacroParameter Parameter; |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 4564 | |
Saleem Abdulrasool | a08585b | 2014-02-19 03:00:23 +0000 | [diff] [blame] | 4565 | if (parseIdentifier(Parameter.Name)) |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 4566 | return TokError("expected identifier in '.irpc' directive"); |
| 4567 | |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 4568 | if (Lexer.isNot(AsmToken::Comma)) |
| 4569 | return TokError("expected comma in '.irpc' directive"); |
| 4570 | |
| 4571 | Lex(); |
| 4572 | |
Eli Bendersky | 3827412 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 4573 | MCAsmMacroArguments A; |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 4574 | if (parseMacroArguments(nullptr, A)) |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 4575 | return true; |
| 4576 | |
| 4577 | if (A.size() != 1 || A.front().size() != 1) |
| 4578 | return TokError("unexpected token in '.irpc' directive"); |
| 4579 | |
| 4580 | // Eat the end of statement. |
| 4581 | Lex(); |
| 4582 | |
| 4583 | // Lex the irpc definition. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4584 | MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc); |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 4585 | if (!M) |
| 4586 | return true; |
| 4587 | |
| 4588 | // Macro instantiation is lexical, unfortunately. We construct a new buffer |
| 4589 | // to hold the macro body with substitutions. |
| 4590 | SmallString<256> Buf; |
| 4591 | raw_svector_ostream OS(Buf); |
| 4592 | |
| 4593 | StringRef Values = A.front().front().getString(); |
Benjamin Kramer | d31aaf1 | 2014-02-09 17:13:11 +0000 | [diff] [blame] | 4594 | for (std::size_t I = 0, End = Values.size(); I != End; ++I) { |
Eli Bendersky | a7b905e | 2013-01-14 19:00:26 +0000 | [diff] [blame] | 4595 | MCAsmMacroArgument Arg; |
Benjamin Kramer | f5e2fc4 | 2015-05-29 19:43:39 +0000 | [diff] [blame] | 4596 | Arg.emplace_back(AsmToken::Identifier, Values.slice(I, I + 1)); |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 4597 | |
Toma Tabacu | 217116e | 2015-04-27 10:50:29 +0000 | [diff] [blame] | 4598 | // Note that the AtPseudoVariable is enabled for instantiations of .irpc. |
| 4599 | // This is undocumented, but GAS seems to support it. |
| 4600 | if (expandMacro(OS, M->Body, Parameter, Arg, true, getTok().getLoc())) |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 4601 | return true; |
| 4602 | } |
| 4603 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4604 | instantiateMacroLikeBody(M, DirectiveLoc, OS); |
Rafael Espindola | f70bea9 | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 4605 | |
| 4606 | return false; |
| 4607 | } |
| 4608 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4609 | bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) { |
Rafael Espindola | 34b9c51 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 4610 | if (ActiveMacros.empty()) |
Preston Gurd | eb3ebf1 | 2012-09-19 20:23:43 +0000 | [diff] [blame] | 4611 | return TokError("unmatched '.endr' directive"); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4612 | |
| 4613 | // The only .repl that should get here are the ones created by |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4614 | // instantiateMacroLikeBody. |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4615 | assert(getLexer().is(AsmToken::EndOfStatement)); |
| 4616 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4617 | handleMacroExit(); |
Rafael Espindola | 47b7dac | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 4618 | return false; |
| 4619 | } |
Rafael Espindola | 12d73d1 | 2010-09-11 16:45:15 +0000 | [diff] [blame] | 4620 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4621 | bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info, |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4622 | size_t Len) { |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 4623 | const MCExpr *Value; |
| 4624 | SMLoc ExprLoc = getLexer().getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4625 | if (parseExpression(Value)) |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 4626 | return true; |
| 4627 | const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value); |
| 4628 | if (!MCE) |
| 4629 | return Error(ExprLoc, "unexpected expression in _emit"); |
| 4630 | uint64_t IntValue = MCE->getValue(); |
Craig Topper | 55b1f29 | 2015-10-10 20:17:07 +0000 | [diff] [blame] | 4631 | if (!isUInt<8>(IntValue) && !isInt<8>(IntValue)) |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 4632 | return Error(ExprLoc, "literal value out of range for directive"); |
| 4633 | |
Craig Topper | 7d5b231 | 2015-10-10 05:25:02 +0000 | [diff] [blame] | 4634 | Info.AsmRewrites->emplace_back(AOK_Emit, IDLoc, Len); |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 4635 | return false; |
| 4636 | } |
| 4637 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4638 | bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) { |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 4639 | const MCExpr *Value; |
| 4640 | SMLoc ExprLoc = getLexer().getLoc(); |
Jim Grosbach | d2037eb | 2013-02-20 22:21:35 +0000 | [diff] [blame] | 4641 | if (parseExpression(Value)) |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 4642 | return true; |
| 4643 | const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value); |
| 4644 | if (!MCE) |
| 4645 | return Error(ExprLoc, "unexpected expression in align"); |
| 4646 | uint64_t IntValue = MCE->getValue(); |
| 4647 | if (!isPowerOf2_64(IntValue)) |
| 4648 | return Error(ExprLoc, "literal value not a power of two greater then zero"); |
| 4649 | |
Craig Topper | 7d5b231 | 2015-10-10 05:25:02 +0000 | [diff] [blame] | 4650 | Info.AsmRewrites->emplace_back(AOK_Align, IDLoc, 5, Log2_64(IntValue)); |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 4651 | return false; |
| 4652 | } |
| 4653 | |
Chad Rosier | f43fcf5 | 2013-02-13 21:27:17 +0000 | [diff] [blame] | 4654 | // We are comparing pointers, but the pointers are relative to a single string. |
| 4655 | // Thus, this should always be deterministic. |
Benjamin Kramer | 8817cca | 2013-09-22 14:09:50 +0000 | [diff] [blame] | 4656 | static int rewritesSort(const AsmRewrite *AsmRewriteA, |
| 4657 | const AsmRewrite *AsmRewriteB) { |
Chad Rosier | eb5c168 | 2013-02-13 18:38:58 +0000 | [diff] [blame] | 4658 | if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer()) |
| 4659 | return -1; |
| 4660 | if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer()) |
| 4661 | return 1; |
Chad Rosier | 42d4e2e | 2013-02-15 22:54:16 +0000 | [diff] [blame] | 4662 | |
Chad Rosier | fce4fab | 2013-04-08 17:43:47 +0000 | [diff] [blame] | 4663 | // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output |
| 4664 | // rewrite to the same location. Make sure the SizeDirective rewrite is |
| 4665 | // performed first, then the Imm/ImmPrefix and finally the Input/Output. This |
| 4666 | // ensures the sort algorithm is stable. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4667 | if (AsmRewritePrecedence[AsmRewriteA->Kind] > |
| 4668 | AsmRewritePrecedence[AsmRewriteB->Kind]) |
Chad Rosier | 42d4e2e | 2013-02-15 22:54:16 +0000 | [diff] [blame] | 4669 | return -1; |
Chad Rosier | fce4fab | 2013-04-08 17:43:47 +0000 | [diff] [blame] | 4670 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4671 | if (AsmRewritePrecedence[AsmRewriteA->Kind] < |
| 4672 | AsmRewritePrecedence[AsmRewriteB->Kind]) |
Chad Rosier | 42d4e2e | 2013-02-15 22:54:16 +0000 | [diff] [blame] | 4673 | return 1; |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4674 | llvm_unreachable("Unstable rewrite sort."); |
Chad Rosier | b2144ce | 2013-02-13 01:03:13 +0000 | [diff] [blame] | 4675 | } |
| 4676 | |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4677 | bool AsmParser::parseMSInlineAsm( |
| 4678 | void *AsmLoc, std::string &AsmString, unsigned &NumOutputs, |
| 4679 | unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool> > &OpDecls, |
| 4680 | SmallVectorImpl<std::string> &Constraints, |
| 4681 | SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII, |
| 4682 | const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) { |
Chad Rosier | 37e755c | 2012-10-23 17:43:43 +0000 | [diff] [blame] | 4683 | SmallVector<void *, 4> InputDecls; |
| 4684 | SmallVector<void *, 4> OutputDecls; |
Chad Rosier | a4bc943 | 2013-01-10 22:10:27 +0000 | [diff] [blame] | 4685 | SmallVector<bool, 4> InputDeclsAddressOf; |
| 4686 | SmallVector<bool, 4> OutputDeclsAddressOf; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4687 | SmallVector<std::string, 4> InputConstraints; |
| 4688 | SmallVector<std::string, 4> OutputConstraints; |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4689 | SmallVector<unsigned, 4> ClobberRegs; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4690 | |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4691 | SmallVector<AsmRewrite, 4> AsmStrRewrites; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4692 | |
| 4693 | // Prime the lexer. |
| 4694 | Lex(); |
| 4695 | |
| 4696 | // While we have input, parse each statement. |
| 4697 | unsigned InputIdx = 0; |
| 4698 | unsigned OutputIdx = 0; |
| 4699 | while (getLexer().isNot(AsmToken::Eof)) { |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 4700 | ParseStatementInfo Info(&AsmStrRewrites); |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 4701 | if (parseStatement(Info, &SI)) |
Chad Rosier | f1f6a72 | 2012-10-19 22:57:33 +0000 | [diff] [blame] | 4702 | return true; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4703 | |
Chad Rosier | 149e8e0 | 2012-12-12 22:45:52 +0000 | [diff] [blame] | 4704 | if (Info.ParseError) |
| 4705 | return true; |
| 4706 | |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4707 | if (Info.Opcode == ~0U) |
| 4708 | continue; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4709 | |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4710 | const MCInstrDesc &Desc = MII->get(Info.Opcode); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4711 | |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4712 | // Build the list of clobbers, outputs and inputs. |
| 4713 | for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) { |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 4714 | MCParsedAsmOperand &Operand = *Info.ParsedOperands[i]; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4715 | |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4716 | // Immediate. |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 4717 | if (Operand.isImm()) |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4718 | continue; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4719 | |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4720 | // Register operand. |
Nico Weber | 42f79db | 2014-07-17 20:24:55 +0000 | [diff] [blame] | 4721 | if (Operand.isReg() && !Operand.needAddressOf() && |
| 4722 | !getTargetParser().OmitRegisterFromClobberLists(Operand.getReg())) { |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4723 | unsigned NumDefs = Desc.getNumDefs(); |
| 4724 | // Clobber. |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 4725 | if (NumDefs && Operand.getMCOperandNum() < NumDefs) |
| 4726 | ClobberRegs.push_back(Operand.getReg()); |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4727 | continue; |
| 4728 | } |
| 4729 | |
| 4730 | // Expr/Input or Output. |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 4731 | StringRef SymName = Operand.getSymName(); |
Chad Rosier | e81309b | 2013-04-09 17:53:49 +0000 | [diff] [blame] | 4732 | if (SymName.empty()) |
| 4733 | continue; |
| 4734 | |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 4735 | void *OpDecl = Operand.getOpDecl(); |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4736 | if (!OpDecl) |
| 4737 | continue; |
| 4738 | |
| 4739 | bool isOutput = (i == 1) && Desc.mayStore(); |
Chad Rosier | e81309b | 2013-04-09 17:53:49 +0000 | [diff] [blame] | 4740 | SMLoc Start = SMLoc::getFromPointer(SymName.data()); |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4741 | if (isOutput) { |
| 4742 | ++InputIdx; |
| 4743 | OutputDecls.push_back(OpDecl); |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 4744 | OutputDeclsAddressOf.push_back(Operand.needAddressOf()); |
Yaron Keren | 075759a | 2015-03-30 15:42:36 +0000 | [diff] [blame] | 4745 | OutputConstraints.push_back(("=" + Operand.getConstraint()).str()); |
Craig Topper | 7d5b231 | 2015-10-10 05:25:02 +0000 | [diff] [blame] | 4746 | AsmStrRewrites.emplace_back(AOK_Output, Start, SymName.size()); |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4747 | } else { |
| 4748 | InputDecls.push_back(OpDecl); |
David Blaikie | 960ea3f | 2014-06-08 16:18:35 +0000 | [diff] [blame] | 4749 | InputDeclsAddressOf.push_back(Operand.needAddressOf()); |
| 4750 | InputConstraints.push_back(Operand.getConstraint().str()); |
Craig Topper | 7d5b231 | 2015-10-10 05:25:02 +0000 | [diff] [blame] | 4751 | AsmStrRewrites.emplace_back(AOK_Input, Start, SymName.size()); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4752 | } |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4753 | } |
Reid Kleckner | ee08897 | 2013-12-10 18:27:32 +0000 | [diff] [blame] | 4754 | |
| 4755 | // Consider implicit defs to be clobbers. Think of cpuid and push. |
Craig Topper | e5e035a3 | 2015-12-05 07:13:35 +0000 | [diff] [blame] | 4756 | ArrayRef<MCPhysReg> ImpDefs(Desc.getImplicitDefs(), |
| 4757 | Desc.getNumImplicitDefs()); |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 4758 | ClobberRegs.insert(ClobberRegs.end(), ImpDefs.begin(), ImpDefs.end()); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4759 | } |
| 4760 | |
| 4761 | // Set the number of Outputs and Inputs. |
Chad Rosier | f641baa | 2012-10-18 19:39:30 +0000 | [diff] [blame] | 4762 | NumOutputs = OutputDecls.size(); |
| 4763 | NumInputs = InputDecls.size(); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4764 | |
| 4765 | // Set the unique clobbers. |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4766 | array_pod_sort(ClobberRegs.begin(), ClobberRegs.end()); |
| 4767 | ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()), |
| 4768 | ClobberRegs.end()); |
| 4769 | Clobbers.assign(ClobberRegs.size(), std::string()); |
| 4770 | for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) { |
| 4771 | raw_string_ostream OS(Clobbers[I]); |
| 4772 | IP->printRegName(OS, ClobberRegs[I]); |
| 4773 | } |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4774 | |
| 4775 | // Merge the various outputs and inputs. Output are expected first. |
| 4776 | if (NumOutputs || NumInputs) { |
| 4777 | unsigned NumExprs = NumOutputs + NumInputs; |
Chad Rosier | f641baa | 2012-10-18 19:39:30 +0000 | [diff] [blame] | 4778 | OpDecls.resize(NumExprs); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4779 | Constraints.resize(NumExprs); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4780 | for (unsigned i = 0; i < NumOutputs; ++i) { |
Chad Rosier | a4bc943 | 2013-01-10 22:10:27 +0000 | [diff] [blame] | 4781 | OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]); |
Chad Rosier | 7245033 | 2013-01-15 23:07:53 +0000 | [diff] [blame] | 4782 | Constraints[i] = OutputConstraints[i]; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4783 | } |
| 4784 | for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) { |
Chad Rosier | a4bc943 | 2013-01-10 22:10:27 +0000 | [diff] [blame] | 4785 | OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]); |
Chad Rosier | 7245033 | 2013-01-15 23:07:53 +0000 | [diff] [blame] | 4786 | Constraints[j] = InputConstraints[i]; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4787 | } |
| 4788 | } |
| 4789 | |
| 4790 | // Build the IR assembly string. |
Alp Toker | e69170a | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 4791 | std::string AsmStringIR; |
| 4792 | raw_string_ostream OS(AsmStringIR); |
Alp Toker | a55b95b | 2014-07-06 10:33:31 +0000 | [diff] [blame] | 4793 | StringRef ASMString = |
| 4794 | SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID())->getBuffer(); |
| 4795 | const char *AsmStart = ASMString.begin(); |
| 4796 | const char *AsmEnd = ASMString.end(); |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4797 | array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort); |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 4798 | for (const AsmRewrite &AR : AsmStrRewrites) { |
| 4799 | AsmRewriteKind Kind = AR.Kind; |
Chad Rosier | ff10ed1 | 2013-04-12 16:26:42 +0000 | [diff] [blame] | 4800 | if (Kind == AOK_Delete) |
| 4801 | continue; |
| 4802 | |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 4803 | const char *Loc = AR.Loc.getPointer(); |
Chad Rosier | 17d3799 | 2013-03-19 21:12:14 +0000 | [diff] [blame] | 4804 | assert(Loc >= AsmStart && "Expected Loc to be at or after Start!"); |
Chad Rosier | 0f48c55 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 4805 | |
Chad Rosier | 120eefd | 2013-03-19 17:32:17 +0000 | [diff] [blame] | 4806 | // Emit everything up to the immediate/expression. |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 4807 | if (unsigned Len = Loc - AsmStart) |
Chad Rosier | 17d3799 | 2013-03-19 21:12:14 +0000 | [diff] [blame] | 4808 | OS << StringRef(AsmStart, Len); |
Chad Rosier | 0f48c55 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 4809 | |
Chad Rosier | 37e755c | 2012-10-23 17:43:43 +0000 | [diff] [blame] | 4810 | // Skip the original expression. |
| 4811 | if (Kind == AOK_Skip) { |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 4812 | AsmStart = Loc + AR.Len; |
Chad Rosier | 37e755c | 2012-10-23 17:43:43 +0000 | [diff] [blame] | 4813 | continue; |
| 4814 | } |
| 4815 | |
Chad Rosier | ff10ed1 | 2013-04-12 16:26:42 +0000 | [diff] [blame] | 4816 | unsigned AdditionalSkip = 0; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4817 | // Rewrite expressions in $N notation. |
Chad Rosier | 0f48c55 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 4818 | switch (Kind) { |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4819 | default: |
| 4820 | break; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4821 | case AOK_Imm: |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 4822 | OS << "$$" << AR.Val; |
Chad Rosier | 11c42f2 | 2012-10-26 18:04:20 +0000 | [diff] [blame] | 4823 | break; |
| 4824 | case AOK_ImmPrefix: |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4825 | OS << "$$"; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4826 | break; |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 4827 | case AOK_Label: |
Matt Arsenault | 4e27343 | 2014-12-04 00:06:57 +0000 | [diff] [blame] | 4828 | OS << Ctx.getAsmInfo()->getPrivateLabelPrefix() << AR.Label; |
Ehsan Akhgari | db0e706 | 2014-09-22 02:21:35 +0000 | [diff] [blame] | 4829 | break; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4830 | case AOK_Input: |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4831 | OS << '$' << InputIdx++; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4832 | break; |
| 4833 | case AOK_Output: |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4834 | OS << '$' << OutputIdx++; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4835 | break; |
Chad Rosier | 0f48c55 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 4836 | case AOK_SizeDirective: |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 4837 | switch (AR.Val) { |
Chad Rosier | 0f48c55 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 4838 | default: break; |
| 4839 | case 8: OS << "byte ptr "; break; |
| 4840 | case 16: OS << "word ptr "; break; |
| 4841 | case 32: OS << "dword ptr "; break; |
| 4842 | case 64: OS << "qword ptr "; break; |
| 4843 | case 80: OS << "xword ptr "; break; |
| 4844 | case 128: OS << "xmmword ptr "; break; |
| 4845 | case 256: OS << "ymmword ptr "; break; |
| 4846 | } |
Eli Friedman | 0f4871d | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 4847 | break; |
| 4848 | case AOK_Emit: |
| 4849 | OS << ".byte"; |
| 4850 | break; |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 4851 | case AOK_Align: { |
Reid Kleckner | fb1c1c7 | 2015-10-27 17:32:48 +0000 | [diff] [blame] | 4852 | // MS alignment directives are measured in bytes. If the native assembler |
| 4853 | // measures alignment in bytes, we can pass it straight through. |
| 4854 | OS << ".align"; |
| 4855 | if (getContext().getAsmInfo()->getAlignmentIsInBytes()) |
| 4856 | break; |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 4857 | |
Reid Kleckner | fb1c1c7 | 2015-10-27 17:32:48 +0000 | [diff] [blame] | 4858 | // Alignment is in log2 form, so print that instead and skip the original |
| 4859 | // immediate. |
| 4860 | unsigned Val = AR.Val; |
| 4861 | OS << ' ' << Val; |
Benjamin Kramer | 1a13611 | 2013-02-15 20:37:21 +0000 | [diff] [blame] | 4862 | assert(Val < 10 && "Expected alignment less then 2^10."); |
Chad Rosier | c7f552c | 2013-02-12 21:33:51 +0000 | [diff] [blame] | 4863 | AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4; |
| 4864 | break; |
| 4865 | } |
Chad Rosier | f0e8720 | 2012-10-25 20:41:34 +0000 | [diff] [blame] | 4866 | case AOK_DotOperator: |
Reid Kleckner | 94a1c4d | 2014-03-06 19:19:12 +0000 | [diff] [blame] | 4867 | // Insert the dot if the user omitted it. |
Alp Toker | e69170a | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 4868 | OS.flush(); |
| 4869 | if (AsmStringIR.back() != '.') |
Reid Kleckner | 94a1c4d | 2014-03-06 19:19:12 +0000 | [diff] [blame] | 4870 | OS << '.'; |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 4871 | OS << AR.Val; |
Chad Rosier | f0e8720 | 2012-10-25 20:41:34 +0000 | [diff] [blame] | 4872 | break; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4873 | } |
Chad Rosier | 0f48c55 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 4874 | |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4875 | // Skip the original expression. |
David Majnemer | 8114c1a | 2014-06-23 02:17:16 +0000 | [diff] [blame] | 4876 | AsmStart = Loc + AR.Len + AdditionalSkip; |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4877 | } |
| 4878 | |
| 4879 | // Emit the remainder of the asm string. |
Chad Rosier | 17d3799 | 2013-03-19 21:12:14 +0000 | [diff] [blame] | 4880 | if (AsmStart != AsmEnd) |
| 4881 | OS << StringRef(AsmStart, AsmEnd - AsmStart); |
Chad Rosier | 8bce664 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4882 | |
| 4883 | AsmString = OS.str(); |
| 4884 | return false; |
| 4885 | } |
| 4886 | |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 4887 | namespace llvm { |
| 4888 | namespace MCParserUtils { |
| 4889 | |
| 4890 | /// Returns whether the given symbol is used anywhere in the given expression, |
| 4891 | /// or subexpressions. |
| 4892 | static bool isSymbolUsedInExpression(const MCSymbol *Sym, const MCExpr *Value) { |
| 4893 | switch (Value->getKind()) { |
| 4894 | case MCExpr::Binary: { |
| 4895 | const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value); |
| 4896 | return isSymbolUsedInExpression(Sym, BE->getLHS()) || |
| 4897 | isSymbolUsedInExpression(Sym, BE->getRHS()); |
| 4898 | } |
| 4899 | case MCExpr::Target: |
| 4900 | case MCExpr::Constant: |
| 4901 | return false; |
| 4902 | case MCExpr::SymbolRef: { |
| 4903 | const MCSymbol &S = |
| 4904 | static_cast<const MCSymbolRefExpr *>(Value)->getSymbol(); |
| 4905 | if (S.isVariable()) |
| 4906 | return isSymbolUsedInExpression(Sym, S.getVariableValue()); |
| 4907 | return &S == Sym; |
| 4908 | } |
| 4909 | case MCExpr::Unary: |
| 4910 | return isSymbolUsedInExpression( |
| 4911 | Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr()); |
| 4912 | } |
| 4913 | |
| 4914 | llvm_unreachable("Unknown expr kind!"); |
| 4915 | } |
| 4916 | |
| 4917 | bool parseAssignmentExpression(StringRef Name, bool allow_redef, |
| 4918 | MCAsmParser &Parser, MCSymbol *&Sym, |
| 4919 | const MCExpr *&Value) { |
| 4920 | MCAsmLexer &Lexer = Parser.getLexer(); |
| 4921 | |
| 4922 | // FIXME: Use better location, we should use proper tokens. |
| 4923 | SMLoc EqualLoc = Lexer.getLoc(); |
| 4924 | |
| 4925 | if (Parser.parseExpression(Value)) { |
| 4926 | Parser.TokError("missing expression"); |
| 4927 | Parser.eatToEndOfStatement(); |
| 4928 | return true; |
| 4929 | } |
| 4930 | |
| 4931 | // Note: we don't count b as used in "a = b". This is to allow |
| 4932 | // a = b |
| 4933 | // b = c |
| 4934 | |
| 4935 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
| 4936 | return Parser.TokError("unexpected token in assignment"); |
| 4937 | |
| 4938 | // Eat the end of statement marker. |
| 4939 | Parser.Lex(); |
| 4940 | |
| 4941 | // Validate that the LHS is allowed to be a variable (either it has not been |
| 4942 | // used as a symbol, or it is an absolute symbol). |
| 4943 | Sym = Parser.getContext().lookupSymbol(Name); |
| 4944 | if (Sym) { |
| 4945 | // Diagnose assignment to a label. |
| 4946 | // |
| 4947 | // FIXME: Diagnostics. Note the location of the definition as a label. |
| 4948 | // FIXME: Diagnose assignment to protected identifier (e.g., register name). |
| 4949 | if (isSymbolUsedInExpression(Sym, Value)) |
| 4950 | return Parser.Error(EqualLoc, "Recursive use of '" + Name + "'"); |
Vedant Kumar | 86dbd92 | 2015-08-31 17:44:53 +0000 | [diff] [blame] | 4951 | else if (Sym->isUndefined(/*SetUsed*/ false) && !Sym->isUsed() && |
| 4952 | !Sym->isVariable()) |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 4953 | ; // Allow redefinitions of undefined symbols only used in directives. |
| 4954 | else if (Sym->isVariable() && !Sym->isUsed() && allow_redef) |
| 4955 | ; // Allow redefinitions of variables that haven't yet been used. |
| 4956 | else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef)) |
| 4957 | return Parser.Error(EqualLoc, "redefinition of '" + Name + "'"); |
| 4958 | else if (!Sym->isVariable()) |
| 4959 | return Parser.Error(EqualLoc, "invalid assignment to '" + Name + "'"); |
| 4960 | else if (!isa<MCConstantExpr>(Sym->getVariableValue())) |
| 4961 | return Parser.Error(EqualLoc, |
| 4962 | "invalid reassignment of non-absolute variable '" + |
| 4963 | Name + "'"); |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 4964 | } else if (Name == ".") { |
Rafael Espindola | 7ae65d8 | 2015-11-04 23:59:18 +0000 | [diff] [blame] | 4965 | Parser.getStreamer().emitValueToOffset(Value, 0); |
Pete Cooper | 80d21cb | 2015-06-22 19:35:57 +0000 | [diff] [blame] | 4966 | return false; |
| 4967 | } else |
| 4968 | Sym = Parser.getContext().getOrCreateSymbol(Name); |
| 4969 | |
| 4970 | Sym->setRedefinable(allow_redef); |
| 4971 | |
| 4972 | return false; |
| 4973 | } |
| 4974 | |
| 4975 | } // namespace MCParserUtils |
| 4976 | } // namespace llvm |
| 4977 | |
Daniel Dunbar | 01e3607 | 2010-07-17 02:26:10 +0000 | [diff] [blame] | 4978 | /// \brief Create an MCAsmParser instance. |
Jim Grosbach | 4b90584 | 2013-09-20 23:08:21 +0000 | [diff] [blame] | 4979 | MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C, |
| 4980 | MCStreamer &Out, const MCAsmInfo &MAI) { |
Jim Grosbach | 345768c | 2011-08-16 18:33:49 +0000 | [diff] [blame] | 4981 | return new AsmParser(SM, C, Out, MAI); |
Daniel Dunbar | 01e3607 | 2010-07-17 02:26:10 +0000 | [diff] [blame] | 4982 | } |